The hidden visibility does get applied in my shared libraries but works for static libraries on linux

ⅰ亾dé卋堺 提交于 2019-12-12 02:23:02

问题


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

来源:https://stackoverflow.com/questions/39348030/the-hidden-visibility-does-get-applied-in-my-shared-libraries-but-works-for-stat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!