[Updated: 04/07/2006] Much accurate information is available in a better format at:
Reducing Symbol Scope with Sun Studio C/C++__________________
The default left-to-right dependency ordering can be overridden by using the link-editor (ld) flag:
-Bsymbolic-Bsymbolic works in dynamic mode only i.e., while creating dynamic objects like shared objects. This flag informs the link-editor to bind all references to global symbols to their definitions, if available within the object.
This is the flag to be used in our example to get the correct result, as we want to bind the call to addtentomyage() to the definition available within hidden.c
Let's create libhidden again with -Bsymbolic flag
bpte4500s001:/sunbuild1/giri/testcases/symbol-hiding/%CC -G -Bsymbolic -o libhidden.so hidden.o
Now let's build & run the driver program again:
bpte4500s001:/sunbuild1/giri/testcases/symbol-hiding/%CC -o driver -lgeneric -lhidden driver.c
bpte4500s001:/sunbuild1/giri/testcases/symbol-hiding/%./driver
lname = mandalika
age = 35
first char = c
Cool; it works!! - got the desired result
Let's examine the symbol table for the scope
bpte4500s001:/sunbuild1/giri/testcases/symbol-hiding/%elfdump -C -s libhidden.so
Symbol Table: .dynsym
index value size type bind oth ver shndx name
[3] 0x00010878 0x00000004 OBJT GLOB P 0 .data age
[12] 0x00000598 0x00000020 FUNC GLOB P 0 .text char firstchar(char*)
[15] 0x00000558 0x0000002c FUNC GLOB P 0 .text char*lastname(char*)
[19] 0x00000518 0x0000002c FUNC GLOB P 0 .text int addtentomyage(int)
[20] 0x000005c8 0x00000038 FUNC GLOB P 0 .text int agefunc()
All the symbols were still global (GLOB), but now marked as "Protected" (P)
Advantage:
No worries about name collisions
Disadvantage:
-Bsymbolic was intended for specialized dynamic objects and is not recommended for general use. That means, there is no support for applications that were built with -Bsymbolic. But still we need the functionality to avoid name collisions. Is there any alternate way to achieve the functionality of -Bsymbolic?
(to be continued ..)