1 //===-- llvm/CodeGen/DwarfUnit.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_DWARFUNIT_H
14 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
15 
16 #include "DwarfDebug.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/CodeGen/AsmPrinter.h"
20 #include "llvm/CodeGen/DIE.h"
21 #include <string>
22 
23 namespace llvm {
24 
25 class ConstantFP;
26 class ConstantInt;
27 class DbgVariable;
28 class DwarfCompileUnit;
29 class MachineOperand;
30 class MCDwarfDwoLineTable;
31 class MCSymbol;
32 
33 //===----------------------------------------------------------------------===//
34 /// This dwarf writer support class manages information associated with a
35 /// source file.
36 class DwarfUnit : public DIEUnit {
37 protected:
38   /// MDNode for the compile unit.
39   const DICompileUnit *CUNode;
40 
41   // All DIEValues are allocated through this allocator.
42   BumpPtrAllocator DIEValueAllocator;
43 
44   /// Target of Dwarf emission.
45   AsmPrinter *Asm;
46 
47   /// Emitted at the end of the CU and used to compute the CU Length field.
48   MCSymbol *EndLabel = nullptr;
49 
50   // Holders for some common dwarf information.
51   DwarfDebug *DD;
52   DwarfFile *DU;
53 
54   /// An anonymous type for index type.  Owned by DIEUnit.
55   DIE *IndexTyDie;
56 
57   /// Tracks the mapping of unit level debug information variables to debug
58   /// information entries.
59   DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
60 
61   /// A list of all the DIEBlocks in use.
62   std::vector<DIEBlock *> DIEBlocks;
63 
64   /// A list of all the DIELocs in use.
65   std::vector<DIELoc *> DIELocs;
66 
67   /// This map is used to keep track of subprogram DIEs that need
68   /// DW_AT_containing_type attribute. This attribute points to a DIE that
69   /// corresponds to the MDNode mapped with the subprogram DIE.
70   DenseMap<DIE *, const DINode *> ContainingTypeMap;
71 
72   DwarfUnit(dwarf::Tag, const DICompileUnit *Node, AsmPrinter *A, DwarfDebug *DW,
73             DwarfFile *DWU);
74 
75   bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie);
76 
77   bool isShareableAcrossCUs(const DINode *D) const;
78 
79 public:
80   // Accessors.
81   AsmPrinter* getAsmPrinter() const { return Asm; }
82   MCSymbol *getEndLabel() const { return EndLabel; }
83   uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
84   const DICompileUnit *getCUNode() const { return CUNode; }
85   DwarfDebug &getDwarfDebug() const { return *DD; }
86 
87   /// Return true if this compile unit has something to write out.
88   bool hasContent() const { return getUnitDie().hasChildren(); }
89 
90   /// Get string containing language specific context for a global name.
91   ///
92   /// Walks the metadata parent chain in a language specific manner (using the
93   /// compile unit language) and returns it as a string. This is done at the
94   /// metadata level because DIEs may not currently have been added to the
95   /// parent context and walking the DIEs looking for names is more expensive
96   /// than walking the metadata.
97   std::string getParentContextString(const DIScope *Context) const;
98 
99   /// Add a new global name to the compile unit.
100   virtual void addGlobalName(StringRef Name, const DIE &Die,
101                              const DIScope *Context) = 0;
102 
103   /// Add a new global type to the compile unit.
104   virtual void addGlobalType(const DIType *Ty, const DIE &Die,
105                              const DIScope *Context) = 0;
106 
107   /// Returns the DIE map slot for the specified debug variable.
108   ///
109   /// We delegate the request to DwarfDebug when the MDNode can be part of the
110   /// type system, since DIEs for the type system can be shared across CUs and
111   /// the mappings are kept in DwarfDebug.
112   DIE *getDIE(const DINode *D) const;
113 
114   /// Returns a fresh newly allocated DIELoc.
115   DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; }
116 
117   /// Insert DIE into the map.
118   ///
119   /// We delegate the request to DwarfDebug when the MDNode can be part of the
120   /// type system, since DIEs for the type system can be shared across CUs and
121   /// the mappings are kept in DwarfDebug.
122   void insertDIE(const DINode *Desc, DIE *D);
123 
124   void insertDIE(DIE *D);
125 
126   /// Add a flag that is true to the DIE.
127   void addFlag(DIE &Die, dwarf::Attribute Attribute);
128 
129   /// Add an unsigned integer attribute data and value.
130   void addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
131                Optional<dwarf::Form> Form, uint64_t Integer);
132 
133   void addUInt(DIEValueList &Block, dwarf::Form Form, uint64_t Integer);
134 
135   /// Add an signed integer attribute data and value.
136   void addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
137                Optional<dwarf::Form> Form, int64_t Integer);
138 
139   void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
140 
141   /// Add a string attribute data and value.
142   ///
143   /// We always emit a reference to the string pool instead of immediate
144   /// strings so that DIEs have more predictable sizes. In the case of split
145   /// dwarf we emit an index into another table which gets us the static offset
146   /// into the string table.
147   void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
148 
149   /// Add a Dwarf label attribute data and value.
150   DIEValueList::value_iterator addLabel(DIEValueList &Die,
151                                         dwarf::Attribute Attribute,
152                                         dwarf::Form Form,
153                                         const MCSymbol *Label);
154 
155   void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
156 
157   /// Add an offset into a section attribute data and value.
158   void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
159 
160   /// Add a dwarf op address data and value using the form given and an
161   /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
162   void addOpAddress(DIELoc &Die, const MCSymbol *Sym);
163 
164   /// Add a label delta attribute data and value.
165   void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
166                      const MCSymbol *Lo);
167 
168   /// Add a DIE attribute data and value.
169   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
170 
171   /// Add a DIE attribute data and value.
172   void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry);
173 
174   /// Add a type's DW_AT_signature and set the  declaration flag.
175   void addDIETypeSignature(DIE &Die, uint64_t Signature);
176 
177   /// Add block data.
178   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc);
179 
180   /// Add block data.
181   void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
182 
183   /// Add location information to specified debug information entry.
184   void addSourceLine(DIE &Die, unsigned Line, const DIFile *File);
185   void addSourceLine(DIE &Die, const DILocalVariable *V);
186   void addSourceLine(DIE &Die, const DIGlobalVariable *G);
187   void addSourceLine(DIE &Die, const DISubprogram *SP);
188   void addSourceLine(DIE &Die, const DILabel *L);
189   void addSourceLine(DIE &Die, const DIType *Ty);
190   void addSourceLine(DIE &Die, const DIObjCProperty *Ty);
191 
192   /// Add constant value entry in variable DIE.
193   void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty);
194   void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty);
195   void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
196   void addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty);
197   void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
198 
199   /// Add constant value entry in variable DIE.
200   void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
201 
202   /// Add a linkage name, if it isn't empty.
203   void addLinkageName(DIE &Die, StringRef LinkageName);
204 
205   /// Add template parameters in buffer.
206   void addTemplateParams(DIE &Buffer, DINodeArray TParams);
207 
208   /// Add thrown types.
209   void addThrownTypes(DIE &Die, DINodeArray ThrownTypes);
210 
211   /// Add a new type attribute to the specified entity.
212   ///
213   /// This takes and attribute parameter because DW_AT_friend attributes are
214   /// also type references.
215   void addType(DIE &Entity, const DIType *Ty,
216                dwarf::Attribute Attribute = dwarf::DW_AT_type);
217 
218   DIE *getOrCreateNameSpace(const DINamespace *NS);
219   DIE *getOrCreateModule(const DIModule *M);
220   DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false);
221 
222   void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
223                                  bool SkipSPAttributes = false);
224 
225   /// Creates type DIE with specific context.
226   DIE *createTypeDIE(const DIScope *Context, DIE &ContextDIE, const DIType *Ty);
227 
228   /// Find existing DIE or create new DIE for the given type.
229   DIE *getOrCreateTypeDIE(const MDNode *TyNode);
230 
231   /// Get context owner's DIE.
232   DIE *getOrCreateContextDIE(const DIScope *Context);
233 
234   /// Construct DIEs for types that contain vtables.
235   void constructContainingTypeDIEs();
236 
237   /// Construct function argument DIEs.
238   void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
239 
240   /// Create a DIE with the given Tag, add the DIE to its parent, and
241   /// call insertDIE if MD is not null.
242   DIE &createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N = nullptr);
243 
244   bool useSegmentedStringOffsetsTable() const {
245     return DD->useSegmentedStringOffsetsTable();
246   }
247 
248   /// Compute the size of a header for this unit, not including the initial
249   /// length field.
250   virtual unsigned getHeaderSize() const {
251     return sizeof(int16_t) +               // DWARF version number
252            Asm->getDwarfOffsetByteSize() + // Offset Into Abbrev. Section
253            sizeof(int8_t) +                // Pointer Size (in bytes)
254            (DD->getDwarfVersion() >= 5 ? sizeof(int8_t)
255                                        : 0); // DWARF v5 unit type
256   }
257 
258   /// Emit the header for this unit, not including the initial length field.
259   virtual void emitHeader(bool UseOffsets) = 0;
260 
261   /// Add the DW_AT_str_offsets_base attribute to the unit DIE.
262   void addStringOffsetsStart();
263 
264   /// Add the DW_AT_rnglists_base attribute to the unit DIE.
265   void addRnglistsBase();
266 
267   virtual DwarfCompileUnit &getCU() = 0;
268 
269   void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);
270 
271   /// addSectionDelta - Add a label delta attribute data and value.
272   DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
273                                       const MCSymbol *Hi, const MCSymbol *Lo);
274 
275   /// Add a Dwarf section label attribute data and value.
276   DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
277                                       const MCSymbol *Label,
278                                       const MCSymbol *Sec);
279 
280   /// Get context owner's DIE.
281   DIE *createTypeDIE(const DICompositeType *Ty);
282 
283 protected:
284   ~DwarfUnit();
285 
286   /// Create new static data member DIE.
287   DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT);
288 
289   /// Look up the source ID for the given file. If none currently exists,
290   /// create a new ID and insert it in the line table.
291   virtual unsigned getOrCreateSourceID(const DIFile *File) = 0;
292 
293   /// Emit the common part of the header for this unit.
294   void emitCommonHeader(bool UseOffsets, dwarf::UnitType UT);
295 
296 private:
297   void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy);
298   void constructTypeDIE(DIE &Buffer, const DIStringType *BTy);
299   void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy);
300   void constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy);
301   void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy);
302   void constructGenericSubrangeDIE(DIE &Buffer, const DIGenericSubrange *SR,
303                                    DIE *IndexTy);
304   void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy);
305   void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy);
306   DIE &constructMemberDIE(DIE &Buffer, const DIDerivedType *DT);
307   void constructTemplateTypeParameterDIE(DIE &Buffer,
308                                          const DITemplateTypeParameter *TP);
309   void constructTemplateValueParameterDIE(DIE &Buffer,
310                                           const DITemplateValueParameter *TVP);
311 
312   /// Return the default lower bound for an array.
313   ///
314   /// If the DWARF version doesn't handle the language, return -1.
315   int64_t getDefaultLowerBound() const;
316 
317   /// Get an anonymous type for index type.
318   DIE *getIndexTyDie();
319 
320   /// Set D as anonymous type for index which can be reused later.
321   void setIndexTyDie(DIE *D) { IndexTyDie = D; }
322 
323   virtual void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) = 0;
324 
325   /// If this is a named finished type then include it in the list of types for
326   /// the accelerator tables.
327   void updateAcceleratorTables(const DIScope *Context, const DIType *Ty,
328                                const DIE &TyDIE);
329 
330   virtual bool isDwoUnit() const = 0;
331   const MCSymbol *getCrossSectionRelativeBaseAddress() const override;
332 };
333 
334 class DwarfTypeUnit final : public DwarfUnit {
335   uint64_t TypeSignature;
336   const DIE *Ty;
337   DwarfCompileUnit &CU;
338   MCDwarfDwoLineTable *SplitLineTable;
339   bool UsedLineTable = false;
340 
341   unsigned getOrCreateSourceID(const DIFile *File) override;
342   void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
343   bool isDwoUnit() const override;
344 
345 public:
346   DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW,
347                 DwarfFile *DWU, MCDwarfDwoLineTable *SplitLineTable = nullptr);
348 
349   void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
350   void setType(const DIE *Ty) { this->Ty = Ty; }
351 
352   /// Emit the header for this unit, not including the initial length field.
353   void emitHeader(bool UseOffsets) override;
354   unsigned getHeaderSize() const override {
355     return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
356            Asm->getDwarfOffsetByteSize();                  // Type DIE Offset
357   }
358   void addGlobalName(StringRef Name, const DIE &Die,
359                      const DIScope *Context) override;
360   void addGlobalType(const DIType *Ty, const DIE &Die,
361                      const DIScope *Context) override;
362   DwarfCompileUnit &getCU() override { return CU; }
363 };
364 } // end llvm namespace
365 #endif
366