Hiding Symbols from Export with +hideallsymbols

Use the +hideallsymbols option to hide all symbols to prevent the linker from exporting them in a shared link. In the following example, main() exports func() and test(). If you use +hideallsymbols, the linker cannot export the following routines in the a.out.

$ ld main.o +hideallsymbols -L. -lfoo -lc

$ elfdump -t a.out

a.out:

 

 

 

 

 

 

 

...

 

 

 

 

 

 

 

.symtab

 

 

 

 

 

 

 

index

Type

Bind

Other

Sect

Value

Size

Name

1

FUNC

LOCL

0

0xb

0x4000000000001104

0

test

...

 

 

 

 

 

 

 

10

FUNC

LOCL

0

0xb

0x4000000000001200

0

func

Hiding Symbols with -h

The -hoption allows you to hide symbols. Hiding a symbol makes the symbol a local definition, accessible only from the object module or library in which it is defined. Use -hif you want to hide a few symbols

You can use -hoption when building a shared library (with -b) and when linking to create an a.out file. When combining .o files with -r, you can use the -hoption.

The syntax of the -hoption is: -hsymbol The -hoption hides symbol. Other global symbols remain exported unless hidden with -h.

Example Using -h

Suppose you want to build a shared library from an object file that contains the following symbol definitions as displayed by the nm command:

$ 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 can do the following:

$ ld -b -h check_sem_val sem.o

Tips on Using -h

You must not run -hand +e options on the same command line. For instance, suppose you specify +e sem. This exports the symbol sem and hides all other symbols. Additional -hoptions becomes unnecessary. If both -hand +e are used on the same symbol, the -hoverrides the +e option.

The linker command line gets lengthy and difficult to read if several such options are specified. In fact, you 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” (page 44) . You can specify any number of -hor +e options in this file.

Using Linker Commands 39

Page 39
Image 39
HP UX Software Transition Kit (STK) manual Hiding Symbols from Export with +hideallsymbols, Hiding Symbols with -h