Skip to content

Commit 0860211

Browse files
committed
[CLANG][DWARF] Handle DIE offset collision in DW_IDX_parent
This fixes llvm#93886. The UnitID is not unique between CUs and TUs. This led to DW_IDX_parent to point ot an entry for a DIE in CU if it had the same relative offset as TU die. Added a IsTU to the hash for parent chain.
1 parent a91c839 commit 0860211

File tree

7 files changed

+120
-21
lines changed

7 files changed

+120
-21
lines changed

llvm/include/llvm/CodeGen/AccelTable.h

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,33 @@ class AppleAccelTableData : public AccelTableData {
257257

258258
/// Helper class to identify an entry in DWARF5AccelTable based on their DIE
259259
/// offset and UnitID.
260-
struct OffsetAndUnitID : std::pair<uint64_t, uint32_t> {
261-
using Base = std::pair<uint64_t, uint32_t>;
262-
OffsetAndUnitID(Base B) : Base(B) {}
263-
264-
OffsetAndUnitID(uint64_t Offset, uint32_t UnitID) : Base(Offset, UnitID) {}
265-
uint64_t offset() const { return first; };
266-
uint32_t unitID() const { return second; };
260+
struct OffsetAndUnitID {
261+
uint64_t Offset = 0;
262+
uint32_t UnitID = 0;
263+
bool IsTU = false;
264+
OffsetAndUnitID() = delete;
265+
OffsetAndUnitID(uint64_t Offset, uint32_t UnitID, bool IsTU)
266+
: Offset(Offset), UnitID(UnitID), IsTU(IsTU) {}
267+
uint64_t offset() const { return Offset; };
268+
uint32_t unitID() const { return UnitID; };
269+
bool isTU() const { return IsTU; }
267270
};
268271

269-
template <>
270-
struct DenseMapInfo<OffsetAndUnitID> : DenseMapInfo<OffsetAndUnitID::Base> {};
272+
template <> struct DenseMapInfo<OffsetAndUnitID> {
273+
static inline OffsetAndUnitID getEmptyKey() {
274+
return OffsetAndUnitID(-1, -1, false);
275+
}
276+
static inline OffsetAndUnitID getTombstoneKey() {
277+
return OffsetAndUnitID(-2, -2, false);
278+
}
279+
static unsigned getHashValue(const OffsetAndUnitID &Val) {
280+
return (unsigned)llvm::hash_combine(Val.offset(), Val.unitID(), Val.IsTU);
281+
}
282+
static bool isEqual(const OffsetAndUnitID &LHS, const OffsetAndUnitID &RHS) {
283+
return LHS.offset() == RHS.offset() && LHS.unitID() == RHS.unitID() &&
284+
LHS.IsTU == RHS.isTU();
285+
}
286+
};
271287

272288
/// The Data class implementation for DWARF v5 accelerator table. Unlike the
273289
/// Apple Data classes, this class is just a DIE wrapper, and does not know to
@@ -277,12 +293,11 @@ class DWARF5AccelTableData : public AccelTableData {
277293
public:
278294
static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }
279295

280-
DWARF5AccelTableData(const DIE &Die, const uint32_t UnitID,
281-
const bool IsTU = false);
296+
DWARF5AccelTableData(const DIE &Die, const uint32_t UnitID, const bool IsTU);
282297
DWARF5AccelTableData(const uint64_t DieOffset,
283298
const std::optional<uint64_t> DefiningParentOffset,
284299
const unsigned DieTag, const unsigned UnitID,
285-
const bool IsTU = false)
300+
const bool IsTU)
286301
: OffsetVal(DieOffset), ParentOffset(DefiningParentOffset),
287302
DieTag(DieTag), AbbrevNumber(0), IsTU(IsTU), UnitID(UnitID) {}
288303

@@ -296,7 +311,7 @@ class DWARF5AccelTableData : public AccelTableData {
296311
}
297312

298313
OffsetAndUnitID getDieOffsetAndUnitID() const {
299-
return {getDieOffset(), UnitID};
314+
return {getDieOffset(), getUnitID(), isTU()};
300315
}
301316

302317
unsigned getDieTag() const { return DieTag; }
@@ -322,7 +337,7 @@ class DWARF5AccelTableData : public AccelTableData {
322337
assert(isNormalized() && "Accessing DIE Offset before normalizing.");
323338
if (!ParentOffset)
324339
return std::nullopt;
325-
return OffsetAndUnitID(*ParentOffset, getUnitID());
340+
return OffsetAndUnitID(*ParentOffset, getUnitID(), isTU());
326341
}
327342

328343
/// Sets AbbrevIndex for an Entry.
@@ -416,7 +431,7 @@ class DWARF5AccelTable : public AccelTable<DWARF5AccelTableData> {
416431
for (auto *Data : Entry.second.getValues<DWARF5AccelTableData *>()) {
417432
addName(Entry.second.Name, Data->getDieOffset(),
418433
Data->getParentDieOffset(), Data->getDieTag(),
419-
Data->getUnitID(), true);
434+
Data->getUnitID(), Data->isTU());
420435
}
421436
}
422437
}

