Search code examples
gccx86-64elfabidwarf

.dbug_loc is missing from binary


I was inspecting ELF binary for the code mentioned below. I compiled it on gcc 7.4.0 on Ubuntu x86_64 system in Virtual Box as :

gcc -g scratch1.c -o scratch1.out

#include <stdio.h>
void do_stuff(int my_arg){
    int my_local = my_arg + 2;
    int i;
    for (i = 0; i < my_local; ++i) printf("i = %d\n", i);
}

int main(){
    do_stuff(2);
    return 0;
}

However when I tried to refer to .debug_loc section, it was empty.

objdump --dwarf=loc scratch1.out

scratch1.out:     file format elf64-x86-64

Although, I do see some other debug_ sections

[26] .debug_aranges    PROGBITS         0000000000000000  0000103b
       0000000000000030  0000000000000000           0     0     1
[27] .debug_info       PROGBITS         0000000000000000  0000106b
       000000000000035f  0000000000000000           0     0     1
[28] .debug_abbrev     PROGBITS         0000000000000000  000013ca
       0000000000000125  0000000000000000           0     0     1
[29] .debug_line       PROGBITS         0000000000000000  000014ef
       00000000000000e6  0000000000000000           0     0     1
[30] .debug_str        PROGBITS         0000000000000000  000015d5
       00000000000002b5  0000000000000001  MS       0     0     1

Need your help why this section is missing.


Solution

  • Need your help why this section is missing.

    The .debug_loc section contains DW_AT_location lists, usually emitted only for optimized code.

    Since you compiled without optimization, the compiler didn't emit this section. Try building with:

    gcc -g -O3 scratch1.c -o scratch1.out
    

    Here is what I see with gcc 8.3.0:

    $ gcc -g scratch.c && readelf -WS a.out | grep debug_loc
      # no output
    
    $ gcc -g scratch.c -O3 && readelf -WS a.out | grep debug_loc
      [32] .debug_loc        PROGBITS        0000000000000000 0039db 000230 00      0   0  1