Oracle® Solaris 11.2 Linkers and Libraries Guide

Exit Print View

Updated: July 2014
 
 

Specifying the Link-Editor Options

Typically, link-edits are completely specified using command line options. However, 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.

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 -ztext -G null.o
ld: warning: option '-ztextoff' and option '-ztext' 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 -zstrip-class=symbol -zstrip-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 -zstrip-class=symbol \
    -zstrip-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 -zstrip-class=symbol \
    -zstrip-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

The output object type is determined from the LD_OPTIONS, command line, and LD_UNSET components. This object type is then used to investigate any LD_{object-type}_UNSET, and LD_{object-type}_OPTIONS environment variables to remove, or add, options specific to the object type being built. The object-type provides the types, in uppercase, defined 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. 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.

For example, a build environment, that creates both dynamic executables and shared objects, can use the LD_EXEC_OPTIONS environment variable to enable address space layout randomization for all dynamic executables.

$ LD_EXEC_OPTIONS=-zaslr  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 Chapter 5, Link-Editor Quick Reference for the most commonly used link-editor options, and ld (1) for a complete description of all link-editor options.