使用-fvisibility = hidden隐藏C ++符号 [英] Hiding C++ symbols with -fvisibility=hidden
问题描述
我有一个带有C API的C ++库,并且设置了-fvisibility=hidden
编译器标志,
然后我在C API方法上设置了__attribute__ ((visibility ("default")))
.
但是,我仍然看到可见的C ++符号.当我为我的图书馆创建一个Debian软件包时, 我得到以下符号文件
为什么这些符号仍然可见?
您应该通过c++filt
运行符号文件,该文件会将乱码"符号名称转换为[在c++
意义上来说]可读的名称.>
如果这样做,您会发现三分之二的符号是std::whatever
,而不是您的符号.因此,由于STL,它们被拉入.您可能无法控制它们.
如果有帮助,其他符号为grk _ *.
有些目标文件实用程序(例如readelf
,objdump
,objcopy
等)可以使您编辑/修补目标文件.
或者,您也许可以使用链接描述文件.
或者,您可以使用-S
进行编译以获得.s
文件.然后,您可以编写[perl/python]脚本来修改asm源并添加/更改需要更改可见性的任何属性.然后,只需执行:c++ -c modified.s
对于给定的符号(例如):
int __attribute__((visibility("hidden")))
main(void)
{
return 0;
}
asm文件为:
.file "main.c"
.text
.globl main
.hidden main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 8.3.1 20190223 (Red Hat 8.3.1-2)"
.section .note.GNU-stack,"",@progbits
注意asm指令:
.hidden main
即使没有这样的指令,也应该很容易编写一个脚本来添加一个脚本[在相应的.globl
之后
I have a C++ library with a C API, and I have set the -fvisibility=hidden
compiler flag,
and then I have set __attribute__ ((visibility ("default")))
on C API methods.
However, I still see visible C++ symbols. When I create a debian package for my library, I get the following symbols file
Why are these symbols still visible ?
You should run your symbols file through c++filt
which converts the "mangled" symbol names to what is readable [in the c++
sense].
If you do, you'll find that two thirds of the symbols are std::whatever
, and not your symbols. So, they are being pulled in because of the STL. You may not be able to control them.
The other symbols are grk_*, if that helps.
There are object file utilities (e.g. readelf
, objdump
, objcopy
, etc) that may allow you to edit/patch your object files.
Or, you might be able to use a linker script.
Or, you could compile with -S
to get a .s
file. You could then write a [perl/python] script to modify the asm source and add/change whatever attribute(s) you need to change the visibility. Then, just do: c++ -c modified.s
For a given symbol (e.g.):
int __attribute__((visibility("hidden")))
main(void)
{
return 0;
}
The asm file is:
.file "main.c"
.text
.globl main
.hidden main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (GNU) 8.3.1 20190223 (Red Hat 8.3.1-2)"
.section .note.GNU-stack,"",@progbits
Notice the asm directive:
.hidden main
Even without such a directive, it should be easy to write a script to add one [after the corresponding .globl
]
这篇关于使用-fvisibility = hidden隐藏C ++符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!