How to read nm output?

Ice picture Ice · Apr 20, 2016 · Viewed 14.4k times · Source

Thats my code:

int const const_global_init = 2;
int const const_global;
int global_init = 4;
int global;

static int static_global_init = 3;
static int static_global;

static int static_function(){
    return 2;
}

double function_with_param(int a){
    static int static_local_init = 3;
    static int static_local;

    return 2.2;
}

int main(){
}

I generate main.o and i try to understood nm output. After i use nm main.o --printfile-name -a i get this output:

main.o:0000000000000000 b .bss
main.o:0000000000000000 n .comment
main.o:0000000000000004 C const_global
main.o:0000000000000000 R const_global_init
main.o:0000000000000000 d .data
main.o:0000000000000000 r .eh_frame
main.o:000000000000000b T function_with_param
main.o:0000000000000004 C global
main.o:0000000000000000 D global_init
main.o:0000000000000027 T main
main.o:0000000000000000 a main.c
main.o:0000000000000000 n .note.GNU-stack
main.o:0000000000000000 r .rodata
main.o:0000000000000000 t static_function
main.o:0000000000000000 b static_global
main.o:0000000000000004 d static_global_init
main.o:0000000000000004 b static_local.1733
main.o:0000000000000008 d static_local_init.1732
main.o:0000000000000000 t .text

I understood 2nd and 3rd column but, i really dont know what is in the first column, whether it is the address or size? I know somethink about .bbs, .comment, .data and .text segments but what is it .eh_frame, .note.GNU-stack and .rodata?

Answer

Useless picture Useless · Apr 20, 2016

... i really dont know what is in the first column, whether it is the address or size?

My local manpage (from man nm) says

DESCRIPTION
       GNU nm lists the symbols from object files objfile....  If no object files are listed as arguments, nm assumes the file a.out.

       For each symbol, nm shows:

       ·   The symbol value, in the radix selected by options (see below), or hexadecimal by default.

that is, the first column is the 'value' of the symbol. To understand what that means, it's helpful to know something about ELF and the runtime linker, but in general it will simply be an offset into the relevant section.

Understanding something about ELF will also help with the other points: man elf tells us that the .rodata section is read-only data (that is: constant values hardcoded into the program that never change. String literals might go here).

.eh_frame is used for exception-handling and other call-stack-frame metadata (a search for eh_frame has this question as the first hit).