llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

BOLT corrupts DWARF file indices when the input has duplicate `file_names` entries

Open not4juu opened this issue 4 months ago • 2 comments

Description

When BOLT rebuilds a compile unit's line-table file list, it re-inserts every entry through MCDwarfLineTableHeader::tryGetFile, which deduplicates identical files. If the input line program contains a duplicate file_names entry (both GCC and Clang can emit the primary source file twice with an identical dir+name, see example below), BOLT collapses it, shrinks the table, and shifts the following file indices — but does not remap the references that point into that table.

Result, in the affected CUs:

  • .debug_info: DW_AT_decl_file / DW_AT_call_file point past the end of the (shrunken) file table.
  • .debug_line: DW_LNS_set_file references a non-existent file number.

The defect is producer-independent. The bug triggers on any input whose file table contains an identical duplicate that collapses on re-insertion. A single reproducer confirms this in both cases — the same main.cpp compiled with either GCC or Clang produces the exact same corruption.

Reproducer

One self-contained source file, main.cpp (plus widget.hpp):

main.cpp

// Both compilers end up with a byte-identical duplicate primary-file entry in the DWARF5 line-table file_names that BOLT collapses (3->2), 
// shifting widget.hpp's index out of range:
//   GCC:   [0]main.cpp [1]main.cpp  [2]widget.hpp   ([0]==[1])
//   Clang: [0]main.cpp [1]widget.hpp [2]main.cpp    ([0]==[2])
#include "widget.hpp"

#line 1 "main.cpp"

int main(int argc, char **) {
  Widget w{argc};
  return w.scaled(3);
}

NOTE: The #line 1 "main.cpp" directive in main.cpp is required for Clang (it re-injects the primary file name so Clang emits the identical duplicate) and harmless for GCC (which already lists the primary file twice). Compile with a relative source path so the injected name matches.

widget.hpp

#pragma once

struct Widget {
  int value;
  int scaled(int k) const { return value * k; }
};

Compilation and BOLT debug section update

# GCC
g++     -g -gdwarf-5 -gz=none -O2 -Wl,-q -Wl,--compress-debug-sections=none main.cpp -o main
# or Clang
clang++ -g -gdwarf-5 -gz=none -O2 -Wl,-q -Wl,--compress-debug-sections=none main.cpp -o main

llvm-bolt main -o main.bolt --update-debug-sections

Baseline (before BOLT)

llvm-dwarfdump --verify main | grep 'invalid file index' // <--- no problem reported

llvm-dwarfdump --debug-info main   | grep -E 'DW_TAG_|DW_AT_name|DW_AT_decl_file|DW_AT_call_file'
0x0000000c: DW_TAG_compile_unit
              DW_AT_name        ("main.cpp")
0x0000002a:   DW_TAG_structure_type
                DW_AT_name      ("Widget")
                DW_AT_decl_file ("/workspace/widget.hpp")
0x00000037:     DW_TAG_member
                  DW_AT_name    ("value")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x00000044:     DW_TAG_subprogram
                  DW_AT_name    ("scaled")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x00000058:       DW_TAG_formal_parameter
0x0000005d:       DW_TAG_formal_parameter
0x00000064:   DW_TAG_const_type
0x00000069:   DW_TAG_base_type
                DW_AT_name      ("int")
0x00000070:   DW_TAG_pointer_type
0x00000075:   DW_TAG_const_type
0x0000007a:   DW_TAG_subprogram
                DW_AT_name      ("main")
                DW_AT_decl_file ("/workspace/main.cpp")
0x0000009c:     DW_TAG_formal_parameter
                  DW_AT_name    ("argc")
                  DW_AT_decl_file       ("/workspace/main.cpp")
0x000000aa:     DW_TAG_formal_parameter
0x000000b1:     DW_TAG_variable
                  DW_AT_name    ("w")
                  DW_AT_decl_file       ("/workspace/main.cpp")
0x000000c3:     DW_TAG_inlined_subroutine
                  DW_AT_call_file       ("/workspace/main.cpp")
0x000000e4:       DW_TAG_formal_parameter
0x000000f1:       DW_TAG_formal_parameter
0x00000100:   DW_TAG_pointer_type
0x00000105:   DW_TAG_pointer_type
0x0000010a:   DW_TAG_base_type
                DW_AT_name      ("char")
0x00000111:   DW_TAG_subprogram
0x0000011b:     DW_TAG_formal_parameter
                  DW_AT_name    ("this")
0x00000124:     DW_TAG_formal_parameter
                  DW_AT_name    ("k")
                  DW_AT_decl_file       ("/workspace/widget.hpp")

Observed corruption (after BOLT)

addr2line -e main.bolt <addr in main> 
addr2line: DWARF error: mangled line number section (bad file number)

llvm-dwarfdump --verify main.bolt | grep 'invalid file index'
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: .debug_line[0x00000000][0] has invalid file index 2  (valid values are [0,2)):

llvm-dwarfdump --debug-info main.bolt   | grep -E 'DW_TAG_|DW_AT_name|DW_AT_decl_file|DW_AT_call_file'
0x0000000c: DW_TAG_compile_unit
              DW_AT_name        ("main.cpp")
