blob: 52a809f9bf0d7dbbdfedc17e642f37090c596b26 [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 Christopheracdcbdb2012-11-27 22:43:45 +0000470// Construct new DW_TAG_lexical_block for this scope and attach
471// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher6a841382012-11-19 22:42:10 +0000472DIE *DwarfDebug::constructLexicalScopeDIE(CompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000473 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000474 if (isLexicalScopeDIENull(Scope))
475 return 0;
476
Devang Patel6c74a872010-04-27 19:46:33 +0000477 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
478 if (Scope->isAbstractScope())
479 return ScopeDIE;
480
Eric Christopher0f63d062013-12-03 00:45:45 +0000481 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherdc42ea82013-07-03 01:57:28 +0000482 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000483 if (ScopeRanges.size() > 1) {
Devang Patel6c74a872010-04-27 19:46:33 +0000484 // .debug_range section has not been laid out yet. Emit offset in
Eric Christopher4751d702013-11-23 00:05:29 +0000485 // .debug_range as a relocatable label. emitDIE will handle
486 // emitting it appropriately.
Eric Christopher0f63d062013-12-03 00:45:45 +0000487 TheCU->addSectionLabel(
488 ScopeDIE, dwarf::DW_AT_ranges,
489 Asm->GetTempSymbol("debug_ranges", GlobalRangeCount));
490 RangeSpanList *List = new RangeSpanList(GlobalRangeCount++);
491 for (SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin(),
492 RE = ScopeRanges.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000493 RI != RE; ++RI) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000494 RangeSpan Range(getLabelBeforeInsn(RI->first),
495 getLabelAfterInsn(RI->second));
496 List->addRange(Range);
Devang Patel6c74a872010-04-27 19:46:33 +0000497 }
Eric Christopherc1110832013-07-03 01:22:29 +0000498
Eric Christopher0f63d062013-12-03 00:45:45 +0000499 // Add the range list to the set of ranges to be emitted.
500 TheCU->addRangeList(List);
Devang Patel6c74a872010-04-27 19:46:33 +0000501 return ScopeDIE;
502 }
503
Eric Christopherdc42ea82013-07-03 01:57:28 +0000504 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000505 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000506 MCSymbol *Start = getLabelBeforeInsn(RI->first);
507 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000508 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000509
Chris Lattnere13c3722010-03-09 01:58:53 +0000510 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
511 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000512
Eric Christopher962c9082013-01-15 23:56:56 +0000513 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
514 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000515
516 return ScopeDIE;
517}
518
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000519// This scope represents inlined body of a function. Construct DIE to
520// represent this concrete inlined copy of the function.
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000521DIE *DwarfDebug::constructInlinedScopeDIE(CompileUnit *TheCU,
522 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000523 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
524 assert(ScopeRanges.empty() == false &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000525 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000526
Devang Patelf098ce22011-07-27 00:34:13 +0000527 if (!Scope->getScopeNode())
528 return NULL;
529 DIScope DS(Scope->getScopeNode());
530 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000531 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000532 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000533 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000534 return NULL;
535 }
536
Devang Patel73bc1722011-05-05 17:54:26 +0000537 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000538 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000539
Eric Christopher0f63d062013-12-03 00:45:45 +0000540 if (ScopeRanges.size() > 1) {
Devang Patelf098ce22011-07-27 00:34:13 +0000541 // .debug_range section has not been laid out yet. Emit offset in
Eric Christopher4751d702013-11-23 00:05:29 +0000542 // .debug_range as a relocatable label. emitDIE will handle
543 // emitting it appropriately.
Eric Christopher0f63d062013-12-03 00:45:45 +0000544 TheCU->addSectionLabel(
545 ScopeDIE, dwarf::DW_AT_ranges,
546 Asm->GetTempSymbol("debug_ranges", GlobalRangeCount));
547 RangeSpanList *List = new RangeSpanList(GlobalRangeCount++);
548 for (SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin(),
549 RE = ScopeRanges.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000550 RI != RE; ++RI) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000551 RangeSpan Range(getLabelBeforeInsn(RI->first),
552 getLabelAfterInsn(RI->second));
553 List->addRange(Range);
Devang Patelf098ce22011-07-27 00:34:13 +0000554 }
Eric Christopher0f63d062013-12-03 00:45:45 +0000555
556 // Add the range list to the set of ranges to be emitted.
557 TheCU->addRangeList(List);
Devang Patelf098ce22011-07-27 00:34:13 +0000558 } else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000559 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000560 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
561 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
562
563 if (StartLabel == 0 || EndLabel == 0)
564 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
565
566 assert(StartLabel->isDefined() &&
567 "Invalid starting label for an inlined scope!");
568 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
569
Eric Christopher962c9082013-01-15 23:56:56 +0000570 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
571 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000572 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000573
574 InlinedSubprogramDIEs.insert(OriginDIE);
575
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000576 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000577 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000578 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000579 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
580 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000581 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000582
Eric Christopher8dda5d02011-12-04 06:02:38 +0000583 // Add name to the name table, we do this here because we're guaranteed
584 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
585 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000586
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000587 return ScopeDIE;
588}
589
Manman Ren2312ed32013-09-10 18:40:41 +0000590DIE *DwarfDebug::createScopeChildrenDIE(CompileUnit *TheCU, LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000591 SmallVectorImpl<DIE *> &Children) {
592 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000593
594 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000595 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000596 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
597 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000598 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000599 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000600 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000601 if (ArgDV->isObjectPointer())
602 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000603 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000604
Eric Christopherf84354b2011-10-03 15:49:16 +0000605 // Collect lexical scope children first.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000606 const SmallVectorImpl<DbgVariable *> &Variables =
607 ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000608 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000609 if (DIE *Variable = TheCU->constructVariableDIE(*Variables[i],
610 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000611 Children.push_back(Variable);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000612 if (Variables[i]->isObjectPointer())
613 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000614 }
Craig Topper977e9cd2013-07-03 04:24:43 +0000615 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000616 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000617 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000618 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000619 return ObjectPointer;
620}
621
622// Construct a DIE for this scope.
623DIE *DwarfDebug::constructScopeDIE(CompileUnit *TheCU, LexicalScope *Scope) {
624 if (!Scope || !Scope->getScopeNode())
625 return NULL;
626
627 DIScope DS(Scope->getScopeNode());
628
629 SmallVector<DIE *, 8> Children;
630 DIE *ObjectPointer = NULL;
631 bool ChildrenCreated = false;
632
Manman Ren5b2f4b02013-09-11 19:40:28 +0000633 // We try to create the scope DIE first, then the children DIEs. This will
634 // avoid creating un-used children then removing them later when we find out
635 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000636 DIE *ScopeDIE = NULL;
637 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000638 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000639 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000640 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000641 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000642 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000643 // Note down abstract DIE.
644 if (ScopeDIE)
645 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000646 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000647 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000648 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000649 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000650 if (isLexicalScopeDIENull(Scope))
651 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000652
653 // We create children here when we know the scope DIE is not going to be
654 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000655 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
656 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000657
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000658 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000659 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000660 ImportedEntityMap::const_iterator> Range =
661 std::equal_range(
662 ScopesWithImportedEntities.begin(),
663 ScopesWithImportedEntities.end(),
664 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
665 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000666 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000667 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000668 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000669 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000670 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
671 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000672 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000673 }
Eric Christopher6a841382012-11-19 22:42:10 +0000674
Benjamin Kramer892daba2013-08-24 11:55:49 +0000675 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000676 assert(Children.empty() &&
677 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000678 return NULL;
679 }
Manman Ren2312ed32013-09-10 18:40:41 +0000680 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000681 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000682 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000683
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000684 // Add children
Craig Topperd8e43652013-07-03 04:17:25 +0000685 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000686 E = Children.end();
687 I != E; ++I)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000688 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000689
Eric Christophere3417762012-09-12 23:36:19 +0000690 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000691 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000692
Eric Christopherd9843b32011-11-10 19:25:34 +0000693 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000694}
695
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000696// Look up the source id with the given directory and source file names.
697// If none currently exists, create a new id and insert it in the
698// SourceIds map. This can update DirectoryNames and SourceFileNames maps
699// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000700unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
701 unsigned CUID) {
Manman Ren1e427202013-03-07 01:42:00 +0000702 // If we use .loc in assembly, we can't separate .file entries according to
703 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000704
705 // FIXME: add a better feature test than hasRawTextSupport. Even better,
706 // extend .file to support this.
707 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000708 CUID = 0;
709
Devang Patel871d0b12010-09-16 20:57:49 +0000710 // If FE did not provide a file name, then assume stdin.
711 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000712 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000713
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000714 // TODO: this might not belong here. See if we can factor this better.
715 if (DirName == CompilationDir)
716 DirName = "";
717
Manman Ren1e427202013-03-07 01:42:00 +0000718 // FileIDCUMap stores the current ID for the given compile unit.
719 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000720
Manman Ren1e427202013-03-07 01:42:00 +0000721 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000722 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000723 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000724 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000725 NamePair += DirName;
726 NamePair += '\0'; // Zero bytes are not allowed in paths.
727 NamePair += FileName;
728
729 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
730 if (Ent.getValue() != SrcId)
731 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000732
Manman Ren1e427202013-03-07 01:42:00 +0000733 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000734 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000735 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000736
737 return SrcId;
738}
739
Eric Christopher48fef592012-12-20 21:58:40 +0000740// Create new CompileUnit for the given metadata node with tag
741// DW_TAG_compile_unit.
David Blaikie5a152402013-11-15 23:52:02 +0000742CompileUnit *DwarfDebug::constructCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000743 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000744 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000745
746 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikie5a152402013-11-15 23:52:02 +0000747 CompileUnit *NewCU = new CompileUnit(GlobalCUIndexCount++, Die, DIUnit, Asm,
748 this, &InfoHolder);
Manman Ren1e427202013-03-07 01:42:00 +0000749
750 FileIDCUMap[NewCU->getUniqueID()] = 0;
751 // Call this to emit a .file directive if it wasn't emitted for the source
752 // file this CU comes from yet.
753 getOrCreateSourceID(FN, CompilationDir, NewCU->getUniqueID());
754
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000755 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000756 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
757 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000758 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000759
Eric Christopherb1b94512012-08-01 18:19:01 +0000760 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher52ce7182013-04-09 19:23:15 +0000761 // into an entity. We're using 0 (or a NULL label) for this. For
762 // split dwarf it's in the skeleton CU so omit it here.
763 if (!useSplitDwarf())
764 NewCU->addLabelAddress(Die, dwarf::DW_AT_low_pc, NULL);
Manman Ren4e042a62013-02-05 21:52:47 +0000765
766 // Define start line table label for each Compile Unit.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000767 MCSymbol *LineTableStartSym =
768 Asm->GetTempSymbol("line_table_start", NewCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +0000769 Asm->OutStreamer.getContext().setMCLineTableSymbol(LineTableStartSym,
770 NewCU->getUniqueID());
771
Manman Ren9d4c7352013-05-21 00:57:22 +0000772 // Use a single line table if we are using .loc and generating assembly.
773 bool UseTheFirstCU =
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000774 (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport()) ||
775 (NewCU->getUniqueID() == 0);
Manman Ren9d4c7352013-05-21 00:57:22 +0000776
Eric Christopher52ce7182013-04-09 19:23:15 +0000777 if (!useSplitDwarf()) {
Eric Christophera51d3fc2013-09-27 22:50:48 +0000778 // DW_AT_stmt_list is a offset of line number information for this
779 // compile unit in debug_line section. For split dwarf this is
780 // left in the skeleton CU and so not included.
781 // The line table entries are not always emitted in assembly, so it
782 // is not okay to use line_table_start here.
Eric Christopher52ce7182013-04-09 19:23:15 +0000783 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000784 NewCU->addSectionLabel(
785 Die, dwarf::DW_AT_stmt_list,
786 UseTheFirstCU ? Asm->GetTempSymbol("section_line")
787 : LineTableStartSym);
Manman Ren9d4c7352013-05-21 00:57:22 +0000788 else if (UseTheFirstCU)
Eric Christopher33ff6972013-11-21 23:46:41 +0000789 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher52ce7182013-04-09 19:23:15 +0000790 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000791 NewCU->addSectionDelta(Die, dwarf::DW_AT_stmt_list,
792 LineTableStartSym, DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000793
794 // If we're using split dwarf the compilation dir is going to be in the
795 // skeleton CU and so we don't need to duplicate it here.
796 if (!CompilationDir.empty())
797 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
798
Eric Christopher1d06eb52013-10-01 00:43:31 +0000799 // Flags to let the linker know we have emitted new style pubnames. Only
Eric Christophera51d3fc2013-09-27 22:50:48 +0000800 // emit it here if we don't have a skeleton CU for split dwarf.
Eric Christopher39eebfa2013-09-30 23:14:16 +0000801 if (GenerateGnuPubSections) {
802 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000803 NewCU->addSectionLabel(
804 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000805 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000806 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000807 NewCU->addSectionDelta(
808 Die, dwarf::DW_AT_GNU_pubnames,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000809 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
810 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000811
812 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +0000813 NewCU->addSectionLabel(
814 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000815 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +0000816 else
Eric Christopher33ff6972013-11-21 23:46:41 +0000817 NewCU->addSectionDelta(
818 Die, dwarf::DW_AT_GNU_pubtypes,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000819 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
820 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +0000821 }
Eric Christopher52ce7182013-04-09 19:23:15 +0000822 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000823
Bill Wendling2b128d72009-05-20 23:19:06 +0000824 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000825 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000826
Devang Patel2d9caf92009-11-25 17:36:49 +0000827 StringRef Flags = DIUnit.getFlags();
828 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000829 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000830
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000831 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000832 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000833 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000834
Devang Patel1a0df9a2010-05-10 22:49:55 +0000835 if (!FirstCU)
836 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000837
Eric Christopherc8a310e2012-12-10 23:34:43 +0000838 InfoHolder.addUnit(NewCU);
839
David Blaikie5a152402013-11-15 23:52:02 +0000840 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000841 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000842 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000843}
844
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000845// Construct subprogram DIE.
Eric Christophera6c38a32013-10-15 23:31:38 +0000846void DwarfDebug::constructSubprogramDIE(CompileUnit *TheCU, const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000847 // FIXME: We should only call this routine once, however, during LTO if a
848 // program is defined in multiple CUs we could end up calling it out of
849 // beginModule as we walk the CUs.
850
851 CompileUnit *&CURef = SPMap[N];
852 if (CURef)
853 return;
854 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000855
Devang Patel80ae3492009-08-28 23:24:31 +0000856 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000857 if (!SP.isDefinition())
858 // This is a method declaration which will be handled while constructing
859 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000860 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000861
Devang Patel89543712011-08-15 17:24:54 +0000862 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000863
Eric Christopherba506db2013-09-09 20:03:20 +0000864 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000865 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000866}
867
David Blaikie1fd43652013-05-07 21:35:53 +0000868void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000869 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000870 DIImportedEntity Module(N);
David Blaikief55abea2013-04-22 06:12:31 +0000871 if (!Module.Verify())
872 return;
David Blaikie684fc532013-05-06 23:33:07 +0000873 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000874 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000875}
876
David Blaikie4dd2de72013-05-08 06:01:38 +0000877void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU, const MDNode *N,
David Blaikie684fc532013-05-06 23:33:07 +0000878 DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000879 DIImportedEntity Module(N);
David Blaikie684fc532013-05-06 23:33:07 +0000880 if (!Module.Verify())
881 return;
David Blaikie4dd2de72013-05-08 06:01:38 +0000882 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000883}
884
David Blaikie4dd2de72013-05-08 06:01:38 +0000885void DwarfDebug::constructImportedEntityDIE(CompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000886 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000887 DIE *Context) {
888 assert(Module.Verify() &&
889 "Use one of the MDNode * overloads to handle invalid metadata");
890 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000891 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000892 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000893 DIE *EntityDie;
894 DIDescriptor Entity = Module.getEntity();
895 if (Entity.isNameSpace())
896 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
897 else if (Entity.isSubprogram())
898 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000899 else if (Entity.isType())
900 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000901 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000902 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000903 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
904 Module.getContext().getDirectory(),
905 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000906 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
907 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000908 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000909 StringRef Name = Module.getName();
910 if (!Name.empty())
911 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000912 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000913}
914
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000915// Emit all Dwarf sections that should come prior to the content. Create
916// global DIEs and emit initial debug info sections. This is invoked by
917// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000918void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000919 if (DisableDebugInfoPrinting)
920 return;
921
Eric Christopher58f41952012-11-19 22:42:15 +0000922 const Module *M = MMI->getModule();
923
Nick Lewycky019d2552011-07-29 03:49:23 +0000924 // If module has named metadata anchors then use them, otherwise scan the
925 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000926 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000927 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000928 return;
Manman Ren60352032013-09-05 18:48:31 +0000929 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000930
David Blaikiedc69ebb2013-03-11 23:39:23 +0000931 // Emit initial sections so we can reference labels later.
932 emitSectionLabels();
933
934 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
935 DICompileUnit CUNode(CU_Nodes->getOperand(i));
936 CompileUnit *CU = constructCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000937 DIArray ImportedEntities = CUNode.getImportedEntities();
938 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000939 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000940 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
941 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000942 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000943 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000944 DIArray GVs = CUNode.getGlobalVariables();
945 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000946 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000947 DIArray SPs = CUNode.getSubprograms();
948 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
949 constructSubprogramDIE(CU, SPs.getElement(i));
950 DIArray EnumTypes = CUNode.getEnumTypes();
951 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
952 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
953 DIArray RetainedTypes = CUNode.getRetainedTypes();
954 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
955 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000956 // Emit imported_modules last so that the relevant context is already
957 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000958 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
959 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000960 }
Eric Christopher6a841382012-11-19 22:42:10 +0000961
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000962 // Tell MMI that we have debug info.
963 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000964
Bill Wendling2b128d72009-05-20 23:19:06 +0000965 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000966 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000967}
968
Eric Christopher960ac372012-11-22 00:59:49 +0000969// Attach DW_AT_inline attribute with inlined subprogram DIEs.
970void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000971 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
972 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000973 AE = InlinedSubprogramDIEs.end();
974 AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000975 DIE *ISP = *AI;
David Blaikief2443192013-10-21 17:28:37 +0000976 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000977 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000978 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000979 AE = AbstractSPDies.end();
980 AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000981 DIE *ISP = AI->second;
982 if (InlinedSubprogramDIEs.count(ISP))
983 continue;
David Blaikief2443192013-10-21 17:28:37 +0000984 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000985 }
Eric Christopher960ac372012-11-22 00:59:49 +0000986}
987
988// Collect info for variables that were optimized out.
989void DwarfDebug::collectDeadVariables() {
990 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000991
992 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
993 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
994 DICompileUnit TheCU(CU_Nodes->getOperand(i));
995 DIArray Subprograms = TheCU.getSubprograms();
996 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000997 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000998 if (ProcessedSPNodes.count(SP) != 0)
999 continue;
1000 if (!SP.isSubprogram())
1001 continue;
1002 if (!SP.isDefinition())
1003 continue;
Eric Christopher735401c2012-11-27 00:13:51 +00001004 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +00001005 if (Variables.getNumElements() == 0)
1006 continue;
Eric Christopher960ac372012-11-22 00:59:49 +00001007
Eric Christopher735401c2012-11-27 00:13:51 +00001008 // Construct subprogram DIE and add variables DIEs.
David Blaikie319a05f2013-12-02 19:33:10 +00001009 CompileUnit *SPCU = static_cast<CompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +00001010 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +00001011 // FIXME: See the comment in constructSubprogramDIE about duplicate
1012 // subprogram DIEs.
1013 constructSubprogramDIE(SPCU, SP);
1014 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +00001015 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
1016 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +00001017 if (!DV.isVariable())
1018 continue;
Manman Renb3388602013-10-05 01:43:03 +00001019 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001020 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +00001021 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +00001022 }
Eric Christopher960ac372012-11-22 00:59:49 +00001023 }
1024 }
1025 }
Eric Christopher960ac372012-11-22 00:59:49 +00001026}
1027
Eric Christopher45731982013-08-08 23:45:55 +00001028// Type Signature [7.27] and ODR Hash code.
Eric Christopher67646432013-07-26 17:02:41 +00001029
1030/// \brief Grabs the string in whichever attribute is passed in and returns
Eric Christopher8552e222013-08-07 01:18:33 +00001031/// a reference to it. Returns "" if the attribute doesn't exist.
Eric Christopher67646432013-07-26 17:02:41 +00001032static StringRef getDIEStringAttr(DIE *Die, unsigned Attr) {
Eric Christopher8552e222013-08-07 01:18:33 +00001033 DIEValue *V = Die->findAttribute(Attr);
Eric Christopher67646432013-07-26 17:02:41 +00001034
Eric Christopher8552e222013-08-07 01:18:33 +00001035 if (DIEString *S = dyn_cast_or_null<DIEString>(V))
1036 return S->getString();
1037
Eric Christopher67646432013-07-26 17:02:41 +00001038 return StringRef("");
1039}
1040
Eric Christopher67646432013-07-26 17:02:41 +00001041/// Return true if the current DIE is contained within an anonymous namespace.
1042static bool isContainedInAnonNamespace(DIE *Die) {
1043 DIE *Parent = Die->getParent();
1044
1045 while (Parent) {
Eric Christophere414ece2013-07-29 23:53:08 +00001046 if (Parent->getTag() == dwarf::DW_TAG_namespace &&
1047 getDIEStringAttr(Parent, dwarf::DW_AT_name) == "")
Eric Christopher67646432013-07-26 17:02:41 +00001048 return true;
1049 Parent = Parent->getParent();
1050 }
1051
1052 return false;
1053}
1054
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001055/// Test if the current CU language is C++ and that we have
1056/// a named type that is not contained in an anonymous namespace.
David Blaikie319a05f2013-12-02 19:33:10 +00001057static bool shouldAddODRHash(TypeUnit *CU, DIE *Die) {
Eric Christopher341770d2013-08-07 08:35:10 +00001058 return CU->getLanguage() == dwarf::DW_LANG_C_plus_plus &&
1059 getDIEStringAttr(Die, dwarf::DW_AT_name) != "" &&
1060 !isContainedInAnonNamespace(Die);
Eric Christopher45731982013-08-08 23:45:55 +00001061}
Eric Christopheraf15f8d2013-08-07 01:18:24 +00001062
Eric Christopher960ac372012-11-22 00:59:49 +00001063void DwarfDebug::finalizeModuleInfo() {
1064 // Collect info for variables that were optimized out.
1065 collectDeadVariables();
1066
1067 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
1068 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001069
Eric Christopher60eb7692013-08-12 20:27:48 +00001070 // Handle anything that needs to be done on a per-cu basis.
David Blaikie319a05f2013-12-02 19:33:10 +00001071 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
1072 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00001073 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00001074 Unit *TheU = *I;
Eric Christopher60eb7692013-08-12 20:27:48 +00001075 // Emit DW_AT_containing_type attribute to connect types with their
1076 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +00001077 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +00001078
1079 // If we're splitting the dwarf out now that we've got the entire
1080 // CU then construct a skeleton CU based upon it.
David Blaikie319a05f2013-12-02 19:33:10 +00001081 if (useSplitDwarf() &&
David Blaikie2a80e442013-12-02 22:09:48 +00001082 TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
Eric Christopherd29614f2013-08-13 01:21:55 +00001083 uint64_t ID = 0;
1084 if (GenerateCUHash) {
1085 DIEHash CUHash;
David Blaikie2a80e442013-12-02 22:09:48 +00001086 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
Eric Christopherd29614f2013-08-13 01:21:55 +00001087 }
Eric Christopher60eb7692013-08-12 20:27:48 +00001088 // This should be a unique identifier when we want to build .dwp files.
David Blaikie2a80e442013-12-02 22:09:48 +00001089 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1090 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001091 // Now construct the skeleton CU associated.
David Blaikie2a80e442013-12-02 22:09:48 +00001092 CompileUnit *SkCU = constructSkeletonCU(static_cast<CompileUnit *>(TheU));
Eric Christopher60eb7692013-08-12 20:27:48 +00001093 // This should be a unique identifier when we want to build .dwp files.
David Blaikie2a80e442013-12-02 22:09:48 +00001094 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
Eric Christopherd29614f2013-08-13 01:21:55 +00001095 dwarf::DW_FORM_data8, ID);
Eric Christopher60eb7692013-08-12 20:27:48 +00001096 }
1097 }
1098
1099 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001100 InfoHolder.computeSizeAndOffsets();
1101 if (useSplitDwarf())
1102 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001103}
1104
1105void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001106 // Filter labels by section.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001107 for (size_t n = 0; n < ArangeLabels.size(); n++) {
1108 const SymbolCU &SCU = ArangeLabels[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001109 if (SCU.Sym->isInSection()) {
1110 // Make a note of this symbol and it's section.
1111 const MCSection *Section = &SCU.Sym->getSection();
1112 if (!Section->getKind().isMetadata())
1113 SectionMap[Section].push_back(SCU);
1114 } else {
1115 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1116 // appear in the output. This sucks as we rely on sections to build
1117 // arange spans. We can do it without, but it's icky.
1118 SectionMap[NULL].push_back(SCU);
1119 }
1120 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001121
Richard Mittonc2508242013-10-03 22:07:08 +00001122 // Build a list of sections used.
1123 std::vector<const MCSection *> Sections;
Richard Mitton21101b32013-09-19 23:21:01 +00001124 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
1125 it++) {
1126 const MCSection *Section = it->first;
Richard Mittonc2508242013-10-03 22:07:08 +00001127 Sections.push_back(Section);
1128 }
1129
1130 // Sort the sections into order.
1131 // This is only done to ensure consistent output order across different runs.
1132 std::sort(Sections.begin(), Sections.end(), SectionSort);
1133
1134 // Add terminating symbols for each section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001135 for (unsigned ID = 0; ID < Sections.size(); ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001136 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001137 MCSymbol *Sym = NULL;
1138
1139 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001140 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001141 // if we know the section name up-front. For user-created sections, the
1142 // resulting
1143 // label may not be valid to use as a label. (section names can use a
1144 // greater
Richard Mittonc2508242013-10-03 22:07:08 +00001145 // set of characters on some systems)
1146 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001147 Asm->OutStreamer.SwitchSection(Section);
1148 Asm->OutStreamer.EmitLabel(Sym);
1149 }
1150
1151 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001152 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001153 }
Eric Christopher960ac372012-11-22 00:59:49 +00001154}
Bill Wendling2b128d72009-05-20 23:19:06 +00001155
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001156// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001157void DwarfDebug::endModule() {
1158
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001159 if (!FirstCU)
1160 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001161
1162 // End any existing sections.
1163 // TODO: Does this need to happen?
1164 endSections();
1165
1166 // Finalize the debug info for the module.
1167 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001168
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001169 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001170
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001171 // Emit all the DIEs into a debug info section.
1172 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001173
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001174 // Corresponding abbreviations into a abbrev section.
1175 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001176
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001177 // Emit info into a debug loc section.
1178 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001179
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001180 // Emit info into a debug aranges section.
1181 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001182
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001183 // Emit info into a debug ranges section.
1184 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001185
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001186 // Emit info into a debug macinfo section.
1187 emitDebugMacInfo();
Eric Christopher95198f502012-11-27 22:43:42 +00001188
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001189 if (useSplitDwarf()) {
1190 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001191 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001192 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001193 // Emit DWO addresses.
1194 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001195 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001196
Eric Christophera876b822012-08-23 07:32:06 +00001197 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001198 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001199 emitAccelNames();
1200 emitAccelObjC();
1201 emitAccelNamespaces();
1202 emitAccelTypes();
1203 }
Eric Christopher6a841382012-11-19 22:42:10 +00001204
Eric Christopher4b358182013-08-30 00:40:17 +00001205 // Emit the pubnames and pubtypes sections if requested.
1206 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001207 emitDebugPubNames(GenerateGnuPubSections);
1208 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001209 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001210
Devang Pateld0701282010-08-02 17:32:15 +00001211 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001212 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001213
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001214 // Reset these for the next Module if we have one.
1215 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001216}
1217
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001218// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001219DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001220 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001221 LLVMContext &Ctx = DV->getContext();
1222 // More then one inlined variable corresponds to one abstract variable.
1223 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001224 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001225 if (AbsDbgVariable)
1226 return AbsDbgVariable;
1227
Devang Patel7e623022011-08-10 20:55:27 +00001228 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001229 if (!Scope)
1230 return NULL;
1231
Manman Renb3388602013-10-05 01:43:03 +00001232 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001233 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001234 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001235 return AbsDbgVariable;
1236}
1237
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001238// If Var is a current function argument then add it to CurrentFnArguments list.
Devang Patel6c622ef2011-03-01 22:58:55 +00001239bool DwarfDebug::addCurrentFnArgument(const MachineFunction *MF,
Devang Patel7e623022011-08-10 20:55:27 +00001240 DbgVariable *Var, LexicalScope *Scope) {
1241 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001242 return false;
1243 DIVariable DV = Var->getVariable();
1244 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1245 return false;
1246 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001247 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001248 return false;
1249
Devang Patel4ab660b2011-03-03 20:02:02 +00001250 size_t Size = CurrentFnArguments.size();
1251 if (Size == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001252 CurrentFnArguments.resize(MF->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001253 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001254 // arguments does the function have at source level.
1255 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001256 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001257 CurrentFnArguments[ArgNo - 1] = Var;
1258 return true;
1259}
1260
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001261// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001262void DwarfDebug::collectVariableInfoFromMMITable(
1263 const MachineFunction *MF, SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001264 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1265 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001266 VE = VMap.end();
1267 VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001268 const MDNode *Var = VI->first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001269 if (!Var)
1270 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001271 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001272 DIVariable DV(Var);
1273 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001274
Devang Patel7e623022011-08-10 20:55:27 +00001275 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001276
Devang Patelcdb7d442009-11-10 23:20:04 +00001277 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001278 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001279 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001280
Devang Patele1c53f22010-05-20 16:36:41 +00001281 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001282 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001283 RegVar->setFrameIndex(VP.first);
Devang Patel6c622ef2011-03-01 22:58:55 +00001284 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001285 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001286 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001287 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001288 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001289}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001290
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001291// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1292// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001293static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001294 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001295 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1296 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001297 (MI->getOperand(1).isImm() ||
1298 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001299}
1300
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001301// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001302static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1303 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001304 const MCSymbol *SLabel,
1305 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001306 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001307
David Blaikie0252265b2013-06-16 20:34:15 +00001308 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001309 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001310 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001311 // If the second operand is an immediate, this is a
1312 // register-indirect address.
1313 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001314 MLoc.set(MI->getOperand(0).getReg());
1315 else
1316 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001317 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1318 }
1319 if (MI->getOperand(0).isImm())
1320 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1321 if (MI->getOperand(0).isFPImm())
1322 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1323 if (MI->getOperand(0).isCImm())
1324 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1325
Craig Topperee4dab52012-02-05 08:31:47 +00001326 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001327}
1328
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001329// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001330void
Devang Patel5c0f85c2010-06-25 22:07:34 +00001331DwarfDebug::collectVariableInfo(const MachineFunction *MF,
1332 SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001333
Eric Christopher270a12c2013-07-03 21:37:03 +00001334 // Grab the variable info that was squirreled away in the MMI side-table.
Devang Patel490c8ab2010-05-20 19:57:06 +00001335 collectVariableInfoFromMMITable(MF, Processed);
1336
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001337 for (SmallVectorImpl<const MDNode *>::const_iterator
1338 UVI = UserVariables.begin(),
1339 UVE = UserVariables.end();
1340 UVI != UVE; ++UVI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001341 const MDNode *Var = *UVI;
1342 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001343 continue;
1344
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001345 // History contains relevant DBG_VALUE instructions for Var and instructions
1346 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001347 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001348 if (History.empty())
1349 continue;
1350 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001351
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001352 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001353 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001354 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
1355 DISubprogram(DV.getContext()).describes(MF->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001356 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001357 else if (MDNode *IA = DV.getInlinedAt())
1358 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1359 else
1360 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001361 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001362 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001363 continue;
1364
1365 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001366 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001367 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001368 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Devang Patel6c622ef2011-03-01 22:58:55 +00001369 if (!addCurrentFnArgument(MF, RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001370 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001371 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001372 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001373
Eric Christophercc10d202012-10-08 20:48:54 +00001374 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001375 if (History.size() <= 1 ||
1376 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001377 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001378 continue;
1379 }
1380
Eric Christopher59cc0712013-01-28 17:33:26 +00001381 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001382 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001383
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001384 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1385 HI = History.begin(),
1386 HE = History.end();
1387 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001388 const MachineInstr *Begin = *HI;
1389 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001390
Devang Patele7181b52011-06-01 23:00:17 +00001391 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001392 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1393 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001394 continue;
1395
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001396 // Compute the range for a register location.
1397 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1398 const MCSymbol *SLabel = 0;
1399
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001400 if (HI + 1 == HE)
1401 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001402 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001403 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001404 else {
1405 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001406 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001407 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001408 if (End->isDebugValue())
1409 SLabel = getLabelBeforeInsn(End);
1410 else {
1411 // End is a normal instruction clobbering the range.
1412 SLabel = getLabelAfterInsn(End);
1413 assert(SLabel && "Forgot label after clobber instruction");
1414 ++HI;
1415 }
1416 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001417
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001418 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001419 DotDebugLocEntries.push_back(
1420 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001421 }
1422 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001423 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001424
1425 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001426 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1427 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1428 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1429 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001430 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001431 continue;
1432 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001433 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001434 }
Devang Patel9fc11702010-05-25 23:40:22 +00001435}
Devang Patele0a94bf2010-05-14 21:01:35 +00001436
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001437// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001438MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001439 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1440 assert(Label && "Didn't insert label before instruction");
1441 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001442}
1443
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001444// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001445MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001446 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001447}
1448
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001449// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001450void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001451 // Check if source location changes, but ignore DBG_VALUE locations.
1452 if (!MI->isDebugValue()) {
1453 DebugLoc DL = MI->getDebugLoc();
1454 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001455 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001456 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001457 if (DL == PrologEndLoc) {
1458 Flags |= DWARF2_FLAG_PROLOGUE_END;
1459 PrologEndLoc = DebugLoc();
1460 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001461 if (PrologEndLoc.isUnknown())
1462 Flags |= DWARF2_FLAG_IS_STMT;
1463
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001464 if (!DL.isUnknown()) {
1465 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001466 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001467 } else
Devang Patel34a66202011-05-11 19:22:19 +00001468 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001469 }
Devang Patel9fc11702010-05-25 23:40:22 +00001470 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001471
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001472 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001473 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1474 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001475
1476 // No label needed.
1477 if (I == LabelsBeforeInsn.end())
1478 return;
1479
1480 // Label already assigned.
1481 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001482 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001483
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001484 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001485 PrevLabel = MMI->getContext().CreateTempSymbol();
1486 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001487 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001488 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001489}
1490
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001491// Process end of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001492void DwarfDebug::endInstruction(const MachineInstr *MI) {
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001493 // Don't create a new label after DBG_VALUE instructions.
1494 // They don't generate code.
1495 if (!MI->isDebugValue())
1496 PrevLabel = 0;
1497
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001498 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1499 LabelsAfterInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001500
1501 // No label needed.
1502 if (I == LabelsAfterInsn.end())
1503 return;
1504
1505 // Label already assigned.
1506 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001507 return;
1508
1509 // We need a label after this instruction.
1510 if (!PrevLabel) {
1511 PrevLabel = MMI->getContext().CreateTempSymbol();
1512 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001513 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001514 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001515}
1516
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001517// Each LexicalScope has first instruction and last instruction to mark
1518// beginning and end of a scope respectively. Create an inverse map that list
1519// scopes starts (and ends) with an instruction. One instruction may start (or
1520// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001521void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001522 SmallVector<LexicalScope *, 4> WorkList;
1523 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001524 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001525 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001526
Craig Topper977e9cd2013-07-03 04:24:43 +00001527 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001528 if (!Children.empty())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001529 for (SmallVectorImpl<LexicalScope *>::const_iterator
1530 SI = Children.begin(),
1531 SE = Children.end();
1532 SI != SE; ++SI)
Devang Patel7771b7c2010-01-20 02:05:23 +00001533 WorkList.push_back(*SI);
1534
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001535 if (S->isAbstractScope())
1536 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001537
Craig Topper977e9cd2013-07-03 04:24:43 +00001538 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001539 if (Ranges.empty())
1540 continue;
Craig Topperd8e43652013-07-03 04:17:25 +00001541 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001542 RE = Ranges.end();
1543 RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001544 assert(RI->first && "InsnRange does not have first instruction!");
1545 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001546 requestLabelBeforeInsn(RI->first);
1547 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001548 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001549 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001550}
1551
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001552// Get MDNode for DebugLoc's scope.
Devang Patel589845d2011-05-09 22:14:49 +00001553static MDNode *getScopeNode(DebugLoc DL, const LLVMContext &Ctx) {
1554 if (MDNode *InlinedAt = DL.getInlinedAt(Ctx))
1555 return getScopeNode(DebugLoc::getFromDILocation(InlinedAt), Ctx);
1556 return DL.getScope(Ctx);
1557}
1558
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001559// Walk up the scope chain of given debug loc and find line number info
1560// for the function.
Devang Patel34a66202011-05-11 19:22:19 +00001561static DebugLoc getFnDebugLoc(DebugLoc DL, const LLVMContext &Ctx) {
1562 const MDNode *Scope = getScopeNode(DL, Ctx);
1563 DISubprogram SP = getDISubprogram(Scope);
Manman Ren7504ed42013-07-08 18:33:29 +00001564 if (SP.isSubprogram()) {
Eric Christopher34164192012-04-03 00:43:49 +00001565 // Check for number of operands since the compatibility is
1566 // cheap here.
Eric Christopherb81e2b42012-04-03 17:55:42 +00001567 if (SP->getNumOperands() > 19)
Eric Christopher34164192012-04-03 00:43:49 +00001568 return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
1569 else
1570 return DebugLoc::get(SP.getLineNumber(), 0, SP);
1571 }
1572
Devang Patel34a66202011-05-11 19:22:19 +00001573 return DebugLoc();
1574}
1575
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001576// Gather pre-function debug information. Assumes being called immediately
1577// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001578void DwarfDebug::beginFunction(const MachineFunction *MF) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001579
1580 // If there's no debug info for the function we're not going to do anything.
1581 if (!MMI->hasDebugInfo())
1582 return;
1583
1584 // Grab the lexical scopes for the function, if we don't have any of those
1585 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001586 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001587 if (LScopes.empty())
1588 return;
1589
1590 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1591
1592 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001593 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001594
Manman Ren4e042a62013-02-05 21:52:47 +00001595 // Set DwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001596 // belongs to so that we add to the correct per-cu line table in the
1597 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001598 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1599 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
1600 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001601 if (Asm->TM.hasMCUseLoc() && Asm->OutStreamer.hasRawTextSupport())
Manman Ren9d4c7352013-05-21 00:57:22 +00001602 // Use a single line table if we are using .loc and generating assembly.
1603 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1604 else
1605 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001606
Eric Christopherfedfa442013-11-01 23:14:17 +00001607 // Emit a label for the function so that we have a beginning address.
1608 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001609 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001610 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001611
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001612 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001613 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001614 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001615
Eric Christopherfedfa442013-11-01 23:14:17 +00001616 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1617 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001618 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001619 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1620 II != IE; ++II) {
1621 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001622
Devang Patel002d54d2010-05-26 19:37:24 +00001623 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001624 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001625
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001626 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001627 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001628 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001629
1630 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001631 if (isDbgValueInDefinedReg(MI))
1632 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1633
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001634 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001635 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001636 if (History.empty()) {
1637 UserVariables.push_back(Var);
1638 // The first mention of a function argument gets the FunctionBeginSym
1639 // label, so arguments are visible when breaking at function entry.
1640 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001641 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001642 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001643 LabelsBeforeInsn[MI] = FunctionBeginSym;
1644 } else {
1645 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1646 const MachineInstr *Prev = History.back();
1647 if (Prev->isDebugValue()) {
1648 // Coalesce identical entries at the end of History.
1649 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001650 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001651 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001652 << "\t" << *Prev << "\t"
1653 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001654 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001655 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001656
1657 // Terminate old register assignments that don't reach MI;
1658 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1659 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1660 isDbgValueInDefinedReg(Prev)) {
1661 // Previous register assignment needs to terminate at the end of
1662 // its basic block.
1663 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001664 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001665 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001666 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001667 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001668 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001669 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001670 } else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001671 // Terminate after LastMI.
1672 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001673 }
1674 }
1675 }
1676 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001677 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001678 // Not a DBG_VALUE instruction.
1679 if (!MI->isLabel())
1680 AtBlockEntry = false;
1681
Eric Christopher133195782012-10-04 20:46:14 +00001682 // First known non-DBG_VALUE and non-frame setup location marks
1683 // the beginning of the function body.
1684 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1685 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001686 PrologEndLoc = MI->getDebugLoc();
1687
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001688 // Check if the instruction clobbers any registers with debug vars.
1689 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
Eric Christopherfedfa442013-11-01 23:14:17 +00001690 MOE = MI->operands_end();
1691 MOI != MOE; ++MOI) {
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001692 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1693 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001694 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true); AI.isValid();
1695 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001696 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001697 const MDNode *Var = LiveUserVar[Reg];
1698 if (!Var)
1699 continue;
1700 // Reg is now clobbered.
1701 LiveUserVar[Reg] = 0;
1702
1703 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001704 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1705 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001706 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001707 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001708 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001709 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001710 const MachineInstr *Prev = History.back();
1711 // Sanity-check: Register assignments are terminated at the end of
1712 // their block.
1713 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1714 continue;
1715 // Is the variable still in Reg?
1716 if (!isDbgValueInDefinedReg(Prev) ||
1717 Prev->getOperand(0).getReg() != Reg)
1718 continue;
1719 // Var is clobbered. Make sure the next instruction gets a label.
1720 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001721 }
1722 }
Devang Patel002d54d2010-05-26 19:37:24 +00001723 }
Devang Patel002d54d2010-05-26 19:37:24 +00001724 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001725 }
1726
1727 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1728 I != E; ++I) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001729 SmallVectorImpl<const MachineInstr *> &History = I->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001730 if (History.empty())
1731 continue;
1732
1733 // Make sure the final register assignments are terminated.
1734 const MachineInstr *Prev = History.back();
1735 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1736 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001737 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001738 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001739 if (LastMI == PrevMBB->end())
1740 // Drop DBG_VALUE for empty range.
1741 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001742 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001743 // Terminate after LastMI.
1744 History.push_back(LastMI);
1745 }
1746 }
1747 // Request labels for the full history.
1748 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1749 const MachineInstr *MI = History[i];
1750 if (MI->isDebugValue())
1751 requestLabelBeforeInsn(MI);
1752 else
1753 requestLabelAfterInsn(MI);
1754 }
1755 }
Devang Patel002d54d2010-05-26 19:37:24 +00001756
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001757 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001758 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001759
1760 // Record beginning of function.
1761 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001762 DebugLoc FnStartDL =
1763 getFnDebugLoc(PrologEndLoc, MF->getFunction()->getContext());
1764 recordSourceLine(
1765 FnStartDL.getLine(), FnStartDL.getCol(),
1766 FnStartDL.getScope(MF->getFunction()->getContext()),
1767 // We'd like to list the prologue as "not statements" but GDB behaves
1768 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1769 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001770 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001771}
1772
Devang Patel7e623022011-08-10 20:55:27 +00001773void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001774 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1775 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001776 // Variables with positive arg numbers are parameters.
1777 if (unsigned ArgNum = DV.getArgNumber()) {
1778 // Keep all parameters in order at the start of the variable list to ensure
1779 // function types are correct (no out-of-order parameters)
1780 //
1781 // This could be improved by only doing it for optimized builds (unoptimized
1782 // builds have the right order to begin with), searching from the back (this
1783 // would catch the unoptimized case quickly), or doing a binary search
1784 // rather than linear search.
1785 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1786 while (I != Vars.end()) {
1787 unsigned CurNum = (*I)->getVariable().getArgNumber();
1788 // A local (non-parameter) variable has been found, insert immediately
1789 // before it.
1790 if (CurNum == 0)
1791 break;
1792 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001793 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001794 break;
David Blaikieb272a752013-06-06 22:28:26 +00001795 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001796 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001797 Vars.insert(I, Var);
1798 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001799 }
1800
1801 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001802}
1803
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001804// Gather and emit post-function debug information.
Chris Lattner76555b52010-01-26 23:18:02 +00001805void DwarfDebug::endFunction(const MachineFunction *MF) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001806 if (!MMI->hasDebugInfo() || LScopes.empty())
1807 return;
Devang Patel2904aa92009-11-12 19:02:56 +00001808
Devang Patel7e623022011-08-10 20:55:27 +00001809 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001810 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001811 // Assumes in correct section after the entry point.
1812 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Manman Ren4e042a62013-02-05 21:52:47 +00001813 // Set DwarfCompileUnitID in MCContext to default value.
1814 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001815
Devang Patel7e623022011-08-10 20:55:27 +00001816 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1817 collectVariableInfo(MF, ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001818
Devang Patel3acc70e2011-08-15 22:04:40 +00001819 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001820 CompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001821 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001822
Devang Patel7e623022011-08-10 20:55:27 +00001823 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001824 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1825 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1826 LexicalScope *AScope = AList[i];
1827 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001828 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001829 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001830 DIArray Variables = SP.getVariables();
1831 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1832 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001833 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001834 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001835 // Check that DbgVariable for DV wasn't created earlier, when
1836 // findAbstractVariable() was called for inlined instance of DV.
1837 LLVMContext &Ctx = DV->getContext();
1838 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1839 if (AbstractVariables.lookup(CleanDV))
1840 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001841 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001842 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001843 }
1844 }
Devang Patel44403472011-08-12 18:10:19 +00001845 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001846 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001847 }
Eric Christopher6a841382012-11-19 22:42:10 +00001848
Devang Patel3acc70e2011-08-15 22:04:40 +00001849 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Eric Christopher6a841382012-11-19 22:42:10 +00001850
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001851 if (!MF->getTarget().Options.DisableFramePointerElim(*MF))
Eric Christopherbb69a272012-08-24 01:14:27 +00001852 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001853
Bill Wendling2b128d72009-05-20 23:19:06 +00001854 // Clear debug info
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001855 for (ScopeVariablesMap::iterator I = ScopeVariables.begin(),
1856 E = ScopeVariables.end();
1857 I != E; ++I)
Devang Patel7e623022011-08-10 20:55:27 +00001858 DeleteContainerPointers(I->second);
1859 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001860 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001861 UserVariables.clear();
1862 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001863 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001864 LabelsBeforeInsn.clear();
1865 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001866 PrevLabel = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001867}
1868
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001869// Register a source line with debug info. Returns the unique label that was
1870// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001871void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1872 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001873 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001874 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001875 unsigned Src = 1;
1876 if (S) {
1877 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001878
Dan Gohman50849c62010-05-05 23:41:32 +00001879 if (Scope.isCompileUnit()) {
1880 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001881 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001882 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001883 } else if (Scope.isFile()) {
1884 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001885 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001886 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001887 } else if (Scope.isSubprogram()) {
1888 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001889 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001890 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001891 } else if (Scope.isLexicalBlockFile()) {
1892 DILexicalBlockFile DBF(S);
1893 Fn = DBF.getFilename();
1894 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001895 } else if (Scope.isLexicalBlock()) {
1896 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001897 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001898 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001899 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001900 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001901
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001902 Src = getOrCreateSourceID(
1903 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001904 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001905 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001906}
1907
Bill Wendling806535f2009-05-20 23:22:40 +00001908//===----------------------------------------------------------------------===//
1909// Emit Methods
1910//===----------------------------------------------------------------------===//
1911
Manman Rence20d462013-10-29 22:57:10 +00001912// Compute the size and offset of a DIE. The offset is relative to start of the
1913// CU. It returns the offset after laying out the DIE.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001914unsigned DwarfUnits::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001915 // Get the children.
1916 const std::vector<DIE *> &Children = Die->getChildren();
1917
Bill Wendling480ff322009-05-20 23:21:38 +00001918 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001919 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001920
1921 // Get the abbreviation for this DIE.
1922 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00001923 const DIEAbbrev *Abbrev = Abbreviations[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00001924
1925 // Set DIE offset
1926 Die->setOffset(Offset);
1927
1928 // Start the size with the size of abbreviation code.
Chris Lattner7b26fce2009-08-22 20:48:53 +00001929 Offset += MCAsmInfo::getULEB128Size(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00001930
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001931 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00001932 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001933
1934 // Size the DIE attribute values.
1935 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1936 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001937 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001938
1939 // Size the DIE children if any.
1940 if (!Children.empty()) {
1941 assert(Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
1942 "Children flag not set");
1943
1944 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001945 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001946
1947 // End of children marker.
1948 Offset += sizeof(int8_t);
1949 }
1950
1951 Die->setSize(Offset - Die->getOffset());
1952 return Offset;
1953}
1954
Eric Christopherb088d2d2013-10-24 21:05:08 +00001955// Compute the size and offset for each DIE.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001956void DwarfUnits::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001957 // Offset from the first CU in the debug info section is 0 initially.
1958 unsigned SecOffset = 0;
1959
Eric Christopherb088d2d2013-10-24 21:05:08 +00001960 // Iterate over each compile unit and set the size and offsets for each
1961 // DIE within each compile unit. All offsets are CU relative.
David Blaikie319a05f2013-12-02 19:33:10 +00001962 for (SmallVectorImpl<Unit *>::const_iterator I = CUs.begin(), E = CUs.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001963 I != E; ++I) {
Manman Rence20d462013-10-29 22:57:10 +00001964 (*I)->setDebugInfoOffset(SecOffset);
1965
1966 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001967 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
David Blaikie6b288cf2013-10-30 20:42:41 +00001968 (*I)->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001969
1970 // EndOffset here is CU-relative, after laying out
1971 // all of the CU DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001972 unsigned EndOffset = computeSizeAndOffset((*I)->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001973 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001974 }
Bill Wendling480ff322009-05-20 23:21:38 +00001975}
1976
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001977// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001978void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001979 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001980
Bill Wendling480ff322009-05-20 23:21:38 +00001981 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001982 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001983 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001984 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001985 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001986 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001987 DwarfAbbrevDWOSectionSym = emitSectionSym(
1988 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001989 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001990
Chris Lattner6629ca92010-04-04 22:59:04 +00001991 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001992 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001993
Eric Christopher74804332013-02-07 21:19:50 +00001994 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001995 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001996 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001997 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001998 DwarfGnuPubNamesSectionSym =
1999 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
2000 DwarfGnuPubTypesSectionSym =
2001 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00002002 } else if (HasDwarfPubSections) {
2003 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
2004 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00002005 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002006
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002007 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002008 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00002009 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002010 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002011 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00002012 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002013 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00002014 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002015 DwarfDebugRangeSectionSym =
2016 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00002017
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002018 DwarfDebugLocSectionSym =
2019 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Devang Patel9fc11702010-05-25 23:40:22 +00002020
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002021 TextSectionSym = emitSectionSym(Asm, TLOF.getTextSection(), "text_begin");
2022 emitSectionSym(Asm, TLOF.getDataSection());
Bill Wendling480ff322009-05-20 23:21:38 +00002023}
2024
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002025// Recursively emits a debug information entry.
David Blaikie2d4e1122013-10-30 17:14:24 +00002026void DwarfDebug::emitDIE(DIE *Die, ArrayRef<DIEAbbrev *> Abbrevs) {
Bill Wendling480ff322009-05-20 23:21:38 +00002027 // Get the abbreviation for this DIE.
2028 unsigned AbbrevNumber = Die->getAbbrevNumber();
David Blaikie2d4e1122013-10-30 17:14:24 +00002029 const DIEAbbrev *Abbrev = Abbrevs[AbbrevNumber - 1];
Bill Wendling480ff322009-05-20 23:21:38 +00002030
Bill Wendling480ff322009-05-20 23:21:38 +00002031 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00002032 if (Asm->isVerbose())
Chris Lattnerfa823552010-01-22 23:18:42 +00002033 Asm->OutStreamer.AddComment("Abbrev [" + Twine(AbbrevNumber) + "] 0x" +
2034 Twine::utohexstr(Die->getOffset()) + ":0x" +
2035 Twine::utohexstr(Die->getSize()) + " " +
2036 dwarf::TagString(Abbrev->getTag()));
Chris Lattner9efd1182010-04-04 19:09:29 +00002037 Asm->EmitULEB128(AbbrevNumber);
Bill Wendling480ff322009-05-20 23:21:38 +00002038
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002039 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
Eric Christopher4887c8f2013-03-29 23:34:06 +00002040 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev->getData();
Bill Wendling480ff322009-05-20 23:21:38 +00002041
2042 // Emit the DIE attribute values.
2043 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00002044 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
2045 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00002046 assert(Form && "Too many attributes for DIE (check abbreviation)");
2047
Chris Lattner7bde8c02010-04-04 18:52:31 +00002048 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00002049 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002050
Bill Wendling480ff322009-05-20 23:21:38 +00002051 switch (Attr) {
Manman Rence20d462013-10-29 22:57:10 +00002052 case dwarf::DW_AT_abstract_origin:
2053 case dwarf::DW_AT_type:
2054 case dwarf::DW_AT_friend:
2055 case dwarf::DW_AT_specification:
2056 case dwarf::DW_AT_import:
2057 case dwarf::DW_AT_containing_type: {
2058 DIEEntry *E = cast<DIEEntry>(Values[i]);
2059 DIE *Origin = E->getEntry();
2060 unsigned Addr = Origin->getOffset();
2061 if (Form == dwarf::DW_FORM_ref_addr) {
2062 assert(!useSplitDwarf() && "TODO: dwo files can't have relocations.");
2063 // For DW_FORM_ref_addr, output the offset from beginning of debug info
2064 // section. Origin->getOffset() returns the offset from start of the
2065 // compile unit.
David Blaikie409dd9c2013-11-19 23:08:21 +00002066 CompileUnit *CU = CUDieMap.lookup(Origin->getUnit());
Manman Rence20d462013-10-29 22:57:10 +00002067 assert(CU && "CUDie should belong to a CU.");
2068 Addr += CU->getDebugInfoOffset();
2069 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2070 Asm->EmitLabelPlusOffset(DwarfInfoSectionSym, Addr,
2071 DIEEntry::getRefAddrSize(Asm));
2072 else
Manman Ren251a1bd2013-10-29 23:14:15 +00002073 Asm->EmitLabelOffsetDifference(DwarfInfoSectionSym, Addr,
2074 DwarfInfoSectionSym,
2075 DIEEntry::getRefAddrSize(Asm));
Manman Rence20d462013-10-29 22:57:10 +00002076 } else {
Manman Ren4dbdc902013-10-31 17:54:35 +00002077 // Make sure Origin belong to the same CU.
David Blaikie409dd9c2013-11-19 23:08:21 +00002078 assert(Die->getUnit() == Origin->getUnit() &&
Manman Ren4dbdc902013-10-31 17:54:35 +00002079 "The referenced DIE should belong to the same CU in ref4");
Manman Rence20d462013-10-29 22:57:10 +00002080 Asm->EmitInt32(Addr);
2081 }
2082 break;
2083 }
Devang Patel12563b32010-04-16 23:33:45 +00002084 case dwarf::DW_AT_ranges: {
2085 // DW_AT_range Value encodes offset in debug_range section.
Eric Christopher4751d702013-11-23 00:05:29 +00002086 DIELabel *V = cast<DIELabel>(Values[i]);
Devang Patelda3ef852010-09-02 16:43:44 +00002087
Eric Christopher4751d702013-11-23 00:05:29 +00002088 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2089 Asm->EmitSectionOffset(V->getValue(), DwarfDebugRangeSectionSym);
2090 else
2091 Asm->EmitLabelDifference(V->getValue(), DwarfDebugRangeSectionSym, 4);
Devang Patel12563b32010-04-16 23:33:45 +00002092 break;
2093 }
Devang Patel9fc11702010-05-25 23:40:22 +00002094 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00002095 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
2096 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherd0b82ae2013-11-16 00:18:40 +00002097 Asm->EmitSectionOffset(L->getValue(), DwarfDebugLocSectionSym);
Nick Lewycky33da3362012-06-22 01:25:12 +00002098 else
Ulrich Weigand396ba8b2013-07-02 18:46:26 +00002099 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Nick Lewycky33da3362012-06-22 01:25:12 +00002100 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00002101 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00002102 }
Devang Patel9fc11702010-05-25 23:40:22 +00002103 break;
2104 }
Devang Patela1bd5a12010-09-29 19:08:08 +00002105 case dwarf::DW_AT_accessibility: {
2106 if (Asm->isVerbose()) {
2107 DIEInteger *V = cast<DIEInteger>(Values[i]);
2108 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2109 }
2110 Values[i]->EmitValue(Asm, Form);
2111 break;
2112 }
Bill Wendling480ff322009-05-20 23:21:38 +00002113 default:
2114 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002115 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002116 break;
2117 }
Bill Wendling480ff322009-05-20 23:21:38 +00002118 }
2119
2120 // Emit the DIE children if any.
2121 if (Abbrev->getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
2122 const std::vector<DIE *> &Children = Die->getChildren();
2123
2124 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher3c5a1912012-12-19 22:02:53 +00002125 emitDIE(Children[j], Abbrevs);
Bill Wendling480ff322009-05-20 23:21:38 +00002126
Chris Lattner7bde8c02010-04-04 18:52:31 +00002127 if (Asm->isVerbose())
Chris Lattner566cae92010-03-09 23:52:58 +00002128 Asm->OutStreamer.AddComment("End Of Children Mark");
2129 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002130 }
2131}
2132
Eric Christophera2de8262012-12-15 00:04:07 +00002133// Emit the various dwarf units to the unit section USection with
2134// the abbreviations going into ASection.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002135void DwarfUnits::emitUnits(DwarfDebug *DD, const MCSection *USection,
Eric Christophera2de8262012-12-15 00:04:07 +00002136 const MCSection *ASection,
2137 const MCSymbol *ASectionSym) {
2138 Asm->OutStreamer.SwitchSection(USection);
David Blaikie319a05f2013-12-02 19:33:10 +00002139 for (SmallVectorImpl<Unit *>::iterator I = CUs.begin(), E = CUs.end(); I != E;
2140 ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002141 Unit *TheU = *I;
2142 DIE *Die = TheU->getUnitDie();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002143
Devang Patel1a0df9a2010-05-10 22:49:55 +00002144 // Emit the compile units header.
David Blaikie2a80e442013-12-02 22:09:48 +00002145 Asm->OutStreamer.EmitLabel(
2146 Asm->GetTempSymbol(USection->getLabelBeginName(), TheU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002147
Devang Patel1a0df9a2010-05-10 22:49:55 +00002148 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00002149 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00002150 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002151
David Blaikie2a80e442013-12-02 22:09:48 +00002152 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002153
Eric Christopher3c5a1912012-12-19 22:02:53 +00002154 DD->emitDIE(Die, Abbreviations);
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002155 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002156 Asm->GetTempSymbol(USection->getLabelEndName(), TheU->getUniqueID()));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002157 }
Bill Wendling480ff322009-05-20 23:21:38 +00002158}
2159
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002160// Emit the debug info section.
2161void DwarfDebug::emitDebugInfo() {
Eric Christophera2de8262012-12-15 00:04:07 +00002162 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2163
2164 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoSection(),
2165 Asm->getObjFileLowering().getDwarfAbbrevSection(),
2166 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002167}
2168
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002169// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002170void DwarfDebug::emitAbbreviations() {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002171 if (!useSplitDwarf())
2172 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection(),
2173 &Abbreviations);
2174 else
2175 emitSkeletonAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
2176}
Bill Wendling480ff322009-05-20 23:21:38 +00002177
Eric Christopher3c5a1912012-12-19 22:02:53 +00002178void DwarfDebug::emitAbbrevs(const MCSection *Section,
2179 std::vector<DIEAbbrev *> *Abbrevs) {
2180 // Check to see if it is worth the effort.
2181 if (!Abbrevs->empty()) {
2182 // Start the debug abbrev section.
2183 Asm->OutStreamer.SwitchSection(Section);
2184
2185 MCSymbol *Begin = Asm->GetTempSymbol(Section->getLabelBeginName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002186 Asm->OutStreamer.EmitLabel(Begin);
Bill Wendling480ff322009-05-20 23:21:38 +00002187
2188 // For each abbrevation.
Eric Christopher3c5a1912012-12-19 22:02:53 +00002189 for (unsigned i = 0, N = Abbrevs->size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00002190 // Get abbreviation data
Eric Christopher3c5a1912012-12-19 22:02:53 +00002191 const DIEAbbrev *Abbrev = Abbrevs->at(i);
Bill Wendling480ff322009-05-20 23:21:38 +00002192
2193 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002194 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002195
2196 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002197 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002198 }
2199
2200 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002201 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002202
Eric Christopher3c5a1912012-12-19 22:02:53 +00002203 MCSymbol *End = Asm->GetTempSymbol(Section->getLabelEndName());
Eric Christopher996b2b72012-12-13 03:00:38 +00002204 Asm->OutStreamer.EmitLabel(End);
Bill Wendling480ff322009-05-20 23:21:38 +00002205 }
2206}
2207
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002208// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002209void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002210 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002211 Asm->OutStreamer.AddComment("Extended Op");
2212 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002213
Chris Lattner566cae92010-03-09 23:52:58 +00002214 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002215 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002216 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2217 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2218
2219 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002220
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002221 Asm->OutStreamer.EmitSymbolValue(
2222 Asm->GetTempSymbol("section_end", SectionEnd),
2223 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002224
2225 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002226 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2227 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002228 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002229 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002230}
2231
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002232// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002233void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002234 DwarfAccelTable AT(
2235 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002236 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2237 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002238 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002239 Unit *TheU = *I;
2240 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelNames();
David Blaikie2ea848b2013-11-19 22:51:04 +00002241 for (StringMap<std::vector<const DIE *> >::const_iterator
2242 GI = Names.begin(),
2243 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002244 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002245 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002246 const std::vector<const DIE *> &Entities = GI->second;
2247 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2248 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002249 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002250 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002251 }
2252 }
2253
2254 AT.FinalizeTable(Asm, "Names");
2255 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002256 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002257 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2258 Asm->OutStreamer.EmitLabel(SectionBegin);
2259
2260 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002261 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002262}
2263
Eric Christopher48fef592012-12-20 21:58:40 +00002264// Emit objective C classes and categories into a hashed accelerator table
2265// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002266void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002267 DwarfAccelTable AT(
2268 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002269 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2270 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002271 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002272 Unit *TheU = *I;
2273 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelObjC();
David Blaikie2ea848b2013-11-19 22:51:04 +00002274 for (StringMap<std::vector<const DIE *> >::const_iterator
2275 GI = Names.begin(),
2276 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002277 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002278 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002279 const std::vector<const DIE *> &Entities = GI->second;
2280 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2281 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002282 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002283 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002284 }
2285 }
2286
2287 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002288 Asm->OutStreamer.SwitchSection(
2289 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002290 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2291 Asm->OutStreamer.EmitLabel(SectionBegin);
2292
2293 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002294 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002295}
2296
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002297// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002298void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002299 DwarfAccelTable AT(
2300 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
David Blaikie319a05f2013-12-02 19:33:10 +00002301 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2302 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002303 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002304 Unit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002305 const StringMap<std::vector<const DIE *> > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002306 TheU->getAccelNamespace();
David Blaikie2ea848b2013-11-19 22:51:04 +00002307 for (StringMap<std::vector<const DIE *> >::const_iterator
2308 GI = Names.begin(),
2309 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002310 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002311 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002312 const std::vector<const DIE *> &Entities = GI->second;
2313 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2314 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002315 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002316 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002317 }
2318 }
2319
2320 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002321 Asm->OutStreamer.SwitchSection(
2322 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002323 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2324 Asm->OutStreamer.EmitLabel(SectionBegin);
2325
2326 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002327 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002328}
2329
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002330// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002331void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002332 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002333 Atoms.push_back(
2334 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2335 Atoms.push_back(
2336 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2337 Atoms.push_back(
2338 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002339 DwarfAccelTable AT(Atoms);
David Blaikie319a05f2013-12-02 19:33:10 +00002340 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2341 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002342 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002343 Unit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002344 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002345 TheU->getAccelTypes();
David Blaikie2ea848b2013-11-19 22:51:04 +00002346 for (StringMap<
2347 std::vector<std::pair<const DIE *, unsigned> > >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002348 GI = Names.begin(),
2349 GE = Names.end();
2350 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002351 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002352 const std::vector<std::pair<const DIE *, unsigned> > &Entities =
2353 GI->second;
2354 for (std::vector<std::pair<const DIE *, unsigned> >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002355 DI = Entities.begin(),
2356 DE = Entities.end();
2357 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002358 AT.AddName(Name, DI->first, DI->second);
Eric Christopher4996c702011-11-07 09:24:32 +00002359 }
2360 }
2361
2362 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002363 Asm->OutStreamer.SwitchSection(
2364 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002365 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2366 Asm->OutStreamer.EmitLabel(SectionBegin);
2367
2368 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002369 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002370}
2371
Eric Christopherdd1a0122013-09-13 00:35:05 +00002372// Public name handling.
2373// The format for the various pubnames:
2374//
2375// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2376// for the DIE that is named.
2377//
2378// gnu pubnames - offset/index value/name tuples where the offset is the offset
2379// into the CU and the index value is computed according to the type of value
2380// for the DIE that is named.
2381//
2382// For type units the offset is the offset of the skeleton DIE. For split dwarf
2383// it's the offset within the debug_info/debug_types dwo section, however, the
2384// reference in the pubname header doesn't change.
2385
2386/// computeIndexValue - Compute the gdb index value for the DIE and CU.
David Blaikie319a05f2013-12-02 19:33:10 +00002387static dwarf::PubIndexEntryDescriptor computeIndexValue(Unit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002388 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002389 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2390
2391 // We could have a specification DIE that has our most of our knowledge,
2392 // look for that now.
2393 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2394 if (SpecVal) {
2395 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2396 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2397 Linkage = dwarf::GIEL_EXTERNAL;
2398 } else if (Die->findAttribute(dwarf::DW_AT_external))
2399 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002400
2401 switch (Die->getTag()) {
2402 case dwarf::DW_TAG_class_type:
2403 case dwarf::DW_TAG_structure_type:
2404 case dwarf::DW_TAG_union_type:
2405 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002406 return dwarf::PubIndexEntryDescriptor(
2407 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2408 ? dwarf::GIEL_STATIC
2409 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002410 case dwarf::DW_TAG_typedef:
2411 case dwarf::DW_TAG_base_type:
2412 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002413 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002414 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002415 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002416 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002417 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002418 case dwarf::DW_TAG_constant:
2419 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002420 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002421 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002422 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2423 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002424 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002425 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002426 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002427}
2428
Eric Christopher5f93bb92013-09-09 20:03:17 +00002429/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002430///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002431void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002432 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopherdd1a0122013-09-13 00:35:05 +00002433 const MCSection *PSec =
2434 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2435 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002436
David Blaikie319a05f2013-12-02 19:33:10 +00002437 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2438 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +00002439 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002440 Unit *TheU = *I;
2441 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002442
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002443 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002444 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002445
Eric Christopher39eebfa2013-09-30 23:14:16 +00002446 // Emit a label so we can reference the beginning of this pubname section.
2447 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002448 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002449 Asm->GetTempSymbol("gnu_pubnames", TheU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002450
Eric Christopherdd1a0122013-09-13 00:35:05 +00002451 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002452 Asm->OutStreamer.AddComment("Length of Public Names Info");
2453 Asm->EmitLabelDifference(Asm->GetTempSymbol("pubnames_end", ID),
2454 Asm->GetTempSymbol("pubnames_begin", ID), 4);
2455
2456 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_begin", ID));
2457
2458 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002459 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002460
2461 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
2462 Asm->EmitSectionOffset(Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2463 DwarfInfoSectionSym);
2464
2465 Asm->OutStreamer.AddComment("Compilation Unit Length");
2466 Asm->EmitLabelDifference(Asm->GetTempSymbol(ISec->getLabelEndName(), ID),
2467 Asm->GetTempSymbol(ISec->getLabelBeginName(), ID),
2468 4);
2469
Eric Christopherdd1a0122013-09-13 00:35:05 +00002470 // Emit the pubnames for this compilation unit.
David Blaikie2a80e442013-12-02 22:09:48 +00002471 const StringMap<const DIE *> &Globals = TheU->getGlobalNames();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002472 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2473 GE = Globals.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002474 GI != GE; ++GI) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002475 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002476 const DIE *Entity = GI->second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002477
2478 Asm->OutStreamer.AddComment("DIE offset");
2479 Asm->EmitInt32(Entity->getOffset());
2480
Eric Christopherdd1a0122013-09-13 00:35:05 +00002481 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002482 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002483 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002484 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002485 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002486 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002487 }
2488
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002489 if (Asm->isVerbose())
2490 Asm->OutStreamer.AddComment("External Name");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002491 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002492 }
2493
2494 Asm->OutStreamer.AddComment("End Mark");
2495 Asm->EmitInt32(0);
2496 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("pubnames_end", ID));
2497 }
2498}
2499
Eric Christopherdd1a0122013-09-13 00:35:05 +00002500void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher8b3737f2013-09-13 00:34:58 +00002501 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
Eric Christopher261d2342013-09-23 20:55:35 +00002502 const MCSection *PSec =
2503 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2504 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002505
David Blaikie319a05f2013-12-02 19:33:10 +00002506 for (SmallVectorImpl<Unit *>::const_iterator I = getUnits().begin(),
2507 E = getUnits().end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002508 I != E; ++I) {
David Blaikie2a80e442013-12-02 22:09:48 +00002509 Unit *TheU = *I;
Eric Christopher6abc9c52011-11-07 09:18:35 +00002510 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002511 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002512
2513 // Emit a label so we can reference the beginning of this pubtype section.
2514 if (GnuStyle)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002515 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002516 Asm->GetTempSymbol("gnu_pubtypes", TheU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002517
2518 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002519 Asm->OutStreamer.AddComment("Length of Public Types Info");
2520 Asm->EmitLabelDifference(
David Blaikie2a80e442013-12-02 22:09:48 +00002521 Asm->GetTempSymbol("pubtypes_end", TheU->getUniqueID()),
2522 Asm->GetTempSymbol("pubtypes_begin", TheU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002523
Eric Christopher8b3737f2013-09-13 00:34:58 +00002524 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002525 Asm->GetTempSymbol("pubtypes_begin", TheU->getUniqueID()));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002526
Eric Christopher8b3737f2013-09-13 00:34:58 +00002527 if (Asm->isVerbose())
2528 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002529 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002530
Devang Patel1a0df9a2010-05-10 22:49:55 +00002531 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002532 Asm->EmitSectionOffset(
David Blaikie2a80e442013-12-02 22:09:48 +00002533 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheU->getUniqueID()),
Eric Christopher8b3737f2013-09-13 00:34:58 +00002534 DwarfInfoSectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002535
Devang Patel1a0df9a2010-05-10 22:49:55 +00002536 Asm->OutStreamer.AddComment("Compilation Unit Length");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002537 Asm->EmitLabelDifference(
David Blaikie2a80e442013-12-02 22:09:48 +00002538 Asm->GetTempSymbol(ISec->getLabelEndName(), TheU->getUniqueID()),
2539 Asm->GetTempSymbol(ISec->getLabelBeginName(), TheU->getUniqueID()), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002540
Eric Christopher39eebfa2013-09-30 23:14:16 +00002541 // Emit the pubtypes.
David Blaikie2a80e442013-12-02 22:09:48 +00002542 const StringMap<const DIE *> &Globals = TheU->getGlobalTypes();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002543 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2544 GE = Globals.end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002545 GI != GE; ++GI) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002546 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002547 const DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002548
Eric Christopher8b3737f2013-09-13 00:34:58 +00002549 if (Asm->isVerbose())
2550 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002551 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002552
Eric Christopherdd1a0122013-09-13 00:35:05 +00002553 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002554 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002555 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002556 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002557 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002558 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002559 }
2560
Eric Christopher8b3737f2013-09-13 00:34:58 +00002561 if (Asm->isVerbose())
2562 Asm->OutStreamer.AddComment("External Name");
2563
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002564 // Emit the name with a terminating null byte.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002565 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002566 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002567
Devang Patel1a0df9a2010-05-10 22:49:55 +00002568 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002569 Asm->EmitInt32(0);
Eric Christopher8b3737f2013-09-13 00:34:58 +00002570 Asm->OutStreamer.EmitLabel(
David Blaikie2a80e442013-12-02 22:09:48 +00002571 Asm->GetTempSymbol("pubtypes_end", TheU->getUniqueID()));
Devang Patel04d2f2d2009-11-24 01:14:22 +00002572 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002573}
2574
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002575// Emit strings into a string section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002576void DwarfUnits::emitStrings(const MCSection *StrSection,
2577 const MCSection *OffsetSection = NULL,
2578 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002579
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002580 if (StringPool.empty())
2581 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002582
Chris Lattner3d72a672010-03-09 23:38:23 +00002583 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002584 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002585
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002586 // Get all of the string pool entries and put them in an array by their ID so
2587 // we can sort them.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002588 SmallVector<
2589 std::pair<unsigned, StringMapEntry<std::pair<MCSymbol *, unsigned> > *>,
2590 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002591
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002592 for (StringMap<std::pair<MCSymbol *, unsigned> >::iterator
2593 I = StringPool.begin(),
2594 E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002595 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002596 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002597
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002598 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002599
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002600 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002601 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002602 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002603
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002604 // Emit the string itself with a terminating null byte.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002605 Asm->OutStreamer.EmitBytes(
2606 StringRef(Entries[i].second->getKeyData(),
2607 Entries[i].second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002608 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002609
2610 // If we've got an offset section go ahead and emit that now as well.
2611 if (OffsetSection) {
2612 Asm->OutStreamer.SwitchSection(OffsetSection);
2613 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002614 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002615 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002616 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002617 offset += Entries[i].second->getKeyLength() + 1;
2618 }
2619 }
Bill Wendling480ff322009-05-20 23:21:38 +00002620}
2621
Eric Christopher65132a82013-11-19 09:11:26 +00002622
2623// Emit addresses into the section given.
Eric Christopher962c9082013-01-15 23:56:56 +00002624void DwarfUnits::emitAddresses(const MCSection *AddrSection) {
2625
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002626 if (AddressPool.empty())
2627 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002628
2629 // Start the dwarf addr section.
2630 Asm->OutStreamer.SwitchSection(AddrSection);
2631
David Blaikiece1960f2013-07-08 17:51:28 +00002632 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002633 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002634
David Blaikiece1960f2013-07-08 17:51:28 +00002635 for (DenseMap<const MCExpr *, unsigned>::iterator I = AddressPool.begin(),
2636 E = AddressPool.end();
Eric Christopher962c9082013-01-15 23:56:56 +00002637 I != E; ++I)
David Blaikieac569a62013-07-08 17:33:10 +00002638 Entries[I->second] = I->first;
Eric Christopher962c9082013-01-15 23:56:56 +00002639
2640 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002641 // Emit an expression for reference from debug information entries.
David Blaikieac569a62013-07-08 17:33:10 +00002642 if (const MCExpr *Expr = Entries[i])
Ulrich Weigand8b3d2262013-07-02 18:46:46 +00002643 Asm->OutStreamer.EmitValue(Expr, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002644 else
2645 Asm->OutStreamer.EmitIntValue(0, Asm->getDataLayout().getPointerSize());
2646 }
Eric Christopher962c9082013-01-15 23:56:56 +00002647}
2648
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002649// Emit visible names into a debug str section.
2650void DwarfDebug::emitDebugStr() {
2651 DwarfUnits &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
2652 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2653}
2654
Eric Christopher9046f942013-07-02 21:36:07 +00002655// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002656void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002657 if (DotDebugLocEntries.empty())
2658 return;
2659
Eric Christopher4887c8f2013-03-29 23:34:06 +00002660 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002661 I = DotDebugLocEntries.begin(),
2662 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002663 I != E; ++I) {
2664 DotDebugLocEntry &Entry = *I;
2665 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002666 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002667 }
2668
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002669 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002670 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002671 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002672 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002673 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2674 unsigned index = 1;
Eric Christopher4887c8f2013-03-29 23:34:06 +00002675 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002676 I = DotDebugLocEntries.begin(),
2677 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002678 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002679 DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002680 if (Entry.isMerged())
2681 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002682 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002683 Asm->OutStreamer.EmitIntValue(0, Size);
2684 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002685 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002686 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002687 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2688 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2689 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002690 Asm->OutStreamer.AddComment("Loc expr size");
2691 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2692 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2693 Asm->EmitLabelDifference(end, begin, 2);
2694 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002695 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002696 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002697 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2698 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002699 Asm->OutStreamer.AddComment("DW_OP_consts");
2700 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002701 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002702 } else {
2703 Asm->OutStreamer.AddComment("DW_OP_constu");
2704 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002705 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002706 }
Devang Pateled9fd452011-07-08 16:49:43 +00002707 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002708 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002709 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002710 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002711 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002712 else {
2713 // Complex address entry.
2714 unsigned N = DV.getNumAddrElements();
2715 unsigned i = 0;
2716 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002717 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002718 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002719 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002720 Asm->OutStreamer.AddComment("DW_OP_deref");
2721 Asm->EmitInt8(dwarf::DW_OP_deref);
2722 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2723 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2724 Asm->EmitSLEB128(DV.getAddrElement(1));
2725 } else {
2726 // If first address element is OpPlus then emit
2727 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002728 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2729 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002730 i = 2;
2731 }
2732 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002733 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002734 }
Eric Christopher6a841382012-11-19 22:42:10 +00002735
Devang Pateled9fd452011-07-08 16:49:43 +00002736 // Emit remaining complex address elements.
2737 for (; i < N; ++i) {
2738 uint64_t Element = DV.getAddrElement(i);
2739 if (Element == DIBuilder::OpPlus) {
2740 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2741 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002742 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002743 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002744 Asm->EmitInt8(dwarf::DW_OP_deref);
2745 } else
2746 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002747 }
Devang Patel3e021532011-04-28 02:22:40 +00002748 }
Devang Patel3e021532011-04-28 02:22:40 +00002749 }
Devang Pateled9fd452011-07-08 16:49:43 +00002750 // else ... ignore constant fp. There is not any good way to
2751 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002752 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002753 }
2754 }
Bill Wendling480ff322009-05-20 23:21:38 +00002755}
2756
Richard Mitton21101b32013-09-19 23:21:01 +00002757struct SymbolCUSorter {
2758 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2759 const MCStreamer &Streamer;
2760
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002761 bool operator()(const SymbolCU &A, const SymbolCU &B) {
Richard Mitton21101b32013-09-19 23:21:01 +00002762 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2763 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2764
2765 // Symbols with no order assigned should be placed at the end.
2766 // (e.g. section end labels)
2767 if (IA == 0)
2768 IA = (unsigned)(-1);
2769 if (IB == 0)
2770 IB = (unsigned)(-1);
2771 return IA < IB;
2772 }
2773};
2774
David Blaikie319a05f2013-12-02 19:33:10 +00002775static bool CUSort(const Unit *A, const Unit *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002776 return (A->getUniqueID() < B->getUniqueID());
Richard Mitton21101b32013-09-19 23:21:01 +00002777}
2778
2779struct ArangeSpan {
2780 const MCSymbol *Start, *End;
2781};
2782
2783// Emit a debug aranges section, containing a CU lookup for any
2784// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002785void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002786 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002787 Asm->OutStreamer.SwitchSection(
2788 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002789
2790 typedef DenseMap<CompileUnit *, std::vector<ArangeSpan> > SpansType;
2791
2792 SpansType Spans;
2793
2794 // Build a list of sections used.
2795 std::vector<const MCSection *> Sections;
2796 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
2797 it++) {
2798 const MCSection *Section = it->first;
2799 Sections.push_back(Section);
2800 }
2801
2802 // Sort the sections into order.
2803 // This is only done to ensure consistent output order across different runs.
2804 std::sort(Sections.begin(), Sections.end(), SectionSort);
2805
2806 // Build a set of address spans, sorted by CU.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002807 for (size_t SecIdx = 0; SecIdx < Sections.size(); SecIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002808 const MCSection *Section = Sections[SecIdx];
2809 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2810 if (List.size() < 2)
2811 continue;
2812
2813 // Sort the symbols by offset within the section.
2814 SymbolCUSorter sorter(Asm->OutStreamer);
2815 std::sort(List.begin(), List.end(), sorter);
2816
2817 // If we have no section (e.g. common), just write out
2818 // individual spans for each symbol.
2819 if (Section == NULL) {
2820 for (size_t n = 0; n < List.size(); n++) {
2821 const SymbolCU &Cur = List[n];
2822
2823 ArangeSpan Span;
2824 Span.Start = Cur.Sym;
2825 Span.End = NULL;
2826 if (Cur.CU)
2827 Spans[Cur.CU].push_back(Span);
2828 }
2829 } else {
2830 // Build spans between each label.
2831 const MCSymbol *StartSym = List[0].Sym;
2832 for (size_t n = 1; n < List.size(); n++) {
2833 const SymbolCU &Prev = List[n - 1];
2834 const SymbolCU &Cur = List[n];
2835
2836 // Try and build the longest span we can within the same CU.
2837 if (Cur.CU != Prev.CU) {
2838 ArangeSpan Span;
2839 Span.Start = StartSym;
2840 Span.End = Cur.Sym;
2841 Spans[Prev.CU].push_back(Span);
2842 StartSym = Cur.Sym;
2843 }
2844 }
2845 }
2846 }
2847
2848 const MCSection *ISec = Asm->getObjFileLowering().getDwarfInfoSection();
2849 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2850
2851 // Build a list of CUs used.
2852 std::vector<CompileUnit *> CUs;
2853 for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
2854 CompileUnit *CU = it->first;
2855 CUs.push_back(CU);
2856 }
2857
2858 // Sort the CU list (again, to ensure consistent output order).
2859 std::sort(CUs.begin(), CUs.end(), CUSort);
2860
2861 // Emit an arange table for each CU we used.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002862 for (size_t CUIdx = 0; CUIdx < CUs.size(); CUIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002863 CompileUnit *CU = CUs[CUIdx];
2864 std::vector<ArangeSpan> &List = Spans[CU];
2865
2866 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002867 unsigned ContentSize =
2868 sizeof(int16_t) + // DWARF ARange version number
2869 sizeof(int32_t) + // Offset of CU in the .debug_info section
2870 sizeof(int8_t) + // Pointer Size (in bytes)
2871 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002872
2873 unsigned TupleSize = PtrSize * 2;
2874
2875 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
2876 unsigned Padding = 0;
2877 while (((sizeof(int32_t) + ContentSize + Padding) % TupleSize) != 0)
2878 Padding++;
2879
2880 ContentSize += Padding;
2881 ContentSize += (List.size() + 1) * TupleSize;
2882
2883 // For each compile unit, write the list of spans it covers.
2884 Asm->OutStreamer.AddComment("Length of ARange Set");
2885 Asm->EmitInt32(ContentSize);
2886 Asm->OutStreamer.AddComment("DWARF Arange version number");
2887 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2888 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
2889 Asm->EmitSectionOffset(
2890 Asm->GetTempSymbol(ISec->getLabelBeginName(), CU->getUniqueID()),
2891 DwarfInfoSectionSym);
2892 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2893 Asm->EmitInt8(PtrSize);
2894 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2895 Asm->EmitInt8(0);
2896
2897 for (unsigned n = 0; n < Padding; n++)
2898 Asm->EmitInt8(0xff);
2899
2900 for (unsigned n = 0; n < List.size(); n++) {
2901 const ArangeSpan &Span = List[n];
2902 Asm->EmitLabelReference(Span.Start, PtrSize);
2903
2904 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002905 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002906 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002907 } else {
2908 // For symbols without an end marker (e.g. common), we
2909 // write a single arange entry containing just that one symbol.
2910 uint64_t Size = SymSize[Span.Start];
2911 if (Size == 0)
2912 Size = 1;
2913
2914 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2915 }
Richard Mitton21101b32013-09-19 23:21:01 +00002916 }
2917
2918 Asm->OutStreamer.AddComment("ARange terminator");
2919 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2920 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2921 }
Bill Wendling480ff322009-05-20 23:21:38 +00002922}
2923
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002924// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002925void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002926 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002927 Asm->OutStreamer.SwitchSection(
2928 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002929
Eric Christopher0f63d062013-12-03 00:45:45 +00002930 // Size for our labels.
2931 unsigned char Size = Asm->getDataLayout().getPointerSize();
2932
2933 // Grab the specific ranges for the compile units in the module.
2934 for (DenseMap<const MDNode *, CompileUnit *>::iterator I = CUMap.begin(),
2935 E = CUMap.end();
2936 I != E; ++I) {
2937 CompileUnit *TheCU = I->second;
2938 unsigned ID = TheCU->getUniqueID();
2939
2940 // Emit a symbol so we can find the beginning of our ranges.
2941 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_ranges", ID));
2942
2943 // Iterate over the misc ranges for the compile units in the module.
2944 const SmallVectorImpl<RangeSpanList *> &RangeLists = TheCU->getRangeLists();
2945 for (SmallVectorImpl<RangeSpanList *>::const_iterator
2946 I = RangeLists.begin(),
2947 E = RangeLists.end();
2948 I != E; ++I) {
2949 RangeSpanList *List = *I;
2950
2951 // Emit a symbol so we can find the beginning of the range.
2952 Asm->OutStreamer.EmitLabel(
2953 Asm->GetTempSymbol("debug_ranges", List->getIndex()));
2954
2955 for (SmallVectorImpl<RangeSpan>::const_iterator
2956 I = List->getRanges().begin(),
2957 E = List->getRanges().end();
2958 I != E; ++I) {
2959 RangeSpan Range = *I;
2960 // We occasionally have ranges without begin/end labels.
2961 // FIXME: Verify and fix.
2962 const MCSymbol *Begin = Range.getStart();
2963 const MCSymbol *End = Range.getEnd();
2964 Begin ? Asm->OutStreamer.EmitSymbolValue(Begin, Size)
2965 : Asm->OutStreamer.EmitIntValue(0, Size);
2966 End ? Asm->OutStreamer.EmitSymbolValue(End, Size)
2967 : Asm->OutStreamer.EmitIntValue(0, Size);
2968 }
2969
2970 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002971 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002972 Asm->OutStreamer.EmitIntValue(0, Size);
2973 }
Devang Patel12563b32010-04-16 23:33:45 +00002974 }
Bill Wendling480ff322009-05-20 23:21:38 +00002975}
2976
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002977// Emit visible names into a debug macinfo section.
Devang Patel930143b2009-11-21 02:48:08 +00002978void DwarfDebug::emitDebugMacInfo() {
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00002979 if (const MCSection *LineInfo =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002980 Asm->getObjFileLowering().getDwarfMacroInfoSection()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002981 // Start the dwarf macinfo section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002982 Asm->OutStreamer.SwitchSection(LineInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00002983 }
2984}
2985
Eric Christopherd692c1d2012-12-11 19:42:09 +00002986// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002987
2988// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2989// 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 +00002990// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher6fdf3242013-08-26 23:50:43 +00002991CompileUnit *DwarfDebug::constructSkeletonCU(const CompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002992
2993 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
David Blaikieb01f13e2013-11-15 23:54:45 +00002994 CompileUnit *NewCU = new CompileUnit(CU->getUniqueID(), Die, CU->getNode(),
2995 Asm, this, &SkeletonHolder);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002996
Eric Christopherdae389b2013-02-22 23:50:08 +00002997 NewCU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikieb01f13e2013-11-15 23:54:45 +00002998 CU->getNode().getSplitDebugFilename());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002999
Eric Christopher44c6aa62013-04-22 07:51:08 +00003000 // Relocate to the beginning of the addr_base section, else 0 for the
3001 // beginning of the one for this compile unit.
Eric Christopher55863be2013-04-07 03:43:09 +00003002 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003003 NewCU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base,
3004 DwarfAddrSectionSym);
Eric Christopher55863be2013-04-07 03:43:09 +00003005 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003006 NewCU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003007
3008 // 2.17.1 requires that we use DW_AT_low_pc for a single entry point
Eric Christopher962c9082013-01-15 23:56:56 +00003009 // into an entity. We're using 0, or a NULL label for this.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003010 NewCU->addUInt(Die, dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
Eric Christopher962c9082013-01-15 23:56:56 +00003011
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003012 // DW_AT_stmt_list is a offset of line number information for this
3013 // compile unit in debug_line section.
Eric Christopher55863be2013-04-07 03:43:09 +00003014 // FIXME: Should handle multiple compile units.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003015 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003016 NewCU->addSectionLabel(Die, dwarf::DW_AT_stmt_list,
3017 DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003018 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003019 NewCU->addSectionOffset(Die, dwarf::DW_AT_stmt_list, 0);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003020
3021 if (!CompilationDir.empty())
Eric Christopher2cbd5762013-01-07 19:32:41 +00003022 NewCU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003023
Eric Christopher1d06eb52013-10-01 00:43:31 +00003024 // Flags to let the linker know we have emitted new style pubnames.
Eric Christopher39eebfa2013-09-30 23:14:16 +00003025 if (GenerateGnuPubSections) {
3026 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003027 NewCU->addSectionLabel(
3028 Die, dwarf::DW_AT_GNU_pubnames,
3029 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00003030 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003031 NewCU->addSectionDelta(
3032 Die, dwarf::DW_AT_GNU_pubnames,
3033 Asm->GetTempSymbol("gnu_pubnames", NewCU->getUniqueID()),
3034 DwarfGnuPubNamesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00003035
3036 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher33ff6972013-11-21 23:46:41 +00003037 NewCU->addSectionLabel(
3038 Die, dwarf::DW_AT_GNU_pubtypes,
3039 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()));
Eric Christopher39eebfa2013-09-30 23:14:16 +00003040 else
Eric Christopher33ff6972013-11-21 23:46:41 +00003041 NewCU->addSectionDelta(
3042 Die, dwarf::DW_AT_GNU_pubtypes,
3043 Asm->GetTempSymbol("gnu_pubtypes", NewCU->getUniqueID()),
3044 DwarfGnuPubTypesSectionSym);
Eric Christopher39eebfa2013-09-30 23:14:16 +00003045 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00003046
Eric Christopher0f63d062013-12-03 00:45:45 +00003047 // Attribute if we've emitted any ranges and their location for the compile unit.
3048 if (CU->getRangeLists().size()) {
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003049 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopher0f63d062013-12-03 00:45:45 +00003050 NewCU->addSectionLabel(
3051 Die, dwarf::DW_AT_GNU_ranges_base,
3052 Asm->GetTempSymbol("gnu_ranges", NewCU->getUniqueID()));
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003053 else
Eric Christopher0f63d062013-12-03 00:45:45 +00003054 NewCU->addSectionDelta(
3055 Die, dwarf::DW_AT_GNU_ranges_base,
3056 Asm->GetTempSymbol("gnu_ranges", NewCU->getUniqueID()),
3057 DwarfDebugRangeSectionSym);
Eric Christopher9a08f9e2013-10-01 00:43:36 +00003058 }
3059
Eric Christopherc8a310e2012-12-10 23:34:43 +00003060 SkeletonHolder.addUnit(NewCU);
3061
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003062 return NewCU;
3063}
3064
Eric Christopher3c5a1912012-12-19 22:02:53 +00003065void DwarfDebug::emitSkeletonAbbrevs(const MCSection *Section) {
3066 assert(useSplitDwarf() && "No split dwarf debug info?");
3067 emitAbbrevs(Section, &SkeletonAbbrevs);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003068}
3069
Eric Christopherd692c1d2012-12-11 19:42:09 +00003070// Emit the .debug_info.dwo section for separated dwarf. This contains the
3071// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003072void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00003073 assert(useSplitDwarf() && "No split dwarf debug info?");
Eric Christophera2de8262012-12-15 00:04:07 +00003074 InfoHolder.emitUnits(this, Asm->getObjFileLowering().getDwarfInfoDWOSection(),
Eric Christopher3c5a1912012-12-19 22:02:53 +00003075 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3076 DwarfAbbrevDWOSectionSym);
3077}
3078
3079// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
3080// abbreviations for the .debug_info.dwo section.
3081void DwarfDebug::emitDebugAbbrevDWO() {
3082 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopher48fef592012-12-20 21:58:40 +00003083 emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
3084 &Abbreviations);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00003085}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003086
3087// Emit the .debug_str.dwo section for separated dwarf. This contains the
3088// string section and is identical in format to traditional .debug_str
3089// sections.
3090void DwarfDebug::emitDebugStrDWO() {
3091 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00003092 const MCSection *OffSec =
3093 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00003094 const MCSymbol *StrSym = DwarfStrSectionSym;
3095 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
3096 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00003097}
David Blaikie409dd9c2013-11-19 23:08:21 +00003098
David Blaikie3c1d3322013-12-02 18:44:29 +00003099void DwarfDebug::addTypeUnitType(uint16_t Language, DIE *RefDie,
3100 DICompositeType CTy) {
Eric Christopherf52eddf2013-11-26 22:23:27 +00003101 DenseMap<const MDNode *,
3102 std::pair<uint64_t, SmallVectorImpl<DIE *> *> >::iterator I =
3103 TypeUnits.find(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003104 SmallVector<DIE *, 8> References;
3105 References.push_back(RefDie);
3106 if (I != TypeUnits.end()) {
3107 if (I->second.second) {
3108 I->second.second->push_back(RefDie);
3109 return;
3110 }
3111 } else {
3112 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie319a05f2013-12-02 19:33:10 +00003113 TypeUnit *NewTU = new TypeUnit(GlobalCUIndexCount++, UnitDie, Language, Asm,
3114 this, &InfoHolder);
3115 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie3c1d3322013-12-02 18:44:29 +00003116 Language);
David Blaikie409dd9c2013-11-19 23:08:21 +00003117
3118 // Register the type in the TypeUnits map with a vector of references to be
3119 // populated whenever a reference is required.
3120 I = TypeUnits.insert(std::make_pair(CTy, std::make_pair(0, &References)))
3121 .first;
3122
3123 // Construct the type, this may, recursively, require more type units that
3124 // may in turn require this type again - in which case they will add DIEs to
3125 // the References vector.
David Blaikie319a05f2013-12-02 19:33:10 +00003126 DIE *Die = NewTU->createTypeDIE(CTy);
David Blaikie409dd9c2013-11-19 23:08:21 +00003127
David Blaikie319a05f2013-12-02 19:33:10 +00003128 if (GenerateODRHash && shouldAddODRHash(NewTU, Die))
3129 NewTU->addUInt(UnitDie, dwarf::DW_AT_GNU_odr_signature,
David Blaikie409dd9c2013-11-19 23:08:21 +00003130 dwarf::DW_FORM_data8,
3131 DIEHash().computeDIEODRSignature(*Die));
3132 // FIXME: This won't handle circularly referential structures, as the DIE
3133 // may have references to other DIEs still under construction and missing
3134 // their signature. Hashing should walk through the signatures to their
3135 // referenced type, or possibly walk the precomputed hashes of related types
3136 // at the end.
3137 uint64_t Signature = DIEHash().computeTypeSignature(*Die);
3138
3139 // Remove the References vector and add the type hash.
3140 I->second.first = Signature;
3141 I->second.second = NULL;
3142
David Blaikie319a05f2013-12-02 19:33:10 +00003143 InfoHolder.addUnit(NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00003144 }
3145
3146 // Populate all the signatures.
3147 for (unsigned i = 0, e = References.size(); i != e; ++i) {
3148 CUMap.begin()->second->addUInt(References[i], dwarf::DW_AT_signature,
3149 dwarf::DW_FORM_ref_sig8, I->second.first);
3150 }
3151}