Creating a Shared Object
-
To create a shared object use the
-Goption.-d yis optional as it is implied by default.
-
Use the link-editor
-z guidanceoption. 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 picoption. See Position-Independent Code. Use the-z textoption to enforce this requirement. -
Avoid including unused relocatable objects. Or, use the
-z discard-unused=sectionsoption, 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%apploption 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
-Moption together with an associatedmapfile. See 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 defsto enforce this self containment. See Generating a Shared Object Output File. -
Avoid unneeded dependencies. Use
lddwith the-uoption to detect and remove unneeded dependencies. See Shared Object Processing. Or, use the-z discard-unused=dependenciesoption, 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 lazyloadoption. 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
-Roption. 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 Direct Bindings.
The following example combines the preceding 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
-hoption. 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 preceding 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
-
Consider the performance implications of the shared object:
-
Maximize shareability, as described in Maximizing Shareability
-
Minimize paging activity, as described in Minimizing Paging Activity
-
Reduce relocation overhead, especially by minimizing symbolic relocations, as described in Reducing Symbol Scope:
-
Allow access to data through functional interfaces, as described in Copy Relocations.
-