0x00000033:   DW_TAG_structure_type
                DW_AT_name      ("Widget")
                DW_AT_decl_file (0x02)
0x00000040:     DW_TAG_member
                  DW_AT_name    ("value")
                  DW_AT_decl_file       (0x02)
0x0000004d:     DW_TAG_subprogram
                  DW_AT_name    ("scaled")
                  DW_AT_decl_file       (0x02)
0x00000061:       DW_TAG_formal_parameter
0x00000066:       DW_TAG_formal_parameter
0x0000006d:   DW_TAG_const_type
0x00000072:   DW_TAG_base_type
                DW_AT_name      ("int")
0x00000079:   DW_TAG_pointer_type
0x0000007e:   DW_TAG_const_type
0x00000083:   DW_TAG_subprogram
                DW_AT_name      ("main")
                DW_AT_decl_file ("/workspace/widget.hpp")
0x000000a5:     DW_TAG_formal_parameter
                  DW_AT_name    ("argc")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x000000b3:     DW_TAG_formal_parameter
0x000000ba:     DW_TAG_variable
                  DW_AT_name    ("w")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x000000c9:     DW_TAG_inlined_subroutine
                  DW_AT_call_file       ("/workspace/widget.hpp")
0x000000ea:       DW_TAG_formal_parameter
0x000000f4:       DW_TAG_formal_parameter
0x00000100:   DW_TAG_pointer_type
0x00000105:   DW_TAG_pointer_type
0x0000010a:   DW_TAG_base_type
                DW_AT_name      ("char")
0x00000111:   DW_TAG_subprogram
0x0000011b:     DW_TAG_formal_parameter
                  DW_AT_name    ("this")
0x00000124:     DW_TAG_formal_parameter
                  DW_AT_name    ("k")
                  DW_AT_decl_file       (0x02)

What to look at — two distinct failure modes. The input file table [0]main.cpp [1]main.cpp [2]widget.hpp collapses to [0]main.cpp [1]widget.hpp, but BOLT keeps emitting the original indices, so:

DIE(s) emitted index resolves to correct?
Widget, value, scaled, k 2 — (out of range → 0x02) ✗ (verify error)
main, argc, w, inlined call_file 1 widget.hpp ✗ (should be main.cpp)

The first case is caught by --verify ("invalid file index 2"); the second is a silent mis-attribution — index 1 is in range, it simply changed meaning after the duplicate was collapsed (input 1 = main.cpp → output 1 = widget.hpp). addr2line inside main therefore reports the wrong file or fails outright.

Root cause

  1. BinaryContext::preprocessDebugInfo calls getDwarfFile(...) and discards the returned (possibly deduplicated) index — no input→output map is recorded.
  2. .debug_line intra-CU (BinaryEmitter::emitLineInfo) and the raw-sequence emitter (emitBinaryDwarfLineTable in DebugData.cpp) emit the raw index; only the cross-CU/inlined case is remapped.
  3. .debug_info DW_AT_decl_file/DW_AT_call_file are copied verbatim by DIEBuilder::cloneScalarAttribute.

The Aug-2025 fixes #151401 (DWARF4/5 index base) and #151230 (cross-CU remapping) do not cover this intra-CU index shift or the .debug_info references.

Spec compliance

The duplicated file_names entry is legal, spec-compliant DWARF, so the fault lies entirely with BOLT (a DWARF consumer/rewriter):

  • DWARF v5, §6.2.4 "The Line Number Program Header" defines file_names and requires only that entry [0] be the primary source file (its name matching the CU's DW_AT_name). It imposes no uniqueness constraint — repeated entries are permitted, merely redundant. Nothing says a consumer may treat a duplicate as an error.

Proposed fix

Record the input→output file-index mapping when the table is rebuilt and apply it in the three places above (.debug_line intra-CU + raw sequence, and .debug_info decl/call file).

diff --git a/bolt/include/bolt/Core/DebugData.h b/bolt/include/bolt/Core/DebugData.h
--- a/bolt/include/bolt/Core/DebugData.h
+++ b/bolt/include/bolt/Core/DebugData.h
@@ -821,6 +821,11 @@ private:
   StringRef RawData;
 
   /// DWARF Version
   uint16_t DwarfVersion;
 
+  /// Maps an input line-table file index to the output (post-deduplication)
+  /// file index for this CU. Slots left as UINT32_MAX fall back to identity.
+  std::vector<uint32_t> FileIndexRemap;
+
 public:
   /// Emit line info for all units in the binary context.
   static void emit(BinaryContext &BC, MCStreamer &Streamer);
@@ -841,6 +846,19 @@ public:
     return Header.tryGetFile(Directory, FileName, Checksum, Source,
                              DwarfVersion, FileNumber);
   }
 
+  /// Record that input file index InputIndex maps to output file index
+  /// OutputIndex after (possible) deduplication in the rebuilt file table.
+  void mapFileIndex(uint32_t InputIndex, uint32_t OutputIndex) {
+    if (InputIndex >= FileIndexRemap.size())
+      FileIndexRemap.resize(InputIndex + 1, UINT32_MAX);
+    FileIndexRemap[InputIndex] = OutputIndex;
+  }
+
+  /// Translate an input file index to the output one, falling back to identity
+  /// when no remap was recorded (e.g. tables that were not rebuilt).
+  uint32_t getMappedFileIndex(uint32_t InputIndex) const {
+    if (InputIndex < FileIndexRemap.size() &&
+        FileIndexRemap[InputIndex] != UINT32_MAX)
+      return FileIndexRemap[InputIndex];
+    return InputIndex;
+  }
+
   /// Return label at the start of the emitted debug line for the unit.
   MCSymbol *getLabel() const { return Header.Label; }
 
