1 //===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains support for writing dwarf compile unit.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
16 #include "DwarfDebug.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/CodeGen/DIE.h"
22 #include "llvm/IR/DIBuilder.h"
23 #include "llvm/IR/DebugInfo.h"
24 #include "llvm/MC/MCDwarf.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCSection.h"
30 class MachineLocation
;
35 class DwarfCompileUnit
;
37 //===----------------------------------------------------------------------===//
38 /// This dwarf writer support class manages information associated with a
40 class DwarfUnit
: public DIEUnit
{
42 /// MDNode for the compile unit.
43 const DICompileUnit
*CUNode
;
45 // All DIEValues are allocated through this allocator.
46 BumpPtrAllocator DIEValueAllocator
;
48 /// Target of Dwarf emission.
51 /// Emitted at the end of the CU and used to compute the CU Length field.
52 MCSymbol
*EndLabel
= nullptr;
54 // Holders for some common dwarf information.
58 /// An anonymous type for index type. Owned by DIEUnit.
61 /// Tracks the mapping of unit level debug information variables to debug
62 /// information entries.
63 DenseMap
<const MDNode
*, DIE
*> MDNodeToDieMap
;
65 /// A list of all the DIEBlocks in use.
66 std::vector
<DIEBlock
*> DIEBlocks
;
68 /// A list of all the DIELocs in use.
69 std::vector
<DIELoc
*> DIELocs
;
71 /// This map is used to keep track of subprogram DIEs that need
72 /// DW_AT_containing_type attribute. This attribute points to a DIE that
73 /// corresponds to the MDNode mapped with the subprogram DIE.
74 DenseMap
<DIE
*, const DINode
*> ContainingTypeMap
;
76 DwarfUnit(dwarf::Tag
, const DICompileUnit
*Node
, AsmPrinter
*A
, DwarfDebug
*DW
,
79 bool applySubprogramDefinitionAttributes(const DISubprogram
*SP
, DIE
&SPDie
);
81 bool shareAcrossDWOCUs() const;
82 bool isShareableAcrossCUs(const DINode
*D
) const;
86 AsmPrinter
* getAsmPrinter() const { return Asm
; }
87 MCSymbol
*getEndLabel() const { return EndLabel
; }
88 uint16_t getLanguage() const { return CUNode
->getSourceLanguage(); }
89 const DICompileUnit
*getCUNode() const { return CUNode
; }
91 uint16_t getDwarfVersion() const { return DD
->getDwarfVersion(); }
93 /// Return true if this compile unit has something to write out.
94 bool hasContent() const { return getUnitDie().hasChildren(); }
96 /// Get string containing language specific context for a global name.
98 /// Walks the metadata parent chain in a language specific manner (using the
99 /// compile unit language) and returns it as a string. This is done at the
100 /// metadata level because DIEs may not currently have been added to the
101 /// parent context and walking the DIEs looking for names is more expensive
102 /// than walking the metadata.
103 std::string
getParentContextString(const DIScope
*Context
) const;
105 /// Add a new global name to the compile unit.
106 virtual void addGlobalName(StringRef Name
, const DIE
&Die
,
107 const DIScope
*Context
) = 0;
109 /// Add a new global type to the compile unit.
110 virtual void addGlobalType(const DIType
*Ty
, const DIE
&Die
,
111 const DIScope
*Context
) = 0;
113 /// Returns the DIE map slot for the specified debug variable.
115 /// We delegate the request to DwarfDebug when the MDNode can be part of the
116 /// type system, since DIEs for the type system can be shared across CUs and
117 /// the mappings are kept in DwarfDebug.
118 DIE
*getDIE(const DINode
*D
) const;
120 /// Returns a fresh newly allocated DIELoc.
121 DIELoc
*getDIELoc() { return new (DIEValueAllocator
) DIELoc
; }
123 /// Insert DIE into the map.
125 /// We delegate the request to DwarfDebug when the MDNode can be part of the
126 /// type system, since DIEs for the type system can be shared across CUs and
127 /// the mappings are kept in DwarfDebug.
128 void insertDIE(const DINode
*Desc
, DIE
*D
);
130 void insertDIE(DIE
*D
);
132 /// Add a flag that is true to the DIE.
133 void addFlag(DIE
&Die
, dwarf::Attribute Attribute
);
135 /// Add an unsigned integer attribute data and value.
136 void addUInt(DIEValueList
&Die
, dwarf::Attribute Attribute
,
137 Optional
<dwarf::Form
> Form
, uint64_t Integer
);
139 void addUInt(DIEValueList
&Block
, dwarf::Form Form
, uint64_t Integer
);
141 /// Add an signed integer attribute data and value.
142 void addSInt(DIEValueList
&Die
, dwarf::Attribute Attribute
,
143 Optional
<dwarf::Form
> Form
, int64_t Integer
);
145 void addSInt(DIELoc
&Die
, Optional
<dwarf::Form
> Form
, int64_t Integer
);
147 /// Add a string attribute data and value.
149 /// We always emit a reference to the string pool instead of immediate
150 /// strings so that DIEs have more predictable sizes. In the case of split
151 /// dwarf we emit an index into another table which gets us the static offset
152 /// into the string table.
153 void addString(DIE
&Die
, dwarf::Attribute Attribute
, StringRef Str
);
155 /// Add a Dwarf label attribute data and value.
156 DIEValueList::value_iterator
addLabel(DIEValueList
&Die
,
157 dwarf::Attribute Attribute
,
159 const MCSymbol
*Label
);
161 void addLabel(DIELoc
&Die
, dwarf::Form Form
, const MCSymbol
*Label
);
163 /// Add an offset into a section attribute data and value.
164 void addSectionOffset(DIE
&Die
, dwarf::Attribute Attribute
, uint64_t Integer
);
166 /// Add a dwarf op address data and value using the form given and an
167 /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
168 void addOpAddress(DIELoc
&Die
, const MCSymbol
*Sym
);
170 /// Add a label delta attribute data and value.
171 void addLabelDelta(DIE
&Die
, dwarf::Attribute Attribute
, const MCSymbol
*Hi
,
174 /// Add a DIE attribute data and value.
175 void addDIEEntry(DIE
&Die
, dwarf::Attribute Attribute
, DIE
&Entry
);
177 /// Add a DIE attribute data and value.
178 void addDIEEntry(DIE
&Die
, dwarf::Attribute Attribute
, DIEEntry Entry
);
180 /// Add a type's DW_AT_signature and set the declaration flag.
181 void addDIETypeSignature(DIE
&Die
, uint64_t Signature
);
184 void addBlock(DIE
&Die
, dwarf::Attribute Attribute
, DIELoc
*Loc
);
187 void addBlock(DIE
&Die
, dwarf::Attribute Attribute
, DIEBlock
*Block
);
189 /// Add location information to specified debug information entry.
190 void addSourceLine(DIE
&Die
, unsigned Line
, const DIFile
*File
);
191 void addSourceLine(DIE
&Die
, const DILocalVariable
*V
);
192 void addSourceLine(DIE
&Die
, const DIGlobalVariable
*G
);
193 void addSourceLine(DIE
&Die
, const DISubprogram
*SP
);
194 void addSourceLine(DIE
&Die
, const DILabel
*L
);
195 void addSourceLine(DIE
&Die
, const DIType
*Ty
);
196 void addSourceLine(DIE
&Die
, const DIObjCProperty
*Ty
);
198 /// Add constant value entry in variable DIE.
199 void addConstantValue(DIE
&Die
, const MachineOperand
&MO
, const DIType
*Ty
);
200 void addConstantValue(DIE
&Die
, const ConstantInt
*CI
, const DIType
*Ty
);
201 void addConstantValue(DIE
&Die
, const APInt
&Val
, const DIType
*Ty
);
202 void addConstantValue(DIE
&Die
, const APInt
&Val
, bool Unsigned
);
203 void addConstantValue(DIE
&Die
, uint64_t Val
, const DIType
*Ty
);
204 void addConstantValue(DIE
&Die
, bool Unsigned
, uint64_t Val
);
206 /// Add constant value entry in variable DIE.
207 void addConstantFPValue(DIE
&Die
, const MachineOperand
&MO
);
208 void addConstantFPValue(DIE
&Die
, const ConstantFP
*CFP
);
210 /// Add a linkage name, if it isn't empty.
211 void addLinkageName(DIE
&Die
, StringRef LinkageName
);
213 /// Add template parameters in buffer.
214 void addTemplateParams(DIE
&Buffer
, DINodeArray TParams
);
216 /// Add thrown types.
217 void addThrownTypes(DIE
&Die
, DINodeArray ThrownTypes
);
219 // FIXME: Should be reformulated in terms of addComplexAddress.
220 /// Start with the address based on the location provided, and generate the
221 /// DWARF information necessary to find the actual Block variable (navigating
222 /// the Block struct) based on the starting location. Add the DWARF
223 /// information to the die. Obsolete, please use addComplexAddress instead.
224 void addBlockByrefAddress(const DbgVariable
&DV
, DIE
&Die
,
225 dwarf::Attribute Attribute
,
226 const MachineLocation
&Location
);
228 /// Add a new type attribute to the specified entity.
230 /// This takes and attribute parameter because DW_AT_friend attributes are
231 /// also type references.
232 void addType(DIE
&Entity
, const DIType
*Ty
,
233 dwarf::Attribute Attribute
= dwarf::DW_AT_type
);
235 DIE
*getOrCreateNameSpace(const DINamespace
*NS
);
236 DIE
*getOrCreateModule(const DIModule
*M
);
237 DIE
*getOrCreateSubprogramDIE(const DISubprogram
*SP
, bool Minimal
= false);
239 void applySubprogramAttributes(const DISubprogram
*SP
, DIE
&SPDie
,
240 bool SkipSPAttributes
= false);
242 /// Creates type DIE with specific context.
243 DIE
*createTypeDIE(const DIScope
*Context
, DIE
&ContextDIE
, const DIType
*Ty
);
245 /// Find existing DIE or create new DIE for the given type.
246 DIE
*getOrCreateTypeDIE(const MDNode
*TyNode
);
248 /// Get context owner's DIE.
249 DIE
*getOrCreateContextDIE(const DIScope
*Context
);
251 /// Construct DIEs for types that contain vtables.
252 void constructContainingTypeDIEs();
254 /// Construct function argument DIEs.
255 void constructSubprogramArguments(DIE
&Buffer
, DITypeRefArray Args
);
257 /// Create a DIE with the given Tag, add the DIE to its parent, and
258 /// call insertDIE if MD is not null.
259 DIE
&createAndAddDIE(unsigned Tag
, DIE
&Parent
, const DINode
*N
= nullptr);
261 bool useSegmentedStringOffsetsTable() const {
262 return DD
->useSegmentedStringOffsetsTable();
265 /// Compute the size of a header for this unit, not including the initial
267 virtual unsigned getHeaderSize() const {
268 return sizeof(int16_t) + // DWARF version number
269 sizeof(int32_t) + // Offset Into Abbrev. Section
270 sizeof(int8_t) + // Pointer Size (in bytes)
271 (DD
->getDwarfVersion() >= 5 ? sizeof(int8_t)
272 : 0); // DWARF v5 unit type
275 /// Emit the header for this unit, not including the initial length field.
276 virtual void emitHeader(bool UseOffsets
) = 0;
278 /// Add the DW_AT_str_offsets_base attribute to the unit DIE.
279 void addStringOffsetsStart();
281 /// Add the DW_AT_rnglists_base attribute to the unit DIE.
282 void addRnglistsBase();
284 /// Add the DW_AT_loclists_base attribute to the unit DIE.
285 void addLoclistsBase();
287 virtual DwarfCompileUnit
&getCU() = 0;
289 void constructTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
);
291 /// addSectionDelta - Add a label delta attribute data and value.
292 DIE::value_iterator
addSectionDelta(DIE
&Die
, dwarf::Attribute Attribute
,
293 const MCSymbol
*Hi
, const MCSymbol
*Lo
);
295 /// Add a Dwarf section label attribute data and value.
296 DIE::value_iterator
addSectionLabel(DIE
&Die
, dwarf::Attribute Attribute
,
297 const MCSymbol
*Label
,
298 const MCSymbol
*Sec
);
300 /// If the \p File has an MD5 checksum, return it as an MD5Result
301 /// allocated in the MCContext.
302 Optional
<MD5::MD5Result
> getMD5AsBytes(const DIFile
*File
) const;
304 /// Get context owner's DIE.
305 DIE
*createTypeDIE(const DICompositeType
*Ty
);
310 /// Create new static data member DIE.
311 DIE
*getOrCreateStaticMemberDIE(const DIDerivedType
*DT
);
313 /// Look up the source ID for the given file. If none currently exists,
314 /// create a new ID and insert it in the line table.
315 virtual unsigned getOrCreateSourceID(const DIFile
*File
) = 0;
317 /// Emit the common part of the header for this unit.
318 void emitCommonHeader(bool UseOffsets
, dwarf::UnitType UT
);
321 void constructTypeDIE(DIE
&Buffer
, const DIBasicType
*BTy
);
322 void constructTypeDIE(DIE
&Buffer
, const DIDerivedType
*DTy
);
323 void constructTypeDIE(DIE
&Buffer
, const DISubroutineType
*CTy
);
324 void constructSubrangeDIE(DIE
&Buffer
, const DISubrange
*SR
, DIE
*IndexTy
);
325 void constructArrayTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
);
326 void constructEnumTypeDIE(DIE
&Buffer
, const DICompositeType
*CTy
);
327 DIE
&constructMemberDIE(DIE
&Buffer
, const DIDerivedType
*DT
);
328 void constructTemplateTypeParameterDIE(DIE
&Buffer
,
329 const DITemplateTypeParameter
*TP
);
330 void constructTemplateValueParameterDIE(DIE
&Buffer
,
331 const DITemplateValueParameter
*TVP
);
333 /// Return the default lower bound for an array.
335 /// If the DWARF version doesn't handle the language, return -1.
336 int64_t getDefaultLowerBound() const;
338 /// Get an anonymous type for index type.
339 DIE
*getIndexTyDie();
341 /// Set D as anonymous type for index which can be reused later.
342 void setIndexTyDie(DIE
*D
) { IndexTyDie
= D
; }
344 virtual void finishNonUnitTypeDIE(DIE
& D
, const DICompositeType
*CTy
) = 0;
346 /// If this is a named finished type then include it in the list of types for
347 /// the accelerator tables.
348 void updateAcceleratorTables(const DIScope
*Context
, const DIType
*Ty
,
351 virtual bool isDwoUnit() const = 0;
352 const MCSymbol
*getCrossSectionRelativeBaseAddress() const override
;
355 class DwarfTypeUnit final
: public DwarfUnit
{
356 uint64_t TypeSignature
;
358 DwarfCompileUnit
&CU
;
359 MCDwarfDwoLineTable
*SplitLineTable
;
360 bool UsedLineTable
= false;
362 unsigned getOrCreateSourceID(const DIFile
*File
) override
;
363 void finishNonUnitTypeDIE(DIE
& D
, const DICompositeType
*CTy
) override
;
364 bool isDwoUnit() const override
;
367 DwarfTypeUnit(DwarfCompileUnit
&CU
, AsmPrinter
*A
, DwarfDebug
*DW
,
368 DwarfFile
*DWU
, MCDwarfDwoLineTable
*SplitLineTable
= nullptr);
370 void setTypeSignature(uint64_t Signature
) { TypeSignature
= Signature
; }
371 void setType(const DIE
*Ty
) { this->Ty
= Ty
; }
373 /// Emit the header for this unit, not including the initial length field.
374 void emitHeader(bool UseOffsets
) override
;
375 unsigned getHeaderSize() const override
{
376 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
377 sizeof(uint32_t); // Type DIE Offset
379 void addGlobalName(StringRef Name
, const DIE
&Die
,
380 const DIScope
*Context
) override
;
381 void addGlobalType(const DIType
*Ty
, const DIE
&Die
,
382 const DIScope
*Context
) override
;
383 DwarfCompileUnit
&getCU() override
{ return CU
; }
385 } // end llvm namespace