Re: [PATCH] Fix macro info lookup for binaries containing DWARFv5 line table

Simon Marchi via Gdb-patches
2021-05-14T18:50:16Z

> One thing I noticed that changed is that GCC is now emitting `dir0` entry in line table as per DWARF5. However this still seems
> Incomplete to me:
> $llvm-dwarfdump --debug-line a.out
> ```
> include_directories[ 1] = "/usr/include" // index still starting from `1` even at "-gdwarf-5"
> file_names[ 1]:
> name: "macro.c"
> dir_index: 0 // -> dir 0 mentioned but dir 0 is not present in list of directories, i.e index still starting from 1
> mod_time: 0x00000000
> length: 0x00000000
> file_names[ 2]:
> name: "stdc-predef.h"
> dir_index: 1
> mod_time: 0x00000000
> length: 0x00000000
> ```

It's not clear how you produced this output (which compiler and which
command). What's the version of that section? Because it looks like a
DWARF 4 or before section. GCC 10 produces a DWARF 3 .debug_line
section, even when using -gdwarf-5.

In DWARF 4 and before, a dir_index of 0 referred to the compilation
directory, even if there wasn't an explicit dir[0] value. See section
6.2.4, bullet 12, of DWARF4.pdf. So you would look up the DW_AT_comp
attribute of the compilation unit, append "macro.c", and that would give
you the location of that file.

On my side, I see this with gcc 11 (from the Ubuntu 21.04 package):

root@bc7daa9a3ffc:/# gcc-11 --version
gcc-11 (Ubuntu 11.1.0-1ubuntu1~21.04) 11.1.0
root@bc7daa9a3ffc:/# cat test.c
#include <stdio.h>

#define FOO

int main() {
}
root@bc7daa9a3ffc:/# gcc-11 -gdwarf-5 -g3 test.c
root@bc7daa9a3ffc:/# llvm-dwarfdump-12 --debug-line a.out
...
include_directories[ 0] = "/usr/include"
include_directories[ 1] = "/usr/include"
include_directories[ 2] = "/usr/include/x86_64-linux-gnu/bits"
include_directories[ 3] = "/usr/include/x86_64-linux-gnu/sys"
include_directories[ 4] = "/usr/include/x86_64-linux-gnu/gnu"
include_directories[ 5] = "/usr/lib/gcc/x86_64-linux-gnu/11/include"
include_directories[ 6] = "/usr/include/x86_64-linux-gnu/bits/types"
file_names[ 0]:
name: "test.c"
dir_index: 0
file_names[ 1]:
name: "test.c"
dir_index: 0
file_names[ 2]:
name: "stdc-predef.h"
dir_index: 1
file_names[ 3]:
name: "stdio.h"
dir_index: 1

So from what I see, GCC 11 generates a dir[0] entry and a file[0] entry.
However, the dir[0] value does not look good. It makes it appear
as if my source file was at /usr/include/test.c, which it wasn't (it was
at /test.c).

Simon