Solaris Transition Guide

Linkers

There are several changes to the link editor, ld(1), in this release. The most important change is its ability to handle the new ELF native file format.


Note -

The recommended method for building libraries and executables is through the compiler driver rather than by invoking the linker directly. The compiler automatically supplies several files needed by the linker.


You cannot mix libraries. 32-bit programs must link with 32-bit libraries. 64-bit programs must link with 64-bit libraries. ELF32 objects link with other ELF32 objects and ELF64 objects link with other ELF64 objects.

Link Editor Option Differences

Some options have been renamed in the new linker, some have remained the same, and others are no longer needed. Table 15-1 compares the SunOS release 4.x ld command to the Solaris 7 ld command.

The sections following Table 15-1 explain how certain linking tasks are affected by the option differences.

Table 15-1 Comparison of ld Options

SunOS release 4.x Option 

Solaris 7 Replacement 

Notes 

-align datum

-M mapfile

Uses mapfile and distinct sections

-assert definitions

Default  

 

-assert nodefinitions

-znodefs

Issues a fatal error instead of a warning  

-assert nosymbolic

-zdefs

Issues a fatal error instead of a warning  

-assert pure-text

-ztext

Issues a fatal error instead of a warning  

-A name

No replacement 

dlopen(3X) and dlclose(3X) can approximate this behavior

-Bdynamic

-Bdynamic

Applies only to the inclusion of shared libraries; use -dy (the default) to build dynamically linked executables. See "Building Executables".

-Bnosymbolic

-zdefs

 

-Bstatic

-dn & -Bstatic

The -dn option must be specified to completely eliminate the dynamic linker. Use -Bstatic in dynamic mode to include archive libraries. (Used as a toggle. See "Building Executables".)

-Bsymbolic

-Bsymbolic

Also gets -assert nosymbolic with this option

-d -dc -dp

Default 

Use -b option in SVR4 to turn off

-D hex

-M mapfile

mapfile contains different mechanisms to accomplish desired effect

-e entry

-e entry

 

no -e

-G

Creates a shared object  

-lx[.v]

-lx

Only major number versioning of shared libraries is currently supported 

-Ldir

-Ldir

dir not recorded in executable; use -R option instead.

-M

-m

 

-n

Default 

SVR4 executable format compresses disk image as -n

-N

No replacement  

 

-o name

-o name

 

-p

Default 

Can override with -M mapfile

-r

-r

 

-S

No replacement  

 

-s

-s

 

-t

No replacement  

 

-T hex

-M mapfile

mapfile contains different mechanisms to accomplish desired effect

-Tdata hex

-M mapfile

mapfile contains different mechanisms to accomplish desired effect

-u name

-u name

 

-x

No replacement  

 

-X

No replacement  

 

-y sym

No replacement  

 

-z

Default 

SVR4 executable format demands pages as -z

Building Shared Libraries

The procedure for building shared libraries in the Solaris 7 operating environment requires the -G option. In the SunOS release 4.x software, the linker would infer that a shared library was being built by the absense of the -e option. As shared libraries may have entry points, this option can no longer be used.

Building Executables

The -Bdynamic and -Bstatic options are still available, but their behavior is different. These options now refer to library inclusions to the executable rather than binding. Executable binding is set exclusively with the new -dy and -dn options in the Solaris 7 software. The -dy option is the default. It is required to create a dynamically linked executable. The -dn option is required to create a statically linked executable.

The -Bdynamic and -Bstatic options apply only when using the -dy option. -Bdynamic tells the link editor to include shared libraries, while -Bstatic tells it to include archive libraries. These options act as a toggle governing subsequent -l arguments until the next -Bdynamic or -Bstatic option is encountered.

The following examples show SunOS release 4.x and Solaris 7 commands that can be used to create similar executables.

Specifying Library Search Paths

In the SunOS release 4.x software, directories specified with the -L option were searched at link time and the information retained for use at execution time. This behavior is now divided between the -L and -R options. The -L option specifies the directories to search at link time; the -R option tells the linker the search paths to be retained for use at run time. See "Search Path Rules", in the next section for more information.

As with the -Bdynamic and -Bstatic options, the position of the -L option has significance; it applies only to the subsequent -l options.

Search Path Rules

The dynamic linker and the runtime linker determine their search paths through a different algorithm from that used by the SunOS release 4.x linker.

The examples below compare the search paths for the dynamic linker and the runtime linker for SunOS release 4.x and the Solaris 7 operating environment. Notice that in the latter, the search path for the link editor and the runtime linker are affected by the LD_LIBRARY_PATH setting. However, the runtime linker permits programs to find shared libraries without having to set LD_LIBRARY_PATH and makes the loading of shared libraries even more efficient. Consequently, with Solaris 7, Sun recommends that you use $ORIGIN instead, since you must build your program with a built-in library path relative to where prog is installed. For example .../package/bin/prog uses .../package/lib/libmine.so.1.

