blob: 7a17b424199a9054b3ea13eec91584a5965a3e22 [file] [log] [blame]
David Blaikie319a05f2013-12-02 19:33:10 +00001//===-- llvm/CodeGen/DwarfUnit.h - Dwarf Compile Unit ---*- C++ -*--===//
Devang Patel5eb43192011-04-12 17:40:32 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Devang Patel5eb43192011-04-12 17:40:32 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing dwarf compile unit.
10//
11//===----------------------------------------------------------------------===//
12
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000013#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
14#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFUNIT_H
Devang Patel5eb43192011-04-12 17:40:32 +000015
Eric Christopher9e429ae2013-10-05 00:27:02 +000016#include "DwarfDebug.h"
Devang Patel5eb43192011-04-12 17:40:32 +000017#include "llvm/ADT/DenseMap.h"
David Blaikief2443192013-10-21 17:28:37 +000018#include "llvm/ADT/Optional.h"
Chandler Carruth802d7552012-12-04 07:12:27 +000019#include "llvm/ADT/StringMap.h"
Adrian Prantl702bf5a2014-03-18 02:34:52 +000020#include "llvm/CodeGen/AsmPrinter.h"
Frederic Risse541e0b2015-01-05 21:29:41 +000021#include "llvm/CodeGen/DIE.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000022#include "llvm/IR/DIBuilder.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000023#include "llvm/IR/DebugInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000024#include "llvm/MC/MCDwarf.h"
David Blaikief3cd7c52013-06-28 20:05:04 +000025#include "llvm/MC/MCExpr.h"
David Blaikie7d734602013-12-06 22:33:05 +000026#include "llvm/MC/MCSection.h"
Devang Patel5eb43192011-04-12 17:40:32 +000027
28namespace llvm {
29
Devang Patelf20c4f72011-04-12 22:53:02 +000030class MachineLocation;
31class MachineOperand;
32class ConstantInt;
David Blaikiea39a76e2013-01-20 01:18:01 +000033class ConstantFP;
Devang Patelf20c4f72011-04-12 22:53:02 +000034class DbgVariable;
David Blaikie15632ae2014-02-12 00:31:30 +000035class DwarfCompileUnit;
Devang Patelf20c4f72011-04-12 22:53:02 +000036
Devang Patel5eb43192011-04-12 17:40:32 +000037//===----------------------------------------------------------------------===//
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000038/// This dwarf writer support class manages information associated with a
39/// source file.
David Blaikie488393f2017-05-12 01:13:45 +000040class DwarfUnit : public DIEUnit {
David Blaikie319a05f2013-12-02 19:33:10 +000041protected:
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000042 /// MDNode for the compile unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000043 const DICompileUnit *CUNode;
David Blaikie7480ae62014-01-09 03:03:27 +000044
Duncan P. N. Exon Smith4fb1f9c2015-06-25 23:46:41 +000045 // All DIEValues are allocated through this allocator.
46 BumpPtrAllocator DIEValueAllocator;
47
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000048 /// Target of Dwarf emission.
Devang Patelf20c4f72011-04-12 22:53:02 +000049 AsmPrinter *Asm;
50
David Blaikiec4e08fe2018-12-18 01:06:09 +000051 /// Emitted at the end of the CU and used to compute the CU Length field.
52 MCSymbol *EndLabel = nullptr;
53
Eric Christophere698f532012-12-20 21:58:36 +000054 // Holders for some common dwarf information.
Devang Patelf20c4f72011-04-12 22:53:02 +000055 DwarfDebug *DD;
Eric Christopherf8194852013-12-05 18:06:10 +000056 DwarfFile *DU;
Devang Patelf20c4f72011-04-12 22:53:02 +000057
Greg Clayton35630c32016-12-01 18:56:29 +000058 /// An anonymous type for index type. Owned by DIEUnit.
Devang Patel5eb43192011-04-12 17:40:32 +000059 DIE *IndexTyDie;
60
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000061 /// Tracks the mapping of unit level debug information variables to debug
62 /// information entries.
Devang Patel5eb43192011-04-12 17:40:32 +000063 DenseMap<const MDNode *, DIE *> MDNodeToDieMap;
64
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000065 /// A list of all the DIEBlocks in use.
Devang Patelf20c4f72011-04-12 22:53:02 +000066 std::vector<DIEBlock *> DIEBlocks;
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000067
68 /// A list of all the DIELocs in use.
Eric Christopher4a741042014-02-16 08:46:55 +000069 std::vector<DIELoc *> DIELocs;
Devang Patelf20c4f72011-04-12 22:53:02 +000070
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000071 /// 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
Devang Patel89543712011-08-15 17:24:54 +000073 /// corresponds to the MDNode mapped with the subprogram DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000074 DenseMap<DIE *, const DINode *> ContainingTypeMap;
Devang Patel89543712011-08-15 17:24:54 +000075
Fangrui Songcb0bab82018-07-16 18:51:40 +000076 DwarfUnit(dwarf::Tag, const DICompileUnit *Node, AsmPrinter *A, DwarfDebug *DW,
Peter Collingbourne7c384cc2016-02-11 19:57:46 +000077 DwarfFile *DWU);
David Blaikie319a05f2013-12-02 19:33:10 +000078
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000079 bool applySubprogramDefinitionAttributes(const DISubprogram *SP, DIE &SPDie);
David Blaikie3a443c22014-11-04 22:12:25 +000080
David Blaikie488393f2017-05-12 01:13:45 +000081 bool shareAcrossDWOCUs() const;
82 bool isShareableAcrossCUs(const DINode *D) const;
83
Devang Patel5eb43192011-04-12 17:40:32 +000084public:
Devang Patel5eb43192011-04-12 17:40:32 +000085 // Accessors.
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000086 AsmPrinter* getAsmPrinter() const { return Asm; }
David Blaikiec4e08fe2018-12-18 01:06:09 +000087 MCSymbol *getEndLabel() const { return EndLabel; }
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +000088 uint16_t getLanguage() const { return CUNode->getSourceLanguage(); }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000089 const DICompileUnit *getCUNode() const { return CUNode; }
Devang Patel5eb43192011-04-12 17:40:32 +000090
Wolfgang Piebfcf38102018-07-12 18:18:21 +000091 uint16_t getDwarfVersion() const { return DD->getDwarfVersion(); }
92
Adrian Prantl857237e2015-07-13 18:25:29 +000093 /// Return true if this compile unit has something to write out.
David Blaikiee40caae2016-12-01 21:59:09 +000094 bool hasContent() const { return getUnitDie().hasChildren(); }
Devang Patel5eb43192011-04-12 17:40:32 +000095
Adrian Prantl857237e2015-07-13 18:25:29 +000096 /// Get string containing language specific context for a global name.
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +000097 ///
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.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000103 std::string getParentContextString(const DIScope *Context) const;
Eric Christopher2c8b7902013-10-17 02:06:06 +0000104
David Blaikie98cf1722014-11-02 06:06:14 +0000105 /// Add a new global name to the compile unit.
David Blaikiea0e3c752017-02-03 00:44:18 +0000106 virtual void addGlobalName(StringRef Name, const DIE &Die,
107 const DIScope *Context) = 0;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +0000108
David Blaikie98cf1722014-11-02 06:06:14 +0000109 /// Add a new global type to the compile unit.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000110 virtual void addGlobalType(const DIType *Ty, const DIE &Die,
David Blaikiea0e3c752017-02-03 00:44:18 +0000111 const DIScope *Context) = 0;
David Blaikie98cf1722014-11-02 06:06:14 +0000112
Adrian Prantl857237e2015-07-13 18:25:29 +0000113 /// Returns the DIE map slot for the specified debug variable.
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +0000114 ///
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.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000118 DIE *getDIE(const DINode *D) const;
Devang Patel5eb43192011-04-12 17:40:32 +0000119
Adrian Prantl857237e2015-07-13 18:25:29 +0000120 /// Returns a fresh newly allocated DIELoc.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000121 DIELoc *getDIELoc() { return new (DIEValueAllocator) DIELoc; }
Devang Patelf20c4f72011-04-12 22:53:02 +0000122
Adrian Prantl857237e2015-07-13 18:25:29 +0000123 /// Insert DIE into the map.
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +0000124 ///
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.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000128 void insertDIE(const DINode *Desc, DIE *D);
Devang Patel5eb43192011-04-12 17:40:32 +0000129
Adrian Prantl857237e2015-07-13 18:25:29 +0000130 /// Add a flag that is true to the DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000131 void addFlag(DIE &Die, dwarf::Attribute Attribute);
Eric Christopher48fef592012-12-20 21:58:40 +0000132
Adrian Prantl857237e2015-07-13 18:25:29 +0000133 /// Add an unsigned integer attribute data and value.
Duncan P. N. Exon Smith55a868a2015-08-02 20:44:46 +0000134 void addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
135 Optional<dwarf::Form> Form, uint64_t Integer);
David Blaikief2443192013-10-21 17:28:37 +0000136
Duncan P. N. Exon Smith55a868a2015-08-02 20:44:46 +0000137 void addUInt(DIEValueList &Block, dwarf::Form Form, uint64_t Integer);
Devang Patelf20c4f72011-04-12 22:53:02 +0000138
Adrian Prantl857237e2015-07-13 18:25:29 +0000139 /// Add an signed integer attribute data and value.
Duncan P. N. Exon Smith55a868a2015-08-02 20:44:46 +0000140 void addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
141 Optional<dwarf::Form> Form, int64_t Integer);
David Blaikief2443192013-10-21 17:28:37 +0000142
David Blaikie65a74662014-04-25 18:26:14 +0000143 void addSInt(DIELoc &Die, Optional<dwarf::Form> Form, int64_t Integer);
Devang Patelf20c4f72011-04-12 22:53:02 +0000144
Adrian Prantl857237e2015-07-13 18:25:29 +0000145 /// Add a string attribute data and value.
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +0000146 ///
147 /// We always emit a reference to the string pool instead of immediate
148 /// strings so that DIEs have more predictable sizes. In the case of split
149 /// dwarf we emit an index into another table which gets us the static offset
150 /// into the string table.
Craig Topper6dc4a8bc2014-08-30 16:48:02 +0000151 void addString(DIE &Die, dwarf::Attribute Attribute, StringRef Str);
Devang Patelf20c4f72011-04-12 22:53:02 +0000152
Adrian Prantl857237e2015-07-13 18:25:29 +0000153 /// Add a Dwarf label attribute data and value.
Duncan P. N. Exon Smith55a868a2015-08-02 20:44:46 +0000154 DIEValueList::value_iterator addLabel(DIEValueList &Die,
155 dwarf::Attribute Attribute,
156 dwarf::Form Form,
157 const MCSymbol *Label);
Devang Patelf20c4f72011-04-12 22:53:02 +0000158
David Blaikie65a74662014-04-25 18:26:14 +0000159 void addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label);
David Blaikief2443192013-10-21 17:28:37 +0000160
Adrian Prantl857237e2015-07-13 18:25:29 +0000161 /// Add an offset into a section attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000162 void addSectionOffset(DIE &Die, dwarf::Attribute Attribute, uint64_t Integer);
Eric Christopher33ff6972013-11-21 23:46:41 +0000163
Adrian Prantl857237e2015-07-13 18:25:29 +0000164 /// Add a dwarf op address data and value using the form given and an
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +0000165 /// op of either DW_FORM_addr or DW_FORM_GNU_addr_index.
Fangrui Songcb0bab82018-07-16 18:51:40 +0000166 void addOpAddress(DIELoc &Die, const MCSymbol *Sym);
Eric Christophere9ec2452013-01-18 22:11:33 +0000167
Adrian Prantl857237e2015-07-13 18:25:29 +0000168 /// Add a label delta attribute data and value.
David Blaikie65a74662014-04-25 18:26:14 +0000169 void addLabelDelta(DIE &Die, dwarf::Attribute Attribute, const MCSymbol *Hi,
David Blaikie48b1bdc2014-03-07 01:30:55 +0000170 const MCSymbol *Lo);
171
Adrian Prantl857237e2015-07-13 18:25:29 +0000172 /// Add a DIE attribute data and value.
David Blaikie8dbcc3f2014-04-25 19:33:43 +0000173 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry);
Eric Christopher48fef592012-12-20 21:58:40 +0000174
Adrian Prantl857237e2015-07-13 18:25:29 +0000175 /// Add a DIE attribute data and value.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +0000176 void addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIEEntry Entry);
Manman Ren4dbdc902013-10-31 17:54:35 +0000177
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000178 /// Add a type's DW_AT_signature and set the declaration flag.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +0000179 void addDIETypeSignature(DIE &Die, uint64_t Signature);
David Blaikie47f615e2013-12-17 23:32:35 +0000180
Adrian Prantl857237e2015-07-13 18:25:29 +0000181 /// Add block data.
Fangrui Songcb0bab82018-07-16 18:51:40 +0000182 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc);
Eric Christopher4a741042014-02-16 08:46:55 +0000183
Adrian Prantl857237e2015-07-13 18:25:29 +0000184 /// Add block data.
David Blaikie65a74662014-04-25 18:26:14 +0000185 void addBlock(DIE &Die, dwarf::Attribute Attribute, DIEBlock *Block);
Devang Patelf20c4f72011-04-12 22:53:02 +0000186
Adrian Prantl857237e2015-07-13 18:25:29 +0000187 /// Add location information to specified debug information entry.
Paul Robinson612e89d2018-01-12 19:17:50 +0000188 void addSourceLine(DIE &Die, unsigned Line, const DIFile *File);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000189 void addSourceLine(DIE &Die, const DILocalVariable *V);
190 void addSourceLine(DIE &Die, const DIGlobalVariable *G);
191 void addSourceLine(DIE &Die, const DISubprogram *SP);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000192 void addSourceLine(DIE &Die, const DILabel *L);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000193 void addSourceLine(DIE &Die, const DIType *Ty);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000194 void addSourceLine(DIE &Die, const DIObjCProperty *Ty);
Devang Patelf20c4f72011-04-12 22:53:02 +0000195
Adrian Prantl857237e2015-07-13 18:25:29 +0000196 /// Add constant value entry in variable DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000197 void addConstantValue(DIE &Die, const MachineOperand &MO, const DIType *Ty);
198 void addConstantValue(DIE &Die, const ConstantInt *CI, const DIType *Ty);
199 void addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty);
David Blaikie65a74662014-04-25 18:26:14 +0000200 void addConstantValue(DIE &Die, const APInt &Val, bool Unsigned);
David Blaikie60cae1b2014-05-11 16:08:41 +0000201 void addConstantValue(DIE &Die, bool Unsigned, uint64_t Val);
Devang Patelf20c4f72011-04-12 22:53:02 +0000202
Adrian Prantl857237e2015-07-13 18:25:29 +0000203 /// Add constant value entry in variable DIE.
David Blaikie65a74662014-04-25 18:26:14 +0000204 void addConstantFPValue(DIE &Die, const MachineOperand &MO);
205 void addConstantFPValue(DIE &Die, const ConstantFP *CFP);
Devang Patelf20c4f72011-04-12 22:53:02 +0000206
Adrian Prantl857237e2015-07-13 18:25:29 +0000207 /// Add a linkage name, if it isn't empty.
Paul Robinson857b4432015-03-10 22:44:45 +0000208 void addLinkageName(DIE &Die, StringRef LinkageName);
209
Adrian Prantl857237e2015-07-13 18:25:29 +0000210 /// Add template parameters in buffer.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000211 void addTemplateParams(DIE &Buffer, DINodeArray TParams);
Devang Patelf20c4f72011-04-12 22:53:02 +0000212
Adrian Prantl1d12b882017-04-26 22:56:44 +0000213 /// Add thrown types.
214 void addThrownTypes(DIE &Die, DINodeArray ThrownTypes);
215
Devang Patelf20c4f72011-04-12 22:53:02 +0000216 // FIXME: Should be reformulated in terms of addComplexAddress.
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +0000217 /// Start with the address based on the location provided, and generate the
218 /// DWARF information necessary to find the actual Block variable (navigating
219 /// the Block struct) based on the starting location. Add the DWARF
220 /// information to the die. Obsolete, please use addComplexAddress instead.
David Blaikie65a74662014-04-25 18:26:14 +0000221 void addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
Eric Christophera07e4f52013-11-19 09:28:34 +0000222 dwarf::Attribute Attribute,
Devang Patelf20c4f72011-04-12 22:53:02 +0000223 const MachineLocation &Location);
224
Adrian Prantl857237e2015-07-13 18:25:29 +0000225 /// Add a new type attribute to the specified entity.
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +0000226 ///
227 /// This takes and attribute parameter because DW_AT_friend attributes are
228 /// also type references.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000229 void addType(DIE &Entity, const DIType *Ty,
Eric Christophera07e4f52013-11-19 09:28:34 +0000230 dwarf::Attribute Attribute = dwarf::DW_AT_type);
Devang Patelf20c4f72011-04-12 22:53:02 +0000231
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000232 DIE *getOrCreateNameSpace(const DINamespace *NS);
Adrian Prantl08a388b2015-06-30 02:13:04 +0000233 DIE *getOrCreateModule(const DIModule *M);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000234 DIE *getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal = false);
Devang Patel89543712011-08-15 17:24:54 +0000235
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000236 void applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
Dehao Chen1ce8d6c2017-01-19 00:44:11 +0000237 bool SkipSPAttributes = false);
David Blaikie7f916862014-05-27 18:37:38 +0000238
Eugene Leviantdaea28a2019-03-04 07:15:36 +0000239 /// Creates type DIE with specific context.
240 DIE *createTypeDIE(const DIScope *Context, DIE &ContextDIE, const DIType *Ty);
241
Adrian Prantl857237e2015-07-13 18:25:29 +0000242 /// Find existing DIE or create new DIE for the given type.
Fangrui Songcb0bab82018-07-16 18:51:40 +0000243 DIE *getOrCreateTypeDIE(const MDNode *TyNode);
Devang Patelf20c4f72011-04-12 22:53:02 +0000244
Adrian Prantl857237e2015-07-13 18:25:29 +0000245 /// Get context owner's DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000246 DIE *getOrCreateContextDIE(const DIScope *Context);
Devang Patelf20c4f72011-04-12 22:53:02 +0000247
Adrian Prantl857237e2015-07-13 18:25:29 +0000248 /// Construct DIEs for types that contain vtables.
Eric Christopherfa205ca2013-10-05 00:05:51 +0000249 void constructContainingTypeDIEs();
Devang Patelf20c4f72011-04-12 22:53:02 +0000250
Adrian Prantl857237e2015-07-13 18:25:29 +0000251 /// Construct function argument DIEs.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000252 void constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args);
Adrian Prantl69140d22014-02-25 22:27:14 +0000253
Manman Renb987e512013-10-29 00:53:03 +0000254 /// Create a DIE with the given Tag, add the DIE to its parent, and
255 /// call insertDIE if MD is not null.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000256 DIE &createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N = nullptr);
Manman Renb987e512013-10-29 00:53:03 +0000257
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000258 bool useSegmentedStringOffsetsTable() const {
259 return DD->useSegmentedStringOffsetsTable();
260 }
261
David Blaikie6b288cf2013-10-30 20:42:41 +0000262 /// Compute the size of a header for this unit, not including the initial
263 /// length field.
David Blaikiebc563272013-12-13 21:33:40 +0000264 virtual unsigned getHeaderSize() const {
David Blaikie6b288cf2013-10-30 20:42:41 +0000265 return sizeof(int16_t) + // DWARF version number
266 sizeof(int32_t) + // Offset Into Abbrev. Section
Paul Robinsoncddd6042017-02-28 20:24:55 +0000267 sizeof(int8_t) + // Pointer Size (in bytes)
268 (DD->getDwarfVersion() >= 5 ? sizeof(int8_t)
269 : 0); // DWARF v5 unit type
David Blaikie6b288cf2013-10-30 20:42:41 +0000270 }
271
272 /// Emit the header for this unit, not including the initial length field.
Paul Robinsoncddd6042017-02-28 20:24:55 +0000273 virtual void emitHeader(bool UseOffsets) = 0;
David Blaikie6b288cf2013-10-30 20:42:41 +0000274
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000275 /// Add the DW_AT_str_offsets_base attribute to the unit DIE.
276 void addStringOffsetsStart();
277
Wolfgang Piebfcf38102018-07-12 18:18:21 +0000278 /// Add the DW_AT_rnglists_base attribute to the unit DIE.
279 void addRnglistsBase();
280
George Rimar088d96b2018-10-26 11:25:12 +0000281 /// Add the DW_AT_loclists_base attribute to the unit DIE.
282 void addLoclistsBase();
283
David Blaikie15632ae2014-02-12 00:31:30 +0000284 virtual DwarfCompileUnit &getCU() = 0;
David Blaikied696fac2014-02-12 00:32:05 +0000285
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000286 void constructTypeDIE(DIE &Buffer, const DICompositeType *CTy);
David Blaikiee12b49a2014-04-26 17:27:38 +0000287
Wolfgang Piebe60147c2017-06-30 00:27:45 +0000288 /// addSectionDelta - Add a label delta attribute data and value.
289 DIE::value_iterator addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
290 const MCSymbol *Hi, const MCSymbol *Lo);
291
292 /// Add a Dwarf section label attribute data and value.
293 DIE::value_iterator addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
294 const MCSymbol *Label,
295 const MCSymbol *Sec);
296
Paul Robinsonb271f312018-03-29 17:16:41 +0000297 /// If the \p File has an MD5 checksum, return it as an MD5Result
298 /// allocated in the MCContext.
Eric Christopher798e83b2019-04-04 23:34:38 +0000299 Optional<MD5::MD5Result> getMD5AsBytes(const DIFile *File) const;
Paul Robinsonb271f312018-03-29 17:16:41 +0000300
David Blaikie832c7d92019-04-24 18:09:44 +0000301 /// Get context owner's DIE.
302 DIE *createTypeDIE(const DICompositeType *Ty);
303
David Blaikie319a05f2013-12-02 19:33:10 +0000304protected:
David Blaikiec0bb21f2017-04-22 02:18:00 +0000305 ~DwarfUnit();
306
Adrian Prantl857237e2015-07-13 18:25:29 +0000307 /// Create new static data member DIE.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000308 DIE *getOrCreateStaticMemberDIE(const DIDerivedType *DT);
David Blaikie319a05f2013-12-02 19:33:10 +0000309
Paul Robinson612e89d2018-01-12 19:17:50 +0000310 /// Look up the source ID for the given file. If none currently exists,
311 /// create a new ID and insert it in the line table.
312 virtual unsigned getOrCreateSourceID(const DIFile *File) = 0;
313
Paul Robinsoncddd6042017-02-28 20:24:55 +0000314 /// Emit the common part of the header for this unit.
315 void emitCommonHeader(bool UseOffsets, dwarf::UnitType UT);
316
Eric Christopherfa205ca2013-10-05 00:05:51 +0000317private:
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000318 void constructTypeDIE(DIE &Buffer, const DIBasicType *BTy);
319 void constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy);
Fangrui Songcb0bab82018-07-16 18:51:40 +0000320 void constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000321 void constructSubrangeDIE(DIE &Buffer, const DISubrange *SR, DIE *IndexTy);
322 void constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy);
323 void constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy);
Adrian Prantl8c599212018-02-06 23:45:59 +0000324 DIE &constructMemberDIE(DIE &Buffer, const DIDerivedType *DT);
Manman Renffc9a712013-10-23 23:05:28 +0000325 void constructTemplateTypeParameterDIE(DIE &Buffer,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000326 const DITemplateTypeParameter *TP);
Manman Renffc9a712013-10-23 23:05:28 +0000327 void constructTemplateValueParameterDIE(DIE &Buffer,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000328 const DITemplateValueParameter *TVP);
Devang Patelf20c4f72011-04-12 22:53:02 +0000329
Adrian Prantl857237e2015-07-13 18:25:29 +0000330 /// Return the default lower bound for an array.
Duncan P. N. Exon Smithd89ef162015-04-20 20:29:51 +0000331 ///
332 /// If the DWARF version doesn't handle the language, return -1.
Eric Christopherfa205ca2013-10-05 00:05:51 +0000333 int64_t getDefaultLowerBound() const;
334
Adrian Prantl857237e2015-07-13 18:25:29 +0000335 /// Get an anonymous type for index type.
David Blaikie871c2d92014-11-02 03:09:13 +0000336 DIE *getIndexTyDie();
Eric Christopherfa205ca2013-10-05 00:05:51 +0000337
Adrian Prantl857237e2015-07-13 18:25:29 +0000338 /// Set D as anonymous type for index which can be reused later.
Eric Christopherfa205ca2013-10-05 00:05:51 +0000339 void setIndexTyDie(DIE *D) { IndexTyDie = D; }
340
David Blaikie832c7d92019-04-24 18:09:44 +0000341 virtual void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) = 0;
342
343 /// If this is a named finished type then include it in the list of types for
344 /// the accelerator tables.
345 void updateAcceleratorTables(const DIScope *Context, const DIType *Ty,
346 const DIE &TyDIE);
347
David Blaikiecafd9622014-11-02 08:51:37 +0000348 virtual bool isDwoUnit() const = 0;
David Blaikie85366ac2017-04-22 07:53:44 +0000349 const MCSymbol *getCrossSectionRelativeBaseAddress() const override;
Devang Patel5eb43192011-04-12 17:40:32 +0000350};
351
David Blaikiec0bb21f2017-04-22 02:18:00 +0000352class DwarfTypeUnit final : public DwarfUnit {
David Blaikiebc563272013-12-13 21:33:40 +0000353 uint64_t TypeSignature;
354 const DIE *Ty;
David Blaikie15632ae2014-02-12 00:31:30 +0000355 DwarfCompileUnit &CU;
David Blaikie8287aff2014-03-18 02:13:23 +0000356 MCDwarfDwoLineTable *SplitLineTable;
Paul Robinson7cb26ad2018-03-27 21:28:59 +0000357 bool UsedLineTable = false;
David Blaikie319a05f2013-12-02 19:33:10 +0000358
Paul Robinson612e89d2018-01-12 19:17:50 +0000359 unsigned getOrCreateSourceID(const DIFile *File) override;
David Blaikie832c7d92019-04-24 18:09:44 +0000360 void finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) override;
David Blaikiecafd9622014-11-02 08:51:37 +0000361 bool isDwoUnit() const override;
362
David Blaikie319a05f2013-12-02 19:33:10 +0000363public:
Peter Collingbourne7c384cc2016-02-11 19:57:46 +0000364 DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A, DwarfDebug *DW,
365 DwarfFile *DWU, MCDwarfDwoLineTable *SplitLineTable = nullptr);
David Blaikie319a05f2013-12-02 19:33:10 +0000366
David Blaikiebc563272013-12-13 21:33:40 +0000367 void setTypeSignature(uint64_t Signature) { TypeSignature = Signature; }
368 void setType(const DIE *Ty) { this->Ty = Ty; }
369
David Blaikiebc563272013-12-13 21:33:40 +0000370 /// Emit the header for this unit, not including the initial length field.
Rafael Espindola063d7252015-03-10 16:58:10 +0000371 void emitHeader(bool UseOffsets) override;
Craig Topper73156022014-03-02 09:09:27 +0000372 unsigned getHeaderSize() const override {
David Blaikiebc563272013-12-13 21:33:40 +0000373 return DwarfUnit::getHeaderSize() + sizeof(uint64_t) + // Type Signature
374 sizeof(uint32_t); // Type DIE Offset
375 }
David Blaikiea0e3c752017-02-03 00:44:18 +0000376 void addGlobalName(StringRef Name, const DIE &Die,
377 const DIScope *Context) override;
378 void addGlobalType(const DIType *Ty, const DIE &Die,
379 const DIScope *Context) override;
Craig Topper73156022014-03-02 09:09:27 +0000380 DwarfCompileUnit &getCU() override { return CU; }
David Blaikie319a05f2013-12-02 19:33:10 +0000381};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000382} // end llvm namespace
Devang Patel5eb43192011-04-12 17:40:32 +0000383#endif