1 //===-- llvm/CodeGen/DwarfUnit.cpp - Dwarf Type and Compile Units ---------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for constructing a dwarf compile unit.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DwarfUnit.h"
14 #include "AddressPool.h"
15 #include "DwarfCompileUnit.h"
16 #include "DwarfExpression.h"
17 #include "llvm/ADT/APFloat.h"
18 #include "llvm/ADT/APInt.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineOperand.h"
24 #include "llvm/CodeGen/TargetRegisterInfo.h"
25 #include "llvm/CodeGen/TargetSubtargetInfo.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/GlobalValue.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/MC/MCContext.h"
32 #include "llvm/MC/MCDwarf.h"
33 #include "llvm/MC/MCSection.h"
34 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MachineLocation.h"
36 #include "llvm/Support/Casting.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Target/TargetLoweringObjectFile.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 DIEDwarfExpression::DIEDwarfExpression(const AsmPrinter &AP,
49                                        DwarfCompileUnit &CU, DIELoc &DIE)
50     : DwarfExpression(AP.getDwarfVersion(), CU), AP(AP), OutDIE(DIE) {}
51 
52 void DIEDwarfExpression::emitOp(uint8_t Op, const char* Comment) {
53   CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Op);
54 }
55 
56 void DIEDwarfExpression::emitSigned(int64_t Value) {
57   CU.addSInt(getActiveDIE(), dwarf::DW_FORM_sdata, Value);
58 }
59 
60 void DIEDwarfExpression::emitUnsigned(uint64_t Value) {
61   CU.addUInt(getActiveDIE(), dwarf::DW_FORM_udata, Value);
62 }
63 
64 void DIEDwarfExpression::emitData1(uint8_t Value) {
65   CU.addUInt(getActiveDIE(), dwarf::DW_FORM_data1, Value);
66 }
67 
68 void DIEDwarfExpression::emitBaseTypeRef(uint64_t Idx) {
69   CU.addBaseTypeRef(getActiveDIE(), Idx);
70 }
71 
72 void DIEDwarfExpression::enableTemporaryBuffer() {
73   assert(!IsBuffering && "Already buffering?");
74   IsBuffering = true;
75 }
76 
77 void DIEDwarfExpression::disableTemporaryBuffer() { IsBuffering = false; }
78 
79 unsigned DIEDwarfExpression::getTemporaryBufferSize() {
80   return TmpDIE.ComputeSize(&AP);
81 }
82 
83 void DIEDwarfExpression::commitTemporaryBuffer() { OutDIE.takeValues(TmpDIE); }
84 
85 bool DIEDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
86                                          llvm::Register MachineReg) {
87   return MachineReg == TRI.getFrameRegister(*AP.MF);
88 }
89 
90 DwarfUnit::DwarfUnit(dwarf::Tag UnitTag, const DICompileUnit *Node,
91                      AsmPrinter *A, DwarfDebug *DW, DwarfFile *DWU)
92     : DIEUnit(UnitTag), CUNode(Node), Asm(A), DD(DW), DU(DWU),
93       IndexTyDie(nullptr) {}
94 
95 DwarfTypeUnit::DwarfTypeUnit(DwarfCompileUnit &CU, AsmPrinter *A,
96                              DwarfDebug *DW, DwarfFile *DWU,
97                              MCDwarfDwoLineTable *SplitLineTable)
98     : DwarfUnit(dwarf::DW_TAG_type_unit, CU.getCUNode(), A, DW, DWU), CU(CU),
99       SplitLineTable(SplitLineTable) {
100 }
101 
102 DwarfUnit::~DwarfUnit() {
103   for (DIEBlock *B : DIEBlocks)
104     B->~DIEBlock();
105   for (DIELoc *L : DIELocs)
106     L->~DIELoc();
107 }
108 
109 int64_t DwarfUnit::getDefaultLowerBound() const {
110   switch (getLanguage()) {
111   default:
112     break;
113 
114   // The languages below have valid values in all DWARF versions.
115   case dwarf::DW_LANG_C:
116   case dwarf::DW_LANG_C89:
117   case dwarf::DW_LANG_C_plus_plus:
118     return 0;
119 
120   case dwarf::DW_LANG_Fortran77:
121   case dwarf::DW_LANG_Fortran90:
122     return 1;
123 
124   // The languages below have valid values only if the DWARF version >= 3.
125   case dwarf::DW_LANG_C99:
126   case dwarf::DW_LANG_ObjC:
127   case dwarf::DW_LANG_ObjC_plus_plus:
128     if (DD->getDwarfVersion() >= 3)
129       return 0;
130     break;
131 
132   case dwarf::DW_LANG_Fortran95:
133     if (DD->getDwarfVersion() >= 3)
134       return 1;
135     break;
136 
137   // Starting with DWARF v4, all defined languages have valid values.
138   case dwarf::DW_LANG_D:
139   case dwarf::DW_LANG_Java:
140   case dwarf::DW_LANG_Python:
141   case dwarf::DW_LANG_UPC:
142     if (DD->getDwarfVersion() >= 4)
143       return 0;
144     break;
145 
146   case dwarf::DW_LANG_Ada83:
147   case dwarf::DW_LANG_Ada95:
148   case dwarf::DW_LANG_Cobol74:
149   case dwarf::DW_LANG_Cobol85:
150   case dwarf::DW_LANG_Modula2:
151   case dwarf::DW_LANG_Pascal83:
152   case dwarf::DW_LANG_PLI:
153     if (DD->getDwarfVersion() >= 4)
154       return 1;
155     break;
156 
157   // The languages below are new in DWARF v5.
158   case dwarf::DW_LANG_BLISS:
159   case dwarf::DW_LANG_C11:
160   case dwarf::DW_LANG_C_plus_plus_03:
161   case dwarf::DW_LANG_C_plus_plus_11:
162   case dwarf::DW_LANG_C_plus_plus_14:
163   case dwarf::DW_LANG_Dylan:
164   case dwarf::DW_LANG_Go:
165   case dwarf::DW_LANG_Haskell:
166   case dwarf::DW_LANG_OCaml:
167   case dwarf::DW_LANG_OpenCL:
168   case dwarf::DW_LANG_RenderScript:
169   case dwarf::DW_LANG_Rust:
170   case dwarf::DW_LANG_Swift:
171     if (DD->getDwarfVersion() >= 5)
172       return 0;
173     break;
174 
175   case dwarf::DW_LANG_Fortran03:
176   case dwarf::DW_LANG_Fortran08:
177   case dwarf::DW_LANG_Julia:
178   case dwarf::DW_LANG_Modula3:
179     if (DD->getDwarfVersion() >= 5)
180       return 1;
181     break;
182   }
183 
184   return -1;
185 }
186 
187 /// Check whether the DIE for this MDNode can be shared across CUs.
188 bool DwarfUnit::isShareableAcrossCUs(const DINode *D) const {
189   // When the MDNode can be part of the type system, the DIE can be shared
190   // across CUs.
191   // Combining type units and cross-CU DIE sharing is lower value (since
192   // cross-CU DIE sharing is used in LTO and removes type redundancy at that
193   // level already) but may be implementable for some value in projects
194   // building multiple independent libraries with LTO and then linking those
195   // together.
196   if (isDwoUnit() && !DD->shareAcrossDWOCUs())
197     return false;
198   return (isa<DIType>(D) ||
199           (isa<DISubprogram>(D) && !cast<DISubprogram>(D)->isDefinition())) &&
200          !DD->generateTypeUnits();
201 }
202 
203 DIE *DwarfUnit::getDIE(const DINode *D) const {
204   if (isShareableAcrossCUs(D))
205     return DU->getDIE(D);
206   return MDNodeToDieMap.lookup(D);
207 }
208 
209 void DwarfUnit::insertDIE(const DINode *Desc, DIE *D) {
210   if (isShareableAcrossCUs(Desc)) {
211     DU->insertDIE(Desc, D);
212     return;
213   }
214   MDNodeToDieMap.insert(std::make_pair(Desc, D));
215 }
216 
217 void DwarfUnit::insertDIE(DIE *D) {
218   MDNodeToDieMap.insert(std::make_pair(nullptr, D));
219 }
220 
221 void DwarfUnit::addFlag(DIE &Die, dwarf::Attribute Attribute) {
222   if (DD->getDwarfVersion() >= 4)
223     addAttribute(Die, Attribute, dwarf::DW_FORM_flag_present, DIEInteger(1));
224   else
225     addAttribute(Die, Attribute, dwarf::DW_FORM_flag, DIEInteger(1));
226 }
227 
228 void DwarfUnit::addUInt(DIEValueList &Die, dwarf::Attribute Attribute,
229                         Optional<dwarf::Form> Form, uint64_t Integer) {
230   if (!Form)
231     Form = DIEInteger::BestForm(false, Integer);
232   assert(Form != dwarf::DW_FORM_implicit_const &&
233          "DW_FORM_implicit_const is used only for signed integers");
234   addAttribute(Die, Attribute, *Form, DIEInteger(Integer));
235 }
236 
237 void DwarfUnit::addUInt(DIEValueList &Block, dwarf::Form Form,
238                         uint64_t Integer) {
239   addUInt(Block, (dwarf::Attribute)0, Form, Integer);
240 }
241 
242 void DwarfUnit::addSInt(DIEValueList &Die, dwarf::Attribute Attribute,
243                         Optional<dwarf::Form> Form, int64_t Integer) {
244   if (!Form)
245     Form = DIEInteger::BestForm(true, Integer);
246   addAttribute(Die, Attribute, *Form, DIEInteger(Integer));
247 }
248 
249 void DwarfUnit::addSInt(DIELoc &Die, Optional<dwarf::Form> Form,
250                         int64_t Integer) {
251   addSInt(Die, (dwarf::Attribute)0, Form, Integer);
252 }
253 
254 void DwarfUnit::addString(DIE &Die, dwarf::Attribute Attribute,
255                           StringRef String) {
256   if (CUNode->isDebugDirectivesOnly())
257     return;
258 
259   if (DD->useInlineStrings()) {
260     addAttribute(Die, Attribute, dwarf::DW_FORM_string,
261                  new (DIEValueAllocator)
262                      DIEInlineString(String, DIEValueAllocator));
263     return;
264   }
265   dwarf::Form IxForm =
266       isDwoUnit() ? dwarf::DW_FORM_GNU_str_index : dwarf::DW_FORM_strp;
267 
268   auto StringPoolEntry =
269       useSegmentedStringOffsetsTable() || IxForm == dwarf::DW_FORM_GNU_str_index
270           ? DU->getStringPool().getIndexedEntry(*Asm, String)
271           : DU->getStringPool().getEntry(*Asm, String);
272 
273   // For DWARF v5 and beyond, use the smallest strx? form possible.
274   if (useSegmentedStringOffsetsTable()) {
275     IxForm = dwarf::DW_FORM_strx1;
276     unsigned Index = StringPoolEntry.getIndex();
277     if (Index > 0xffffff)
278       IxForm = dwarf::DW_FORM_strx4;
279     else if (Index > 0xffff)
280       IxForm = dwarf::DW_FORM_strx3;
281     else if (Index > 0xff)
282       IxForm = dwarf::DW_FORM_strx2;
283   }
284   addAttribute(Die, Attribute, IxForm, DIEString(StringPoolEntry));
285 }
286 
287 void DwarfUnit::addLabel(DIEValueList &Die, dwarf::Attribute Attribute,
288                          dwarf::Form Form, const MCSymbol *Label) {
289   addAttribute(Die, Attribute, Form, DIELabel(Label));
290 }
291 
292 void DwarfUnit::addLabel(DIELoc &Die, dwarf::Form Form, const MCSymbol *Label) {
293   addLabel(Die, (dwarf::Attribute)0, Form, Label);
294 }
295 
296 void DwarfUnit::addSectionOffset(DIE &Die, dwarf::Attribute Attribute,
297                                  uint64_t Integer) {
298   addUInt(Die, Attribute, DD->getDwarfSectionOffsetForm(), Integer);
299 }
300 
301 unsigned DwarfTypeUnit::getOrCreateSourceID(const DIFile *File) {
302   if (!SplitLineTable)
303     return getCU().getOrCreateSourceID(File);
304   if (!UsedLineTable) {
305     UsedLineTable = true;
306     // This is a split type unit that needs a line table.
307     addSectionOffset(getUnitDie(), dwarf::DW_AT_stmt_list, 0);
308   }
309   return SplitLineTable->getFile(
310       File->getDirectory(), File->getFilename(), DD->getMD5AsBytes(File),
311       Asm->OutContext.getDwarfVersion(), File->getSource());
312 }
313 
314 void DwarfUnit::addPoolOpAddress(DIEValueList &Die, const MCSymbol *Label) {
315   bool UseAddrOffsetFormOrExpressions =
316       DD->useAddrOffsetForm() || DD->useAddrOffsetExpressions();
317 
318   const MCSymbol *Base = nullptr;
319   if (Label->isInSection() && UseAddrOffsetFormOrExpressions)
320     Base = DD->getSectionLabel(&Label->getSection());
321 
322   uint32_t Index = DD->getAddressPool().getIndex(Base ? Base : Label);
323 
324   if (DD->getDwarfVersion() >= 5) {
325     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addrx);
326     addUInt(Die, dwarf::DW_FORM_addrx, Index);
327   } else {
328     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_addr_index);
329     addUInt(Die, dwarf::DW_FORM_GNU_addr_index, Index);
330   }
331 
332   if (Base && Base != Label) {
333     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_const4u);
334     addLabelDelta(Die, (dwarf::Attribute)0, Label, Base);
335     addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
336   }
337 }
338 
339 void DwarfUnit::addOpAddress(DIELoc &Die, const MCSymbol *Sym) {
340   if (DD->getDwarfVersion() >= 5) {
341     addPoolOpAddress(Die, Sym);
342     return;
343   }
344 
345   if (DD->useSplitDwarf()) {
346     addPoolOpAddress(Die, Sym);
347     return;
348   }
349 
350   addUInt(Die, dwarf::DW_FORM_data1, dwarf::DW_OP_addr);
351   addLabel(Die, dwarf::DW_FORM_addr, Sym);
352 }
353 
354 void DwarfUnit::addLabelDelta(DIEValueList &Die, dwarf::Attribute Attribute,
355                               const MCSymbol *Hi, const MCSymbol *Lo) {
356   addAttribute(Die, Attribute, dwarf::DW_FORM_data4,
357                new (DIEValueAllocator) DIEDelta(Hi, Lo));
358 }
359 
360 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute, DIE &Entry) {
361   addDIEEntry(Die, Attribute, DIEEntry(Entry));
362 }
363 
364 void DwarfUnit::addDIETypeSignature(DIE &Die, uint64_t Signature) {
365   // Flag the type unit reference as a declaration so that if it contains
366   // members (implicit special members, static data member definitions, member
367   // declarations for definitions in this CU, etc) consumers don't get confused
368   // and think this is a full definition.
369   addFlag(Die, dwarf::DW_AT_declaration);
370 
371   addAttribute(Die, dwarf::DW_AT_signature, dwarf::DW_FORM_ref_sig8,
372                DIEInteger(Signature));
373 }
374 
375 void DwarfUnit::addDIEEntry(DIE &Die, dwarf::Attribute Attribute,
376                             DIEEntry Entry) {
377   const DIEUnit *CU = Die.getUnit();
378   const DIEUnit *EntryCU = Entry.getEntry().getUnit();
379   if (!CU)
380     // We assume that Die belongs to this CU, if it is not linked to any CU yet.
381     CU = getUnitDie().getUnit();
382   if (!EntryCU)
383     EntryCU = getUnitDie().getUnit();
384   addAttribute(Die, Attribute,
385                EntryCU == CU ? dwarf::DW_FORM_ref4 : dwarf::DW_FORM_ref_addr,
386                Entry);
387 }
388 
389 DIE &DwarfUnit::createAndAddDIE(dwarf::Tag Tag, DIE &Parent, const DINode *N) {
390   DIE &Die = Parent.addChild(DIE::get(DIEValueAllocator, Tag));
391   if (N)
392     insertDIE(N, &Die);
393   return Die;
394 }
395 
396 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, DIELoc *Loc) {
397   Loc->ComputeSize(Asm);
398   DIELocs.push_back(Loc); // Memoize so we can call the destructor later on.
399   addAttribute(Die, Attribute, Loc->BestForm(DD->getDwarfVersion()), Loc);
400 }
401 
402 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute, dwarf::Form Form,
403                          DIEBlock *Block) {
404   Block->ComputeSize(Asm);
405   DIEBlocks.push_back(Block); // Memoize so we can call the destructor later on.
406   addAttribute(Die, Attribute, Form, Block);
407 }
408 
409 void DwarfUnit::addBlock(DIE &Die, dwarf::Attribute Attribute,
410                          DIEBlock *Block) {
411   addBlock(Die, Attribute, Block->BestForm(), Block);
412 }
413 
414 void DwarfUnit::addSourceLine(DIE &Die, unsigned Line, const DIFile *File) {
415   if (Line == 0)
416     return;
417 
418   unsigned FileID = getOrCreateSourceID(File);
419   addUInt(Die, dwarf::DW_AT_decl_file, None, FileID);
420   addUInt(Die, dwarf::DW_AT_decl_line, None, Line);
421 }
422 
423 void DwarfUnit::addSourceLine(DIE &Die, const DILocalVariable *V) {
424   assert(V);
425 
426   addSourceLine(Die, V->getLine(), V->getFile());
427 }
428 
429 void DwarfUnit::addSourceLine(DIE &Die, const DIGlobalVariable *G) {
430   assert(G);
431 
432   addSourceLine(Die, G->getLine(), G->getFile());
433 }
434 
435 void DwarfUnit::addSourceLine(DIE &Die, const DISubprogram *SP) {
436   assert(SP);
437 
438   addSourceLine(Die, SP->getLine(), SP->getFile());
439 }
440 
441 void DwarfUnit::addSourceLine(DIE &Die, const DILabel *L) {
442   assert(L);
443 
444   addSourceLine(Die, L->getLine(), L->getFile());
445 }
446 
447 void DwarfUnit::addSourceLine(DIE &Die, const DIType *Ty) {
448   assert(Ty);
449 
450   addSourceLine(Die, Ty->getLine(), Ty->getFile());
451 }
452 
453 void DwarfUnit::addSourceLine(DIE &Die, const DIObjCProperty *Ty) {
454   assert(Ty);
455 
456   addSourceLine(Die, Ty->getLine(), Ty->getFile());
457 }
458 
459 void DwarfUnit::addConstantFPValue(DIE &Die, const ConstantFP *CFP) {
460   // Pass this down to addConstantValue as an unsigned bag of bits.
461   addConstantValue(Die, CFP->getValueAPF().bitcastToAPInt(), true);
462 }
463 
464 void DwarfUnit::addConstantValue(DIE &Die, const ConstantInt *CI,
465                                  const DIType *Ty) {
466   addConstantValue(Die, CI->getValue(), Ty);
467 }
468 
469 void DwarfUnit::addConstantValue(DIE &Die, uint64_t Val, const DIType *Ty) {
470   addConstantValue(Die, DD->isUnsignedDIType(Ty), Val);
471 }
472 
473 void DwarfUnit::addConstantValue(DIE &Die, bool Unsigned, uint64_t Val) {
474   // FIXME: This is a bit conservative/simple - it emits negative values always
475   // sign extended to 64 bits rather than minimizing the number of bytes.
476   addUInt(Die, dwarf::DW_AT_const_value,
477           Unsigned ? dwarf::DW_FORM_udata : dwarf::DW_FORM_sdata, Val);
478 }
479 
480 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, const DIType *Ty) {
481   addConstantValue(Die, Val, DD->isUnsignedDIType(Ty));
482 }
483 
484 void DwarfUnit::addConstantValue(DIE &Die, const APInt &Val, bool Unsigned) {
485   unsigned CIBitWidth = Val.getBitWidth();
486   if (CIBitWidth <= 64) {
487     addConstantValue(Die, Unsigned,
488                      Unsigned ? Val.getZExtValue() : Val.getSExtValue());
489     return;
490   }
491 
492   DIEBlock *Block = new (DIEValueAllocator) DIEBlock;
493 
494   // Get the raw data form of the large APInt.
495   const uint64_t *Ptr64 = Val.getRawData();
496 
497   int NumBytes = Val.getBitWidth() / 8; // 8 bits per byte.
498   bool LittleEndian = Asm->getDataLayout().isLittleEndian();
499 
500   // Output the constant to DWARF one byte at a time.
501   for (int i = 0; i < NumBytes; i++) {
502     uint8_t c;
503     if (LittleEndian)
504       c = Ptr64[i / 8] >> (8 * (i & 7));
505     else
506       c = Ptr64[(NumBytes - 1 - i) / 8] >> (8 * ((NumBytes - 1 - i) & 7));
507     addUInt(*Block, dwarf::DW_FORM_data1, c);
508   }
509 
510   addBlock(Die, dwarf::DW_AT_const_value, Block);
511 }
512 
513 void DwarfUnit::addLinkageName(DIE &Die, StringRef LinkageName) {
514   if (!LinkageName.empty())
515     addString(Die,
516               DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
517                                          : dwarf::DW_AT_MIPS_linkage_name,
518               GlobalValue::dropLLVMManglingEscape(LinkageName));
519 }
520 
521 void DwarfUnit::addTemplateParams(DIE &Buffer, DINodeArray TParams) {
522   // Add template parameters.
523   for (const auto *Element : TParams) {
524     if (auto *TTP = dyn_cast<DITemplateTypeParameter>(Element))
525       constructTemplateTypeParameterDIE(Buffer, TTP);
526     else if (auto *TVP = dyn_cast<DITemplateValueParameter>(Element))
527       constructTemplateValueParameterDIE(Buffer, TVP);
528   }
529 }
530 
531 /// Add thrown types.
532 void DwarfUnit::addThrownTypes(DIE &Die, DINodeArray ThrownTypes) {
533   for (const auto *Ty : ThrownTypes) {
534     DIE &TT = createAndAddDIE(dwarf::DW_TAG_thrown_type, Die);
535     addType(TT, cast<DIType>(Ty));
536   }
537 }
538 
539 DIE *DwarfUnit::getOrCreateContextDIE(const DIScope *Context) {
540   if (!Context || isa<DIFile>(Context))
541     return &getUnitDie();
542   if (auto *T = dyn_cast<DIType>(Context))
543     return getOrCreateTypeDIE(T);
544   if (auto *NS = dyn_cast<DINamespace>(Context))
545     return getOrCreateNameSpace(NS);
546   if (auto *SP = dyn_cast<DISubprogram>(Context))
547     return getOrCreateSubprogramDIE(SP);
548   if (auto *M = dyn_cast<DIModule>(Context))
549     return getOrCreateModule(M);
550   return getDIE(Context);
551 }
552 
553 DIE *DwarfUnit::createTypeDIE(const DICompositeType *Ty) {
554   auto *Context = Ty->getScope();
555   DIE *ContextDIE = getOrCreateContextDIE(Context);
556 
557   if (DIE *TyDIE = getDIE(Ty))
558     return TyDIE;
559 
560   // Create new type.
561   DIE &TyDIE = createAndAddDIE(Ty->getTag(), *ContextDIE, Ty);
562 
563   constructTypeDIE(TyDIE, cast<DICompositeType>(Ty));
564 
565   updateAcceleratorTables(Context, Ty, TyDIE);
566   return &TyDIE;
567 }
568 
569 DIE *DwarfUnit::createTypeDIE(const DIScope *Context, DIE &ContextDIE,
570                               const DIType *Ty) {
571   // Create new type.
572   DIE &TyDIE = createAndAddDIE(Ty->getTag(), ContextDIE, Ty);
573 
574   updateAcceleratorTables(Context, Ty, TyDIE);
575 
576   if (auto *BT = dyn_cast<DIBasicType>(Ty))
577     constructTypeDIE(TyDIE, BT);
578   else if (auto *ST = dyn_cast<DIStringType>(Ty))
579     constructTypeDIE(TyDIE, ST);
580   else if (auto *STy = dyn_cast<DISubroutineType>(Ty))
581     constructTypeDIE(TyDIE, STy);
582   else if (auto *CTy = dyn_cast<DICompositeType>(Ty)) {
583     if (DD->generateTypeUnits() && !Ty->isForwardDecl() &&
584         (Ty->getRawName() || CTy->getRawIdentifier())) {
585       // Skip updating the accelerator tables since this is not the full type.
586       if (MDString *TypeId = CTy->getRawIdentifier())
587         DD->addDwarfTypeUnitType(getCU(), TypeId->getString(), TyDIE, CTy);
588       else {
589         auto X = DD->enterNonTypeUnitContext();
590         finishNonUnitTypeDIE(TyDIE, CTy);
591       }
592       return &TyDIE;
593     }
594     constructTypeDIE(TyDIE, CTy);
595   } else {
596     constructTypeDIE(TyDIE, cast<DIDerivedType>(Ty));
597   }
598 
599   return &TyDIE;
600 }
601 
602 DIE *DwarfUnit::getOrCreateTypeDIE(const MDNode *TyNode) {
603   if (!TyNode)
604     return nullptr;
605 
606   auto *Ty = cast<DIType>(TyNode);
607 
608   // DW_TAG_restrict_type is not supported in DWARF2
609   if (Ty->getTag() == dwarf::DW_TAG_restrict_type && DD->getDwarfVersion() <= 2)
610     return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType());
611 
612   // DW_TAG_atomic_type is not supported in DWARF < 5
613   if (Ty->getTag() == dwarf::DW_TAG_atomic_type && DD->getDwarfVersion() < 5)
614     return getOrCreateTypeDIE(cast<DIDerivedType>(Ty)->getBaseType());
615 
616   // Construct the context before querying for the existence of the DIE in case
617   // such construction creates the DIE.
618   auto *Context = Ty->getScope();
619   DIE *ContextDIE = getOrCreateContextDIE(Context);
620   assert(ContextDIE);
621 
622   if (DIE *TyDIE = getDIE(Ty))
623     return TyDIE;
624 
625   return static_cast<DwarfUnit *>(ContextDIE->getUnit())
626       ->createTypeDIE(Context, *ContextDIE, Ty);
627 }
628 
629 void DwarfUnit::updateAcceleratorTables(const DIScope *Context,
630                                         const DIType *Ty, const DIE &TyDIE) {
631   if (!Ty->getName().empty() && !Ty->isForwardDecl()) {
632     bool IsImplementation = false;
633     if (auto *CT = dyn_cast<DICompositeType>(Ty)) {
634       // A runtime language of 0 actually means C/C++ and that any
635       // non-negative value is some version of Objective-C/C++.
636       IsImplementation = CT->getRuntimeLang() == 0 || CT->isObjcClassComplete();
637     }
638     unsigned Flags = IsImplementation ? dwarf::DW_FLAG_type_implementation : 0;
639     DD->addAccelType(*CUNode, Ty->getName(), TyDIE, Flags);
640 
641     if (!Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
642         isa<DINamespace>(Context) || isa<DICommonBlock>(Context))
643       addGlobalType(Ty, TyDIE, Context);
644   }
645 }
646 
647 void DwarfUnit::addType(DIE &Entity, const DIType *Ty,
648                         dwarf::Attribute Attribute) {
649   assert(Ty && "Trying to add a type that doesn't exist?");
650   addDIEEntry(Entity, Attribute, DIEEntry(*getOrCreateTypeDIE(Ty)));
651 }
652 
653 std::string DwarfUnit::getParentContextString(const DIScope *Context) const {
654   if (!Context)
655     return "";
656 
657   // FIXME: Decide whether to implement this for non-C++ languages.
658   if (!dwarf::isCPlusPlus((dwarf::SourceLanguage)getLanguage()))
659     return "";
660 
661   std::string CS;
662   SmallVector<const DIScope *, 1> Parents;
663   while (!isa<DICompileUnit>(Context)) {
664     Parents.push_back(Context);
665     if (const DIScope *S = Context->getScope())
666       Context = S;
667     else
668       // Structure, etc types will have a NULL context if they're at the top
669       // level.
670       break;
671   }
672 
673   // Reverse iterate over our list to go from the outermost construct to the
674   // innermost.
675   for (const DIScope *Ctx : make_range(Parents.rbegin(), Parents.rend())) {
676     StringRef Name = Ctx->getName();
677     if (Name.empty() && isa<DINamespace>(Ctx))
678       Name = "(anonymous namespace)";
679     if (!Name.empty()) {
680       CS += Name;
681       CS += "::";
682     }
683   }
684   return CS;
685 }
686 
687 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIBasicType *BTy) {
688   // Get core information.
689   StringRef Name = BTy->getName();
690   // Add name if not anonymous or intermediate type.
691   if (!Name.empty())
692     addString(Buffer, dwarf::DW_AT_name, Name);
693 
694   // An unspecified type only has a name attribute.
695   if (BTy->getTag() == dwarf::DW_TAG_unspecified_type)
696     return;
697 
698   if (BTy->getTag() != dwarf::DW_TAG_string_type)
699     addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
700             BTy->getEncoding());
701 
702   uint64_t Size = BTy->getSizeInBits() >> 3;
703   addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
704 
705   if (BTy->isBigEndian())
706     addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_big);
707   else if (BTy->isLittleEndian())
708     addUInt(Buffer, dwarf::DW_AT_endianity, None, dwarf::DW_END_little);
709 }
710 
711 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIStringType *STy) {
712   // Get core information.
713   StringRef Name = STy->getName();
714   // Add name if not anonymous or intermediate type.
715   if (!Name.empty())
716     addString(Buffer, dwarf::DW_AT_name, Name);
717 
718   if (DIVariable *Var = STy->getStringLength()) {
719     if (auto *VarDIE = getDIE(Var))
720       addDIEEntry(Buffer, dwarf::DW_AT_string_length, *VarDIE);
721   } else if (DIExpression *Expr = STy->getStringLengthExp()) {
722     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
723     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
724     // This is to describe the memory location of the
725     // length of a Fortran deferred length string, so
726     // lock it down as such.
727     DwarfExpr.setMemoryLocationKind();
728     DwarfExpr.addExpression(Expr);
729     addBlock(Buffer, dwarf::DW_AT_string_length, DwarfExpr.finalize());
730   } else {
731     uint64_t Size = STy->getSizeInBits() >> 3;
732     addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
733   }
734 
735   if (STy->getEncoding()) {
736     // For eventual Unicode support.
737     addUInt(Buffer, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
738             STy->getEncoding());
739   }
740 }
741 
742 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DIDerivedType *DTy) {
743   // Get core information.
744   StringRef Name = DTy->getName();
745   uint64_t Size = DTy->getSizeInBits() >> 3;
746   uint16_t Tag = Buffer.getTag();
747 
748   // Map to main type, void will not have a type.
749   const DIType *FromTy = DTy->getBaseType();
750   if (FromTy)
751     addType(Buffer, FromTy);
752 
753   // Add name if not anonymous or intermediate type.
754   if (!Name.empty())
755     addString(Buffer, dwarf::DW_AT_name, Name);
756 
757   // If alignment is specified for a typedef , create and insert DW_AT_alignment
758   // attribute in DW_TAG_typedef DIE.
759   if (Tag == dwarf::DW_TAG_typedef && DD->getDwarfVersion() >= 5) {
760     uint32_t AlignInBytes = DTy->getAlignInBytes();
761     if (AlignInBytes > 0)
762       addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
763               AlignInBytes);
764   }
765 
766   // Add size if non-zero (derived types might be zero-sized.)
767   if (Size && Tag != dwarf::DW_TAG_pointer_type
768            && Tag != dwarf::DW_TAG_ptr_to_member_type
769            && Tag != dwarf::DW_TAG_reference_type
770            && Tag != dwarf::DW_TAG_rvalue_reference_type)
771     addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
772 
773   if (Tag == dwarf::DW_TAG_ptr_to_member_type)
774     addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
775                 *getOrCreateTypeDIE(cast<DIDerivedType>(DTy)->getClassType()));
776   // Add source line info if available and TyDesc is not a forward declaration.
777   if (!DTy->isForwardDecl())
778     addSourceLine(Buffer, DTy);
779 
780   // If DWARF address space value is other than None, add it.  The IR
781   // verifier checks that DWARF address space only exists for pointer
782   // or reference types.
783   if (DTy->getDWARFAddressSpace())
784     addUInt(Buffer, dwarf::DW_AT_address_class, dwarf::DW_FORM_data4,
785             DTy->getDWARFAddressSpace().getValue());
786 }
787 
788 void DwarfUnit::constructSubprogramArguments(DIE &Buffer, DITypeRefArray Args) {
789   for (unsigned i = 1, N = Args.size(); i < N; ++i) {
790     const DIType *Ty = Args[i];
791     if (!Ty) {
792       assert(i == N-1 && "Unspecified parameter must be the last argument");
793       createAndAddDIE(dwarf::DW_TAG_unspecified_parameters, Buffer);
794     } else {
795       DIE &Arg = createAndAddDIE(dwarf::DW_TAG_formal_parameter, Buffer);
796       addType(Arg, Ty);
797       if (Ty->isArtificial())
798         addFlag(Arg, dwarf::DW_AT_artificial);
799     }
800   }
801 }
802 
803 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DISubroutineType *CTy) {
804   // Add return type.  A void return won't have a type.
805   auto Elements = cast<DISubroutineType>(CTy)->getTypeArray();
806   if (Elements.size())
807     if (auto RTy = Elements[0])
808       addType(Buffer, RTy);
809 
810   bool isPrototyped = true;
811   if (Elements.size() == 2 && !Elements[1])
812     isPrototyped = false;
813 
814   constructSubprogramArguments(Buffer, Elements);
815 
816   // Add prototype flag if we're dealing with a C language and the function has
817   // been prototyped.
818   uint16_t Language = getLanguage();
819   if (isPrototyped &&
820       (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
821        Language == dwarf::DW_LANG_ObjC))
822     addFlag(Buffer, dwarf::DW_AT_prototyped);
823 
824   // Add a DW_AT_calling_convention if this has an explicit convention.
825   if (CTy->getCC() && CTy->getCC() != dwarf::DW_CC_normal)
826     addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
827             CTy->getCC());
828 
829   if (CTy->isLValueReference())
830     addFlag(Buffer, dwarf::DW_AT_reference);
831 
832   if (CTy->isRValueReference())
833     addFlag(Buffer, dwarf::DW_AT_rvalue_reference);
834 }
835 
836 void DwarfUnit::addAnnotation(DIE &Buffer, DINodeArray Annotations) {
837   if (!Annotations)
838     return;
839 
840   for (const Metadata *Annotation : Annotations->operands()) {
841     const MDNode *MD = cast<MDNode>(Annotation);
842     const MDString *Name = cast<MDString>(MD->getOperand(0));
843 
844     // Currently, only MDString is supported with btf_tag attribute.
845     const MDString *Value = cast<MDString>(MD->getOperand(1));
846 
847     DIE &AnnotationDie = createAndAddDIE(dwarf::DW_TAG_LLVM_annotation, Buffer);
848     addString(AnnotationDie, dwarf::DW_AT_name, Name->getString());
849     addString(AnnotationDie, dwarf::DW_AT_const_value, Value->getString());
850   }
851 }
852 
853 void DwarfUnit::constructTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
854   // Add name if not anonymous or intermediate type.
855   StringRef Name = CTy->getName();
856 
857   uint64_t Size = CTy->getSizeInBits() >> 3;
858   uint16_t Tag = Buffer.getTag();
859 
860   switch (Tag) {
861   case dwarf::DW_TAG_array_type:
862     constructArrayTypeDIE(Buffer, CTy);
863     break;
864   case dwarf::DW_TAG_enumeration_type:
865     constructEnumTypeDIE(Buffer, CTy);
866     break;
867   case dwarf::DW_TAG_variant_part:
868   case dwarf::DW_TAG_structure_type:
869   case dwarf::DW_TAG_union_type:
870   case dwarf::DW_TAG_class_type: {
871     // Emit the discriminator for a variant part.
872     DIDerivedType *Discriminator = nullptr;
873     if (Tag == dwarf::DW_TAG_variant_part) {
874       Discriminator = CTy->getDiscriminator();
875       if (Discriminator) {
876         // DWARF says:
877         //    If the variant part has a discriminant, the discriminant is
878         //    represented by a separate debugging information entry which is
879         //    a child of the variant part entry.
880         DIE &DiscMember = constructMemberDIE(Buffer, Discriminator);
881         addDIEEntry(Buffer, dwarf::DW_AT_discr, DiscMember);
882       }
883     }
884 
885     // Add template parameters to a class, structure or union types.
886     if (Tag == dwarf::DW_TAG_class_type ||
887         Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type)
888       addTemplateParams(Buffer, CTy->getTemplateParams());
889 
890     // Add elements to structure type.
891     DINodeArray Elements = CTy->getElements();
892     for (const auto *Element : Elements) {
893       if (!Element)
894         continue;
895       if (auto *SP = dyn_cast<DISubprogram>(Element))
896         getOrCreateSubprogramDIE(SP);
897       else if (auto *DDTy = dyn_cast<DIDerivedType>(Element)) {
898         if (DDTy->getTag() == dwarf::DW_TAG_friend) {
899           DIE &ElemDie = createAndAddDIE(dwarf::DW_TAG_friend, Buffer);
900           addType(ElemDie, DDTy->getBaseType(), dwarf::DW_AT_friend);
901         } else if (DDTy->isStaticMember()) {
902           getOrCreateStaticMemberDIE(DDTy);
903         } else if (Tag == dwarf::DW_TAG_variant_part) {
904           // When emitting a variant part, wrap each member in
905           // DW_TAG_variant.
906           DIE &Variant = createAndAddDIE(dwarf::DW_TAG_variant, Buffer);
907           if (const ConstantInt *CI =
908               dyn_cast_or_null<ConstantInt>(DDTy->getDiscriminantValue())) {
909             if (DD->isUnsignedDIType(Discriminator->getBaseType()))
910               addUInt(Variant, dwarf::DW_AT_discr_value, None, CI->getZExtValue());
911             else
912               addSInt(Variant, dwarf::DW_AT_discr_value, None, CI->getSExtValue());
913           }
914           constructMemberDIE(Variant, DDTy);
915         } else {
916           constructMemberDIE(Buffer, DDTy);
917         }
918       } else if (auto *Property = dyn_cast<DIObjCProperty>(Element)) {
919         DIE &ElemDie = createAndAddDIE(Property->getTag(), Buffer);
920         StringRef PropertyName = Property->getName();
921         addString(ElemDie, dwarf::DW_AT_APPLE_property_name, PropertyName);
922         if (Property->getType())
923           addType(ElemDie, Property->getType());
924         addSourceLine(ElemDie, Property);
925         StringRef GetterName = Property->getGetterName();
926         if (!GetterName.empty())
927           addString(ElemDie, dwarf::DW_AT_APPLE_property_getter, GetterName);
928         StringRef SetterName = Property->getSetterName();
929         if (!SetterName.empty())
930           addString(ElemDie, dwarf::DW_AT_APPLE_property_setter, SetterName);
931         if (unsigned PropertyAttributes = Property->getAttributes())
932           addUInt(ElemDie, dwarf::DW_AT_APPLE_property_attribute, None,
933                   PropertyAttributes);
934       } else if (auto *Composite = dyn_cast<DICompositeType>(Element)) {
935         if (Composite->getTag() == dwarf::DW_TAG_variant_part) {
936           DIE &VariantPart = createAndAddDIE(Composite->getTag(), Buffer);
937           constructTypeDIE(VariantPart, Composite);
938         }
939       }
940     }
941 
942     if (CTy->isAppleBlockExtension())
943       addFlag(Buffer, dwarf::DW_AT_APPLE_block);
944 
945     if (CTy->getExportSymbols())
946       addFlag(Buffer, dwarf::DW_AT_export_symbols);
947 
948     // This is outside the DWARF spec, but GDB expects a DW_AT_containing_type
949     // inside C++ composite types to point to the base class with the vtable.
950     // Rust uses DW_AT_containing_type to link a vtable to the type
951     // for which it was created.
952     if (auto *ContainingType = CTy->getVTableHolder())
953       addDIEEntry(Buffer, dwarf::DW_AT_containing_type,
954                   *getOrCreateTypeDIE(ContainingType));
955 
956     if (CTy->isObjcClassComplete())
957       addFlag(Buffer, dwarf::DW_AT_APPLE_objc_complete_type);
958 
959     // Add the type's non-standard calling convention.
960     // DW_CC_pass_by_value/DW_CC_pass_by_reference are introduced in DWARF 5.
961     if (!Asm->TM.Options.DebugStrictDwarf || DD->getDwarfVersion() >= 5) {
962       uint8_t CC = 0;
963       if (CTy->isTypePassByValue())
964         CC = dwarf::DW_CC_pass_by_value;
965       else if (CTy->isTypePassByReference())
966         CC = dwarf::DW_CC_pass_by_reference;
967       if (CC)
968         addUInt(Buffer, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1,
969                 CC);
970     }
971     break;
972   }
973   default:
974     break;
975   }
976 
977   // Add name if not anonymous or intermediate type.
978   if (!Name.empty())
979     addString(Buffer, dwarf::DW_AT_name, Name);
980 
981   addAnnotation(Buffer, CTy->getAnnotations());
982 
983   if (Tag == dwarf::DW_TAG_enumeration_type ||
984       Tag == dwarf::DW_TAG_class_type || Tag == dwarf::DW_TAG_structure_type ||
985       Tag == dwarf::DW_TAG_union_type) {
986     // Add size if non-zero (derived types might be zero-sized.)
987     // Ignore the size if it's a non-enum forward decl.
988     // TODO: Do we care about size for enum forward declarations?
989     if (Size &&
990         (!CTy->isForwardDecl() || Tag == dwarf::DW_TAG_enumeration_type))
991       addUInt(Buffer, dwarf::DW_AT_byte_size, None, Size);
992     else if (!CTy->isForwardDecl())
993       // Add zero size if it is not a forward declaration.
994       addUInt(Buffer, dwarf::DW_AT_byte_size, None, 0);
995 
996     // If we're a forward decl, say so.
997     if (CTy->isForwardDecl())
998       addFlag(Buffer, dwarf::DW_AT_declaration);
999 
1000     // Add source line info if available.
1001     if (!CTy->isForwardDecl())
1002       addSourceLine(Buffer, CTy);
1003 
1004     // No harm in adding the runtime language to the declaration.
1005     unsigned RLang = CTy->getRuntimeLang();
1006     if (RLang)
1007       addUInt(Buffer, dwarf::DW_AT_APPLE_runtime_class, dwarf::DW_FORM_data1,
1008               RLang);
1009 
1010     // Add align info if available.
1011     if (uint32_t AlignInBytes = CTy->getAlignInBytes())
1012       addUInt(Buffer, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1013               AlignInBytes);
1014   }
1015 }
1016 
1017 void DwarfUnit::constructTemplateTypeParameterDIE(
1018     DIE &Buffer, const DITemplateTypeParameter *TP) {
1019   DIE &ParamDIE =
1020       createAndAddDIE(dwarf::DW_TAG_template_type_parameter, Buffer);
1021   // Add the type if it exists, it could be void and therefore no type.
1022   if (TP->getType())
1023     addType(ParamDIE, TP->getType());
1024   if (!TP->getName().empty())
1025     addString(ParamDIE, dwarf::DW_AT_name, TP->getName());
1026   if (TP->isDefault() && (DD->getDwarfVersion() >= 5))
1027     addFlag(ParamDIE, dwarf::DW_AT_default_value);
1028 }
1029 
1030 void DwarfUnit::constructTemplateValueParameterDIE(
1031     DIE &Buffer, const DITemplateValueParameter *VP) {
1032   DIE &ParamDIE = createAndAddDIE(VP->getTag(), Buffer);
1033 
1034   // Add the type if there is one, template template and template parameter
1035   // packs will not have a type.
1036   if (VP->getTag() == dwarf::DW_TAG_template_value_parameter)
1037     addType(ParamDIE, VP->getType());
1038   if (!VP->getName().empty())
1039     addString(ParamDIE, dwarf::DW_AT_name, VP->getName());
1040   if (VP->isDefault() && (DD->getDwarfVersion() >= 5))
1041     addFlag(ParamDIE, dwarf::DW_AT_default_value);
1042   if (Metadata *Val = VP->getValue()) {
1043     if (ConstantInt *CI = mdconst::dyn_extract<ConstantInt>(Val))
1044       addConstantValue(ParamDIE, CI, VP->getType());
1045     else if (GlobalValue *GV = mdconst::dyn_extract<GlobalValue>(Val)) {
1046       // We cannot describe the location of dllimport'd entities: the
1047       // computation of their address requires loads from the IAT.
1048       if (!GV->hasDLLImportStorageClass()) {
1049         // For declaration non-type template parameters (such as global values
1050         // and functions)
1051         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1052         addOpAddress(*Loc, Asm->getSymbol(GV));
1053         // Emit DW_OP_stack_value to use the address as the immediate value of
1054         // the parameter, rather than a pointer to it.
1055         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_stack_value);
1056         addBlock(ParamDIE, dwarf::DW_AT_location, Loc);
1057       }
1058     } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_template_param) {
1059       assert(isa<MDString>(Val));
1060       addString(ParamDIE, dwarf::DW_AT_GNU_template_name,
1061                 cast<MDString>(Val)->getString());
1062     } else if (VP->getTag() == dwarf::DW_TAG_GNU_template_parameter_pack) {
1063       addTemplateParams(ParamDIE, cast<MDTuple>(Val));
1064     }
1065   }
1066 }
1067 
1068 DIE *DwarfUnit::getOrCreateNameSpace(const DINamespace *NS) {
1069   // Construct the context before querying for the existence of the DIE in case
1070   // such construction creates the DIE.
1071   DIE *ContextDIE = getOrCreateContextDIE(NS->getScope());
1072 
1073   if (DIE *NDie = getDIE(NS))
1074     return NDie;
1075   DIE &NDie = createAndAddDIE(dwarf::DW_TAG_namespace, *ContextDIE, NS);
1076 
1077   StringRef Name = NS->getName();
1078   if (!Name.empty())
1079     addString(NDie, dwarf::DW_AT_name, NS->getName());
1080   else
1081     Name = "(anonymous namespace)";
1082   DD->addAccelNamespace(*CUNode, Name, NDie);
1083   addGlobalName(Name, NDie, NS->getScope());
1084   if (NS->getExportSymbols())
1085     addFlag(NDie, dwarf::DW_AT_export_symbols);
1086   return &NDie;
1087 }
1088 
1089 DIE *DwarfUnit::getOrCreateModule(const DIModule *M) {
1090   // Construct the context before querying for the existence of the DIE in case
1091   // such construction creates the DIE.
1092   DIE *ContextDIE = getOrCreateContextDIE(M->getScope());
1093 
1094   if (DIE *MDie = getDIE(M))
1095     return MDie;
1096   DIE &MDie = createAndAddDIE(dwarf::DW_TAG_module, *ContextDIE, M);
1097 
1098   if (!M->getName().empty()) {
1099     addString(MDie, dwarf::DW_AT_name, M->getName());
1100     addGlobalName(M->getName(), MDie, M->getScope());
1101   }
1102   if (!M->getConfigurationMacros().empty())
1103     addString(MDie, dwarf::DW_AT_LLVM_config_macros,
1104               M->getConfigurationMacros());
1105   if (!M->getIncludePath().empty())
1106     addString(MDie, dwarf::DW_AT_LLVM_include_path, M->getIncludePath());
1107   if (!M->getAPINotesFile().empty())
1108     addString(MDie, dwarf::DW_AT_LLVM_apinotes, M->getAPINotesFile());
1109   if (M->getFile())
1110     addUInt(MDie, dwarf::DW_AT_decl_file, None,
1111             getOrCreateSourceID(M->getFile()));
1112   if (M->getLineNo())
1113     addUInt(MDie, dwarf::DW_AT_decl_line, None, M->getLineNo());
1114   if (M->getIsDecl())
1115     addFlag(MDie, dwarf::DW_AT_declaration);
1116 
1117   return &MDie;
1118 }
1119 
1120 DIE *DwarfUnit::getOrCreateSubprogramDIE(const DISubprogram *SP, bool Minimal) {
1121   // Construct the context before querying for the existence of the DIE in case
1122   // such construction creates the DIE (as is the case for member function
1123   // declarations).
1124   DIE *ContextDIE =
1125       Minimal ? &getUnitDie() : getOrCreateContextDIE(SP->getScope());
1126 
1127   if (DIE *SPDie = getDIE(SP))
1128     return SPDie;
1129 
1130   if (auto *SPDecl = SP->getDeclaration()) {
1131     if (!Minimal) {
1132       // Add subprogram definitions to the CU die directly.
1133       ContextDIE = &getUnitDie();
1134       // Build the decl now to ensure it precedes the definition.
1135       getOrCreateSubprogramDIE(SPDecl);
1136     }
1137   }
1138 
1139   // DW_TAG_inlined_subroutine may refer to this DIE.
1140   DIE &SPDie = createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, SP);
1141 
1142   // Stop here and fill this in later, depending on whether or not this
1143   // subprogram turns out to have inlined instances or not.
1144   if (SP->isDefinition())
1145     return &SPDie;
1146 
1147   static_cast<DwarfUnit *>(SPDie.getUnit())
1148       ->applySubprogramAttributes(SP, SPDie);
1149   return &SPDie;
1150 }
1151 
1152 bool DwarfUnit::applySubprogramDefinitionAttributes(const DISubprogram *SP,
1153                                                     DIE &SPDie, bool Minimal) {
1154   DIE *DeclDie = nullptr;
1155   StringRef DeclLinkageName;
1156   if (auto *SPDecl = SP->getDeclaration()) {
1157     if (!Minimal) {
1158       DITypeRefArray DeclArgs, DefinitionArgs;
1159       DeclArgs = SPDecl->getType()->getTypeArray();
1160       DefinitionArgs = SP->getType()->getTypeArray();
1161 
1162       if (DeclArgs.size() && DefinitionArgs.size())
1163         if (DefinitionArgs[0] != NULL && DeclArgs[0] != DefinitionArgs[0])
1164           addType(SPDie, DefinitionArgs[0]);
1165 
1166       DeclDie = getDIE(SPDecl);
1167       assert(DeclDie && "This DIE should've already been constructed when the "
1168                         "definition DIE was created in "
1169                         "getOrCreateSubprogramDIE");
1170       // Look at the Decl's linkage name only if we emitted it.
1171       if (DD->useAllLinkageNames())
1172         DeclLinkageName = SPDecl->getLinkageName();
1173       unsigned DeclID = getOrCreateSourceID(SPDecl->getFile());
1174       unsigned DefID = getOrCreateSourceID(SP->getFile());
1175       if (DeclID != DefID)
1176         addUInt(SPDie, dwarf::DW_AT_decl_file, None, DefID);
1177 
1178       if (SP->getLine() != SPDecl->getLine())
1179         addUInt(SPDie, dwarf::DW_AT_decl_line, None, SP->getLine());
1180     }
1181   }
1182 
1183   // Add function template parameters.
1184   addTemplateParams(SPDie, SP->getTemplateParams());
1185 
1186   // Add the linkage name if we have one and it isn't in the Decl.
1187   StringRef LinkageName = SP->getLinkageName();
1188   assert(((LinkageName.empty() || DeclLinkageName.empty()) ||
1189           LinkageName == DeclLinkageName) &&
1190          "decl has a linkage name and it is different");
1191   if (DeclLinkageName.empty() &&
1192       // Always emit it for abstract subprograms.
1193       (DD->useAllLinkageNames() || DU->getAbstractSPDies().lookup(SP)))
1194     addLinkageName(SPDie, LinkageName);
1195 
1196   if (!DeclDie)
1197     return false;
1198 
1199   // Refer to the function declaration where all the other attributes will be
1200   // found.
1201   addDIEEntry(SPDie, dwarf::DW_AT_specification, *DeclDie);
1202   return true;
1203 }
1204 
1205 void DwarfUnit::applySubprogramAttributes(const DISubprogram *SP, DIE &SPDie,
1206                                           bool SkipSPAttributes) {
1207   // If -fdebug-info-for-profiling is enabled, need to emit the subprogram
1208   // and its source location.
1209   bool SkipSPSourceLocation = SkipSPAttributes &&
1210                               !CUNode->getDebugInfoForProfiling();
1211   if (!SkipSPSourceLocation)
1212     if (applySubprogramDefinitionAttributes(SP, SPDie, SkipSPAttributes))
1213       return;
1214 
1215   // Constructors and operators for anonymous aggregates do not have names.
1216   if (!SP->getName().empty())
1217     addString(SPDie, dwarf::DW_AT_name, SP->getName());
1218 
1219   addAnnotation(SPDie, SP->getAnnotations());
1220 
1221   if (!SkipSPSourceLocation)
1222     addSourceLine(SPDie, SP);
1223 
1224   // Skip the rest of the attributes under -gmlt to save space.
1225   if (SkipSPAttributes)
1226     return;
1227 
1228   // Add the prototype if we have a prototype and we have a C like
1229   // language.
1230   uint16_t Language = getLanguage();
1231   if (SP->isPrototyped() &&
1232       (Language == dwarf::DW_LANG_C89 || Language == dwarf::DW_LANG_C99 ||
1233        Language == dwarf::DW_LANG_ObjC))
1234     addFlag(SPDie, dwarf::DW_AT_prototyped);
1235 
1236   if (SP->isObjCDirect())
1237     addFlag(SPDie, dwarf::DW_AT_APPLE_objc_direct);
1238 
1239   unsigned CC = 0;
1240   DITypeRefArray Args;
1241   if (const DISubroutineType *SPTy = SP->getType()) {
1242     Args = SPTy->getTypeArray();
1243     CC = SPTy->getCC();
1244   }
1245 
1246   // Add a DW_AT_calling_convention if this has an explicit convention.
1247   if (CC && CC != dwarf::DW_CC_normal)
1248     addUInt(SPDie, dwarf::DW_AT_calling_convention, dwarf::DW_FORM_data1, CC);
1249 
1250   // Add a return type. If this is a type like a C/C++ void type we don't add a
1251   // return type.
1252   if (Args.size())
1253     if (auto Ty = Args[0])
1254       addType(SPDie, Ty);
1255 
1256   unsigned VK = SP->getVirtuality();
1257   if (VK) {
1258     addUInt(SPDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1, VK);
1259     if (SP->getVirtualIndex() != -1u) {
1260       DIELoc *Block = getDIELoc();
1261       addUInt(*Block, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1262       addUInt(*Block, dwarf::DW_FORM_udata, SP->getVirtualIndex());
1263       addBlock(SPDie, dwarf::DW_AT_vtable_elem_location, Block);
1264     }
1265     ContainingTypeMap.insert(std::make_pair(&SPDie, SP->getContainingType()));
1266   }
1267 
1268   if (!SP->isDefinition()) {
1269     addFlag(SPDie, dwarf::DW_AT_declaration);
1270 
1271     // Add arguments. Do not add arguments for subprogram definition. They will
1272     // be handled while processing variables.
1273     constructSubprogramArguments(SPDie, Args);
1274   }
1275 
1276   addThrownTypes(SPDie, SP->getThrownTypes());
1277 
1278   if (SP->isArtificial())
1279     addFlag(SPDie, dwarf::DW_AT_artificial);
1280 
1281   if (!SP->isLocalToUnit())
1282     addFlag(SPDie, dwarf::DW_AT_external);
1283 
1284   if (DD->useAppleExtensionAttributes()) {
1285     if (SP->isOptimized())
1286       addFlag(SPDie, dwarf::DW_AT_APPLE_optimized);
1287 
1288     if (unsigned isa = Asm->getISAEncoding())
1289       addUInt(SPDie, dwarf::DW_AT_APPLE_isa, dwarf::DW_FORM_flag, isa);
1290   }
1291 
1292   if (SP->isLValueReference())
1293     addFlag(SPDie, dwarf::DW_AT_reference);
1294 
1295   if (SP->isRValueReference())
1296     addFlag(SPDie, dwarf::DW_AT_rvalue_reference);
1297 
1298   if (SP->isNoReturn())
1299     addFlag(SPDie, dwarf::DW_AT_noreturn);
1300 
1301   if (SP->isProtected())
1302     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1303             dwarf::DW_ACCESS_protected);
1304   else if (SP->isPrivate())
1305     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1306             dwarf::DW_ACCESS_private);
1307   else if (SP->isPublic())
1308     addUInt(SPDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1309             dwarf::DW_ACCESS_public);
1310 
1311   if (SP->isExplicit())
1312     addFlag(SPDie, dwarf::DW_AT_explicit);
1313 
1314   if (SP->isMainSubprogram())
1315     addFlag(SPDie, dwarf::DW_AT_main_subprogram);
1316   if (SP->isPure())
1317     addFlag(SPDie, dwarf::DW_AT_pure);
1318   if (SP->isElemental())
1319     addFlag(SPDie, dwarf::DW_AT_elemental);
1320   if (SP->isRecursive())
1321     addFlag(SPDie, dwarf::DW_AT_recursive);
1322 
1323   if (DD->getDwarfVersion() >= 5 && SP->isDeleted())
1324     addFlag(SPDie, dwarf::DW_AT_deleted);
1325 }
1326 
1327 void DwarfUnit::constructSubrangeDIE(DIE &Buffer, const DISubrange *SR,
1328                                      DIE *IndexTy) {
1329   DIE &DW_Subrange = createAndAddDIE(dwarf::DW_TAG_subrange_type, Buffer);
1330   addDIEEntry(DW_Subrange, dwarf::DW_AT_type, *IndexTy);
1331 
1332   // The LowerBound value defines the lower bounds which is typically zero for
1333   // C/C++. The Count value is the number of elements.  Values are 64 bit. If
1334   // Count == -1 then the array is unbounded and we do not emit
1335   // DW_AT_lower_bound and DW_AT_count attributes.
1336   int64_t DefaultLowerBound = getDefaultLowerBound();
1337 
1338   auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1339                                DISubrange::BoundType Bound) -> void {
1340     if (auto *BV = Bound.dyn_cast<DIVariable *>()) {
1341       if (auto *VarDIE = getDIE(BV))
1342         addDIEEntry(DW_Subrange, Attr, *VarDIE);
1343     } else if (auto *BE = Bound.dyn_cast<DIExpression *>()) {
1344       DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1345       DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1346       DwarfExpr.setMemoryLocationKind();
1347       DwarfExpr.addExpression(BE);
1348       addBlock(DW_Subrange, Attr, DwarfExpr.finalize());
1349     } else if (auto *BI = Bound.dyn_cast<ConstantInt *>()) {
1350       if (Attr == dwarf::DW_AT_count) {
1351         if (BI->getSExtValue() != -1)
1352           addUInt(DW_Subrange, Attr, None, BI->getSExtValue());
1353       } else if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1354                  BI->getSExtValue() != DefaultLowerBound)
1355         addSInt(DW_Subrange, Attr, dwarf::DW_FORM_sdata, BI->getSExtValue());
1356     }
1357   };
1358 
1359   AddBoundTypeEntry(dwarf::DW_AT_lower_bound, SR->getLowerBound());
1360 
1361   AddBoundTypeEntry(dwarf::DW_AT_count, SR->getCount());
1362 
1363   AddBoundTypeEntry(dwarf::DW_AT_upper_bound, SR->getUpperBound());
1364 
1365   AddBoundTypeEntry(dwarf::DW_AT_byte_stride, SR->getStride());
1366 }
1367 
1368 void DwarfUnit::constructGenericSubrangeDIE(DIE &Buffer,
1369                                             const DIGenericSubrange *GSR,
1370                                             DIE *IndexTy) {
1371   DIE &DwGenericSubrange =
1372       createAndAddDIE(dwarf::DW_TAG_generic_subrange, Buffer);
1373   addDIEEntry(DwGenericSubrange, dwarf::DW_AT_type, *IndexTy);
1374 
1375   int64_t DefaultLowerBound = getDefaultLowerBound();
1376 
1377   auto AddBoundTypeEntry = [&](dwarf::Attribute Attr,
1378                                DIGenericSubrange::BoundType Bound) -> void {
1379     if (auto *BV = Bound.dyn_cast<DIVariable *>()) {
1380       if (auto *VarDIE = getDIE(BV))
1381         addDIEEntry(DwGenericSubrange, Attr, *VarDIE);
1382     } else if (auto *BE = Bound.dyn_cast<DIExpression *>()) {
1383       if (BE->isConstant() &&
1384           DIExpression::SignedOrUnsignedConstant::SignedConstant ==
1385               *BE->isConstant()) {
1386         if (Attr != dwarf::DW_AT_lower_bound || DefaultLowerBound == -1 ||
1387             static_cast<int64_t>(BE->getElement(1)) != DefaultLowerBound)
1388           addSInt(DwGenericSubrange, Attr, dwarf::DW_FORM_sdata,
1389                   BE->getElement(1));
1390       } else {
1391         DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1392         DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1393         DwarfExpr.setMemoryLocationKind();
1394         DwarfExpr.addExpression(BE);
1395         addBlock(DwGenericSubrange, Attr, DwarfExpr.finalize());
1396       }
1397     }
1398   };
1399 
1400   AddBoundTypeEntry(dwarf::DW_AT_lower_bound, GSR->getLowerBound());
1401   AddBoundTypeEntry(dwarf::DW_AT_count, GSR->getCount());
1402   AddBoundTypeEntry(dwarf::DW_AT_upper_bound, GSR->getUpperBound());
1403   AddBoundTypeEntry(dwarf::DW_AT_byte_stride, GSR->getStride());
1404 }
1405 
1406 DIE *DwarfUnit::getIndexTyDie() {
1407   if (IndexTyDie)
1408     return IndexTyDie;
1409   // Construct an integer type to use for indexes.
1410   IndexTyDie = &createAndAddDIE(dwarf::DW_TAG_base_type, getUnitDie());
1411   StringRef Name = "__ARRAY_SIZE_TYPE__";
1412   addString(*IndexTyDie, dwarf::DW_AT_name, Name);
1413   addUInt(*IndexTyDie, dwarf::DW_AT_byte_size, None, sizeof(int64_t));
1414   addUInt(*IndexTyDie, dwarf::DW_AT_encoding, dwarf::DW_FORM_data1,
1415           dwarf::DW_ATE_unsigned);
1416   DD->addAccelType(*CUNode, Name, *IndexTyDie, /*Flags*/ 0);
1417   return IndexTyDie;
1418 }
1419 
1420 /// Returns true if the vector's size differs from the sum of sizes of elements
1421 /// the user specified.  This can occur if the vector has been rounded up to
1422 /// fit memory alignment constraints.
1423 static bool hasVectorBeenPadded(const DICompositeType *CTy) {
1424   assert(CTy && CTy->isVector() && "Composite type is not a vector");
1425   const uint64_t ActualSize = CTy->getSizeInBits();
1426 
1427   // Obtain the size of each element in the vector.
1428   DIType *BaseTy = CTy->getBaseType();
1429   assert(BaseTy && "Unknown vector element type.");
1430   const uint64_t ElementSize = BaseTy->getSizeInBits();
1431 
1432   // Locate the number of elements in the vector.
1433   const DINodeArray Elements = CTy->getElements();
1434   assert(Elements.size() == 1 &&
1435          Elements[0]->getTag() == dwarf::DW_TAG_subrange_type &&
1436          "Invalid vector element array, expected one element of type subrange");
1437   const auto Subrange = cast<DISubrange>(Elements[0]);
1438   const auto NumVecElements =
1439       Subrange->getCount()
1440           ? Subrange->getCount().get<ConstantInt *>()->getSExtValue()
1441           : 0;
1442 
1443   // Ensure we found the element count and that the actual size is wide
1444   // enough to contain the requested size.
1445   assert(ActualSize >= (NumVecElements * ElementSize) && "Invalid vector size");
1446   return ActualSize != (NumVecElements * ElementSize);
1447 }
1448 
1449 void DwarfUnit::constructArrayTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1450   if (CTy->isVector()) {
1451     addFlag(Buffer, dwarf::DW_AT_GNU_vector);
1452     if (hasVectorBeenPadded(CTy))
1453       addUInt(Buffer, dwarf::DW_AT_byte_size, None,
1454               CTy->getSizeInBits() / CHAR_BIT);
1455   }
1456 
1457   if (DIVariable *Var = CTy->getDataLocation()) {
1458     if (auto *VarDIE = getDIE(Var))
1459       addDIEEntry(Buffer, dwarf::DW_AT_data_location, *VarDIE);
1460   } else if (DIExpression *Expr = CTy->getDataLocationExp()) {
1461     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1462     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1463     DwarfExpr.setMemoryLocationKind();
1464     DwarfExpr.addExpression(Expr);
1465     addBlock(Buffer, dwarf::DW_AT_data_location, DwarfExpr.finalize());
1466   }
1467 
1468   if (DIVariable *Var = CTy->getAssociated()) {
1469     if (auto *VarDIE = getDIE(Var))
1470       addDIEEntry(Buffer, dwarf::DW_AT_associated, *VarDIE);
1471   } else if (DIExpression *Expr = CTy->getAssociatedExp()) {
1472     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1473     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1474     DwarfExpr.setMemoryLocationKind();
1475     DwarfExpr.addExpression(Expr);
1476     addBlock(Buffer, dwarf::DW_AT_associated, DwarfExpr.finalize());
1477   }
1478 
1479   if (DIVariable *Var = CTy->getAllocated()) {
1480     if (auto *VarDIE = getDIE(Var))
1481       addDIEEntry(Buffer, dwarf::DW_AT_allocated, *VarDIE);
1482   } else if (DIExpression *Expr = CTy->getAllocatedExp()) {
1483     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1484     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1485     DwarfExpr.setMemoryLocationKind();
1486     DwarfExpr.addExpression(Expr);
1487     addBlock(Buffer, dwarf::DW_AT_allocated, DwarfExpr.finalize());
1488   }
1489 
1490   if (auto *RankConst = CTy->getRankConst()) {
1491     addSInt(Buffer, dwarf::DW_AT_rank, dwarf::DW_FORM_sdata,
1492             RankConst->getSExtValue());
1493   } else if (auto *RankExpr = CTy->getRankExp()) {
1494     DIELoc *Loc = new (DIEValueAllocator) DIELoc;
1495     DIEDwarfExpression DwarfExpr(*Asm, getCU(), *Loc);
1496     DwarfExpr.setMemoryLocationKind();
1497     DwarfExpr.addExpression(RankExpr);
1498     addBlock(Buffer, dwarf::DW_AT_rank, DwarfExpr.finalize());
1499   }
1500 
1501   // Emit the element type.
1502   addType(Buffer, CTy->getBaseType());
1503 
1504   // Get an anonymous type for index type.
1505   // FIXME: This type should be passed down from the front end
1506   // as different languages may have different sizes for indexes.
1507   DIE *IdxTy = getIndexTyDie();
1508 
1509   // Add subranges to array type.
1510   DINodeArray Elements = CTy->getElements();
1511   for (DINode *E : Elements) {
1512     // FIXME: Should this really be such a loose cast?
1513     if (auto *Element = dyn_cast_or_null<DINode>(E)) {
1514       if (Element->getTag() == dwarf::DW_TAG_subrange_type)
1515         constructSubrangeDIE(Buffer, cast<DISubrange>(Element), IdxTy);
1516       else if (Element->getTag() == dwarf::DW_TAG_generic_subrange)
1517         constructGenericSubrangeDIE(Buffer, cast<DIGenericSubrange>(Element),
1518                                     IdxTy);
1519     }
1520   }
1521 }
1522 
1523 void DwarfUnit::constructEnumTypeDIE(DIE &Buffer, const DICompositeType *CTy) {
1524   const DIType *DTy = CTy->getBaseType();
1525   bool IsUnsigned = DTy && DD->isUnsignedDIType(DTy);
1526   if (DTy) {
1527     if (DD->getDwarfVersion() >= 3)
1528       addType(Buffer, DTy);
1529     if (DD->getDwarfVersion() >= 4 && (CTy->getFlags() & DINode::FlagEnumClass))
1530       addFlag(Buffer, dwarf::DW_AT_enum_class);
1531   }
1532 
1533   auto *Context = CTy->getScope();
1534   bool IndexEnumerators = !Context || isa<DICompileUnit>(Context) || isa<DIFile>(Context) ||
1535       isa<DINamespace>(Context) || isa<DICommonBlock>(Context);
1536   DINodeArray Elements = CTy->getElements();
1537 
1538   // Add enumerators to enumeration type.
1539   for (const DINode *E : Elements) {
1540     auto *Enum = dyn_cast_or_null<DIEnumerator>(E);
1541     if (Enum) {
1542       DIE &Enumerator = createAndAddDIE(dwarf::DW_TAG_enumerator, Buffer);
1543       StringRef Name = Enum->getName();
1544       addString(Enumerator, dwarf::DW_AT_name, Name);
1545       addConstantValue(Enumerator, Enum->getValue(), IsUnsigned);
1546       if (IndexEnumerators)
1547         addGlobalName(Name, Enumerator, Context);
1548     }
1549   }
1550 }
1551 
1552 void DwarfUnit::constructContainingTypeDIEs() {
1553   for (auto &P : ContainingTypeMap) {
1554     DIE &SPDie = *P.first;
1555     const DINode *D = P.second;
1556     if (!D)
1557       continue;
1558     DIE *NDie = getDIE(D);
1559     if (!NDie)
1560       continue;
1561     addDIEEntry(SPDie, dwarf::DW_AT_containing_type, *NDie);
1562   }
1563 }
1564 
1565 DIE &DwarfUnit::constructMemberDIE(DIE &Buffer, const DIDerivedType *DT) {
1566   DIE &MemberDie = createAndAddDIE(DT->getTag(), Buffer);
1567   StringRef Name = DT->getName();
1568   if (!Name.empty())
1569     addString(MemberDie, dwarf::DW_AT_name, Name);
1570 
1571   addAnnotation(MemberDie, DT->getAnnotations());
1572 
1573   if (DIType *Resolved = DT->getBaseType())
1574     addType(MemberDie, Resolved);
1575 
1576   addSourceLine(MemberDie, DT);
1577 
1578   if (DT->getTag() == dwarf::DW_TAG_inheritance && DT->isVirtual()) {
1579 
1580     // For C++, virtual base classes are not at fixed offset. Use following
1581     // expression to extract appropriate offset from vtable.
1582     // BaseAddr = ObAddr + *((*ObAddr) - Offset)
1583 
1584     DIELoc *VBaseLocationDie = new (DIEValueAllocator) DIELoc;
1585     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_dup);
1586     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1587     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
1588     addUInt(*VBaseLocationDie, dwarf::DW_FORM_udata, DT->getOffsetInBits());
1589     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_minus);
1590     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_deref);
1591     addUInt(*VBaseLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
1592 
1593     addBlock(MemberDie, dwarf::DW_AT_data_member_location, VBaseLocationDie);
1594   } else {
1595     uint64_t Size = DT->getSizeInBits();
1596     uint64_t FieldSize = DD->getBaseTypeSize(DT);
1597     uint32_t AlignInBytes = DT->getAlignInBytes();
1598     uint64_t OffsetInBytes;
1599 
1600     bool IsBitfield = FieldSize && Size != FieldSize;
1601     if (IsBitfield) {
1602       // Handle bitfield, assume bytes are 8 bits.
1603       if (DD->useDWARF2Bitfields())
1604         addUInt(MemberDie, dwarf::DW_AT_byte_size, None, FieldSize/8);
1605       addUInt(MemberDie, dwarf::DW_AT_bit_size, None, Size);
1606 
1607       uint64_t Offset = DT->getOffsetInBits();
1608       // We can't use DT->getAlignInBits() here: AlignInBits for member type
1609       // is non-zero if and only if alignment was forced (e.g. _Alignas()),
1610       // which can't be done with bitfields. Thus we use FieldSize here.
1611       uint32_t AlignInBits = FieldSize;
1612       uint32_t AlignMask = ~(AlignInBits - 1);
1613       // The bits from the start of the storage unit to the start of the field.
1614       uint64_t StartBitOffset = Offset - (Offset & AlignMask);
1615       // The byte offset of the field's aligned storage unit inside the struct.
1616       OffsetInBytes = (Offset - StartBitOffset) / 8;
1617 
1618       if (DD->useDWARF2Bitfields()) {
1619         uint64_t HiMark = (Offset + FieldSize) & AlignMask;
1620         uint64_t FieldOffset = (HiMark - FieldSize);
1621         Offset -= FieldOffset;
1622 
1623         // Maybe we need to work from the other end.
1624         if (Asm->getDataLayout().isLittleEndian())
1625           Offset = FieldSize - (Offset + Size);
1626 
1627         addUInt(MemberDie, dwarf::DW_AT_bit_offset, None, Offset);
1628         OffsetInBytes = FieldOffset >> 3;
1629       } else {
1630         addUInt(MemberDie, dwarf::DW_AT_data_bit_offset, None, Offset);
1631       }
1632     } else {
1633       // This is not a bitfield.
1634       OffsetInBytes = DT->getOffsetInBits() / 8;
1635       if (AlignInBytes)
1636         addUInt(MemberDie, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1637                 AlignInBytes);
1638     }
1639 
1640     if (DD->getDwarfVersion() <= 2) {
1641       DIELoc *MemLocationDie = new (DIEValueAllocator) DIELoc;
1642       addUInt(*MemLocationDie, dwarf::DW_FORM_data1, dwarf::DW_OP_plus_uconst);
1643       addUInt(*MemLocationDie, dwarf::DW_FORM_udata, OffsetInBytes);
1644       addBlock(MemberDie, dwarf::DW_AT_data_member_location, MemLocationDie);
1645     } else if (!IsBitfield || DD->useDWARF2Bitfields()) {
1646       // In DWARF v3, DW_FORM_data4/8 in DW_AT_data_member_location are
1647       // interpreted as location-list pointers. Interpreting constants as
1648       // pointers is not expected, so we use DW_FORM_udata to encode the
1649       // constants here.
1650       if (DD->getDwarfVersion() == 3)
1651         addUInt(MemberDie, dwarf::DW_AT_data_member_location,
1652                 dwarf::DW_FORM_udata, OffsetInBytes);
1653       else
1654         addUInt(MemberDie, dwarf::DW_AT_data_member_location, None,
1655                 OffsetInBytes);
1656     }
1657   }
1658 
1659   if (DT->isProtected())
1660     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1661             dwarf::DW_ACCESS_protected);
1662   else if (DT->isPrivate())
1663     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1664             dwarf::DW_ACCESS_private);
1665   // Otherwise C++ member and base classes are considered public.
1666   else if (DT->isPublic())
1667     addUInt(MemberDie, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1668             dwarf::DW_ACCESS_public);
1669   if (DT->isVirtual())
1670     addUInt(MemberDie, dwarf::DW_AT_virtuality, dwarf::DW_FORM_data1,
1671             dwarf::DW_VIRTUALITY_virtual);
1672 
1673   // Objective-C properties.
1674   if (DINode *PNode = DT->getObjCProperty())
1675     if (DIE *PDie = getDIE(PNode))
1676       addAttribute(MemberDie, dwarf::DW_AT_APPLE_property,
1677                    dwarf::DW_FORM_ref4, DIEEntry(*PDie));
1678 
1679   if (DT->isArtificial())
1680     addFlag(MemberDie, dwarf::DW_AT_artificial);
1681 
1682   return MemberDie;
1683 }
1684 
1685 DIE *DwarfUnit::getOrCreateStaticMemberDIE(const DIDerivedType *DT) {
1686   if (!DT)
1687     return nullptr;
1688 
1689   // Construct the context before querying for the existence of the DIE in case
1690   // such construction creates the DIE.
1691   DIE *ContextDIE = getOrCreateContextDIE(DT->getScope());
1692   assert(dwarf::isType(ContextDIE->getTag()) &&
1693          "Static member should belong to a type.");
1694 
1695   if (DIE *StaticMemberDIE = getDIE(DT))
1696     return StaticMemberDIE;
1697 
1698   DIE &StaticMemberDIE = createAndAddDIE(DT->getTag(), *ContextDIE, DT);
1699 
1700   const DIType *Ty = DT->getBaseType();
1701 
1702   addString(StaticMemberDIE, dwarf::DW_AT_name, DT->getName());
1703   addType(StaticMemberDIE, Ty);
1704   addSourceLine(StaticMemberDIE, DT);
1705   addFlag(StaticMemberDIE, dwarf::DW_AT_external);
1706   addFlag(StaticMemberDIE, dwarf::DW_AT_declaration);
1707 
1708   // FIXME: We could omit private if the parent is a class_type, and
1709   // public if the parent is something else.
1710   if (DT->isProtected())
1711     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1712             dwarf::DW_ACCESS_protected);
1713   else if (DT->isPrivate())
1714     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1715             dwarf::DW_ACCESS_private);
1716   else if (DT->isPublic())
1717     addUInt(StaticMemberDIE, dwarf::DW_AT_accessibility, dwarf::DW_FORM_data1,
1718             dwarf::DW_ACCESS_public);
1719 
1720   if (const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(DT->getConstant()))
1721     addConstantValue(StaticMemberDIE, CI, Ty);
1722   if (const ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(DT->getConstant()))
1723     addConstantFPValue(StaticMemberDIE, CFP);
1724 
1725   if (uint32_t AlignInBytes = DT->getAlignInBytes())
1726     addUInt(StaticMemberDIE, dwarf::DW_AT_alignment, dwarf::DW_FORM_udata,
1727             AlignInBytes);
1728 
1729   return &StaticMemberDIE;
1730 }
1731 
1732 void DwarfUnit::emitCommonHeader(bool UseOffsets, dwarf::UnitType UT) {
1733   // Emit size of content not including length itself
1734   if (!DD->useSectionsAsReferences())
1735     EndLabel = Asm->emitDwarfUnitLength(
1736         isDwoUnit() ? "debug_info_dwo" : "debug_info", "Length of Unit");
1737   else
1738     Asm->emitDwarfUnitLength(getHeaderSize() + getUnitDie().getSize(),
1739                              "Length of Unit");
1740 
1741   Asm->OutStreamer->AddComment("DWARF version number");
1742   unsigned Version = DD->getDwarfVersion();
1743   Asm->emitInt16(Version);
1744 
1745   // DWARF v5 reorders the address size and adds a unit type.
1746   if (Version >= 5) {
1747     Asm->OutStreamer->AddComment("DWARF Unit Type");
1748     Asm->emitInt8(UT);
1749     Asm->OutStreamer->AddComment("Address Size (in bytes)");
1750     Asm->emitInt8(Asm->MAI->getCodePointerSize());
1751   }
1752 
1753   // We share one abbreviations table across all units so it's always at the
1754   // start of the section. Use a relocatable offset where needed to ensure
1755   // linking doesn't invalidate that offset.
1756   Asm->OutStreamer->AddComment("Offset Into Abbrev. Section");
1757   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1758   if (UseOffsets)
1759     Asm->emitDwarfLengthOrOffset(0);
1760   else
1761     Asm->emitDwarfSymbolReference(
1762         TLOF.getDwarfAbbrevSection()->getBeginSymbol(), false);
1763 
1764   if (Version <= 4) {
1765     Asm->OutStreamer->AddComment("Address Size (in bytes)");
1766     Asm->emitInt8(Asm->MAI->getCodePointerSize());
1767   }
1768 }
1769 
1770 void DwarfTypeUnit::emitHeader(bool UseOffsets) {
1771   DwarfUnit::emitCommonHeader(UseOffsets,
1772                               DD->useSplitDwarf() ? dwarf::DW_UT_split_type
1773                                                   : dwarf::DW_UT_type);
1774   Asm->OutStreamer->AddComment("Type Signature");
1775   Asm->OutStreamer->emitIntValue(TypeSignature, sizeof(TypeSignature));
1776   Asm->OutStreamer->AddComment("Type DIE Offset");
1777   // In a skeleton type unit there is no type DIE so emit a zero offset.
1778   Asm->emitDwarfLengthOrOffset(Ty ? Ty->getOffset() : 0);
1779 }
1780 
1781 void DwarfUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
1782                                 const MCSymbol *Hi, const MCSymbol *Lo) {
1783   addAttribute(Die, Attribute, DD->getDwarfSectionOffsetForm(),
1784                new (DIEValueAllocator) DIEDelta(Hi, Lo));
1785 }
1786 
1787 void DwarfUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
1788                                 const MCSymbol *Label, const MCSymbol *Sec) {
1789   if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1790     addLabel(Die, Attribute, DD->getDwarfSectionOffsetForm(), Label);
1791   else
1792     addSectionDelta(Die, Attribute, Label, Sec);
1793 }
1794 
1795 bool DwarfTypeUnit::isDwoUnit() const {
1796   // Since there are no skeleton type units, all type units are dwo type units
1797   // when split DWARF is being used.
1798   return DD->useSplitDwarf();
1799 }
1800 
1801 void DwarfTypeUnit::addGlobalName(StringRef Name, const DIE &Die,
1802                                   const DIScope *Context) {
1803   getCU().addGlobalNameForTypeUnit(Name, Context);
1804 }
1805 
1806 void DwarfTypeUnit::addGlobalType(const DIType *Ty, const DIE &Die,
1807                                   const DIScope *Context) {
1808   getCU().addGlobalTypeUnitType(Ty, Context);
1809 }
1810 
1811 const MCSymbol *DwarfUnit::getCrossSectionRelativeBaseAddress() const {
1812   if (!Asm->MAI->doesDwarfUseRelocationsAcrossSections())
1813     return nullptr;
1814   if (isDwoUnit())
1815     return nullptr;
1816   return getSection()->getBeginSymbol();
1817 }
1818 
1819 void DwarfUnit::addStringOffsetsStart() {
1820   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1821   addSectionLabel(getUnitDie(), dwarf::DW_AT_str_offsets_base,
1822                   DU->getStringOffsetsStartSym(),
1823                   TLOF.getDwarfStrOffSection()->getBeginSymbol());
1824 }
1825 
1826 void DwarfUnit::addRnglistsBase() {
1827   assert(DD->getDwarfVersion() >= 5 &&
1828          "DW_AT_rnglists_base requires DWARF version 5 or later");
1829   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
1830   addSectionLabel(getUnitDie(), dwarf::DW_AT_rnglists_base,
1831                   DU->getRnglistsTableBaseSym(),
1832                   TLOF.getDwarfRnglistsSection()->getBeginSymbol());
1833 }
1834 
1835 void DwarfTypeUnit::finishNonUnitTypeDIE(DIE& D, const DICompositeType *CTy) {
1836   addFlag(D, dwarf::DW_AT_declaration);
1837   StringRef Name = CTy->getName();
1838   if (!Name.empty())
1839     addString(D, dwarf::DW_AT_name, Name);
1840   getCU().createTypeDIE(CTy);
1841 }
1842