82 Chapter 3
Linker Tasks
Using Linker commands
$nm -p sem.o
0000000000 U $global$
1073741824 d $THIS_DATA$
1073741864 b $THIS_BSS$
0000000004 cS sem_val
0000000000 T check_sem_val
0000000036 T foo
0000000000 U printf
0000000088 T bar
0000000140 T sem
In this example, check_sem_val,foo,bar, and sem are all global
definitions. To create a shared library wherecheck_sem_val is a
hidden, local definition, you could do the following:
$ld -b -h check_sem_val sem.o
Tips on Using -hYou should not combine-h and +e options on the same command line.
For instance, suppose you specify+e sem. This would export the symbol
sem and hide all other symbols. Any additional -h options would be
unnecessary. If both-h and +e are used on the same symbol, the -h
overrides the +e option.
The linker command line could get quite lengthy and difficult to read if
several such options were specified. And in fact, you could exceed the
maximum HP-UX command line length if you specify too many options.
To get around this, useld linker option files, described under “Passing
Linker Options in a file with -c”. You can specify any number of-h or +e
options in this file.
Hiding and Exporting Symbols When Building aShared Library
When building a shared library, you might want to hide a symbol in the
library for several reasons:
• It canimprove performance because the dynamic loader does not have
to bind hidden symbols. Since most symbols need not be exported
from a shared library, hiding selected symbols can have a significant
impact on performance.
• It ensures that the definition can only be accessed by other routines
in the same library. When linking with other object modules or
libraries, the definition will be hidden from them.