1 //===- DWARFLinkerCompileUnit.cpp -----------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/DWARFLinker/DWARFLinkerDeclContext.h"
12 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
13 #include "llvm/DebugInfo/DWARF/DWARFExpression.h"
14 #include "llvm/Support/FormatVariadic.h"
18 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
19 LLVM_DUMP_METHOD
void CompileUnit::DIEInfo::dump() {
20 llvm::errs() << "{\n";
21 llvm::errs() << " AddrAdjust: " << AddrAdjust
<< '\n';
22 llvm::errs() << " Ctxt: " << formatv("{0:x}", Ctxt
) << '\n';
23 llvm::errs() << " Clone: " << formatv("{0:x}", Clone
) << '\n';
24 llvm::errs() << " ParentIdx: " << ParentIdx
<< '\n';
25 llvm::errs() << " Keep: " << Keep
<< '\n';
26 llvm::errs() << " InDebugMap: " << InDebugMap
<< '\n';
27 llvm::errs() << " Prune: " << Prune
<< '\n';
28 llvm::errs() << " Incomplete: " << Incomplete
<< '\n';
29 llvm::errs() << " InModuleScope: " << InModuleScope
<< '\n';
30 llvm::errs() << " ODRMarkingDone: " << ODRMarkingDone
<< '\n';
31 llvm::errs() << " UnclonedReference: " << UnclonedReference
<< '\n';
32 llvm::errs() << "}\n";
34 #endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
36 /// Check if the DIE at \p Idx is in the scope of a function.
37 static bool inFunctionScope(CompileUnit
&U
, unsigned Idx
) {
39 if (U
.getOrigUnit().getDIEAtIndex(Idx
).getTag() == dwarf::DW_TAG_subprogram
)
41 Idx
= U
.getInfo(Idx
).ParentIdx
;
46 uint16_t CompileUnit::getLanguage() {
48 DWARFDie CU
= getOrigUnit().getUnitDIE();
49 Language
= dwarf::toUnsigned(CU
.find(dwarf::DW_AT_language
), 0);
54 StringRef
CompileUnit::getSysRoot() {
55 if (SysRoot
.empty()) {
56 DWARFDie CU
= getOrigUnit().getUnitDIE();
57 SysRoot
= dwarf::toStringRef(CU
.find(dwarf::DW_AT_LLVM_sysroot
)).str();
62 void CompileUnit::markEverythingAsKept() {
65 for (auto &I
: Info
) {
66 // Mark everything that wasn't explicit marked for pruning.
68 auto DIE
= OrigUnit
.getDIEAtIndex(Idx
++);
69 DWARFUnit
*U
= DIE
.getDwarfUnit();
71 // Try to guess which DIEs must go to the accelerator tables. We do that
72 // just for variables, because functions will be handled depending on
73 // whether they carry a DW_AT_low_pc attribute or not.
74 if (DIE
.getTag() != dwarf::DW_TAG_variable
&&
75 DIE
.getTag() != dwarf::DW_TAG_constant
)
78 std::optional
<DWARFFormValue
> Value
;
79 if (!(Value
= DIE
.find(dwarf::DW_AT_location
))) {
80 if ((Value
= DIE
.find(dwarf::DW_AT_const_value
)) &&
81 !inFunctionScope(*this, I
.ParentIdx
))
86 if (auto ExprLockBlock
= Value
->getAsBlock()) {
87 // Parse 'exprloc' expression.
88 DataExtractor
Data(toStringRef(*ExprLockBlock
),
89 U
->getContext().isLittleEndian(),
90 U
->getAddressByteSize());
91 DWARFExpression
Expression(Data
, U
->getAddressByteSize(),
92 U
->getFormParams().Format
);
94 for (DWARFExpression::iterator It
= Expression
.begin();
95 (It
!= Expression
.end()) && !I
.InDebugMap
; ++It
) {
96 DWARFExpression::iterator NextIt
= It
;
99 switch (It
->getCode()) {
100 case dwarf::DW_OP_const2u
:
101 case dwarf::DW_OP_const4u
:
102 case dwarf::DW_OP_const8u
:
103 case dwarf::DW_OP_const2s
:
104 case dwarf::DW_OP_const4s
:
105 case dwarf::DW_OP_const8s
:
106 if (NextIt
== Expression
.end() ||
107 NextIt
->getCode() != dwarf::DW_OP_form_tls_address
)
110 case dwarf::DW_OP_constx
:
111 case dwarf::DW_OP_addr
:
112 case dwarf::DW_OP_addrx
:
124 uint64_t CompileUnit::computeNextUnitOffset(uint16_t DwarfVersion
) {
125 NextUnitOffset
= StartOffset
;
127 NextUnitOffset
+= (DwarfVersion
>= 5) ? 12 : 11; // Header size
128 NextUnitOffset
+= NewUnit
->getUnitDie().getSize();
130 return NextUnitOffset
;
133 /// Keep track of a forward cross-cu reference from this unit
134 /// to \p Die that lives in \p RefUnit.
135 void CompileUnit::noteForwardReference(DIE
*Die
, const CompileUnit
*RefUnit
,
136 DeclContext
*Ctxt
, PatchLocation Attr
) {
137 ForwardDIEReferences
.emplace_back(Die
, RefUnit
, Ctxt
, Attr
);
140 void CompileUnit::fixupForwardReferences() {
141 for (const auto &Ref
: ForwardDIEReferences
) {
143 const CompileUnit
*RefUnit
;
146 std::tie(RefDie
, RefUnit
, Ctxt
, Attr
) = Ref
;
147 if (Ctxt
&& Ctxt
->hasCanonicalDIE()) {
148 assert(Ctxt
->getCanonicalDIEOffset() &&
149 "Canonical die offset is not set");
150 Attr
.set(Ctxt
->getCanonicalDIEOffset());
152 assert(RefDie
->getOffset() && "Referenced die offset is not set");
153 Attr
.set(RefDie
->getOffset() + RefUnit
->getStartOffset());
158 void CompileUnit::addLabelLowPc(uint64_t LabelLowPc
, int64_t PcOffset
) {
159 Labels
.insert({LabelLowPc
, PcOffset
});
162 void CompileUnit::addFunctionRange(uint64_t FuncLowPc
, uint64_t FuncHighPc
,
164 Ranges
.insert({FuncLowPc
, FuncHighPc
}, PcOffset
);
166 LowPc
= std::min(*LowPc
, FuncLowPc
+ PcOffset
);
168 LowPc
= FuncLowPc
+ PcOffset
;
169 this->HighPc
= std::max(HighPc
, FuncHighPc
+ PcOffset
);
172 void CompileUnit::noteRangeAttribute(const DIE
&Die
, PatchLocation Attr
) {
173 if (Die
.getTag() == dwarf::DW_TAG_compile_unit
) {
174 UnitRangeAttribute
= Attr
;
178 RangeAttributes
.emplace_back(Attr
);
181 void CompileUnit::noteLocationAttribute(PatchLocation Attr
) {
182 LocationAttributes
.emplace_back(Attr
);
185 void CompileUnit::addNamespaceAccelerator(const DIE
*Die
,
186 DwarfStringPoolEntryRef Name
) {
187 Namespaces
.emplace_back(Name
, Die
);
190 void CompileUnit::addObjCAccelerator(const DIE
*Die
,
191 DwarfStringPoolEntryRef Name
,
192 bool SkipPubSection
) {
193 ObjC
.emplace_back(Name
, Die
, SkipPubSection
);
196 void CompileUnit::addNameAccelerator(const DIE
*Die
,
197 DwarfStringPoolEntryRef Name
,
198 bool SkipPubSection
) {
199 Pubnames
.emplace_back(Name
, Die
, SkipPubSection
);
202 void CompileUnit::addTypeAccelerator(const DIE
*Die
,
203 DwarfStringPoolEntryRef Name
,
204 bool ObjcClassImplementation
,
205 uint32_t QualifiedNameHash
) {
206 Pubtypes
.emplace_back(Name
, Die
, QualifiedNameHash
, ObjcClassImplementation
);