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