有没有办法在链接时覆盖 -fvisibility=hidden ? [英] Is there any way to override the -fvisibility=hidden at link time?
问题描述
我们正在使用第三方静态库,比如说 A.a
用于 android 开发.我们将其链接为共享库,它在一个 App 中运行良好,但是当使用 B.so
构建另一个 C.so
时,Aa<中的一些符号/code> 找不到.我们已经使用
-Wl,--export-dynamic
和 -Wl,--whole-archive
来构建 B.so
.我们已经使用nm
来检查那些符号,它存在但列为t"
而不是T"
,这意味着它是本地符号而不是外部.A.a
的接缝是在经过 som 调查后使用 -fvisibility=hidden
构建的.
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.
但是由于某些原因我们很难立即获得一个新的构建库,所以我们需要一些解决方法.有没有办法将这些符号导出为全局符号,即使它是在链接时使用 B.so
中的 -fvisibility=hidden
构建的.
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.
推荐答案
我们使用 nm 来检查这些符号
We have using nm to check those symbols
你不应该:在 ELF 平台上,nm
对于这项工作不足.请改用 readelf -Ws
.
You shouldn't: on ELF platforms, nm
is inadequate for the job. Use readelf -Ws
instead.
它存在但以t"而不是T"列出,这意味着它是本地符号而不是外部符号.在 som 调查后,A.a 使用 -fvisibility=hidden 构建接缝.
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.
您的结论不符合:有许多 原因,一个符号可能显示为 t
.使用 -fvisibility=hidden
编译只是众多可能性之一.
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.
有没有办法将这些符号导出为全局符号,即使它是使用 -fvisibility=hidden
Is there any way to export those symbols as global even it has been build with
-fvisibility=hidden
符号表只是 Elf{32,64}_Sym[]
的线性表.您可以使用 readelf -WS foo.o | 在目标文件中找到该表的开头.grep '.symtab'
,从readelf -Ws
中找到违规符号的个数,通过组合找到该符号在foo.o
中的偏移量二:
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))
一旦你有了偏移量,你可以用 STV_DEFAULT
覆盖它的 .st_info
(如果你的理论是正确的并且你正确定位了符号,你应该找到 STV_HIDDEN
目前在那里).
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).
修补 foo.o
后,符号将不再隐藏,当您将 foo.o
链接到 B.so
,它将是全局/导出的.
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.
这篇关于有没有办法在链接时覆盖 -fvisibility=hidden ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!