DWARF 5 specifies that in a line number program header in .debug_line,
directory entry 0 must be the compilation directory and file name entry
0 must be the compilation file name. Consider the following correct
output:
$ pwd /home/osandov/test $ cat foo/bar/baz.c int my_var = 1; $ clang -g -gdwarf-5 -c foo/bar/baz.c $ readelf --debug-dump=line baz.o ... The Directory Table (offset 0x22, lines 1, columns 1): Entry Name 0 (indirect line string, offset: 0x0): /home/osandov/test The File Name Table (offset 0x2e, lines 1, columns 3): Entry Dir MD5 Name 0 0 0x436e7c8f87b60ba4ecab92f1eddfcbb4 (indirect line string, offset: 0x13): foo/bar/baz.c
However, when using -no-integrated-as, the compilation directory is
incorrectly given as the parent directory of the source file, and the
compilation file name is given relative to that:
$ clang -g -gdwarf-5 -no-integrated-as -c foo/bar/baz.c $ readelf --debug-dump=line baz.o ... The Directory Table (offset 0x22, lines 1, columns 1): Entry Name 0 (indirect line string, offset: 0x0): /home/osandov/test/foo/bar The File Name Table (offset 0x2e, lines 1, columns 3): Entry Dir MD5 Name 0 0 0xb4cbdfedf192abeca40bb6878f7c6e43 (indirect line string, offset: 0x1b): baz.c
This is because the generated assembly .file directive does not split
the directory from the file name if -no-integrated-as is used:
$ clang -g -gdwarf-5 -S foo/bar/baz.c -o - | grep '\.file\s\+0' .file 0 "/home/osandov/test" "foo/bar/baz.c" md5 0xb4cbdfedf192abeca40bb6878f7c6e43 $ clang -g -gdwarf-5 -no-integrated-as -S foo/bar/baz.c -o - | grep '\.file\s\+0' .file 0 "/home/osandov/test/foo/bar/baz.c" md5 0xb4cbdfedf192abeca40bb6878f7c6e43
This is ostensibly for compatibility, since the [extended .file
syntax](https://sourceware.org/binutils/docs/as/File.html) that allows
specifying the directory separately was only recently added to the GNU
Assembler in binutils
2.35.
But, the md5 argument also relies on the extended syntax, so this
isn't actually improving compatibility when using DWARF 5. Fix this bug
by always using the directory argument for DWARF 5.
I noticed this bug when compiling the Linux kernel with Clang and DWARF
5, as the Linux kernel is compiled with -no-integrated-as.
Test Plan:
Updated CodeGen tests. Ran the reproducer above and saw that it matched
the -integrated-as version.
You may repurpose the patch to drop UseDwarfDirectory. The refactoring is still good to have.