blob: 3e71735d61cf7f24b7d903a1e17e0c277b5c5e4e [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000017#include "DIEHash.h"
Eric Christopher4996c702011-11-07 09:24:32 +000018#include "DwarfAccelTable.h"
David Blaikie2c86a722013-12-02 19:33:15 +000019#include "DwarfUnit.h"
Chandler Carruthed0881b2012-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 Greene829b3e82009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/DIBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-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 Lattnere13c3722010-03-09 01:58:53 +000032#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000033#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000034#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000035#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
David Majnemered89b5c2013-08-21 06:13:34 +000038#include "llvm/Support/Dwarf.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000039#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000040#include "llvm/Support/FormattedStream.h"
Eric Christopher67646432013-07-26 17:02:41 +000041#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000042#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-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 Wendling2f921f82009-05-15 09:23:25 +000050using namespace llvm;
51
Eric Christopher7f2b5512013-07-23 22:16:41 +000052static cl::opt<bool>
53DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
54 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000055
Eric Christopher7f2b5512013-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 Gohman7421ae42010-05-07 01:08:53 +000060
Eric Christopher7f2b5512013-07-23 22:16:41 +000061static cl::opt<bool>
Eric Christopher67646432013-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 Christopherb4bef6d2013-11-19 09:04:36 +000066static cl::opt<bool> GenerateCUHash("generate-cu-hash", cl::Hidden,
67 cl::desc("Add the CU hash as the dwo_id."),
68 cl::init(false));
Eric Christopherd29614f2013-08-13 01:21:55 +000069
Eric Christopherdd1a0122013-09-13 00:35:05 +000070static cl::opt<bool>
71GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
72 cl::desc("Generate GNU-style pubnames and pubtypes"),
73 cl::init(false));
74
Eric Christopher20b76a72012-08-23 22:36:40 +000075namespace {
Eric Christopher7f2b5512013-07-23 22:16:41 +000076enum DefaultOnOff {
77 Default,
78 Enable,
79 Disable
80};
Eric Christopher20b76a72012-08-23 22:36:40 +000081}
Eric Christopher4996c702011-11-07 09:24:32 +000082
Eric Christopher7f2b5512013-07-23 22:16:41 +000083static cl::opt<DefaultOnOff>
84DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
85 cl::desc("Output prototype dwarf accelerator tables."),
86 cl::values(clEnumVal(Default, "Default for platform"),
87 clEnumVal(Enable, "Enabled"),
88 clEnumVal(Disable, "Disabled"), clEnumValEnd),
89 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +000090
Eric Christopher7f2b5512013-07-23 22:16:41 +000091static cl::opt<DefaultOnOff>
Eric Christopher7f2b5512013-07-23 22:16:41 +000092SplitDwarf("split-dwarf", cl::Hidden,
93 cl::desc("Output prototype dwarf split debug info."),
94 cl::values(clEnumVal(Default, "Default for platform"),
95 clEnumVal(Enable, "Enabled"),
96 clEnumVal(Disable, "Disabled"), clEnumValEnd),
97 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +000098
Eric Christopher7da24882013-08-19 21:07:38 +000099static cl::opt<DefaultOnOff>
Eric Christopher4d36ca02013-08-26 23:24:35 +0000100DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
101 cl::desc("Generate DWARF pubnames and pubtypes sections"),
102 cl::values(clEnumVal(Default, "Default for platform"),
103 clEnumVal(Enable, "Enabled"),
104 clEnumVal(Disable, "Disabled"), clEnumValEnd),
105 cl::init(Default));
Eric Christopher7da24882013-08-19 21:07:38 +0000106
Eric Christopher33ff6972013-11-21 23:46:41 +0000107static cl::opt<unsigned>
108DwarfVersionNumber("dwarf-version", cl::Hidden,
109 cl::desc("Generate DWARF for dwarf version."),
110 cl::init(0));
111
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000112static const char *const DWARFGroupName = "DWARF Emission";
113static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000114
Bill Wendling2f921f82009-05-15 09:23:25 +0000115//===----------------------------------------------------------------------===//
116
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000117// Configuration values for initial hash set sizes (log2).
118//
Bill Wendling2f921f82009-05-15 09:23:25 +0000119static const unsigned InitAbbreviationsSetSize = 9; // log2(512)
Bill Wendling2f921f82009-05-15 09:23:25 +0000120
121namespace llvm {
122
Manman Renbe5576f2013-10-08 19:07:44 +0000123/// resolve - Look in the DwarfDebug map for the MDNode that
124/// corresponds to the reference.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000125template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
Manman Renbe5576f2013-10-08 19:07:44 +0000126 return DD->resolve(Ref);
127}
128
Nick Lewycky019d2552011-07-29 03:49:23 +0000129DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000130 DIType Ty = Var.getType();
131 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
132 // addresses instead.
133 if (Var.isBlockByrefVariable()) {
134 /* Byref variables, in Blocks, are declared by the programmer as
135 "SomeType VarName;", but the compiler creates a
136 __Block_byref_x_VarName struct, and gives the variable VarName
137 either the struct, or a pointer to the struct, as its type. This
138 is necessary for various behind-the-scenes things the compiler
139 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000140
Devang Patelf20c4f72011-04-12 22:53:02 +0000141 However, as far as the original *programmer* is concerned, the
142 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000143
Devang Patelf20c4f72011-04-12 22:53:02 +0000144 The following function dives into the __Block_byref_x_VarName
145 struct to find the original type of the variable. This will be
146 passed back to the code generating the type for the Debug
147 Information Entry for the variable 'VarName'. 'VarName' will then
148 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000149
Devang Patelf20c4f72011-04-12 22:53:02 +0000150 The original type 'SomeType' will be the type of the field named
151 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000152
Devang Patelf20c4f72011-04-12 22:53:02 +0000153 NOTE: In order for this to not completely fail on the debugger
154 side, the Debug Information Entry for the variable VarName needs to
155 have a DW_AT_location that tells the debugger how to unwind through
156 the pointers and __Block_byref_x_VarName struct to find the actual
157 value of the variable. The function addBlockByrefType does this. */
158 DIType subType = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000159 uint16_t tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000160
Eric Christopher9adc55f2013-09-04 19:53:21 +0000161 if (tag == dwarf::DW_TAG_pointer_type)
Manman Renbe5576f2013-10-08 19:07:44 +0000162 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher6a841382012-11-19 22:42:10 +0000163
Eric Christopher9adc55f2013-09-04 19:53:21 +0000164 DIArray Elements = DICompositeType(subType).getTypeArray();
Devang Patelf20c4f72011-04-12 22:53:02 +0000165 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie5af2aca2013-11-18 23:57:26 +0000166 DIDerivedType DT(Elements.getElement(i));
Devang Patelf20c4f72011-04-12 22:53:02 +0000167 if (getName() == DT.getName())
Manman Renbe5576f2013-10-08 19:07:44 +0000168 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000169 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000170 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000171 return Ty;
172}
Bill Wendling2f921f82009-05-15 09:23:25 +0000173
Chris Lattnerf5d06362010-04-05 04:09:20 +0000174} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000175
Manman Renac8062b2013-07-02 23:40:10 +0000176/// Return Dwarf Version by checking module flags.
177static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Ren8bfde892013-07-16 23:21:16 +0000178 Value *Val = M->getModuleFlag("Dwarf Version");
179 if (!Val)
Eric Christophere31e0722013-09-04 22:21:24 +0000180 return dwarf::DWARF_VERSION;
Manman Ren8bfde892013-07-16 23:21:16 +0000181 return cast<ConstantInt>(Val)->getZExtValue();
Manman Renac8062b2013-07-02 23:40:10 +0000182}
183
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000184DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000185 : Asm(A), MMI(Asm->MMI), FirstCU(0),
186 AbbreviationsSet(InitAbbreviationsSetSize),
187 SourceIdMap(DIEValueAllocator), PrevLabel(NULL), GlobalCUIndexCount(0),
188 InfoHolder(A, &AbbreviationsSet, Abbreviations, "info_string",
189 DIEValueAllocator),
190 SkeletonAbbrevSet(InitAbbreviationsSetSize),
191 SkeletonHolder(A, &SkeletonAbbrevSet, SkeletonAbbrevs, "skel_string",
192 DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000193
Rafael Espindolaa7160962011-05-06 14:56:22 +0000194 DwarfInfoSectionSym = DwarfAbbrevSectionSym = 0;
Chris Lattnere58b5472010-04-04 23:10:38 +0000195 DwarfStrSectionSym = TextSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000196 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher55863be2013-04-07 03:43:09 +0000197 DwarfAddrSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000198 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000199 FunctionBeginSym = FunctionEndSym = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000200
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000201 // Turn on accelerator tables for Darwin by default, pubnames by
202 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000203 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000204
Eric Christopher574b5c82013-08-19 21:41:38 +0000205 if (DwarfAccelTables == Default)
206 HasDwarfAccelTables = IsDarwin;
207 else
Eric Christopher5297df02013-08-26 20:58:35 +0000208 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000209
Eric Christophercdf218d2012-12-10 19:51:21 +0000210 if (SplitDwarf == Default)
211 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000212 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000213 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000214
Eric Christopher4d36ca02013-08-26 23:24:35 +0000215 if (DwarfPubSections == Default)
216 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000217 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000218 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000219
Eric Christopher33ff6972013-11-21 23:46:41 +0000220 DwarfVersion = DwarfVersionNumber
221 ? DwarfVersionNumber
222 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000223
Dan Gohman6e681a52010-06-18 15:56:31 +0000224 {
225 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000226 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000227 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000228}
Bill Wendling2f921f82009-05-15 09:23:25 +0000229
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000230// Switch to the specified MCSection and emit an assembler
231// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000232static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000233 const char *SymbolStem = 0) {
234 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000235 if (!SymbolStem)
236 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000237
238 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
239 Asm->OutStreamer.EmitLabel(TmpSym);
240 return TmpSym;
241}
242
David Blaikie72f1a3e2013-11-23 01:17:34 +0000243DwarfUnits::~DwarfUnits() {
David Blaikie319a05f2013-12-02 19:33:10 +0000244 for (SmallVectorImpl<Unit *>::iterator I = CUs.begin(), E = CUs.end(); I != E;
245 ++I)
David Blaikie72f1a3e2013-11-23 01:17:34 +0000246 delete *I;
247}
248
Eric Christophere698f532012-12-20 21:58:36 +0000249MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000250 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000251}
252
Eric Christophere698f532012-12-20 21:58:36 +0000253MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000254 std::pair<MCSymbol *, unsigned> &Entry =
255 StringPool.GetOrCreateValue(Str).getValue();
256 if (Entry.first)
257 return Entry.first;
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000258
259 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000260 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000261}
262
Eric Christopher2cbd5762013-01-07 19:32:41 +0000263unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000264 std::pair<MCSymbol *, unsigned> &Entry =
265 StringPool.GetOrCreateValue(Str).getValue();
266 if (Entry.first)
267 return Entry.second;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000268
269 Entry.second = NextStringPoolNumber++;
270 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
271 return Entry.second;
272}
273
David Blaikiedea547b2013-06-28 18:47:14 +0000274unsigned DwarfUnits::getAddrPoolIndex(const MCSymbol *Sym) {
David Blaikie8466ca82013-07-01 23:55:52 +0000275 return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
276}
277
Ulrich Weigand8b3d2262013-07-02 18:46:46 +0000278unsigned DwarfUnits::getAddrPoolIndex(const MCExpr *Sym) {
279 std::pair<DenseMap<const MCExpr *, unsigned>::iterator, bool> P =
David Blaikieb8ef7852013-06-28 18:47:19 +0000280 AddressPool.insert(std::make_pair(Sym, NextAddrPoolNumber));
David Blaikiea67de2b2013-06-28 18:55:13 +0000281 if (P.second)
282 ++NextAddrPoolNumber;
David Blaikieb8ef7852013-06-28 18:47:19 +0000283 return P.first->second;
Eric Christopher962c9082013-01-15 23:56:56 +0000284}
285
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000286// Define a unique number for the abbreviation.
287//
Eric Christopherc8a310e2012-12-10 23:34:43 +0000288void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000289 // Check the set for priors.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000290 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000291
292 // If it's newly added.
293 if (InSet == &Abbrev) {
294 // Add to abbreviation list.
David Blaikie2d4e1122013-10-30 17:14:24 +0000295 Abbreviations.push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000296
297 // Assign the vector position + 1 as its number.
David Blaikie2d4e1122013-10-30 17:14:24 +0000298 Abbrev.setNumber(Abbreviations.size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000299 } else {
300 // Assign existing abbreviation number.
301 Abbrev.setNumber(InSet->getNumber());
302 }
303}
304
Eric Christopherd9843b32011-11-10 19:25:34 +0000305static bool isObjCClass(StringRef Name) {
306 return Name.startswith("+") || Name.startswith("-");
307}
308
309static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000310 if (!isObjCClass(Name))
311 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000312
Benjamin Kramer260de742013-08-24 12:15:54 +0000313 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000314}
315
316static void getObjCClassCategory(StringRef In, StringRef &Class,
317 StringRef &Category) {
318 if (!hasObjCCategory(In)) {
319 Class = In.slice(In.find('[') + 1, In.find(' '));
320 Category = "";
321 return;
322 }
323
324 Class = In.slice(In.find('[') + 1, In.find('('));
325 Category = In.slice(In.find('[') + 1, In.find(' '));
326 return;
327}
328
329static StringRef getObjCMethodName(StringRef In) {
330 return In.slice(In.find(' ') + 1, In.find(']'));
331}
332
Richard Mittonc2508242013-10-03 22:07:08 +0000333// Helper for sorting sections into a stable output order.
334static bool SectionSort(const MCSection *A, const MCSection *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000335 std::string LA = (A ? A->getLabelBeginName() : "");
336 std::string LB = (B ? B->getLabelBeginName() : "");
337 return LA < LB;
Richard Mittonc2508242013-10-03 22:07:08 +0000338}
339
Eric Christopherd9843b32011-11-10 19:25:34 +0000340// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000341// TODO: Determine whether or not we should add names for programs
342// that do not have a DW_AT_name or DW_AT_linkage_name field - this
343// is only slightly different than the lookup of non-standard ObjC names.
David Blaikie319a05f2013-12-02 19:33:10 +0000344static void addSubprogramNames(Unit *TheCU, DISubprogram SP, DIE *Die) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000345 if (!SP.isDefinition())
346 return;
Eric Christopherd9843b32011-11-10 19:25:34 +0000347 TheCU->addAccelName(SP.getName(), Die);
348
349 // If the linkage name is different than the name, go ahead and output
350 // that as well into the name table.
351 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
352 TheCU->addAccelName(SP.getLinkageName(), Die);
353
354 // If this is an Objective-C selector name add it to the ObjC accelerator
355 // too.
356 if (isObjCClass(SP.getName())) {
357 StringRef Class, Category;
358 getObjCClassCategory(SP.getName(), Class, Category);
359 TheCU->addAccelObjC(Class, Die);
360 if (Category != "")
361 TheCU->addAccelObjC(Category, Die);
362 // Also add the base method name to the name table.
363 TheCU->addAccelName(getObjCMethodName(SP.getName()), Die);
364 }
365}
366
Manman Ren3eb9dff2013-09-09 19:05:21 +0000367/// isSubprogramContext - Return true if Context is either a subprogram
368/// or another context nested inside a subprogram.
369bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
370 if (!Context)
371 return false;
372 DIDescriptor D(Context);
373 if (D.isSubprogram())
374 return true;
375 if (D.isType())
Manman Ren116868e2013-09-09 19:47:11 +0000376 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000377 return false;
378}
379
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000380// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
381// and DW_AT_high_pc attributes. If there are global variables in this
382// scope then create and insert DIEs for these variables.
David Blaikie25bc7192013-11-15 23:13:08 +0000383DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU, DISubprogram SP) {
384 DIE *SPDie = SPCU->getDIE(SP);
Devang Patela37a95e2010-07-07 22:20:57 +0000385
Chris Lattner3a383cb2010-04-05 00:13:49 +0000386 assert(SPDie && "Unable to find subprogram DIE!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000387
Bill Wendlingf720bf62012-11-07 05:19:04 +0000388 // If we're updating an abstract DIE, then we will be adding the children and
389 // object pointer later on. But what we don't want to do is process the
390 // concrete DIE twice.
David Blaikie25bc7192013-11-15 23:13:08 +0000391 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000392 // Pick up abstract subprogram DIE.
Manman Ren4a841a82013-10-29 01:03:01 +0000393 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getCUDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000394 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000395 } else {
396 DISubprogram SPDecl = SP.getFunctionDeclaration();
397 if (!SPDecl.isSubprogram()) {
398 // There is not any need to generate specification DIE for a function
399 // defined at compile unit level. If a function is defined inside another
400 // function then gdb prefers the definition at top level and but does not
401 // expect specification DIE in parent function. So avoid creating
402 // specification DIE for a function defined inside a function.
Manman Renc50fa112013-10-10 18:40:01 +0000403 DIScope SPContext = resolve(SP.getContext());
404 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000405 !SPContext.isFile() && !isSubprogramContext(SPContext)) {
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000406 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
407
408 // Add arguments.
409 DICompositeType SPTy = SP.getType();
410 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000411 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000412 if (SPTag == dwarf::DW_TAG_subroutine_type)
413 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Ren4a841a82013-10-29 01:03:01 +0000414 DIE *Arg =
415 SPCU->createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie5af2aca2013-11-18 23:57:26 +0000416 DIType ATy(Args.getElement(i));
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000417 SPCU->addType(Arg, ATy);
418 if (ATy.isArtificial())
419 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
420 if (ATy.isObjectPointer())
Manman Ren4c4b69c2013-10-11 23:58:05 +0000421 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, Arg);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000422 }
423 DIE *SPDeclDie = SPDie;
Manman Ren4a841a82013-10-29 01:03:01 +0000424 SPDie =
425 SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getCUDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000426 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000427 }
428 }
Devang Patela37a95e2010-07-07 22:20:57 +0000429 }
430
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000431 SPCU->addLabelAddress(
432 SPDie, dwarf::DW_AT_low_pc,
433 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber()));
434 SPCU->addLabelAddress(
435 SPDie, dwarf::DW_AT_high_pc,
436 Asm->GetTempSymbol("func_end", Asm->getFunctionNumber()));
Chris Lattner3a383cb2010-04-05 00:13:49 +0000437 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
438 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000439 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000440
Eric Christopherd9843b32011-11-10 19:25:34 +0000441 // Add name to the name table, we do this here because we're guaranteed
442 // to have concrete versions of our DW_TAG_subprogram nodes.
443 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000444
Chris Lattner3a383cb2010-04-05 00:13:49 +0000445 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000446}
447
Manman Ren5b2f4b02013-09-11 19:40:28 +0000448/// Check whether we should create a DIE for the given Scope, return true
449/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000450bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
451 if (Scope->isAbstractScope())
452 return false;
453
Manman Ren5b2f4b02013-09-11 19:40:28 +0000454 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000455 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
456 if (Ranges.empty())
457 return true;
458
459 if (Ranges.size() > 1)
460 return false;
461
Manman Ren5b2f4b02013-09-11 19:40:28 +0000462 // We don't create a DIE if we have a single Range and the end label
463 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000464 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
465 MCSymbol *End = getLabelAfterInsn(RI->second);
466 return !End;
467}
468
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000469// Construct new DW_TAG_lexical_block for this scope and attach
470// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher6a841382012-11-19 22:42:10 +0000471DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000472 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000473 if (isLexicalScopeDIENull(Scope))
474 return 0;
475
Devang Patel6c74a872010-04-27 19:46:33 +0000476 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
477 if (Scope->isAbstractScope())
478 return ScopeDIE;
479
Craig Topper977e9cd2013-07-03 04:24:43 +0000480 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
Eric Christopherdc42ea82013-07-03 01:57:28 +0000481 // If we have multiple ranges, emit them into the range section.
Devang Patel6c74a872010-04-27 19:46:33 +0000482 if (Ranges.size() > 1) {
483 // .debug_range section has not been laid out yet. Emit offset in
Eric Christopher4751d702013-11-23 00:05:29 +0000484 // .debug_range as a relocatable label. emitDIE will handle
485 // emitting it appropriately.
486 unsigned Offset = DebugRangeSymbols.size();
487 TheCU->addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges,
488 Asm->GetTempSymbol("debug_ranges", Offset));
Craig Topperd8e43652013-07-03 04:17:25 +0000489 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000490 RE = Ranges.end();
491 RI != RE; ++RI) {
Devang Patel9fc11702010-05-25 23:40:22 +0000492 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
493 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
Devang Patel6c74a872010-04-27 19:46:33 +0000494 }
Eric Christopherc1110832013-07-03 01:22:29 +0000495
496 // Terminate the range list.
Devang Patel6c74a872010-04-27 19:46:33 +0000497 DebugRangeSymbols.push_back(NULL);
498 DebugRangeSymbols.push_back(NULL);
499 return ScopeDIE;
500 }
501
Eric Christopherdc42ea82013-07-03 01:57:28 +0000502 // Construct the address range for this DIE.
Craig Topperd8e43652013-07-03 04:17:25 +0000503 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000504 MCSymbol *Start = getLabelBeforeInsn(RI->first);
505 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000506 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000507
Chris Lattnere13c3722010-03-09 01:58:53 +0000508 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
509 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000510
Eric Christopher962c9082013-01-15 23:56:56 +0000511 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
512 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000513
514 return ScopeDIE;
515}
516
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000517// This scope represents inlined body of a function. Construct DIE to
518// represent this concrete inlined copy of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000519DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
520 LexicalScope *Scope) {
Craig Topper977e9cd2013-07-03 04:24:43 +0000521 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000522 assert(Ranges.empty() == false &&
523 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000524
Devang Patelf098ce22011-07-27 00:34:13 +0000525 if (!Scope->getScopeNode())
526 return NULL;
527 DIScope DS(Scope->getScopeNode());
528 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000529 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000530 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000531 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000532 return NULL;
533 }
534
Devang Patel73bc1722011-05-05 17:54:26 +0000535 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000536 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000537
Devang Patelf098ce22011-07-27 00:34:13 +0000538 if (Ranges.size() > 1) {
539 // .debug_range section has not been laid out yet. Emit offset in
Eric Christopher4751d702013-11-23 00:05:29 +0000540 // .debug_range as a relocatable label. emitDIE will handle
541 // emitting it appropriately.
542 unsigned Offset = DebugRangeSymbols.size();
543 TheCU->addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges,
544 Asm->GetTempSymbol("debug_ranges", Offset));
Craig Topperd8e43652013-07-03 04:17:25 +0000545 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000546 RE = Ranges.end();
547 RI != RE; ++RI) {
Devang Patelf098ce22011-07-27 00:34:13 +0000548 DebugRangeSymbols.push_back(getLabelBeforeInsn(RI->first));
549 DebugRangeSymbols.push_back(getLabelAfterInsn(RI->second));
550 }
551 DebugRangeSymbols.push_back(NULL);
552 DebugRangeSymbols.push_back(NULL);
553 } else {
Craig Topperd8e43652013-07-03 04:17:25 +0000554 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000555 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
556 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
557
558 if (StartLabel == 0 || EndLabel == 0)
559 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
560
561 assert(StartLabel->isDefined() &&
562 "Invalid starting label for an inlined scope!");
563 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
564
Eric Christopher962c9082013-01-15 23:56:56 +0000565 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
566 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000567 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000568
569 InlinedSubprogramDIEs.insert(OriginDIE);
570
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000571 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000572 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000573 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000574 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
575 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000576 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000577
Eric Christopher8dda5d02011-12-04 06:02:38 +0000578 // Add name to the name table, we do this here because we're guaranteed
579 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
580 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000581
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000582 return ScopeDIE;
583}
584
Manman Ren2312ed32013-09-10 18:40:41 +0000585DIE *DwarfDebug::createScopeChildrenDIE(CompileUnit *TheCU, LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000586 SmallVectorImpl<DIE *> &Children) {
587 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000588
589 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000590 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000591 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
592 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000593 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000594 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000595 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000596 if (ArgDV->isObjectPointer())
597 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000598 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000599
Eric Christopherf84354b2011-10-03 15:49:16 +0000600 // Collect lexical scope children first.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000601 const SmallVectorImpl<DbgVariable *> &Variables =
602 ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000603 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000604 if (DIE *Variable = TheCU->constructVariableDIE(*Variables[i],
605 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000606 Children.push_back(Variable);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000607 if (Variables[i]->isObjectPointer())
608 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000609 }
Craig Topper977e9cd2013-07-03 04:24:43 +0000610 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000611 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000612 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000613 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000614 return ObjectPointer;
615}
616
617// Construct a DIE for this scope.
618DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
619 if (!Scope || !Scope->getScopeNode())
620 return NULL;
621
622 DIScope DS(Scope->getScopeNode());
623
624 SmallVector<DIE *, 8> Children;
625 DIE *ObjectPointer = NULL;
626 bool ChildrenCreated = false;
627
Manman Ren5b2f4b02013-09-11 19:40:28 +0000628 // We try to create the scope DIE first, then the children DIEs. This will
629 // avoid creating un-used children then removing them later when we find out
630 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000631 DIE *ScopeDIE = NULL;
632 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000633 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000634 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000635 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000636 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000637 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000638 // Note down abstract DIE.
639 if (ScopeDIE)
640 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000641 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000642 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000643 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000644 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000645 if (isLexicalScopeDIENull(Scope))
646 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000647
648 // We create children here when we know the scope DIE is not going to be
649 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000650 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
651 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000652
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000653 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000654 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000655 ImportedEntityMap::const_iterator> Range =
656 std::equal_range(
657 ScopesWithImportedEntities.begin(),
658 ScopesWithImportedEntities.end(),
659 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
660 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000661 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000662 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000663 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000664 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000665 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
666 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000667 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000668 }
Eric Christopher6a841382012-11-19 22:42:10 +0000669
Benjamin Kramer892daba2013-08-24 11:55:49 +0000670 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000671 assert(Children.empty() &&
672 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000673 return NULL;
674 }
Manman Ren2312ed32013-09-10 18:40:41 +0000675 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000676 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000677 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000678
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000679 // Add children
Craig Topperd8e43652013-07-03 04:17:25 +0000680 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000681 E = Children.end();
682 I != E; ++I)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000683 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000684
Eric Christophere3417762012-09-12 23:36:19 +0000685 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000686 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000687
Eric Christopherd9843b32011-11-10 19:25:34 +0000688 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000689}
690
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000691// Look up the source id with the given directory and source file names.
692// If none currently exists, create a new id and insert it in the
693// SourceIds map. This can update DirectoryNames and SourceFileNames maps
694// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000695unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
696 unsigned CUID) {
Manman Ren1e427202013-03-07 01:42:00 +0000697 // If we use .loc in assembly, we can't separate .file entries according to
698 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000699
700 // FIXME: add a better feature test than hasRawTextSupport. Even better,
701 // extend .file to support this.
702 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000703 CUID = 0;
704
Devang Patel871d0b12010-09-16 20:57:49 +0000705 // If FE did not provide a file name, then assume stdin.
706 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000707 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000708
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000709 // TODO: this might not belong here. See if we can factor this better.
710 if (DirName == CompilationDir)
711 DirName = "";
712
Manman Ren1e427202013-03-07 01:42:00 +0000713 // FileIDCUMap stores the current ID for the given compile unit.
714 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000715
Manman Ren1e427202013-03-07 01:42:00 +0000716 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000717 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000718 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000719 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000720 NamePair += DirName;
721 NamePair += '\0'; // Zero bytes are not allowed in paths.
722 NamePair += FileName;
723
724 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
725 if (Ent.getValue() != SrcId)
726 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000727
Manman Ren1e427202013-03-07 01:42:00 +0000728 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000729 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000730 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000731
732 return SrcId;
733}
734
Eric Christopher48fef592012-12-20 21:58:40 +0000735// Create new CompileUnit for the given metadata node with tag
736// DW_TAG_compile_unit.
David Blaikie5a152402013-11-15 23:52:02 +0000737CompileUnit *DwarfDebug::constructCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000738 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000739 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000740
741 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikie5a152402013-11-15 23:52:02 +0000742 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++, Die, DIUnit, Asm,
743 this, &InfoHolder);
Manman Ren1e427202013-03-07 01:42:00 +0000744
745 FileIDCUMap[NewCU->getUniqueID()] = 0;
746 // Call this to emit a .file directive if it wasn't emitted for the source
747 // file this CU comes from yet.
748 getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
749
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000750 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000751 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
752 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000753 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000754
Eric Christopherb1b94512012-08-01 18:19:01 +0000755 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher52ce7182013-04-09 19:23:15 +0000756 // into an entity. We're using 0 (or a NULL label) for this. For
757 // split dwarf it's in the skeleton CU so omit it here.
758 if (!useSplitDwarf())
759 NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
Manman Ren4e042a62013-02-05 21:52:47 +0000760
761 // Define start line table label for each Compile Unit.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000762 MCSymbol *LineTableStartSym =
763 Asm->GetTempSymbol("line_table_start", NewCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +0000764 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
765 NewCU->getUniqueID());
766
Manman Ren9d4c7352013-05-21 00:57:22 +0000767 // Use a single line table if we are using .loc and generating assembly.
768 bool UseTheFirstCU =
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000769 (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport()) ||
770 (NewCU->getUniqueID() == 0);
Manman Ren9d4c7352013-05-21 00:57:22 +0000771
Eric Christopher52ce7182013-04-09 19:23:15 +0000772 if (!useSplitDwarf()) {
Eric Christophera51d3fc2013-09-27 22:50:48 +0000773 // DW_AT_stmt_list is a offset of line number information for this
774 // compile unit in debug_line section. For split dwarf this is
775 // left in the skeleton CU and so not included.
776 // The line table entries are not always emitted in assembly, so it
777 // is not okay to use line_table_start here.
Eric Christopher52ce7182013-04-09 19:23:15 +0000778 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000779 NewCU->addSectionLabel(
780 Die, dwarf::DW_AT_stmt_list,
781 UseTheFirstCU ? Asm->GetTempSymbol("section_line")
782 : LineTableStartSym);
Manman Ren9d4c7352013-05-21 00:57:22 +0000783 else if (UseTheFirstCU)
Eric Christopher33ff6972013-11-21 23:46:41 +0000784 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher52ce7182013-04-09 19:23:15 +0000785 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000786 NewCU->addSectionDelta(Die, dwarf::DW_AT_stmt_list,
787 LineTableStartSym, DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000788
789 // If we're using split dwarf the compilation dir is going to be in the
790 // skeleton CU and so we don't need to duplicate it here.
791 if (!CompilationDir.empty())
792 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
793
Eric Christopher1d06eb52013-10-01 00:43:31 +0000794 // Flags to let the linker know we have emitted new style pubnames. Only
Eric Christophera51d3fc2013-09-27 22:50:48 +0000795 // emit it here if we don't have a skeleton CU for split dwarf.
Eric Christopher39eebfa2013-09-30 23:14:16 +0000796 if (GenerateGnuPubSections) {
797 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000798 NewCU->addSectionLabel(
799 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000800 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000801 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000802 NewCU->addSectionDelta(
803 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000804 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
805 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000806
807 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000808 NewCU->addSectionLabel(
809 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000810 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000811 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000812 NewCU->addSectionDelta(
813 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000814 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
815 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000816 }
Eric Christopher52ce7182013-04-09 19:23:15 +0000817 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000818
Bill Wendling2b128d72009-05-20 23:19:06 +0000819 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000820 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000821
Devang Patel2d9caf92009-11-25 17:36:49 +0000822 StringRef Flags = DIUnit.getFlags();
823 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000824 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000825
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000826 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000827 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000828 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000829
Devang Patel1a0df9a2010-05-10 22:49:55 +0000830 if (!FirstCU)
831 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000832
Eric Christopherc8a310e2012-12-10 23:34:43 +0000833 InfoHolder.addUnit(NewCU);
834
David Blaikie5a152402013-11-15 23:52:02 +0000835 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000836 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000837 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000838}
839
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000840// Construct subprogram DIE.
Eric Christophera6c38a32013-10-15 23:31:38 +0000841void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000842 // FIXME: We should only call this routine once, however, during LTO if a
843 // program is defined in multiple CUs we could end up calling it out of
844 // beginModule as we walk the CUs.
845
846 CompileUnit *&CURef = SPMap[N];
847 if (CURef)
848 return;
849 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000850
Devang Patel80ae3492009-08-28 23:24:31 +0000851 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000852 if (!SP.isDefinition())
853 // This is a method declaration which will be handled while constructing
854 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000855 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000856
Devang Patel89543712011-08-15 17:24:54 +0000857 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000858
Eric Christopherba506db2013-09-09 20:03:20 +0000859 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000860 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000861}
862
David Blaikie1fd43652013-05-07 21:35:53 +0000863void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000864 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000865 DIImportedEntity Module(N);
David Blaikief55abea2013-04-22 06:12:31 +0000866 if (!Module.Verify())
867 return;
David Blaikie684fc532013-05-06 23:33:07 +0000868 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000869 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000870}
871
David Blaikie4dd2de72013-05-08 06:01:38 +0000872void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
David Blaikie684fc532013-05-06 23:33:07 +0000873 DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000874 DIImportedEntity Module(N);
David Blaikie684fc532013-05-06 23:33:07 +0000875 if (!Module.Verify())
876 return;
David Blaikie4dd2de72013-05-08 06:01:38 +0000877 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000878}
879
David Blaikie4dd2de72013-05-08 06:01:38 +0000880void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000881 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000882 DIE *Context) {
883 assert(Module.Verify() &&
884 "Use one of the MDNode * overloads to handle invalid metadata");
885 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000886 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000887 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000888 DIE *EntityDie;
889 DIDescriptor Entity = Module.getEntity();
890 if (Entity.isNameSpace())
891 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
892 else if (Entity.isSubprogram())
893 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000894 else if (Entity.isType())
895 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000896 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000897 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000898 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
899 Module.getContext().getDirectory(),
900 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000901 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
902 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000903 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000904 StringRef Name = Module.getName();
905 if (!Name.empty())
906 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000907 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000908}
909
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000910// Emit all Dwarf sections that should come prior to the content. Create
911// global DIEs and emit initial debug info sections. This is invoked by
912// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000913void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000914 if (DisableDebugInfoPrinting)
915 return;
916
Eric Christopher58f41952012-11-19 22:42:15 +0000917 const Module *M = MMI->getModule();
918
Nick Lewycky019d2552011-07-29 03:49:23 +0000919 // If module has named metadata anchors then use them, otherwise scan the
920 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000921 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000922 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000923 return;
Manman Ren60352032013-09-05 18:48:31 +0000924 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000925
David Blaikiedc69ebb2013-03-11 23:39:23 +0000926 // Emit initial sections so we can reference labels later.
927 emitSectionLabels();
928
929 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
930 DICompileUnit CUNode(CU_Nodes->getOperand(i));
931 CompileUnit *CU = constructCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000932 DIArray ImportedEntities = CUNode.getImportedEntities();
933 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000934 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000935 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
936 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000937 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000938 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000939 DIArray GVs = CUNode.getGlobalVariables();
940 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000941 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000942 DIArray SPs = CUNode.getSubprograms();
943 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
944 constructSubprogramDIE(CU, SPs.getElement(i));
945 DIArray EnumTypes = CUNode.getEnumTypes();
946 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
947 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
948 DIArray RetainedTypes = CUNode.getRetainedTypes();
949 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
950 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000951 // Emit imported_modules last so that the relevant context is already
952 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000953 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
954 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000955 }
Eric Christopher6a841382012-11-19 22:42:10 +0000956
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000957 // Tell MMI that we have debug info.
958 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000959
Bill Wendling2b128d72009-05-20 23:19:06 +0000960 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000961 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000962}
963
Eric Christopher960ac372012-11-22 00:59:49 +0000964// Attach DW_AT_inline attribute with inlined subprogram DIEs.
965void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000966 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
967 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000968 AE = InlinedSubprogramDIEs.end();
969 AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000970 DIE *ISP = *AI;
David Blaikief2443192013-10-21 17:28:37 +0000971 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000972 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000973 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000974 AE = AbstractSPDies.end();
975 AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000976 DIE *ISP = AI->second;
977 if (InlinedSubprogramDIEs.count(ISP))
978 continue;
David Blaikief2443192013-10-21 17:28:37 +0000979 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000980 }
Eric Christopher960ac372012-11-22 00:59:49 +0000981}
982
983// Collect info for variables that were optimized out.
984void DwarfDebug::collectDeadVariables() {
985 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000986
987 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
988 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
989 DICompileUnit TheCU(CU_Nodes->getOperand(i));
990 DIArray Subprograms = TheCU.getSubprograms();
991 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000992 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000993 if (ProcessedSPNodes.count(SP) != 0)
994 continue;
995 if (!SP.isSubprogram())
996 continue;
997 if (!SP.isDefinition())
998 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000999 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +00001000 if (Variables.getNumElements() == 0)
1001 continue;
Eric Christopher960ac372012-11-22 00:59:49 +00001002
Eric Christopher735401c2012-11-27 00:13:51 +00001003 // Construct subprogram DIE and add variables DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +00001004 CompileUnit *SPCU = static_cast<CompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +00001005 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +00001006 // FIXME: See the comment in constructSubprogramDIE about duplicate
1007 // subprogram DIEs.
1008 constructSubprogramDIE(SPCU, SP);
1009 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +00001010 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
1011 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +00001012 if (!DV.isVariable())
1013 continue;
Manman Renb3388602013-10-05 01:43:03 +00001014 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001015 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +00001016 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +00001017 }
Eric Christopher960ac372012-11-22 00:59:49 +00001018 }
1019 }
1020 }
Eric Christopher960ac372012-11-22 00:59:49 +00001021}
1022
Eric Christopher45731982013-08-08 23:45:55 +00001023// Type Signature [7.27] and ODR Hash code.
Eric Christopher67646432013-07-26 17:02:41 +00001024
1025/// \brief Grabs the string in whichever attribute is passed in and returns
Eric Christopher8552e222013-08-07 01:18:33 +00001026/// a reference to it. Returns "" if the attribute doesn't exist.
Eric Christopher67646432013-07-26 17:02:41 +00001027static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
Eric Christopher8552e222013-08-07 01:18:33 +00001028 DIEValue *V = Die->findAttribute(Attr);
Eric Christopher67646432013-07-26 17:02:41 +00001029
Eric Christopher8552e222013-08-07 01:18:33 +00001030 if (DIEString *S = dyn_cast_or_null<DIEString>(V))
1031 return S->getString();
1032
Eric Christopher67646432013-07-26 17:02:41 +00001033 return StringRef("");
1034}
1035
Eric Christopher67646432013-07-26 17:02:41 +00001036/// Return true if the current DIE is contained within an anonymous namespace.
1037static bool isContainedInAnonNamespace(DIE *Die) {
1038 DIE *Parent = Die->getParent();
1039
1040 while (Parent) {
Eric Christophere414ece2013-07-29 23:53:08 +00001041 if (Parent->getTag() == dwarf::DW_TAG_namespace &&
1042 getDIEStringAttr(Parent, dwarf::DW_AT_name) == "")
Eric Christopher67646432013-07-26 17:02:41 +00001043 return true;
1044 Parent = Parent->getParent();
1045 }
1046
1047 return false;
1048}
1049
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001050/// Test if the current CU language is C++ and that we have
1051/// a named type that is not contained in an anonymous namespace.
David Blaikie319a05f2013-12-02 19:33:10 +00001052static bool shouldAddODRHash(TypeUnit *CU, DIE *Die) {
Eric Christopher341770d2013-08-07 08:35:10 +00001053 return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
1054 getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
1055 !isContainedInAnonNamespace(Die);
Eric Christopher45731982013-08-08 23:45:55 +00001056}
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001057
Eric Christopher960ac372012-11-22 00:59:49 +00001058void DwarfDebug::finalizeModuleInfo() {
1059 // Collect info for variables that were optimized out.
1060 collectDeadVariables();
1061
1062 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1063 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001064
Eric Christopher60eb7692013-08-12 20:27:48 +00001065 // Handle anything that needs to be done on a per-cu basis.
David Blaikie319a05f2013-12-02 19:33:10 +00001066 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
1067 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00001068 I != E; ++I) {
David Blaikie319a05f2013-12-02 19:33:10 +00001069 Unit *TheCU = *I;
Eric Christopher60eb7692013-08-12 20:27:48 +00001070 // Emit DW_AT_containing_type attribute to connect types with their
1071 // vtable holding type.
1072 TheCU->constructContainingTypeDIEs();
1073
1074 // If we're splitting the dwarf out now that we've got the entire
1075 // CU then construct a skeleton CU based upon it.
David Blaikie319a05f2013-12-02 19:33:10 +00001076 if (useSplitDwarf() &&
1077 TheCU->getCUDie()->getTag() == dwarf::DW_TAG_compile_unit) {
Eric Christopherd29614f2013-08-13 01:21:55 +00001078 uint64_t ID = 0;
1079 if (GenerateCUHash) {
1080 DIEHash CUHash;
David Blaikie2aee7be2013-10-24 18:29:03 +00001081 ID = CUHash.computeCUSignature(*TheCU->getCUDie());
Eric Christopherd29614f2013-08-13 01:21:55 +00001082 }
Eric Christopher60eb7692013-08-12 20:27:48 +00001083 // This should be a unique identifier when we want to build .dwp files.
1084 TheCU->addUInt(TheCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopherd29614f2013-08-13 01:21:55 +00001085 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001086 // Now construct the skeleton CU associated.
David Blaikie319a05f2013-12-02 19:33:10 +00001087 CompileUnit *SkCU =
1088 constructSkeletonCU(static_cast<CompileUnit *>(TheCU));
Eric Christopher60eb7692013-08-12 20:27:48 +00001089 // This should be a unique identifier when we want to build .dwp files.
1090 SkCU->addUInt(SkCU->getCUDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopherd29614f2013-08-13 01:21:55 +00001091 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001092 }
1093 }
1094
1095 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001096 InfoHolder.computeSizeAndOffsets();
1097 if (useSplitDwarf())
1098 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001099}
1100
1101void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001102 // Filter labels by section.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001103 for (size_t n = 0; n < ArangeLabels.size(); n++) {
1104 const SymbolCU &SCU = ArangeLabels[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001105 if (SCU.Sym->isInSection()) {
1106 // Make a note of this symbol and it's section.
1107 const MCSection *Section = &SCU.Sym->getSection();
1108 if (!Section->getKind().isMetadata())
1109 SectionMap[Section].push_back(SCU);
1110 } else {
1111 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1112 // appear in the output. This sucks as we rely on sections to build
1113 // arange spans. We can do it without, but it's icky.
1114 SectionMap[NULL].push_back(SCU);
1115 }
1116 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001117
Richard Mittonc2508242013-10-03 22:07:08 +00001118 // Build a list of sections used.
1119 std::vector<const MCSection *> Sections;
Richard Mitton21101b32013-09-19 23:21:01 +00001120 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
1121 it++) {
1122 const MCSection *Section = it->first;
Richard Mittonc2508242013-10-03 22:07:08 +00001123 Sections.push_back(Section);
1124 }
1125
1126 // Sort the sections into order.
1127 // This is only done to ensure consistent output order across different runs.
1128 std::sort(Sections.begin(), Sections.end(), SectionSort);
1129
1130 // Add terminating symbols for each section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001131 for (unsigned ID = 0; ID < Sections.size(); ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001132 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001133 MCSymbol *Sym = NULL;
1134
1135 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001136 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001137 // if we know the section name up-front. For user-created sections, the
1138 // resulting
1139 // label may not be valid to use as a label. (section names can use a
1140 // greater
Richard Mittonc2508242013-10-03 22:07:08 +00001141 // set of characters on some systems)
1142 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001143 Asm->OutStreamer.SwitchSection(Section);
1144 Asm->OutStreamer.EmitLabel(Sym);
1145 }
1146
1147 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001148 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001149 }
Eric Christopher960ac372012-11-22 00:59:49 +00001150}
Bill Wendling2b128d72009-05-20 23:19:06 +00001151
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001152// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001153void DwarfDebug::endModule() {
1154
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001155 if (!FirstCU)
1156 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001157
1158 // End any existing sections.
1159 // TODO: Does this need to happen?
1160 endSections();
1161
1162 // Finalize the debug info for the module.
1163 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001164
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001165 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001166
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001167 // Emit all the DIEs into a debug info section.
1168 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001169
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001170 // Corresponding abbreviations into a abbrev section.
1171 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001172
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001173 // Emit info into a debug loc section.
1174 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001175
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001176 // Emit info into a debug aranges section.
1177 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001178
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001179 // Emit info into a debug ranges section.
1180 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001181
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001182 // Emit info into a debug macinfo section.
1183 emitDebugMacInfo();
Eric Christopher95198f502012-11-27 22:43:42 +00001184
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001185 if (useSplitDwarf()) {
1186 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001187 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001188 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001189 // Emit DWO addresses.
1190 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001191 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001192
Eric Christophera876b822012-08-23 07:32:06 +00001193 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001194 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001195 emitAccelNames();
1196 emitAccelObjC();
1197 emitAccelNamespaces();
1198 emitAccelTypes();
1199 }
Eric Christopher6a841382012-11-19 22:42:10 +00001200
Eric Christopher4b358182013-08-30 00:40:17 +00001201 // Emit the pubnames and pubtypes sections if requested.
1202 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001203 emitDebugPubNames(GenerateGnuPubSections);
1204 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001205 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001206
Devang Pateld0701282010-08-02 17:32:15 +00001207 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001208 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001209
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001210 // Reset these for the next Module if we have one.
1211 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001212}
1213
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001214// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001215DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001216 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001217 LLVMContext &Ctx = DV->getContext();
1218 // More then one inlined variable corresponds to one abstract variable.
1219 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001220 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001221 if (AbsDbgVariable)
1222 return AbsDbgVariable;
1223
Devang Patel7e623022011-08-10 20:55:27 +00001224 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001225 if (!Scope)
1226 return NULL;
1227
Manman Renb3388602013-10-05 01:43:03 +00001228 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001229 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001230 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001231 return AbsDbgVariable;
1232}
1233
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001234// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +00001235bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +00001236 DbgVariable *Var, LexicalScope *Scope) {
1237 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001238 return false;
1239 DIVariable DV = Var->getVariable();
1240 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1241 return false;
1242 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001243 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001244 return false;
1245
Devang Patel4ab660b2011-03-03 20:02:02 +00001246 size_t Size = CurrentFnArguments.size();
1247 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001248 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001249 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001250 // arguments does the function have at source level.
1251 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001252 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001253 CurrentFnArguments[ArgNo - 1] = Var;
1254 return true;
1255}
1256
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001257// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001258void DwarfDebug::collectVariableInfoFromMMITable(
1259 const MachineFunction *MF, SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001260 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1261 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001262 VE = VMap.end();
1263 VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001264 const MDNode *Var = VI->first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001265 if (!Var)
1266 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001267 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001268 DIVariable DV(Var);
1269 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001270
Devang Patel7e623022011-08-10 20:55:27 +00001271 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001272
Devang Patelcdb7d442009-11-10 23:20:04 +00001273 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001274 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001275 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001276
Devang Patele1c53f22010-05-20 16:36:41 +00001277 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001278 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001279 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00001280 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001281 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001282 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001283 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001284 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001285}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001286
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001287// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1288// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001289static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001290 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001291 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1292 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001293 (MI->getOperand(1).isImm() ||
1294 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001295}
1296
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001297// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001298static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1299 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001300 const MCSymbol *SLabel,
1301 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001302 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001303
David Blaikie0252265b2013-06-16 20:34:15 +00001304 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001305 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001306 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001307 // If the second operand is an immediate, this is a
1308 // register-indirect address.
1309 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001310 MLoc.set(MI->getOperand(0).getReg());
1311 else
1312 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001313 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1314 }
1315 if (MI->getOperand(0).isImm())
1316 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1317 if (MI->getOperand(0).isFPImm())
1318 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1319 if (MI->getOperand(0).isCImm())
1320 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1321
Craig Topperee4dab52012-02-05 08:31:47 +00001322 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001323}
1324
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001325// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001326void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001327DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1328 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001329
Eric Christopher270a12c2013-07-03 21:37:03 +00001330 // Grab the variable info that was squirreled away in the MMI side-table.
Devang Patel490c8ab2010-05-20 19:57:06 +00001331 collectVariableInfoFromMMITable(MF, Processed);
1332
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001333 for (SmallVectorImpl<const MDNode *>::const_iterator
1334 UVI = UserVariables.begin(),
1335 UVE = UserVariables.end();
1336 UVI != UVE; ++UVI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001337 const MDNode *Var = *UVI;
1338 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001339 continue;
1340
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001341 // History contains relevant DBG_VALUE instructions for Var and instructions
1342 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001343 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001344 if (History.empty())
1345 continue;
1346 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001347
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001348 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001349 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001350 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1351 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001352 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001353 else if (MDNode *IA = DV.getInlinedAt())
1354 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1355 else
1356 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001357 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001358 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001359 continue;
1360
1361 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001362 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001363 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001364 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Devang Patel6c622ef2011-03-01 22:58:55 +00001365 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001366 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001367 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001368 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001369
Eric Christophercc10d202012-10-08 20:48:54 +00001370 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001371 if (History.size() <= 1 ||
1372 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001373 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001374 continue;
1375 }
1376
Eric Christopher59cc0712013-01-28 17:33:26 +00001377 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001378 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001379
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001380 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1381 HI = History.begin(),
1382 HE = History.end();
1383 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001384 const MachineInstr *Begin = *HI;
1385 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001386
Devang Patele7181b52011-06-01 23:00:17 +00001387 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001388 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1389 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001390 continue;
1391
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001392 // Compute the range for a register location.
1393 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1394 const MCSymbol *SLabel = 0;
1395
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001396 if (HI + 1 == HE)
1397 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001398 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001399 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001400 else {
1401 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001402 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001403 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001404 if (End->isDebugValue())
1405 SLabel = getLabelBeforeInsn(End);
1406 else {
1407 // End is a normal instruction clobbering the range.
1408 SLabel = getLabelAfterInsn(End);
1409 assert(SLabel && "Forgot label after clobber instruction");
1410 ++HI;
1411 }
1412 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001413
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001414 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001415 DotDebugLocEntries.push_back(
1416 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001417 }
1418 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001419 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001420
1421 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001422 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1423 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1424 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1425 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001426 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001427 continue;
1428 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001429 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001430 }
Devang Patel9fc11702010-05-25 23:40:22 +00001431}
Devang Patele0a94bf2010-05-14 21:01:35 +00001432
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001433// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001434MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001435 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1436 assert(Label && "Didn't insert label before instruction");
1437 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001438}
1439
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001440// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001441MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001442 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001443}
1444
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001445// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001446void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001447 // Check if source location changes, but ignore DBG_VALUE locations.
1448 if (!MI->isDebugValue()) {
1449 DebugLoc DL = MI->getDebugLoc();
1450 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001451 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001452 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001453 if (DL == PrologEndLoc) {
1454 Flags |= DWARF2_FLAG_PROLOGUE_END;
1455 PrologEndLoc = DebugLoc();
1456 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001457 if (PrologEndLoc.isUnknown())
1458 Flags |= DWARF2_FLAG_IS_STMT;
1459
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001460 if (!DL.isUnknown()) {
1461 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001462 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001463 } else
Devang Patel34a66202011-05-11 19:22:19 +00001464 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001465 }
Devang Patel9fc11702010-05-25 23:40:22 +00001466 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001467
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001468 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001469 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1470 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001471
1472 // No label needed.
1473 if (I == LabelsBeforeInsn.end())
1474 return;
1475
1476 // Label already assigned.
1477 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001478 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001479
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001480 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001481 PrevLabel = MMI->getContext().CreateTempSymbol();
1482 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001483 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001484 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001485}
1486
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001487// Process end of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001488void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001489 // Don't create a new label after DBG_VALUE instructions.
1490 // They don't generate code.
1491 if (!MI->isDebugValue())
1492 PrevLabel = 0;
1493
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001494 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1495 LabelsAfterInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001496
1497 // No label needed.
1498 if (I == LabelsAfterInsn.end())
1499 return;
1500
1501 // Label already assigned.
1502 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001503 return;
1504
1505 // We need a label after this instruction.
1506 if (!PrevLabel) {
1507 PrevLabel = MMI->getContext().CreateTempSymbol();
1508 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001509 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001510 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001511}
1512
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001513// Each LexicalScope has first instruction and last instruction to mark
1514// beginning and end of a scope respectively. Create an inverse map that list
1515// scopes starts (and ends) with an instruction. One instruction may start (or
1516// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001517void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001518 SmallVector<LexicalScope *, 4> WorkList;
1519 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001520 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001521 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001522
Craig Topper977e9cd2013-07-03 04:24:43 +00001523 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001524 if (!Children.empty())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001525 for (SmallVectorImpl<LexicalScope *>::const_iterator
1526 SI = Children.begin(),
1527 SE = Children.end();
1528 SI != SE; ++SI)
Devang Patel7771b7c2010-01-20 02:05:23 +00001529 WorkList.push_back(*SI);
1530
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001531 if (S->isAbstractScope())
1532 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001533
Craig Topper977e9cd2013-07-03 04:24:43 +00001534 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001535 if (Ranges.empty())
1536 continue;
Craig Topperd8e43652013-07-03 04:17:25 +00001537 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001538 RE = Ranges.end();
1539 RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001540 assert(RI->first && "InsnRange does not have first instruction!");
1541 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001542 requestLabelBeforeInsn(RI->first);
1543 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001544 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001545 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001546}
1547
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001548// Get MDNode for DebugLoc's scope.
Devang Patel589845d2011-05-09 22:14:49 +00001549static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1550 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1551 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1552 return DL.getScope(Ctx);
1553}
1554
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001555// Walk up the scope chain of given debug loc and find line number info
1556// for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001557static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1558 const MDNode *Scope = getScopeNode(DL, Ctx);
1559 DISubprogram SP = getDISubprogram(Scope);
Manman Ren7504ed42013-07-08 18:33:29 +00001560 if (SP.isSubprogram()) {
Eric Christopher34164192012-04-03 00:43:49 +00001561 // Check for number of operands since the compatibility is
1562 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001563 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001564 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1565 else
1566 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1567 }
1568
Devang Patel34a66202011-05-11 19:22:19 +00001569 return DebugLoc();
1570}
1571
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001572// Gather pre-function debug information. Assumes being called immediately
1573// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001574void DwarfDebug::beginFunction(const MachineFunction *MF) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001575
1576 // If there's no debug info for the function we're not going to do anything.
1577 if (!MMI->hasDebugInfo())
1578 return;
1579
1580 // Grab the lexical scopes for the function, if we don't have any of those
1581 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001582 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001583 if (LScopes.empty())
1584 return;
1585
1586 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1587
1588 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001589 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001590
Manman Ren4e042a62013-02-05 21:52:47 +00001591 // Set DwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001592 // belongs to so that we add to the correct per-cu line table in the
1593 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001594 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1595 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1596 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001597 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren9d4c7352013-05-21 00:57:22 +00001598 // Use a single line table if we are using .loc and generating assembly.
1599 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1600 else
1601 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001602
Eric Christopherfedfa442013-11-01 23:14:17 +00001603 // Emit a label for the function so that we have a beginning address.
1604 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001605 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001606 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001607
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001608 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001609 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001610 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001611
Eric Christopherfedfa442013-11-01 23:14:17 +00001612 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1613 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001614 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001615 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1616 II != IE; ++II) {
1617 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001618
Devang Patel002d54d2010-05-26 19:37:24 +00001619 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001620 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001621
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001622 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001623 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001624 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001625
1626 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001627 if (isDbgValueInDefinedReg(MI))
1628 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1629
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001630 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001631 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001632 if (History.empty()) {
1633 UserVariables.push_back(Var);
1634 // The first mention of a function argument gets the FunctionBeginSym
1635 // label, so arguments are visible when breaking at function entry.
1636 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001637 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001638 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001639 LabelsBeforeInsn[MI] = FunctionBeginSym;
1640 } else {
1641 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1642 const MachineInstr *Prev = History.back();
1643 if (Prev->isDebugValue()) {
1644 // Coalesce identical entries at the end of History.
1645 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001646 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001647 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001648 << "\t" << *Prev << "\t"
1649 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001650 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001651 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001652
1653 // Terminate old register assignments that don't reach MI;
1654 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1655 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1656 isDbgValueInDefinedReg(Prev)) {
1657 // Previous register assignment needs to terminate at the end of
1658 // its basic block.
1659 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001660 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001661 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001662 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001663 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001664 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001665 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001666 } else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001667 // Terminate after LastMI.
1668 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001669 }
1670 }
1671 }
1672 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001673 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001674 // Not a DBG_VALUE instruction.
1675 if (!MI->isLabel())
1676 AtBlockEntry = false;
1677
Eric Christopher133195782012-10-04 20:46:14 +00001678 // First known non-DBG_VALUE and non-frame setup location marks
1679 // the beginning of the function body.
1680 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1681 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001682 PrologEndLoc = MI->getDebugLoc();
1683
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001684 // Check if the instruction clobbers any registers with debug vars.
1685 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
Eric Christopherfedfa442013-11-01 23:14:17 +00001686 MOE = MI->operands_end();
1687 MOI != MOE; ++MOI) {
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001688 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1689 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001690 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true); AI.isValid();
1691 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001692 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001693 const MDNode *Var = LiveUserVar[Reg];
1694 if (!Var)
1695 continue;
1696 // Reg is now clobbered.
1697 LiveUserVar[Reg] = 0;
1698
1699 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001700 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1701 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001702 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001703 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001704 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001705 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001706 const MachineInstr *Prev = History.back();
1707 // Sanity-check: Register assignments are terminated at the end of
1708 // their block.
1709 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1710 continue;
1711 // Is the variable still in Reg?
1712 if (!isDbgValueInDefinedReg(Prev) ||
1713 Prev->getOperand(0).getReg() != Reg)
1714 continue;
1715 // Var is clobbered. Make sure the next instruction gets a label.
1716 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001717 }
1718 }
Devang Patel002d54d2010-05-26 19:37:24 +00001719 }
Devang Patel002d54d2010-05-26 19:37:24 +00001720 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001721 }
1722
1723 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1724 I != E; ++I) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001725 SmallVectorImpl<const MachineInstr *> &History = I->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001726 if (History.empty())
1727 continue;
1728
1729 // Make sure the final register assignments are terminated.
1730 const MachineInstr *Prev = History.back();
1731 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1732 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001733 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001734 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001735 if (LastMI == PrevMBB->end())
1736 // Drop DBG_VALUE for empty range.
1737 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001738 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001739 // Terminate after LastMI.
1740 History.push_back(LastMI);
1741 }
1742 }
1743 // Request labels for the full history.
1744 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1745 const MachineInstr *MI = History[i];
1746 if (MI->isDebugValue())
1747 requestLabelBeforeInsn(MI);
1748 else
1749 requestLabelAfterInsn(MI);
1750 }
1751 }
Devang Patel002d54d2010-05-26 19:37:24 +00001752
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001753 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001754 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001755
1756 // Record beginning of function.
1757 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001758 DebugLoc FnStartDL =
1759 getFnDebugLoc(PrologEndLoc, MF->getFunction()->getContext());
1760 recordSourceLine(
1761 FnStartDL.getLine(), FnStartDL.getCol(),
1762 FnStartDL.getScope(MF->getFunction()->getContext()),
1763 // We'd like to list the prologue as "not statements" but GDB behaves
1764 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1765 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001766 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001767}
1768
Devang Patel7e623022011-08-10 20:55:27 +00001769void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001770 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1771 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001772 // Variables with positive arg numbers are parameters.
1773 if (unsigned ArgNum = DV.getArgNumber()) {
1774 // Keep all parameters in order at the start of the variable list to ensure
1775 // function types are correct (no out-of-order parameters)
1776 //
1777 // This could be improved by only doing it for optimized builds (unoptimized
1778 // builds have the right order to begin with), searching from the back (this
1779 // would catch the unoptimized case quickly), or doing a binary search
1780 // rather than linear search.
1781 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1782 while (I != Vars.end()) {
1783 unsigned CurNum = (*I)->getVariable().getArgNumber();
1784 // A local (non-parameter) variable has been found, insert immediately
1785 // before it.
1786 if (CurNum == 0)
1787 break;
1788 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001789 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001790 break;
David Blaikieb272a752013-06-06 22:28:26 +00001791 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001792 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001793 Vars.insert(I, Var);
1794 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001795 }
1796
1797 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001798}
1799
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001800// Gather and emit post-function debug information.
Chris Lattner76555b52010-01-26 23:18:02 +00001801void DwarfDebug::endFunction(const MachineFunction *MF) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001802 if (!MMI->hasDebugInfo() || LScopes.empty())
1803 return;
Devang Patel2904aa92009-11-12 19:02:56 +00001804
Devang Patel7e623022011-08-10 20:55:27 +00001805 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001806 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001807 // Assumes in correct section after the entry point.
1808 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Manman Ren4e042a62013-02-05 21:52:47 +00001809 // Set DwarfCompileUnitID in MCContext to default value.
1810 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001811
Devang Patel7e623022011-08-10 20:55:27 +00001812 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1813 collectVariableInfo(MF, ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001814
Devang Patel3acc70e2011-08-15 22:04:40 +00001815 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001816 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001817 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001818
Devang Patel7e623022011-08-10 20:55:27 +00001819 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001820 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1821 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1822 LexicalScope *AScope = AList[i];
1823 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001824 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001825 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001826 DIArray Variables = SP.getVariables();
1827 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1828 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001829 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001830 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001831 // Check that DbgVariable for DV wasn't created earlier, when
1832 // findAbstractVariable() was called for inlined instance of DV.
1833 LLVMContext &Ctx = DV->getContext();
1834 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1835 if (AbstractVariables.lookup(CleanDV))
1836 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001837 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001838 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001839 }
1840 }
Devang Patel44403472011-08-12 18:10:19 +00001841 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001842 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001843 }
Eric Christopher6a841382012-11-19 22:42:10 +00001844
Devang Patel3acc70e2011-08-15 22:04:40 +00001845 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher6a841382012-11-19 22:42:10 +00001846
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001847 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001848 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001849
Bill Wendling2b128d72009-05-20 23:19:06 +00001850 // Clear debug info
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001851 for (ScopeVariablesMap::iterator I = ScopeVariables.begin(),
1852 E = ScopeVariables.end();
1853 I != E; ++I)
Devang Patel7e623022011-08-10 20:55:27 +00001854 DeleteContainerPointers(I->second);
1855 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001856 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001857 UserVariables.clear();
1858 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001859 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001860 LabelsBeforeInsn.clear();
1861 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001862 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001863}
1864
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001865// Register a source line with debug info. Returns the unique label that was
1866// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001867void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1868 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001869 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001870 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001871 unsigned Src = 1;
1872 if (S) {
1873 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001874
Dan Gohman50849c62010-05-05 23:41:32 +00001875 if (Scope.isCompileUnit()) {
1876 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001877 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001878 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001879 } else if (Scope.isFile()) {
1880 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001881 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001882 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001883 } else if (Scope.isSubprogram()) {
1884 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001885 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001886 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001887 } else if (Scope.isLexicalBlockFile()) {
1888 DILexicalBlockFile DBF(S);
1889 Fn = DBF.getFilename();
1890 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001891 } else if (Scope.isLexicalBlock()) {
1892 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001893 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001894 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001895 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001896 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001897
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001898 Src = getOrCreateSourceID(
1899 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001900 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001901 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001902}
1903
Bill Wendling806535f2009-05-20 23:22:40 +00001904//===----------------------------------------------------------------------===//
1905// Emit Methods
1906//===----------------------------------------------------------------------===//
1907
Manman Rence20d462013-10-29 22:57:10 +00001908// Compute the size and offset of a DIE. The offset is relative to start of the
1909// CU. It returns the offset after laying out the DIE.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001910unsigned DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001911 // Get the children.
1912 const std::vector<DIE *> &Children = Die->getChildren();
1913
Bill Wendling480ff322009-05-20 23:21:38 +00001914 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001915 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001916
1917 // Get the abbreviation for this DIE.
1918 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00001919 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00001920
1921 // Set DIE offset
1922 Die->setOffset(Offset);
1923
1924 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001925 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001926
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001927 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00001928 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001929
1930 // Size the DIE attribute values.
1931 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1932 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001933 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001934
1935 // Size the DIE children if any.
1936 if (!Children.empty()) {
1937 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1938 "Children flag not set");
1939
1940 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001941 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001942
1943 // End of children marker.
1944 Offset += sizeof(int8_t);
1945 }
1946
1947 Die->setSize(Offset - Die->getOffset());
1948 return Offset;
1949}
1950
Eric Christopherb088d2d2013-10-24 21:05:08 +00001951// Compute the size and offset for each DIE.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001952void DwarfUnits::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001953 // Offset from the first CU in the debug info section is 0 initially.
1954 unsigned SecOffset = 0;
1955
Eric Christopherb088d2d2013-10-24 21:05:08 +00001956 // Iterate over each compile unit and set the size and offsets for each
1957 // DIE within each compile unit. All offsets are CU relative.
David Blaikie319a05f2013-12-02 19:33:10 +00001958 for (SmallVectorImpl<Unit *>::const_iterator I = CUs.begin(), E = CUs.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001959 I != E; ++I) {
Manman Rence20d462013-10-29 22:57:10 +00001960 (*I)->setDebugInfoOffset(SecOffset);
1961
1962 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001963 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
David Blaikie6b288cf2013-10-30 20:42:41 +00001964 (*I)->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001965
1966 // EndOffset here is CU-relative, after laying out
1967 // all of the CU DIE.
1968 unsigned EndOffset = computeSizeAndOffset((*I)->getCUDie(), Offset);
1969 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001970 }
Bill Wendling480ff322009-05-20 23:21:38 +00001971}
1972
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001973// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001974void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001975 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001976
Bill Wendling480ff322009-05-20 23:21:38 +00001977 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001978 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001979 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001980 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001981 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001982 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001983 DwarfAbbrevDWOSectionSym = emitSectionSym(
1984 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001985 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001986
Chris Lattner6629ca92010-04-04 22:59:04 +00001987 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001988 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001989
Eric Christopher74804332013-02-07 21:19:50 +00001990 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001991 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001992 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001993 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001994 DwarfGnuPubNamesSectionSym =
1995 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1996 DwarfGnuPubTypesSectionSym =
1997 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001998 } else if (HasDwarfPubSections) {
1999 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2000 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00002001 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002002
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002003 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002004 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00002005 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002006 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002007 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00002008 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002009 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00002010 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002011 DwarfDebugRangeSectionSym =
2012 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00002013
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002014 DwarfDebugLocSectionSym =
2015 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Devang Patel9fc11702010-05-25 23:40:22 +00002016
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002017 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
2018 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002019}
2020
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002021// Recursively emits a debug information entry.
David Blaikie2d4e1122013-10-30 17:14:24 +00002022void DwarfDebug::emitDIE(DIE *Die, ArrayRef<DIEAbbrev *> Abbrevs) {
Bill Wendling480ff322009-05-20 23:21:38 +00002023 // Get the abbreviation for this DIE.
2024 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00002025 const DIEAbbrev *Abbrev = Abbrevs[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00002026
Bill Wendling480ff322009-05-20 23:21:38 +00002027 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00002028 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00002029 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2030 Twine::utohexstr(Die->getOffset()) + ":0x" +
2031 Twine::utohexstr(Die->getSize()) + " " +
2032 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00002033 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002034
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002035 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00002036 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00002037
2038 // Emit the DIE attribute values.
2039 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00002040 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
2041 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00002042 assert(Form && "Too many attributes for DIE (check abbreviation)");
2043
Chris Lattner7bde8c02010-04-04 18:52:31 +00002044 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00002045 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002046
Bill Wendling480ff322009-05-20 23:21:38 +00002047 switch (Attr) {
Manman Rence20d462013-10-29 22:57:10 +00002048 case dwarf::DW_AT_abstract_origin:
2049 case dwarf::DW_AT_type:
2050 case dwarf::DW_AT_friend:
2051 case dwarf::DW_AT_specification:
2052 case dwarf::DW_AT_import:
2053 case dwarf::DW_AT_containing_type: {
2054 DIEEntry *E = cast<DIEEntry>(Values[i]);
2055 DIE *Origin = E->getEntry();
2056 unsigned Addr = Origin->getOffset();
2057 if (Form == dwarf::DW_FORM_ref_addr) {
2058 assert(!useSplitDwarf() && "TODO: dwo files can't have relocations.");
2059 // For DW_FORM_ref_addr, output the offset from beginning of debug info
2060 // section. Origin->getOffset() returns the offset from start of the
2061 // compile unit.
David Blaikie409dd9c2013-11-19 23:08:21 +00002062 CompileUnit *CU = CUDieMap.lookup(Origin->getUnit());
Manman Rence20d462013-10-29 22:57:10 +00002063 assert(CU && "CUDie should belong to a CU.");
2064 Addr += CU->getDebugInfoOffset();
2065 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2066 Asm->EmitLabelPlusOffset(DwarfInfoSectionSym, Addr,
2067 DIEEntry::getRefAddrSize(Asm));
2068 else
Manman Ren251a1bd2013-10-29 23:14:15 +00002069 Asm->EmitLabelOffsetDifference(DwarfInfoSectionSym, Addr,
2070 DwarfInfoSectionSym,
2071 DIEEntry::getRefAddrSize(Asm));
Manman Rence20d462013-10-29 22:57:10 +00002072 } else {
Manman Ren4dbdc902013-10-31 17:54:35 +00002073 // Make sure Origin belong to the same CU.
David Blaikie409dd9c2013-11-19 23:08:21 +00002074 assert(Die->getUnit() == Origin->getUnit() &&
Manman Ren4dbdc902013-10-31 17:54:35 +00002075 "The referenced DIE should belong to the same CU in ref4");
Manman Rence20d462013-10-29 22:57:10 +00002076 Asm->EmitInt32(Addr);
2077 }
2078 break;
2079 }
Devang Patel12563b32010-04-16 23:33:45 +00002080 case dwarf::DW_AT_ranges: {
2081 // DW_AT_range Value encodes offset in debug_range section.
Eric Christopher4751d702013-11-23 00:05:29 +00002082 DIELabel *V = cast<DIELabel>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00002083
Eric Christopher4751d702013-11-23 00:05:29 +00002084 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2085 Asm->EmitSectionOffset(V->getValue(), DwarfDebugRangeSectionSym);
2086 else
2087 Asm->EmitLabelDifference(V->getValue(), DwarfDebugRangeSectionSym, 4);
Devang Patel12563b32010-04-16 23:33:45 +00002088 break;
2089 }
Devang Patel9fc11702010-05-25 23:40:22 +00002090 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00002091 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
2092 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherd0b82ae2013-11-16 00:18:40 +00002093 Asm->EmitSectionOffset(L->getValue(), DwarfDebugLocSectionSym);
Nick Lewycky33da3362012-06-22 01:25:12 +00002094 else
Ulrich Weigand396ba8b2013-07-02 18:46:26 +00002095 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Nick Lewycky33da3362012-06-22 01:25:12 +00002096 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00002097 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00002098 }
Devang Patel9fc11702010-05-25 23:40:22 +00002099 break;
2100 }
Devang Patela1bd5a12010-09-29 19:08:08 +00002101 case dwarf::DW_AT_accessibility: {
2102 if (Asm->isVerbose()) {
2103 DIEInteger *V = cast<DIEInteger>(Values[i]);
2104 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2105 }
2106 Values[i]->EmitValue(Asm, Form);
2107 break;
2108 }
Bill Wendling480ff322009-05-20 23:21:38 +00002109 default:
2110 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002111 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002112 break;
2113 }
Bill Wendling480ff322009-05-20 23:21:38 +00002114 }
2115
2116 // Emit the DIE children if any.
2117 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2118 const std::vector<DIE *> &Children = Die->getChildren();
2119
2120 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher3c5a1912012-12-19 22:02:53 +00002121 emitDIE(Children[j], Abbrevs);
Bill Wendling480ff322009-05-20 23:21:38 +00002122
Chris Lattner7bde8c02010-04-04 18:52:31 +00002123 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00002124 Asm->OutStreamer.AddComment("End Of Children Mark");
2125 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002126 }
2127}
2128
Eric Christophera2de8262012-12-15 00:04:07 +00002129// Emit the various dwarf units to the unit section USection with
2130// the abbreviations going into ASection.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002131void DwarfUnits::emitUnits(DwarfDebug *DD, const MCSection *USection,
Eric Christophera2de8262012-12-15 00:04:07 +00002132 const MCSection *ASection,
2133 const MCSymbol *ASectionSym) {
2134 Asm->OutStreamer.SwitchSection(USection);
David Blaikie319a05f2013-12-02 19:33:10 +00002135 for (SmallVectorImpl<Unit *>::iterator I = CUs.begin(), E = CUs.end(); I != E;
2136 ++I) {
2137 Unit *TheCU = *I;
Devang Patel1a0df9a2010-05-10 22:49:55 +00002138 DIE *Die = TheCU->getCUDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002139
Devang Patel1a0df9a2010-05-10 22:49:55 +00002140 // Emit the compile units header.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002141 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol(USection->getLabelBeginName(),
2142 TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002143
Devang Patel1a0df9a2010-05-10 22:49:55 +00002144 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00002145 Asm->OutStreamer.AddComment("Length of Unit");
2146 Asm->EmitInt32(TheCU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002147
David Blaikie6b288cf2013-10-30 20:42:41 +00002148 TheCU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002149
Eric Christopher3c5a1912012-12-19 22:02:53 +00002150 DD->emitDIE(Die, Abbreviations);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002151 Asm->OutStreamer.EmitLabel(
2152 Asm->GetTempSymbol(USection->getLabelEndName(), TheCU->getUniqueID()));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002153 }
Bill Wendling480ff322009-05-20 23:21:38 +00002154}
2155
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002156// Emit the debug info section.
2157void DwarfDebug::emitDebugInfo() {
Eric Christophera2de8262012-12-15 00:04:07 +00002158 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2159
2160 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
2161 Asm->getObjFileLowering().getDwarfAbbrevSection(),
2162 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002163}
2164
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002165// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002166void DwarfDebug::emitAbbreviations() {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002167 if (!useSplitDwarf())
2168 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
2169 &Abbreviations);
2170 else
2171 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2172}
Bill Wendling480ff322009-05-20 23:21:38 +00002173
Eric Christopher3c5a1912012-12-19 22:02:53 +00002174void DwarfDebug::emitAbbrevs(const MCSection *Section,
2175 std::vector<DIEAbbrev *> *Abbrevs) {
2176 // Check to see if it is worth the effort.
2177 if (!Abbrevs->empty()) {
2178 // Start the debug abbrev section.
2179 Asm->OutStreamer.SwitchSection(Section);
2180
2181 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002182 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling480ff322009-05-20 23:21:38 +00002183
2184 // For each abbrevation.
Eric Christopher3c5a1912012-12-19 22:02:53 +00002185 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00002186 // Get abbreviation data
Eric Christopher3c5a1912012-12-19 22:02:53 +00002187 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling480ff322009-05-20 23:21:38 +00002188
2189 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002190 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002191
2192 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002193 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002194 }
2195
2196 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002197 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002198
Eric Christopher3c5a1912012-12-19 22:02:53 +00002199 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002200 Asm->OutStreamer.EmitLabel(End);
Bill Wendling480ff322009-05-20 23:21:38 +00002201 }
2202}
2203
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002204// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002205void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002206 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002207 Asm->OutStreamer.AddComment("Extended Op");
2208 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002209
Chris Lattner566cae92010-03-09 23:52:58 +00002210 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002211 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002212 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2213 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2214
2215 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002216
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002217 Asm->OutStreamer.EmitSymbolValue(
2218 Asm->GetTempSymbol("section_end", SectionEnd),
2219 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002220
2221 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002222 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2223 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002224 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002225 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002226}
2227
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002228// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002229void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002230 DwarfAccelTable AT(
2231 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002232 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2233 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002234 I != E; ++I) {
David Blaikie319a05f2013-12-02 19:33:10 +00002235 Unit *TheCU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002236 const StringMap<std::vector<const DIE *> > &Names = TheCU->getAccelNames();
2237 for (StringMap<std::vector<const DIE *> >::const_iterator
2238 GI = Names.begin(),
2239 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002240 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002241 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002242 const std::vector<const DIE *> &Entities = GI->second;
2243 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2244 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002245 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002246 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002247 }
2248 }
2249
2250 AT.FinalizeTable(Asm, "Names");
2251 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002252 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002253 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2254 Asm->OutStreamer.EmitLabel(SectionBegin);
2255
2256 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002257 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002258}
2259
Eric Christopher48fef592012-12-20 21:58:40 +00002260// Emit objective C classes and categories into a hashed accelerator table
2261// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002262void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002263 DwarfAccelTable AT(
2264 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002265 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2266 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002267 I != E; ++I) {
David Blaikie319a05f2013-12-02 19:33:10 +00002268 Unit *TheCU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002269 const StringMap<std::vector<const DIE *> > &Names = TheCU->getAccelObjC();
2270 for (StringMap<std::vector<const DIE *> >::const_iterator
2271 GI = Names.begin(),
2272 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002273 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002274 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002275 const std::vector<const DIE *> &Entities = GI->second;
2276 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2277 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002278 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002279 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002280 }
2281 }
2282
2283 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002284 Asm->OutStreamer.SwitchSection(
2285 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002286 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2287 Asm->OutStreamer.EmitLabel(SectionBegin);
2288
2289 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002290 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002291}
2292
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002293// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002294void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002295 DwarfAccelTable AT(
2296 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002297 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2298 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002299 I != E; ++I) {
David Blaikie319a05f2013-12-02 19:33:10 +00002300 Unit *TheCU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002301 const StringMap<std::vector<const DIE *> > &Names =
2302 TheCU->getAccelNamespace();
2303 for (StringMap<std::vector<const DIE *> >::const_iterator
2304 GI = Names.begin(),
2305 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002306 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002307 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002308 const std::vector<const DIE *> &Entities = GI->second;
2309 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2310 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002311 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002312 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002313 }
2314 }
2315
2316 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002317 Asm->OutStreamer.SwitchSection(
2318 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002319 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2320 Asm->OutStreamer.EmitLabel(SectionBegin);
2321
2322 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002323 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002324}
2325
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002326// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002327void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002328 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002329 Atoms.push_back(
2330 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2331 Atoms.push_back(
2332 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2333 Atoms.push_back(
2334 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002335 DwarfAccelTable AT(Atoms);
David Blaikie319a05f2013-12-02 19:33:10 +00002336 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2337 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002338 I != E; ++I) {
David Blaikie319a05f2013-12-02 19:33:10 +00002339 Unit *TheCU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002340 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &Names =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002341 TheCU->getAccelTypes();
David Blaikie2ea848b2013-11-19 22:51:04 +00002342 for (StringMap<
2343 std::vector<std::pair<const DIE *, unsigned> > >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002344 GI = Names.begin(),
2345 GE = Names.end();
2346 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002347 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002348 const std::vector<std::pair<const DIE *, unsigned> > &Entities =
2349 GI->second;
2350 for (std::vector<std::pair<const DIE *, unsigned> >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002351 DI = Entities.begin(),
2352 DE = Entities.end();
2353 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002354 AT.AddName(Name, DI->first, DI->second);
Eric Christopher4996c702011-11-07 09:24:32 +00002355 }
2356 }
2357
2358 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002359 Asm->OutStreamer.SwitchSection(
2360 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002361 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2362 Asm->OutStreamer.EmitLabel(SectionBegin);
2363
2364 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002365 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002366}
2367
Eric Christopherdd1a0122013-09-13 00:35:05 +00002368// Public name handling.
2369// The format for the various pubnames:
2370//
2371// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2372// for the DIE that is named.
2373//
2374// gnu pubnames - offset/index value/name tuples where the offset is the offset
2375// into the CU and the index value is computed according to the type of value
2376// for the DIE that is named.
2377//
2378// For type units the offset is the offset of the skeleton DIE. For split dwarf
2379// it's the offset within the debug_info/debug_types dwo section, however, the
2380// reference in the pubname header doesn't change.
2381
2382/// computeIndexValue - Compute the gdb index value for the DIE and CU.
David Blaikie319a05f2013-12-02 19:33:10 +00002383static dwarf::PubIndexEntryDescriptor computeIndexValue(Unit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002384 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002385 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2386
2387 // We could have a specification DIE that has our most of our knowledge,
2388 // look for that now.
2389 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2390 if (SpecVal) {
2391 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2392 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2393 Linkage = dwarf::GIEL_EXTERNAL;
2394 } else if (Die->findAttribute(dwarf::DW_AT_external))
2395 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002396
2397 switch (Die->getTag()) {
2398 case dwarf::DW_TAG_class_type:
2399 case dwarf::DW_TAG_structure_type:
2400 case dwarf::DW_TAG_union_type:
2401 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002402 return dwarf::PubIndexEntryDescriptor(
2403 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2404 ? dwarf::GIEL_STATIC
2405 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002406 case dwarf::DW_TAG_typedef:
2407 case dwarf::DW_TAG_base_type:
2408 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002409 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002410 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002411 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002412 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002413 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002414 case dwarf::DW_TAG_constant:
2415 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002416 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002417 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002418 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2419 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002420 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002421 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002422 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002423}
2424
Eric Christopher5f93bb92013-09-09 20:03:17 +00002425/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002426///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002427void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002428 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopherdd1a0122013-09-13 00:35:05 +00002429 const MCSection *PSec =
2430 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2431 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002432
David Blaikie319a05f2013-12-02 19:33:10 +00002433 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2434 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00002435 I != E; ++I) {
David Blaikie319a05f2013-12-02 19:33:10 +00002436 Unit *TheCU = *I;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002437 unsigned ID = TheCU->getUniqueID();
2438
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002439 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002440 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002441
Eric Christopher39eebfa2013-09-30 23:14:16 +00002442 // Emit a label so we can reference the beginning of this pubname section.
2443 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002444 Asm->OutStreamer.EmitLabel(
2445 Asm->GetTempSymbol("gnu_pubnames", TheCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002446
Eric Christopherdd1a0122013-09-13 00:35:05 +00002447 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002448 Asm->OutStreamer.AddComment("Length of Public Names Info");
2449 Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
2450 Asm->GetTempSymbol("pubnames_begin", ID), 4);
2451
2452 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
2453
2454 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002455 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002456
2457 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2458 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2459 DwarfInfoSectionSym);
2460
2461 Asm->OutStreamer.AddComment("Compilation Unit Length");
2462 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
2463 Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2464 4);
2465
Eric Christopherdd1a0122013-09-13 00:35:05 +00002466 // Emit the pubnames for this compilation unit.
Eric Christopher0fe676a2013-11-21 00:48:22 +00002467 const StringMap<const DIE *> &Globals = TheCU->getGlobalNames();
2468 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2469 GE = Globals.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002470 GI != GE; ++GI) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002471 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002472 const DIE *Entity = GI->second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002473
2474 Asm->OutStreamer.AddComment("DIE offset");
2475 Asm->EmitInt32(Entity->getOffset());
2476
Eric Christopherdd1a0122013-09-13 00:35:05 +00002477 if (GnuStyle) {
David Blaikied0a869d2013-09-19 22:19:37 +00002478 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheCU, Entity);
2479 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002480 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002481 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002482 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002483 }
2484
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002485 if (Asm->isVerbose())
2486 Asm->OutStreamer.AddComment("External Name");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002487 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002488 }
2489
2490 Asm->OutStreamer.AddComment("End Mark");
2491 Asm->EmitInt32(0);
2492 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
2493 }
2494}
2495
Eric Christopherdd1a0122013-09-13 00:35:05 +00002496void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher8b3737f2013-09-13 00:34:58 +00002497 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopher261d2342013-09-23 20:55:35 +00002498 const MCSection *PSec =
2499 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2500 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002501
David Blaikie319a05f2013-12-02 19:33:10 +00002502 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2503 E = getUnits().end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002504 I != E; ++I) {
David Blaikie319a05f2013-12-02 19:33:10 +00002505 Unit *TheCU = *I;
Eric Christopher6abc9c52011-11-07 09:18:35 +00002506 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002507 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002508
2509 // Emit a label so we can reference the beginning of this pubtype section.
2510 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002511 Asm->OutStreamer.EmitLabel(
2512 Asm->GetTempSymbol("gnu_pubtypes", TheCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002513
2514 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002515 Asm->OutStreamer.AddComment("Length of Public Types Info");
2516 Asm->EmitLabelDifference(
Eric Christopher8b3737f2013-09-13 00:34:58 +00002517 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()),
2518 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002519
Eric Christopher8b3737f2013-09-13 00:34:58 +00002520 Asm->OutStreamer.EmitLabel(
2521 Asm->GetTempSymbol("pubtypes_begin", TheCU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002522
Eric Christopher8b3737f2013-09-13 00:34:58 +00002523 if (Asm->isVerbose())
2524 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002525 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002526
Devang Patel1a0df9a2010-05-10 22:49:55 +00002527 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002528 Asm->EmitSectionOffset(
2529 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheCU->getUniqueID()),
2530 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002531
Devang Patel1a0df9a2010-05-10 22:49:55 +00002532 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002533 Asm->EmitLabelDifference(
2534 Asm->GetTempSymbol(ISec->getLabelEndName(), TheCU->getUniqueID()),
2535 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheCU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002536
Eric Christopher39eebfa2013-09-30 23:14:16 +00002537 // Emit the pubtypes.
Eric Christopher0fe676a2013-11-21 00:48:22 +00002538 const StringMap<const DIE *> &Globals = TheCU->getGlobalTypes();
2539 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2540 GE = Globals.end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002541 GI != GE; ++GI) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002542 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002543 const DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002544
Eric Christopher8b3737f2013-09-13 00:34:58 +00002545 if (Asm->isVerbose())
2546 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002547 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002548
Eric Christopherdd1a0122013-09-13 00:35:05 +00002549 if (GnuStyle) {
David Blaikied0a869d2013-09-19 22:19:37 +00002550 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheCU, Entity);
2551 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002552 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002553 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002554 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002555 }
2556
Eric Christopher8b3737f2013-09-13 00:34:58 +00002557 if (Asm->isVerbose())
2558 Asm->OutStreamer.AddComment("External Name");
2559
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002560 // Emit the name with a terminating null byte.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002561 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002562 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002563
Devang Patel1a0df9a2010-05-10 22:49:55 +00002564 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002565 Asm->EmitInt32(0);
Eric Christopher8b3737f2013-09-13 00:34:58 +00002566 Asm->OutStreamer.EmitLabel(
2567 Asm->GetTempSymbol("pubtypes_end", TheCU->getUniqueID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002568 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002569}
2570
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002571// Emit strings into a string section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002572void DwarfUnits::emitStrings(const MCSection *StrSection,
2573 const MCSection *OffsetSection = NULL,
2574 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002575
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002576 if (StringPool.empty())
2577 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002578
Chris Lattner3d72a672010-03-09 23:38:23 +00002579 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002580 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002581
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002582 // Get all of the string pool entries and put them in an array by their ID so
2583 // we can sort them.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002584 SmallVector<
2585 std::pair<unsigned, StringMapEntry<std::pair<MCSymbol *, unsigned> > *>,
2586 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002587
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002588 for (StringMap<std::pair<MCSymbol *, unsigned> >::iterator
2589 I = StringPool.begin(),
2590 E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002591 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002592 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002593
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002594 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002595
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002596 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002597 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002598 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002599
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002600 // Emit the string itself with a terminating null byte.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002601 Asm->OutStreamer.EmitBytes(
2602 StringRef(Entries[i].second->getKeyData(),
2603 Entries[i].second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002604 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002605
2606 // If we've got an offset section go ahead and emit that now as well.
2607 if (OffsetSection) {
2608 Asm->OutStreamer.SwitchSection(OffsetSection);
2609 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002610 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002611 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002612 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002613 offset += Entries[i].second->getKeyLength() + 1;
2614 }
2615 }
Bill Wendling480ff322009-05-20 23:21:38 +00002616}
2617
Eric Christopher65132a82013-11-19 09:11:26 +00002618
2619// Emit addresses into the section given.
Eric Christopher962c9082013-01-15 23:56:56 +00002620void DwarfUnits::emitAddresses(const MCSection *AddrSection) {
2621
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002622 if (AddressPool.empty())
2623 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002624
2625 // Start the dwarf addr section.
2626 Asm->OutStreamer.SwitchSection(AddrSection);
2627
David Blaikiece1960f2013-07-08 17:51:28 +00002628 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002629 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002630
David Blaikiece1960f2013-07-08 17:51:28 +00002631 for (DenseMap<const MCExpr *, unsigned>::iterator I = AddressPool.begin(),
2632 E = AddressPool.end();
Eric Christopher962c9082013-01-15 23:56:56 +00002633 I != E; ++I)
David Blaikieac569a62013-07-08 17:33:10 +00002634 Entries[I->second] = I->first;
Eric Christopher962c9082013-01-15 23:56:56 +00002635
2636 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002637 // Emit an expression for reference from debug information entries.
David Blaikieac569a62013-07-08 17:33:10 +00002638 if (const MCExpr *Expr = Entries[i])
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002639 Asm->OutStreamer.EmitValue(Expr, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002640 else
2641 Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
2642 }
Eric Christopher962c9082013-01-15 23:56:56 +00002643}
2644
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002645// Emit visible names into a debug str section.
2646void DwarfDebug::emitDebugStr() {
2647 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2648 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2649}
2650
Eric Christopher9046f942013-07-02 21:36:07 +00002651// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002652void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002653 if (DotDebugLocEntries.empty())
2654 return;
2655
Eric Christopher4887c8f2013-03-29 23:34:06 +00002656 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002657 I = DotDebugLocEntries.begin(),
2658 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002659 I != E; ++I) {
2660 DotDebugLocEntry &Entry = *I;
2661 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002662 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002663 }
2664
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002665 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002666 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002667 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002668 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002669 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2670 unsigned index = 1;
Eric Christopher4887c8f2013-03-29 23:34:06 +00002671 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002672 I = DotDebugLocEntries.begin(),
2673 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002674 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002675 DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002676 if (Entry.isMerged())
2677 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002678 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002679 Asm->OutStreamer.EmitIntValue(0, Size);
2680 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002681 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002682 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002683 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2684 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2685 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002686 Asm->OutStreamer.AddComment("Loc expr size");
2687 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2688 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2689 Asm->EmitLabelDifference(end, begin, 2);
2690 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002691 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002692 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002693 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2694 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002695 Asm->OutStreamer.AddComment("DW_OP_consts");
2696 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002697 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002698 } else {
2699 Asm->OutStreamer.AddComment("DW_OP_constu");
2700 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002701 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002702 }
Devang Pateled9fd452011-07-08 16:49:43 +00002703 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002704 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002705 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002706 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002707 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002708 else {
2709 // Complex address entry.
2710 unsigned N = DV.getNumAddrElements();
2711 unsigned i = 0;
2712 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002713 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002714 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002715 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002716 Asm->OutStreamer.AddComment("DW_OP_deref");
2717 Asm->EmitInt8(dwarf::DW_OP_deref);
2718 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2719 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2720 Asm->EmitSLEB128(DV.getAddrElement(1));
2721 } else {
2722 // If first address element is OpPlus then emit
2723 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002724 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2725 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002726 i = 2;
2727 }
2728 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002729 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002730 }
Eric Christopher6a841382012-11-19 22:42:10 +00002731
Devang Pateled9fd452011-07-08 16:49:43 +00002732 // Emit remaining complex address elements.
2733 for (; i < N; ++i) {
2734 uint64_t Element = DV.getAddrElement(i);
2735 if (Element == DIBuilder::OpPlus) {
2736 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2737 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002738 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002739 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002740 Asm->EmitInt8(dwarf::DW_OP_deref);
2741 } else
2742 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002743 }
Devang Patel3e021532011-04-28 02:22:40 +00002744 }
Devang Patel3e021532011-04-28 02:22:40 +00002745 }
Devang Pateled9fd452011-07-08 16:49:43 +00002746 // else ... ignore constant fp. There is not any good way to
2747 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002748 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002749 }
2750 }
Bill Wendling480ff322009-05-20 23:21:38 +00002751}
2752
Richard Mitton21101b32013-09-19 23:21:01 +00002753struct SymbolCUSorter {
2754 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2755 const MCStreamer &Streamer;
2756
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002757 bool operator()(const SymbolCU &A, const SymbolCU &B) {
Richard Mitton21101b32013-09-19 23:21:01 +00002758 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2759 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2760
2761 // Symbols with no order assigned should be placed at the end.
2762 // (e.g. section end labels)
2763 if (IA == 0)
2764 IA = (unsigned)(-1);
2765 if (IB == 0)
2766 IB = (unsigned)(-1);
2767 return IA < IB;
2768 }
2769};
2770
David Blaikie319a05f2013-12-02 19:33:10 +00002771static bool CUSort(const Unit *A, const Unit *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002772 return (A->getUniqueID() < B->getUniqueID());
Richard Mitton21101b32013-09-19 23:21:01 +00002773}
2774
2775struct ArangeSpan {
2776 const MCSymbol *Start, *End;
2777};
2778
2779// Emit a debug aranges section, containing a CU lookup for any
2780// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002781void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002782 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002783 Asm->OutStreamer.SwitchSection(
2784 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002785
2786 typedef DenseMap<CompileUnit *, std::vector<ArangeSpan> > SpansType;
2787
2788 SpansType Spans;
2789
2790 // Build a list of sections used.
2791 std::vector<const MCSection *> Sections;
2792 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
2793 it++) {
2794 const MCSection *Section = it->first;
2795 Sections.push_back(Section);
2796 }
2797
2798 // Sort the sections into order.
2799 // This is only done to ensure consistent output order across different runs.
2800 std::sort(Sections.begin(), Sections.end(), SectionSort);
2801
2802 // Build a set of address spans, sorted by CU.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002803 for (size_t SecIdx = 0; SecIdx < Sections.size(); SecIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002804 const MCSection *Section = Sections[SecIdx];
2805 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2806 if (List.size() < 2)
2807 continue;
2808
2809 // Sort the symbols by offset within the section.
2810 SymbolCUSorter sorter(Asm->OutStreamer);
2811 std::sort(List.begin(), List.end(), sorter);
2812
2813 // If we have no section (e.g. common), just write out
2814 // individual spans for each symbol.
2815 if (Section == NULL) {
2816 for (size_t n = 0; n < List.size(); n++) {
2817 const SymbolCU &Cur = List[n];
2818
2819 ArangeSpan Span;
2820 Span.Start = Cur.Sym;
2821 Span.End = NULL;
2822 if (Cur.CU)
2823 Spans[Cur.CU].push_back(Span);
2824 }
2825 } else {
2826 // Build spans between each label.
2827 const MCSymbol *StartSym = List[0].Sym;
2828 for (size_t n = 1; n < List.size(); n++) {
2829 const SymbolCU &Prev = List[n - 1];
2830 const SymbolCU &Cur = List[n];
2831
2832 // Try and build the longest span we can within the same CU.
2833 if (Cur.CU != Prev.CU) {
2834 ArangeSpan Span;
2835 Span.Start = StartSym;
2836 Span.End = Cur.Sym;
2837 Spans[Prev.CU].push_back(Span);
2838 StartSym = Cur.Sym;
2839 }
2840 }
2841 }
2842 }
2843
2844 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2845 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2846
2847 // Build a list of CUs used.
2848 std::vector<CompileUnit *> CUs;
2849 for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
2850 CompileUnit *CU = it->first;
2851 CUs.push_back(CU);
2852 }
2853
2854 // Sort the CU list (again, to ensure consistent output order).
2855 std::sort(CUs.begin(), CUs.end(), CUSort);
2856
2857 // Emit an arange table for each CU we used.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002858 for (size_t CUIdx = 0; CUIdx < CUs.size(); CUIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002859 CompileUnit *CU = CUs[CUIdx];
2860 std::vector<ArangeSpan> &List = Spans[CU];
2861
2862 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002863 unsigned ContentSize =
2864 sizeof(int16_t) + // DWARF ARange version number
2865 sizeof(int32_t) + // Offset of CU in the .debug_info section
2866 sizeof(int8_t) + // Pointer Size (in bytes)
2867 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002868
2869 unsigned TupleSize = PtrSize * 2;
2870
2871 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
2872 unsigned Padding = 0;
2873 while (((sizeof(int32_t) + ContentSize + Padding) % TupleSize) != 0)
2874 Padding++;
2875
2876 ContentSize += Padding;
2877 ContentSize += (List.size() + 1) * TupleSize;
2878
2879 // For each compile unit, write the list of spans it covers.
2880 Asm->OutStreamer.AddComment("Length of ARange Set");
2881 Asm->EmitInt32(ContentSize);
2882 Asm->OutStreamer.AddComment("DWARF Arange version number");
2883 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2884 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
2885 Asm->EmitSectionOffset(
2886 Asm->GetTempSymbol(ISec->getLabelBeginName(), CU->getUniqueID()),
2887 DwarfInfoSectionSym);
2888 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2889 Asm->EmitInt8(PtrSize);
2890 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2891 Asm->EmitInt8(0);
2892
2893 for (unsigned n = 0; n < Padding; n++)
2894 Asm->EmitInt8(0xff);
2895
2896 for (unsigned n = 0; n < List.size(); n++) {
2897 const ArangeSpan &Span = List[n];
2898 Asm->EmitLabelReference(Span.Start, PtrSize);
2899
2900 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002901 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002902 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002903 } else {
2904 // For symbols without an end marker (e.g. common), we
2905 // write a single arange entry containing just that one symbol.
2906 uint64_t Size = SymSize[Span.Start];
2907 if (Size == 0)
2908 Size = 1;
2909
2910 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2911 }
Richard Mitton21101b32013-09-19 23:21:01 +00002912 }
2913
2914 Asm->OutStreamer.AddComment("ARange terminator");
2915 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2916 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2917 }
Bill Wendling480ff322009-05-20 23:21:38 +00002918}
2919
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002920// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002921void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002922 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002923 Asm->OutStreamer.SwitchSection(
2924 Asm->getObjFileLowering().getDwarfRangesSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002925 unsigned char Size = Asm->getDataLayout().getPointerSize();
Eric Christopher4751d702013-11-23 00:05:29 +00002926 for (uint32_t i = 0, e = DebugRangeSymbols.size(); i < e; ++i) {
2927 // Only emit a symbol for every range pair for now.
2928 // FIXME: Make this per range list.
2929 if ((i % 2) == 0)
2930 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_ranges", i));
2931
2932 const MCSymbol *I = DebugRangeSymbols[i];
2933 if (I)
2934 Asm->OutStreamer.EmitSymbolValue(I, Size);
Devang Patel12563b32010-04-16 23:33:45 +00002935 else
Eric Christopherce0cfce2013-01-09 01:35:34 +00002936 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel12563b32010-04-16 23:33:45 +00002937 }
Bill Wendling480ff322009-05-20 23:21:38 +00002938}
2939
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002940// Emit visible names into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002941void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002942 if (const MCSection *LineInfo =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002943 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002944 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002945 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002946 }
2947}
2948
Eric Christopherd692c1d2012-12-11 19:42:09 +00002949// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002950
2951// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2952// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
Eric Christopher9a08f9e2013-10-01 00:43:36 +00002953// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher6fdf3242013-08-26 23:50:43 +00002954CompileUnit *DwarfDebug::constructSkeletonCU(const CompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002955
2956 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikieb01f13e2013-11-15 23:54:45 +00002957 CompileUnit *NewCU = new CompileUnit(CU->getUniqueID(), Die, CU->getNode(),
2958 Asm, this, &SkeletonHolder);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002959
Eric Christopherdae389b2013-02-22 23:50:08 +00002960 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikieb01f13e2013-11-15 23:54:45 +00002961 CU->getNode().getSplitDebugFilename());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002962
Eric Christopher44c6aa62013-04-22 07:51:08 +00002963 // Relocate to the beginning of the addr_base section, else 0 for the
2964 // beginning of the one for this compile unit.
Eric Christopher55863be2013-04-07 03:43:09 +00002965 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00002966 NewCU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base,
2967 DwarfAddrSectionSym);
Eric Christopher55863be2013-04-07 03:43:09 +00002968 else
Eric Christopher33ff6972013-11-21 23:46:41 +00002969 NewCU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002970
2971 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher962c9082013-01-15 23:56:56 +00002972 // into an entity. We're using 0, or a NULL label for this.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002973 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Eric Christopher962c9082013-01-15 23:56:56 +00002974
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002975 // DW_AT_stmt_list is a offset of line number information for this
2976 // compile unit in debug_line section.
Eric Christopher55863be2013-04-07 03:43:09 +00002977 // FIXME: Should handle multiple compile units.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002978 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00002979 NewCU->addSectionLabel(Die, dwarf::DW_AT_stmt_list,
2980 DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002981 else
Eric Christopher33ff6972013-11-21 23:46:41 +00002982 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002983
2984 if (!CompilationDir.empty())
Eric Christopher2cbd5762013-01-07 19:32:41 +00002985 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002986
Eric Christopher1d06eb52013-10-01 00:43:31 +00002987 // Flags to let the linker know we have emitted new style pubnames.
Eric Christopher39eebfa2013-09-30 23:14:16 +00002988 if (GenerateGnuPubSections) {
2989 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00002990 NewCU->addSectionLabel(
2991 Die, dwarf::DW_AT_GNU_pubnames,
2992 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002993 else
Eric Christopher33ff6972013-11-21 23:46:41 +00002994 NewCU->addSectionDelta(
2995 Die, dwarf::DW_AT_GNU_pubnames,
2996 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
2997 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002998
2999 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003000 NewCU->addSectionLabel(
3001 Die, dwarf::DW_AT_GNU_pubtypes,
3002 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00003003 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003004 NewCU->addSectionDelta(
3005 Die, dwarf::DW_AT_GNU_pubtypes,
3006 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
3007 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00003008 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00003009
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003010 // Flag if we've emitted any ranges and their location for the compile unit.
3011 if (DebugRangeSymbols.size()) {
3012 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003013 NewCU->addSectionLabel(Die, dwarf::DW_AT_GNU_ranges_base,
3014 DwarfDebugRangeSectionSym);
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003015 else
3016 NewCU->addUInt(Die, dwarf::DW_AT_GNU_ranges_base, dwarf::DW_FORM_data4,
3017 0);
3018 }
3019
Eric Christopherc8a310e2012-12-10 23:34:43 +00003020 SkeletonHolder.addUnit(NewCU);
3021
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003022 return NewCU;
3023}
3024
Eric Christopher3c5a1912012-12-19 22:02:53 +00003025void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
3026 assert(useSplitDwarf() && "No split dwarf debug info?");
3027 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003028}
3029
Eric Christopherd692c1d2012-12-11 19:42:09 +00003030// Emit the .debug_info.dwo section for separated dwarf. This contains the
3031// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003032void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00003033 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christophera2de8262012-12-15 00:04:07 +00003034 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher3c5a1912012-12-19 22:02:53 +00003035 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3036 DwarfAbbrevDWOSectionSym);
3037}
3038
3039// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3040// abbreviations for the .debug_info.dwo section.
3041void DwarfDebug::emitDebugAbbrevDWO() {
3042 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher48fef592012-12-20 21:58:40 +00003043 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3044 &Abbreviations);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003045}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003046
3047// Emit the .debug_str.dwo section for separated dwarf. This contains the
3048// string section and is identical in format to traditional .debug_str
3049// sections.
3050void DwarfDebug::emitDebugStrDWO() {
3051 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00003052 const MCSection *OffSec =
3053 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00003054 const MCSymbol *StrSym = DwarfStrSectionSym;
3055 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
3056 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003057}
David Blaikie409dd9c2013-11-19 23:08:21 +00003058
David Blaikie3c1d3322013-12-02 18:44:29 +00003059void DwarfDebug::addTypeUnitType(uint16_t Language, DIE *RefDie,
3060 DICompositeType CTy) {
Eric Christopherf52eddf2013-11-26 22:23:27 +00003061 DenseMap<const MDNode *,
3062 std::pair<uint64_t, SmallVectorImpl<DIE *> *> >::iterator I =
3063 TypeUnits.find(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003064 SmallVector<DIE *, 8> References;
3065 References.push_back(RefDie);
3066 if (I != TypeUnits.end()) {
3067 if (I->second.second) {
3068 I->second.second->push_back(RefDie);
3069 return;
3070 }
3071 } else {
3072 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie319a05f2013-12-02 19:33:10 +00003073 TypeUnit *NewTU = new TypeUnit(GlobalCUIndexCount++, UnitDie, Language, Asm,
3074 this, &InfoHolder);
3075 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie3c1d3322013-12-02 18:44:29 +00003076 Language);
David Blaikie409dd9c2013-11-19 23:08:21 +00003077
3078 // Register the type in the TypeUnits map with a vector of references to be
3079 // populated whenever a reference is required.
3080 I = TypeUnits.insert(std::make_pair(CTy, std::make_pair(0, &References)))
3081 .first;
3082
3083 // Construct the type, this may, recursively, require more type units that
3084 // may in turn require this type again - in which case they will add DIEs to
3085 // the References vector.
David Blaikie319a05f2013-12-02 19:33:10 +00003086 DIE *Die = NewTU->createTypeDIE(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003087
David Blaikie319a05f2013-12-02 19:33:10 +00003088 if (GenerateODRHash && shouldAddODRHash(NewTU, Die))
3089 NewTU->addUInt(UnitDie, dwarf::DW_AT_GNU_odr_signature,
David Blaikie409dd9c2013-11-19 23:08:21 +00003090 dwarf::DW_FORM_data8,
3091 DIEHash().computeDIEODRSignature(*Die));
3092 // FIXME: This won't handle circularly referential structures, as the DIE
3093 // may have references to other DIEs still under construction and missing
3094 // their signature. Hashing should walk through the signatures to their
3095 // referenced type, or possibly walk the precomputed hashes of related types
3096 // at the end.
3097 uint64_t Signature = DIEHash().computeTypeSignature(*Die);
3098
3099 // Remove the References vector and add the type hash.
3100 I->second.first = Signature;
3101 I->second.second = NULL;
3102
David Blaikie319a05f2013-12-02 19:33:10 +00003103 InfoHolder.addUnit(NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00003104 }
3105
3106 // Populate all the signatures.
3107 for (unsigned i = 0, e = References.size(); i != e; ++i) {
3108 CUMap.begin()->second->addUInt(References[i], dwarf::DW_AT_signature,
3109 dwarf::DW_FORM_ref_sig8, I->second.first);
3110 }
3111}