llvm/include/llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class CompileUnit {
137137
return nullptr;
138138
}
139139

140+
dwarf::Tag getTag() const { return OrigUnit.getUnitDIE().getTag(); }
141+
140142
bool hasODR() const { return HasODR; }
141143
bool isClangModule() const { return !ClangModuleName.empty(); }
142144
uint16_t getLanguage();

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3592,7 +3592,8 @@ void DwarfDebug::addAccelNameImpl(
35923592
"Kind is TU but CU is being processed.");
35933593
// The type unit can be discarded, so need to add references to final
35943594
// acceleration table once we know it's complete and we emit it.
3595-
Current.addName(Ref, Die, Unit.getUniqueID());
3595+
Current.addName(Ref, Die, Unit.getUniqueID(),
3596+
Unit.getUnitDie().getTag() == dwarf::DW_TAG_type_unit);
35963597
break;
35973598
}
35983599
case AccelTableKind::Default:

llvm/lib/DWARFLinker/Classic/DWARFLinker.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,17 +2247,20 @@ void DWARFLinker::emitAcceleratorEntriesForUnit(CompileUnit &Unit) {
22472247
DebugNames.addName(
22482248
Namespace.Name, Namespace.Die->getOffset(),
22492249
DWARF5AccelTableData::getDefiningParentDieOffset(*Namespace.Die),
2250-
Namespace.Die->getTag(), Unit.getUniqueID());
2250+
Namespace.Die->getTag(), Unit.getUniqueID(),
2251+
Unit.getTag() == dwarf::DW_TAG_type_unit);
22512252
for (const auto &Pubname : Unit.getPubnames())
22522253
DebugNames.addName(
22532254
Pubname.Name, Pubname.Die->getOffset(),
22542255
DWARF5AccelTableData::getDefiningParentDieOffset(*Pubname.Die),
2255-
Pubname.Die->getTag(), Unit.getUniqueID());
2256+
Pubname.Die->getTag(), Unit.getUniqueID(),
2257+
Unit.getTag() == dwarf::DW_TAG_type_unit);
22562258
for (const auto &Pubtype : Unit.getPubtypes())
22572259
DebugNames.addName(
22582260
Pubtype.Name, Pubtype.Die->getOffset(),
22592261
DWARF5AccelTableData::getDefiningParentDieOffset(*Pubtype.Die),
2260-
Pubtype.Die->getTag(), Unit.getUniqueID());
2262+
Pubtype.Die->getTag(), Unit.getUniqueID(),
2263+
Unit.getTag() == dwarf::DW_TAG_type_unit);
22612264
} break;
22622265
}
22632266
}

llvm/lib/DWARFLinker/Parallel/DWARFLinkerImpl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1358,7 +1358,8 @@ void DWARFLinkerImpl::emitDWARFv5DebugNamesSection(const Triple &TargetTriple) {
13581358
case DwarfUnit::AccelType::Type: {
13591359
DebugNames->addName(*DebugStrStrings.getExistingEntry(Info.String),
13601360
Info.OutOffset, std::nullopt /*ParentDIEOffset*/,
1361-
Info.Tag, CU->getUniqueID());
1361+
Info.Tag, CU->getUniqueID(),
1362+
CU->getTag() == dwarf::DW_TAG_type_unit);
13621363
} break;
13631364

13641365
default:

