]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/AsmPrinter/DwarfUnit.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304460, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / CodeGen / AsmPrinter / DwarfUnit.cpp
1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for constructing a dwarf compile unit.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DwarfUnit.h"
15 #include "AddressPool.h"
16 #include "DwarfCompileUnit.h"
17 #include "DwarfDebug.h"
18 #include "DwarfExpression.h"
19 #include "llvm/ADT/APFloat.h"
20 #include "llvm/ADT/APInt.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineOperand.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/Metadata.h"
29 #include "llvm/MC/MachineLocation.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCDwarf.h"
32 #include "llvm/MC/MCSection.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Target/TargetLoweringObjectFile.h"
37 #include "llvm/Target/TargetRegisterInfo.h"
38 #include "llvm/Target/TargetSubtargetInfo.h"
39 #include <cassert>
40 #include <cstdint>
41 #include <string>
42 #include <utility>
43
44 using namespace llvm;
45
46 #define DEBUG_TYPE "dwarfdebug"
47
48 static cl::opt<bool>
49 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
50                        cl::desc("Generate DWARF4 type units."),
51                        cl::init(false));
52
53 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU,
54                                        DIELoc &DIE)
55     : DwarfExpression(AP.getDwarfVersion()), AP(AP), DU(DU),
56       DIE(DIE) {}
57
58 void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
59   DU.addUInt(DIE, dwarf::DW_FORM_data1, Op);
60 }
61
62 void DIEDwarfExpression::emitSigned(int64_t Value) {
63   DU.addSInt(DIE, dwarf::DW_FORM_sdata, Value);
64 }
65
66 void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
67   DU.addUInt(DIE, dwarf::DW_FORM_udata, Value);
68 }
69
70 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
71                                          unsigned MachineReg) {
72   return MachineReg == TRI.getFrameRegister(*AP.MF);
73 }
74
75 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
76                      AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
77     : DIEUnit(A->getDwarfVersion(), A->MAI->getCodePointerSize(), UnitTag),
78       CUNode(Node), Asm(A), DD(DW), DU(DWU), IndexTyDie(nullptr) {
79 }
80
81 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
82                              DwarfDebug *DW, DwarfFile *DWU,
83                              MCDwarfDwoLineTable *SplitLineTable)
84     : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU),
85       SplitLineTable(SplitLineTable) {
86   if (SplitLineTable)
87     addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
88 }
89
90 DwarfUnit::~DwarfUnit() {
91   for (unsigned j = 0, M = DIEBlocks.size(); j < M; ++j)
92     DIEBlocks[j]->~DIEBlock();
93   for (unsigned j = 0, M = DIELocs.size(); j < M; ++j)
94     DIELocs[j]->~DIELoc();
95 }
96
97 int64_t DwarfUnit::getDefaultLowerBound() const {
98   switch (getLanguage()) {
99   default:
100     break;
101
102   // The languages below have valid values in all DWARF versions.
103   case dwarf::DW_LANG_C:
104   case dwarf::DW_LANG_C89:
105   case dwarf::DW_LANG_C_plus_plus:
106     return 0;
107
108   case dwarf::DW_LANG_Fortran77:
109   case dwarf::DW_LANG_Fortran90:
110     return 1;
111
112   // The languages below have valid values only if the DWARF version >= 3.
113   case dwarf::DW_LANG_C99:
114   case dwarf::DW_LANG_ObjC:
115   case dwarf::DW_LANG_ObjC_plus_plus:
116     if (DD->getDwarfVersion() >= 3)
117       return 0;
118     break;
119
120   case dwarf::DW_LANG_Fortran95:
121     if (DD->getDwarfVersion() >= 3)
122       return 1;
123     break;
124
125   // Starting with DWARF v4, all defined languages have valid values.
126   case dwarf::DW_LANG_D:
127   case dwarf::DW_LANG_Java:
128   case dwarf::DW_LANG_Python:
129   case dwarf::DW_LANG_UPC:
130     if (DD->getDwarfVersion() >= 4)
131       return 0;
132     break;
133
134   case dwarf::DW_LANG_Ada83:
135   case dwarf::DW_LANG_Ada95:
136   case dwarf::DW_LANG_Cobol74:
137   case dwarf::DW_LANG_Cobol85:
138   case dwarf::DW_LANG_Modula2:
139   case dwarf::DW_LANG_Pascal83:
140   case dwarf::DW_LANG_PLI:
141     if (DD->getDwarfVersion() >= 4)
142       return 1;
143     break;
144
145   // The languages below are new in DWARF v5.
146   case dwarf::DW_LANG_BLISS:
147   case dwarf::DW_LANG_C11:
148   case dwarf::DW_LANG_C_plus_plus_03:
149   case dwarf::DW_LANG_C_plus_plus_11:
150   case dwarf::DW_LANG_C_plus_plus_14:
151   case dwarf::DW_LANG_Dylan:
152   case dwarf::DW_LANG_Go:
153   case dwarf::DW_LANG_Haskell:
154   case dwarf::DW_LANG_OCaml:
155   case dwarf::DW_LANG_OpenCL:
156   case dwarf::DW_LANG_RenderScript:
157   case dwarf::DW_LANG_Rust:
158   case dwarf::DW_LANG_Swift:
159     if (DD->getDwarfVersion() >= 5)
160       return 0;
161     break;
162
163   case dwarf::DW_LANG_Fortran03:
164   case dwarf::DW_LANG_Fortran08:
165   case dwarf::DW_LANG_Julia:
166   case dwarf::DW_LANG_Modula3:
167     if (DD->getDwarfVersion() >= 5)
168       return 1;
169     break;
170   }
171
172   return -1;
173 }
174
175 /// Check whether the DIE for this MDNode can be shared across CUs.
176 bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const {
177   // When the MDNode can be part of the type system, the DIE can be shared
178   // across CUs.
179   // Combining type units and cross-CU DIE sharing is lower value (since
180   // cross-CU DIE sharing is used in LTO and removes type redundancy at that
181   // level already) but may be implementable for some value in projects
182   // building multiple independent libraries with LTO and then linking those
183   // together.
184   if (isDwoUnit() && !DD->shareAcrossDWOCUs())
185     return false;
186   return (isa<DIType>(D) ||
187           (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
188          !GenerateDwarfTypeUnits;
189 }
190
191 DIE *DwarfUnit::getDIE(const DINode *D) const {
192   if (isShareableAcrossCUs(D))
193     return DU->getDIE(D);
194   return MDNodeToDieMap.lookup(D);
195 }
196
197 void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) {
198   if (isShareableAcrossCUs(Desc)) {
199     DU->insertDIE(Desc, D);
200     return;
201   }
202   MDNodeToDieMap.insert(std::make_pair(Desc, D));
203 }
204
205 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
206   if (DD->getDwarfVersion() >= 4)
207     Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag_present,
208                  DIEInteger(1));
209   else
210     Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_flag,
211                  DIEInteger(1));
212 }
213
214 void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
215                         Optional<dwarf::Form> Form, uint64_t Integer) {
216   if (!Form)
217     Form = DIEInteger::BestForm(false, Integer);
218   assert(Form != dwarf::DW_FORM_implicit_const &&
219          "DW_FORM_implicit_const is used only for signed integers");
220   Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer));
221 }
222
223 void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form,
224                         uint64_t Integer) {
225   addUInt(Block, (dwarf::Attribute)0, Form, Integer);
226 }
227
228 void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
229                         Optional<dwarf::Form> Form, int64_t Integer) {
230   if (!Form)
231     Form = DIEInteger::BestForm(true, Integer);
232   Die.addValue(DIEValueAllocator, Attribute, *Form, DIEInteger(Integer));
233 }
234
235 void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
236                         int64_t Integer) {
237   addSInt(Die, (dwarf::Attribute)0, Form, Integer);
238 }
239
240 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
241                           StringRef String) {
242   Die.addValue(DIEValueAllocator, Attribute,
243                isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp,
244                DIEString(DU->getStringPool().getEntry(*Asm, String)));
245 }
246
247 DIEValueList::value_iterator DwarfUnit::addLabel(DIEValueList &Die,
248                                                  dwarf::Attribute Attribute,
249                                                  dwarf::Form Form,
250                                                  const MCSymbol *Label) {
251   return Die.addValue(DIEValueAllocator, Attribute, Form, DIELabel(Label));
252 }
253
254 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
255   addLabel(Die, (dwarf::Attribute)0, Form, Label);
256 }
257
258 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
259                                  uint64_t Integer) {
260   if (DD->getDwarfVersion() >= 4)
261     addUInt(Die, Attribute, dwarf::DW_FORM_sec_offset, Integer);
262   else
263     addUInt(Die, Attribute, dwarf::DW_FORM_data4, Integer);
264 }
265
266 unsigned DwarfTypeUnit::getOrCreateSourceID(StringRef FileName, StringRef DirName) {
267   return SplitLineTable ? SplitLineTable->getFile(DirName, FileName)
268                         : getCU().getOrCreateSourceID(FileName, DirName);
269 }
270
271 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
272   if (!DD->useSplitDwarf()) {
273     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
274     addLabel(Die, dwarf::DW_FORM_udata, Sym);
275   } else {
276     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
277     addUInt(Die, dwarf::DW_FORM_GNU_addr_index,
278             DD->getAddressPool().getIndex(Sym));
279   }
280 }
281
282 void DwarfUnit::addLabelDelta(DIE &Die, dwarf::Attribute Attribute,
283                               const MCSymbol *Hi, const MCSymbol *Lo) {
284   Die.addValue(DIEValueAllocator, Attribute, dwarf::DW_FORM_data4,
285                new (DIEValueAllocator) DIEDelta(Hi, Lo));
286 }
287
288 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
289   addDIEEntry(Die, Attribute, DIEEntry(Entry));
290 }
291
292 void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
293   // Flag the type unit reference as a declaration so that if it contains
294   // members (implicit special members, static data member definitions, member
295   // declarations for definitions in this CU, etc) consumers don't get confused
296   // and think this is a full definition.
297   addFlag(Die, dwarf::DW_AT_declaration);
298
299   Die.addValue(DIEValueAllocator, dwarf::DW_AT_signature,
300                dwarf::DW_FORM_ref_sig8, DIEInteger(Signature));
301 }
302
303 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
304                             DIEEntry Entry) {
305   const DIEUnit *CU = Die.getUnit();
306   const DIEUnit *EntryCU = Entry.getEntry().getUnit();
307   if (!CU)
308     // We assume that Die belongs to this CU, if it is not linked to any CU yet.
309     CU = getUnitDie().getUnit();
310   if (!EntryCU)
311     EntryCU = getUnitDie().getUnit();
312   Die.addValue(DIEValueAllocator, Attribute,
313                EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
314                Entry);
315 }
316
317 DIE &DwarfUnit::createAndAddDIE(unsigned Tag, DIE &Parent, const DINode *N) {
318   DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, (dwarf::Tag)Tag));
319   if (N)
320     insertDIE(N, &Die);
321   return Die;
322 }
323
324 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
325   Loc->ComputeSize(Asm);
326   DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
327   Die.addValue(DIEValueAllocator, Attribute,
328                Loc->BestForm(DD->getDwarfVersion()), Loc);
329 }
330
331 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
332                          DIEBlock *Block) {
333   Block->ComputeSize(Asm);
334   DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
335   Die.addValue(DIEValueAllocator, Attribute, Block->BestForm(), Block);
336 }
337
338 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, StringRef File,
339                               StringRef Directory) {
340   if (Line == 0)
341     return;
342
343   unsigned FileID = getOrCreateSourceID(File, Directory);
344   assert(FileID && "Invalid file id");
345   addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
346   addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
347 }
348
349 void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) {
350   assert(V);
351
352   addSourceLine(Die, V->getLine(), V->getScope()->getFilename(),
353                 V->getScope()->getDirectory());
354 }
355
356 void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) {
357   assert(G);
358
359   addSourceLine(Die, G->getLine(), G->getFilename(), G->getDirectory());
360 }
361
362 void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) {
363   assert(SP);
364
365   addSourceLine(Die, SP->getLine(), SP->getFilename(), SP->getDirectory());
366 }
367
368 void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
369   assert(Ty);
370
371   addSourceLine(Die, Ty->getLine(), Ty->getFilename(), Ty->getDirectory());
372 }
373
374 void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) {
375   assert(Ty);
376
377   addSourceLine(Die, Ty->getLine(), Ty->getFilename(), Ty->getDirectory());
378 }
379
380 /* Byref variables, in Blocks, are declared by the programmer as "SomeType
381    VarName;", but the compiler creates a __Block_byref_x_VarName struct, and
382    gives the variable VarName either the struct, or a pointer to the struct, as
383    its type.  This is necessary for various behind-the-scenes things the
384    compiler needs to do with by-reference variables in Blocks.
385
386    However, as far as the original *programmer* is concerned, the variable
387    should still have type 'SomeType', as originally declared.
388
389    The function getBlockByrefType dives into the __Block_byref_x_VarName
390    struct to find the original type of the variable, which is then assigned to
391    the variable's Debug Information Entry as its real type.  So far, so good.
392    However now the debugger will expect the variable VarName to have the type
393    SomeType.  So we need the location attribute for the variable to be an
394    expression that explains to the debugger how to navigate through the
395    pointers and struct to find the actual variable of type SomeType.
396
397    The following function does just that.  We start by getting
398    the "normal" location for the variable. This will be the location
399    of either the struct __Block_byref_x_VarName or the pointer to the
400    struct __Block_byref_x_VarName.
401
402    The struct will look something like:
403
404    struct __Block_byref_x_VarName {
405      ... <various fields>
406      struct __Block_byref_x_VarName *forwarding;
407      ... <various other fields>
408      SomeType VarName;
409      ... <maybe more fields>
410    };
411
412    If we are given the struct directly (as our starting point) we
413    need to tell the debugger to:
414
415    1).  Add the offset of the forwarding field.
416
417    2).  Follow that pointer to get the real __Block_byref_x_VarName
418    struct to use (the real one may have been copied onto the heap).
419
420    3).  Add the offset for the field VarName, to find the actual variable.
421
422    If we started with a pointer to the struct, then we need to
423    dereference that pointer first, before the other steps.
424    Translating this into DWARF ops, we will need to append the following
425    to the current location description for the variable:
426
427    DW_OP_deref                    -- optional, if we start with a pointer
428    DW_OP_plus_uconst <forward_fld_offset>
429    DW_OP_deref
430    DW_OP_plus_uconst <varName_fld_offset>
431
432    That is what this function does.  */
433
434 void DwarfUnit::addBlockByrefAddress(const DbgVariable &DV, DIE &Die,
435                                      dwarf::Attribute Attribute,
436                                      const MachineLocation &Location) {
437   const DIType *Ty = DV.getType();
438   const DIType *TmpTy = Ty;
439   uint16_t Tag = Ty->getTag();
440   bool isPointer = false;
441
442   StringRef varName = DV.getName();
443
444   if (Tag == dwarf::DW_TAG_pointer_type) {
445     auto *DTy = cast<DIDerivedType>(Ty);
446     TmpTy = resolve(DTy->getBaseType());
447     isPointer = true;
448   }
449
450   // Find the __forwarding field and the variable field in the __Block_byref
451   // struct.
452   DINodeArray Fields = cast<DICompositeType>(TmpTy)->getElements();
453   const DIDerivedType *varField = nullptr;
454   const DIDerivedType *forwardingField = nullptr;
455
456   for (unsigned i = 0, N = Fields.size(); i < N; ++i) {
457     auto *DT = cast<DIDerivedType>(Fields[i]);
458     StringRef fieldName = DT->getName();
459     if (fieldName == "__forwarding")
460       forwardingField = DT;
461     else if (fieldName == varName)
462       varField = DT;
463   }
464
465   // Get the offsets for the forwarding field and the variable field.
466   unsigned forwardingFieldOffset = forwardingField->getOffsetInBits() >> 3;
467   unsigned varFieldOffset = varField->getOffsetInBits() >> 2;
468
469   // Decode the original location, and use that as the start of the byref
470   // variable's location.
471   DIELoc *Loc = new (DIEValueAllocator) DIELoc;
472   DIEDwarfExpression DwarfExpr(*Asm, *this, *Loc);
473   if (Location.isIndirect())
474     DwarfExpr.setMemoryLocationKind();
475
476   SmallVector<uint64_t, 9> Ops;
477   if (Location.isIndirect() && Location.getOffset()) {
478     Ops.push_back(dwarf::DW_OP_plus);
479     Ops.push_back(Location.getOffset());
480   }
481   // If we started with a pointer to the __Block_byref... struct, then
482   // the first thing we need to do is dereference the pointer (DW_OP_deref).
483   if (isPointer)
484     Ops.push_back(dwarf::DW_OP_deref);
485
486   // Next add the offset for the '__forwarding' field:
487   // DW_OP_plus_uconst ForwardingFieldOffset.  Note there's no point in
488   // adding the offset if it's 0.
489   if (forwardingFieldOffset > 0) {
490     Ops.push_back(dwarf::DW_OP_plus);
491     Ops.push_back(forwardingFieldOffset);
492   }
493
494   // Now dereference the __forwarding field to get to the real __Block_byref
495   // struct:  DW_OP_deref.
496   Ops.push_back(dwarf::DW_OP_deref);
497
498   // Now that we've got the real __Block_byref... struct, add the offset
499   // for the variable's field to get to the location of the actual variable:
500   // DW_OP_plus_uconst varFieldOffset.  Again, don't add if it's 0.
501   if (varFieldOffset > 0) {
502     Ops.push_back(dwarf::DW_OP_plus);
503     Ops.push_back(varFieldOffset);
504   }
505
506   DIExpressionCursor Cursor(Ops);
507   const TargetRegisterInfo &TRI = *Asm->MF->getSubtarget().getRegisterInfo();
508   if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
509     return;
510   DwarfExpr.addExpression(std::move(Cursor));
511
512   // Now attach the location information to the DIE.
513   addBlock(Die, Attribute, DwarfExpr.finalize());
514 }
515
516 /// Return true if type encoding is unsigned.
517 static bool isUnsignedDIType(DwarfDebug *DD, const DIType *Ty) {
518   if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
519     // FIXME: Enums without a fixed underlying type have unknown signedness
520     // here, leading to incorrectly emitted constants.
521     if (CTy->getTag() == dwarf::DW_TAG_enumeration_type)
522       return false;
523
524     // (Pieces of) aggregate types that get hacked apart by SROA may be
525     // represented by a constant. Encode them as unsigned bytes.
526     return true;
527   }
528
529   if (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
530     dwarf::Tag T = (dwarf::Tag)Ty->getTag();
531     // Encode pointer constants as unsigned bytes. This is used at least for
532     // null pointer constant emission.
533     // FIXME: reference and rvalue_reference /probably/ shouldn't be allowed
534     // here, but accept them for now due to a bug in SROA producing bogus
535     // dbg.values.
536     if (T == dwarf::DW_TAG_pointer_type ||
537         T == dwarf::DW_TAG_ptr_to_member_type ||
538         T == dwarf::DW_TAG_reference_type ||
539         T == dwarf::DW_TAG_rvalue_reference_type)
540       return true;
541     assert(T == dwarf::DW_TAG_typedef || T == dwarf::DW_TAG_const_type ||
542            T == dwarf::DW_TAG_volatile_type ||
543            T == dwarf::DW_TAG_restrict_type || T == dwarf::DW_TAG_atomic_type);
544     DITypeRef Deriv = DTy->getBaseType();
545     assert(Deriv && "Expected valid base type");
546     return isUnsignedDIType(DD, DD->resolve(Deriv));
547   }
548
549   auto *BTy = cast<DIBasicType>(Ty);
550   unsigned Encoding = BTy->getEncoding();
551   assert((Encoding == dwarf::DW_ATE_unsigned ||
552           Encoding == dwarf::DW_ATE_unsigned_char ||
553           Encoding == dwarf::DW_ATE_signed ||
554           Encoding == dwarf::DW_ATE_signed_char ||
555           Encoding == dwarf::DW_ATE_float || Encoding == dwarf::DW_ATE_UTF ||
556           Encoding == dwarf::DW_ATE_boolean ||
557           (Ty->getTag() == dwarf::DW_TAG_unspecified_type &&
558            Ty->getName() == "decltype(nullptr)")) &&
559          "Unsupported encoding");
560   return Encoding == dwarf::DW_ATE_unsigned ||
561          Encoding == dwarf::DW_ATE_unsigned_char ||
562          Encoding == dwarf::DW_ATE_UTF || Encoding == dwarf::DW_ATE_boolean ||
563          Ty->getTag() == dwarf::DW_TAG_unspecified_type;
564 }
565
566 void DwarfUnit::addConstantFPValue(DIE &Die, const MachineOperand &MO) {
567   assert(MO.isFPImm() && "Invalid machine operand!");
568   DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
569   APFloat FPImm = MO.getFPImm()->getValueAPF();
570
571   // Get the raw data form of the floating point.
572   const APInt FltVal = FPImm.bitcastToAPInt();
573   const char *FltPtr = (const char *)FltVal.getRawData();
574
575   int NumBytes = FltVal.getBitWidth() / 8; // 8 bits per byte.
576   bool LittleEndian = Asm->getDataLayout().isLittleEndian();
577   int Incr = (LittleEndian ? 1 : -1);
578   int Start = (LittleEndian ? 0 : NumBytes - 1);
579   int Stop = (LittleEndian ? NumBytes : -1);
580
581   // Output the constant to DWARF one byte at a time.
582   for (; Start != Stop; Start += Incr)
583     addUInt(*Block, dwarf::DW_FORM_data1, (unsigned char)0xFF & FltPtr[Start]);
584
585   addBlock(Die, dwarf::DW_AT_const_value, Block);
586 }
587
588 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
589   // Pass this down to addConstantValue as an unsigned bag of bits.
590   addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
591 }
592
593 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
594                                  const DIType *Ty) {
595   addConstantValue(Die, CI->getValue(), Ty);
596 }
597
598 void DwarfUnit::addConstantValue(DIE &Die, const MachineOperand &MO,
599                                  const DIType *Ty) {
600   assert(MO.isImm() && "Invalid machine operand!");
601
602   addConstantValue(Die, isUnsignedDIType(DD, Ty), MO.getImm());
603 }
604
605 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
606   // FIXME: This is a bit conservative/simple - it emits negative values always
607   // sign extended to 64 bits rather than minimizing the number of bytes.
608   addUInt(Die, dwarf::DW_AT_const_value,
609           Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
610 }
611
612 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
613   addConstantValue(Die, Val, isUnsignedDIType(DD, Ty));
614 }
615
616 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
617   unsigned CIBitWidth = Val.getBitWidth();
618   if (CIBitWidth <= 64) {
619     addConstantValue(Die, Unsigned,
620                      Unsigned ? Val.getZExtValue() : Val.getSExtValue());
621     return;
622   }
623
624   DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
625
626   // Get the raw data form of the large APInt.
627   const uint64_t *Ptr64 = Val.getRawData();
628
629   int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
630   bool LittleEndian = Asm->getDataLayout().isLittleEndian();
631
632   // Output the constant to DWARF one byte at a time.
633   for (int i = 0; i < NumBytes; i++) {
634     uint8_t c;
635     if (LittleEndian)
636       c = Ptr64[i / 8] >> (8 * (i & 7));
637     else
638       c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
639     addUInt(*Block, dwarf::DW_FORM_data1, c);
640   }
641
642   addBlock(Die, dwarf::DW_AT_const_value, Block);
643 }
644
645 void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) {
646   if (!LinkageName.empty())
647     addString(Die,
648               DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
649                                          : dwarf::DW_AT_MIPS_linkage_name,
650               GlobalValue::dropLLVMManglingEscape(LinkageName));
651 }
652
653 void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
654   // Add template parameters.
655   for (const auto *Element : TParams) {
656     if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
657       constructTemplateTypeParameterDIE(Buffer, TTP);
658     else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
659       constructTemplateValueParameterDIE(Buffer, TVP);
660   }
661 }
662
663 /// Add thrown types.
664 void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
665   for (const auto *Ty : ThrownTypes) {
666     DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
667     addType(TT, cast<DIType>(Ty));
668   }
669 }
670
671 DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) {
672   if (!Context || isa<DIFile>(Context))
673     return &getUnitDie();
674   if (auto *T = dyn_cast<DIType>(Context))
675     return getOrCreateTypeDIE(T);
676   if (auto *NS = dyn_cast<DINamespace>(Context))
677     return getOrCreateNameSpace(NS);
678   if (auto *SP = dyn_cast<DISubprogram>(Context))
679     return getOrCreateSubprogramDIE(SP);
680   if (auto *M = dyn_cast<DIModule>(Context))
681     return getOrCreateModule(M);
682   return getDIE(Context);
683 }
684
685 DIE *DwarfTypeUnit::createTypeDIE(const DICompositeType *Ty) {
686   auto *Context = resolve(Ty->getScope());
687   DIE *ContextDIE = getOrCreateContextDIE(Context);
688
689   if (DIE *TyDIE = getDIE(Ty))
690     return TyDIE;
691
692   // Create new type.
693   DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
694
695   constructTypeDIE(TyDIE, cast<DICompositeType>(Ty));
696
697   updateAcceleratorTables(Context, Ty, TyDIE);
698   return &TyDIE;
699 }
700
701 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
702   if (!TyNode)
703     return nullptr;
704
705   auto *Ty = cast<DIType>(TyNode);
706
707   // DW_TAG_restrict_type is not supported in DWARF2
708   if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
709     return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType()));
710
711   // DW_TAG_atomic_type is not supported in DWARF < 5
712   if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
713     return getOrCreateTypeDIE(resolve(cast<DIDerivedType>(Ty)->getBaseType()));
714
715   // Construct the context before querying for the existence of the DIE in case
716   // such construction creates the DIE.
717   auto *Context = resolve(Ty->getScope());
718   DIE *ContextDIE = getOrCreateContextDIE(Context);
719   assert(ContextDIE);
720
721   if (DIE *TyDIE = getDIE(Ty))
722     return TyDIE;
723
724   // Create new type.
725   DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
726
727   updateAcceleratorTables(Context, Ty, TyDIE);
728
729   if (auto *BT = dyn_cast<DIBasicType>(Ty))
730     constructTypeDIE(TyDIE, BT);
731   else if (auto *STy = dyn_cast<DISubroutineType>(Ty))
732     constructTypeDIE(TyDIE, STy);
733   else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
734     if (GenerateDwarfTypeUnits && !Ty->isForwardDecl())
735       if (MDString *TypeId = CTy->getRawIdentifier()) {
736         DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
737         // Skip updating the accelerator tables since this is not the full type.
738         return &TyDIE;
739       }
740     constructTypeDIE(TyDIE, CTy);
741   } else {
742     constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty));
743   }
744
745   return &TyDIE;
746 }
747
748 void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
749                                         const DIType *Ty, const DIE &TyDIE) {
750   if (!Ty->getName().empty() && !Ty->isForwardDecl()) {
751     bool IsImplementation = false;
752     if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
753       // A runtime language of 0 actually means C/C++ and that any
754       // non-negative value is some version of Objective-C/C++.
755       IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
756     }
757     unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
758     DD->addAccelType(Ty->getName(), TyDIE, Flags);
759
760     if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
761         isa<DINamespace>(Context))
762       addGlobalType(Ty, TyDIE, Context);
763   }
764 }
765
766 void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
767                         dwarf::Attribute Attribute) {
768   assert(Ty && "Trying to add a type that doesn't exist?");
769   addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty)));
770 }
771
772 std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
773   if (!Context)
774     return "";
775
776   // FIXME: Decide whether to implement this for non-C++ languages.
777   if (getLanguage() != dwarf::DW_LANG_C_plus_plus)
778     return "";
779
780   std::string CS;
781   SmallVector<const DIScope *, 1> Parents;
782   while (!isa<DICompileUnit>(Context)) {
783     Parents.push_back(Context);
784     if (Context->getScope())
785       Context = resolve(Context->getScope());
786     else
787       // Structure, etc types will have a NULL context if they're at the top
788       // level.
789       break;
790   }
791
792   // Reverse iterate over our list to go from the outermost construct to the
793   // innermost.
794   for (const DIScope *Ctx : make_range(Parents.rbegin(), Parents.rend())) {
795     StringRef Name = Ctx->getName();
796     if (Name.empty() && isa<DINamespace>(Ctx))
797       Name = "(anonymous namespace)";
798     if (!Name.empty()) {
799       CS += Name;
800       CS += "::";
801     }
802   }
803   return CS;
804 }
805
806 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
807   // Get core information.
808   StringRef Name = BTy->getName();
809   // Add name if not anonymous or intermediate type.
810   if (!Name.empty())
811     addString(Buffer, dwarf::DW_AT_name, Name);
812
813   // An unspecified type only has a name attribute.
814   if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
815     return;
816
817   addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
818           BTy->getEncoding());
819
820   uint64_t Size = BTy->getSizeInBits() >> 3;
821   addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
822 }
823
824 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
825   // Get core information.
826   StringRef Name = DTy->getName();
827   uint64_t Size = DTy->getSizeInBits() >> 3;
828   uint16_t Tag = Buffer.getTag();
829
830   // Map to main type, void will not have a type.
831   const DIType *FromTy = resolve(DTy->getBaseType());
832   if (FromTy)
833     addType(Buffer, FromTy);
834
835   // Add name if not anonymous or intermediate type.
836   if (!Name.empty())
837     addString(Buffer, dwarf::DW_AT_name, Name);
838
839   // Add size if non-zero (derived types might be zero-sized.)
840   if (Size && Tag != dwarf::DW_TAG_pointer_type
841            && Tag != dwarf::DW_TAG_ptr_to_member_type
842            && Tag != dwarf::DW_TAG_reference_type
843            && Tag != dwarf::DW_TAG_rvalue_reference_type)
844     addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
845
846   if (Tag == dwarf::DW_TAG_ptr_to_member_type)
847     addDIEEntry(
848         Buffer, dwarf::DW_AT_containing_type,
849         *getOrCreateTypeDIE(resolve(cast<DIDerivedType>(DTy)->getClassType())));
850   // Add source line info if available and TyDesc is not a forward declaration.
851   if (!DTy->isForwardDecl())
852     addSourceLine(Buffer, DTy);
853
854   // If DWARF address space value is other than None, add it for pointer and
855   // reference types as DW_AT_address_class.
856   if (DTy->getDWARFAddressSpace() && (Tag == dwarf::DW_TAG_pointer_type ||
857                                       Tag == dwarf::DW_TAG_reference_type))
858     addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
859             DTy->getDWARFAddressSpace().getValue());
860 }
861
862 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
863   for (unsigned i = 1, N = Args.size(); i < N; ++i) {
864     const DIType *Ty = resolve(Args[i]);
865     if (!Ty) {
866       assert(i == N-1 && "Unspecified parameter must be the last argument");
867       createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
868     } else {
869       DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
870       addType(Arg, Ty);
871       if (Ty->isArtificial())
872         addFlag(Arg, dwarf::DW_AT_artificial);
873     }
874   }
875 }
876
877 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
878   // Add return type.  A void return won't have a type.
879   auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
880   if (Elements.size())
881     if (auto RTy = resolve(Elements[0]))
882       addType(Buffer, RTy);
883
884   bool isPrototyped = true;
885   if (Elements.size() == 2 && !Elements[1])
886     isPrototyped = false;
887
888   constructSubprogramArguments(Buffer, Elements);
889
890   // Add prototype flag if we're dealing with a C language and the function has
891   // been prototyped.
892   uint16_t Language = getLanguage();
893   if (isPrototyped &&
894       (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
895        Language == dwarf::DW_LANG_ObjC))
896     addFlag(Buffer, dwarf::DW_AT_prototyped);
897
898   // Add a DW_AT_calling_convention if this has an explicit convention.
899   if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
900     addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
901             CTy->getCC());
902
903   if (CTy->isLValueReference())
904     addFlag(Buffer, dwarf::DW_AT_reference);
905
906   if (CTy->isRValueReference())
907     addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
908 }
909
910 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
911   // Add name if not anonymous or intermediate type.
912   StringRef Name = CTy->getName();
913
914   uint64_t Size = CTy->getSizeInBits() >> 3;
915   uint16_t Tag = Buffer.getTag();
916
917   switch (Tag) {
918   case dwarf::DW_TAG_array_type:
919     constructArrayTypeDIE(Buffer, CTy);
920     break;
921   case dwarf::DW_TAG_enumeration_type:
922     constructEnumTypeDIE(Buffer, CTy);
923     break;
924   case dwarf::DW_TAG_structure_type:
925   case dwarf::DW_TAG_union_type:
926   case dwarf::DW_TAG_class_type: {
927     // Add elements to structure type.
928     DINodeArray Elements = CTy->getElements();
929     for (const auto *Element : Elements) {
930       if (!Element)
931         continue;
932       if (auto *SP = dyn_cast<DISubprogram>(Element))
933         getOrCreateSubprogramDIE(SP);
934       else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
935         if (DDTy->getTag() == dwarf::DW_TAG_friend) {
936           DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
937           addType(ElemDie, resolve(DDTy->getBaseType()), dwarf::DW_AT_friend);
938         } else if (DDTy->isStaticMember()) {
939           getOrCreateStaticMemberDIE(DDTy);
940         } else {
941           constructMemberDIE(Buffer, DDTy);
942         }
943       } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
944         DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer);
945         StringRef PropertyName = Property->getName();
946         addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
947         if (Property->getType())
948           addType(ElemDie, resolve(Property->getType()));
949         addSourceLine(ElemDie, Property);
950         StringRef GetterName = Property->getGetterName();
951         if (!GetterName.empty())
952           addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
953         StringRef SetterName = Property->getSetterName();
954         if (!SetterName.empty())
955           addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
956         if (unsigned PropertyAttributes = Property->getAttributes())
957           addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
958                   PropertyAttributes);
959       }
960     }
961
962     if (CTy->isAppleBlockExtension())
963       addFlag(Buffer, dwarf::DW_AT_APPLE_block);
964
965     // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
966     // inside C++ composite types to point to the base class with the vtable.
967     if (auto *ContainingType =
968             dyn_cast_or_null<DICompositeType>(resolve(CTy->getVTableHolder())))
969       addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
970                   *getOrCreateTypeDIE(ContainingType));
971
972     if (CTy->isObjcClassComplete())
973       addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
974
975     // Add template parameters to a class, structure or union types.
976     // FIXME: The support isn't in the metadata for this yet.
977     if (Tag == dwarf::DW_TAG_class_type ||
978         Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
979       addTemplateParams(Buffer, CTy->getTemplateParams());
980
981     break;
982   }
983   default:
984     break;
985   }
986
987   // Add name if not anonymous or intermediate type.
988   if (!Name.empty())
989     addString(Buffer, dwarf::DW_AT_name, Name);
990
991   if (Tag == dwarf::DW_TAG_enumeration_type ||
992       Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
993       Tag == dwarf::DW_TAG_union_type) {
994     // Add size if non-zero (derived types might be zero-sized.)
995     // TODO: Do we care about size for enum forward declarations?
996     if (Size)
997       addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
998     else if (!CTy->isForwardDecl())
999       // Add zero size if it is not a forward declaration.
1000       addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
1001
1002     // If we're a forward decl, say so.
1003     if (CTy->isForwardDecl())
1004       addFlag(Buffer, dwarf::DW_AT_declaration);
1005
1006     // Add source line info if available.
1007     if (!CTy->isForwardDecl())
1008       addSourceLine(Buffer, CTy);
1009
1010     // No harm in adding the runtime language to the declaration.
1011     unsigned RLang = CTy->getRuntimeLang();
1012     if (RLang)
1013       addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1014               RLang);
1015
1016     // Add align info if available.
1017     if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1018       addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1019               AlignInBytes);
1020   }
1021 }
1022
1023 void DwarfUnit::constructTemplateTypeParameterDIE(
1024     DIE &Buffer, const DITemplateTypeParameter *TP) {
1025   DIE &ParamDIE =
1026       createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1027   // Add the type if it exists, it could be void and therefore no type.
1028   if (TP->getType())
1029     addType(ParamDIE, resolve(TP->getType()));
1030   if (!TP->getName().empty())
1031     addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1032 }
1033
1034 void DwarfUnit::constructTemplateValueParameterDIE(
1035     DIE &Buffer, const DITemplateValueParameter *VP) {
1036   DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1037
1038   // Add the type if there is one, template template and template parameter
1039   // packs will not have a type.
1040   if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1041     addType(ParamDIE, resolve(VP->getType()));
1042   if (!VP->getName().empty())
1043     addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1044   if (Metadata *Val = VP->getValue()) {
1045     if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
1046       addConstantValue(ParamDIE, CI, resolve(VP->getType()));
1047     else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1048       // We cannot describe the location of dllimport'd entities: the
1049       // computation of their address requires loads from the IAT.
1050       if (!GV->hasDLLImportStorageClass()) {
1051         // For declaration non-type template parameters (such as global values
1052         // and functions)
1053         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1054         addOpAddress(*Loc, Asm->getSymbol(GV));
1055         // Emit DW_OP_stack_value to use the address as the immediate value of
1056         // the parameter, rather than a pointer to it.
1057         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1058         addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1059       }
1060     } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1061       assert(isa<MDString>(Val));
1062       addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1063                 cast<MDString>(Val)->getString());
1064     } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1065       addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1066     }
1067   }
1068 }
1069
1070 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) {
1071   // Construct the context before querying for the existence of the DIE in case
1072   // such construction creates the DIE.
1073   DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1074
1075   if (DIE *NDie = getDIE(NS))
1076     return NDie;
1077   DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1078
1079   StringRef Name = NS->getName();
1080   if (!Name.empty())
1081     addString(NDie, dwarf::DW_AT_name, NS->getName());
1082   else
1083     Name = "(anonymous namespace)";
1084   DD->addAccelNamespace(Name, NDie);
1085   addGlobalName(Name, NDie, NS->getScope());
1086   if (NS->getExportSymbols())
1087     addFlag(NDie, dwarf::DW_AT_export_symbols);
1088   return &NDie;
1089 }
1090
1091 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) {
1092   // Construct the context before querying for the existence of the DIE in case
1093   // such construction creates the DIE.
1094   DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1095
1096   if (DIE *MDie = getDIE(M))
1097     return MDie;
1098   DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1099
1100   if (!M->getName().empty()) {
1101     addString(MDie, dwarf::DW_AT_name, M->getName());
1102     addGlobalName(M->getName(), MDie, M->getScope());
1103   }
1104   if (!M->getConfigurationMacros().empty())
1105     addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1106               M->getConfigurationMacros());
1107   if (!M->getIncludePath().empty())
1108     addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1109   if (!M->getISysRoot().empty())
1110     addString(MDie, dwarf::DW_AT_LLVM_isysroot, M->getISysRoot());
1111   
1112   return &MDie;
1113 }
1114
1115 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) {
1116   // Construct the context before querying for the existence of the DIE in case
1117   // such construction creates the DIE (as is the case for member function
1118   // declarations).
1119   DIE *ContextDIE =
1120       Minimal ? &getUnitDie() : getOrCreateContextDIE(resolve(SP->getScope()));
1121
1122   if (DIE *SPDie = getDIE(SP))
1123     return SPDie;
1124
1125   if (auto *SPDecl = SP->getDeclaration()) {
1126     if (!Minimal) {
1127       // Add subprogram definitions to the CU die directly.
1128       ContextDIE = &getUnitDie();
1129       // Build the decl now to ensure it precedes the definition.
1130       getOrCreateSubprogramDIE(SPDecl);
1131     }
1132   }
1133
1134   // DW_TAG_inlined_subroutine may refer to this DIE.
1135   DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1136
1137   // Stop here and fill this in later, depending on whether or not this
1138   // subprogram turns out to have inlined instances or not.
1139   if (SP->isDefinition())
1140     return &SPDie;
1141
1142   applySubprogramAttributes(SP, SPDie);
1143   return &SPDie;
1144 }
1145
1146 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP,
1147                                                     DIE &SPDie) {
1148   DIE *DeclDie = nullptr;
1149   StringRef DeclLinkageName;
1150   if (auto *SPDecl = SP->getDeclaration()) {
1151     DeclDie = getDIE(SPDecl);
1152     assert(DeclDie && "This DIE should've already been constructed when the "
1153                       "definition DIE was created in "
1154                       "getOrCreateSubprogramDIE");
1155     // Look at the Decl's linkage name only if we emitted it.
1156     if (DD->useAllLinkageNames())
1157       DeclLinkageName = SPDecl->getLinkageName();
1158     unsigned DeclID =
1159         getOrCreateSourceID(SPDecl->getFilename(), SPDecl->getDirectory());
1160     unsigned DefID = getOrCreateSourceID(SP->getFilename(), SP->getDirectory());
1161     if (DeclID != DefID)
1162       addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID);
1163
1164     if (SP->getLine() != SPDecl->getLine())
1165       addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine());
1166   }
1167
1168   // Add function template parameters.
1169   addTemplateParams(SPDie, SP->getTemplateParams());
1170
1171   // Add the linkage name if we have one and it isn't in the Decl.
1172   StringRef LinkageName = SP->getLinkageName();
1173   assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1174           LinkageName == DeclLinkageName) &&
1175          "decl has a linkage name and it is different");
1176   if (DeclLinkageName.empty() &&
1177       // Always emit it for abstract subprograms.
1178       (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP)))
1179     addLinkageName(SPDie, LinkageName);
1180
1181   if (!DeclDie)
1182     return false;
1183
1184   // Refer to the function declaration where all the other attributes will be
1185   // found.
1186   addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1187   return true;
1188 }
1189
1190 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
1191                                           bool SkipSPAttributes) {
1192   // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1193   // and its source location.
1194   bool SkipSPSourceLocation = SkipSPAttributes &&
1195                               !CUNode->getDebugInfoForProfiling();
1196   if (!SkipSPSourceLocation)
1197     if (applySubprogramDefinitionAttributes(SP, SPDie))
1198       return;
1199
1200   // Constructors and operators for anonymous aggregates do not have names.
1201   if (!SP->getName().empty())
1202     addString(SPDie, dwarf::DW_AT_name, SP->getName());
1203
1204   if (!SkipSPSourceLocation)
1205     addSourceLine(SPDie, SP);
1206
1207   // Skip the rest of the attributes under -gmlt to save space.
1208   if (SkipSPAttributes)
1209     return;
1210
1211   // Add the prototype if we have a prototype and we have a C like
1212   // language.
1213   uint16_t Language = getLanguage();
1214   if (SP->isPrototyped() &&
1215       (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1216        Language == dwarf::DW_LANG_ObjC))
1217     addFlag(SPDie, dwarf::DW_AT_prototyped);
1218
1219   unsigned CC = 0;
1220   DITypeRefArray Args;
1221   if (const DISubroutineType *SPTy = SP->getType()) {
1222     Args = SPTy->getTypeArray();
1223     CC = SPTy->getCC();
1224   }
1225
1226   // Add a DW_AT_calling_convention if this has an explicit convention.
1227   if (CC && CC != dwarf::DW_CC_normal)
1228     addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1229
1230   // Add a return type. If this is a type like a C/C++ void type we don't add a
1231   // return type.
1232   if (Args.size())
1233     if (auto Ty = resolve(Args[0]))
1234       addType(SPDie, Ty);
1235
1236   unsigned VK = SP->getVirtuality();
1237   if (VK) {
1238     addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1239     if (SP->getVirtualIndex() != -1u) {
1240       DIELoc *Block = getDIELoc();
1241       addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1242       addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1243       addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1244     }
1245     ContainingTypeMap.insert(
1246         std::make_pair(&SPDie, resolve(SP->getContainingType())));
1247   }
1248
1249   if (!SP->isDefinition()) {
1250     addFlag(SPDie, dwarf::DW_AT_declaration);
1251
1252     // Add arguments. Do not add arguments for subprogram definition. They will
1253     // be handled while processing variables.
1254     constructSubprogramArguments(SPDie, Args);
1255   }
1256
1257   addThrownTypes(SPDie, SP->getThrownTypes());
1258
1259   if (SP->isArtificial())
1260     addFlag(SPDie, dwarf::DW_AT_artificial);
1261
1262   if (!SP->isLocalToUnit())
1263     addFlag(SPDie, dwarf::DW_AT_external);
1264
1265   if (DD->useAppleExtensionAttributes()) {
1266     if (SP->isOptimized())
1267       addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1268
1269     if (unsigned isa = Asm->getISAEncoding())
1270       addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1271   }
1272
1273   if (SP->isLValueReference())
1274     addFlag(SPDie, dwarf::DW_AT_reference);
1275
1276   if (SP->isRValueReference())
1277     addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1278
1279   if (SP->isNoReturn())
1280     addFlag(SPDie, dwarf::DW_AT_noreturn);
1281
1282   if (SP->isProtected())
1283     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1284             dwarf::DW_ACCESS_protected);
1285   else if (SP->isPrivate())
1286     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1287             dwarf::DW_ACCESS_private);
1288   else if (SP->isPublic())
1289     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1290             dwarf::DW_ACCESS_public);
1291
1292   if (SP->isExplicit())
1293     addFlag(SPDie, dwarf::DW_AT_explicit);
1294
1295   if (SP->isMainSubprogram())
1296     addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1297 }
1298
1299 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR,
1300                                      DIE *IndexTy) {
1301   DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1302   addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
1303
1304   // The LowerBound value defines the lower bounds which is typically zero for
1305   // C/C++. The Count value is the number of elements.  Values are 64 bit. If
1306   // Count == -1 then the array is unbounded and we do not emit
1307   // DW_AT_lower_bound and DW_AT_count attributes.
1308   int64_t LowerBound = SR->getLowerBound();
1309   int64_t DefaultLowerBound = getDefaultLowerBound();
1310   int64_t Count = SR->getCount();
1311
1312   if (DefaultLowerBound == -1 || LowerBound != DefaultLowerBound)
1313     addUInt(DW_Subrange, dwarf::DW_AT_lower_bound, None, LowerBound);
1314
1315   if (Count != -1)
1316     // FIXME: An unbounded array should reference the expression that defines
1317     // the array.
1318     addUInt(DW_Subrange, dwarf::DW_AT_count, None, Count);
1319 }
1320
1321 DIE *DwarfUnit::getIndexTyDie() {
1322   if (IndexTyDie)
1323     return IndexTyDie;
1324   // Construct an integer type to use for indexes.
1325   IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1326   addString(*IndexTyDie, dwarf::DW_AT_name, "sizetype");
1327   addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1328   addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1329           dwarf::DW_ATE_unsigned);
1330   return IndexTyDie;
1331 }
1332
1333 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1334   if (CTy->isVector())
1335     addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1336
1337   // Emit the element type.
1338   addType(Buffer, resolve(CTy->getBaseType()));
1339
1340   // Get an anonymous type for index type.
1341   // FIXME: This type should be passed down from the front end
1342   // as different languages may have different sizes for indexes.
1343   DIE *IdxTy = getIndexTyDie();
1344
1345   // Add subranges to array type.
1346   DINodeArray Elements = CTy->getElements();
1347   for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
1348     // FIXME: Should this really be such a loose cast?
1349     if (auto *Element = dyn_cast_or_null<DINode>(Elements[i]))
1350       if (Element->getTag() == dwarf::DW_TAG_subrange_type)
1351         constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy);
1352   }
1353 }
1354
1355 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1356   DINodeArray Elements = CTy->getElements();
1357
1358   // Add enumerators to enumeration type.
1359   for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
1360     auto *Enum = dyn_cast_or_null<DIEnumerator>(Elements[i]);
1361     if (Enum) {
1362       DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1363       StringRef Name = Enum->getName();
1364       addString(Enumerator, dwarf::DW_AT_name, Name);
1365       int64_t Value = Enum->getValue();
1366       addSInt(Enumerator, dwarf::DW_AT_const_value, dwarf::DW_FORM_sdata,
1367               Value);
1368     }
1369   }
1370   const DIType *DTy = resolve(CTy->getBaseType());
1371   if (DTy) {
1372     addType(Buffer, DTy);
1373     addFlag(Buffer, dwarf::DW_AT_enum_class);
1374   }
1375 }
1376
1377 void DwarfUnit::constructContainingTypeDIEs() {
1378   for (auto CI = ContainingTypeMap.begin(), CE = ContainingTypeMap.end();
1379        CI != CE; ++CI) {
1380     DIE &SPDie = *CI->first;
1381     const DINode *D = CI->second;
1382     if (!D)
1383       continue;
1384     DIE *NDie = getDIE(D);
1385     if (!NDie)
1386       continue;
1387     addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1388   }
1389 }
1390
1391 void DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1392   DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer);
1393   StringRef Name = DT->getName();
1394   if (!Name.empty())
1395     addString(MemberDie, dwarf::DW_AT_name, Name);
1396
1397   addType(MemberDie, resolve(DT->getBaseType()));
1398
1399   addSourceLine(MemberDie, DT);
1400
1401   if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1402
1403     // For C++, virtual base classes are not at fixed offset. Use following
1404     // expression to extract appropriate offset from vtable.
1405     // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1406
1407     DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1408     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1409     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1410     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1411     addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1412     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1413     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1414     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1415
1416     addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1417   } else {
1418     uint64_t Size = DT->getSizeInBits();
1419     uint64_t FieldSize = DD->getBaseTypeSize(DT);
1420     uint32_t AlignInBytes = DT->getAlignInBytes();
1421     uint64_t OffsetInBytes;
1422
1423     bool IsBitfield = FieldSize && Size != FieldSize;
1424     if (IsBitfield) {
1425       // Handle bitfield, assume bytes are 8 bits.
1426       if (DD->useDWARF2Bitfields())
1427         addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1428       addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1429
1430       uint64_t Offset = DT->getOffsetInBits();
1431       // We can't use DT->getAlignInBits() here: AlignInBits for member type
1432       // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1433       // which can't be done with bitfields. Thus we use FieldSize here.
1434       uint32_t AlignInBits = FieldSize;
1435       uint32_t AlignMask = ~(AlignInBits - 1);
1436       // The bits from the start of the storage unit to the start of the field.
1437       uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1438       // The byte offset of the field's aligned storage unit inside the struct.
1439       OffsetInBytes = (Offset - StartBitOffset) / 8;
1440
1441       if (DD->useDWARF2Bitfields()) {
1442         uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1443         uint64_t FieldOffset = (HiMark - FieldSize);
1444         Offset -= FieldOffset;
1445
1446         // Maybe we need to work from the other end.
1447         if (Asm->getDataLayout().isLittleEndian())
1448           Offset = FieldSize - (Offset + Size);
1449
1450         addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1451         OffsetInBytes = FieldOffset >> 3;
1452       } else {
1453         addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset);
1454       }
1455     } else {
1456       // This is not a bitfield.
1457       OffsetInBytes = DT->getOffsetInBits() / 8;
1458       if (AlignInBytes)
1459         addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1460                 AlignInBytes);
1461     }
1462
1463     if (DD->getDwarfVersion() <= 2) {
1464       DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1465       addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1466       addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1467       addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1468     } else if (!IsBitfield || DD->useDWARF2Bitfields())
1469       addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1470               OffsetInBytes);
1471   }
1472
1473   if (DT->isProtected())
1474     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1475             dwarf::DW_ACCESS_protected);
1476   else if (DT->isPrivate())
1477     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1478             dwarf::DW_ACCESS_private);
1479   // Otherwise C++ member and base classes are considered public.
1480   else if (DT->isPublic())
1481     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1482             dwarf::DW_ACCESS_public);
1483   if (DT->isVirtual())
1484     addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1485             dwarf::DW_VIRTUALITY_virtual);
1486
1487   // Objective-C properties.
1488   if (DINode *PNode = DT->getObjCProperty())
1489     if (DIE *PDie = getDIE(PNode))
1490       MemberDie.addValue(DIEValueAllocator, dwarf::DW_AT_APPLE_property,
1491                          dwarf::DW_FORM_ref4, DIEEntry(*PDie));
1492
1493   if (DT->isArtificial())
1494     addFlag(MemberDie, dwarf::DW_AT_artificial);
1495 }
1496
1497 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
1498   if (!DT)
1499     return nullptr;
1500
1501   // Construct the context before querying for the existence of the DIE in case
1502   // such construction creates the DIE.
1503   DIE *ContextDIE = getOrCreateContextDIE(resolve(DT->getScope()));
1504   assert(dwarf::isType(ContextDIE->getTag()) &&
1505          "Static member should belong to a type.");
1506
1507   if (DIE *StaticMemberDIE = getDIE(DT))
1508     return StaticMemberDIE;
1509
1510   DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
1511
1512   const DIType *Ty = resolve(DT->getBaseType());
1513
1514   addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
1515   addType(StaticMemberDIE, Ty);
1516   addSourceLine(StaticMemberDIE, DT);
1517   addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1518   addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1519
1520   // FIXME: We could omit private if the parent is a class_type, and
1521   // public if the parent is something else.
1522   if (DT->isProtected())
1523     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1524             dwarf::DW_ACCESS_protected);
1525   else if (DT->isPrivate())
1526     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1527             dwarf::DW_ACCESS_private);
1528   else if (DT->isPublic())
1529     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1530             dwarf::DW_ACCESS_public);
1531
1532   if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant()))
1533     addConstantValue(StaticMemberDIE, CI, Ty);
1534   if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
1535     addConstantFPValue(StaticMemberDIE, CFP);
1536
1537   if (uint32_t AlignInBytes = DT->getAlignInBytes())
1538     addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1539             AlignInBytes);
1540
1541   return &StaticMemberDIE;
1542 }
1543
1544 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
1545   // Emit size of content not including length itself
1546   Asm->OutStreamer->AddComment("Length of Unit");
1547   Asm->EmitInt32(getHeaderSize() + getUnitDie().getSize());
1548
1549   Asm->OutStreamer->AddComment("DWARF version number");
1550   unsigned Version = DD->getDwarfVersion();
1551   Asm->EmitInt16(Version);
1552
1553   // DWARF v5 reorders the address size and adds a unit type.
1554   if (Version >= 5) {
1555     Asm->OutStreamer->AddComment("DWARF Unit Type");
1556     Asm->EmitInt8(UT);
1557     Asm->OutStreamer->AddComment("Address Size (in bytes)");
1558     Asm->EmitInt8(Asm->MAI->getCodePointerSize());
1559   }
1560
1561   // We share one abbreviations table across all units so it's always at the
1562   // start of the section. Use a relocatable offset where needed to ensure
1563   // linking doesn't invalidate that offset.
1564   Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
1565   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1566   if (UseOffsets)
1567     Asm->EmitInt32(0);
1568   else
1569     Asm->emitDwarfSymbolReference(
1570         TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
1571
1572   if (Version <= 4) {
1573     Asm->OutStreamer->AddComment("Address Size (in bytes)");
1574     Asm->EmitInt8(Asm->MAI->getCodePointerSize());
1575   }
1576 }
1577
1578 void DwarfTypeUnit::emitHeader(bool UseOffsets) {
1579   DwarfUnit::emitCommonHeader(UseOffsets, 
1580                               DD->useSplitDwarf() ? dwarf::DW_UT_split_type
1581                                                   : dwarf::DW_UT_type);
1582   Asm->OutStreamer->AddComment("Type Signature");
1583   Asm->OutStreamer->EmitIntValue(TypeSignature, sizeof(TypeSignature));
1584   Asm->OutStreamer->AddComment("Type DIE Offset");
1585   // In a skeleton type unit there is no type DIE so emit a zero offset.
1586   Asm->OutStreamer->EmitIntValue(Ty ? Ty->getOffset() : 0,
1587                                  sizeof(Ty->getOffset()));
1588 }
1589
1590 bool DwarfTypeUnit::isDwoUnit() const {
1591   // Since there are no skeleton type units, all type units are dwo type units
1592   // when split DWARF is being used.
1593   return DD->useSplitDwarf();
1594 }
1595
1596 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die,
1597                                   const DIScope *Context) {
1598   getCU().addGlobalNameForTypeUnit(Name, Context);
1599 }
1600
1601 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1602                                   const DIScope *Context) {
1603   getCU().addGlobalTypeUnitType(Ty, Context);
1604 }
1605
1606 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1607   if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1608     return nullptr;
1609   if (isDwoUnit())
1610     return nullptr;
1611   return getSection()->getBeginSymbol();
1612 }