1 //===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for writing dwarf debug info into asm files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "DwarfDebug.h"
15 #include "ByteStreamer.h"
16 #include "DIEHash.h"
17 #include "DebugLocEntry.h"
18 #include "DwarfCompileUnit.h"
19 #include "DwarfExpression.h"
20 #include "DwarfUnit.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/CodeGen/DIE.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineModuleInfo.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DIBuilder.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DebugInfo.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/ValueHandle.h"
35 #include "llvm/MC/MCAsmInfo.h"
36 #include "llvm/MC/MCDwarf.h"
37 #include "llvm/MC/MCSection.h"
38 #include "llvm/MC/MCStreamer.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/Support/CommandLine.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/Dwarf.h"
43 #include "llvm/Support/Endian.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/FormattedStream.h"
46 #include "llvm/Support/LEB128.h"
47 #include "llvm/Support/MD5.h"
48 #include "llvm/Support/Path.h"
49 #include "llvm/Support/Timer.h"
50 #include "llvm/Support/raw_ostream.h"
51 #include "llvm/Target/TargetFrameLowering.h"
52 #include "llvm/Target/TargetLoweringObjectFile.h"
53 #include "llvm/Target/TargetMachine.h"
54 #include "llvm/Target/TargetOptions.h"
55 #include "llvm/Target/TargetRegisterInfo.h"
56 #include "llvm/Target/TargetSubtargetInfo.h"
57 using namespace llvm;
58
59 #define DEBUG_TYPE "dwarfdebug"
60
61 static cl::opt<bool>
62 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
63 cl::desc("Disable debug info printing"));
64
65 static cl::opt<bool> UnknownLocations(
66 "use-unknown-locations", cl::Hidden,
67 cl::desc("Make an absence of debug location information explicit."),
68 cl::init(false));
69
70 static cl::opt<bool>
71 GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
72 cl::desc("Generate GNU-style pubnames and pubtypes"),
73 cl::init(false));
74
75 static cl::opt<bool> GenerateARangeSection("generate-arange-section",
76 cl::Hidden,
77 cl::desc("Generate dwarf aranges"),
78 cl::init(false));
79
80 namespace {
81 enum DefaultOnOff { Default, Enable, Disable };
82 }
83
84 static cl::opt<DefaultOnOff>
85 DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
86 cl::desc("Output prototype dwarf accelerator tables."),
87 cl::values(clEnumVal(Default, "Default for platform"),
88 clEnumVal(Enable, "Enabled"),
89 clEnumVal(Disable, "Disabled"), clEnumValEnd),
90 cl::init(Default));
91
92 static cl::opt<DefaultOnOff>
93 SplitDwarf("split-dwarf", cl::Hidden,
94 cl::desc("Output DWARF5 split debug info."),
95 cl::values(clEnumVal(Default, "Default for platform"),
96 clEnumVal(Enable, "Enabled"),
97 clEnumVal(Disable, "Disabled"), clEnumValEnd),
98 cl::init(Default));
99
100 static cl::opt<DefaultOnOff>
101 DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
102 cl::desc("Generate DWARF pubnames and pubtypes sections"),
103 cl::values(clEnumVal(Default, "Default for platform"),
104 clEnumVal(Enable, "Enabled"),
105 clEnumVal(Disable, "Disabled"), clEnumValEnd),
106 cl::init(Default));
107
108 static cl::opt<DefaultOnOff>
109 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
110 cl::desc("Emit DWARF linkage-name attributes."),
111 cl::values(clEnumVal(Default, "Default for platform"),
112 clEnumVal(Enable, "Enabled"),
113 clEnumVal(Disable, "Disabled"), clEnumValEnd),
114 cl::init(Default));
115
116 static const char *const DWARFGroupName = "DWARF Emission";
117 static const char *const DbgTimerName = "DWARF Debug Writer";
118
EmitOp(uint8_t Op,const char * Comment)119 void DebugLocDwarfExpression::EmitOp(uint8_t Op, const char *Comment) {
120 BS.EmitInt8(
121 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
122 : dwarf::OperationEncodingString(Op));
123 }
124
EmitSigned(int64_t Value)125 void DebugLocDwarfExpression::EmitSigned(int64_t Value) {
126 BS.EmitSLEB128(Value, Twine(Value));
127 }
128
EmitUnsigned(uint64_t Value)129 void DebugLocDwarfExpression::EmitUnsigned(uint64_t Value) {
130 BS.EmitULEB128(Value, Twine(Value));
131 }
132
isFrameRegister(unsigned MachineReg)133 bool DebugLocDwarfExpression::isFrameRegister(unsigned MachineReg) {
134 // This information is not available while emitting .debug_loc entries.
135 return false;
136 }
137
138 //===----------------------------------------------------------------------===//
139
140 /// resolve - Look in the DwarfDebug map for the MDNode that
141 /// corresponds to the reference.
resolve(TypedDINodeRef<T> Ref) const142 template <typename T> T *DbgVariable::resolve(TypedDINodeRef<T> Ref) const {
143 return DD->resolve(Ref);
144 }
145
isBlockByrefVariable() const146 bool DbgVariable::isBlockByrefVariable() const {
147 assert(Var && "Invalid complex DbgVariable!");
148 return Var->getType()
149 .resolve(DD->getTypeIdentifierMap())
150 ->isBlockByrefStruct();
151 }
152
getType() const153 const DIType *DbgVariable::getType() const {
154 DIType *Ty = Var->getType().resolve(DD->getTypeIdentifierMap());
155 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
156 // addresses instead.
157 if (Ty->isBlockByrefStruct()) {
158 /* Byref variables, in Blocks, are declared by the programmer as
159 "SomeType VarName;", but the compiler creates a
160 __Block_byref_x_VarName struct, and gives the variable VarName
161 either the struct, or a pointer to the struct, as its type. This
162 is necessary for various behind-the-scenes things the compiler
163 needs to do with by-reference variables in blocks.
164
165 However, as far as the original *programmer* is concerned, the
166 variable should still have type 'SomeType', as originally declared.
167
168 The following function dives into the __Block_byref_x_VarName
169 struct to find the original type of the variable. This will be
170 passed back to the code generating the type for the Debug
171 Information Entry for the variable 'VarName'. 'VarName' will then
172 have the original type 'SomeType' in its debug information.
173
174 The original type 'SomeType' will be the type of the field named
175 'VarName' inside the __Block_byref_x_VarName struct.
176
177 NOTE: In order for this to not completely fail on the debugger
178 side, the Debug Information Entry for the variable VarName needs to
179 have a DW_AT_location that tells the debugger how to unwind through
180 the pointers and __Block_byref_x_VarName struct to find the actual
181 value of the variable. The function addBlockByrefType does this. */
182 DIType *subType = Ty;
183 uint16_t tag = Ty->getTag();
184
185 if (tag == dwarf::DW_TAG_pointer_type)
186 subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
187
188 auto Elements = cast<DICompositeType>(subType)->getElements();
189 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
190 auto *DT = cast<DIDerivedType>(Elements[i]);
191 if (getName() == DT->getName())
192 return resolve(DT->getBaseType());
193 }
194 }
195 return Ty;
196 }
197
198 static LLVM_CONSTEXPR DwarfAccelTable::Atom TypeAtoms[] = {
199 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
200 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
201 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
202
DwarfDebug(AsmPrinter * A,Module * M)203 DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
204 : Asm(A), MMI(Asm->MMI), DebugLocs(A->OutStreamer->isVerboseAsm()),
205 PrevLabel(nullptr), InfoHolder(A, "info_string", DIEValueAllocator),
206 SkeletonHolder(A, "skel_string", DIEValueAllocator),
207 IsDarwin(Triple(A->getTargetTriple()).isOSDarwin()),
208 AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
209 dwarf::DW_FORM_data4)),
210 AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
211 dwarf::DW_FORM_data4)),
212 AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
213 dwarf::DW_FORM_data4)),
214 AccelTypes(TypeAtoms), DebuggerTuning(DebuggerKind::Default) {
215
216 CurFn = nullptr;
217 CurMI = nullptr;
218 Triple TT(Asm->getTargetTriple());
219
220 // Make sure we know our "debugger tuning." The target option takes
221 // precedence; fall back to triple-based defaults.
222 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
223 DebuggerTuning = Asm->TM.Options.DebuggerTuning;
224 else if (IsDarwin || TT.isOSFreeBSD())
225 DebuggerTuning = DebuggerKind::LLDB;
226 else if (TT.isPS4CPU())
227 DebuggerTuning = DebuggerKind::SCE;
228 else
229 DebuggerTuning = DebuggerKind::GDB;
230
231 // Turn on accelerator tables for LLDB by default.
232 if (DwarfAccelTables == Default)
233 HasDwarfAccelTables = tuneForLLDB();
234 else
235 HasDwarfAccelTables = DwarfAccelTables == Enable;
236
237 // Handle split DWARF. Off by default for now.
238 if (SplitDwarf == Default)
239 HasSplitDwarf = false;
240 else
241 HasSplitDwarf = SplitDwarf == Enable;
242
243 // Pubnames/pubtypes on by default for GDB.
244 if (DwarfPubSections == Default)
245 HasDwarfPubSections = tuneForGDB();
246 else
247 HasDwarfPubSections = DwarfPubSections == Enable;
248
249 // SCE does not use linkage names.
250 if (DwarfLinkageNames == Default)
251 UseLinkageNames = !tuneForSCE();
252 else
253 UseLinkageNames = DwarfLinkageNames == Enable;
254
255 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
256 DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
257 : MMI->getModule()->getDwarfVersion();
258 // Use dwarf 4 by default if nothing is requested.
259 DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION;
260
261 // Work around a GDB bug. GDB doesn't support the standard opcode;
262 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
263 // is defined as of DWARF 3.
264 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
265 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
266 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
267
268 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
269
270 {
271 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
272 beginModule();
273 }
274 }
275
276 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
~DwarfDebug()277 DwarfDebug::~DwarfDebug() { }
278
isObjCClass(StringRef Name)279 static bool isObjCClass(StringRef Name) {
280 return Name.startswith("+") || Name.startswith("-");
281 }
282
hasObjCCategory(StringRef Name)283 static bool hasObjCCategory(StringRef Name) {
284 if (!isObjCClass(Name))
285 return false;
286
287 return Name.find(") ") != StringRef::npos;
288 }
289
getObjCClassCategory(StringRef In,StringRef & Class,StringRef & Category)290 static void getObjCClassCategory(StringRef In, StringRef &Class,
291 StringRef &Category) {
292 if (!hasObjCCategory(In)) {
293 Class = In.slice(In.find('[') + 1, In.find(' '));
294 Category = "";
295 return;
296 }
297
298 Class = In.slice(In.find('[') + 1, In.find('('));
299 Category = In.slice(In.find('[') + 1, In.find(' '));
300 return;
301 }
302
getObjCMethodName(StringRef In)303 static StringRef getObjCMethodName(StringRef In) {
304 return In.slice(In.find(' ') + 1, In.find(']'));
305 }
306
307 // Add the various names to the Dwarf accelerator table names.
308 // TODO: Determine whether or not we should add names for programs
309 // that do not have a DW_AT_name or DW_AT_linkage_name field - this
310 // is only slightly different than the lookup of non-standard ObjC names.
addSubprogramNames(const DISubprogram * SP,DIE & Die)311 void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) {
312 if (!SP->isDefinition())
313 return;
314 addAccelName(SP->getName(), Die);
315
316 // If the linkage name is different than the name, go ahead and output
317 // that as well into the name table.
318 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName())
319 addAccelName(SP->getLinkageName(), Die);
320
321 // If this is an Objective-C selector name add it to the ObjC accelerator
322 // too.
323 if (isObjCClass(SP->getName())) {
324 StringRef Class, Category;
325 getObjCClassCategory(SP->getName(), Class, Category);
326 addAccelObjC(Class, Die);
327 if (Category != "")
328 addAccelObjC(Category, Die);
329 // Also add the base method name to the name table.
330 addAccelName(getObjCMethodName(SP->getName()), Die);
331 }
332 }
333
334 /// Check whether we should create a DIE for the given Scope, return true
335 /// if we don't create a DIE (the corresponding DIE is null).
isLexicalScopeDIENull(LexicalScope * Scope)336 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
337 if (Scope->isAbstractScope())
338 return false;
339
340 // We don't create a DIE if there is no Range.
341 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
342 if (Ranges.empty())
343 return true;
344
345 if (Ranges.size() > 1)
346 return false;
347
348 // We don't create a DIE if we have a single Range and the end label
349 // is null.
350 return !getLabelAfterInsn(Ranges.front().second);
351 }
352
forBothCUs(DwarfCompileUnit & CU,Func F)353 template <typename Func> void forBothCUs(DwarfCompileUnit &CU, Func F) {
354 F(CU);
355 if (auto *SkelCU = CU.getSkeleton())
356 F(*SkelCU);
357 }
358
constructAbstractSubprogramScopeDIE(LexicalScope * Scope)359 void DwarfDebug::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) {
360 assert(Scope && Scope->getScopeNode());
361 assert(Scope->isAbstractScope());
362 assert(!Scope->getInlinedAt());
363
364 const MDNode *SP = Scope->getScopeNode();
365
366 ProcessedSPNodes.insert(SP);
367
368 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
369 // was inlined from another compile unit.
370 auto &CU = SPMap[SP];
371 forBothCUs(*CU, [&](DwarfCompileUnit &CU) {
372 CU.constructAbstractSubprogramScopeDIE(Scope);
373 });
374 }
375
addGnuPubAttributes(DwarfUnit & U,DIE & D) const376 void DwarfDebug::addGnuPubAttributes(DwarfUnit &U, DIE &D) const {
377 if (!GenerateGnuPubSections)
378 return;
379
380 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
381 }
382
383 // Create new DwarfCompileUnit for the given metadata node with tag
384 // DW_TAG_compile_unit.
385 DwarfCompileUnit &
constructDwarfCompileUnit(const DICompileUnit * DIUnit)386 DwarfDebug::constructDwarfCompileUnit(const DICompileUnit *DIUnit) {
387 StringRef FN = DIUnit->getFilename();
388 CompilationDir = DIUnit->getDirectory();
389
390 auto OwnedUnit = make_unique<DwarfCompileUnit>(
391 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
392 DwarfCompileUnit &NewCU = *OwnedUnit;
393 DIE &Die = NewCU.getUnitDie();
394 InfoHolder.addUnit(std::move(OwnedUnit));
395 if (useSplitDwarf())
396 NewCU.setSkeleton(constructSkeletonCU(NewCU));
397
398 // LTO with assembly output shares a single line table amongst multiple CUs.
399 // To avoid the compilation directory being ambiguous, let the line table
400 // explicitly describe the directory of all files, never relying on the
401 // compilation directory.
402 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
403 Asm->OutStreamer->getContext().setMCLineTableCompilationDir(
404 NewCU.getUniqueID(), CompilationDir);
405
406 NewCU.addString(Die, dwarf::DW_AT_producer, DIUnit->getProducer());
407 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
408 DIUnit->getSourceLanguage());
409 NewCU.addString(Die, dwarf::DW_AT_name, FN);
410
411 if (!useSplitDwarf()) {
412 NewCU.initStmtList();
413
414 // If we're using split dwarf the compilation dir is going to be in the
415 // skeleton CU and so we don't need to duplicate it here.
416 if (!CompilationDir.empty())
417 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
418
419 addGnuPubAttributes(NewCU, Die);
420 }
421
422 if (DIUnit->isOptimized())
423 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
424
425 StringRef Flags = DIUnit->getFlags();
426 if (!Flags.empty())
427 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
428
429 if (unsigned RVer = DIUnit->getRuntimeVersion())
430 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
431 dwarf::DW_FORM_data1, RVer);
432
433 if (useSplitDwarf())
434 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
435 else
436 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
437
438 if (DIUnit->getDWOId()) {
439 // This CU is either a clang module DWO or a skeleton CU.
440 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
441 DIUnit->getDWOId());
442 if (!DIUnit->getSplitDebugFilename().empty())
443 // This is a prefabricated skeleton CU.
444 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
445 DIUnit->getSplitDebugFilename());
446 }
447
448 CUMap.insert(std::make_pair(DIUnit, &NewCU));
449 CUDieMap.insert(std::make_pair(&Die, &NewCU));
450 return NewCU;
451 }
452
constructAndAddImportedEntityDIE(DwarfCompileUnit & TheCU,const DIImportedEntity * N)453 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
454 const DIImportedEntity *N) {
455 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
456 D->addChild(TheCU.constructImportedEntityDIE(N));
457 }
458
459 // Emit all Dwarf sections that should come prior to the content. Create
460 // global DIEs and emit initial debug info sections. This is invoked by
461 // the target AsmPrinter.
beginModule()462 void DwarfDebug::beginModule() {
463 if (DisableDebugInfoPrinting)
464 return;
465
466 const Module *M = MMI->getModule();
467
468 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
469 if (!CU_Nodes)
470 return;
471 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
472
473 SingleCU = CU_Nodes->getNumOperands() == 1;
474
475 for (MDNode *N : CU_Nodes->operands()) {
476 auto *CUNode = cast<DICompileUnit>(N);
477 DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
478 for (auto *IE : CUNode->getImportedEntities())
479 CU.addImportedEntity(IE);
480 for (auto *GV : CUNode->getGlobalVariables())
481 CU.getOrCreateGlobalVariableDIE(GV);
482 for (auto *SP : CUNode->getSubprograms())
483 SPMap.insert(std::make_pair(SP, &CU));
484 for (auto *Ty : CUNode->getEnumTypes()) {
485 // The enum types array by design contains pointers to
486 // MDNodes rather than DIRefs. Unique them here.
487 CU.getOrCreateTypeDIE(cast<DIType>(resolve(Ty->getRef())));
488 }
489 for (auto *Ty : CUNode->getRetainedTypes()) {
490 // The retained types array by design contains pointers to
491 // MDNodes rather than DIRefs. Unique them here.
492 DIType *RT = cast<DIType>(resolve(Ty->getRef()));
493 if (!RT->isExternalTypeRef())
494 // There is no point in force-emitting a forward declaration.
495 CU.getOrCreateTypeDIE(RT);
496 }
497 // Emit imported_modules last so that the relevant context is already
498 // available.
499 for (auto *IE : CUNode->getImportedEntities())
500 constructAndAddImportedEntityDIE(CU, IE);
501 }
502
503 // Tell MMI that we have debug info.
504 MMI->setDebugInfoAvailability(true);
505 }
506
finishVariableDefinitions()507 void DwarfDebug::finishVariableDefinitions() {
508 for (const auto &Var : ConcreteVariables) {
509 DIE *VariableDie = Var->getDIE();
510 assert(VariableDie);
511 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
512 // in the ConcreteVariables list, rather than looking it up again here.
513 // DIE::getUnit isn't simple - it walks parent pointers, etc.
514 DwarfCompileUnit *Unit = lookupUnit(VariableDie->getUnit());
515 assert(Unit);
516 DbgVariable *AbsVar = getExistingAbstractVariable(
517 InlinedVariable(Var->getVariable(), Var->getInlinedAt()));
518 if (AbsVar && AbsVar->getDIE()) {
519 Unit->addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin,
520 *AbsVar->getDIE());
521 } else
522 Unit->applyVariableAttributes(*Var, *VariableDie);
523 }
524 }
525
finishSubprogramDefinitions()526 void DwarfDebug::finishSubprogramDefinitions() {
527 for (const auto &P : SPMap)
528 forBothCUs(*P.second, [&](DwarfCompileUnit &CU) {
529 CU.finishSubprogramDefinition(cast<DISubprogram>(P.first));
530 });
531 }
532
533
534 // Collect info for variables that were optimized out.
collectDeadVariables()535 void DwarfDebug::collectDeadVariables() {
536 const Module *M = MMI->getModule();
537
538 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
539 for (MDNode *N : CU_Nodes->operands()) {
540 auto *TheCU = cast<DICompileUnit>(N);
541 // Construct subprogram DIE and add variables DIEs.
542 DwarfCompileUnit *SPCU =
543 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
544 assert(SPCU && "Unable to find Compile Unit!");
545 for (auto *SP : TheCU->getSubprograms()) {
546 if (ProcessedSPNodes.count(SP) != 0)
547 continue;
548 SPCU->collectDeadVariables(SP);
549 }
550 }
551 }
552 }
553
finalizeModuleInfo()554 void DwarfDebug::finalizeModuleInfo() {
555 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
556
557 finishSubprogramDefinitions();
558
559 finishVariableDefinitions();
560
561 // Collect info for variables that were optimized out.
562 collectDeadVariables();
563
564 // Handle anything that needs to be done on a per-unit basis after
565 // all other generation.
566 for (const auto &P : CUMap) {
567 auto &TheCU = *P.second;
568 // Emit DW_AT_containing_type attribute to connect types with their
569 // vtable holding type.
570 TheCU.constructContainingTypeDIEs();
571
572 // Add CU specific attributes if we need to add any.
573 // If we're splitting the dwarf out now that we've got the entire
574 // CU then add the dwo id to it.
575 auto *SkCU = TheCU.getSkeleton();
576 if (useSplitDwarf()) {
577 // Emit a unique identifier for this CU.
578 uint64_t ID = DIEHash(Asm).computeCUSignature(TheCU.getUnitDie());
579 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
580 dwarf::DW_FORM_data8, ID);
581 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
582 dwarf::DW_FORM_data8, ID);
583
584 // We don't keep track of which addresses are used in which CU so this
585 // is a bit pessimistic under LTO.
586 if (!AddrPool.isEmpty()) {
587 const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol();
588 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
589 Sym, Sym);
590 }
591 if (!SkCU->getRangeLists().empty()) {
592 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
593 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
594 Sym, Sym);
595 }
596 }
597
598 // If we have code split among multiple sections or non-contiguous
599 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
600 // remain in the .o file, otherwise add a DW_AT_low_pc.
601 // FIXME: We should use ranges allow reordering of code ala
602 // .subsections_via_symbols in mach-o. This would mean turning on
603 // ranges for all subprogram DIEs for mach-o.
604 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
605 if (unsigned NumRanges = TheCU.getRanges().size()) {
606 if (NumRanges > 1)
607 // A DW_AT_low_pc attribute may also be specified in combination with
608 // DW_AT_ranges to specify the default base address for use in
609 // location lists (see Section 2.6.2) and range lists (see Section
610 // 2.17.3).
611 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
612 else
613 U.setBaseAddress(TheCU.getRanges().front().getStart());
614 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
615 }
616 }
617
618 // Compute DIE offsets and sizes.
619 InfoHolder.computeSizeAndOffsets();
620 if (useSplitDwarf())
621 SkeletonHolder.computeSizeAndOffsets();
622 }
623
624 // Emit all Dwarf sections that should come after the content.
endModule()625 void DwarfDebug::endModule() {
626 assert(CurFn == nullptr);
627 assert(CurMI == nullptr);
628
629 // If we aren't actually generating debug info (check beginModule -
630 // conditionalized on !DisableDebugInfoPrinting and the presence of the
631 // llvm.dbg.cu metadata node)
632 if (!MMI->hasDebugInfo())
633 return;
634
635 // Finalize the debug info for the module.
636 finalizeModuleInfo();
637
638 emitDebugStr();
639
640 if (useSplitDwarf())
641 emitDebugLocDWO();
642 else
643 // Emit info into a debug loc section.
644 emitDebugLoc();
645
646 // Corresponding abbreviations into a abbrev section.
647 emitAbbreviations();
648
649 // Emit all the DIEs into a debug info section.
650 emitDebugInfo();
651
652 // Emit info into a debug aranges section.
653 if (GenerateARangeSection)
654 emitDebugARanges();
655
656 // Emit info into a debug ranges section.
657 emitDebugRanges();
658
659 if (useSplitDwarf()) {
660 emitDebugStrDWO();
661 emitDebugInfoDWO();
662 emitDebugAbbrevDWO();
663 emitDebugLineDWO();
664 // Emit DWO addresses.
665 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
666 }
667
668 // Emit info into the dwarf accelerator table sections.
669 if (useDwarfAccelTables()) {
670 emitAccelNames();
671 emitAccelObjC();
672 emitAccelNamespaces();
673 emitAccelTypes();
674 }
675
676 // Emit the pubnames and pubtypes sections if requested.
677 if (HasDwarfPubSections) {
678 emitDebugPubNames(GenerateGnuPubSections);
679 emitDebugPubTypes(GenerateGnuPubSections);
680 }
681
682 // clean up.
683 SPMap.clear();
684 AbstractVariables.clear();
685 }
686
687 // Find abstract variable, if any, associated with Var.
688 DbgVariable *
getExistingAbstractVariable(InlinedVariable IV,const DILocalVariable * & Cleansed)689 DwarfDebug::getExistingAbstractVariable(InlinedVariable IV,
690 const DILocalVariable *&Cleansed) {
691 // More then one inlined variable corresponds to one abstract variable.
692 Cleansed = IV.first;
693 auto I = AbstractVariables.find(Cleansed);
694 if (I != AbstractVariables.end())
695 return I->second.get();
696 return nullptr;
697 }
698
getExistingAbstractVariable(InlinedVariable IV)699 DbgVariable *DwarfDebug::getExistingAbstractVariable(InlinedVariable IV) {
700 const DILocalVariable *Cleansed;
701 return getExistingAbstractVariable(IV, Cleansed);
702 }
703
createAbstractVariable(const DILocalVariable * Var,LexicalScope * Scope)704 void DwarfDebug::createAbstractVariable(const DILocalVariable *Var,
705 LexicalScope *Scope) {
706 auto AbsDbgVariable = make_unique<DbgVariable>(Var, /* IA */ nullptr, this);
707 InfoHolder.addScopeVariable(Scope, AbsDbgVariable.get());
708 AbstractVariables[Var] = std::move(AbsDbgVariable);
709 }
710
ensureAbstractVariableIsCreated(InlinedVariable IV,const MDNode * ScopeNode)711 void DwarfDebug::ensureAbstractVariableIsCreated(InlinedVariable IV,
712 const MDNode *ScopeNode) {
713 const DILocalVariable *Cleansed = nullptr;
714 if (getExistingAbstractVariable(IV, Cleansed))
715 return;
716
717 createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(
718 cast<DILocalScope>(ScopeNode)));
719 }
720
ensureAbstractVariableIsCreatedIfScoped(InlinedVariable IV,const MDNode * ScopeNode)721 void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(
722 InlinedVariable IV, const MDNode *ScopeNode) {
723 const DILocalVariable *Cleansed = nullptr;
724 if (getExistingAbstractVariable(IV, Cleansed))
725 return;
726
727 if (LexicalScope *Scope =
728 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
729 createAbstractVariable(Cleansed, Scope);
730 }
731
732 // Collect variable information from side table maintained by MMI.
collectVariableInfoFromMMITable(DenseSet<InlinedVariable> & Processed)733 void DwarfDebug::collectVariableInfoFromMMITable(
734 DenseSet<InlinedVariable> &Processed) {
735 for (const auto &VI : MMI->getVariableDbgInfo()) {
736 if (!VI.Var)
737 continue;
738 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
739 "Expected inlined-at fields to agree");
740
741 InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt());
742 Processed.insert(Var);
743 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
744
745 // If variable scope is not found then skip this variable.
746 if (!Scope)
747 continue;
748
749 ensureAbstractVariableIsCreatedIfScoped(Var, Scope->getScopeNode());
750 auto RegVar = make_unique<DbgVariable>(Var.first, Var.second, this);
751 RegVar->initializeMMI(VI.Expr, VI.Slot);
752 if (InfoHolder.addScopeVariable(Scope, RegVar.get()))
753 ConcreteVariables.push_back(std::move(RegVar));
754 }
755 }
756
757 // Get .debug_loc entry for the instruction range starting at MI.
getDebugLocValue(const MachineInstr * MI)758 static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
759 const DIExpression *Expr = MI->getDebugExpression();
760
761 assert(MI->getNumOperands() == 4);
762 if (MI->getOperand(0).isReg()) {
763 MachineLocation MLoc;
764 // If the second operand is an immediate, this is a
765 // register-indirect address.
766 if (!MI->getOperand(1).isImm())
767 MLoc.set(MI->getOperand(0).getReg());
768 else
769 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
770 return DebugLocEntry::Value(Expr, MLoc);
771 }
772 if (MI->getOperand(0).isImm())
773 return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
774 if (MI->getOperand(0).isFPImm())
775 return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
776 if (MI->getOperand(0).isCImm())
777 return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
778
779 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
780 }
781
782 /// Determine whether two variable pieces overlap.
piecesOverlap(const DIExpression * P1,const DIExpression * P2)783 static bool piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
784 if (!P1->isBitPiece() || !P2->isBitPiece())
785 return true;
786 unsigned l1 = P1->getBitPieceOffset();
787 unsigned l2 = P2->getBitPieceOffset();
788 unsigned r1 = l1 + P1->getBitPieceSize();
789 unsigned r2 = l2 + P2->getBitPieceSize();
790 // True where [l1,r1[ and [r1,r2[ overlap.
791 return (l1 < r2) && (l2 < r1);
792 }
793
794 /// Build the location list for all DBG_VALUEs in the function that
795 /// describe the same variable. If the ranges of several independent
796 /// pieces of the same variable overlap partially, split them up and
797 /// combine the ranges. The resulting DebugLocEntries are will have
798 /// strict monotonically increasing begin addresses and will never
799 /// overlap.
800 //
801 // Input:
802 //
803 // Ranges History [var, loc, piece ofs size]
804 // 0 | [x, (reg0, piece 0, 32)]
805 // 1 | | [x, (reg1, piece 32, 32)] <- IsPieceOfPrevEntry
806 // 2 | | ...
807 // 3 | [clobber reg0]
808 // 4 [x, (mem, piece 0, 64)] <- overlapping with both previous pieces of
809 // x.
810 //
811 // Output:
812 //
813 // [0-1] [x, (reg0, piece 0, 32)]
814 // [1-3] [x, (reg0, piece 0, 32), (reg1, piece 32, 32)]
815 // [3-4] [x, (reg1, piece 32, 32)]
816 // [4- ] [x, (mem, piece 0, 64)]
817 void
buildLocationList(SmallVectorImpl<DebugLocEntry> & DebugLoc,const DbgValueHistoryMap::InstrRanges & Ranges)818 DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
819 const DbgValueHistoryMap::InstrRanges &Ranges) {
820 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
821
822 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
823 const MachineInstr *Begin = I->first;
824 const MachineInstr *End = I->second;
825 assert(Begin->isDebugValue() && "Invalid History entry");
826
827 // Check if a variable is inaccessible in this range.
828 if (Begin->getNumOperands() > 1 &&
829 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
830 OpenRanges.clear();
831 continue;
832 }
833
834 // If this piece overlaps with any open ranges, truncate them.
835 const DIExpression *DIExpr = Begin->getDebugExpression();
836 auto Last = std::remove_if(OpenRanges.begin(), OpenRanges.end(),
837 [&](DebugLocEntry::Value R) {
838 return piecesOverlap(DIExpr, R.getExpression());
839 });
840 OpenRanges.erase(Last, OpenRanges.end());
841
842 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
843 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
844
845 const MCSymbol *EndLabel;
846 if (End != nullptr)
847 EndLabel = getLabelAfterInsn(End);
848 else if (std::next(I) == Ranges.end())
849 EndLabel = Asm->getFunctionEnd();
850 else
851 EndLabel = getLabelBeforeInsn(std::next(I)->first);
852 assert(EndLabel && "Forgot label after instruction ending a range!");
853
854 DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
855
856 auto Value = getDebugLocValue(Begin);
857 DebugLocEntry Loc(StartLabel, EndLabel, Value);
858 bool couldMerge = false;
859
860 // If this is a piece, it may belong to the current DebugLocEntry.
861 if (DIExpr->isBitPiece()) {
862 // Add this value to the list of open ranges.
863 OpenRanges.push_back(Value);
864
865 // Attempt to add the piece to the last entry.
866 if (!DebugLoc.empty())
867 if (DebugLoc.back().MergeValues(Loc))
868 couldMerge = true;
869 }
870
871 if (!couldMerge) {
872 // Need to add a new DebugLocEntry. Add all values from still
873 // valid non-overlapping pieces.
874 if (OpenRanges.size())
875 Loc.addValues(OpenRanges);
876
877 DebugLoc.push_back(std::move(Loc));
878 }
879
880 // Attempt to coalesce the ranges of two otherwise identical
881 // DebugLocEntries.
882 auto CurEntry = DebugLoc.rbegin();
883 DEBUG({
884 dbgs() << CurEntry->getValues().size() << " Values:\n";
885 for (auto &Value : CurEntry->getValues())
886 Value.getExpression()->dump();
887 dbgs() << "-----\n";
888 });
889
890 auto PrevEntry = std::next(CurEntry);
891 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
892 DebugLoc.pop_back();
893 }
894 }
895
createConcreteVariable(LexicalScope & Scope,InlinedVariable IV)896 DbgVariable *DwarfDebug::createConcreteVariable(LexicalScope &Scope,
897 InlinedVariable IV) {
898 ensureAbstractVariableIsCreatedIfScoped(IV, Scope.getScopeNode());
899 ConcreteVariables.push_back(
900 make_unique<DbgVariable>(IV.first, IV.second, this));
901 InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get());
902 return ConcreteVariables.back().get();
903 }
904
905 // Find variables for each lexical scope.
collectVariableInfo(DwarfCompileUnit & TheCU,const DISubprogram * SP,DenseSet<InlinedVariable> & Processed)906 void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
907 const DISubprogram *SP,
908 DenseSet<InlinedVariable> &Processed) {
909 // Grab the variable info that was squirreled away in the MMI side-table.
910 collectVariableInfoFromMMITable(Processed);
911
912 for (const auto &I : DbgValues) {
913 InlinedVariable IV = I.first;
914 if (Processed.count(IV))
915 continue;
916
917 // Instruction ranges, specifying where IV is accessible.
918 const auto &Ranges = I.second;
919 if (Ranges.empty())
920 continue;
921
922 LexicalScope *Scope = nullptr;
923 if (const DILocation *IA = IV.second)
924 Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
925 else
926 Scope = LScopes.findLexicalScope(IV.first->getScope());
927 // If variable scope is not found then skip this variable.
928 if (!Scope)
929 continue;
930
931 Processed.insert(IV);
932 DbgVariable *RegVar = createConcreteVariable(*Scope, IV);
933
934 const MachineInstr *MInsn = Ranges.front().first;
935 assert(MInsn->isDebugValue() && "History must begin with debug value");
936
937 // Check if the first DBG_VALUE is valid for the rest of the function.
938 if (Ranges.size() == 1 && Ranges.front().second == nullptr) {
939 RegVar->initializeDbgValue(MInsn);
940 continue;
941 }
942
943 // Handle multiple DBG_VALUE instructions describing one variable.
944 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
945
946 // Build the location list for this variable.
947 SmallVector<DebugLocEntry, 8> Entries;
948 buildLocationList(Entries, Ranges);
949
950 // If the variable has an DIBasicType, extract it. Basic types cannot have
951 // unique identifiers, so don't bother resolving the type with the
952 // identifier map.
953 const DIBasicType *BT = dyn_cast<DIBasicType>(
954 static_cast<const Metadata *>(IV.first->getType()));
955
956 // Finalize the entry by lowering it into a DWARF bytestream.
957 for (auto &Entry : Entries)
958 Entry.finalize(*Asm, List, BT);
959 }
960
961 // Collect info for variables that were optimized out.
962 for (const DILocalVariable *DV : SP->getVariables()) {
963 if (Processed.insert(InlinedVariable(DV, nullptr)).second)
964 if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
965 createConcreteVariable(*Scope, InlinedVariable(DV, nullptr));
966 }
967 }
968
969 // Return Label preceding the instruction.
getLabelBeforeInsn(const MachineInstr * MI)970 MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
971 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
972 assert(Label && "Didn't insert label before instruction");
973 return Label;
974 }
975
976 // Return Label immediately following the instruction.
getLabelAfterInsn(const MachineInstr * MI)977 MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
978 return LabelsAfterInsn.lookup(MI);
979 }
980
981 // Process beginning of an instruction.
beginInstruction(const MachineInstr * MI)982 void DwarfDebug::beginInstruction(const MachineInstr *MI) {
983 assert(CurMI == nullptr);
984 CurMI = MI;
985 // Check if source location changes, but ignore DBG_VALUE locations.
986 if (!MI->isDebugValue()) {
987 DebugLoc DL = MI->getDebugLoc();
988 if (DL != PrevInstLoc) {
989 if (DL) {
990 unsigned Flags = 0;
991 PrevInstLoc = DL;
992 if (DL == PrologEndLoc) {
993 Flags |= DWARF2_FLAG_PROLOGUE_END;
994 PrologEndLoc = DebugLoc();
995 Flags |= DWARF2_FLAG_IS_STMT;
996 }
997 if (DL.getLine() !=
998 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine())
999 Flags |= DWARF2_FLAG_IS_STMT;
1000
1001 const MDNode *Scope = DL.getScope();
1002 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
1003 } else if (UnknownLocations) {
1004 PrevInstLoc = DL;
1005 recordSourceLine(0, 0, nullptr, 0);
1006 }
1007 }
1008 }
1009
1010 // Insert labels where requested.
1011 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1012 LabelsBeforeInsn.find(MI);
1013
1014 // No label needed.
1015 if (I == LabelsBeforeInsn.end())
1016 return;
1017
1018 // Label already assigned.
1019 if (I->second)
1020 return;
1021
1022 if (!PrevLabel) {
1023 PrevLabel = MMI->getContext().createTempSymbol();
1024 Asm->OutStreamer->EmitLabel(PrevLabel);
1025 }
1026 I->second = PrevLabel;
1027 }
1028
1029 // Process end of an instruction.
endInstruction()1030 void DwarfDebug::endInstruction() {
1031 assert(CurMI != nullptr);
1032 // Don't create a new label after DBG_VALUE instructions.
1033 // They don't generate code.
1034 if (!CurMI->isDebugValue())
1035 PrevLabel = nullptr;
1036
1037 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1038 LabelsAfterInsn.find(CurMI);
1039 CurMI = nullptr;
1040
1041 // No label needed.
1042 if (I == LabelsAfterInsn.end())
1043 return;
1044
1045 // Label already assigned.
1046 if (I->second)
1047 return;
1048
1049 // We need a label after this instruction.
1050 if (!PrevLabel) {
1051 PrevLabel = MMI->getContext().createTempSymbol();
1052 Asm->OutStreamer->EmitLabel(PrevLabel);
1053 }
1054 I->second = PrevLabel;
1055 }
1056
1057 // Each LexicalScope has first instruction and last instruction to mark
1058 // beginning and end of a scope respectively. Create an inverse map that list
1059 // scopes starts (and ends) with an instruction. One instruction may start (or
1060 // end) multiple scopes. Ignore scopes that are not reachable.
identifyScopeMarkers()1061 void DwarfDebug::identifyScopeMarkers() {
1062 SmallVector<LexicalScope *, 4> WorkList;
1063 WorkList.push_back(LScopes.getCurrentFunctionScope());
1064 while (!WorkList.empty()) {
1065 LexicalScope *S = WorkList.pop_back_val();
1066
1067 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
1068 if (!Children.empty())
1069 WorkList.append(Children.begin(), Children.end());
1070
1071 if (S->isAbstractScope())
1072 continue;
1073
1074 for (const InsnRange &R : S->getRanges()) {
1075 assert(R.first && "InsnRange does not have first instruction!");
1076 assert(R.second && "InsnRange does not have second instruction!");
1077 requestLabelBeforeInsn(R.first);
1078 requestLabelAfterInsn(R.second);
1079 }
1080 }
1081 }
1082
findPrologueEndLoc(const MachineFunction * MF)1083 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1084 // First known non-DBG_VALUE and non-frame setup location marks
1085 // the beginning of the function body.
1086 for (const auto &MBB : *MF)
1087 for (const auto &MI : MBB)
1088 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
1089 MI.getDebugLoc())
1090 return MI.getDebugLoc();
1091 return DebugLoc();
1092 }
1093
1094 // Gather pre-function debug information. Assumes being called immediately
1095 // after the function entry point has been emitted.
beginFunction(const MachineFunction * MF)1096 void DwarfDebug::beginFunction(const MachineFunction *MF) {
1097 CurFn = MF;
1098
1099 // If there's no debug info for the function we're not going to do anything.
1100 if (!MMI->hasDebugInfo())
1101 return;
1102
1103 auto DI = MF->getFunction()->getSubprogram();
1104 if (!DI)
1105 return;
1106
1107 // Grab the lexical scopes for the function, if we don't have any of those
1108 // then we're not going to be able to do anything.
1109 LScopes.initialize(*MF);
1110 if (LScopes.empty())
1111 return;
1112
1113 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
1114
1115 // Make sure that each lexical scope will have a begin/end label.
1116 identifyScopeMarkers();
1117
1118 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1119 // belongs to so that we add to the correct per-cu line table in the
1120 // non-asm case.
1121 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1122 // FnScope->getScopeNode() and DI->second should represent the same function,
1123 // though they may not be the same MDNode due to inline functions merged in
1124 // LTO where the debug info metadata still differs (either due to distinct
1125 // written differences - two versions of a linkonce_odr function
1126 // written/copied into two separate files, or some sub-optimal metadata that
1127 // isn't structurally identical (see: file path/name info from clang, which
1128 // includes the directory of the cpp file being built, even when the file name
1129 // is absolute (such as an <> lookup header)))
1130 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1131 assert(TheCU && "Unable to find compile unit!");
1132 if (Asm->OutStreamer->hasRawTextSupport())
1133 // Use a single line table if we are generating assembly.
1134 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1135 else
1136 Asm->OutStreamer->getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
1137
1138 // Calculate history for local variables.
1139 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
1140 DbgValues);
1141
1142 // Request labels for the full history.
1143 for (const auto &I : DbgValues) {
1144 const auto &Ranges = I.second;
1145 if (Ranges.empty())
1146 continue;
1147
1148 // The first mention of a function argument gets the CurrentFnBegin
1149 // label, so arguments are visible when breaking at function entry.
1150 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
1151 if (DIVar->isParameter() &&
1152 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
1153 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
1154 if (Ranges.front().first->getDebugExpression()->isBitPiece()) {
1155 // Mark all non-overlapping initial pieces.
1156 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
1157 const DIExpression *Piece = I->first->getDebugExpression();
1158 if (std::all_of(Ranges.begin(), I,
1159 [&](DbgValueHistoryMap::InstrRange Pred) {
1160 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
1161 }))
1162 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
1163 else
1164 break;
1165 }
1166 }
1167 }
1168
1169 for (const auto &Range : Ranges) {
1170 requestLabelBeforeInsn(Range.first);
1171 if (Range.second)
1172 requestLabelAfterInsn(Range.second);
1173 }
1174 }
1175
1176 PrevInstLoc = DebugLoc();
1177 PrevLabel = Asm->getFunctionBegin();
1178
1179 // Record beginning of function.
1180 PrologEndLoc = findPrologueEndLoc(MF);
1181 if (DILocation *L = PrologEndLoc) {
1182 // We'd like to list the prologue as "not statements" but GDB behaves
1183 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1184 auto *SP = L->getInlinedAtScope()->getSubprogram();
1185 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
1186 }
1187 }
1188
1189 // Gather and emit post-function debug information.
endFunction(const MachineFunction * MF)1190 void DwarfDebug::endFunction(const MachineFunction *MF) {
1191 assert(CurFn == MF &&
1192 "endFunction should be called with the same function as beginFunction");
1193
1194 if (!MMI->hasDebugInfo() || LScopes.empty() ||
1195 !MF->getFunction()->getSubprogram()) {
1196 // If we don't have a lexical scope for this function then there will
1197 // be a hole in the range information. Keep note of this by setting the
1198 // previously used section to nullptr.
1199 PrevCU = nullptr;
1200 CurFn = nullptr;
1201 return;
1202 }
1203
1204 // Set DwarfDwarfCompileUnitID in MCContext to default value.
1205 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1206
1207 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1208 auto *SP = cast<DISubprogram>(FnScope->getScopeNode());
1209 DwarfCompileUnit &TheCU = *SPMap.lookup(SP);
1210
1211 DenseSet<InlinedVariable> ProcessedVars;
1212 collectVariableInfo(TheCU, SP, ProcessedVars);
1213
1214 // Add the range of this function to the list of ranges for the CU.
1215 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
1216
1217 // Under -gmlt, skip building the subprogram if there are no inlined
1218 // subroutines inside it.
1219 if (TheCU.getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly &&
1220 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1221 assert(InfoHolder.getScopeVariables().empty());
1222 assert(DbgValues.empty());
1223 // FIXME: This wouldn't be true in LTO with a -g (with inlining) CU followed
1224 // by a -gmlt CU. Add a test and remove this assertion.
1225 assert(AbstractVariables.empty());
1226 LabelsBeforeInsn.clear();
1227 LabelsAfterInsn.clear();
1228 PrevLabel = nullptr;
1229 CurFn = nullptr;
1230 return;
1231 }
1232
1233 #ifndef NDEBUG
1234 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1235 #endif
1236 // Construct abstract scopes.
1237 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
1238 auto *SP = cast<DISubprogram>(AScope->getScopeNode());
1239 // Collect info for variables that were optimized out.
1240 for (const DILocalVariable *DV : SP->getVariables()) {
1241 if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
1242 continue;
1243 ensureAbstractVariableIsCreated(InlinedVariable(DV, nullptr),
1244 DV->getScope());
1245 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1246 && "ensureAbstractVariableIsCreated inserted abstract scopes");
1247 }
1248 constructAbstractSubprogramScopeDIE(AScope);
1249 }
1250
1251 TheCU.constructSubprogramScopeDIE(FnScope);
1252 if (auto *SkelCU = TheCU.getSkeleton())
1253 if (!LScopes.getAbstractScopesList().empty())
1254 SkelCU->constructSubprogramScopeDIE(FnScope);
1255
1256 // Clear debug info
1257 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1258 // DbgVariables except those that are also in AbstractVariables (since they
1259 // can be used cross-function)
1260 InfoHolder.getScopeVariables().clear();
1261 DbgValues.clear();
1262 LabelsBeforeInsn.clear();
1263 LabelsAfterInsn.clear();
1264 PrevLabel = nullptr;
1265 CurFn = nullptr;
1266 }
1267
1268 // Register a source line with debug info. Returns the unique label that was
1269 // emitted and which provides correspondence to the source line list.
recordSourceLine(unsigned Line,unsigned Col,const MDNode * S,unsigned Flags)1270 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1271 unsigned Flags) {
1272 StringRef Fn;
1273 StringRef Dir;
1274 unsigned Src = 1;
1275 unsigned Discriminator = 0;
1276 if (auto *Scope = cast_or_null<DIScope>(S)) {
1277 Fn = Scope->getFilename();
1278 Dir = Scope->getDirectory();
1279 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1280 Discriminator = LBF->getDiscriminator();
1281
1282 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
1283 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1284 .getOrCreateSourceID(Fn, Dir);
1285 }
1286 Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1287 Discriminator, Fn);
1288 }
1289
1290 //===----------------------------------------------------------------------===//
1291 // Emit Methods
1292 //===----------------------------------------------------------------------===//
1293
1294 // Emit the debug info section.
emitDebugInfo()1295 void DwarfDebug::emitDebugInfo() {
1296 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1297 Holder.emitUnits(/* UseOffsets */ false);
1298 }
1299
1300 // Emit the abbreviation section.
emitAbbreviations()1301 void DwarfDebug::emitAbbreviations() {
1302 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1303
1304 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1305 }
1306
emitAccel(DwarfAccelTable & Accel,MCSection * Section,StringRef TableName)1307 void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section,
1308 StringRef TableName) {
1309 Accel.FinalizeTable(Asm, TableName);
1310 Asm->OutStreamer->SwitchSection(Section);
1311
1312 // Emit the full data.
1313 Accel.emit(Asm, Section->getBeginSymbol(), this);
1314 }
1315
1316 // Emit visible names into a hashed accelerator table section.
emitAccelNames()1317 void DwarfDebug::emitAccelNames() {
1318 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1319 "Names");
1320 }
1321
1322 // Emit objective C classes and categories into a hashed accelerator table
1323 // section.
emitAccelObjC()1324 void DwarfDebug::emitAccelObjC() {
1325 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1326 "ObjC");
1327 }
1328
1329 // Emit namespace dies into a hashed accelerator table.
emitAccelNamespaces()1330 void DwarfDebug::emitAccelNamespaces() {
1331 emitAccel(AccelNamespace,
1332 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
1333 "namespac");
1334 }
1335
1336 // Emit type dies into a hashed accelerator table.
emitAccelTypes()1337 void DwarfDebug::emitAccelTypes() {
1338 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1339 "types");
1340 }
1341
1342 // Public name handling.
1343 // The format for the various pubnames:
1344 //
1345 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1346 // for the DIE that is named.
1347 //
1348 // gnu pubnames - offset/index value/name tuples where the offset is the offset
1349 // into the CU and the index value is computed according to the type of value
1350 // for the DIE that is named.
1351 //
1352 // For type units the offset is the offset of the skeleton DIE. For split dwarf
1353 // it's the offset within the debug_info/debug_types dwo section, however, the
1354 // reference in the pubname header doesn't change.
1355
1356 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
computeIndexValue(DwarfUnit * CU,const DIE * Die)1357 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
1358 const DIE *Die) {
1359 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1360
1361 // We could have a specification DIE that has our most of our knowledge,
1362 // look for that now.
1363 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1364 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
1365 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
1366 Linkage = dwarf::GIEL_EXTERNAL;
1367 } else if (Die->findAttribute(dwarf::DW_AT_external))
1368 Linkage = dwarf::GIEL_EXTERNAL;
1369
1370 switch (Die->getTag()) {
1371 case dwarf::DW_TAG_class_type:
1372 case dwarf::DW_TAG_structure_type:
1373 case dwarf::DW_TAG_union_type:
1374 case dwarf::DW_TAG_enumeration_type:
1375 return dwarf::PubIndexEntryDescriptor(
1376 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1377 ? dwarf::GIEL_STATIC
1378 : dwarf::GIEL_EXTERNAL);
1379 case dwarf::DW_TAG_typedef:
1380 case dwarf::DW_TAG_base_type:
1381 case dwarf::DW_TAG_subrange_type:
1382 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
1383 case dwarf::DW_TAG_namespace:
1384 return dwarf::GIEK_TYPE;
1385 case dwarf::DW_TAG_subprogram:
1386 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
1387 case dwarf::DW_TAG_variable:
1388 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
1389 case dwarf::DW_TAG_enumerator:
1390 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1391 dwarf::GIEL_STATIC);
1392 default:
1393 return dwarf::GIEK_NONE;
1394 }
1395 }
1396
1397 /// emitDebugPubNames - Emit visible names into a debug pubnames section.
1398 ///
emitDebugPubNames(bool GnuStyle)1399 void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
1400 MCSection *PSec = GnuStyle
1401 ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1402 : Asm->getObjFileLowering().getDwarfPubNamesSection();
1403
1404 emitDebugPubSection(GnuStyle, PSec, "Names",
1405 &DwarfCompileUnit::getGlobalNames);
1406 }
1407
emitDebugPubSection(bool GnuStyle,MCSection * PSec,StringRef Name,const StringMap<const DIE * > & (DwarfCompileUnit::* Accessor)()const)1408 void DwarfDebug::emitDebugPubSection(
1409 bool GnuStyle, MCSection *PSec, StringRef Name,
1410 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) {
1411 for (const auto &NU : CUMap) {
1412 DwarfCompileUnit *TheU = NU.second;
1413
1414 const auto &Globals = (TheU->*Accessor)();
1415
1416 if (Globals.empty())
1417 continue;
1418
1419 if (auto *Skeleton = TheU->getSkeleton())
1420 TheU = Skeleton;
1421
1422 // Start the dwarf pubnames section.
1423 Asm->OutStreamer->SwitchSection(PSec);
1424
1425 // Emit the header.
1426 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1427 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1428 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1429 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1430
1431 Asm->OutStreamer->EmitLabel(BeginLabel);
1432
1433 Asm->OutStreamer->AddComment("DWARF Version");
1434 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
1435
1436 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
1437 Asm->emitDwarfSymbolReference(TheU->getLabelBegin());
1438
1439 Asm->OutStreamer->AddComment("Compilation Unit Length");
1440 Asm->EmitInt32(TheU->getLength());
1441
1442 // Emit the pubnames for this compilation unit.
1443 for (const auto &GI : Globals) {
1444 const char *Name = GI.getKeyData();
1445 const DIE *Entity = GI.second;
1446
1447 Asm->OutStreamer->AddComment("DIE offset");
1448 Asm->EmitInt32(Entity->getOffset());
1449
1450 if (GnuStyle) {
1451 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
1452 Asm->OutStreamer->AddComment(
1453 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1454 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
1455 Asm->EmitInt8(Desc.toBits());
1456 }
1457
1458 Asm->OutStreamer->AddComment("External Name");
1459 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1460 }
1461
1462 Asm->OutStreamer->AddComment("End Mark");
1463 Asm->EmitInt32(0);
1464 Asm->OutStreamer->EmitLabel(EndLabel);
1465 }
1466 }
1467
emitDebugPubTypes(bool GnuStyle)1468 void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
1469 MCSection *PSec = GnuStyle
1470 ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1471 : Asm->getObjFileLowering().getDwarfPubTypesSection();
1472
1473 emitDebugPubSection(GnuStyle, PSec, "Types",
1474 &DwarfCompileUnit::getGlobalTypes);
1475 }
1476
1477 // Emit visible names into a debug str section.
emitDebugStr()1478 void DwarfDebug::emitDebugStr() {
1479 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1480 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1481 }
1482
emitDebugLocEntry(ByteStreamer & Streamer,const DebugLocStream::Entry & Entry)1483 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
1484 const DebugLocStream::Entry &Entry) {
1485 auto &&Comments = DebugLocs.getComments(Entry);
1486 auto Comment = Comments.begin();
1487 auto End = Comments.end();
1488 for (uint8_t Byte : DebugLocs.getBytes(Entry))
1489 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
1490 }
1491
emitDebugLocValue(const AsmPrinter & AP,const DIBasicType * BT,ByteStreamer & Streamer,const DebugLocEntry::Value & Value,unsigned PieceOffsetInBits)1492 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
1493 ByteStreamer &Streamer,
1494 const DebugLocEntry::Value &Value,
1495 unsigned PieceOffsetInBits) {
1496 DebugLocDwarfExpression DwarfExpr(*AP.MF->getSubtarget().getRegisterInfo(),
1497 AP.getDwarfDebug()->getDwarfVersion(),
1498 Streamer);
1499 // Regular entry.
1500 if (Value.isInt()) {
1501 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1502 BT->getEncoding() == dwarf::DW_ATE_signed_char))
1503 DwarfExpr.AddSignedConstant(Value.getInt());
1504 else
1505 DwarfExpr.AddUnsignedConstant(Value.getInt());
1506 } else if (Value.isLocation()) {
1507 MachineLocation Loc = Value.getLoc();
1508 const DIExpression *Expr = Value.getExpression();
1509 if (!Expr || !Expr->getNumElements())
1510 // Regular entry.
1511 AP.EmitDwarfRegOp(Streamer, Loc);
1512 else {
1513 // Complex address entry.
1514 if (Loc.getOffset()) {
1515 DwarfExpr.AddMachineRegIndirect(Loc.getReg(), Loc.getOffset());
1516 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end(),
1517 PieceOffsetInBits);
1518 } else
1519 DwarfExpr.AddMachineRegExpression(Expr, Loc.getReg(),
1520 PieceOffsetInBits);
1521 }
1522 }
1523 // else ... ignore constant fp. There is not any good way to
1524 // to represent them here in dwarf.
1525 // FIXME: ^
1526 }
1527
finalize(const AsmPrinter & AP,DebugLocStream::ListBuilder & List,const DIBasicType * BT)1528 void DebugLocEntry::finalize(const AsmPrinter &AP,
1529 DebugLocStream::ListBuilder &List,
1530 const DIBasicType *BT) {
1531 DebugLocStream::EntryBuilder Entry(List, Begin, End);
1532 BufferByteStreamer Streamer = Entry.getStreamer();
1533 const DebugLocEntry::Value &Value = Values[0];
1534 if (Value.isBitPiece()) {
1535 // Emit all pieces that belong to the same variable and range.
1536 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value P) {
1537 return P.isBitPiece();
1538 }) && "all values are expected to be pieces");
1539 assert(std::is_sorted(Values.begin(), Values.end()) &&
1540 "pieces are expected to be sorted");
1541
1542 unsigned Offset = 0;
1543 for (auto Piece : Values) {
1544 const DIExpression *Expr = Piece.getExpression();
1545 unsigned PieceOffset = Expr->getBitPieceOffset();
1546 unsigned PieceSize = Expr->getBitPieceSize();
1547 assert(Offset <= PieceOffset && "overlapping or duplicate pieces");
1548 if (Offset < PieceOffset) {
1549 // The DWARF spec seriously mandates pieces with no locations for gaps.
1550 DebugLocDwarfExpression Expr(*AP.MF->getSubtarget().getRegisterInfo(),
1551 AP.getDwarfDebug()->getDwarfVersion(),
1552 Streamer);
1553 Expr.AddOpPiece(PieceOffset-Offset, 0);
1554 Offset += PieceOffset-Offset;
1555 }
1556 Offset += PieceSize;
1557
1558 emitDebugLocValue(AP, BT, Streamer, Piece, PieceOffset);
1559 }
1560 } else {
1561 assert(Values.size() == 1 && "only pieces may have >1 value");
1562 emitDebugLocValue(AP, BT, Streamer, Value, 0);
1563 }
1564 }
1565
emitDebugLocEntryLocation(const DebugLocStream::Entry & Entry)1566 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
1567 // Emit the size.
1568 Asm->OutStreamer->AddComment("Loc expr size");
1569 Asm->EmitInt16(DebugLocs.getBytes(Entry).size());
1570
1571 // Emit the entry.
1572 APByteStreamer Streamer(*Asm);
1573 emitDebugLocEntry(Streamer, Entry);
1574 }
1575
1576 // Emit locations into the debug loc section.
emitDebugLoc()1577 void DwarfDebug::emitDebugLoc() {
1578 // Start the dwarf loc section.
1579 Asm->OutStreamer->SwitchSection(
1580 Asm->getObjFileLowering().getDwarfLocSection());
1581 unsigned char Size = Asm->getDataLayout().getPointerSize();
1582 for (const auto &List : DebugLocs.getLists()) {
1583 Asm->OutStreamer->EmitLabel(List.Label);
1584 const DwarfCompileUnit *CU = List.CU;
1585 for (const auto &Entry : DebugLocs.getEntries(List)) {
1586 // Set up the range. This range is relative to the entry point of the
1587 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1588 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
1589 if (auto *Base = CU->getBaseAddress()) {
1590 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1591 Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
1592 } else {
1593 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1594 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
1595 }
1596
1597 emitDebugLocEntryLocation(Entry);
1598 }
1599 Asm->OutStreamer->EmitIntValue(0, Size);
1600 Asm->OutStreamer->EmitIntValue(0, Size);
1601 }
1602 }
1603
emitDebugLocDWO()1604 void DwarfDebug::emitDebugLocDWO() {
1605 Asm->OutStreamer->SwitchSection(
1606 Asm->getObjFileLowering().getDwarfLocDWOSection());
1607 for (const auto &List : DebugLocs.getLists()) {
1608 Asm->OutStreamer->EmitLabel(List.Label);
1609 for (const auto &Entry : DebugLocs.getEntries(List)) {
1610 // Just always use start_length for now - at least that's one address
1611 // rather than two. We could get fancier and try to, say, reuse an
1612 // address we know we've emitted elsewhere (the start of the function?
1613 // The start of the CU or CU subrange that encloses this range?)
1614 Asm->EmitInt8(dwarf::DW_LLE_start_length_entry);
1615 unsigned idx = AddrPool.getIndex(Entry.BeginSym);
1616 Asm->EmitULEB128(idx);
1617 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
1618
1619 emitDebugLocEntryLocation(Entry);
1620 }
1621 Asm->EmitInt8(dwarf::DW_LLE_end_of_list_entry);
1622 }
1623 }
1624
1625 struct ArangeSpan {
1626 const MCSymbol *Start, *End;
1627 };
1628
1629 // Emit a debug aranges section, containing a CU lookup for any
1630 // address we can tie back to a CU.
emitDebugARanges()1631 void DwarfDebug::emitDebugARanges() {
1632 // Provides a unique id per text section.
1633 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
1634
1635 // Filter labels by section.
1636 for (const SymbolCU &SCU : ArangeLabels) {
1637 if (SCU.Sym->isInSection()) {
1638 // Make a note of this symbol and it's section.
1639 MCSection *Section = &SCU.Sym->getSection();
1640 if (!Section->getKind().isMetadata())
1641 SectionMap[Section].push_back(SCU);
1642 } else {
1643 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1644 // appear in the output. This sucks as we rely on sections to build
1645 // arange spans. We can do it without, but it's icky.
1646 SectionMap[nullptr].push_back(SCU);
1647 }
1648 }
1649
1650 // Add terminating symbols for each section.
1651 for (const auto &I : SectionMap) {
1652 MCSection *Section = I.first;
1653 MCSymbol *Sym = nullptr;
1654
1655 if (Section)
1656 Sym = Asm->OutStreamer->endSection(Section);
1657
1658 // Insert a final terminator.
1659 SectionMap[Section].push_back(SymbolCU(nullptr, Sym));
1660 }
1661
1662 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
1663
1664 for (auto &I : SectionMap) {
1665 const MCSection *Section = I.first;
1666 SmallVector<SymbolCU, 8> &List = I.second;
1667 if (List.size() < 2)
1668 continue;
1669
1670 // If we have no section (e.g. common), just write out
1671 // individual spans for each symbol.
1672 if (!Section) {
1673 for (const SymbolCU &Cur : List) {
1674 ArangeSpan Span;
1675 Span.Start = Cur.Sym;
1676 Span.End = nullptr;
1677 if (Cur.CU)
1678 Spans[Cur.CU].push_back(Span);
1679 }
1680 continue;
1681 }
1682
1683 // Sort the symbols by offset within the section.
1684 std::sort(List.begin(), List.end(),
1685 [&](const SymbolCU &A, const SymbolCU &B) {
1686 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1687 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
1688
1689 // Symbols with no order assigned should be placed at the end.
1690 // (e.g. section end labels)
1691 if (IA == 0)
1692 return false;
1693 if (IB == 0)
1694 return true;
1695 return IA < IB;
1696 });
1697
1698 // Build spans between each label.
1699 const MCSymbol *StartSym = List[0].Sym;
1700 for (size_t n = 1, e = List.size(); n < e; n++) {
1701 const SymbolCU &Prev = List[n - 1];
1702 const SymbolCU &Cur = List[n];
1703
1704 // Try and build the longest span we can within the same CU.
1705 if (Cur.CU != Prev.CU) {
1706 ArangeSpan Span;
1707 Span.Start = StartSym;
1708 Span.End = Cur.Sym;
1709 Spans[Prev.CU].push_back(Span);
1710 StartSym = Cur.Sym;
1711 }
1712 }
1713 }
1714
1715 // Start the dwarf aranges section.
1716 Asm->OutStreamer->SwitchSection(
1717 Asm->getObjFileLowering().getDwarfARangesSection());
1718
1719 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
1720
1721 // Build a list of CUs used.
1722 std::vector<DwarfCompileUnit *> CUs;
1723 for (const auto &it : Spans) {
1724 DwarfCompileUnit *CU = it.first;
1725 CUs.push_back(CU);
1726 }
1727
1728 // Sort the CU list (again, to ensure consistent output order).
1729 std::sort(CUs.begin(), CUs.end(), [](const DwarfUnit *A, const DwarfUnit *B) {
1730 return A->getUniqueID() < B->getUniqueID();
1731 });
1732
1733 // Emit an arange table for each CU we used.
1734 for (DwarfCompileUnit *CU : CUs) {
1735 std::vector<ArangeSpan> &List = Spans[CU];
1736
1737 // Describe the skeleton CU's offset and length, not the dwo file's.
1738 if (auto *Skel = CU->getSkeleton())
1739 CU = Skel;
1740
1741 // Emit size of content not including length itself.
1742 unsigned ContentSize =
1743 sizeof(int16_t) + // DWARF ARange version number
1744 sizeof(int32_t) + // Offset of CU in the .debug_info section
1745 sizeof(int8_t) + // Pointer Size (in bytes)
1746 sizeof(int8_t); // Segment Size (in bytes)
1747
1748 unsigned TupleSize = PtrSize * 2;
1749
1750 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
1751 unsigned Padding =
1752 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
1753
1754 ContentSize += Padding;
1755 ContentSize += (List.size() + 1) * TupleSize;
1756
1757 // For each compile unit, write the list of spans it covers.
1758 Asm->OutStreamer->AddComment("Length of ARange Set");
1759 Asm->EmitInt32(ContentSize);
1760 Asm->OutStreamer->AddComment("DWARF Arange version number");
1761 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
1762 Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
1763 Asm->emitDwarfSymbolReference(CU->getLabelBegin());
1764 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1765 Asm->EmitInt8(PtrSize);
1766 Asm->OutStreamer->AddComment("Segment Size (in bytes)");
1767 Asm->EmitInt8(0);
1768
1769 Asm->OutStreamer->EmitFill(Padding, 0xff);
1770
1771 for (const ArangeSpan &Span : List) {
1772 Asm->EmitLabelReference(Span.Start, PtrSize);
1773
1774 // Calculate the size as being from the span start to it's end.
1775 if (Span.End) {
1776 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
1777 } else {
1778 // For symbols without an end marker (e.g. common), we
1779 // write a single arange entry containing just that one symbol.
1780 uint64_t Size = SymSize[Span.Start];
1781 if (Size == 0)
1782 Size = 1;
1783
1784 Asm->OutStreamer->EmitIntValue(Size, PtrSize);
1785 }
1786 }
1787
1788 Asm->OutStreamer->AddComment("ARange terminator");
1789 Asm->OutStreamer->EmitIntValue(0, PtrSize);
1790 Asm->OutStreamer->EmitIntValue(0, PtrSize);
1791 }
1792 }
1793
1794 // Emit visible names into a debug ranges section.
emitDebugRanges()1795 void DwarfDebug::emitDebugRanges() {
1796 // Start the dwarf ranges section.
1797 Asm->OutStreamer->SwitchSection(
1798 Asm->getObjFileLowering().getDwarfRangesSection());
1799
1800 // Size for our labels.
1801 unsigned char Size = Asm->getDataLayout().getPointerSize();
1802
1803 // Grab the specific ranges for the compile units in the module.
1804 for (const auto &I : CUMap) {
1805 DwarfCompileUnit *TheCU = I.second;
1806
1807 if (auto *Skel = TheCU->getSkeleton())
1808 TheCU = Skel;
1809
1810 // Iterate over the misc ranges for the compile units in the module.
1811 for (const RangeSpanList &List : TheCU->getRangeLists()) {
1812 // Emit our symbol so we can find the beginning of the range.
1813 Asm->OutStreamer->EmitLabel(List.getSym());
1814
1815 for (const RangeSpan &Range : List.getRanges()) {
1816 const MCSymbol *Begin = Range.getStart();
1817 const MCSymbol *End = Range.getEnd();
1818 assert(Begin && "Range without a begin symbol?");
1819 assert(End && "Range without an end symbol?");
1820 if (auto *Base = TheCU->getBaseAddress()) {
1821 Asm->EmitLabelDifference(Begin, Base, Size);
1822 Asm->EmitLabelDifference(End, Base, Size);
1823 } else {
1824 Asm->OutStreamer->EmitSymbolValue(Begin, Size);
1825 Asm->OutStreamer->EmitSymbolValue(End, Size);
1826 }
1827 }
1828
1829 // And terminate the list with two 0 values.
1830 Asm->OutStreamer->EmitIntValue(0, Size);
1831 Asm->OutStreamer->EmitIntValue(0, Size);
1832 }
1833 }
1834 }
1835
1836 // DWARF5 Experimental Separate Dwarf emitters.
1837
initSkeletonUnit(const DwarfUnit & U,DIE & Die,std::unique_ptr<DwarfUnit> NewU)1838 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
1839 std::unique_ptr<DwarfUnit> NewU) {
1840 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
1841 U.getCUNode()->getSplitDebugFilename());
1842
1843 if (!CompilationDir.empty())
1844 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
1845
1846 addGnuPubAttributes(*NewU, Die);
1847
1848 SkeletonHolder.addUnit(std::move(NewU));
1849 }
1850
1851 // This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
1852 // DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
1853 // DW_AT_addr_base, DW_AT_ranges_base.
constructSkeletonCU(const DwarfCompileUnit & CU)1854 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
1855
1856 auto OwnedUnit = make_unique<DwarfCompileUnit>(
1857 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
1858 DwarfCompileUnit &NewCU = *OwnedUnit;
1859 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
1860
1861 NewCU.initStmtList();
1862
1863 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
1864
1865 return NewCU;
1866 }
1867
1868 // Emit the .debug_info.dwo section for separated dwarf. This contains the
1869 // compile units that would normally be in debug_info.
emitDebugInfoDWO()1870 void DwarfDebug::emitDebugInfoDWO() {
1871 assert(useSplitDwarf() && "No split dwarf debug info?");
1872 // Don't emit relocations into the dwo file.
1873 InfoHolder.emitUnits(/* UseOffsets */ true);
1874 }
1875
1876 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
1877 // abbreviations for the .debug_info.dwo section.
emitDebugAbbrevDWO()1878 void DwarfDebug::emitDebugAbbrevDWO() {
1879 assert(useSplitDwarf() && "No split dwarf?");
1880 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
1881 }
1882
emitDebugLineDWO()1883 void DwarfDebug::emitDebugLineDWO() {
1884 assert(useSplitDwarf() && "No split dwarf?");
1885 Asm->OutStreamer->SwitchSection(
1886 Asm->getObjFileLowering().getDwarfLineDWOSection());
1887 SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams());
1888 }
1889
1890 // Emit the .debug_str.dwo section for separated dwarf. This contains the
1891 // string section and is identical in format to traditional .debug_str
1892 // sections.
emitDebugStrDWO()1893 void DwarfDebug::emitDebugStrDWO() {
1894 assert(useSplitDwarf() && "No split dwarf?");
1895 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
1896 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
1897 OffSec);
1898 }
1899
getDwoLineTable(const DwarfCompileUnit & CU)1900 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
1901 if (!useSplitDwarf())
1902 return nullptr;
1903 if (SingleCU)
1904 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory());
1905 return &SplitTypeUnitFileTable;
1906 }
1907
makeTypeSignature(StringRef Identifier)1908 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
1909 MD5 Hash;
1910 Hash.update(Identifier);
1911 // ... take the least significant 8 bytes and return those. Our MD5
1912 // implementation always returns its results in little endian, swap bytes
1913 // appropriately.
1914 MD5::MD5Result Result;
1915 Hash.final(Result);
1916 return support::endian::read64le(Result + 8);
1917 }
1918
addDwarfTypeUnitType(DwarfCompileUnit & CU,StringRef Identifier,DIE & RefDie,const DICompositeType * CTy)1919 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
1920 StringRef Identifier, DIE &RefDie,
1921 const DICompositeType *CTy) {
1922 // Fast path if we're building some type units and one has already used the
1923 // address pool we know we're going to throw away all this work anyway, so
1924 // don't bother building dependent types.
1925 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
1926 return;
1927
1928 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
1929 if (TU) {
1930 CU.addDIETypeSignature(RefDie, *TU);
1931 return;
1932 }
1933
1934 bool TopLevelType = TypeUnitsUnderConstruction.empty();
1935 AddrPool.resetUsedFlag();
1936
1937 auto OwnedUnit = make_unique<DwarfTypeUnit>(
1938 InfoHolder.getUnits().size() + TypeUnitsUnderConstruction.size(), CU, Asm,
1939 this, &InfoHolder, getDwoLineTable(CU));
1940 DwarfTypeUnit &NewTU = *OwnedUnit;
1941 DIE &UnitDie = NewTU.getUnitDie();
1942 TU = &NewTU;
1943 TypeUnitsUnderConstruction.push_back(
1944 std::make_pair(std::move(OwnedUnit), CTy));
1945
1946 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
1947 CU.getLanguage());
1948
1949 uint64_t Signature = makeTypeSignature(Identifier);
1950 NewTU.setTypeSignature(Signature);
1951
1952 if (useSplitDwarf())
1953 NewTU.initSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
1954 else {
1955 CU.applyStmtList(UnitDie);
1956 NewTU.initSection(
1957 Asm->getObjFileLowering().getDwarfTypesSection(Signature));
1958 }
1959
1960 NewTU.setType(NewTU.createTypeDIE(CTy));
1961
1962 if (TopLevelType) {
1963 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
1964 TypeUnitsUnderConstruction.clear();
1965
1966 // Types referencing entries in the address table cannot be placed in type
1967 // units.
1968 if (AddrPool.hasBeenUsed()) {
1969
1970 // Remove all the types built while building this type.
1971 // This is pessimistic as some of these types might not be dependent on
1972 // the type that used an address.
1973 for (const auto &TU : TypeUnitsToAdd)
1974 DwarfTypeUnits.erase(TU.second);
1975
1976 // Construct this type in the CU directly.
1977 // This is inefficient because all the dependent types will be rebuilt
1978 // from scratch, including building them in type units, discovering that
1979 // they depend on addresses, throwing them out and rebuilding them.
1980 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
1981 return;
1982 }
1983
1984 // If the type wasn't dependent on fission addresses, finish adding the type
1985 // and all its dependent types.
1986 for (auto &TU : TypeUnitsToAdd)
1987 InfoHolder.addUnit(std::move(TU.first));
1988 }
1989 CU.addDIETypeSignature(RefDie, NewTU);
1990 }
1991
1992 // Accelerator table mutators - add each name along with its companion
1993 // DIE to the proper table while ensuring that the name that we're going
1994 // to reference is in the string table. We do this since the names we
1995 // add may not only be identical to the names in the DIE.
addAccelName(StringRef Name,const DIE & Die)1996 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
1997 if (!useDwarfAccelTables())
1998 return;
1999 AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2000 }
2001
addAccelObjC(StringRef Name,const DIE & Die)2002 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2003 if (!useDwarfAccelTables())
2004 return;
2005 AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2006 }
2007
addAccelNamespace(StringRef Name,const DIE & Die)2008 void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
2009 if (!useDwarfAccelTables())
2010 return;
2011 AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2012 }
2013
addAccelType(StringRef Name,const DIE & Die,char Flags)2014 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2015 if (!useDwarfAccelTables())
2016 return;
2017 AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
2018 }
2019