blob: e731542d41984b2f5c5d78295b4a5ca3c15f5639 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
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"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "DwarfCompileUnit.h"
19#include "DwarfExpression.h"
David Blaikie2c86a722013-12-02 19:33:15 +000020#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Triple.h"
Frederic Risse541e0b2015-01-05 21:29:41 +000025#include "llvm/CodeGen/DIE.h"
David Greene829b3e82009-08-19 21:52:55 +000026#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000027#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000029#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000031#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/Instructions.h"
33#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000034#include "llvm/IR/ValueHandle.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000035#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000036#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000037#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000038#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000039#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000040#include "llvm/Support/Debug.h"
David Majnemered89b5c2013-08-21 06:13:34 +000041#include "llvm/Support/Dwarf.h"
Saleem Abdulrasool271ac582014-07-14 16:28:09 +000042#include "llvm/Support/Endian.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000043#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000044#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000045#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000046#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000047#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000048#include "llvm/Support/Timer.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000049#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000050#include "llvm/Target/TargetFrameLowering.h"
51#include "llvm/Target/TargetLoweringObjectFile.h"
52#include "llvm/Target/TargetMachine.h"
53#include "llvm/Target/TargetOptions.h"
54#include "llvm/Target/TargetRegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000055#include "llvm/Target/TargetSubtargetInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000056using namespace llvm;
57
Chandler Carruth1b9dde02014-04-22 02:02:50 +000058#define DEBUG_TYPE "dwarfdebug"
59
Eric Christopher7f2b5512013-07-23 22:16:41 +000060static cl::opt<bool>
61DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
62 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000063
Eric Christopher7f2b5512013-07-23 22:16:41 +000064static cl::opt<bool> UnknownLocations(
65 "use-unknown-locations", cl::Hidden,
66 cl::desc("Make an absence of debug location information explicit."),
67 cl::init(false));
Dan Gohman7421ae42010-05-07 01:08:53 +000068
Eric Christopherdd1a0122013-09-13 00:35:05 +000069static cl::opt<bool>
70GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
71 cl::desc("Generate GNU-style pubnames and pubtypes"),
72 cl::init(false));
73
Eric Christopher02dbadb2014-02-14 01:26:55 +000074static cl::opt<bool> GenerateARangeSection("generate-arange-section",
75 cl::Hidden,
76 cl::desc("Generate dwarf aranges"),
77 cl::init(false));
78
Paul Robinsonb9de1062015-07-15 22:04:54 +000079static cl::opt<DebuggerKind>
80DebuggerTuningOpt("debugger-tune",
81 cl::desc("Tune debug info for a particular debugger"),
82 cl::init(DebuggerKind::Default),
83 cl::values(
84 clEnumValN(DebuggerKind::GDB, "gdb", "gdb"),
85 clEnumValN(DebuggerKind::LLDB, "lldb", "lldb"),
86 clEnumValN(DebuggerKind::SCE, "sce",
87 "SCE targets (e.g. PS4)"),
88 clEnumValEnd));
89
Eric Christopher20b76a72012-08-23 22:36:40 +000090namespace {
Eric Christopherf07ee3a2014-01-27 23:50:03 +000091enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher20b76a72012-08-23 22:36:40 +000092}
Eric Christopher4996c702011-11-07 09:24:32 +000093
Eric Christopher7f2b5512013-07-23 22:16:41 +000094static cl::opt<DefaultOnOff>
95DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
96 cl::desc("Output prototype dwarf accelerator tables."),
97 cl::values(clEnumVal(Default, "Default for platform"),
98 clEnumVal(Enable, "Enabled"),
99 clEnumVal(Disable, "Disabled"), clEnumValEnd),
100 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +0000101
Eric Christopher7f2b5512013-07-23 22:16:41 +0000102static cl::opt<DefaultOnOff>
Eric Christopher7f2b5512013-07-23 22:16:41 +0000103SplitDwarf("split-dwarf", cl::Hidden,
Eric Christopher5d008fe2013-12-04 23:24:28 +0000104 cl::desc("Output DWARF5 split debug info."),
Eric Christopher7f2b5512013-07-23 22:16:41 +0000105 cl::values(clEnumVal(Default, "Default for platform"),
106 clEnumVal(Enable, "Enabled"),
107 clEnumVal(Disable, "Disabled"), clEnumValEnd),
108 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +0000109
Eric Christopher7da24882013-08-19 21:07:38 +0000110static cl::opt<DefaultOnOff>
Eric Christopher4d36ca02013-08-26 23:24:35 +0000111DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
112 cl::desc("Generate DWARF pubnames and pubtypes sections"),
113 cl::values(clEnumVal(Default, "Default for platform"),
114 clEnumVal(Enable, "Enabled"),
115 clEnumVal(Disable, "Disabled"), clEnumValEnd),
116 cl::init(Default));
Eric Christopher7da24882013-08-19 21:07:38 +0000117
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000118static const char *const DWARFGroupName = "DWARF Emission";
119static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000120
Adrian Prantl92da14b2015-03-02 22:02:33 +0000121void DebugLocDwarfExpression::EmitOp(uint8_t Op, const char *Comment) {
122 BS.EmitInt8(
123 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
124 : dwarf::OperationEncodingString(Op));
125}
126
Adrian Prantl51233682015-03-10 19:23:37 +0000127void DebugLocDwarfExpression::EmitSigned(int64_t Value) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000128 BS.EmitSLEB128(Value, Twine(Value));
129}
130
Adrian Prantl51233682015-03-10 19:23:37 +0000131void DebugLocDwarfExpression::EmitUnsigned(uint64_t Value) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000132 BS.EmitULEB128(Value, Twine(Value));
133}
134
135bool DebugLocDwarfExpression::isFrameRegister(unsigned MachineReg) {
136 // This information is not available while emitting .debug_loc entries.
137 return false;
138}
139
Bill Wendling2f921f82009-05-15 09:23:25 +0000140//===----------------------------------------------------------------------===//
141
Manman Renbe5576f2013-10-08 19:07:44 +0000142/// resolve - Look in the DwarfDebug map for the MDNode that
143/// corresponds to the reference.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000144template <typename T> T *DbgVariable::resolve(TypedDINodeRef<T> Ref) const {
Manman Renbe5576f2013-10-08 19:07:44 +0000145 return DD->resolve(Ref);
146}
147
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000148bool DbgVariable::isBlockByrefVariable() const {
Duncan P. N. Exon Smithe686f152015-04-06 23:27:40 +0000149 assert(Var && "Invalid complex DbgVariable!");
Duncan P. N. Exon Smith571baeb2015-04-14 01:59:58 +0000150 return Var->getType()
151 .resolve(DD->getTypeIdentifierMap())
152 ->isBlockByrefStruct();
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000153}
154
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000155const DIType *DbgVariable::getType() const {
156 DIType *Ty = Var->getType().resolve(DD->getTypeIdentifierMap());
Devang Patelf20c4f72011-04-12 22:53:02 +0000157 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
158 // addresses instead.
Duncan P. N. Exon Smith571baeb2015-04-14 01:59:58 +0000159 if (Ty->isBlockByrefStruct()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000160 /* Byref variables, in Blocks, are declared by the programmer as
161 "SomeType VarName;", but the compiler creates a
162 __Block_byref_x_VarName struct, and gives the variable VarName
163 either the struct, or a pointer to the struct, as its type. This
164 is necessary for various behind-the-scenes things the compiler
165 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000166
Devang Patelf20c4f72011-04-12 22:53:02 +0000167 However, as far as the original *programmer* is concerned, the
168 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000169
Devang Patelf20c4f72011-04-12 22:53:02 +0000170 The following function dives into the __Block_byref_x_VarName
171 struct to find the original type of the variable. This will be
172 passed back to the code generating the type for the Debug
173 Information Entry for the variable 'VarName'. 'VarName' will then
174 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000175
Devang Patelf20c4f72011-04-12 22:53:02 +0000176 The original type 'SomeType' will be the type of the field named
177 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000178
Devang Patelf20c4f72011-04-12 22:53:02 +0000179 NOTE: In order for this to not completely fail on the debugger
180 side, the Debug Information Entry for the variable VarName needs to
181 have a DW_AT_location that tells the debugger how to unwind through
182 the pointers and __Block_byref_x_VarName struct to find the actual
183 value of the variable. The function addBlockByrefType does this. */
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000184 DIType *subType = Ty;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000185 uint16_t tag = Ty->getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000186
Eric Christopher9adc55f2013-09-04 19:53:21 +0000187 if (tag == dwarf::DW_TAG_pointer_type)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000188 subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
Eric Christopher6a841382012-11-19 22:42:10 +0000189
Duncan P. N. Exon Smith6ac940d2015-07-24 18:17:17 +0000190 auto Elements = cast<DICompositeType>(subType)->getElements();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000191 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Duncan P. N. Exon Smith099ea1c2015-07-24 18:58:32 +0000192 auto *DT = cast<DIDerivedType>(Elements[i]);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000193 if (getName() == DT->getName())
Duncan P. N. Exon Smith848af382015-04-20 18:20:03 +0000194 return resolve(DT->getBaseType());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000195 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000196 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000197 return Ty;
198}
Bill Wendling2f921f82009-05-15 09:23:25 +0000199
David Blaikie18d33752014-04-24 01:23:49 +0000200static LLVM_CONSTEXPR DwarfAccelTable::Atom TypeAtoms[] = {
201 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
202 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
203 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
204
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000205DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Pete Coopera05c0822015-05-20 22:51:27 +0000206 : Asm(A), MMI(Asm->MMI), DebugLocs(A->OutStreamer->isVerboseAsm()),
207 PrevLabel(nullptr), InfoHolder(A, "info_string", DIEValueAllocator),
Eric Christopher2037caf2014-01-28 00:49:26 +0000208 UsedNonDefaultText(false),
Frederic Riss9412d632015-03-04 02:30:17 +0000209 SkeletonHolder(A, "skel_string", DIEValueAllocator),
David Blaikiee1c79742014-09-30 21:28:32 +0000210 IsDarwin(Triple(A->getTargetTriple()).isOSDarwin()),
David Blaikie2406a0622014-04-23 23:37:35 +0000211 AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
David Blaikie0ee82b92014-04-24 00:53:32 +0000212 dwarf::DW_FORM_data4)),
213 AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
David Blaikieecf04152014-04-24 01:02:42 +0000214 dwarf::DW_FORM_data4)),
215 AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
David Blaikie18d33752014-04-24 01:23:49 +0000216 dwarf::DW_FORM_data4)),
Paul Robinsonb9de1062015-07-15 22:04:54 +0000217 AccelTypes(TypeAtoms), DebuggerTuning(DebuggerKind::Default) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000218
Craig Topper353eda42014-04-24 06:44:33 +0000219 CurFn = nullptr;
220 CurMI = nullptr;
Paul Robinsonb9de1062015-07-15 22:04:54 +0000221 Triple TT(Asm->getTargetTriple());
Eric Christopherad9fe892012-04-02 17:58:52 +0000222
Paul Robinsonb9de1062015-07-15 22:04:54 +0000223 // Make sure we know our "debugger tuning." The command-line option takes
224 // precedence; fall back to triple-based defaults.
225 if (DebuggerTuningOpt != DebuggerKind::Default)
226 DebuggerTuning = DebuggerTuningOpt;
227 else if (IsDarwin || TT.isOSFreeBSD())
228 DebuggerTuning = DebuggerKind::LLDB;
229 else if (TT.isPS4CPU())
230 DebuggerTuning = DebuggerKind::SCE;
231 else
232 DebuggerTuning = DebuggerKind::GDB;
233
234 // Turn on accelerator tables for LLDB by default.
Eric Christopher574b5c82013-08-19 21:41:38 +0000235 if (DwarfAccelTables == Default)
Paul Robinsonb9de1062015-07-15 22:04:54 +0000236 HasDwarfAccelTables = tuneForLLDB();
Eric Christopher574b5c82013-08-19 21:41:38 +0000237 else
Eric Christopher5297df02013-08-26 20:58:35 +0000238 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000239
Paul Robinsonb9de1062015-07-15 22:04:54 +0000240 // Handle split DWARF. Off by default for now.
Eric Christophercdf218d2012-12-10 19:51:21 +0000241 if (SplitDwarf == Default)
242 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000243 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000244 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000245
Paul Robinsonb9de1062015-07-15 22:04:54 +0000246 // Pubnames/pubtypes on by default for GDB.
Eric Christopher4d36ca02013-08-26 23:24:35 +0000247 if (DwarfPubSections == Default)
Paul Robinsonb9de1062015-07-15 22:04:54 +0000248 HasDwarfPubSections = tuneForGDB();
Eric Christopher574b5c82013-08-19 21:41:38 +0000249 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000250 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000251
Eric Christopher4c5bff32014-06-19 06:22:08 +0000252 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
Eric Christopher793c7472014-04-28 20:42:22 +0000253 DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
254 : MMI->getModule()->getDwarfVersion();
Reid Kleckner12d2c122015-08-05 22:26:20 +0000255 // Use dwarf 4 by default if nothing is requested.
256 DwarfVersion = DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION;
Manman Renac8062b2013-07-02 23:40:10 +0000257
Paul Robinsonb9de1062015-07-15 22:04:54 +0000258 // Work around a GDB bug. GDB doesn't support the standard opcode;
259 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
260 // is defined as of DWARF 3.
261 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
262 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
263 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
Paul Robinson78cc0822015-03-04 20:55:11 +0000264
Lang Hames9ff69c82015-04-24 19:11:51 +0000265 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
Oliver Stannardf7693f42014-06-19 15:39:33 +0000266
Dan Gohman6e681a52010-06-18 15:56:31 +0000267 {
268 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000269 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000270 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000271}
Bill Wendling2f921f82009-05-15 09:23:25 +0000272
Reid Klecknerdd2647e2014-04-30 20:34:31 +0000273// Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
274DwarfDebug::~DwarfDebug() { }
275
Eric Christopherd9843b32011-11-10 19:25:34 +0000276static bool isObjCClass(StringRef Name) {
277 return Name.startswith("+") || Name.startswith("-");
278}
279
280static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000281 if (!isObjCClass(Name))
282 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000283
Benjamin Kramer260de742013-08-24 12:15:54 +0000284 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000285}
286
287static void getObjCClassCategory(StringRef In, StringRef &Class,
288 StringRef &Category) {
289 if (!hasObjCCategory(In)) {
290 Class = In.slice(In.find('[') + 1, In.find(' '));
291 Category = "";
292 return;
293 }
294
295 Class = In.slice(In.find('[') + 1, In.find('('));
296 Category = In.slice(In.find('[') + 1, In.find(' '));
297 return;
298}
299
300static StringRef getObjCMethodName(StringRef In) {
301 return In.slice(In.find(' ') + 1, In.find(']'));
302}
303
304// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000305// TODO: Determine whether or not we should add names for programs
306// that do not have a DW_AT_name or DW_AT_linkage_name field - this
307// is only slightly different than the lookup of non-standard ObjC names.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000308void DwarfDebug::addSubprogramNames(const DISubprogram *SP, DIE &Die) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000309 if (!SP->isDefinition())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000310 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000311 addAccelName(SP->getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000312
313 // If the linkage name is different than the name, go ahead and output
314 // that as well into the name table.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000315 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName())
316 addAccelName(SP->getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000317
318 // If this is an Objective-C selector name add it to the ObjC accelerator
319 // too.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000320 if (isObjCClass(SP->getName())) {
Eric Christopherd9843b32011-11-10 19:25:34 +0000321 StringRef Class, Category;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000322 getObjCClassCategory(SP->getName(), Class, Category);
David Blaikie0ee82b92014-04-24 00:53:32 +0000323 addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000324 if (Category != "")
David Blaikie0ee82b92014-04-24 00:53:32 +0000325 addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000326 // Also add the base method name to the name table.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000327 addAccelName(getObjCMethodName(SP->getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000328 }
329}
330
Manman Ren3eb9dff2013-09-09 19:05:21 +0000331/// isSubprogramContext - Return true if Context is either a subprogram
332/// or another context nested inside a subprogram.
333bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
334 if (!Context)
335 return false;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000336 if (isa<DISubprogram>(Context))
Manman Ren3eb9dff2013-09-09 19:05:21 +0000337 return true;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000338 if (auto *T = dyn_cast<DIType>(Context))
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000339 return isSubprogramContext(resolve(T->getScope()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000340 return false;
341}
342
Manman Ren5b2f4b02013-09-11 19:40:28 +0000343/// Check whether we should create a DIE for the given Scope, return true
344/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000345bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
346 if (Scope->isAbstractScope())
347 return false;
348
Manman Ren5b2f4b02013-09-11 19:40:28 +0000349 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000350 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
351 if (Ranges.empty())
352 return true;
353
354 if (Ranges.size() > 1)
355 return false;
356
Manman Ren5b2f4b02013-09-11 19:40:28 +0000357 // We don't create a DIE if we have a single Range and the end label
358 // is null.
David Blaikiecd4b8a22014-08-31 02:14:26 +0000359 return !getLabelAfterInsn(Ranges.front().second);
Manman Ren2312ed32013-09-10 18:40:41 +0000360}
361
David Blaikie3a443c22014-11-04 22:12:25 +0000362template <typename Func> void forBothCUs(DwarfCompileUnit &CU, Func F) {
363 F(CU);
364 if (auto *SkelCU = CU.getSkeleton())
365 F(*SkelCU);
366}
367
David Blaikie73cc7052014-10-09 20:36:27 +0000368void DwarfDebug::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) {
David Blaikiee872a6e2014-04-29 15:58:35 +0000369 assert(Scope && Scope->getScopeNode());
370 assert(Scope->isAbstractScope());
371 assert(!Scope->getInlinedAt());
372
David Blaikie58410f22014-10-10 06:39:26 +0000373 const MDNode *SP = Scope->getScopeNode();
David Blaikiee872a6e2014-04-29 15:58:35 +0000374
David Blaikie9ab48842014-10-10 03:09:38 +0000375 ProcessedSPNodes.insert(SP);
376
David Blaikie1ea9db22014-05-21 23:14:12 +0000377 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
378 // was inlined from another compile unit.
David Blaikie3a443c22014-11-04 22:12:25 +0000379 auto &CU = SPMap[SP];
380 forBothCUs(*CU, [&](DwarfCompileUnit &CU) {
381 CU.constructAbstractSubprogramScopeDIE(Scope);
382 });
David Blaikiee872a6e2014-04-29 15:58:35 +0000383}
384
David Blaikie65a74662014-04-25 18:26:14 +0000385void DwarfDebug::addGnuPubAttributes(DwarfUnit &U, DIE &D) const {
David Blaikie3c842622013-12-04 21:31:26 +0000386 if (!GenerateGnuPubSections)
387 return;
388
David Blaikief9b6a552014-04-22 22:39:41 +0000389 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
David Blaikie3c842622013-12-04 21:31:26 +0000390}
391
Eric Christopher4287a492013-12-09 23:57:44 +0000392// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000393// DW_TAG_compile_unit.
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000394DwarfCompileUnit &
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000395DwarfDebug::constructDwarfCompileUnit(const DICompileUnit *DIUnit) {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000396 StringRef FN = DIUnit->getFilename();
397 CompilationDir = DIUnit->getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000398
David Blaikief9b6a552014-04-22 22:39:41 +0000399 auto OwnedUnit = make_unique<DwarfCompileUnit>(
David Blaikiebd579052014-04-28 21:14:27 +0000400 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
David Blaikief9b6a552014-04-22 22:39:41 +0000401 DwarfCompileUnit &NewCU = *OwnedUnit;
David Blaikie92a2f8a2014-04-28 21:04:29 +0000402 DIE &Die = NewCU.getUnitDie();
David Blaikief9b6a552014-04-22 22:39:41 +0000403 InfoHolder.addUnit(std::move(OwnedUnit));
David Blaikiecafd9622014-11-02 08:51:37 +0000404 if (useSplitDwarf())
405 NewCU.setSkeleton(constructSkeletonCU(NewCU));
David Blaikief9b6a552014-04-22 22:39:41 +0000406
David Blaikie7ac51492014-03-20 17:05:45 +0000407 // LTO with assembly output shares a single line table amongst multiple CUs.
408 // To avoid the compilation directory being ambiguous, let the line table
409 // explicitly describe the directory of all files, never relying on the
410 // compilation directory.
Lang Hames9ff69c82015-04-24 19:11:51 +0000411 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
412 Asm->OutStreamer->getContext().setMCLineTableCompilationDir(
David Blaikief9b6a552014-04-22 22:39:41 +0000413 NewCU.getUniqueID(), CompilationDir);
David Blaikie2666e242013-12-06 19:38:46 +0000414
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000415 NewCU.addString(Die, dwarf::DW_AT_producer, DIUnit->getProducer());
David Blaikie92a2f8a2014-04-28 21:04:29 +0000416 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000417 DIUnit->getSourceLanguage());
David Blaikie92a2f8a2014-04-28 21:04:29 +0000418 NewCU.addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000419
Eric Christopher52ce7182013-04-09 19:23:15 +0000420 if (!useSplitDwarf()) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000421 NewCU.initStmtList();
Eric Christophera51d3fc2013-09-27 22:50:48 +0000422
423 // If we're using split dwarf the compilation dir is going to be in the
424 // skeleton CU and so we don't need to duplicate it here.
425 if (!CompilationDir.empty())
David Blaikie92a2f8a2014-04-28 21:04:29 +0000426 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000427
David Blaikie92a2f8a2014-04-28 21:04:29 +0000428 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000429 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000430
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000431 if (DIUnit->isOptimized())
David Blaikie92a2f8a2014-04-28 21:04:29 +0000432 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000433
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000434 StringRef Flags = DIUnit->getFlags();
Devang Patel2d9caf92009-11-25 17:36:49 +0000435 if (!Flags.empty())
David Blaikie92a2f8a2014-04-28 21:04:29 +0000436 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000437
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000438 if (unsigned RVer = DIUnit->getRuntimeVersion())
David Blaikie92a2f8a2014-04-28 21:04:29 +0000439 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
David Blaikie637cac42014-04-22 23:09:36 +0000440 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000441
David Blaikiecafd9622014-11-02 08:51:37 +0000442 if (useSplitDwarf())
Rafael Espindola063d7252015-03-10 16:58:10 +0000443 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
David Blaikiecafd9622014-11-02 08:51:37 +0000444 else
Rafael Espindola063d7252015-03-10 16:58:10 +0000445 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
Eric Christopherd039baa2013-12-30 03:40:32 +0000446
David Blaikief9b6a552014-04-22 22:39:41 +0000447 CUMap.insert(std::make_pair(DIUnit, &NewCU));
David Blaikie92a2f8a2014-04-28 21:04:29 +0000448 CUDieMap.insert(std::make_pair(&Die, &NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000449 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000450}
451
David Blaikie8912df12014-08-31 05:41:15 +0000452void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000453 const DIImportedEntity *N) {
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000454 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
455 D->addChild(TheCU.constructImportedEntityDIE(N));
David Blaikief55abea2013-04-22 06:12:31 +0000456}
457
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000458// Emit all Dwarf sections that should come prior to the content. Create
459// global DIEs and emit initial debug info sections. This is invoked by
460// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000461void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000462 if (DisableDebugInfoPrinting)
463 return;
464
Eric Christopher58f41952012-11-19 22:42:15 +0000465 const Module *M = MMI->getModule();
466
David Blaikie2f040112014-07-25 16:10:16 +0000467 FunctionDIs = makeSubprogramMap(*M);
468
Devang Patele02e5852011-05-03 16:45:22 +0000469 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000470 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000471 return;
Manman Ren60352032013-09-05 18:48:31 +0000472 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000473
David Blaikie47f4b822014-03-19 00:11:28 +0000474 SingleCU = CU_Nodes->getNumOperands() == 1;
David Blaikiee05274d2014-03-18 00:11:48 +0000475
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000476 for (MDNode *N : CU_Nodes->operands()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000477 auto *CUNode = cast<DICompileUnit>(N);
David Blaikief9b6a552014-04-22 22:39:41 +0000478 DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000479 for (auto *IE : CUNode->getImportedEntities())
David Blaikied51dea62015-07-01 18:07:16 +0000480 ScopesWithImportedEntities.push_back(std::make_pair(IE->getScope(), IE));
481 // Stable sort to preserve the order of appearance of imported entities.
482 // This is to avoid out-of-order processing of interdependent declarations
483 // within the same scope, e.g. { namespace A = base; namespace B = A; }
484 std::stable_sort(ScopesWithImportedEntities.begin(),
485 ScopesWithImportedEntities.end(), less_first());
486 for (auto *GV : CUNode->getGlobalVariables())
487 CU.getOrCreateGlobalVariableDIE(GV);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000488 for (auto *SP : CUNode->getSubprograms())
489 SPMap.insert(std::make_pair(SP, &CU));
Duncan P. N. Exon Smith9928a902015-04-20 18:52:06 +0000490 for (auto *Ty : CUNode->getEnumTypes()) {
Manman Renbd1628a2014-07-28 23:04:20 +0000491 // The enum types array by design contains pointers to
492 // MDNodes rather than DIRefs. Unique them here.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000493 CU.getOrCreateTypeDIE(cast<DIType>(resolve(Ty->getRef())));
Manman Renbd1628a2014-07-28 23:04:20 +0000494 }
Duncan P. N. Exon Smith9928a902015-04-20 18:52:06 +0000495 for (auto *Ty : CUNode->getRetainedTypes()) {
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000496 // The retained types array by design contains pointers to
497 // MDNodes rather than DIRefs. Unique them here.
Adrian Prantlee5feaf2015-07-15 17:01:41 +0000498 DIType *RT = cast<DIType>(resolve(Ty->getRef()));
499 if (!RT->isExternalTypeRef())
500 // There is no point in force-emitting a forward declaration.
501 CU.getOrCreateTypeDIE(RT);
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000502 }
David Blaikief55abea2013-04-22 06:12:31 +0000503 // Emit imported_modules last so that the relevant context is already
504 // available.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000505 for (auto *IE : CUNode->getImportedEntities())
506 constructAndAddImportedEntityDIE(CU, IE);
David Blaikiedc69ebb2013-03-11 23:39:23 +0000507 }
Eric Christopher6a841382012-11-19 22:42:10 +0000508
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000509 // Tell MMI that we have debug info.
510 MMI->setDebugInfoAvailability(true);
Bill Wendling2b128d72009-05-20 23:19:06 +0000511}
512
David Blaikieeb1a2722014-06-13 22:18:23 +0000513void DwarfDebug::finishVariableDefinitions() {
514 for (const auto &Var : ConcreteVariables) {
515 DIE *VariableDie = Var->getDIE();
David Blaikief73ae4f2014-08-12 00:00:31 +0000516 assert(VariableDie);
David Blaikieeb1a2722014-06-13 22:18:23 +0000517 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
518 // in the ConcreteVariables list, rather than looking it up again here.
519 // DIE::getUnit isn't simple - it walks parent pointers, etc.
520 DwarfCompileUnit *Unit = lookupUnit(VariableDie->getUnit());
521 assert(Unit);
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000522 DbgVariable *AbsVar = getExistingAbstractVariable(
523 InlinedVariable(Var->getVariable(), Var->getInlinedAt()));
David Blaikieeb1a2722014-06-13 22:18:23 +0000524 if (AbsVar && AbsVar->getDIE()) {
525 Unit->addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin,
526 *AbsVar->getDIE());
527 } else
528 Unit->applyVariableAttributes(*Var, *VariableDie);
529 }
530}
531
David Blaikief7221ad2014-05-27 18:37:43 +0000532void DwarfDebug::finishSubprogramDefinitions() {
David Blaikie7d6f29d2014-10-10 16:59:52 +0000533 for (const auto &P : SPMap)
David Blaikie3a443c22014-11-04 22:12:25 +0000534 forBothCUs(*P.second, [&](DwarfCompileUnit &CU) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000535 CU.finishSubprogramDefinition(cast<DISubprogram>(P.first));
David Blaikie3a443c22014-11-04 22:12:25 +0000536 });
David Blaikief7221ad2014-05-27 18:37:43 +0000537}
538
539
Eric Christopher960ac372012-11-22 00:59:49 +0000540// Collect info for variables that were optimized out.
541void DwarfDebug::collectDeadVariables() {
542 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000543
544 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000545 for (MDNode *N : CU_Nodes->operands()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000546 auto *TheCU = cast<DICompileUnit>(N);
David Blaikie9ba72542014-05-14 21:52:46 +0000547 // Construct subprogram DIE and add variables DIEs.
548 DwarfCompileUnit *SPCU =
549 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
550 assert(SPCU && "Unable to find Compile Unit!");
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000551 for (auto *SP : TheCU->getSubprograms()) {
Eric Christophera6c38a32013-10-15 23:31:38 +0000552 if (ProcessedSPNodes.count(SP) != 0)
553 continue;
David Blaikie1d96cc22014-10-31 22:30:30 +0000554 SPCU->collectDeadVariables(SP);
Eric Christopher960ac372012-11-22 00:59:49 +0000555 }
556 }
557 }
Eric Christopher960ac372012-11-22 00:59:49 +0000558}
559
560void DwarfDebug::finalizeModuleInfo() {
Rafael Espindola063d7252015-03-10 16:58:10 +0000561 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
562
David Blaikie3c2fff32014-05-27 18:37:48 +0000563 finishSubprogramDefinitions();
564
David Blaikieeb1a2722014-06-13 22:18:23 +0000565 finishVariableDefinitions();
566
Eric Christopher960ac372012-11-22 00:59:49 +0000567 // Collect info for variables that were optimized out.
568 collectDeadVariables();
569
Eric Christopherad10cb52013-12-04 23:24:38 +0000570 // Handle anything that needs to be done on a per-unit basis after
571 // all other generation.
David Blaikie1998a2e2014-11-01 01:11:19 +0000572 for (const auto &P : CUMap) {
573 auto &TheCU = *P.second;
Eric Christopher60eb7692013-08-12 20:27:48 +0000574 // Emit DW_AT_containing_type attribute to connect types with their
575 // vtable holding type.
David Blaikie1998a2e2014-11-01 01:11:19 +0000576 TheCU.constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000577
Eric Christopher46e23432013-12-20 04:16:18 +0000578 // Add CU specific attributes if we need to add any.
David Blaikie1998a2e2014-11-01 01:11:19 +0000579 // If we're splitting the dwarf out now that we've got the entire
580 // CU then add the dwo id to it.
581 auto *SkCU = TheCU.getSkeleton();
582 if (useSplitDwarf()) {
583 // Emit a unique identifier for this CU.
584 uint64_t ID = DIEHash(Asm).computeCUSignature(TheCU.getUnitDie());
585 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
586 dwarf::DW_FORM_data8, ID);
587 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
588 dwarf::DW_FORM_data8, ID);
David Blaikie3ffe4dd2014-03-25 05:34:24 +0000589
David Blaikie1998a2e2014-11-01 01:11:19 +0000590 // We don't keep track of which addresses are used in which CU so this
591 // is a bit pessimistic under LTO.
Rafael Espindola063d7252015-03-10 16:58:10 +0000592 if (!AddrPool.isEmpty()) {
593 const MCSymbol *Sym = TLOF.getDwarfAddrSection()->getBeginSymbol();
David Blaikie1998a2e2014-11-01 01:11:19 +0000594 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
Rafael Espindola063d7252015-03-10 16:58:10 +0000595 Sym, Sym);
596 }
597 if (!SkCU->getRangeLists().empty()) {
598 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
David Blaikie1998a2e2014-11-01 01:11:19 +0000599 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
Rafael Espindola063d7252015-03-10 16:58:10 +0000600 Sym, Sym);
601 }
David Blaikie1998a2e2014-11-01 01:11:19 +0000602 }
Eric Christopherd8667202013-12-30 17:22:27 +0000603
David Blaikie1998a2e2014-11-01 01:11:19 +0000604 // If we have code split among multiple sections or non-contiguous
605 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
606 // remain in the .o file, otherwise add a DW_AT_low_pc.
607 // FIXME: We should use ranges allow reordering of code ala
608 // .subsections_via_symbols in mach-o. This would mean turning on
609 // ranges for all subprogram DIEs for mach-o.
David Blaikiea8be0a72014-11-01 01:15:24 +0000610 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
David Blaikie5b02a192014-11-03 23:10:59 +0000611 if (unsigned NumRanges = TheCU.getRanges().size()) {
612 if (NumRanges > 1)
David Blaikie1998a2e2014-11-01 01:11:19 +0000613 // A DW_AT_low_pc attribute may also be specified in combination with
614 // DW_AT_ranges to specify the default base address for use in
615 // location lists (see Section 2.6.2) and range lists (see Section
616 // 2.17.3).
617 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
David Blaikie5b02a192014-11-03 23:10:59 +0000618 else
David Blaikie72d03ef2015-05-02 02:31:49 +0000619 U.setBaseAddress(TheCU.getRanges().front().getStart());
David Blaikie5b02a192014-11-03 23:10:59 +0000620 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
Eric Christopher60eb7692013-08-12 20:27:48 +0000621 }
622 }
623
624 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000625 InfoHolder.computeSizeAndOffsets();
626 if (useSplitDwarf())
627 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +0000628}
629
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000630// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +0000631void DwarfDebug::endModule() {
Craig Toppere73658d2014-04-28 04:05:08 +0000632 assert(CurFn == nullptr);
633 assert(CurMI == nullptr);
Eric Christopher960ac372012-11-22 00:59:49 +0000634
David Blaikie0dc623c2014-10-24 17:53:38 +0000635 // If we aren't actually generating debug info (check beginModule -
636 // conditionalized on !DisableDebugInfoPrinting and the presence of the
637 // llvm.dbg.cu metadata node)
Rafael Espindolaf1a13f52015-03-11 00:51:37 +0000638 if (!MMI->hasDebugInfo())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000639 return;
Eric Christopher960ac372012-11-22 00:59:49 +0000640
Eric Christopher960ac372012-11-22 00:59:49 +0000641 // Finalize the debug info for the module.
642 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000643
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000644 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +0000645
Rafael Espindola063d7252015-03-10 16:58:10 +0000646 if (useSplitDwarf())
647 emitDebugLocDWO();
648 else
649 // Emit info into a debug loc section.
650 emitDebugLoc();
651
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000652 // Corresponding abbreviations into a abbrev section.
653 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +0000654
Rafael Espindolaf1a13f52015-03-11 00:51:37 +0000655 // Emit all the DIEs into a debug info section.
656 emitDebugInfo();
657
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000658 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +0000659 if (GenerateARangeSection)
660 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +0000661
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000662 // Emit info into a debug ranges section.
663 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +0000664
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000665 if (useSplitDwarf()) {
666 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +0000667 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +0000668 emitDebugAbbrevDWO();
David Blaikie4a2f95f2014-03-18 01:17:26 +0000669 emitDebugLineDWO();
Eric Christopher962c9082013-01-15 23:56:56 +0000670 // Emit DWO addresses.
David Blaikied75fb282014-04-23 21:20:10 +0000671 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
Rafael Espindola063d7252015-03-10 16:58:10 +0000672 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000673
Eric Christophera876b822012-08-23 07:32:06 +0000674 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +0000675 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +0000676 emitAccelNames();
677 emitAccelObjC();
678 emitAccelNamespaces();
679 emitAccelTypes();
680 }
Eric Christopher6a841382012-11-19 22:42:10 +0000681
Eric Christopher4b358182013-08-30 00:40:17 +0000682 // Emit the pubnames and pubtypes sections if requested.
683 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +0000684 emitDebugPubNames(GenerateGnuPubSections);
685 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +0000686 }
Devang Patel04d2f2d2009-11-24 01:14:22 +0000687
Devang Pateld0701282010-08-02 17:32:15 +0000688 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000689 SPMap.clear();
David Blaikie825bdd22014-05-21 22:41:17 +0000690 AbstractVariables.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +0000691}
692
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000693// Find abstract variable, if any, associated with Var.
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000694DbgVariable *
695DwarfDebug::getExistingAbstractVariable(InlinedVariable IV,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000696 const DILocalVariable *&Cleansed) {
Devang Patelbb23a4a2011-08-10 21:50:54 +0000697 // More then one inlined variable corresponds to one abstract variable.
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000698 Cleansed = IV.first;
David Blaikie36408e72014-06-04 23:50:52 +0000699 auto I = AbstractVariables.find(Cleansed);
David Blaikie825bdd22014-05-21 22:41:17 +0000700 if (I != AbstractVariables.end())
701 return I->second.get();
David Blaikie36408e72014-06-04 23:50:52 +0000702 return nullptr;
703}
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000704
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000705DbgVariable *DwarfDebug::getExistingAbstractVariable(InlinedVariable IV) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000706 const DILocalVariable *Cleansed;
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000707 return getExistingAbstractVariable(IV, Cleansed);
David Blaikiee847f132014-06-13 22:35:44 +0000708}
709
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000710void DwarfDebug::createAbstractVariable(const DILocalVariable *Var,
David Blaikie6f9e8672014-06-13 23:52:55 +0000711 LexicalScope *Scope) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000712 auto AbsDbgVariable = make_unique<DbgVariable>(Var, /* IA */ nullptr, this);
David Blaikief4c504e2014-10-24 00:43:47 +0000713 InfoHolder.addScopeVariable(Scope, AbsDbgVariable.get());
David Blaikie6f9e8672014-06-13 23:52:55 +0000714 AbstractVariables[Var] = std::move(AbsDbgVariable);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000715}
716
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000717void DwarfDebug::ensureAbstractVariableIsCreated(InlinedVariable IV,
David Blaikie6f9e8672014-06-13 23:52:55 +0000718 const MDNode *ScopeNode) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000719 const DILocalVariable *Cleansed = nullptr;
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000720 if (getExistingAbstractVariable(IV, Cleansed))
David Blaikie6f9e8672014-06-13 23:52:55 +0000721 return;
David Blaikie36408e72014-06-04 23:50:52 +0000722
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000723 createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000724 cast<DILocalScope>(ScopeNode)));
David Blaikie36408e72014-06-04 23:50:52 +0000725}
726
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000727void DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(
728 InlinedVariable IV, const MDNode *ScopeNode) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000729 const DILocalVariable *Cleansed = nullptr;
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000730 if (getExistingAbstractVariable(IV, Cleansed))
David Blaikie6f9e8672014-06-13 23:52:55 +0000731 return;
David Blaikie36408e72014-06-04 23:50:52 +0000732
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +0000733 if (LexicalScope *Scope =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000734 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
David Blaikie6f9e8672014-06-13 23:52:55 +0000735 createAbstractVariable(Cleansed, Scope);
David Blaikie36408e72014-06-04 23:50:52 +0000736}
737
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000738// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000739void DwarfDebug::collectVariableInfoFromMMITable(
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000740 DenseSet<InlinedVariable> &Processed) {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000741 for (const auto &VI : MMI->getVariableDbgInfo()) {
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +0000742 if (!VI.Var)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000743 continue;
Duncan P. N. Exon Smith78a95272015-04-16 22:12:59 +0000744 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
745 "Expected inlined-at fields to agree");
746
747 InlinedVariable Var(VI.Var, VI.Loc->getInlinedAt());
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000748 Processed.insert(Var);
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +0000749 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000750
Devang Patelcdb7d442009-11-10 23:20:04 +0000751 // If variable scope is not found then skip this variable.
Craig Topper353eda42014-04-24 06:44:33 +0000752 if (!Scope)
Devang Patelcdb7d442009-11-10 23:20:04 +0000753 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000754
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000755 ensureAbstractVariableIsCreatedIfScoped(Var, Scope->getScopeNode());
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000756 auto RegVar = make_unique<DbgVariable>(Var.first, Var.second, this);
757 RegVar->initializeMMI(VI.Expr, VI.Slot);
Adrian Prantlca7e4702015-02-10 23:18:28 +0000758 if (InfoHolder.addScopeVariable(Scope, RegVar.get()))
759 ConcreteVariables.push_back(std::move(RegVar));
Devang Patel475d32a2009-10-06 01:26:37 +0000760 }
Devang Patel490c8ab2010-05-20 19:57:06 +0000761}
Devang Patela3e9c9c2010-03-15 18:33:46 +0000762
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000763// Get .debug_loc entry for the instruction range starting at MI.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000764static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000765 const DIExpression *Expr = MI->getDebugExpression();
Devang Patel2442a892011-07-08 17:09:57 +0000766
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000767 assert(MI->getNumOperands() == 4);
Adrian Prantl418d1d12013-07-09 20:28:37 +0000768 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +0000769 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +0000770 // If the second operand is an immediate, this is a
771 // register-indirect address.
772 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +0000773 MLoc.set(MI->getOperand(0).getReg());
774 else
775 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000776 return DebugLocEntry::Value(Expr, MLoc);
Devang Patel2442a892011-07-08 17:09:57 +0000777 }
778 if (MI->getOperand(0).isImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000779 return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
Devang Patel2442a892011-07-08 17:09:57 +0000780 if (MI->getOperand(0).isFPImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000781 return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
Devang Patel2442a892011-07-08 17:09:57 +0000782 if (MI->getOperand(0).isCImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +0000783 return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
Devang Patel2442a892011-07-08 17:09:57 +0000784
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000785 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +0000786}
787
Adrian Prantlb1416832014-08-01 22:11:58 +0000788/// Determine whether two variable pieces overlap.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000789static bool piecesOverlap(const DIExpression *P1, const DIExpression *P2) {
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +0000790 if (!P1->isBitPiece() || !P2->isBitPiece())
Adrian Prantlb1416832014-08-01 22:11:58 +0000791 return true;
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +0000792 unsigned l1 = P1->getBitPieceOffset();
793 unsigned l2 = P2->getBitPieceOffset();
794 unsigned r1 = l1 + P1->getBitPieceSize();
795 unsigned r2 = l2 + P2->getBitPieceSize();
Adrian Prantlb1416832014-08-01 22:11:58 +0000796 // True where [l1,r1[ and [r1,r2[ overlap.
797 return (l1 < r2) && (l2 < r1);
798}
799
800/// Build the location list for all DBG_VALUEs in the function that
801/// describe the same variable. If the ranges of several independent
802/// pieces of the same variable overlap partially, split them up and
803/// combine the ranges. The resulting DebugLocEntries are will have
804/// strict monotonically increasing begin addresses and will never
805/// overlap.
806//
807// Input:
808//
809// Ranges History [var, loc, piece ofs size]
810// 0 | [x, (reg0, piece 0, 32)]
811// 1 | | [x, (reg1, piece 32, 32)] <- IsPieceOfPrevEntry
812// 2 | | ...
813// 3 | [clobber reg0]
Eric Christopherffc5ff32015-02-17 20:02:28 +0000814// 4 [x, (mem, piece 0, 64)] <- overlapping with both previous pieces of
815// x.
Adrian Prantlb1416832014-08-01 22:11:58 +0000816//
817// Output:
818//
819// [0-1] [x, (reg0, piece 0, 32)]
820// [1-3] [x, (reg0, piece 0, 32), (reg1, piece 32, 32)]
821// [3-4] [x, (reg1, piece 32, 32)]
822// [4- ] [x, (mem, piece 0, 64)]
David Blaikiee1a26a62014-08-05 23:14:16 +0000823void
824DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
825 const DbgValueHistoryMap::InstrRanges &Ranges) {
Adrian Prantlcaaf0532014-08-11 21:05:57 +0000826 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
Adrian Prantlb1416832014-08-01 22:11:58 +0000827
828 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
829 const MachineInstr *Begin = I->first;
830 const MachineInstr *End = I->second;
831 assert(Begin->isDebugValue() && "Invalid History entry");
832
833 // Check if a variable is inaccessible in this range.
Adrian Prantl5e1fa852014-08-12 21:55:58 +0000834 if (Begin->getNumOperands() > 1 &&
835 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000836 OpenRanges.clear();
837 continue;
838 }
839
840 // If this piece overlaps with any open ranges, truncate them.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000841 const DIExpression *DIExpr = Begin->getDebugExpression();
Adrian Prantlcaaf0532014-08-11 21:05:57 +0000842 auto Last = std::remove_if(OpenRanges.begin(), OpenRanges.end(),
Adrian Prantl76502d82014-08-11 23:22:59 +0000843 [&](DebugLocEntry::Value R) {
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000844 return piecesOverlap(DIExpr, R.getExpression());
845 });
Adrian Prantlb1416832014-08-01 22:11:58 +0000846 OpenRanges.erase(Last, OpenRanges.end());
847
848 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
849 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
850
851 const MCSymbol *EndLabel;
852 if (End != nullptr)
853 EndLabel = getLabelAfterInsn(End);
854 else if (std::next(I) == Ranges.end())
Rafael Espindola07c03d32015-03-05 02:05:42 +0000855 EndLabel = Asm->getFunctionEnd();
Adrian Prantlb1416832014-08-01 22:11:58 +0000856 else
857 EndLabel = getLabelBeforeInsn(std::next(I)->first);
858 assert(EndLabel && "Forgot label after instruction ending a range!");
859
860 DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
861
862 auto Value = getDebugLocValue(Begin);
David Blaikiee1a26a62014-08-05 23:14:16 +0000863 DebugLocEntry Loc(StartLabel, EndLabel, Value);
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000864 bool couldMerge = false;
865
866 // If this is a piece, it may belong to the current DebugLocEntry.
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +0000867 if (DIExpr->isBitPiece()) {
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000868 // Add this value to the list of open ranges.
Adrian Prantlcaaf0532014-08-11 21:05:57 +0000869 OpenRanges.push_back(Value);
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000870
871 // Attempt to add the piece to the last entry.
872 if (!DebugLoc.empty())
873 if (DebugLoc.back().MergeValues(Loc))
874 couldMerge = true;
875 }
876
877 if (!couldMerge) {
878 // Need to add a new DebugLocEntry. Add all values from still
879 // valid non-overlapping pieces.
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +0000880 if (OpenRanges.size())
881 Loc.addValues(OpenRanges);
882
Adrian Prantlb1416832014-08-01 22:11:58 +0000883 DebugLoc.push_back(std::move(Loc));
884 }
Adrian Prantle09ee3f2014-08-11 20:59:28 +0000885
886 // Attempt to coalesce the ranges of two otherwise identical
887 // DebugLocEntries.
888 auto CurEntry = DebugLoc.rbegin();
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000889 DEBUG({
890 dbgs() << CurEntry->getValues().size() << " Values:\n";
Adrian Prantl6f8c1b62015-05-26 20:06:51 +0000891 for (auto &Value : CurEntry->getValues())
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000892 Value.getExpression()->dump();
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000893 dbgs() << "-----\n";
894 });
Adrian Prantl75707312015-05-26 20:06:48 +0000895
896 auto PrevEntry = std::next(CurEntry);
897 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
898 DebugLoc.pop_back();
Adrian Prantlb1416832014-08-01 22:11:58 +0000899 }
900}
901
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000902DbgVariable *DwarfDebug::createConcreteVariable(LexicalScope &Scope,
903 InlinedVariable IV) {
904 ensureAbstractVariableIsCreatedIfScoped(IV, Scope.getScopeNode());
905 ConcreteVariables.push_back(
906 make_unique<DbgVariable>(IV.first, IV.second, this));
907 InfoHolder.addScopeVariable(&Scope, ConcreteVariables.back().get());
908 return ConcreteVariables.back().get();
909}
Adrian Prantlb1416832014-08-01 22:11:58 +0000910
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000911// Find variables for each lexical scope.
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000912void DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000913 const DISubprogram *SP,
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000914 DenseSet<InlinedVariable> &Processed) {
Eric Christopher270a12c2013-07-03 21:37:03 +0000915 // Grab the variable info that was squirreled away in the MMI side-table.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000916 collectVariableInfoFromMMITable(Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +0000917
Alexey Samsonov0436caa2014-04-30 23:02:40 +0000918 for (const auto &I : DbgValues) {
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000919 InlinedVariable IV = I.first;
920 if (Processed.count(IV))
Devang Patel490c8ab2010-05-20 19:57:06 +0000921 continue;
922
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000923 // Instruction ranges, specifying where IV is accessible.
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000924 const auto &Ranges = I.second;
925 if (Ranges.empty())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000926 continue;
Devang Patel9fc11702010-05-25 23:40:22 +0000927
Craig Topper353eda42014-04-24 06:44:33 +0000928 LexicalScope *Scope = nullptr;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000929 if (const DILocation *IA = IV.second)
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000930 Scope = LScopes.findInlinedScope(IV.first->getScope(), IA);
Duncan P. N. Exon Smithb4749372015-02-17 00:02:27 +0000931 else
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000932 Scope = LScopes.findLexicalScope(IV.first->getScope());
Devang Patel490c8ab2010-05-20 19:57:06 +0000933 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +0000934 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +0000935 continue;
936
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +0000937 Processed.insert(IV);
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000938 DbgVariable *RegVar = createConcreteVariable(*Scope, IV);
939
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000940 const MachineInstr *MInsn = Ranges.front().first;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000941 assert(MInsn->isDebugValue() && "History must begin with debug value");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000942
Alexey Samsonovbb2990d2014-05-27 23:09:50 +0000943 // Check if the first DBG_VALUE is valid for the rest of the function.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000944 if (Ranges.size() == 1 && Ranges.front().second == nullptr) {
945 RegVar->initializeDbgValue(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +0000946 continue;
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000947 }
Devang Patel9fc11702010-05-25 23:40:22 +0000948
Eric Christopher59cc0712013-01-28 17:33:26 +0000949 // Handle multiple DBG_VALUE instructions describing one variable.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000950 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +0000951
Adrian Prantlb1416832014-08-01 22:11:58 +0000952 // Build the location list for this variable.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000953 SmallVector<DebugLocEntry, 8> Entries;
954 buildLocationList(Entries, Ranges);
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +0000955
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000956 // If the variable has an DIBasicType, extract it. Basic types cannot have
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +0000957 // unique identifiers, so don't bother resolving the type with the
958 // identifier map.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000959 const DIBasicType *BT = dyn_cast<DIBasicType>(
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +0000960 static_cast<const Metadata *>(IV.first->getType()));
961
Adrian Prantl92da14b2015-03-02 22:02:33 +0000962 // Finalize the entry by lowering it into a DWARF bytestream.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +0000963 for (auto &Entry : Entries)
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +0000964 Entry.finalize(*Asm, List, BT);
Devang Patela3e9c9c2010-03-15 18:33:46 +0000965 }
Devang Patele0a94bf2010-05-14 21:01:35 +0000966
967 // Collect info for variables that were optimized out.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000968 for (const DILocalVariable *DV : SP->getVariables()) {
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +0000969 if (Processed.insert(InlinedVariable(DV, nullptr)).second)
970 if (LexicalScope *Scope = LScopes.findLexicalScope(DV->getScope()))
971 createConcreteVariable(*Scope, InlinedVariable(DV, nullptr));
Devang Patele0a94bf2010-05-14 21:01:35 +0000972 }
Devang Patel9fc11702010-05-25 23:40:22 +0000973}
Devang Patele0a94bf2010-05-14 21:01:35 +0000974
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000975// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +0000976MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000977 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
978 assert(Label && "Didn't insert label before instruction");
979 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +0000980}
981
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000982// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +0000983MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +0000984 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +0000985}
986
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000987// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +0000988void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Craig Toppere73658d2014-04-28 04:05:08 +0000989 assert(CurMI == nullptr);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000990 CurMI = MI;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +0000991 // Check if source location changes, but ignore DBG_VALUE locations.
992 if (!MI->isDebugValue()) {
993 DebugLoc DL = MI->getDebugLoc();
Duncan P. N. Exon Smithd3a05772015-03-20 19:37:03 +0000994 if (DL != PrevInstLoc) {
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +0000995 if (DL) {
Duncan P. N. Exon Smithd3a05772015-03-20 19:37:03 +0000996 unsigned Flags = 0;
997 PrevInstLoc = DL;
998 if (DL == PrologEndLoc) {
999 Flags |= DWARF2_FLAG_PROLOGUE_END;
1000 PrologEndLoc = DebugLoc();
1001 Flags |= DWARF2_FLAG_IS_STMT;
1002 }
1003 if (DL.getLine() !=
Lang Hames9ff69c82015-04-24 19:11:51 +00001004 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine())
Duncan P. N. Exon Smithd3a05772015-03-20 19:37:03 +00001005 Flags |= DWARF2_FLAG_IS_STMT;
1006
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001007 const MDNode *Scope = DL.getScope();
Devang Patel34a66202011-05-11 19:22:19 +00001008 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Duncan P. N. Exon Smithd3a05772015-03-20 19:37:03 +00001009 } else if (UnknownLocations) {
1010 PrevInstLoc = DL;
Craig Topper353eda42014-04-24 06:44:33 +00001011 recordSourceLine(0, 0, nullptr, 0);
Duncan P. N. Exon Smithd3a05772015-03-20 19:37:03 +00001012 }
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001013 }
Devang Patel9fc11702010-05-25 23:40:22 +00001014 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001015
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001016 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001017 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1018 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001019
1020 // No label needed.
1021 if (I == LabelsBeforeInsn.end())
1022 return;
1023
1024 // Label already assigned.
1025 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001026 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001027
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001028 if (!PrevLabel) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001029 PrevLabel = MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001030 Asm->OutStreamer->EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001031 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001032 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001033}
1034
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001035// Process end of an instruction.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001036void DwarfDebug::endInstruction() {
Craig Toppere73658d2014-04-28 04:05:08 +00001037 assert(CurMI != nullptr);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001038 // Don't create a new label after DBG_VALUE instructions.
1039 // They don't generate code.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001040 if (!CurMI->isDebugValue())
Craig Topper353eda42014-04-24 06:44:33 +00001041 PrevLabel = nullptr;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001042
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001043 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001044 LabelsAfterInsn.find(CurMI);
Craig Topper353eda42014-04-24 06:44:33 +00001045 CurMI = nullptr;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001046
1047 // No label needed.
1048 if (I == LabelsAfterInsn.end())
1049 return;
1050
1051 // Label already assigned.
1052 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001053 return;
1054
1055 // We need a label after this instruction.
1056 if (!PrevLabel) {
Jim Grosbach6f482002015-05-18 18:43:14 +00001057 PrevLabel = MMI->getContext().createTempSymbol();
Lang Hames9ff69c82015-04-24 19:11:51 +00001058 Asm->OutStreamer->EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001059 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001060 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001061}
1062
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001063// Each LexicalScope has first instruction and last instruction to mark
1064// beginning and end of a scope respectively. Create an inverse map that list
1065// scopes starts (and ends) with an instruction. One instruction may start (or
1066// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001067void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001068 SmallVector<LexicalScope *, 4> WorkList;
1069 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001070 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001071 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001072
Craig Topper977e9cd2013-07-03 04:24:43 +00001073 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001074 if (!Children.empty())
Benjamin Kramer15596c72014-03-07 19:09:39 +00001075 WorkList.append(Children.begin(), Children.end());
Devang Patel7771b7c2010-01-20 02:05:23 +00001076
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001077 if (S->isAbstractScope())
1078 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001079
Benjamin Kramer15596c72014-03-07 19:09:39 +00001080 for (const InsnRange &R : S->getRanges()) {
1081 assert(R.first && "InsnRange does not have first instruction!");
1082 assert(R.second && "InsnRange does not have second instruction!");
1083 requestLabelBeforeInsn(R.first);
1084 requestLabelAfterInsn(R.second);
Devang Patel6c74a872010-04-27 19:46:33 +00001085 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001086 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001087}
1088
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001089static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1090 // First known non-DBG_VALUE and non-frame setup location marks
1091 // the beginning of the function body.
1092 for (const auto &MBB : *MF)
1093 for (const auto &MI : MBB)
1094 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
Duncan P. N. Exon Smith9dffcd02015-03-30 19:14:47 +00001095 MI.getDebugLoc()) {
Adrian Prantlb9fa9452014-12-16 00:20:49 +00001096 // Did the target forget to set the FrameSetup flag for CFI insns?
1097 assert(!MI.isCFIInstruction() &&
1098 "First non-frame-setup instruction is a CFI instruction.");
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001099 return MI.getDebugLoc();
Adrian Prantlb9fa9452014-12-16 00:20:49 +00001100 }
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001101 return DebugLoc();
1102}
1103
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001104// Gather pre-function debug information. Assumes being called immediately
1105// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001106void DwarfDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001107 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001108
1109 // If there's no debug info for the function we're not going to do anything.
1110 if (!MMI->hasDebugInfo())
1111 return;
1112
David Blaikie2f040112014-07-25 16:10:16 +00001113 auto DI = FunctionDIs.find(MF->getFunction());
1114 if (DI == FunctionDIs.end())
1115 return;
1116
Eric Christopherfedfa442013-11-01 23:14:17 +00001117 // Grab the lexical scopes for the function, if we don't have any of those
1118 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001119 LScopes.initialize(*MF);
Eric Christopher384f3fe2014-03-20 19:16:16 +00001120 if (LScopes.empty())
Eric Christopherfedfa442013-11-01 23:14:17 +00001121 return;
1122
Alexey Samsonov0436caa2014-04-30 23:02:40 +00001123 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
Eric Christopherfedfa442013-11-01 23:14:17 +00001124
1125 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001126 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001127
Eric Christopher4287a492013-12-09 23:57:44 +00001128 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001129 // belongs to so that we add to the correct per-cu line table in the
1130 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001131 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
David Blaikie2f040112014-07-25 16:10:16 +00001132 // FnScope->getScopeNode() and DI->second should represent the same function,
1133 // though they may not be the same MDNode due to inline functions merged in
1134 // LTO where the debug info metadata still differs (either due to distinct
1135 // written differences - two versions of a linkonce_odr function
1136 // written/copied into two separate files, or some sub-optimal metadata that
1137 // isn't structurally identical (see: file path/name info from clang, which
1138 // includes the directory of the cpp file being built, even when the file name
1139 // is absolute (such as an <> lookup header)))
Eric Christopher4287a492013-12-09 23:57:44 +00001140 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren4e042a62013-02-05 21:52:47 +00001141 assert(TheCU && "Unable to find compile unit!");
Lang Hames9ff69c82015-04-24 19:11:51 +00001142 if (Asm->OutStreamer->hasRawTextSupport())
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001143 // Use a single line table if we are generating assembly.
Lang Hames9ff69c82015-04-24 19:11:51 +00001144 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
Manman Ren9d4c7352013-05-21 00:57:22 +00001145 else
Lang Hames9ff69c82015-04-24 19:11:51 +00001146 Asm->OutStreamer->getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001147
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00001148 // Calculate history for local variables.
Eric Christopherd4e723f2015-02-20 22:36:11 +00001149 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
Eric Christopherd9134482014-08-04 21:25:23 +00001150 DbgValues);
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00001151
1152 // Request labels for the full history.
Alexey Samsonovbb2990d2014-05-27 23:09:50 +00001153 for (const auto &I : DbgValues) {
1154 const auto &Ranges = I.second;
1155 if (Ranges.empty())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001156 continue;
1157
Rafael Espindola07c03d32015-03-05 02:05:42 +00001158 // The first mention of a function argument gets the CurrentFnBegin
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00001159 // label, so arguments are visible when breaking at function entry.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001160 const DILocalVariable *DIVar = Ranges.front().first->getDebugVariable();
Duncan P. N. Exon Smithed013cd2015-07-31 18:58:39 +00001161 if (DIVar->isParameter() &&
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +00001162 getDISubprogram(DIVar->getScope())->describes(MF->getFunction())) {
Rafael Espindola07c03d32015-03-05 02:05:42 +00001163 LabelsBeforeInsn[Ranges.front().first] = Asm->getFunctionBegin();
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +00001164 if (Ranges.front().first->getDebugExpression()->isBitPiece()) {
Adrian Prantlb1416832014-08-01 22:11:58 +00001165 // Mark all non-overlapping initial pieces.
1166 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001167 const DIExpression *Piece = I->first->getDebugExpression();
Adrian Prantlb1416832014-08-01 22:11:58 +00001168 if (std::all_of(Ranges.begin(), I,
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001169 [&](DbgValueHistoryMap::InstrRange Pred) {
1170 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
Adrian Prantlb1416832014-08-01 22:11:58 +00001171 }))
Rafael Espindola07c03d32015-03-05 02:05:42 +00001172 LabelsBeforeInsn[I->first] = Asm->getFunctionBegin();
Adrian Prantlb1416832014-08-01 22:11:58 +00001173 else
1174 break;
1175 }
1176 }
1177 }
Alexey Samsonov414b6fb2014-04-30 21:34:11 +00001178
Alexey Samsonovbb2990d2014-05-27 23:09:50 +00001179 for (const auto &Range : Ranges) {
1180 requestLabelBeforeInsn(Range.first);
1181 if (Range.second)
1182 requestLabelAfterInsn(Range.second);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001183 }
1184 }
Devang Patel002d54d2010-05-26 19:37:24 +00001185
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001186 PrevInstLoc = DebugLoc();
Rafael Espindola07c03d32015-03-05 02:05:42 +00001187 PrevLabel = Asm->getFunctionBegin();
Devang Patel34a66202011-05-11 19:22:19 +00001188
1189 // Record beginning of function.
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001190 PrologEndLoc = findPrologueEndLoc(MF);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001191 if (DILocation *L = PrologEndLoc) {
Saleem Abdulrasoolfc07c722015-01-24 20:19:45 +00001192 // We'd like to list the prologue as "not statements" but GDB behaves
1193 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +00001194 auto *SP = L->getInlinedAtScope()->getSubprogram();
1195 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001196 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001197}
1198
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001199// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001200void DwarfDebug::endFunction(const MachineFunction *MF) {
David Blaikiee75f9632014-10-14 17:12:02 +00001201 assert(CurFn == MF &&
1202 "endFunction should be called with the same function as beginFunction");
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001203
David Blaikie2f040112014-07-25 16:10:16 +00001204 if (!MMI->hasDebugInfo() || LScopes.empty() ||
1205 !FunctionDIs.count(MF->getFunction())) {
Eric Christopher384f3fe2014-03-20 19:16:16 +00001206 // If we don't have a lexical scope for this function then there will
1207 // be a hole in the range information. Keep note of this by setting the
1208 // previously used section to nullptr.
Eric Christopher384f3fe2014-03-20 19:16:16 +00001209 PrevCU = nullptr;
Craig Topper353eda42014-04-24 06:44:33 +00001210 CurFn = nullptr;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001211 return;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001212 }
Devang Patel2904aa92009-11-12 19:02:56 +00001213
Eric Christopher4287a492013-12-09 23:57:44 +00001214 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Lang Hames9ff69c82015-04-24 19:11:51 +00001215 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001216
Devang Patel3acc70e2011-08-15 22:04:40 +00001217 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001218 auto *SP = cast<DISubprogram>(FnScope->getScopeNode());
David Blaikie263a0082014-10-23 00:06:27 +00001219 DwarfCompileUnit &TheCU = *SPMap.lookup(SP);
1220
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001221 DenseSet<InlinedVariable> ProcessedVars;
David Blaikie263a0082014-10-23 00:06:27 +00001222 collectVariableInfo(TheCU, SP, ProcessedVars);
Devang Patel3acc70e2011-08-15 22:04:40 +00001223
David Blaikie3a7ce252014-09-19 17:03:16 +00001224 // Add the range of this function to the list of ranges for the CU.
Rafael Espindola07c03d32015-03-05 02:05:42 +00001225 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
David Blaikie3a7ce252014-09-19 17:03:16 +00001226
1227 // Under -gmlt, skip building the subprogram if there are no inlined
1228 // subroutines inside it.
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +00001229 if (TheCU.getCUNode()->getEmissionKind() == DIBuilder::LineTablesOnly &&
David Blaikiee1c79742014-09-30 21:28:32 +00001230 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
David Blaikie80e5b1e2014-10-24 17:57:34 +00001231 assert(InfoHolder.getScopeVariables().empty());
David Blaikie3a7ce252014-09-19 17:03:16 +00001232 assert(DbgValues.empty());
David Blaikie8b2fdb82014-10-09 18:24:28 +00001233 // FIXME: This wouldn't be true in LTO with a -g (with inlining) CU followed
1234 // by a -gmlt CU. Add a test and remove this assertion.
David Blaikie3a7ce252014-09-19 17:03:16 +00001235 assert(AbstractVariables.empty());
1236 LabelsBeforeInsn.clear();
1237 LabelsAfterInsn.clear();
1238 PrevLabel = nullptr;
1239 CurFn = nullptr;
1240 return;
1241 }
1242
Adrian Prantl049d21c2014-10-13 20:44:58 +00001243#ifndef NDEBUG
1244 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1245#endif
Devang Patel7e623022011-08-10 20:55:27 +00001246 // Construct abstract scopes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001247 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001248 auto *SP = cast<DISubprogram>(AScope->getScopeNode());
David Blaikie4abe19e2014-05-12 18:23:35 +00001249 // Collect info for variables that were optimized out.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001250 for (const DILocalVariable *DV : SP->getVariables()) {
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001251 if (!ProcessedVars.insert(InlinedVariable(DV, nullptr)).second)
David Blaikie4abe19e2014-05-12 18:23:35 +00001252 continue;
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001253 ensureAbstractVariableIsCreated(InlinedVariable(DV, nullptr),
1254 DV->getScope());
Adrian Prantl049d21c2014-10-13 20:44:58 +00001255 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1256 && "ensureAbstractVariableIsCreated inserted abstract scopes");
Devang Patel5c0f85c2010-06-25 22:07:34 +00001257 }
David Blaikie73cc7052014-10-09 20:36:27 +00001258 constructAbstractSubprogramScopeDIE(AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001259 }
Eric Christopher6a841382012-11-19 22:42:10 +00001260
David Blaikie1d072342014-10-09 20:21:36 +00001261 TheCU.constructSubprogramScopeDIE(FnScope);
David Blaikie3a443c22014-11-04 22:12:25 +00001262 if (auto *SkelCU = TheCU.getSkeleton())
1263 if (!LScopes.getAbstractScopesList().empty())
1264 SkelCU->constructSubprogramScopeDIE(FnScope);
Devang Patel3acc70e2011-08-15 22:04:40 +00001265
Bill Wendling2b128d72009-05-20 23:19:06 +00001266 // Clear debug info
David Blaikie825bdd22014-05-21 22:41:17 +00001267 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1268 // DbgVariables except those that are also in AbstractVariables (since they
1269 // can be used cross-function)
David Blaikie80e5b1e2014-10-24 17:57:34 +00001270 InfoHolder.getScopeVariables().clear();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001271 DbgValues.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001272 LabelsBeforeInsn.clear();
1273 LabelsAfterInsn.clear();
Craig Topper353eda42014-04-24 06:44:33 +00001274 PrevLabel = nullptr;
1275 CurFn = nullptr;
Bill Wendling2b128d72009-05-20 23:19:06 +00001276}
1277
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001278// Register a source line with debug info. Returns the unique label that was
1279// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001280void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1281 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001282 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001283 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001284 unsigned Src = 1;
Diego Novillo282450d2014-03-03 18:53:17 +00001285 unsigned Discriminator = 0;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001286 if (auto *Scope = cast_or_null<DIScope>(S)) {
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +00001287 Fn = Scope->getFilename();
1288 Dir = Scope->getDirectory();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001289 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +00001290 Discriminator = LBF->getDiscriminator();
Dan Gohman50849c62010-05-05 23:41:32 +00001291
Lang Hames9ff69c82015-04-24 19:11:51 +00001292 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
David Blaikiec33b3cd2014-04-22 21:27:37 +00001293 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1294 .getOrCreateSourceID(Fn, Dir);
Dan Gohman50849c62010-05-05 23:41:32 +00001295 }
Lang Hames9ff69c82015-04-24 19:11:51 +00001296 Asm->OutStreamer->EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1297 Discriminator, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001298}
1299
Bill Wendling806535f2009-05-20 23:22:40 +00001300//===----------------------------------------------------------------------===//
1301// Emit Methods
1302//===----------------------------------------------------------------------===//
1303
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001304// Emit the debug info section.
1305void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00001306 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Rafael Espindola063d7252015-03-10 16:58:10 +00001307 Holder.emitUnits(/* UseOffsets */ false);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001308}
1309
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001310// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00001311void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00001312 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00001313
1314 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00001315}
Bill Wendling480ff322009-05-20 23:21:38 +00001316
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001317void DwarfDebug::emitAccel(DwarfAccelTable &Accel, MCSection *Section,
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001318 StringRef TableName) {
David Blaikie6741bb02014-09-11 21:12:48 +00001319 Accel.FinalizeTable(Asm, TableName);
Lang Hames9ff69c82015-04-24 19:11:51 +00001320 Asm->OutStreamer->SwitchSection(Section);
Eric Christopher4996c702011-11-07 09:24:32 +00001321
1322 // Emit the full data.
Rafael Espindola063d7252015-03-10 16:58:10 +00001323 Accel.emit(Asm, Section->getBeginSymbol(), this);
David Blaikie6741bb02014-09-11 21:12:48 +00001324}
1325
1326// Emit visible names into a hashed accelerator table section.
1327void DwarfDebug::emitAccelNames() {
1328 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001329 "Names");
Eric Christopher4996c702011-11-07 09:24:32 +00001330}
1331
Eric Christopher48fef592012-12-20 21:58:40 +00001332// Emit objective C classes and categories into a hashed accelerator table
1333// section.
Eric Christopher4996c702011-11-07 09:24:32 +00001334void DwarfDebug::emitAccelObjC() {
David Blaikie6741bb02014-09-11 21:12:48 +00001335 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001336 "ObjC");
Eric Christopher4996c702011-11-07 09:24:32 +00001337}
1338
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001339// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00001340void DwarfDebug::emitAccelNamespaces() {
David Blaikie6741bb02014-09-11 21:12:48 +00001341 emitAccel(AccelNamespace,
1342 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001343 "namespac");
Eric Christopher4996c702011-11-07 09:24:32 +00001344}
1345
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001346// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00001347void DwarfDebug::emitAccelTypes() {
David Blaikie6741bb02014-09-11 21:12:48 +00001348 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001349 "types");
Eric Christopher4996c702011-11-07 09:24:32 +00001350}
1351
Eric Christopherdd1a0122013-09-13 00:35:05 +00001352// Public name handling.
1353// The format for the various pubnames:
1354//
1355// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1356// for the DIE that is named.
1357//
1358// gnu pubnames - offset/index value/name tuples where the offset is the offset
1359// into the CU and the index value is computed according to the type of value
1360// for the DIE that is named.
1361//
1362// For type units the offset is the offset of the skeleton DIE. For split dwarf
1363// it's the offset within the debug_info/debug_types dwo section, however, the
1364// reference in the pubname header doesn't change.
1365
1366/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00001367static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00001368 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00001369 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1370
1371 // We could have a specification DIE that has our most of our knowledge,
1372 // look for that now.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +00001373 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1374 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001375 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
Eric Christopherd2b497b2013-10-16 01:37:49 +00001376 Linkage = dwarf::GIEL_EXTERNAL;
1377 } else if (Die->findAttribute(dwarf::DW_AT_external))
1378 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001379
1380 switch (Die->getTag()) {
1381 case dwarf::DW_TAG_class_type:
1382 case dwarf::DW_TAG_structure_type:
1383 case dwarf::DW_TAG_union_type:
1384 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00001385 return dwarf::PubIndexEntryDescriptor(
1386 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1387 ? dwarf::GIEL_STATIC
1388 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001389 case dwarf::DW_TAG_typedef:
1390 case dwarf::DW_TAG_base_type:
1391 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00001392 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001393 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00001394 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001395 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00001396 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001397 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00001398 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001399 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00001400 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1401 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001402 default:
David Blaikie8dec4072013-09-19 20:40:26 +00001403 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001404 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001405}
1406
Eric Christopher5f93bb92013-09-09 20:03:17 +00001407/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001408///
Eric Christopherdd1a0122013-09-13 00:35:05 +00001409void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001410 MCSection *PSec = GnuStyle
1411 ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1412 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001413
David Blaikie192b45c2014-11-02 06:16:39 +00001414 emitDebugPubSection(GnuStyle, PSec, "Names",
1415 &DwarfCompileUnit::getGlobalNames);
David Blaikie0f55e832014-03-11 23:18:15 +00001416}
1417
1418void DwarfDebug::emitDebugPubSection(
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001419 bool GnuStyle, MCSection *PSec, StringRef Name,
David Blaikie192b45c2014-11-02 06:16:39 +00001420 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) {
David Blaikiec3d9e9e2014-03-06 01:42:00 +00001421 for (const auto &NU : CUMap) {
1422 DwarfCompileUnit *TheU = NU.second;
David Blaikie55bb8ac2014-03-11 23:23:39 +00001423
1424 const auto &Globals = (TheU->*Accessor)();
1425
David Blaikiece2f1cb2014-03-11 23:35:06 +00001426 if (Globals.empty())
1427 continue;
1428
David Blaikiebbad0bb2014-11-01 01:03:39 +00001429 if (auto *Skeleton = TheU->getSkeleton())
David Blaikiec3d9e9e2014-03-06 01:42:00 +00001430 TheU = Skeleton;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001431
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001432 // Start the dwarf pubnames section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001433 Asm->OutStreamer->SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001434
Eric Christopherdd1a0122013-09-13 00:35:05 +00001435 // Emit the header.
Lang Hames9ff69c82015-04-24 19:11:51 +00001436 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
Rafael Espindola9ab09232015-03-17 20:07:06 +00001437 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1438 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00001439 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001440
Lang Hames9ff69c82015-04-24 19:11:51 +00001441 Asm->OutStreamer->EmitLabel(BeginLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001442
Lang Hames9ff69c82015-04-24 19:11:51 +00001443 Asm->OutStreamer->AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00001444 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001445
Lang Hames9ff69c82015-04-24 19:11:51 +00001446 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
Rafael Espindola857546e2015-06-16 23:22:02 +00001447 Asm->emitDwarfSymbolReference(TheU->getLabelBegin());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001448
Lang Hames9ff69c82015-04-24 19:11:51 +00001449 Asm->OutStreamer->AddComment("Compilation Unit Length");
David Blaikie983bfea2014-11-01 23:07:14 +00001450 Asm->EmitInt32(TheU->getLength());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001451
Eric Christopherdd1a0122013-09-13 00:35:05 +00001452 // Emit the pubnames for this compilation unit.
David Blaikie55bb8ac2014-03-11 23:23:39 +00001453 for (const auto &GI : Globals) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00001454 const char *Name = GI.getKeyData();
1455 const DIE *Entity = GI.second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001456
Lang Hames9ff69c82015-04-24 19:11:51 +00001457 Asm->OutStreamer->AddComment("DIE offset");
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001458 Asm->EmitInt32(Entity->getOffset());
1459
Eric Christopherdd1a0122013-09-13 00:35:05 +00001460 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00001461 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
Lang Hames9ff69c82015-04-24 19:11:51 +00001462 Asm->OutStreamer->AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00001463 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00001464 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00001465 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00001466 }
1467
Lang Hames9ff69c82015-04-24 19:11:51 +00001468 Asm->OutStreamer->AddComment("External Name");
1469 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001470 }
1471
Lang Hames9ff69c82015-04-24 19:11:51 +00001472 Asm->OutStreamer->AddComment("End Mark");
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001473 Asm->EmitInt32(0);
Lang Hames9ff69c82015-04-24 19:11:51 +00001474 Asm->OutStreamer->EmitLabel(EndLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001475 }
1476}
1477
Eric Christopherdd1a0122013-09-13 00:35:05 +00001478void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001479 MCSection *PSec = GnuStyle
1480 ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1481 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00001482
David Blaikie192b45c2014-11-02 06:16:39 +00001483 emitDebugPubSection(GnuStyle, PSec, "Types",
1484 &DwarfCompileUnit::getGlobalTypes);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001485}
1486
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001487// Emit visible names into a debug str section.
1488void DwarfDebug::emitDebugStr() {
Eric Christopherf8194852013-12-05 18:06:10 +00001489 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001490 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1491}
1492
Eric Christopher29e874d2014-03-07 22:40:37 +00001493void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001494 const DebugLocStream::Entry &Entry) {
1495 auto &&Comments = DebugLocs.getComments(Entry);
1496 auto Comment = Comments.begin();
1497 auto End = Comments.end();
1498 for (uint8_t Byte : DebugLocs.getBytes(Entry))
Adrian Prantl92da14b2015-03-02 22:02:33 +00001499 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
Adrian Prantlb1416832014-08-01 22:11:58 +00001500}
1501
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001502static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
Adrian Prantl92da14b2015-03-02 22:02:33 +00001503 ByteStreamer &Streamer,
1504 const DebugLocEntry::Value &Value,
1505 unsigned PieceOffsetInBits) {
Eric Christopher5ab3b792015-03-13 00:38:19 +00001506 DebugLocDwarfExpression DwarfExpr(*AP.MF->getSubtarget().getRegisterInfo(),
1507 AP.getDwarfDebug()->getDwarfVersion(),
1508 Streamer);
Adrian Prantlb1416832014-08-01 22:11:58 +00001509 // Regular entry.
Adrian Prantle19e5ef2014-04-27 18:25:40 +00001510 if (Value.isInt()) {
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001511 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1512 BT->getEncoding() == dwarf::DW_ATE_signed_char))
Adrian Prantl092d9482015-01-13 23:39:11 +00001513 DwarfExpr.AddSignedConstant(Value.getInt());
Adrian Prantl66f25952015-01-13 00:04:06 +00001514 else
Adrian Prantl092d9482015-01-13 23:39:11 +00001515 DwarfExpr.AddUnsignedConstant(Value.getInt());
Adrian Prantle19e5ef2014-04-27 18:25:40 +00001516 } else if (Value.isLocation()) {
1517 MachineLocation Loc = Value.getLoc();
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001518 const DIExpression *Expr = Value.getExpression();
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +00001519 if (!Expr || !Expr->getNumElements())
Eric Christopher29e874d2014-03-07 22:40:37 +00001520 // Regular entry.
Adrian Prantl92da14b2015-03-02 22:02:33 +00001521 AP.EmitDwarfRegOp(Streamer, Loc);
Eric Christopher29e874d2014-03-07 22:40:37 +00001522 else {
1523 // Complex address entry.
Adrian Prantl092d9482015-01-13 23:39:11 +00001524 if (Loc.getOffset()) {
1525 DwarfExpr.AddMachineRegIndirect(Loc.getReg(), Loc.getOffset());
Duncan P. N. Exon Smith76c91842015-04-07 03:45:57 +00001526 DwarfExpr.AddExpression(Expr->expr_op_begin(), Expr->expr_op_end(),
1527 PieceOffsetInBits);
Adrian Prantl092d9482015-01-13 23:39:11 +00001528 } else
1529 DwarfExpr.AddMachineRegExpression(Expr, Loc.getReg(),
1530 PieceOffsetInBits);
Eric Christopher29e874d2014-03-07 22:40:37 +00001531 }
1532 }
1533 // else ... ignore constant fp. There is not any good way to
1534 // to represent them here in dwarf.
1535 // FIXME: ^
1536}
1537
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001538void DebugLocEntry::finalize(const AsmPrinter &AP,
1539 DebugLocStream::ListBuilder &List,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001540 const DIBasicType *BT) {
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001541 DebugLocStream::EntryBuilder Entry(List, Begin, End);
1542 BufferByteStreamer Streamer = Entry.getStreamer();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001543 const DebugLocEntry::Value &Value = Values[0];
Adrian Prantl92da14b2015-03-02 22:02:33 +00001544 if (Value.isBitPiece()) {
1545 // Emit all pieces that belong to the same variable and range.
1546 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value P) {
1547 return P.isBitPiece();
1548 }) && "all values are expected to be pieces");
1549 assert(std::is_sorted(Values.begin(), Values.end()) &&
1550 "pieces are expected to be sorted");
1551
1552 unsigned Offset = 0;
1553 for (auto Piece : Values) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001554 const DIExpression *Expr = Piece.getExpression();
Duncan P. N. Exon Smith6a0320a2015-04-14 01:12:42 +00001555 unsigned PieceOffset = Expr->getBitPieceOffset();
1556 unsigned PieceSize = Expr->getBitPieceSize();
Adrian Prantl92da14b2015-03-02 22:02:33 +00001557 assert(Offset <= PieceOffset && "overlapping or duplicate pieces");
1558 if (Offset < PieceOffset) {
1559 // The DWARF spec seriously mandates pieces with no locations for gaps.
Eric Christopher5ab3b792015-03-13 00:38:19 +00001560 DebugLocDwarfExpression Expr(*AP.MF->getSubtarget().getRegisterInfo(),
1561 AP.getDwarfDebug()->getDwarfVersion(),
1562 Streamer);
Adrian Prantl92da14b2015-03-02 22:02:33 +00001563 Expr.AddOpPiece(PieceOffset-Offset, 0);
1564 Offset += PieceOffset-Offset;
1565 }
1566 Offset += PieceSize;
Duncan P. N. Exon Smithc82570b2015-04-13 18:53:11 +00001567
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001568 emitDebugLocValue(AP, BT, Streamer, Piece, PieceOffset);
Adrian Prantl92da14b2015-03-02 22:02:33 +00001569 }
1570 } else {
1571 assert(Values.size() == 1 && "only pieces may have >1 value");
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001572 emitDebugLocValue(AP, BT, Streamer, Value, 0);
Adrian Prantl92da14b2015-03-02 22:02:33 +00001573 }
1574}
1575
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001576void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
Duncan P. N. Exon Smith653c1092015-05-06 19:11:20 +00001577 // Emit the size.
Lang Hames9ff69c82015-04-24 19:11:51 +00001578 Asm->OutStreamer->AddComment("Loc expr size");
Duncan P. N. Exon Smith653c1092015-05-06 19:11:20 +00001579 Asm->EmitInt16(DebugLocs.getBytes(Entry).size());
1580
David Blaikie0e84adc2014-04-01 16:17:41 +00001581 // Emit the entry.
1582 APByteStreamer Streamer(*Asm);
1583 emitDebugLocEntry(Streamer, Entry);
David Blaikie0e84adc2014-04-01 16:17:41 +00001584}
1585
Eric Christopher9046f942013-07-02 21:36:07 +00001586// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00001587void DwarfDebug::emitDebugLoc() {
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001588 // Start the dwarf loc section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001589 Asm->OutStreamer->SwitchSection(
David Blaikie94c1d7f2014-04-02 01:50:20 +00001590 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00001591 unsigned char Size = Asm->getDataLayout().getPointerSize();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001592 for (const auto &List : DebugLocs.getLists()) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001593 Asm->OutStreamer->EmitLabel(List.Label);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001594 const DwarfCompileUnit *CU = List.CU;
1595 for (const auto &Entry : DebugLocs.getEntries(List)) {
Eric Christopher384f3fe2014-03-20 19:16:16 +00001596 // Set up the range. This range is relative to the entry point of the
1597 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1598 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
David Blaikiece343492014-11-03 21:15:30 +00001599 if (auto *Base = CU->getBaseAddress()) {
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001600 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1601 Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
Eric Christopher384f3fe2014-03-20 19:16:16 +00001602 } else {
Lang Hames9ff69c82015-04-24 19:11:51 +00001603 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1604 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
Eric Christopher384f3fe2014-03-20 19:16:16 +00001605 }
David Blaikie94c1d7f2014-04-02 01:50:20 +00001606
David Blaikie0e84adc2014-04-01 16:17:41 +00001607 emitDebugLocEntryLocation(Entry);
Devang Patel9fc11702010-05-25 23:40:22 +00001608 }
Lang Hames9ff69c82015-04-24 19:11:51 +00001609 Asm->OutStreamer->EmitIntValue(0, Size);
1610 Asm->OutStreamer->EmitIntValue(0, Size);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001611 }
1612}
1613
1614void DwarfDebug::emitDebugLocDWO() {
Lang Hames9ff69c82015-04-24 19:11:51 +00001615 Asm->OutStreamer->SwitchSection(
David Blaikie94c1d7f2014-04-02 01:50:20 +00001616 Asm->getObjFileLowering().getDwarfLocDWOSection());
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001617 for (const auto &List : DebugLocs.getLists()) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001618 Asm->OutStreamer->EmitLabel(List.Label);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001619 for (const auto &Entry : DebugLocs.getEntries(List)) {
David Blaikie94c1d7f2014-04-02 01:50:20 +00001620 // Just always use start_length for now - at least that's one address
1621 // rather than two. We could get fancier and try to, say, reuse an
1622 // address we know we've emitted elsewhere (the start of the function?
1623 // The start of the CU or CU subrange that encloses this range?)
1624 Asm->EmitInt8(dwarf::DW_LLE_start_length_entry);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001625 unsigned idx = AddrPool.getIndex(Entry.BeginSym);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001626 Asm->EmitULEB128(idx);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001627 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001628
1629 emitDebugLocEntryLocation(Entry);
David Blaikie9c550ac2014-03-25 01:44:02 +00001630 }
David Blaikie94c1d7f2014-04-02 01:50:20 +00001631 Asm->EmitInt8(dwarf::DW_LLE_end_of_list_entry);
Devang Patel9fc11702010-05-25 23:40:22 +00001632 }
Bill Wendling480ff322009-05-20 23:21:38 +00001633}
1634
Richard Mitton21101b32013-09-19 23:21:01 +00001635struct ArangeSpan {
1636 const MCSymbol *Start, *End;
1637};
1638
1639// Emit a debug aranges section, containing a CU lookup for any
1640// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001641void DwarfDebug::emitDebugARanges() {
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001642 // Provides a unique id per text section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001643 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
Richard Mitton21101b32013-09-19 23:21:01 +00001644
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001645 // Filter labels by section.
1646 for (const SymbolCU &SCU : ArangeLabels) {
1647 if (SCU.Sym->isInSection()) {
1648 // Make a note of this symbol and it's section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001649 MCSection *Section = &SCU.Sym->getSection();
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001650 if (!Section->getKind().isMetadata())
1651 SectionMap[Section].push_back(SCU);
1652 } else {
1653 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1654 // appear in the output. This sucks as we rely on sections to build
1655 // arange spans. We can do it without, but it's icky.
1656 SectionMap[nullptr].push_back(SCU);
1657 }
1658 }
Richard Mitton21101b32013-09-19 23:21:01 +00001659
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001660 // Add terminating symbols for each section.
Rafael Espindola4f4ef152015-03-09 22:08:37 +00001661 for (const auto &I : SectionMap) {
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001662 MCSection *Section = I.first;
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001663 MCSymbol *Sym = nullptr;
1664
Rafael Espindolaf2b408c2015-03-23 21:22:04 +00001665 if (Section)
Lang Hames9ff69c82015-04-24 19:11:51 +00001666 Sym = Asm->OutStreamer->endSection(Section);
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001667
1668 // Insert a final terminator.
1669 SectionMap[Section].push_back(SymbolCU(nullptr, Sym));
1670 }
1671
1672 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
1673
Rafael Espindola4f4ef152015-03-09 22:08:37 +00001674 for (auto &I : SectionMap) {
1675 const MCSection *Section = I.first;
1676 SmallVector<SymbolCU, 8> &List = I.second;
Richard Mitton21101b32013-09-19 23:21:01 +00001677 if (List.size() < 2)
1678 continue;
1679
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001680 // If we have no section (e.g. common), just write out
1681 // individual spans for each symbol.
1682 if (!Section) {
1683 for (const SymbolCU &Cur : List) {
1684 ArangeSpan Span;
1685 Span.Start = Cur.Sym;
1686 Span.End = nullptr;
1687 if (Cur.CU)
1688 Spans[Cur.CU].push_back(Span);
1689 }
1690 continue;
1691 }
1692
Richard Mitton21101b32013-09-19 23:21:01 +00001693 // Sort the symbols by offset within the section.
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00001694 std::sort(List.begin(), List.end(),
1695 [&](const SymbolCU &A, const SymbolCU &B) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001696 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
1697 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00001698
1699 // Symbols with no order assigned should be placed at the end.
1700 // (e.g. section end labels)
1701 if (IA == 0)
1702 return false;
1703 if (IB == 0)
1704 return true;
1705 return IA < IB;
1706 });
Richard Mitton21101b32013-09-19 23:21:01 +00001707
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001708 // Build spans between each label.
1709 const MCSymbol *StartSym = List[0].Sym;
1710 for (size_t n = 1, e = List.size(); n < e; n++) {
1711 const SymbolCU &Prev = List[n - 1];
1712 const SymbolCU &Cur = List[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001713
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00001714 // Try and build the longest span we can within the same CU.
1715 if (Cur.CU != Prev.CU) {
1716 ArangeSpan Span;
1717 Span.Start = StartSym;
1718 Span.End = Cur.Sym;
1719 Spans[Prev.CU].push_back(Span);
1720 StartSym = Cur.Sym;
Richard Mitton21101b32013-09-19 23:21:01 +00001721 }
1722 }
1723 }
1724
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001725 // Start the dwarf aranges section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001726 Asm->OutStreamer->SwitchSection(
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001727 Asm->getObjFileLowering().getDwarfARangesSection());
1728
Richard Mitton21101b32013-09-19 23:21:01 +00001729 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
1730
1731 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00001732 std::vector<DwarfCompileUnit *> CUs;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001733 for (const auto &it : Spans) {
1734 DwarfCompileUnit *CU = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00001735 CUs.push_back(CU);
1736 }
1737
1738 // Sort the CU list (again, to ensure consistent output order).
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00001739 std::sort(CUs.begin(), CUs.end(), [](const DwarfUnit *A, const DwarfUnit *B) {
1740 return A->getUniqueID() < B->getUniqueID();
1741 });
Richard Mitton21101b32013-09-19 23:21:01 +00001742
1743 // Emit an arange table for each CU we used.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001744 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton21101b32013-09-19 23:21:01 +00001745 std::vector<ArangeSpan> &List = Spans[CU];
1746
David Blaikie27e35f22014-11-02 01:21:43 +00001747 // Describe the skeleton CU's offset and length, not the dwo file's.
1748 if (auto *Skel = CU->getSkeleton())
1749 CU = Skel;
1750
Richard Mitton21101b32013-09-19 23:21:01 +00001751 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001752 unsigned ContentSize =
1753 sizeof(int16_t) + // DWARF ARange version number
1754 sizeof(int32_t) + // Offset of CU in the .debug_info section
1755 sizeof(int8_t) + // Pointer Size (in bytes)
1756 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00001757
1758 unsigned TupleSize = PtrSize * 2;
1759
1760 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00001761 unsigned Padding =
1762 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00001763
1764 ContentSize += Padding;
1765 ContentSize += (List.size() + 1) * TupleSize;
1766
1767 // For each compile unit, write the list of spans it covers.
Lang Hames9ff69c82015-04-24 19:11:51 +00001768 Asm->OutStreamer->AddComment("Length of ARange Set");
Richard Mitton21101b32013-09-19 23:21:01 +00001769 Asm->EmitInt32(ContentSize);
Lang Hames9ff69c82015-04-24 19:11:51 +00001770 Asm->OutStreamer->AddComment("DWARF Arange version number");
Richard Mitton21101b32013-09-19 23:21:01 +00001771 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
Lang Hames9ff69c82015-04-24 19:11:51 +00001772 Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
Rafael Espindola857546e2015-06-16 23:22:02 +00001773 Asm->emitDwarfSymbolReference(CU->getLabelBegin());
Lang Hames9ff69c82015-04-24 19:11:51 +00001774 Asm->OutStreamer->AddComment("Address Size (in bytes)");
Richard Mitton21101b32013-09-19 23:21:01 +00001775 Asm->EmitInt8(PtrSize);
Lang Hames9ff69c82015-04-24 19:11:51 +00001776 Asm->OutStreamer->AddComment("Segment Size (in bytes)");
Richard Mitton21101b32013-09-19 23:21:01 +00001777 Asm->EmitInt8(0);
1778
Lang Hames9ff69c82015-04-24 19:11:51 +00001779 Asm->OutStreamer->EmitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00001780
Benjamin Kramer15596c72014-03-07 19:09:39 +00001781 for (const ArangeSpan &Span : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00001782 Asm->EmitLabelReference(Span.Start, PtrSize);
1783
1784 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00001785 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00001786 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00001787 } else {
1788 // For symbols without an end marker (e.g. common), we
1789 // write a single arange entry containing just that one symbol.
1790 uint64_t Size = SymSize[Span.Start];
1791 if (Size == 0)
1792 Size = 1;
1793
Lang Hames9ff69c82015-04-24 19:11:51 +00001794 Asm->OutStreamer->EmitIntValue(Size, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00001795 }
Richard Mitton21101b32013-09-19 23:21:01 +00001796 }
1797
Lang Hames9ff69c82015-04-24 19:11:51 +00001798 Asm->OutStreamer->AddComment("ARange terminator");
1799 Asm->OutStreamer->EmitIntValue(0, PtrSize);
1800 Asm->OutStreamer->EmitIntValue(0, PtrSize);
Richard Mitton21101b32013-09-19 23:21:01 +00001801 }
Bill Wendling480ff322009-05-20 23:21:38 +00001802}
1803
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001804// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00001805void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00001806 // Start the dwarf ranges section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001807 Asm->OutStreamer->SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001808 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00001809
Eric Christopher0f63d062013-12-03 00:45:45 +00001810 // Size for our labels.
1811 unsigned char Size = Asm->getDataLayout().getPointerSize();
1812
1813 // Grab the specific ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001814 for (const auto &I : CUMap) {
1815 DwarfCompileUnit *TheCU = I.second;
Eric Christopher0f63d062013-12-03 00:45:45 +00001816
David Blaikie3a443c22014-11-04 22:12:25 +00001817 if (auto *Skel = TheCU->getSkeleton())
1818 TheCU = Skel;
1819
Eric Christopher0f63d062013-12-03 00:45:45 +00001820 // Iterate over the misc ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001821 for (const RangeSpanList &List : TheCU->getRangeLists()) {
Eric Christopherf8790642013-12-04 22:04:50 +00001822 // Emit our symbol so we can find the beginning of the range.
Lang Hames9ff69c82015-04-24 19:11:51 +00001823 Asm->OutStreamer->EmitLabel(List.getSym());
Eric Christopher0f63d062013-12-03 00:45:45 +00001824
Benjamin Kramer15596c72014-03-07 19:09:39 +00001825 for (const RangeSpan &Range : List.getRanges()) {
Eric Christopher0f63d062013-12-03 00:45:45 +00001826 const MCSymbol *Begin = Range.getStart();
1827 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00001828 assert(Begin && "Range without a begin symbol?");
1829 assert(End && "Range without an end symbol?");
David Blaikiece343492014-11-03 21:15:30 +00001830 if (auto *Base = TheCU->getBaseAddress()) {
Eric Christopherece0e902014-04-25 22:23:54 +00001831 Asm->EmitLabelDifference(Begin, Base, Size);
1832 Asm->EmitLabelDifference(End, Base, Size);
1833 } else {
Lang Hames9ff69c82015-04-24 19:11:51 +00001834 Asm->OutStreamer->EmitSymbolValue(Begin, Size);
1835 Asm->OutStreamer->EmitSymbolValue(End, Size);
Eric Christopherece0e902014-04-25 22:23:54 +00001836 }
Eric Christopher0f63d062013-12-03 00:45:45 +00001837 }
1838
1839 // And terminate the list with two 0 values.
Lang Hames9ff69c82015-04-24 19:11:51 +00001840 Asm->OutStreamer->EmitIntValue(0, Size);
1841 Asm->OutStreamer->EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00001842 }
Devang Patel12563b32010-04-16 23:33:45 +00001843 }
Bill Wendling480ff322009-05-20 23:21:38 +00001844}
1845
Eric Christopherd692c1d2012-12-11 19:42:09 +00001846// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001847
David Blaikie65a74662014-04-25 18:26:14 +00001848void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
David Blaikief9b6a552014-04-22 22:39:41 +00001849 std::unique_ptr<DwarfUnit> NewU) {
David Blaikiecafd9622014-11-02 08:51:37 +00001850 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +00001851 U.getCUNode()->getSplitDebugFilename());
David Blaikie38fe6342014-01-09 04:28:46 +00001852
David Blaikie38fe6342014-01-09 04:28:46 +00001853 if (!CompilationDir.empty())
David Blaikiecafd9622014-11-02 08:51:37 +00001854 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
David Blaikie38fe6342014-01-09 04:28:46 +00001855
David Blaikief9b6a552014-04-22 22:39:41 +00001856 addGnuPubAttributes(*NewU, Die);
David Blaikie38fe6342014-01-09 04:28:46 +00001857
David Blaikief9b6a552014-04-22 22:39:41 +00001858 SkeletonHolder.addUnit(std::move(NewU));
David Blaikie38fe6342014-01-09 04:28:46 +00001859}
1860
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001861// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
1862// 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 +00001863// DW_AT_addr_base, DW_AT_ranges_base.
David Blaikief9b6a552014-04-22 22:39:41 +00001864DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001865
David Blaikief9b6a552014-04-22 22:39:41 +00001866 auto OwnedUnit = make_unique<DwarfCompileUnit>(
David Blaikiebd579052014-04-28 21:14:27 +00001867 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
David Blaikief9b6a552014-04-22 22:39:41 +00001868 DwarfCompileUnit &NewCU = *OwnedUnit;
Rafael Espindola063d7252015-03-10 16:58:10 +00001869 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection());
Eric Christopher4c7765f2013-01-17 03:00:04 +00001870
Rafael Espindola063d7252015-03-10 16:58:10 +00001871 NewCU.initStmtList();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001872
David Blaikie92a2f8a2014-04-28 21:04:29 +00001873 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
Eric Christopherc8a310e2012-12-10 23:34:43 +00001874
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001875 return NewCU;
1876}
1877
Eric Christopherd692c1d2012-12-11 19:42:09 +00001878// Emit the .debug_info.dwo section for separated dwarf. This contains the
1879// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001880void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00001881 assert(useSplitDwarf() && "No split dwarf debug info?");
Rafael Espindola063d7252015-03-10 16:58:10 +00001882 // Don't emit relocations into the dwo file.
1883 InfoHolder.emitUnits(/* UseOffsets */ true);
Eric Christopher3c5a1912012-12-19 22:02:53 +00001884}
1885
1886// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
1887// abbreviations for the .debug_info.dwo section.
1888void DwarfDebug::emitDebugAbbrevDWO() {
1889 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00001890 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001891}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001892
David Blaikie4a2f95f2014-03-18 01:17:26 +00001893void DwarfDebug::emitDebugLineDWO() {
1894 assert(useSplitDwarf() && "No split dwarf?");
Lang Hames9ff69c82015-04-24 19:11:51 +00001895 Asm->OutStreamer->SwitchSection(
David Blaikie4a2f95f2014-03-18 01:17:26 +00001896 Asm->getObjFileLowering().getDwarfLineDWOSection());
Lang Hames9ff69c82015-04-24 19:11:51 +00001897 SplitTypeUnitFileTable.Emit(*Asm->OutStreamer);
David Blaikie4a2f95f2014-03-18 01:17:26 +00001898}
1899
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001900// Emit the .debug_str.dwo section for separated dwarf. This contains the
1901// string section and is identical in format to traditional .debug_str
1902// sections.
1903void DwarfDebug::emitDebugStrDWO() {
1904 assert(useSplitDwarf() && "No split dwarf?");
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001905 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00001906 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
David Blaikie6741bb02014-09-11 21:12:48 +00001907 OffSec);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001908}
David Blaikie409dd9c2013-11-19 23:08:21 +00001909
David Blaikie47f4b822014-03-19 00:11:28 +00001910MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
1911 if (!useSplitDwarf())
1912 return nullptr;
1913 if (SingleCU)
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +00001914 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode()->getDirectory());
David Blaikie47f4b822014-03-19 00:11:28 +00001915 return &SplitTypeUnitFileTable;
1916}
1917
Adrian Prantlee5feaf2015-07-15 17:01:41 +00001918uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
David Blaikief3de2ab2014-04-26 16:26:41 +00001919 MD5 Hash;
1920 Hash.update(Identifier);
1921 // ... take the least significant 8 bytes and return those. Our MD5
1922 // implementation always returns its results in little endian, swap bytes
1923 // appropriately.
1924 MD5::MD5Result Result;
1925 Hash.final(Result);
Rui Ueyama3206b792015-03-02 21:19:12 +00001926 return support::endian::read64le(Result + 8);
David Blaikief3de2ab2014-04-26 16:26:41 +00001927}
1928
David Blaikie15632ae2014-02-12 00:31:30 +00001929void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikie65a74662014-04-25 18:26:14 +00001930 StringRef Identifier, DIE &RefDie,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001931 const DICompositeType *CTy) {
David Blaikiee12b49a2014-04-26 17:27:38 +00001932 // Fast path if we're building some type units and one has already used the
1933 // address pool we know we're going to throw away all this work anyway, so
1934 // don't bother building dependent types.
1935 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
1936 return;
1937
David Blaikie47f615e2013-12-17 23:32:35 +00001938 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
Chandler Carruthb587ab62014-01-20 08:07:07 +00001939 if (TU) {
David Blaikie15632ae2014-02-12 00:31:30 +00001940 CU.addDIETypeSignature(RefDie, *TU);
Chandler Carruthb587ab62014-01-20 08:07:07 +00001941 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00001942 }
1943
David Blaikiee12b49a2014-04-26 17:27:38 +00001944 bool TopLevelType = TypeUnitsUnderConstruction.empty();
1945 AddrPool.resetUsedFlag();
1946
David Blaikie29459ae2014-07-25 17:11:58 +00001947 auto OwnedUnit = make_unique<DwarfTypeUnit>(
1948 InfoHolder.getUnits().size() + TypeUnitsUnderConstruction.size(), CU, Asm,
1949 this, &InfoHolder, getDwoLineTable(CU));
David Blaikief9b6a552014-04-22 22:39:41 +00001950 DwarfTypeUnit &NewTU = *OwnedUnit;
David Blaikie92a2f8a2014-04-28 21:04:29 +00001951 DIE &UnitDie = NewTU.getUnitDie();
David Blaikief9b6a552014-04-22 22:39:41 +00001952 TU = &NewTU;
Eric Christopher793c7472014-04-28 20:42:22 +00001953 TypeUnitsUnderConstruction.push_back(
1954 std::make_pair(std::move(OwnedUnit), CTy));
Chandler Carruthb587ab62014-01-20 08:07:07 +00001955
David Blaikie92a2f8a2014-04-28 21:04:29 +00001956 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie637cac42014-04-22 23:09:36 +00001957 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00001958
David Blaikief3de2ab2014-04-26 16:26:41 +00001959 uint64_t Signature = makeTypeSignature(Identifier);
David Blaikief9b6a552014-04-22 22:39:41 +00001960 NewTU.setTypeSignature(Signature);
David Blaikief3de2ab2014-04-26 16:26:41 +00001961
David Blaikie29459ae2014-07-25 17:11:58 +00001962 if (useSplitDwarf())
David Blaikiea34568b2014-11-01 20:06:28 +00001963 NewTU.initSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
David Blaikie29459ae2014-07-25 17:11:58 +00001964 else {
David Blaikie92a2f8a2014-04-28 21:04:29 +00001965 CU.applyStmtList(UnitDie);
David Blaikie29459ae2014-07-25 17:11:58 +00001966 NewTU.initSection(
1967 Asm->getObjFileLowering().getDwarfTypesSection(Signature));
1968 }
Chandler Carruthb587ab62014-01-20 08:07:07 +00001969
David Blaikief3de2ab2014-04-26 16:26:41 +00001970 NewTU.setType(NewTU.createTypeDIE(CTy));
1971
David Blaikiee12b49a2014-04-26 17:27:38 +00001972 if (TopLevelType) {
1973 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
1974 TypeUnitsUnderConstruction.clear();
1975
1976 // Types referencing entries in the address table cannot be placed in type
1977 // units.
1978 if (AddrPool.hasBeenUsed()) {
1979
1980 // Remove all the types built while building this type.
1981 // This is pessimistic as some of these types might not be dependent on
1982 // the type that used an address.
1983 for (const auto &TU : TypeUnitsToAdd)
1984 DwarfTypeUnits.erase(TU.second);
1985
1986 // Construct this type in the CU directly.
1987 // This is inefficient because all the dependent types will be rebuilt
1988 // from scratch, including building them in type units, discovering that
1989 // they depend on addresses, throwing them out and rebuilding them.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001990 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
David Blaikiee12b49a2014-04-26 17:27:38 +00001991 return;
1992 }
1993
1994 // If the type wasn't dependent on fission addresses, finish adding the type
1995 // and all its dependent types.
David Blaikiec13bc972014-08-27 05:04:14 +00001996 for (auto &TU : TypeUnitsToAdd)
David Blaikiee12b49a2014-04-26 17:27:38 +00001997 InfoHolder.addUnit(std::move(TU.first));
David Blaikiee12b49a2014-04-26 17:27:38 +00001998 }
David Blaikief9b6a552014-04-22 22:39:41 +00001999 CU.addDIETypeSignature(RefDie, NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00002000}
David Blaikie4bd13b72014-03-07 18:49:45 +00002001
David Blaikie2406a0622014-04-23 23:37:35 +00002002// Accelerator table mutators - add each name along with its companion
2003// DIE to the proper table while ensuring that the name that we're going
2004// to reference is in the string table. We do this since the names we
2005// add may not only be identical to the names in the DIE.
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002006void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
David Blaikie2406a0622014-04-23 23:37:35 +00002007 if (!useDwarfAccelTables())
2008 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002009 AccelNames.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikie2406a0622014-04-23 23:37:35 +00002010}
David Blaikie0ee82b92014-04-24 00:53:32 +00002011
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002012void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
David Blaikie0ee82b92014-04-24 00:53:32 +00002013 if (!useDwarfAccelTables())
2014 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002015 AccelObjC.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikie0ee82b92014-04-24 00:53:32 +00002016}
David Blaikieecf04152014-04-24 01:02:42 +00002017
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002018void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
David Blaikieecf04152014-04-24 01:02:42 +00002019 if (!useDwarfAccelTables())
2020 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002021 AccelNamespace.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikieecf04152014-04-24 01:02:42 +00002022}
David Blaikie18d33752014-04-24 01:23:49 +00002023
David Blaikieb0b3fcf2014-04-25 18:52:29 +00002024void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
David Blaikie18d33752014-04-24 01:23:49 +00002025 if (!useDwarfAccelTables())
2026 return;
Duncan P. N. Exon Smithf4599942015-05-24 16:44:32 +00002027 AccelTypes.AddName(InfoHolder.getStringPool().getEntry(*Asm, Name), &Die);
David Blaikie18d33752014-04-24 01:23:49 +00002028}