LLVM 17.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_DWARFLINKERCOMPILEUNIT_H
10#define LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
11
13#include "llvm/ADT/DenseMap.h"
14#include "llvm/CodeGen/DIE.h"
16#include <optional>
17
18namespace llvm {
19
20class DeclContext;
21
22/// Mapped value in the address map is the offset to apply to the
23/// linked address.
25
26// FIXME: Delete this structure.
29
30 PatchLocation() = default;
32
33 void set(uint64_t New) const {
34 assert(I);
35 const auto &Old = *I;
36 assert(Old.getType() == DIEValue::isInteger);
37 *I = DIEValue(Old.getAttribute(), Old.getForm(), DIEInteger(New));
38 }
39
40 uint64_t get() const {
41 assert(I);
42 return I->getDIEInteger().getValue();
43 }
44};
45
48
49/// Stores all information relating to a compile unit, be it in its original
50/// instance in the object file to its brand new cloned and generated DIE tree.
52public:
53 /// Information gathered about a DIE in the object file.
54 struct DIEInfo {
55 /// Address offset to apply to the described entity.
56 int64_t AddrAdjust;
57
58 /// ODR Declaration context.
60
61 /// Cloned version of that DIE.
63
64 /// The index of this DIE's parent.
66
67 /// Is the DIE part of the linked output?
68 bool Keep : 1;
69
70 /// Was this DIE's entity found in the map?
71 bool InDebugMap : 1;
72
73 /// Is this a pure forward declaration we can strip?
74 bool Prune : 1;
75
76 /// Does DIE transitively refer an incomplete decl?
77 bool Incomplete : 1;
78
79 /// Is DIE in the clang module scope?
80 bool InModuleScope : 1;
81
82 /// Is ODR marking done?
84
85 /// Is this a reference to a DIE that hasn't been cloned yet?
87
88#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
90#endif // if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
91 };
92
93 CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR,
94 StringRef ClangModuleName)
95 : OrigUnit(OrigUnit), ID(ID), ClangModuleName(ClangModuleName) {
96 Info.resize(OrigUnit.getNumDIEs());
97
98 auto CUDie = OrigUnit.getUnitDIE(false);
99 if (!CUDie) {
100 HasODR = false;
101 return;
102 }
103 if (auto Lang = dwarf::toUnsigned(CUDie.find(dwarf::DW_AT_language)))
104 HasODR = CanUseODR && (*Lang == dwarf::DW_LANG_C_plus_plus ||
105 *Lang == dwarf::DW_LANG_C_plus_plus_03 ||
106 *Lang == dwarf::DW_LANG_C_plus_plus_11 ||
107 *Lang == dwarf::DW_LANG_C_plus_plus_14 ||
108 *Lang == dwarf::DW_LANG_ObjC_plus_plus);
109 else
110 HasODR = false;
111 }
112
113 DWARFUnit &getOrigUnit() const { return OrigUnit; }
114
115 unsigned getUniqueID() const { return ID; }
116
117 void createOutputDIE() { NewUnit.emplace(OrigUnit.getUnitDIE().getTag()); }
118
120 if (NewUnit)
121 return &const_cast<BasicDIEUnit &>(*NewUnit).getUnitDie();
122 return nullptr;
123 }
124
125 bool hasODR() const { return HasODR; }
126 bool isClangModule() const { return !ClangModuleName.empty(); }
128 /// Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
130
131 const std::string &getClangModuleName() const { return ClangModuleName; }
132
133 DIEInfo &getInfo(unsigned Idx) { return Info[Idx]; }
134 const DIEInfo &getInfo(unsigned Idx) const { return Info[Idx]; }
135
136 DIEInfo &getInfo(const DWARFDie &Die) {
137 unsigned Idx = getOrigUnit().getDIEIndex(Die);
138 return Info[Idx];
139 }
140
141 uint64_t getStartOffset() const { return StartOffset; }
142 uint64_t getNextUnitOffset() const { return NextUnitOffset; }
144
145 std::optional<uint64_t> getLowPc() const { return LowPc; }
146 uint64_t getHighPc() const { return HighPc; }
147 bool hasLabelAt(uint64_t Addr) const { return Labels.count(Addr); }
148
149 const RangesTy &getFunctionRanges() const { return Ranges; }
150
151 const RngListAttributesTy &getRangesAttributes() { return RangeAttributes; }
152
153 std::optional<PatchLocation> getUnitRangesAttribute() const {
154 return UnitRangeAttribute;
155 }
156
158 return LocationAttributes;
159 }
160
161 /// Mark every DIE in this unit as kept. This function also
162 /// marks variables as InDebugMap so that they appear in the
163 /// reconstructed accelerator tables.
165
166 /// Compute the end offset for this unit. Must be called after the CU's DIEs
167 /// have been cloned. \returns the next unit offset (which is also the
168 /// current debug_info section size).
170
171 /// Keep track of a forward reference to DIE \p Die in \p RefUnit by \p
172 /// Attr. The attribute should be fixed up later to point to the absolute
173 /// offset of \p Die in the debug_info section or to the canonical offset of
174 /// \p Ctxt if it is non-null.
175 void noteForwardReference(DIE *Die, const CompileUnit *RefUnit,
176 DeclContext *Ctxt, PatchLocation Attr);
177
178 /// Apply all fixups recorded by noteForwardReference().
180
181 /// Add the low_pc of a label that is relocated by applying
182 /// offset \p PCOffset.
183 void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset);
184
185 /// Add a function range [\p LowPC, \p HighPC) that is relocated by applying
186 /// offset \p PCOffset.
187 void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset);
188
189 /// Keep track of a DW_AT_range attribute that we will need to patch up later.
190 void noteRangeAttribute(const DIE &Die, PatchLocation Attr);
191
192 /// Keep track of a location attribute pointing to a location list in the
193 /// debug_loc section.
194 void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset);
195
196 /// Add a name accelerator entry for \a Die with \a Name.
198
199 /// Add a name accelerator entry for \a Die with \a Name.
201 bool SkipPubnamesSection = false);
202
203 /// Add various accelerator entries for \p Die with \p Name which is stored
204 /// in the string table at \p Offset. \p Name must be an Objective-C
205 /// selector.
207 bool SkipPubnamesSection = false);
208
209 /// Add a type accelerator entry for \p Die with \p Name which is stored in
210 /// the string table at \p Offset.
212 bool ObjcClassImplementation,
213 uint32_t QualifiedNameHash);
214
215 struct AccelInfo {
216 /// Name of the entry.
218
219 /// DIE this entry describes.
220 const DIE *Die;
221
222 /// Hash of the fully qualified name.
224
225 /// Emit this entry only in the apple_* sections.
227
228 /// Is this an ObjC class implementation?
230
232 bool SkipPubSection = false)
234
236 uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
239 ObjcClassImplementation(ObjCClassIsImplementation) {}
240 };
241
242 const std::vector<AccelInfo> &getPubnames() const { return Pubnames; }
243 const std::vector<AccelInfo> &getPubtypes() const { return Pubtypes; }
244 const std::vector<AccelInfo> &getNamespaces() const { return Namespaces; }
245 const std::vector<AccelInfo> &getObjC() const { return ObjC; }
246
247 MCSymbol *getLabelBegin() { return LabelBegin; }
248 void setLabelBegin(MCSymbol *S) { LabelBegin = S; }
249
250private:
251 DWARFUnit &OrigUnit;
252 unsigned ID;
253 std::vector<DIEInfo> Info; ///< DIE info indexed by DIE index.
254 std::optional<BasicDIEUnit> NewUnit;
255 MCSymbol *LabelBegin = nullptr;
256
257 uint64_t StartOffset;
258 uint64_t NextUnitOffset;
259
260 std::optional<uint64_t> LowPc;
261 uint64_t HighPc = 0;
262
263 /// A list of attributes to fixup with the absolute offset of
264 /// a DIE in the debug_info section.
265 ///
266 /// The offsets for the attributes in this array couldn't be set while
267 /// cloning because for cross-cu forward references the target DIE's offset
268 /// isn't known you emit the reference attribute.
269 std::vector<
270 std::tuple<DIE *, const CompileUnit *, DeclContext *, PatchLocation>>
271 ForwardDIEReferences;
272
273 /// The ranges in that map are the PC ranges for functions in this unit,
274 /// associated with the PC offset to apply to the addresses to get
275 /// the linked address.
276 RangesTy Ranges;
277
278 /// The DW_AT_low_pc of each DW_TAG_label.
280
281 /// 'rnglist'(DW_AT_ranges, DW_AT_start_scope) attributes to patch after
282 /// we have gathered all the unit's function addresses.
283 /// @{
284 RngListAttributesTy RangeAttributes;
285 std::optional<PatchLocation> UnitRangeAttribute;
286 /// @}
287
288 /// Location attributes that need to be transferred from the
289 /// original debug_loc section to the liked one. They are stored
290 /// along with the PC offset that is to be applied to their
291 /// function's address.
292 LocListAttributesTy LocationAttributes;
293
294 /// Accelerator entries for the unit, both for the pub*
295 /// sections and the apple* ones.
296 /// @{
297 std::vector<AccelInfo> Pubnames;
298 std::vector<AccelInfo> Pubtypes;
299 std::vector<AccelInfo> Namespaces;
300 std::vector<AccelInfo> ObjC;
301 /// @}
302
303 /// Is this unit subject to the ODR rule?
304 bool HasODR;
305
306 /// The DW_AT_language of this unit.
307 uint16_t Language = 0;
308
309 /// The DW_AT_LLVM_sysroot of this unit.
310 std::string SysRoot;
311
312 /// If this is a Clang module, this holds the module's name.
313 std::string ClangModuleName;
314};
315
316} // end namespace llvm
317
318#endif // LLVM_DWARFLINKER_DWARFLINKERCOMPILEUNIT_H
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:492
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
uint64_t Addr
std::string Name
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
AddressRangesMap class maps values to the address ranges.
Stores all information relating to a compile unit, be it in its original instance in the object file ...
void fixupForwardReferences()
Apply all fixups recorded by noteForwardReference().
const DIEInfo & getInfo(unsigned Idx) const
DIEInfo & getInfo(const DWARFDie &Die)
void noteForwardReference(DIE *Die, const CompileUnit *RefUnit, DeclContext *Ctxt, PatchLocation Attr)
Keep track of a forward reference to DIE Die in RefUnit by Attr.
bool hasLabelAt(uint64_t Addr) const
const std::vector< AccelInfo > & getPubnames() const
const LocListAttributesTy & getLocationAttributes() const
const std::string & getClangModuleName() const
const RngListAttributesTy & getRangesAttributes()
void addNameAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name, bool SkipPubnamesSection=false)
Add a name accelerator entry for Die with Name.
unsigned getUniqueID() const
void addNamespaceAccelerator(const DIE *Die, DwarfStringPoolEntryRef Name)
Add a name accelerator entry for Die with Name.
DIEInfo & getInfo(unsigned Idx)
const std::vector< AccelInfo > & getObjC() const
DIE * getOutputUnitDIE() const
DWARFUnit & getOrigUnit() const
StringRef getSysRoot()
Return the DW_AT_LLVM_sysroot of the compile unit or an empty StringRef.
void setLabelBegin(MCSymbol *S)
const RangesTy & getFunctionRanges() const
void noteRangeAttribute(const DIE &Die, PatchLocation Attr)
Keep track of a DW_AT_range attribute that we will need to patch up later.
void addFunctionRange(uint64_t LowPC, uint64_t HighPC, int64_t PCOffset)
Add a function range [LowPC, HighPC) that is relocated by applying offset PCOffset.
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.
const std::vector< AccelInfo > & getPubtypes() const
uint64_t getNextUnitOffset() const
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.
uint64_t getStartOffset() const
const std::vector< AccelInfo > & getNamespaces() const
uint64_t computeNextUnitOffset(uint16_t DwarfVersion)
Compute the end offset for this unit.
std::optional< uint64_t > getLowPc() const
CompileUnit(DWARFUnit &OrigUnit, unsigned ID, bool CanUseODR, StringRef ClangModuleName)
std::optional< PatchLocation > getUnitRangesAttribute() const
void noteLocationAttribute(PatchLocation Attr, int64_t PcOffset)
Keep track of a location attribute pointing to a location list in the debug_loc section.
void addLabelLowPc(uint64_t LabelLowPc, int64_t PcOffset)
Add the low_pc of a label that is relocated by applying offset PCOffset.
void markEverythingAsKept()
Mark every DIE in this unit as kept.
uint64_t getHighPc() const
void setStartOffset(uint64_t DebugInfoSize)
An integer value DIE.
Definition: DIE.h:168
DIE & getUnitDie()
Definition: DIE.h:924
A structured debug information entry.
Definition: DIE.h:744
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition: DWARFDie.h:42
dwarf::Tag getTag() const
Definition: DWARFDie.h:71
unsigned getNumDIEs()
Returns the number of DIEs in the unit.
Definition: DWARFUnit.h:488
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:426
uint32_t getDIEIndex(const DWARFDebugInfoEntry *Die) const
Return the index of a Die entry inside the unit's DIE vector.
Definition: DWARFUnit.h:262
A DeclContext is a named program scope that is used for ODR uniquing of types.
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
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:41
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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.
Definition: AddressRanges.h:18
const DIE * Die
DIE this entry describes.
DwarfStringPoolEntryRef Name
Name of the entry.
bool ObjcClassImplementation
Is this an ObjC class implementation?
uint32_t QualifiedNameHash
Hash of the fully qualified name.
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, bool SkipPubSection=false)
bool SkipPubSection
Emit this entry only in the apple_* sections.
AccelInfo(DwarfStringPoolEntryRef Name, const DIE *Die, uint32_t QualifiedNameHash, bool ObjCClassIsImplementation)
Information gathered about a DIE in the object file.
DeclContext * Ctxt
ODR Declaration context.
bool InModuleScope
Is DIE in the clang module scope?
DIE * Clone
Cloned version of that DIE.
int64_t AddrAdjust
Address offset to apply to the described entity.
uint32_t ParentIdx
The index of this DIE's parent.
bool UnclonedReference
Is this a reference to a DIE that hasn't been cloned yet?
bool ODRMarkingDone
Is ODR marking done?
bool InDebugMap
Was this DIE's entity found in the map?
bool Prune
Is this a pure forward declaration we can strip?
bool Keep
Is the DIE part of the linked output?
bool Incomplete
Does DIE transitively refer an incomplete decl?
Hold the input and output of the debug info size in bytes.
Definition: DWARFLinker.cpp:40
PatchLocation()=default
PatchLocation(DIE::value_iterator I)
DIE::value_iterator I
void set(uint64_t New) const