blob: a0094b2be2fbec2ca203867188548e0a11caae94 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===//
Bill Wendling2f921f82009-05-15 09:23:25 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Bill Wendling2f921f82009-05-15 09:23:25 +000014#include "DwarfDebug.h"
David Blaikie37c52312014-10-04 15:49:50 +000015#include "ByteStreamer.h"
Eric Christopher45731982013-08-08 23:45:55 +000016#include "DIEHash.h"
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000017#include "DebugLocEntry.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000018#include "DebugLocStream.h"
19#include "DwarfAccelTable.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000020#include "DwarfCompileUnit.h"
21#include "DwarfExpression.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000022#include "DwarfFile.h"
David Blaikie2c86a722013-12-02 19:33:15 +000023#include "DwarfUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000024#include "llvm/ADT/APInt.h"
25#include "llvm/ADT/DenseMap.h"
26#include "llvm/ADT/DenseSet.h"
27#include "llvm/ADT/MapVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/ADT/STLExtras.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000029#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/ADT/Triple.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000032#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000033#include "llvm/BinaryFormat/Dwarf.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000034#include "llvm/CodeGen/AsmPrinter.h"
Frederic Risse541e0b2015-01-05 21:29:41 +000035#include "llvm/CodeGen/DIE.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000036#include "llvm/CodeGen/LexicalScopes.h"
37#include "llvm/CodeGen/MachineBasicBlock.h"
David Greene829b3e82009-08-19 21:52:55 +000038#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000039#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000040#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000041#include "llvm/CodeGen/MachineOperand.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000042#include "llvm/CodeGen/TargetLoweringObjectFile.h"
43#include "llvm/CodeGen/TargetRegisterInfo.h"
44#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Constants.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000046#include "llvm/IR/DebugInfoMetadata.h"
47#include "llvm/IR/DebugLoc.h"
48#include "llvm/IR/Function.h"
49#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000050#include "llvm/IR/Module.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000051#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000052#include "llvm/MC/MCContext.h"
Frederic Rissa5ab8442015-08-07 15:14:08 +000053#include "llvm/MC/MCDwarf.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000054#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000055#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000056#include "llvm/MC/MCSymbol.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000057#include "llvm/MC/MCTargetOptions.h"
58#include "llvm/MC/MachineLocation.h"
59#include "llvm/MC/SectionKind.h"
60#include "llvm/Pass.h"
61#include "llvm/Support/Casting.h"
Devang Patel6c74a872010-04-27 19:46:33 +000062#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000063#include "llvm/Support/Debug.h"
64#include "llvm/Support/ErrorHandling.h"
Eric Christopher67646432013-07-26 17:02:41 +000065#include "llvm/Support/MD5.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000066#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000067#include "llvm/Support/Timer.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000068#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000069#include "llvm/Target/TargetMachine.h"
70#include "llvm/Target/TargetOptions.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000071#include <algorithm>
72#include <cassert>
73#include <cstddef>
74#include <cstdint>
75#include <iterator>
76#include <string>
77#include <utility>
78#include <vector>
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +000079
Bill Wendling2f921f82009-05-15 09:23:25 +000080using namespace llvm;
81
Chandler Carruth1b9dde02014-04-22 02:02:50 +000082#define DEBUG_TYPE "dwarfdebug"
83
Eric Christopher7f2b5512013-07-23 22:16:41 +000084static cl::opt<bool>
85DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
86 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000087
David Blaikie038e28a2017-07-31 21:48:42 +000088static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier(
89 "use-dwarf-ranges-base-address-specifier", cl::Hidden,
David Blaikiec2be8632017-08-01 14:50:50 +000090 cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
David Blaikie038e28a2017-07-31 21:48:42 +000091
Eric Christopher02dbadb2014-02-14 01:26:55 +000092static cl::opt<bool> GenerateARangeSection("generate-arange-section",
93 cl::Hidden,
94 cl::desc("Generate dwarf aranges"),
95 cl::init(false));
96
David Blaikie488393f2017-05-12 01:13:45 +000097static cl::opt<bool> SplitDwarfCrossCuReferences(
98 "split-dwarf-cross-cu-references", cl::Hidden,
99 cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
100
Eric Christopherf07ee3a2014-01-27 23:50:03 +0000101enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher4996c702011-11-07 09:24:32 +0000102
Paul Robinsonac7fe5e2016-12-12 20:49:11 +0000103static cl::opt<DefaultOnOff> UnknownLocations(
104 "use-unknown-locations", cl::Hidden,
105 cl::desc("Make an absence of debug location information explicit."),
106 cl::values(clEnumVal(Default, "At top of block or after label"),
107 clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
108 cl::init(Default));
109
Eric Christopher7f2b5512013-07-23 22:16:41 +0000110static cl::opt<DefaultOnOff>
111DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
112 cl::desc("Output prototype dwarf accelerator tables."),
113 cl::values(clEnumVal(Default, "Default for platform"),
114 clEnumVal(Enable, "Enabled"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000115 clEnumVal(Disable, "Disabled")),
Eric Christopher7f2b5512013-07-23 22:16:41 +0000116 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +0000117
Paul Robinson43d1e452016-04-18 22:41:41 +0000118enum LinkageNameOption {
119 DefaultLinkageNames,
120 AllLinkageNames,
121 AbstractLinkageNames
122};
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000123
Paul Robinson43d1e452016-04-18 22:41:41 +0000124static cl::opt<LinkageNameOption>
125 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
126 cl::desc("Which DWARF linkage-name attributes to emit."),
127 cl::values(clEnumValN(DefaultLinkageNames, "Default",
128 "Default for platform"),
129 clEnumValN(AllLinkageNames, "All", "All"),
130 clEnumValN(AbstractLinkageNames, "Abstract",
Mehdi Amini732afdd2016-10-08 19:41:06 +0000131 "Abstract subprograms")),
Paul Robinson43d1e452016-04-18 22:41:41 +0000132 cl::init(DefaultLinkageNames));
Paul Robinson78046b42015-08-11 21:36:45 +0000133
Matthias Braun9f15a792016-11-18 19:43:18 +0000134static const char *const DWARFGroupName = "dwarf";
135static const char *const DWARFGroupDescription = "DWARF Emission";
136static const char *const DbgTimerName = "writer";
137static const char *const DbgTimerDescription = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000138
Adrian Prantla63b8e82017-03-16 17:42:45 +0000139void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000140 BS.EmitInt8(
141 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
142 : dwarf::OperationEncodingString(Op));
143}
144
Adrian Prantla63b8e82017-03-16 17:42:45 +0000145void DebugLocDwarfExpression::emitSigned(int64_t Value) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000146 BS.EmitSLEB128(Value, Twine(Value));
147}
148
Adrian Prantla63b8e82017-03-16 17:42:45 +0000149void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000150 BS.EmitULEB128(Value, Twine(Value));
151}
152
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000153bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
154 unsigned MachineReg) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000155 // This information is not available while emitting .debug_loc entries.
156 return false;
157}
158
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000159bool DbgVariable::isBlockByrefVariable() const {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000160 assert(Var && "Invalid complex DbgVariable!");
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000161 return Var->getType().resolve()->isBlockByrefStruct();
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000162}
163
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000164const DIType *DbgVariable::getType() const {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000165 DIType *Ty = Var->getType().resolve();
Devang Patelf20c4f72011-04-12 22:53:02 +0000166 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
167 // addresses instead.
Duncan P. N. Exon Smith571baeb2015-04-14 01:59:58 +0000168 if (Ty->isBlockByrefStruct()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000169 /* Byref variables, in Blocks, are declared by the programmer as
170 "SomeType VarName;", but the compiler creates a
171 __Block_byref_x_VarName struct, and gives the variable VarName
172 either the struct, or a pointer to the struct, as its type. This
173 is necessary for various behind-the-scenes things the compiler
174 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000175
Devang Patelf20c4f72011-04-12 22:53:02 +0000176 However, as far as the original *programmer* is concerned, the
177 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000178
Devang Patelf20c4f72011-04-12 22:53:02 +0000179 The following function dives into the __Block_byref_x_VarName
180 struct to find the original type of the variable. This will be
181 passed back to the code generating the type for the Debug
182 Information Entry for the variable 'VarName'. 'VarName' will then
183 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000184
Devang Patelf20c4f72011-04-12 22:53:02 +0000185 The original type 'SomeType' will be the type of the field named
186 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000187
Devang Patelf20c4f72011-04-12 22:53:02 +0000188 NOTE: In order for this to not completely fail on the debugger
189 side, the Debug Information Entry for the variable VarName needs to
190 have a DW_AT_location that tells the debugger how to unwind through
191 the pointers and __Block_byref_x_VarName struct to find the actual
192 value of the variable. The function addBlockByrefType does this. */
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000193 DIType *subType = Ty;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000194 uint16_t tag = Ty->getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000195
Eric Christopher9adc55f2013-09-04 19:53:21 +0000196 if (tag == dwarf::DW_TAG_pointer_type)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000197 subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
Eric Christopher6a841382012-11-19 22:42:10 +0000198
Duncan P. N. Exon Smith6ac940d2015-07-24 18:17:17 +0000199 auto Elements = cast<DICompositeType>(subType)->getElements();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000200 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Duncan P. N. Exon Smith099ea1c2015-07-24 18:58:32 +0000201 auto *DT = cast<DIDerivedType>(Elements[i]);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000202 if (getName() == DT->getName())
Duncan P. N. Exon Smith848af382015-04-20 18:20:03 +0000203 return resolve(DT->getBaseType());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000204 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000205 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000206 return Ty;
207}
Bill Wendling2f921f82009-05-15 09:23:25 +0000208
Adrian Prantl67c24422017-02-17 19:42:32 +0000209ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
Adrian Prantl55030772017-03-22 16:50:16 +0000210 if (FrameIndexExprs.size() == 1)
211 return FrameIndexExprs;
212
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000213 assert(llvm::all_of(FrameIndexExprs,
214 [](const FrameIndexExpr &A) {
215 return A.Expr->isFragment();
216 }) &&
Adrian Prantl55030772017-03-22 16:50:16 +0000217 "multiple FI expressions without DW_OP_LLVM_fragment");
Adrian Prantl67c24422017-02-17 19:42:32 +0000218 std::sort(FrameIndexExprs.begin(), FrameIndexExprs.end(),
219 [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
220 return A.Expr->getFragmentInfo()->OffsetInBits <
221 B.Expr->getFragmentInfo()->OffsetInBits;
222 });
Bjorn Steinbrinkd36bbe92017-10-10 07:46:17 +0000223
Adrian Prantl67c24422017-02-17 19:42:32 +0000224 return FrameIndexExprs;
225}
226
Bjorn Steinbrinkd36bbe92017-10-10 07:46:17 +0000227void DbgVariable::addMMIEntry(const DbgVariable &V) {
228 assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry");
229 assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry");
230 assert(V.Var == Var && "conflicting variable");
231 assert(V.IA == IA && "conflicting inlined-at location");
232
233 assert(!FrameIndexExprs.empty() && "Expected an MMI entry");
234 assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry");
235
Daniel Jasper4d931202017-10-12 13:25:05 +0000236 // FIXME: This logic should not be necessary anymore, as we now have proper
237 // deduplication. However, without it, we currently run into the assertion
238 // below, which means that we are likely dealing with broken input, i.e. two
239 // non-fragment entries for the same variable at different frame indices.
240 if (FrameIndexExprs.size()) {
241 auto *Expr = FrameIndexExprs.back().Expr;
242 if (!Expr || !Expr->isFragment())
243 return;
244 }
245
Bjorn Steinbrinkd36bbe92017-10-10 07:46:17 +0000246 for (const auto &FIE : V.FrameIndexExprs)
247 // Ignore duplicate entries.
248 if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) {
249 return FIE.FI == Other.FI && FIE.Expr == Other.Expr;
250 }))
251 FrameIndexExprs.push_back(FIE);
252
253 assert((FrameIndexExprs.size() == 1 ||
254 llvm::all_of(FrameIndexExprs,
255 [](FrameIndexExpr &FIE) {
256 return FIE.Expr && FIE.Expr->isFragment();
257 })) &&
258 "conflicting locations for variable");
259}
260
George Burgess IV381fc0e2016-08-25 01:05:08 +0000261static const DwarfAccelTable::Atom TypeAtoms[] = {
David Blaikie18d33752014-04-24 01:23:49 +0000262 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
263 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
264 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
265
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000266DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000267 : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
268 InfoHolder(A, "info_string", DIEValueAllocator),
Frederic Riss9412d632015-03-04 02:30:17 +0000269 SkeletonHolder(A, "skel_string", DIEValueAllocator),
Eric Christopher364dbe02016-10-01 01:50:29 +0000270 IsDarwin(A->TM.getTargetTriple().isOSDarwin()),
David Blaikie2406a0622014-04-23 23:37:35 +0000271 AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
David Blaikie0ee82b92014-04-24 00:53:32 +0000272 dwarf::DW_FORM_data4)),
273 AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
David Blaikieecf04152014-04-24 01:02:42 +0000274 dwarf::DW_FORM_data4)),
275 AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
David Blaikie18d33752014-04-24 01:23:49 +0000276 dwarf::DW_FORM_data4)),
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000277 AccelTypes(TypeAtoms) {
Eric Christopher364dbe02016-10-01 01:50:29 +0000278 const Triple &TT = Asm->TM.getTargetTriple();
Eric Christopherad9fe892012-04-02 17:58:52 +0000279
Paul Robinson6c27a2c2015-12-16 19:58:30 +0000280 // Make sure we know our "debugger tuning." The target option takes
Paul Robinsonb9de1062015-07-15 22:04:54 +0000281 // precedence; fall back to triple-based defaults.
Paul Robinson6c27a2c2015-12-16 19:58:30 +0000282 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
283 DebuggerTuning = Asm->TM.Options.DebuggerTuning;
Dimitry Andric2c364212016-01-07 22:09:12 +0000284 else if (IsDarwin)
Paul Robinsonb9de1062015-07-15 22:04:54 +0000285 DebuggerTuning = DebuggerKind::LLDB;
286 else if (TT.isPS4CPU())
287 DebuggerTuning = DebuggerKind::SCE;
288 else
289 DebuggerTuning = DebuggerKind::GDB;
290
291 // Turn on accelerator tables for LLDB by default.
Eric Christopher574b5c82013-08-19 21:41:38 +0000292 if (DwarfAccelTables == Default)
Paul Robinsonb9de1062015-07-15 22:04:54 +0000293 HasDwarfAccelTables = tuneForLLDB();
Eric Christopher574b5c82013-08-19 21:41:38 +0000294 else
Eric Christopher5297df02013-08-26 20:58:35 +0000295 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000296
David Blaikiec53e18d2016-05-24 21:19:28 +0000297 HasAppleExtensionAttributes = tuneForLLDB();
298
David Blaikie96b1ed52017-04-21 23:35:26 +0000299 // Handle split DWARF.
300 HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty();
Eric Christopher29424312012-11-12 22:22:20 +0000301
Paul Robinson43d1e452016-04-18 22:41:41 +0000302 // SCE defaults to linkage names only for abstract subprograms.
303 if (DwarfLinkageNames == DefaultLinkageNames)
304 UseAllLinkageNames = !tuneForSCE();
Paul Robinson78046b42015-08-11 21:36:45 +0000305 else
Paul Robinson43d1e452016-04-18 22:41:41 +0000306 UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
Paul Robinson78046b42015-08-11 21:36:45 +0000307
Eric Christopher4c5bff32014-06-19 06:22:08 +0000308 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
Greg Claytone6543972016-11-23 23:30:37 +0000309 unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
Eric Christopher793c7472014-04-28 20:42:22 +0000310 : MMI->getModule()->getDwarfVersion();
Reid Kleckner12d2c122015-08-05 22:26:20 +0000311 // Use dwarf 4 by default if nothing is requested.
312 DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION;
Manman Renac8062b2013-07-02 23:40:10 +0000313
Paul Robinsonb9de1062015-07-15 22:04:54 +0000314 // Work around a GDB bug. GDB doesn't support the standard opcode;
315 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
316 // is defined as of DWARF 3.
317 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
318 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
319 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
Paul Robinson78cc0822015-03-04 20:55:11 +0000320
Adrian Prantl6323ddf2016-05-17 21:07:16 +0000321 // GDB does not fully support the DWARF 4 representation for bitfields.
322 UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB();
323
Lang Hames9ff69c82015-04-24 19:11:51 +0000324 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
Bill Wendling2f921f82009-05-15 09:23:25 +0000325}
Bill Wendling2f921f82009-05-15 09:23:25 +0000326
Reid Klecknerdd2647e2014-04-30 20:34:31 +0000327// Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000328DwarfDebug::~DwarfDebug() = default;
Reid Klecknerdd2647e2014-04-30 20:34:31 +0000329
Eric Christopherd9843b32011-11-10 19:25:34 +0000330static bool isObjCClass(StringRef Name) {
331 return Name.startswith("+") || Name.startswith("-");
332}
333
334static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000335 if (!isObjCClass(Name))
336 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000337
Benjamin Kramer260de742013-08-24 12:15:54 +0000338 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000339}
340
341static void getObjCClassCategory(StringRef In, StringRef &Class,
342 StringRef &Category) {
343 if (!hasObjCCategory(In)) {
344 Class = In.slice(In.find('[') + 1, In.find(' '));
345 Category = "";
346 return;
347 }
348
349 Class = In.slice(In.find('[') + 1, In.find('('));
350 Category = In.slice(In.find('[') + 1, In.find(' '));
Eric Christopherd9843b32011-11-10 19:25:34 +0000351}
352
353static StringRef getObjCMethodName(StringRef In) {
354 return In.slice(In.find(' ') + 1, In.find(']'));
355}
356
357// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000358// TODO: Determine whether or not we should add names for programs
359// that do not have a DW_AT_name or DW_AT_linkage_name field - this
360// is only slightly different than the lookup of non-standard ObjC names.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000361void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000362 if (!SP->isDefinition())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000363 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000364 addAccelName(SP->getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000365
366 // If the linkage name is different than the name, go ahead and output
367 // that as well into the name table.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000368 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName())
369 addAccelName(SP->getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000370
371 // If this is an Objective-C selector name add it to the ObjC accelerator
372 // too.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000373 if (isObjCClass(SP->getName())) {
Eric Christopherd9843b32011-11-10 19:25:34 +0000374 StringRef Class, Category;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000375 getObjCClassCategory(SP->getName(), Class, Category);
David Blaikie0ee82b92014-04-24 00:53:32 +0000376 addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000377 if (Category != "")
David Blaikie0ee82b92014-04-24 00:53:32 +0000378 addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000379 // Also add the base method name to the name table.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000380 addAccelName(getObjCMethodName(SP->getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000381 }
382}
383
Manman Ren5b2f4b02013-09-11 19:40:28 +0000384/// Check whether we should create a DIE for the given Scope, return true
385/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000386bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
387 if (Scope->isAbstractScope())
388 return false;
389
Manman Ren5b2f4b02013-09-11 19:40:28 +0000390 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000391 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
392 if (Ranges.empty())
393 return true;
394
395 if (Ranges.size() > 1)
396 return false;
397
Manman Ren5b2f4b02013-09-11 19:40:28 +0000398 // We don't create a DIE if we have a single Range and the end label
399 // is null.
David Blaikiecd4b8a22014-08-31 02:14:26 +0000400 return !getLabelAfterInsn(Ranges.front().second);
Manman Ren2312ed32013-09-10 18:40:41 +0000401}
402
Benjamin Kramerb7d33112016-08-06 11:13:10 +0000403template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
David Blaikie3a443c22014-11-04 22:12:25 +0000404 F(CU);
405 if (auto *SkelCU = CU.getSkeleton())
David Blaikiea01f2952016-08-24 18:29:49 +0000406 if (CU.getCUNode()->getSplitDebugInlining())
407 F(*SkelCU);
David Blaikie3a443c22014-11-04 22:12:25 +0000408}
409
David Blaikie488393f2017-05-12 01:13:45 +0000410bool DwarfDebug::shareAcrossDWOCUs() const {
411 return SplitDwarfCrossCuReferences;
412}
413
414void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU,
415 LexicalScope *Scope) {
David Blaikiee872a6e2014-04-29 15:58:35 +0000416 assert(Scope && Scope->getScopeNode());
417 assert(Scope->isAbstractScope());
418 assert(!Scope->getInlinedAt());
419
David Blaikieb4614682016-12-14 19:38:39 +0000420 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikiee872a6e2014-04-29 15:58:35 +0000421
David Blaikie1ea9db22014-05-21 23:14:12 +0000422 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
423 // was inlined from another compile unit.
Mehdi Amini41812052017-05-29 06:25:30 +0000424 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining())
425 // Avoid building the original CU if it won't be used
426 SrcCU.constructAbstractSubprogramScopeDIE(Scope);
427 else {
428 auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
429 if (auto *SkelCU = CU.getSkeleton()) {
430 (shareAcrossDWOCUs() ? CU : SrcCU)
431 .constructAbstractSubprogramScopeDIE(Scope);
432 if (CU.getCUNode()->getSplitDebugInlining())
433 SkelCU->constructAbstractSubprogramScopeDIE(Scope);
434 } else
435 CU.constructAbstractSubprogramScopeDIE(Scope);
David Blaikie488393f2017-05-12 01:13:45 +0000436 }
David Blaikiee872a6e2014-04-29 15:58:35 +0000437}
438
David Blaikieb3cee2f2017-05-25 18:50:28 +0000439void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const {
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000440 if (!U.hasDwarfPubSections())
David Blaikie3c842622013-12-04 21:31:26 +0000441 return;
442
David Blaikief9b6a552014-04-22 22:39:41 +0000443 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
David Blaikie3c842622013-12-04 21:31:26 +0000444}
445
Eric Christopher4287a492013-12-09 23:57:44 +0000446// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000447// DW_TAG_compile_unit.
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000448DwarfCompileUnit &
David Blaikie07963bd2017-05-26 18:52:56 +0000449DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) {
450 if (auto *CU = CUMap.lookup(DIUnit))
451 return *CU;
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000452 StringRef FN = DIUnit->getFilename();
453 CompilationDir = DIUnit->getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000454
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000455 auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
David Blaikiebd579052014-04-28 21:14:27 +0000456 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
David Blaikief9b6a552014-04-22 22:39:41 +0000457 DwarfCompileUnit &NewCU = *OwnedUnit;
David Blaikie92a2f8a2014-04-28 21:04:29 +0000458 DIE &Die = NewCU.getUnitDie();
David Blaikief9b6a552014-04-22 22:39:41 +0000459 InfoHolder.addUnit(std::move(OwnedUnit));
David Blaikie0b214e42016-03-24 18:37:08 +0000460 if (useSplitDwarf()) {
David Blaikiecafd9622014-11-02 08:51:37 +0000461 NewCU.setSkeleton(constructSkeletonCU(NewCU));
David Blaikie0b214e42016-03-24 18:37:08 +0000462 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikie96b1ed52017-04-21 23:35:26 +0000463 Asm->TM.Options.MCOptions.SplitDwarfFile);
David Blaikie0b214e42016-03-24 18:37:08 +0000464 }
David Blaikief9b6a552014-04-22 22:39:41 +0000465
David Blaikie89daf772017-07-28 03:06:25 +0000466 for (auto *IE : DIUnit->getImportedEntities())
467 NewCU.addImportedEntity(IE);
468
David Blaikie7ac51492014-03-20 17:05:45 +0000469 // LTO with assembly output shares a single line table amongst multiple CUs.
470 // To avoid the compilation directory being ambiguous, let the line table
471 // explicitly describe the directory of all files, never relying on the
472 // compilation directory.
Lang Hames9ff69c82015-04-24 19:11:51 +0000473 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
474 Asm->OutStreamer->getContext().setMCLineTableCompilationDir(
David Blaikief9b6a552014-04-22 22:39:41 +0000475 NewCU.getUniqueID(), CompilationDir);
David Blaikie2666e242013-12-06 19:38:46 +0000476
Eric Christopher9ea300f2017-03-29 23:34:27 +0000477 StringRef Producer = DIUnit->getProducer();
478 StringRef Flags = DIUnit->getFlags();
479 if (!Flags.empty()) {
480 std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
481 NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
482 } else
483 NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
484
David Blaikie92a2f8a2014-04-28 21:04:29 +0000485 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000486 DIUnit->getSourceLanguage());
David Blaikie92a2f8a2014-04-28 21:04:29 +0000487 NewCU.addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000488
Eric Christopher52ce7182013-04-09 19:23:15 +0000489 if (!useSplitDwarf()) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000490 NewCU.initStmtList();
Eric Christophera51d3fc2013-09-27 22:50:48 +0000491
492 // If we're using split dwarf the compilation dir is going to be in the
493 // skeleton CU and so we don't need to duplicate it here.
494 if (!CompilationDir.empty())
David Blaikie92a2f8a2014-04-28 21:04:29 +0000495 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000496
David Blaikie92a2f8a2014-04-28 21:04:29 +0000497 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000498 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000499
David Blaikiec53e18d2016-05-24 21:19:28 +0000500 if (useAppleExtensionAttributes()) {
501 if (DIUnit->isOptimized())
502 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000503
David Blaikiec53e18d2016-05-24 21:19:28 +0000504 StringRef Flags = DIUnit->getFlags();
505 if (!Flags.empty())
506 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000507
David Blaikiec53e18d2016-05-24 21:19:28 +0000508 if (unsigned RVer = DIUnit->getRuntimeVersion())
509 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
510 dwarf::DW_FORM_data1, RVer);
511 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000512
David Blaikiecafd9622014-11-02 08:51:37 +0000513 if (useSplitDwarf())
Greg Clayton35630c32016-12-01 18:56:29 +0000514 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
David Blaikiecafd9622014-11-02 08:51:37 +0000515 else
Greg Clayton35630c32016-12-01 18:56:29 +0000516 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
Eric Christopherd039baa2013-12-30 03:40:32 +0000517
Adrian Prantldeef90d2015-09-14 22:10:22 +0000518 if (DIUnit->getDWOId()) {
Adrian Prantl77fefeb2015-09-22 23:21:00 +0000519 // This CU is either a clang module DWO or a skeleton CU.
Adrian Prantldeef90d2015-09-14 22:10:22 +0000520 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
521 DIUnit->getDWOId());
Adrian Prantl77fefeb2015-09-22 23:21:00 +0000522 if (!DIUnit->getSplitDebugFilename().empty())
523 // This is a prefabricated skeleton CU.
524 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
525 DIUnit->getSplitDebugFilename());
Adrian Prantldeef90d2015-09-14 22:10:22 +0000526 }
527
David Majnemer0a16c222016-08-11 21:15:00 +0000528 CUMap.insert({DIUnit, &NewCU});
529 CUDieMap.insert({&Die, &NewCU});
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000530 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000531}
532
David Blaikie8912df12014-08-31 05:41:15 +0000533void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000534 const DIImportedEntity *N) {
David Blaikie2195e132017-07-27 00:06:53 +0000535 if (isa<DILocalScope>(N->getScope()))
536 return;
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000537 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
538 D->addChild(TheCU.constructImportedEntityDIE(N));
David Blaikief55abea2013-04-22 06:12:31 +0000539}
540
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000541/// Sort and unique GVEs by comparing their fragment offset.
542static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
543sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
544 std::sort(GVEs.begin(), GVEs.end(),
545 [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
Eli Friedman48e75492017-10-12 20:54:08 +0000546 // Sort order: first null exprs, then exprs without fragment
547 // info, then sort by fragment offset in bits.
548 // FIXME: Come up with a more comprehensive comparator so
549 // the sorting isn't non-deterministic, and so the following
550 // std::unique call works correctly.
551 if (!A.Expr || !B.Expr)
552 return !!B.Expr;
553 auto FragmentA = A.Expr->getFragmentInfo();
554 auto FragmentB = B.Expr->getFragmentInfo();
555 if (!FragmentA || !FragmentB)
556 return !!FragmentB;
557 return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000558 });
559 GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
560 [](DwarfCompileUnit::GlobalExpr A,
561 DwarfCompileUnit::GlobalExpr B) {
562 return A.Expr == B.Expr;
563 }),
564 GVEs.end());
565 return GVEs;
566}
567
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000568// Emit all Dwarf sections that should come prior to the content. Create
569// global DIEs and emit initial debug info sections. This is invoked by
570// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000571void DwarfDebug::beginModule() {
Matthias Braun9f15a792016-11-18 19:43:18 +0000572 NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName,
573 DWARFGroupDescription, TimePassesIsEnabled);
Devang Patel6c74a872010-04-27 19:46:33 +0000574 if (DisableDebugInfoPrinting)
575 return;
576
Eric Christopher58f41952012-11-19 22:42:15 +0000577 const Module *M = MMI->getModule();
578
Peter Collingbourneb3269862016-06-01 02:58:40 +0000579 unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
580 M->debug_compile_units_end());
Adrian Prantl5992a722016-04-08 22:43:03 +0000581 // Tell MMI whether we have debug info.
582 MMI->setDebugInfoAvailability(NumDebugCUs > 0);
583 SingleCU = NumDebugCUs == 1;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000584 DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
585 GVMap;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000586 for (const GlobalVariable &Global : M->globals()) {
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000587 SmallVector<DIGlobalVariableExpression *, 1> GVs;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000588 Global.getDebugInfo(GVs);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000589 for (auto *GVE : GVs)
590 GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000591 }
592
Adrian Prantl5992a722016-04-08 22:43:03 +0000593 for (DICompileUnit *CUNode : M->debug_compile_units()) {
David Blaikie89daf772017-07-28 03:06:25 +0000594 // FIXME: Move local imported entities into a list attached to the
595 // subprogram, then this search won't be needed and a
596 // getImportedEntities().empty() test should go below with the rest.
597 bool HasNonLocalImportedEntities = llvm::any_of(
598 CUNode->getImportedEntities(), [](const DIImportedEntity *IE) {
599 return !isa<DILocalScope>(IE->getScope());
600 });
601
602 if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() &&
603 CUNode->getRetainedTypes().empty() &&
604 CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
David Blaikie07963bd2017-05-26 18:52:56 +0000605 continue;
606
607 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000608
609 // Global Variables.
Adrian Prantl2116dd32017-08-19 01:15:06 +0000610 for (auto *GVE : CUNode->getGlobalVariables()) {
611 // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
612 // already know about the variable and it isn't adding a constant
613 // expression.
614 auto &GVMapEntry = GVMap[GVE->getVariable()];
615 auto *Expr = GVE->getExpression();
616 if (!GVMapEntry.size() || (Expr && Expr->isConstant()))
617 GVMapEntry.push_back({nullptr, Expr});
618 }
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000619 DenseSet<DIGlobalVariable *> Processed;
620 for (auto *GVE : CUNode->getGlobalVariables()) {
621 DIGlobalVariable *GV = GVE->getVariable();
622 if (Processed.insert(GV).second)
623 CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
624 }
625
Duncan P. N. Exon Smith9928a902015-04-20 18:52:06 +0000626 for (auto *Ty : CUNode->getEnumTypes()) {
Manman Renbd1628a2014-07-28 23:04:20 +0000627 // The enum types array by design contains pointers to
628 // MDNodes rather than DIRefs. Unique them here.
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000629 CU.getOrCreateTypeDIE(cast<DIType>(Ty));
Manman Renbd1628a2014-07-28 23:04:20 +0000630 }
Duncan P. N. Exon Smith9928a902015-04-20 18:52:06 +0000631 for (auto *Ty : CUNode->getRetainedTypes()) {
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000632 // The retained types array by design contains pointers to
633 // MDNodes rather than DIRefs. Unique them here.
Amjad Aboud72da9392016-04-30 01:44:07 +0000634 if (DIType *RT = dyn_cast<DIType>(Ty))
Adrian Prantl75819ae2016-04-15 15:57:41 +0000635 // There is no point in force-emitting a forward declaration.
636 CU.getOrCreateTypeDIE(RT);
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000637 }
David Blaikief55abea2013-04-22 06:12:31 +0000638 // Emit imported_modules last so that the relevant context is already
639 // available.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000640 for (auto *IE : CUNode->getImportedEntities())
Amjad Aboud72da9392016-04-30 01:44:07 +0000641 constructAndAddImportedEntityDIE(CU, IE);
David Blaikiedc69ebb2013-03-11 23:39:23 +0000642 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000643}
644
David Blaikieeb1a2722014-06-13 22:18:23 +0000645void DwarfDebug::finishVariableDefinitions() {
646 for (const auto &Var : ConcreteVariables) {
647 DIE *VariableDie = Var->getDIE();
David Blaikief73ae4f2014-08-12 00:00:31 +0000648 assert(VariableDie);
David Blaikieeb1a2722014-06-13 22:18:23 +0000649 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
650 // in the ConcreteVariables list, rather than looking it up again here.
651 // DIE::getUnit isn't simple - it walks parent pointers, etc.
Greg Clayton35630c32016-12-01 18:56:29 +0000652 DwarfCompileUnit *Unit = CUDieMap.lookup(VariableDie->getUnitDie());
David Blaikieeb1a2722014-06-13 22:18:23 +0000653 assert(Unit);
David Blaikie488393f2017-05-12 01:13:45 +0000654 Unit->finishVariableDefinition(*Var);
David Blaikieeb1a2722014-06-13 22:18:23 +0000655 }
656}
657
David Blaikief7221ad2014-05-27 18:37:43 +0000658void DwarfDebug::finishSubprogramDefinitions() {
David Blaikie07963bd2017-05-26 18:52:56 +0000659 for (const DISubprogram *SP : ProcessedSPNodes) {
660 assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
661 forBothCUs(
662 getOrCreateDwarfCompileUnit(SP->getUnit()),
663 [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); });
664 }
Eric Christopher960ac372012-11-22 00:59:49 +0000665}
666
667void DwarfDebug::finalizeModuleInfo() {
Rafael Espindola063d7252015-03-10 16:58:10 +0000668 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
669
David Blaikie3c2fff32014-05-27 18:37:48 +0000670 finishSubprogramDefinitions();
671
David Blaikieeb1a2722014-06-13 22:18:23 +0000672 finishVariableDefinitions();
673
Mehdi Amini96ab48f2017-05-29 06:32:34 +0000674 // Include the DWO file name in the hash if there's more than one CU.
675 // This handles ThinLTO's situation where imported CUs may very easily be
676 // duplicate with the same CU partially imported into another ThinLTO unit.
677 StringRef DWOName;
678 if (CUMap.size() > 1)
679 DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile;
680
Eric Christopherad10cb52013-12-04 23:24:38 +0000681 // Handle anything that needs to be done on a per-unit basis after
682 // all other generation.
David Blaikie1998a2e2014-11-01 01:11:19 +0000683 for (const auto &P : CUMap) {
684 auto &TheCU = *P.second;
Eric Christopher60eb7692013-08-12 20:27:48 +0000685 // Emit DW_AT_containing_type attribute to connect types with their
686 // vtable holding type.
David Blaikie1998a2e2014-11-01 01:11:19 +0000687 TheCU.constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000688
Eric Christopher46e23432013-12-20 04:16:18 +0000689 // Add CU specific attributes if we need to add any.
David Blaikie1998a2e2014-11-01 01:11:19 +0000690 // If we're splitting the dwarf out now that we've got the entire
691 // CU then add the dwo id to it.
692 auto *SkCU = TheCU.getSkeleton();
693 if (useSplitDwarf()) {
694 // Emit a unique identifier for this CU.
Mehdi Amini96ab48f2017-05-29 06:32:34 +0000695 uint64_t ID =
696 DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie());
David Blaikie1998a2e2014-11-01 01:11:19 +0000697 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
698 dwarf::DW_FORM_data8, ID);
699 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
700 dwarf::DW_FORM_data8, ID);
David Blaikie3ffe4dd2014-03-25 05:34:24 +0000701
David Blaikie1998a2e2014-11-01 01:11:19 +0000702 // We don't keep track of which addresses are used in which CU so this
703 // is a bit pessimistic under LTO.
Rafael Espindola063d7252015-03-10 16:58:10 +0000704 if (!AddrPool.isEmpty()) {
705 const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol();
David Blaikie1998a2e2014-11-01 01:11:19 +0000706 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
Rafael Espindola063d7252015-03-10 16:58:10 +0000707 Sym, Sym);
708 }
709 if (!SkCU->getRangeLists().empty()) {
710 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
David Blaikie1998a2e2014-11-01 01:11:19 +0000711 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
Rafael Espindola063d7252015-03-10 16:58:10 +0000712 Sym, Sym);
713 }
David Blaikie1998a2e2014-11-01 01:11:19 +0000714 }
Eric Christopherd8667202013-12-30 17:22:27 +0000715
David Blaikie1998a2e2014-11-01 01:11:19 +0000716 // If we have code split among multiple sections or non-contiguous
717 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
718 // remain in the .o file, otherwise add a DW_AT_low_pc.
719 // FIXME: We should use ranges allow reordering of code ala
720 // .subsections_via_symbols in mach-o. This would mean turning on
721 // ranges for all subprogram DIEs for mach-o.
David Blaikiea8be0a72014-11-01 01:15:24 +0000722 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
David Blaikie5b02a192014-11-03 23:10:59 +0000723 if (unsigned NumRanges = TheCU.getRanges().size()) {
724 if (NumRanges > 1)
David Blaikie1998a2e2014-11-01 01:11:19 +0000725 // A DW_AT_low_pc attribute may also be specified in combination with
726 // DW_AT_ranges to specify the default base address for use in
727 // location lists (see Section 2.6.2) and range lists (see Section
728 // 2.17.3).
729 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
David Blaikie5b02a192014-11-03 23:10:59 +0000730 else
David Blaikie72d03ef2015-05-02 02:31:49 +0000731 U.setBaseAddress(TheCU.getRanges().front().getStart());
David Blaikie5b02a192014-11-03 23:10:59 +0000732 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
Eric Christopher60eb7692013-08-12 20:27:48 +0000733 }
Amjad Aboudd7cfb482016-01-07 14:28:20 +0000734
735 auto *CUNode = cast<DICompileUnit>(P.first);
Amjad Aboud8bbce8a2016-02-01 14:09:41 +0000736 // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
737 if (CUNode->getMacros())
738 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
739 U.getMacroLabelBegin(),
740 TLOF.getDwarfMacinfoSection()->getBeginSymbol());
Eric Christopher60eb7692013-08-12 20:27:48 +0000741 }
742
Adrian Prantl833ad372017-07-26 18:48:32 +0000743 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
744 for (auto *CUNode : MMI->getModule()->debug_compile_units())
Adrian Prantl960e7662017-07-27 15:24:20 +0000745 if (CUNode->getDWOId())
Adrian Prantl833ad372017-07-26 18:48:32 +0000746 getOrCreateDwarfCompileUnit(CUNode);
747
Eric Christopher60eb7692013-08-12 20:27:48 +0000748 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000749 InfoHolder.computeSizeAndOffsets();
750 if (useSplitDwarf())
751 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +0000752}
753
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000754// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +0000755void DwarfDebug::endModule() {
Craig Toppere73658d2014-04-28 04:05:08 +0000756 assert(CurFn == nullptr);
757 assert(CurMI == nullptr);
Eric Christopher960ac372012-11-22 00:59:49 +0000758
David Blaikie0dc623c2014-10-24 17:53:38 +0000759 // If we aren't actually generating debug info (check beginModule -
760 // conditionalized on !DisableDebugInfoPrinting and the presence of the
761 // llvm.dbg.cu metadata node)
Rafael Espindolaf1a13f52015-03-11 00:51:37 +0000762 if (!MMI->hasDebugInfo())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000763 return;
Eric Christopher960ac372012-11-22 00:59:49 +0000764
Eric Christopher960ac372012-11-22 00:59:49 +0000765 // Finalize the debug info for the module.
766 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000767
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000768 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +0000769
Rafael Espindola063d7252015-03-10 16:58:10 +0000770 if (useSplitDwarf())
771 emitDebugLocDWO();
772 else
773 // Emit info into a debug loc section.
774 emitDebugLoc();
775
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000776 // Corresponding abbreviations into a abbrev section.
777 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +0000778
Rafael Espindolaf1a13f52015-03-11 00:51:37 +0000779 // Emit all the DIEs into a debug info section.
780 emitDebugInfo();
781
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000782 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +0000783 if (GenerateARangeSection)
784 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +0000785
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000786 // Emit info into a debug ranges section.
787 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +0000788
Amjad Aboudd7cfb482016-01-07 14:28:20 +0000789 // Emit info into a debug macinfo section.
790 emitDebugMacinfo();
791
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000792 if (useSplitDwarf()) {
793 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +0000794 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +0000795 emitDebugAbbrevDWO();
David Blaikie4a2f95f2014-03-18 01:17:26 +0000796 emitDebugLineDWO();
Eric Christopher962c9082013-01-15 23:56:56 +0000797 // Emit DWO addresses.
David Blaikied75fb282014-04-23 21:20:10 +0000798 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
Rafael Espindola063d7252015-03-10 16:58:10 +0000799 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000800
Eric Christophera876b822012-08-23 07:32:06 +0000801 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +0000802 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +0000803 emitAccelNames();
804 emitAccelObjC();
805 emitAccelNamespaces();
806 emitAccelTypes();
807 }
Eric Christopher6a841382012-11-19 22:42:10 +0000808
Eric Christopher4b358182013-08-30 00:40:17 +0000809 // Emit the pubnames and pubtypes sections if requested.
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000810 emitDebugPubSections();
Devang Patel04d2f2d2009-11-24 01:14:22 +0000811
Devang Pateld0701282010-08-02 17:32:15 +0000812 // clean up.
David Blaikie488393f2017-05-12 01:13:45 +0000813 // FIXME: AbstractVariables.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +0000814}
815
David Blaikie488393f2017-05-12 01:13:45 +0000816void DwarfDebug::ensureAbstractVariableIsCreated(DwarfCompileUnit &CU, InlinedVariable IV,
David Blaikie6f9e8672014-06-13 23:52:55 +0000817 const MDNode *ScopeNode) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000818 const DILocalVariable *Cleansed = nullptr;
David Blaikie488393f2017-05-12 01:13:45 +0000819 if (CU.getExistingAbstractVariable(IV, Cleansed))
David Blaikie6f9e8672014-06-13 23:52:55 +0000820 return;
David Blaikie36408e72014-06-04 23:50:52 +0000821
David Blaikie488393f2017-05-12 01:13:45 +0000822 CU.createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000823 cast<DILocalScope>(ScopeNode)));
David Blaikie36408e72014-06-04 23:50:52 +0000824}
825
David Blaikie488393f2017-05-12 01:13:45 +0000826void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(DwarfCompileUnit &CU,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000827 InlinedVariable IV, const MDNode *ScopeNode) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000828 const DILocalVariable *Cleansed = nullptr;
David Blaikie488393f2017-05-12 01:13:45 +0000829 if (CU.getExistingAbstractVariable(IV, Cleansed))
David Blaikie6f9e8672014-06-13 23:52:55 +0000830 return;
David Blaikie36408e72014-06-04 23:50:52 +0000831
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000832 if (LexicalScope *Scope =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000833 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
David Blaikie488393f2017-05-12 01:13:45 +0000834 CU.createAbstractVariable(Cleansed, Scope);
David Blaikie36408e72014-06-04 23:50:52 +0000835}
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000836
Matthias Braunef331ef2016-11-30 23:48:50 +0000837// Collect variable information from side table maintained by MF.
838void DwarfDebug::collectVariableInfoFromMFTable(
David Blaikie488393f2017-05-12 01:13:45 +0000839 DwarfCompileUnit &TheCU, DenseSet<InlinedVariable> &Processed) {
Adrian Prantlbe662712017-07-25 23:32:59 +0000840 SmallDenseMap<InlinedVariable, DbgVariable *> MFVars;
Matthias Braunef331ef2016-11-30 23:48:50 +0000841 for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +0000842 if (!VI.Var)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000843 continue;
Duncan P. N. Exon Smith78a95272015-04-16 22:12:59 +0000844 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
845 "Expected inlined-at fields to agree");
846
847 InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt());
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000848 Processed.insert(Var);
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +0000849 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000850
Devang Patelcdb7d442009-11-10 23:20:04 +0000851 // If variable scope is not found then skip this variable.
Craig Topper353eda42014-04-24 06:44:33 +0000852 if (!Scope)
Devang Patelcdb7d442009-11-10 23:20:04 +0000853 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000854
David Blaikie488393f2017-05-12 01:13:45 +0000855 ensureAbstractVariableIsCreatedIfScoped(TheCU, Var, Scope->getScopeNode());
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000856 auto RegVar = llvm::make_unique<DbgVariable>(Var.first, Var.second);
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000857 RegVar->initializeMMI(VI.Expr, VI.Slot);
Adrian Prantlbe662712017-07-25 23:32:59 +0000858 if (DbgVariable *DbgVar = MFVars.lookup(Var))
859 DbgVar->addMMIEntry(*RegVar);
860 else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) {
861 MFVars.insert({Var, RegVar.get()});
Adrian Prantlca7e4702015-02-10 23:18:28 +0000862 ConcreteVariables.push_back(std::move(RegVar));
Adrian Prantlbe662712017-07-25 23:32:59 +0000863 }
Devang Patel475d32a2009-10-06 01:26:37 +0000864 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000865}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000866
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000867// Get .debug_loc entry for the instruction range starting at MI.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000868static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000869 const DIExpression *Expr = MI->getDebugExpression();
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000870 assert(MI->getNumOperands() == 4);
Adrian Prantl418d1d12013-07-09 20:28:37 +0000871 if (MI->getOperand(0).isReg()) {
Adrian Prantlbd6d2912017-08-02 17:19:13 +0000872 auto RegOp = MI->getOperand(0);
873 auto Op1 = MI->getOperand(1);
Adrian Prantl418d1d12013-07-09 20:28:37 +0000874 // If the second operand is an immediate, this is a
875 // register-indirect address.
Adrian Prantlbd6d2912017-08-02 17:19:13 +0000876 assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset");
877 MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000878 return DebugLocEntry::Value(Expr, MLoc);
Devang Patel2442a892011-07-08 17:09:57 +0000879 }
880 if (MI->getOperand(0).isImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000881 return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
Devang Patel2442a892011-07-08 17:09:57 +0000882 if (MI->getOperand(0).isFPImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000883 return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
Devang Patel2442a892011-07-08 17:09:57 +0000884 if (MI->getOperand(0).isCImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000885 return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
Devang Patel2442a892011-07-08 17:09:57 +0000886
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000887 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +0000888}
889
Adrian Prantl941fa752016-12-05 18:04:47 +0000890/// \brief If this and Next are describing different fragments of the same
Keno Fischerf8eb6a12016-01-16 01:11:33 +0000891/// variable, merge them by appending Next's values to the current
892/// list of values.
893/// Return true if the merge was successful.
894bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) {
895 if (Begin == Next.Begin) {
Keno Fischer6c1e47a2016-02-03 21:13:33 +0000896 auto *FirstExpr = cast<DIExpression>(Values[0].Expression);
897 auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression);
Adrian Prantl941fa752016-12-05 18:04:47 +0000898 if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment())
Keno Fischer6c1e47a2016-02-03 21:13:33 +0000899 return false;
900
Adrian Prantl941fa752016-12-05 18:04:47 +0000901 // We can only merge entries if none of the fragments overlap any others.
Keno Fischer6c1e47a2016-02-03 21:13:33 +0000902 // In doing so, we can take advantage of the fact that both lists are
903 // sorted.
904 for (unsigned i = 0, j = 0; i < Values.size(); ++i) {
905 for (; j < Next.Values.size(); ++j) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000906 int res = DebugHandlerBase::fragmentCmp(
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000907 cast<DIExpression>(Values[i].Expression),
908 cast<DIExpression>(Next.Values[j].Expression));
Keno Fischer6c1e47a2016-02-03 21:13:33 +0000909 if (res == 0) // The two expressions overlap, we can't merge.
910 return false;
911 // Values[i] is entirely before Next.Values[j],
912 // so go back to the next entry of Values.
913 else if (res == -1)
914 break;
915 // Next.Values[j] is entirely before Values[i], so go on to the
916 // next entry of Next.Values.
917 }
Keno Fischerf8eb6a12016-01-16 01:11:33 +0000918 }
Keno Fischer6c1e47a2016-02-03 21:13:33 +0000919
920 addValues(Next.Values);
921 End = Next.End;
922 return true;
Keno Fischerf8eb6a12016-01-16 01:11:33 +0000923 }
924 return false;
925}
926
Adrian Prantlb1416832014-08-01 22:11:58 +0000927/// Build the location list for all DBG_VALUEs in the function that
928/// describe the same variable. If the ranges of several independent
Adrian Prantl941fa752016-12-05 18:04:47 +0000929/// fragments of the same variable overlap partially, split them up and
Adrian Prantlb1416832014-08-01 22:11:58 +0000930/// combine the ranges. The resulting DebugLocEntries are will have
931/// strict monotonically increasing begin addresses and will never
932/// overlap.
933//
934// Input:
935//
Adrian Prantl941fa752016-12-05 18:04:47 +0000936// Ranges History [var, loc, fragment ofs size]
937// 0 | [x, (reg0, fragment 0, 32)]
938// 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry
Adrian Prantlb1416832014-08-01 22:11:58 +0000939// 2 | | ...
940// 3 | [clobber reg0]
Adrian Prantl941fa752016-12-05 18:04:47 +0000941// 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of
Eric Christopherffc5ff32015-02-17 20:02:28 +0000942// x.
Adrian Prantlb1416832014-08-01 22:11:58 +0000943//
944// Output:
945//
Adrian Prantl941fa752016-12-05 18:04:47 +0000946// [0-1] [x, (reg0, fragment 0, 32)]
947// [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)]
948// [3-4] [x, (reg1, fragment 32, 32)]
949// [4- ] [x, (mem, fragment 0, 64)]
David Blaikiee1a26a62014-08-05 23:14:16 +0000950void
951DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
952 const DbgValueHistoryMap::InstrRanges &Ranges) {
Adrian Prantlcaaf0532014-08-11 21:05:57 +0000953 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
Adrian Prantlb1416832014-08-01 22:11:58 +0000954
955 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
956 const MachineInstr *Begin = I->first;
957 const MachineInstr *End = I->second;
958 assert(Begin->isDebugValue() && "Invalid History entry");
959
960 // Check if a variable is inaccessible in this range.
Adrian Prantl5e1fa852014-08-12 21:55:58 +0000961 if (Begin->getNumOperands() > 1 &&
962 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000963 OpenRanges.clear();
964 continue;
965 }
966
Adrian Prantl941fa752016-12-05 18:04:47 +0000967 // If this fragment overlaps with any open ranges, truncate them.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000968 const DIExpression *DIExpr = Begin->getDebugExpression();
David Majnemer0a16c222016-08-11 21:15:00 +0000969 auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) {
Adrian Prantl941fa752016-12-05 18:04:47 +0000970 return fragmentsOverlap(DIExpr, R.getExpression());
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000971 });
Adrian Prantlb1416832014-08-01 22:11:58 +0000972 OpenRanges.erase(Last, OpenRanges.end());
973
974 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
975 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
976
977 const MCSymbol *EndLabel;
978 if (End != nullptr)
979 EndLabel = getLabelAfterInsn(End);
980 else if (std::next(I) == Ranges.end())
Rafael Espindola07c03d32015-03-05 02:05:42 +0000981 EndLabel = Asm->getFunctionEnd();
Adrian Prantlb1416832014-08-01 22:11:58 +0000982 else
983 EndLabel = getLabelBeforeInsn(std::next(I)->first);
984 assert(EndLabel && "Forgot label after instruction ending a range!");
985
986 DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
987
988 auto Value = getDebugLocValue(Begin);
David Blaikiee1a26a62014-08-05 23:14:16 +0000989 DebugLocEntry Loc(StartLabel, EndLabel, Value);
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000990 bool couldMerge = false;
991
Adrian Prantl941fa752016-12-05 18:04:47 +0000992 // If this is a fragment, it may belong to the current DebugLocEntry.
993 if (DIExpr->isFragment()) {
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000994 // Add this value to the list of open ranges.
Adrian Prantlcaaf0532014-08-11 21:05:57 +0000995 OpenRanges.push_back(Value);
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000996
Adrian Prantl941fa752016-12-05 18:04:47 +0000997 // Attempt to add the fragment to the last entry.
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000998 if (!DebugLoc.empty())
999 if (DebugLoc.back().MergeValues(Loc))
1000 couldMerge = true;
1001 }
1002
1003 if (!couldMerge) {
1004 // Need to add a new DebugLocEntry. Add all values from still
Adrian Prantl941fa752016-12-05 18:04:47 +00001005 // valid non-overlapping fragments.
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +00001006 if (OpenRanges.size())
1007 Loc.addValues(OpenRanges);
1008
Adrian Prantlb1416832014-08-01 22:11:58 +00001009 DebugLoc.push_back(std::move(Loc));
1010 }
Adrian Prantle09ee3f2014-08-11 20:59:28 +00001011
1012 // Attempt to coalesce the ranges of two otherwise identical
1013 // DebugLocEntries.
1014 auto CurEntry = DebugLoc.rbegin();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001015 DEBUG({
1016 dbgs() << CurEntry->getValues().size() << " Values:\n";
Adrian Prantl6f8c1b62015-05-26 20:06:51 +00001017 for (auto &Value : CurEntry->getValues())
Adrian Prantldba58fb2016-02-29 22:28:22 +00001018 Value.dump();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001019 dbgs() << "-----\n";
1020 });
Adrian Prantl75707312015-05-26 20:06:48 +00001021
1022 auto PrevEntry = std::next(CurEntry);
1023 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
1024 DebugLoc.pop_back();
Adrian Prantlb1416832014-08-01 22:11:58 +00001025 }
1026}
1027
David Blaikie488393f2017-05-12 01:13:45 +00001028DbgVariable *DwarfDebug::createConcreteVariable(DwarfCompileUnit &TheCU,
1029 LexicalScope &Scope,
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001030 InlinedVariable IV) {
David Blaikie488393f2017-05-12 01:13:45 +00001031 ensureAbstractVariableIsCreatedIfScoped(TheCU, IV, Scope.getScopeNode());
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001032 ConcreteVariables.push_back(
1033 llvm::make_unique<DbgVariable>(IV.first, IV.second));
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001034 InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get());
1035 return ConcreteVariables.back().get();
1036}
Adrian Prantlb1416832014-08-01 22:11:58 +00001037
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001038/// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1039/// enclosing lexical scope. The check ensures there are no other instructions
1040/// in the same lexical scope preceding the DBG_VALUE and that its range is
1041/// either open or otherwise rolls off the end of the scope.
1042static bool validThroughout(LexicalScopes &LScopes,
1043 const MachineInstr *DbgValue,
1044 const MachineInstr *RangeEnd) {
1045 assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
1046 auto MBB = DbgValue->getParent();
1047 auto DL = DbgValue->getDebugLoc();
1048 auto *LScope = LScopes.findLexicalScope(DL);
1049 // Scope doesn't exist; this is a dead DBG_VALUE.
1050 if (!LScope)
Adrian Prantlfb2add22016-02-29 19:49:46 +00001051 return false;
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001052 auto &LSRange = LScope->getRanges();
1053 if (LSRange.size() == 0)
1054 return false;
1055
1056 // Determine if the DBG_VALUE is valid at the beginning of its lexical block.
1057 const MachineInstr *LScopeBegin = LSRange.front().first;
1058 // Early exit if the lexical scope begins outside of the current block.
1059 if (LScopeBegin->getParent() != MBB)
1060 return false;
1061 MachineBasicBlock::const_reverse_iterator Pred(DbgValue);
1062 for (++Pred; Pred != MBB->rend(); ++Pred) {
1063 if (Pred->getFlag(MachineInstr::FrameSetup))
1064 break;
1065 auto PredDL = Pred->getDebugLoc();
Adrian Prantl25422dc2017-06-20 21:08:52 +00001066 if (!PredDL || Pred->isMetaInstruction())
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001067 continue;
1068 // Check whether the instruction preceding the DBG_VALUE is in the same
1069 // (sub)scope as the DBG_VALUE.
Adrian Prantl25422dc2017-06-20 21:08:52 +00001070 if (DL->getScope() == PredDL->getScope())
1071 return false;
1072 auto *PredScope = LScopes.findLexicalScope(PredDL);
1073 if (!PredScope || LScope->dominates(PredScope))
Adrian Prantlfb2add22016-02-29 19:49:46 +00001074 return false;
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001075 }
1076
1077 // If the range of the DBG_VALUE is open-ended, report success.
1078 if (!RangeEnd)
1079 return true;
1080
1081 // Fail if there are instructions belonging to our scope in another block.
1082 const MachineInstr *LScopeEnd = LSRange.back().second;
1083 if (LScopeEnd->getParent() != MBB)
1084 return false;
1085
1086 // Single, constant DBG_VALUEs in the prologue are promoted to be live
1087 // throughout the function. This is a hack, presumably for DWARF v2 and not
1088 // necessarily correct. It would be much better to use a dbg.declare instead
1089 // if we know the constant is live throughout the scope.
1090 if (DbgValue->getOperand(0).isImm() && MBB->pred_empty())
1091 return true;
1092
1093 return false;
Adrian Prantlfb2add22016-02-29 19:49:46 +00001094}
1095
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001096// Find variables for each lexical scope.
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +00001097void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001098 const DISubprogram *SP,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001099 DenseSet<InlinedVariable> &Processed) {
Eric Christopher270a12c2013-07-03 21:37:03 +00001100 // Grab the variable info that was squirreled away in the MMI side-table.
David Blaikie488393f2017-05-12 01:13:45 +00001101 collectVariableInfoFromMFTable(TheCU, Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001102
Alexey Samsonov0436caa2014-04-30 23:02:40 +00001103 for (const auto &I : DbgValues) {
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001104 InlinedVariable IV = I.first;
1105 if (Processed.count(IV))
Devang Patel490c8ab2010-05-20 19:57:06 +00001106 continue;
1107
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001108 // Instruction ranges, specifying where IV is accessible.
Alexey Samsonovbb2990d2014-05-27 23:09:50 +00001109 const auto &Ranges = I.second;
1110 if (Ranges.empty())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001111 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00001112
Craig Topper353eda42014-04-24 06:44:33 +00001113 LexicalScope *Scope = nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001114 if (const DILocation *IA = IV.second)
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001115 Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
Duncan P. N. Exon Smithb4749372015-02-17 00:02:27 +00001116 else
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001117 Scope = LScopes.findLexicalScope(IV.first->getScope());
Devang Patel490c8ab2010-05-20 19:57:06 +00001118 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001119 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001120 continue;
1121
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001122 Processed.insert(IV);
David Blaikie488393f2017-05-12 01:13:45 +00001123 DbgVariable *RegVar = createConcreteVariable(TheCU, *Scope, IV);
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001124
Alexey Samsonovbb2990d2014-05-27 23:09:50 +00001125 const MachineInstr *MInsn = Ranges.front().first;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001126 assert(MInsn->isDebugValue() && "History must begin with debug value");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001127
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001128 // Check if there is a single DBG_VALUE, valid throughout the var's scope.
Adrian Prantlfb2add22016-02-29 19:49:46 +00001129 if (Ranges.size() == 1 &&
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001130 validThroughout(LScopes, MInsn, Ranges.front().second)) {
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001131 RegVar->initializeDbgValue(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001132 continue;
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001133 }
Devang Patel9fc11702010-05-25 23:40:22 +00001134
Eric Christopher59cc0712013-01-28 17:33:26 +00001135 // Handle multiple DBG_VALUE instructions describing one variable.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001136 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001137
Adrian Prantlb1416832014-08-01 22:11:58 +00001138 // Build the location list for this variable.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001139 SmallVector<DebugLocEntry, 8> Entries;
1140 buildLocationList(Entries, Ranges);
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001141
Adrian Prantl693e8de2016-02-29 17:06:46 +00001142 // If the variable has a DIBasicType, extract it. Basic types cannot have
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001143 // unique identifiers, so don't bother resolving the type with the
1144 // identifier map.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001145 const DIBasicType *BT = dyn_cast<DIBasicType>(
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001146 static_cast<const Metadata *>(IV.first->getType()));
1147
Adrian Prantl92da14b2015-03-02 22:02:33 +00001148 // Finalize the entry by lowering it into a DWARF bytestream.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001149 for (auto &Entry : Entries)
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001150 Entry.finalize(*Asm, List, BT);
Devang Patela3e9c9c2010-03-15 18:33:46 +00001151 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001152
1153 // Collect info for variables that were optimized out.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001154 for (const DILocalVariable *DV : SP->getVariables()) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001155 if (Processed.insert(InlinedVariable(DV, nullptr)).second)
1156 if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
David Blaikie488393f2017-05-12 01:13:45 +00001157 createConcreteVariable(TheCU, *Scope, InlinedVariable(DV, nullptr));
Devang Patele0a94bf2010-05-14 21:01:35 +00001158 }
Devang Patel9fc11702010-05-25 23:40:22 +00001159}
Devang Patele0a94bf2010-05-14 21:01:35 +00001160
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001161// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001162void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +00001163 DebugHandlerBase::beginInstruction(MI);
1164 assert(CurMI);
1165
Matthias Braunf1caa282017-12-15 22:22:58 +00001166 const auto *SP = MI->getMF()->getFunction().getSubprogram();
David Blaikie7f2b7172017-05-26 17:05:15 +00001167 if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1168 return;
1169
Paul Robinson4fa7b57a2016-12-09 19:15:32 +00001170 // Check if source location changes, but ignore DBG_VALUE and CFI locations.
Robert Lougher51529eb2017-09-12 18:23:15 +00001171 if (MI->isMetaInstruction())
Paul Robinsonf428c9b2016-11-22 19:46:51 +00001172 return;
1173 const DebugLoc &DL = MI->getDebugLoc();
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001174 // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1175 // the last line number actually emitted, to see if it was line 0.
1176 unsigned LastAsmLine =
1177 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
Paul Robinson96de8c72016-11-29 22:41:16 +00001178
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001179 if (DL == PrevInstLoc) {
1180 // If we have an ongoing unspecified location, nothing to do here.
1181 if (!DL)
1182 return;
1183 // We have an explicit location, same as the previous location.
1184 // But we might be coming back to it after a line 0 record.
1185 if (LastAsmLine == 0 && DL.getLine() != 0) {
1186 // Reinstate the source location but not marked as a statement.
1187 const MDNode *Scope = DL.getScope();
1188 recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0);
Paul Robinson37a13dd2016-11-30 22:49:55 +00001189 }
1190 return;
1191 }
1192
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001193 if (!DL) {
1194 // We have an unspecified location, which might want to be line 0.
1195 // If we have already emitted a line-0 record, don't repeat it.
1196 if (LastAsmLine == 0)
1197 return;
1198 // If user said Don't Do That, don't do that.
1199 if (UnknownLocations == Disable)
1200 return;
1201 // See if we have a reason to emit a line-0 record now.
1202 // Reasons to emit a line-0 record include:
1203 // - User asked for it (UnknownLocations).
1204 // - Instruction has a label, so it's referenced from somewhere else,
1205 // possibly debug information; we want it to have a source location.
1206 // - Instruction is at the top of a block; we don't want to inherit the
1207 // location from the physically previous (maybe unrelated) block.
1208 if (UnknownLocations == Enable || PrevLabel ||
1209 (PrevInstBB && PrevInstBB != MI->getParent())) {
Paul Robinson8fec3da2016-12-14 00:27:35 +00001210 // Preserve the file and column numbers, if we can, to save space in
1211 // the encoded line table.
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001212 // Do not update PrevInstLoc, it remembers the last non-0 line.
Paul Robinson8fec3da2016-12-14 00:27:35 +00001213 const MDNode *Scope = nullptr;
1214 unsigned Column = 0;
1215 if (PrevInstLoc) {
1216 Scope = PrevInstLoc.getScope();
1217 Column = PrevInstLoc.getCol();
1218 }
1219 recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001220 }
1221 return;
1222 }
1223
1224 // We have an explicit location, different from the previous location.
1225 // Don't repeat a line-0 record, but otherwise emit the new location.
1226 // (The new location might be an explicit line 0, which we do emit.)
Paul Robinson2dfb6882016-12-16 23:54:33 +00001227 if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0)
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001228 return;
Paul Robinsonf428c9b2016-11-22 19:46:51 +00001229 unsigned Flags = 0;
Paul Robinsonf428c9b2016-11-22 19:46:51 +00001230 if (DL == PrologEndLoc) {
1231 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
1232 PrologEndLoc = DebugLoc();
1233 }
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001234 // If the line changed, we call that a new statement; unless we went to
1235 // line 0 and came back, in which case it is not a new statement.
1236 unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
1237 if (DL.getLine() && DL.getLine() != OldLine)
Paul Robinsonf428c9b2016-11-22 19:46:51 +00001238 Flags |= DWARF2_FLAG_IS_STMT;
1239
1240 const MDNode *Scope = DL.getScope();
1241 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001242
1243 // If we're not at line 0, remember this location.
1244 if (DL.getLine())
1245 PrevInstLoc = DL;
Devang Patel75cc16c2009-10-01 20:31:14 +00001246}
1247
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001248static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1249 // First known non-DBG_VALUE and non-frame setup location marks
1250 // the beginning of the function body.
1251 for (const auto &MBB : *MF)
1252 for (const auto &MI : MBB)
Adrian Prantlfb31da12017-05-22 20:47:09 +00001253 if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
Michael Kuperstein2b3c16c2015-10-08 07:48:49 +00001254 MI.getDebugLoc())
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001255 return MI.getDebugLoc();
1256 return DebugLoc();
1257}
1258
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001259// Gather pre-function debug information. Assumes being called immediately
1260// after the function entry point has been emitted.
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001261void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001262 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001263
Matthias Braunf1caa282017-12-15 22:22:58 +00001264 auto *SP = MF->getFunction().getSubprogram();
David Blaikie2c78f182017-05-25 23:11:28 +00001265 assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode());
David Blaikie07963bd2017-05-26 18:52:56 +00001266 if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
Adrian Prantl5992a722016-04-08 22:43:03 +00001267 return;
David Blaikie07963bd2017-05-26 18:52:56 +00001268
1269 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
David Blaikie2c78f182017-05-25 23:11:28 +00001270
1271 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1272 // belongs to so that we add to the correct per-cu line table in the
1273 // non-asm case.
Lang Hames9ff69c82015-04-24 19:11:51 +00001274 if (Asm->OutStreamer->hasRawTextSupport())
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001275 // Use a single line table if we are generating assembly.
Lang Hames9ff69c82015-04-24 19:11:51 +00001276 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
Manman Ren9d4c7352013-05-21 00:57:22 +00001277 else
David Blaikie07963bd2017-05-26 18:52:56 +00001278 Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001279
Devang Patel34a66202011-05-11 19:22:19 +00001280 // Record beginning of function.
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001281 PrologEndLoc = findPrologueEndLoc(MF);
David Blaikie2c78f182017-05-25 23:11:28 +00001282 if (PrologEndLoc) {
Saleem Abdulrasoolfc07c722015-01-24 20:19:45 +00001283 // We'd like to list the prologue as "not statements" but GDB behaves
1284 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikie2c78f182017-05-25 23:11:28 +00001285 auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +00001286 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001287 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001288}
1289
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001290void DwarfDebug::skippedNonDebugFunction() {
1291 // If we don't have a subprogram for this function then there will be a hole
1292 // in the range information. Keep note of this by setting the previously used
1293 // section to nullptr.
1294 PrevCU = nullptr;
1295 CurFn = nullptr;
1296}
1297
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001298// Gather and emit post-function debug information.
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001299void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +00001300 const DISubprogram *SP = MF->getFunction().getSubprogram();
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001301
David Blaikiee75f9632014-10-14 17:12:02 +00001302 assert(CurFn == MF &&
1303 "endFunction should be called with the same function as beginFunction");
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001304
Eric Christopher4287a492013-12-09 23:57:44 +00001305 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Lang Hames9ff69c82015-04-24 19:11:51 +00001306 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001307
Devang Patel3acc70e2011-08-15 22:04:40 +00001308 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
David Blaikie3e3eb332016-12-15 23:17:52 +00001309 assert(!FnScope || SP == FnScope->getScopeNode());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001310 DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit());
David Blaikie263a0082014-10-23 00:06:27 +00001311
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001312 DenseSet<InlinedVariable> ProcessedVars;
David Blaikie263a0082014-10-23 00:06:27 +00001313 collectVariableInfo(TheCU, SP, ProcessedVars);
Devang Patel3acc70e2011-08-15 22:04:40 +00001314
David Blaikie3a7ce252014-09-19 17:03:16 +00001315 // Add the range of this function to the list of ranges for the CU.
Rafael Espindola07c03d32015-03-05 02:05:42 +00001316 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
David Blaikie3a7ce252014-09-19 17:03:16 +00001317
1318 // Under -gmlt, skip building the subprogram if there are no inlined
Dehao Chen1ce8d6c2017-01-19 00:44:11 +00001319 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
1320 // is still needed as we need its source location.
Dehao Chen0944a8c2017-02-01 22:45:09 +00001321 if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
Dehao Chen1ce8d6c2017-01-19 00:44:11 +00001322 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
David Blaikiee1c79742014-09-30 21:28:32 +00001323 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
David Blaikie80e5b1e2014-10-24 17:57:34 +00001324 assert(InfoHolder.getScopeVariables().empty());
David Blaikie3a7ce252014-09-19 17:03:16 +00001325 PrevLabel = nullptr;
1326 CurFn = nullptr;
1327 return;
1328 }
1329
Adrian Prantl049d21c2014-10-13 20:44:58 +00001330#ifndef NDEBUG
1331 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1332#endif
Devang Patel7e623022011-08-10 20:55:27 +00001333 // Construct abstract scopes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001334 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001335 auto *SP = cast<DISubprogram>(AScope->getScopeNode());
David Blaikie4abe19e2014-05-12 18:23:35 +00001336 // Collect info for variables that were optimized out.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001337 for (const DILocalVariable *DV : SP->getVariables()) {
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001338 if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
David Blaikie4abe19e2014-05-12 18:23:35 +00001339 continue;
David Blaikie488393f2017-05-12 01:13:45 +00001340 ensureAbstractVariableIsCreated(TheCU, InlinedVariable(DV, nullptr),
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001341 DV->getScope());
Adrian Prantl049d21c2014-10-13 20:44:58 +00001342 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1343 && "ensureAbstractVariableIsCreated inserted abstract scopes");
Devang Patel5c0f85c2010-06-25 22:07:34 +00001344 }
David Blaikie488393f2017-05-12 01:13:45 +00001345 constructAbstractSubprogramScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001346 }
Eric Christopher6a841382012-11-19 22:42:10 +00001347
David Blaikie38b74bf2016-12-15 23:37:38 +00001348 ProcessedSPNodes.insert(SP);
David Blaikie3e3eb332016-12-15 23:17:52 +00001349 TheCU.constructSubprogramScopeDIE(SP, FnScope);
David Blaikie3a443c22014-11-04 22:12:25 +00001350 if (auto *SkelCU = TheCU.getSkeleton())
David Blaikiea01f2952016-08-24 18:29:49 +00001351 if (!LScopes.getAbstractScopesList().empty() &&
1352 TheCU.getCUNode()->getSplitDebugInlining())
David Blaikie3e3eb332016-12-15 23:17:52 +00001353 SkelCU->constructSubprogramScopeDIE(SP, FnScope);
Devang Patel3acc70e2011-08-15 22:04:40 +00001354
Bill Wendling2b128d72009-05-20 23:19:06 +00001355 // Clear debug info
David Blaikie825bdd22014-05-21 22:41:17 +00001356 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1357 // DbgVariables except those that are also in AbstractVariables (since they
1358 // can be used cross-function)
David Blaikie80e5b1e2014-10-24 17:57:34 +00001359 InfoHolder.getScopeVariables().clear();
Craig Topper353eda42014-04-24 06:44:33 +00001360 PrevLabel = nullptr;
1361 CurFn = nullptr;
Bill Wendling2b128d72009-05-20 23:19:06 +00001362}
1363
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001364// Register a source line with debug info. Returns the unique label that was
1365// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001366void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1367 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001368 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001369 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001370 unsigned Src = 1;
Diego Novillo282450d2014-03-03 18:53:17 +00001371 unsigned Discriminator = 0;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001372 if (auto *Scope = cast_or_null<DIScope>(S)) {
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +00001373 Fn = Scope->getFilename();
1374 Dir = Scope->getDirectory();
Paul Robinsonbb921372017-09-07 22:15:44 +00001375 if (Line != 0 && getDwarfVersion() >= 4)
1376 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
Dehao Chen6e0c8442016-10-07 15:21:31 +00001377 Discriminator = LBF->getDiscriminator();
Dan Gohman50849c62010-05-05 23:41:32 +00001378
Lang Hames9ff69c82015-04-24 19:11:51 +00001379 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
David Blaikiec33b3cd2014-04-22 21:27:37 +00001380 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
Paul Robinson29f5f982018-01-09 23:31:48 +00001381 .getOrCreateSourceID(Fn, Dir, nullptr);
Dan Gohman50849c62010-05-05 23:41:32 +00001382 }
Lang Hames9ff69c82015-04-24 19:11:51 +00001383 Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1384 Discriminator, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001385}
1386
Bill Wendling806535f2009-05-20 23:22:40 +00001387//===----------------------------------------------------------------------===//
1388// Emit Methods
1389//===----------------------------------------------------------------------===//
1390
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001391// Emit the debug info section.
1392void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00001393 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Rafael Espindola063d7252015-03-10 16:58:10 +00001394 Holder.emitUnits(/* UseOffsets */ false);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001395}
1396
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001397// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00001398void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00001399 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00001400
1401 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00001402}
Bill Wendling480ff322009-05-20 23:21:38 +00001403
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001404void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section,
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001405 StringRef TableName) {
David Blaikie6741bb02014-09-11 21:12:48 +00001406 Accel.FinalizeTable(Asm, TableName);
Lang Hames9ff69c82015-04-24 19:11:51 +00001407 Asm->OutStreamer->SwitchSection(Section);
Eric Christopher4996c702011-11-07 09:24:32 +00001408
1409 // Emit the full data.
Rafael Espindola063d7252015-03-10 16:58:10 +00001410 Accel.emit(Asm, Section->getBeginSymbol(), this);
David Blaikie6741bb02014-09-11 21:12:48 +00001411}
1412
1413// Emit visible names into a hashed accelerator table section.
1414void DwarfDebug::emitAccelNames() {
1415 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001416 "Names");
Eric Christopher4996c702011-11-07 09:24:32 +00001417}
1418
Eric Christopher48fef592012-12-20 21:58:40 +00001419// Emit objective C classes and categories into a hashed accelerator table
1420// section.
Eric Christopher4996c702011-11-07 09:24:32 +00001421void DwarfDebug::emitAccelObjC() {
David Blaikie6741bb02014-09-11 21:12:48 +00001422 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001423 "ObjC");
Eric Christopher4996c702011-11-07 09:24:32 +00001424}
1425
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001426// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00001427void DwarfDebug::emitAccelNamespaces() {
David Blaikie6741bb02014-09-11 21:12:48 +00001428 emitAccel(AccelNamespace,
1429 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001430 "namespac");
Eric Christopher4996c702011-11-07 09:24:32 +00001431}
1432
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001433// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00001434void DwarfDebug::emitAccelTypes() {
David Blaikie6741bb02014-09-11 21:12:48 +00001435 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001436 "types");
Eric Christopher4996c702011-11-07 09:24:32 +00001437}
1438
Eric Christopherdd1a0122013-09-13 00:35:05 +00001439// Public name handling.
1440// The format for the various pubnames:
1441//
1442// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1443// for the DIE that is named.
1444//
1445// gnu pubnames - offset/index value/name tuples where the offset is the offset
1446// into the CU and the index value is computed according to the type of value
1447// for the DIE that is named.
1448//
1449// For type units the offset is the offset of the skeleton DIE. For split dwarf
1450// it's the offset within the debug_info/debug_types dwo section, however, the
1451// reference in the pubname header doesn't change.
1452
1453/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00001454static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00001455 const DIE *Die) {
David Blaikiea0e3c752017-02-03 00:44:18 +00001456 // Entities that ended up only in a Type Unit reference the CU instead (since
1457 // the pub entry has offsets within the CU there's no real offset that can be
1458 // provided anyway). As it happens all such entities (namespaces and types,
1459 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
1460 // not to be true it would be necessary to persist this information from the
1461 // point at which the entry is added to the index data structure - since by
1462 // the time the index is built from that, the original type/namespace DIE in a
1463 // type unit has already been destroyed so it can't be queried for properties
1464 // like tag, etc.
1465 if (Die->getTag() == dwarf::DW_TAG_compile_unit)
1466 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
1467 dwarf::GIEL_EXTERNAL);
Eric Christopherd2b497b2013-10-16 01:37:49 +00001468 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1469
1470 // We could have a specification DIE that has our most of our knowledge,
1471 // look for that now.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +00001472 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1473 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001474 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
Eric Christopherd2b497b2013-10-16 01:37:49 +00001475 Linkage = dwarf::GIEL_EXTERNAL;
1476 } else if (Die->findAttribute(dwarf::DW_AT_external))
1477 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001478
1479 switch (Die->getTag()) {
1480 case dwarf::DW_TAG_class_type:
1481 case dwarf::DW_TAG_structure_type:
1482 case dwarf::DW_TAG_union_type:
1483 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00001484 return dwarf::PubIndexEntryDescriptor(
1485 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1486 ? dwarf::GIEL_STATIC
1487 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001488 case dwarf::DW_TAG_typedef:
1489 case dwarf::DW_TAG_base_type:
1490 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00001491 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001492 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00001493 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001494 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00001495 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001496 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00001497 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001498 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00001499 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1500 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001501 default:
David Blaikie8dec4072013-09-19 20:40:26 +00001502 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001503 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001504}
1505
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001506/// emitDebugPubSections - Emit visible names and types into debug pubnames and
1507/// pubtypes sections.
1508void DwarfDebug::emitDebugPubSections() {
David Blaikiec3d9e9e2014-03-06 01:42:00 +00001509 for (const auto &NU : CUMap) {
1510 DwarfCompileUnit *TheU = NU.second;
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001511 if (!TheU->hasDwarfPubSections())
David Blaikiece2f1cb2014-03-11 23:35:06 +00001512 continue;
1513
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001514 bool GnuStyle = TheU->getCUNode()->getGnuPubnames();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001515
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001516 Asm->OutStreamer->SwitchSection(
1517 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1518 : Asm->getObjFileLowering().getDwarfPubNamesSection());
1519 emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001520
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001521 Asm->OutStreamer->SwitchSection(
1522 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1523 : Asm->getObjFileLowering().getDwarfPubTypesSection());
1524 emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001525 }
1526}
1527
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001528void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
1529 DwarfCompileUnit *TheU,
1530 const StringMap<const DIE *> &Globals) {
1531 if (auto *Skeleton = TheU->getSkeleton())
1532 TheU = Skeleton;
Eric Christopher8b3737f2013-09-13 00:34:58 +00001533
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001534 // Emit the header.
1535 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1536 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1537 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1538 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1539
1540 Asm->OutStreamer->EmitLabel(BeginLabel);
1541
1542 Asm->OutStreamer->AddComment("DWARF Version");
1543 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
1544
1545 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
1546 Asm->emitDwarfSymbolReference(TheU->getLabelBegin());
1547
1548 Asm->OutStreamer->AddComment("Compilation Unit Length");
1549 Asm->EmitInt32(TheU->getLength());
1550
1551 // Emit the pubnames for this compilation unit.
1552 for (const auto &GI : Globals) {
1553 const char *Name = GI.getKeyData();
1554 const DIE *Entity = GI.second;
1555
1556 Asm->OutStreamer->AddComment("DIE offset");
1557 Asm->EmitInt32(Entity->getOffset());
1558
1559 if (GnuStyle) {
1560 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
1561 Asm->OutStreamer->AddComment(
1562 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1563 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
1564 Asm->EmitInt8(Desc.toBits());
1565 }
1566
1567 Asm->OutStreamer->AddComment("External Name");
1568 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1569 }
1570
1571 Asm->OutStreamer->AddComment("End Mark");
1572 Asm->EmitInt32(0);
1573 Asm->OutStreamer->EmitLabel(EndLabel);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001574}
1575
Amjad Aboudc0778412016-01-24 08:18:55 +00001576/// Emit null-terminated strings into a debug str section.
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001577void DwarfDebug::emitDebugStr() {
Eric Christopherf8194852013-12-05 18:06:10 +00001578 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001579 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1580}
1581
Eric Christopher29e874d2014-03-07 22:40:37 +00001582void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001583 const DebugLocStream::Entry &Entry) {
1584 auto &&Comments = DebugLocs.getComments(Entry);
1585 auto Comment = Comments.begin();
1586 auto End = Comments.end();
1587 for (uint8_t Byte : DebugLocs.getBytes(Entry))
Adrian Prantl92da14b2015-03-02 22:02:33 +00001588 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
Adrian Prantlb1416832014-08-01 22:11:58 +00001589}
1590
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001591static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
Adrian Prantl92da14b2015-03-02 22:02:33 +00001592 ByteStreamer &Streamer,
1593 const DebugLocEntry::Value &Value,
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001594 DwarfExpression &DwarfExpr) {
Adrian Prantl956484b2017-03-20 21:35:09 +00001595 auto *DIExpr = Value.getExpression();
1596 DIExpressionCursor ExprCursor(DIExpr);
1597 DwarfExpr.addFragmentOffset(DIExpr);
Adrian Prantlb1416832014-08-01 22:11:58 +00001598 // Regular entry.
Adrian Prantle19e5ef2014-04-27 18:25:40 +00001599 if (Value.isInt()) {
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001600 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1601 BT->getEncoding() == dwarf::DW_ATE_signed_char))
Adrian Prantla63b8e82017-03-16 17:42:45 +00001602 DwarfExpr.addSignedConstant(Value.getInt());
Adrian Prantl66f25952015-01-13 00:04:06 +00001603 else
Adrian Prantla63b8e82017-03-16 17:42:45 +00001604 DwarfExpr.addUnsignedConstant(Value.getInt());
Adrian Prantle19e5ef2014-04-27 18:25:40 +00001605 } else if (Value.isLocation()) {
Adrian Prantl956484b2017-03-20 21:35:09 +00001606 MachineLocation Location = Value.getLoc();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001607 if (Location.isIndirect())
1608 DwarfExpr.setMemoryLocationKind();
Adrian Prantl2049c0d2017-08-02 15:22:17 +00001609 DIExpressionCursor Cursor(DIExpr);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001610 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001611 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
Adrian Prantl956484b2017-03-20 21:35:09 +00001612 return;
1613 return DwarfExpr.addExpression(std::move(Cursor));
Adrian Prantl3e9c8872016-04-08 00:38:37 +00001614 } else if (Value.isConstantFP()) {
1615 APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
Adrian Prantla63b8e82017-03-16 17:42:45 +00001616 DwarfExpr.addUnsignedConstant(RawBytes);
Eric Christopher29e874d2014-03-07 22:40:37 +00001617 }
Adrian Prantla63b8e82017-03-16 17:42:45 +00001618 DwarfExpr.addExpression(std::move(ExprCursor));
Eric Christopher29e874d2014-03-07 22:40:37 +00001619}
1620
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001621void DebugLocEntry::finalize(const AsmPrinter &AP,
1622 DebugLocStream::ListBuilder &List,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001623 const DIBasicType *BT) {
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001624 DebugLocStream::EntryBuilder Entry(List, Begin, End);
1625 BufferByteStreamer Streamer = Entry.getStreamer();
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001626 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001627 const DebugLocEntry::Value &Value = Values[0];
Adrian Prantl941fa752016-12-05 18:04:47 +00001628 if (Value.isFragment()) {
1629 // Emit all fragments that belong to the same variable and range.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001630 assert(llvm::all_of(Values, [](DebugLocEntry::Value P) {
Adrian Prantl941fa752016-12-05 18:04:47 +00001631 return P.isFragment();
1632 }) && "all values are expected to be fragments");
Adrian Prantl92da14b2015-03-02 22:02:33 +00001633 assert(std::is_sorted(Values.begin(), Values.end()) &&
Adrian Prantl941fa752016-12-05 18:04:47 +00001634 "fragments are expected to be sorted");
Duncan P. N. Exon Smithc82570b2015-04-13 18:53:11 +00001635
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001636 for (auto Fragment : Values)
1637 emitDebugLocValue(AP, BT, Streamer, Fragment, DwarfExpr);
1638
Adrian Prantl92da14b2015-03-02 22:02:33 +00001639 } else {
Adrian Prantl941fa752016-12-05 18:04:47 +00001640 assert(Values.size() == 1 && "only fragments may have >1 value");
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001641 emitDebugLocValue(AP, BT, Streamer, Value, DwarfExpr);
Adrian Prantl92da14b2015-03-02 22:02:33 +00001642 }
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001643 DwarfExpr.finalize();
Adrian Prantl92da14b2015-03-02 22:02:33 +00001644}
1645
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001646void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
Duncan P. N. Exon Smith653c1092015-05-06 19:11:20 +00001647 // Emit the size.
Lang Hames9ff69c82015-04-24 19:11:51 +00001648 Asm->OutStreamer->AddComment("Loc expr size");
Duncan P. N. Exon Smith653c1092015-05-06 19:11:20 +00001649 Asm->EmitInt16(DebugLocs.getBytes(Entry).size());
1650
David Blaikie0e84adc2014-04-01 16:17:41 +00001651 // Emit the entry.
1652 APByteStreamer Streamer(*Asm);
1653 emitDebugLocEntry(Streamer, Entry);
David Blaikie0e84adc2014-04-01 16:17:41 +00001654}
1655
Eric Christopher9046f942013-07-02 21:36:07 +00001656// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00001657void DwarfDebug::emitDebugLoc() {
David Blaikie07963bd2017-05-26 18:52:56 +00001658 if (DebugLocs.getLists().empty())
1659 return;
1660
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001661 // Start the dwarf loc section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001662 Asm->OutStreamer->SwitchSection(
David Blaikie94c1d7f2014-04-02 01:50:20 +00001663 Asm->getObjFileLowering().getDwarfLocSection());
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001664 unsigned char Size = Asm->MAI->getCodePointerSize();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001665 for (const auto &List : DebugLocs.getLists()) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001666 Asm->OutStreamer->EmitLabel(List.Label);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001667 const DwarfCompileUnit *CU = List.CU;
1668 for (const auto &Entry : DebugLocs.getEntries(List)) {
Eric Christopher384f3fe2014-03-20 19:16:16 +00001669 // Set up the range. This range is relative to the entry point of the
1670 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1671 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
David Blaikiece343492014-11-03 21:15:30 +00001672 if (auto *Base = CU->getBaseAddress()) {
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001673 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1674 Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
Eric Christopher384f3fe2014-03-20 19:16:16 +00001675 } else {
Lang Hames9ff69c82015-04-24 19:11:51 +00001676 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1677 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
Eric Christopher384f3fe2014-03-20 19:16:16 +00001678 }
David Blaikie94c1d7f2014-04-02 01:50:20 +00001679
David Blaikie0e84adc2014-04-01 16:17:41 +00001680 emitDebugLocEntryLocation(Entry);
Devang Patel9fc11702010-05-25 23:40:22 +00001681 }
Lang Hames9ff69c82015-04-24 19:11:51 +00001682 Asm->OutStreamer->EmitIntValue(0, Size);
1683 Asm->OutStreamer->EmitIntValue(0, Size);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001684 }
1685}
1686
1687void DwarfDebug::emitDebugLocDWO() {
Lang Hames9ff69c82015-04-24 19:11:51 +00001688 Asm->OutStreamer->SwitchSection(
David Blaikie94c1d7f2014-04-02 01:50:20 +00001689 Asm->getObjFileLowering().getDwarfLocDWOSection());
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001690 for (const auto &List : DebugLocs.getLists()) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001691 Asm->OutStreamer->EmitLabel(List.Label);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001692 for (const auto &Entry : DebugLocs.getEntries(List)) {
David Blaikie94c1d7f2014-04-02 01:50:20 +00001693 // Just always use start_length for now - at least that's one address
1694 // rather than two. We could get fancier and try to, say, reuse an
1695 // address we know we've emitted elsewhere (the start of the function?
1696 // The start of the CU or CU subrange that encloses this range?)
Adrian Prantlc4fbbcf2016-10-28 17:59:50 +00001697 Asm->EmitInt8(dwarf::DW_LLE_startx_length);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001698 unsigned idx = AddrPool.getIndex(Entry.BeginSym);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001699 Asm->EmitULEB128(idx);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001700 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001701
1702 emitDebugLocEntryLocation(Entry);
David Blaikie9c550ac2014-03-25 01:44:02 +00001703 }
Adrian Prantlc4fbbcf2016-10-28 17:59:50 +00001704 Asm->EmitInt8(dwarf::DW_LLE_end_of_list);
Devang Patel9fc11702010-05-25 23:40:22 +00001705 }
Bill Wendling480ff322009-05-20 23:21:38 +00001706}
1707
Richard Mitton21101b32013-09-19 23:21:01 +00001708struct ArangeSpan {
1709 const MCSymbol *Start, *End;
1710};
1711
1712// Emit a debug aranges section, containing a CU lookup for any
1713// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001714void DwarfDebug::emitDebugARanges() {
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001715 // Provides a unique id per text section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001716 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
Richard Mitton21101b32013-09-19 23:21:01 +00001717
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001718 // Filter labels by section.
1719 for (const SymbolCU &SCU : ArangeLabels) {
1720 if (SCU.Sym->isInSection()) {
1721 // Make a note of this symbol and it's section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001722 MCSection *Section = &SCU.Sym->getSection();
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001723 if (!Section->getKind().isMetadata())
1724 SectionMap[Section].push_back(SCU);
1725 } else {
1726 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1727 // appear in the output. This sucks as we rely on sections to build
1728 // arange spans. We can do it without, but it's icky.
1729 SectionMap[nullptr].push_back(SCU);
1730 }
1731 }
Richard Mitton21101b32013-09-19 23:21:01 +00001732
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001733 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
1734
Rafael Espindola4f4ef152015-03-09 22:08:37 +00001735 for (auto &I : SectionMap) {
Rafael Espindola78d947b2016-05-20 00:38:28 +00001736 MCSection *Section = I.first;
Rafael Espindola4f4ef152015-03-09 22:08:37 +00001737 SmallVector<SymbolCU, 8> &List = I.second;
Rafael Espindola78d947b2016-05-20 00:38:28 +00001738 if (List.size() < 1)
Richard Mitton21101b32013-09-19 23:21:01 +00001739 continue;
1740
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001741 // If we have no section (e.g. common), just write out
1742 // individual spans for each symbol.
1743 if (!Section) {
1744 for (const SymbolCU &Cur : List) {
1745 ArangeSpan Span;
1746 Span.Start = Cur.Sym;
1747 Span.End = nullptr;
Rafael Espindola78d947b2016-05-20 00:38:28 +00001748 assert(Cur.CU);
1749 Spans[Cur.CU].push_back(Span);
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001750 }
1751 continue;
1752 }
1753
Richard Mitton21101b32013-09-19 23:21:01 +00001754 // Sort the symbols by offset within the section.
Rafael Espindola0a78f8c2016-05-19 23:17:37 +00001755 std::sort(
1756 List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) {
1757 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1758 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00001759
Rafael Espindola0a78f8c2016-05-19 23:17:37 +00001760 // Symbols with no order assigned should be placed at the end.
1761 // (e.g. section end labels)
1762 if (IA == 0)
1763 return false;
1764 if (IB == 0)
1765 return true;
1766 return IA < IB;
1767 });
Richard Mitton21101b32013-09-19 23:21:01 +00001768
Rafael Espindola78d947b2016-05-20 00:38:28 +00001769 // Insert a final terminator.
1770 List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
1771
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001772 // Build spans between each label.
1773 const MCSymbol *StartSym = List[0].Sym;
1774 for (size_t n = 1, e = List.size(); n < e; n++) {
1775 const SymbolCU &Prev = List[n - 1];
1776 const SymbolCU &Cur = List[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001777
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001778 // Try and build the longest span we can within the same CU.
1779 if (Cur.CU != Prev.CU) {
1780 ArangeSpan Span;
1781 Span.Start = StartSym;
1782 Span.End = Cur.Sym;
Rafael Espindola78d947b2016-05-20 00:38:28 +00001783 assert(Prev.CU);
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001784 Spans[Prev.CU].push_back(Span);
1785 StartSym = Cur.Sym;
Richard Mitton21101b32013-09-19 23:21:01 +00001786 }
1787 }
1788 }
1789
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001790 // Start the dwarf aranges section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001791 Asm->OutStreamer->SwitchSection(
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001792 Asm->getObjFileLowering().getDwarfARangesSection());
1793
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001794 unsigned PtrSize = Asm->MAI->getCodePointerSize();
Richard Mitton21101b32013-09-19 23:21:01 +00001795
1796 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00001797 std::vector<DwarfCompileUnit *> CUs;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001798 for (const auto &it : Spans) {
1799 DwarfCompileUnit *CU = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00001800 CUs.push_back(CU);
1801 }
1802
1803 // Sort the CU list (again, to ensure consistent output order).
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00001804 std::sort(CUs.begin(), CUs.end(),
1805 [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
1806 return A->getUniqueID() < B->getUniqueID();
1807 });
Richard Mitton21101b32013-09-19 23:21:01 +00001808
1809 // Emit an arange table for each CU we used.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001810 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton21101b32013-09-19 23:21:01 +00001811 std::vector<ArangeSpan> &List = Spans[CU];
1812
David Blaikie27e35f22014-11-02 01:21:43 +00001813 // Describe the skeleton CU's offset and length, not the dwo file's.
1814 if (auto *Skel = CU->getSkeleton())
1815 CU = Skel;
1816
Richard Mitton21101b32013-09-19 23:21:01 +00001817 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001818 unsigned ContentSize =
1819 sizeof(int16_t) + // DWARF ARange version number
1820 sizeof(int32_t) + // Offset of CU in the .debug_info section
1821 sizeof(int8_t) + // Pointer Size (in bytes)
1822 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00001823
1824 unsigned TupleSize = PtrSize * 2;
1825
1826 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00001827 unsigned Padding =
1828 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00001829
1830 ContentSize += Padding;
1831 ContentSize += (List.size() + 1) * TupleSize;
1832
1833 // For each compile unit, write the list of spans it covers.
Lang Hames9ff69c82015-04-24 19:11:51 +00001834 Asm->OutStreamer->AddComment("Length of ARange Set");
Richard Mitton21101b32013-09-19 23:21:01 +00001835 Asm->EmitInt32(ContentSize);
Lang Hames9ff69c82015-04-24 19:11:51 +00001836 Asm->OutStreamer->AddComment("DWARF Arange version number");
Richard Mitton21101b32013-09-19 23:21:01 +00001837 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
Lang Hames9ff69c82015-04-24 19:11:51 +00001838 Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
Rafael Espindola857546e2015-06-16 23:22:02 +00001839 Asm->emitDwarfSymbolReference(CU->getLabelBegin());
Lang Hames9ff69c82015-04-24 19:11:51 +00001840 Asm->OutStreamer->AddComment("Address Size (in bytes)");
Richard Mitton21101b32013-09-19 23:21:01 +00001841 Asm->EmitInt8(PtrSize);
Lang Hames9ff69c82015-04-24 19:11:51 +00001842 Asm->OutStreamer->AddComment("Segment Size (in bytes)");
Richard Mitton21101b32013-09-19 23:21:01 +00001843 Asm->EmitInt8(0);
1844
Petr Hosekfaef3202016-06-01 01:59:58 +00001845 Asm->OutStreamer->emitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00001846
Benjamin Kramer15596c72014-03-07 19:09:39 +00001847 for (const ArangeSpan &Span : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00001848 Asm->EmitLabelReference(Span.Start, PtrSize);
1849
1850 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00001851 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00001852 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00001853 } else {
1854 // For symbols without an end marker (e.g. common), we
1855 // write a single arange entry containing just that one symbol.
1856 uint64_t Size = SymSize[Span.Start];
1857 if (Size == 0)
1858 Size = 1;
1859
Lang Hames9ff69c82015-04-24 19:11:51 +00001860 Asm->OutStreamer->EmitIntValue(Size, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00001861 }
Richard Mitton21101b32013-09-19 23:21:01 +00001862 }
1863
Lang Hames9ff69c82015-04-24 19:11:51 +00001864 Asm->OutStreamer->AddComment("ARange terminator");
1865 Asm->OutStreamer->EmitIntValue(0, PtrSize);
1866 Asm->OutStreamer->EmitIntValue(0, PtrSize);
Richard Mitton21101b32013-09-19 23:21:01 +00001867 }
Bill Wendling480ff322009-05-20 23:21:38 +00001868}
1869
Amjad Aboudc0778412016-01-24 08:18:55 +00001870/// Emit address ranges into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00001871void DwarfDebug::emitDebugRanges() {
David Blaikie07963bd2017-05-26 18:52:56 +00001872 if (CUMap.empty())
1873 return;
1874
Bill Wendling480ff322009-05-20 23:21:38 +00001875 // Start the dwarf ranges section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001876 Asm->OutStreamer->SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001877 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00001878
Eric Christopher0f63d062013-12-03 00:45:45 +00001879 // Size for our labels.
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001880 unsigned char Size = Asm->MAI->getCodePointerSize();
Eric Christopher0f63d062013-12-03 00:45:45 +00001881
1882 // Grab the specific ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001883 for (const auto &I : CUMap) {
1884 DwarfCompileUnit *TheCU = I.second;
Eric Christopher0f63d062013-12-03 00:45:45 +00001885
David Blaikie3a443c22014-11-04 22:12:25 +00001886 if (auto *Skel = TheCU->getSkeleton())
1887 TheCU = Skel;
1888
Eric Christopher0f63d062013-12-03 00:45:45 +00001889 // Iterate over the misc ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001890 for (const RangeSpanList &List : TheCU->getRangeLists()) {
Eric Christopherf8790642013-12-04 22:04:50 +00001891 // Emit our symbol so we can find the beginning of the range.
Lang Hames9ff69c82015-04-24 19:11:51 +00001892 Asm->OutStreamer->EmitLabel(List.getSym());
Eric Christopher0f63d062013-12-03 00:45:45 +00001893
David Blaikie89c81a02017-07-30 22:10:00 +00001894 // Gather all the ranges that apply to the same section so they can share
1895 // a base address entry.
1896 MapVector<const MCSection *, std::vector<const RangeSpan *>> MV;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001897 for (const RangeSpan &Range : List.getRanges()) {
David Blaikie89c81a02017-07-30 22:10:00 +00001898 MV[&Range.getStart()->getSection()].push_back(&Range);
1899 }
1900
1901 auto *CUBase = TheCU->getBaseAddress();
David Blaikie4dd66372017-07-31 00:18:24 +00001902 bool BaseIsSet = false;
David Blaikie89c81a02017-07-30 22:10:00 +00001903 for (const auto &P : MV) {
1904 // Don't bother with a base address entry if there's only one range in
1905 // this section in this range list - for example ranges for a CU will
1906 // usually consist of single regions from each of many sections
1907 // (-ffunction-sections, or just C++ inline functions) except under LTO
1908 // or optnone where there may be holes in a single CU's section
1909 // contrubutions.
1910 auto *Base = CUBase;
David Blaikie038e28a2017-07-31 21:48:42 +00001911 if (!Base && P.second.size() > 1 &&
1912 UseDwarfRangesBaseAddressSpecifier) {
David Blaikie4dd66372017-07-31 00:18:24 +00001913 BaseIsSet = true;
David Blaikie89c81a02017-07-30 22:10:00 +00001914 // FIXME/use care: This may not be a useful base address if it's not
1915 // the lowest address/range in this object.
1916 Base = P.second.front()->getStart();
1917 Asm->OutStreamer->EmitIntValue(-1, Size);
1918 Asm->OutStreamer->EmitSymbolValue(Base, Size);
David Blaikie4dd66372017-07-31 00:18:24 +00001919 } else if (BaseIsSet) {
1920 BaseIsSet = false;
1921 Asm->OutStreamer->EmitIntValue(-1, Size);
1922 Asm->OutStreamer->EmitIntValue(0, Size);
David Blaikie89c81a02017-07-30 22:10:00 +00001923 }
1924
1925 for (const auto *RS : P.second) {
1926 const MCSymbol *Begin = RS->getStart();
1927 const MCSymbol *End = RS->getEnd();
1928 assert(Begin && "Range without a begin symbol?");
1929 assert(End && "Range without an end symbol?");
1930 if (Base) {
1931 Asm->EmitLabelDifference(Begin, Base, Size);
1932 Asm->EmitLabelDifference(End, Base, Size);
1933 } else {
1934 Asm->OutStreamer->EmitSymbolValue(Begin, Size);
1935 Asm->OutStreamer->EmitSymbolValue(End, Size);
1936 }
Eric Christopherece0e902014-04-25 22:23:54 +00001937 }
Eric Christopher0f63d062013-12-03 00:45:45 +00001938 }
1939
1940 // And terminate the list with two 0 values.
Lang Hames9ff69c82015-04-24 19:11:51 +00001941 Asm->OutStreamer->EmitIntValue(0, Size);
1942 Asm->OutStreamer->EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00001943 }
Devang Patel12563b32010-04-16 23:33:45 +00001944 }
Bill Wendling480ff322009-05-20 23:21:38 +00001945}
1946
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001947void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001948 for (auto *MN : Nodes) {
1949 if (auto *M = dyn_cast<DIMacro>(MN))
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001950 emitMacro(*M);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001951 else if (auto *F = dyn_cast<DIMacroFile>(MN))
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001952 emitMacroFile(*F, U);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001953 else
1954 llvm_unreachable("Unexpected DI type!");
1955 }
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001956}
1957
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001958void DwarfDebug::emitMacro(DIMacro &M) {
1959 Asm->EmitULEB128(M.getMacinfoType());
1960 Asm->EmitULEB128(M.getLine());
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001961 StringRef Name = M.getName();
1962 StringRef Value = M.getValue();
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001963 Asm->OutStreamer->EmitBytes(Name);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001964 if (!Value.empty()) {
1965 // There should be one space between macro name and macro value.
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001966 Asm->EmitInt8(' ');
1967 Asm->OutStreamer->EmitBytes(Value);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001968 }
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001969 Asm->EmitInt8('\0');
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001970}
1971
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001972void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001973 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001974 Asm->EmitULEB128(dwarf::DW_MACINFO_start_file);
1975 Asm->EmitULEB128(F.getLine());
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001976 DIFile *File = F.getFile();
1977 unsigned FID =
Paul Robinson29f5f982018-01-09 23:31:48 +00001978 U.getOrCreateSourceID(File->getFilename(), File->getDirectory(),
1979 nullptr); // FIXME: MD5?
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001980 Asm->EmitULEB128(FID);
1981 handleMacroNodes(F.getElements(), U);
1982 Asm->EmitULEB128(dwarf::DW_MACINFO_end_file);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001983}
1984
Amjad Aboudc0778412016-01-24 08:18:55 +00001985/// Emit macros into a debug macinfo section.
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001986void DwarfDebug::emitDebugMacinfo() {
David Blaikie07963bd2017-05-26 18:52:56 +00001987 if (CUMap.empty())
1988 return;
1989
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001990 // Start the dwarf macinfo section.
1991 Asm->OutStreamer->SwitchSection(
1992 Asm->getObjFileLowering().getDwarfMacinfoSection());
1993
Amjad Aboudd7cfb482016-01-07 14:28:20 +00001994 for (const auto &P : CUMap) {
1995 auto &TheCU = *P.second;
1996 auto *SkCU = TheCU.getSkeleton();
1997 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
1998 auto *CUNode = cast<DICompileUnit>(P.first);
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00001999 Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin());
2000 handleMacroNodes(CUNode->getMacros(), U);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002001 }
2002 Asm->OutStreamer->AddComment("End Of Macro List Mark");
2003 Asm->EmitInt8(0);
2004}
2005
Eric Christopherd692c1d2012-12-11 19:42:09 +00002006// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002007
David Blaikie65a74662014-04-25 18:26:14 +00002008void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002009 std::unique_ptr<DwarfCompileUnit> NewU) {
David Blaikiecafd9622014-11-02 08:51:37 +00002010 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikie96b1ed52017-04-21 23:35:26 +00002011 Asm->TM.Options.MCOptions.SplitDwarfFile);
David Blaikie38fe6342014-01-09 04:28:46 +00002012
David Blaikie38fe6342014-01-09 04:28:46 +00002013 if (!CompilationDir.empty())
David Blaikiecafd9622014-11-02 08:51:37 +00002014 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
David Blaikie38fe6342014-01-09 04:28:46 +00002015
David Blaikief9b6a552014-04-22 22:39:41 +00002016 addGnuPubAttributes(*NewU, Die);
David Blaikie38fe6342014-01-09 04:28:46 +00002017
David Blaikief9b6a552014-04-22 22:39:41 +00002018 SkeletonHolder.addUnit(std::move(NewU));
David Blaikie38fe6342014-01-09 04:28:46 +00002019}
2020
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002021// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2022// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
David Blaikie96dea052014-03-24 21:31:35 +00002023// DW_AT_addr_base, DW_AT_ranges_base.
David Blaikief9b6a552014-04-22 22:39:41 +00002024DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002025
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00002026 auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
David Blaikiebd579052014-04-28 21:14:27 +00002027 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
David Blaikief9b6a552014-04-22 22:39:41 +00002028 DwarfCompileUnit &NewCU = *OwnedUnit;
Greg Clayton35630c32016-12-01 18:56:29 +00002029 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
Eric Christopher4c7765f2013-01-17 03:00:04 +00002030
Rafael Espindola063d7252015-03-10 16:58:10 +00002031 NewCU.initStmtList();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002032
David Blaikie92a2f8a2014-04-28 21:04:29 +00002033 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
Eric Christopherc8a310e2012-12-10 23:34:43 +00002034
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002035 return NewCU;
2036}
2037
Eric Christopherd692c1d2012-12-11 19:42:09 +00002038// Emit the .debug_info.dwo section for separated dwarf. This contains the
2039// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002040void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002041 assert(useSplitDwarf() && "No split dwarf debug info?");
Rafael Espindola063d7252015-03-10 16:58:10 +00002042 // Don't emit relocations into the dwo file.
2043 InfoHolder.emitUnits(/* UseOffsets */ true);
Eric Christopher3c5a1912012-12-19 22:02:53 +00002044}
2045
2046// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2047// abbreviations for the .debug_info.dwo section.
2048void DwarfDebug::emitDebugAbbrevDWO() {
2049 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00002050 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002051}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002052
David Blaikie4a2f95f2014-03-18 01:17:26 +00002053void DwarfDebug::emitDebugLineDWO() {
2054 assert(useSplitDwarf() && "No split dwarf?");
Lang Hames9ff69c82015-04-24 19:11:51 +00002055 Asm->OutStreamer->SwitchSection(
David Blaikie4a2f95f2014-03-18 01:17:26 +00002056 Asm->getObjFileLowering().getDwarfLineDWOSection());
Frederic Rissa5ab8442015-08-07 15:14:08 +00002057 SplitTypeUnitFileTable.Emit(*Asm->OutStreamer, MCDwarfLineTableParams());
David Blaikie4a2f95f2014-03-18 01:17:26 +00002058}
2059
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002060// Emit the .debug_str.dwo section for separated dwarf. This contains the
2061// string section and is identical in format to traditional .debug_str
2062// sections.
2063void DwarfDebug::emitDebugStrDWO() {
2064 assert(useSplitDwarf() && "No split dwarf?");
Rafael Espindola0709a7b2015-05-21 19:20:38 +00002065 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002066 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
David Blaikie6741bb02014-09-11 21:12:48 +00002067 OffSec);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002068}
David Blaikie409dd9c2013-11-19 23:08:21 +00002069
David Blaikie47f4b822014-03-19 00:11:28 +00002070MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
2071 if (!useSplitDwarf())
2072 return nullptr;
2073 if (SingleCU)
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +00002074 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory());
David Blaikie47f4b822014-03-19 00:11:28 +00002075 return &SplitTypeUnitFileTable;
2076}
2077
Adrian Prantlee5feaf2015-07-15 17:01:41 +00002078uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
David Blaikief3de2ab2014-04-26 16:26:41 +00002079 MD5 Hash;
2080 Hash.update(Identifier);
2081 // ... take the least significant 8 bytes and return those. Our MD5
Zachary Turner82a0c972017-03-20 23:33:18 +00002082 // implementation always returns its results in little endian, so we actually
2083 // need the "high" word.
David Blaikief3de2ab2014-04-26 16:26:41 +00002084 MD5::MD5Result Result;
2085 Hash.final(Result);
Zachary Turner82a0c972017-03-20 23:33:18 +00002086 return Result.high();
David Blaikief3de2ab2014-04-26 16:26:41 +00002087}
2088
David Blaikie15632ae2014-02-12 00:31:30 +00002089void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikie65a74662014-04-25 18:26:14 +00002090 StringRef Identifier, DIE &RefDie,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002091 const DICompositeType *CTy) {
David Blaikiee12b49a2014-04-26 17:27:38 +00002092 // Fast path if we're building some type units and one has already used the
2093 // address pool we know we're going to throw away all this work anyway, so
2094 // don't bother building dependent types.
2095 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
2096 return;
2097
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002098 auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
2099 if (!Ins.second) {
2100 CU.addDIETypeSignature(RefDie, Ins.first->second);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002101 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00002102 }
2103
David Blaikiee12b49a2014-04-26 17:27:38 +00002104 bool TopLevelType = TypeUnitsUnderConstruction.empty();
2105 AddrPool.resetUsedFlag();
2106
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00002107 auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
2108 getDwoLineTable(CU));
David Blaikief9b6a552014-04-22 22:39:41 +00002109 DwarfTypeUnit &NewTU = *OwnedUnit;
David Blaikie92a2f8a2014-04-28 21:04:29 +00002110 DIE &UnitDie = NewTU.getUnitDie();
David Majnemer0a16c222016-08-11 21:15:00 +00002111 TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002112
David Blaikie92a2f8a2014-04-28 21:04:29 +00002113 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie637cac42014-04-22 23:09:36 +00002114 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00002115
David Blaikief3de2ab2014-04-26 16:26:41 +00002116 uint64_t Signature = makeTypeSignature(Identifier);
David Blaikief9b6a552014-04-22 22:39:41 +00002117 NewTU.setTypeSignature(Signature);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002118 Ins.first->second = Signature;
David Blaikief3de2ab2014-04-26 16:26:41 +00002119
David Blaikie29459ae2014-07-25 17:11:58 +00002120 if (useSplitDwarf())
Greg Clayton35630c32016-12-01 18:56:29 +00002121 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
David Blaikie29459ae2014-07-25 17:11:58 +00002122 else {
David Blaikie92a2f8a2014-04-28 21:04:29 +00002123 CU.applyStmtList(UnitDie);
Greg Clayton35630c32016-12-01 18:56:29 +00002124 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
David Blaikie29459ae2014-07-25 17:11:58 +00002125 }
Chandler Carruthb587ab62014-01-20 08:07:07 +00002126
David Blaikief3de2ab2014-04-26 16:26:41 +00002127 NewTU.setType(NewTU.createTypeDIE(CTy));
2128
David Blaikiee12b49a2014-04-26 17:27:38 +00002129 if (TopLevelType) {
2130 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2131 TypeUnitsUnderConstruction.clear();
2132
2133 // Types referencing entries in the address table cannot be placed in type
2134 // units.
2135 if (AddrPool.hasBeenUsed()) {
2136
2137 // Remove all the types built while building this type.
2138 // This is pessimistic as some of these types might not be dependent on
2139 // the type that used an address.
2140 for (const auto &TU : TypeUnitsToAdd)
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002141 TypeSignatures.erase(TU.second);
David Blaikiee12b49a2014-04-26 17:27:38 +00002142
2143 // Construct this type in the CU directly.
2144 // This is inefficient because all the dependent types will be rebuilt
2145 // from scratch, including building them in type units, discovering that
2146 // they depend on addresses, throwing them out and rebuilding them.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002147 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
David Blaikiee12b49a2014-04-26 17:27:38 +00002148 return;
2149 }
2150
2151 // If the type wasn't dependent on fission addresses, finish adding the type
2152 // and all its dependent types.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002153 for (auto &TU : TypeUnitsToAdd) {
2154 InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
2155 InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
2156 }
David Blaikiee12b49a2014-04-26 17:27:38 +00002157 }
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002158 CU.addDIETypeSignature(RefDie, Signature);
David Blaikie409dd9c2013-11-19 23:08:21 +00002159}
David Blaikie4bd13b72014-03-07 18:49:45 +00002160
David Blaikie2406a0622014-04-23 23:37:35 +00002161// Accelerator table mutators - add each name along with its companion
2162// DIE to the proper table while ensuring that the name that we're going
2163// to reference is in the string table. We do this since the names we
2164// add may not only be identical to the names in the DIE.
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002165void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
David Blaikie2406a0622014-04-23 23:37:35 +00002166 if (!useDwarfAccelTables())
2167 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002168 AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikie2406a0622014-04-23 23:37:35 +00002169}
David Blaikie0ee82b92014-04-24 00:53:32 +00002170
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002171void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
David Blaikie0ee82b92014-04-24 00:53:32 +00002172 if (!useDwarfAccelTables())
2173 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002174 AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikie0ee82b92014-04-24 00:53:32 +00002175}
David Blaikieecf04152014-04-24 01:02:42 +00002176
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002177void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
David Blaikieecf04152014-04-24 01:02:42 +00002178 if (!useDwarfAccelTables())
2179 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002180 AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikieecf04152014-04-24 01:02:42 +00002181}
David Blaikie18d33752014-04-24 01:23:49 +00002182
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002183void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
David Blaikie18d33752014-04-24 01:23:49 +00002184 if (!useDwarfAccelTables())
2185 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002186 AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikie18d33752014-04-24 01:23:49 +00002187}
Greg Claytone6543972016-11-23 23:30:37 +00002188
2189uint16_t DwarfDebug::getDwarfVersion() const {
2190 return Asm->OutStreamer->getContext().getDwarfVersion();
2191}