1//===- DWARFLinkerCompileUnit.cpp -----------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h"
10#include "llvm/ADT/StringExtras.h"
11#include "llvm/DWARFLinker/Classic/DWARFLinkerDeclContext.h"
12#include "llvm/DebugInfo/DWARF/DWARFContext.h"
13#include "llvm/DebugInfo/DWARF/LowLevel/DWARFExpression.h"
14#include "llvm/Support/FormatVariadic.h"
15
16namespace llvm {
17
18using namespace dwarf_linker;
19using namespace dwarf_linker::classic;
20
21#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
22LLVM_DUMP_METHOD void CompileUnit::DIEInfo::dump() {
23 llvm::errs() << "{\n";
24 llvm::errs() << " AddrAdjust: " << AddrAdjust << '\n';
25 llvm::errs() << " Ctxt: " << formatv("{0:x}", Ctxt) << '\n';
26 llvm::errs() << " Clone: " << formatv("{0:x}", Clone) << '\n';
27 llvm::errs() << " ParentIdx: " << ParentIdx << '\n';
28 llvm::errs() << " Keep: " << Keep << '\n';
29 llvm::errs() << " InDebugMap: " << InDebugMap << '\n';
30 llvm::errs() << " Prune: " << Prune << '\n';
31 llvm::errs() << " Incomplete: " << Incomplete << '\n';
32 llvm::errs() << " InModuleScope: " << InModuleScope << '\n';
33 llvm::errs() << " ODRMarkingDone: " << ODRMarkingDone << '\n';
34 llvm::errs() << " UnclonedReference: " << UnclonedReference << '\n';
35 llvm::errs() << "}\n";
36}
37#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
38
39/// Check if the DIE at \p Idx is in the scope of a function.
40static bool inFunctionScope(CompileUnit &U, unsigned Idx) {
41 while (Idx) {
42 if (U.getOrigUnit().getDIEAtIndex(Index: Idx).getTag() == dwarf::DW_TAG_subprogram)
43 return true;
44 Idx = U.getInfo(Idx).ParentIdx;
45 }
46 return false;
47}
48
49uint16_t CompileUnit::getLanguage() {
50 if (!Language) {
51 DWARFDie CU = getOrigUnit().getUnitDIE();
52 Language = dwarf::toUnsigned(V: CU.find(Attr: dwarf::DW_AT_language), Default: 0);
53 }
54 return Language;
55}
56
57StringRef CompileUnit::getSysRoot() {
58 if (SysRoot.empty()) {
59 DWARFDie CU = getOrigUnit().getUnitDIE();
60 SysRoot = dwarf::toStringRef(V: CU.find(Attr: dwarf::DW_AT_LLVM_sysroot)).str();
61 }
62 return SysRoot;
63}
64
65void CompileUnit::markEverythingAsKept() {
66 unsigned Idx = 0;
67
68 for (auto &I : Info) {
69 // Mark everything that wasn't explicit marked for pruning.
70 I.Keep = !I.Prune;
71 auto DIE = OrigUnit.getDIEAtIndex(Index: Idx++);
72 DWARFUnit *U = DIE.getDwarfUnit();
73
74 // Try to guess which DIEs must go to the accelerator tables. We do that
75 // just for variables, because functions will be handled depending on
76 // whether they carry a DW_AT_low_pc attribute or not.
77 if (DIE.getTag() != dwarf::DW_TAG_variable &&
78 DIE.getTag() != dwarf::DW_TAG_constant)
79 continue;
80
81 std::optional<DWARFFormValue> Value;
82 if (!(Value = DIE.find(Attr: dwarf::DW_AT_location))) {
83 if ((Value = DIE.find(Attr: dwarf::DW_AT_const_value)) &&
84 !inFunctionScope(U&: *this, Idx: I.ParentIdx))
85 I.InDebugMap = true;
86 continue;
87 }
88
89 if (auto ExprLockBlock = Value->getAsBlock()) {
90 // Parse 'exprloc' expression.
91 DataExtractor Data(*ExprLockBlock, U->getContext().isLittleEndian());
92 DWARFExpression Expression(Data, U->getAddressByteSize(),
93 U->getFormParams().Format);
94
95 for (DWARFExpression::iterator It = Expression.begin();
96 (It != Expression.end()) && !I.InDebugMap; ++It) {
97 DWARFExpression::iterator NextIt = It;
98 ++NextIt;
99
100 switch (It->getCode()) {
101 case dwarf::DW_OP_const2u:
102 case dwarf::DW_OP_const4u:
103 case dwarf::DW_OP_const8u:
104 case dwarf::DW_OP_const2s:
105 case dwarf::DW_OP_const4s:
106 case dwarf::DW_OP_const8s:
107 if (NextIt == Expression.end() ||
108 !dwarf::isTlsAddressOp(O: NextIt->getCode()))
109 break;
110 [[fallthrough]];
111 case dwarf::DW_OP_constx:
112 case dwarf::DW_OP_addr:
113 case dwarf::DW_OP_addrx:
114 I.InDebugMap = true;
115 break;
116 default:
117 // Nothing to do.
118 break;
119 }
120 }
121 }
122 }
123}
124
125uint64_t CompileUnit::computeNextUnitOffset(uint16_t DwarfVersion) {
126 NextUnitOffset = StartOffset;
127 if (NewUnit) {
128 NextUnitOffset += (DwarfVersion >= 5) ? 12 : 11; // Header size
129 NextUnitOffset += NewUnit->getUnitDie().getSize();
130 }
131 return NextUnitOffset;
132}
133
134/// Keep track of a forward cross-cu reference from this unit
135/// to \p Die that lives in \p RefUnit.
136void CompileUnit::noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
137 DeclContext *Ctxt, PatchLocation Attr) {
138 ForwardDIEReferences.emplace_back(args&: Die, args&: RefUnit, args&: Ctxt, args&: Attr);
139}
140
141void CompileUnit::fixupForwardReferences() {
142 for (const auto &Ref : ForwardDIEReferences) {
143 DIE *RefDie;
144 const CompileUnit *RefUnit;
145 PatchLocation Attr;
146 DeclContext *Ctxt;
147 std::tie(args&: RefDie, args&: RefUnit, args&: Ctxt, args&: Attr) = Ref;
148 if (Ctxt && Ctxt->hasCanonicalDIE()) {
149 assert(Ctxt->getCanonicalDIEOffset() &&
150 "Canonical die offset is not set");
151 Attr.set(Ctxt->getCanonicalDIEOffset());
152 } else {
153 assert(RefDie->getOffset() && "Referenced die offset is not set");
154 Attr.set(RefDie->getOffset() + RefUnit->getStartOffset());
155 }
156 }
157}
158
159void CompileUnit::addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset) {
160 Labels.insert(KV: {LabelLowPc, PcOffset});
161}
162
163void CompileUnit::addFunctionRange(uint64_t FuncLowPc, uint64_t FuncHighPc,
164 int64_t PcOffset) {
165 Ranges.insert(Range: {FuncLowPc, FuncHighPc}, Value: PcOffset);
166 if (LowPc)
167 LowPc = std::min(a: *LowPc, b: FuncLowPc + PcOffset);
168 else
169 LowPc = FuncLowPc + PcOffset;
170 this->HighPc = std::max(a: HighPc, b: FuncHighPc + PcOffset);
171}
172
173void CompileUnit::noteRangeAttribute(const DIE &Die, PatchLocation Attr) {
174 if (Die.getTag() == dwarf::DW_TAG_compile_unit) {
175 UnitRangeAttribute = Attr;
176 return;
177 }
178
179 RangeAttributes.emplace_back(Args&: Attr);
180}
181
182void CompileUnit::noteLocationAttribute(PatchLocation Attr) {
183 LocationAttributes.emplace_back(Args&: Attr);
184}
185
186void CompileUnit::noteStmtSeqListAttribute(PatchLocation Attr) {
187 StmtSeqListAttributes.emplace_back(Args&: Attr);
188}
189
190void CompileUnit::addNamespaceAccelerator(const DIE *Die,
191 DwarfStringPoolEntryRef Name) {
192 Namespaces.emplace_back(args&: Name, args&: Die);
193}
194
195void CompileUnit::addObjCAccelerator(const DIE *Die,
196 DwarfStringPoolEntryRef Name,
197 bool SkipPubSection) {
198 ObjC.emplace_back(args&: Name, args&: Die, args&: SkipPubSection);
199}
200
201void CompileUnit::addNameAccelerator(const DIE *Die,
202 DwarfStringPoolEntryRef Name,
203 bool SkipPubSection) {
204 Pubnames.emplace_back(args&: Name, args&: Die, args&: SkipPubSection);
205}
206
207void CompileUnit::addTypeAccelerator(const DIE *Die,
208 DwarfStringPoolEntryRef Name,
209 bool ObjcClassImplementation,
210 uint32_t QualifiedNameHash) {
211 Pubtypes.emplace_back(args&: Name, args&: Die, args&: QualifiedNameHash, args&: ObjcClassImplementation);
212}
213
214} // namespace llvm
215