Is there any way to override the -fvisibility=hidden at link time?

时光怂恿深爱的人放手 提交于 2019-12-02 18:05:00

问题


We are using an third party static library, let say A.a for android development. We link it as shared library and it works fine in the one App, but when use B.so to build another C.so, some symbols in A.a cannot find. We have already use -Wl,--export-dynamic and -Wl,--whole-archive to build B.so. We have using nm to check those symbols, it exist but list as “t” instead of “T”,which means it is local symbols instead of external. Seams the A.a are build with -fvisibility=hidden after som investigation.

But because some reason it is hard for us to got an new build library immediately, so we need some workaround. Is there any way to export those symbols as global even it has been build with -fvisibility=hidden in B.so at link time.


回答1:


We have using nm to check those symbols

You shouldn't: on ELF platforms, nm is inadequate for the job. Use readelf -Ws instead.

it exist but list as “t” instead of “T”,which means it is local symbols instead of external. Seams the A.a are build with -fvisibility=hidden after som investigation.

Your conclusion does not follow: there are many reasons a symbol may show up as a t. Being compiled with -fvisibility=hidden is only one of many possibilities.

Is there any way to export those symbols as global even it has been build with -fvisibility=hidden

The symbol table is just a linear table of Elf{32,64}_Sym[]s. You can find the start of this table in the object file with readelf -WS foo.o | grep '\.symtab', find the number of offending symbol from readelf -Ws, and find the offset of the symbol in the foo.o by combining the two:

sym-offset = .symtab offset + (sym-number * sizeof(Sym))

Once you have the offset, you can override its .st_info with STV_DEFAULT (if your theory is correct and you located the symbol correctly, you should find STV_HIDDEN there currently).

Once you've patched your foo.o, the symbol will no longer be hidden, and when you link foo.o into B.so, it will be global / exported.



来源:https://stackoverflow.com/questions/36273404/is-there-any-way-to-override-the-fvisibility-hidden-at-link-time

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