Restricting symbols in a Linux static library

后端 未结 5 748
逝去的感伤
逝去的感伤 2020-12-02 11:40

I\'m looking for ways to restrict the number of C symbols exported to a Linux static library (archive). I\'d like to limit these to only those symbols that are part of the

5条回答
  •  时光说笑
    2020-12-02 12:08

    I don't believe GNU ld has any such options; Ulrich must have meant objcopy, which has many such options: --localize-hidden, --localize-symbol=symbolname, --localize-symbols=filename.

    The --localize-hidden in particular allows one to have a very fine control over which symbols are exposed. Consider:

    int foo() { return 42; }
    int __attribute__((visibility("hidden"))) bar() { return 24; }
    
    gcc -c foo.c
    nm foo.o
    000000000000000b T bar
    0000000000000000 T foo
    
    objcopy --localize-hidden foo.o bar.o
    nm bar.o
    000000000000000b t bar
    0000000000000000 T foo
    

    So bar() is no longer exported from the object (even though it is still present and usable for debugging). You could also remove bar() all together with objcopy --strip-unneeded.

提交回复
热议问题