Skip to content

Commit

Permalink
[DWARF5][COFF] Fix wrong relocation in .debug_line
Browse files Browse the repository at this point in the history
Dwarf 5 allows separating filenames from .debug_line into a separate
.debug_line_str section. The strings are referenced relative to the
start of the .debug_line_str section. Previously, on COFF, the
relocation information instead caused offsets to be relocated to the
base address of the COFF-File. This lead  to wrong offsets in linked
COFF files which caused the debugger to be unable to find the correct
source files.

This patch fixes this problem by making the offsets relative to the
start of the .debug_line_str section instead. There should be no
changes for ELF-Files as everything seems to be working there.

A test is also added to ensure that the correct relocation entries are
emitted.

See also llvm#83773
  • Loading branch information
timoh-ba committed Mar 12, 2024
1 parent aa6ebf9 commit 2709aff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion llvm/lib/MC/MCDwarf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,12 @@ void MCDwarfLineStr::emitRef(MCStreamer *MCOS, StringRef Path) {
size_t Offset = addString(Path);
if (UseRelocs) {
MCContext &Ctx = MCOS->getContext();
MCOS->emitValue(makeStartPlusIntExpr(Ctx, *LineStrLabel, Offset), RefSize);
if (Ctx.getAsmInfo()->needsDwarfSectionOffsetDirective()) {
MCOS->emitCOFFSecRel32(LineStrLabel, Offset);
} else {
MCOS->emitValue(makeStartPlusIntExpr(Ctx, *LineStrLabel, Offset),
RefSize);
}
} else
MCOS->emitIntValue(Offset, RefSize);
}
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/MC/COFF/dwarf5lineinfo.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-windows-gnu %s -o - | llvm-readobj -r - | FileCheck %s

// CHECK: Relocations [
// CHECK: Section (4) .debug_line {
// CHECK: 0x22 IMAGE_REL_AMD64_SECREL .debug_line_str (8)
// CHECK: 0x2C IMAGE_REL_AMD64_SECREL .debug_line_str (8)
// CHECK: 0x36 IMAGE_REL_AMD64_ADDR64 .text (0)
// CHECK: }

main:
.file 0 "/" "test.c"
.loc 0 1 0
retq

0 comments on commit 2709aff

Please sign in to comment.