diff --git a/bolt/include/bolt/Core/DIEBuilder.h b/bolt/include/bolt/Core/DIEBuilder.h
--- a/bolt/include/bolt/Core/DIEBuilder.h
+++ b/bolt/include/bolt/Core/DIEBuilder.h
@@ -190,7 +190,7 @@
 
   /// Clone an attribute in scalar format.
   void cloneScalarAttribute(
-      DIE &Die, const DWARFDie &InputDIE,
+      DIE &Die, const DWARFDie &InputDIE, DWARFUnit &U,
       const DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
       const DWARFFormValue &Val);
 
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -1948,8 +1948,11 @@
       std::optional<MD5::MD5Result> Checksum;
       if (DwarfVersion >= 5 && LineTable->Prologue.ContentTypes.HasMD5)
         Checksum = LineTable->Prologue.FileNames[I].Checksum;
-      cantFail(getDwarfFile(Dir, FileName, 0, Checksum, std::nullopt, CUID,
-                            DwarfVersion));
+      unsigned OutputFileIndex = cantFail(getDwarfFile(
+          Dir, FileName, 0, Checksum, std::nullopt, CUID, DwarfVersion));
+      // Files may be deduplicated on insertion; record the input->output index
+      // mapping so .debug_info and .debug_line references can be fixed up.
+      BinaryLineTable.mapFileIndex(I + Offset, OutputFileIndex);
     }
   }
 
diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp
--- a/bolt/lib/Core/BinaryEmitter.cpp
+++ b/bolt/lib/Core/BinaryEmitter.cpp
@@ -684,6 +684,12 @@
     if (TargetUnitIndex != CurrentUnitIndex) {
       // Add filename from the inlined function to the current CU.
       TargetFilenum = BC.addDebugFilenameToUnit(
           TargetUnitIndex, CurrentUnitIndex, CurrentRow.File);
+    } else {
+      // Same CU: the file table may have been deduplicated when rebuilt, so
+      // translate the input file index to the output one.
+      TargetFilenum = BC.getDwarfLineTable(TargetUnitIndex)
+                          .getMappedFileIndex(TargetFilenum);
     }
     BC.Ctx->setCurrentDwarfLoc(TargetFilenum, CurrentRow.Line,
                                CurrentRow.Column, Flags, CurrentRow.Isa,
diff --git a/bolt/lib/Core/DebugData.cpp b/bolt/lib/Core/DebugData.cpp
--- a/bolt/lib/Core/DebugData.cpp
+++ b/bolt/lib/Core/DebugData.cpp
@@ -1001,7 +1001,8 @@
 static inline void emitBinaryDwarfLineTable(
     MCStreamer *MCOS, MCDwarfLineTableParams Params,
     const DWARFDebugLine::LineTable *Table,
-    const std::vector<DwarfLineTable::RowSequence> &InputSequences) {
+    const std::vector<DwarfLineTable::RowSequence> &InputSequences,
+    const DwarfLineTable &DLT) {
   if (InputSequences.empty())
     return;
 
@@ -1044,7 +1045,7 @@
       if (FileNum != Row.File) {
         FileNum = Row.File;
         MCOS->emitInt8(dwarf::DW_LNS_set_file);
-        MCOS->emitULEB128IntValue(FileNum);
+        MCOS->emitULEB128IntValue(DLT.getMappedFileIndex(FileNum));
       }
 
diff --git a/bolt/lib/Core/DebugData.cpp b/bolt/lib/Core/DebugData.cpp
--- a/bolt/lib/Core/DebugData.cpp
+++ b/bolt/lib/Core/DebugData.cpp
@@ -1204,7 +1204,7 @@
 
   // Emit line tables for the original code.
-  emitBinaryDwarfLineTable(MCOS, Params, InputTable, InputSequences);
+  emitBinaryDwarfLineTable(MCOS, Params, InputTable, InputSequences, *this);
 
   // This is the end of the section, so set the value of the symbol at the end
diff --git a/bolt/lib/Core/DIEBuilder.cpp b/bolt/lib/Core/DIEBuilder.cpp
--- a/bolt/lib/Core/DIEBuilder.cpp
+++ b/bolt/lib/Core/DIEBuilder.cpp
@@ -927,7 +927,7 @@
 
 void DIEBuilder::cloneScalarAttribute(
-    DIE &Die, const DWARFDie &InputDIE,
+    DIE &Die, const DWARFDie &InputDIE, DWARFUnit &U,
     const DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
     const DWARFFormValue &Val) {
   uint64_t Value;
@@ -944,6 +944,17 @@
     return;
   }
 
+  // DW_AT_decl_file / DW_AT_call_file index the CU's line-table file list,
+  // which BOLT may deduplicate when rebuilding. Translate the input index to
+  // the output one. DW_FORM_implicit_const stores its value in the abbrev, not
+  // the DIE, so it is left untouched (not observed to be affected in practice).
+  if ((AttrSpec.Attr == dwarf::DW_AT_decl_file ||
+       AttrSpec.Attr == dwarf::DW_AT_call_file) &&
+      AttrSpec.Form != dwarf::DW_FORM_implicit_const)
+    Value =
+        BC.getDwarfLineTable(U.getOffset()).getMappedFileIndex(Value);
+
   Die.addValue(getState().DIEAlloc, AttrSpec.Attr, AttrSpec.Form,
                DIEInteger(Value));
 }
@@ -1024,7 +1035,7 @@
   case dwarf::DW_FORM_implicit_const:
-    cloneScalarAttribute(Die, InputDIE, AttrSpec, Val);
+    cloneScalarAttribute(Die, InputDIE, U, AttrSpec, Val);
     break;
   case dwarf::DW_FORM_loclistx:
     cloneLoclistAttrubute(Die, InputDIE, AttrSpec, Val);

Known limitation: DW_AT_decl_file encoded as DW_FORM_implicit_const stores its value in the abbreviation, not the DIE, and is left untouched. - NO MORE VALID please see comment below in that thread

Environment

  • llvm-project HEAD: bd6adfedc776c07caf158e59367d9c246c933510 (2026-06-30) + the fix for #192084 (PR #192166, "Fix DW_FORM_implicit_const values lost during DWARF5 rewriting", merged 2026-07-08 as c7c7cab — it postdates the HEAD used here, so nothing is needed on current main). It matters only for the DW_FORM_implicit_const follow-up in the comments below: without it DIEBuilder::assignAbbrev() discards implicit_const values, so a remapped index never reaches the emitted abbreviation)
  • Aug-2025 DWARF file-index fixes #151401, #151230 already present.
  • Reproduced with the same main.cpp under both GCC and Clang.
  • Should be platform-agnostic (x86_64 and AArch64).

not4juu avatar Jul 13 '26 13:07 not4juu

@llvm/issue-subscribers-bolt

Author: Sebastian (not4juu)

## Description

When BOLT rebuilds a compile unit's line-table file list, it re-inserts every entry through MCDwarfLineTableHeader::tryGetFile, which deduplicates identical files. If the input line program contains a duplicate file_names entry (both GCC and Clang can emit the primary source file twice with an identical dir+name, see example below), BOLT collapses it, shrinks the table, and shifts the following file indices — but does not remap the references that point into that table.

Result, in the affected CUs:

  • .debug_info: DW_AT_decl_file / DW_AT_call_file point past the end of the (shrunken) file table.
  • .debug_line: DW_LNS_set_file references a non-existent file number.

The defect is producer-independent. The bug triggers on any input whose file table contains an identical duplicate that collapses on re-insertion. A single reproducer confirms this in both cases — the same main.cpp compiled with either GCC or Clang produces the exact same corruption.

Reproducer

One self-contained source file, main.cpp (plus widget.hpp):

main.cpp

// Both compilers end up with a byte-identical duplicate primary-file entry in the DWARF5 line-table file_names that BOLT collapses (3-&gt;2), 
// shifting widget.hpp's index out of range:
//   GCC:   [0]main.cpp [1]main.cpp  [2]widget.hpp   ([0]==[1])
//   Clang: [0]main.cpp [1]widget.hpp [2]main.cpp    ([0]==[2])
#include "widget.hpp"

#line 1 "main.cpp"

int main(int argc, char **) {
  Widget w{argc};
  return w.scaled(3);
}

> NOTE: The #line 1 "main.cpp" directive in main.cpp is required for Clang (it re-injects the primary file name so Clang emits the identical duplicate) and harmless for GCC (which already lists the primary file twice). Compile with a relative source path so the injected name matches.

widget.hpp

#pragma once

struct Widget {
  int value;
  int scaled(int k) const { return value * k; }
};

Compilation and BOLT debug section update

# GCC
g++     -g -gdwarf-5 -gz=none -O2 -Wl,-q -Wl,--compress-debug-sections=none main.cpp -o main
# or Clang
clang++ -g -gdwarf-5 -gz=none -O2 -Wl,-q -Wl,--compress-debug-sections=none main.cpp -o main

llvm-bolt main -o main.bolt --update-debug-sections

Baseline (before BOLT)

llvm-dwarfdump --verify main | grep 'invalid file index' // &lt;--- no problem reported

llvm-dwarfdump --debug-info main   | grep -E 'DW_TAG_|DW_AT_name|DW_AT_decl_file|DW_AT_call_file'
0x0000000c: DW_TAG_compile_unit
              DW_AT_name        ("main.cpp")
0x0000002a:   DW_TAG_structure_type
                DW_AT_name      ("Widget")
                DW_AT_decl_file ("/workspace/widget.hpp")
0x00000037:     DW_TAG_member
                  DW_AT_name    ("value")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x00000044:     DW_TAG_subprogram
                  DW_AT_name    ("scaled")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x00000058:       DW_TAG_formal_parameter
0x0000005d:       DW_TAG_formal_parameter
0x00000064:   DW_TAG_const_type
0x00000069:   DW_TAG_base_type
                DW_AT_name      ("int")
0x00000070:   DW_TAG_pointer_type
0x00000075:   DW_TAG_const_type
0x0000007a:   DW_TAG_subprogram
                DW_AT_name      ("main")
                DW_AT_decl_file ("/workspace/main.cpp")
0x0000009c:     DW_TAG_formal_parameter
                  DW_AT_name    ("argc")
                  DW_AT_decl_file       ("/workspace/main.cpp")
0x000000aa:     DW_TAG_formal_parameter
0x000000b1:     DW_TAG_variable
                  DW_AT_name    ("w")
                  DW_AT_decl_file       ("/workspace/main.cpp")
0x000000c3:     DW_TAG_inlined_subroutine
                  DW_AT_call_file       ("/workspace/main.cpp")
0x000000e4:       DW_TAG_formal_parameter
0x000000f1:       DW_TAG_formal_parameter
0x00000100:   DW_TAG_pointer_type
0x00000105:   DW_TAG_pointer_type
0x0000010a:   DW_TAG_base_type
                DW_AT_name      ("char")
0x00000111:   DW_TAG_subprogram
0x0000011b:     DW_TAG_formal_parameter
                  DW_AT_name    ("this")
0x00000124:     DW_TAG_formal_parameter
                  DW_AT_name    ("k")
                  DW_AT_decl_file       ("/workspace/widget.hpp")

Observed corruption (after BOLT)

addr2line -e main.bolt &lt;addr in main&gt; 
addr2line: DWARF error: mangled line number section (bad file number)

llvm-dwarfdump --verify main.bolt | grep 'invalid file index'
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: DIE has DW_AT_decl_file with an invalid file index 2 (valid values are [0-1])
error: .debug_line[0x00000000][0] has invalid file index 2  (valid values are [0,2)):

llvm-dwarfdump --debug-info main.bolt   | grep -E 'DW_TAG_|DW_AT_name|DW_AT_decl_file|DW_AT_call_file'
0x0000000c: DW_TAG_compile_unit
              DW_AT_name        ("main.cpp")
0x00000033:   DW_TAG_structure_type
                DW_AT_name      ("Widget")
                DW_AT_decl_file (0x02)
0x00000040:     DW_TAG_member
                  DW_AT_name    ("value")
                  DW_AT_decl_file       (0x02)
0x0000004d:     DW_TAG_subprogram
                  DW_AT_name    ("scaled")
                  DW_AT_decl_file       (0x02)
0x00000061:       DW_TAG_formal_parameter
0x00000066:       DW_TAG_formal_parameter
0x0000006d:   DW_TAG_const_type
0x00000072:   DW_TAG_base_type
                DW_AT_name      ("int")
0x00000079:   DW_TAG_pointer_type
0x0000007e:   DW_TAG_const_type
0x00000083:   DW_TAG_subprogram
                DW_AT_name      ("main")
                DW_AT_decl_file ("/workspace/widget.hpp")
0x000000a5:     DW_TAG_formal_parameter
                  DW_AT_name    ("argc")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x000000b3:     DW_TAG_formal_parameter
0x000000ba:     DW_TAG_variable
                  DW_AT_name    ("w")
                  DW_AT_decl_file       ("/workspace/widget.hpp")
0x000000c9:     DW_TAG_inlined_subroutine
                  DW_AT_call_file       ("/workspace/widget.hpp")
0x000000ea:       DW_TAG_formal_parameter
0x000000f4:       DW_TAG_formal_parameter
0x00000100:   DW_TAG_pointer_type
0x00000105:   DW_TAG_pointer_type
0x0000010a:   DW_TAG_base_type
                DW_AT_name      ("char")
0x00000111:   DW_TAG_subprogram
0x0000011b:     DW_TAG_formal_parameter
                  DW_AT_name    ("this")
0x00000124:     DW_TAG_formal_parameter
                  DW_AT_name    ("k")
                  DW_AT_decl_file       (0x02)

What to look at — two distinct failure modes. The input file table [0]main.cpp [1]main.cpp [2]widget.hpp collapses to [0]main.cpp [1]widget.hpp, but BOLT keeps emitting the original indices, so:

DIE(s) emitted index resolves to correct?
Widget, value, scaled, k 2 — (out of range → 0x02) ✗ (verify error)
main, argc, w, inlined call_file 1 widget.hpp ✗ (should be main.cpp)

The first case is caught by --verify ("invalid file index 2"); the second is a silent mis-attribution — index 1 is in range, it simply changed meaning after the duplicate was collapsed (input 1 = main.cpp → output 1 = widget.hpp). addr2line inside main therefore reports the wrong file or fails outright.

Root cause

  1. BinaryContext::preprocessDebugInfo calls getDwarfFile(...) and discards the returned (possibly deduplicated) index — no input→output map is recorded.
  2. .debug_line intra-CU (BinaryEmitter::emitLineInfo) and the raw-sequence emitter (emitBinaryDwarfLineTable in DebugData.cpp) emit the raw index; only the cross-CU/inlined case is remapped.
  3. .debug_info DW_AT_decl_file/DW_AT_call_file are copied verbatim by DIEBuilder::cloneScalarAttribute.

The Aug-2025 fixes #<!-- -->151401 (DWARF4/5 index base) and #<!-- -->151230 (cross-CU remapping) do not cover this intra-CU index shift or the .debug_info references.

Spec compliance

The duplicated file_names entry is legal, spec-compliant DWARF, so the fault lies entirely with BOLT (a DWARF consumer/rewriter):

  • DWARF v5, §6.2.4 "The Line Number Program Header" defines file_names and requires only that entry [0] be the primary source file (its name matching the CU's DW_AT_name). It imposes no uniqueness constraint — repeated entries are permitted, merely redundant. Nothing says a consumer may treat a duplicate as an error.

Proposed fix

Record the input→output file-index mapping when the table is rebuilt and apply it in the three places above (.debug_line intra-CU + raw sequence, and .debug_info decl/call file).

diff --git a/bolt/include/bolt/Core/DebugData.h b/bolt/include/bolt/Core/DebugData.h
--- a/bolt/include/bolt/Core/DebugData.h
+++ b/bolt/include/bolt/Core/DebugData.h
@@ -821,6 +821,11 @@ private:
   StringRef RawData;
 
   /// DWARF Version
   uint16_t DwarfVersion;
 
+  /// Maps an input line-table file index to the output (post-deduplication)
+  /// file index for this CU. Slots left as UINT32_MAX fall back to identity.
+  std::vector&lt;uint32_t&gt; FileIndexRemap;
+
 public:
   /// Emit line info for all units in the binary context.
   static void emit(BinaryContext &amp;BC, MCStreamer &amp;Streamer);
@@ -841,6 +846,19 @@ public:
     return Header.tryGetFile(Directory, FileName, Checksum, Source,
                              DwarfVersion, FileNumber);
   }
 
+  /// Record that input file index InputIndex maps to output file index
+  /// OutputIndex after (possible) deduplication in the rebuilt file table.
+  void mapFileIndex(uint32_t InputIndex, uint32_t OutputIndex) {
+    if (InputIndex &gt;= FileIndexRemap.size())
+      FileIndexRemap.resize(InputIndex + 1, UINT32_MAX);
+    FileIndexRemap[InputIndex] = OutputIndex;
+  }
+
+  /// Translate an input file index to the output one, falling back to identity
+  /// when no remap was recorded (e.g. tables that were not rebuilt).
+  uint32_t getMappedFileIndex(uint32_t InputIndex) const {
+    if (InputIndex &lt; FileIndexRemap.size() &amp;&amp;
+        FileIndexRemap[InputIndex] != UINT32_MAX)
+      return FileIndexRemap[InputIndex];
+    return InputIndex;
+  }
+
   /// Return label at the start of the emitted debug line for the unit.
   MCSymbol *getLabel() const { return Header.Label; }
 
diff --git a/bolt/include/bolt/Core/DIEBuilder.h b/bolt/include/bolt/Core/DIEBuilder.h
--- a/bolt/include/bolt/Core/DIEBuilder.h
+++ b/bolt/include/bolt/Core/DIEBuilder.h
@@ -190,7 +190,7 @@
 
   /// Clone an attribute in scalar format.
   void cloneScalarAttribute(
-      DIE &amp;Die, const DWARFDie &amp;InputDIE,
+      DIE &amp;Die, const DWARFDie &amp;InputDIE, DWARFUnit &amp;U,
       const DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
       const DWARFFormValue &amp;Val);
 
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -1948,8 +1948,11 @@
       std::optional&lt;MD5::MD5Result&gt; Checksum;
       if (DwarfVersion &gt;= 5 &amp;&amp; LineTable-&gt;Prologue.ContentTypes.HasMD5)
         Checksum = LineTable-&gt;Prologue.FileNames[I].Checksum;
-      cantFail(getDwarfFile(Dir, FileName, 0, Checksum, std::nullopt, CUID,
-                            DwarfVersion));
+      unsigned OutputFileIndex = cantFail(getDwarfFile(
+          Dir, FileName, 0, Checksum, std::nullopt, CUID, DwarfVersion));
+      // Files may be deduplicated on insertion; record the input-&gt;output index
+      // mapping so .debug_info and .debug_line references can be fixed up.
+      BinaryLineTable.mapFileIndex(I + Offset, OutputFileIndex);
     }
   }
 
