Hiding C++ symbols with -fvisibility=hidden [duplicate]

你。 提交于 2020-05-17 06:49:05

问题


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 ?


回答1:


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]



来源:https://stackoverflow.com/questions/61648274/hiding-c-symbols-with-fvisibility-hidden

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