Search code examples
cfunctionvariablesgcccompiler-construction

Why compile file have function name?


I have a c program:

#include<stdio.h> 
long fibo(int n); 
int main(void) 
{ 
        int input = 0; 
 
        printf("Enter your number: "); 
        scanf("%i", &input); 
 
        /*check buffer*/ 
        if (getchar() != '\n' || input <= 0) 
        { 
                printf("Try again\n"); 
                return 0; 
        } 
 
        printf("result: %li\n", fibo(input)); 
 
        return 1; 
} 
 
long fibo(int n) 
{ 
        if (n > 2) 
        { 
                return fibo(n - 1) + fibo(n - 2); 
        } 
        else 
        { 
                return 1; 
        } 
}

I compiled it with gcc.

000035e0: 735f 7374 6172 7400 6d61 696e 0066 6962  s_start.**main.fib**
000035f0: 6f00 5f5f 6973 6f63 3939 5f73 6361 6e66  **o**.__isoc99_scanf

When I review compile file, I see functions name(main and fibo) but I couldn't find variables name.

My question is why compiler keep function name? And what is usage? If for developersreview binery file and deassembling why didn't save variables name?

I am new

Objdump


Solution

  • If you run strings over either the object file or the binary it has a string fibo. You didn't say what platform you are using but this is on Linux. Linux these days use the ELF format (pie for executable, and lsb relocable for the object file. The symbols, like function names, are stored in the symbol table. A better way to inspect it is using nm (or objdump):

    $ nm fibo.o
    0000000000000000 T fibo
                     U getchar
                     U _GLOBAL_OFFSET_TABLE_
                     U __isoc99_scanf
    000000000000003f T main
                     U printf
                     U puts
    

    and the executable:

    $ nm fibo
    0000000000004048 B __bss_start
    0000000000004048 b completed.0
                     w __cxa_finalize@GLIBC_2.2.5
    0000000000004038 D __data_start
    0000000000004038 W data_start
    00000000000010b0 t deregister_tm_clones
    0000000000001120 t __do_global_dtors_aux
    0000000000003df0 d __do_global_dtors_aux_fini_array_entry
    0000000000004040 D __dso_handle
    0000000000003df8 d _DYNAMIC
    0000000000004048 D _edata
    0000000000004050 B _end
    0000000000001165 T fibo
    0000000000001294 T _fini
    0000000000001160 t frame_dummy
    0000000000003de8 d __frame_dummy_init_array_entry
    000000000000219c r __FRAME_END__
                     U getchar@GLIBC_2.2.5
    0000000000004000 d _GLOBAL_OFFSET_TABLE_
                     w __gmon_start__
    0000000000002034 r __GNU_EH_FRAME_HDR
    0000000000001000 t _init
    0000000000003df0 d __init_array_end
    0000000000003de8 d __init_array_start
    0000000000002000 R _IO_stdin_used
                     U __isoc99_scanf@GLIBC_2.7
                     w _ITM_deregisterTMCloneTable
                     w _ITM_registerTMCloneTable
    0000000000001290 T __libc_csu_fini
    0000000000001230 T __libc_csu_init
                     U __libc_start_main@GLIBC_2.2.5
    00000000000011a4 T main
                     U printf@GLIBC_2.2.5
                     U puts@GLIBC_2.2.5
    00000000000010e0 t register_tm_clones
    0000000000001080 T _start
    0000000000004048 D __TMC_END__