SunOS release 4.x linker search paths:

Solaris 7 linker search paths (with LD_LIBRARY_PATH=dirlist1):

Solaris 7 linker search paths (with LD_LIBRARY_PATH=dirlist1,dirlist2):

Solaris 7 linker search paths using $ORIGIN

Also, with Solaris 7, LD_LIBRARY_PATH_64 is a 64-bit only version of LD_LIBRARY_PATH.

Version Numbering

The SunOS release 4.x software supported both major and minor version numbers on shared libraries. The Solaris 7 operating environment supports only the major version number. For binary compatibility support, major and minor version numbers are recognized on SunOS release 4.x shared libraries. These libraries are required to retain the same major and minor version number they had in the SunOS release 4.x software.

Table 15-2 shows versions of SunOS release 4.x and Solaris 7 shared libraries.

Table 15-2 Example Shared Libraries

SunOS release 4.x 

Solaris 7 

libc.so.1.7

libc.so.1

libdl.so.1.0

libdl.so.1

In SunOS release 4.x system software, when the -l option was specified, the build environment linker searched for a library with both major and minor numbers. For example, if -ldl was specified, the library, libdl.so.1.0 was linked. In the Solaris 7 environment, even though major numbers are still supported, the default behavior of the link editor is to ignore version numbers. Using the previous example, the build environment link editor now searches for libdl.so and a symbolic link points to a specific version file.

The recording of a dependency in a dynamic executable or shared object is, by default, the file name of the associated shared object as it is referenced by the link editor. To provide a more consistent means of specifying dependencies, shared objects can record within themselves the file name by which they should be referenced at runtime. This is specified with the -h option when linking the library file.

Symbolic links have been created for most libraries in this release. You should build any new shared libraries with major numbers, then create a symbolic link to the version of the library that is used most often.

Examples

A new utility, dump(1), makes it easier to debug object files or to check the static and dynamic linking, see "Backing Up and Restoring Files"). The dump -L option displays the information needed by the runtime linker that is contained in the executable. This information is contained in the dynamic section of an ELF file. The RPATH entry displays search paths specified by the -R option to ld.

The following example:


examples% cc -G -o libx.so.1 -h libx.so.1 libx.o

examples% cp libx.so.1 /mylibs
examples% ln -s /mylibs/libx.so.1 /mylibs/libx.so
examples% dump -Lv libx.so.1

libx.so.1:

  **** DYNAMIC SECTION INFORMATION ****
.dynamic :
[INDEX] Tag      Value
[1]     INIT     0x3b8
[2]     FINI     0x3f4
[3]     SONAME   libx.so.1
[4]     HASH     0x94
[5]     STRTAB   0x33c
[6]     SYMTAB   0x14c
[7]     STRSZ    0x62
[8]     SYMENT   0x10
[9]     PLTGOT   0x10404
[10]    PLTSZ    0xc
[11]    PLTREL   0x7
[12]    JMPREL   0x3ac
[13]    RELA     0x3a0
[14]    RELASZ   0x18
[15]    RELAENT  0xc

If a library needs other dynamic libraries, they should be specified along with an RPATH, as the next example shows.

The next example compiles prog.c, dynamically linking libx.so (as built in the previous example), and specifies that the binary retain the current directory information for execution. This example shows the output of dump from the compiled program, prog.c. Here, the information stored in the SONAME field of the previous example is shown as NEEDED by prog. When prog is run, it will use libx.so.1 even if libx.so is linked to a different version.


examples% cc -o prog prog.c -L/mylibs -R/mylibs -lx
example% dump -Lv prog

prog:

  **** DYNAMIC SECTION INFORMATION ****
.dynamic :
[INDEX]   Tag   Value
[1]  NEEDED   libx.so.1
[2]  NEEDED   libc.so.1
[3]  INIT     0x1b1ac
[4]  FINI     0x1b248
[5]  RPATH    /mylibs
[6]  HASH     0x100e8
[7]  STRTAB   0x17f90
[8]  SYMTAB   0x12be0
[9]  STRSZ    0x31e1
[10] SYMENT   0x10
[11] DEBUG    0x0
[12] PLTGOT   0x2b25c
[13] PLTSZ    0x30
[14] PLTREL   0x7
[15] JMPREL   0x1b180
[16] RELA     0x1b174
[17] RELASZ   0x3c
[18] RELAENT  0xc