blob: ec5df1ec4c7b0736fa6b795ac00e225baa23942b [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;
Eric Christopherad9fe892012-04-02 17:58:52 +0000200
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000201 // Turn on accelerator tables for Darwin by default, pubnames by
202 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000203 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000204
Eric Christopher574b5c82013-08-19 21:41:38 +0000205 if (DwarfAccelTables == Default)
206 HasDwarfAccelTables = IsDarwin;
207 else
Eric Christopher5297df02013-08-26 20:58:35 +0000208 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000209
Eric Christophercdf218d2012-12-10 19:51:21 +0000210 if (SplitDwarf == Default)
211 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000212 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000213 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000214
Eric Christopher4d36ca02013-08-26 23:24:35 +0000215 if (DwarfPubSections == Default)
216 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000217 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000218 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000219
Eric Christopher33ff6972013-11-21 23:46:41 +0000220 DwarfVersion = DwarfVersionNumber
221 ? DwarfVersionNumber
222 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000223
Dan Gohman6e681a52010-06-18 15:56:31 +0000224 {
225 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000226 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000227 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000228}
Bill Wendling2f921f82009-05-15 09:23:25 +0000229
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000230// Switch to the specified MCSection and emit an assembler
231// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000232static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000233 const char *SymbolStem = 0) {
234 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000235 if (!SymbolStem)
236 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000237
238 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
239 Asm->OutStreamer.EmitLabel(TmpSym);
240 return TmpSym;
241}
242
David Blaikie72f1a3e2013-11-23 01:17:34 +0000243DwarfUnits::~DwarfUnits() {
David Blaikie319a05f2013-12-02 19:33:10 +0000244 for (SmallVectorImpl<Unit *>::iterator I = CUs.begin(), E = CUs.end(); I != E;
245 ++I)
David Blaikie72f1a3e2013-11-23 01:17:34 +0000246 delete *I;
247}
248
Eric Christophere698f532012-12-20 21:58:36 +0000249MCSymbol *DwarfUnits::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000250 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000251}
252
Eric Christophere698f532012-12-20 21:58:36 +0000253MCSymbol *DwarfUnits::getStringPoolEntry(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000254 std::pair<MCSymbol *, unsigned> &Entry =
255 StringPool.GetOrCreateValue(Str).getValue();
256 if (Entry.first)
257 return Entry.first;
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000258
259 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000260 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000261}
262
Eric Christopher2cbd5762013-01-07 19:32:41 +0000263unsigned DwarfUnits::getStringPoolIndex(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000264 std::pair<MCSymbol *, unsigned> &Entry =
265 StringPool.GetOrCreateValue(Str).getValue();
266 if (Entry.first)
267 return Entry.second;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000268
269 Entry.second = NextStringPoolNumber++;
270 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
271 return Entry.second;
272}
273
David Blaikiedea547b2013-06-28 18:47:14 +0000274unsigned DwarfUnits::getAddrPoolIndex(const MCSymbol *Sym) {
David Blaikie8466ca82013-07-01 23:55:52 +0000275 return getAddrPoolIndex(MCSymbolRefExpr::Create(Sym, Asm->OutContext));
276}
277
Ulrich Weigand8b3d2262013-07-02 18:46:46 +0000278unsigned DwarfUnits::getAddrPoolIndex(const MCExpr *Sym) {
279 std::pair<DenseMap<const MCExpr *, unsigned>::iterator, bool> P =
David Blaikieb8ef7852013-06-28 18:47:19 +0000280 AddressPool.insert(std::make_pair(Sym, NextAddrPoolNumber));
David Blaikiea67de2b2013-06-28 18:55:13 +0000281 if (P.second)
282 ++NextAddrPoolNumber;
David Blaikieb8ef7852013-06-28 18:47:19 +0000283 return P.first->second;
Eric Christopher962c9082013-01-15 23:56:56 +0000284}
285
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000286// Define a unique number for the abbreviation.
287//
Eric Christopherc8a310e2012-12-10 23:34:43 +0000288void DwarfUnits::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000289 // Check the set for priors.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000290 DIEAbbrev *InSet = AbbreviationsSet->GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000291
292 // If it's newly added.
293 if (InSet == &Abbrev) {
294 // Add to abbreviation list.
David Blaikie2d4e1122013-10-30 17:14:24 +0000295 Abbreviations.push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000296
297 // Assign the vector position + 1 as its number.
David Blaikie2d4e1122013-10-30 17:14:24 +0000298 Abbrev.setNumber(Abbreviations.size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000299 } else {
300 // Assign existing abbreviation number.
301 Abbrev.setNumber(InSet->getNumber());
302 }
303}
304
Eric Christopherd9843b32011-11-10 19:25:34 +0000305static bool isObjCClass(StringRef Name) {
306 return Name.startswith("+") || Name.startswith("-");
307}
308
309static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000310 if (!isObjCClass(Name))
311 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000312
Benjamin Kramer260de742013-08-24 12:15:54 +0000313 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000314}
315
316static void getObjCClassCategory(StringRef In, StringRef &Class,
317 StringRef &Category) {
318 if (!hasObjCCategory(In)) {
319 Class = In.slice(In.find('[') + 1, In.find(' '));
320 Category = "";
321 return;
322 }
323
324 Class = In.slice(In.find('[') + 1, In.find('('));
325 Category = In.slice(In.find('[') + 1, In.find(' '));
326 return;
327}
328
329static StringRef getObjCMethodName(StringRef In) {
330 return In.slice(In.find(' ') + 1, In.find(']'));
331}
332
Richard Mittonc2508242013-10-03 22:07:08 +0000333// Helper for sorting sections into a stable output order.
334static bool SectionSort(const MCSection *A, const MCSection *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000335 std::string LA = (A ? A->getLabelBeginName() : "");
336 std::string LB = (B ? B->getLabelBeginName() : "");
337 return LA < LB;
Richard Mittonc2508242013-10-03 22:07:08 +0000338}
339
Eric Christopherd9843b32011-11-10 19:25:34 +0000340// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000341// TODO: Determine whether or not we should add names for programs
342// that do not have a DW_AT_name or DW_AT_linkage_name field - this
343// is only slightly different than the lookup of non-standard ObjC names.
David Blaikie2a80e442013-12-02 22:09:48 +0000344static void addSubprogramNames(Unit *TheU, DISubprogram SP, DIE *Die) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000345 if (!SP.isDefinition())
346 return;
David Blaikie2a80e442013-12-02 22:09:48 +0000347 TheU->addAccelName(SP.getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000348
349 // If the linkage name is different than the name, go ahead and output
350 // that as well into the name table.
351 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
David Blaikie2a80e442013-12-02 22:09:48 +0000352 TheU->addAccelName(SP.getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000353
354 // If this is an Objective-C selector name add it to the ObjC accelerator
355 // too.
356 if (isObjCClass(SP.getName())) {
357 StringRef Class, Category;
358 getObjCClassCategory(SP.getName(), Class, Category);
David Blaikie2a80e442013-12-02 22:09:48 +0000359 TheU->addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000360 if (Category != "")
David Blaikie2a80e442013-12-02 22:09:48 +0000361 TheU->addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000362 // Also add the base method name to the name table.
David Blaikie2a80e442013-12-02 22:09:48 +0000363 TheU->addAccelName(getObjCMethodName(SP.getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000364 }
365}
366
Manman Ren3eb9dff2013-09-09 19:05:21 +0000367/// isSubprogramContext - Return true if Context is either a subprogram
368/// or another context nested inside a subprogram.
369bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
370 if (!Context)
371 return false;
372 DIDescriptor D(Context);
373 if (D.isSubprogram())
374 return true;
375 if (D.isType())
Manman Ren116868e2013-09-09 19:47:11 +0000376 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000377 return false;
378}
379
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000380// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
381// and DW_AT_high_pc attributes. If there are global variables in this
382// scope then create and insert DIEs for these variables.
David Blaikie25bc7192013-11-15 23:13:08 +0000383DIE *DwarfDebug::updateSubprogramScopeDIE(CompileUnit *SPCU, DISubprogram SP) {
384 DIE *SPDie = SPCU->getDIE(SP);
Devang Patela37a95e2010-07-07 22:20:57 +0000385
Chris Lattner3a383cb2010-04-05 00:13:49 +0000386 assert(SPDie && "Unable to find subprogram DIE!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000387
Bill Wendlingf720bf62012-11-07 05:19:04 +0000388 // If we're updating an abstract DIE, then we will be adding the children and
389 // object pointer later on. But what we don't want to do is process the
390 // concrete DIE twice.
David Blaikie25bc7192013-11-15 23:13:08 +0000391 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000392 // Pick up abstract subprogram DIE.
David Blaikie2a80e442013-12-02 22:09:48 +0000393 SPDie =
394 SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000395 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000396 } else {
397 DISubprogram SPDecl = SP.getFunctionDeclaration();
398 if (!SPDecl.isSubprogram()) {
399 // There is not any need to generate specification DIE for a function
400 // defined at compile unit level. If a function is defined inside another
401 // function then gdb prefers the definition at top level and but does not
402 // expect specification DIE in parent function. So avoid creating
403 // specification DIE for a function defined inside a function.
Manman Renc50fa112013-10-10 18:40:01 +0000404 DIScope SPContext = resolve(SP.getContext());
405 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000406 !SPContext.isFile() && !isSubprogramContext(SPContext)) {
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000407 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
408
409 // Add arguments.
410 DICompositeType SPTy = SP.getType();
411 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000412 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000413 if (SPTag == dwarf::DW_TAG_subroutine_type)
414 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Ren4a841a82013-10-29 01:03:01 +0000415 DIE *Arg =
416 SPCU->createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie5af2aca2013-11-18 23:57:26 +0000417 DIType ATy(Args.getElement(i));
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000418 SPCU->addType(Arg, ATy);
419 if (ATy.isArtificial())
420 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
421 if (ATy.isObjectPointer())
Manman Ren4c4b69c2013-10-11 23:58:05 +0000422 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, Arg);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000423 }
424 DIE *SPDeclDie = SPDie;
David Blaikie2a80e442013-12-02 22:09:48 +0000425 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
426 *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000427 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000428 }
429 }
Devang Patela37a95e2010-07-07 22:20:57 +0000430 }
431
Eric Christopher0f63d062013-12-03 00:45:45 +0000432 MCSymbol *FuncBegin =
433 Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
434 MCSymbol *FuncEnd = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
435 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc, FuncBegin);
436 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc, FuncEnd);
437
Chris Lattner3a383cb2010-04-05 00:13:49 +0000438 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
439 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000440 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000441
Eric Christopherd9843b32011-11-10 19:25:34 +0000442 // Add name to the name table, we do this here because we're guaranteed
443 // to have concrete versions of our DW_TAG_subprogram nodes.
444 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000445
Chris Lattner3a383cb2010-04-05 00:13:49 +0000446 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000447}
448
Manman Ren5b2f4b02013-09-11 19:40:28 +0000449/// Check whether we should create a DIE for the given Scope, return true
450/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000451bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
452 if (Scope->isAbstractScope())
453 return false;
454
Manman Ren5b2f4b02013-09-11 19:40:28 +0000455 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000456 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
457 if (Ranges.empty())
458 return true;
459
460 if (Ranges.size() > 1)
461 return false;
462
Manman Ren5b2f4b02013-09-11 19:40:28 +0000463 // We don't create a DIE if we have a single Range and the end label
464 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000465 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
466 MCSymbol *End = getLabelAfterInsn(RI->second);
467 return !End;
468}
469
Eric Christopherbe2513e2013-12-03 00:45:59 +0000470void DwarfDebug::addScopeRangeList(CompileUnit *TheCU, DIE *ScopeDIE,
471 const SmallVectorImpl<InsnRange> &Range) {
472 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
473 // emitting it appropriately.
474 TheCU->addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges,
475 Asm->GetTempSymbol("debug_ranges", GlobalRangeCount));
476 RangeSpanList *List = new RangeSpanList(GlobalRangeCount++);
477 for (SmallVectorImpl<InsnRange>::const_iterator RI = Range.begin(),
478 RE = Range.end();
479 RI != RE; ++RI) {
480 RangeSpan Span(getLabelBeforeInsn(RI->first),
481 getLabelAfterInsn(RI->second));
482 List->addRange(Span);
483 }
484
485 // Add the range list to the set of ranges to be emitted.
486 TheCU->addRangeList(List);
487}
488
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000489// Construct new DW_TAG_lexical_block for this scope and attach
490// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher6a841382012-11-19 22:42:10 +0000491DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000492 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000493 if (isLexicalScopeDIENull(Scope))
494 return 0;
495
Devang Patel6c74a872010-04-27 19:46:33 +0000496 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
497 if (Scope->isAbstractScope())
498 return ScopeDIE;
499
Eric Christopher0f63d062013-12-03 00:45:45 +0000500 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherbe2513e2013-12-03 00:45:59 +0000501
Eric Christopherdc42ea82013-07-03 01:57:28 +0000502 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000503 if (ScopeRanges.size() > 1) {
Eric Christopherbe2513e2013-12-03 00:45:59 +0000504 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
Devang Patel6c74a872010-04-27 19:46:33 +0000505 return ScopeDIE;
506 }
507
Eric Christopherdc42ea82013-07-03 01:57:28 +0000508 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000509 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000510 MCSymbol *Start = getLabelBeforeInsn(RI->first);
511 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000512 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000513
Chris Lattnere13c3722010-03-09 01:58:53 +0000514 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
515 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000516
Eric Christopher962c9082013-01-15 23:56:56 +0000517 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
518 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000519
520 return ScopeDIE;
521}
522
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000523// This scope represents inlined body of a function. Construct DIE to
524// represent this concrete inlined copy of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000525DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
526 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000527 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
528 assert(ScopeRanges.empty() == false &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000529 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000530
Devang Patelf098ce22011-07-27 00:34:13 +0000531 if (!Scope->getScopeNode())
532 return NULL;
533 DIScope DS(Scope->getScopeNode());
534 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000535 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000536 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000537 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000538 return NULL;
539 }
540
Devang Patel73bc1722011-05-05 17:54:26 +0000541 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000542 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000543
Eric Christopherbe2513e2013-12-03 00:45:59 +0000544 // If we have multiple ranges, emit them into the range section.
545 if (ScopeRanges.size() > 1)
546 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
547 else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000548 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000549 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
550 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
551
552 if (StartLabel == 0 || EndLabel == 0)
553 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
554
555 assert(StartLabel->isDefined() &&
556 "Invalid starting label for an inlined scope!");
557 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
558
Eric Christopher962c9082013-01-15 23:56:56 +0000559 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
560 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000561 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000562
563 InlinedSubprogramDIEs.insert(OriginDIE);
564
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000565 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000566 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000567 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000568 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
569 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000570 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000571
Eric Christopher8dda5d02011-12-04 06:02:38 +0000572 // Add name to the name table, we do this here because we're guaranteed
573 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
574 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000575
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000576 return ScopeDIE;
577}
578
Manman Ren2312ed32013-09-10 18:40:41 +0000579DIE *DwarfDebug::createScopeChildrenDIE(CompileUnit *TheCU, LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000580 SmallVectorImpl<DIE *> &Children) {
581 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000582
583 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000584 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000585 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
586 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000587 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000588 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000589 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000590 if (ArgDV->isObjectPointer())
591 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000592 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000593
Eric Christopherf84354b2011-10-03 15:49:16 +0000594 // Collect lexical scope children first.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000595 const SmallVectorImpl<DbgVariable *> &Variables =
596 ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000597 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000598 if (DIE *Variable = TheCU->constructVariableDIE(*Variables[i],
599 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000600 Children.push_back(Variable);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000601 if (Variables[i]->isObjectPointer())
602 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000603 }
Craig Topper977e9cd2013-07-03 04:24:43 +0000604 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000605 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000606 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000607 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000608 return ObjectPointer;
609}
610
611// Construct a DIE for this scope.
612DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
613 if (!Scope || !Scope->getScopeNode())
614 return NULL;
615
616 DIScope DS(Scope->getScopeNode());
617
618 SmallVector<DIE *, 8> Children;
619 DIE *ObjectPointer = NULL;
620 bool ChildrenCreated = false;
621
Manman Ren5b2f4b02013-09-11 19:40:28 +0000622 // We try to create the scope DIE first, then the children DIEs. This will
623 // avoid creating un-used children then removing them later when we find out
624 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000625 DIE *ScopeDIE = NULL;
626 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000627 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000628 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000629 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000630 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000631 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000632 // Note down abstract DIE.
633 if (ScopeDIE)
634 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000635 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000636 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000637 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000638 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000639 if (isLexicalScopeDIENull(Scope))
640 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000641
642 // We create children here when we know the scope DIE is not going to be
643 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000644 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
645 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000646
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000647 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000648 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000649 ImportedEntityMap::const_iterator> Range =
650 std::equal_range(
651 ScopesWithImportedEntities.begin(),
652 ScopesWithImportedEntities.end(),
653 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
654 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000655 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000656 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000657 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000658 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000659 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
660 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000661 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000662 }
Eric Christopher6a841382012-11-19 22:42:10 +0000663
Benjamin Kramer892daba2013-08-24 11:55:49 +0000664 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000665 assert(Children.empty() &&
666 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000667 return NULL;
668 }
Manman Ren2312ed32013-09-10 18:40:41 +0000669 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000670 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000671 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000672
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000673 // Add children
Craig Topperd8e43652013-07-03 04:17:25 +0000674 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000675 E = Children.end();
676 I != E; ++I)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000677 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000678
Eric Christophere3417762012-09-12 23:36:19 +0000679 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000680 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000681
Eric Christopherd9843b32011-11-10 19:25:34 +0000682 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000683}
684
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000685// Look up the source id with the given directory and source file names.
686// If none currently exists, create a new id and insert it in the
687// SourceIds map. This can update DirectoryNames and SourceFileNames maps
688// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000689unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
690 unsigned CUID) {
Manman Ren1e427202013-03-07 01:42:00 +0000691 // If we use .loc in assembly, we can't separate .file entries according to
692 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000693
694 // FIXME: add a better feature test than hasRawTextSupport. Even better,
695 // extend .file to support this.
696 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000697 CUID = 0;
698
Devang Patel871d0b12010-09-16 20:57:49 +0000699 // If FE did not provide a file name, then assume stdin.
700 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000701 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000702
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000703 // TODO: this might not belong here. See if we can factor this better.
704 if (DirName == CompilationDir)
705 DirName = "";
706
Manman Ren1e427202013-03-07 01:42:00 +0000707 // FileIDCUMap stores the current ID for the given compile unit.
708 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000709
Manman Ren1e427202013-03-07 01:42:00 +0000710 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000711 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000712 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000713 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000714 NamePair += DirName;
715 NamePair += '\0'; // Zero bytes are not allowed in paths.
716 NamePair += FileName;
717
718 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
719 if (Ent.getValue() != SrcId)
720 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000721
Manman Ren1e427202013-03-07 01:42:00 +0000722 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000723 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000724 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000725
726 return SrcId;
727}
728
Eric Christopher48fef592012-12-20 21:58:40 +0000729// Create new CompileUnit for the given metadata node with tag
730// DW_TAG_compile_unit.
David Blaikie5a152402013-11-15 23:52:02 +0000731CompileUnit *DwarfDebug::constructCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000732 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000733 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000734
735 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikie5a152402013-11-15 23:52:02 +0000736 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++, Die, DIUnit, Asm,
737 this, &InfoHolder);
Manman Ren1e427202013-03-07 01:42:00 +0000738
739 FileIDCUMap[NewCU->getUniqueID()] = 0;
740 // Call this to emit a .file directive if it wasn't emitted for the source
741 // file this CU comes from yet.
742 getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
743
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000744 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000745 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
746 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000747 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000748
Eric Christopherb1b94512012-08-01 18:19:01 +0000749 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher52ce7182013-04-09 19:23:15 +0000750 // into an entity. We're using 0 (or a NULL label) for this. For
751 // split dwarf it's in the skeleton CU so omit it here.
752 if (!useSplitDwarf())
753 NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
Manman Ren4e042a62013-02-05 21:52:47 +0000754
755 // Define start line table label for each Compile Unit.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000756 MCSymbol *LineTableStartSym =
757 Asm->GetTempSymbol("line_table_start", NewCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +0000758 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
759 NewCU->getUniqueID());
760
Manman Ren9d4c7352013-05-21 00:57:22 +0000761 // Use a single line table if we are using .loc and generating assembly.
762 bool UseTheFirstCU =
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000763 (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport()) ||
764 (NewCU->getUniqueID() == 0);
Manman Ren9d4c7352013-05-21 00:57:22 +0000765
Eric Christopher52ce7182013-04-09 19:23:15 +0000766 if (!useSplitDwarf()) {
Eric Christophera51d3fc2013-09-27 22:50:48 +0000767 // DW_AT_stmt_list is a offset of line number information for this
768 // compile unit in debug_line section. For split dwarf this is
769 // left in the skeleton CU and so not included.
770 // The line table entries are not always emitted in assembly, so it
771 // is not okay to use line_table_start here.
Eric Christopher52ce7182013-04-09 19:23:15 +0000772 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000773 NewCU->addSectionLabel(
774 Die, dwarf::DW_AT_stmt_list,
775 UseTheFirstCU ? Asm->GetTempSymbol("section_line")
776 : LineTableStartSym);
Manman Ren9d4c7352013-05-21 00:57:22 +0000777 else if (UseTheFirstCU)
Eric Christopher33ff6972013-11-21 23:46:41 +0000778 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher52ce7182013-04-09 19:23:15 +0000779 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000780 NewCU->addSectionDelta(Die, dwarf::DW_AT_stmt_list,
781 LineTableStartSym, DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000782
783 // If we're using split dwarf the compilation dir is going to be in the
784 // skeleton CU and so we don't need to duplicate it here.
785 if (!CompilationDir.empty())
786 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
787
Eric Christopher1d06eb52013-10-01 00:43:31 +0000788 // Flags to let the linker know we have emitted new style pubnames. Only
Eric Christophera51d3fc2013-09-27 22:50:48 +0000789 // emit it here if we don't have a skeleton CU for split dwarf.
Eric Christopher39eebfa2013-09-30 23:14:16 +0000790 if (GenerateGnuPubSections) {
791 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000792 NewCU->addSectionLabel(
793 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000794 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000795 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000796 NewCU->addSectionDelta(
797 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000798 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
799 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000800
801 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000802 NewCU->addSectionLabel(
803 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000804 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000805 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000806 NewCU->addSectionDelta(
807 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000808 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
809 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000810 }
Eric Christopher52ce7182013-04-09 19:23:15 +0000811 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000812
Bill Wendling2b128d72009-05-20 23:19:06 +0000813 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000814 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000815
Devang Patel2d9caf92009-11-25 17:36:49 +0000816 StringRef Flags = DIUnit.getFlags();
817 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000818 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000819
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000820 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000821 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000822 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000823
Devang Patel1a0df9a2010-05-10 22:49:55 +0000824 if (!FirstCU)
825 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000826
Eric Christopherc8a310e2012-12-10 23:34:43 +0000827 InfoHolder.addUnit(NewCU);
828
David Blaikie5a152402013-11-15 23:52:02 +0000829 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000830 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000831 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000832}
833
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000834// Construct subprogram DIE.
Eric Christophera6c38a32013-10-15 23:31:38 +0000835void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000836 // FIXME: We should only call this routine once, however, during LTO if a
837 // program is defined in multiple CUs we could end up calling it out of
838 // beginModule as we walk the CUs.
839
840 CompileUnit *&CURef = SPMap[N];
841 if (CURef)
842 return;
843 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000844
Devang Patel80ae3492009-08-28 23:24:31 +0000845 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000846 if (!SP.isDefinition())
847 // This is a method declaration which will be handled while constructing
848 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000849 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000850
Devang Patel89543712011-08-15 17:24:54 +0000851 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000852
Eric Christopherba506db2013-09-09 20:03:20 +0000853 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000854 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000855}
856
David Blaikie1fd43652013-05-07 21:35:53 +0000857void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000858 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000859 DIImportedEntity Module(N);
David Blaikief55abea2013-04-22 06:12:31 +0000860 if (!Module.Verify())
861 return;
David Blaikie684fc532013-05-06 23:33:07 +0000862 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000863 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000864}
865
David Blaikie4dd2de72013-05-08 06:01:38 +0000866void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
David Blaikie684fc532013-05-06 23:33:07 +0000867 DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000868 DIImportedEntity Module(N);
David Blaikie684fc532013-05-06 23:33:07 +0000869 if (!Module.Verify())
870 return;
David Blaikie4dd2de72013-05-08 06:01:38 +0000871 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000872}
873
David Blaikie4dd2de72013-05-08 06:01:38 +0000874void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000875 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000876 DIE *Context) {
877 assert(Module.Verify() &&
878 "Use one of the MDNode * overloads to handle invalid metadata");
879 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000880 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000881 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000882 DIE *EntityDie;
883 DIDescriptor Entity = Module.getEntity();
884 if (Entity.isNameSpace())
885 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
886 else if (Entity.isSubprogram())
887 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000888 else if (Entity.isType())
889 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000890 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000891 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000892 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
893 Module.getContext().getDirectory(),
894 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000895 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
896 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000897 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000898 StringRef Name = Module.getName();
899 if (!Name.empty())
900 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000901 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000902}
903
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000904// Emit all Dwarf sections that should come prior to the content. Create
905// global DIEs and emit initial debug info sections. This is invoked by
906// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000907void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000908 if (DisableDebugInfoPrinting)
909 return;
910
Eric Christopher58f41952012-11-19 22:42:15 +0000911 const Module *M = MMI->getModule();
912
Nick Lewycky019d2552011-07-29 03:49:23 +0000913 // If module has named metadata anchors then use them, otherwise scan the
914 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000915 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000916 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000917 return;
Manman Ren60352032013-09-05 18:48:31 +0000918 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000919
David Blaikiedc69ebb2013-03-11 23:39:23 +0000920 // Emit initial sections so we can reference labels later.
921 emitSectionLabels();
922
923 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
924 DICompileUnit CUNode(CU_Nodes->getOperand(i));
925 CompileUnit *CU = constructCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000926 DIArray ImportedEntities = CUNode.getImportedEntities();
927 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000928 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000929 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
930 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000931 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000932 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000933 DIArray GVs = CUNode.getGlobalVariables();
934 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000935 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000936 DIArray SPs = CUNode.getSubprograms();
937 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
938 constructSubprogramDIE(CU, SPs.getElement(i));
939 DIArray EnumTypes = CUNode.getEnumTypes();
940 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
941 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
942 DIArray RetainedTypes = CUNode.getRetainedTypes();
943 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
944 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000945 // Emit imported_modules last so that the relevant context is already
946 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000947 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
948 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000949 }
Eric Christopher6a841382012-11-19 22:42:10 +0000950
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000951 // Tell MMI that we have debug info.
952 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000953
Bill Wendling2b128d72009-05-20 23:19:06 +0000954 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000955 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000956}
957
Eric Christopher960ac372012-11-22 00:59:49 +0000958// Attach DW_AT_inline attribute with inlined subprogram DIEs.
959void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000960 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
961 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000962 AE = InlinedSubprogramDIEs.end();
963 AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000964 DIE *ISP = *AI;
David Blaikief2443192013-10-21 17:28:37 +0000965 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000966 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000967 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000968 AE = AbstractSPDies.end();
969 AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000970 DIE *ISP = AI->second;
971 if (InlinedSubprogramDIEs.count(ISP))
972 continue;
David Blaikief2443192013-10-21 17:28:37 +0000973 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000974 }
Eric Christopher960ac372012-11-22 00:59:49 +0000975}
976
977// Collect info for variables that were optimized out.
978void DwarfDebug::collectDeadVariables() {
979 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000980
981 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
982 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
983 DICompileUnit TheCU(CU_Nodes->getOperand(i));
984 DIArray Subprograms = TheCU.getSubprograms();
985 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000986 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000987 if (ProcessedSPNodes.count(SP) != 0)
988 continue;
989 if (!SP.isSubprogram())
990 continue;
991 if (!SP.isDefinition())
992 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000993 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +0000994 if (Variables.getNumElements() == 0)
995 continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000996
Eric Christopher735401c2012-11-27 00:13:51 +0000997 // Construct subprogram DIE and add variables DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +0000998 CompileUnit *SPCU = static_cast<CompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +0000999 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +00001000 // FIXME: See the comment in constructSubprogramDIE about duplicate
1001 // subprogram DIEs.
1002 constructSubprogramDIE(SPCU, SP);
1003 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +00001004 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
1005 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +00001006 if (!DV.isVariable())
1007 continue;
Manman Renb3388602013-10-05 01:43:03 +00001008 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001009 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +00001010 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +00001011 }
Eric Christopher960ac372012-11-22 00:59:49 +00001012 }
1013 }
1014 }
Eric Christopher960ac372012-11-22 00:59:49 +00001015}
1016
Eric Christopher45731982013-08-08 23:45:55 +00001017// Type Signature [7.27] and ODR Hash code.
Eric Christopher67646432013-07-26 17:02:41 +00001018
1019/// \brief Grabs the string in whichever attribute is passed in and returns
Eric Christopher8552e222013-08-07 01:18:33 +00001020/// a reference to it. Returns "" if the attribute doesn't exist.
Eric Christopher67646432013-07-26 17:02:41 +00001021static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
Eric Christopher8552e222013-08-07 01:18:33 +00001022 DIEValue *V = Die->findAttribute(Attr);
Eric Christopher67646432013-07-26 17:02:41 +00001023
Eric Christopher8552e222013-08-07 01:18:33 +00001024 if (DIEString *S = dyn_cast_or_null<DIEString>(V))
1025 return S->getString();
1026
Eric Christopher67646432013-07-26 17:02:41 +00001027 return StringRef("");
1028}
1029
Eric Christopher67646432013-07-26 17:02:41 +00001030/// Return true if the current DIE is contained within an anonymous namespace.
1031static bool isContainedInAnonNamespace(DIE *Die) {
1032 DIE *Parent = Die->getParent();
1033
1034 while (Parent) {
Eric Christophere414ece2013-07-29 23:53:08 +00001035 if (Parent->getTag() == dwarf::DW_TAG_namespace &&
1036 getDIEStringAttr(Parent, dwarf::DW_AT_name) == "")
Eric Christopher67646432013-07-26 17:02:41 +00001037 return true;
1038 Parent = Parent->getParent();
1039 }
1040
1041 return false;
1042}
1043
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001044/// Test if the current CU language is C++ and that we have
1045/// a named type that is not contained in an anonymous namespace.
David Blaikie319a05f2013-12-02 19:33:10 +00001046static bool shouldAddODRHash(TypeUnit *CU, DIE *Die) {
Eric Christopher341770d2013-08-07 08:35:10 +00001047 return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
1048 getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
1049 !isContainedInAnonNamespace(Die);
Eric Christopher45731982013-08-08 23:45:55 +00001050}
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001051
Eric Christopher960ac372012-11-22 00:59:49 +00001052void DwarfDebug::finalizeModuleInfo() {
1053 // Collect info for variables that were optimized out.
1054 collectDeadVariables();
1055
1056 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1057 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001058
Eric Christopher60eb7692013-08-12 20:27:48 +00001059 // Handle anything that needs to be done on a per-cu basis.
David Blaikie319a05f2013-12-02 19:33:10 +00001060 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
1061 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00001062 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00001063 Unit *TheU = *I;
Eric Christopher60eb7692013-08-12 20:27:48 +00001064 // Emit DW_AT_containing_type attribute to connect types with their
1065 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +00001066 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +00001067
1068 // If we're splitting the dwarf out now that we've got the entire
1069 // CU then construct a skeleton CU based upon it.
David Blaikie319a05f2013-12-02 19:33:10 +00001070 if (useSplitDwarf() &&
David Blaikie2a80e442013-12-02 22:09:48 +00001071 TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
Eric Christopherd29614f2013-08-13 01:21:55 +00001072 uint64_t ID = 0;
1073 if (GenerateCUHash) {
1074 DIEHash CUHash;
David Blaikie2a80e442013-12-02 22:09:48 +00001075 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
Eric Christopherd29614f2013-08-13 01:21:55 +00001076 }
Eric Christopher60eb7692013-08-12 20:27:48 +00001077 // This should be a unique identifier when we want to build .dwp files.
David Blaikie2a80e442013-12-02 22:09:48 +00001078 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1079 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001080 // Now construct the skeleton CU associated.
David Blaikie2a80e442013-12-02 22:09:48 +00001081 CompileUnit *SkCU = constructSkeletonCU(static_cast<CompileUnit *>(TheU));
Eric Christopher60eb7692013-08-12 20:27:48 +00001082 // This should be a unique identifier when we want to build .dwp files.
David Blaikie2a80e442013-12-02 22:09:48 +00001083 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopherd29614f2013-08-13 01:21:55 +00001084 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001085 }
1086 }
1087
1088 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001089 InfoHolder.computeSizeAndOffsets();
1090 if (useSplitDwarf())
1091 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001092}
1093
1094void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001095 // Filter labels by section.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001096 for (size_t n = 0; n < ArangeLabels.size(); n++) {
1097 const SymbolCU &SCU = ArangeLabels[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001098 if (SCU.Sym->isInSection()) {
1099 // Make a note of this symbol and it's section.
1100 const MCSection *Section = &SCU.Sym->getSection();
1101 if (!Section->getKind().isMetadata())
1102 SectionMap[Section].push_back(SCU);
1103 } else {
1104 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1105 // appear in the output. This sucks as we rely on sections to build
1106 // arange spans. We can do it without, but it's icky.
1107 SectionMap[NULL].push_back(SCU);
1108 }
1109 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001110
Richard Mittonc2508242013-10-03 22:07:08 +00001111 // Build a list of sections used.
1112 std::vector<const MCSection *> Sections;
Richard Mitton21101b32013-09-19 23:21:01 +00001113 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
1114 it++) {
1115 const MCSection *Section = it->first;
Richard Mittonc2508242013-10-03 22:07:08 +00001116 Sections.push_back(Section);
1117 }
1118
1119 // Sort the sections into order.
1120 // This is only done to ensure consistent output order across different runs.
1121 std::sort(Sections.begin(), Sections.end(), SectionSort);
1122
1123 // Add terminating symbols for each section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001124 for (unsigned ID = 0; ID < Sections.size(); ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001125 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001126 MCSymbol *Sym = NULL;
1127
1128 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001129 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001130 // if we know the section name up-front. For user-created sections, the
1131 // resulting
1132 // label may not be valid to use as a label. (section names can use a
1133 // greater
Richard Mittonc2508242013-10-03 22:07:08 +00001134 // set of characters on some systems)
1135 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001136 Asm->OutStreamer.SwitchSection(Section);
1137 Asm->OutStreamer.EmitLabel(Sym);
1138 }
1139
1140 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001141 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001142 }
Eric Christopher960ac372012-11-22 00:59:49 +00001143}
Bill Wendling2b128d72009-05-20 23:19:06 +00001144
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001145// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001146void DwarfDebug::endModule() {
1147
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001148 if (!FirstCU)
1149 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001150
1151 // End any existing sections.
1152 // TODO: Does this need to happen?
1153 endSections();
1154
1155 // Finalize the debug info for the module.
1156 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001157
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001158 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001159
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001160 // Emit all the DIEs into a debug info section.
1161 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001162
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001163 // Corresponding abbreviations into a abbrev section.
1164 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001165
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001166 // Emit info into a debug loc section.
1167 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001168
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001169 // Emit info into a debug aranges section.
1170 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001171
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001172 // Emit info into a debug ranges section.
1173 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001174
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001175 // Emit info into a debug macinfo section.
1176 emitDebugMacInfo();
Eric Christopher95198f502012-11-27 22:43:42 +00001177
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001178 if (useSplitDwarf()) {
1179 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001180 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001181 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001182 // Emit DWO addresses.
1183 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001184 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001185
Eric Christophera876b822012-08-23 07:32:06 +00001186 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001187 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001188 emitAccelNames();
1189 emitAccelObjC();
1190 emitAccelNamespaces();
1191 emitAccelTypes();
1192 }
Eric Christopher6a841382012-11-19 22:42:10 +00001193
Eric Christopher4b358182013-08-30 00:40:17 +00001194 // Emit the pubnames and pubtypes sections if requested.
1195 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001196 emitDebugPubNames(GenerateGnuPubSections);
1197 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001198 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001199
Devang Pateld0701282010-08-02 17:32:15 +00001200 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001201 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001202
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001203 // Reset these for the next Module if we have one.
1204 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001205}
1206
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001207// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001208DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001209 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001210 LLVMContext &Ctx = DV->getContext();
1211 // More then one inlined variable corresponds to one abstract variable.
1212 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001213 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001214 if (AbsDbgVariable)
1215 return AbsDbgVariable;
1216
Devang Patel7e623022011-08-10 20:55:27 +00001217 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001218 if (!Scope)
1219 return NULL;
1220
Manman Renb3388602013-10-05 01:43:03 +00001221 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001222 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001223 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001224 return AbsDbgVariable;
1225}
1226
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001227// If Var is a current function argument then add it to CurrentFnArguments list.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001228bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
1229 DbgVariable *Var, LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +00001230 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001231 return false;
1232 DIVariable DV = Var->getVariable();
1233 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1234 return false;
1235 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001236 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001237 return false;
1238
Devang Patel4ab660b2011-03-03 20:02:02 +00001239 size_t Size = CurrentFnArguments.size();
1240 if (Size == 0)
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001241 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001242 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001243 // arguments does the function have at source level.
1244 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001245 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001246 CurrentFnArguments[ArgNo - 1] = Var;
1247 return true;
1248}
1249
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001250// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001251void DwarfDebug::collectVariableInfoFromMMITable(
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001252 const MachineFunction *MF, SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001253 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1254 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001255 VE = VMap.end();
1256 VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001257 const MDNode *Var = VI->first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001258 if (!Var)
1259 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001260 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001261 DIVariable DV(Var);
1262 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001263
Devang Patel7e623022011-08-10 20:55:27 +00001264 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001265
Devang Patelcdb7d442009-11-10 23:20:04 +00001266 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001267 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001268 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001269
Devang Patele1c53f22010-05-20 16:36:41 +00001270 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001271 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001272 RegVar->setFrameIndex(VP.first);
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001273 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001274 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001275 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001276 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001277 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001278}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001279
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001280// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1281// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001282static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001283 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001284 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1285 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001286 (MI->getOperand(1).isImm() ||
1287 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001288}
1289
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001290// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001291static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1292 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001293 const MCSymbol *SLabel,
1294 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001295 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001296
David Blaikie0252265b2013-06-16 20:34:15 +00001297 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001298 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001299 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001300 // If the second operand is an immediate, this is a
1301 // register-indirect address.
1302 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001303 MLoc.set(MI->getOperand(0).getReg());
1304 else
1305 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001306 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1307 }
1308 if (MI->getOperand(0).isImm())
1309 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1310 if (MI->getOperand(0).isFPImm())
1311 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1312 if (MI->getOperand(0).isCImm())
1313 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1314
Craig Topperee4dab52012-02-05 08:31:47 +00001315 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001316}
1317
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001318// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001319void
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001320DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1321 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001322
Eric Christopher270a12c2013-07-03 21:37:03 +00001323 // Grab the variable info that was squirreled away in the MMI side-table.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001324 collectVariableInfoFromMMITable(MF, Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001325
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001326 for (SmallVectorImpl<const MDNode *>::const_iterator
1327 UVI = UserVariables.begin(),
1328 UVE = UserVariables.end();
1329 UVI != UVE; ++UVI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001330 const MDNode *Var = *UVI;
1331 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001332 continue;
1333
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001334 // History contains relevant DBG_VALUE instructions for Var and instructions
1335 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001336 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001337 if (History.empty())
1338 continue;
1339 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001340
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001341 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001342 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001343 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001344 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001345 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001346 else if (MDNode *IA = DV.getInlinedAt())
1347 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1348 else
1349 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001350 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001351 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001352 continue;
1353
1354 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001355 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001356 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001357 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001358 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001359 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001360 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001361 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001362
Eric Christophercc10d202012-10-08 20:48:54 +00001363 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001364 if (History.size() <= 1 ||
1365 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001366 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001367 continue;
1368 }
1369
Eric Christopher59cc0712013-01-28 17:33:26 +00001370 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001371 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001372
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001373 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1374 HI = History.begin(),
1375 HE = History.end();
1376 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001377 const MachineInstr *Begin = *HI;
1378 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001379
Devang Patele7181b52011-06-01 23:00:17 +00001380 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001381 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1382 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001383 continue;
1384
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001385 // Compute the range for a register location.
1386 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1387 const MCSymbol *SLabel = 0;
1388
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001389 if (HI + 1 == HE)
1390 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001391 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001392 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001393 else {
1394 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001395 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001396 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001397 if (End->isDebugValue())
1398 SLabel = getLabelBeforeInsn(End);
1399 else {
1400 // End is a normal instruction clobbering the range.
1401 SLabel = getLabelAfterInsn(End);
1402 assert(SLabel && "Forgot label after clobber instruction");
1403 ++HI;
1404 }
1405 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001406
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001407 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001408 DotDebugLocEntries.push_back(
1409 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001410 }
1411 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001412 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001413
1414 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001415 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1416 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1417 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1418 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001419 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001420 continue;
1421 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001422 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001423 }
Devang Patel9fc11702010-05-25 23:40:22 +00001424}
Devang Patele0a94bf2010-05-14 21:01:35 +00001425
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001426// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001427MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001428 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1429 assert(Label && "Didn't insert label before instruction");
1430 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001431}
1432
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001433// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001434MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001435 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001436}
1437
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001438// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001439void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001440 // Check if source location changes, but ignore DBG_VALUE locations.
1441 if (!MI->isDebugValue()) {
1442 DebugLoc DL = MI->getDebugLoc();
1443 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001444 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001445 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001446 if (DL == PrologEndLoc) {
1447 Flags |= DWARF2_FLAG_PROLOGUE_END;
1448 PrologEndLoc = DebugLoc();
1449 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001450 if (PrologEndLoc.isUnknown())
1451 Flags |= DWARF2_FLAG_IS_STMT;
1452
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001453 if (!DL.isUnknown()) {
1454 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001455 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001456 } else
Devang Patel34a66202011-05-11 19:22:19 +00001457 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001458 }
Devang Patel9fc11702010-05-25 23:40:22 +00001459 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001460
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001461 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001462 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1463 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001464
1465 // No label needed.
1466 if (I == LabelsBeforeInsn.end())
1467 return;
1468
1469 // Label already assigned.
1470 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001471 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001472
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001473 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001474 PrevLabel = MMI->getContext().CreateTempSymbol();
1475 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001476 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001477 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001478}
1479
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001480// Process end of an instruction.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001481void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001482 // Don't create a new label after DBG_VALUE instructions.
1483 // They don't generate code.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001484 if (!MI->isDebugValue())
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001485 PrevLabel = 0;
1486
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001487 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001488 LabelsAfterInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001489
1490 // No label needed.
1491 if (I == LabelsAfterInsn.end())
1492 return;
1493
1494 // Label already assigned.
1495 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001496 return;
1497
1498 // We need a label after this instruction.
1499 if (!PrevLabel) {
1500 PrevLabel = MMI->getContext().CreateTempSymbol();
1501 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001502 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001503 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001504}
1505
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001506// Each LexicalScope has first instruction and last instruction to mark
1507// beginning and end of a scope respectively. Create an inverse map that list
1508// scopes starts (and ends) with an instruction. One instruction may start (or
1509// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001510void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001511 SmallVector<LexicalScope *, 4> WorkList;
1512 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001513 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001514 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001515
Craig Topper977e9cd2013-07-03 04:24:43 +00001516 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001517 if (!Children.empty())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001518 for (SmallVectorImpl<LexicalScope *>::const_iterator
1519 SI = Children.begin(),
1520 SE = Children.end();
1521 SI != SE; ++SI)
Devang Patel7771b7c2010-01-20 02:05:23 +00001522 WorkList.push_back(*SI);
1523
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001524 if (S->isAbstractScope())
1525 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001526
Craig Topper977e9cd2013-07-03 04:24:43 +00001527 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001528 if (Ranges.empty())
1529 continue;
Craig Topperd8e43652013-07-03 04:17:25 +00001530 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001531 RE = Ranges.end();
1532 RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001533 assert(RI->first && "InsnRange does not have first instruction!");
1534 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001535 requestLabelBeforeInsn(RI->first);
1536 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001537 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001538 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001539}
1540
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001541// Get MDNode for DebugLoc's scope.
Devang Patel589845d2011-05-09 22:14:49 +00001542static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1543 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1544 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1545 return DL.getScope(Ctx);
1546}
1547
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001548// Walk up the scope chain of given debug loc and find line number info
1549// for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001550static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1551 const MDNode *Scope = getScopeNode(DL, Ctx);
1552 DISubprogram SP = getDISubprogram(Scope);
Manman Ren7504ed42013-07-08 18:33:29 +00001553 if (SP.isSubprogram()) {
Eric Christopher34164192012-04-03 00:43:49 +00001554 // Check for number of operands since the compatibility is
1555 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001556 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001557 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1558 else
1559 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1560 }
1561
Devang Patel34a66202011-05-11 19:22:19 +00001562 return DebugLoc();
1563}
1564
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001565// Gather pre-function debug information. Assumes being called immediately
1566// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001567void DwarfDebug::beginFunction(const MachineFunction *MF) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001568
1569 // If there's no debug info for the function we're not going to do anything.
1570 if (!MMI->hasDebugInfo())
1571 return;
1572
1573 // Grab the lexical scopes for the function, if we don't have any of those
1574 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001575 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001576 if (LScopes.empty())
1577 return;
1578
1579 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1580
1581 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001582 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001583
Manman Ren4e042a62013-02-05 21:52:47 +00001584 // Set DwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001585 // belongs to so that we add to the correct per-cu line table in the
1586 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001587 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1588 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1589 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001590 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren9d4c7352013-05-21 00:57:22 +00001591 // Use a single line table if we are using .loc and generating assembly.
1592 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1593 else
1594 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001595
Eric Christopherfedfa442013-11-01 23:14:17 +00001596 // Emit a label for the function so that we have a beginning address.
1597 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001598 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001599 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001600
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001601 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001602 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001603 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001604
Eric Christopherfedfa442013-11-01 23:14:17 +00001605 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1606 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001607 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001608 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1609 II != IE; ++II) {
1610 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001611
Devang Patel002d54d2010-05-26 19:37:24 +00001612 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001613 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001614
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001615 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001616 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001617 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001618
1619 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001620 if (isDbgValueInDefinedReg(MI))
1621 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1622
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001623 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001624 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001625 if (History.empty()) {
1626 UserVariables.push_back(Var);
1627 // The first mention of a function argument gets the FunctionBeginSym
1628 // label, so arguments are visible when breaking at function entry.
1629 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001630 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001631 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001632 LabelsBeforeInsn[MI] = FunctionBeginSym;
1633 } else {
1634 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1635 const MachineInstr *Prev = History.back();
1636 if (Prev->isDebugValue()) {
1637 // Coalesce identical entries at the end of History.
1638 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001639 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001640 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001641 << "\t" << *Prev << "\t"
1642 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001643 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001644 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001645
1646 // Terminate old register assignments that don't reach MI;
1647 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1648 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1649 isDbgValueInDefinedReg(Prev)) {
1650 // Previous register assignment needs to terminate at the end of
1651 // its basic block.
1652 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001653 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001654 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001655 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001656 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001657 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001658 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001659 } else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001660 // Terminate after LastMI.
1661 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001662 }
1663 }
1664 }
1665 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001666 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001667 // Not a DBG_VALUE instruction.
1668 if (!MI->isLabel())
1669 AtBlockEntry = false;
1670
Eric Christopher133195782012-10-04 20:46:14 +00001671 // First known non-DBG_VALUE and non-frame setup location marks
1672 // the beginning of the function body.
1673 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1674 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001675 PrologEndLoc = MI->getDebugLoc();
1676
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001677 // Check if the instruction clobbers any registers with debug vars.
1678 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
Eric Christopherfedfa442013-11-01 23:14:17 +00001679 MOE = MI->operands_end();
1680 MOI != MOE; ++MOI) {
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001681 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1682 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001683 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true); AI.isValid();
1684 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001685 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001686 const MDNode *Var = LiveUserVar[Reg];
1687 if (!Var)
1688 continue;
1689 // Reg is now clobbered.
1690 LiveUserVar[Reg] = 0;
1691
1692 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001693 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1694 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001695 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001696 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001697 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001698 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001699 const MachineInstr *Prev = History.back();
1700 // Sanity-check: Register assignments are terminated at the end of
1701 // their block.
1702 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1703 continue;
1704 // Is the variable still in Reg?
1705 if (!isDbgValueInDefinedReg(Prev) ||
1706 Prev->getOperand(0).getReg() != Reg)
1707 continue;
1708 // Var is clobbered. Make sure the next instruction gets a label.
1709 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001710 }
1711 }
Devang Patel002d54d2010-05-26 19:37:24 +00001712 }
Devang Patel002d54d2010-05-26 19:37:24 +00001713 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001714 }
1715
1716 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1717 I != E; ++I) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001718 SmallVectorImpl<const MachineInstr *> &History = I->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001719 if (History.empty())
1720 continue;
1721
1722 // Make sure the final register assignments are terminated.
1723 const MachineInstr *Prev = History.back();
1724 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1725 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001726 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001727 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001728 if (LastMI == PrevMBB->end())
1729 // Drop DBG_VALUE for empty range.
1730 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001731 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001732 // Terminate after LastMI.
1733 History.push_back(LastMI);
1734 }
1735 }
1736 // Request labels for the full history.
1737 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1738 const MachineInstr *MI = History[i];
1739 if (MI->isDebugValue())
1740 requestLabelBeforeInsn(MI);
1741 else
1742 requestLabelAfterInsn(MI);
1743 }
1744 }
Devang Patel002d54d2010-05-26 19:37:24 +00001745
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001746 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001747 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001748
1749 // Record beginning of function.
1750 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001751 DebugLoc FnStartDL =
1752 getFnDebugLoc(PrologEndLoc, MF->getFunction()->getContext());
1753 recordSourceLine(
1754 FnStartDL.getLine(), FnStartDL.getCol(),
1755 FnStartDL.getScope(MF->getFunction()->getContext()),
1756 // We'd like to list the prologue as "not statements" but GDB behaves
1757 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1758 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001759 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001760}
1761
Devang Patel7e623022011-08-10 20:55:27 +00001762void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001763 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1764 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001765 // Variables with positive arg numbers are parameters.
1766 if (unsigned ArgNum = DV.getArgNumber()) {
1767 // Keep all parameters in order at the start of the variable list to ensure
1768 // function types are correct (no out-of-order parameters)
1769 //
1770 // This could be improved by only doing it for optimized builds (unoptimized
1771 // builds have the right order to begin with), searching from the back (this
1772 // would catch the unoptimized case quickly), or doing a binary search
1773 // rather than linear search.
1774 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1775 while (I != Vars.end()) {
1776 unsigned CurNum = (*I)->getVariable().getArgNumber();
1777 // A local (non-parameter) variable has been found, insert immediately
1778 // before it.
1779 if (CurNum == 0)
1780 break;
1781 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001782 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001783 break;
David Blaikieb272a752013-06-06 22:28:26 +00001784 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001785 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001786 Vars.insert(I, Var);
1787 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001788 }
1789
1790 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001791}
1792
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001793// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001794void DwarfDebug::endFunction(const MachineFunction *MF) {
1795 if (!MMI->hasDebugInfo() || LScopes.empty())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001796 return;
Devang Patel2904aa92009-11-12 19:02:56 +00001797
Devang Patel7e623022011-08-10 20:55:27 +00001798 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001799 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001800 // Assumes in correct section after the entry point.
1801 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Manman Ren4e042a62013-02-05 21:52:47 +00001802 // Set DwarfCompileUnitID in MCContext to default value.
1803 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001804
Devang Patel7e623022011-08-10 20:55:27 +00001805 SmallPtrSet<const MDNode *, 16> ProcessedVars;
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001806 collectVariableInfo(MF, ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001807
Devang Patel3acc70e2011-08-15 22:04:40 +00001808 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001809 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001810 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001811
Devang Patel7e623022011-08-10 20:55:27 +00001812 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001813 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1814 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1815 LexicalScope *AScope = AList[i];
1816 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001817 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001818 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001819 DIArray Variables = SP.getVariables();
1820 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1821 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001822 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001823 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001824 // Check that DbgVariable for DV wasn't created earlier, when
1825 // findAbstractVariable() was called for inlined instance of DV.
1826 LLVMContext &Ctx = DV->getContext();
1827 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1828 if (AbstractVariables.lookup(CleanDV))
1829 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001830 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001831 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001832 }
1833 }
Devang Patel44403472011-08-12 18:10:19 +00001834 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001835 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001836 }
Eric Christopher6a841382012-11-19 22:42:10 +00001837
Devang Patel3acc70e2011-08-15 22:04:40 +00001838 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher6a841382012-11-19 22:42:10 +00001839
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001840 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001841 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001842
Bill Wendling2b128d72009-05-20 23:19:06 +00001843 // Clear debug info
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001844 for (ScopeVariablesMap::iterator I = ScopeVariables.begin(),
1845 E = ScopeVariables.end();
1846 I != E; ++I)
Devang Patel7e623022011-08-10 20:55:27 +00001847 DeleteContainerPointers(I->second);
1848 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001849 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001850 UserVariables.clear();
1851 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001852 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001853 LabelsBeforeInsn.clear();
1854 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001855 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001856}
1857
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001858// Register a source line with debug info. Returns the unique label that was
1859// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001860void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1861 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001862 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001863 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001864 unsigned Src = 1;
1865 if (S) {
1866 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001867
Dan Gohman50849c62010-05-05 23:41:32 +00001868 if (Scope.isCompileUnit()) {
1869 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001870 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001871 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001872 } else if (Scope.isFile()) {
1873 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001874 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001875 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001876 } else if (Scope.isSubprogram()) {
1877 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001878 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001879 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001880 } else if (Scope.isLexicalBlockFile()) {
1881 DILexicalBlockFile DBF(S);
1882 Fn = DBF.getFilename();
1883 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001884 } else if (Scope.isLexicalBlock()) {
1885 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001886 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001887 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001888 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001889 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001890
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001891 Src = getOrCreateSourceID(
1892 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001893 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001894 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001895}
1896
Bill Wendling806535f2009-05-20 23:22:40 +00001897//===----------------------------------------------------------------------===//
1898// Emit Methods
1899//===----------------------------------------------------------------------===//
1900
Manman Rence20d462013-10-29 22:57:10 +00001901// Compute the size and offset of a DIE. The offset is relative to start of the
1902// CU. It returns the offset after laying out the DIE.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001903unsigned DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001904 // Get the children.
1905 const std::vector<DIE *> &Children = Die->getChildren();
1906
Bill Wendling480ff322009-05-20 23:21:38 +00001907 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001908 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001909
1910 // Get the abbreviation for this DIE.
1911 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00001912 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00001913
1914 // Set DIE offset
1915 Die->setOffset(Offset);
1916
1917 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001918 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001919
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001920 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00001921 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001922
1923 // Size the DIE attribute values.
1924 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1925 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001926 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001927
1928 // Size the DIE children if any.
1929 if (!Children.empty()) {
1930 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1931 "Children flag not set");
1932
1933 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001934 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001935
1936 // End of children marker.
1937 Offset += sizeof(int8_t);
1938 }
1939
1940 Die->setSize(Offset - Die->getOffset());
1941 return Offset;
1942}
1943
Eric Christopherb088d2d2013-10-24 21:05:08 +00001944// Compute the size and offset for each DIE.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001945void DwarfUnits::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001946 // Offset from the first CU in the debug info section is 0 initially.
1947 unsigned SecOffset = 0;
1948
Eric Christopherb088d2d2013-10-24 21:05:08 +00001949 // Iterate over each compile unit and set the size and offsets for each
1950 // DIE within each compile unit. All offsets are CU relative.
David Blaikie319a05f2013-12-02 19:33:10 +00001951 for (SmallVectorImpl<Unit *>::const_iterator I = CUs.begin(), E = CUs.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001952 I != E; ++I) {
Manman Rence20d462013-10-29 22:57:10 +00001953 (*I)->setDebugInfoOffset(SecOffset);
1954
1955 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001956 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
David Blaikie6b288cf2013-10-30 20:42:41 +00001957 (*I)->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001958
1959 // EndOffset here is CU-relative, after laying out
1960 // all of the CU DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001961 unsigned EndOffset = computeSizeAndOffset((*I)->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001962 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001963 }
Bill Wendling480ff322009-05-20 23:21:38 +00001964}
1965
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001966// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001967void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001968 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001969
Bill Wendling480ff322009-05-20 23:21:38 +00001970 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001971 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001972 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001973 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001974 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001975 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001976 DwarfAbbrevDWOSectionSym = emitSectionSym(
1977 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001978 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001979
Chris Lattner6629ca92010-04-04 22:59:04 +00001980 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001981 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001982
Eric Christopher74804332013-02-07 21:19:50 +00001983 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001984 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001985 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001986 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001987 DwarfGnuPubNamesSectionSym =
1988 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1989 DwarfGnuPubTypesSectionSym =
1990 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001991 } else if (HasDwarfPubSections) {
1992 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1993 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001994 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001995
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001996 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001997 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001998 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001999 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002000 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00002001 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002002 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00002003 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002004 DwarfDebugRangeSectionSym =
2005 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00002006
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002007 DwarfDebugLocSectionSym =
2008 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Devang Patel9fc11702010-05-25 23:40:22 +00002009
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002010 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
2011 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002012}
2013
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002014// Recursively emits a debug information entry.
David Blaikie2d4e1122013-10-30 17:14:24 +00002015void DwarfDebug::emitDIE(DIE *Die, ArrayRef<DIEAbbrev *> Abbrevs) {
Bill Wendling480ff322009-05-20 23:21:38 +00002016 // Get the abbreviation for this DIE.
2017 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00002018 const DIEAbbrev *Abbrev = Abbrevs[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00002019
Bill Wendling480ff322009-05-20 23:21:38 +00002020 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00002021 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00002022 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2023 Twine::utohexstr(Die->getOffset()) + ":0x" +
2024 Twine::utohexstr(Die->getSize()) + " " +
2025 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00002026 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002027
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002028 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00002029 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00002030
2031 // Emit the DIE attribute values.
2032 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00002033 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
2034 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00002035 assert(Form && "Too many attributes for DIE (check abbreviation)");
2036
Chris Lattner7bde8c02010-04-04 18:52:31 +00002037 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00002038 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002039
Bill Wendling480ff322009-05-20 23:21:38 +00002040 switch (Attr) {
Manman Rence20d462013-10-29 22:57:10 +00002041 case dwarf::DW_AT_abstract_origin:
2042 case dwarf::DW_AT_type:
2043 case dwarf::DW_AT_friend:
2044 case dwarf::DW_AT_specification:
2045 case dwarf::DW_AT_import:
2046 case dwarf::DW_AT_containing_type: {
2047 DIEEntry *E = cast<DIEEntry>(Values[i]);
2048 DIE *Origin = E->getEntry();
2049 unsigned Addr = Origin->getOffset();
2050 if (Form == dwarf::DW_FORM_ref_addr) {
2051 assert(!useSplitDwarf() && "TODO: dwo files can't have relocations.");
2052 // For DW_FORM_ref_addr, output the offset from beginning of debug info
2053 // section. Origin->getOffset() returns the offset from start of the
2054 // compile unit.
David Blaikie409dd9c2013-11-19 23:08:21 +00002055 CompileUnit *CU = CUDieMap.lookup(Origin->getUnit());
Manman Rence20d462013-10-29 22:57:10 +00002056 assert(CU && "CUDie should belong to a CU.");
2057 Addr += CU->getDebugInfoOffset();
2058 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2059 Asm->EmitLabelPlusOffset(DwarfInfoSectionSym, Addr,
2060 DIEEntry::getRefAddrSize(Asm));
2061 else
Manman Ren251a1bd2013-10-29 23:14:15 +00002062 Asm->EmitLabelOffsetDifference(DwarfInfoSectionSym, Addr,
2063 DwarfInfoSectionSym,
2064 DIEEntry::getRefAddrSize(Asm));
Manman Rence20d462013-10-29 22:57:10 +00002065 } else {
Manman Ren4dbdc902013-10-31 17:54:35 +00002066 // Make sure Origin belong to the same CU.
David Blaikie409dd9c2013-11-19 23:08:21 +00002067 assert(Die->getUnit() == Origin->getUnit() &&
Manman Ren4dbdc902013-10-31 17:54:35 +00002068 "The referenced DIE should belong to the same CU in ref4");
Manman Rence20d462013-10-29 22:57:10 +00002069 Asm->EmitInt32(Addr);
2070 }
2071 break;
2072 }
Devang Patel12563b32010-04-16 23:33:45 +00002073 case dwarf::DW_AT_ranges: {
2074 // DW_AT_range Value encodes offset in debug_range section.
Eric Christopher4751d702013-11-23 00:05:29 +00002075 DIELabel *V = cast<DIELabel>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00002076
Eric Christopher4751d702013-11-23 00:05:29 +00002077 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2078 Asm->EmitSectionOffset(V->getValue(), DwarfDebugRangeSectionSym);
2079 else
2080 Asm->EmitLabelDifference(V->getValue(), DwarfDebugRangeSectionSym, 4);
Devang Patel12563b32010-04-16 23:33:45 +00002081 break;
2082 }
Devang Patel9fc11702010-05-25 23:40:22 +00002083 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00002084 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
2085 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherd0b82ae2013-11-16 00:18:40 +00002086 Asm->EmitSectionOffset(L->getValue(), DwarfDebugLocSectionSym);
Nick Lewycky33da3362012-06-22 01:25:12 +00002087 else
Ulrich Weigand396ba8b2013-07-02 18:46:26 +00002088 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Nick Lewycky33da3362012-06-22 01:25:12 +00002089 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00002090 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00002091 }
Devang Patel9fc11702010-05-25 23:40:22 +00002092 break;
2093 }
Devang Patela1bd5a12010-09-29 19:08:08 +00002094 case dwarf::DW_AT_accessibility: {
2095 if (Asm->isVerbose()) {
2096 DIEInteger *V = cast<DIEInteger>(Values[i]);
2097 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2098 }
2099 Values[i]->EmitValue(Asm, Form);
2100 break;
2101 }
Bill Wendling480ff322009-05-20 23:21:38 +00002102 default:
2103 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002104 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002105 break;
2106 }
Bill Wendling480ff322009-05-20 23:21:38 +00002107 }
2108
2109 // Emit the DIE children if any.
2110 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2111 const std::vector<DIE *> &Children = Die->getChildren();
2112
2113 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher3c5a1912012-12-19 22:02:53 +00002114 emitDIE(Children[j], Abbrevs);
Bill Wendling480ff322009-05-20 23:21:38 +00002115
Chris Lattner7bde8c02010-04-04 18:52:31 +00002116 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00002117 Asm->OutStreamer.AddComment("End Of Children Mark");
2118 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002119 }
2120}
2121
Eric Christophera2de8262012-12-15 00:04:07 +00002122// Emit the various dwarf units to the unit section USection with
2123// the abbreviations going into ASection.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002124void DwarfUnits::emitUnits(DwarfDebug *DD, const MCSection *USection,
Eric Christophera2de8262012-12-15 00:04:07 +00002125 const MCSection *ASection,
2126 const MCSymbol *ASectionSym) {
2127 Asm->OutStreamer.SwitchSection(USection);
David Blaikie319a05f2013-12-02 19:33:10 +00002128 for (SmallVectorImpl<Unit *>::iterator I = CUs.begin(), E = CUs.end(); I != E;
2129 ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002130 Unit *TheU = *I;
2131 DIE *Die = TheU->getUnitDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002132
Devang Patel1a0df9a2010-05-10 22:49:55 +00002133 // Emit the compile units header.
David Blaikie2a80e442013-12-02 22:09:48 +00002134 Asm->OutStreamer.EmitLabel(
2135 Asm->GetTempSymbol(USection->getLabelBeginName(), TheU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002136
Devang Patel1a0df9a2010-05-10 22:49:55 +00002137 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00002138 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00002139 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002140
David Blaikie2a80e442013-12-02 22:09:48 +00002141 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002142
Eric Christopher3c5a1912012-12-19 22:02:53 +00002143 DD->emitDIE(Die, Abbreviations);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002144 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002145 Asm->GetTempSymbol(USection->getLabelEndName(), TheU->getUniqueID()));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002146 }
Bill Wendling480ff322009-05-20 23:21:38 +00002147}
2148
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002149// Emit the debug info section.
2150void DwarfDebug::emitDebugInfo() {
Eric Christophera2de8262012-12-15 00:04:07 +00002151 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2152
2153 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
2154 Asm->getObjFileLowering().getDwarfAbbrevSection(),
2155 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002156}
2157
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002158// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002159void DwarfDebug::emitAbbreviations() {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002160 if (!useSplitDwarf())
2161 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
2162 &Abbreviations);
2163 else
2164 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2165}
Bill Wendling480ff322009-05-20 23:21:38 +00002166
Eric Christopher3c5a1912012-12-19 22:02:53 +00002167void DwarfDebug::emitAbbrevs(const MCSection *Section,
2168 std::vector<DIEAbbrev *> *Abbrevs) {
2169 // Check to see if it is worth the effort.
2170 if (!Abbrevs->empty()) {
2171 // Start the debug abbrev section.
2172 Asm->OutStreamer.SwitchSection(Section);
2173
2174 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002175 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling480ff322009-05-20 23:21:38 +00002176
2177 // For each abbrevation.
Eric Christopher3c5a1912012-12-19 22:02:53 +00002178 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00002179 // Get abbreviation data
Eric Christopher3c5a1912012-12-19 22:02:53 +00002180 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling480ff322009-05-20 23:21:38 +00002181
2182 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002183 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002184
2185 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002186 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002187 }
2188
2189 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002190 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002191
Eric Christopher3c5a1912012-12-19 22:02:53 +00002192 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002193 Asm->OutStreamer.EmitLabel(End);
Bill Wendling480ff322009-05-20 23:21:38 +00002194 }
2195}
2196
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002197// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002198void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002199 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002200 Asm->OutStreamer.AddComment("Extended Op");
2201 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002202
Chris Lattner566cae92010-03-09 23:52:58 +00002203 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002204 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002205 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2206 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2207
2208 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002209
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002210 Asm->OutStreamer.EmitSymbolValue(
2211 Asm->GetTempSymbol("section_end", SectionEnd),
2212 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002213
2214 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002215 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2216 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002217 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002218 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002219}
2220
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002221// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002222void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002223 DwarfAccelTable AT(
2224 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002225 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2226 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002227 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002228 Unit *TheU = *I;
2229 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelNames();
David Blaikie2ea848b2013-11-19 22:51:04 +00002230 for (StringMap<std::vector<const DIE *> >::const_iterator
2231 GI = Names.begin(),
2232 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002233 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002234 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002235 const std::vector<const DIE *> &Entities = GI->second;
2236 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2237 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002238 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002239 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002240 }
2241 }
2242
2243 AT.FinalizeTable(Asm, "Names");
2244 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002245 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002246 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2247 Asm->OutStreamer.EmitLabel(SectionBegin);
2248
2249 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002250 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002251}
2252
Eric Christopher48fef592012-12-20 21:58:40 +00002253// Emit objective C classes and categories into a hashed accelerator table
2254// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002255void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002256 DwarfAccelTable AT(
2257 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002258 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2259 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002260 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002261 Unit *TheU = *I;
2262 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelObjC();
David Blaikie2ea848b2013-11-19 22:51:04 +00002263 for (StringMap<std::vector<const DIE *> >::const_iterator
2264 GI = Names.begin(),
2265 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002266 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002267 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002268 const std::vector<const DIE *> &Entities = GI->second;
2269 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2270 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002271 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002272 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002273 }
2274 }
2275
2276 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002277 Asm->OutStreamer.SwitchSection(
2278 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002279 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2280 Asm->OutStreamer.EmitLabel(SectionBegin);
2281
2282 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002283 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002284}
2285
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002286// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002287void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002288 DwarfAccelTable AT(
2289 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002290 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2291 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002292 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002293 Unit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002294 const StringMap<std::vector<const DIE *> > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002295 TheU->getAccelNamespace();
David Blaikie2ea848b2013-11-19 22:51:04 +00002296 for (StringMap<std::vector<const DIE *> >::const_iterator
2297 GI = Names.begin(),
2298 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002299 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002300 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002301 const std::vector<const DIE *> &Entities = GI->second;
2302 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2303 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002304 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002305 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002306 }
2307 }
2308
2309 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002310 Asm->OutStreamer.SwitchSection(
2311 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002312 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2313 Asm->OutStreamer.EmitLabel(SectionBegin);
2314
2315 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002316 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002317}
2318
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002319// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002320void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002321 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002322 Atoms.push_back(
2323 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2324 Atoms.push_back(
2325 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2326 Atoms.push_back(
2327 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002328 DwarfAccelTable AT(Atoms);
David Blaikie319a05f2013-12-02 19:33:10 +00002329 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2330 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002331 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002332 Unit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002333 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002334 TheU->getAccelTypes();
David Blaikie2ea848b2013-11-19 22:51:04 +00002335 for (StringMap<
2336 std::vector<std::pair<const DIE *, unsigned> > >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002337 GI = Names.begin(),
2338 GE = Names.end();
2339 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002340 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002341 const std::vector<std::pair<const DIE *, unsigned> > &Entities =
2342 GI->second;
2343 for (std::vector<std::pair<const DIE *, unsigned> >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002344 DI = Entities.begin(),
2345 DE = Entities.end();
2346 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002347 AT.AddName(Name, DI->first, DI->second);
Eric Christopher4996c702011-11-07 09:24:32 +00002348 }
2349 }
2350
2351 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002352 Asm->OutStreamer.SwitchSection(
2353 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002354 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2355 Asm->OutStreamer.EmitLabel(SectionBegin);
2356
2357 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002358 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002359}
2360
Eric Christopherdd1a0122013-09-13 00:35:05 +00002361// Public name handling.
2362// The format for the various pubnames:
2363//
2364// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2365// for the DIE that is named.
2366//
2367// gnu pubnames - offset/index value/name tuples where the offset is the offset
2368// into the CU and the index value is computed according to the type of value
2369// for the DIE that is named.
2370//
2371// For type units the offset is the offset of the skeleton DIE. For split dwarf
2372// it's the offset within the debug_info/debug_types dwo section, however, the
2373// reference in the pubname header doesn't change.
2374
2375/// computeIndexValue - Compute the gdb index value for the DIE and CU.
David Blaikie319a05f2013-12-02 19:33:10 +00002376static dwarf::PubIndexEntryDescriptor computeIndexValue(Unit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002377 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002378 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2379
2380 // We could have a specification DIE that has our most of our knowledge,
2381 // look for that now.
2382 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2383 if (SpecVal) {
2384 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2385 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2386 Linkage = dwarf::GIEL_EXTERNAL;
2387 } else if (Die->findAttribute(dwarf::DW_AT_external))
2388 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002389
2390 switch (Die->getTag()) {
2391 case dwarf::DW_TAG_class_type:
2392 case dwarf::DW_TAG_structure_type:
2393 case dwarf::DW_TAG_union_type:
2394 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002395 return dwarf::PubIndexEntryDescriptor(
2396 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2397 ? dwarf::GIEL_STATIC
2398 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002399 case dwarf::DW_TAG_typedef:
2400 case dwarf::DW_TAG_base_type:
2401 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002402 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002403 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002404 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002405 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002406 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002407 case dwarf::DW_TAG_constant:
2408 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002409 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002410 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002411 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2412 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002413 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002414 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002415 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002416}
2417
Eric Christopher5f93bb92013-09-09 20:03:17 +00002418/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002419///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002420void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002421 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopherdd1a0122013-09-13 00:35:05 +00002422 const MCSection *PSec =
2423 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2424 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002425
David Blaikie319a05f2013-12-02 19:33:10 +00002426 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2427 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00002428 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002429 Unit *TheU = *I;
2430 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002431
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002432 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002433 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002434
Eric Christopher39eebfa2013-09-30 23:14:16 +00002435 // Emit a label so we can reference the beginning of this pubname section.
2436 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002437 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002438 Asm->GetTempSymbol("gnu_pubnames", TheU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002439
Eric Christopherdd1a0122013-09-13 00:35:05 +00002440 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002441 Asm->OutStreamer.AddComment("Length of Public Names Info");
2442 Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
2443 Asm->GetTempSymbol("pubnames_begin", ID), 4);
2444
2445 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
2446
2447 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002448 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002449
2450 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2451 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2452 DwarfInfoSectionSym);
2453
2454 Asm->OutStreamer.AddComment("Compilation Unit Length");
2455 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
2456 Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2457 4);
2458
Eric Christopherdd1a0122013-09-13 00:35:05 +00002459 // Emit the pubnames for this compilation unit.
David Blaikie2a80e442013-12-02 22:09:48 +00002460 const StringMap<const DIE *> &Globals = TheU->getGlobalNames();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002461 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2462 GE = Globals.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002463 GI != GE; ++GI) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002464 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002465 const DIE *Entity = GI->second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002466
2467 Asm->OutStreamer.AddComment("DIE offset");
2468 Asm->EmitInt32(Entity->getOffset());
2469
Eric Christopherdd1a0122013-09-13 00:35:05 +00002470 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002471 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002472 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002473 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002474 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002475 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002476 }
2477
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002478 if (Asm->isVerbose())
2479 Asm->OutStreamer.AddComment("External Name");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002480 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002481 }
2482
2483 Asm->OutStreamer.AddComment("End Mark");
2484 Asm->EmitInt32(0);
2485 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
2486 }
2487}
2488
Eric Christopherdd1a0122013-09-13 00:35:05 +00002489void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher8b3737f2013-09-13 00:34:58 +00002490 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopher261d2342013-09-23 20:55:35 +00002491 const MCSection *PSec =
2492 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2493 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002494
David Blaikie319a05f2013-12-02 19:33:10 +00002495 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2496 E = getUnits().end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002497 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002498 Unit *TheU = *I;
Eric Christopher6abc9c52011-11-07 09:18:35 +00002499 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002500 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002501
2502 // Emit a label so we can reference the beginning of this pubtype section.
2503 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002504 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002505 Asm->GetTempSymbol("gnu_pubtypes", TheU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002506
2507 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002508 Asm->OutStreamer.AddComment("Length of Public Types Info");
2509 Asm->EmitLabelDifference(
David Blaikie2a80e442013-12-02 22:09:48 +00002510 Asm->GetTempSymbol("pubtypes_end", TheU->getUniqueID()),
2511 Asm->GetTempSymbol("pubtypes_begin", TheU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002512
Eric Christopher8b3737f2013-09-13 00:34:58 +00002513 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002514 Asm->GetTempSymbol("pubtypes_begin", TheU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002515
Eric Christopher8b3737f2013-09-13 00:34:58 +00002516 if (Asm->isVerbose())
2517 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002518 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002519
Devang Patel1a0df9a2010-05-10 22:49:55 +00002520 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002521 Asm->EmitSectionOffset(
David Blaikie2a80e442013-12-02 22:09:48 +00002522 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheU->getUniqueID()),
Eric Christopher8b3737f2013-09-13 00:34:58 +00002523 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002524
Devang Patel1a0df9a2010-05-10 22:49:55 +00002525 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002526 Asm->EmitLabelDifference(
David Blaikie2a80e442013-12-02 22:09:48 +00002527 Asm->GetTempSymbol(ISec->getLabelEndName(), TheU->getUniqueID()),
2528 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002529
Eric Christopher39eebfa2013-09-30 23:14:16 +00002530 // Emit the pubtypes.
David Blaikie2a80e442013-12-02 22:09:48 +00002531 const StringMap<const DIE *> &Globals = TheU->getGlobalTypes();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002532 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2533 GE = Globals.end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002534 GI != GE; ++GI) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002535 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002536 const DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002537
Eric Christopher8b3737f2013-09-13 00:34:58 +00002538 if (Asm->isVerbose())
2539 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002540 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002541
Eric Christopherdd1a0122013-09-13 00:35:05 +00002542 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002543 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002544 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002545 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002546 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002547 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002548 }
2549
Eric Christopher8b3737f2013-09-13 00:34:58 +00002550 if (Asm->isVerbose())
2551 Asm->OutStreamer.AddComment("External Name");
2552
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002553 // Emit the name with a terminating null byte.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002554 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002555 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002556
Devang Patel1a0df9a2010-05-10 22:49:55 +00002557 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002558 Asm->EmitInt32(0);
Eric Christopher8b3737f2013-09-13 00:34:58 +00002559 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002560 Asm->GetTempSymbol("pubtypes_end", TheU->getUniqueID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002561 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002562}
2563
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002564// Emit strings into a string section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002565void DwarfUnits::emitStrings(const MCSection *StrSection,
2566 const MCSection *OffsetSection = NULL,
2567 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002568
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002569 if (StringPool.empty())
2570 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002571
Chris Lattner3d72a672010-03-09 23:38:23 +00002572 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002573 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002574
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002575 // Get all of the string pool entries and put them in an array by their ID so
2576 // we can sort them.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002577 SmallVector<
2578 std::pair<unsigned, StringMapEntry<std::pair<MCSymbol *, unsigned> > *>,
2579 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002580
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002581 for (StringMap<std::pair<MCSymbol *, unsigned> >::iterator
2582 I = StringPool.begin(),
2583 E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002584 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002585 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002586
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002587 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002588
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002589 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002590 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002591 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002592
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002593 // Emit the string itself with a terminating null byte.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002594 Asm->OutStreamer.EmitBytes(
2595 StringRef(Entries[i].second->getKeyData(),
2596 Entries[i].second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002597 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002598
2599 // If we've got an offset section go ahead and emit that now as well.
2600 if (OffsetSection) {
2601 Asm->OutStreamer.SwitchSection(OffsetSection);
2602 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002603 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002604 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002605 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002606 offset += Entries[i].second->getKeyLength() + 1;
2607 }
2608 }
Bill Wendling480ff322009-05-20 23:21:38 +00002609}
2610
Eric Christopher65132a82013-11-19 09:11:26 +00002611
2612// Emit addresses into the section given.
Eric Christopher962c9082013-01-15 23:56:56 +00002613void DwarfUnits::emitAddresses(const MCSection *AddrSection) {
2614
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002615 if (AddressPool.empty())
2616 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002617
2618 // Start the dwarf addr section.
2619 Asm->OutStreamer.SwitchSection(AddrSection);
2620
David Blaikiece1960f2013-07-08 17:51:28 +00002621 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002622 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002623
David Blaikiece1960f2013-07-08 17:51:28 +00002624 for (DenseMap<const MCExpr *, unsigned>::iterator I = AddressPool.begin(),
2625 E = AddressPool.end();
Eric Christopher962c9082013-01-15 23:56:56 +00002626 I != E; ++I)
David Blaikieac569a62013-07-08 17:33:10 +00002627 Entries[I->second] = I->first;
Eric Christopher962c9082013-01-15 23:56:56 +00002628
2629 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002630 // Emit an expression for reference from debug information entries.
David Blaikieac569a62013-07-08 17:33:10 +00002631 if (const MCExpr *Expr = Entries[i])
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002632 Asm->OutStreamer.EmitValue(Expr, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002633 else
2634 Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
2635 }
Eric Christopher962c9082013-01-15 23:56:56 +00002636}
2637
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002638// Emit visible names into a debug str section.
2639void DwarfDebug::emitDebugStr() {
2640 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2641 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2642}
2643
Eric Christopher9046f942013-07-02 21:36:07 +00002644// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002645void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002646 if (DotDebugLocEntries.empty())
2647 return;
2648
Eric Christopher4887c8f2013-03-29 23:34:06 +00002649 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002650 I = DotDebugLocEntries.begin(),
2651 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002652 I != E; ++I) {
2653 DotDebugLocEntry &Entry = *I;
2654 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002655 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002656 }
2657
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002658 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002659 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002660 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002661 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002662 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2663 unsigned index = 1;
Eric Christopher4887c8f2013-03-29 23:34:06 +00002664 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002665 I = DotDebugLocEntries.begin(),
2666 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002667 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002668 DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002669 if (Entry.isMerged())
2670 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002671 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002672 Asm->OutStreamer.EmitIntValue(0, Size);
2673 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002674 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002675 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002676 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2677 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2678 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002679 Asm->OutStreamer.AddComment("Loc expr size");
2680 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2681 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2682 Asm->EmitLabelDifference(end, begin, 2);
2683 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002684 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002685 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002686 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2687 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002688 Asm->OutStreamer.AddComment("DW_OP_consts");
2689 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002690 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002691 } else {
2692 Asm->OutStreamer.AddComment("DW_OP_constu");
2693 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002694 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002695 }
Devang Pateled9fd452011-07-08 16:49:43 +00002696 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002697 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002698 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002699 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002700 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002701 else {
2702 // Complex address entry.
2703 unsigned N = DV.getNumAddrElements();
2704 unsigned i = 0;
2705 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002706 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002707 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002708 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002709 Asm->OutStreamer.AddComment("DW_OP_deref");
2710 Asm->EmitInt8(dwarf::DW_OP_deref);
2711 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2712 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2713 Asm->EmitSLEB128(DV.getAddrElement(1));
2714 } else {
2715 // If first address element is OpPlus then emit
2716 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002717 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2718 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002719 i = 2;
2720 }
2721 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002722 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002723 }
Eric Christopher6a841382012-11-19 22:42:10 +00002724
Devang Pateled9fd452011-07-08 16:49:43 +00002725 // Emit remaining complex address elements.
2726 for (; i < N; ++i) {
2727 uint64_t Element = DV.getAddrElement(i);
2728 if (Element == DIBuilder::OpPlus) {
2729 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2730 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002731 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002732 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002733 Asm->EmitInt8(dwarf::DW_OP_deref);
2734 } else
2735 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002736 }
Devang Patel3e021532011-04-28 02:22:40 +00002737 }
Devang Patel3e021532011-04-28 02:22:40 +00002738 }
Devang Pateled9fd452011-07-08 16:49:43 +00002739 // else ... ignore constant fp. There is not any good way to
2740 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002741 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002742 }
2743 }
Bill Wendling480ff322009-05-20 23:21:38 +00002744}
2745
Richard Mitton21101b32013-09-19 23:21:01 +00002746struct SymbolCUSorter {
2747 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2748 const MCStreamer &Streamer;
2749
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002750 bool operator()(const SymbolCU &A, const SymbolCU &B) {
Richard Mitton21101b32013-09-19 23:21:01 +00002751 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2752 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2753
2754 // Symbols with no order assigned should be placed at the end.
2755 // (e.g. section end labels)
2756 if (IA == 0)
2757 IA = (unsigned)(-1);
2758 if (IB == 0)
2759 IB = (unsigned)(-1);
2760 return IA < IB;
2761 }
2762};
2763
David Blaikie319a05f2013-12-02 19:33:10 +00002764static bool CUSort(const Unit *A, const Unit *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002765 return (A->getUniqueID() < B->getUniqueID());
Richard Mitton21101b32013-09-19 23:21:01 +00002766}
2767
2768struct ArangeSpan {
2769 const MCSymbol *Start, *End;
2770};
2771
2772// Emit a debug aranges section, containing a CU lookup for any
2773// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002774void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002775 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002776 Asm->OutStreamer.SwitchSection(
2777 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002778
2779 typedef DenseMap<CompileUnit *, std::vector<ArangeSpan> > SpansType;
2780
2781 SpansType Spans;
2782
2783 // Build a list of sections used.
2784 std::vector<const MCSection *> Sections;
2785 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
2786 it++) {
2787 const MCSection *Section = it->first;
2788 Sections.push_back(Section);
2789 }
2790
2791 // Sort the sections into order.
2792 // This is only done to ensure consistent output order across different runs.
2793 std::sort(Sections.begin(), Sections.end(), SectionSort);
2794
2795 // Build a set of address spans, sorted by CU.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002796 for (size_t SecIdx = 0; SecIdx < Sections.size(); SecIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002797 const MCSection *Section = Sections[SecIdx];
2798 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2799 if (List.size() < 2)
2800 continue;
2801
2802 // Sort the symbols by offset within the section.
2803 SymbolCUSorter sorter(Asm->OutStreamer);
2804 std::sort(List.begin(), List.end(), sorter);
2805
2806 // If we have no section (e.g. common), just write out
2807 // individual spans for each symbol.
2808 if (Section == NULL) {
2809 for (size_t n = 0; n < List.size(); n++) {
2810 const SymbolCU &Cur = List[n];
2811
2812 ArangeSpan Span;
2813 Span.Start = Cur.Sym;
2814 Span.End = NULL;
2815 if (Cur.CU)
2816 Spans[Cur.CU].push_back(Span);
2817 }
2818 } else {
2819 // Build spans between each label.
2820 const MCSymbol *StartSym = List[0].Sym;
2821 for (size_t n = 1; n < List.size(); n++) {
2822 const SymbolCU &Prev = List[n - 1];
2823 const SymbolCU &Cur = List[n];
2824
2825 // Try and build the longest span we can within the same CU.
2826 if (Cur.CU != Prev.CU) {
2827 ArangeSpan Span;
2828 Span.Start = StartSym;
2829 Span.End = Cur.Sym;
2830 Spans[Prev.CU].push_back(Span);
2831 StartSym = Cur.Sym;
2832 }
2833 }
2834 }
2835 }
2836
2837 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2838 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2839
2840 // Build a list of CUs used.
2841 std::vector<CompileUnit *> CUs;
2842 for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
2843 CompileUnit *CU = it->first;
2844 CUs.push_back(CU);
2845 }
2846
2847 // Sort the CU list (again, to ensure consistent output order).
2848 std::sort(CUs.begin(), CUs.end(), CUSort);
2849
2850 // Emit an arange table for each CU we used.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002851 for (size_t CUIdx = 0; CUIdx < CUs.size(); CUIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002852 CompileUnit *CU = CUs[CUIdx];
2853 std::vector<ArangeSpan> &List = Spans[CU];
2854
2855 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002856 unsigned ContentSize =
2857 sizeof(int16_t) + // DWARF ARange version number
2858 sizeof(int32_t) + // Offset of CU in the .debug_info section
2859 sizeof(int8_t) + // Pointer Size (in bytes)
2860 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002861
2862 unsigned TupleSize = PtrSize * 2;
2863
2864 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
2865 unsigned Padding = 0;
2866 while (((sizeof(int32_t) + ContentSize + Padding) % TupleSize) != 0)
2867 Padding++;
2868
2869 ContentSize += Padding;
2870 ContentSize += (List.size() + 1) * TupleSize;
2871
2872 // For each compile unit, write the list of spans it covers.
2873 Asm->OutStreamer.AddComment("Length of ARange Set");
2874 Asm->EmitInt32(ContentSize);
2875 Asm->OutStreamer.AddComment("DWARF Arange version number");
2876 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2877 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
2878 Asm->EmitSectionOffset(
2879 Asm->GetTempSymbol(ISec->getLabelBeginName(), CU->getUniqueID()),
2880 DwarfInfoSectionSym);
2881 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2882 Asm->EmitInt8(PtrSize);
2883 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2884 Asm->EmitInt8(0);
2885
2886 for (unsigned n = 0; n < Padding; n++)
2887 Asm->EmitInt8(0xff);
2888
2889 for (unsigned n = 0; n < List.size(); n++) {
2890 const ArangeSpan &Span = List[n];
2891 Asm->EmitLabelReference(Span.Start, PtrSize);
2892
2893 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002894 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002895 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002896 } else {
2897 // For symbols without an end marker (e.g. common), we
2898 // write a single arange entry containing just that one symbol.
2899 uint64_t Size = SymSize[Span.Start];
2900 if (Size == 0)
2901 Size = 1;
2902
2903 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2904 }
Richard Mitton21101b32013-09-19 23:21:01 +00002905 }
2906
2907 Asm->OutStreamer.AddComment("ARange terminator");
2908 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2909 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2910 }
Bill Wendling480ff322009-05-20 23:21:38 +00002911}
2912
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002913// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002914void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002915 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002916 Asm->OutStreamer.SwitchSection(
2917 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002918
Eric Christopher0f63d062013-12-03 00:45:45 +00002919 // Size for our labels.
2920 unsigned char Size = Asm->getDataLayout().getPointerSize();
2921
2922 // Grab the specific ranges for the compile units in the module.
2923 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2924 E = CUMap.end();
2925 I != E; ++I) {
2926 CompileUnit *TheCU = I->second;
2927 unsigned ID = TheCU->getUniqueID();
2928
2929 // Emit a symbol so we can find the beginning of our ranges.
2930 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_ranges", ID));
2931
2932 // Iterate over the misc ranges for the compile units in the module.
2933 const SmallVectorImpl<RangeSpanList *> &RangeLists = TheCU->getRangeLists();
2934 for (SmallVectorImpl<RangeSpanList *>::const_iterator
2935 I = RangeLists.begin(),
2936 E = RangeLists.end();
2937 I != E; ++I) {
2938 RangeSpanList *List = *I;
2939
2940 // Emit a symbol so we can find the beginning of the range.
2941 Asm->OutStreamer.EmitLabel(
2942 Asm->GetTempSymbol("debug_ranges", List->getIndex()));
2943
2944 for (SmallVectorImpl<RangeSpan>::const_iterator
2945 I = List->getRanges().begin(),
2946 E = List->getRanges().end();
2947 I != E; ++I) {
2948 RangeSpan Range = *I;
2949 // We occasionally have ranges without begin/end labels.
2950 // FIXME: Verify and fix.
2951 const MCSymbol *Begin = Range.getStart();
2952 const MCSymbol *End = Range.getEnd();
2953 Begin ? Asm->OutStreamer.EmitSymbolValue(Begin, Size)
2954 : Asm->OutStreamer.EmitIntValue(0, Size);
2955 End ? Asm->OutStreamer.EmitSymbolValue(End, Size)
2956 : Asm->OutStreamer.EmitIntValue(0, Size);
2957 }
2958
2959 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002960 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002961 Asm->OutStreamer.EmitIntValue(0, Size);
2962 }
Devang Patel12563b32010-04-16 23:33:45 +00002963 }
Bill Wendling480ff322009-05-20 23:21:38 +00002964}
2965
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002966// Emit visible names into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002967void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002968 if (const MCSection *LineInfo =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002969 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002970 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002971 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002972 }
2973}
2974
Eric Christopherd692c1d2012-12-11 19:42:09 +00002975// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002976
2977// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2978// 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 +00002979// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher6fdf3242013-08-26 23:50:43 +00002980CompileUnit *DwarfDebug::constructSkeletonCU(const CompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002981
2982 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikieb01f13e2013-11-15 23:54:45 +00002983 CompileUnit *NewCU = new CompileUnit(CU->getUniqueID(), Die, CU->getNode(),
2984 Asm, this, &SkeletonHolder);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002985
Eric Christopherdae389b2013-02-22 23:50:08 +00002986 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikieb01f13e2013-11-15 23:54:45 +00002987 CU->getNode().getSplitDebugFilename());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002988
Eric Christopher44c6aa62013-04-22 07:51:08 +00002989 // Relocate to the beginning of the addr_base section, else 0 for the
2990 // beginning of the one for this compile unit.
Eric Christopher55863be2013-04-07 03:43:09 +00002991 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00002992 NewCU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base,
2993 DwarfAddrSectionSym);
Eric Christopher55863be2013-04-07 03:43:09 +00002994 else
Eric Christopher33ff6972013-11-21 23:46:41 +00002995 NewCU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002996
2997 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher962c9082013-01-15 23:56:56 +00002998 // into an entity. We're using 0, or a NULL label for this.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002999 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Eric Christopher962c9082013-01-15 23:56:56 +00003000
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003001 // DW_AT_stmt_list is a offset of line number information for this
3002 // compile unit in debug_line section.
Eric Christopher55863be2013-04-07 03:43:09 +00003003 // FIXME: Should handle multiple compile units.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003004 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003005 NewCU->addSectionLabel(Die, dwarf::DW_AT_stmt_list,
3006 DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003007 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003008 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003009
3010 if (!CompilationDir.empty())
Eric Christopher2cbd5762013-01-07 19:32:41 +00003011 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003012
Eric Christopher1d06eb52013-10-01 00:43:31 +00003013 // Flags to let the linker know we have emitted new style pubnames.
Eric Christopher39eebfa2013-09-30 23:14:16 +00003014 if (GenerateGnuPubSections) {
3015 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003016 NewCU->addSectionLabel(
3017 Die, dwarf::DW_AT_GNU_pubnames,
3018 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00003019 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003020 NewCU->addSectionDelta(
3021 Die, dwarf::DW_AT_GNU_pubnames,
3022 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
3023 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00003024
3025 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003026 NewCU->addSectionLabel(
3027 Die, dwarf::DW_AT_GNU_pubtypes,
3028 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00003029 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003030 NewCU->addSectionDelta(
3031 Die, dwarf::DW_AT_GNU_pubtypes,
3032 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
3033 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00003034 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00003035
Eric Christopher0f63d062013-12-03 00:45:45 +00003036 // Attribute if we've emitted any ranges and their location for the compile unit.
3037 if (CU->getRangeLists().size()) {
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003038 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher0f63d062013-12-03 00:45:45 +00003039 NewCU->addSectionLabel(
3040 Die, dwarf::DW_AT_GNU_ranges_base,
3041 Asm->GetTempSymbol("gnu_ranges", NewCU->getUniqueID()));
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003042 else
Eric Christopher0f63d062013-12-03 00:45:45 +00003043 NewCU->addSectionDelta(
3044 Die, dwarf::DW_AT_GNU_ranges_base,
3045 Asm->GetTempSymbol("gnu_ranges", NewCU->getUniqueID()),
3046 DwarfDebugRangeSectionSym);
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003047 }
3048
Eric Christopherc8a310e2012-12-10 23:34:43 +00003049 SkeletonHolder.addUnit(NewCU);
3050
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003051 return NewCU;
3052}
3053
Eric Christopher3c5a1912012-12-19 22:02:53 +00003054void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
3055 assert(useSplitDwarf() && "No split dwarf debug info?");
3056 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003057}
3058
Eric Christopherd692c1d2012-12-11 19:42:09 +00003059// Emit the .debug_info.dwo section for separated dwarf. This contains the
3060// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003061void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00003062 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christophera2de8262012-12-15 00:04:07 +00003063 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher3c5a1912012-12-19 22:02:53 +00003064 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3065 DwarfAbbrevDWOSectionSym);
3066}
3067
3068// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3069// abbreviations for the .debug_info.dwo section.
3070void DwarfDebug::emitDebugAbbrevDWO() {
3071 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher48fef592012-12-20 21:58:40 +00003072 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3073 &Abbreviations);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003074}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003075
3076// Emit the .debug_str.dwo section for separated dwarf. This contains the
3077// string section and is identical in format to traditional .debug_str
3078// sections.
3079void DwarfDebug::emitDebugStrDWO() {
3080 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00003081 const MCSection *OffSec =
3082 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00003083 const MCSymbol *StrSym = DwarfStrSectionSym;
3084 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
3085 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003086}
David Blaikie409dd9c2013-11-19 23:08:21 +00003087
David Blaikie3c1d3322013-12-02 18:44:29 +00003088void DwarfDebug::addTypeUnitType(uint16_t Language, DIE *RefDie,
3089 DICompositeType CTy) {
Eric Christopherf52eddf2013-11-26 22:23:27 +00003090 DenseMap<const MDNode *,
3091 std::pair<uint64_t, SmallVectorImpl<DIE *> *> >::iterator I =
3092 TypeUnits.find(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003093 SmallVector<DIE *, 8> References;
3094 References.push_back(RefDie);
3095 if (I != TypeUnits.end()) {
3096 if (I->second.second) {
3097 I->second.second->push_back(RefDie);
3098 return;
3099 }
3100 } else {
3101 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie319a05f2013-12-02 19:33:10 +00003102 TypeUnit *NewTU = new TypeUnit(GlobalCUIndexCount++, UnitDie, Language, Asm,
3103 this, &InfoHolder);
3104 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie3c1d3322013-12-02 18:44:29 +00003105 Language);
David Blaikie409dd9c2013-11-19 23:08:21 +00003106
3107 // Register the type in the TypeUnits map with a vector of references to be
3108 // populated whenever a reference is required.
3109 I = TypeUnits.insert(std::make_pair(CTy, std::make_pair(0, &References)))
3110 .first;
3111
3112 // Construct the type, this may, recursively, require more type units that
3113 // may in turn require this type again - in which case they will add DIEs to
3114 // the References vector.
David Blaikie319a05f2013-12-02 19:33:10 +00003115 DIE *Die = NewTU->createTypeDIE(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003116
David Blaikie319a05f2013-12-02 19:33:10 +00003117 if (GenerateODRHash && shouldAddODRHash(NewTU, Die))
3118 NewTU->addUInt(UnitDie, dwarf::DW_AT_GNU_odr_signature,
David Blaikie409dd9c2013-11-19 23:08:21 +00003119 dwarf::DW_FORM_data8,
3120 DIEHash().computeDIEODRSignature(*Die));
3121 // FIXME: This won't handle circularly referential structures, as the DIE
3122 // may have references to other DIEs still under construction and missing
3123 // their signature. Hashing should walk through the signatures to their
3124 // referenced type, or possibly walk the precomputed hashes of related types
3125 // at the end.
3126 uint64_t Signature = DIEHash().computeTypeSignature(*Die);
3127
3128 // Remove the References vector and add the type hash.
3129 I->second.first = Signature;
3130 I->second.second = NULL;
3131
David Blaikie319a05f2013-12-02 19:33:10 +00003132 InfoHolder.addUnit(NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00003133 }
3134
3135 // Populate all the signatures.
3136 for (unsigned i = 0, e = References.size(); i != e; ++i) {
3137 CUMap.begin()->second->addUInt(References[i], dwarf::DW_AT_signature,
3138 dwarf::DW_FORM_ref_sig8, I->second.first);
3139 }
3140}