DebugInfo/Symbolize: Retrieve filename from the preceding STT_FILE for .symtab symbol...
authorFangrui Song <i@maskray.me>
Wed, 10 Feb 2021 17:47:10 +0000 (09:47 -0800)
committerFangrui Song <i@maskray.me>
Wed, 10 Feb 2021 17:47:10 +0000 (09:47 -0800)
commit04a2e12612f49e65a7dc14fa45f11f0632d427f2
tree9d7506a5b251a504b4c6c4f62e14ffd6b95b09c5
parent4f30a3d3d26d55c0701dc56dde711667320fdd30
DebugInfo/Symbolize: Retrieve filename from the preceding STT_FILE for .symtab symbolization

The ELF spec says:

> STT_FILE: Conventionally, the symbol's name gives the name of the source file associated with the object file. A file symbol has STB_LOCAL binding, its section index is SHN_ABS, and it precedes the other STB_LOCAL symbols for the file, if it is present.

For a local symbol, the preceding STT_FILE symbol is almost always in the same
file[1]. GNU addr2line uses this heuristic to retrieve the filename associated
with a local symbol (e.g. internal linkage functions in C/C++).

GNU addr2line can assign STT_FILE filename to a non-local symbol, too, but the trick
only works if no regular symbol precede STT_FILE. This patch does not implement this corner case
(not useful for most executables which have more than one files).

In case of filename mismatch between .debug_line & .symtab, arbitrarily make .debug_line win.

[1]: LLD does not synthesize STT_FILE symbols
(https://bugs.llvm.org/show_bug.cgi?id=48023 see also
https://sourceware.org/bugzilla/show_bug.cgi?id=26822).  An assembly file
without `.file` directives can cause mis-attribution. This is an edge case.

Differential Revision: https://reviews.llvm.org/D95927
llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.cpp
llvm/lib/DebugInfo/Symbolize/SymbolizableObjectFile.h
llvm/test/DebugInfo/Symbolize/ELF/symtab-file-conflict.s [new file with mode: 0644]
llvm/test/DebugInfo/Symbolize/ELF/symtab-file.s
llvm/test/DebugInfo/Symbolize/ELF/symtab-file2.yaml [new file with mode: 0644]
llvm/test/DebugInfo/Symbolize/ELF/symtab-ignored.s
llvm/test/DebugInfo/Symbolize/ELF/symtab-notype.s