84 Chapter 3
Linker Tasks
Using Linker commands
As mentioned previously in the section “Exporting Symbols from main
with -E”, the -E option forces the export of all symbols from the program,
regardless of whether they are referenced by shared libraries on the
linker command line. The +e option allows you to be more selective in
what symbols are exported. You can use +e to limit the exported symbols
to only those symbols you want to be visible.
For example, the following ld command exports the symbols main and
foo. The symbol main is referenced by libc. The symbol foo is
referenced at run time by an explicitly loaded library not specified at link
time:
$ ld /usr/ccs/lib/crt0.o prog.o +e main +e foo -lm -lc -ldld
When using +e, be sure to export any data symbols defined in the
program that may also be defined in explicitly loaded libraries. If a data
symbol that a shared library imports is not exported from the program
file, the program uses its own copy while the shared library uses a
different copy if a definition exists outside the program file. In such
cases, a shared library might update a global variable needed by the
program, but the program would never see the change because it would
be referencing its own copy.
One example of a data symbol that should almost always be exported
from a program is errno. errno is defined in every shared library and
program; if this definition is hidden, the value of errno will not be
shared outside of the program in which it is hidden.
Moving Libraries after Linking with +b
A library can be moved even after an application has been linked with it.
This is done by providing the executable with a list of directories to
search at run time for any required libraries. One way you can store a
directory path list in the program is by using the +b path_list linker
option.
Note that dynamic path list search works only for libraries specified with
-l on the linker command line (for example, -lfoo). It won't work for
libraries whose full path name is specified (for example,
/usr/contrib/lib/libfoo.sl). However, it can be enabled for such
libraries with the -l option to the chatr command (see “Changing a
Program's Attributes with chatr(1)” on page 104).