Go to main content

Oracle® Solaris 11.4 Linkers and Libraries Guide

Exit Print View

Updated: October 2019
 
 

Dynamic Mode

Dynamic mode is the default mode of operation for the link-editor. It can be enforced by specifying the –d y option, but is implied when not using the –d n option.

Under this mode, relocatable objects, shared objects and archive libraries are acceptable forms of input. Use of the –l option results in a directory search, where each directory is searched for a shared object. If no shared object is found, the same directory is then searched for an archive library. A search only for archive libraries can be enforced by using the –B static option. See Linking With a Mix of Shared Objects and Archives.

Creating a Shared Object

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

  • Use the link-editor –z guidance option. 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 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 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 –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 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

Creating a Dynamic Executable

  • To create a dynamic executable, don't use the –G or –d n options.

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

  • Indicate that the dependencies of the dynamic executable should be lazily loaded using the –z lazyload option. See Lazy Loading of Dynamic Dependencies.

  • 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 dependencies of the dynamic executable do not reside in the default search locations, record their path name in the output file using the –R option. See Directories Searched by the Runtime Linker.

  • Establish direct binding information using –B direct. See Direct Bindings.

The following example combines the preceding points.

$ cc -o prog -R /home/lib -z discard-unused=dependencies -z lazyload -B direct -L. \ 
    -lfoo file1.o file2.o file3.o ....