llvm/lib/DWARFLinker/Parallel/DWARFLinkerUnit.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@ class DwarfUnit : public OutputSections {
7777
void setOutUnitDIE(DIE *UnitDie) {
7878
OutUnitDIE = UnitDie;
7979

80-
if (OutUnitDIE != nullptr)
80+
if (OutUnitDIE != nullptr) {
8181
UnitSize = getDebugInfoHeaderSize() + OutUnitDIE->getSize();
82+
UnitTag = OutUnitDIE->getTag();
83+
}
8284
}
8385

86+
/// Returns unit DWARF tag.
87+
dwarf::Tag getTag() const { return UnitTag; }
88+
8489
/// \defgroup Methods used to emit unit's debug info:
8590
///
8691
/// @{
@@ -180,6 +185,9 @@ class DwarfUnit : public OutputSections {
180185

181186
uint64_t UnitSize = 0;
182187

188+
/// DWARF unit tag.
189+
dwarf::Tag UnitTag = dwarf::DW_TAG_null;
190+
183191
/// true if current unit references_to/is_referenced by other unit.
184192
std::atomic<bool> IsInterconnectedCU = {false};
185193

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
; UNSUPPORTED: system-windows
2+
3+
;; This test checks that DW_IDX_parent is generated correctly when there is DIE relative offset collision between CU and TU.
4+
5+
; RUN: llc -mtriple=x86_64 -generate-type-units -dwarf-version=5 -filetype=obj %s -o %t
6+
; RUN: llvm-dwarfdump -debug-info -debug-names %t | FileCheck %s
7+
8+
; CHECK: .debug_info contents:
9+
; CHECK: 0x00000023: DW_TAG_namespace
10+
; CHECK-NEXT: DW_AT_name ("B")
11+
; CHECK: 0x00000023: DW_TAG_subprogram
12+
; CHECK-NEXT: DW_AT_low_pc
13+
; CHECK-NEXT: DW_AT_high_pc
14+
; CHECK-NEXT: DW_AT_frame_base
15+
; CHECK-NEXT: DW_AT_linkage_name ("_Z9get_statev")
16+
; CHECK-NEXT: DW_AT_name ("get_state")
17+
18+
; CHECK: .debug_names contents:
19+
; CHECK: String: {{.*}} "B"
20+
; CHECK: Entry @ [[ENTRY:0x[0-9a-f]*]]
21+
; CHECK: String: {{.*}} "State"
22+
; CHECK: Entry @ 0xd3 {
23+
; CHECK: Abbrev: 0x4
24+
; CHECK: Tag: DW_TAG_structure_type
25+
; CHECK: DW_IDX_type_unit: 0x00
26+
; CHECK: DW_IDX_die_offset: 0x00000025
27+
; CHECK: DW_IDX_parent: Entry @ [[ENTRY:0x[0-9a-f]*]]
28+
; CHECK: }
29+
30+
31+
;; namespace B { struct State { class InnerState{}; }; }
32+
;; B::State::InnerState get_state() { return B::State::InnerState(); }
33+
;; clang++ main.cpp -g2 -O0 -fdebug-types-section -gpubnames
34+
35+
; ModuleID = 'main.cpp'
36+
source_filename = "main.cpp"
37+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
38+
target triple = "x86_64-unknown-linux-gnu"
39+
40+
; Function Attrs: mustprogress noinline nounwind optnone uwtable
41+
define dso_local void @_Z9get_statev() #0 !dbg !10 {
42+
entry:
43+
ret void, !dbg !17
44+
}
45+
46+
attributes #0 = { mustprogress noinline nounwind optnone uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
47+
48+
!llvm.dbg.cu = !{!0}
49+
!llvm.module.flags = !{!2, !3, !4, !5, !6, !7, !8}
50+
!llvm.ident = !{!9}
51+
52+
!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 19.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false)
53+
!1 = !DIFile(filename: "main.cpp", directory: "/folder", checksumkind: CSK_MD5, checksum: "a84fe2e4ecb77633f6c33f3b6833b9e7")
54+
!2 = !{i32 7, !"Dwarf Version", i32 5}
55+
!3 = !{i32 2, !"Debug Info Version", i32 3}
56+
!4 = !{i32 1, !"wchar_size", i32 4}
57+
!5 = !{i32 8, !"PIC Level", i32 2}
58+
!6 = !{i32 7, !"PIE Level", i32 2}
59+
!7 = !{i32 7, !"uwtable", i32 2}
60+
!8 = !{i32 7, !"frame-pointer", i32 2}
61+
!9 = !{!"clang version 19.0.0git"}
62+
!10 = distinct !DISubprogram(name: "get_state", linkageName: "_Z9get_statev", scope: !1, file: !1, line: 2, type: !11, scopeLine: 2, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
63+
!11 = !DISubroutineType(types: !12)
64+
!12 = !{!13}
65+
!13 = distinct !DICompositeType(tag: DW_TAG_class_type, name: "InnerState", scope: !14, file: !1, line: 1, size: 8, flags: DIFlagTypePassByValue, elements: !16, identifier: "_ZTSN1B5State10InnerStateE")
66+
!14 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "State", scope: !15, file: !1, line: 1, size: 8, flags: DIFlagTypePassByValue, elements: !16, identifier: "_ZTSN1B5StateE")
67+
!15 = !DINamespace(name: "B", scope: null)
68+
!16 = !{}
69+
!17 = !DILocation(line: 2, column: 36, scope: !10)

0 commit comments

Comments
 (0)