LLVM 22.0.0git
DWARFLinkerCompileUnit.h
Go to the documentation of this file.
1//===- DWARFLinkerCompileUnit.h ---------------------------------*- C++ -*-===//
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#ifndef LLVM_DWARFLINKER_CLASSIC_DWARFLINKERCOMPILEUNIT_H
10#define LLVM_DWARFLINKER_CLASSIC_DWARFLINKERCOMPILEUNIT_H
11
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/CodeGen/DIE.h"
17#include <optional>
18
19namespace llvm {
20namespace dwarf_linker {
21namespace classic {
22
23class DeclContext;
24
25/// Mapped value in the address map is the offset to apply to the
26/// linked address.
28
29// This structure keeps patch for the attribute and, optionally,
30// the value of relocation which should be applied. Currently,
31// only location attribute needs to have relocation: either to the
32// function ranges if location attribute is of type 'loclist',
33// either to the operand of DW_OP_addr/DW_OP_addrx if location attribute
34// is of type 'exprloc'.
35// ASSUMPTION: Location attributes of 'loclist' type containing 'exprloc'
36// with address expression operands are not supported yet.
39 int64_t RelocAdjustment = 0;
40
41 PatchLocation() = default;
45
46 void set(uint64_t New) const {
47 assert(I);
48 const auto &Old = *I;
49 assert(Old.getType() == DIEValue::isInteger);
50 *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
51 }
52
53 uint64_t get() const {
54 assert(I);
55 return I->getDIEInteger().getValue();
56 }
57};
58
62
63/// Stores all information relating to a compile unit, be it in its original
64/// instance in the object file to its brand new cloned and generated DIE tree.
66public:
67 /// Information gathered about a DIE in the object file.
68 struct DIEInfo {
69 /// Address offset to apply to the described entity.
70 int64_t AddrAdjust;
71
72 /// ODR Declaration context.
74
75 /// Cloned version of that DIE.
77
78 /// The index of this DIE's parent.
80
81 /// Is the DIE part of the linked output?
82 bool Keep : 1;
83
84 /// Was this DIE's entity found in the map?
85 bool InDebugMap : 1;
86
87 /// Is this a pure forward declaration we can strip?
88 bool Prune : 1;
89
90 /// Does DIE transitively refer an incomplete decl?
91 bool Incomplete : 1;
92
93 /// Is DIE in the clang module scope?
94 bool InModuleScope : 1;
95
96 /// Is ODR marking done?
98
99 /// Is this a reference to a DIE that hasn't been cloned yet?
101
102 /// Is this a variable with a location attribute referencing address?
104
105#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
106 LLVM_DUMP_METHOD void dump();
107#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
108 };
109
110 CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
111 StringRef ClangModuleName)
112 : OrigUnit(OrigUnit), ID(ID), ClangModuleName(ClangModuleName) {
113 Info.resize(OrigUnit.getNumDIEs());
114
115 auto CUDie = OrigUnit.getUnitDIE(false);
116 if (!CUDie) {
117 HasODR = false;
118 return;
119 }
120 if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
121 HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
122 *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
123 *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
124 *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
125 *Lang == dwarf::DW_LANG_ObjC_plus_plus);
126 else
127 HasODR = false;
128 }
129
130 DWARFUnit &getOrigUnit() const { return OrigUnit; }
131
132 unsigned getUniqueID() const { return ID; }
133
134 void createOutputDIE() { NewUnit.emplace(OrigUnit.getUnitDIE().getTag()); }
135
137 if (NewUnit)
138 return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
139 return nullptr;
140 }
141
142 dwarf::Tag getTag() const { return OrigUnit.getUnitDIE().getTag(); }
143
144 bool hasODR() const { return HasODR; }
145 bool isClangModule() const { return !ClangModuleName.empty(); }
147 /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
149
150 const std::string &getClangModuleName() const { return ClangModuleName; }
151
152 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
153 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
154
155 DIEInfo &getInfo(const DWARFDie &Die) {
156 unsigned Idx = getOrigUnit().getDIEIndex(Die);
157 return Info[Idx];
158 }
159
160 uint64_t getStartOffset() const { return StartOffset; }
161 uint64_t getNextUnitOffset() const { return NextUnitOffset; }
163
164 std::optional<uint64_t> getLowPc() const { return LowPc; }
165 uint64_t getHighPc() const { return HighPc; }
166 bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
167
168 const RangesTy &getFunctionRanges() const { return Ranges; }
169
170 const RngListAttributesTy &getRangesAttributes() { return RangeAttributes; }
171
172 std::optional<PatchLocation> getUnitRangesAttribute() const {
173 return UnitRangeAttribute;
174 }
175
177 return LocationAttributes;
178 }
179
180 // Provide access to the list of DW_AT_LLVM_stmt_sequence attributes that may
181 // need to be patched.
183 return StmtSeqListAttributes;
184 }
185
186 /// Mark every DIE in this unit as kept. This function also
187 /// marks variables as InDebugMap so that they appear in the
188 /// reconstructed accelerator tables.
190
191 /// Compute the end offset for this unit. Must be called after the CU's DIEs
192 /// have been cloned. \returns the next unit offset (which is also the
193 /// current debug_info section size).
195
196 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
197 /// Attr. The attribute should be fixed up later to point to the absolute
198 /// offset of \p Die in the debug_info section or to the canonical offset of
199 /// \p Ctxt if it is non-null.
200 LLVM_ABI void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
201 DeclContext *Ctxt, PatchLocation Attr);
202
203 /// Apply all fixups recorded by noteForwardReference().
205
206 /// Add the low_pc of a label that is relocated by applying
207 /// offset \p PCOffset.
208 LLVM_ABI void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
209
210 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
211 /// offset \p PCOffset.
212 LLVM_ABI void addFunctionRange(uint64_t LowPC, uint64_t HighPC,
213 int64_t PCOffset);
214
215 /// Keep track of a DW_AT_range attribute that we will need to patch up later.
216 LLVM_ABI void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
217
218 /// Keep track of a location attribute pointing to a location list in the
219 /// debug_loc section.
221
222 // Record that the given DW_AT_LLVM_stmt_sequence attribute may need to be
223 // patched later.
225
226 /// Add a name accelerator entry for \a Die with \a Name.
227 LLVM_ABI void addNamespaceAccelerator(const DIE *Die,
229
230 /// Add a name accelerator entry for \a Die with \a Name.
232 bool SkipPubnamesSection = false);
233
234 /// Add various accelerator entries for \p Die with \p Name which is stored
235 /// in the string table at \p Offset. \p Name must be an Objective-C
236 /// selector.
238 bool SkipPubnamesSection = false);
239
240 /// Add a type accelerator entry for \p Die with \p Name which is stored in
241 /// the string table at \p Offset.
243 bool ObjcClassImplementation,
244 uint32_t QualifiedNameHash);
245
246 struct AccelInfo {
247 /// Name of the entry.
249
250 /// DIE this entry describes.
251 const DIE *Die;
252
253 /// Hash of the fully qualified name.
255
256 /// Emit this entry only in the apple_* sections.
258
259 /// Is this an ObjC class implementation?
261
265
267 uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
270 ObjcClassImplementation(ObjCClassIsImplementation) {}
271 };
272
273 const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
274 const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
275 const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
276 const std::vector<AccelInfo> &getObjC() const { return ObjC; }
277
278 MCSymbol *getLabelBegin() { return LabelBegin; }
279 void setLabelBegin(MCSymbol *S) { LabelBegin = S; }
280
281private:
282 DWARFUnit &OrigUnit;
283 unsigned ID;
284 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
285 std::optional<BasicDIEUnit> NewUnit;
286 MCSymbol *LabelBegin = nullptr;
287
288 uint64_t StartOffset;
289 uint64_t NextUnitOffset;
290
291 std::optional<uint64_t> LowPc;
292 uint64_t HighPc = 0;
293
294 /// A list of attributes to fixup with the absolute offset of
295 /// a DIE in the debug_info section.
296 ///
297 /// The offsets for the attributes in this array couldn't be set while
298 /// cloning because for cross-cu forward references the target DIE's offset
299 /// isn't known you emit the reference attribute.
300 std::vector<
301 std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
302 ForwardDIEReferences;
303
304 /// The ranges in that map are the PC ranges for functions in this unit,
305 /// associated with the PC offset to apply to the addresses to get
306 /// the linked address.
307 RangesTy Ranges;
308
309 /// The DW_AT_low_pc of each DW_TAG_label.
311
312 /// 'rnglist'(DW_AT_ranges, DW_AT_start_scope) attributes to patch after
313 /// we have gathered all the unit's function addresses.
314 /// @{
315 RngListAttributesTy RangeAttributes;
316 std::optional<PatchLocation> UnitRangeAttribute;
317 /// @}
318
319 /// Location attributes that need to be transferred from the
320 /// original debug_loc section to the linked one. They are stored
321 /// along with the PC offset that is to be applied to their
322 /// function's address or to be applied to address operands of
323 /// location expression.
324 LocListAttributesTy LocationAttributes;
325
326 // List of DW_AT_LLVM_stmt_sequence attributes that may need to be patched
327 // after the dwarf linker rewrites the line table. During line table rewrite
328 // the line table format might change, so we have to patch any offsets that
329 // reference its contents.
330 StmtSeqListAttributesTy StmtSeqListAttributes;
331
332 /// Accelerator entries for the unit, both for the pub*
333 /// sections and the apple* ones.
334 /// @{
335 std::vector<AccelInfo> Pubnames;
336 std::vector<AccelInfo> Pubtypes;
337 std::vector<AccelInfo> Namespaces;
338 std::vector<AccelInfo> ObjC;
339 /// @}
340
341 /// Is this unit subject to the ODR rule?
342 bool HasODR;
343
344 /// The DW_AT_language of this unit.
345 uint16_t Language = 0;
346
347 /// The DW_AT_LLVM_sysroot of this unit.
348 std::string SysRoot;
349
350 /// If this is a Clang module, this holds the module's name.
351 std::string ClangModuleName;
352};
353
354} // end of namespace classic
355} // end of namespace dwarf_linker
356} // end of namespace llvm
357
358#endif // LLVM_DWARFLINKER_CLASSIC_DWARFLINKERCOMPILEUNIT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#define LLVM_ABI
Definition Compiler.h:213
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
This file defines the DenseMap class.
if(PassOpts->AAPipeline)
AddressRangesMap class maps values to the address ranges.
An integer value DIE.
Definition DIE.h:169
DIE & getUnitDie()
Definition DIE.h:1009
A structured debug information entry.
Definition DIE.h:828
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) const
Return the index of a Die entry inside the unit's DIE vector.
Definition DWARFUnit.h:273
DwarfStringPoolEntryRef: Dwarf string pool entry reference.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Stores all information relating to a compile unit, be it in its original instance in the object file ...
const std::vector< AccelInfo > & getNamespaces() const
const std::vector< AccelInfo > & getPubnames() const
LLVM_ABI void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset)
Add a function range [LowPC, HighPC) that is relocated by applying offset PCOffset.
LLVM_ABI void noteStmtSeqListAttribute(PatchLocation Attr)
const std::vector< AccelInfo > & getPubtypes() const
const std::vector< AccelInfo > & getObjC() const
LLVM_ABI void fixupForwardReferences()
Apply all fixups recorded by noteForwardReference().
const DIEInfo & getInfo(unsigned Idx) const
LLVM_ABI void noteForwardReference(DIE *Die, const CompileUnit *RefUnit, DeclContext *Ctxt, PatchLocation Attr)
Keep track of a forward reference to DIE Die in RefUnit by Attr.
LLVM_ABI void noteLocationAttribute(PatchLocation Attr)
Keep track of a location attribute pointing to a location list in the debug_loc section.
std::optional< uint64_t > getLowPc() const
LLVM_ABI void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool SkipPubnamesSection=false)
Add a name accelerator entry for Die with Name.
const StmtSeqListAttributesTy & getStmtSeqListAttributes() const
LLVM_ABI void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name)
Add a name accelerator entry for Die with Name.
LLVM_ABI StringRef getSysRoot()
Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
std::optional< PatchLocation > getUnitRangesAttribute() const
LLVM_ABI void noteRangeAttribute(const DIE &Die, PatchLocation Attr)
Keep track of a DW_AT_range attribute that we will need to patch up later.
LLVM_ABI void addObjCAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool SkipPubnamesSection=false)
Add various accelerator entries for Die with Name which is stored in the string table at Offset.
LLVM_ABI void addTypeAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool ObjcClassImplementation, uint32_t QualifiedNameHash)
Add a type accelerator entry for Die with Name which is stored in the string table at Offset.
const LocListAttributesTy & getLocationAttributes() const
LLVM_ABI uint64_t computeNextUnitOffset(uint16_t DwarfVersion)
Compute the end offset for this unit.
LLVM_ABI void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset)
Add the low_pc of a label that is relocated by applying offset PCOffset.
LLVM_ABI void markEverythingAsKept()
Mark every DIE in this unit as kept.
CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR, StringRef ClangModuleName)
const RngListAttributesTy & getRangesAttributes()
A DeclContext is a named program scope that is used for ODR uniquing of types.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
SmallVector< PatchLocation > RngListAttributesTy
SmallVector< PatchLocation > StmtSeqListAttributesTy
AddressRangesMap RangesTy
Mapped value in the address map is the offset to apply to the linked address.
SmallVector< PatchLocation > LocListAttributesTy
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
This is an optimization pass for GlobalISel generic memory operations.
Hold the input and output of the debug info size in bytes.
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, bool SkipPubSection=false)
DwarfStringPoolEntryRef Name
Name of the entry.
uint32_t QualifiedNameHash
Hash of the fully qualified name.
bool ObjcClassImplementation
Is this an ObjC class implementation?
bool SkipPubSection
Emit this entry only in the apple_* sections.
Information gathered about a DIE in the object file.
uint32_t ParentIdx
The index of this DIE's parent.
bool Prune
Is this a pure forward declaration we can strip?
int64_t AddrAdjust
Address offset to apply to the described entity.
bool HasLocationExpressionAddr
Is this a variable with a location attribute referencing address?
bool InDebugMap
Was this DIE's entity found in the map?
bool UnclonedReference
Is this a reference to a DIE that hasn't been cloned yet?
bool InModuleScope
Is DIE in the clang module scope?
bool Incomplete
Does DIE transitively refer an incomplete decl?
bool Keep
Is the DIE part of the linked output?
PatchLocation(DIE::value_iterator I, int64_t Reloc)