Bob5421
Bob5421

Reputation: 9073

Hide function name in GCC compilation

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.

Upvotes: 9

Views: 5613

Answers (2)

Jens
Jens

Reputation: 72639

Use the -s option to strip the symbol table:

gcc -s -o hello hello.c

Upvotes: 7

Picodev
Picodev

Reputation: 556

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__

Upvotes: 5

Related Questions