Coverage Report

Created: 2019-07-24 05:18

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