Oracle® Solaris 11.2 Linkers and Libraries Guide

Exit Print View

Updated: July 2014
 
 

Creating a Shared Object

  • To create a shared object use the –G option. –d y is optional as it is implied by default.

  • The use of the link-editor –z guidance option is recommended. Guidance messages offer suggestions for link-editor options and other actions that can improve the resulting object.

  • Input relocatable objects should be built from position-independent code. For example, the C compiler generates position-independent code under the –K pic option. See Position-Independent Code. Use the –z text option to enforce this requirement.

  • Avoid including unused relocatable objects. Or, use the –z discard-unused=sections option, which instructs the link-editor to eliminate unreferenced ELF sections. See Removing Unused Material.

  • Application registers are a feature of the SPARC architecture which are reserved for use by the end user. SPARC shared objects intended for external use should use the –xregs=no%appl option to the C compiler in order to ensure that the shared object does not use any application registers. This makes the application registers available to any external users without compromising the shared object's implementation.

  • Establish the shared object's public interface by defining the global symbols that should be visible from the shared object, and reducing any other global symbols to local scope. This definition is provided by the –M option together with an associated mapfile. See Chapter 9, Interfaces and Versioning.

  • Use a versioned name for the shared object to allow for future upgrades. See Coordination of Versioned Filenames.

  • Self-contained shared objects offer maximum flexibility. They are produced when the object expresses all dependency needs. Use the –z defs to enforce this self containment. See Generating a Shared Object Output File.

  • Avoid unneeded dependencies. Use ldd with the –u option to detect and remove unneeded dependencies. See Shared Object Processing. Or, use the –z discard-unused=dependencies option, which instructs the link-editor to record dependencies only to objects that are referenced.

  • If the shared object being generated has dependencies on other shared objects, indicate they should be lazily loaded using the –z lazyload option. See Lazy Loading of Dynamic Dependencies.

  • If the shared object being generated has dependencies on other shared objects, and these dependencies do not reside in the default search locations, record their path name in the output file using the –R option. See Shared Objects With Dependencies.

  • If interposing symbols are not used on this object or its dependencies, establish direct binding information with –B direct. See Chapter 6, Direct Bindings.

The following example combines the above points.

$ cc -c -o foo.o -K pic -xregs=no%appl foo.c
$ cc -M mapfile -G -o libfoo.so.1 -z text -z defs -B direct -z lazyload \
    -z discard-unused=sections -R /home/lib foo.o -L. -lbar -lc
  • If the shared object being generated is used as input to another link-edit, record within it the shared object's runtime name using the –h option. See Recording a Shared Object Name.

  • Make the shared object available to the compilation environment by creating a file system link to a non-versioned shared object name. See Coordination of Versioned Filenames.

The following example combines the above points.

$ cc -M mapfile -G -o libfoo.so.1 -z text -z defs -B direct -z lazyload \
    -z discard-unused=sections -R /home/lib -h libfoo.so.1 foo.o -L. -lbar -lc
$ ln -s libfoo.so.1 libfoo.so