0

I went through this post Hiding symbol names in library but still could not get proper conclusion

1) Filename : lib_mylib.h

int fun();
int fun1(void);
int fun2(void);

2) Filename: lib_mylib.c */

#include <stdio.h>
int fun()  {
    return 2;
}
int fun1(void) {
    return 2;
}
int fun2(void) {  
    return 2;
}

3) File :my_lib_shared.c

#include "my_lib.h"
int foo(void) {
    return fun() + 4;
}

4) File: my_lib_shared.h

int foo();

5) File driver.c

/* filename: driver.c  */
#include "my_lib.h"
#include "my_lib_shared.h"
#include <stdio.h>
int main() {
    int x =   foo() ;
    return 0;
}

If I execute following steps

1)gcc -fPIC -fvisibility=hidden -c my_lib.c -o lib_mylib.o
2)gcc -fPIC -fvisibility=hidden -shared -o libMyLibHidden.so lib_mylib.o
3)gcc -fPIC -c my_lib_shared.c -o my_lib_shared.o
4)gcc -shared -o libMyShared2.so my_lib_shared.o
5)gcc -L. -Wall -o test_hidden  driver.c -lMyLibHidden -lMyShared2

The output of objdump is as

objdump -T libMyLibHidden.so

libMyLibHidden.so:     file format elf64-x86-64

DYNAMIC SYMBOL TABLE:
0000000000000448 l    d  .init  0000000000000000       .init
0000000000000000  w   D  *UND*  0000000000000000        __gmon_start__
0000000000000000  w   D  *UND*  0000000000000000       _Jv_RegisterClasses
0000000000000658 g    DF .fini  0000000000000000  Base  _fini
0000000000000000  w   D  *UND*  0000000000000000     _deregisterTMCloneTable
0000000000000000  w   D  *UND*  0000000000000000  _ITM_registerTMCloneTable
0000000000000000  w   DF *UND*  0000000000000126   GLIBC_2.2.5 _cxa_finalize
0000000000200938 g    D  *ABS*  0000000000000000  Base        __bss_start
0000000000200948 g    D  *ABS*  0000000000000000  Base        _end
0000000000200938 g    D  *ABS*  0000000000000000  Base        _edata
0000000000000448 g    DF .init  0000000000000000  Base        _init

I did not any reference of functions of fun, fun1 and fun2 in the above objdump

and I get following error at final linking step

/tmp/ccMsEpFq.o: In function main': driver.c:(.text+0xe): undefined reference tofun' collect2: error: ld returned 1 exit status

When I make static library from lib_mylib.c

1)gcc -g -fvisibility=hidden -c my_lib.c -o lib_mylib.o
2)ar rcs libMylib_static_hidden.a lib_mylib.o
3)gcc -g -fvisibility=hidden -c -fPIC my_lib_shared.c -o my_lib_shared.o
4)gcc -g -shared -o libMyShare.so my_lib_shared.o
5)gcc -g driver.c -L . -Wall -o  test_static_hidden -lMyShare -lMylib_static_hidden

and I get following objdump

objdump -t libMylib_static_hidden.a

In archive libMylib_static_hidden.a:
lib_mylib.o:     file format elf64-x86-64

SYMBOL TABLE:
0000000000000000 l    df *ABS*  0000000000000000 my_lib.c
0000000000000000 l    d  .text  0000000000000000 .text
0000000000000000 l    d  .data  0000000000000000 .data
0000000000000000 l    d  .bss   0000000000000000 .bss
0000000000000000 l    d  .debug_abbrev  0000000000000000 .debug_abbrev
0000000000000000 l    d  .debug_info    0000000000000000 .debug_info
0000000000000000 l    d  .debug_line    0000000000000000 .debug_line
0000000000000000 l    d  .debug_frame   0000000000000000 .debug_frame
0000000000000000 l    d  .eh_frame      0000000000000000 .eh_frame
0000000000000000 l    d  .debug_pubnames        0000000000000000 .debug_pubnames
0000000000000000 l    d  .debug_aranges 0000000000000000 .debug_aranges
0000000000000000 l    d  .debug_str     0000000000000000 .debug_str
0000000000000000 l    d  .note.GNU-stack    0000000000000000 .note.GNU-stack
0000000000000000 l    d  .comment       0000000000000000 .comment
0000000000000000 g     F .text  000000000000000b .hidden fun
000000000000000b g     F .text  000000000000000b .hidden fun1
0000000000000016 g     F .text  000000000000000b .hidden fun2

When I final link the library

5)gcc -g driver.c -L . -Wall -o  test_static_hidden -lMylib_static_hidden -lMyShare

I get following linker error

test_static_hidden: hidden symbol `fun' in libMylib_static_hidden.a(lib_mylib.o) is referenced by DSO /tools/linux64/gcc-4.0.2/bin/ld: final link failed: Nonrepresentable section on output I need guidance on why hidden visibility does not get applied when I make shared libraries and why I get undefined reference in case of linking shared library libMyLibHidden.so

I

Community
  • 1
  • 1
TechEnthusiast
  • 273
  • 4
  • 18
  • What result did you expect? – n. m. could be an AI Sep 06 '16 at 11:54
  • When I do a objdump -T libMyLibHidden.so I should references of fun, fun1 and fun2 with .hidden flag and the I should get the same error message Nonrepresentable section on output hidden symbol `fun is referenced by DSO on output when I execute gcc -L. -Wall -o test_hidden driver.c -lMyShared2 -lMyLibHidden – TechEnthusiast Sep 06 '16 at 11:55
  • (1) `objdump -T` only shows dynamic symbols. Dynamic symbols are not created for hidden symbols. (2) You are not getting an error on undefined symbols because shared objects are allowed to have them by default, and the notion is not applicable to static libraries. – n. m. could be an AI Sep 06 '16 at 12:30
  • your second (2) You are not getting an error on undefined symbols because shared objects are allowed to have them by default, and the notion is not applicable to static libraries. is not clear > Could you please elaborate . – TechEnthusiast Sep 06 '16 at 13:13
  • What exactly is unclear? (2a) When creating a shared library, the linker may not be able to resolve some symbols. This is not an error. Such symbols are left undefined. The idea is to resolve them later, e.g. when the library is actually loaded into an executable. (2b) Creation of static libraries does not involve linking. There's no linker and no symbol resolution process that may fail. – n. m. could be an AI Sep 06 '16 at 13:38
  • One more question . if we want to solve this issue fun' in libMylib_static_hidden.a(lib_mylib.o) is referenced by DSO /tools/linux64/gcc-4.0.2/bin/ld: final link failed: Nonrepresentable section on output in case of hidden symbols is static library what is solution – TechEnthusiast Sep 06 '16 at 15:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122766/discussion-between-techenthusiast-and-n-m). – TechEnthusiast Sep 06 '16 at 16:05

0 Answers0