Go to main content

Oracle® Solaris 11.3 Linkers and Libraries Guide

Exit Print View

Updated: March 2018
 
 

Specifying the Link-Editor Options

Typically, link-edits are completely specified using command line options. In addition, a variety of environment variables are provided to augment command line processing. These variables provide for supplying options that might clash with compiler options. These variables also provide for overriding, or unsetting, the command line options that are embedded in scripts and build environments.

Any inconsistencies between command line options result in a fatal error condition. Any inconsistencies that involve an option provided by an environment variable result in a warning, and the first option taking precedence. Any UNSET operation is accompanied with a warning notification.

Initial options are interpreted from the environment and the command line in the following order.

  • From the LD_OPTIONS environment variable.

  • From the command line.

  • From the LD_UNSET environment variable.

LD_OPTIONS can be used to pass arguments to the link-editor that would otherwise be interpreted by the compiler drivers. For example, diagnostics related to the link-edit can be obtained using the –D option. This option is normally interpreted by the compiler preprocessor.

$ LD_OPTIONS=-Dargs  cc -o main main.c
...
debug: arg[0]  option=-D:  option-argument: args  (LD_OPTIONS)
debug:
debug: arg[0]  /usr/ccs/bin/ld
debug: arg[2]  option=-o:  option-argument: main
debug: arg[3]  option=-Q:  option-argument: y
debug: arg[4]  option=-l:  option-argument: c

LD_OPTIONS can also be used to override options that have a family of variants. For example, an embedded –z text option can be overridden by a –z textoff option.

$ LD_OPTIONS=-ztextoff  cc -z text -G null.o
ld: warning: option '-ztextoff' and option '-z text' are incompatible, \
    first option taken

Some options have no alternative variants, and therefore can not be overridden. However, they can be unset. For example, a standard link-edit can create the following sections.

$ cc -o main main.c
$ elfdump -c main | egrep "symtab|debug"
Section Header[19]:  sh_name: .symtab
Section Header[22]:  sh_name: .debug_info
Section Header[23]:  sh_name: .debug_line

These sections can be removed with the –z strip-class option.

$ cc -o main -z strip-class=symbol -z strip-class=debug main.c
$ elfdump -c main | egrep "symtab|debug"
$

Individual strip options can be unset. The follow example unsets the stripping of debug sections.

$ LD_UNSET=-zstrip-class=debug  cc -o main -z strip-class=symbol \
    -z strip-class=debug main.c
ld: warning: unsetting option '-zstrip-class=debug': LD_UNSET directed
$ elfdump -c main | egrep "symtab|debug"
Section Header[20]:  sh_name: .debug_info
Section Header[21]:  sh_name: .debug_line

In addition, options that provide for multiple instances, such as –z strip-class can have all family members unset by specifying the option without any qualifying option string. The following example unsets the stripping of debug and symbol table sections.

$ LD_UNSET=-zstrip-class  cc -o main -z strip-class=symbol \
    -z strip-class=debug main.c
ld: warning: unsetting option '-zstrip-class': LD_UNSET directed
$ elfdump -c main | egrep "symtab|debug"
Section Header[19]:  sh_name: .symtab
Section Header[22]:  sh_name: .debug_info
Section Header[23]:  sh_name: .debug_line

From the three components, LD_OPTIONS, the command line, and LD_UNSET, the initial object type of the output file being created, is determined. This object type is then used to investigate any LD_{object-type}_UNSET, and LD_{object-type}_OPTIONS environment variables. These variables can remove, or add, options specific to the object type being built.

The object-type corresponds to the values, in uppercase, accepted by the –z type option, and is one of EXEC, PIE, RELOC or SHARED. For example, the LD_EXEC_OPTIONS option is interpreted when the output file type is a dynamic executable.

If an LD_{object-type}_OPTIONS exists, the variable is first searched to discover whether a –z type option is specified. This search provides a final chance to affect the object type of the output file being created. The following example redefines a dynamic executable to be a position-independent executable.

$ LD_EXEC_OPTIONS=-ztype=pie  cc -o main main.c

The object type is now finalized, and the associated environment variables for this final object type are processed. Note that these variables can not change the object type or class.

These environment variables are processed in the following order.

  • From the LD_{object-type}_UNSET environment variable.

  • From the LD_{object-type}_OPTIONS environment variable.

The following example executes a build process where all objects created by the link-editor have guidance enabled. Any dynamic executables become position-independent executables, and have a number of security extensions enabled. Any shared objects are ensured to contain position-independent code and have all their dependencies defined.

$ LD_OPTIONS=-zguidance  \
    LD_EXEC_OPTIONS=-ztype=pie  \
    LD_PIE_OPTIONS=-zaslr,nxheap,nxstack  \
    LD_SHARED_OPTIONS='-ztext -zdefs'  build.sh

Any command line options that are inconsistent with this output object type result in a fatal error condition. Any inconsistent option provided by an environment variable results in a warning, and the option being ignored.

See Link-Editor Quick Reference for the most commonly used link-editor options, and ld(1) for a complete description of all link-editor options.