diff --git a/bolt/lib/Core/BinaryEmitter.cpp b/bolt/lib/Core/BinaryEmitter.cpp
--- a/bolt/lib/Core/BinaryEmitter.cpp
+++ b/bolt/lib/Core/BinaryEmitter.cpp
@@ -684,6 +684,12 @@
     if (TargetUnitIndex != CurrentUnitIndex) {
       // Add filename from the inlined function to the current CU.
       TargetFilenum = BC.addDebugFilenameToUnit(
           TargetUnitIndex, CurrentUnitIndex, CurrentRow.File);
+    } else {
+      // Same CU: the file table may have been deduplicated when rebuilt, so
+      // translate the input file index to the output one.
+      TargetFilenum = BC.getDwarfLineTable(TargetUnitIndex)
+                          .getMappedFileIndex(TargetFilenum);
     }
     BC.Ctx-&gt;setCurrentDwarfLoc(TargetFilenum, CurrentRow.Line,
                                CurrentRow.Column, Flags, CurrentRow.Isa,
diff --git a/bolt/lib/Core/DebugData.cpp b/bolt/lib/Core/DebugData.cpp
--- a/bolt/lib/Core/DebugData.cpp
+++ b/bolt/lib/Core/DebugData.cpp
@@ -1001,7 +1001,8 @@
 static inline void emitBinaryDwarfLineTable(
     MCStreamer *MCOS, MCDwarfLineTableParams Params,
     const DWARFDebugLine::LineTable *Table,
-    const std::vector&lt;DwarfLineTable::RowSequence&gt; &amp;InputSequences) {
+    const std::vector&lt;DwarfLineTable::RowSequence&gt; &amp;InputSequences,
+    const DwarfLineTable &amp;DLT) {
   if (InputSequences.empty())
     return;
 
@@ -1044,7 +1045,7 @@
       if (FileNum != Row.File) {
         FileNum = Row.File;
         MCOS-&gt;emitInt8(dwarf::DW_LNS_set_file);
-        MCOS-&gt;emitULEB128IntValue(FileNum);
+        MCOS-&gt;emitULEB128IntValue(DLT.getMappedFileIndex(FileNum));
       }
 
diff --git a/bolt/lib/Core/DebugData.cpp b/bolt/lib/Core/DebugData.cpp
--- a/bolt/lib/Core/DebugData.cpp
+++ b/bolt/lib/Core/DebugData.cpp
@@ -1204,7 +1204,7 @@
 
   // Emit line tables for the original code.
-  emitBinaryDwarfLineTable(MCOS, Params, InputTable, InputSequences);
+  emitBinaryDwarfLineTable(MCOS, Params, InputTable, InputSequences, *this);
 
   // This is the end of the section, so set the value of the symbol at the end
diff --git a/bolt/lib/Core/DIEBuilder.cpp b/bolt/lib/Core/DIEBuilder.cpp
--- a/bolt/lib/Core/DIEBuilder.cpp
+++ b/bolt/lib/Core/DIEBuilder.cpp
@@ -927,7 +927,7 @@
 
 void DIEBuilder::cloneScalarAttribute(
-    DIE &amp;Die, const DWARFDie &amp;InputDIE,
+    DIE &amp;Die, const DWARFDie &amp;InputDIE, DWARFUnit &amp;U,
     const DWARFAbbreviationDeclaration::AttributeSpec AttrSpec,
     const DWARFFormValue &amp;Val) {
   uint64_t Value;
@@ -944,6 +944,17 @@
     return;
   }
 
+  // DW_AT_decl_file / DW_AT_call_file index the CU's line-table file list,
+  // which BOLT may deduplicate when rebuilding. Translate the input index to
+  // the output one. DW_FORM_implicit_const stores its value in the abbrev, not
+  // the DIE, so it is left untouched (not observed to be affected in practice).
+  if ((AttrSpec.Attr == dwarf::DW_AT_decl_file ||
+       AttrSpec.Attr == dwarf::DW_AT_call_file) &amp;&amp;
+      AttrSpec.Form != dwarf::DW_FORM_implicit_const)
+    Value =
+        BC.getDwarfLineTable(U.getOffset()).getMappedFileIndex(Value);
+
   Die.addValue(getState().DIEAlloc, AttrSpec.Attr, AttrSpec.Form,
                DIEInteger(Value));
 }
@@ -1024,7 +1035,7 @@
   case dwarf::DW_FORM_implicit_const:
-    cloneScalarAttribute(Die, InputDIE, AttrSpec, Val);
+    cloneScalarAttribute(Die, InputDIE, U, AttrSpec, Val);
     break;
   case dwarf::DW_FORM_loclistx:
     cloneLoclistAttrubute(Die, InputDIE, AttrSpec, Val);

Known limitation: DW_AT_decl_file encoded as DW_FORM_implicit_const stores its value in the abbreviation, not the DIE, and is left untouched.

Environment

  • llvm-project HEAD: bd6adfedc776c07caf158e59367d9c246c933510 (2026-06-30)
  • Aug-2025 DWARF file-index fixes #<!-- -->151401, #<!-- -->151230 already present.
  • Reproduced with the same main.cpp under both GCC and Clang.
  • Should be platform-agnostic (x86_64 and AArch64).

Hi

Follow-up on the last sentence of the patch

Known limitation: DW_AT_decl_file encoded as DW_FORM_implicit_const stores its value in the abbreviation, not the DIE, and is left untouched.

Measured on bd6adfe + the fix for #192084 (merged as c7c7cab). Without it the implicit_const values are zeroed during rewriting, so the 14 invalid indices do not show up and dropping the exclusion has no visible effect. The main.cpp from the issue body emits DW_AT_decl_file only as DW_FORM_data1, so the excluded path was never exercised:

llvm-dwarfdump --debug-info --show-form main | grep -oP 'DW_AT_(decl|call)_file\s+\[\K[^\]]+' | sort | uniq -c
      8 DW_FORM_data1

New reproducer that does emit implicit_const

Compiler: g++ (GCC) 14.2.0

widget.hpp

#pragma once
struct Flags {
  int a; int b; int c; int d; int e; int f; int g; int h;
};
struct More {
  int p; int q; int r; int s;
};

main.cpp

#include "widget.hpp"

#line 1 "main.cpp"

struct Flags gF;
struct More gM;

int main(int argc, char **argv) { return gF.a + gM.p + argc + (argv != 0); }
g++ -gdwarf-5 -O3 -gz=none main.cpp -o main
llvm-bolt main -o main.bolt --update-debug-sections

llvm-dwarfdump --verify main.bolt | grep -c 'invalid file index'
14

llvm-dwarfdump --debug-info --show-form main | grep -oP 'DW_AT_(decl|call)_file\s+\[\K[^\]]+' | sort | uniq -c
      3 DW_FORM_data1
     16 DW_FORM_implicit_const

Note that --verify catches only 14 of the 16 affected attributes. The other two (gF, gM) had index 1, which is still in range after the table shrinks, so they silently resolve to widget.hpp instead of main.cpp and no tool reports it:

llvm-dwarfdump --debug-info main | grep -A1 '("g[FM]")'
                DW_AT_name      ("gF")
                DW_AT_decl_file ("/workspace/main.cpp")
--
                DW_AT_name      ("gM")
                DW_AT_decl_file ("/workspace/main.cpp")

llvm-dwarfdump --debug-info main.bolt | grep -A1 '("g[FM]")'
                DW_AT_name      ("gF")
                DW_AT_decl_file ("/workspace/widget.hpp")
--
                DW_AT_name      ("gM")
                DW_AT_decl_file ("/workspace/widget.hpp")

Based on llvm/lib/CodeGen/AsmPrinter/DIE.cpp it looks like BOLT does not copy input abbrevs; it regenerates them, and the implicit_const value in the generated abbrev is taken from the value stored in the DIE:

DIEAbbrev DIE::generateAbbrev() const {
  DIEAbbrev Abbrev(Tag, hasChildren());
  for (const DIEValue &V : values())
    if (V.getForm() == dwarf::DW_FORM_implicit_const)
      Abbrev.AddImplicitConstAttribute(V.getAttribute(),
                                       V.getDIEInteger().getValue());

cloneScalarAttribute already stores the value through Die.addValue(..., AttrSpec.Form, DIEInteger(Value)), so writing the remapped index there propagates into the emitted abbrev and the form is preserved. Dropping the exclusion is therefore enough for implicit_const.

The same hunk has two more problems, fixed in the diff below as well:

  • Data race. BC.getDwarfLineTable(CUID) is DwarfLineTablesCUMap[CUID] (std::map::operator[]) and inserts for unknown keys, while cloneScalarAttribute runs concurrently with --debug-thread-count > 1. X86/dwarf5-df-types-debug-names.test then crashes intermittently in _Rb_tree_insert_and_rebalance.
  • Wrong key for split DWARF. The map is stored under the skeleton CU offset in .debug_info, but for a DWO unit U.getOffset() is its offset in .debug_info.dwo. Every .dwo starts at 0, so all split CUs use the first skeleton's map instead of their own. Depending on link order, a split CU is either left unmapped or remapped with another unit's map.

Diff relative to the patch proposed in this issue:

   void DIEBuilder::cloneScalarAttribute(
-  // DW_AT_decl_file / DW_AT_call_file index the CU's line-table file list,
-  // which BOLT may deduplicate when rebuilding. Translate the input index to
-  // the output one. DW_FORM_implicit_const stores its value in the abbrev, not
-  // the DIE, so it is left untouched (not observed to be affected in practice).
-  if ((AttrSpec.Attr == dwarf::DW_AT_decl_file ||
-       AttrSpec.Attr == dwarf::DW_AT_call_file) &&
-      AttrSpec.Form != dwarf::DW_FORM_implicit_const)
-    Value =
-        BC.getDwarfLineTable(U.getOffset()).getMappedFileIndex(Value);
+  // decl_file/call_file index the unit's line-table file list, which may be
+  // deduplicated. Split CUs use their skeleton's table; split type units use
+  // .debug_line.dwo, which is not rebuilt. find(): units are cloned in parallel.
+  // DW_FORM_implicit_const is covered too: the output abbrev uses this value.
+  if ((AttrSpec.Attr == dwarf::DW_AT_decl_file ||
+       AttrSpec.Attr == dwarf::DW_AT_call_file) &&
+      !(U.isDWOUnit() && U.isTypeUnit())) {
+    const DWARFUnit &LineUnit = U.isDWOUnit() && SkeletonCU ? *SkeletonCU : U;
+    const auto &LineTables = BC.getDwarfLineTables();
+    auto It = LineTables.find(LineUnit.getOffset());
+    if (It != LineTables.end())
+      Value = It->second.getMappedFileIndex(Value);
+  }

With that change llvm-dwarfdump --verify reports no invalid file index, all 19 file attributes (3 × data1, 16 × implicit_const) resolve to the same file as in the input, split DWARF CUs are remapped with their own skeleton's table regardless of link order, and the crash in X86/dwarf5-df-types-debug-names.test no longer reproduces. The limitation is therefore unnecessary - please include this change upstream.

not4juu avatar Aug 04 '26 16:08 not4juu