blob: 010dcdf05556a9a687d99055dafefd2f8cfc86a9 [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),
Eric Christopher0f63d062013-12-03 00:45:45 +0000188 GlobalRangeCount(0), InfoHolder(A, &AbbreviationsSet, Abbreviations,
189 "info_string", DIEValueAllocator),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000190 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;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +0000200 CurFn = 0; CurMI = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000201
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000202 // Turn on accelerator tables for Darwin by default, pubnames by
203 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000204 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000205
Eric Christopher574b5c82013-08-19 21:41:38 +0000206 if (DwarfAccelTables == Default)
207 HasDwarfAccelTables = IsDarwin;
208 else
Eric Christopher5297df02013-08-26 20:58:35 +0000209 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000210
Eric Christophercdf218d2012-12-10 19:51:21 +0000211 if (SplitDwarf == Default)
212 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000213 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000214 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000215
Eric Christopher4d36ca02013-08-26 23:24:35 +0000216 if (DwarfPubSections == Default)
217 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000218 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000219 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000220
Eric Christopher33ff6972013-11-21 23:46:41 +0000221 DwarfVersion = DwarfVersionNumber
222 ? DwarfVersionNumber
223 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000224
Dan Gohman6e681a52010-06-18 15:56:31 +0000225 {
226 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000227 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000228 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000229}
Bill Wendling2f921f82009-05-15 09:23:25 +0000230
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000231// Switch to the specified MCSection and emit an assembler
232// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000233static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000234 const char *SymbolStem = 0) {
235 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000236 if (!SymbolStem)
237 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000238
239 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
240 Asm->OutStreamer.EmitLabel(TmpSym);
241 return TmpSym;
242}
243
David Blaikie72f1a3e2013-11-23 01:17:34 +0000244DwarfUnits::~DwarfUnits() {
David Blaikie319a05f2013-12-02 19:33:10 +0000245 for (SmallVectorImpl<Unit *>::iterator I = CUs.begin(), E = CUs.end(); I != E;
246 ++I)
David Blaikie72f1a3e2013-11-23 01:17:34 +0000247 delete *I;
248}
249
Eric Christophere698f532012-12-20 21:58:36 +0000250MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000251 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000252}
253
Eric Christophere698f532012-12-20 21:58:36 +0000254MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000255 std::pair<MCSymbol *, unsigned> &Entry =
256 StringPool.GetOrCreateValue(Str).getValue();
257 if (Entry.first)
258 return Entry.first;
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000259
260 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000261 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000262}
263
Eric Christopher2cbd5762013-01-07 19:32:41 +0000264unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000265 std::pair<MCSymbol *, unsigned> &Entry =
266 StringPool.GetOrCreateValue(Str).getValue();
267 if (Entry.first)
268 return Entry.second;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000269
270 Entry.second = NextStringPoolNumber++;
271 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
272 return Entry.second;
273}
274
David Blaikiedea547b2013-06-28 18:47:14 +0000275unsigned DwarfUnits::getAddrPoolIndex(const MCSymbol *Sym) {
David Blaikie8466ca82013-07-01 23:55:52 +0000276 return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
277}
278
Ulrich Weigand8b3d2262013-07-02 18:46:46 +0000279unsigned DwarfUnits::getAddrPoolIndex(const MCExpr *Sym) {
280 std::pair<DenseMap<const MCExpr *, unsigned>::iterator, bool> P =
David Blaikieb8ef7852013-06-28 18:47:19 +0000281 AddressPool.insert(std::make_pair(Sym, NextAddrPoolNumber));
David Blaikiea67de2b2013-06-28 18:55:13 +0000282 if (P.second)
283 ++NextAddrPoolNumber;
David Blaikieb8ef7852013-06-28 18:47:19 +0000284 return P.first->second;
Eric Christopher962c9082013-01-15 23:56:56 +0000285}
286
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000287// Define a unique number for the abbreviation.
288//
Eric Christopherc8a310e2012-12-10 23:34:43 +0000289void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000290 // Check the set for priors.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000291 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000292
293 // If it's newly added.
294 if (InSet == &Abbrev) {
295 // Add to abbreviation list.
David Blaikie2d4e1122013-10-30 17:14:24 +0000296 Abbreviations.push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000297
298 // Assign the vector position + 1 as its number.
David Blaikie2d4e1122013-10-30 17:14:24 +0000299 Abbrev.setNumber(Abbreviations.size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000300 } else {
301 // Assign existing abbreviation number.
302 Abbrev.setNumber(InSet->getNumber());
303 }
304}
305
Eric Christopherd9843b32011-11-10 19:25:34 +0000306static bool isObjCClass(StringRef Name) {
307 return Name.startswith("+") || Name.startswith("-");
308}
309
310static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000311 if (!isObjCClass(Name))
312 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000313
Benjamin Kramer260de742013-08-24 12:15:54 +0000314 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000315}
316
317static void getObjCClassCategory(StringRef In, StringRef &Class,
318 StringRef &Category) {
319 if (!hasObjCCategory(In)) {
320 Class = In.slice(In.find('[') + 1, In.find(' '));
321 Category = "";
322 return;
323 }
324
325 Class = In.slice(In.find('[') + 1, In.find('('));
326 Category = In.slice(In.find('[') + 1, In.find(' '));
327 return;
328}
329
330static StringRef getObjCMethodName(StringRef In) {
331 return In.slice(In.find(' ') + 1, In.find(']'));
332}
333
Richard Mittonc2508242013-10-03 22:07:08 +0000334// Helper for sorting sections into a stable output order.
335static bool SectionSort(const MCSection *A, const MCSection *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000336 std::string LA = (A ? A->getLabelBeginName() : "");
337 std::string LB = (B ? B->getLabelBeginName() : "");
338 return LA < LB;
Richard Mittonc2508242013-10-03 22:07:08 +0000339}
340
Eric Christopherd9843b32011-11-10 19:25:34 +0000341// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000342// TODO: Determine whether or not we should add names for programs
343// that do not have a DW_AT_name or DW_AT_linkage_name field - this
344// is only slightly different than the lookup of non-standard ObjC names.
David Blaikie2a80e442013-12-02 22:09:48 +0000345static void addSubprogramNames(Unit *TheU, DISubprogram SP, DIE *Die) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000346 if (!SP.isDefinition())
347 return;
David Blaikie2a80e442013-12-02 22:09:48 +0000348 TheU->addAccelName(SP.getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000349
350 // If the linkage name is different than the name, go ahead and output
351 // that as well into the name table.
352 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
David Blaikie2a80e442013-12-02 22:09:48 +0000353 TheU->addAccelName(SP.getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000354
355 // If this is an Objective-C selector name add it to the ObjC accelerator
356 // too.
357 if (isObjCClass(SP.getName())) {
358 StringRef Class, Category;
359 getObjCClassCategory(SP.getName(), Class, Category);
David Blaikie2a80e442013-12-02 22:09:48 +0000360 TheU->addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000361 if (Category != "")
David Blaikie2a80e442013-12-02 22:09:48 +0000362 TheU->addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000363 // Also add the base method name to the name table.
David Blaikie2a80e442013-12-02 22:09:48 +0000364 TheU->addAccelName(getObjCMethodName(SP.getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000365 }
366}
367
Manman Ren3eb9dff2013-09-09 19:05:21 +0000368/// isSubprogramContext - Return true if Context is either a subprogram
369/// or another context nested inside a subprogram.
370bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
371 if (!Context)
372 return false;
373 DIDescriptor D(Context);
374 if (D.isSubprogram())
375 return true;
376 if (D.isType())
Manman Ren116868e2013-09-09 19:47:11 +0000377 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000378 return false;
379}
380
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000381// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
382// and DW_AT_high_pc attributes. If there are global variables in this
383// scope then create and insert DIEs for these variables.
David Blaikie25bc7192013-11-15 23:13:08 +0000384DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU, DISubprogram SP) {
385 DIE *SPDie = SPCU->getDIE(SP);
Devang Patela37a95e2010-07-07 22:20:57 +0000386
Chris Lattner3a383cb2010-04-05 00:13:49 +0000387 assert(SPDie && "Unable to find subprogram DIE!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000388
Bill Wendlingf720bf62012-11-07 05:19:04 +0000389 // If we're updating an abstract DIE, then we will be adding the children and
390 // object pointer later on. But what we don't want to do is process the
391 // concrete DIE twice.
David Blaikie25bc7192013-11-15 23:13:08 +0000392 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000393 // Pick up abstract subprogram DIE.
David Blaikie2a80e442013-12-02 22:09:48 +0000394 SPDie =
395 SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000396 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000397 } else {
398 DISubprogram SPDecl = SP.getFunctionDeclaration();
399 if (!SPDecl.isSubprogram()) {
400 // There is not any need to generate specification DIE for a function
401 // defined at compile unit level. If a function is defined inside another
402 // function then gdb prefers the definition at top level and but does not
403 // expect specification DIE in parent function. So avoid creating
404 // specification DIE for a function defined inside a function.
Manman Renc50fa112013-10-10 18:40:01 +0000405 DIScope SPContext = resolve(SP.getContext());
406 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000407 !SPContext.isFile() && !isSubprogramContext(SPContext)) {
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000408 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
409
410 // Add arguments.
411 DICompositeType SPTy = SP.getType();
412 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000413 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000414 if (SPTag == dwarf::DW_TAG_subroutine_type)
415 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Ren4a841a82013-10-29 01:03:01 +0000416 DIE *Arg =
417 SPCU->createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie5af2aca2013-11-18 23:57:26 +0000418 DIType ATy(Args.getElement(i));
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000419 SPCU->addType(Arg, ATy);
420 if (ATy.isArtificial())
421 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
422 if (ATy.isObjectPointer())
Manman Ren4c4b69c2013-10-11 23:58:05 +0000423 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, Arg);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000424 }
425 DIE *SPDeclDie = SPDie;
David Blaikie2a80e442013-12-02 22:09:48 +0000426 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
427 *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000428 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000429 }
430 }
Devang Patela37a95e2010-07-07 22:20:57 +0000431 }
432
Eric Christopher0f63d062013-12-03 00:45:45 +0000433 MCSymbol *FuncBegin =
434 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
435 MCSymbol *FuncEnd = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
436 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc, FuncBegin);
437 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc, FuncEnd);
438
Chris Lattner3a383cb2010-04-05 00:13:49 +0000439 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
440 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000441 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000442
Eric Christopherd9843b32011-11-10 19:25:34 +0000443 // Add name to the name table, we do this here because we're guaranteed
444 // to have concrete versions of our DW_TAG_subprogram nodes.
445 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000446
Chris Lattner3a383cb2010-04-05 00:13:49 +0000447 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000448}
449
Manman Ren5b2f4b02013-09-11 19:40:28 +0000450/// Check whether we should create a DIE for the given Scope, return true
451/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000452bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
453 if (Scope->isAbstractScope())
454 return false;
455
Manman Ren5b2f4b02013-09-11 19:40:28 +0000456 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000457 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
458 if (Ranges.empty())
459 return true;
460
461 if (Ranges.size() > 1)
462 return false;
463
Manman Ren5b2f4b02013-09-11 19:40:28 +0000464 // We don't create a DIE if we have a single Range and the end label
465 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000466 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
467 MCSymbol *End = getLabelAfterInsn(RI->second);
468 return !End;
469}
470
Eric Christopherbe2513e2013-12-03 00:45:59 +0000471void DwarfDebug::addScopeRangeList(CompileUnit *TheCU, DIE *ScopeDIE,
472 const SmallVectorImpl<InsnRange> &Range) {
473 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
474 // emitting it appropriately.
475 TheCU->addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges,
476 Asm->GetTempSymbol("debug_ranges", GlobalRangeCount));
477 RangeSpanList *List = new RangeSpanList(GlobalRangeCount++);
478 for (SmallVectorImpl<InsnRange>::const_iterator RI = Range.begin(),
479 RE = Range.end();
480 RI != RE; ++RI) {
481 RangeSpan Span(getLabelBeforeInsn(RI->first),
482 getLabelAfterInsn(RI->second));
483 List->addRange(Span);
484 }
485
486 // Add the range list to the set of ranges to be emitted.
487 TheCU->addRangeList(List);
488}
489
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000490// Construct new DW_TAG_lexical_block for this scope and attach
491// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher6a841382012-11-19 22:42:10 +0000492DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000493 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000494 if (isLexicalScopeDIENull(Scope))
495 return 0;
496
Devang Patel6c74a872010-04-27 19:46:33 +0000497 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
498 if (Scope->isAbstractScope())
499 return ScopeDIE;
500
Eric Christopher0f63d062013-12-03 00:45:45 +0000501 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherbe2513e2013-12-03 00:45:59 +0000502
Eric Christopherdc42ea82013-07-03 01:57:28 +0000503 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000504 if (ScopeRanges.size() > 1) {
Eric Christopherbe2513e2013-12-03 00:45:59 +0000505 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
Devang Patel6c74a872010-04-27 19:46:33 +0000506 return ScopeDIE;
507 }
508
Eric Christopherdc42ea82013-07-03 01:57:28 +0000509 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000510 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000511 MCSymbol *Start = getLabelBeforeInsn(RI->first);
512 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000513 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000514
Chris Lattnere13c3722010-03-09 01:58:53 +0000515 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
516 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000517
Eric Christopher962c9082013-01-15 23:56:56 +0000518 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
519 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000520
521 return ScopeDIE;
522}
523
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000524// This scope represents inlined body of a function. Construct DIE to
525// represent this concrete inlined copy of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000526DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
527 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000528 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
529 assert(ScopeRanges.empty() == false &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000530 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000531
Devang Patelf098ce22011-07-27 00:34:13 +0000532 if (!Scope->getScopeNode())
533 return NULL;
534 DIScope DS(Scope->getScopeNode());
535 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000536 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000537 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000538 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000539 return NULL;
540 }
541
Devang Patel73bc1722011-05-05 17:54:26 +0000542 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000543 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000544
Eric Christopherbe2513e2013-12-03 00:45:59 +0000545 // If we have multiple ranges, emit them into the range section.
546 if (ScopeRanges.size() > 1)
547 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
548 else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000549 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000550 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
551 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
552
553 if (StartLabel == 0 || EndLabel == 0)
554 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
555
556 assert(StartLabel->isDefined() &&
557 "Invalid starting label for an inlined scope!");
558 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
559
Eric Christopher962c9082013-01-15 23:56:56 +0000560 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
561 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000562 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000563
564 InlinedSubprogramDIEs.insert(OriginDIE);
565
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000566 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000567 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000568 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000569 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
570 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000571 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000572
Eric Christopher8dda5d02011-12-04 06:02:38 +0000573 // Add name to the name table, we do this here because we're guaranteed
574 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
575 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000576
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000577 return ScopeDIE;
578}
579
Manman Ren2312ed32013-09-10 18:40:41 +0000580DIE *DwarfDebug::createScopeChildrenDIE(CompileUnit *TheCU, LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000581 SmallVectorImpl<DIE *> &Children) {
582 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000583
584 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000585 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000586 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
587 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000588 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000589 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000590 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000591 if (ArgDV->isObjectPointer())
592 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000593 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000594
Eric Christopherf84354b2011-10-03 15:49:16 +0000595 // Collect lexical scope children first.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000596 const SmallVectorImpl<DbgVariable *> &Variables =
597 ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000598 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000599 if (DIE *Variable = TheCU->constructVariableDIE(*Variables[i],
600 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000601 Children.push_back(Variable);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000602 if (Variables[i]->isObjectPointer())
603 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000604 }
Craig Topper977e9cd2013-07-03 04:24:43 +0000605 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000606 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000607 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000608 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000609 return ObjectPointer;
610}
611
612// Construct a DIE for this scope.
613DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
614 if (!Scope || !Scope->getScopeNode())
615 return NULL;
616
617 DIScope DS(Scope->getScopeNode());
618
619 SmallVector<DIE *, 8> Children;
620 DIE *ObjectPointer = NULL;
621 bool ChildrenCreated = false;
622
Manman Ren5b2f4b02013-09-11 19:40:28 +0000623 // We try to create the scope DIE first, then the children DIEs. This will
624 // avoid creating un-used children then removing them later when we find out
625 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000626 DIE *ScopeDIE = NULL;
627 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000628 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000629 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000630 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000631 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000632 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000633 // Note down abstract DIE.
634 if (ScopeDIE)
635 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000636 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000637 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000638 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000639 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000640 if (isLexicalScopeDIENull(Scope))
641 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000642
643 // We create children here when we know the scope DIE is not going to be
644 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000645 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
646 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000647
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000648 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000649 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000650 ImportedEntityMap::const_iterator> Range =
651 std::equal_range(
652 ScopesWithImportedEntities.begin(),
653 ScopesWithImportedEntities.end(),
654 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
655 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000656 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000657 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000658 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000659 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000660 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
661 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000662 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000663 }
Eric Christopher6a841382012-11-19 22:42:10 +0000664
Benjamin Kramer892daba2013-08-24 11:55:49 +0000665 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000666 assert(Children.empty() &&
667 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000668 return NULL;
669 }
Manman Ren2312ed32013-09-10 18:40:41 +0000670 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000671 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000672 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000673
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000674 // Add children
Craig Topperd8e43652013-07-03 04:17:25 +0000675 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000676 E = Children.end();
677 I != E; ++I)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000678 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000679
Eric Christophere3417762012-09-12 23:36:19 +0000680 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000681 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000682
Eric Christopherd9843b32011-11-10 19:25:34 +0000683 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000684}
685
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000686// Look up the source id with the given directory and source file names.
687// If none currently exists, create a new id and insert it in the
688// SourceIds map. This can update DirectoryNames and SourceFileNames maps
689// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000690unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
691 unsigned CUID) {
Manman Ren1e427202013-03-07 01:42:00 +0000692 // If we use .loc in assembly, we can't separate .file entries according to
693 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000694
695 // FIXME: add a better feature test than hasRawTextSupport. Even better,
696 // extend .file to support this.
697 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000698 CUID = 0;
699
Devang Patel871d0b12010-09-16 20:57:49 +0000700 // If FE did not provide a file name, then assume stdin.
701 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000702 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000703
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000704 // TODO: this might not belong here. See if we can factor this better.
705 if (DirName == CompilationDir)
706 DirName = "";
707
Manman Ren1e427202013-03-07 01:42:00 +0000708 // FileIDCUMap stores the current ID for the given compile unit.
709 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000710
Manman Ren1e427202013-03-07 01:42:00 +0000711 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000712 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000713 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000714 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000715 NamePair += DirName;
716 NamePair += '\0'; // Zero bytes are not allowed in paths.
717 NamePair += FileName;
718
719 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
720 if (Ent.getValue() != SrcId)
721 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000722
Manman Ren1e427202013-03-07 01:42:00 +0000723 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000724 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000725 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000726
727 return SrcId;
728}
729
Eric Christopher48fef592012-12-20 21:58:40 +0000730// Create new CompileUnit for the given metadata node with tag
731// DW_TAG_compile_unit.
David Blaikie5a152402013-11-15 23:52:02 +0000732CompileUnit *DwarfDebug::constructCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000733 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000734 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000735
736 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikie5a152402013-11-15 23:52:02 +0000737 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++, Die, DIUnit, Asm,
738 this, &InfoHolder);
Manman Ren1e427202013-03-07 01:42:00 +0000739
740 FileIDCUMap[NewCU->getUniqueID()] = 0;
741 // Call this to emit a .file directive if it wasn't emitted for the source
742 // file this CU comes from yet.
743 getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
744
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000745 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000746 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
747 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000748 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000749
Eric Christopherb1b94512012-08-01 18:19:01 +0000750 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher52ce7182013-04-09 19:23:15 +0000751 // into an entity. We're using 0 (or a NULL label) for this. For
752 // split dwarf it's in the skeleton CU so omit it here.
753 if (!useSplitDwarf())
754 NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
Manman Ren4e042a62013-02-05 21:52:47 +0000755
756 // Define start line table label for each Compile Unit.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000757 MCSymbol *LineTableStartSym =
758 Asm->GetTempSymbol("line_table_start", NewCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +0000759 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
760 NewCU->getUniqueID());
761
Manman Ren9d4c7352013-05-21 00:57:22 +0000762 // Use a single line table if we are using .loc and generating assembly.
763 bool UseTheFirstCU =
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000764 (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport()) ||
765 (NewCU->getUniqueID() == 0);
Manman Ren9d4c7352013-05-21 00:57:22 +0000766
Eric Christopher52ce7182013-04-09 19:23:15 +0000767 if (!useSplitDwarf()) {
Eric Christophera51d3fc2013-09-27 22:50:48 +0000768 // DW_AT_stmt_list is a offset of line number information for this
769 // compile unit in debug_line section. For split dwarf this is
770 // left in the skeleton CU and so not included.
771 // The line table entries are not always emitted in assembly, so it
772 // is not okay to use line_table_start here.
Eric Christopher52ce7182013-04-09 19:23:15 +0000773 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000774 NewCU->addSectionLabel(
775 Die, dwarf::DW_AT_stmt_list,
776 UseTheFirstCU ? Asm->GetTempSymbol("section_line")
777 : LineTableStartSym);
Manman Ren9d4c7352013-05-21 00:57:22 +0000778 else if (UseTheFirstCU)
Eric Christopher33ff6972013-11-21 23:46:41 +0000779 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher52ce7182013-04-09 19:23:15 +0000780 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000781 NewCU->addSectionDelta(Die, dwarf::DW_AT_stmt_list,
782 LineTableStartSym, DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000783
784 // If we're using split dwarf the compilation dir is going to be in the
785 // skeleton CU and so we don't need to duplicate it here.
786 if (!CompilationDir.empty())
787 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
788
Eric Christopher1d06eb52013-10-01 00:43:31 +0000789 // Flags to let the linker know we have emitted new style pubnames. Only
Eric Christophera51d3fc2013-09-27 22:50:48 +0000790 // emit it here if we don't have a skeleton CU for split dwarf.
Eric Christopher39eebfa2013-09-30 23:14:16 +0000791 if (GenerateGnuPubSections) {
792 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000793 NewCU->addSectionLabel(
794 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000795 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000796 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000797 NewCU->addSectionDelta(
798 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000799 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
800 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000801
802 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000803 NewCU->addSectionLabel(
804 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000805 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000806 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000807 NewCU->addSectionDelta(
808 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000809 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
810 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000811 }
Eric Christopher52ce7182013-04-09 19:23:15 +0000812 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000813
Bill Wendling2b128d72009-05-20 23:19:06 +0000814 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000815 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000816
Devang Patel2d9caf92009-11-25 17:36:49 +0000817 StringRef Flags = DIUnit.getFlags();
818 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000819 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000820
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000821 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000822 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000823 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000824
Devang Patel1a0df9a2010-05-10 22:49:55 +0000825 if (!FirstCU)
826 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000827
Eric Christopherc8a310e2012-12-10 23:34:43 +0000828 InfoHolder.addUnit(NewCU);
829
David Blaikie5a152402013-11-15 23:52:02 +0000830 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000831 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000832 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000833}
834
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000835// Construct subprogram DIE.
Eric Christophera6c38a32013-10-15 23:31:38 +0000836void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000837 // FIXME: We should only call this routine once, however, during LTO if a
838 // program is defined in multiple CUs we could end up calling it out of
839 // beginModule as we walk the CUs.
840
841 CompileUnit *&CURef = SPMap[N];
842 if (CURef)
843 return;
844 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000845
Devang Patel80ae3492009-08-28 23:24:31 +0000846 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000847 if (!SP.isDefinition())
848 // This is a method declaration which will be handled while constructing
849 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000850 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000851
Devang Patel89543712011-08-15 17:24:54 +0000852 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000853
Eric Christopherba506db2013-09-09 20:03:20 +0000854 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000855 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000856}
857
David Blaikie1fd43652013-05-07 21:35:53 +0000858void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000859 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000860 DIImportedEntity Module(N);
David Blaikief55abea2013-04-22 06:12:31 +0000861 if (!Module.Verify())
862 return;
David Blaikie684fc532013-05-06 23:33:07 +0000863 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000864 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000865}
866
David Blaikie4dd2de72013-05-08 06:01:38 +0000867void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
David Blaikie684fc532013-05-06 23:33:07 +0000868 DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000869 DIImportedEntity Module(N);
David Blaikie684fc532013-05-06 23:33:07 +0000870 if (!Module.Verify())
871 return;
David Blaikie4dd2de72013-05-08 06:01:38 +0000872 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000873}
874
David Blaikie4dd2de72013-05-08 06:01:38 +0000875void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000876 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000877 DIE *Context) {
878 assert(Module.Verify() &&
879 "Use one of the MDNode * overloads to handle invalid metadata");
880 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000881 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000882 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000883 DIE *EntityDie;
884 DIDescriptor Entity = Module.getEntity();
885 if (Entity.isNameSpace())
886 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
887 else if (Entity.isSubprogram())
888 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000889 else if (Entity.isType())
890 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000891 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000892 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000893 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
894 Module.getContext().getDirectory(),
895 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000896 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
897 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000898 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000899 StringRef Name = Module.getName();
900 if (!Name.empty())
901 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000902 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000903}
904
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000905// Emit all Dwarf sections that should come prior to the content. Create
906// global DIEs and emit initial debug info sections. This is invoked by
907// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000908void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000909 if (DisableDebugInfoPrinting)
910 return;
911
Eric Christopher58f41952012-11-19 22:42:15 +0000912 const Module *M = MMI->getModule();
913
Nick Lewycky019d2552011-07-29 03:49:23 +0000914 // If module has named metadata anchors then use them, otherwise scan the
915 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000916 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000917 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000918 return;
Manman Ren60352032013-09-05 18:48:31 +0000919 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000920
David Blaikiedc69ebb2013-03-11 23:39:23 +0000921 // Emit initial sections so we can reference labels later.
922 emitSectionLabels();
923
924 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
925 DICompileUnit CUNode(CU_Nodes->getOperand(i));
926 CompileUnit *CU = constructCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000927 DIArray ImportedEntities = CUNode.getImportedEntities();
928 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000929 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000930 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
931 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000932 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000933 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000934 DIArray GVs = CUNode.getGlobalVariables();
935 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000936 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000937 DIArray SPs = CUNode.getSubprograms();
938 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
939 constructSubprogramDIE(CU, SPs.getElement(i));
940 DIArray EnumTypes = CUNode.getEnumTypes();
941 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
942 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
943 DIArray RetainedTypes = CUNode.getRetainedTypes();
944 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
945 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000946 // Emit imported_modules last so that the relevant context is already
947 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000948 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
949 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000950 }
Eric Christopher6a841382012-11-19 22:42:10 +0000951
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000952 // Tell MMI that we have debug info.
953 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000954
Bill Wendling2b128d72009-05-20 23:19:06 +0000955 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000956 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000957}
958
Eric Christopher960ac372012-11-22 00:59:49 +0000959// Attach DW_AT_inline attribute with inlined subprogram DIEs.
960void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000961 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
962 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000963 AE = InlinedSubprogramDIEs.end();
964 AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000965 DIE *ISP = *AI;
David Blaikief2443192013-10-21 17:28:37 +0000966 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000967 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000968 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000969 AE = AbstractSPDies.end();
970 AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000971 DIE *ISP = AI->second;
972 if (InlinedSubprogramDIEs.count(ISP))
973 continue;
David Blaikief2443192013-10-21 17:28:37 +0000974 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000975 }
Eric Christopher960ac372012-11-22 00:59:49 +0000976}
977
978// Collect info for variables that were optimized out.
979void DwarfDebug::collectDeadVariables() {
980 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000981
982 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
983 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
984 DICompileUnit TheCU(CU_Nodes->getOperand(i));
985 DIArray Subprograms = TheCU.getSubprograms();
986 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000987 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000988 if (ProcessedSPNodes.count(SP) != 0)
989 continue;
990 if (!SP.isSubprogram())
991 continue;
992 if (!SP.isDefinition())
993 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000994 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +0000995 if (Variables.getNumElements() == 0)
996 continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000997
Eric Christopher735401c2012-11-27 00:13:51 +0000998 // Construct subprogram DIE and add variables DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +0000999 CompileUnit *SPCU = static_cast<CompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +00001000 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +00001001 // FIXME: See the comment in constructSubprogramDIE about duplicate
1002 // subprogram DIEs.
1003 constructSubprogramDIE(SPCU, SP);
1004 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +00001005 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
1006 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +00001007 if (!DV.isVariable())
1008 continue;
Manman Renb3388602013-10-05 01:43:03 +00001009 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001010 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +00001011 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +00001012 }
Eric Christopher960ac372012-11-22 00:59:49 +00001013 }
1014 }
1015 }
Eric Christopher960ac372012-11-22 00:59:49 +00001016}
1017
Eric Christopher45731982013-08-08 23:45:55 +00001018// Type Signature [7.27] and ODR Hash code.
Eric Christopher67646432013-07-26 17:02:41 +00001019
1020/// \brief Grabs the string in whichever attribute is passed in and returns
Eric Christopher8552e222013-08-07 01:18:33 +00001021/// a reference to it. Returns "" if the attribute doesn't exist.
Eric Christopher67646432013-07-26 17:02:41 +00001022static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
Eric Christopher8552e222013-08-07 01:18:33 +00001023 DIEValue *V = Die->findAttribute(Attr);
Eric Christopher67646432013-07-26 17:02:41 +00001024
Eric Christopher8552e222013-08-07 01:18:33 +00001025 if (DIEString *S = dyn_cast_or_null<DIEString>(V))
1026 return S->getString();
1027
Eric Christopher67646432013-07-26 17:02:41 +00001028 return StringRef("");
1029}
1030
Eric Christopher67646432013-07-26 17:02:41 +00001031/// Return true if the current DIE is contained within an anonymous namespace.
1032static bool isContainedInAnonNamespace(DIE *Die) {
1033 DIE *Parent = Die->getParent();
1034
1035 while (Parent) {
Eric Christophere414ece2013-07-29 23:53:08 +00001036 if (Parent->getTag() == dwarf::DW_TAG_namespace &&
1037 getDIEStringAttr(Parent, dwarf::DW_AT_name) == "")
Eric Christopher67646432013-07-26 17:02:41 +00001038 return true;
1039 Parent = Parent->getParent();
1040 }
1041
1042 return false;
1043}
1044
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001045/// Test if the current CU language is C++ and that we have
1046/// a named type that is not contained in an anonymous namespace.
David Blaikie319a05f2013-12-02 19:33:10 +00001047static bool shouldAddODRHash(TypeUnit *CU, DIE *Die) {
Eric Christopher341770d2013-08-07 08:35:10 +00001048 return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
1049 getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
1050 !isContainedInAnonNamespace(Die);
Eric Christopher45731982013-08-08 23:45:55 +00001051}
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001052
Eric Christopher960ac372012-11-22 00:59:49 +00001053void DwarfDebug::finalizeModuleInfo() {
1054 // Collect info for variables that were optimized out.
1055 collectDeadVariables();
1056
1057 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1058 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001059
Eric Christopher60eb7692013-08-12 20:27:48 +00001060 // Handle anything that needs to be done on a per-cu basis.
David Blaikie319a05f2013-12-02 19:33:10 +00001061 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
1062 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00001063 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00001064 Unit *TheU = *I;
Eric Christopher60eb7692013-08-12 20:27:48 +00001065 // Emit DW_AT_containing_type attribute to connect types with their
1066 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +00001067 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +00001068
1069 // If we're splitting the dwarf out now that we've got the entire
1070 // CU then construct a skeleton CU based upon it.
David Blaikie319a05f2013-12-02 19:33:10 +00001071 if (useSplitDwarf() &&
David Blaikie2a80e442013-12-02 22:09:48 +00001072 TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
Eric Christopherd29614f2013-08-13 01:21:55 +00001073 uint64_t ID = 0;
1074 if (GenerateCUHash) {
1075 DIEHash CUHash;
David Blaikie2a80e442013-12-02 22:09:48 +00001076 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
Eric Christopherd29614f2013-08-13 01:21:55 +00001077 }
Eric Christopher60eb7692013-08-12 20:27:48 +00001078 // This should be a unique identifier when we want to build .dwp files.
David Blaikie2a80e442013-12-02 22:09:48 +00001079 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1080 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001081 // Now construct the skeleton CU associated.
David Blaikie2a80e442013-12-02 22:09:48 +00001082 CompileUnit *SkCU = constructSkeletonCU(static_cast<CompileUnit *>(TheU));
Eric Christopher60eb7692013-08-12 20:27:48 +00001083 // This should be a unique identifier when we want to build .dwp files.
David Blaikie2a80e442013-12-02 22:09:48 +00001084 SkCU->addUInt(SkCU->getUnitDie(), 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 }
1087 }
1088
1089 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001090 InfoHolder.computeSizeAndOffsets();
1091 if (useSplitDwarf())
1092 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001093}
1094
1095void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001096 // Filter labels by section.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001097 for (size_t n = 0; n < ArangeLabels.size(); n++) {
1098 const SymbolCU &SCU = ArangeLabels[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001099 if (SCU.Sym->isInSection()) {
1100 // Make a note of this symbol and it's section.
1101 const MCSection *Section = &SCU.Sym->getSection();
1102 if (!Section->getKind().isMetadata())
1103 SectionMap[Section].push_back(SCU);
1104 } else {
1105 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1106 // appear in the output. This sucks as we rely on sections to build
1107 // arange spans. We can do it without, but it's icky.
1108 SectionMap[NULL].push_back(SCU);
1109 }
1110 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001111
Richard Mittonc2508242013-10-03 22:07:08 +00001112 // Build a list of sections used.
1113 std::vector<const MCSection *> Sections;
Richard Mitton21101b32013-09-19 23:21:01 +00001114 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
1115 it++) {
1116 const MCSection *Section = it->first;
Richard Mittonc2508242013-10-03 22:07:08 +00001117 Sections.push_back(Section);
1118 }
1119
1120 // Sort the sections into order.
1121 // This is only done to ensure consistent output order across different runs.
1122 std::sort(Sections.begin(), Sections.end(), SectionSort);
1123
1124 // Add terminating symbols for each section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001125 for (unsigned ID = 0; ID < Sections.size(); ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001126 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001127 MCSymbol *Sym = NULL;
1128
1129 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001130 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001131 // if we know the section name up-front. For user-created sections, the
1132 // resulting
1133 // label may not be valid to use as a label. (section names can use a
1134 // greater
Richard Mittonc2508242013-10-03 22:07:08 +00001135 // set of characters on some systems)
1136 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001137 Asm->OutStreamer.SwitchSection(Section);
1138 Asm->OutStreamer.EmitLabel(Sym);
1139 }
1140
1141 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001142 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001143 }
Eric Christopher960ac372012-11-22 00:59:49 +00001144}
Bill Wendling2b128d72009-05-20 23:19:06 +00001145
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001146// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001147void DwarfDebug::endModule() {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001148 assert(CurFn == 0);
1149 assert(CurMI == 0);
Eric Christopher960ac372012-11-22 00:59:49 +00001150
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001151 if (!FirstCU)
1152 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001153
1154 // End any existing sections.
1155 // TODO: Does this need to happen?
1156 endSections();
1157
1158 // Finalize the debug info for the module.
1159 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001160
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001161 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001162
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001163 // Emit all the DIEs into a debug info section.
1164 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001165
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001166 // Corresponding abbreviations into a abbrev section.
1167 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001168
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001169 // Emit info into a debug loc section.
1170 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001171
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001172 // Emit info into a debug aranges section.
1173 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001174
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001175 // Emit info into a debug ranges section.
1176 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001177
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001178 // Emit info into a debug macinfo section.
1179 emitDebugMacInfo();
Eric Christopher95198f502012-11-27 22:43:42 +00001180
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001181 if (useSplitDwarf()) {
1182 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001183 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001184 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001185 // Emit DWO addresses.
1186 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001187 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001188
Eric Christophera876b822012-08-23 07:32:06 +00001189 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001190 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001191 emitAccelNames();
1192 emitAccelObjC();
1193 emitAccelNamespaces();
1194 emitAccelTypes();
1195 }
Eric Christopher6a841382012-11-19 22:42:10 +00001196
Eric Christopher4b358182013-08-30 00:40:17 +00001197 // Emit the pubnames and pubtypes sections if requested.
1198 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001199 emitDebugPubNames(GenerateGnuPubSections);
1200 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001201 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001202
Devang Pateld0701282010-08-02 17:32:15 +00001203 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001204 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001205
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001206 // Reset these for the next Module if we have one.
1207 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001208}
1209
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001210// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001211DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001212 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001213 LLVMContext &Ctx = DV->getContext();
1214 // More then one inlined variable corresponds to one abstract variable.
1215 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001216 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001217 if (AbsDbgVariable)
1218 return AbsDbgVariable;
1219
Devang Patel7e623022011-08-10 20:55:27 +00001220 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001221 if (!Scope)
1222 return NULL;
1223
Manman Renb3388602013-10-05 01:43:03 +00001224 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001225 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001226 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001227 return AbsDbgVariable;
1228}
1229
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001230// If Var is a current function argument then add it to CurrentFnArguments list.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001231bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +00001232 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001233 return false;
1234 DIVariable DV = Var->getVariable();
1235 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1236 return false;
1237 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001238 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001239 return false;
1240
Devang Patel4ab660b2011-03-03 20:02:02 +00001241 size_t Size = CurrentFnArguments.size();
1242 if (Size == 0)
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001243 CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001244 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001245 // arguments does the function have at source level.
1246 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001247 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001248 CurrentFnArguments[ArgNo - 1] = Var;
1249 return true;
1250}
1251
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001252// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001253void DwarfDebug::collectVariableInfoFromMMITable(
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001254 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001255 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1256 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001257 VE = VMap.end();
1258 VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001259 const MDNode *Var = VI->first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001260 if (!Var)
1261 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001262 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001263 DIVariable DV(Var);
1264 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001265
Devang Patel7e623022011-08-10 20:55:27 +00001266 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001267
Devang Patelcdb7d442009-11-10 23:20:04 +00001268 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001269 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001270 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001271
Devang Patele1c53f22010-05-20 16:36:41 +00001272 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001273 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001274 RegVar->setFrameIndex(VP.first);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001275 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001276 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001277 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001278 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001279 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001280}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001281
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001282// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1283// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001284static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001285 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001286 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1287 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001288 (MI->getOperand(1).isImm() ||
1289 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001290}
1291
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001292// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001293static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1294 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001295 const MCSymbol *SLabel,
1296 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001297 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001298
David Blaikie0252265b2013-06-16 20:34:15 +00001299 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001300 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001301 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001302 // If the second operand is an immediate, this is a
1303 // register-indirect address.
1304 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001305 MLoc.set(MI->getOperand(0).getReg());
1306 else
1307 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001308 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1309 }
1310 if (MI->getOperand(0).isImm())
1311 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1312 if (MI->getOperand(0).isFPImm())
1313 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1314 if (MI->getOperand(0).isCImm())
1315 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1316
Craig Topperee4dab52012-02-05 08:31:47 +00001317 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001318}
1319
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001320// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001321void
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001322DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001323
Eric Christopher270a12c2013-07-03 21:37:03 +00001324 // Grab the variable info that was squirreled away in the MMI side-table.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001325 collectVariableInfoFromMMITable(Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001326
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001327 for (SmallVectorImpl<const MDNode *>::const_iterator
1328 UVI = UserVariables.begin(),
1329 UVE = UserVariables.end();
1330 UVI != UVE; ++UVI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001331 const MDNode *Var = *UVI;
1332 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001333 continue;
1334
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001335 // History contains relevant DBG_VALUE instructions for Var and instructions
1336 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001337 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001338 if (History.empty())
1339 continue;
1340 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001341
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001342 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001343 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001344 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001345 DISubprogram(DV.getContext()).describes(CurFn->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001346 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001347 else if (MDNode *IA = DV.getInlinedAt())
1348 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1349 else
1350 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001351 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001352 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001353 continue;
1354
1355 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001356 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001357 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001358 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001359 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001360 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001361 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001362 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001363
Eric Christophercc10d202012-10-08 20:48:54 +00001364 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001365 if (History.size() <= 1 ||
1366 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001367 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001368 continue;
1369 }
1370
Eric Christopher59cc0712013-01-28 17:33:26 +00001371 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001372 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001373
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001374 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1375 HI = History.begin(),
1376 HE = History.end();
1377 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001378 const MachineInstr *Begin = *HI;
1379 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001380
Devang Patele7181b52011-06-01 23:00:17 +00001381 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001382 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1383 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001384 continue;
1385
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001386 // Compute the range for a register location.
1387 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1388 const MCSymbol *SLabel = 0;
1389
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001390 if (HI + 1 == HE)
1391 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001392 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001393 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001394 else {
1395 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001396 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001397 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001398 if (End->isDebugValue())
1399 SLabel = getLabelBeforeInsn(End);
1400 else {
1401 // End is a normal instruction clobbering the range.
1402 SLabel = getLabelAfterInsn(End);
1403 assert(SLabel && "Forgot label after clobber instruction");
1404 ++HI;
1405 }
1406 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001407
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001408 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001409 DotDebugLocEntries.push_back(
1410 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001411 }
1412 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001413 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001414
1415 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001416 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1417 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1418 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1419 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001420 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001421 continue;
1422 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001423 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001424 }
Devang Patel9fc11702010-05-25 23:40:22 +00001425}
Devang Patele0a94bf2010-05-14 21:01:35 +00001426
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001427// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001428MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001429 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1430 assert(Label && "Didn't insert label before instruction");
1431 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001432}
1433
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001434// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001435MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001436 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001437}
1438
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001439// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001440void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001441 assert(CurMI == 0);
1442 CurMI = MI;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001443 // Check if source location changes, but ignore DBG_VALUE locations.
1444 if (!MI->isDebugValue()) {
1445 DebugLoc DL = MI->getDebugLoc();
1446 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001447 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001448 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001449 if (DL == PrologEndLoc) {
1450 Flags |= DWARF2_FLAG_PROLOGUE_END;
1451 PrologEndLoc = DebugLoc();
1452 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001453 if (PrologEndLoc.isUnknown())
1454 Flags |= DWARF2_FLAG_IS_STMT;
1455
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001456 if (!DL.isUnknown()) {
1457 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001458 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001459 } else
Devang Patel34a66202011-05-11 19:22:19 +00001460 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001461 }
Devang Patel9fc11702010-05-25 23:40:22 +00001462 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001463
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001464 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001465 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1466 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001467
1468 // No label needed.
1469 if (I == LabelsBeforeInsn.end())
1470 return;
1471
1472 // Label already assigned.
1473 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001474 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001475
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001476 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001477 PrevLabel = MMI->getContext().CreateTempSymbol();
1478 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001479 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001480 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001481}
1482
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001483// Process end of an instruction.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001484void DwarfDebug::endInstruction() {
1485 assert(CurMI != 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001486 // Don't create a new label after DBG_VALUE instructions.
1487 // They don't generate code.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001488 if (!CurMI->isDebugValue())
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001489 PrevLabel = 0;
1490
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001491 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001492 LabelsAfterInsn.find(CurMI);
1493 CurMI = 0;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001494
1495 // No label needed.
1496 if (I == LabelsAfterInsn.end())
1497 return;
1498
1499 // Label already assigned.
1500 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001501 return;
1502
1503 // We need a label after this instruction.
1504 if (!PrevLabel) {
1505 PrevLabel = MMI->getContext().CreateTempSymbol();
1506 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001507 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001508 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001509}
1510
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001511// Each LexicalScope has first instruction and last instruction to mark
1512// beginning and end of a scope respectively. Create an inverse map that list
1513// scopes starts (and ends) with an instruction. One instruction may start (or
1514// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001515void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001516 SmallVector<LexicalScope *, 4> WorkList;
1517 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001518 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001519 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001520
Craig Topper977e9cd2013-07-03 04:24:43 +00001521 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001522 if (!Children.empty())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001523 for (SmallVectorImpl<LexicalScope *>::const_iterator
1524 SI = Children.begin(),
1525 SE = Children.end();
1526 SI != SE; ++SI)
Devang Patel7771b7c2010-01-20 02:05:23 +00001527 WorkList.push_back(*SI);
1528
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001529 if (S->isAbstractScope())
1530 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001531
Craig Topper977e9cd2013-07-03 04:24:43 +00001532 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001533 if (Ranges.empty())
1534 continue;
Craig Topperd8e43652013-07-03 04:17:25 +00001535 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001536 RE = Ranges.end();
1537 RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001538 assert(RI->first && "InsnRange does not have first instruction!");
1539 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001540 requestLabelBeforeInsn(RI->first);
1541 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001542 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001543 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001544}
1545
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001546// Get MDNode for DebugLoc's scope.
Devang Patel589845d2011-05-09 22:14:49 +00001547static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1548 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1549 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1550 return DL.getScope(Ctx);
1551}
1552
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001553// Walk up the scope chain of given debug loc and find line number info
1554// for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001555static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1556 const MDNode *Scope = getScopeNode(DL, Ctx);
1557 DISubprogram SP = getDISubprogram(Scope);
Manman Ren7504ed42013-07-08 18:33:29 +00001558 if (SP.isSubprogram()) {
Eric Christopher34164192012-04-03 00:43:49 +00001559 // Check for number of operands since the compatibility is
1560 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001561 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001562 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1563 else
1564 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1565 }
1566
Devang Patel34a66202011-05-11 19:22:19 +00001567 return DebugLoc();
1568}
1569
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001570// Gather pre-function debug information. Assumes being called immediately
1571// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001572void DwarfDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001573 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001574
1575 // If there's no debug info for the function we're not going to do anything.
1576 if (!MMI->hasDebugInfo())
1577 return;
1578
1579 // Grab the lexical scopes for the function, if we don't have any of those
1580 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001581 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001582 if (LScopes.empty())
1583 return;
1584
1585 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1586
1587 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001588 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001589
Manman Ren4e042a62013-02-05 21:52:47 +00001590 // Set DwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001591 // belongs to so that we add to the correct per-cu line table in the
1592 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001593 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1594 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1595 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001596 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren9d4c7352013-05-21 00:57:22 +00001597 // Use a single line table if we are using .loc and generating assembly.
1598 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1599 else
1600 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001601
Eric Christopherfedfa442013-11-01 23:14:17 +00001602 // Emit a label for the function so that we have a beginning address.
1603 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001604 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001605 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001606
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001607 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001608 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001609 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001610
Eric Christopherfedfa442013-11-01 23:14:17 +00001611 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1612 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001613 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001614 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1615 II != IE; ++II) {
1616 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001617
Devang Patel002d54d2010-05-26 19:37:24 +00001618 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001619 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001620
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001621 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001622 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001623 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001624
1625 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001626 if (isDbgValueInDefinedReg(MI))
1627 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1628
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001629 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001630 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001631 if (History.empty()) {
1632 UserVariables.push_back(Var);
1633 // The first mention of a function argument gets the FunctionBeginSym
1634 // label, so arguments are visible when breaking at function entry.
1635 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001636 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001637 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001638 LabelsBeforeInsn[MI] = FunctionBeginSym;
1639 } else {
1640 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1641 const MachineInstr *Prev = History.back();
1642 if (Prev->isDebugValue()) {
1643 // Coalesce identical entries at the end of History.
1644 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001645 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001646 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001647 << "\t" << *Prev << "\t"
1648 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001649 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001650 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001651
1652 // Terminate old register assignments that don't reach MI;
1653 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1654 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1655 isDbgValueInDefinedReg(Prev)) {
1656 // Previous register assignment needs to terminate at the end of
1657 // its basic block.
1658 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001659 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001660 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001661 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001662 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001663 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001664 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001665 } else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001666 // Terminate after LastMI.
1667 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001668 }
1669 }
1670 }
1671 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001672 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001673 // Not a DBG_VALUE instruction.
1674 if (!MI->isLabel())
1675 AtBlockEntry = false;
1676
Eric Christopher133195782012-10-04 20:46:14 +00001677 // First known non-DBG_VALUE and non-frame setup location marks
1678 // the beginning of the function body.
1679 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1680 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001681 PrologEndLoc = MI->getDebugLoc();
1682
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001683 // Check if the instruction clobbers any registers with debug vars.
1684 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
Eric Christopherfedfa442013-11-01 23:14:17 +00001685 MOE = MI->operands_end();
1686 MOI != MOE; ++MOI) {
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001687 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1688 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001689 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true); AI.isValid();
1690 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001691 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001692 const MDNode *Var = LiveUserVar[Reg];
1693 if (!Var)
1694 continue;
1695 // Reg is now clobbered.
1696 LiveUserVar[Reg] = 0;
1697
1698 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001699 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1700 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001701 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001702 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001703 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001704 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001705 const MachineInstr *Prev = History.back();
1706 // Sanity-check: Register assignments are terminated at the end of
1707 // their block.
1708 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1709 continue;
1710 // Is the variable still in Reg?
1711 if (!isDbgValueInDefinedReg(Prev) ||
1712 Prev->getOperand(0).getReg() != Reg)
1713 continue;
1714 // Var is clobbered. Make sure the next instruction gets a label.
1715 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001716 }
1717 }
Devang Patel002d54d2010-05-26 19:37:24 +00001718 }
Devang Patel002d54d2010-05-26 19:37:24 +00001719 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001720 }
1721
1722 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1723 I != E; ++I) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001724 SmallVectorImpl<const MachineInstr *> &History = I->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001725 if (History.empty())
1726 continue;
1727
1728 // Make sure the final register assignments are terminated.
1729 const MachineInstr *Prev = History.back();
1730 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1731 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001732 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001733 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001734 if (LastMI == PrevMBB->end())
1735 // Drop DBG_VALUE for empty range.
1736 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001737 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001738 // Terminate after LastMI.
1739 History.push_back(LastMI);
1740 }
1741 }
1742 // Request labels for the full history.
1743 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1744 const MachineInstr *MI = History[i];
1745 if (MI->isDebugValue())
1746 requestLabelBeforeInsn(MI);
1747 else
1748 requestLabelAfterInsn(MI);
1749 }
1750 }
Devang Patel002d54d2010-05-26 19:37:24 +00001751
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001752 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001753 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001754
1755 // Record beginning of function.
1756 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001757 DebugLoc FnStartDL =
1758 getFnDebugLoc(PrologEndLoc, MF->getFunction()->getContext());
1759 recordSourceLine(
1760 FnStartDL.getLine(), FnStartDL.getCol(),
1761 FnStartDL.getScope(MF->getFunction()->getContext()),
1762 // We'd like to list the prologue as "not statements" but GDB behaves
1763 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1764 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001765 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001766}
1767
Devang Patel7e623022011-08-10 20:55:27 +00001768void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001769 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1770 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001771 // Variables with positive arg numbers are parameters.
1772 if (unsigned ArgNum = DV.getArgNumber()) {
1773 // Keep all parameters in order at the start of the variable list to ensure
1774 // function types are correct (no out-of-order parameters)
1775 //
1776 // This could be improved by only doing it for optimized builds (unoptimized
1777 // builds have the right order to begin with), searching from the back (this
1778 // would catch the unoptimized case quickly), or doing a binary search
1779 // rather than linear search.
1780 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1781 while (I != Vars.end()) {
1782 unsigned CurNum = (*I)->getVariable().getArgNumber();
1783 // A local (non-parameter) variable has been found, insert immediately
1784 // before it.
1785 if (CurNum == 0)
1786 break;
1787 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001788 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001789 break;
David Blaikieb272a752013-06-06 22:28:26 +00001790 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001791 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001792 Vars.insert(I, Var);
1793 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001794 }
1795
1796 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001797}
1798
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001799// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001800void DwarfDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001801 // Every beginFunction(MF) call should be followed by an endFunction(MF) call,
1802 // though the beginFunction may not be called at all.
1803 // We should handle both cases.
1804 if (CurFn == 0)
1805 CurFn = MF;
1806 else
1807 assert(CurFn == MF);
1808 assert(CurFn != 0);
1809
1810 if (!MMI->hasDebugInfo() || LScopes.empty()) {
1811 CurFn = 0;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001812 return;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001813 }
Devang Patel2904aa92009-11-12 19:02:56 +00001814
Devang Patel7e623022011-08-10 20:55:27 +00001815 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001816 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001817 // Assumes in correct section after the entry point.
1818 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Manman Ren4e042a62013-02-05 21:52:47 +00001819 // Set DwarfCompileUnitID in MCContext to default value.
1820 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001821
Devang Patel7e623022011-08-10 20:55:27 +00001822 SmallPtrSet<const MDNode *, 16> ProcessedVars;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001823 collectVariableInfo(ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001824
Devang Patel3acc70e2011-08-15 22:04:40 +00001825 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001826 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001827 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001828
Devang Patel7e623022011-08-10 20:55:27 +00001829 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001830 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1831 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1832 LexicalScope *AScope = AList[i];
1833 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001834 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001835 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001836 DIArray Variables = SP.getVariables();
1837 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1838 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001839 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001840 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001841 // Check that DbgVariable for DV wasn't created earlier, when
1842 // findAbstractVariable() was called for inlined instance of DV.
1843 LLVMContext &Ctx = DV->getContext();
1844 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1845 if (AbstractVariables.lookup(CleanDV))
1846 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001847 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001848 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001849 }
1850 }
Devang Patel44403472011-08-12 18:10:19 +00001851 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001852 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001853 }
Eric Christopher6a841382012-11-19 22:42:10 +00001854
Devang Patel3acc70e2011-08-15 22:04:40 +00001855 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher6a841382012-11-19 22:42:10 +00001856
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001857 if (!CurFn->getTarget().Options.DisableFramePointerElim(*CurFn))
Eric Christopherbb69a272012-08-24 01:14:27 +00001858 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001859
Bill Wendling2b128d72009-05-20 23:19:06 +00001860 // Clear debug info
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001861 for (ScopeVariablesMap::iterator I = ScopeVariables.begin(),
1862 E = ScopeVariables.end();
1863 I != E; ++I)
Devang Patel7e623022011-08-10 20:55:27 +00001864 DeleteContainerPointers(I->second);
1865 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001866 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001867 UserVariables.clear();
1868 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001869 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001870 LabelsBeforeInsn.clear();
1871 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001872 PrevLabel = NULL;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001873 CurFn = 0;
Bill Wendling2b128d72009-05-20 23:19:06 +00001874}
1875
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001876// Register a source line with debug info. Returns the unique label that was
1877// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001878void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1879 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001880 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001881 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001882 unsigned Src = 1;
1883 if (S) {
1884 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001885
Dan Gohman50849c62010-05-05 23:41:32 +00001886 if (Scope.isCompileUnit()) {
1887 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001888 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001889 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001890 } else if (Scope.isFile()) {
1891 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001892 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001893 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001894 } else if (Scope.isSubprogram()) {
1895 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001896 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001897 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001898 } else if (Scope.isLexicalBlockFile()) {
1899 DILexicalBlockFile DBF(S);
1900 Fn = DBF.getFilename();
1901 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001902 } else if (Scope.isLexicalBlock()) {
1903 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001904 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001905 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001906 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001907 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001908
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001909 Src = getOrCreateSourceID(
1910 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001911 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001912 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001913}
1914
Bill Wendling806535f2009-05-20 23:22:40 +00001915//===----------------------------------------------------------------------===//
1916// Emit Methods
1917//===----------------------------------------------------------------------===//
1918
Manman Rence20d462013-10-29 22:57:10 +00001919// Compute the size and offset of a DIE. The offset is relative to start of the
1920// CU. It returns the offset after laying out the DIE.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001921unsigned DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001922 // Get the children.
1923 const std::vector<DIE *> &Children = Die->getChildren();
1924
Bill Wendling480ff322009-05-20 23:21:38 +00001925 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001926 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001927
1928 // Get the abbreviation for this DIE.
1929 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00001930 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00001931
1932 // Set DIE offset
1933 Die->setOffset(Offset);
1934
1935 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001936 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001937
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001938 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00001939 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001940
1941 // Size the DIE attribute values.
1942 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1943 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001944 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001945
1946 // Size the DIE children if any.
1947 if (!Children.empty()) {
1948 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1949 "Children flag not set");
1950
1951 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001952 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001953
1954 // End of children marker.
1955 Offset += sizeof(int8_t);
1956 }
1957
1958 Die->setSize(Offset - Die->getOffset());
1959 return Offset;
1960}
1961
Eric Christopherb088d2d2013-10-24 21:05:08 +00001962// Compute the size and offset for each DIE.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001963void DwarfUnits::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001964 // Offset from the first CU in the debug info section is 0 initially.
1965 unsigned SecOffset = 0;
1966
Eric Christopherb088d2d2013-10-24 21:05:08 +00001967 // Iterate over each compile unit and set the size and offsets for each
1968 // DIE within each compile unit. All offsets are CU relative.
David Blaikie319a05f2013-12-02 19:33:10 +00001969 for (SmallVectorImpl<Unit *>::const_iterator I = CUs.begin(), E = CUs.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001970 I != E; ++I) {
Manman Rence20d462013-10-29 22:57:10 +00001971 (*I)->setDebugInfoOffset(SecOffset);
1972
1973 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001974 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
David Blaikie6b288cf2013-10-30 20:42:41 +00001975 (*I)->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001976
1977 // EndOffset here is CU-relative, after laying out
1978 // all of the CU DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001979 unsigned EndOffset = computeSizeAndOffset((*I)->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001980 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001981 }
Bill Wendling480ff322009-05-20 23:21:38 +00001982}
1983
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001984// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001985void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001986 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001987
Bill Wendling480ff322009-05-20 23:21:38 +00001988 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001989 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001990 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001991 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001992 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001993 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001994 DwarfAbbrevDWOSectionSym = emitSectionSym(
1995 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001996 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001997
Chris Lattner6629ca92010-04-04 22:59:04 +00001998 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001999 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002000
Eric Christopher74804332013-02-07 21:19:50 +00002001 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002002 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002003 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00002004 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00002005 DwarfGnuPubNamesSectionSym =
2006 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
2007 DwarfGnuPubTypesSectionSym =
2008 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00002009 } else if (HasDwarfPubSections) {
2010 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2011 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00002012 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002013
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002014 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002015 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00002016 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002017 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002018 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00002019 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002020 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00002021 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002022 DwarfDebugRangeSectionSym =
2023 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00002024
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002025 DwarfDebugLocSectionSym =
2026 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Devang Patel9fc11702010-05-25 23:40:22 +00002027
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002028 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
2029 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002030}
2031
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002032// Recursively emits a debug information entry.
David Blaikie2d4e1122013-10-30 17:14:24 +00002033void DwarfDebug::emitDIE(DIE *Die, ArrayRef<DIEAbbrev *> Abbrevs) {
Bill Wendling480ff322009-05-20 23:21:38 +00002034 // Get the abbreviation for this DIE.
2035 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00002036 const DIEAbbrev *Abbrev = Abbrevs[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00002037
Bill Wendling480ff322009-05-20 23:21:38 +00002038 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00002039 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00002040 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2041 Twine::utohexstr(Die->getOffset()) + ":0x" +
2042 Twine::utohexstr(Die->getSize()) + " " +
2043 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00002044 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002045
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002046 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00002047 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00002048
2049 // Emit the DIE attribute values.
2050 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00002051 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
2052 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00002053 assert(Form && "Too many attributes for DIE (check abbreviation)");
2054
Chris Lattner7bde8c02010-04-04 18:52:31 +00002055 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00002056 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002057
Bill Wendling480ff322009-05-20 23:21:38 +00002058 switch (Attr) {
Manman Rence20d462013-10-29 22:57:10 +00002059 case dwarf::DW_AT_abstract_origin:
2060 case dwarf::DW_AT_type:
2061 case dwarf::DW_AT_friend:
2062 case dwarf::DW_AT_specification:
2063 case dwarf::DW_AT_import:
2064 case dwarf::DW_AT_containing_type: {
2065 DIEEntry *E = cast<DIEEntry>(Values[i]);
2066 DIE *Origin = E->getEntry();
2067 unsigned Addr = Origin->getOffset();
2068 if (Form == dwarf::DW_FORM_ref_addr) {
2069 assert(!useSplitDwarf() && "TODO: dwo files can't have relocations.");
2070 // For DW_FORM_ref_addr, output the offset from beginning of debug info
2071 // section. Origin->getOffset() returns the offset from start of the
2072 // compile unit.
David Blaikie409dd9c2013-11-19 23:08:21 +00002073 CompileUnit *CU = CUDieMap.lookup(Origin->getUnit());
Manman Rence20d462013-10-29 22:57:10 +00002074 assert(CU && "CUDie should belong to a CU.");
2075 Addr += CU->getDebugInfoOffset();
2076 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2077 Asm->EmitLabelPlusOffset(DwarfInfoSectionSym, Addr,
2078 DIEEntry::getRefAddrSize(Asm));
2079 else
Manman Ren251a1bd2013-10-29 23:14:15 +00002080 Asm->EmitLabelOffsetDifference(DwarfInfoSectionSym, Addr,
2081 DwarfInfoSectionSym,
2082 DIEEntry::getRefAddrSize(Asm));
Manman Rence20d462013-10-29 22:57:10 +00002083 } else {
Manman Ren4dbdc902013-10-31 17:54:35 +00002084 // Make sure Origin belong to the same CU.
David Blaikie409dd9c2013-11-19 23:08:21 +00002085 assert(Die->getUnit() == Origin->getUnit() &&
Manman Ren4dbdc902013-10-31 17:54:35 +00002086 "The referenced DIE should belong to the same CU in ref4");
Manman Rence20d462013-10-29 22:57:10 +00002087 Asm->EmitInt32(Addr);
2088 }
2089 break;
2090 }
Devang Patel12563b32010-04-16 23:33:45 +00002091 case dwarf::DW_AT_ranges: {
2092 // DW_AT_range Value encodes offset in debug_range section.
Eric Christopher4751d702013-11-23 00:05:29 +00002093 DIELabel *V = cast<DIELabel>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00002094
Eric Christopher4751d702013-11-23 00:05:29 +00002095 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2096 Asm->EmitSectionOffset(V->getValue(), DwarfDebugRangeSectionSym);
2097 else
2098 Asm->EmitLabelDifference(V->getValue(), DwarfDebugRangeSectionSym, 4);
Devang Patel12563b32010-04-16 23:33:45 +00002099 break;
2100 }
Devang Patel9fc11702010-05-25 23:40:22 +00002101 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00002102 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
2103 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherd0b82ae2013-11-16 00:18:40 +00002104 Asm->EmitSectionOffset(L->getValue(), DwarfDebugLocSectionSym);
Nick Lewycky33da3362012-06-22 01:25:12 +00002105 else
Ulrich Weigand396ba8b2013-07-02 18:46:26 +00002106 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Nick Lewycky33da3362012-06-22 01:25:12 +00002107 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00002108 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00002109 }
Devang Patel9fc11702010-05-25 23:40:22 +00002110 break;
2111 }
Devang Patela1bd5a12010-09-29 19:08:08 +00002112 case dwarf::DW_AT_accessibility: {
2113 if (Asm->isVerbose()) {
2114 DIEInteger *V = cast<DIEInteger>(Values[i]);
2115 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2116 }
2117 Values[i]->EmitValue(Asm, Form);
2118 break;
2119 }
Bill Wendling480ff322009-05-20 23:21:38 +00002120 default:
2121 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002122 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002123 break;
2124 }
Bill Wendling480ff322009-05-20 23:21:38 +00002125 }
2126
2127 // Emit the DIE children if any.
2128 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2129 const std::vector<DIE *> &Children = Die->getChildren();
2130
2131 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher3c5a1912012-12-19 22:02:53 +00002132 emitDIE(Children[j], Abbrevs);
Bill Wendling480ff322009-05-20 23:21:38 +00002133
Chris Lattner7bde8c02010-04-04 18:52:31 +00002134 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00002135 Asm->OutStreamer.AddComment("End Of Children Mark");
2136 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002137 }
2138}
2139
Eric Christophera2de8262012-12-15 00:04:07 +00002140// Emit the various dwarf units to the unit section USection with
2141// the abbreviations going into ASection.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002142void DwarfUnits::emitUnits(DwarfDebug *DD, const MCSection *USection,
Eric Christophera2de8262012-12-15 00:04:07 +00002143 const MCSection *ASection,
2144 const MCSymbol *ASectionSym) {
2145 Asm->OutStreamer.SwitchSection(USection);
David Blaikie319a05f2013-12-02 19:33:10 +00002146 for (SmallVectorImpl<Unit *>::iterator I = CUs.begin(), E = CUs.end(); I != E;
2147 ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002148 Unit *TheU = *I;
2149 DIE *Die = TheU->getUnitDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002150
Devang Patel1a0df9a2010-05-10 22:49:55 +00002151 // Emit the compile units header.
David Blaikie2a80e442013-12-02 22:09:48 +00002152 Asm->OutStreamer.EmitLabel(
2153 Asm->GetTempSymbol(USection->getLabelBeginName(), TheU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002154
Devang Patel1a0df9a2010-05-10 22:49:55 +00002155 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00002156 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00002157 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002158
David Blaikie2a80e442013-12-02 22:09:48 +00002159 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002160
Eric Christopher3c5a1912012-12-19 22:02:53 +00002161 DD->emitDIE(Die, Abbreviations);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002162 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002163 Asm->GetTempSymbol(USection->getLabelEndName(), TheU->getUniqueID()));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002164 }
Bill Wendling480ff322009-05-20 23:21:38 +00002165}
2166
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002167// Emit the debug info section.
2168void DwarfDebug::emitDebugInfo() {
Eric Christophera2de8262012-12-15 00:04:07 +00002169 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2170
2171 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
2172 Asm->getObjFileLowering().getDwarfAbbrevSection(),
2173 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002174}
2175
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002176// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002177void DwarfDebug::emitAbbreviations() {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002178 if (!useSplitDwarf())
2179 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
2180 &Abbreviations);
2181 else
2182 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2183}
Bill Wendling480ff322009-05-20 23:21:38 +00002184
Eric Christopher3c5a1912012-12-19 22:02:53 +00002185void DwarfDebug::emitAbbrevs(const MCSection *Section,
2186 std::vector<DIEAbbrev *> *Abbrevs) {
2187 // Check to see if it is worth the effort.
2188 if (!Abbrevs->empty()) {
2189 // Start the debug abbrev section.
2190 Asm->OutStreamer.SwitchSection(Section);
2191
2192 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002193 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling480ff322009-05-20 23:21:38 +00002194
2195 // For each abbrevation.
Eric Christopher3c5a1912012-12-19 22:02:53 +00002196 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00002197 // Get abbreviation data
Eric Christopher3c5a1912012-12-19 22:02:53 +00002198 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling480ff322009-05-20 23:21:38 +00002199
2200 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002201 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002202
2203 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002204 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002205 }
2206
2207 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002208 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002209
Eric Christopher3c5a1912012-12-19 22:02:53 +00002210 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002211 Asm->OutStreamer.EmitLabel(End);
Bill Wendling480ff322009-05-20 23:21:38 +00002212 }
2213}
2214
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002215// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002216void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002217 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002218 Asm->OutStreamer.AddComment("Extended Op");
2219 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002220
Chris Lattner566cae92010-03-09 23:52:58 +00002221 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002222 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002223 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2224 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2225
2226 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002227
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002228 Asm->OutStreamer.EmitSymbolValue(
2229 Asm->GetTempSymbol("section_end", SectionEnd),
2230 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002231
2232 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002233 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2234 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002235 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002236 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002237}
2238
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002239// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002240void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002241 DwarfAccelTable AT(
2242 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002243 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2244 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002245 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002246 Unit *TheU = *I;
2247 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelNames();
David Blaikie2ea848b2013-11-19 22:51:04 +00002248 for (StringMap<std::vector<const DIE *> >::const_iterator
2249 GI = Names.begin(),
2250 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002251 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002252 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002253 const std::vector<const DIE *> &Entities = GI->second;
2254 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2255 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002256 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002257 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002258 }
2259 }
2260
2261 AT.FinalizeTable(Asm, "Names");
2262 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002263 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002264 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2265 Asm->OutStreamer.EmitLabel(SectionBegin);
2266
2267 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002268 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002269}
2270
Eric Christopher48fef592012-12-20 21:58:40 +00002271// Emit objective C classes and categories into a hashed accelerator table
2272// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002273void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002274 DwarfAccelTable AT(
2275 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002276 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2277 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002278 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002279 Unit *TheU = *I;
2280 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelObjC();
David Blaikie2ea848b2013-11-19 22:51:04 +00002281 for (StringMap<std::vector<const DIE *> >::const_iterator
2282 GI = Names.begin(),
2283 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002284 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002285 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002286 const std::vector<const DIE *> &Entities = GI->second;
2287 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2288 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002289 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002290 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002291 }
2292 }
2293
2294 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002295 Asm->OutStreamer.SwitchSection(
2296 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002297 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2298 Asm->OutStreamer.EmitLabel(SectionBegin);
2299
2300 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002301 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002302}
2303
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002304// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002305void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002306 DwarfAccelTable AT(
2307 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002308 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2309 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002310 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002311 Unit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002312 const StringMap<std::vector<const DIE *> > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002313 TheU->getAccelNamespace();
David Blaikie2ea848b2013-11-19 22:51:04 +00002314 for (StringMap<std::vector<const DIE *> >::const_iterator
2315 GI = Names.begin(),
2316 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002317 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002318 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002319 const std::vector<const DIE *> &Entities = GI->second;
2320 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2321 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002322 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002323 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002324 }
2325 }
2326
2327 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002328 Asm->OutStreamer.SwitchSection(
2329 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002330 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2331 Asm->OutStreamer.EmitLabel(SectionBegin);
2332
2333 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002334 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002335}
2336
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002337// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002338void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002339 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002340 Atoms.push_back(
2341 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2342 Atoms.push_back(
2343 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2344 Atoms.push_back(
2345 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002346 DwarfAccelTable AT(Atoms);
David Blaikie319a05f2013-12-02 19:33:10 +00002347 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2348 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002349 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002350 Unit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002351 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002352 TheU->getAccelTypes();
David Blaikie2ea848b2013-11-19 22:51:04 +00002353 for (StringMap<
2354 std::vector<std::pair<const DIE *, unsigned> > >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002355 GI = Names.begin(),
2356 GE = Names.end();
2357 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002358 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002359 const std::vector<std::pair<const DIE *, unsigned> > &Entities =
2360 GI->second;
2361 for (std::vector<std::pair<const DIE *, unsigned> >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002362 DI = Entities.begin(),
2363 DE = Entities.end();
2364 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002365 AT.AddName(Name, DI->first, DI->second);
Eric Christopher4996c702011-11-07 09:24:32 +00002366 }
2367 }
2368
2369 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002370 Asm->OutStreamer.SwitchSection(
2371 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002372 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2373 Asm->OutStreamer.EmitLabel(SectionBegin);
2374
2375 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002376 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002377}
2378
Eric Christopherdd1a0122013-09-13 00:35:05 +00002379// Public name handling.
2380// The format for the various pubnames:
2381//
2382// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2383// for the DIE that is named.
2384//
2385// gnu pubnames - offset/index value/name tuples where the offset is the offset
2386// into the CU and the index value is computed according to the type of value
2387// for the DIE that is named.
2388//
2389// For type units the offset is the offset of the skeleton DIE. For split dwarf
2390// it's the offset within the debug_info/debug_types dwo section, however, the
2391// reference in the pubname header doesn't change.
2392
2393/// computeIndexValue - Compute the gdb index value for the DIE and CU.
David Blaikie319a05f2013-12-02 19:33:10 +00002394static dwarf::PubIndexEntryDescriptor computeIndexValue(Unit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002395 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002396 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2397
2398 // We could have a specification DIE that has our most of our knowledge,
2399 // look for that now.
2400 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2401 if (SpecVal) {
2402 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2403 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2404 Linkage = dwarf::GIEL_EXTERNAL;
2405 } else if (Die->findAttribute(dwarf::DW_AT_external))
2406 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002407
2408 switch (Die->getTag()) {
2409 case dwarf::DW_TAG_class_type:
2410 case dwarf::DW_TAG_structure_type:
2411 case dwarf::DW_TAG_union_type:
2412 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002413 return dwarf::PubIndexEntryDescriptor(
2414 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2415 ? dwarf::GIEL_STATIC
2416 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002417 case dwarf::DW_TAG_typedef:
2418 case dwarf::DW_TAG_base_type:
2419 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002420 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002421 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002422 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002423 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002424 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002425 case dwarf::DW_TAG_constant:
2426 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002427 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002428 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002429 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2430 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002431 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002432 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002433 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002434}
2435
Eric Christopher5f93bb92013-09-09 20:03:17 +00002436/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002437///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002438void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002439 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopherdd1a0122013-09-13 00:35:05 +00002440 const MCSection *PSec =
2441 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2442 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002443
David Blaikie319a05f2013-12-02 19:33:10 +00002444 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2445 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00002446 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002447 Unit *TheU = *I;
2448 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002449
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002450 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002451 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002452
Eric Christopher39eebfa2013-09-30 23:14:16 +00002453 // Emit a label so we can reference the beginning of this pubname section.
2454 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002455 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002456 Asm->GetTempSymbol("gnu_pubnames", TheU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002457
Eric Christopherdd1a0122013-09-13 00:35:05 +00002458 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002459 Asm->OutStreamer.AddComment("Length of Public Names Info");
2460 Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
2461 Asm->GetTempSymbol("pubnames_begin", ID), 4);
2462
2463 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
2464
2465 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002466 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002467
2468 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2469 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2470 DwarfInfoSectionSym);
2471
2472 Asm->OutStreamer.AddComment("Compilation Unit Length");
2473 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
2474 Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2475 4);
2476
Eric Christopherdd1a0122013-09-13 00:35:05 +00002477 // Emit the pubnames for this compilation unit.
David Blaikie2a80e442013-12-02 22:09:48 +00002478 const StringMap<const DIE *> &Globals = TheU->getGlobalNames();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002479 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2480 GE = Globals.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002481 GI != GE; ++GI) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002482 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002483 const DIE *Entity = GI->second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002484
2485 Asm->OutStreamer.AddComment("DIE offset");
2486 Asm->EmitInt32(Entity->getOffset());
2487
Eric Christopherdd1a0122013-09-13 00:35:05 +00002488 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002489 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002490 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002491 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002492 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002493 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002494 }
2495
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002496 if (Asm->isVerbose())
2497 Asm->OutStreamer.AddComment("External Name");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002498 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002499 }
2500
2501 Asm->OutStreamer.AddComment("End Mark");
2502 Asm->EmitInt32(0);
2503 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
2504 }
2505}
2506
Eric Christopherdd1a0122013-09-13 00:35:05 +00002507void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher8b3737f2013-09-13 00:34:58 +00002508 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopher261d2342013-09-23 20:55:35 +00002509 const MCSection *PSec =
2510 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2511 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002512
David Blaikie319a05f2013-12-02 19:33:10 +00002513 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2514 E = getUnits().end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002515 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002516 Unit *TheU = *I;
Eric Christopher6abc9c52011-11-07 09:18:35 +00002517 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002518 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002519
2520 // Emit a label so we can reference the beginning of this pubtype section.
2521 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002522 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002523 Asm->GetTempSymbol("gnu_pubtypes", TheU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002524
2525 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002526 Asm->OutStreamer.AddComment("Length of Public Types Info");
2527 Asm->EmitLabelDifference(
David Blaikie2a80e442013-12-02 22:09:48 +00002528 Asm->GetTempSymbol("pubtypes_end", TheU->getUniqueID()),
2529 Asm->GetTempSymbol("pubtypes_begin", TheU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002530
Eric Christopher8b3737f2013-09-13 00:34:58 +00002531 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002532 Asm->GetTempSymbol("pubtypes_begin", TheU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002533
Eric Christopher8b3737f2013-09-13 00:34:58 +00002534 if (Asm->isVerbose())
2535 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002536 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002537
Devang Patel1a0df9a2010-05-10 22:49:55 +00002538 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002539 Asm->EmitSectionOffset(
David Blaikie2a80e442013-12-02 22:09:48 +00002540 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheU->getUniqueID()),
Eric Christopher8b3737f2013-09-13 00:34:58 +00002541 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002542
Devang Patel1a0df9a2010-05-10 22:49:55 +00002543 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002544 Asm->EmitLabelDifference(
David Blaikie2a80e442013-12-02 22:09:48 +00002545 Asm->GetTempSymbol(ISec->getLabelEndName(), TheU->getUniqueID()),
2546 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002547
Eric Christopher39eebfa2013-09-30 23:14:16 +00002548 // Emit the pubtypes.
David Blaikie2a80e442013-12-02 22:09:48 +00002549 const StringMap<const DIE *> &Globals = TheU->getGlobalTypes();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002550 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2551 GE = Globals.end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002552 GI != GE; ++GI) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002553 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002554 const DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002555
Eric Christopher8b3737f2013-09-13 00:34:58 +00002556 if (Asm->isVerbose())
2557 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002558 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002559
Eric Christopherdd1a0122013-09-13 00:35:05 +00002560 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002561 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002562 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002563 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002564 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002565 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002566 }
2567
Eric Christopher8b3737f2013-09-13 00:34:58 +00002568 if (Asm->isVerbose())
2569 Asm->OutStreamer.AddComment("External Name");
2570
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002571 // Emit the name with a terminating null byte.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002572 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002573 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002574
Devang Patel1a0df9a2010-05-10 22:49:55 +00002575 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002576 Asm->EmitInt32(0);
Eric Christopher8b3737f2013-09-13 00:34:58 +00002577 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002578 Asm->GetTempSymbol("pubtypes_end", TheU->getUniqueID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002579 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002580}
2581
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002582// Emit strings into a string section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002583void DwarfUnits::emitStrings(const MCSection *StrSection,
2584 const MCSection *OffsetSection = NULL,
2585 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002586
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002587 if (StringPool.empty())
2588 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002589
Chris Lattner3d72a672010-03-09 23:38:23 +00002590 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002591 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002592
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002593 // Get all of the string pool entries and put them in an array by their ID so
2594 // we can sort them.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002595 SmallVector<
2596 std::pair<unsigned, StringMapEntry<std::pair<MCSymbol *, unsigned> > *>,
2597 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002598
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002599 for (StringMap<std::pair<MCSymbol *, unsigned> >::iterator
2600 I = StringPool.begin(),
2601 E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002602 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002603 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002604
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002605 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002606
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002607 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002608 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002609 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002610
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002611 // Emit the string itself with a terminating null byte.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002612 Asm->OutStreamer.EmitBytes(
2613 StringRef(Entries[i].second->getKeyData(),
2614 Entries[i].second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002615 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002616
2617 // If we've got an offset section go ahead and emit that now as well.
2618 if (OffsetSection) {
2619 Asm->OutStreamer.SwitchSection(OffsetSection);
2620 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002621 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002622 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002623 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002624 offset += Entries[i].second->getKeyLength() + 1;
2625 }
2626 }
Bill Wendling480ff322009-05-20 23:21:38 +00002627}
2628
Eric Christopher65132a82013-11-19 09:11:26 +00002629
2630// Emit addresses into the section given.
Eric Christopher962c9082013-01-15 23:56:56 +00002631void DwarfUnits::emitAddresses(const MCSection *AddrSection) {
2632
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002633 if (AddressPool.empty())
2634 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002635
2636 // Start the dwarf addr section.
2637 Asm->OutStreamer.SwitchSection(AddrSection);
2638
David Blaikiece1960f2013-07-08 17:51:28 +00002639 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002640 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002641
David Blaikiece1960f2013-07-08 17:51:28 +00002642 for (DenseMap<const MCExpr *, unsigned>::iterator I = AddressPool.begin(),
2643 E = AddressPool.end();
Eric Christopher962c9082013-01-15 23:56:56 +00002644 I != E; ++I)
David Blaikieac569a62013-07-08 17:33:10 +00002645 Entries[I->second] = I->first;
Eric Christopher962c9082013-01-15 23:56:56 +00002646
2647 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002648 // Emit an expression for reference from debug information entries.
David Blaikieac569a62013-07-08 17:33:10 +00002649 if (const MCExpr *Expr = Entries[i])
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002650 Asm->OutStreamer.EmitValue(Expr, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002651 else
2652 Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
2653 }
Eric Christopher962c9082013-01-15 23:56:56 +00002654}
2655
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002656// Emit visible names into a debug str section.
2657void DwarfDebug::emitDebugStr() {
2658 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2659 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2660}
2661
Eric Christopher9046f942013-07-02 21:36:07 +00002662// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002663void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002664 if (DotDebugLocEntries.empty())
2665 return;
2666
Eric Christopher4887c8f2013-03-29 23:34:06 +00002667 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002668 I = DotDebugLocEntries.begin(),
2669 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002670 I != E; ++I) {
2671 DotDebugLocEntry &Entry = *I;
2672 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002673 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002674 }
2675
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002676 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002677 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002678 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002679 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002680 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2681 unsigned index = 1;
Eric Christopher4887c8f2013-03-29 23:34:06 +00002682 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002683 I = DotDebugLocEntries.begin(),
2684 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002685 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002686 DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002687 if (Entry.isMerged())
2688 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002689 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002690 Asm->OutStreamer.EmitIntValue(0, Size);
2691 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002692 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002693 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002694 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2695 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2696 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002697 Asm->OutStreamer.AddComment("Loc expr size");
2698 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2699 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2700 Asm->EmitLabelDifference(end, begin, 2);
2701 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002702 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002703 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002704 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2705 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002706 Asm->OutStreamer.AddComment("DW_OP_consts");
2707 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002708 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002709 } else {
2710 Asm->OutStreamer.AddComment("DW_OP_constu");
2711 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002712 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002713 }
Devang Pateled9fd452011-07-08 16:49:43 +00002714 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002715 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002716 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002717 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002718 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002719 else {
2720 // Complex address entry.
2721 unsigned N = DV.getNumAddrElements();
2722 unsigned i = 0;
2723 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002724 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002725 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002726 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002727 Asm->OutStreamer.AddComment("DW_OP_deref");
2728 Asm->EmitInt8(dwarf::DW_OP_deref);
2729 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2730 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2731 Asm->EmitSLEB128(DV.getAddrElement(1));
2732 } else {
2733 // If first address element is OpPlus then emit
2734 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002735 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2736 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002737 i = 2;
2738 }
2739 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002740 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002741 }
Eric Christopher6a841382012-11-19 22:42:10 +00002742
Devang Pateled9fd452011-07-08 16:49:43 +00002743 // Emit remaining complex address elements.
2744 for (; i < N; ++i) {
2745 uint64_t Element = DV.getAddrElement(i);
2746 if (Element == DIBuilder::OpPlus) {
2747 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2748 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002749 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002750 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002751 Asm->EmitInt8(dwarf::DW_OP_deref);
2752 } else
2753 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002754 }
Devang Patel3e021532011-04-28 02:22:40 +00002755 }
Devang Patel3e021532011-04-28 02:22:40 +00002756 }
Devang Pateled9fd452011-07-08 16:49:43 +00002757 // else ... ignore constant fp. There is not any good way to
2758 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002759 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002760 }
2761 }
Bill Wendling480ff322009-05-20 23:21:38 +00002762}
2763
Richard Mitton21101b32013-09-19 23:21:01 +00002764struct SymbolCUSorter {
2765 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2766 const MCStreamer &Streamer;
2767
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002768 bool operator()(const SymbolCU &A, const SymbolCU &B) {
Richard Mitton21101b32013-09-19 23:21:01 +00002769 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2770 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2771
2772 // Symbols with no order assigned should be placed at the end.
2773 // (e.g. section end labels)
2774 if (IA == 0)
2775 IA = (unsigned)(-1);
2776 if (IB == 0)
2777 IB = (unsigned)(-1);
2778 return IA < IB;
2779 }
2780};
2781
David Blaikie319a05f2013-12-02 19:33:10 +00002782static bool CUSort(const Unit *A, const Unit *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002783 return (A->getUniqueID() < B->getUniqueID());
Richard Mitton21101b32013-09-19 23:21:01 +00002784}
2785
2786struct ArangeSpan {
2787 const MCSymbol *Start, *End;
2788};
2789
2790// Emit a debug aranges section, containing a CU lookup for any
2791// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002792void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002793 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002794 Asm->OutStreamer.SwitchSection(
2795 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002796
2797 typedef DenseMap<CompileUnit *, std::vector<ArangeSpan> > SpansType;
2798
2799 SpansType Spans;
2800
2801 // Build a list of sections used.
2802 std::vector<const MCSection *> Sections;
2803 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
2804 it++) {
2805 const MCSection *Section = it->first;
2806 Sections.push_back(Section);
2807 }
2808
2809 // Sort the sections into order.
2810 // This is only done to ensure consistent output order across different runs.
2811 std::sort(Sections.begin(), Sections.end(), SectionSort);
2812
2813 // Build a set of address spans, sorted by CU.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002814 for (size_t SecIdx = 0; SecIdx < Sections.size(); SecIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002815 const MCSection *Section = Sections[SecIdx];
2816 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2817 if (List.size() < 2)
2818 continue;
2819
2820 // Sort the symbols by offset within the section.
2821 SymbolCUSorter sorter(Asm->OutStreamer);
2822 std::sort(List.begin(), List.end(), sorter);
2823
2824 // If we have no section (e.g. common), just write out
2825 // individual spans for each symbol.
2826 if (Section == NULL) {
2827 for (size_t n = 0; n < List.size(); n++) {
2828 const SymbolCU &Cur = List[n];
2829
2830 ArangeSpan Span;
2831 Span.Start = Cur.Sym;
2832 Span.End = NULL;
2833 if (Cur.CU)
2834 Spans[Cur.CU].push_back(Span);
2835 }
2836 } else {
2837 // Build spans between each label.
2838 const MCSymbol *StartSym = List[0].Sym;
2839 for (size_t n = 1; n < List.size(); n++) {
2840 const SymbolCU &Prev = List[n - 1];
2841 const SymbolCU &Cur = List[n];
2842
2843 // Try and build the longest span we can within the same CU.
2844 if (Cur.CU != Prev.CU) {
2845 ArangeSpan Span;
2846 Span.Start = StartSym;
2847 Span.End = Cur.Sym;
2848 Spans[Prev.CU].push_back(Span);
2849 StartSym = Cur.Sym;
2850 }
2851 }
2852 }
2853 }
2854
2855 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2856 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2857
2858 // Build a list of CUs used.
2859 std::vector<CompileUnit *> CUs;
2860 for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
2861 CompileUnit *CU = it->first;
2862 CUs.push_back(CU);
2863 }
2864
2865 // Sort the CU list (again, to ensure consistent output order).
2866 std::sort(CUs.begin(), CUs.end(), CUSort);
2867
2868 // Emit an arange table for each CU we used.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002869 for (size_t CUIdx = 0; CUIdx < CUs.size(); CUIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002870 CompileUnit *CU = CUs[CUIdx];
2871 std::vector<ArangeSpan> &List = Spans[CU];
2872
2873 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002874 unsigned ContentSize =
2875 sizeof(int16_t) + // DWARF ARange version number
2876 sizeof(int32_t) + // Offset of CU in the .debug_info section
2877 sizeof(int8_t) + // Pointer Size (in bytes)
2878 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002879
2880 unsigned TupleSize = PtrSize * 2;
2881
2882 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
2883 unsigned Padding = 0;
2884 while (((sizeof(int32_t) + ContentSize + Padding) % TupleSize) != 0)
2885 Padding++;
2886
2887 ContentSize += Padding;
2888 ContentSize += (List.size() + 1) * TupleSize;
2889
2890 // For each compile unit, write the list of spans it covers.
2891 Asm->OutStreamer.AddComment("Length of ARange Set");
2892 Asm->EmitInt32(ContentSize);
2893 Asm->OutStreamer.AddComment("DWARF Arange version number");
2894 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2895 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
2896 Asm->EmitSectionOffset(
2897 Asm->GetTempSymbol(ISec->getLabelBeginName(), CU->getUniqueID()),
2898 DwarfInfoSectionSym);
2899 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2900 Asm->EmitInt8(PtrSize);
2901 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2902 Asm->EmitInt8(0);
2903
2904 for (unsigned n = 0; n < Padding; n++)
2905 Asm->EmitInt8(0xff);
2906
2907 for (unsigned n = 0; n < List.size(); n++) {
2908 const ArangeSpan &Span = List[n];
2909 Asm->EmitLabelReference(Span.Start, PtrSize);
2910
2911 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002912 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002913 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002914 } else {
2915 // For symbols without an end marker (e.g. common), we
2916 // write a single arange entry containing just that one symbol.
2917 uint64_t Size = SymSize[Span.Start];
2918 if (Size == 0)
2919 Size = 1;
2920
2921 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2922 }
Richard Mitton21101b32013-09-19 23:21:01 +00002923 }
2924
2925 Asm->OutStreamer.AddComment("ARange terminator");
2926 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2927 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2928 }
Bill Wendling480ff322009-05-20 23:21:38 +00002929}
2930
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002931// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002932void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002933 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002934 Asm->OutStreamer.SwitchSection(
2935 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002936
Eric Christopher0f63d062013-12-03 00:45:45 +00002937 // Size for our labels.
2938 unsigned char Size = Asm->getDataLayout().getPointerSize();
2939
2940 // Grab the specific ranges for the compile units in the module.
2941 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2942 E = CUMap.end();
2943 I != E; ++I) {
2944 CompileUnit *TheCU = I->second;
2945 unsigned ID = TheCU->getUniqueID();
2946
2947 // Emit a symbol so we can find the beginning of our ranges.
2948 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_ranges", ID));
2949
2950 // Iterate over the misc ranges for the compile units in the module.
2951 const SmallVectorImpl<RangeSpanList *> &RangeLists = TheCU->getRangeLists();
2952 for (SmallVectorImpl<RangeSpanList *>::const_iterator
2953 I = RangeLists.begin(),
2954 E = RangeLists.end();
2955 I != E; ++I) {
2956 RangeSpanList *List = *I;
2957
2958 // Emit a symbol so we can find the beginning of the range.
2959 Asm->OutStreamer.EmitLabel(
2960 Asm->GetTempSymbol("debug_ranges", List->getIndex()));
2961
2962 for (SmallVectorImpl<RangeSpan>::const_iterator
2963 I = List->getRanges().begin(),
2964 E = List->getRanges().end();
2965 I != E; ++I) {
2966 RangeSpan Range = *I;
2967 // We occasionally have ranges without begin/end labels.
2968 // FIXME: Verify and fix.
2969 const MCSymbol *Begin = Range.getStart();
2970 const MCSymbol *End = Range.getEnd();
2971 Begin ? Asm->OutStreamer.EmitSymbolValue(Begin, Size)
2972 : Asm->OutStreamer.EmitIntValue(0, Size);
2973 End ? Asm->OutStreamer.EmitSymbolValue(End, Size)
2974 : Asm->OutStreamer.EmitIntValue(0, Size);
2975 }
2976
2977 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002978 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002979 Asm->OutStreamer.EmitIntValue(0, Size);
2980 }
Devang Patel12563b32010-04-16 23:33:45 +00002981 }
Bill Wendling480ff322009-05-20 23:21:38 +00002982}
2983
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002984// Emit visible names into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002985void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002986 if (const MCSection *LineInfo =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002987 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002988 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002989 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002990 }
2991}
2992
Eric Christopherd692c1d2012-12-11 19:42:09 +00002993// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002994
2995// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2996// 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 +00002997// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher6fdf3242013-08-26 23:50:43 +00002998CompileUnit *DwarfDebug::constructSkeletonCU(const CompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002999
3000 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikieb01f13e2013-11-15 23:54:45 +00003001 CompileUnit *NewCU = new CompileUnit(CU->getUniqueID(), Die, CU->getNode(),
3002 Asm, this, &SkeletonHolder);
Eric Christopher4c7765f2013-01-17 03:00:04 +00003003
Eric Christopherdae389b2013-02-22 23:50:08 +00003004 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikieb01f13e2013-11-15 23:54:45 +00003005 CU->getNode().getSplitDebugFilename());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003006
Eric Christopher44c6aa62013-04-22 07:51:08 +00003007 // Relocate to the beginning of the addr_base section, else 0 for the
3008 // beginning of the one for this compile unit.
Eric Christopher55863be2013-04-07 03:43:09 +00003009 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003010 NewCU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base,
3011 DwarfAddrSectionSym);
Eric Christopher55863be2013-04-07 03:43:09 +00003012 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003013 NewCU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003014
3015 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher962c9082013-01-15 23:56:56 +00003016 // into an entity. We're using 0, or a NULL label for this.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003017 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Eric Christopher962c9082013-01-15 23:56:56 +00003018
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003019 // DW_AT_stmt_list is a offset of line number information for this
3020 // compile unit in debug_line section.
Eric Christopher55863be2013-04-07 03:43:09 +00003021 // FIXME: Should handle multiple compile units.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003022 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003023 NewCU->addSectionLabel(Die, dwarf::DW_AT_stmt_list,
3024 DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003025 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003026 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003027
3028 if (!CompilationDir.empty())
Eric Christopher2cbd5762013-01-07 19:32:41 +00003029 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003030
Eric Christopher1d06eb52013-10-01 00:43:31 +00003031 // Flags to let the linker know we have emitted new style pubnames.
Eric Christopher39eebfa2013-09-30 23:14:16 +00003032 if (GenerateGnuPubSections) {
3033 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003034 NewCU->addSectionLabel(
3035 Die, dwarf::DW_AT_GNU_pubnames,
3036 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00003037 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003038 NewCU->addSectionDelta(
3039 Die, dwarf::DW_AT_GNU_pubnames,
3040 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
3041 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00003042
3043 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003044 NewCU->addSectionLabel(
3045 Die, dwarf::DW_AT_GNU_pubtypes,
3046 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00003047 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003048 NewCU->addSectionDelta(
3049 Die, dwarf::DW_AT_GNU_pubtypes,
3050 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
3051 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00003052 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00003053
Eric Christopher0f63d062013-12-03 00:45:45 +00003054 // Attribute if we've emitted any ranges and their location for the compile unit.
3055 if (CU->getRangeLists().size()) {
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003056 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher0f63d062013-12-03 00:45:45 +00003057 NewCU->addSectionLabel(
3058 Die, dwarf::DW_AT_GNU_ranges_base,
3059 Asm->GetTempSymbol("gnu_ranges", NewCU->getUniqueID()));
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003060 else
Eric Christopher0f63d062013-12-03 00:45:45 +00003061 NewCU->addSectionDelta(
3062 Die, dwarf::DW_AT_GNU_ranges_base,
3063 Asm->GetTempSymbol("gnu_ranges", NewCU->getUniqueID()),
3064 DwarfDebugRangeSectionSym);
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003065 }
3066
Eric Christopherc8a310e2012-12-10 23:34:43 +00003067 SkeletonHolder.addUnit(NewCU);
3068
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003069 return NewCU;
3070}
3071
Eric Christopher3c5a1912012-12-19 22:02:53 +00003072void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
3073 assert(useSplitDwarf() && "No split dwarf debug info?");
3074 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003075}
3076
Eric Christopherd692c1d2012-12-11 19:42:09 +00003077// Emit the .debug_info.dwo section for separated dwarf. This contains the
3078// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003079void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00003080 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christophera2de8262012-12-15 00:04:07 +00003081 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher3c5a1912012-12-19 22:02:53 +00003082 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3083 DwarfAbbrevDWOSectionSym);
3084}
3085
3086// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3087// abbreviations for the .debug_info.dwo section.
3088void DwarfDebug::emitDebugAbbrevDWO() {
3089 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher48fef592012-12-20 21:58:40 +00003090 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3091 &Abbreviations);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003092}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003093
3094// Emit the .debug_str.dwo section for separated dwarf. This contains the
3095// string section and is identical in format to traditional .debug_str
3096// sections.
3097void DwarfDebug::emitDebugStrDWO() {
3098 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00003099 const MCSection *OffSec =
3100 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00003101 const MCSymbol *StrSym = DwarfStrSectionSym;
3102 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
3103 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003104}
David Blaikie409dd9c2013-11-19 23:08:21 +00003105
David Blaikie3c1d3322013-12-02 18:44:29 +00003106void DwarfDebug::addTypeUnitType(uint16_t Language, DIE *RefDie,
3107 DICompositeType CTy) {
Eric Christopherf52eddf2013-11-26 22:23:27 +00003108 DenseMap<const MDNode *,
3109 std::pair<uint64_t, SmallVectorImpl<DIE *> *> >::iterator I =
3110 TypeUnits.find(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003111 SmallVector<DIE *, 8> References;
3112 References.push_back(RefDie);
3113 if (I != TypeUnits.end()) {
3114 if (I->second.second) {
3115 I->second.second->push_back(RefDie);
3116 return;
3117 }
3118 } else {
3119 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie319a05f2013-12-02 19:33:10 +00003120 TypeUnit *NewTU = new TypeUnit(GlobalCUIndexCount++, UnitDie, Language, Asm,
3121 this, &InfoHolder);
3122 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie3c1d3322013-12-02 18:44:29 +00003123 Language);
David Blaikie409dd9c2013-11-19 23:08:21 +00003124
3125 // Register the type in the TypeUnits map with a vector of references to be
3126 // populated whenever a reference is required.
3127 I = TypeUnits.insert(std::make_pair(CTy, std::make_pair(0, &References)))
3128 .first;
3129
3130 // Construct the type, this may, recursively, require more type units that
3131 // may in turn require this type again - in which case they will add DIEs to
3132 // the References vector.
David Blaikie319a05f2013-12-02 19:33:10 +00003133 DIE *Die = NewTU->createTypeDIE(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003134
David Blaikie319a05f2013-12-02 19:33:10 +00003135 if (GenerateODRHash && shouldAddODRHash(NewTU, Die))
3136 NewTU->addUInt(UnitDie, dwarf::DW_AT_GNU_odr_signature,
David Blaikie409dd9c2013-11-19 23:08:21 +00003137 dwarf::DW_FORM_data8,
3138 DIEHash().computeDIEODRSignature(*Die));
3139 // FIXME: This won't handle circularly referential structures, as the DIE
3140 // may have references to other DIEs still under construction and missing
3141 // their signature. Hashing should walk through the signatures to their
3142 // referenced type, or possibly walk the precomputed hashes of related types
3143 // at the end.
3144 uint64_t Signature = DIEHash().computeTypeSignature(*Die);
3145
3146 // Remove the References vector and add the type hash.
3147 I->second.first = Signature;
3148 I->second.second = NULL;
3149
David Blaikie319a05f2013-12-02 19:33:10 +00003150 InfoHolder.addUnit(NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00003151 }
3152
3153 // Populate all the signatures.
3154 for (unsigned i = 0, e = References.size(); i != e; ++i) {
3155 CUMap.begin()->second->addUInt(References[i], dwarf::DW_AT_signature,
3156 dwarf::DW_FORM_ref_sig8, I->second.first);
3157 }
3158}