[llvm] [DWARF5][COFF] Fix wrong relocation in .debug_line (PR #83773)

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 00:45:23 PDT 2024


https://github.com/timoh-ba updated https://github.com/llvm/llvm-project/pull/83773

>From 37570d095050eda6a709234173757a5e92a17075 Mon Sep 17 00:00:00 2001
From: Timo Habighorst <t.habighorst at beckhoff.com>
Date: Fri, 23 Feb 2024 10:48:22 +0100
Subject: [PATCH] [DWARF5][COFF] Fix wrong relocation in .debug_line

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 https://github.com/llvm/llvm-project/pull/83773
---
 llvm/lib/MC/MCDwarf.cpp            |  7 ++++++-
 llvm/test/MC/COFF/dwarf5lineinfo.s | 17 +++++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/MC/COFF/dwarf5lineinfo.s

diff --git a/llvm/lib/MC/MCDwarf.cpp b/llvm/lib/MC/MCDwarf.cpp
index d0face9140de66..2ee0c3eb27b92e 100644
--- a/llvm/lib/MC/MCDwarf.cpp
+++ b/llvm/lib/MC/MCDwarf.cpp
@@ -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);
 }
diff --git a/llvm/test/MC/COFF/dwarf5lineinfo.s b/llvm/test/MC/COFF/dwarf5lineinfo.s
new file mode 100644
index 00000000000000..d57d07dee761a5
--- /dev/null
+++ b/llvm/test/MC/COFF/dwarf5lineinfo.s
@@ -0,0 +1,17 @@
+// 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                           # test.c:1:0
+	retq
+.Linfo_string:
+.Linfo_string1:
+	.secrel32	.Linfo_string1
+	.section	.debug_line,"dr"



More information about the llvm-commits mailing list