80 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 where check_sem_val is a
hidden, local definition, you could use either of the following commands:
$ ld -b -h check_sem_val sem.o One -h option.
$ ld -b +e foo +e bar +e sem sem.o Three +e options.
In contrast, suppose you want to export only the check_sem_val
symbol. Either of the following commands would work:
$ ld -b -h foo -h bar -h sem sem.o Three -h options.
$ ld -b +e check_sem_val sem.o One +e option.
When to use -h versus +e
How do you decide whether to use -h or +e? In general, use -h if you
simply want to hide a few symbols. And use +e if you want to export a
few symbols and hide a large number of symbols.
You 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, use ld 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.
You can use -h or +e options when building a shared library (with -b)
and when linking to create an a.out file. When combining .o files with
-r, you can still use only the -h option.