blob: f400eb99219c474caba2732c104f6bf0e48fd728 [file] [log] [blame]
Bill Wendling0310d762009-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 Lattner6cde3e62010-03-09 00:39:24 +000013
Devang Patele4b27562009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling0310d762009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher0d27ca12013-08-08 23:45:55 +000017#include "DIEHash.h"
Eric Christopher09ac3d82011-11-07 09:24:32 +000018#include "DwarfAccelTable.h"
Devang Patel8b9df622011-04-12 17:40:32 +000019#include "DwarfCompileUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/Triple.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/DIBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/Constants.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000032#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000033#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000034#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000035#include "llvm/MC/MCSymbol.h"
Devang Pateleac9c072010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
David Majnemerd7d43dc2013-08-21 06:13:34 +000038#include "llvm/Support/Dwarf.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000039#include "llvm/Support/ErrorHandling.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000040#include "llvm/Support/FormattedStream.h"
Eric Christopher3dee5752013-07-26 17:02:41 +000041#include "llvm/Support/MD5.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000042#include "llvm/Support/Path.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000043#include "llvm/Support/Timer.h"
44#include "llvm/Support/ValueHandle.h"
45#include "llvm/Target/TargetFrameLowering.h"
46#include "llvm/Target/TargetLoweringObjectFile.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetOptions.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000050using namespace llvm;
51
Eric Christopher6e61e0b2013-07-23 22:16:41 +000052static cl::opt<bool>
53DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
54 cl::desc("Disable debug info printing"));
Devang Pateleac9c072010-04-27 19:46:33 +000055
Eric Christopher6e61e0b2013-07-23 22:16:41 +000056static cl::opt<bool> UnknownLocations(
57 "use-unknown-locations", cl::Hidden,
58 cl::desc("Make an absence of debug location information explicit."),
59 cl::init(false));
Dan Gohman281d65d2010-05-07 01:08:53 +000060
Eric Christopher6e61e0b2013-07-23 22:16:41 +000061static cl::opt<bool>
Eric Christopher3dee5752013-07-26 17:02:41 +000062GenerateODRHash("generate-odr-hash", cl::Hidden,
63 cl::desc("Add an ODR hash to external type DIEs."),
64 cl::init(false));
65
Eric Christopher0710bfa2013-08-13 01:21:55 +000066static cl::opt<bool>
67GenerateCUHash("generate-cu-hash", cl::Hidden,
68 cl::desc("Add the CU hash as the dwo_id."),
69 cl::init(false));
70
Eric Christopher8f1a9292013-09-13 00:35:05 +000071static cl::opt<bool>
72GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
73 cl::desc("Generate GNU-style pubnames and pubtypes"),
74 cl::init(false));
75
Eric Christopher20f47ab2012-08-23 22:36:40 +000076namespace {
Eric Christopher6e61e0b2013-07-23 22:16:41 +000077enum DefaultOnOff {
78 Default,
79 Enable,
80 Disable
81};
Eric Christopher20f47ab2012-08-23 22:36:40 +000082}
Eric Christopher09ac3d82011-11-07 09:24:32 +000083
Eric Christopher6e61e0b2013-07-23 22:16:41 +000084static cl::opt<DefaultOnOff>
85DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
86 cl::desc("Output prototype dwarf accelerator tables."),
87 cl::values(clEnumVal(Default, "Default for platform"),
88 clEnumVal(Enable, "Enabled"),
89 clEnumVal(Disable, "Disabled"), clEnumValEnd),
90 cl::init(Default));
Eric Christopher20f47ab2012-08-23 22:36:40 +000091
Eric Christopher6e61e0b2013-07-23 22:16:41 +000092static cl::opt<DefaultOnOff>
Eric Christopher6e61e0b2013-07-23 22:16:41 +000093SplitDwarf("split-dwarf", cl::Hidden,
94 cl::desc("Output prototype dwarf split debug info."),
95 cl::values(clEnumVal(Default, "Default for platform"),
96 clEnumVal(Enable, "Enabled"),
97 clEnumVal(Disable, "Disabled"), clEnumValEnd),
98 cl::init(Default));
Eric Christopherf5b6dcd2012-11-12 22:22:20 +000099
Eric Christopherf04e4ef2013-08-19 21:07:38 +0000100static cl::opt<DefaultOnOff>
Eric Christopher63ae7c92013-08-26 23:24:35 +0000101DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
102 cl::desc("Generate DWARF pubnames and pubtypes sections"),
103 cl::values(clEnumVal(Default, "Default for platform"),
104 clEnumVal(Enable, "Enabled"),
105 clEnumVal(Disable, "Disabled"), clEnumValEnd),
106 cl::init(Default));
Eric Christopherf04e4ef2013-08-19 21:07:38 +0000107
Benjamin Kramer0b6962f2013-08-24 12:54:27 +0000108static const char *const DWARFGroupName = "DWARF Emission";
109static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendling5f017e82010-04-07 09:28:04 +0000110
Bill Wendling0310d762009-05-15 09:23:25 +0000111//===----------------------------------------------------------------------===//
112
Eric Christopherb6dc8652012-11-27 22:43:45 +0000113// Configuration values for initial hash set sizes (log2).
114//
Bill Wendling0310d762009-05-15 09:23:25 +0000115static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling0310d762009-05-15 09:23:25 +0000116
117namespace llvm {
118
Manman Ren8199f0b2013-10-08 19:07:44 +0000119/// resolve - Look in the DwarfDebug map for the MDNode that
120/// corresponds to the reference.
121template <typename T>
122T DbgVariable::resolve(DIRef<T> Ref) const {
123 return DD->resolve(Ref);
124}
125
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000126DIType DbgVariable::getType() const {
Devang Patel3cbee302011-04-12 22:53:02 +0000127 DIType Ty = Var.getType();
128 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
129 // addresses instead.
130 if (Var.isBlockByrefVariable()) {
131 /* Byref variables, in Blocks, are declared by the programmer as
132 "SomeType VarName;", but the compiler creates a
133 __Block_byref_x_VarName struct, and gives the variable VarName
134 either the struct, or a pointer to the struct, as its type. This
135 is necessary for various behind-the-scenes things the compiler
136 needs to do with by-reference variables in blocks.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000137
Devang Patel3cbee302011-04-12 22:53:02 +0000138 However, as far as the original *programmer* is concerned, the
139 variable should still have type 'SomeType', as originally declared.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000140
Devang Patel3cbee302011-04-12 22:53:02 +0000141 The following function dives into the __Block_byref_x_VarName
142 struct to find the original type of the variable. This will be
143 passed back to the code generating the type for the Debug
144 Information Entry for the variable 'VarName'. 'VarName' will then
145 have the original type 'SomeType' in its debug information.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000146
Devang Patel3cbee302011-04-12 22:53:02 +0000147 The original type 'SomeType' will be the type of the field named
148 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000149
Devang Patel3cbee302011-04-12 22:53:02 +0000150 NOTE: In order for this to not completely fail on the debugger
151 side, the Debug Information Entry for the variable VarName needs to
152 have a DW_AT_location that tells the debugger how to unwind through
153 the pointers and __Block_byref_x_VarName struct to find the actual
154 value of the variable. The function addBlockByrefType does this. */
155 DIType subType = Ty;
Eric Christopher31667622013-08-08 01:41:00 +0000156 uint16_t tag = Ty.getTag();
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000157
Eric Christopher4dc211a2013-09-04 19:53:21 +0000158 if (tag == dwarf::DW_TAG_pointer_type)
Manman Ren8199f0b2013-10-08 19:07:44 +0000159 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000160
Eric Christopher4dc211a2013-09-04 19:53:21 +0000161 DIArray Elements = DICompositeType(subType).getTypeArray();
Devang Patel3cbee302011-04-12 22:53:02 +0000162 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
Eric Christopher4dc211a2013-09-04 19:53:21 +0000163 DIDerivedType DT = DIDerivedType(Elements.getElement(i));
Devang Patel3cbee302011-04-12 22:53:02 +0000164 if (getName() == DT.getName())
Manman Ren8199f0b2013-10-08 19:07:44 +0000165 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel8bd11de2010-08-09 21:01:39 +0000166 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000167 }
Devang Patel3cbee302011-04-12 22:53:02 +0000168 return Ty;
169}
Bill Wendling0310d762009-05-15 09:23:25 +0000170
Chris Lattnerea761862010-04-05 04:09:20 +0000171} // end llvm namespace
Bill Wendling0310d762009-05-15 09:23:25 +0000172
Manman Ren0e6783f2013-07-02 23:40:10 +0000173/// Return Dwarf Version by checking module flags.
174static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Renc8cfaa12013-07-16 23:21:16 +0000175 Value *Val = M->getModuleFlag("Dwarf Version");
176 if (!Val)
Eric Christopherefc47ec2013-09-04 22:21:24 +0000177 return dwarf::DWARF_VERSION;
Manman Renc8cfaa12013-07-16 23:21:16 +0000178 return cast<ConstantInt>(Val)->getZExtValue();
Manman Ren0e6783f2013-07-02 23:40:10 +0000179}
180
Chris Lattner49cd6642010-04-05 05:11:15 +0000181DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Eric Christopher28bd25a2012-12-10 19:51:13 +0000182 : Asm(A), MMI(Asm->MMI), FirstCU(0),
Jim Grosbach1e20b962010-07-21 21:21:52 +0000183 AbbreviationsSet(InitAbbreviationsSetSize),
Eric Christopherb6714222013-01-08 22:22:06 +0000184 SourceIdMap(DIEValueAllocator),
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000185 PrevLabel(NULL), GlobalCUIndexCount(0),
Eric Christopherb6714222013-01-08 22:22:06 +0000186 InfoHolder(A, &AbbreviationsSet, &Abbreviations, "info_string",
187 DIEValueAllocator),
Eric Christopher6eebe472012-12-19 22:02:53 +0000188 SkeletonAbbrevSet(InitAbbreviationsSetSize),
Eric Christopherb6714222013-01-08 22:22:06 +0000189 SkeletonHolder(A, &SkeletonAbbrevSet, &SkeletonAbbrevs, "skel_string",
190 DIEValueAllocator) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000191
Rafael Espindolaf2b04232011-05-06 14:56:22 +0000192 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattner4ad1efe2010-04-04 23:10:38 +0000193 DwarfStrSectionSym = TextSectionSym = 0;
Eric Christopher7a0103c2013-02-07 21:19:50 +0000194 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher9a9e73b2013-04-07 03:43:09 +0000195 DwarfAddrSectionSym = 0;
Eric Christopher64f824c2012-12-27 02:14:01 +0000196 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patelc3f5f782010-05-25 23:40:22 +0000197 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopher60777d82012-04-02 17:58:52 +0000198
Eric Christopher10cb7442012-08-23 07:10:46 +0000199 // Turn on accelerator tables and older gdb compatibility
Eric Christopher05bae3b2013-08-19 21:41:38 +0000200 // for Darwin by default, pubnames by default for non-Darwin,
201 // and handle split dwarf.
Eric Christopher00297ba2013-04-27 01:07:52 +0000202 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopherc1610fa2012-08-23 22:36:36 +0000203
Eric Christopher05bae3b2013-08-19 21:41:38 +0000204 if (DwarfAccelTables == Default)
205 HasDwarfAccelTables = IsDarwin;
206 else
Eric Christopher03be5e52013-08-26 20:58:35 +0000207 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000208
Eric Christopher4daaed12012-12-10 19:51:21 +0000209 if (SplitDwarf == Default)
210 HasSplitDwarf = false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000211 else
Eric Christopher05bae3b2013-08-19 21:41:38 +0000212 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000213
Eric Christopher63ae7c92013-08-26 23:24:35 +0000214 if (DwarfPubSections == Default)
215 HasDwarfPubSections = !IsDarwin;
Eric Christopher05bae3b2013-08-19 21:41:38 +0000216 else
Eric Christopher63ae7c92013-08-26 23:24:35 +0000217 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopherf04e4ef2013-08-19 21:07:38 +0000218
Manman Ren0e6783f2013-07-02 23:40:10 +0000219 DwarfVersion = getDwarfVersionFromModule(MMI->getModule());
220
Dan Gohman03c3dc72010-06-18 15:56:31 +0000221 {
222 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopherc4639d62012-11-19 22:42:15 +0000223 beginModule();
Torok Edwin9c421072010-04-07 10:44:46 +0000224 }
Bill Wendling0310d762009-05-15 09:23:25 +0000225}
226DwarfDebug::~DwarfDebug() {
Bill Wendling0310d762009-05-15 09:23:25 +0000227}
228
Eric Christopherb6dc8652012-11-27 22:43:45 +0000229// Switch to the specified MCSection and emit an assembler
230// temporary label to it if SymbolStem is specified.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000231static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christopherd8a87522011-11-07 09:18:38 +0000232 const char *SymbolStem = 0) {
233 Asm->OutStreamer.SwitchSection(Section);
234 if (!SymbolStem) return 0;
235
236 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
237 Asm->OutStreamer.EmitLabel(TmpSym);
238 return TmpSym;
239}
240
Eric Christopher2e5d8702012-12-20 21:58:36 +0000241MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher64f824c2012-12-27 02:14:01 +0000242 return Asm->GetTempSymbol(StringPref);
Nick Lewycky390c40d2011-10-27 06:44:11 +0000243}
244
Eric Christopher2e5d8702012-12-20 21:58:36 +0000245MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
246 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopherb6714222013-01-08 22:22:06 +0000247 StringPool.GetOrCreateValue(Str).getValue();
Chris Lattnerbc733f52010-03-13 02:17:42 +0000248 if (Entry.first) return Entry.first;
249
250 Entry.second = NextStringPoolNumber++;
Eric Christopher64f824c2012-12-27 02:14:01 +0000251 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerbc733f52010-03-13 02:17:42 +0000252}
253
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000254unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
255 std::pair<MCSymbol*, unsigned> &Entry =
Eric Christopherb6714222013-01-08 22:22:06 +0000256 StringPool.GetOrCreateValue(Str).getValue();
Eric Christopherdd8e9f32013-01-07 19:32:41 +0000257 if (Entry.first) return Entry.second;
258
259 Entry.second = NextStringPoolNumber++;
260 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
261 return Entry.second;
262}
263
David Blaikie321d83b2013-06-28 18:47:14 +0000264unsigned DwarfUnits::getAddrPoolIndex(const MCSymbol *Sym) {
David Blaikie8fed05e2013-07-01 23:55:52 +0000265 return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
266}
267
Ulrich Weigand18dbe502013-07-02 18:46:46 +0000268unsigned DwarfUnits::getAddrPoolIndex(const MCExpr *Sym) {
269 std::pair<DenseMap<const MCExpr *, unsigned>::iterator, bool> P =
David Blaikie1c736322013-06-28 18:47:19 +0000270 AddressPool.insert(std::make_pair(Sym, NextAddrPoolNumber));
David Blaikie1ca79902013-06-28 18:55:13 +0000271 if (P.second)
272 ++NextAddrPoolNumber;
David Blaikie1c736322013-06-28 18:47:19 +0000273 return P.first->second;
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000274}
275
Eric Christopherb6dc8652012-11-27 22:43:45 +0000276// Define a unique number for the abbreviation.
277//
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000278void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling0310d762009-05-15 09:23:25 +0000279 // Check the set for priors.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000280 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling0310d762009-05-15 09:23:25 +0000281
282 // If it's newly added.
283 if (InSet == &Abbrev) {
284 // Add to abbreviation list.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000285 Abbreviations->push_back(&Abbrev);
Bill Wendling0310d762009-05-15 09:23:25 +0000286
287 // Assign the vector position + 1 as its number.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000288 Abbrev.setNumber(Abbreviations->size());
Bill Wendling0310d762009-05-15 09:23:25 +0000289 } else {
290 // Assign existing abbreviation number.
291 Abbrev.setNumber(InSet->getNumber());
292 }
293}
294
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000295static bool isObjCClass(StringRef Name) {
296 return Name.startswith("+") || Name.startswith("-");
297}
298
299static bool hasObjCCategory(StringRef Name) {
300 if (!isObjCClass(Name)) return false;
301
Benjamin Kramer4321d4e2013-08-24 12:15:54 +0000302 return Name.find(") ") != StringRef::npos;
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000303}
304
305static void getObjCClassCategory(StringRef In, StringRef &Class,
306 StringRef &Category) {
307 if (!hasObjCCategory(In)) {
308 Class = In.slice(In.find('[') + 1, In.find(' '));
309 Category = "";
310 return;
311 }
312
313 Class = In.slice(In.find('[') + 1, In.find('('));
314 Category = In.slice(In.find('[') + 1, In.find(' '));
315 return;
316}
317
318static StringRef getObjCMethodName(StringRef In) {
319 return In.slice(In.find(' ') + 1, In.find(']'));
320}
321
Richard Mitton7c9659a2013-10-03 22:07:08 +0000322// Helper for sorting sections into a stable output order.
323static bool SectionSort(const MCSection *A, const MCSection *B) {
324 std::string LA = (A ? A->getLabelBeginName() : "");
325 std::string LB = (B ? B->getLabelBeginName() : "");
326 return LA < LB;
327}
328
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000329// Add the various names to the Dwarf accelerator table names.
Eric Christopher9e0b08d2013-09-20 23:22:52 +0000330// TODO: Determine whether or not we should add names for programs
331// that do not have a DW_AT_name or DW_AT_linkage_name field - this
332// is only slightly different than the lookup of non-standard ObjC names.
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000333static void addSubprogramNames(CompileUnit *TheCU, DISubprogram SP,
334 DIE* Die) {
335 if (!SP.isDefinition()) return;
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000336 TheCU->addAccelName(SP.getName(), Die);
337
338 // If the linkage name is different than the name, go ahead and output
339 // that as well into the name table.
340 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
341 TheCU->addAccelName(SP.getLinkageName(), Die);
342
343 // If this is an Objective-C selector name add it to the ObjC accelerator
344 // too.
345 if (isObjCClass(SP.getName())) {
346 StringRef Class, Category;
347 getObjCClassCategory(SP.getName(), Class, Category);
348 TheCU->addAccelObjC(Class, Die);
349 if (Category != "")
350 TheCU->addAccelObjC(Category, Die);
351 // Also add the base method name to the name table.
352 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
353 }
354}
355
Manman Ren02d29672013-09-09 19:05:21 +0000356/// isSubprogramContext - Return true if Context is either a subprogram
357/// or another context nested inside a subprogram.
358bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
359 if (!Context)
360 return false;
361 DIDescriptor D(Context);
362 if (D.isSubprogram())
363 return true;
364 if (D.isType())
Manman Ren2c9905a2013-09-09 19:47:11 +0000365 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren02d29672013-09-09 19:05:21 +0000366 return false;
367}
368
Eric Christopherb6dc8652012-11-27 22:43:45 +0000369// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
370// and DW_AT_high_pc attributes. If there are global variables in this
371// scope then create and insert DIEs for these variables.
Devang Pateld3024342011-08-15 22:24:32 +0000372DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU,
373 const MDNode *SPNode) {
Eric Christopher3f045002013-10-04 17:08:38 +0000374 DIE *SPDie = SPCU->getDIE(SPNode);
Devang Patel8aa61472010-07-07 22:20:57 +0000375
Chris Lattnerd38fee82010-04-05 00:13:49 +0000376 assert(SPDie && "Unable to find subprogram DIE!");
377 DISubprogram SP(SPNode);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000378
Bill Wendling168c1902012-11-07 05:19:04 +0000379 // If we're updating an abstract DIE, then we will be adding the children and
380 // object pointer later on. But what we don't want to do is process the
381 // concrete DIE twice.
Eric Christophere7099f62013-10-04 19:40:33 +0000382 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SPNode)) {
Bill Wendling168c1902012-11-07 05:19:04 +0000383 // Pick up abstract subprogram DIE.
Devang Patel8aa61472010-07-07 22:20:57 +0000384 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Devang Patel3cbee302011-04-12 22:53:02 +0000385 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin,
Eric Christophere7099f62013-10-04 19:40:33 +0000386 dwarf::DW_FORM_ref4, AbsSPDIE);
Devang Patel8aa61472010-07-07 22:20:57 +0000387 SPCU->addDie(SPDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000388 } else {
389 DISubprogram SPDecl = SP.getFunctionDeclaration();
390 if (!SPDecl.isSubprogram()) {
391 // There is not any need to generate specification DIE for a function
392 // defined at compile unit level. If a function is defined inside another
393 // function then gdb prefers the definition at top level and but does not
394 // expect specification DIE in parent function. So avoid creating
395 // specification DIE for a function defined inside a function.
Manman Renb8e48a62013-10-10 18:40:01 +0000396 DIScope SPContext = resolve(SP.getContext());
397 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
398 !SPContext.isFile() &&
399 !isSubprogramContext(SPContext)) {
Bill Wendlinga4c76932012-11-07 04:42:18 +0000400 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
401
402 // Add arguments.
403 DICompositeType SPTy = SP.getType();
404 DIArray Args = SPTy.getTypeArray();
Eric Christopher31667622013-08-08 01:41:00 +0000405 uint16_t SPTag = SPTy.getTag();
Bill Wendlinga4c76932012-11-07 04:42:18 +0000406 if (SPTag == dwarf::DW_TAG_subroutine_type)
407 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
408 DIE *Arg = new DIE(dwarf::DW_TAG_formal_parameter);
409 DIType ATy = DIType(Args.getElement(i));
410 SPCU->addType(Arg, ATy);
411 if (ATy.isArtificial())
412 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
413 if (ATy.isObjectPointer())
414 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer,
415 dwarf::DW_FORM_ref4, Arg);
416 SPDie->addChild(Arg);
417 }
418 DIE *SPDeclDie = SPDie;
419 SPDie = new DIE(dwarf::DW_TAG_subprogram);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000420 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification,
421 dwarf::DW_FORM_ref4, SPDeclDie);
Bill Wendlinga4c76932012-11-07 04:42:18 +0000422 SPCU->addDie(SPDie);
423 }
424 }
Devang Patel8aa61472010-07-07 22:20:57 +0000425 }
426
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000427 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc,
428 Asm->GetTempSymbol("func_begin",
429 Asm->getFunctionNumber()));
430 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc,
431 Asm->GetTempSymbol("func_end",
432 Asm->getFunctionNumber()));
Chris Lattnerd38fee82010-04-05 00:13:49 +0000433 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
434 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patel3cbee302011-04-12 22:53:02 +0000435 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patelb4645642010-02-06 01:02:37 +0000436
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000437 // Add name to the name table, we do this here because we're guaranteed
438 // to have concrete versions of our DW_TAG_subprogram nodes.
439 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000440
Chris Lattnerd38fee82010-04-05 00:13:49 +0000441 return SPDie;
Devang Patel53bb5c92009-11-10 23:06:00 +0000442}
443
Manman Ren995de6c2013-09-11 19:40:28 +0000444/// Check whether we should create a DIE for the given Scope, return true
445/// if we don't create a DIE (the corresponding DIE is null).
Manman Renfd2210a2013-09-10 18:40:41 +0000446bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
447 if (Scope->isAbstractScope())
448 return false;
449
Manman Ren995de6c2013-09-11 19:40:28 +0000450 // We don't create a DIE if there is no Range.
Manman Renfd2210a2013-09-10 18:40:41 +0000451 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
452 if (Ranges.empty())
453 return true;
454
455 if (Ranges.size() > 1)
456 return false;
457
Manman Ren995de6c2013-09-11 19:40:28 +0000458 // We don't create a DIE if we have a single Range and the end label
459 // is null.
Manman Renfd2210a2013-09-10 18:40:41 +0000460 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
461 MCSymbol *End = getLabelAfterInsn(RI->second);
462 return !End;
463}
464
Eric Christopherb6dc8652012-11-27 22:43:45 +0000465// Construct new DW_TAG_lexical_block for this scope and attach
466// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000467DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld3024342011-08-15 22:24:32 +0000468 LexicalScope *Scope) {
Manman Renfd2210a2013-09-10 18:40:41 +0000469 if (isLexicalScopeDIENull(Scope))
470 return 0;
471
Devang Pateleac9c072010-04-27 19:46:33 +0000472 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
473 if (Scope->isAbstractScope())
474 return ScopeDIE;
475
Craig Topper9f43ac62013-07-03 04:24:43 +0000476 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
Eric Christopherf53d6432013-07-03 01:57:28 +0000477 // If we have multiple ranges, emit them into the range section.
Devang Pateleac9c072010-04-27 19:46:33 +0000478 if (Ranges.size() > 1) {
479 // .debug_range section has not been laid out yet. Emit offset in
Jim Grosbach1e20b962010-07-21 21:21:52 +0000480 // .debug_range as a uint, size 4, for now. emitDIE will handle
Devang Pateleac9c072010-04-27 19:46:33 +0000481 // DW_AT_ranges appropriately.
Devang Patel3cbee302011-04-12 22:53:02 +0000482 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000483 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000484 * Asm->getDataLayout().getPointerSize());
Craig Topper2d785db2013-07-03 04:17:25 +0000485 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +0000486 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelc3f5f782010-05-25 23:40:22 +0000487 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
488 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Pateleac9c072010-04-27 19:46:33 +0000489 }
Eric Christopherda965de2013-07-03 01:22:29 +0000490
491 // Terminate the range list.
Devang Pateleac9c072010-04-27 19:46:33 +0000492 DebugRangeSymbols.push_back(NULL);
493 DebugRangeSymbols.push_back(NULL);
494 return ScopeDIE;
495 }
496
Eric Christopherf53d6432013-07-03 01:57:28 +0000497 // Construct the address range for this DIE.
Craig Topper2d785db2013-07-03 04:17:25 +0000498 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000499 MCSymbol *Start = getLabelBeforeInsn(RI->first);
500 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Renfd2210a2013-09-10 18:40:41 +0000501 assert(End && "End label should not be null!");
Devang Patel53bb5c92009-11-10 23:06:00 +0000502
Chris Lattnerb7db7332010-03-09 01:58:53 +0000503 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
504 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbach1e20b962010-07-21 21:21:52 +0000505
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000506 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
507 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patel53bb5c92009-11-10 23:06:00 +0000508
509 return ScopeDIE;
510}
511
Eric Christopherb6dc8652012-11-27 22:43:45 +0000512// This scope represents inlined body of a function. Construct DIE to
513// represent this concrete inlined copy of the function.
Devang Pateld3024342011-08-15 22:24:32 +0000514DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
515 LexicalScope *Scope) {
Craig Topper9f43ac62013-07-03 04:24:43 +0000516 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
Nick Lewycky746cb672011-10-26 22:55:33 +0000517 assert(Ranges.empty() == false &&
518 "LexicalScope does not have instruction markers!");
Devang Pateleac9c072010-04-27 19:46:33 +0000519
Devang Patel26a92002011-07-27 00:34:13 +0000520 if (!Scope->getScopeNode())
521 return NULL;
522 DIScope DS(Scope->getScopeNode());
523 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christopher3f045002013-10-04 17:08:38 +0000524 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patel26a92002011-07-27 00:34:13 +0000525 if (!OriginDIE) {
Bill Wendlinge0aae5b2012-10-30 17:51:02 +0000526 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patel26a92002011-07-27 00:34:13 +0000527 return NULL;
528 }
529
Devang Pateld96efb82011-05-05 17:54:26 +0000530 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Devang Patel3cbee302011-04-12 22:53:02 +0000531 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin,
Manman Ren742671b2013-05-29 17:16:59 +0000532 dwarf::DW_FORM_ref4, OriginDIE);
Devang Patel53bb5c92009-11-10 23:06:00 +0000533
Devang Patel26a92002011-07-27 00:34:13 +0000534 if (Ranges.size() > 1) {
535 // .debug_range section has not been laid out yet. Emit offset in
536 // .debug_range as a uint, size 4, for now. emitDIE will handle
537 // DW_AT_ranges appropriately.
538 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_ranges, dwarf::DW_FORM_data4,
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000539 DebugRangeSymbols.size()
Chandler Carruth426c2bf2012-11-01 09:14:31 +0000540 * Asm->getDataLayout().getPointerSize());
Craig Topper2d785db2013-07-03 04:17:25 +0000541 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Devang Patel26a92002011-07-27 00:34:13 +0000542 RE = Ranges.end(); RI != RE; ++RI) {
543 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
544 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
545 }
546 DebugRangeSymbols.push_back(NULL);
547 DebugRangeSymbols.push_back(NULL);
548 } else {
Craig Topper2d785db2013-07-03 04:17:25 +0000549 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
Eric Christophere7177132013-07-03 02:23:53 +0000550 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
551 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
552
553 if (StartLabel == 0 || EndLabel == 0)
554 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
555
556 assert(StartLabel->isDefined() &&
557 "Invalid starting label for an inlined scope!");
558 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
559
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000560 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
561 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patel26a92002011-07-27 00:34:13 +0000562 }
Devang Patel53bb5c92009-11-10 23:06:00 +0000563
564 InlinedSubprogramDIEs.insert(OriginDIE);
565
Eric Christophere7177132013-07-03 02:23:53 +0000566 // Add the call site information to the DIE.
Devang Patel53bb5c92009-11-10 23:06:00 +0000567 DILocation DL(Scope->getInlinedAt());
Eric Christopher08212002012-03-26 21:38:38 +0000568 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, 0,
Manman Ren3de61b42013-03-07 01:42:00 +0000569 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
570 TheCU->getUniqueID()));
Devang Patel3cbee302011-04-12 22:53:02 +0000571 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, 0, DL.getLineNumber());
Devang Patel53bb5c92009-11-10 23:06:00 +0000572
Eric Christopher309bedd2011-12-04 06:02:38 +0000573 // Add name to the name table, we do this here because we're guaranteed
574 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
575 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000576
Devang Patel53bb5c92009-11-10 23:06:00 +0000577 return ScopeDIE;
578}
579
Manman Renfd2210a2013-09-10 18:40:41 +0000580DIE *DwarfDebug::createScopeChildrenDIE(CompileUnit *TheCU, LexicalScope *Scope,
581 SmallVectorImpl<DIE*> &Children) {
582 DIE *ObjectPointer = NULL;
Devang Patel0478c152011-03-01 22:58:55 +0000583
584 // Collect arguments for current function.
Devang Patelbf47fdb2011-08-10 20:55:27 +0000585 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +0000586 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
587 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000588 if (DIE *Arg =
Eric Christophere5212782012-09-12 23:36:19 +0000589 TheCU->constructVariableDIE(ArgDV, Scope->isAbstractScope())) {
Devang Patel0478c152011-03-01 22:58:55 +0000590 Children.push_back(Arg);
Eric Christophere5212782012-09-12 23:36:19 +0000591 if (ArgDV->isObjectPointer()) ObjectPointer = Arg;
592 }
Devang Patel0478c152011-03-01 22:58:55 +0000593
Eric Christopher1aeb7ac2011-10-03 15:49:16 +0000594 // Collect lexical scope children first.
Craig Topper9f43ac62013-07-03 04:24:43 +0000595 const SmallVectorImpl<DbgVariable *> &Variables =ScopeVariables.lookup(Scope);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000596 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000597 if (DIE *Variable =
Eric Christopher7b451cf2012-09-21 22:18:52 +0000598 TheCU->constructVariableDIE(Variables[i], Scope->isAbstractScope())) {
Devang Patel5bc9fec2011-02-19 01:31:27 +0000599 Children.push_back(Variable);
Eric Christopher7b451cf2012-09-21 22:18:52 +0000600 if (Variables[i]->isObjectPointer()) ObjectPointer = Variable;
601 }
Craig Topper9f43ac62013-07-03 04:24:43 +0000602 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5bc9fec2011-02-19 01:31:27 +0000603 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Pateld0b5a5e2011-08-15 22:04:40 +0000604 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5bc9fec2011-02-19 01:31:27 +0000605 Children.push_back(Nested);
Manman Renfd2210a2013-09-10 18:40:41 +0000606 return ObjectPointer;
607}
608
609// Construct a DIE for this scope.
610DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
611 if (!Scope || !Scope->getScopeNode())
612 return NULL;
613
614 DIScope DS(Scope->getScopeNode());
615
616 SmallVector<DIE *, 8> Children;
617 DIE *ObjectPointer = NULL;
618 bool ChildrenCreated = false;
619
Manman Ren995de6c2013-09-11 19:40:28 +0000620 // We try to create the scope DIE first, then the children DIEs. This will
621 // avoid creating un-used children then removing them later when we find out
622 // the scope DIE is null.
Devang Patel3c91b052010-03-08 20:52:55 +0000623 DIE *ScopeDIE = NULL;
624 if (Scope->getInlinedAt())
Devang Pateld3024342011-08-15 22:24:32 +0000625 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3c91b052010-03-08 20:52:55 +0000626 else if (DS.isSubprogram()) {
Devang Patel0dd45582010-06-28 20:53:04 +0000627 ProcessedSPNodes.insert(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000628 if (Scope->isAbstractScope()) {
Eric Christopher3f045002013-10-04 17:08:38 +0000629 ScopeDIE = TheCU->getDIE(DS);
Devang Patel8aa61472010-07-07 22:20:57 +0000630 // Note down abstract DIE.
631 if (ScopeDIE)
632 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
633 }
Devang Patel3c91b052010-03-08 20:52:55 +0000634 else
Devang Pateld3024342011-08-15 22:24:32 +0000635 ScopeDIE = updateSubprogramScopeDIE(TheCU, DS);
Devang Patel3c91b052010-03-08 20:52:55 +0000636 }
Devang Patel5bc9fec2011-02-19 01:31:27 +0000637 else {
Manman Ren995de6c2013-09-11 19:40:28 +0000638 // Early exit when we know the scope DIE is going to be null.
Manman Renfd2210a2013-09-10 18:40:41 +0000639 if (isLexicalScopeDIENull(Scope))
640 return NULL;
Manman Ren995de6c2013-09-11 19:40:28 +0000641
642 // We create children here when we know the scope DIE is not going to be
643 // null and the children will be added to the scope DIE.
Manman Renfd2210a2013-09-10 18:40:41 +0000644 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
645 ChildrenCreated = true;
Manman Ren995de6c2013-09-11 19:40:28 +0000646
Devang Patel5bc9fec2011-02-19 01:31:27 +0000647 // There is no need to emit empty lexical block DIE.
David Blaikied2e0f7e2013-05-06 23:33:07 +0000648 std::pair<ImportedEntityMap::const_iterator,
649 ImportedEntityMap::const_iterator> Range = std::equal_range(
650 ScopesWithImportedEntities.begin(), ScopesWithImportedEntities.end(),
Timur Iskhodzhanov664fbee2013-05-07 07:47:47 +0000651 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode*)0),
Benjamin Kramer0b6962f2013-08-24 12:54:27 +0000652 less_first());
David Blaikied2e0f7e2013-05-06 23:33:07 +0000653 if (Children.empty() && Range.first == Range.second)
Devang Patel5bc9fec2011-02-19 01:31:27 +0000654 return NULL;
Devang Pateld3024342011-08-15 22:24:32 +0000655 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Renfd2210a2013-09-10 18:40:41 +0000656 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopherfdfc81f2013-06-24 23:20:02 +0000657 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
658 ++i)
David Blaikied1221e32013-05-08 06:01:38 +0000659 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5bc9fec2011-02-19 01:31:27 +0000660 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000661
Benjamin Kramer6bee6312013-08-24 11:55:49 +0000662 if (!ScopeDIE) {
Manman Renfd2210a2013-09-10 18:40:41 +0000663 assert(Children.empty() &&
664 "We create children only when the scope DIE is not null.");
Benjamin Kramer6bee6312013-08-24 11:55:49 +0000665 return NULL;
666 }
Manman Renfd2210a2013-09-10 18:40:41 +0000667 if (!ChildrenCreated)
Manman Ren995de6c2013-09-11 19:40:28 +0000668 // We create children when the scope DIE is not null.
Manman Renfd2210a2013-09-10 18:40:41 +0000669 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000670
Devang Patel5bc9fec2011-02-19 01:31:27 +0000671 // Add children
Craig Topper2d785db2013-07-03 04:17:25 +0000672 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Devang Patel5bc9fec2011-02-19 01:31:27 +0000673 E = Children.end(); I != E; ++I)
674 ScopeDIE->addChild(*I);
Devang Patel193f7202009-11-24 01:14:22 +0000675
Eric Christophere5212782012-09-12 23:36:19 +0000676 if (DS.isSubprogram() && ObjectPointer != NULL)
677 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer,
678 dwarf::DW_FORM_ref4, ObjectPointer);
679
Jim Grosbach1e20b962010-07-21 21:21:52 +0000680 if (DS.isSubprogram())
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000681 TheCU->addPubTypes(DISubprogram(DS));
Jim Grosbach1e20b962010-07-21 21:21:52 +0000682
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000683 return ScopeDIE;
Devang Patel53bb5c92009-11-10 23:06:00 +0000684}
685
Eric Christopherb6dc8652012-11-27 22:43:45 +0000686// Look up the source id with the given directory and source file names.
687// If none currently exists, create a new id and insert it in the
688// SourceIds map. This can update DirectoryNames and SourceFileNames maps
689// as well.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000690unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName,
Manman Ren3de61b42013-03-07 01:42:00 +0000691 StringRef DirName, unsigned CUID) {
692 // If we use .loc in assembly, we can't separate .file entries according to
693 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindola5e195a42013-10-05 16:42:21 +0000694
695 // FIXME: add a better feature test than hasRawTextSupport. Even better,
696 // extend .file to support this.
697 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren3de61b42013-03-07 01:42:00 +0000698 CUID = 0;
699
Devang Patel1905a182010-09-16 20:57:49 +0000700 // If FE did not provide a file name, then assume stdin.
701 if (FileName.empty())
Manman Ren3de61b42013-03-07 01:42:00 +0000702 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patel23670e52011-03-24 20:30:50 +0000703
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000704 // TODO: this might not belong here. See if we can factor this better.
705 if (DirName == CompilationDir)
706 DirName = "";
707
Manman Ren3de61b42013-03-07 01:42:00 +0000708 // FileIDCUMap stores the current ID for the given compile unit.
709 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel1905a182010-09-16 20:57:49 +0000710
Manman Ren3de61b42013-03-07 01:42:00 +0000711 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer74612c22012-03-11 14:56:26 +0000712 SmallString<128> NamePair;
Manman Ren5044a932013-04-06 01:02:38 +0000713 NamePair += utostr(CUID);
Manman Ren3de61b42013-03-07 01:42:00 +0000714 NamePair += '\0';
Benjamin Kramer74612c22012-03-11 14:56:26 +0000715 NamePair += DirName;
716 NamePair += '\0'; // Zero bytes are not allowed in paths.
717 NamePair += FileName;
718
719 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
720 if (Ent.getValue() != SrcId)
721 return Ent.getValue();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000722
Manman Ren3de61b42013-03-07 01:42:00 +0000723 FileIDCUMap[CUID] = SrcId;
Rafael Espindola5c055632010-11-18 02:04:25 +0000724 // Print out a .file directive to specify files for .loc directives.
Manman Ren3de61b42013-03-07 01:42:00 +0000725 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000726
727 return SrcId;
728}
729
Eric Christopher72c16552012-12-20 21:58:40 +0000730// Create new CompileUnit for the given metadata node with tag
731// DW_TAG_compile_unit.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000732CompileUnit *DwarfDebug::constructCompileUnit(const MDNode *N) {
Devang Patele4b27562009-08-28 23:24:31 +0000733 DICompileUnit DIUnit(N);
Devang Patel65dbc902009-11-25 17:36:49 +0000734 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000735 CompilationDir = DIUnit.getDirectory();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000736
737 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher7b878952013-08-26 23:50:38 +0000738 CompileUnit *NewCU =
739 new CompileUnit(GlobalCUIndexCount++, Die, N, Asm, this, &InfoHolder);
Manman Ren3de61b42013-03-07 01:42:00 +0000740
741 FileIDCUMap[NewCU->getUniqueID()] = 0;
742 // Call this to emit a .file directive if it wasn't emitted for the source
743 // file this CU comes from yet.
744 getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
745
Nick Lewycky390c40d2011-10-27 06:44:11 +0000746 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patel3cbee302011-04-12 22:53:02 +0000747 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
748 DIUnit.getLanguage());
Nick Lewycky390c40d2011-10-27 06:44:11 +0000749 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christophere0828ec2013-04-09 19:23:15 +0000750
Eric Christopher6635cad2012-08-01 18:19:01 +0000751 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christophere0828ec2013-04-09 19:23:15 +0000752 // into an entity. We're using 0 (or a NULL label) for this. For
753 // split dwarf it's in the skeleton CU so omit it here.
754 if (!useSplitDwarf())
755 NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
Manman Ren43213cf2013-02-05 21:52:47 +0000756
757 // Define start line table label for each Compile Unit.
758 MCSymbol *LineTableStartSym = Asm->GetTempSymbol("line_table_start",
759 NewCU->getUniqueID());
760 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
761 NewCU->getUniqueID());
762
Manman Ren01cb18e2013-05-21 00:57:22 +0000763 // Use a single line table if we are using .loc and generating assembly.
764 bool UseTheFirstCU =
Rafael Espindola5e195a42013-10-05 16:42:21 +0000765 (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport()) ||
766 (NewCU->getUniqueID() == 0);
Manman Ren01cb18e2013-05-21 00:57:22 +0000767
Eric Christophere0828ec2013-04-09 19:23:15 +0000768 if (!useSplitDwarf()) {
Eric Christopher0b15e982013-09-27 22:50:48 +0000769 // DW_AT_stmt_list is a offset of line number information for this
770 // compile unit in debug_line section. For split dwarf this is
771 // left in the skeleton CU and so not included.
772 // The line table entries are not always emitted in assembly, so it
773 // is not okay to use line_table_start here.
Eric Christophere0828ec2013-04-09 19:23:15 +0000774 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Carlo Kok46f0b6f2013-08-13 17:46:57 +0000775 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset,
Eric Christopher0b15e982013-09-27 22:50:48 +0000776 UseTheFirstCU ? Asm->GetTempSymbol("section_line")
777 : LineTableStartSym);
Manman Ren01cb18e2013-05-21 00:57:22 +0000778 else if (UseTheFirstCU)
Eric Christophere0828ec2013-04-09 19:23:15 +0000779 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4, 0);
780 else
781 NewCU->addDelta(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_data4,
782 LineTableStartSym, DwarfLineSectionSym);
Eric Christopher0b15e982013-09-27 22:50:48 +0000783
784 // If we're using split dwarf the compilation dir is going to be in the
785 // skeleton CU and so we don't need to duplicate it here.
786 if (!CompilationDir.empty())
787 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
788
Eric Christopher08bd9232013-10-01 00:43:31 +0000789 // Flags to let the linker know we have emitted new style pubnames. Only
Eric Christopher0b15e982013-09-27 22:50:48 +0000790 // emit it here if we don't have a skeleton CU for split dwarf.
Eric Christophera6d84152013-09-30 23:14:16 +0000791 if (GenerateGnuPubSections) {
792 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
793 NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubnames,
794 dwarf::DW_FORM_sec_offset,
795 Asm->GetTempSymbol("gnu_pubnames",
796 NewCU->getUniqueID()));
797 else
798 NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubnames, dwarf::DW_FORM_data4,
799 Asm->GetTempSymbol("gnu_pubnames",
800 NewCU->getUniqueID()),
801 DwarfGnuPubNamesSectionSym);
802
803 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
804 NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubtypes,
805 dwarf::DW_FORM_sec_offset,
806 Asm->GetTempSymbol("gnu_pubtypes",
807 NewCU->getUniqueID()));
808 else
809 NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_data4,
Eric Christopher7c9fc902013-10-03 17:41:16 +0000810 Asm->GetTempSymbol("gnu_pubtypes",
Eric Christophera6d84152013-09-30 23:14:16 +0000811 NewCU->getUniqueID()),
812 DwarfGnuPubTypesSectionSym);
813 }
Eric Christophere0828ec2013-04-09 19:23:15 +0000814 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000815
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000816 if (DIUnit.isOptimized())
Eric Christopher873cf0a2012-08-24 01:14:27 +0000817 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000818
Devang Patel65dbc902009-11-25 17:36:49 +0000819 StringRef Flags = DIUnit.getFlags();
820 if (!Flags.empty())
Nick Lewycky390c40d2011-10-27 06:44:11 +0000821 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000822
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000823 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patel3cbee302011-04-12 22:53:02 +0000824 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000825 dwarf::DW_FORM_data1, RVer);
826
Devang Patel163a9f72010-05-10 22:49:55 +0000827 if (!FirstCU)
828 FirstCU = NewCU;
Eric Christopher208cc6f2013-02-05 07:31:55 +0000829
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000830 InfoHolder.addUnit(NewCU);
831
Devang Patel163a9f72010-05-10 22:49:55 +0000832 CUMap.insert(std::make_pair(N, NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000833 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000834}
835
Eric Christopherb6dc8652012-11-27 22:43:45 +0000836// Construct subprogram DIE.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000837void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU,
Devang Patel3655a212011-08-15 23:36:40 +0000838 const MDNode *N) {
Rafael Espindolab0527282011-11-04 19:00:29 +0000839 CompileUnit *&CURef = SPMap[N];
840 if (CURef)
841 return;
842 CURef = TheCU;
843
Devang Patele4b27562009-08-28 23:24:31 +0000844 DISubprogram SP(N);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000845 if (!SP.isDefinition())
846 // This is a method declaration which will be handled while constructing
847 // class type.
Devang Patel13e16b62009-06-26 01:49:18 +0000848 return;
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000849
Devang Pateldbc64af2011-08-15 17:24:54 +0000850 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings639336e2010-04-06 21:38:29 +0000851
Eric Christopher28b020d2013-09-09 20:03:20 +0000852 // Expose as a global name.
853 TheCU->addGlobalName(SP.getName(), SubprogramDie);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000854}
855
David Blaikie20d9e412013-05-07 21:35:53 +0000856void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikiec462db62013-04-22 06:12:31 +0000857 const MDNode *N) {
David Blaikie20d9e412013-05-07 21:35:53 +0000858 DIImportedEntity Module(N);
David Blaikiec462db62013-04-22 06:12:31 +0000859 if (!Module.Verify())
860 return;
David Blaikied2e0f7e2013-05-06 23:33:07 +0000861 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikied1221e32013-05-08 06:01:38 +0000862 constructImportedEntityDIE(TheCU, Module, D);
David Blaikied2e0f7e2013-05-06 23:33:07 +0000863}
864
David Blaikied1221e32013-05-08 06:01:38 +0000865void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
David Blaikied2e0f7e2013-05-06 23:33:07 +0000866 DIE *Context) {
David Blaikie20d9e412013-05-07 21:35:53 +0000867 DIImportedEntity Module(N);
David Blaikied2e0f7e2013-05-06 23:33:07 +0000868 if (!Module.Verify())
869 return;
David Blaikied1221e32013-05-08 06:01:38 +0000870 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikied2e0f7e2013-05-06 23:33:07 +0000871}
872
David Blaikied1221e32013-05-08 06:01:38 +0000873void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikie20d9e412013-05-07 21:35:53 +0000874 const DIImportedEntity &Module,
David Blaikied2e0f7e2013-05-06 23:33:07 +0000875 DIE *Context) {
876 assert(Module.Verify() &&
877 "Use one of the MDNode * overloads to handle invalid metadata");
878 assert(Context && "Should always have a context for an imported_module");
David Blaikie20d9e412013-05-07 21:35:53 +0000879 DIE *IMDie = new DIE(Module.getTag());
David Blaikiec462db62013-04-22 06:12:31 +0000880 TheCU->insertDIE(Module, IMDie);
David Blaikie20d9e412013-05-07 21:35:53 +0000881 DIE *EntityDie;
882 DIDescriptor Entity = Module.getEntity();
883 if (Entity.isNameSpace())
884 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
885 else if (Entity.isSubprogram())
886 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikieaa76a932013-05-08 06:01:41 +0000887 else if (Entity.isType())
888 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie20d9e412013-05-07 21:35:53 +0000889 else
David Blaikieaa76a932013-05-08 06:01:41 +0000890 EntityDie = TheCU->getDIE(Entity);
David Blaikiec462db62013-04-22 06:12:31 +0000891 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
892 Module.getContext().getDirectory(),
893 TheCU->getUniqueID());
894 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, 0, FileID);
895 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, 0, Module.getLineNumber());
Eric Christopherfdfc81f2013-06-24 23:20:02 +0000896 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, dwarf::DW_FORM_ref4,
897 EntityDie);
David Blaikie7b72cc72013-05-20 22:50:35 +0000898 StringRef Name = Module.getName();
899 if (!Name.empty())
900 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikied2e0f7e2013-05-06 23:33:07 +0000901 Context->addChild(IMDie);
David Blaikiec462db62013-04-22 06:12:31 +0000902}
903
Eric Christopherb6dc8652012-11-27 22:43:45 +0000904// Emit all Dwarf sections that should come prior to the content. Create
905// global DIEs and emit initial debug info sections. This is invoked by
906// the target AsmPrinter.
Eric Christopherc4639d62012-11-19 22:42:15 +0000907void DwarfDebug::beginModule() {
Devang Pateleac9c072010-04-27 19:46:33 +0000908 if (DisableDebugInfoPrinting)
909 return;
910
Eric Christopherc4639d62012-11-19 22:42:15 +0000911 const Module *M = MMI->getModule();
912
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000913 // If module has named metadata anchors then use them, otherwise scan the
914 // module using debug info finder to collect debug info.
Devang Patel30692ab2011-05-03 16:45:22 +0000915 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiec0ec8a42013-03-11 23:39:23 +0000916 if (!CU_Nodes)
Devang Patel02e603f2011-08-15 23:47:24 +0000917 return;
Manman Renbc660712013-09-05 18:48:31 +0000918 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patel30692ab2011-05-03 16:45:22 +0000919
David Blaikiec0ec8a42013-03-11 23:39:23 +0000920 // Emit initial sections so we can reference labels later.
921 emitSectionLabels();
922
923 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
924 DICompileUnit CUNode(CU_Nodes->getOperand(i));
925 CompileUnit *CU = constructCompileUnit(CUNode);
David Blaikie20d9e412013-05-07 21:35:53 +0000926 DIArray ImportedEntities = CUNode.getImportedEntities();
927 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikied2e0f7e2013-05-06 23:33:07 +0000928 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie20d9e412013-05-07 21:35:53 +0000929 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
930 ImportedEntities.getElement(i)));
David Blaikied2e0f7e2013-05-06 23:33:07 +0000931 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramer0b6962f2013-08-24 12:54:27 +0000932 ScopesWithImportedEntities.end(), less_first());
David Blaikiec0ec8a42013-03-11 23:39:23 +0000933 DIArray GVs = CUNode.getGlobalVariables();
934 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
935 CU->createGlobalVariableDIE(GVs.getElement(i));
936 DIArray SPs = CUNode.getSubprograms();
937 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
938 constructSubprogramDIE(CU, SPs.getElement(i));
939 DIArray EnumTypes = CUNode.getEnumTypes();
940 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
941 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
942 DIArray RetainedTypes = CUNode.getRetainedTypes();
943 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
944 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikiec462db62013-04-22 06:12:31 +0000945 // Emit imported_modules last so that the relevant context is already
946 // available.
David Blaikie20d9e412013-05-07 21:35:53 +0000947 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
948 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiec0ec8a42013-03-11 23:39:23 +0000949 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000950
Chris Lattnerd850ac72010-04-05 02:19:28 +0000951 // Tell MMI that we have debug info.
952 MMI->setDebugInfoAvailability(true);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000953
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000954 // Prime section data.
Richard Mitton5cc319a2013-09-19 23:21:01 +0000955 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000956}
957
Eric Christopher4117bec2012-11-22 00:59:49 +0000958// Attach DW_AT_inline attribute with inlined subprogram DIEs.
959void DwarfDebug::computeInlinedDIEs() {
Devang Patel53bb5c92009-11-10 23:06:00 +0000960 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
961 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000962 AE = InlinedSubprogramDIEs.end(); AI != AE; ++AI) {
Devang Patel53bb5c92009-11-10 23:06:00 +0000963 DIE *ISP = *AI;
Devang Patel3cbee302011-04-12 22:53:02 +0000964 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
Devang Patel53bb5c92009-11-10 23:06:00 +0000965 }
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000966 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherbdab8002012-11-27 00:13:51 +0000967 AE = AbstractSPDies.end(); AI != AE; ++AI) {
Rafael Espindolad1ac3a42011-11-12 01:57:54 +0000968 DIE *ISP = AI->second;
969 if (InlinedSubprogramDIEs.count(ISP))
970 continue;
971 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, 0, dwarf::DW_INL_inlined);
972 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000973}
974
975// Collect info for variables that were optimized out.
976void DwarfDebug::collectDeadVariables() {
977 const Module *M = MMI->getModule();
978 DenseMap<const MDNode *, LexicalScope *> DeadFnScopeMap;
979
980 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
981 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
982 DICompileUnit TheCU(CU_Nodes->getOperand(i));
983 DIArray Subprograms = TheCU.getSubprograms();
984 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopherbdab8002012-11-27 00:13:51 +0000985 DISubprogram SP(Subprograms.getElement(i));
986 if (ProcessedSPNodes.count(SP) != 0) continue;
Manman Rend03d2b22013-07-08 18:33:29 +0000987 if (!SP.isSubprogram()) continue;
Eric Christopherbdab8002012-11-27 00:13:51 +0000988 if (!SP.isDefinition()) continue;
989 DIArray Variables = SP.getVariables();
990 if (Variables.getNumElements() == 0) continue;
Eric Christopher4117bec2012-11-22 00:59:49 +0000991
Eric Christopherbdab8002012-11-27 00:13:51 +0000992 LexicalScope *Scope =
993 new LexicalScope(NULL, DIDescriptor(SP), NULL, false);
994 DeadFnScopeMap[SP] = Scope;
Eric Christopher4117bec2012-11-22 00:59:49 +0000995
Eric Christopherbdab8002012-11-27 00:13:51 +0000996 // Construct subprogram DIE and add variables DIEs.
997 CompileUnit *SPCU = CUMap.lookup(TheCU);
998 assert(SPCU && "Unable to find Compile Unit!");
999 constructSubprogramDIE(SPCU, SP);
Eric Christopher3f045002013-10-04 17:08:38 +00001000 DIE *ScopeDIE = SPCU->getDIE(SP);
Eric Christopherbdab8002012-11-27 00:13:51 +00001001 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
1002 DIVariable DV(Variables.getElement(vi));
Manman Rend03d2b22013-07-08 18:33:29 +00001003 if (!DV.isVariable()) continue;
Manman Renc664d762013-10-05 01:43:03 +00001004 DbgVariable NewVar(DV, NULL, this);
Eric Christopherbdab8002012-11-27 00:13:51 +00001005 if (DIE *VariableDIE =
Chandler Carruth05497cc2013-07-27 11:09:58 +00001006 SPCU->constructVariableDIE(&NewVar, Scope->isAbstractScope()))
Eric Christopherbdab8002012-11-27 00:13:51 +00001007 ScopeDIE->addChild(VariableDIE);
1008 }
Eric Christopher4117bec2012-11-22 00:59:49 +00001009 }
1010 }
1011 }
1012 DeleteContainerSeconds(DeadFnScopeMap);
1013}
1014
Eric Christopher0d27ca12013-08-08 23:45:55 +00001015// Type Signature [7.27] and ODR Hash code.
Eric Christopher3dee5752013-07-26 17:02:41 +00001016
1017/// \brief Grabs the string in whichever attribute is passed in and returns
Eric Christopherb7669132013-08-07 01:18:33 +00001018/// a reference to it. Returns "" if the attribute doesn't exist.
Eric Christopher3dee5752013-07-26 17:02:41 +00001019static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
Eric Christopherb7669132013-08-07 01:18:33 +00001020 DIEValue *V = Die->findAttribute(Attr);
Eric Christopher3dee5752013-07-26 17:02:41 +00001021
Eric Christopherb7669132013-08-07 01:18:33 +00001022 if (DIEString *S = dyn_cast_or_null<DIEString>(V))
1023 return S->getString();
1024
Eric Christopher3dee5752013-07-26 17:02:41 +00001025 return StringRef("");
1026}
1027
Eric Christopher3dee5752013-07-26 17:02:41 +00001028/// Return true if the current DIE is contained within an anonymous namespace.
1029static bool isContainedInAnonNamespace(DIE *Die) {
1030 DIE *Parent = Die->getParent();
1031
1032 while (Parent) {
Eric Christopher86aa03d2013-07-29 23:53:08 +00001033 if (Parent->getTag() == dwarf::DW_TAG_namespace &&
1034 getDIEStringAttr(Parent, dwarf::DW_AT_name) == "")
Eric Christopher3dee5752013-07-26 17:02:41 +00001035 return true;
1036 Parent = Parent->getParent();
1037 }
1038
1039 return false;
1040}
1041
Eric Christopher028f3472013-08-07 01:18:24 +00001042/// Test if the current CU language is C++ and that we have
1043/// a named type that is not contained in an anonymous namespace.
1044static bool shouldAddODRHash(CompileUnit *CU, DIE *Die) {
Eric Christopherf76e1182013-08-07 08:35:10 +00001045 return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
1046 getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
1047 !isContainedInAnonNamespace(Die);
Eric Christopher0d27ca12013-08-08 23:45:55 +00001048}
Eric Christopher028f3472013-08-07 01:18:24 +00001049
Eric Christopher4117bec2012-11-22 00:59:49 +00001050void DwarfDebug::finalizeModuleInfo() {
1051 // Collect info for variables that were optimized out.
1052 collectDeadVariables();
1053
1054 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1055 computeInlinedDIEs();
Devang Patel53bb5c92009-11-10 23:06:00 +00001056
Eric Christopherbe482042013-07-29 22:24:32 +00001057 // Split out type units and conditionally add an ODR tag to the split
1058 // out type.
Eric Christopher3dee5752013-07-26 17:02:41 +00001059 // FIXME: Do type splitting.
1060 for (unsigned i = 0, e = TypeUnits.size(); i != e; ++i) {
Eric Christopher3dee5752013-07-26 17:02:41 +00001061 DIE *Die = TypeUnits[i];
Eric Christopher0d27ca12013-08-08 23:45:55 +00001062 DIEHash Hash;
Eric Christopher028f3472013-08-07 01:18:24 +00001063 // If we've requested ODR hashes and it's applicable for an ODR hash then
1064 // add the ODR signature now.
Eric Christopher0d27ca12013-08-08 23:45:55 +00001065 // FIXME: This should be added onto the type unit, not the type, but this
1066 // works as an intermediate stage.
Eric Christopher028f3472013-08-07 01:18:24 +00001067 if (GenerateODRHash && shouldAddODRHash(CUMap.begin()->second, Die))
Eric Christopher0d27ca12013-08-08 23:45:55 +00001068 CUMap.begin()->second->addUInt(Die, dwarf::DW_AT_GNU_odr_signature,
1069 dwarf::DW_FORM_data8,
1070 Hash.computeDIEODRSignature(Die));
Eric Christopher3dee5752013-07-26 17:02:41 +00001071 }
1072
Eric Christopherf962c022013-08-12 20:27:48 +00001073 // Handle anything that needs to be done on a per-cu basis.
1074 for (DenseMap<const MDNode *, CompileUnit *>::iterator CUI = CUMap.begin(),
1075 CUE = CUMap.end();
1076 CUI != CUE; ++CUI) {
1077 CompileUnit *TheCU = CUI->second;
1078 // Emit DW_AT_containing_type attribute to connect types with their
1079 // vtable holding type.
1080 TheCU->constructContainingTypeDIEs();
1081
1082 // If we're splitting the dwarf out now that we've got the entire
1083 // CU then construct a skeleton CU based upon it.
1084 if (useSplitDwarf()) {
Eric Christopher0710bfa2013-08-13 01:21:55 +00001085 uint64_t ID = 0;
1086 if (GenerateCUHash) {
1087 DIEHash CUHash;
1088 ID = CUHash.computeCUSignature(TheCU->getCUDie());
1089 }
Eric Christopherf962c022013-08-12 20:27:48 +00001090 // This should be a unique identifier when we want to build .dwp files.
1091 TheCU->addUInt(TheCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopher0710bfa2013-08-13 01:21:55 +00001092 dwarf::DW_FORM_data8, ID);
Eric Christopherf962c022013-08-12 20:27:48 +00001093 // Now construct the skeleton CU associated.
Eric Christophera9b2c792013-08-26 23:50:43 +00001094 CompileUnit *SkCU = constructSkeletonCU(TheCU);
Eric Christopherf962c022013-08-12 20:27:48 +00001095 // This should be a unique identifier when we want to build .dwp files.
1096 SkCU->addUInt(SkCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopher0710bfa2013-08-13 01:21:55 +00001097 dwarf::DW_FORM_data8, ID);
Eric Christopherf962c022013-08-12 20:27:48 +00001098 }
1099 }
1100
1101 // Compute DIE offsets and sizes.
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001102 InfoHolder.computeSizeAndOffsets();
1103 if (useSplitDwarf())
1104 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher4117bec2012-11-22 00:59:49 +00001105}
1106
1107void DwarfDebug::endSections() {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001108 // Filter labels by section.
Alexey Samsonov9d08d692013-10-03 08:54:43 +00001109 for (size_t n = 0; n < ArangeLabels.size(); n++) {
1110 const SymbolCU &SCU = ArangeLabels[n];
Richard Mitton5cc319a2013-09-19 23:21:01 +00001111 if (SCU.Sym->isInSection()) {
1112 // Make a note of this symbol and it's section.
1113 const MCSection *Section = &SCU.Sym->getSection();
1114 if (!Section->getKind().isMetadata())
1115 SectionMap[Section].push_back(SCU);
1116 } else {
1117 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1118 // appear in the output. This sucks as we rely on sections to build
1119 // arange spans. We can do it without, but it's icky.
1120 SectionMap[NULL].push_back(SCU);
1121 }
1122 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001123
Richard Mitton7c9659a2013-10-03 22:07:08 +00001124 // Build a list of sections used.
1125 std::vector<const MCSection *> Sections;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001126 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
1127 it++) {
1128 const MCSection *Section = it->first;
Richard Mitton7c9659a2013-10-03 22:07:08 +00001129 Sections.push_back(Section);
1130 }
1131
1132 // Sort the sections into order.
1133 // This is only done to ensure consistent output order across different runs.
1134 std::sort(Sections.begin(), Sections.end(), SectionSort);
1135
1136 // Add terminating symbols for each section.
1137 for (unsigned ID=0;ID<Sections.size();ID++) {
1138 const MCSection *Section = Sections[ID];
Richard Mitton5cc319a2013-09-19 23:21:01 +00001139 MCSymbol *Sym = NULL;
1140
1141 if (Section) {
Richard Mitton7c9659a2013-10-03 22:07:08 +00001142 // We can't call MCSection::getLabelEndName, as it's only safe to do so
1143 // if we know the section name up-front. For user-created sections, the resulting
1144 // label may not be valid to use as a label. (section names can use a greater
1145 // set of characters on some systems)
1146 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001147 Asm->OutStreamer.SwitchSection(Section);
1148 Asm->OutStreamer.EmitLabel(Sym);
1149 }
1150
1151 // Insert a final terminator.
Alexey Samsonov9d08d692013-10-03 08:54:43 +00001152 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001153 }
Eric Christopher4117bec2012-11-22 00:59:49 +00001154}
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001155
Eric Christopherb6dc8652012-11-27 22:43:45 +00001156// Emit all Dwarf sections that should come after the content.
Eric Christopher4117bec2012-11-22 00:59:49 +00001157void DwarfDebug::endModule() {
1158
1159 if (!FirstCU) return;
1160
1161 // End any existing sections.
1162 // TODO: Does this need to happen?
1163 endSections();
1164
1165 // Finalize the debug info for the module.
1166 finalizeModuleInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001167
Eric Christopher4daaed12012-12-10 19:51:21 +00001168 if (!useSplitDwarf()) {
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001169 emitDebugStr();
1170
Eric Christopher42885022012-11-27 22:43:42 +00001171 // Emit all the DIEs into a debug info section.
1172 emitDebugInfo();
Eric Christopher74802f72012-11-27 00:41:54 +00001173
Eric Christopher42885022012-11-27 22:43:42 +00001174 // Corresponding abbreviations into a abbrev section.
1175 emitAbbreviations();
1176
1177 // Emit info into a debug loc section.
1178 emitDebugLoc();
1179
1180 // Emit info into a debug aranges section.
1181 emitDebugARanges();
1182
1183 // Emit info into a debug ranges section.
1184 emitDebugRanges();
1185
1186 // Emit info into a debug macinfo section.
1187 emitDebugMacInfo();
1188
Eric Christopher42885022012-11-27 22:43:42 +00001189 } else {
Eric Christopher0b944ee2012-12-11 19:42:09 +00001190 // TODO: Fill this in for separated debug sections and separate
Eric Christopher42885022012-11-27 22:43:42 +00001191 // out information into new sections.
Eric Christopher9e0b08d2013-09-20 23:22:52 +00001192 emitDebugStr();
1193 if (useSplitDwarf())
1194 emitDebugStrDWO();
Eric Christopher42885022012-11-27 22:43:42 +00001195
Eric Christopher98e237f2012-11-30 23:59:06 +00001196 // Emit the debug info section and compile units.
Eric Christopher42885022012-11-27 22:43:42 +00001197 emitDebugInfo();
Eric Christopher98e237f2012-11-30 23:59:06 +00001198 emitDebugInfoDWO();
Eric Christopher42885022012-11-27 22:43:42 +00001199
1200 // Corresponding abbreviations into a abbrev section.
1201 emitAbbreviations();
Eric Christopher6eebe472012-12-19 22:02:53 +00001202 emitDebugAbbrevDWO();
Eric Christopher42885022012-11-27 22:43:42 +00001203
1204 // Emit info into a debug loc section.
1205 emitDebugLoc();
1206
1207 // Emit info into a debug aranges section.
1208 emitDebugARanges();
1209
1210 // Emit info into a debug ranges section.
1211 emitDebugRanges();
1212
1213 // Emit info into a debug macinfo section.
1214 emitDebugMacInfo();
1215
Eric Christopher72f7bfb2013-01-15 23:56:56 +00001216 // Emit DWO addresses.
1217 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
1218
Eric Christopher42885022012-11-27 22:43:42 +00001219 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001220
Eric Christopher9d9f5a52012-08-23 07:32:06 +00001221 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +00001222 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +00001223 emitAccelNames();
1224 emitAccelObjC();
1225 emitAccelNamespaces();
1226 emitAccelTypes();
1227 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001228
Eric Christopher26123542013-08-30 00:40:17 +00001229 // Emit the pubnames and pubtypes sections if requested.
1230 if (HasDwarfPubSections) {
David Blaikiee56a4d92013-09-19 17:33:35 +00001231 emitDebugPubNames(GenerateGnuPubSections);
1232 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher26123542013-08-30 00:40:17 +00001233 }
Devang Patel193f7202009-11-24 01:14:22 +00001234
Devang Patele9a1cca2010-08-02 17:32:15 +00001235 // clean up.
Devang Patel94c7ddb2011-08-16 22:09:43 +00001236 SPMap.clear();
Devang Patel163a9f72010-05-10 22:49:55 +00001237 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
1238 E = CUMap.end(); I != E; ++I)
1239 delete I->second;
Eric Christopher9ec87b32012-12-10 19:51:18 +00001240
Craig Topper2d785db2013-07-03 04:17:25 +00001241 for (SmallVectorImpl<CompileUnit *>::iterator I = SkeletonCUs.begin(),
Eric Christopher01776a52013-02-06 21:53:56 +00001242 E = SkeletonCUs.end(); I != E; ++I)
1243 delete *I;
Eric Christopher9ec87b32012-12-10 19:51:18 +00001244
Eric Christopher98e237f2012-11-30 23:59:06 +00001245 // Reset these for the next Module if we have one.
1246 FirstCU = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001247}
1248
Eric Christopherb6dc8652012-11-27 22:43:45 +00001249// Find abstract variable, if any, associated with Var.
Devang Patelb549bcf2011-08-10 21:50:54 +00001250DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattnerde4845c2010-04-02 19:42:39 +00001251 DebugLoc ScopeLoc) {
Devang Patelb549bcf2011-08-10 21:50:54 +00001252 LLVMContext &Ctx = DV->getContext();
1253 // More then one inlined variable corresponds to one abstract variable.
1254 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patel2db49d72010-05-07 18:11:54 +00001255 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patel53bb5c92009-11-10 23:06:00 +00001256 if (AbsDbgVariable)
1257 return AbsDbgVariable;
1258
Devang Patelbf47fdb2011-08-10 20:55:27 +00001259 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patel53bb5c92009-11-10 23:06:00 +00001260 if (!Scope)
1261 return NULL;
1262
Manman Renc664d762013-10-05 01:43:03 +00001263 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001264 addScopeVariable(Scope, AbsDbgVariable);
Devang Patel2db49d72010-05-07 18:11:54 +00001265 AbstractVariables[Var] = AbsDbgVariable;
Devang Patel53bb5c92009-11-10 23:06:00 +00001266 return AbsDbgVariable;
1267}
1268
Eric Christopherb6dc8652012-11-27 22:43:45 +00001269// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel0478c152011-03-01 22:58:55 +00001270bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patelbf47fdb2011-08-10 20:55:27 +00001271 DbgVariable *Var, LexicalScope *Scope) {
1272 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel0478c152011-03-01 22:58:55 +00001273 return false;
1274 DIVariable DV = Var->getVariable();
1275 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1276 return false;
1277 unsigned ArgNo = DV.getArgNumber();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001278 if (ArgNo == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001279 return false;
1280
Devang Patelcb3a6572011-03-03 20:02:02 +00001281 size_t Size = CurrentFnArguments.size();
1282 if (Size == 0)
Devang Patel0478c152011-03-01 22:58:55 +00001283 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patelbbd0f452011-03-03 21:49:41 +00001284 // llvm::Function argument size is not good indicator of how many
Devang Patel6f676be2011-03-03 20:08:10 +00001285 // arguments does the function have at source level.
1286 if (ArgNo > Size)
Devang Patelcb3a6572011-03-03 20:02:02 +00001287 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel0478c152011-03-01 22:58:55 +00001288 CurrentFnArguments[ArgNo - 1] = Var;
1289 return true;
1290}
1291
Eric Christopherb6dc8652012-11-27 22:43:45 +00001292// Collect variable information from side table maintained by MMI.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001293void
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001294DwarfDebug::collectVariableInfoFromMMITable(const MachineFunction *MF,
Devang Patelee432862010-05-20 19:57:06 +00001295 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patele717faa2009-10-06 01:26:37 +00001296 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1297 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
1298 VE = VMap.end(); VI != VE; ++VI) {
Devang Patele9f8f5e2010-05-07 20:54:48 +00001299 const MDNode *Var = VI->first;
Devang Patel53bb5c92009-11-10 23:06:00 +00001300 if (!Var) continue;
Devang Patel98e1cac2010-05-14 21:01:35 +00001301 Processed.insert(Var);
Chris Lattnerde4845c2010-04-02 19:42:39 +00001302 DIVariable DV(Var);
1303 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patel53bb5c92009-11-10 23:06:00 +00001304
Devang Patelbf47fdb2011-08-10 20:55:27 +00001305 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001306
Devang Patelfb0ee432009-11-10 23:20:04 +00001307 // If variable scope is not found then skip this variable.
Chris Lattnerde4845c2010-04-02 19:42:39 +00001308 if (Scope == 0)
Devang Patelfb0ee432009-11-10 23:20:04 +00001309 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +00001310
Devang Patel26c1e562010-05-20 16:36:41 +00001311 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renc664d762013-10-05 01:43:03 +00001312 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patelff9dd0a2011-08-15 21:24:36 +00001313 RegVar->setFrameIndex(VP.first);
Devang Patel0478c152011-03-01 22:58:55 +00001314 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001315 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001316 if (AbsDbgVariable)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001317 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patele717faa2009-10-06 01:26:37 +00001318 }
Devang Patelee432862010-05-20 19:57:06 +00001319}
Devang Patel90a48ad2010-03-15 18:33:46 +00001320
Eric Christopherb6dc8652012-11-27 22:43:45 +00001321// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1322// defined reg.
Devang Patelc3f5f782010-05-25 23:40:22 +00001323static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001324 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001325 return MI->getNumOperands() == 3 &&
1326 MI->getOperand(0).isReg() && MI->getOperand(0).getReg() &&
Adrian Prantl35176402013-07-09 20:28:37 +00001327 (MI->getOperand(1).isImm() ||
1328 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patelc3f5f782010-05-25 23:40:22 +00001329}
1330
Eric Christopherb6dc8652012-11-27 22:43:45 +00001331// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001332static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1333 const MCSymbol *FLabel,
Devang Patel90b40412011-07-08 17:09:57 +00001334 const MCSymbol *SLabel,
1335 const MachineInstr *MI) {
1336 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
1337
David Blaikie6d9dbd52013-06-16 20:34:15 +00001338 assert(MI->getNumOperands() == 3);
Adrian Prantl35176402013-07-09 20:28:37 +00001339 if (MI->getOperand(0).isReg()) {
Devang Patel90b40412011-07-08 17:09:57 +00001340 MachineLocation MLoc;
Adrian Prantl35176402013-07-09 20:28:37 +00001341 // If the second operand is an immediate, this is a
1342 // register-indirect address.
1343 if (!MI->getOperand(1).isImm())
Adrian Prantl13131e62013-04-26 21:57:17 +00001344 MLoc.set(MI->getOperand(0).getReg());
1345 else
1346 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel90b40412011-07-08 17:09:57 +00001347 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1348 }
1349 if (MI->getOperand(0).isImm())
1350 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1351 if (MI->getOperand(0).isFPImm())
1352 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1353 if (MI->getOperand(0).isCImm())
1354 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1355
Craig Topper5e25ee82012-02-05 08:31:47 +00001356 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +00001357}
1358
Eric Christopherb6dc8652012-11-27 22:43:45 +00001359// Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001360void
Devang Patel78e127d2010-06-25 22:07:34 +00001361DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1362 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbach1e20b962010-07-21 21:21:52 +00001363
Eric Christopher4e976452013-07-03 21:37:03 +00001364 // Grab the variable info that was squirreled away in the MMI side-table.
Devang Patelee432862010-05-20 19:57:06 +00001365 collectVariableInfoFromMMITable(MF, Processed);
1366
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001367 for (SmallVectorImpl<const MDNode*>::const_iterator
1368 UVI = UserVariables.begin(), UVE = UserVariables.end(); UVI != UVE;
1369 ++UVI) {
1370 const MDNode *Var = *UVI;
1371 if (Processed.count(Var))
Devang Patelee432862010-05-20 19:57:06 +00001372 continue;
1373
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001374 // History contains relevant DBG_VALUE instructions for Var and instructions
1375 // clobbering it.
1376 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1377 if (History.empty())
1378 continue;
1379 const MachineInstr *MInsn = History.front();
Devang Patelc3f5f782010-05-25 23:40:22 +00001380
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001381 DIVariable DV(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001382 LexicalScope *Scope = NULL;
Devang Pateld8720f42010-05-27 20:25:04 +00001383 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1384 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001385 Scope = LScopes.getCurrentFunctionScope();
David Blaikiec0ec8a42013-03-11 23:39:23 +00001386 else if (MDNode *IA = DV.getInlinedAt())
1387 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1388 else
1389 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patelee432862010-05-20 19:57:06 +00001390 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +00001391 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +00001392 continue;
1393
1394 Processed.insert(DV);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001395 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel5a1a67c2011-08-15 19:01:20 +00001396 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renc664d762013-10-05 01:43:03 +00001397 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Devang Patel0478c152011-03-01 22:58:55 +00001398 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patelbf47fdb2011-08-10 20:55:27 +00001399 addScopeVariable(Scope, RegVar);
Devang Patel5a1a67c2011-08-15 19:01:20 +00001400 if (AbsVar)
Devang Patelff9dd0a2011-08-15 21:24:36 +00001401 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001402
Eric Christopherc56e3f02012-10-08 20:48:54 +00001403 // Simplify ranges that are fully coalesced.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001404 if (History.size() <= 1 || (History.size() == 2 &&
1405 MInsn->isIdenticalTo(History.back()))) {
Devang Patelff9dd0a2011-08-15 21:24:36 +00001406 RegVar->setMInsn(MInsn);
Devang Patelc3f5f782010-05-25 23:40:22 +00001407 continue;
1408 }
1409
Eric Christopher498703b2013-01-28 17:33:26 +00001410 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001411 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001412
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001413 for (SmallVectorImpl<const MachineInstr*>::const_iterator
1414 HI = History.begin(), HE = History.end(); HI != HE; ++HI) {
1415 const MachineInstr *Begin = *HI;
1416 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +00001417
Devang Patel4ada1d72011-06-01 23:00:17 +00001418 // Check if DBG_VALUE is truncating a range.
1419 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg()
1420 && !Begin->getOperand(0).getReg())
1421 continue;
1422
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001423 // Compute the range for a register location.
1424 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1425 const MCSymbol *SLabel = 0;
1426
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001427 if (HI + 1 == HE)
1428 // If Begin is the last instruction in History then its value is valid
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001429 // until the end of the function.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001430 SLabel = FunctionEndSym;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001431 else {
1432 const MachineInstr *End = HI[1];
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001433 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Devang Patel476df5f2011-07-07 21:44:42 +00001434 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001435 if (End->isDebugValue())
1436 SLabel = getLabelBeforeInsn(End);
1437 else {
1438 // End is a normal instruction clobbering the range.
1439 SLabel = getLabelAfterInsn(End);
1440 assert(SLabel && "Forgot label after clobber instruction");
1441 ++HI;
1442 }
1443 }
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001444
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001445 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherabbb2002011-12-16 23:42:31 +00001446 DotDebugLocEntries.push_back(getDebugLocEntry(Asm, FLabel, SLabel,
1447 Begin));
Devang Patelc3f5f782010-05-25 23:40:22 +00001448 }
1449 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patel90a48ad2010-03-15 18:33:46 +00001450 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001451
1452 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001453 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1454 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1455 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1456 DIVariable DV(Variables.getElement(i));
Manman Rend03d2b22013-07-08 18:33:29 +00001457 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel93d39be2011-08-19 23:28:12 +00001458 continue;
1459 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renc664d762013-10-05 01:43:03 +00001460 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel98e1cac2010-05-14 21:01:35 +00001461 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001462}
Devang Patel98e1cac2010-05-14 21:01:35 +00001463
Eric Christopherb6dc8652012-11-27 22:43:45 +00001464// Return Label preceding the instruction.
Eric Christopher72f7bfb2013-01-15 23:56:56 +00001465MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001466 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1467 assert(Label && "Didn't insert label before instruction");
1468 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001469}
1470
Eric Christopherb6dc8652012-11-27 22:43:45 +00001471// Return Label immediately following the instruction.
Eric Christopher72f7bfb2013-01-15 23:56:56 +00001472MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001473 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001474}
1475
Eric Christopherb6dc8652012-11-27 22:43:45 +00001476// Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001477void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001478 // Check if source location changes, but ignore DBG_VALUE locations.
1479 if (!MI->isDebugValue()) {
1480 DebugLoc DL = MI->getDebugLoc();
1481 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001482 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001483 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001484 if (DL == PrologEndLoc) {
1485 Flags |= DWARF2_FLAG_PROLOGUE_END;
1486 PrologEndLoc = DebugLoc();
1487 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001488 if (PrologEndLoc.isUnknown())
1489 Flags |= DWARF2_FLAG_IS_STMT;
1490
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001491 if (!DL.isUnknown()) {
1492 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001493 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001494 } else
Devang Patel4243e672011-05-11 19:22:19 +00001495 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001496 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001497 }
Devang Patelaead63c2010-03-29 22:59:58 +00001498
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001499 // Insert labels where requested.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001500 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1501 LabelsBeforeInsn.find(MI);
1502
1503 // No label needed.
1504 if (I == LabelsBeforeInsn.end())
1505 return;
1506
1507 // Label already assigned.
1508 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001509 return;
Devang Patel553881b2010-03-29 17:20:31 +00001510
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001511 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001512 PrevLabel = MMI->getContext().CreateTempSymbol();
1513 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001514 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001515 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001516}
1517
Eric Christopherb6dc8652012-11-27 22:43:45 +00001518// Process end of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001519void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001520 // Don't create a new label after DBG_VALUE instructions.
1521 // They don't generate code.
1522 if (!MI->isDebugValue())
1523 PrevLabel = 0;
1524
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001525 DenseMap<const MachineInstr*, MCSymbol*>::iterator I =
1526 LabelsAfterInsn.find(MI);
1527
1528 // No label needed.
1529 if (I == LabelsAfterInsn.end())
1530 return;
1531
1532 // Label already assigned.
1533 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001534 return;
1535
1536 // We need a label after this instruction.
1537 if (!PrevLabel) {
1538 PrevLabel = MMI->getContext().CreateTempSymbol();
1539 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001540 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001541 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001542}
1543
Eric Christopherb6dc8652012-11-27 22:43:45 +00001544// Each LexicalScope has first instruction and last instruction to mark
1545// beginning and end of a scope respectively. Create an inverse map that list
1546// scopes starts (and ends) with an instruction. One instruction may start (or
1547// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001548void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001549 SmallVector<LexicalScope *, 4> WorkList;
1550 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001551 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001552 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001553
Craig Topper9f43ac62013-07-03 04:24:43 +00001554 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001555 if (!Children.empty())
Craig Topper2d785db2013-07-03 04:17:25 +00001556 for (SmallVectorImpl<LexicalScope *>::const_iterator SI = Children.begin(),
Devang Patel42aafd72010-01-20 02:05:23 +00001557 SE = Children.end(); SI != SE; ++SI)
1558 WorkList.push_back(*SI);
1559
Devang Patel53bb5c92009-11-10 23:06:00 +00001560 if (S->isAbstractScope())
1561 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001562
Craig Topper9f43ac62013-07-03 04:24:43 +00001563 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Pateleac9c072010-04-27 19:46:33 +00001564 if (Ranges.empty())
1565 continue;
Craig Topper2d785db2013-07-03 04:17:25 +00001566 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Devang Pateleac9c072010-04-27 19:46:33 +00001567 RE = Ranges.end(); RI != RE; ++RI) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001568 assert(RI->first && "InsnRange does not have first instruction!");
1569 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001570 requestLabelBeforeInsn(RI->first);
1571 requestLabelAfterInsn(RI->second);
Devang Pateleac9c072010-04-27 19:46:33 +00001572 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001573 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001574}
1575
Eric Christopherb6dc8652012-11-27 22:43:45 +00001576// Get MDNode for DebugLoc's scope.
Devang Patela3f48672011-05-09 22:14:49 +00001577static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1578 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1579 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1580 return DL.getScope(Ctx);
1581}
1582
Eric Christopherb6dc8652012-11-27 22:43:45 +00001583// Walk up the scope chain of given debug loc and find line number info
1584// for the function.
Devang Patel4243e672011-05-11 19:22:19 +00001585static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1586 const MDNode *Scope = getScopeNode(DL, Ctx);
1587 DISubprogram SP = getDISubprogram(Scope);
Manman Rend03d2b22013-07-08 18:33:29 +00001588 if (SP.isSubprogram()) {
Eric Christopher6126a1e2012-04-03 00:43:49 +00001589 // Check for number of operands since the compatibility is
1590 // cheap here.
Eric Christopherfa5b0502012-04-03 17:55:42 +00001591 if (SP->getNumOperands() > 19)
Eric Christopher6126a1e2012-04-03 00:43:49 +00001592 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1593 else
1594 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1595 }
1596
Devang Patel4243e672011-05-11 19:22:19 +00001597 return DebugLoc();
1598}
1599
Eric Christopherb6dc8652012-11-27 22:43:45 +00001600// Gather pre-function debug information. Assumes being called immediately
1601// after the function entry point has been emitted.
Chris Lattnereec791a2010-01-26 23:18:02 +00001602void DwarfDebug::beginFunction(const MachineFunction *MF) {
Chris Lattner994cb122010-04-05 03:52:55 +00001603 if (!MMI->hasDebugInfo()) return;
Devang Patelbf47fdb2011-08-10 20:55:27 +00001604 LScopes.initialize(*MF);
1605 if (LScopes.empty()) return;
1606 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001607
Manman Ren43213cf2013-02-05 21:52:47 +00001608 // Set DwarfCompileUnitID in MCContext to the Compile Unit this function
1609 // belongs to.
1610 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1611 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1612 assert(TheCU && "Unable to find compile unit!");
Rafael Espindola5e195a42013-10-05 16:42:21 +00001613 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren01cb18e2013-05-21 00:57:22 +00001614 // Use a single line table if we are using .loc and generating assembly.
1615 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1616 else
1617 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren43213cf2013-02-05 21:52:47 +00001618
Devang Pateleac9c072010-04-27 19:46:33 +00001619 FunctionBeginSym = Asm->GetTempSymbol("func_begin",
1620 Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001621 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001622 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001623
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001624 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1625
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001626 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopherb6dc8652012-11-27 22:43:45 +00001627 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001628 std::vector<const MDNode*> LiveUserVar(TRI->getNumRegs());
1629
Devang Patelb2b31a62010-05-26 19:37:24 +00001630 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001631 I != E; ++I) {
1632 bool AtBlockEntry = true;
Devang Patelb2b31a62010-05-26 19:37:24 +00001633 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1634 II != IE; ++II) {
1635 const MachineInstr *MI = II;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001636
Devang Patelb2b31a62010-05-26 19:37:24 +00001637 if (MI->isDebugValue()) {
Nick Lewycky746cb672011-10-26 22:55:33 +00001638 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001639
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001640 // Keep track of user variables.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001641 const MDNode *Var =
1642 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001643
1644 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001645 if (isDbgValueInDefinedReg(MI))
1646 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1647
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001648 // Check the history of this variable.
1649 SmallVectorImpl<const MachineInstr*> &History = DbgValues[Var];
1650 if (History.empty()) {
1651 UserVariables.push_back(Var);
1652 // The first mention of a function argument gets the FunctionBeginSym
1653 // label, so arguments are visible when breaking at function entry.
1654 DIVariable DV(Var);
Manman Rend03d2b22013-07-08 18:33:29 +00001655 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001656 DISubprogram(getDISubprogram(DV.getContext()))
1657 .describes(MF->getFunction()))
1658 LabelsBeforeInsn[MI] = FunctionBeginSym;
1659 } else {
1660 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1661 const MachineInstr *Prev = History.back();
1662 if (Prev->isDebugValue()) {
1663 // Coalesce identical entries at the end of History.
1664 if (History.size() >= 2 &&
Devang Patel79862892011-07-07 00:14:27 +00001665 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher02c1a642012-10-08 20:48:49 +00001666 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001667 << "\t" << *Prev
Devang Patel79862892011-07-07 00:14:27 +00001668 << "\t" << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001669 History.pop_back();
Devang Patel79862892011-07-07 00:14:27 +00001670 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001671
1672 // Terminate old register assignments that don't reach MI;
1673 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1674 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1675 isDbgValueInDefinedReg(Prev)) {
1676 // Previous register assignment needs to terminate at the end of
1677 // its basic block.
1678 MachineBasicBlock::const_iterator LastMI =
1679 PrevMBB->getLastNonDebugInstr();
Devang Patel79862892011-07-07 00:14:27 +00001680 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001681 // Drop DBG_VALUE for empty range.
Eric Christopher02c1a642012-10-08 20:48:49 +00001682 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Devang Patel79862892011-07-07 00:14:27 +00001683 << "\t" << *Prev << "\n");
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001684 History.pop_back();
David Blaikie4532c282013-06-20 00:25:24 +00001685 } else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001686 // Terminate after LastMI.
1687 History.push_back(LastMI);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001688 }
1689 }
1690 }
1691 History.push_back(MI);
Devang Patelb2b31a62010-05-26 19:37:24 +00001692 } else {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001693 // Not a DBG_VALUE instruction.
1694 if (!MI->isLabel())
1695 AtBlockEntry = false;
1696
Eric Christopher0313ced2012-10-04 20:46:14 +00001697 // First known non-DBG_VALUE and non-frame setup location marks
1698 // the beginning of the function body.
1699 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1700 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel4243e672011-05-11 19:22:19 +00001701 PrologEndLoc = MI->getDebugLoc();
1702
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001703 // Check if the instruction clobbers any registers with debug vars.
1704 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
1705 MOE = MI->operands_end(); MOI != MOE; ++MOI) {
1706 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1707 continue;
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001708 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true);
1709 AI.isValid(); ++AI) {
1710 unsigned Reg = *AI;
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001711 const MDNode *Var = LiveUserVar[Reg];
1712 if (!Var)
1713 continue;
1714 // Reg is now clobbered.
1715 LiveUserVar[Reg] = 0;
1716
1717 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001718 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1719 if (HistI == DbgValues.end())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001720 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001721 SmallVectorImpl<const MachineInstr*> &History = HistI->second;
1722 if (History.empty())
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001723 continue;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001724 const MachineInstr *Prev = History.back();
1725 // Sanity-check: Register assignments are terminated at the end of
1726 // their block.
1727 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1728 continue;
1729 // Is the variable still in Reg?
1730 if (!isDbgValueInDefinedReg(Prev) ||
1731 Prev->getOperand(0).getReg() != Reg)
1732 continue;
1733 // Var is clobbered. Make sure the next instruction gets a label.
1734 History.push_back(MI);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001735 }
1736 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001737 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001738 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001739 }
1740
1741 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1742 I != E; ++I) {
1743 SmallVectorImpl<const MachineInstr*> &History = I->second;
1744 if (History.empty())
1745 continue;
1746
1747 // Make sure the final register assignments are terminated.
1748 const MachineInstr *Prev = History.back();
1749 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1750 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001751 MachineBasicBlock::const_iterator LastMI =
Devang Patel5bc942c2011-08-10 23:58:09 +00001752 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001753 if (LastMI == PrevMBB->end())
1754 // Drop DBG_VALUE for empty range.
1755 History.pop_back();
David Blaikie4532c282013-06-20 00:25:24 +00001756 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001757 // Terminate after LastMI.
1758 History.push_back(LastMI);
1759 }
1760 }
1761 // Request labels for the full history.
1762 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1763 const MachineInstr *MI = History[i];
1764 if (MI->isDebugValue())
1765 requestLabelBeforeInsn(MI);
1766 else
1767 requestLabelAfterInsn(MI);
1768 }
1769 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001770
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001771 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001772 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001773
1774 // Record beginning of function.
1775 if (!PrologEndLoc.isUnknown()) {
1776 DebugLoc FnStartDL = getFnDebugLoc(PrologEndLoc,
1777 MF->getFunction()->getContext());
1778 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1779 FnStartDL.getScope(MF->getFunction()->getContext()),
David Blaikie836cfc42012-12-04 22:02:33 +00001780 // We'd like to list the prologue as "not statements" but GDB behaves
1781 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikieb36c5312012-12-04 21:05:36 +00001782 DWARF2_FLAG_IS_STMT);
Devang Patel4243e672011-05-11 19:22:19 +00001783 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001784}
1785
Devang Patelbf47fdb2011-08-10 20:55:27 +00001786void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie032d6242013-06-05 05:39:59 +00001787 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1788 DIVariable DV = Var->getVariable();
David Blaikieb20fdff2013-06-06 21:04:51 +00001789 // Variables with positive arg numbers are parameters.
1790 if (unsigned ArgNum = DV.getArgNumber()) {
1791 // Keep all parameters in order at the start of the variable list to ensure
1792 // function types are correct (no out-of-order parameters)
1793 //
1794 // This could be improved by only doing it for optimized builds (unoptimized
1795 // builds have the right order to begin with), searching from the back (this
1796 // would catch the unoptimized case quickly), or doing a binary search
1797 // rather than linear search.
1798 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1799 while (I != Vars.end()) {
1800 unsigned CurNum = (*I)->getVariable().getArgNumber();
1801 // A local (non-parameter) variable has been found, insert immediately
1802 // before it.
1803 if (CurNum == 0)
1804 break;
1805 // A later indexed parameter has been found, insert immediately before it.
David Blaikiebabfebb2013-06-06 22:28:26 +00001806 if (CurNum > ArgNum)
David Blaikieb20fdff2013-06-06 21:04:51 +00001807 break;
David Blaikiebabfebb2013-06-06 22:28:26 +00001808 ++I;
David Blaikie032d6242013-06-05 05:39:59 +00001809 }
David Blaikieb20fdff2013-06-06 21:04:51 +00001810 Vars.insert(I, Var);
1811 return;
David Blaikie032d6242013-06-05 05:39:59 +00001812 }
1813
1814 Vars.push_back(Var);
Devang Patelbf47fdb2011-08-10 20:55:27 +00001815}
1816
Eric Christopherb6dc8652012-11-27 22:43:45 +00001817// Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +00001818void DwarfDebug::endFunction(const MachineFunction *MF) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001819 if (!MMI->hasDebugInfo() || LScopes.empty()) return;
Devang Patel70d75ca2009-11-12 19:02:56 +00001820
Devang Patelbf47fdb2011-08-10 20:55:27 +00001821 // Define end label for subprogram.
1822 FunctionEndSym = Asm->GetTempSymbol("func_end",
1823 Asm->getFunctionNumber());
1824 // Assumes in correct section after the entry point.
1825 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Manman Ren43213cf2013-02-05 21:52:47 +00001826 // Set DwarfCompileUnitID in MCContext to default value.
1827 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001828
Devang Patelbf47fdb2011-08-10 20:55:27 +00001829 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1830 collectVariableInfo(MF, ProcessedVars);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001831
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001832 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Patel94c7ddb2011-08-16 22:09:43 +00001833 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky746cb672011-10-26 22:55:33 +00001834 assert(TheCU && "Unable to find compile unit!");
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001835
Devang Patelbf47fdb2011-08-10 20:55:27 +00001836 // Construct abstract scopes.
Devang Patelcd9f6c52011-08-12 18:10:19 +00001837 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1838 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1839 LexicalScope *AScope = AList[i];
1840 DISubprogram SP(AScope->getScopeNode());
Manman Rend03d2b22013-07-08 18:33:29 +00001841 if (SP.isSubprogram()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001842 // Collect info for variables that were optimized out.
Devang Patel93d39be2011-08-19 23:28:12 +00001843 DIArray Variables = SP.getVariables();
1844 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1845 DIVariable DV(Variables.getElement(i));
Manman Rend03d2b22013-07-08 18:33:29 +00001846 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel93d39be2011-08-19 23:28:12 +00001847 continue;
Alexey Samsonovb67bd332012-07-06 08:45:08 +00001848 // Check that DbgVariable for DV wasn't created earlier, when
1849 // findAbstractVariable() was called for inlined instance of DV.
1850 LLVMContext &Ctx = DV->getContext();
1851 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1852 if (AbstractVariables.lookup(CleanDV))
1853 continue;
Devang Patel93d39be2011-08-19 23:28:12 +00001854 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renc664d762013-10-05 01:43:03 +00001855 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel78e127d2010-06-25 22:07:34 +00001856 }
1857 }
Devang Patelcd9f6c52011-08-12 18:10:19 +00001858 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren742671b2013-05-29 17:16:59 +00001859 constructScopeDIE(TheCU, AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001860 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001861
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001862 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001863
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001864 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopher873cf0a2012-08-24 01:14:27 +00001865 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001866
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001867 // Clear debug info
Craig Topper01eb99a2013-07-03 04:40:27 +00001868 for (ScopeVariablesMap::iterator
Devang Patelbf47fdb2011-08-10 20:55:27 +00001869 I = ScopeVariables.begin(), E = ScopeVariables.end(); I != E; ++I)
1870 DeleteContainerPointers(I->second);
1871 ScopeVariables.clear();
Devang Pateleac0c9d2011-04-22 18:09:57 +00001872 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001873 UserVariables.clear();
1874 DbgValues.clear();
Jeffrey Yasskin5c213dc2010-03-12 17:45:06 +00001875 AbstractVariables.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001876 LabelsBeforeInsn.clear();
1877 LabelsAfterInsn.clear();
Devang Patelf2548ca2010-04-16 23:33:45 +00001878 PrevLabel = NULL;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001879}
1880
Eric Christopherb6dc8652012-11-27 22:43:45 +00001881// Register a source line with debug info. Returns the unique label that was
1882// emitted and which provides correspondence to the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001883void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1884 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001885 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001886 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001887 unsigned Src = 1;
1888 if (S) {
1889 DIDescriptor Scope(S);
Devang Patelf84548d2009-10-05 18:03:19 +00001890
Dan Gohman1cc0d622010-05-05 23:41:32 +00001891 if (Scope.isCompileUnit()) {
1892 DICompileUnit CU(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001893 Fn = CU.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001894 Dir = CU.getDirectory();
Devang Patel3cabc9d2010-10-28 17:30:52 +00001895 } else if (Scope.isFile()) {
1896 DIFile F(S);
Devang Patel3cabc9d2010-10-28 17:30:52 +00001897 Fn = F.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001898 Dir = F.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001899 } else if (Scope.isSubprogram()) {
1900 DISubprogram SP(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001901 Fn = SP.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001902 Dir = SP.getDirectory();
Eric Christopher6618a242011-10-11 22:59:11 +00001903 } else if (Scope.isLexicalBlockFile()) {
1904 DILexicalBlockFile DBF(S);
1905 Fn = DBF.getFilename();
1906 Dir = DBF.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001907 } else if (Scope.isLexicalBlock()) {
1908 DILexicalBlock DB(S);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001909 Fn = DB.getFilename();
Devang Patel23670e52011-03-24 20:30:50 +00001910 Dir = DB.getDirectory();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001911 } else
Craig Topper5e25ee82012-02-05 08:31:47 +00001912 llvm_unreachable("Unexpected scope info");
Dan Gohman1cc0d622010-05-05 23:41:32 +00001913
Manman Ren3de61b42013-03-07 01:42:00 +00001914 Src = getOrCreateSourceID(Fn, Dir,
1915 Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman1cc0d622010-05-05 23:41:32 +00001916 }
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00001917 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001918}
1919
Bill Wendling829e67b2009-05-20 23:22:40 +00001920//===----------------------------------------------------------------------===//
1921// Emit Methods
1922//===----------------------------------------------------------------------===//
1923
Eric Christopherb6dc8652012-11-27 22:43:45 +00001924// Compute the size and offset of a DIE.
Jim Grosbach7ab38df2009-11-22 19:20:36 +00001925unsigned
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001926DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001927 // Get the children.
1928 const std::vector<DIE *> &Children = Die->getChildren();
1929
Bill Wendling94d04b82009-05-20 23:21:38 +00001930 // Record the abbreviation.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001931 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling94d04b82009-05-20 23:21:38 +00001932
1933 // Get the abbreviation for this DIE.
1934 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001935 const DIEAbbrev *Abbrev = Abbreviations->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001936
1937 // Set DIE offset
1938 Die->setOffset(Offset);
1939
1940 // Start the size with the size of abbreviation code.
Chris Lattneraf76e592009-08-22 20:48:53 +00001941 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00001942
Eric Christopherf7cef702013-03-29 23:34:06 +00001943 const SmallVectorImpl<DIEValue*> &Values = Die->getValues();
1944 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling94d04b82009-05-20 23:21:38 +00001945
1946 // Size the DIE attribute values.
1947 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1948 // Size attribute value.
Chris Lattnera37d5382010-04-05 00:18:22 +00001949 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling94d04b82009-05-20 23:21:38 +00001950
1951 // Size the DIE children if any.
1952 if (!Children.empty()) {
1953 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1954 "Children flag not set");
1955
1956 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopherfbd19752012-11-20 22:14:13 +00001957 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling94d04b82009-05-20 23:21:38 +00001958
1959 // End of children marker.
1960 Offset += sizeof(int8_t);
1961 }
1962
1963 Die->setSize(Offset - Die->getOffset());
1964 return Offset;
1965}
1966
Eric Christopherb6dc8652012-11-27 22:43:45 +00001967// Compute the size and offset of all the DIEs.
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001968void DwarfUnits::computeSizeAndOffsets() {
Manman Renbc3e96f2013-03-12 18:27:15 +00001969 // Offset from the beginning of debug info section.
Eric Christopherf7cef702013-03-29 23:34:06 +00001970 for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(),
Eric Christopher0e3e9b72012-12-10 23:34:43 +00001971 E = CUs.end(); I != E; ++I) {
Eric Christopher98e237f2012-11-30 23:59:06 +00001972 unsigned Offset =
1973 sizeof(int32_t) + // Length of Compilation Unit Info
1974 sizeof(int16_t) + // DWARF version number
1975 sizeof(int32_t) + // Offset Into Abbrev. Section
1976 sizeof(int8_t); // Pointer Size (in bytes)
Eric Christophere7099f62013-10-04 19:40:33 +00001977 computeSizeAndOffset((*I)->getCUDie(), Offset);
Devang Patel163a9f72010-05-10 22:49:55 +00001978 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001979}
1980
Eric Christopherb6dc8652012-11-27 22:43:45 +00001981// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001982void DwarfDebug::emitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001983 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001984
Bill Wendling94d04b82009-05-20 23:21:38 +00001985 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001986 DwarfInfoSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001987 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbach1e20b962010-07-21 21:21:52 +00001988 DwarfAbbrevSectionSym =
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001989 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher6eebe472012-12-19 22:02:53 +00001990 if (useSplitDwarf())
1991 DwarfAbbrevDWOSectionSym =
1992 emitSectionSym(Asm, TLOF.getDwarfAbbrevDWOSection(),
1993 "section_abbrev_dwo");
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001994 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbach1e20b962010-07-21 21:21:52 +00001995
Chris Lattner9c69e285532010-04-04 22:59:04 +00001996 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001997 emitSectionSym(Asm, MacroInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00001998
Eric Christopher7a0103c2013-02-07 21:19:50 +00001999 DwarfLineSectionSym =
2000 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002001 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christophercdeaae42013-09-23 20:55:35 +00002002 if (GenerateGnuPubSections) {
Eric Christophera6d84152013-09-30 23:14:16 +00002003 DwarfGnuPubNamesSectionSym =
2004 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
2005 DwarfGnuPubTypesSectionSym =
2006 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christophercdeaae42013-09-23 20:55:35 +00002007 } else if (HasDwarfPubSections) {
2008 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2009 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmandc42d032013-09-04 04:39:38 +00002010 }
Eric Christopher8f1a9292013-09-13 00:35:05 +00002011
Jim Grosbach1e20b962010-07-21 21:21:52 +00002012 DwarfStrSectionSym =
Eric Christopher64f824c2012-12-27 02:14:01 +00002013 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher9a9e73b2013-04-07 03:43:09 +00002014 if (useSplitDwarf()) {
Eric Christopher64f824c2012-12-27 02:14:01 +00002015 DwarfStrDWOSectionSym =
2016 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher9a9e73b2013-04-07 03:43:09 +00002017 DwarfAddrSectionSym =
2018 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
2019 }
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002020 DwarfDebugRangeSectionSym = emitSectionSym(Asm, TLOF.getDwarfRangesSection(),
Devang Patelf2548ca2010-04-16 23:33:45 +00002021 "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00002022
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002023 DwarfDebugLocSectionSym = emitSectionSym(Asm, TLOF.getDwarfLocSection(),
Devang Patelc3f5f782010-05-25 23:40:22 +00002024 "section_debug_loc");
2025
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002026 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
2027 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00002028}
2029
Eric Christopherb6dc8652012-11-27 22:43:45 +00002030// Recursively emits a debug information entry.
Eric Christopher6eebe472012-12-19 22:02:53 +00002031void DwarfDebug::emitDIE(DIE *Die, std::vector<DIEAbbrev *> *Abbrevs) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002032 // Get the abbreviation for this DIE.
2033 unsigned AbbrevNumber = Die->getAbbrevNumber();
Eric Christopher6eebe472012-12-19 22:02:53 +00002034 const DIEAbbrev *Abbrev = Abbrevs->at(AbbrevNumber - 1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002035
Bill Wendling94d04b82009-05-20 23:21:38 +00002036 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00002037 if (Asm->isVerbose())
Chris Lattner894d75a2010-01-22 23:18:42 +00002038 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2039 Twine::utohexstr(Die->getOffset()) + ":0x" +
2040 Twine::utohexstr(Die->getSize()) + " " +
2041 dwarf::TagString(Abbrev->getTag()));
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002042 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling94d04b82009-05-20 23:21:38 +00002043
Eric Christopherf7cef702013-03-29 23:34:06 +00002044 const SmallVectorImpl<DIEValue*> &Values = Die->getValues();
2045 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling94d04b82009-05-20 23:21:38 +00002046
2047 // Emit the DIE attribute values.
2048 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
2049 unsigned Attr = AbbrevData[i].getAttribute();
2050 unsigned Form = AbbrevData[i].getForm();
2051 assert(Form && "Too many attributes for DIE (check abbreviation)");
2052
Chris Lattner3f53c832010-04-04 18:52:31 +00002053 if (Asm->isVerbose())
Chris Lattnera8013622010-01-24 18:54:17 +00002054 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002055
Bill Wendling94d04b82009-05-20 23:21:38 +00002056 switch (Attr) {
Eric Christopher3f045002013-10-04 17:08:38 +00002057 case dwarf::DW_AT_abstract_origin: {
Bill Wendling94d04b82009-05-20 23:21:38 +00002058 DIEEntry *E = cast<DIEEntry>(Values[i]);
2059 DIE *Origin = E->getEntry();
Devang Patel53bb5c92009-11-10 23:06:00 +00002060 unsigned Addr = Origin->getOffset();
Eric Christophere7099f62013-10-04 19:40:33 +00002061 Asm->EmitInt32(Addr);
Bill Wendling94d04b82009-05-20 23:21:38 +00002062 break;
2063 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002064 case dwarf::DW_AT_ranges: {
2065 // DW_AT_range Value encodes offset in debug_range section.
2066 DIEInteger *V = cast<DIEInteger>(Values[i]);
Devang Patelb1fcfbe2010-09-02 16:43:44 +00002067
Nick Lewyckyffccd922012-06-22 01:25:12 +00002068 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections()) {
Devang Patelb1fcfbe2010-09-02 16:43:44 +00002069 Asm->EmitLabelPlusOffset(DwarfDebugRangeSectionSym,
2070 V->getValue(),
2071 4);
2072 } else {
2073 Asm->EmitLabelOffsetDifference(DwarfDebugRangeSectionSym,
2074 V->getValue(),
2075 DwarfDebugRangeSectionSym,
2076 4);
2077 }
Devang Patelf2548ca2010-04-16 23:33:45 +00002078 break;
2079 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002080 case dwarf::DW_AT_location: {
Nick Lewyckyffccd922012-06-22 01:25:12 +00002081 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
2082 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +00002083 Asm->EmitLabelReference(L->getValue(), 4);
Nick Lewyckyffccd922012-06-22 01:25:12 +00002084 else
Ulrich Weigand1f8aacd2013-07-02 18:46:26 +00002085 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Nick Lewyckyffccd922012-06-22 01:25:12 +00002086 } else {
Devang Patelc3f5f782010-05-25 23:40:22 +00002087 Values[i]->EmitValue(Asm, Form);
Nick Lewyckyffccd922012-06-22 01:25:12 +00002088 }
Devang Patelc3f5f782010-05-25 23:40:22 +00002089 break;
2090 }
Devang Patel2a361602010-09-29 19:08:08 +00002091 case dwarf::DW_AT_accessibility: {
2092 if (Asm->isVerbose()) {
2093 DIEInteger *V = cast<DIEInteger>(Values[i]);
2094 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2095 }
2096 Values[i]->EmitValue(Asm, Form);
2097 break;
2098 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002099 default:
2100 // Emit an attribute using the defined form.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002101 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00002102 break;
2103 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002104 }
2105
2106 // Emit the DIE children if any.
2107 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2108 const std::vector<DIE *> &Children = Die->getChildren();
2109
2110 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher6eebe472012-12-19 22:02:53 +00002111 emitDIE(Children[j], Abbrevs);
Bill Wendling94d04b82009-05-20 23:21:38 +00002112
Chris Lattner3f53c832010-04-04 18:52:31 +00002113 if (Asm->isVerbose())
Chris Lattner233f52b2010-03-09 23:52:58 +00002114 Asm->OutStreamer.AddComment("End Of Children Mark");
2115 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00002116 }
2117}
2118
Eric Christopherb1e66d02012-12-15 00:04:07 +00002119// Emit the various dwarf units to the unit section USection with
2120// the abbreviations going into ASection.
2121void DwarfUnits::emitUnits(DwarfDebug *DD,
2122 const MCSection *USection,
2123 const MCSection *ASection,
2124 const MCSymbol *ASectionSym) {
2125 Asm->OutStreamer.SwitchSection(USection);
Eric Christopherf7cef702013-03-29 23:34:06 +00002126 for (SmallVectorImpl<CompileUnit *>::iterator I = CUs.begin(),
Eric Christopherb1e66d02012-12-15 00:04:07 +00002127 E = CUs.end(); I != E; ++I) {
2128 CompileUnit *TheCU = *I;
Devang Patel163a9f72010-05-10 22:49:55 +00002129 DIE *Die = TheCU->getCUDie();
Jim Grosbach1e20b962010-07-21 21:21:52 +00002130
Devang Patel163a9f72010-05-10 22:49:55 +00002131 // Emit the compile units header.
Eric Christopherb1e66d02012-12-15 00:04:07 +00002132 Asm->OutStreamer
2133 .EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
2134 TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002135
Devang Patel163a9f72010-05-10 22:49:55 +00002136 // Emit size of content not including length itself
2137 unsigned ContentSize = Die->getSize() +
2138 sizeof(int16_t) + // DWARF version number
2139 sizeof(int32_t) + // Offset Into Abbrev. Section
Devang Patel65705d52011-04-13 19:41:17 +00002140 sizeof(int8_t); // Pointer Size (in bytes)
Jim Grosbach1e20b962010-07-21 21:21:52 +00002141
Devang Patel163a9f72010-05-10 22:49:55 +00002142 Asm->OutStreamer.AddComment("Length of Compilation Unit Info");
2143 Asm->EmitInt32(ContentSize);
2144 Asm->OutStreamer.AddComment("DWARF version number");
Manman Ren0e6783f2013-07-02 23:40:10 +00002145 Asm->EmitInt16(DD->getDwarfVersion());
Devang Patel163a9f72010-05-10 22:49:55 +00002146 Asm->OutStreamer.AddComment("Offset Into Abbrev. Section");
Eric Christopherb1e66d02012-12-15 00:04:07 +00002147 Asm->EmitSectionOffset(Asm->GetTempSymbol(ASection->getLabelBeginName()),
2148 ASectionSym);
Devang Patel163a9f72010-05-10 22:49:55 +00002149 Asm->OutStreamer.AddComment("Address Size (in bytes)");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002150 Asm->EmitInt8(Asm->getDataLayout().getPointerSize());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002151
Eric Christopher6eebe472012-12-19 22:02:53 +00002152 DD->emitDIE(Die, Abbreviations);
Eric Christopherb1e66d02012-12-15 00:04:07 +00002153 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelEndName(),
Eli Benderskyd4a05e02012-12-03 18:45:45 +00002154 TheCU->getUniqueID()));
Devang Patel163a9f72010-05-10 22:49:55 +00002155 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002156}
2157
Eric Christopher98e237f2012-11-30 23:59:06 +00002158// Emit the debug info section.
2159void DwarfDebug::emitDebugInfo() {
Eric Christopherb1e66d02012-12-15 00:04:07 +00002160 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2161
2162 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
2163 Asm->getObjFileLowering().getDwarfAbbrevSection(),
2164 DwarfAbbrevSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00002165}
2166
Eric Christopherb6dc8652012-11-27 22:43:45 +00002167// Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +00002168void DwarfDebug::emitAbbreviations() {
Eric Christopher6eebe472012-12-19 22:02:53 +00002169 if (!useSplitDwarf())
2170 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
2171 &Abbreviations);
2172 else
2173 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2174}
Bill Wendling94d04b82009-05-20 23:21:38 +00002175
Eric Christopher6eebe472012-12-19 22:02:53 +00002176void DwarfDebug::emitAbbrevs(const MCSection *Section,
2177 std::vector<DIEAbbrev *> *Abbrevs) {
2178 // Check to see if it is worth the effort.
2179 if (!Abbrevs->empty()) {
2180 // Start the debug abbrev section.
2181 Asm->OutStreamer.SwitchSection(Section);
2182
2183 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher44fedba2012-12-13 03:00:38 +00002184 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling94d04b82009-05-20 23:21:38 +00002185
2186 // For each abbrevation.
Eric Christopher6eebe472012-12-19 22:02:53 +00002187 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002188 // Get abbreviation data
Eric Christopher6eebe472012-12-19 22:02:53 +00002189 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling94d04b82009-05-20 23:21:38 +00002190
2191 // Emit the abbrevations code (base 1 index.)
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002192 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling94d04b82009-05-20 23:21:38 +00002193
2194 // Emit the abbreviations data.
Chris Lattnerd38fee82010-04-05 00:13:49 +00002195 Abbrev->Emit(Asm);
Bill Wendling94d04b82009-05-20 23:21:38 +00002196 }
2197
2198 // Mark end of abbreviations.
Chris Lattner7e1a8f82010-04-04 19:09:29 +00002199 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling94d04b82009-05-20 23:21:38 +00002200
Eric Christopher6eebe472012-12-19 22:02:53 +00002201 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher44fedba2012-12-13 03:00:38 +00002202 Asm->OutStreamer.EmitLabel(End);
Bill Wendling94d04b82009-05-20 23:21:38 +00002203 }
2204}
2205
Eric Christopherb6dc8652012-11-27 22:43:45 +00002206// Emit the last address of the section and the end of the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002207void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002208 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00002209 Asm->OutStreamer.AddComment("Extended Op");
2210 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002211
Chris Lattner233f52b2010-03-09 23:52:58 +00002212 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002213 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00002214 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2215 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2216
2217 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00002218
Chris Lattnerc0215722010-04-04 19:25:43 +00002219 Asm->OutStreamer.EmitSymbolValue(Asm->GetTempSymbol("section_end",SectionEnd),
Eric Christopherca1dd052013-01-09 01:35:34 +00002220 Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00002221
2222 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00002223 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2224 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00002225 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00002226 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00002227}
2228
Eric Christopherb6dc8652012-11-27 22:43:45 +00002229// Emit visible names into a hashed accelerator table section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002230void DwarfDebug::emitAccelNames() {
Eric Christopher577056f2013-09-05 18:20:16 +00002231 DwarfAccelTable AT(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
Eric Christopher09ac3d82011-11-07 09:24:32 +00002232 dwarf::DW_FORM_data4));
2233 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2234 E = CUMap.end(); I != E; ++I) {
2235 CompileUnit *TheCU = I->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00002236 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNames();
2237 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002238 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer03406c42013-05-11 18:24:28 +00002239 StringRef Name = GI->getKey();
Eric Christopherfa03db02012-01-06 23:03:34 +00002240 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher0ffe2b42011-11-10 19:25:34 +00002241 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2242 DE = Entities.end(); DI != DE; ++DI)
2243 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00002244 }
2245 }
2246
2247 AT.FinalizeTable(Asm, "Names");
2248 Asm->OutStreamer.SwitchSection(
2249 Asm->getObjFileLowering().getDwarfAccelNamesSection());
2250 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2251 Asm->OutStreamer.EmitLabel(SectionBegin);
2252
2253 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002254 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002255}
2256
Eric Christopher72c16552012-12-20 21:58:40 +00002257// Emit objective C classes and categories into a hashed accelerator table
2258// section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002259void DwarfDebug::emitAccelObjC() {
Eric Christopher577056f2013-09-05 18:20:16 +00002260 DwarfAccelTable AT(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
Eric Christopher09ac3d82011-11-07 09:24:32 +00002261 dwarf::DW_FORM_data4));
2262 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2263 E = CUMap.end(); I != E; ++I) {
2264 CompileUnit *TheCU = I->second;
2265 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelObjC();
2266 for (StringMap<std::vector<DIE*> >::const_iterator
2267 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer03406c42013-05-11 18:24:28 +00002268 StringRef Name = GI->getKey();
Eric Christopherfa03db02012-01-06 23:03:34 +00002269 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher09ac3d82011-11-07 09:24:32 +00002270 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2271 DE = Entities.end(); DI != DE; ++DI)
2272 AT.AddName(Name, (*DI));
2273 }
2274 }
2275
2276 AT.FinalizeTable(Asm, "ObjC");
2277 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2278 .getDwarfAccelObjCSection());
2279 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2280 Asm->OutStreamer.EmitLabel(SectionBegin);
2281
2282 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002283 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002284}
2285
Eric Christopherb6dc8652012-11-27 22:43:45 +00002286// Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002287void DwarfDebug::emitAccelNamespaces() {
Eric Christopher577056f2013-09-05 18:20:16 +00002288 DwarfAccelTable AT(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
Eric Christopher09ac3d82011-11-07 09:24:32 +00002289 dwarf::DW_FORM_data4));
2290 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2291 E = CUMap.end(); I != E; ++I) {
2292 CompileUnit *TheCU = I->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002293 const StringMap<std::vector<DIE*> > &Names = TheCU->getAccelNamespace();
2294 for (StringMap<std::vector<DIE*> >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002295 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer03406c42013-05-11 18:24:28 +00002296 StringRef Name = GI->getKey();
Eric Christopherfa03db02012-01-06 23:03:34 +00002297 const std::vector<DIE *> &Entities = GI->second;
Eric Christopher8bd36ea2011-11-10 21:47:55 +00002298 for (std::vector<DIE *>::const_iterator DI = Entities.begin(),
2299 DE = Entities.end(); DI != DE; ++DI)
2300 AT.AddName(Name, (*DI));
Eric Christopher09ac3d82011-11-07 09:24:32 +00002301 }
2302 }
2303
2304 AT.FinalizeTable(Asm, "namespac");
2305 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2306 .getDwarfAccelNamespaceSection());
2307 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2308 Asm->OutStreamer.EmitLabel(SectionBegin);
2309
2310 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002311 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002312}
2313
Eric Christopherb6dc8652012-11-27 22:43:45 +00002314// Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00002315void DwarfDebug::emitAccelTypes() {
Eric Christopherc36145f2012-01-06 04:35:23 +00002316 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopher577056f2013-09-05 18:20:16 +00002317 Atoms.push_back(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
Eric Christopherc36145f2012-01-06 04:35:23 +00002318 dwarf::DW_FORM_data4));
Eric Christopher577056f2013-09-05 18:20:16 +00002319 Atoms.push_back(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag,
Eric Christopherc36145f2012-01-06 04:35:23 +00002320 dwarf::DW_FORM_data2));
Eric Christopher577056f2013-09-05 18:20:16 +00002321 Atoms.push_back(DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags,
Eric Christopherc36145f2012-01-06 04:35:23 +00002322 dwarf::DW_FORM_data1));
2323 DwarfAccelTable AT(Atoms);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002324 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2325 E = CUMap.end(); I != E; ++I) {
2326 CompileUnit *TheCU = I->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002327 const StringMap<std::vector<std::pair<DIE*, unsigned > > > &Names
2328 = TheCU->getAccelTypes();
2329 for (StringMap<std::vector<std::pair<DIE*, unsigned> > >::const_iterator
Eric Christopher09ac3d82011-11-07 09:24:32 +00002330 GI = Names.begin(), GE = Names.end(); GI != GE; ++GI) {
Benjamin Kramer03406c42013-05-11 18:24:28 +00002331 StringRef Name = GI->getKey();
Eric Christopherfa03db02012-01-06 23:03:34 +00002332 const std::vector<std::pair<DIE *, unsigned> > &Entities = GI->second;
Eric Christopherc36145f2012-01-06 04:35:23 +00002333 for (std::vector<std::pair<DIE *, unsigned> >::const_iterator DI
2334 = Entities.begin(), DE = Entities.end(); DI !=DE; ++DI)
2335 AT.AddName(Name, (*DI).first, (*DI).second);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002336 }
2337 }
2338
2339 AT.FinalizeTable(Asm, "types");
2340 Asm->OutStreamer.SwitchSection(Asm->getObjFileLowering()
2341 .getDwarfAccelTypesSection());
2342 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2343 Asm->OutStreamer.EmitLabel(SectionBegin);
2344
2345 // Emit the full data.
Eric Christopher2e5d8702012-12-20 21:58:36 +00002346 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher09ac3d82011-11-07 09:24:32 +00002347}
2348
Eric Christopher8f1a9292013-09-13 00:35:05 +00002349// Public name handling.
2350// The format for the various pubnames:
2351//
2352// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2353// for the DIE that is named.
2354//
2355// gnu pubnames - offset/index value/name tuples where the offset is the offset
2356// into the CU and the index value is computed according to the type of value
2357// for the DIE that is named.
2358//
2359// For type units the offset is the offset of the skeleton DIE. For split dwarf
2360// it's the offset within the debug_info/debug_types dwo section, however, the
2361// reference in the pubname header doesn't change.
2362
2363/// computeIndexValue - Compute the gdb index value for the DIE and CU.
David Blaikie9599f512013-09-19 20:40:26 +00002364static dwarf::PubIndexEntryDescriptor computeIndexValue(CompileUnit *CU,
2365 DIE *Die) {
Eric Christophera31a9752013-09-23 22:59:14 +00002366 dwarf::GDBIndexEntryLinkage Linkage =
David Blaikie9599f512013-09-19 20:40:26 +00002367 Die->findAttribute(dwarf::DW_AT_external) ? dwarf::GIEL_EXTERNAL
2368 : dwarf::GIEL_STATIC;
Eric Christopher8f1a9292013-09-13 00:35:05 +00002369
2370 switch (Die->getTag()) {
2371 case dwarf::DW_TAG_class_type:
2372 case dwarf::DW_TAG_structure_type:
2373 case dwarf::DW_TAG_union_type:
2374 case dwarf::DW_TAG_enumeration_type:
Eric Christophercdeaae42013-09-23 20:55:35 +00002375 return dwarf::PubIndexEntryDescriptor(
2376 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2377 ? dwarf::GIEL_STATIC
2378 : dwarf::GIEL_EXTERNAL);
Eric Christopher8f1a9292013-09-13 00:35:05 +00002379 case dwarf::DW_TAG_typedef:
2380 case dwarf::DW_TAG_base_type:
2381 case dwarf::DW_TAG_subrange_type:
David Blaikie9599f512013-09-19 20:40:26 +00002382 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopher8f1a9292013-09-13 00:35:05 +00002383 case dwarf::DW_TAG_namespace:
David Blaikie9599f512013-09-19 20:40:26 +00002384 return dwarf::GIEK_TYPE;
Eric Christopher8f1a9292013-09-13 00:35:05 +00002385 case dwarf::DW_TAG_subprogram:
Eric Christophera31a9752013-09-23 22:59:14 +00002386 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopher8f1a9292013-09-13 00:35:05 +00002387 case dwarf::DW_TAG_constant:
2388 case dwarf::DW_TAG_variable:
Eric Christophera31a9752013-09-23 22:59:14 +00002389 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopher8f1a9292013-09-13 00:35:05 +00002390 case dwarf::DW_TAG_enumerator:
David Blaikie9599f512013-09-19 20:40:26 +00002391 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2392 dwarf::GIEL_STATIC);
Eric Christopher8f1a9292013-09-13 00:35:05 +00002393 default:
David Blaikie9599f512013-09-19 20:40:26 +00002394 return dwarf::GIEK_NONE;
Eric Christopher8f1a9292013-09-13 00:35:05 +00002395 }
Eric Christopher8f1a9292013-09-13 00:35:05 +00002396}
2397
Eric Christopher57479322013-09-09 20:03:17 +00002398/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002399///
Eric Christopher8f1a9292013-09-13 00:35:05 +00002400void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002401 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopher8f1a9292013-09-13 00:35:05 +00002402 const MCSection *PSec =
2403 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2404 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002405
2406 typedef DenseMap<const MDNode*, CompileUnit*> CUMapType;
2407 for (CUMapType::iterator I = CUMap.begin(), E = CUMap.end(); I != E; ++I) {
2408 CompileUnit *TheCU = I->second;
2409 unsigned ID = TheCU->getUniqueID();
2410
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002411 // Start the dwarf pubnames section.
Eric Christopher21a6a502013-09-10 21:49:37 +00002412 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002413
Eric Christophera6d84152013-09-30 23:14:16 +00002414 // Emit a label so we can reference the beginning of this pubname section.
2415 if (GnuStyle)
2416 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubnames",
2417 TheCU->getUniqueID()));
2418
Eric Christopher8f1a9292013-09-13 00:35:05 +00002419 // Emit the header.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002420 Asm->OutStreamer.AddComment("Length of Public Names Info");
2421 Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
2422 Asm->GetTempSymbol("pubnames_begin", ID), 4);
2423
2424 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
2425
2426 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemerd7d43dc2013-08-21 06:13:34 +00002427 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002428
2429 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2430 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2431 DwarfInfoSectionSym);
2432
2433 Asm->OutStreamer.AddComment("Compilation Unit Length");
2434 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
2435 Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2436 4);
2437
Eric Christopher8f1a9292013-09-13 00:35:05 +00002438 // Emit the pubnames for this compilation unit.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002439 const StringMap<DIE*> &Globals = TheCU->getGlobalNames();
2440 for (StringMap<DIE*>::const_iterator
2441 GI = Globals.begin(), GE = Globals.end(); GI != GE; ++GI) {
2442 const char *Name = GI->getKeyData();
Eric Christopher8f1a9292013-09-13 00:35:05 +00002443 DIE *Entity = GI->second;
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002444
2445 Asm->OutStreamer.AddComment("DIE offset");
2446 Asm->EmitInt32(Entity->getOffset());
2447
Eric Christopher8f1a9292013-09-13 00:35:05 +00002448 if (GnuStyle) {
David Blaikie18a6ade2013-09-19 22:19:37 +00002449 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheCU, Entity);
2450 Asm->OutStreamer.AddComment(
David Blaikieac3f0162013-09-20 00:33:15 +00002451 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie994c37f2013-09-19 23:01:29 +00002452 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikie18a6ade2013-09-19 22:19:37 +00002453 Asm->EmitInt8(Desc.toBits());
Eric Christopher8f1a9292013-09-13 00:35:05 +00002454 }
2455
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002456 if (Asm->isVerbose())
2457 Asm->OutStreamer.AddComment("External Name");
Rafael Espindolaa3863ea2013-07-02 15:49:13 +00002458 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength()+1));
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00002459 }
2460
2461 Asm->OutStreamer.AddComment("End Mark");
2462 Asm->EmitInt32(0);
2463 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
2464 }
2465}
2466
Eric Christopher8f1a9292013-09-13 00:35:05 +00002467void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher5704d642013-09-13 00:34:58 +00002468 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christophercdeaae42013-09-23 20:55:35 +00002469 const MCSection *PSec =
2470 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2471 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher5704d642013-09-13 00:34:58 +00002472
Devang Patel163a9f72010-05-10 22:49:55 +00002473 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
Eric Christopher5704d642013-09-13 00:34:58 +00002474 E = CUMap.end();
2475 I != E; ++I) {
Devang Patel163a9f72010-05-10 22:49:55 +00002476 CompileUnit *TheCU = I->second;
Eric Christopher63701182011-11-07 09:18:35 +00002477 // Start the dwarf pubtypes section.
Eric Christopher5704d642013-09-13 00:34:58 +00002478 Asm->OutStreamer.SwitchSection(PSec);
Eric Christophera6d84152013-09-30 23:14:16 +00002479
2480 // Emit a label so we can reference the beginning of this pubtype section.
2481 if (GnuStyle)
2482 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubtypes",
2483 TheCU->getUniqueID()));
2484
2485 // Emit the header.
Devang Patel163a9f72010-05-10 22:49:55 +00002486 Asm->OutStreamer.AddComment("Length of Public Types Info");
2487 Asm->EmitLabelDifference(
Eric Christopher5704d642013-09-13 00:34:58 +00002488 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2489 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002490
Eric Christopher5704d642013-09-13 00:34:58 +00002491 Asm->OutStreamer.EmitLabel(
2492 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002493
Eric Christopher5704d642013-09-13 00:34:58 +00002494 if (Asm->isVerbose())
2495 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemerd7d43dc2013-08-21 06:13:34 +00002496 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002497
Devang Patel163a9f72010-05-10 22:49:55 +00002498 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher5704d642013-09-13 00:34:58 +00002499 Asm->EmitSectionOffset(
2500 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheCU->getUniqueID()),
2501 DwarfInfoSectionSym);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002502
Devang Patel163a9f72010-05-10 22:49:55 +00002503 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher5704d642013-09-13 00:34:58 +00002504 Asm->EmitLabelDifference(
2505 Asm->GetTempSymbol(ISec->getLabelEndName(), TheCU->getUniqueID()),
2506 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheCU->getUniqueID()), 4);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002507
Eric Christophera6d84152013-09-30 23:14:16 +00002508 // Emit the pubtypes.
Eric Christopher5704d642013-09-13 00:34:58 +00002509 const StringMap<DIE *> &Globals = TheCU->getGlobalTypes();
2510 for (StringMap<DIE *>::const_iterator GI = Globals.begin(),
2511 GE = Globals.end();
2512 GI != GE; ++GI) {
Devang Patel163a9f72010-05-10 22:49:55 +00002513 const char *Name = GI->getKeyData();
Nick Lewycky3bbb6f72011-07-29 03:49:23 +00002514 DIE *Entity = GI->second;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002515
Eric Christopher5704d642013-09-13 00:34:58 +00002516 if (Asm->isVerbose())
2517 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel163a9f72010-05-10 22:49:55 +00002518 Asm->EmitInt32(Entity->getOffset());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002519
Eric Christopher8f1a9292013-09-13 00:35:05 +00002520 if (GnuStyle) {
David Blaikie18a6ade2013-09-19 22:19:37 +00002521 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheCU, Entity);
2522 Asm->OutStreamer.AddComment(
David Blaikieac3f0162013-09-20 00:33:15 +00002523 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie994c37f2013-09-19 23:01:29 +00002524 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikie18a6ade2013-09-19 22:19:37 +00002525 Asm->EmitInt8(Desc.toBits());
Eric Christopher8f1a9292013-09-13 00:35:05 +00002526 }
2527
Eric Christopher5704d642013-09-13 00:34:58 +00002528 if (Asm->isVerbose())
2529 Asm->OutStreamer.AddComment("External Name");
2530
Benjamin Kramer983c4572011-11-09 18:16:11 +00002531 // Emit the name with a terminating null byte.
Eric Christopher5704d642013-09-13 00:34:58 +00002532 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Devang Patel163a9f72010-05-10 22:49:55 +00002533 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00002534
Devang Patel163a9f72010-05-10 22:49:55 +00002535 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbach1e20b962010-07-21 21:21:52 +00002536 Asm->EmitInt32(0);
Eric Christopher5704d642013-09-13 00:34:58 +00002537 Asm->OutStreamer.EmitLabel(
2538 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()));
Devang Patel193f7202009-11-24 01:14:22 +00002539 }
Devang Patel193f7202009-11-24 01:14:22 +00002540}
2541
Eric Christopher64f824c2012-12-27 02:14:01 +00002542// Emit strings into a string section.
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002543void DwarfUnits::emitStrings(const MCSection *StrSection,
2544 const MCSection *OffsetSection = NULL,
2545 const MCSymbol *StrSecSym = NULL) {
Eric Christopher64f824c2012-12-27 02:14:01 +00002546
Eric Christopherb6714222013-01-08 22:22:06 +00002547 if (StringPool.empty()) return;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002548
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002549 // Start the dwarf str section.
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002550 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling94d04b82009-05-20 23:21:38 +00002551
Chris Lattnerbc733f52010-03-13 02:17:42 +00002552 // Get all of the string pool entries and put them in an array by their ID so
2553 // we can sort them.
Jim Grosbach1e20b962010-07-21 21:21:52 +00002554 SmallVector<std::pair<unsigned,
Eric Christopherff348452013-01-07 22:40:45 +00002555 StringMapEntry<std::pair<MCSymbol*, unsigned> >*>, 64> Entries;
Jim Grosbach1e20b962010-07-21 21:21:52 +00002556
Chris Lattnerbc733f52010-03-13 02:17:42 +00002557 for (StringMap<std::pair<MCSymbol*, unsigned> >::iterator
Eric Christopherb6714222013-01-08 22:22:06 +00002558 I = StringPool.begin(), E = StringPool.end();
Eric Christopher72c16552012-12-20 21:58:40 +00002559 I != E; ++I)
Chris Lattnerbc733f52010-03-13 02:17:42 +00002560 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbach1e20b962010-07-21 21:21:52 +00002561
Chris Lattnerbc733f52010-03-13 02:17:42 +00002562 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbach1e20b962010-07-21 21:21:52 +00002563
Chris Lattnerbc733f52010-03-13 02:17:42 +00002564 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner0d9d70f2010-03-09 23:38:23 +00002565 // Emit a label for reference from debug information entries.
Chris Lattnerbc733f52010-03-13 02:17:42 +00002566 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbach1e20b962010-07-21 21:21:52 +00002567
Benjamin Kramer983c4572011-11-09 18:16:11 +00002568 // Emit the string itself with a terminating null byte.
Benjamin Kramer0c45f7d2011-11-09 12:12:04 +00002569 Asm->OutStreamer.EmitBytes(StringRef(Entries[i].second->getKeyData(),
Eric Christopherca1dd052013-01-09 01:35:34 +00002570 Entries[i].second->getKeyLength()+1));
Bill Wendling94d04b82009-05-20 23:21:38 +00002571 }
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002572
2573 // If we've got an offset section go ahead and emit that now as well.
2574 if (OffsetSection) {
2575 Asm->OutStreamer.SwitchSection(OffsetSection);
2576 unsigned offset = 0;
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002577 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002578 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopher1ced2082013-01-09 03:52:05 +00002579 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002580 offset += Entries[i].second->getKeyLength() + 1;
2581 }
2582 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002583}
2584
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002585// Emit strings into a string section.
2586void DwarfUnits::emitAddresses(const MCSection *AddrSection) {
2587
2588 if (AddressPool.empty()) return;
2589
2590 // Start the dwarf addr section.
2591 Asm->OutStreamer.SwitchSection(AddrSection);
2592
David Blaikie66f464e2013-07-08 17:51:28 +00002593 // Order the address pool entries by ID
David Blaikie5ce40912013-07-08 17:33:10 +00002594 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002595
David Blaikie66f464e2013-07-08 17:51:28 +00002596 for (DenseMap<const MCExpr *, unsigned>::iterator I = AddressPool.begin(),
2597 E = AddressPool.end();
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002598 I != E; ++I)
David Blaikie5ce40912013-07-08 17:33:10 +00002599 Entries[I->second] = I->first;
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002600
2601 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Ulrich Weigand18dbe502013-07-02 18:46:46 +00002602 // Emit an expression for reference from debug information entries.
David Blaikie5ce40912013-07-08 17:33:10 +00002603 if (const MCExpr *Expr = Entries[i])
Ulrich Weigand18dbe502013-07-02 18:46:46 +00002604 Asm->OutStreamer.EmitValue(Expr, Asm->getDataLayout().getPointerSize());
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002605 else
2606 Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
2607 }
2608
2609}
2610
Eric Christopher64f824c2012-12-27 02:14:01 +00002611// Emit visible names into a debug str section.
2612void DwarfDebug::emitDebugStr() {
2613 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2614 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2615}
2616
Eric Christopherd3b98532013-07-02 21:36:07 +00002617// Emit locations into the debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002618void DwarfDebug::emitDebugLoc() {
Devang Patel80250682010-05-26 23:55:23 +00002619 if (DotDebugLocEntries.empty())
2620 return;
2621
Eric Christopherf7cef702013-03-29 23:34:06 +00002622 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Devang Patel6c3ea902011-02-04 22:57:18 +00002623 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
2624 I != E; ++I) {
2625 DotDebugLocEntry &Entry = *I;
2626 if (I + 1 != DotDebugLocEntries.end())
2627 Entry.Merge(I+1);
2628 }
2629
Daniel Dunbar83320a02011-03-16 22:16:39 +00002630 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002631 Asm->OutStreamer.SwitchSection(
Devang Patelc3f5f782010-05-25 23:40:22 +00002632 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002633 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel80250682010-05-26 23:55:23 +00002634 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2635 unsigned index = 1;
Eric Christopherf7cef702013-03-29 23:34:06 +00002636 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002637 I = DotDebugLocEntries.begin(), E = DotDebugLocEntries.end();
Devang Patel61409622010-07-07 20:12:52 +00002638 I != E; ++I, ++index) {
Devang Patel6c3ea902011-02-04 22:57:18 +00002639 DotDebugLocEntry &Entry = *I;
2640 if (Entry.isMerged()) continue;
Devang Patelc3f5f782010-05-25 23:40:22 +00002641 if (Entry.isEmpty()) {
Eric Christopherca1dd052013-01-09 01:35:34 +00002642 Asm->OutStreamer.EmitIntValue(0, Size);
2643 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel80250682010-05-26 23:55:23 +00002644 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patelc3f5f782010-05-25 23:40:22 +00002645 } else {
Eric Christopher0d0782a2013-07-03 22:40:18 +00002646 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2647 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2648 DIVariable DV(Entry.getVariable());
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002649 Asm->OutStreamer.AddComment("Loc expr size");
2650 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2651 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2652 Asm->EmitLabelDifference(end, begin, 2);
2653 Asm->OutStreamer.EmitLabel(begin);
Devang Patel80efd4e2011-07-08 16:49:43 +00002654 if (Entry.isInt()) {
Devang Patelc4329072011-06-01 22:03:25 +00002655 DIBasicType BTy(DV.getType());
2656 if (BTy.Verify() &&
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002657 (BTy.getEncoding() == dwarf::DW_ATE_signed
Devang Patelc4329072011-06-01 22:03:25 +00002658 || BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2659 Asm->OutStreamer.AddComment("DW_OP_consts");
2660 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Patel80efd4e2011-07-08 16:49:43 +00002661 Asm->EmitSLEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002662 } else {
2663 Asm->OutStreamer.AddComment("DW_OP_constu");
2664 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Patel80efd4e2011-07-08 16:49:43 +00002665 Asm->EmitULEB128(Entry.getInt());
Devang Patelc4329072011-06-01 22:03:25 +00002666 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002667 } else if (Entry.isLocation()) {
Eric Christopher7c2f3e82013-07-03 22:40:21 +00002668 MachineLocation Loc = Entry.getLoc();
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002669 if (!DV.hasComplexAddress())
Devang Patel80efd4e2011-07-08 16:49:43 +00002670 // Regular entry.
Eric Christopher7c2f3e82013-07-03 22:40:21 +00002671 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Patel80efd4e2011-07-08 16:49:43 +00002672 else {
2673 // Complex address entry.
2674 unsigned N = DV.getNumAddrElements();
2675 unsigned i = 0;
2676 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher7c2f3e82013-07-03 22:40:21 +00002677 if (Loc.getOffset()) {
Devang Patel80efd4e2011-07-08 16:49:43 +00002678 i = 2;
Eric Christopher7c2f3e82013-07-03 22:40:21 +00002679 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Patel80efd4e2011-07-08 16:49:43 +00002680 Asm->OutStreamer.AddComment("DW_OP_deref");
2681 Asm->EmitInt8(dwarf::DW_OP_deref);
2682 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2683 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2684 Asm->EmitSLEB128(DV.getAddrElement(1));
2685 } else {
2686 // If first address element is OpPlus then emit
2687 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher7c2f3e82013-07-03 22:40:21 +00002688 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2689 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Patel80efd4e2011-07-08 16:49:43 +00002690 i = 2;
2691 }
2692 } else {
Eric Christopher7c2f3e82013-07-03 22:40:21 +00002693 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Patel80efd4e2011-07-08 16:49:43 +00002694 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00002695
Devang Patel80efd4e2011-07-08 16:49:43 +00002696 // Emit remaining complex address elements.
2697 for (; i < N; ++i) {
2698 uint64_t Element = DV.getAddrElement(i);
2699 if (Element == DIBuilder::OpPlus) {
2700 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2701 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher50120762012-05-08 18:56:00 +00002702 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher7c2f3e82013-07-03 22:40:21 +00002703 if (!Loc.isReg())
Eric Christopher50120762012-05-08 18:56:00 +00002704 Asm->EmitInt8(dwarf::DW_OP_deref);
2705 } else
2706 llvm_unreachable("unknown Opcode found in complex address");
Devang Patel80efd4e2011-07-08 16:49:43 +00002707 }
Devang Patelc26f5442011-04-28 02:22:40 +00002708 }
Devang Patelc26f5442011-04-28 02:22:40 +00002709 }
Devang Patel80efd4e2011-07-08 16:49:43 +00002710 // else ... ignore constant fp. There is not any good way to
2711 // to represent them here in dwarf.
Rafael Espindola5b23b7f2011-05-27 22:05:41 +00002712 Asm->OutStreamer.EmitLabel(end);
Devang Patelc3f5f782010-05-25 23:40:22 +00002713 }
2714 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002715}
2716
Richard Mitton5cc319a2013-09-19 23:21:01 +00002717struct SymbolCUSorter {
2718 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2719 const MCStreamer &Streamer;
2720
2721 bool operator() (const SymbolCU &A, const SymbolCU &B) {
2722 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2723 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2724
2725 // Symbols with no order assigned should be placed at the end.
2726 // (e.g. section end labels)
2727 if (IA == 0)
2728 IA = (unsigned)(-1);
2729 if (IB == 0)
2730 IB = (unsigned)(-1);
2731 return IA < IB;
2732 }
2733};
2734
Richard Mitton5cc319a2013-09-19 23:21:01 +00002735static bool CUSort(const CompileUnit *A, const CompileUnit *B) {
2736 return (A->getUniqueID() < B->getUniqueID());
2737}
2738
2739struct ArangeSpan {
2740 const MCSymbol *Start, *End;
2741};
2742
2743// Emit a debug aranges section, containing a CU lookup for any
2744// address we can tie back to a CU.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00002745void DwarfDebug::emitDebugARanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002746 // Start the dwarf aranges section.
Eric Christopherab6cd832013-08-30 00:39:57 +00002747 Asm->OutStreamer
2748 .SwitchSection(Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton5cc319a2013-09-19 23:21:01 +00002749
2750 typedef DenseMap<CompileUnit *, std::vector<ArangeSpan> > SpansType;
2751
2752 SpansType Spans;
2753
2754 // Build a list of sections used.
2755 std::vector<const MCSection *> Sections;
2756 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
2757 it++) {
2758 const MCSection *Section = it->first;
2759 Sections.push_back(Section);
2760 }
2761
2762 // Sort the sections into order.
2763 // This is only done to ensure consistent output order across different runs.
2764 std::sort(Sections.begin(), Sections.end(), SectionSort);
2765
2766 // Build a set of address spans, sorted by CU.
2767 for (size_t SecIdx=0;SecIdx<Sections.size();SecIdx++) {
2768 const MCSection *Section = Sections[SecIdx];
2769 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2770 if (List.size() < 2)
2771 continue;
2772
2773 // Sort the symbols by offset within the section.
2774 SymbolCUSorter sorter(Asm->OutStreamer);
2775 std::sort(List.begin(), List.end(), sorter);
2776
2777 // If we have no section (e.g. common), just write out
2778 // individual spans for each symbol.
2779 if (Section == NULL) {
2780 for (size_t n = 0; n < List.size(); n++) {
2781 const SymbolCU &Cur = List[n];
2782
2783 ArangeSpan Span;
2784 Span.Start = Cur.Sym;
2785 Span.End = NULL;
2786 if (Cur.CU)
2787 Spans[Cur.CU].push_back(Span);
2788 }
2789 } else {
2790 // Build spans between each label.
2791 const MCSymbol *StartSym = List[0].Sym;
2792 for (size_t n = 1; n < List.size(); n++) {
2793 const SymbolCU &Prev = List[n - 1];
2794 const SymbolCU &Cur = List[n];
2795
2796 // Try and build the longest span we can within the same CU.
2797 if (Cur.CU != Prev.CU) {
2798 ArangeSpan Span;
2799 Span.Start = StartSym;
2800 Span.End = Cur.Sym;
2801 Spans[Prev.CU].push_back(Span);
2802 StartSym = Cur.Sym;
2803 }
2804 }
2805 }
2806 }
2807
2808 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2809 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2810
2811 // Build a list of CUs used.
2812 std::vector<CompileUnit *> CUs;
2813 for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
2814 CompileUnit *CU = it->first;
2815 CUs.push_back(CU);
2816 }
2817
2818 // Sort the CU list (again, to ensure consistent output order).
2819 std::sort(CUs.begin(), CUs.end(), CUSort);
2820
2821 // Emit an arange table for each CU we used.
2822 for (size_t CUIdx=0;CUIdx<CUs.size();CUIdx++) {
2823 CompileUnit *CU = CUs[CUIdx];
2824 std::vector<ArangeSpan> &List = Spans[CU];
2825
2826 // Emit size of content not including length itself.
2827 unsigned ContentSize
2828 = sizeof(int16_t) // DWARF ARange version number
2829 + sizeof(int32_t) // Offset of CU in the .debug_info section
2830 + sizeof(int8_t) // Pointer Size (in bytes)
2831 + sizeof(int8_t); // Segment Size (in bytes)
2832
2833 unsigned TupleSize = PtrSize * 2;
2834
2835 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
2836 unsigned Padding = 0;
2837 while (((sizeof(int32_t) + ContentSize + Padding) % TupleSize) != 0)
2838 Padding++;
2839
2840 ContentSize += Padding;
2841 ContentSize += (List.size() + 1) * TupleSize;
2842
2843 // For each compile unit, write the list of spans it covers.
2844 Asm->OutStreamer.AddComment("Length of ARange Set");
2845 Asm->EmitInt32(ContentSize);
2846 Asm->OutStreamer.AddComment("DWARF Arange version number");
2847 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2848 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
2849 Asm->EmitSectionOffset(
2850 Asm->GetTempSymbol(ISec->getLabelBeginName(), CU->getUniqueID()),
2851 DwarfInfoSectionSym);
2852 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2853 Asm->EmitInt8(PtrSize);
2854 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2855 Asm->EmitInt8(0);
2856
2857 for (unsigned n = 0; n < Padding; n++)
2858 Asm->EmitInt8(0xff);
2859
2860 for (unsigned n = 0; n < List.size(); n++) {
2861 const ArangeSpan &Span = List[n];
2862 Asm->EmitLabelReference(Span.Start, PtrSize);
2863
2864 // Calculate the size as being from the span start to it's end.
Richard Mittoneb46def2013-09-23 17:56:20 +00002865 if (Span.End) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00002866 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mittoneb46def2013-09-23 17:56:20 +00002867 } else {
2868 // For symbols without an end marker (e.g. common), we
2869 // write a single arange entry containing just that one symbol.
2870 uint64_t Size = SymSize[Span.Start];
2871 if (Size == 0)
2872 Size = 1;
2873
2874 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2875 }
Richard Mitton5cc319a2013-09-19 23:21:01 +00002876 }
2877
2878 Asm->OutStreamer.AddComment("ARange terminator");
2879 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2880 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2881 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002882}
2883
Eric Christopherb6dc8652012-11-27 22:43:45 +00002884// Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002885void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00002886 // Start the dwarf ranges section.
Eric Christopherab6cd832013-08-30 00:39:57 +00002887 Asm->OutStreamer
2888 .SwitchSection(Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00002889 unsigned char Size = Asm->getDataLayout().getPointerSize();
Eric Christopherf7cef702013-03-29 23:34:06 +00002890 for (SmallVectorImpl<const MCSymbol *>::iterator
Jim Grosbach1e20b962010-07-21 21:21:52 +00002891 I = DebugRangeSymbols.begin(), E = DebugRangeSymbols.end();
Devang Pateleac9c072010-04-27 19:46:33 +00002892 I != E; ++I) {
2893 if (*I)
Eric Christopher1ced2082013-01-09 03:52:05 +00002894 Asm->OutStreamer.EmitSymbolValue(const_cast<MCSymbol*>(*I), Size);
Devang Patelf2548ca2010-04-16 23:33:45 +00002895 else
Eric Christopherca1dd052013-01-09 01:35:34 +00002896 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patelf2548ca2010-04-16 23:33:45 +00002897 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002898}
2899
Eric Christopherb6dc8652012-11-27 22:43:45 +00002900// Emit visible names into a debug macinfo section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00002901void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00002902 if (const MCSection *LineInfo =
Chris Lattner18a4c162009-08-02 07:24:22 +00002903 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling94d04b82009-05-20 23:21:38 +00002904 // Start the dwarf macinfo section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00002905 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling94d04b82009-05-20 23:21:38 +00002906 }
2907}
2908
Eric Christopher0b944ee2012-12-11 19:42:09 +00002909// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher98e237f2012-11-30 23:59:06 +00002910
2911// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2912// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
Eric Christopher451c71d2013-10-01 00:43:36 +00002913// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christophera9b2c792013-08-26 23:50:43 +00002914CompileUnit *DwarfDebug::constructSkeletonCU(const CompileUnit *CU) {
Eric Christopher98e237f2012-11-30 23:59:06 +00002915
2916 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christophera9b2c792013-08-26 23:50:43 +00002917 CompileUnit *NewCU = new CompileUnit(CU->getUniqueID(), Die, CU->getNode(),
2918 Asm, this, &SkeletonHolder);
Eric Christopher446b88f2013-01-17 03:00:04 +00002919
Eric Christopher3ce51a92013-02-22 23:50:08 +00002920 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
Eric Christopher30410d52013-08-26 23:57:03 +00002921 DICompileUnit(CU->getNode()).getSplitDebugFilename());
Eric Christopher98e237f2012-11-30 23:59:06 +00002922
Eric Christopher1b0cd2f2013-04-22 07:51:08 +00002923 // Relocate to the beginning of the addr_base section, else 0 for the
2924 // beginning of the one for this compile unit.
Eric Christopher9a9e73b2013-04-07 03:43:09 +00002925 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2926 NewCU->addLabel(Die, dwarf::DW_AT_GNU_addr_base, dwarf::DW_FORM_sec_offset,
2927 DwarfAddrSectionSym);
2928 else
Eric Christopher1b0cd2f2013-04-22 07:51:08 +00002929 NewCU->addUInt(Die, dwarf::DW_AT_GNU_addr_base,
2930 dwarf::DW_FORM_sec_offset, 0);
Eric Christopher98e237f2012-11-30 23:59:06 +00002931
2932 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002933 // into an entity. We're using 0, or a NULL label for this.
Eric Christopher98e237f2012-11-30 23:59:06 +00002934 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Eric Christopher72f7bfb2013-01-15 23:56:56 +00002935
Eric Christopher98e237f2012-11-30 23:59:06 +00002936 // DW_AT_stmt_list is a offset of line number information for this
2937 // compile unit in debug_line section.
Eric Christopher9a9e73b2013-04-07 03:43:09 +00002938 // FIXME: Should handle multiple compile units.
Eric Christopher98e237f2012-11-30 23:59:06 +00002939 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher446b88f2013-01-17 03:00:04 +00002940 NewCU->addLabel(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset,
Eric Christopher7a0103c2013-02-07 21:19:50 +00002941 DwarfLineSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00002942 else
Eric Christopher446b88f2013-01-17 03:00:04 +00002943 NewCU->addUInt(Die, dwarf::DW_AT_stmt_list, dwarf::DW_FORM_sec_offset, 0);
Eric Christopher98e237f2012-11-30 23:59:06 +00002944
2945 if (!CompilationDir.empty())
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002946 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher98e237f2012-11-30 23:59:06 +00002947
Eric Christopher08bd9232013-10-01 00:43:31 +00002948 // Flags to let the linker know we have emitted new style pubnames.
Eric Christophera6d84152013-09-30 23:14:16 +00002949 if (GenerateGnuPubSections) {
2950 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2951 NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubnames, dwarf::DW_FORM_sec_offset,
2952 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
2953 else
2954 NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubnames, dwarf::DW_FORM_data4,
2955 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
2956 DwarfGnuPubNamesSectionSym);
2957
2958 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2959 NewCU->addLabel(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_sec_offset,
2960 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
2961 else
2962 NewCU->addDelta(Die, dwarf::DW_AT_GNU_pubtypes, dwarf::DW_FORM_data4,
Eric Christopher7c9fc902013-10-03 17:41:16 +00002963 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
Eric Christophera6d84152013-09-30 23:14:16 +00002964 DwarfGnuPubTypesSectionSym);
2965 }
Eric Christopher8f1a9292013-09-13 00:35:05 +00002966
Eric Christopher451c71d2013-10-01 00:43:36 +00002967 // Flag if we've emitted any ranges and their location for the compile unit.
2968 if (DebugRangeSymbols.size()) {
2969 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2970 NewCU->addLabel(Die, dwarf::DW_AT_GNU_ranges_base,
2971 dwarf::DW_FORM_sec_offset, DwarfDebugRangeSectionSym);
2972 else
2973 NewCU->addUInt(Die, dwarf::DW_AT_GNU_ranges_base, dwarf::DW_FORM_data4,
2974 0);
2975 }
2976
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002977 SkeletonHolder.addUnit(NewCU);
Eric Christopher01776a52013-02-06 21:53:56 +00002978 SkeletonCUs.push_back(NewCU);
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002979
Eric Christopher98e237f2012-11-30 23:59:06 +00002980 return NewCU;
2981}
2982
Eric Christopher6eebe472012-12-19 22:02:53 +00002983void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
2984 assert(useSplitDwarf() && "No split dwarf debug info?");
2985 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher98e237f2012-11-30 23:59:06 +00002986}
2987
Eric Christopher0b944ee2012-12-11 19:42:09 +00002988// Emit the .debug_info.dwo section for separated dwarf. This contains the
2989// compile units that would normally be in debug_info.
Eric Christopher98e237f2012-11-30 23:59:06 +00002990void DwarfDebug::emitDebugInfoDWO() {
Eric Christopher4daaed12012-12-10 19:51:21 +00002991 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christopherb1e66d02012-12-15 00:04:07 +00002992 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher6eebe472012-12-19 22:02:53 +00002993 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2994 DwarfAbbrevDWOSectionSym);
2995}
2996
2997// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2998// abbreviations for the .debug_info.dwo section.
2999void DwarfDebug::emitDebugAbbrevDWO() {
3000 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher72c16552012-12-20 21:58:40 +00003001 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3002 &Abbreviations);
Eric Christopher98e237f2012-11-30 23:59:06 +00003003}
Eric Christopher64f824c2012-12-27 02:14:01 +00003004
3005// Emit the .debug_str.dwo section for separated dwarf. This contains the
3006// string section and is identical in format to traditional .debug_str
3007// sections.
3008void DwarfDebug::emitDebugStrDWO() {
3009 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherff348452013-01-07 22:40:45 +00003010 const MCSection *OffSec = Asm->getObjFileLowering()
3011 .getDwarfStrOffDWOSection();
Eric Christopherdd8e9f32013-01-07 19:32:41 +00003012 const MCSymbol *StrSym = DwarfStrSectionSym;
3013 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
3014 OffSec, StrSym);
Eric Christopher64f824c2012-12-27 02:14:01 +00003015}