1//===- llvm/CodeGen/DwarfCompileUnit.h - Dwarf Compile Unit -----*- 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// This file contains support for writing dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
14#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
15
16#include "DwarfDebug.h"
17#include "DwarfUnit.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/BinaryFormat/Dwarf.h"
24#include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
25#include "llvm/CodeGen/LexicalScopes.h"
26#include "llvm/IR/DebugInfoMetadata.h"
27#include "llvm/Support/Casting.h"
28#include <cstdint>
29#include <memory>
30
31namespace llvm {
32
33class AsmPrinter;
34class DIE;
35class DIELoc;
36class DIEValueList;
37class DwarfFile;
38class GlobalVariable;
39class MCExpr;
40class MCSymbol;
41class MDNode;
42
43enum class UnitKind { Skeleton, Full };
44
45class DwarfCompileUnit final : public DwarfUnit {
46 bool HasRangeLists = false;
47
48 /// The start of the unit line section, this is also
49 /// reused in appyStmtList.
50 MCSymbol *LineTableStartSym;
51
52 /// Skeleton unit associated with this unit.
53 DwarfCompileUnit *Skeleton = nullptr;
54
55 /// The start of the unit macro info within macro section.
56 MCSymbol *MacroLabelBegin;
57
58 /// GlobalNames - A map of globally visible named entities for this unit.
59 StringMap<const DIE *> GlobalNames;
60
61 /// GlobalTypes - A map of globally visible types for this unit.
62 StringMap<const DIE *> GlobalTypes;
63
64 // List of ranges for a given compile unit.
65 SmallVector<RangeSpan, 2> CURanges;
66
67 // The base address of this unit, if any. Used for relative references in
68 // ranges/locs.
69 const MCSymbol *BaseAddress = nullptr;
70
71 using MDNodeSetVector =
72 SetVector<const MDNode *, SmallVector<const MDNode *, 4>,
73 SmallPtrSet<const MDNode *, 4>>;
74
75 // List of entities (either static locals, types or imports) that
76 // belong to subprograms within this CU.
77 MDNodeSetVector DeferredLocalDecls;
78
79 // List of concrete lexical block scopes belong to subprograms within this CU.
80 DenseMap<const DILocalScope *, DIE *> LexicalBlockDIEs;
81
82 // List of abstract local scopes (either DISubprogram or DILexicalBlock).
83 DenseMap<const DILocalScope *, DIE *> AbstractLocalScopeDIEs;
84 SmallPtrSet<const DISubprogram *, 8> FinalizedAbstractSubprograms;
85
86 // List of inlined lexical block scopes that belong to subprograms within this
87 // CU.
88 DenseMap<const DILocalScope *, SmallVector<DIE *, 2>> InlinedLocalScopeDIEs;
89
90 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> AbstractEntities;
91
92 /// DWO ID for correlating skeleton and split units.
93 uint64_t DWOId = 0;
94
95 const DIFile *LastFile = nullptr;
96 unsigned LastFileID;
97
98 /// \anchor applyConcreteDbgVariableAttribute
99 /// \name applyConcreteDbgVariableAttribute
100 /// Overload set which applies attributes to \c VariableDie based on
101 /// the active variant of \c DV, which is passed as the first argument.
102 ///@{
103
104 /// See \ref applyConcreteDbgVariableAttribute
105 void applyConcreteDbgVariableAttributes(const Loc::Single &Single,
106 const DbgVariable &DV,
107 DIE &VariableDie);
108 /// See \ref applyConcreteDbgVariableAttribute
109 void applyConcreteDbgVariableAttributes(const Loc::Multi &Multi,
110 const DbgVariable &DV,
111 DIE &VariableDie);
112 /// See \ref applyConcreteDbgVariableAttribute
113 void applyConcreteDbgVariableAttributes(const Loc::MMI &MMI,
114 const DbgVariable &DV,
115 DIE &VariableDie);
116 /// See \ref applyConcreteDbgVariableAttribute
117 void applyConcreteDbgVariableAttributes(const Loc::EntryValue &EntryValue,
118 const DbgVariable &DV,
119 DIE &VariableDie);
120 /// See \ref applyConcreteDbgVariableAttribute
121 void applyConcreteDbgVariableAttributes(const std::monostate &,
122 const DbgVariable &DV,
123 DIE &VariableDie);
124
125 ///@}
126
127 bool isDwoUnit() const override;
128
129 DenseMap<const DILocalScope *, DIE *> &getAbstractScopeDIEs() {
130 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
131 return AbstractLocalScopeDIEs;
132 return DU->getAbstractScopeDIEs();
133 }
134
135 DenseMap<const DINode *, std::unique_ptr<DbgEntity>> &getAbstractEntities() {
136 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
137 return AbstractEntities;
138 return DU->getAbstractEntities();
139 }
140
141 auto &getFinalizedAbstractSubprograms() {
142 if (isDwoUnit() && !DD->shareAcrossDWOCUs())
143 return FinalizedAbstractSubprograms;
144 return DU->getFinalizedAbstractSubprograms();
145 }
146
147 void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
148
149 /// Add info for Wasm-global-based relocation.
150 void addWasmRelocBaseGlobal(DIELoc *Loc, StringRef GlobalName,
151 uint64_t GlobalIndex);
152
153 /// Create context DIE for abstract subprogram.
154 /// \returns The context DIE and the compile unit where abstract
155 /// DIE should be constructed.
156 std::pair<DIE *, DwarfCompileUnit *>
157 getOrCreateAbstractSubprogramContextDIE(const DISubprogram *SP);
158
159 /// Create new DIE for abstract subprogram.
160 DIE &createAbstractSubprogramDIE(const DISubprogram *SP, DIE *ContextDIE,
161 DwarfCompileUnit *ContextCU);
162
163 /// Add a location exprloc to \p DIE with attribute \p Attribute at
164 /// for \p Location modified by raw DIExpression \p Expr.
165 void addLocationWithExpr(DIE &Die, dwarf::Attribute Attribute,
166 const MachineLocation &Location,
167 ArrayRef<uint64_t> Expr);
168
169public:
170 DwarfCompileUnit(unsigned UID, const DICompileUnit *Node, AsmPrinter *A,
171 DwarfDebug *DW, DwarfFile *DWU,
172 UnitKind Kind = UnitKind::Full);
173
174 bool hasRangeLists() const { return HasRangeLists; }
175
176 DwarfCompileUnit *getSkeleton() const {
177 return Skeleton;
178 }
179
180 bool includeMinimalInlineScopes() const;
181
182 bool emitFuncLineTableOffsets() const;
183
184 void initStmtList();
185
186 /// Apply the DW_AT_stmt_list from this compile unit to the specified DIE.
187 void applyStmtList(DIE &D);
188
189 /// Get line table start symbol for this unit.
190 MCSymbol *getLineTableStartSym() const { return LineTableStartSym; }
191
192 /// A pair of GlobalVariable and DIExpression.
193 struct GlobalExpr {
194 const GlobalVariable *Var;
195 const DIExpression *Expr;
196 };
197
198 struct BaseTypeRef {
199 BaseTypeRef(unsigned BitSize, dwarf::TypeKind Encoding) :
200 BitSize(BitSize), Encoding(Encoding) {}
201 unsigned BitSize;
202 dwarf::TypeKind Encoding;
203 DIE *Die = nullptr;
204 };
205
206 std::vector<BaseTypeRef> ExprRefedBaseTypes;
207
208 /// Get or create global variable DIE.
209 DIE *
210 getOrCreateGlobalVariableDIE(const DIGlobalVariable *GV,
211 ArrayRef<GlobalExpr> GlobalExprs);
212
213 DIE *getOrCreateCommonBlock(const DICommonBlock *CB,
214 ArrayRef<GlobalExpr> GlobalExprs);
215
216 void addLocationAttribute(DIE *ToDIE, const DIGlobalVariable *GV,
217 ArrayRef<GlobalExpr> GlobalExprs);
218
219 /// addLabelAddress - Add a dwarf label attribute data and value using
220 /// either DW_FORM_addr or DW_FORM_GNU_addr_index.
221 void addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
222 const MCSymbol *Label);
223
224 /// addLocalLabelAddress - Add a dwarf label attribute data and value using
225 /// DW_FORM_addr only.
226 void addLocalLabelAddress(DIE &Die, dwarf::Attribute Attribute,
227 const MCSymbol *Label);
228
229 DwarfCompileUnit &getCU() override { return *this; }
230
231 unsigned getOrCreateSourceID(const DIFile *File) override;
232
233 /// addRange - Add an address range to the list of ranges for this unit.
234 void addRange(RangeSpan Range);
235
236 void attachLowHighPC(DIE &D, const MCSymbol *Begin, const MCSymbol *End);
237
238 /// Find DIE for the given subprogram and attach appropriate
239 /// DW_AT_low_pc, DW_AT_high_pc and DW_AT_LLVM_stmt_sequence attributes.
240 /// If there are global variables in this scope then create and insert DIEs
241 /// for these variables.
242 DIE &updateSubprogramScopeDIE(const DISubprogram *SP, const Function &F,
243 MCSymbol *LineTableSym);
244
245 void constructScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE);
246
247 /// A helper function to construct a RangeSpanList for a given
248 /// lexical scope.
249 void addScopeRangeList(DIE &ScopeDIE, SmallVector<RangeSpan, 2> Range);
250
251 void attachRangesOrLowHighPC(DIE &D, SmallVector<RangeSpan, 2> Ranges);
252
253 void attachRangesOrLowHighPC(DIE &D,
254 const SmallVectorImpl<InsnRange> &Ranges);
255
256 /// This scope represents an inlined body of a function. Construct a
257 /// DIE to represent this concrete inlined copy of the function.
258 DIE *constructInlinedScopeDIE(LexicalScope *Scope, DIE &ParentScopeDIE);
259
260 /// Construct new DW_TAG_lexical_block for this scope and
261 /// attach DW_AT_low_pc/DW_AT_high_pc labels.
262 DIE *constructLexicalScopeDIE(LexicalScope *Scope);
263
264 /// Get a DIE for the given DILexicalBlock.
265 /// Note that this function assumes that the DIE has been already created
266 /// and it's an error, if it hasn't.
267 DIE *getLexicalBlockDIE(const DILexicalBlock *LB);
268
269 /// Construct a DIE for the given DbgVariable.
270 DIE *constructVariableDIE(DbgVariable &DV, bool Abstract = false);
271
272 /// Convenience overload which writes the DIE pointer into an out variable
273 /// ObjectPointer in addition to returning it.
274 DIE *constructVariableDIE(DbgVariable &DV, const LexicalScope &Scope,
275 DIE *&ObjectPointer);
276
277 /// Construct a DIE for the given DbgLabel.
278 DIE *constructLabelDIE(DbgLabel &DL, const LexicalScope &Scope);
279
280 void createBaseTypeDIEs();
281
282 /// Construct a DIE for a given scope.
283 /// This instance of 'getOrCreateContextDIE()' can handle DILocalScope.
284 DIE *getOrCreateContextDIE(const DIScope *Ty) override;
285
286 DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, const Function *F,
287 bool Minimal = false) override;
288
289 /// Construct a DIE for this subprogram scope.
290 DIE &constructSubprogramScopeDIE(const DISubprogram *Sub, const Function &F,
291 LexicalScope *Scope, MCSymbol *LineTableSym);
292
293 DIE *createAndAddScopeChildren(LexicalScope *Scope, DIE &ScopeDIE);
294
295 /// Create an abstract subprogram DIE, that should later be populated
296 /// by \ref constructAbstractSubprogramScopeDIE.
297 DIE &getOrCreateAbstractSubprogramDIE(const DISubprogram *SP);
298 void constructAbstractSubprogramScopeDIE(LexicalScope *Scope);
299
300 /// Whether to use the GNU analog for a DWARF5 tag, attribute, or location
301 /// atom. Only applicable when emitting otherwise DWARF4-compliant debug info.
302 bool useGNUAnalogForDwarf5Feature() const;
303
304 /// This takes a DWARF 5 tag and returns it or a GNU analog.
305 dwarf::Tag getDwarf5OrGNUTag(dwarf::Tag Tag) const;
306
307 /// This takes a DWARF 5 attribute and returns it or a GNU analog.
308 dwarf::Attribute getDwarf5OrGNUAttr(dwarf::Attribute Attr) const;
309
310 /// This takes a DWARF 5 location atom and either returns it or a GNU analog.
311 dwarf::LocationAtom getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const;
312
313 /// Construct a call site entry DIE describing a call within \p Scope to a
314 /// callee described by \p CalleeSP and \p CalleeF.
315 /// \p IsTail specifies whether the call is a tail call.
316 /// \p PCAddr points to the PC value after the call instruction.
317 /// \p CallAddr points to the PC value at the call instruction (or is null).
318 /// \p CallTarget a location holding the target address for an indirect call.
319 /// For direct calls \p CallTarget register is set to 0.
320 /// \p Offset from \p CallTarget register value if the location is indirect.
321 DIE &constructCallSiteEntryDIE(DIE &ScopeDIE, const DISubprogram *CalleeSP,
322 const Function *CalleeF, bool IsTail,
323 const MCSymbol *PCAddr,
324 const MCSymbol *CallAddr,
325 MachineLocation CallTarget, int64_t Offset,
326 DIType *AllocSiteTy);
327 /// Construct call site parameter DIEs for the \p CallSiteDIE. The \p Params
328 /// were collected by the \ref collectCallSiteParameters.
329 /// Note: The order of parameters does not matter, since debuggers recognize
330 /// call site parameters by the DW_AT_location attribute.
331 void constructCallSiteParmEntryDIEs(DIE &CallSiteDIE,
332 SmallVector<DbgCallSiteParam, 4> &Params);
333
334 /// Get or create a DIE for an imported entity.
335 DIE *getOrCreateImportedEntityDIE(const DIImportedEntity *IE);
336 DIE *constructImportedEntityDIE(const DIImportedEntity *IE);
337
338 void finishSubprogramDefinition(const DISubprogram *SP);
339 void finishEntityDefinition(const DbgEntity *Entity);
340 void attachLexicalScopesAbstractOrigins();
341
342 /// Find abstract variable associated with Var.
343 using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
344 DbgEntity *getExistingAbstractEntity(const DINode *Node);
345 void createAbstractEntity(const DINode *Node, LexicalScope *Scope);
346
347 /// Set the skeleton unit associated with this unit.
348 void setSkeleton(DwarfCompileUnit &Skel) { Skeleton = &Skel; }
349
350 unsigned getHeaderSize() const override {
351 // DWARF v5 added the DWO ID to the header for split/skeleton units.
352 unsigned DWOIdSize =
353 DD->getDwarfVersion() >= 5 && DD->useSplitDwarf() ? sizeof(uint64_t)
354 : 0;
355 return DwarfUnit::getHeaderSize() + DWOIdSize;
356 }
357 unsigned getLength() {
358 return Asm->getUnitLengthFieldByteSize() + // Length field
359 getHeaderSize() + getUnitDie().getSize();
360 }
361
362 void emitHeader(bool UseOffsets) override;
363
364 /// Add the DW_AT_addr_base attribute to the unit DIE.
365 void addAddrTableBase();
366
367 MCSymbol *getMacroLabelBegin() const {
368 return MacroLabelBegin;
369 }
370
371 /// Add a new global name to the compile unit.
372 void addGlobalName(StringRef Name, const DIE &Die,
373 const DIScope *Context) override;
374
375 /// Add a new global name present in a type unit to this compile unit.
376 void addGlobalNameForTypeUnit(StringRef Name, const DIScope *Context);
377
378 /// Add a new global type to the compile unit.
379 void addGlobalTypeImpl(const DIType *Ty, const DIE &Die,
380 const DIScope *Context) override;
381
382 /// Add a new global type present in a type unit to this compile unit.
383 void addGlobalTypeUnitType(const DIType *Ty, const DIScope *Context);
384
385 const StringMap<const DIE *> &getGlobalNames() const { return GlobalNames; }
386 const StringMap<const DIE *> &getGlobalTypes() const { return GlobalTypes; }
387
388 /// Add DW_AT_location attribute for a DbgVariable based on provided
389 /// MachineLocation.
390 void addVariableAddress(const DbgVariable &DV, DIE &Die,
391 MachineLocation Location);
392 /// Add an address attribute to a die based on the location provided.
393 void addAddress(DIE &Die, dwarf::Attribute Attribute,
394 const MachineLocation &Location);
395
396 /// Add a memory location exprloc to \p DIE with attribute \p Attribute
397 /// at \p Location + \p Offset.
398 void addMemoryLocation(DIE &Die, dwarf::Attribute Attribute,
399 const MachineLocation &Location, int64_t Offset);
400 /// Start with the address based on the location provided, and generate the
401 /// DWARF information necessary to find the actual variable (navigating the
402 /// extra location information encoded in the type) based on the starting
403 /// location. Add the DWARF information to the die.
404 void addComplexAddress(const DIExpression *DIExpr, DIE &Die,
405 dwarf::Attribute Attribute,
406 const MachineLocation &Location);
407
408 /// Add a Dwarf loclistptr attribute data and value.
409 void addLocationList(DIE &Die, dwarf::Attribute Attribute, unsigned Index);
410
411 /// Add attributes to \p Var which reflect the common attributes of \p
412 /// VariableDie, namely those which are not dependant on the active variant.
413 void applyCommonDbgVariableAttributes(const DbgVariable &Var,
414 DIE &VariableDie);
415
416 /// Add a Dwarf expression attribute data and value.
417 void addExpr(DIELoc &Die, dwarf::Form Form, const MCExpr *Expr);
418
419 void applySubprogramAttributesToDefinition(const DISubprogram *SP,
420 DIE &SPDie);
421
422 void applyLabelAttributes(const DbgLabel &Label, DIE &LabelDie);
423
424 /// getRanges - Get the list of ranges for this unit.
425 const SmallVectorImpl<RangeSpan> &getRanges() const { return CURanges; }
426 SmallVector<RangeSpan, 2> takeRanges() { return std::move(CURanges); }
427
428 void setBaseAddress(const MCSymbol *Base) { BaseAddress = Base; }
429 const MCSymbol *getBaseAddress() const { return BaseAddress; }
430
431 uint64_t getDWOId() const { return DWOId; }
432 void setDWOId(uint64_t DwoId) { DWOId = DwoId; }
433
434 bool hasDwarfPubSections() const;
435
436 void addBaseTypeRef(DIEValueList &Die, int64_t Idx);
437
438 MDNodeSetVector &getDeferredLocalDecls() { return DeferredLocalDecls; }
439};
440
441} // end namespace llvm
442
443#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFCOMPILEUNIT_H
444