Hide function name in GCC compilation

喜欢而已 提交于 2019-12-23 10:49:06

问题


I am compiling a c "hello world" program that juste include one simple function and a main function.

I am using GCC under Linux.

When I run readelf command on the binary, I can see symbol table and I can see function names in clear.

  • Is there a way to tell GCC (or the linker) to not generate this symbol table?

  • Is it possible to tell GCC to store only functions addresses, without storing function names in clear?


回答1:


The utility strip discards symbols from object files.

Consider :

 #include <stdio.h>

 static void static_func(void)
 {
   puts(__FUNCTION__);
 }

 void func(void)
 {
   puts(__FUNCTION__);
 }

 int main(void)
 {
   static_func();
   func();

   return 0;
 }

readelf produces on a fresh compiled binary :

Symbol table '.symtab' contains 71 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
....
   37: 0000000000000000     0 FILE    LOCAL  DEFAULT  ABS hide.c
   38: 0000000000400526    17 FUNC    LOCAL  DEFAULT   14 static_func
....
   61: 0000000000400537    17 FUNC    GLOBAL DEFAULT   14 func
....
   66: 0000000000400548    21 FUNC    GLOBAL DEFAULT   14 main
....

And after stripping the binary the whole output is :

Symbol table '.dynsym' contains 4 entries:
   Num:    Value          Size Type    Bind   Vis      Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT  UND 
     1: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND puts@GLIBC_2.2.5 (2)
     2: 0000000000000000     0 FUNC    GLOBAL DEFAULT  UND __libc_start_main@GLIBC_2.2.5 (2)
     3: 0000000000000000     0 NOTYPE  WEAK   DEFAULT  UND __gmon_start__



回答2:


Use the -s option to strip the symbol table:

gcc -s -o hello hello.c


来源:https://stackoverflow.com/questions/37829699/hide-function-name-in-gcc-compilation

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