动态库的符号隐藏

overview

•       gcc -fPIC -c a.c

•       gcc -fPIC -c b.c

•       gcc -fPIC -c c.c

•       gcc -fPIC -c d.c

•       gcc -shared -o libfoo2.so a.o b.o  -Wl,--version-script=libfoo.map

•       gcc -shared -o libfoob.so c.o d.o  -Wl,--version-script=libfoob.map

•       gcc -o hello_hide main.c -L. -lfoo2 –lfoob

•       export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH

•       ./hello_hide

Source code

•       a.c void foo(){bar();}

•       b.c void bar(){printf("ok\n");}

•       c.c void bar2(){printf("ok222\n");}

•       d.c void foo2(){bar2();}

  map file

•       Libfoob.map {global: foo2;local: *;};

•       Libfoo.map    {global: foo;local: *;};

 

Old build & nm compare

•       gcc -shared -o libfoo.so a.o b.o 

•       gcc -shared -o libfoob.so c.o d.o

•       gcc -o hello main.c -L. -lfoo -lfoob

•       nm -D libfoo.so|grep -v '_‘

•       nm -D libfoob.so|grep -v '_‘

•       nm -D libfoo2.so|grep -v '_‘

•       nm -D libfoob.so|grep -v '_'

 

Libindex /command

•       objdump  –T

libopt

Libopt hello_hide

Objdump –T elf

result

•       Libfoo.so   7895bytes ,  7887 bytes

•       Libfoob.so 7897bytes , 7,889 bytes

•       Hello  &hello_hide   8407 並沒有變化

 

•        通过版本控制脚本--version-script=  libc.map 传送到gcc并用-Wl 链接,便会隐藏不需要对外公开的函数符号.-Wl 是用來隔開兩參數反傳送到連接器上的選項.

–   庫的利用者不能看到非公開的API

–   共享庫的符號表格變小了

–   降低了動態鏈接的開銷

 

c++ mapfile

{

    global:

       extern “C++”{

           some_class::some_func*

  };

  local:*;

}


other tool:
mklibs.py: An automated way to create a minimal /lib/ directory.

你可能感兴趣的:(动态库的符号隐藏)