有什么办法可以在 link 时间覆盖 -fvisibility=hidden 吗?

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

我们正在使用第三方静态库,假设 A.a 用于 android 开发。我们link它作为共享库,它在一个应用程序中工作正常,但是当使用B.so构建另一个C.so时,A.a中的一些符号找不到。我们已经使用 -Wl,--export-dynamic-Wl,--whole-archive 构建了 B.so。我们使用 nm 来检查这些符号,它存在但列为 “t” 而不是 “T”,这意味着它是本地符号而不是外部符号。在 som 调查之后,A.a 的接缝是用 -fvisibility=hidden 构建的。

但由于某些原因我们很难立即获得新的构建库,因此我们需要一些解决方法。有什么方法可以将这些符号导出为全局符号,即使它是在 link 时间 B.so 中使用 -fvisibility=hidden 构建的。

We have using nm to check those symbols

你不应该:在 ELF 平台上,nm 不适合 工作。请改用 readelf -Ws

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 编译只是众多可能性中的一种。

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

符号table只是Elf{32,64}_Sym[]s的线性table。您可以在 readelf -WS foo.o | grep '\.symtab' 的目标文件中找到此 table 的开头,从 readelf -Ws 中找到违规符号的数量,并在 foo.o 中找到符号的偏移量通过结合两者:

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

一旦你有了偏移量,你就可以用 STV_DEFAULT 覆盖它的 .st_info(如果你的理论是正确的并且你正确地找到了符号,你现在应该在那里找到 STV_HIDDEN) .

一旦你修补了你的 foo.o,符号将不再被隐藏,当你 link foo.oB.so 时,它将是全局的 /导出。