9

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?

alk
  • 69,737
  • 10
  • 105
  • 255
Bob5421
  • 7,757
  • 14
  • 81
  • 175

2 Answers2

7

Use the -s option to strip the symbol table:

gcc -s -o hello hello.c
Jens
  • 69,818
  • 15
  • 125
  • 179
  • Okay thanks. Is this symbole table called debug informations or is it something different? – Bob5421 Jun 15 '16 at 10:44
  • 1
    @Bob5421 The symbol table is needed for linking. Once linked, it can be discarded. This means it is different from debug information. While you can remove debug info from object files without ill effects, you should not remove the symbol table from object files (they won't link with other files if you do). – Jens Jun 15 '16 at 12:39
  • The "-s" is the linker option to strip out the symbols – Nathan B Aug 26 '18 at 15:02
5

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__
Picodev
  • 506
  • 4
  • 8