LLVM 24.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"
18#include <optional>
19
20namespace llvm {
21namespace dwarf_linker {
22namespace classic {
23
24class DeclContext;
25
26/// Mapped value in the address map is the offset to apply to the
27/// linked address.
29
30// This structure keeps patch for the attribute and, optionally,
31// the value of relocation which should be applied. Currently,
32// only location attribute needs to have relocation: either to the
33// function ranges if location attribute is of type 'loclist',
34// either to the operand of DW_OP_addr/DW_OP_addrx if location attribute
35// is of type 'exprloc'.
36// ASSUMPTION: Location attributes of 'loclist' type containing 'exprloc'
37// with address expression operands are not supported yet.
40 int64_t RelocAdjustment = 0;
41
42 PatchLocation() = default;
46
47 void set(uint64_t New) const {
48 assert(I);
49 const auto &Old = *I;
50 assert(Old.getType() == DIEValue::isInteger);
51 *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
52 }
53
54 uint64_t get() const {
55 assert(I);
56 return I->getDIEInteger().getValue();
57 }
58};
59
63
64/// Stores all information relating to a compile unit, be it in its original
65/// instance in the object file to its brand new cloned and generated DIE tree.
67public:
68 /// Information gathered about a DIE in the object file.
69 struct DIEInfo {
70 /// Address offset to apply to the described entity.
71 int64_t AddrAdjust;
72
73 /// ODR Declaration context.
75
76 /// Cloned version of that DIE.
78
79 /// The index of this DIE's parent.
81
82 /// Is the DIE part of the linked output?
83 bool Keep : 1;
84
85 /// Was this DIE's entity found in the map?
86 bool InDebugMap : 1;
87
88 /// Is this a pure forward declaration we can strip?
89 bool Prune : 1;
90
91 /// Does DIE transitively refer an incomplete decl?
92 bool Incomplete : 1;
93
94 /// Is DIE in the clang module scope?
95 bool InModuleScope : 1;
96
97 /// Is ODR marking done?
99
100 /// Is this a reference to a DIE that hasn't been cloned yet?
102
103 /// Is this a variable with a location attribute referencing address?
105
106#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
107 LLVM_DUMP_METHOD void dump();
108#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
109 };
110
111 CompileUnit(DWARFUnit &OrigUnit, const DWARFFile &File, unsigned ID,
112 bool CanUseODR, StringRef ClangModuleName)
113 : OrigUnit(OrigUnit), File(File), ID(ID),
114 ClangModuleName(ClangModuleName) {
115 Info.resize(OrigUnit.getNumDIEs());
116
117 auto CUDie = OrigUnit.getUnitDIE(false);
118 if (!CUDie) {
119 HasODR = false;
120 return;
121 }
122 if (auto Lang = CUDie.getLanguage())
123 HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
124 *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
125 *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
126 *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
127 *Lang == dwarf::DW_LANG_ObjC_plus_plus);
128 else
129 HasODR = false;
130 }
131
132 DWARFUnit &getOrigUnit() const { return OrigUnit; }
133
134 /// Returns the input file containing the original unit.
135 const DWARFFile &getContainingFile() const { return File; }
136
137 unsigned getUniqueID() const { return ID; }
138
140 NewUnit.emplace(OrigUnit.getUnitDIE().getTag());
141
142 // Propogate the section offset so that DIEntry can compute
143 // correct absolute offsets for DW_FORM_ref_addr references
144 NewUnit->setDebugSectionOffset(StartOffset);
145 }
146
148 if (NewUnit)
149 return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
150 return nullptr;
151 }
152
153 dwarf::Tag getTag() const { return OrigUnit.getUnitDIE().getTag(); }
154
155 bool hasODR() const { return HasODR; }
156 bool isClangModule() const { return !ClangModuleName.empty(); }
158 /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
160
161 const std::string &getClangModuleName() const { return ClangModuleName; }
162
163 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
164 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
165
166 DIEInfo &getInfo(const DWARFDie &Die) {
167 unsigned Idx = getOrigUnit().getDIEIndex(Die);
168 return Info[Idx];
169 }
170
171 uint64_t getStartOffset() const { return StartOffset; }
172 uint64_t getNextUnitOffset() const { return NextUnitOffset; }
174 StartOffset = DebugInfoSize;
175 if (NewUnit)
176 NewUnit->setDebugSectionOffset(DebugInfoSize);
177 }
178
179 std::optional<uint64_t> getLowPc() const { return LowPc; }
180 uint64_t getHighPc() const { return HighPc; }
181 bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
182
183 const RangesTy &getFunctionRanges() const { return Ranges; }
184
185 const RngListAttributesTy &getRangesAttributes() { return RangeAttributes; }
186
187 std::optional<PatchLocation> getUnitRangesAttribute() const {
188 return UnitRangeAttribute;
189 }
190
192 return LocationAttributes;
193 }
194
195 // Provide access to the list of DW_AT_LLVM_stmt_sequence attributes that may
196 // need to be patched.
198 return StmtSeqListAttributes;
199 }
200
201 /// Mark every DIE in this unit as kept. This function also
202 /// marks variables as InDebugMap so that they appear in the
203 /// reconstructed accelerator tables.
205
206 /// Compute the end offset for this unit. Must be called after the CU's DIEs
207 /// have been cloned. \returns the next unit offset (which is also the
208 /// current debug_info section size).
210
211 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
212 /// Attr. The attribute should be fixed up later to point to the absolute
213 /// offset of \p Die in the debug_info section or to the canonical offset of
214 /// \p Ctxt if it is non-null.
215 LLVM_ABI void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
216 DeclContext *Ctxt, PatchLocation Attr);
217
218 /// Apply all fixups recorded by noteForwardReference().
220
221 /// Add the low_pc of a label that is relocated by applying
222 /// offset \p PCOffset.
223 LLVM_ABI void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
224
225 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
226 /// offset \p PCOffset.
227 LLVM_ABI void addFunctionRange(uint64_t LowPC, uint64_t HighPC,
228 int64_t PCOffset);
229
230 /// Keep track of a DW_AT_range attribute that we will need to patch up later.
231 LLVM_ABI void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
232
233 /// Keep track of a location attribute pointing to a location list in the
234 /// debug_loc section.
236
237 // Record that the given DW_AT_LLVM_stmt_sequence attribute may need to be
238 // patched later.
240
241 /// Add a name accelerator entry for \a Die with \a Name.
242 LLVM_ABI void addNamespaceAccelerator(const DIE *Die,
244
245 /// Add a name accelerator entry for \a Die with \a Name.
247 bool SkipPubnamesSection = false);
248
249 /// Add various accelerator entries for \p Die with \p Name which is stored
250 /// in the string table at \p Offset. \p Name must be an Objective-C
251 /// selector.
253 bool SkipPubnamesSection = false);
254
255 /// Add a type accelerator entry for \p Die with \p Name which is stored in
256 /// the string table at \p Offset.
258 bool ObjcClassImplementation,
259 uint32_t QualifiedNameHash);
260
261 struct AccelInfo {
262 /// Name of the entry.
264
265 /// DIE this entry describes.
266 const DIE *Die;
267
268 /// Hash of the fully qualified name.
270
271 /// Emit this entry only in the apple_* sections.
273
274 /// Is this an ObjC class implementation?
276
280
282 uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
285 ObjcClassImplementation(ObjCClassIsImplementation) {}
286 };
287
288 const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
289 const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
290 const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
291 const std::vector<AccelInfo> &getObjC() const { return ObjC; }
292
293 MCSymbol *getLabelBegin() { return LabelBegin; }
294 void setLabelBegin(MCSymbol *S) { LabelBegin = S; }
295
296private:
297 DWARFUnit &OrigUnit;
298 const DWARFFile &File;
299 unsigned ID;
300 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
301 std::optional<BasicDIEUnit> NewUnit;
302 MCSymbol *LabelBegin = nullptr;
303
304 uint64_t StartOffset;
305 uint64_t NextUnitOffset;
306
307 std::optional<uint64_t> LowPc;
308 uint64_t HighPc = 0;
309
310 /// A list of attributes to fixup with the absolute offset of
311 /// a DIE in the debug_info section.
312 ///
313 /// The offsets for the attributes in this array couldn't be set while
314 /// cloning because for cross-cu forward references the target DIE's offset
315 /// isn't known you emit the reference attribute.
316 std::vector<
317 std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
318 ForwardDIEReferences;
319
320 /// The ranges in that map are the PC ranges for functions in this unit,
321 /// associated with the PC offset to apply to the addresses to get
322 /// the linked address.
323 RangesTy Ranges;
324
325 /// The DW_AT_low_pc of each DW_TAG_label.
327
328 /// 'rnglist'(DW_AT_ranges, DW_AT_start_scope) attributes to patch after
329 /// we have gathered all the unit's function addresses.
330 /// @{
331 RngListAttributesTy RangeAttributes;
332 std::optional<PatchLocation> UnitRangeAttribute;
333 /// @}
334
335 /// Location attributes that need to be transferred from the
336 /// original debug_loc section to the linked one. They are stored
337 /// along with the PC offset that is to be applied to their
338 /// function's address or to be applied to address operands of
339 /// location expression.
340 LocListAttributesTy LocationAttributes;
341
342 // List of DW_AT_LLVM_stmt_sequence attributes that may need to be patched
343 // after the dwarf linker rewrites the line table. During line table rewrite
344 // the line table format might change, so we have to patch any offsets that
345 // reference its contents.
346 StmtSeqListAttributesTy StmtSeqListAttributes;
347
348 /// Accelerator entries for the unit, both for the pub*
349 /// sections and the apple* ones.
350 /// @{
351 std::vector<AccelInfo> Pubnames;
352 std::vector<AccelInfo> Pubtypes;
353 std::vector<AccelInfo> Namespaces;
354 std::vector<AccelInfo> ObjC;
355 /// @}
356
357 /// Is this unit subject to the ODR rule?
358 bool HasODR;
359
360 /// The DW_AT_language of this unit.
361 uint16_t Language = 0;
362
363 /// The DW_AT_LLVM_sysroot of this unit.
364 std::string SysRoot;
365
366 /// If this is a Clang module, this holds the module's name.
367 std::string ClangModuleName;
368};
369
370} // end of namespace classic
371} // end of namespace dwarf_linker
372} // end of namespace llvm
373
374#endif // LLVM_DWARFLINKER_CLASSIC_DWARFLINKERCOMPILEUNIT_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:702
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:1034
A structured debug information entry.
Definition DIE.h:835
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:276
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class represents DWARF information for source file and it's address map.
Definition DWARFFile.h:27
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
const DWARFFile & getContainingFile() const
Returns the input file containing the original unit.
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, const DWARFFile &File, unsigned ID, bool CanUseODR, StringRef ClangModuleName)
const RngListAttributesTy & getRangesAttributes()
A DeclContext is a named program scope that is used for ODR uniquing of types.
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
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)