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 "DebugLocStream.h"
19 #include "DwarfCompileUnit.h"
20 #include "DwarfExpression.h"
21 #include "DwarfFile.h"
22 #include "DwarfUnit.h"
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/MapVector.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/Triple.h"
31 #include "llvm/ADT/Twine.h"
32 #include "llvm/BinaryFormat/Dwarf.h"
33 #include "llvm/CodeGen/AccelTable.h"
34 #include "llvm/CodeGen/AsmPrinter.h"
35 #include "llvm/CodeGen/DIE.h"
36 #include "llvm/CodeGen/LexicalScopes.h"
37 #include "llvm/CodeGen/MachineBasicBlock.h"
38 #include "llvm/CodeGen/MachineFunction.h"
39 #include "llvm/CodeGen/MachineInstr.h"
40 #include "llvm/CodeGen/MachineModuleInfo.h"
41 #include "llvm/CodeGen/MachineOperand.h"
42 #include "llvm/CodeGen/TargetRegisterInfo.h"
43 #include "llvm/CodeGen/TargetSubtargetInfo.h"
44 #include "llvm/IR/Constants.h"
45 #include "llvm/IR/DebugInfoMetadata.h"
46 #include "llvm/IR/DebugLoc.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/GlobalVariable.h"
49 #include "llvm/IR/Module.h"
50 #include "llvm/MC/MCAsmInfo.h"
51 #include "llvm/MC/MCContext.h"
52 #include "llvm/MC/MCDwarf.h"
53 #include "llvm/MC/MCSection.h"
54 #include "llvm/MC/MCStreamer.h"
55 #include "llvm/MC/MCSymbol.h"
56 #include "llvm/MC/MCTargetOptions.h"
57 #include "llvm/MC/MachineLocation.h"
58 #include "llvm/MC/SectionKind.h"
59 #include "llvm/Pass.h"
60 #include "llvm/Support/Casting.h"
61 #include "llvm/Support/CommandLine.h"
62 #include "llvm/Support/Debug.h"
63 #include "llvm/Support/ErrorHandling.h"
64 #include "llvm/Support/MD5.h"
65 #include "llvm/Support/MathExtras.h"
66 #include "llvm/Support/Timer.h"
67 #include "llvm/Support/raw_ostream.h"
68 #include "llvm/Target/TargetLoweringObjectFile.h"
69 #include "llvm/Target/TargetMachine.h"
70 #include "llvm/Target/TargetOptions.h"
71 #include <algorithm>
72 #include <cassert>
73 #include <cstddef>
74 #include <cstdint>
75 #include <iterator>
76 #include <string>
77 #include <utility>
78 #include <vector>
79
80 using namespace llvm;
81
82 #define DEBUG_TYPE "dwarfdebug"
83
84 static cl::opt<bool>
85 DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
86 cl::desc("Disable debug info printing"));
87
88 static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier(
89 "use-dwarf-ranges-base-address-specifier", cl::Hidden,
90 cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
91
92 static cl::opt<bool> GenerateARangeSection("generate-arange-section",
93 cl::Hidden,
94 cl::desc("Generate dwarf aranges"),
95 cl::init(false));
96
97 static cl::opt<bool>
98 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
99 cl::desc("Generate DWARF4 type units."),
100 cl::init(false));
101
102 static cl::opt<bool> SplitDwarfCrossCuReferences(
103 "split-dwarf-cross-cu-references", cl::Hidden,
104 cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
105
106 enum DefaultOnOff { Default, Enable, Disable };
107
108 static cl::opt<DefaultOnOff> UnknownLocations(
109 "use-unknown-locations", cl::Hidden,
110 cl::desc("Make an absence of debug location information explicit."),
111 cl::values(clEnumVal(Default, "At top of block or after label"),
112 clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
113 cl::init(Default));
114
115 static cl::opt<AccelTableKind> AccelTables(
116 "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."),
117 cl::values(clEnumValN(AccelTableKind::Default, "Default",
118 "Default for platform"),
119 clEnumValN(AccelTableKind::None, "Disable", "Disabled."),
120 clEnumValN(AccelTableKind::Apple, "Apple", "Apple"),
121 clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")),
122 cl::init(AccelTableKind::Default));
123
124 static cl::opt<DefaultOnOff>
125 DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden,
126 cl::desc("Use inlined strings rather than string section."),
127 cl::values(clEnumVal(Default, "Default for platform"),
128 clEnumVal(Enable, "Enabled"),
129 clEnumVal(Disable, "Disabled")),
130 cl::init(Default));
131
132 static cl::opt<bool>
133 NoDwarfPubSections("no-dwarf-pub-sections", cl::Hidden,
134 cl::desc("Disable emission of DWARF pub sections."),
135 cl::init(false));
136
137 static cl::opt<bool>
138 NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden,
139 cl::desc("Disable emission .debug_ranges section."),
140 cl::init(false));
141
142 static cl::opt<DefaultOnOff> DwarfSectionsAsReferences(
143 "dwarf-sections-as-references", cl::Hidden,
144 cl::desc("Use sections+offset as references rather than labels."),
145 cl::values(clEnumVal(Default, "Default for platform"),
146 clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
147 cl::init(Default));
148
149 enum LinkageNameOption {
150 DefaultLinkageNames,
151 AllLinkageNames,
152 AbstractLinkageNames
153 };
154
155 static cl::opt<LinkageNameOption>
156 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
157 cl::desc("Which DWARF linkage-name attributes to emit."),
158 cl::values(clEnumValN(DefaultLinkageNames, "Default",
159 "Default for platform"),
160 clEnumValN(AllLinkageNames, "All", "All"),
161 clEnumValN(AbstractLinkageNames, "Abstract",
162 "Abstract subprograms")),
163 cl::init(DefaultLinkageNames));
164
165 static const char *const DWARFGroupName = "dwarf";
166 static const char *const DWARFGroupDescription = "DWARF Emission";
167 static const char *const DbgTimerName = "writer";
168 static const char *const DbgTimerDescription = "DWARF Debug Writer";
169
emitOp(uint8_t Op,const char * Comment)170 void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
171 BS.EmitInt8(
172 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
173 : dwarf::OperationEncodingString(Op));
174 }
175
emitSigned(int64_t Value)176 void DebugLocDwarfExpression::emitSigned(int64_t Value) {
177 BS.EmitSLEB128(Value, Twine(Value));
178 }
179
emitUnsigned(uint64_t Value)180 void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
181 BS.EmitULEB128(Value, Twine(Value));
182 }
183
isFrameRegister(const TargetRegisterInfo & TRI,unsigned MachineReg)184 bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
185 unsigned MachineReg) {
186 // This information is not available while emitting .debug_loc entries.
187 return false;
188 }
189
isBlockByrefVariable() const190 bool DbgVariable::isBlockByrefVariable() const {
191 assert(Var && "Invalid complex DbgVariable!");
192 return Var->getType().resolve()->isBlockByrefStruct();
193 }
194
getType() const195 const DIType *DbgVariable::getType() const {
196 DIType *Ty = Var->getType().resolve();
197 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
198 // addresses instead.
199 if (Ty->isBlockByrefStruct()) {
200 /* Byref variables, in Blocks, are declared by the programmer as
201 "SomeType VarName;", but the compiler creates a
202 __Block_byref_x_VarName struct, and gives the variable VarName
203 either the struct, or a pointer to the struct, as its type. This
204 is necessary for various behind-the-scenes things the compiler
205 needs to do with by-reference variables in blocks.
206
207 However, as far as the original *programmer* is concerned, the
208 variable should still have type 'SomeType', as originally declared.
209
210 The following function dives into the __Block_byref_x_VarName
211 struct to find the original type of the variable. This will be
212 passed back to the code generating the type for the Debug
213 Information Entry for the variable 'VarName'. 'VarName' will then
214 have the original type 'SomeType' in its debug information.
215
216 The original type 'SomeType' will be the type of the field named
217 'VarName' inside the __Block_byref_x_VarName struct.
218
219 NOTE: In order for this to not completely fail on the debugger
220 side, the Debug Information Entry for the variable VarName needs to
221 have a DW_AT_location that tells the debugger how to unwind through
222 the pointers and __Block_byref_x_VarName struct to find the actual
223 value of the variable. The function addBlockByrefType does this. */
224 DIType *subType = Ty;
225 uint16_t tag = Ty->getTag();
226
227 if (tag == dwarf::DW_TAG_pointer_type)
228 subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
229
230 auto Elements = cast<DICompositeType>(subType)->getElements();
231 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
232 auto *DT = cast<DIDerivedType>(Elements[i]);
233 if (getName() == DT->getName())
234 return resolve(DT->getBaseType());
235 }
236 }
237 return Ty;
238 }
239
getFrameIndexExprs() const240 ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
241 if (FrameIndexExprs.size() == 1)
242 return FrameIndexExprs;
243
244 assert(llvm::all_of(FrameIndexExprs,
245 [](const FrameIndexExpr &A) {
246 return A.Expr->isFragment();
247 }) &&
248 "multiple FI expressions without DW_OP_LLVM_fragment");
249 llvm::sort(FrameIndexExprs.begin(), FrameIndexExprs.end(),
250 [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
251 return A.Expr->getFragmentInfo()->OffsetInBits <
252 B.Expr->getFragmentInfo()->OffsetInBits;
253 });
254
255 return FrameIndexExprs;
256 }
257
addMMIEntry(const DbgVariable & V)258 void DbgVariable::addMMIEntry(const DbgVariable &V) {
259 assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry");
260 assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry");
261 assert(V.Var == Var && "conflicting variable");
262 assert(V.IA == IA && "conflicting inlined-at location");
263
264 assert(!FrameIndexExprs.empty() && "Expected an MMI entry");
265 assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry");
266
267 // FIXME: This logic should not be necessary anymore, as we now have proper
268 // deduplication. However, without it, we currently run into the assertion
269 // below, which means that we are likely dealing with broken input, i.e. two
270 // non-fragment entries for the same variable at different frame indices.
271 if (FrameIndexExprs.size()) {
272 auto *Expr = FrameIndexExprs.back().Expr;
273 if (!Expr || !Expr->isFragment())
274 return;
275 }
276
277 for (const auto &FIE : V.FrameIndexExprs)
278 // Ignore duplicate entries.
279 if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) {
280 return FIE.FI == Other.FI && FIE.Expr == Other.Expr;
281 }))
282 FrameIndexExprs.push_back(FIE);
283
284 assert((FrameIndexExprs.size() == 1 ||
285 llvm::all_of(FrameIndexExprs,
286 [](FrameIndexExpr &FIE) {
287 return FIE.Expr && FIE.Expr->isFragment();
288 })) &&
289 "conflicting locations for variable");
290 }
291
computeAccelTableKind(unsigned DwarfVersion,bool GenerateTypeUnits,DebuggerKind Tuning,const Triple & TT)292 static AccelTableKind computeAccelTableKind(unsigned DwarfVersion,
293 bool GenerateTypeUnits,
294 DebuggerKind Tuning,
295 const Triple &TT) {
296 // Honor an explicit request.
297 if (AccelTables != AccelTableKind::Default)
298 return AccelTables;
299
300 // Accelerator tables with type units are currently not supported.
301 if (GenerateTypeUnits)
302 return AccelTableKind::None;
303
304 // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5
305 // always implies debug_names. For lower standard versions we use apple
306 // accelerator tables on apple platforms and debug_names elsewhere.
307 if (DwarfVersion >= 5)
308 return AccelTableKind::Dwarf;
309 if (Tuning == DebuggerKind::LLDB)
310 return TT.isOSBinFormatMachO() ? AccelTableKind::Apple
311 : AccelTableKind::Dwarf;
312 return AccelTableKind::None;
313 }
314
DwarfDebug(AsmPrinter * A,Module * M)315 DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
316 : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
317 InfoHolder(A, "info_string", DIEValueAllocator),
318 SkeletonHolder(A, "skel_string", DIEValueAllocator),
319 IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
320 const Triple &TT = Asm->TM.getTargetTriple();
321
322 // Make sure we know our "debugger tuning." The target option takes
323 // precedence; fall back to triple-based defaults.
324 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
325 DebuggerTuning = Asm->TM.Options.DebuggerTuning;
326 else if (IsDarwin)
327 DebuggerTuning = DebuggerKind::LLDB;
328 else if (TT.isPS4CPU())
329 DebuggerTuning = DebuggerKind::SCE;
330 else
331 DebuggerTuning = DebuggerKind::GDB;
332
333 if (DwarfInlinedStrings == Default)
334 UseInlineStrings = TT.isNVPTX();
335 else
336 UseInlineStrings = DwarfInlinedStrings == Enable;
337
338 UseLocSection = !TT.isNVPTX();
339
340 HasAppleExtensionAttributes = tuneForLLDB();
341
342 // Handle split DWARF.
343 HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty();
344
345 // SCE defaults to linkage names only for abstract subprograms.
346 if (DwarfLinkageNames == DefaultLinkageNames)
347 UseAllLinkageNames = !tuneForSCE();
348 else
349 UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
350
351 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
352 unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
353 : MMI->getModule()->getDwarfVersion();
354 // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2.
355 DwarfVersion =
356 TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION);
357
358 UsePubSections = !NoDwarfPubSections && !TT.isNVPTX();
359 UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX();
360
361 // Use sections as references. Force for NVPTX.
362 if (DwarfSectionsAsReferences == Default)
363 UseSectionsAsReferences = TT.isNVPTX();
364 else
365 UseSectionsAsReferences = DwarfSectionsAsReferences == Enable;
366
367 // Don't generate type units for unsupported object file formats.
368 GenerateTypeUnits =
369 A->TM.getTargetTriple().isOSBinFormatELF() && GenerateDwarfTypeUnits;
370
371 TheAccelTableKind = computeAccelTableKind(
372 DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple());
373
374 // Work around a GDB bug. GDB doesn't support the standard opcode;
375 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
376 // is defined as of DWARF 3.
377 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
378 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
379 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
380
381 // GDB does not fully support the DWARF 4 representation for bitfields.
382 UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB();
383
384 // The DWARF v5 string offsets table has - possibly shared - contributions
385 // from each compile and type unit each preceded by a header. The string
386 // offsets table used by the pre-DWARF v5 split-DWARF implementation uses
387 // a monolithic string offsets table without any header.
388 UseSegmentedStringOffsetsTable = DwarfVersion >= 5;
389
390 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
391 }
392
393 // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
394 DwarfDebug::~DwarfDebug() = default;
395
isObjCClass(StringRef Name)396 static bool isObjCClass(StringRef Name) {
397 return Name.startswith("+") || Name.startswith("-");
398 }
399
hasObjCCategory(StringRef Name)400 static bool hasObjCCategory(StringRef Name) {
401 if (!isObjCClass(Name))
402 return false;
403
404 return Name.find(") ") != StringRef::npos;
405 }
406
getObjCClassCategory(StringRef In,StringRef & Class,StringRef & Category)407 static void getObjCClassCategory(StringRef In, StringRef &Class,
408 StringRef &Category) {
409 if (!hasObjCCategory(In)) {
410 Class = In.slice(In.find('[') + 1, In.find(' '));
411 Category = "";
412 return;
413 }
414
415 Class = In.slice(In.find('[') + 1, In.find('('));
416 Category = In.slice(In.find('[') + 1, In.find(' '));
417 }
418
getObjCMethodName(StringRef In)419 static StringRef getObjCMethodName(StringRef In) {
420 return In.slice(In.find(' ') + 1, In.find(']'));
421 }
422
423 // Add the various names to the Dwarf accelerator table names.
addSubprogramNames(const DISubprogram * SP,DIE & Die)424 void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) {
425 if (!SP->isDefinition())
426 return;
427
428 if (SP->getName() != "")
429 addAccelName(SP->getName(), Die);
430
431 // If the linkage name is different than the name, go ahead and output that as
432 // well into the name table. Only do that if we are going to actually emit
433 // that name.
434 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() &&
435 (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP)))
436 addAccelName(SP->getLinkageName(), Die);
437
438 // If this is an Objective-C selector name add it to the ObjC accelerator
439 // too.
440 if (isObjCClass(SP->getName())) {
441 StringRef Class, Category;
442 getObjCClassCategory(SP->getName(), Class, Category);
443 addAccelObjC(Class, Die);
444 if (Category != "")
445 addAccelObjC(Category, Die);
446 // Also add the base method name to the name table.
447 addAccelName(getObjCMethodName(SP->getName()), Die);
448 }
449 }
450
451 /// Check whether we should create a DIE for the given Scope, return true
452 /// if we don't create a DIE (the corresponding DIE is null).
isLexicalScopeDIENull(LexicalScope * Scope)453 bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
454 if (Scope->isAbstractScope())
455 return false;
456
457 // We don't create a DIE if there is no Range.
458 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
459 if (Ranges.empty())
460 return true;
461
462 if (Ranges.size() > 1)
463 return false;
464
465 // We don't create a DIE if we have a single Range and the end label
466 // is null.
467 return !getLabelAfterInsn(Ranges.front().second);
468 }
469
forBothCUs(DwarfCompileUnit & CU,Func F)470 template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
471 F(CU);
472 if (auto *SkelCU = CU.getSkeleton())
473 if (CU.getCUNode()->getSplitDebugInlining())
474 F(*SkelCU);
475 }
476
shareAcrossDWOCUs() const477 bool DwarfDebug::shareAcrossDWOCUs() const {
478 return SplitDwarfCrossCuReferences;
479 }
480
constructAbstractSubprogramScopeDIE(DwarfCompileUnit & SrcCU,LexicalScope * Scope)481 void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU,
482 LexicalScope *Scope) {
483 assert(Scope && Scope->getScopeNode());
484 assert(Scope->isAbstractScope());
485 assert(!Scope->getInlinedAt());
486
487 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
488
489 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
490 // was inlined from another compile unit.
491 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining())
492 // Avoid building the original CU if it won't be used
493 SrcCU.constructAbstractSubprogramScopeDIE(Scope);
494 else {
495 auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
496 if (auto *SkelCU = CU.getSkeleton()) {
497 (shareAcrossDWOCUs() ? CU : SrcCU)
498 .constructAbstractSubprogramScopeDIE(Scope);
499 if (CU.getCUNode()->getSplitDebugInlining())
500 SkelCU->constructAbstractSubprogramScopeDIE(Scope);
501 } else
502 CU.constructAbstractSubprogramScopeDIE(Scope);
503 }
504 }
505
addGnuPubAttributes(DwarfCompileUnit & U,DIE & D) const506 void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const {
507 if (!U.hasDwarfPubSections())
508 return;
509
510 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
511 }
512
513 // Create new DwarfCompileUnit for the given metadata node with tag
514 // DW_TAG_compile_unit.
515 DwarfCompileUnit &
getOrCreateDwarfCompileUnit(const DICompileUnit * DIUnit)516 DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) {
517 if (auto *CU = CUMap.lookup(DIUnit))
518 return *CU;
519 StringRef FN = DIUnit->getFilename();
520 CompilationDir = DIUnit->getDirectory();
521
522 auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
523 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
524 DwarfCompileUnit &NewCU = *OwnedUnit;
525 DIE &Die = NewCU.getUnitDie();
526 InfoHolder.addUnit(std::move(OwnedUnit));
527 if (useSplitDwarf()) {
528 NewCU.setSkeleton(constructSkeletonCU(NewCU));
529 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
530 Asm->TM.Options.MCOptions.SplitDwarfFile);
531 }
532
533 for (auto *IE : DIUnit->getImportedEntities())
534 NewCU.addImportedEntity(IE);
535
536 // LTO with assembly output shares a single line table amongst multiple CUs.
537 // To avoid the compilation directory being ambiguous, let the line table
538 // explicitly describe the directory of all files, never relying on the
539 // compilation directory.
540 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
541 Asm->OutStreamer->emitDwarfFile0Directive(
542 CompilationDir, FN, NewCU.getMD5AsBytes(DIUnit->getFile()),
543 DIUnit->getSource(), NewCU.getUniqueID());
544
545 StringRef Producer = DIUnit->getProducer();
546 StringRef Flags = DIUnit->getFlags();
547 if (!Flags.empty()) {
548 std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
549 NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
550 } else
551 NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
552
553 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
554 DIUnit->getSourceLanguage());
555 NewCU.addString(Die, dwarf::DW_AT_name, FN);
556
557 // Add DW_str_offsets_base to the unit DIE, except for split units.
558 if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
559 NewCU.addStringOffsetsStart();
560
561 if (!useSplitDwarf()) {
562 NewCU.initStmtList();
563
564 // If we're using split dwarf the compilation dir is going to be in the
565 // skeleton CU and so we don't need to duplicate it here.
566 if (!CompilationDir.empty())
567 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
568
569 addGnuPubAttributes(NewCU, Die);
570 }
571
572 if (useAppleExtensionAttributes()) {
573 if (DIUnit->isOptimized())
574 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
575
576 StringRef Flags = DIUnit->getFlags();
577 if (!Flags.empty())
578 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
579
580 if (unsigned RVer = DIUnit->getRuntimeVersion())
581 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
582 dwarf::DW_FORM_data1, RVer);
583 }
584
585 if (useSplitDwarf())
586 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
587 else
588 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
589
590 if (DIUnit->getDWOId()) {
591 // This CU is either a clang module DWO or a skeleton CU.
592 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
593 DIUnit->getDWOId());
594 if (!DIUnit->getSplitDebugFilename().empty())
595 // This is a prefabricated skeleton CU.
596 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
597 DIUnit->getSplitDebugFilename());
598 }
599
600 CUMap.insert({DIUnit, &NewCU});
601 CUDieMap.insert({&Die, &NewCU});
602 return NewCU;
603 }
604
constructAndAddImportedEntityDIE(DwarfCompileUnit & TheCU,const DIImportedEntity * N)605 void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
606 const DIImportedEntity *N) {
607 if (isa<DILocalScope>(N->getScope()))
608 return;
609 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
610 D->addChild(TheCU.constructImportedEntityDIE(N));
611 }
612
613 /// Sort and unique GVEs by comparing their fragment offset.
614 static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> & GVEs)615 sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
616 llvm::sort(GVEs.begin(), GVEs.end(),
617 [](DwarfCompileUnit::GlobalExpr A,
618 DwarfCompileUnit::GlobalExpr B) {
619 // Sort order: first null exprs, then exprs without fragment
620 // info, then sort by fragment offset in bits.
621 // FIXME: Come up with a more comprehensive comparator so
622 // the sorting isn't non-deterministic, and so the following
623 // std::unique call works correctly.
624 if (!A.Expr || !B.Expr)
625 return !!B.Expr;
626 auto FragmentA = A.Expr->getFragmentInfo();
627 auto FragmentB = B.Expr->getFragmentInfo();
628 if (!FragmentA || !FragmentB)
629 return !!FragmentB;
630 return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
631 });
632 GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
633 [](DwarfCompileUnit::GlobalExpr A,
634 DwarfCompileUnit::GlobalExpr B) {
635 return A.Expr == B.Expr;
636 }),
637 GVEs.end());
638 return GVEs;
639 }
640
641 // Emit all Dwarf sections that should come prior to the content. Create
642 // global DIEs and emit initial debug info sections. This is invoked by
643 // the target AsmPrinter.
beginModule()644 void DwarfDebug::beginModule() {
645 NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName,
646 DWARFGroupDescription, TimePassesIsEnabled);
647 if (DisableDebugInfoPrinting)
648 return;
649
650 const Module *M = MMI->getModule();
651
652 unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
653 M->debug_compile_units_end());
654 // Tell MMI whether we have debug info.
655 MMI->setDebugInfoAvailability(NumDebugCUs > 0);
656 SingleCU = NumDebugCUs == 1;
657 DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
658 GVMap;
659 for (const GlobalVariable &Global : M->globals()) {
660 SmallVector<DIGlobalVariableExpression *, 1> GVs;
661 Global.getDebugInfo(GVs);
662 for (auto *GVE : GVs)
663 GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
664 }
665
666 // Create the symbol that designates the start of the unit's contribution
667 // to the string offsets table. In a split DWARF scenario, only the skeleton
668 // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol).
669 if (useSegmentedStringOffsetsTable())
670 (useSplitDwarf() ? SkeletonHolder : InfoHolder)
671 .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base"));
672
673 // Create the symbol that designates the start of the DWARF v5 range list
674 // table. It is located past the header and before the offsets table.
675 if (getDwarfVersion() >= 5)
676 (useSplitDwarf() ? SkeletonHolder : InfoHolder)
677 .setRnglistsTableBaseSym(Asm->createTempSymbol("rnglists_table_base"));
678
679 for (DICompileUnit *CUNode : M->debug_compile_units()) {
680 // FIXME: Move local imported entities into a list attached to the
681 // subprogram, then this search won't be needed and a
682 // getImportedEntities().empty() test should go below with the rest.
683 bool HasNonLocalImportedEntities = llvm::any_of(
684 CUNode->getImportedEntities(), [](const DIImportedEntity *IE) {
685 return !isa<DILocalScope>(IE->getScope());
686 });
687
688 if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() &&
689 CUNode->getRetainedTypes().empty() &&
690 CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
691 continue;
692
693 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
694
695 // Global Variables.
696 for (auto *GVE : CUNode->getGlobalVariables()) {
697 // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
698 // already know about the variable and it isn't adding a constant
699 // expression.
700 auto &GVMapEntry = GVMap[GVE->getVariable()];
701 auto *Expr = GVE->getExpression();
702 if (!GVMapEntry.size() || (Expr && Expr->isConstant()))
703 GVMapEntry.push_back({nullptr, Expr});
704 }
705 DenseSet<DIGlobalVariable *> Processed;
706 for (auto *GVE : CUNode->getGlobalVariables()) {
707 DIGlobalVariable *GV = GVE->getVariable();
708 if (Processed.insert(GV).second)
709 CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
710 }
711
712 for (auto *Ty : CUNode->getEnumTypes()) {
713 // The enum types array by design contains pointers to
714 // MDNodes rather than DIRefs. Unique them here.
715 CU.getOrCreateTypeDIE(cast<DIType>(Ty));
716 }
717 for (auto *Ty : CUNode->getRetainedTypes()) {
718 // The retained types array by design contains pointers to
719 // MDNodes rather than DIRefs. Unique them here.
720 if (DIType *RT = dyn_cast<DIType>(Ty))
721 // There is no point in force-emitting a forward declaration.
722 CU.getOrCreateTypeDIE(RT);
723 }
724 // Emit imported_modules last so that the relevant context is already
725 // available.
726 for (auto *IE : CUNode->getImportedEntities())
727 constructAndAddImportedEntityDIE(CU, IE);
728 }
729 }
730
finishVariableDefinitions()731 void DwarfDebug::finishVariableDefinitions() {
732 for (const auto &Var : ConcreteVariables) {
733 DIE *VariableDie = Var->getDIE();
734 assert(VariableDie);
735 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
736 // in the ConcreteVariables list, rather than looking it up again here.
737 // DIE::getUnit isn't simple - it walks parent pointers, etc.
738 DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie());
739 assert(Unit);
740 Unit->finishVariableDefinition(*Var);
741 }
742 }
743
finishSubprogramDefinitions()744 void DwarfDebug::finishSubprogramDefinitions() {
745 for (const DISubprogram *SP : ProcessedSPNodes) {
746 assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
747 forBothCUs(
748 getOrCreateDwarfCompileUnit(SP->getUnit()),
749 [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); });
750 }
751 }
752
finalizeModuleInfo()753 void DwarfDebug::finalizeModuleInfo() {
754 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
755
756 finishSubprogramDefinitions();
757
758 finishVariableDefinitions();
759
760 // Include the DWO file name in the hash if there's more than one CU.
761 // This handles ThinLTO's situation where imported CUs may very easily be
762 // duplicate with the same CU partially imported into another ThinLTO unit.
763 StringRef DWOName;
764 if (CUMap.size() > 1)
765 DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile;
766
767 // Handle anything that needs to be done on a per-unit basis after
768 // all other generation.
769 for (const auto &P : CUMap) {
770 auto &TheCU = *P.second;
771 // Emit DW_AT_containing_type attribute to connect types with their
772 // vtable holding type.
773 TheCU.constructContainingTypeDIEs();
774
775 // Add CU specific attributes if we need to add any.
776 // If we're splitting the dwarf out now that we've got the entire
777 // CU then add the dwo id to it.
778 auto *SkCU = TheCU.getSkeleton();
779 if (useSplitDwarf()) {
780 // Emit a unique identifier for this CU.
781 uint64_t ID =
782 DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie());
783 if (getDwarfVersion() >= 5) {
784 TheCU.setDWOId(ID);
785 SkCU->setDWOId(ID);
786 } else {
787 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
788 dwarf::DW_FORM_data8, ID);
789 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
790 dwarf::DW_FORM_data8, ID);
791 }
792 // We don't keep track of which addresses are used in which CU so this
793 // is a bit pessimistic under LTO.
794 if (!AddrPool.isEmpty()) {
795 const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol();
796 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
797 Sym, Sym);
798 }
799 if (getDwarfVersion() < 5 && !SkCU->getRangeLists().empty()) {
800 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
801 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
802 Sym, Sym);
803 }
804 }
805
806 // If we have code split among multiple sections or non-contiguous
807 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
808 // remain in the .o file, otherwise add a DW_AT_low_pc.
809 // FIXME: We should use ranges allow reordering of code ala
810 // .subsections_via_symbols in mach-o. This would mean turning on
811 // ranges for all subprogram DIEs for mach-o.
812 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
813 if (unsigned NumRanges = TheCU.getRanges().size()) {
814 if (NumRanges > 1 && useRangesSection())
815 // A DW_AT_low_pc attribute may also be specified in combination with
816 // DW_AT_ranges to specify the default base address for use in
817 // location lists (see Section 2.6.2) and range lists (see Section
818 // 2.17.3).
819 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
820 else
821 U.setBaseAddress(TheCU.getRanges().front().getStart());
822 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
823 }
824
825 if (getDwarfVersion() >= 5 && !useSplitDwarf() &&
826 !U.getRangeLists().empty())
827 U.addRnglistsBase();
828
829 auto *CUNode = cast<DICompileUnit>(P.first);
830 // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
831 if (CUNode->getMacros())
832 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
833 U.getMacroLabelBegin(),
834 TLOF.getDwarfMacinfoSection()->getBeginSymbol());
835 }
836
837 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
838 for (auto *CUNode : MMI->getModule()->debug_compile_units())
839 if (CUNode->getDWOId())
840 getOrCreateDwarfCompileUnit(CUNode);
841
842 // Compute DIE offsets and sizes.
843 InfoHolder.computeSizeAndOffsets();
844 if (useSplitDwarf())
845 SkeletonHolder.computeSizeAndOffsets();
846 }
847
848 // Emit all Dwarf sections that should come after the content.
endModule()849 void DwarfDebug::endModule() {
850 assert(CurFn == nullptr);
851 assert(CurMI == nullptr);
852
853 // If we aren't actually generating debug info (check beginModule -
854 // conditionalized on !DisableDebugInfoPrinting and the presence of the
855 // llvm.dbg.cu metadata node)
856 if (!MMI->hasDebugInfo())
857 return;
858
859 // Finalize the debug info for the module.
860 finalizeModuleInfo();
861
862 emitDebugStr();
863
864 if (useSplitDwarf())
865 emitDebugLocDWO();
866 else
867 // Emit info into a debug loc section.
868 emitDebugLoc();
869
870 // Corresponding abbreviations into a abbrev section.
871 emitAbbreviations();
872
873 // Emit all the DIEs into a debug info section.
874 emitDebugInfo();
875
876 // Emit info into a debug aranges section.
877 if (GenerateARangeSection)
878 emitDebugARanges();
879
880 // Emit info into a debug ranges section.
881 emitDebugRanges();
882
883 // Emit info into a debug macinfo section.
884 emitDebugMacinfo();
885
886 if (useSplitDwarf()) {
887 emitDebugStrDWO();
888 emitDebugInfoDWO();
889 emitDebugAbbrevDWO();
890 emitDebugLineDWO();
891 emitDebugAddr();
892 }
893
894 // Emit info into the dwarf accelerator table sections.
895 switch (getAccelTableKind()) {
896 case AccelTableKind::Apple:
897 emitAccelNames();
898 emitAccelObjC();
899 emitAccelNamespaces();
900 emitAccelTypes();
901 break;
902 case AccelTableKind::Dwarf:
903 emitAccelDebugNames();
904 break;
905 case AccelTableKind::None:
906 break;
907 case AccelTableKind::Default:
908 llvm_unreachable("Default should have already been resolved.");
909 }
910
911 // Emit the pubnames and pubtypes sections if requested.
912 emitDebugPubSections();
913
914 // clean up.
915 // FIXME: AbstractVariables.clear();
916 }
917
ensureAbstractVariableIsCreated(DwarfCompileUnit & CU,InlinedVariable IV,const MDNode * ScopeNode)918 void DwarfDebug::ensureAbstractVariableIsCreated(DwarfCompileUnit &CU, InlinedVariable IV,
919 const MDNode *ScopeNode) {
920 const DILocalVariable *Cleansed = nullptr;
921 if (CU.getExistingAbstractVariable(IV, Cleansed))
922 return;
923
924 CU.createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(
925 cast<DILocalScope>(ScopeNode)));
926 }
927
ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit & CU,InlinedVariable IV,const MDNode * ScopeNode)928 void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit &CU,
929 InlinedVariable IV, const MDNode *ScopeNode) {
930 const DILocalVariable *Cleansed = nullptr;
931 if (CU.getExistingAbstractVariable(IV, Cleansed))
932 return;
933
934 if (LexicalScope *Scope =
935 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
936 CU.createAbstractVariable(Cleansed, Scope);
937 }
938
939 // Collect variable information from side table maintained by MF.
collectVariableInfoFromMFTable(DwarfCompileUnit & TheCU,DenseSet<InlinedVariable> & Processed)940 void DwarfDebug::collectVariableInfoFromMFTable(
941 DwarfCompileUnit &TheCU, DenseSet<InlinedVariable> &Processed) {
942 SmallDenseMap<InlinedVariable, DbgVariable *> MFVars;
943 for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
944 if (!VI.Var)
945 continue;
946 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
947 "Expected inlined-at fields to agree");
948
949 InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt());
950 Processed.insert(Var);
951 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
952
953 // If variable scope is not found then skip this variable.
954 if (!Scope)
955 continue;
956
957 ensureAbstractVariableIsCreatedIfScoped(TheCU, Var, Scope->getScopeNode());
958 auto RegVar = llvm::make_unique<DbgVariable>(Var.first, Var.second);
959 RegVar->initializeMMI(VI.Expr, VI.Slot);
960 if (DbgVariable *DbgVar = MFVars.lookup(Var))
961 DbgVar->addMMIEntry(*RegVar);
962 else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) {
963 MFVars.insert({Var, RegVar.get()});
964 ConcreteVariables.push_back(std::move(RegVar));
965 }
966 }
967 }
968
969 // Get .debug_loc entry for the instruction range starting at MI.
getDebugLocValue(const MachineInstr * MI)970 static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
971 const DIExpression *Expr = MI->getDebugExpression();
972 assert(MI->getNumOperands() == 4);
973 if (MI->getOperand(0).isReg()) {
974 auto RegOp = MI->getOperand(0);
975 auto Op1 = MI->getOperand(1);
976 // If the second operand is an immediate, this is a
977 // register-indirect address.
978 assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset");
979 MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
980 return DebugLocEntry::Value(Expr, MLoc);
981 }
982 if (MI->getOperand(0).isImm())
983 return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
984 if (MI->getOperand(0).isFPImm())
985 return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
986 if (MI->getOperand(0).isCImm())
987 return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
988
989 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
990 }
991
992 /// If this and Next are describing different fragments of the same
993 /// variable, merge them by appending Next's values to the current
994 /// list of values.
995 /// Return true if the merge was successful.
MergeValues(const DebugLocEntry & Next)996 bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) {
997 if (Begin == Next.Begin) {
998 auto *FirstExpr = cast<DIExpression>(Values[0].Expression);
999 auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression);
1000 if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment())
1001 return false;
1002
1003 // We can only merge entries if none of the fragments overlap any others.
1004 // In doing so, we can take advantage of the fact that both lists are
1005 // sorted.
1006 for (unsigned i = 0, j = 0; i < Values.size(); ++i) {
1007 for (; j < Next.Values.size(); ++j) {
1008 int res = cast<DIExpression>(Values[i].Expression)->fragmentCmp(
1009 cast<DIExpression>(Next.Values[j].Expression));
1010 if (res == 0) // The two expressions overlap, we can't merge.
1011 return false;
1012 // Values[i] is entirely before Next.Values[j],
1013 // so go back to the next entry of Values.
1014 else if (res == -1)
1015 break;
1016 // Next.Values[j] is entirely before Values[i], so go on to the
1017 // next entry of Next.Values.
1018 }
1019 }
1020
1021 addValues(Next.Values);
1022 End = Next.End;
1023 return true;
1024 }
1025 return false;
1026 }
1027
1028 /// Build the location list for all DBG_VALUEs in the function that
1029 /// describe the same variable. If the ranges of several independent
1030 /// fragments of the same variable overlap partially, split them up and
1031 /// combine the ranges. The resulting DebugLocEntries are will have
1032 /// strict monotonically increasing begin addresses and will never
1033 /// overlap.
1034 //
1035 // Input:
1036 //
1037 // Ranges History [var, loc, fragment ofs size]
1038 // 0 | [x, (reg0, fragment 0, 32)]
1039 // 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry
1040 // 2 | | ...
1041 // 3 | [clobber reg0]
1042 // 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of
1043 // x.
1044 //
1045 // Output:
1046 //
1047 // [0-1] [x, (reg0, fragment 0, 32)]
1048 // [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)]
1049 // [3-4] [x, (reg1, fragment 32, 32)]
1050 // [4- ] [x, (mem, fragment 0, 64)]
1051 void
buildLocationList(SmallVectorImpl<DebugLocEntry> & DebugLoc,const DbgValueHistoryMap::InstrRanges & Ranges)1052 DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
1053 const DbgValueHistoryMap::InstrRanges &Ranges) {
1054 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
1055
1056 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1057 const MachineInstr *Begin = I->first;
1058 const MachineInstr *End = I->second;
1059 assert(Begin->isDebugValue() && "Invalid History entry");
1060
1061 // Check if a variable is inaccessible in this range.
1062 if (Begin->getNumOperands() > 1 &&
1063 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
1064 OpenRanges.clear();
1065 continue;
1066 }
1067
1068 // If this fragment overlaps with any open ranges, truncate them.
1069 const DIExpression *DIExpr = Begin->getDebugExpression();
1070 auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) {
1071 return DIExpr->fragmentsOverlap(R.getExpression());
1072 });
1073 OpenRanges.erase(Last, OpenRanges.end());
1074
1075 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
1076 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
1077
1078 const MCSymbol *EndLabel;
1079 if (End != nullptr)
1080 EndLabel = getLabelAfterInsn(End);
1081 else if (std::next(I) == Ranges.end())
1082 EndLabel = Asm->getFunctionEnd();
1083 else
1084 EndLabel = getLabelBeforeInsn(std::next(I)->first);
1085 assert(EndLabel && "Forgot label after instruction ending a range!");
1086
1087 LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
1088
1089 auto Value = getDebugLocValue(Begin);
1090 DebugLocEntry Loc(StartLabel, EndLabel, Value);
1091 bool couldMerge = false;
1092
1093 // If this is a fragment, it may belong to the current DebugLocEntry.
1094 if (DIExpr->isFragment()) {
1095 // Add this value to the list of open ranges.
1096 OpenRanges.push_back(Value);
1097
1098 // Attempt to add the fragment to the last entry.
1099 if (!DebugLoc.empty())
1100 if (DebugLoc.back().MergeValues(Loc))
1101 couldMerge = true;
1102 }
1103
1104 if (!couldMerge) {
1105 // Need to add a new DebugLocEntry. Add all values from still
1106 // valid non-overlapping fragments.
1107 if (OpenRanges.size())
1108 Loc.addValues(OpenRanges);
1109
1110 DebugLoc.push_back(std::move(Loc));
1111 }
1112
1113 // Attempt to coalesce the ranges of two otherwise identical
1114 // DebugLocEntries.
1115 auto CurEntry = DebugLoc.rbegin();
1116 LLVM_DEBUG({
1117 dbgs() << CurEntry->getValues().size() << " Values:\n";
1118 for (auto &Value : CurEntry->getValues())
1119 Value.dump();
1120 dbgs() << "-----\n";
1121 });
1122
1123 auto PrevEntry = std::next(CurEntry);
1124 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
1125 DebugLoc.pop_back();
1126 }
1127 }
1128
createConcreteVariable(DwarfCompileUnit & TheCU,LexicalScope & Scope,InlinedVariable IV)1129 DbgVariable *DwarfDebug::createConcreteVariable(DwarfCompileUnit &TheCU,
1130 LexicalScope &Scope,
1131 InlinedVariable IV) {
1132 ensureAbstractVariableIsCreatedIfScoped(TheCU, IV, Scope.getScopeNode());
1133 ConcreteVariables.push_back(
1134 llvm::make_unique<DbgVariable>(IV.first, IV.second));
1135 InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get());
1136 return ConcreteVariables.back().get();
1137 }
1138
1139 /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1140 /// enclosing lexical scope. The check ensures there are no other instructions
1141 /// in the same lexical scope preceding the DBG_VALUE and that its range is
1142 /// either open or otherwise rolls off the end of the scope.
validThroughout(LexicalScopes & LScopes,const MachineInstr * DbgValue,const MachineInstr * RangeEnd)1143 static bool validThroughout(LexicalScopes &LScopes,
1144 const MachineInstr *DbgValue,
1145 const MachineInstr *RangeEnd) {
1146 assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
1147 auto MBB = DbgValue->getParent();
1148 auto DL = DbgValue->getDebugLoc();
1149 auto *LScope = LScopes.findLexicalScope(DL);
1150 // Scope doesn't exist; this is a dead DBG_VALUE.
1151 if (!LScope)
1152 return false;
1153 auto &LSRange = LScope->getRanges();
1154 if (LSRange.size() == 0)
1155 return false;
1156
1157 // Determine if the DBG_VALUE is valid at the beginning of its lexical block.
1158 const MachineInstr *LScopeBegin = LSRange.front().first;
1159 // Early exit if the lexical scope begins outside of the current block.
1160 if (LScopeBegin->getParent() != MBB)
1161 return false;
1162 MachineBasicBlock::const_reverse_iterator Pred(DbgValue);
1163 for (++Pred; Pred != MBB->rend(); ++Pred) {
1164 if (Pred->getFlag(MachineInstr::FrameSetup))
1165 break;
1166 auto PredDL = Pred->getDebugLoc();
1167 if (!PredDL || Pred->isMetaInstruction())
1168 continue;
1169 // Check whether the instruction preceding the DBG_VALUE is in the same
1170 // (sub)scope as the DBG_VALUE.
1171 if (DL->getScope() == PredDL->getScope())
1172 return false;
1173 auto *PredScope = LScopes.findLexicalScope(PredDL);
1174 if (!PredScope || LScope->dominates(PredScope))
1175 return false;
1176 }
1177
1178 // If the range of the DBG_VALUE is open-ended, report success.
1179 if (!RangeEnd)
1180 return true;
1181
1182 // Fail if there are instructions belonging to our scope in another block.
1183 const MachineInstr *LScopeEnd = LSRange.back().second;
1184 if (LScopeEnd->getParent() != MBB)
1185 return false;
1186
1187 // Single, constant DBG_VALUEs in the prologue are promoted to be live
1188 // throughout the function. This is a hack, presumably for DWARF v2 and not
1189 // necessarily correct. It would be much better to use a dbg.declare instead
1190 // if we know the constant is live throughout the scope.
1191 if (DbgValue->getOperand(0).isImm() && MBB->pred_empty())
1192 return true;
1193
1194 return false;
1195 }
1196
1197 // Find variables for each lexical scope.
collectVariableInfo(DwarfCompileUnit & TheCU,const DISubprogram * SP,DenseSet<InlinedVariable> & Processed)1198 void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
1199 const DISubprogram *SP,
1200 DenseSet<InlinedVariable> &Processed) {
1201 // Grab the variable info that was squirreled away in the MMI side-table.
1202 collectVariableInfoFromMFTable(TheCU, Processed);
1203
1204 for (const auto &I : DbgValues) {
1205 InlinedVariable IV = I.first;
1206 if (Processed.count(IV))
1207 continue;
1208
1209 // Instruction ranges, specifying where IV is accessible.
1210 const auto &Ranges = I.second;
1211 if (Ranges.empty())
1212 continue;
1213
1214 LexicalScope *Scope = nullptr;
1215 if (const DILocation *IA = IV.second)
1216 Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
1217 else
1218 Scope = LScopes.findLexicalScope(IV.first->getScope());
1219 // If variable scope is not found then skip this variable.
1220 if (!Scope)
1221 continue;
1222
1223 Processed.insert(IV);
1224 DbgVariable *RegVar = createConcreteVariable(TheCU, *Scope, IV);
1225
1226 const MachineInstr *MInsn = Ranges.front().first;
1227 assert(MInsn->isDebugValue() && "History must begin with debug value");
1228
1229 // Check if there is a single DBG_VALUE, valid throughout the var's scope.
1230 if (Ranges.size() == 1 &&
1231 validThroughout(LScopes, MInsn, Ranges.front().second)) {
1232 RegVar->initializeDbgValue(MInsn);
1233 continue;
1234 }
1235 // Do not emit location lists if .debug_loc secton is disabled.
1236 if (!useLocSection())
1237 continue;
1238
1239 // Handle multiple DBG_VALUE instructions describing one variable.
1240 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
1241
1242 // Build the location list for this variable.
1243 SmallVector<DebugLocEntry, 8> Entries;
1244 buildLocationList(Entries, Ranges);
1245
1246 // If the variable has a DIBasicType, extract it. Basic types cannot have
1247 // unique identifiers, so don't bother resolving the type with the
1248 // identifier map.
1249 const DIBasicType *BT = dyn_cast<DIBasicType>(
1250 static_cast<const Metadata *>(IV.first->getType()));
1251
1252 // Finalize the entry by lowering it into a DWARF bytestream.
1253 for (auto &Entry : Entries)
1254 Entry.finalize(*Asm, List, BT);
1255 }
1256
1257 // Collect info for variables that were optimized out.
1258 for (const DINode *DN : SP->getRetainedNodes()) {
1259 if (auto *DV = dyn_cast<DILocalVariable>(DN)) {
1260 if (Processed.insert(InlinedVariable(DV, nullptr)).second)
1261 if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
1262 createConcreteVariable(TheCU, *Scope, InlinedVariable(DV, nullptr));
1263 }
1264 }
1265 }
1266
1267 // Process beginning of an instruction.
beginInstruction(const MachineInstr * MI)1268 void DwarfDebug::beginInstruction(const MachineInstr *MI) {
1269 DebugHandlerBase::beginInstruction(MI);
1270 assert(CurMI);
1271
1272 const auto *SP = MI->getMF()->getFunction().getSubprogram();
1273 if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1274 return;
1275
1276 // Check if source location changes, but ignore DBG_VALUE and CFI locations.
1277 // If the instruction is part of the function frame setup code, do not emit
1278 // any line record, as there is no correspondence with any user code.
1279 if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup))
1280 return;
1281 const DebugLoc &DL = MI->getDebugLoc();
1282 // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1283 // the last line number actually emitted, to see if it was line 0.
1284 unsigned LastAsmLine =
1285 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
1286
1287 if (DL == PrevInstLoc) {
1288 // If we have an ongoing unspecified location, nothing to do here.
1289 if (!DL)
1290 return;
1291 // We have an explicit location, same as the previous location.
1292 // But we might be coming back to it after a line 0 record.
1293 if (LastAsmLine == 0 && DL.getLine() != 0) {
1294 // Reinstate the source location but not marked as a statement.
1295 const MDNode *Scope = DL.getScope();
1296 recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0);
1297 }
1298 return;
1299 }
1300
1301 if (!DL) {
1302 // We have an unspecified location, which might want to be line 0.
1303 // If we have already emitted a line-0 record, don't repeat it.
1304 if (LastAsmLine == 0)
1305 return;
1306 // If user said Don't Do That, don't do that.
1307 if (UnknownLocations == Disable)
1308 return;
1309 // See if we have a reason to emit a line-0 record now.
1310 // Reasons to emit a line-0 record include:
1311 // - User asked for it (UnknownLocations).
1312 // - Instruction has a label, so it's referenced from somewhere else,
1313 // possibly debug information; we want it to have a source location.
1314 // - Instruction is at the top of a block; we don't want to inherit the
1315 // location from the physically previous (maybe unrelated) block.
1316 if (UnknownLocations == Enable || PrevLabel ||
1317 (PrevInstBB && PrevInstBB != MI->getParent())) {
1318 // Preserve the file and column numbers, if we can, to save space in
1319 // the encoded line table.
1320 // Do not update PrevInstLoc, it remembers the last non-0 line.
1321 const MDNode *Scope = nullptr;
1322 unsigned Column = 0;
1323 if (PrevInstLoc) {
1324 Scope = PrevInstLoc.getScope();
1325 Column = PrevInstLoc.getCol();
1326 }
1327 recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
1328 }
1329 return;
1330 }
1331
1332 // We have an explicit location, different from the previous location.
1333 // Don't repeat a line-0 record, but otherwise emit the new location.
1334 // (The new location might be an explicit line 0, which we do emit.)
1335 if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0)
1336 return;
1337 unsigned Flags = 0;
1338 if (DL == PrologEndLoc) {
1339 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
1340 PrologEndLoc = DebugLoc();
1341 }
1342 // If the line changed, we call that a new statement; unless we went to
1343 // line 0 and came back, in which case it is not a new statement.
1344 unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
1345 if (DL.getLine() && DL.getLine() != OldLine)
1346 Flags |= DWARF2_FLAG_IS_STMT;
1347
1348 const MDNode *Scope = DL.getScope();
1349 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
1350
1351 // If we're not at line 0, remember this location.
1352 if (DL.getLine())
1353 PrevInstLoc = DL;
1354 }
1355
findPrologueEndLoc(const MachineFunction * MF)1356 static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1357 // First known non-DBG_VALUE and non-frame setup location marks
1358 // the beginning of the function body.
1359 for (const auto &MBB : *MF)
1360 for (const auto &MI : MBB)
1361 if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
1362 MI.getDebugLoc())
1363 return MI.getDebugLoc();
1364 return DebugLoc();
1365 }
1366
1367 // Gather pre-function debug information. Assumes being called immediately
1368 // after the function entry point has been emitted.
beginFunctionImpl(const MachineFunction * MF)1369 void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
1370 CurFn = MF;
1371
1372 auto *SP = MF->getFunction().getSubprogram();
1373 assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode());
1374 if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1375 return;
1376
1377 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
1378
1379 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1380 // belongs to so that we add to the correct per-cu line table in the
1381 // non-asm case.
1382 if (Asm->OutStreamer->hasRawTextSupport())
1383 // Use a single line table if we are generating assembly.
1384 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1385 else
1386 Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID());
1387
1388 // Record beginning of function.
1389 PrologEndLoc = findPrologueEndLoc(MF);
1390 if (PrologEndLoc) {
1391 // We'd like to list the prologue as "not statements" but GDB behaves
1392 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1393 auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
1394 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
1395 }
1396 }
1397
skippedNonDebugFunction()1398 void DwarfDebug::skippedNonDebugFunction() {
1399 // If we don't have a subprogram for this function then there will be a hole
1400 // in the range information. Keep note of this by setting the previously used
1401 // section to nullptr.
1402 PrevCU = nullptr;
1403 CurFn = nullptr;
1404 }
1405
1406 // Gather and emit post-function debug information.
endFunctionImpl(const MachineFunction * MF)1407 void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
1408 const DISubprogram *SP = MF->getFunction().getSubprogram();
1409
1410 assert(CurFn == MF &&
1411 "endFunction should be called with the same function as beginFunction");
1412
1413 // Set DwarfDwarfCompileUnitID in MCContext to default value.
1414 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
1415
1416 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1417 assert(!FnScope || SP == FnScope->getScopeNode());
1418 DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit());
1419
1420 DenseSet<InlinedVariable> ProcessedVars;
1421 collectVariableInfo(TheCU, SP, ProcessedVars);
1422
1423 // Add the range of this function to the list of ranges for the CU.
1424 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
1425
1426 // Under -gmlt, skip building the subprogram if there are no inlined
1427 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
1428 // is still needed as we need its source location.
1429 if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
1430 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
1431 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1432 assert(InfoHolder.getScopeVariables().empty());
1433 PrevLabel = nullptr;
1434 CurFn = nullptr;
1435 return;
1436 }
1437
1438 #ifndef NDEBUG
1439 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1440 #endif
1441 // Construct abstract scopes.
1442 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
1443 auto *SP = cast<DISubprogram>(AScope->getScopeNode());
1444 for (const DINode *DN : SP->getRetainedNodes()) {
1445 if (auto *DV = dyn_cast<DILocalVariable>(DN)) {
1446 // Collect info for variables that were optimized out.
1447 if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
1448 continue;
1449 ensureAbstractVariableIsCreated(TheCU, InlinedVariable(DV, nullptr),
1450 DV->getScope());
1451 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1452 && "ensureAbstractVariableIsCreated inserted abstract scopes");
1453 }
1454 }
1455 constructAbstractSubprogramScopeDIE(TheCU, AScope);
1456 }
1457
1458 ProcessedSPNodes.insert(SP);
1459 TheCU.constructSubprogramScopeDIE(SP, FnScope);
1460 if (auto *SkelCU = TheCU.getSkeleton())
1461 if (!LScopes.getAbstractScopesList().empty() &&
1462 TheCU.getCUNode()->getSplitDebugInlining())
1463 SkelCU->constructSubprogramScopeDIE(SP, FnScope);
1464
1465 // Clear debug info
1466 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1467 // DbgVariables except those that are also in AbstractVariables (since they
1468 // can be used cross-function)
1469 InfoHolder.getScopeVariables().clear();
1470 PrevLabel = nullptr;
1471 CurFn = nullptr;
1472 }
1473
1474 // Register a source line with debug info. Returns the unique label that was
1475 // emitted and which provides correspondence to the source line list.
recordSourceLine(unsigned Line,unsigned Col,const MDNode * S,unsigned Flags)1476 void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1477 unsigned Flags) {
1478 StringRef Fn;
1479 unsigned FileNo = 1;
1480 unsigned Discriminator = 0;
1481 if (auto *Scope = cast_or_null<DIScope>(S)) {
1482 Fn = Scope->getFilename();
1483 if (Line != 0 && getDwarfVersion() >= 4)
1484 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
1485 Discriminator = LBF->getDiscriminator();
1486
1487 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
1488 FileNo = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1489 .getOrCreateSourceID(Scope->getFile());
1490 }
1491 Asm->OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
1492 Discriminator, Fn);
1493 }
1494
1495 //===----------------------------------------------------------------------===//
1496 // Emit Methods
1497 //===----------------------------------------------------------------------===//
1498
1499 // Emit the debug info section.
emitDebugInfo()1500 void DwarfDebug::emitDebugInfo() {
1501 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1502 Holder.emitUnits(/* UseOffsets */ false);
1503 }
1504
1505 // Emit the abbreviation section.
emitAbbreviations()1506 void DwarfDebug::emitAbbreviations() {
1507 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1508
1509 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
1510 }
1511
emitStringOffsetsTableHeader()1512 void DwarfDebug::emitStringOffsetsTableHeader() {
1513 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1514 Holder.getStringPool().emitStringOffsetsTableHeader(
1515 *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(),
1516 Holder.getStringOffsetsStartSym());
1517 }
1518
1519 template <typename AccelTableT>
emitAccel(AccelTableT & Accel,MCSection * Section,StringRef TableName)1520 void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section,
1521 StringRef TableName) {
1522 Asm->OutStreamer->SwitchSection(Section);
1523
1524 // Emit the full data.
1525 emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol());
1526 }
1527
emitAccelDebugNames()1528 void DwarfDebug::emitAccelDebugNames() {
1529 // Don't emit anything if we have no compilation units to index.
1530 if (getUnits().empty())
1531 return;
1532
1533 Asm->OutStreamer->SwitchSection(
1534 Asm->getObjFileLowering().getDwarfDebugNamesSection());
1535 emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits());
1536 }
1537
1538 // Emit visible names into a hashed accelerator table section.
emitAccelNames()1539 void DwarfDebug::emitAccelNames() {
1540 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1541 "Names");
1542 }
1543
1544 // Emit objective C classes and categories into a hashed accelerator table
1545 // section.
emitAccelObjC()1546 void DwarfDebug::emitAccelObjC() {
1547 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1548 "ObjC");
1549 }
1550
1551 // Emit namespace dies into a hashed accelerator table.
emitAccelNamespaces()1552 void DwarfDebug::emitAccelNamespaces() {
1553 emitAccel(AccelNamespace,
1554 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
1555 "namespac");
1556 }
1557
1558 // Emit type dies into a hashed accelerator table.
emitAccelTypes()1559 void DwarfDebug::emitAccelTypes() {
1560 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1561 "types");
1562 }
1563
1564 // Public name handling.
1565 // The format for the various pubnames:
1566 //
1567 // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1568 // for the DIE that is named.
1569 //
1570 // gnu pubnames - offset/index value/name tuples where the offset is the offset
1571 // into the CU and the index value is computed according to the type of value
1572 // for the DIE that is named.
1573 //
1574 // For type units the offset is the offset of the skeleton DIE. For split dwarf
1575 // it's the offset within the debug_info/debug_types dwo section, however, the
1576 // reference in the pubname header doesn't change.
1577
1578 /// computeIndexValue - Compute the gdb index value for the DIE and CU.
computeIndexValue(DwarfUnit * CU,const DIE * Die)1579 static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
1580 const DIE *Die) {
1581 // Entities that ended up only in a Type Unit reference the CU instead (since
1582 // the pub entry has offsets within the CU there's no real offset that can be
1583 // provided anyway). As it happens all such entities (namespaces and types,
1584 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
1585 // not to be true it would be necessary to persist this information from the
1586 // point at which the entry is added to the index data structure - since by
1587 // the time the index is built from that, the original type/namespace DIE in a
1588 // type unit has already been destroyed so it can't be queried for properties
1589 // like tag, etc.
1590 if (Die->getTag() == dwarf::DW_TAG_compile_unit)
1591 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
1592 dwarf::GIEL_EXTERNAL);
1593 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1594
1595 // We could have a specification DIE that has our most of our knowledge,
1596 // look for that now.
1597 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1598 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
1599 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
1600 Linkage = dwarf::GIEL_EXTERNAL;
1601 } else if (Die->findAttribute(dwarf::DW_AT_external))
1602 Linkage = dwarf::GIEL_EXTERNAL;
1603
1604 switch (Die->getTag()) {
1605 case dwarf::DW_TAG_class_type:
1606 case dwarf::DW_TAG_structure_type:
1607 case dwarf::DW_TAG_union_type:
1608 case dwarf::DW_TAG_enumeration_type:
1609 return dwarf::PubIndexEntryDescriptor(
1610 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1611 ? dwarf::GIEL_STATIC
1612 : dwarf::GIEL_EXTERNAL);
1613 case dwarf::DW_TAG_typedef:
1614 case dwarf::DW_TAG_base_type:
1615 case dwarf::DW_TAG_subrange_type:
1616 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
1617 case dwarf::DW_TAG_namespace:
1618 return dwarf::GIEK_TYPE;
1619 case dwarf::DW_TAG_subprogram:
1620 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
1621 case dwarf::DW_TAG_variable:
1622 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
1623 case dwarf::DW_TAG_enumerator:
1624 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1625 dwarf::GIEL_STATIC);
1626 default:
1627 return dwarf::GIEK_NONE;
1628 }
1629 }
1630
1631 /// emitDebugPubSections - Emit visible names and types into debug pubnames and
1632 /// pubtypes sections.
emitDebugPubSections()1633 void DwarfDebug::emitDebugPubSections() {
1634 for (const auto &NU : CUMap) {
1635 DwarfCompileUnit *TheU = NU.second;
1636 if (!TheU->hasDwarfPubSections())
1637 continue;
1638
1639 bool GnuStyle = TheU->getCUNode()->getGnuPubnames();
1640
1641 Asm->OutStreamer->SwitchSection(
1642 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1643 : Asm->getObjFileLowering().getDwarfPubNamesSection());
1644 emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames());
1645
1646 Asm->OutStreamer->SwitchSection(
1647 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1648 : Asm->getObjFileLowering().getDwarfPubTypesSection());
1649 emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes());
1650 }
1651 }
1652
emitSectionReference(const DwarfCompileUnit & CU)1653 void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) {
1654 if (useSectionsAsReferences())
1655 Asm->EmitDwarfOffset(CU.getSection()->getBeginSymbol(),
1656 CU.getDebugSectionOffset());
1657 else
1658 Asm->emitDwarfSymbolReference(CU.getLabelBegin());
1659 }
1660
emitDebugPubSection(bool GnuStyle,StringRef Name,DwarfCompileUnit * TheU,const StringMap<const DIE * > & Globals)1661 void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
1662 DwarfCompileUnit *TheU,
1663 const StringMap<const DIE *> &Globals) {
1664 if (auto *Skeleton = TheU->getSkeleton())
1665 TheU = Skeleton;
1666
1667 // Emit the header.
1668 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1669 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1670 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1671 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1672
1673 Asm->OutStreamer->EmitLabel(BeginLabel);
1674
1675 Asm->OutStreamer->AddComment("DWARF Version");
1676 Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION);
1677
1678 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
1679 emitSectionReference(*TheU);
1680
1681 Asm->OutStreamer->AddComment("Compilation Unit Length");
1682 Asm->emitInt32(TheU->getLength());
1683
1684 // Emit the pubnames for this compilation unit.
1685 for (const auto &GI : Globals) {
1686 const char *Name = GI.getKeyData();
1687 const DIE *Entity = GI.second;
1688
1689 Asm->OutStreamer->AddComment("DIE offset");
1690 Asm->emitInt32(Entity->getOffset());
1691
1692 if (GnuStyle) {
1693 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
1694 Asm->OutStreamer->AddComment(
1695 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1696 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
1697 Asm->emitInt8(Desc.toBits());
1698 }
1699
1700 Asm->OutStreamer->AddComment("External Name");
1701 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1702 }
1703
1704 Asm->OutStreamer->AddComment("End Mark");
1705 Asm->emitInt32(0);
1706 Asm->OutStreamer->EmitLabel(EndLabel);
1707 }
1708
1709 /// Emit null-terminated strings into a debug str section.
emitDebugStr()1710 void DwarfDebug::emitDebugStr() {
1711 MCSection *StringOffsetsSection = nullptr;
1712 if (useSegmentedStringOffsetsTable()) {
1713 emitStringOffsetsTableHeader();
1714 StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection();
1715 }
1716 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1717 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(),
1718 StringOffsetsSection, /* UseRelativeOffsets = */ true);
1719 }
1720
emitDebugLocEntry(ByteStreamer & Streamer,const DebugLocStream::Entry & Entry)1721 void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
1722 const DebugLocStream::Entry &Entry) {
1723 auto &&Comments = DebugLocs.getComments(Entry);
1724 auto Comment = Comments.begin();
1725 auto End = Comments.end();
1726 for (uint8_t Byte : DebugLocs.getBytes(Entry))
1727 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
1728 }
1729
emitDebugLocValue(const AsmPrinter & AP,const DIBasicType * BT,const DebugLocEntry::Value & Value,DwarfExpression & DwarfExpr)1730 static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
1731 const DebugLocEntry::Value &Value,
1732 DwarfExpression &DwarfExpr) {
1733 auto *DIExpr = Value.getExpression();
1734 DIExpressionCursor ExprCursor(DIExpr);
1735 DwarfExpr.addFragmentOffset(DIExpr);
1736 // Regular entry.
1737 if (Value.isInt()) {
1738 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1739 BT->getEncoding() == dwarf::DW_ATE_signed_char))
1740 DwarfExpr.addSignedConstant(Value.getInt());
1741 else
1742 DwarfExpr.addUnsignedConstant(Value.getInt());
1743 } else if (Value.isLocation()) {
1744 MachineLocation Location = Value.getLoc();
1745 if (Location.isIndirect())
1746 DwarfExpr.setMemoryLocationKind();
1747 DIExpressionCursor Cursor(DIExpr);
1748 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
1749 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
1750 return;
1751 return DwarfExpr.addExpression(std::move(Cursor));
1752 } else if (Value.isConstantFP()) {
1753 APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
1754 DwarfExpr.addUnsignedConstant(RawBytes);
1755 }
1756 DwarfExpr.addExpression(std::move(ExprCursor));
1757 }
1758
finalize(const AsmPrinter & AP,DebugLocStream::ListBuilder & List,const DIBasicType * BT)1759 void DebugLocEntry::finalize(const AsmPrinter &AP,
1760 DebugLocStream::ListBuilder &List,
1761 const DIBasicType *BT) {
1762 DebugLocStream::EntryBuilder Entry(List, Begin, End);
1763 BufferByteStreamer Streamer = Entry.getStreamer();
1764 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer);
1765 const DebugLocEntry::Value &Value = Values[0];
1766 if (Value.isFragment()) {
1767 // Emit all fragments that belong to the same variable and range.
1768 assert(llvm::all_of(Values, [](DebugLocEntry::Value P) {
1769 return P.isFragment();
1770 }) && "all values are expected to be fragments");
1771 assert(std::is_sorted(Values.begin(), Values.end()) &&
1772 "fragments are expected to be sorted");
1773
1774 for (auto Fragment : Values)
1775 emitDebugLocValue(AP, BT, Fragment, DwarfExpr);
1776
1777 } else {
1778 assert(Values.size() == 1 && "only fragments may have >1 value");
1779 emitDebugLocValue(AP, BT, Value, DwarfExpr);
1780 }
1781 DwarfExpr.finalize();
1782 }
1783
emitDebugLocEntryLocation(const DebugLocStream::Entry & Entry)1784 void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
1785 // Emit the size.
1786 Asm->OutStreamer->AddComment("Loc expr size");
1787 Asm->emitInt16(DebugLocs.getBytes(Entry).size());
1788
1789 // Emit the entry.
1790 APByteStreamer Streamer(*Asm);
1791 emitDebugLocEntry(Streamer, Entry);
1792 }
1793
1794 // Emit locations into the debug loc section.
emitDebugLoc()1795 void DwarfDebug::emitDebugLoc() {
1796 if (DebugLocs.getLists().empty())
1797 return;
1798
1799 // Start the dwarf loc section.
1800 Asm->OutStreamer->SwitchSection(
1801 Asm->getObjFileLowering().getDwarfLocSection());
1802 unsigned char Size = Asm->MAI->getCodePointerSize();
1803 for (const auto &List : DebugLocs.getLists()) {
1804 Asm->OutStreamer->EmitLabel(List.Label);
1805 const DwarfCompileUnit *CU = List.CU;
1806 for (const auto &Entry : DebugLocs.getEntries(List)) {
1807 // Set up the range. This range is relative to the entry point of the
1808 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1809 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
1810 if (auto *Base = CU->getBaseAddress()) {
1811 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1812 Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
1813 } else {
1814 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1815 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
1816 }
1817
1818 emitDebugLocEntryLocation(Entry);
1819 }
1820 Asm->OutStreamer->EmitIntValue(0, Size);
1821 Asm->OutStreamer->EmitIntValue(0, Size);
1822 }
1823 }
1824
emitDebugLocDWO()1825 void DwarfDebug::emitDebugLocDWO() {
1826 Asm->OutStreamer->SwitchSection(
1827 Asm->getObjFileLowering().getDwarfLocDWOSection());
1828 for (const auto &List : DebugLocs.getLists()) {
1829 Asm->OutStreamer->EmitLabel(List.Label);
1830 for (const auto &Entry : DebugLocs.getEntries(List)) {
1831 // Just always use start_length for now - at least that's one address
1832 // rather than two. We could get fancier and try to, say, reuse an
1833 // address we know we've emitted elsewhere (the start of the function?
1834 // The start of the CU or CU subrange that encloses this range?)
1835 Asm->emitInt8(dwarf::DW_LLE_startx_length);
1836 unsigned idx = AddrPool.getIndex(Entry.BeginSym);
1837 Asm->EmitULEB128(idx);
1838 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
1839
1840 emitDebugLocEntryLocation(Entry);
1841 }
1842 Asm->emitInt8(dwarf::DW_LLE_end_of_list);
1843 }
1844 }
1845
1846 struct ArangeSpan {
1847 const MCSymbol *Start, *End;
1848 };
1849
1850 // Emit a debug aranges section, containing a CU lookup for any
1851 // address we can tie back to a CU.
emitDebugARanges()1852 void DwarfDebug::emitDebugARanges() {
1853 // Provides a unique id per text section.
1854 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
1855
1856 // Filter labels by section.
1857 for (const SymbolCU &SCU : ArangeLabels) {
1858 if (SCU.Sym->isInSection()) {
1859 // Make a note of this symbol and it's section.
1860 MCSection *Section = &SCU.Sym->getSection();
1861 if (!Section->getKind().isMetadata())
1862 SectionMap[Section].push_back(SCU);
1863 } else {
1864 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1865 // appear in the output. This sucks as we rely on sections to build
1866 // arange spans. We can do it without, but it's icky.
1867 SectionMap[nullptr].push_back(SCU);
1868 }
1869 }
1870
1871 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
1872
1873 for (auto &I : SectionMap) {
1874 MCSection *Section = I.first;
1875 SmallVector<SymbolCU, 8> &List = I.second;
1876 if (List.size() < 1)
1877 continue;
1878
1879 // If we have no section (e.g. common), just write out
1880 // individual spans for each symbol.
1881 if (!Section) {
1882 for (const SymbolCU &Cur : List) {
1883 ArangeSpan Span;
1884 Span.Start = Cur.Sym;
1885 Span.End = nullptr;
1886 assert(Cur.CU);
1887 Spans[Cur.CU].push_back(Span);
1888 }
1889 continue;
1890 }
1891
1892 // Sort the symbols by offset within the section.
1893 std::stable_sort(
1894 List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) {
1895 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1896 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
1897
1898 // Symbols with no order assigned should be placed at the end.
1899 // (e.g. section end labels)
1900 if (IA == 0)
1901 return false;
1902 if (IB == 0)
1903 return true;
1904 return IA < IB;
1905 });
1906
1907 // Insert a final terminator.
1908 List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
1909
1910 // Build spans between each label.
1911 const MCSymbol *StartSym = List[0].Sym;
1912 for (size_t n = 1, e = List.size(); n < e; n++) {
1913 const SymbolCU &Prev = List[n - 1];
1914 const SymbolCU &Cur = List[n];
1915
1916 // Try and build the longest span we can within the same CU.
1917 if (Cur.CU != Prev.CU) {
1918 ArangeSpan Span;
1919 Span.Start = StartSym;
1920 Span.End = Cur.Sym;
1921 assert(Prev.CU);
1922 Spans[Prev.CU].push_back(Span);
1923 StartSym = Cur.Sym;
1924 }
1925 }
1926 }
1927
1928 // Start the dwarf aranges section.
1929 Asm->OutStreamer->SwitchSection(
1930 Asm->getObjFileLowering().getDwarfARangesSection());
1931
1932 unsigned PtrSize = Asm->MAI->getCodePointerSize();
1933
1934 // Build a list of CUs used.
1935 std::vector<DwarfCompileUnit *> CUs;
1936 for (const auto &it : Spans) {
1937 DwarfCompileUnit *CU = it.first;
1938 CUs.push_back(CU);
1939 }
1940
1941 // Sort the CU list (again, to ensure consistent output order).
1942 llvm::sort(CUs.begin(), CUs.end(),
1943 [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
1944 return A->getUniqueID() < B->getUniqueID();
1945 });
1946
1947 // Emit an arange table for each CU we used.
1948 for (DwarfCompileUnit *CU : CUs) {
1949 std::vector<ArangeSpan> &List = Spans[CU];
1950
1951 // Describe the skeleton CU's offset and length, not the dwo file's.
1952 if (auto *Skel = CU->getSkeleton())
1953 CU = Skel;
1954
1955 // Emit size of content not including length itself.
1956 unsigned ContentSize =
1957 sizeof(int16_t) + // DWARF ARange version number
1958 sizeof(int32_t) + // Offset of CU in the .debug_info section
1959 sizeof(int8_t) + // Pointer Size (in bytes)
1960 sizeof(int8_t); // Segment Size (in bytes)
1961
1962 unsigned TupleSize = PtrSize * 2;
1963
1964 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
1965 unsigned Padding =
1966 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
1967
1968 ContentSize += Padding;
1969 ContentSize += (List.size() + 1) * TupleSize;
1970
1971 // For each compile unit, write the list of spans it covers.
1972 Asm->OutStreamer->AddComment("Length of ARange Set");
1973 Asm->emitInt32(ContentSize);
1974 Asm->OutStreamer->AddComment("DWARF Arange version number");
1975 Asm->emitInt16(dwarf::DW_ARANGES_VERSION);
1976 Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
1977 emitSectionReference(*CU);
1978 Asm->OutStreamer->AddComment("Address Size (in bytes)");
1979 Asm->emitInt8(PtrSize);
1980 Asm->OutStreamer->AddComment("Segment Size (in bytes)");
1981 Asm->emitInt8(0);
1982
1983 Asm->OutStreamer->emitFill(Padding, 0xff);
1984
1985 for (const ArangeSpan &Span : List) {
1986 Asm->EmitLabelReference(Span.Start, PtrSize);
1987
1988 // Calculate the size as being from the span start to it's end.
1989 if (Span.End) {
1990 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
1991 } else {
1992 // For symbols without an end marker (e.g. common), we
1993 // write a single arange entry containing just that one symbol.
1994 uint64_t Size = SymSize[Span.Start];
1995 if (Size == 0)
1996 Size = 1;
1997
1998 Asm->OutStreamer->EmitIntValue(Size, PtrSize);
1999 }
2000 }
2001
2002 Asm->OutStreamer->AddComment("ARange terminator");
2003 Asm->OutStreamer->EmitIntValue(0, PtrSize);
2004 Asm->OutStreamer->EmitIntValue(0, PtrSize);
2005 }
2006 }
2007
2008 /// Emit a single range list. We handle both DWARF v5 and earlier.
emitRangeList(AsmPrinter * Asm,DwarfCompileUnit * CU,const RangeSpanList & List)2009 static void emitRangeList(AsmPrinter *Asm, DwarfCompileUnit *CU,
2010 const RangeSpanList &List) {
2011
2012 auto DwarfVersion = CU->getDwarfVersion();
2013 // Emit our symbol so we can find the beginning of the range.
2014 Asm->OutStreamer->EmitLabel(List.getSym());
2015 // Gather all the ranges that apply to the same section so they can share
2016 // a base address entry.
2017 MapVector<const MCSection *, std::vector<const RangeSpan *>> SectionRanges;
2018 // Size for our labels.
2019 auto Size = Asm->MAI->getCodePointerSize();
2020
2021 for (const RangeSpan &Range : List.getRanges())
2022 SectionRanges[&Range.getStart()->getSection()].push_back(&Range);
2023
2024 auto *CUBase = CU->getBaseAddress();
2025 bool BaseIsSet = false;
2026 for (const auto &P : SectionRanges) {
2027 // Don't bother with a base address entry if there's only one range in
2028 // this section in this range list - for example ranges for a CU will
2029 // usually consist of single regions from each of many sections
2030 // (-ffunction-sections, or just C++ inline functions) except under LTO
2031 // or optnone where there may be holes in a single CU's section
2032 // contributions.
2033 auto *Base = CUBase;
2034 if (!Base && P.second.size() > 1 &&
2035 (UseDwarfRangesBaseAddressSpecifier || DwarfVersion >= 5)) {
2036 BaseIsSet = true;
2037 // FIXME/use care: This may not be a useful base address if it's not
2038 // the lowest address/range in this object.
2039 Base = P.second.front()->getStart();
2040 if (DwarfVersion >= 5) {
2041 Asm->OutStreamer->AddComment("DW_RLE_base_address");
2042 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_base_address, 1);
2043 } else
2044 Asm->OutStreamer->EmitIntValue(-1, Size);
2045 Asm->OutStreamer->AddComment(" base address");
2046 Asm->OutStreamer->EmitSymbolValue(Base, Size);
2047 } else if (BaseIsSet && DwarfVersion < 5) {
2048 BaseIsSet = false;
2049 assert(!Base);
2050 Asm->OutStreamer->EmitIntValue(-1, Size);
2051 Asm->OutStreamer->EmitIntValue(0, Size);
2052 }
2053
2054 for (const auto *RS : P.second) {
2055 const MCSymbol *Begin = RS->getStart();
2056 const MCSymbol *End = RS->getEnd();
2057 assert(Begin && "Range without a begin symbol?");
2058 assert(End && "Range without an end symbol?");
2059 if (Base) {
2060 if (DwarfVersion >= 5) {
2061 // Emit DW_RLE_offset_pair when we have a base.
2062 Asm->OutStreamer->AddComment("DW_RLE_offset_pair");
2063 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_offset_pair, 1);
2064 Asm->OutStreamer->AddComment(" starting offset");
2065 Asm->EmitLabelDifferenceAsULEB128(Begin, Base);
2066 Asm->OutStreamer->AddComment(" ending offset");
2067 Asm->EmitLabelDifferenceAsULEB128(End, Base);
2068 } else {
2069 Asm->EmitLabelDifference(Begin, Base, Size);
2070 Asm->EmitLabelDifference(End, Base, Size);
2071 }
2072 } else if (DwarfVersion >= 5) {
2073 Asm->OutStreamer->AddComment("DW_RLE_start_length");
2074 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_start_length, 1);
2075 Asm->OutStreamer->AddComment(" start");
2076 Asm->OutStreamer->EmitSymbolValue(Begin, Size);
2077 Asm->OutStreamer->AddComment(" length");
2078 Asm->EmitLabelDifferenceAsULEB128(End, Begin);
2079 } else {
2080 Asm->OutStreamer->EmitSymbolValue(Begin, Size);
2081 Asm->OutStreamer->EmitSymbolValue(End, Size);
2082 }
2083 }
2084 }
2085 if (DwarfVersion >= 5) {
2086 Asm->OutStreamer->AddComment("DW_RLE_end_of_list");
2087 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_end_of_list, 1);
2088 } else {
2089 // Terminate the list with two 0 values.
2090 Asm->OutStreamer->EmitIntValue(0, Size);
2091 Asm->OutStreamer->EmitIntValue(0, Size);
2092 }
2093 }
2094
2095 // Emit the header of a DWARF 5 range list table. Returns the symbol that
2096 // designates the end of the table for the caller to emit when the table is
2097 // complete.
emitRnglistsTableHeader(AsmPrinter * Asm,DwarfFile & Holder)2098 static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm, DwarfFile &Holder) {
2099 // The length is described by a starting label right after the length field
2100 // and an end label.
2101 MCSymbol *TableStart = Asm->createTempSymbol("debug_rnglist_table_start");
2102 MCSymbol *TableEnd = Asm->createTempSymbol("debug_rnglist_table_end");
2103 // Build the range table header, which starts with the length field.
2104 Asm->EmitLabelDifference(TableEnd, TableStart, 4);
2105 Asm->OutStreamer->EmitLabel(TableStart);
2106 // Version number (DWARF v5 and later).
2107 Asm->emitInt16(Asm->OutStreamer->getContext().getDwarfVersion());
2108 // Address size.
2109 Asm->emitInt8(Asm->MAI->getCodePointerSize());
2110 // Segment selector size.
2111 Asm->emitInt8(0);
2112
2113 MCSymbol *RnglistTableBaseSym = Holder.getRnglistsTableBaseSym();
2114
2115 // FIXME: Generate the offsets table and use DW_FORM_rnglistx with the
2116 // DW_AT_ranges attribute. Until then set the number of offsets to 0.
2117 Asm->emitInt32(0);
2118 Asm->OutStreamer->EmitLabel(RnglistTableBaseSym);
2119 return TableEnd;
2120 }
2121
2122 /// Emit address ranges into the .debug_ranges section or into the DWARF v5
2123 /// .debug_rnglists section.
emitDebugRanges()2124 void DwarfDebug::emitDebugRanges() {
2125 if (CUMap.empty())
2126 return;
2127
2128 auto NoRangesPresent = [this]() {
2129 return llvm::all_of(
2130 CUMap, [](const decltype(CUMap)::value_type &Pair) {
2131 return Pair.second->getRangeLists().empty();
2132 });
2133 };
2134
2135 if (!useRangesSection()) {
2136 assert(NoRangesPresent() && "No debug ranges expected.");
2137 return;
2138 }
2139
2140 if (NoRangesPresent())
2141 return;
2142
2143 // Start the dwarf ranges section.
2144 MCSymbol *TableEnd = nullptr;
2145 if (getDwarfVersion() >= 5) {
2146 Asm->OutStreamer->SwitchSection(
2147 Asm->getObjFileLowering().getDwarfRnglistsSection());
2148 TableEnd = emitRnglistsTableHeader(Asm, useSplitDwarf() ? SkeletonHolder
2149 : InfoHolder);
2150 } else
2151 Asm->OutStreamer->SwitchSection(
2152 Asm->getObjFileLowering().getDwarfRangesSection());
2153
2154 // Grab the specific ranges for the compile units in the module.
2155 for (const auto &I : CUMap) {
2156 DwarfCompileUnit *TheCU = I.second;
2157
2158 if (auto *Skel = TheCU->getSkeleton())
2159 TheCU = Skel;
2160
2161 // Iterate over the misc ranges for the compile units in the module.
2162 for (const RangeSpanList &List : TheCU->getRangeLists())
2163 emitRangeList(Asm, TheCU, List);
2164 }
2165
2166 if (TableEnd)
2167 Asm->OutStreamer->EmitLabel(TableEnd);
2168 }
2169
handleMacroNodes(DIMacroNodeArray Nodes,DwarfCompileUnit & U)2170 void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
2171 for (auto *MN : Nodes) {
2172 if (auto *M = dyn_cast<DIMacro>(MN))
2173 emitMacro(*M);
2174 else if (auto *F = dyn_cast<DIMacroFile>(MN))
2175 emitMacroFile(*F, U);
2176 else
2177 llvm_unreachable("Unexpected DI type!");
2178 }
2179 }
2180
emitMacro(DIMacro & M)2181 void DwarfDebug::emitMacro(DIMacro &M) {
2182 Asm->EmitULEB128(M.getMacinfoType());
2183 Asm->EmitULEB128(M.getLine());
2184 StringRef Name = M.getName();
2185 StringRef Value = M.getValue();
2186 Asm->OutStreamer->EmitBytes(Name);
2187 if (!Value.empty()) {
2188 // There should be one space between macro name and macro value.
2189 Asm->emitInt8(' ');
2190 Asm->OutStreamer->EmitBytes(Value);
2191 }
2192 Asm->emitInt8('\0');
2193 }
2194
emitMacroFile(DIMacroFile & F,DwarfCompileUnit & U)2195 void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
2196 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
2197 Asm->EmitULEB128(dwarf::DW_MACINFO_start_file);
2198 Asm->EmitULEB128(F.getLine());
2199 Asm->EmitULEB128(U.getOrCreateSourceID(F.getFile()));
2200 handleMacroNodes(F.getElements(), U);
2201 Asm->EmitULEB128(dwarf::DW_MACINFO_end_file);
2202 }
2203
2204 /// Emit macros into a debug macinfo section.
emitDebugMacinfo()2205 void DwarfDebug::emitDebugMacinfo() {
2206 if (CUMap.empty())
2207 return;
2208
2209 // Start the dwarf macinfo section.
2210 Asm->OutStreamer->SwitchSection(
2211 Asm->getObjFileLowering().getDwarfMacinfoSection());
2212
2213 for (const auto &P : CUMap) {
2214 auto &TheCU = *P.second;
2215 auto *SkCU = TheCU.getSkeleton();
2216 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
2217 auto *CUNode = cast<DICompileUnit>(P.first);
2218 DIMacroNodeArray Macros = CUNode->getMacros();
2219 if (!Macros.empty()) {
2220 Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin());
2221 handleMacroNodes(Macros, U);
2222 }
2223 }
2224 Asm->OutStreamer->AddComment("End Of Macro List Mark");
2225 Asm->emitInt8(0);
2226 }
2227
2228 // DWARF5 Experimental Separate Dwarf emitters.
2229
initSkeletonUnit(const DwarfUnit & U,DIE & Die,std::unique_ptr<DwarfCompileUnit> NewU)2230 void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
2231 std::unique_ptr<DwarfCompileUnit> NewU) {
2232 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
2233 Asm->TM.Options.MCOptions.SplitDwarfFile);
2234
2235 if (!CompilationDir.empty())
2236 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2237
2238 addGnuPubAttributes(*NewU, Die);
2239
2240 SkeletonHolder.addUnit(std::move(NewU));
2241 }
2242
constructSkeletonCU(const DwarfCompileUnit & CU)2243 DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
2244
2245 auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
2246 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
2247 DwarfCompileUnit &NewCU = *OwnedUnit;
2248 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
2249
2250 NewCU.initStmtList();
2251
2252 if (useSegmentedStringOffsetsTable())
2253 NewCU.addStringOffsetsStart();
2254
2255 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
2256
2257 return NewCU;
2258 }
2259
2260 // Emit the .debug_info.dwo section for separated dwarf. This contains the
2261 // compile units that would normally be in debug_info.
emitDebugInfoDWO()2262 void DwarfDebug::emitDebugInfoDWO() {
2263 assert(useSplitDwarf() && "No split dwarf debug info?");
2264 // Don't emit relocations into the dwo file.
2265 InfoHolder.emitUnits(/* UseOffsets */ true);
2266 }
2267
2268 // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2269 // abbreviations for the .debug_info.dwo section.
emitDebugAbbrevDWO()2270 void DwarfDebug::emitDebugAbbrevDWO() {
2271 assert(useSplitDwarf() && "No split dwarf?");
2272 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
2273 }
2274
emitDebugLineDWO()2275 void DwarfDebug::emitDebugLineDWO() {
2276 assert(useSplitDwarf() && "No split dwarf?");
2277 SplitTypeUnitFileTable.Emit(
2278 *Asm->OutStreamer, MCDwarfLineTableParams(),
2279 Asm->getObjFileLowering().getDwarfLineDWOSection());
2280 }
2281
emitStringOffsetsTableHeaderDWO()2282 void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
2283 assert(useSplitDwarf() && "No split dwarf?");
2284 InfoHolder.getStringPool().emitStringOffsetsTableHeader(
2285 *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(),
2286 InfoHolder.getStringOffsetsStartSym());
2287 }
2288
2289 // Emit the .debug_str.dwo section for separated dwarf. This contains the
2290 // string section and is identical in format to traditional .debug_str
2291 // sections.
emitDebugStrDWO()2292 void DwarfDebug::emitDebugStrDWO() {
2293 if (useSegmentedStringOffsetsTable())
2294 emitStringOffsetsTableHeaderDWO();
2295 assert(useSplitDwarf() && "No split dwarf?");
2296 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
2297 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2298 OffSec, /* UseRelativeOffsets = */ false);
2299 }
2300
2301 // Emit DWO addresses.
emitDebugAddr()2302 void DwarfDebug::emitDebugAddr() {
2303 assert(useSplitDwarf() && "No split dwarf?");
2304 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
2305 }
2306
getDwoLineTable(const DwarfCompileUnit & CU)2307 MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
2308 if (!useSplitDwarf())
2309 return nullptr;
2310 const DICompileUnit *DIUnit = CU.getCUNode();
2311 SplitTypeUnitFileTable.maybeSetRootFile(
2312 DIUnit->getDirectory(), DIUnit->getFilename(),
2313 CU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource());
2314 return &SplitTypeUnitFileTable;
2315 }
2316
makeTypeSignature(StringRef Identifier)2317 uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
2318 MD5 Hash;
2319 Hash.update(Identifier);
2320 // ... take the least significant 8 bytes and return those. Our MD5
2321 // implementation always returns its results in little endian, so we actually
2322 // need the "high" word.
2323 MD5::MD5Result Result;
2324 Hash.final(Result);
2325 return Result.high();
2326 }
2327
addDwarfTypeUnitType(DwarfCompileUnit & CU,StringRef Identifier,DIE & RefDie,const DICompositeType * CTy)2328 void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
2329 StringRef Identifier, DIE &RefDie,
2330 const DICompositeType *CTy) {
2331 // Fast path if we're building some type units and one has already used the
2332 // address pool we know we're going to throw away all this work anyway, so
2333 // don't bother building dependent types.
2334 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
2335 return;
2336
2337 auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
2338 if (!Ins.second) {
2339 CU.addDIETypeSignature(RefDie, Ins.first->second);
2340 return;
2341 }
2342
2343 bool TopLevelType = TypeUnitsUnderConstruction.empty();
2344 AddrPool.resetUsedFlag();
2345
2346 auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
2347 getDwoLineTable(CU));
2348 DwarfTypeUnit &NewTU = *OwnedUnit;
2349 DIE &UnitDie = NewTU.getUnitDie();
2350 TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
2351
2352 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
2353 CU.getLanguage());
2354
2355 uint64_t Signature = makeTypeSignature(Identifier);
2356 NewTU.setTypeSignature(Signature);
2357 Ins.first->second = Signature;
2358
2359 if (useSplitDwarf())
2360 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
2361 else {
2362 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2363 // Non-split type units reuse the compile unit's line table.
2364 CU.applyStmtList(UnitDie);
2365 }
2366
2367 // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
2368 // units.
2369 if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
2370 NewTU.addStringOffsetsStart();
2371
2372 NewTU.setType(NewTU.createTypeDIE(CTy));
2373
2374 if (TopLevelType) {
2375 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2376 TypeUnitsUnderConstruction.clear();
2377
2378 // Types referencing entries in the address table cannot be placed in type
2379 // units.
2380 if (AddrPool.hasBeenUsed()) {
2381
2382 // Remove all the types built while building this type.
2383 // This is pessimistic as some of these types might not be dependent on
2384 // the type that used an address.
2385 for (const auto &TU : TypeUnitsToAdd)
2386 TypeSignatures.erase(TU.second);
2387
2388 // Construct this type in the CU directly.
2389 // This is inefficient because all the dependent types will be rebuilt
2390 // from scratch, including building them in type units, discovering that
2391 // they depend on addresses, throwing them out and rebuilding them.
2392 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
2393 return;
2394 }
2395
2396 // If the type wasn't dependent on fission addresses, finish adding the type
2397 // and all its dependent types.
2398 for (auto &TU : TypeUnitsToAdd) {
2399 InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
2400 InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
2401 }
2402 }
2403 CU.addDIETypeSignature(RefDie, Signature);
2404 }
2405
2406 // Add the Name along with its companion DIE to the appropriate accelerator
2407 // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
2408 // AccelTableKind::Apple, we use the table we got as an argument). If
2409 // accelerator tables are disabled, this function does nothing.
2410 template <typename DataT>
addAccelNameImpl(AccelTable<DataT> & AppleAccel,StringRef Name,const DIE & Die)2411 void DwarfDebug::addAccelNameImpl(AccelTable<DataT> &AppleAccel, StringRef Name,
2412 const DIE &Die) {
2413 if (getAccelTableKind() == AccelTableKind::None)
2414 return;
2415
2416 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2417 DwarfStringPoolEntryRef Ref =
2418 Holder.getStringPool().getEntry(*Asm, Name);
2419
2420 switch (getAccelTableKind()) {
2421 case AccelTableKind::Apple:
2422 AppleAccel.addName(Ref, Die);
2423 break;
2424 case AccelTableKind::Dwarf:
2425 AccelDebugNames.addName(Ref, Die);
2426 break;
2427 case AccelTableKind::Default:
2428 llvm_unreachable("Default should have already been resolved.");
2429 case AccelTableKind::None:
2430 llvm_unreachable("None handled above");
2431 }
2432 }
2433
addAccelName(StringRef Name,const DIE & Die)2434 void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
2435 addAccelNameImpl(AccelNames, Name, Die);
2436 }
2437
addAccelObjC(StringRef Name,const DIE & Die)2438 void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2439 // ObjC names go only into the Apple accelerator tables.
2440 if (getAccelTableKind() == AccelTableKind::Apple)
2441 addAccelNameImpl(AccelObjC, Name, Die);
2442 }
2443
addAccelNamespace(StringRef Name,const DIE & Die)2444 void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
2445 addAccelNameImpl(AccelNamespace, Name, Die);
2446 }
2447
addAccelType(StringRef Name,const DIE & Die,char Flags)2448 void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2449 addAccelNameImpl(AccelTypes, Name, Die);
2450 }
2451
getDwarfVersion() const2452 uint16_t DwarfDebug::getDwarfVersion() const {
2453 return Asm->OutStreamer->getContext().getDwarfVersion();
2454 }
2455