blob: e9171b00eaa414858897576f252cfd0aa4f7d6f4 [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 Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000027#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000032#include "llvm/IR/ValueHandle.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000033#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000034#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000035#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000036#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000037#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000038#include "llvm/Support/Debug.h"
David Majnemered89b5c2013-08-21 06:13:34 +000039#include "llvm/Support/Dwarf.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000040#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000041#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000042#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000043#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000045#include "llvm/Support/Timer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Target/TargetFrameLowering.h"
47#include "llvm/Target/TargetLoweringObjectFile.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetOptions.h"
50#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000051using namespace llvm;
52
Eric Christopher7f2b5512013-07-23 22:16:41 +000053static cl::opt<bool>
54DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
55 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000056
Eric Christopher7f2b5512013-07-23 22:16:41 +000057static cl::opt<bool> UnknownLocations(
58 "use-unknown-locations", cl::Hidden,
59 cl::desc("Make an absence of debug location information explicit."),
60 cl::init(false));
Dan Gohman7421ae42010-05-07 01:08:53 +000061
Eric Christopherb4bef6d2013-11-19 09:04:36 +000062static cl::opt<bool> GenerateCUHash("generate-cu-hash", cl::Hidden,
63 cl::desc("Add the CU hash as the dwo_id."),
64 cl::init(false));
Eric Christopherd29614f2013-08-13 01:21:55 +000065
Eric Christopherdd1a0122013-09-13 00:35:05 +000066static cl::opt<bool>
67GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
68 cl::desc("Generate GNU-style pubnames and pubtypes"),
69 cl::init(false));
70
Eric Christopher02dbadb2014-02-14 01:26:55 +000071static cl::opt<bool> GenerateARangeSection("generate-arange-section",
72 cl::Hidden,
73 cl::desc("Generate dwarf aranges"),
74 cl::init(false));
75
Eric Christopher20b76a72012-08-23 22:36:40 +000076namespace {
Eric Christopherf07ee3a2014-01-27 23:50:03 +000077enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher20b76a72012-08-23 22:36:40 +000078}
Eric Christopher4996c702011-11-07 09:24:32 +000079
Eric Christopher7f2b5512013-07-23 22:16:41 +000080static cl::opt<DefaultOnOff>
81DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
82 cl::desc("Output prototype dwarf accelerator tables."),
83 cl::values(clEnumVal(Default, "Default for platform"),
84 clEnumVal(Enable, "Enabled"),
85 clEnumVal(Disable, "Disabled"), clEnumValEnd),
86 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +000087
Eric Christopher7f2b5512013-07-23 22:16:41 +000088static cl::opt<DefaultOnOff>
Eric Christopher7f2b5512013-07-23 22:16:41 +000089SplitDwarf("split-dwarf", cl::Hidden,
Eric Christopher5d008fe2013-12-04 23:24:28 +000090 cl::desc("Output DWARF5 split debug info."),
Eric Christopher7f2b5512013-07-23 22:16:41 +000091 cl::values(clEnumVal(Default, "Default for platform"),
92 clEnumVal(Enable, "Enabled"),
93 clEnumVal(Disable, "Disabled"), clEnumValEnd),
94 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +000095
Eric Christopher7da24882013-08-19 21:07:38 +000096static cl::opt<DefaultOnOff>
Eric Christopher4d36ca02013-08-26 23:24:35 +000097DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
98 cl::desc("Generate DWARF pubnames and pubtypes sections"),
99 cl::values(clEnumVal(Default, "Default for platform"),
100 clEnumVal(Enable, "Enabled"),
101 clEnumVal(Disable, "Disabled"), clEnumValEnd),
102 cl::init(Default));
Eric Christopher7da24882013-08-19 21:07:38 +0000103
Eric Christopher33ff6972013-11-21 23:46:41 +0000104static cl::opt<unsigned>
105DwarfVersionNumber("dwarf-version", cl::Hidden,
Eric Christophera5a79422013-12-09 23:32:48 +0000106 cl::desc("Generate DWARF for dwarf version."), cl::init(0));
Eric Christopher33ff6972013-11-21 23:46:41 +0000107
Eric Christopher46e23432013-12-20 04:16:18 +0000108static cl::opt<bool>
109DwarfCURanges("generate-dwarf-cu-ranges", cl::Hidden,
110 cl::desc("Generate DW_AT_ranges for compile units"),
111 cl::init(false));
112
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000113static const char *const DWARFGroupName = "DWARF Emission";
114static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000115
Bill Wendling2f921f82009-05-15 09:23:25 +0000116//===----------------------------------------------------------------------===//
117
Bill Wendling2f921f82009-05-15 09:23:25 +0000118namespace llvm {
119
Manman Renbe5576f2013-10-08 19:07:44 +0000120/// resolve - Look in the DwarfDebug map for the MDNode that
121/// corresponds to the reference.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000122template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
Manman Renbe5576f2013-10-08 19:07:44 +0000123 return DD->resolve(Ref);
124}
125
Nick Lewycky019d2552011-07-29 03:49:23 +0000126DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000127 DIType Ty = Var.getType();
128 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
129 // addresses instead.
130 if (Var.isBlockByrefVariable()) {
131 /* Byref variables, in Blocks, are declared by the programmer as
132 "SomeType VarName;", but the compiler creates a
133 __Block_byref_x_VarName struct, and gives the variable VarName
134 either the struct, or a pointer to the struct, as its type. This
135 is necessary for various behind-the-scenes things the compiler
136 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000137
Devang Patelf20c4f72011-04-12 22:53:02 +0000138 However, as far as the original *programmer* is concerned, the
139 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000140
Devang Patelf20c4f72011-04-12 22:53:02 +0000141 The following function dives into the __Block_byref_x_VarName
142 struct to find the original type of the variable. This will be
143 passed back to the code generating the type for the Debug
144 Information Entry for the variable 'VarName'. 'VarName' will then
145 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000146
Devang Patelf20c4f72011-04-12 22:53:02 +0000147 The original type 'SomeType' will be the type of the field named
148 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000149
Devang Patelf20c4f72011-04-12 22:53:02 +0000150 NOTE: In order for this to not completely fail on the debugger
151 side, the Debug Information Entry for the variable VarName needs to
152 have a DW_AT_location that tells the debugger how to unwind through
153 the pointers and __Block_byref_x_VarName struct to find the actual
154 value of the variable. The function addBlockByrefType does this. */
155 DIType subType = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000156 uint16_t tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000157
Eric Christopher9adc55f2013-09-04 19:53:21 +0000158 if (tag == dwarf::DW_TAG_pointer_type)
Manman Renbe5576f2013-10-08 19:07:44 +0000159 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher6a841382012-11-19 22:42:10 +0000160
Eric Christopher9adc55f2013-09-04 19:53:21 +0000161 DIArray Elements = DICompositeType(subType).getTypeArray();
Devang Patelf20c4f72011-04-12 22:53:02 +0000162 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie5af2aca2013-11-18 23:57:26 +0000163 DIDerivedType DT(Elements.getElement(i));
Devang Patelf20c4f72011-04-12 22:53:02 +0000164 if (getName() == DT.getName())
Manman Renbe5576f2013-10-08 19:07:44 +0000165 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000166 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000167 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000168 return Ty;
169}
Bill Wendling2f921f82009-05-15 09:23:25 +0000170
Chris Lattnerf5d06362010-04-05 04:09:20 +0000171} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000172
Eric Christopher942f22c2014-01-11 00:28:12 +0000173/// Return Dwarf Version by checking module flags.
174static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Ren8bfde892013-07-16 23:21:16 +0000175 Value *Val = M->getModuleFlag("Dwarf Version");
176 if (!Val)
Eric Christophere31e0722013-09-04 22:21:24 +0000177 return dwarf::DWARF_VERSION;
Manman Ren8bfde892013-07-16 23:21:16 +0000178 return cast<ConstantInt>(Val)->getZExtValue();
Manman Renac8062b2013-07-02 23:40:10 +0000179}
180
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000181DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
David Blaikie0504cda2013-12-05 07:43:55 +0000182 : Asm(A), MMI(Asm->MMI), FirstCU(0), SourceIdMap(DIEValueAllocator),
David Blaikie2666e242013-12-06 19:38:46 +0000183 PrevLabel(NULL), GlobalRangeCount(0),
Eric Christopher1bca60d2014-01-23 22:55:47 +0000184 InfoHolder(A, "info_string", DIEValueAllocator), HasCURanges(false),
Eric Christopher2037caf2014-01-28 00:49:26 +0000185 UsedNonDefaultText(false),
David Blaikie0504cda2013-12-05 07:43:55 +0000186 SkeletonHolder(A, "skel_string", DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000187
Eric Christopher50effa02014-01-03 02:16:44 +0000188 DwarfInfoSectionSym = DwarfAbbrevSectionSym = DwarfStrSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000189 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher55863be2013-04-07 03:43:09 +0000190 DwarfAddrSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000191 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000192 FunctionBeginSym = FunctionEndSym = 0;
Eric Christophera5a79422013-12-09 23:32:48 +0000193 CurFn = 0;
194 CurMI = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000195
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000196 // Turn on accelerator tables for Darwin by default, pubnames by
197 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000198 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000199
Eric Christopher574b5c82013-08-19 21:41:38 +0000200 if (DwarfAccelTables == Default)
201 HasDwarfAccelTables = IsDarwin;
202 else
Eric Christopher5297df02013-08-26 20:58:35 +0000203 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000204
Eric Christophercdf218d2012-12-10 19:51:21 +0000205 if (SplitDwarf == Default)
206 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000207 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000208 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000209
Eric Christopher4d36ca02013-08-26 23:24:35 +0000210 if (DwarfPubSections == Default)
211 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000212 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000213 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000214
Eric Christopher942f22c2014-01-11 00:28:12 +0000215 DwarfVersion = DwarfVersionNumber
216 ? DwarfVersionNumber
217 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000218
Dan Gohman6e681a52010-06-18 15:56:31 +0000219 {
220 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000221 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000222 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000223}
Bill Wendling2f921f82009-05-15 09:23:25 +0000224
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000225// Switch to the specified MCSection and emit an assembler
226// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000227static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000228 const char *SymbolStem = 0) {
229 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000230 if (!SymbolStem)
231 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000232
233 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
234 Asm->OutStreamer.EmitLabel(TmpSym);
235 return TmpSym;
236}
237
Eric Christopherf8194852013-12-05 18:06:10 +0000238DwarfFile::~DwarfFile() {
Eric Christophera5a79422013-12-09 23:32:48 +0000239 for (SmallVectorImpl<DwarfUnit *>::iterator I = CUs.begin(), E = CUs.end();
240 I != E; ++I)
David Blaikie72f1a3e2013-11-23 01:17:34 +0000241 delete *I;
242}
243
Eric Christopherf8194852013-12-05 18:06:10 +0000244MCSymbol *DwarfFile::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000245 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000246}
247
Eric Christopherf8194852013-12-05 18:06:10 +0000248MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000249 std::pair<MCSymbol *, unsigned> &Entry =
250 StringPool.GetOrCreateValue(Str).getValue();
251 if (Entry.first)
252 return Entry.first;
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000253
254 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000255 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000256}
257
Eric Christopherf8194852013-12-05 18:06:10 +0000258unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000259 std::pair<MCSymbol *, unsigned> &Entry =
260 StringPool.GetOrCreateValue(Str).getValue();
261 if (Entry.first)
262 return Entry.second;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000263
264 Entry.second = NextStringPoolNumber++;
265 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
266 return Entry.second;
267}
268
David Blaikief1a6dea2014-02-15 19:34:03 +0000269unsigned DwarfFile::getAddrPoolIndex(const MCSymbol *Sym, bool TLS) {
270 std::pair<AddrPool::iterator, bool> P = AddressPool.insert(
271 std::make_pair(Sym, AddressPoolEntry(NextAddrPoolNumber, TLS)));
David Blaikiea67de2b2013-06-28 18:55:13 +0000272 if (P.second)
273 ++NextAddrPoolNumber;
David Blaikief1a6dea2014-02-15 19:34:03 +0000274 return P.first->second.Number;
Eric Christopher962c9082013-01-15 23:56:56 +0000275}
276
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000277// Define a unique number for the abbreviation.
278//
Eric Christopherf8194852013-12-05 18:06:10 +0000279void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000280 // Check the set for priors.
David Blaikie0504cda2013-12-05 07:43:55 +0000281 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000282
283 // If it's newly added.
284 if (InSet == &Abbrev) {
285 // Add to abbreviation list.
David Blaikie2d4e1122013-10-30 17:14:24 +0000286 Abbreviations.push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000287
288 // Assign the vector position + 1 as its number.
David Blaikie2d4e1122013-10-30 17:14:24 +0000289 Abbrev.setNumber(Abbreviations.size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000290 } else {
291 // Assign existing abbreviation number.
292 Abbrev.setNumber(InSet->getNumber());
293 }
294}
295
Eric Christopherd9843b32011-11-10 19:25:34 +0000296static bool isObjCClass(StringRef Name) {
297 return Name.startswith("+") || Name.startswith("-");
298}
299
300static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000301 if (!isObjCClass(Name))
302 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000303
Benjamin Kramer260de742013-08-24 12:15:54 +0000304 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000305}
306
307static void getObjCClassCategory(StringRef In, StringRef &Class,
308 StringRef &Category) {
309 if (!hasObjCCategory(In)) {
310 Class = In.slice(In.find('[') + 1, In.find(' '));
311 Category = "";
312 return;
313 }
314
315 Class = In.slice(In.find('[') + 1, In.find('('));
316 Category = In.slice(In.find('[') + 1, In.find(' '));
317 return;
318}
319
320static StringRef getObjCMethodName(StringRef In) {
321 return In.slice(In.find(' ') + 1, In.find(']'));
322}
323
Richard Mittonc2508242013-10-03 22:07:08 +0000324// Helper for sorting sections into a stable output order.
325static bool SectionSort(const MCSection *A, const MCSection *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000326 std::string LA = (A ? A->getLabelBeginName() : "");
327 std::string LB = (B ? B->getLabelBeginName() : "");
328 return LA < LB;
Richard Mittonc2508242013-10-03 22:07:08 +0000329}
330
Eric Christopherd9843b32011-11-10 19:25:34 +0000331// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000332// TODO: Determine whether or not we should add names for programs
333// that do not have a DW_AT_name or DW_AT_linkage_name field - this
334// is only slightly different than the lookup of non-standard ObjC names.
Eric Christophera5a79422013-12-09 23:32:48 +0000335static void addSubprogramNames(DwarfUnit *TheU, DISubprogram SP, DIE *Die) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000336 if (!SP.isDefinition())
337 return;
David Blaikie2a80e442013-12-02 22:09:48 +0000338 TheU->addAccelName(SP.getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000339
340 // If the linkage name is different than the name, go ahead and output
341 // that as well into the name table.
342 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
David Blaikie2a80e442013-12-02 22:09:48 +0000343 TheU->addAccelName(SP.getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000344
345 // If this is an Objective-C selector name add it to the ObjC accelerator
346 // too.
347 if (isObjCClass(SP.getName())) {
348 StringRef Class, Category;
349 getObjCClassCategory(SP.getName(), Class, Category);
David Blaikie2a80e442013-12-02 22:09:48 +0000350 TheU->addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000351 if (Category != "")
David Blaikie2a80e442013-12-02 22:09:48 +0000352 TheU->addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000353 // Also add the base method name to the name table.
David Blaikie2a80e442013-12-02 22:09:48 +0000354 TheU->addAccelName(getObjCMethodName(SP.getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000355 }
356}
357
Manman Ren3eb9dff2013-09-09 19:05:21 +0000358/// isSubprogramContext - Return true if Context is either a subprogram
359/// or another context nested inside a subprogram.
360bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
361 if (!Context)
362 return false;
363 DIDescriptor D(Context);
364 if (D.isSubprogram())
365 return true;
366 if (D.isType())
Manman Ren116868e2013-09-09 19:47:11 +0000367 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000368 return false;
369}
370
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000371// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
372// and DW_AT_high_pc attributes. If there are global variables in this
373// scope then create and insert DIEs for these variables.
Eric Christopher4287a492013-12-09 23:57:44 +0000374DIE *DwarfDebug::updateSubprogramScopeDIE(DwarfCompileUnit *SPCU,
375 DISubprogram SP) {
David Blaikie25bc7192013-11-15 23:13:08 +0000376 DIE *SPDie = SPCU->getDIE(SP);
Devang Patela37a95e2010-07-07 22:20:57 +0000377
Chris Lattner3a383cb2010-04-05 00:13:49 +0000378 assert(SPDie && "Unable to find subprogram DIE!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000379
Bill Wendlingf720bf62012-11-07 05:19:04 +0000380 // If we're updating an abstract DIE, then we will be adding the children and
381 // object pointer later on. But what we don't want to do is process the
382 // concrete DIE twice.
David Blaikie25bc7192013-11-15 23:13:08 +0000383 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000384 // Pick up abstract subprogram DIE.
David Blaikie2a80e442013-12-02 22:09:48 +0000385 SPDie =
386 SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000387 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000388 } else {
389 DISubprogram SPDecl = SP.getFunctionDeclaration();
390 if (!SPDecl.isSubprogram()) {
391 // There is not any need to generate specification DIE for a function
392 // defined at compile unit level. If a function is defined inside another
393 // function then gdb prefers the definition at top level and but does not
394 // expect specification DIE in parent function. So avoid creating
395 // specification DIE for a function defined inside a function.
Manman Renc50fa112013-10-10 18:40:01 +0000396 DIScope SPContext = resolve(SP.getContext());
397 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000398 !SPContext.isFile() && !isSubprogramContext(SPContext)) {
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000399 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
400
401 // Add arguments.
402 DICompositeType SPTy = SP.getType();
403 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000404 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000405 if (SPTag == dwarf::DW_TAG_subroutine_type)
Adrian Prantl69140d22014-02-25 22:27:14 +0000406 SPCU->constructSubprogramArguments(*SPDie, Args);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000407 DIE *SPDeclDie = SPDie;
David Blaikie2a80e442013-12-02 22:09:48 +0000408 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
409 *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000410 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000411 }
412 }
Devang Patela37a95e2010-07-07 22:20:57 +0000413 }
414
David Blaikie4bd13b72014-03-07 18:49:45 +0000415 attachLowHighPC(SPCU, SPDie, FunctionBeginSym, FunctionEndSym);
Eric Christopher0f63d062013-12-03 00:45:45 +0000416
Chris Lattner3a383cb2010-04-05 00:13:49 +0000417 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
418 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000419 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000420
Eric Christopherd9843b32011-11-10 19:25:34 +0000421 // Add name to the name table, we do this here because we're guaranteed
422 // to have concrete versions of our DW_TAG_subprogram nodes.
423 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000424
Chris Lattner3a383cb2010-04-05 00:13:49 +0000425 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000426}
427
Manman Ren5b2f4b02013-09-11 19:40:28 +0000428/// Check whether we should create a DIE for the given Scope, return true
429/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000430bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
431 if (Scope->isAbstractScope())
432 return false;
433
Manman Ren5b2f4b02013-09-11 19:40:28 +0000434 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000435 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
436 if (Ranges.empty())
437 return true;
438
439 if (Ranges.size() > 1)
440 return false;
441
Manman Ren5b2f4b02013-09-11 19:40:28 +0000442 // We don't create a DIE if we have a single Range and the end label
443 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000444 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
445 MCSymbol *End = getLabelAfterInsn(RI->second);
446 return !End;
447}
448
Eric Christophera5a79422013-12-09 23:32:48 +0000449static void addSectionLabel(AsmPrinter *Asm, DwarfUnit *U, DIE *D,
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000450 dwarf::Attribute A, const MCSymbol *L,
451 const MCSymbol *Sec) {
452 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
453 U->addSectionLabel(D, A, L);
454 else
455 U->addSectionDelta(D, A, L, Sec);
456}
457
Eric Christopher4287a492013-12-09 23:57:44 +0000458void DwarfDebug::addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
Eric Christopherbe2513e2013-12-03 00:45:59 +0000459 const SmallVectorImpl<InsnRange> &Range) {
460 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
461 // emitting it appropriately.
Eric Christopherf8790642013-12-04 22:04:50 +0000462 MCSymbol *RangeSym = Asm->GetTempSymbol("debug_ranges", GlobalRangeCount++);
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000463 addSectionLabel(Asm, TheCU, ScopeDIE, dwarf::DW_AT_ranges, RangeSym,
464 DwarfDebugRangeSectionSym);
465
Eric Christopherf8790642013-12-04 22:04:50 +0000466 RangeSpanList List(RangeSym);
Eric Christopherbe2513e2013-12-03 00:45:59 +0000467 for (SmallVectorImpl<InsnRange>::const_iterator RI = Range.begin(),
468 RE = Range.end();
469 RI != RE; ++RI) {
470 RangeSpan Span(getLabelBeforeInsn(RI->first),
471 getLabelAfterInsn(RI->second));
Chandler Carruth002da5d2014-03-02 04:08:41 +0000472 List.addRange(std::move(Span));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000473 }
474
475 // Add the range list to the set of ranges to be emitted.
Chandler Carruth002da5d2014-03-02 04:08:41 +0000476 TheCU->addRangeList(std::move(List));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000477}
478
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000479// Construct new DW_TAG_lexical_block for this scope and attach
480// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher4287a492013-12-09 23:57:44 +0000481DIE *DwarfDebug::constructLexicalScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000482 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000483 if (isLexicalScopeDIENull(Scope))
484 return 0;
485
Devang Patel6c74a872010-04-27 19:46:33 +0000486 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
487 if (Scope->isAbstractScope())
488 return ScopeDIE;
489
Eric Christopher0f63d062013-12-03 00:45:45 +0000490 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherbe2513e2013-12-03 00:45:59 +0000491
Eric Christopherdc42ea82013-07-03 01:57:28 +0000492 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000493 if (ScopeRanges.size() > 1) {
Eric Christopherbe2513e2013-12-03 00:45:59 +0000494 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
Devang Patel6c74a872010-04-27 19:46:33 +0000495 return ScopeDIE;
496 }
497
Eric Christopherdc42ea82013-07-03 01:57:28 +0000498 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000499 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000500 MCSymbol *Start = getLabelBeforeInsn(RI->first);
501 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000502 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000503
Chris Lattnere13c3722010-03-09 01:58:53 +0000504 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
505 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000506
Eric Christopher962c9082013-01-15 23:56:56 +0000507 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
508 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000509
510 return ScopeDIE;
511}
512
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000513// This scope represents inlined body of a function. Construct DIE to
514// represent this concrete inlined copy of the function.
Eric Christopher4287a492013-12-09 23:57:44 +0000515DIE *DwarfDebug::constructInlinedScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000516 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000517 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopher1cdb63d2013-12-04 21:20:15 +0000518 assert(!ScopeRanges.empty() &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000519 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000520
Devang Patelf098ce22011-07-27 00:34:13 +0000521 if (!Scope->getScopeNode())
522 return NULL;
523 DIScope DS(Scope->getScopeNode());
524 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000525 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000526 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000527 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000528 return NULL;
529 }
530
Devang Patel73bc1722011-05-05 17:54:26 +0000531 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000532 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000533
Eric Christopherbe2513e2013-12-03 00:45:59 +0000534 // If we have multiple ranges, emit them into the range section.
535 if (ScopeRanges.size() > 1)
536 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
537 else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000538 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000539 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
540 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
541
542 if (StartLabel == 0 || EndLabel == 0)
543 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
544
545 assert(StartLabel->isDefined() &&
546 "Invalid starting label for an inlined scope!");
547 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
548
Eric Christopher962c9082013-01-15 23:56:56 +0000549 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
550 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000551 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000552
553 InlinedSubprogramDIEs.insert(OriginDIE);
554
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000555 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000556 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000557 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000558 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
559 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000560 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000561
Eric Christopher8dda5d02011-12-04 06:02:38 +0000562 // Add name to the name table, we do this here because we're guaranteed
563 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
564 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000565
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000566 return ScopeDIE;
567}
568
Eric Christopher4287a492013-12-09 23:57:44 +0000569DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
570 LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000571 SmallVectorImpl<DIE *> &Children) {
572 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000573
574 // Collect arguments for current function.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000575 if (LScopes.isCurrentFunctionScope(Scope)) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000576 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
577 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000578 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000579 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000580 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000581 if (ArgDV->isObjectPointer())
582 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000583 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000584
Adrian Prantl69140d22014-02-25 22:27:14 +0000585 // If this is a variadic function, add an unspecified parameter.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000586 DISubprogram SP(Scope->getScopeNode());
Adrian Prantl3f49c892014-02-25 19:57:42 +0000587 DIArray FnArgs = SP.getType().getTypeArray();
Eric Christopher73ffdb82014-02-26 02:50:56 +0000588 if (FnArgs.getElement(FnArgs.getNumElements() - 1)
589 .isUnspecifiedParameter()) {
Adrian Prantl3f49c892014-02-25 19:57:42 +0000590 DIE *Ellipsis = new DIE(dwarf::DW_TAG_unspecified_parameters);
591 Children.push_back(Ellipsis);
592 }
593 }
594
Eric Christopherf84354b2011-10-03 15:49:16 +0000595 // Collect lexical scope children first.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000596 const SmallVectorImpl<DbgVariable *> &Variables =
597 ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000598 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000599 if (DIE *Variable = TheCU->constructVariableDIE(*Variables[i],
600 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000601 Children.push_back(Variable);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000602 if (Variables[i]->isObjectPointer())
603 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000604 }
Craig Topper977e9cd2013-07-03 04:24:43 +0000605 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000606 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000607 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000608 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000609 return ObjectPointer;
610}
611
612// Construct a DIE for this scope.
Eric Christopher4287a492013-12-09 23:57:44 +0000613DIE *DwarfDebug::constructScopeDIE(DwarfCompileUnit *TheCU,
614 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000615 if (!Scope || !Scope->getScopeNode())
616 return NULL;
617
618 DIScope DS(Scope->getScopeNode());
619
620 SmallVector<DIE *, 8> Children;
621 DIE *ObjectPointer = NULL;
622 bool ChildrenCreated = false;
623
Manman Ren5b2f4b02013-09-11 19:40:28 +0000624 // We try to create the scope DIE first, then the children DIEs. This will
625 // avoid creating un-used children then removing them later when we find out
626 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000627 DIE *ScopeDIE = NULL;
628 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000629 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000630 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000631 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000632 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000633 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000634 // Note down abstract DIE.
635 if (ScopeDIE)
636 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000637 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000638 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000639 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000640 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000641 if (isLexicalScopeDIENull(Scope))
642 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000643
644 // We create children here when we know the scope DIE is not going to be
645 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000646 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
647 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000648
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000649 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000650 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000651 ImportedEntityMap::const_iterator> Range =
652 std::equal_range(
653 ScopesWithImportedEntities.begin(),
654 ScopesWithImportedEntities.end(),
655 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
656 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000657 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000658 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000659 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000660 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000661 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
662 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000663 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000664 }
Eric Christopher6a841382012-11-19 22:42:10 +0000665
Benjamin Kramer892daba2013-08-24 11:55:49 +0000666 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000667 assert(Children.empty() &&
668 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000669 return NULL;
670 }
Manman Ren2312ed32013-09-10 18:40:41 +0000671 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000672 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000673 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000674
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000675 // Add children
Craig Topperd8e43652013-07-03 04:17:25 +0000676 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000677 E = Children.end();
678 I != E; ++I)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000679 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000680
Eric Christophere3417762012-09-12 23:36:19 +0000681 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000682 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000683
Eric Christopherd9843b32011-11-10 19:25:34 +0000684 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000685}
686
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000687// Look up the source id with the given directory and source file names.
688// If none currently exists, create a new id and insert it in the
689// SourceIds map. This can update DirectoryNames and SourceFileNames maps
690// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000691unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
692 unsigned CUID) {
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000693 // If we print assembly, we can't separate .file entries according to
Manman Ren1e427202013-03-07 01:42:00 +0000694 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000695
696 // FIXME: add a better feature test than hasRawTextSupport. Even better,
697 // extend .file to support this.
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000698 if (Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000699 CUID = 0;
700
Devang Patel871d0b12010-09-16 20:57:49 +0000701 // If FE did not provide a file name, then assume stdin.
702 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000703 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000704
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000705 // TODO: this might not belong here. See if we can factor this better.
706 if (DirName == CompilationDir)
707 DirName = "";
708
Manman Ren1e427202013-03-07 01:42:00 +0000709 // FileIDCUMap stores the current ID for the given compile unit.
710 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000711
Manman Ren1e427202013-03-07 01:42:00 +0000712 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000713 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000714 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000715 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000716 NamePair += DirName;
717 NamePair += '\0'; // Zero bytes are not allowed in paths.
718 NamePair += FileName;
719
720 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
721 if (Ent.getValue() != SrcId)
722 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000723
Manman Ren1e427202013-03-07 01:42:00 +0000724 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000725 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000726 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000727
728 return SrcId;
729}
730
Eric Christophera5a79422013-12-09 23:32:48 +0000731void DwarfDebug::addGnuPubAttributes(DwarfUnit *U, DIE *D) const {
David Blaikie3c842622013-12-04 21:31:26 +0000732 if (!GenerateGnuPubSections)
733 return;
734
David Blaikie47c254b2014-03-06 05:47:39 +0000735 U->addFlag(D, dwarf::DW_AT_GNU_pubnames);
David Blaikie3c842622013-12-04 21:31:26 +0000736}
737
Eric Christopher4287a492013-12-09 23:57:44 +0000738// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000739// DW_TAG_compile_unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000740DwarfCompileUnit *DwarfDebug::constructDwarfCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000741 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000742 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000743
744 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +0000745 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
746 InfoHolder.getUnits().size(), Die, DIUnit, Asm, this, &InfoHolder);
David Blaikie2666e242013-12-06 19:38:46 +0000747 InfoHolder.addUnit(NewCU);
748
Manman Ren1e427202013-03-07 01:42:00 +0000749 FileIDCUMap[NewCU->getUniqueID()] = 0;
Manman Ren1e427202013-03-07 01:42:00 +0000750
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000751 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000752 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
753 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000754 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000755
Eric Christopher52ce7182013-04-09 19:23:15 +0000756 if (!useSplitDwarf()) {
David Blaikie2494fdb2014-02-14 22:41:51 +0000757 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000758
759 // If we're using split dwarf the compilation dir is going to be in the
760 // skeleton CU and so we don't need to duplicate it here.
761 if (!CompilationDir.empty())
762 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
763
David Blaikie3c842622013-12-04 21:31:26 +0000764 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000765 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000766
Bill Wendling2b128d72009-05-20 23:19:06 +0000767 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000768 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000769
Devang Patel2d9caf92009-11-25 17:36:49 +0000770 StringRef Flags = DIUnit.getFlags();
771 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000772 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000773
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000774 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000775 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000776 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000777
Devang Patel1a0df9a2010-05-10 22:49:55 +0000778 if (!FirstCU)
779 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000780
Eric Christopherd4368fd2014-01-02 21:03:28 +0000781 if (useSplitDwarf()) {
782 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection(),
783 DwarfInfoDWOSectionSym);
Eric Christopherd8667202013-12-30 17:22:27 +0000784 NewCU->setSkeleton(constructSkeletonCU(NewCU));
Eric Christopherd4368fd2014-01-02 21:03:28 +0000785 } else
786 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
787 DwarfInfoSectionSym);
Eric Christopherd039baa2013-12-30 03:40:32 +0000788
David Blaikie5a152402013-11-15 23:52:02 +0000789 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000790 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000791 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000792}
793
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000794// Construct subprogram DIE.
Eric Christopher4287a492013-12-09 23:57:44 +0000795void DwarfDebug::constructSubprogramDIE(DwarfCompileUnit *TheCU,
796 const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000797 // FIXME: We should only call this routine once, however, during LTO if a
798 // program is defined in multiple CUs we could end up calling it out of
799 // beginModule as we walk the CUs.
800
Eric Christopher4287a492013-12-09 23:57:44 +0000801 DwarfCompileUnit *&CURef = SPMap[N];
Eric Christopherffbc4de2013-10-18 01:57:30 +0000802 if (CURef)
803 return;
804 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000805
Devang Patel80ae3492009-08-28 23:24:31 +0000806 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000807 if (!SP.isDefinition())
808 // This is a method declaration which will be handled while constructing
809 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000810 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000811
Devang Patel89543712011-08-15 17:24:54 +0000812 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000813
Eric Christopherba506db2013-09-09 20:03:20 +0000814 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000815 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000816}
817
Eric Christopher4287a492013-12-09 23:57:44 +0000818void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000819 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000820 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000821 assert(Module.Verify());
David Blaikie684fc532013-05-06 23:33:07 +0000822 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000823 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000824}
825
Eric Christopher4287a492013-12-09 23:57:44 +0000826void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
827 const MDNode *N, DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000828 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000829 assert(Module.Verify());
David Blaikie4dd2de72013-05-08 06:01:38 +0000830 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000831}
832
Eric Christopher4287a492013-12-09 23:57:44 +0000833void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000834 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000835 DIE *Context) {
836 assert(Module.Verify() &&
837 "Use one of the MDNode * overloads to handle invalid metadata");
838 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000839 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000840 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000841 DIE *EntityDie;
842 DIDescriptor Entity = Module.getEntity();
843 if (Entity.isNameSpace())
844 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
845 else if (Entity.isSubprogram())
846 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000847 else if (Entity.isType())
848 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000849 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000850 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000851 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
852 Module.getContext().getDirectory(),
853 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000854 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
855 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000856 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000857 StringRef Name = Module.getName();
858 if (!Name.empty())
859 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000860 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000861}
862
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000863// Emit all Dwarf sections that should come prior to the content. Create
864// global DIEs and emit initial debug info sections. This is invoked by
865// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000866void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000867 if (DisableDebugInfoPrinting)
868 return;
869
Eric Christopher58f41952012-11-19 22:42:15 +0000870 const Module *M = MMI->getModule();
871
Nick Lewycky019d2552011-07-29 03:49:23 +0000872 // If module has named metadata anchors then use them, otherwise scan the
873 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000874 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000875 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000876 return;
Manman Ren60352032013-09-05 18:48:31 +0000877 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000878
David Blaikiedc69ebb2013-03-11 23:39:23 +0000879 // Emit initial sections so we can reference labels later.
880 emitSectionLabels();
881
882 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
883 DICompileUnit CUNode(CU_Nodes->getOperand(i));
Eric Christopher4287a492013-12-09 23:57:44 +0000884 DwarfCompileUnit *CU = constructDwarfCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000885 DIArray ImportedEntities = CUNode.getImportedEntities();
886 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000887 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000888 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
889 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000890 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000891 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000892 DIArray GVs = CUNode.getGlobalVariables();
893 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000894 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000895 DIArray SPs = CUNode.getSubprograms();
896 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
897 constructSubprogramDIE(CU, SPs.getElement(i));
898 DIArray EnumTypes = CUNode.getEnumTypes();
899 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
900 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
901 DIArray RetainedTypes = CUNode.getRetainedTypes();
902 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
903 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000904 // Emit imported_modules last so that the relevant context is already
905 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000906 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
907 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000908 }
Eric Christopher6a841382012-11-19 22:42:10 +0000909
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000910 // Tell MMI that we have debug info.
911 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000912
Bill Wendling2b128d72009-05-20 23:19:06 +0000913 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000914 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000915}
916
Eric Christopher960ac372012-11-22 00:59:49 +0000917// Attach DW_AT_inline attribute with inlined subprogram DIEs.
918void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000919 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
920 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000921 AE = InlinedSubprogramDIEs.end();
922 AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000923 DIE *ISP = *AI;
David Blaikief2443192013-10-21 17:28:37 +0000924 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000925 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000926 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000927 AE = AbstractSPDies.end();
928 AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000929 DIE *ISP = AI->second;
930 if (InlinedSubprogramDIEs.count(ISP))
931 continue;
David Blaikief2443192013-10-21 17:28:37 +0000932 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000933 }
Eric Christopher960ac372012-11-22 00:59:49 +0000934}
935
936// Collect info for variables that were optimized out.
937void DwarfDebug::collectDeadVariables() {
938 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000939
940 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
941 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
942 DICompileUnit TheCU(CU_Nodes->getOperand(i));
943 DIArray Subprograms = TheCU.getSubprograms();
944 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000945 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000946 if (ProcessedSPNodes.count(SP) != 0)
947 continue;
948 if (!SP.isSubprogram())
949 continue;
950 if (!SP.isDefinition())
951 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000952 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +0000953 if (Variables.getNumElements() == 0)
954 continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000955
Eric Christopher735401c2012-11-27 00:13:51 +0000956 // Construct subprogram DIE and add variables DIEs.
Eric Christopher4287a492013-12-09 23:57:44 +0000957 DwarfCompileUnit *SPCU =
958 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +0000959 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +0000960 // FIXME: See the comment in constructSubprogramDIE about duplicate
961 // subprogram DIEs.
962 constructSubprogramDIE(SPCU, SP);
963 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +0000964 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
965 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +0000966 if (!DV.isVariable())
967 continue;
Manman Renb3388602013-10-05 01:43:03 +0000968 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000969 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +0000970 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +0000971 }
Eric Christopher960ac372012-11-22 00:59:49 +0000972 }
973 }
974 }
Eric Christopher960ac372012-11-22 00:59:49 +0000975}
976
977void DwarfDebug::finalizeModuleInfo() {
978 // Collect info for variables that were optimized out.
979 collectDeadVariables();
980
981 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
982 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000983
Eric Christopherad10cb52013-12-04 23:24:38 +0000984 // Handle anything that needs to be done on a per-unit basis after
985 // all other generation.
Eric Christophera5a79422013-12-09 23:32:48 +0000986 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
987 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +0000988 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +0000989 DwarfUnit *TheU = *I;
Eric Christopher60eb7692013-08-12 20:27:48 +0000990 // Emit DW_AT_containing_type attribute to connect types with their
991 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +0000992 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000993
Eric Christopher46e23432013-12-20 04:16:18 +0000994 // Add CU specific attributes if we need to add any.
995 if (TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
996 // If we're splitting the dwarf out now that we've got the entire
Eric Christopherd8667202013-12-30 17:22:27 +0000997 // CU then add the dwo id to it.
998 DwarfCompileUnit *SkCU =
999 static_cast<DwarfCompileUnit *>(TheU->getSkeleton());
Eric Christopher46e23432013-12-20 04:16:18 +00001000 if (useSplitDwarf()) {
1001 // This should be a unique identifier when we want to build .dwp files.
1002 uint64_t ID = 0;
1003 if (GenerateCUHash) {
Eric Christopher420569b2014-02-20 02:50:45 +00001004 DIEHash CUHash(Asm);
Eric Christopher46e23432013-12-20 04:16:18 +00001005 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
1006 }
1007 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1008 dwarf::DW_FORM_data8, ID);
Eric Christopher46e23432013-12-20 04:16:18 +00001009 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1010 dwarf::DW_FORM_data8, ID);
Eric Christopherd29614f2013-08-13 01:21:55 +00001011 }
Eric Christopherd8667202013-12-30 17:22:27 +00001012
Eric Christopher39cde8c2014-01-14 22:44:17 +00001013 // If we have code split among multiple sections or we've requested
1014 // it then emit a DW_AT_ranges attribute on the unit that will remain
1015 // in the .o file, otherwise add a DW_AT_low_pc.
Eric Christopherd8667202013-12-30 17:22:27 +00001016 // FIXME: Also add a high pc if we can.
Eric Christopher39cde8c2014-01-14 22:44:17 +00001017 // FIXME: We should use ranges if we have multiple compile units or
1018 // allow reordering of code ala .subsections_via_symbols in mach-o.
Eric Christopherd8667202013-12-30 17:22:27 +00001019 DwarfCompileUnit *U = SkCU ? SkCU : static_cast<DwarfCompileUnit *>(TheU);
Eric Christopher8873ada2014-01-29 22:22:56 +00001020 if (useCURanges() && TheU->getRanges().size()) {
Eric Christopherd8667202013-12-30 17:22:27 +00001021 addSectionLabel(Asm, U, U->getUnitDie(), dwarf::DW_AT_ranges,
1022 Asm->GetTempSymbol("cu_ranges", U->getUniqueID()),
1023 DwarfDebugRangeSectionSym);
Eric Christopher8873ada2014-01-29 22:22:56 +00001024
1025 // A DW_AT_low_pc attribute may also be specified in combination with
1026 // DW_AT_ranges to specify the default base address for use in location
1027 // lists (see Section 2.6.2) and range lists (see Section 2.17.3).
1028 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1029 0);
1030 } else
Eric Christophercf48ade2014-01-24 11:52:53 +00001031 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1032 0);
Eric Christopher60eb7692013-08-12 20:27:48 +00001033 }
1034 }
1035
1036 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001037 InfoHolder.computeSizeAndOffsets();
1038 if (useSplitDwarf())
1039 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001040}
1041
1042void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001043 // Filter labels by section.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001044 for (size_t n = 0; n < ArangeLabels.size(); n++) {
1045 const SymbolCU &SCU = ArangeLabels[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001046 if (SCU.Sym->isInSection()) {
1047 // Make a note of this symbol and it's section.
1048 const MCSection *Section = &SCU.Sym->getSection();
1049 if (!Section->getKind().isMetadata())
1050 SectionMap[Section].push_back(SCU);
1051 } else {
1052 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1053 // appear in the output. This sucks as we rely on sections to build
1054 // arange spans. We can do it without, but it's icky.
1055 SectionMap[NULL].push_back(SCU);
1056 }
1057 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001058
Richard Mittonc2508242013-10-03 22:07:08 +00001059 // Build a list of sections used.
1060 std::vector<const MCSection *> Sections;
Richard Mitton21101b32013-09-19 23:21:01 +00001061 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
1062 it++) {
1063 const MCSection *Section = it->first;
Richard Mittonc2508242013-10-03 22:07:08 +00001064 Sections.push_back(Section);
1065 }
1066
1067 // Sort the sections into order.
1068 // This is only done to ensure consistent output order across different runs.
1069 std::sort(Sections.begin(), Sections.end(), SectionSort);
1070
1071 // Add terminating symbols for each section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001072 for (unsigned ID = 0; ID < Sections.size(); ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001073 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001074 MCSymbol *Sym = NULL;
1075
1076 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001077 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001078 // if we know the section name up-front. For user-created sections, the
Eric Christopher95531b62014-01-29 22:06:21 +00001079 // resulting label may not be valid to use as a label. (section names can
1080 // use a greater set of characters on some systems)
Richard Mittonc2508242013-10-03 22:07:08 +00001081 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001082 Asm->OutStreamer.SwitchSection(Section);
1083 Asm->OutStreamer.EmitLabel(Sym);
1084 }
1085
1086 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001087 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001088 }
Eric Christopher1bca60d2014-01-23 22:55:47 +00001089
1090 // For now only turn on CU ranges if we've explicitly asked for it,
Eric Christopherfb8dd002014-01-29 22:06:27 +00001091 // we have -ffunction-sections enabled, we've emitted a function
1092 // into a unique section, or we're using LTO. If we're using LTO then
1093 // we can't know that any particular function in the module is correlated
1094 // to a particular CU and so we need to be conservative. At this point all
1095 // sections should be finalized except for dwarf sections.
1096 HasCURanges = DwarfCURanges || UsedNonDefaultText || (CUMap.size() > 1) ||
Eric Christopher1bca60d2014-01-23 22:55:47 +00001097 TargetMachine::getFunctionSections();
Eric Christopher960ac372012-11-22 00:59:49 +00001098}
Bill Wendling2b128d72009-05-20 23:19:06 +00001099
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001100// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001101void DwarfDebug::endModule() {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001102 assert(CurFn == 0);
1103 assert(CurMI == 0);
Eric Christopher960ac372012-11-22 00:59:49 +00001104
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001105 if (!FirstCU)
1106 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001107
1108 // End any existing sections.
1109 // TODO: Does this need to happen?
1110 endSections();
1111
1112 // Finalize the debug info for the module.
1113 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001114
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001115 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001116
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001117 // Emit all the DIEs into a debug info section.
1118 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001119
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001120 // Corresponding abbreviations into a abbrev section.
1121 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001122
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001123 // Emit info into a debug loc section.
1124 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001125
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001126 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +00001127 if (GenerateARangeSection)
1128 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001129
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001130 // Emit info into a debug ranges section.
1131 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001132
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001133 if (useSplitDwarf()) {
1134 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001135 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001136 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001137 // Emit DWO addresses.
1138 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001139 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001140
Eric Christophera876b822012-08-23 07:32:06 +00001141 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001142 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001143 emitAccelNames();
1144 emitAccelObjC();
1145 emitAccelNamespaces();
1146 emitAccelTypes();
1147 }
Eric Christopher6a841382012-11-19 22:42:10 +00001148
Eric Christopher4b358182013-08-30 00:40:17 +00001149 // Emit the pubnames and pubtypes sections if requested.
1150 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001151 emitDebugPubNames(GenerateGnuPubSections);
1152 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001153 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001154
Devang Pateld0701282010-08-02 17:32:15 +00001155 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001156 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001157
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001158 // Reset these for the next Module if we have one.
1159 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001160}
1161
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001162// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001163DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001164 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001165 LLVMContext &Ctx = DV->getContext();
1166 // More then one inlined variable corresponds to one abstract variable.
1167 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001168 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001169 if (AbsDbgVariable)
1170 return AbsDbgVariable;
1171
Devang Patel7e623022011-08-10 20:55:27 +00001172 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001173 if (!Scope)
1174 return NULL;
1175
Manman Renb3388602013-10-05 01:43:03 +00001176 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001177 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001178 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001179 return AbsDbgVariable;
1180}
1181
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001182// If Var is a current function argument then add it to CurrentFnArguments list.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001183bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +00001184 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001185 return false;
1186 DIVariable DV = Var->getVariable();
1187 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1188 return false;
1189 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001190 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001191 return false;
1192
Devang Patel4ab660b2011-03-03 20:02:02 +00001193 size_t Size = CurrentFnArguments.size();
1194 if (Size == 0)
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001195 CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001196 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001197 // arguments does the function have at source level.
1198 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001199 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001200 CurrentFnArguments[ArgNo - 1] = Var;
1201 return true;
1202}
1203
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001204// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001205void DwarfDebug::collectVariableInfoFromMMITable(
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001206 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001207 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1208 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001209 VE = VMap.end();
1210 VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001211 const MDNode *Var = VI->first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001212 if (!Var)
1213 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001214 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001215 DIVariable DV(Var);
1216 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001217
Devang Patel7e623022011-08-10 20:55:27 +00001218 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001219
Devang Patelcdb7d442009-11-10 23:20:04 +00001220 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001221 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001222 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001223
Devang Patele1c53f22010-05-20 16:36:41 +00001224 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001225 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001226 RegVar->setFrameIndex(VP.first);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001227 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001228 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001229 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001230 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001231 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001232}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001233
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001234// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1235// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001236static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001237 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001238 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1239 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001240 (MI->getOperand(1).isImm() ||
1241 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001242}
1243
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001244// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001245static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1246 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001247 const MCSymbol *SLabel,
1248 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001249 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001250
David Blaikie0252265b2013-06-16 20:34:15 +00001251 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001252 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001253 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001254 // If the second operand is an immediate, this is a
1255 // register-indirect address.
1256 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001257 MLoc.set(MI->getOperand(0).getReg());
1258 else
1259 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001260 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1261 }
1262 if (MI->getOperand(0).isImm())
1263 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1264 if (MI->getOperand(0).isFPImm())
1265 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1266 if (MI->getOperand(0).isCImm())
1267 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1268
Craig Topperee4dab52012-02-05 08:31:47 +00001269 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001270}
1271
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001272// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001273void
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001274DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001275
Eric Christopher270a12c2013-07-03 21:37:03 +00001276 // Grab the variable info that was squirreled away in the MMI side-table.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001277 collectVariableInfoFromMMITable(Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001278
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001279 for (SmallVectorImpl<const MDNode *>::const_iterator
1280 UVI = UserVariables.begin(),
1281 UVE = UserVariables.end();
1282 UVI != UVE; ++UVI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001283 const MDNode *Var = *UVI;
1284 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001285 continue;
1286
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001287 // History contains relevant DBG_VALUE instructions for Var and instructions
1288 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001289 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001290 if (History.empty())
1291 continue;
1292 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001293
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001294 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001295 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001296 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001297 DISubprogram(DV.getContext()).describes(CurFn->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001298 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001299 else if (MDNode *IA = DV.getInlinedAt())
1300 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1301 else
1302 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001303 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001304 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001305 continue;
1306
1307 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001308 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001309 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001310 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001311 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001312 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001313 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001314 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001315
Eric Christophercc10d202012-10-08 20:48:54 +00001316 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001317 if (History.size() <= 1 ||
1318 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001319 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001320 continue;
1321 }
1322
Eric Christopher59cc0712013-01-28 17:33:26 +00001323 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001324 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001325
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001326 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1327 HI = History.begin(),
1328 HE = History.end();
1329 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001330 const MachineInstr *Begin = *HI;
1331 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001332
Devang Patele7181b52011-06-01 23:00:17 +00001333 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001334 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1335 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001336 continue;
1337
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001338 // Compute the range for a register location.
1339 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1340 const MCSymbol *SLabel = 0;
1341
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001342 if (HI + 1 == HE)
1343 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001344 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001345 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001346 else {
1347 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001348 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001349 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001350 if (End->isDebugValue())
1351 SLabel = getLabelBeforeInsn(End);
1352 else {
1353 // End is a normal instruction clobbering the range.
1354 SLabel = getLabelAfterInsn(End);
1355 assert(SLabel && "Forgot label after clobber instruction");
1356 ++HI;
1357 }
1358 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001359
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001360 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001361 DotDebugLocEntries.push_back(
1362 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001363 }
1364 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001365 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001366
1367 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001368 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1369 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1370 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1371 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001372 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001373 continue;
1374 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001375 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001376 }
Devang Patel9fc11702010-05-25 23:40:22 +00001377}
Devang Patele0a94bf2010-05-14 21:01:35 +00001378
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001379// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001380MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001381 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1382 assert(Label && "Didn't insert label before instruction");
1383 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001384}
1385
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001386// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001387MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001388 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001389}
1390
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001391// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001392void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001393 assert(CurMI == 0);
1394 CurMI = MI;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001395 // Check if source location changes, but ignore DBG_VALUE locations.
1396 if (!MI->isDebugValue()) {
1397 DebugLoc DL = MI->getDebugLoc();
1398 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001399 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001400 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001401 if (DL == PrologEndLoc) {
1402 Flags |= DWARF2_FLAG_PROLOGUE_END;
1403 PrologEndLoc = DebugLoc();
1404 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001405 if (PrologEndLoc.isUnknown())
1406 Flags |= DWARF2_FLAG_IS_STMT;
1407
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001408 if (!DL.isUnknown()) {
1409 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001410 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001411 } else
Devang Patel34a66202011-05-11 19:22:19 +00001412 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001413 }
Devang Patel9fc11702010-05-25 23:40:22 +00001414 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001415
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001416 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001417 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1418 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001419
1420 // No label needed.
1421 if (I == LabelsBeforeInsn.end())
1422 return;
1423
1424 // Label already assigned.
1425 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001426 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001427
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001428 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001429 PrevLabel = MMI->getContext().CreateTempSymbol();
1430 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001431 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001432 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001433}
1434
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001435// Process end of an instruction.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001436void DwarfDebug::endInstruction() {
1437 assert(CurMI != 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001438 // Don't create a new label after DBG_VALUE instructions.
1439 // They don't generate code.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001440 if (!CurMI->isDebugValue())
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001441 PrevLabel = 0;
1442
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001443 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001444 LabelsAfterInsn.find(CurMI);
1445 CurMI = 0;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001446
1447 // No label needed.
1448 if (I == LabelsAfterInsn.end())
1449 return;
1450
1451 // Label already assigned.
1452 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001453 return;
1454
1455 // We need a label after this instruction.
1456 if (!PrevLabel) {
1457 PrevLabel = MMI->getContext().CreateTempSymbol();
1458 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001459 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001460 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001461}
1462
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001463// Each LexicalScope has first instruction and last instruction to mark
1464// beginning and end of a scope respectively. Create an inverse map that list
1465// scopes starts (and ends) with an instruction. One instruction may start (or
1466// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001467void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001468 SmallVector<LexicalScope *, 4> WorkList;
1469 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001470 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001471 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001472
Craig Topper977e9cd2013-07-03 04:24:43 +00001473 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001474 if (!Children.empty())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001475 for (SmallVectorImpl<LexicalScope *>::const_iterator
1476 SI = Children.begin(),
1477 SE = Children.end();
1478 SI != SE; ++SI)
Devang Patel7771b7c2010-01-20 02:05:23 +00001479 WorkList.push_back(*SI);
1480
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001481 if (S->isAbstractScope())
1482 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001483
Craig Topper977e9cd2013-07-03 04:24:43 +00001484 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001485 if (Ranges.empty())
1486 continue;
Craig Topperd8e43652013-07-03 04:17:25 +00001487 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001488 RE = Ranges.end();
1489 RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001490 assert(RI->first && "InsnRange does not have first instruction!");
1491 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001492 requestLabelBeforeInsn(RI->first);
1493 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001494 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001495 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001496}
1497
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001498// Gather pre-function debug information. Assumes being called immediately
1499// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001500void DwarfDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001501 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001502
1503 // If there's no debug info for the function we're not going to do anything.
1504 if (!MMI->hasDebugInfo())
1505 return;
1506
1507 // Grab the lexical scopes for the function, if we don't have any of those
1508 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001509 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001510 if (LScopes.empty())
1511 return;
1512
1513 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1514
1515 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001516 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001517
Eric Christopher4287a492013-12-09 23:57:44 +00001518 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001519 // belongs to so that we add to the correct per-cu line table in the
1520 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001521 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001522 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren4e042a62013-02-05 21:52:47 +00001523 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001524 if (Asm->OutStreamer.hasRawTextSupport())
1525 // Use a single line table if we are generating assembly.
Manman Ren9d4c7352013-05-21 00:57:22 +00001526 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1527 else
1528 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001529
Eric Christopher2037caf2014-01-28 00:49:26 +00001530 // Check the current section against the standard text section. If different
1531 // keep track so that we will know when we're emitting functions into multiple
1532 // sections.
1533 if (Asm->getObjFileLowering().getTextSection() != Asm->getCurrentSection())
1534 UsedNonDefaultText = true;
1535
Eric Christopherfedfa442013-11-01 23:14:17 +00001536 // Emit a label for the function so that we have a beginning address.
1537 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001538 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001539 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001540
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001541 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001542 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001543 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001544
Eric Christopherfedfa442013-11-01 23:14:17 +00001545 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1546 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001547 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001548 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1549 II != IE; ++II) {
1550 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001551
Devang Patel002d54d2010-05-26 19:37:24 +00001552 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001553 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001554
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001555 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001556 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001557 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001558
1559 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001560 if (isDbgValueInDefinedReg(MI))
1561 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1562
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001563 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001564 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001565 if (History.empty()) {
1566 UserVariables.push_back(Var);
1567 // The first mention of a function argument gets the FunctionBeginSym
1568 // label, so arguments are visible when breaking at function entry.
1569 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001570 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001571 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001572 LabelsBeforeInsn[MI] = FunctionBeginSym;
1573 } else {
1574 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1575 const MachineInstr *Prev = History.back();
1576 if (Prev->isDebugValue()) {
1577 // Coalesce identical entries at the end of History.
1578 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001579 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001580 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001581 << "\t" << *Prev << "\t"
1582 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001583 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001584 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001585
1586 // Terminate old register assignments that don't reach MI;
1587 MachineFunction::const_iterator PrevMBB = Prev->getParent();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001588 if (PrevMBB != I && (!AtBlockEntry || std::next(PrevMBB) != I) &&
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001589 isDbgValueInDefinedReg(Prev)) {
1590 // Previous register assignment needs to terminate at the end of
1591 // its basic block.
1592 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001593 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001594 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001595 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001596 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001597 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001598 History.pop_back();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001599 } else if (std::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001600 // Terminate after LastMI.
1601 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001602 }
1603 }
1604 }
1605 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001606 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001607 // Not a DBG_VALUE instruction.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001608 if (!MI->isPosition())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001609 AtBlockEntry = false;
1610
Eric Christopher133195782012-10-04 20:46:14 +00001611 // First known non-DBG_VALUE and non-frame setup location marks
1612 // the beginning of the function body.
1613 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1614 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001615 PrologEndLoc = MI->getDebugLoc();
1616
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001617 // Check if the instruction clobbers any registers with debug vars.
1618 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
Eric Christopherfedfa442013-11-01 23:14:17 +00001619 MOE = MI->operands_end();
1620 MOI != MOE; ++MOI) {
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001621 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1622 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001623 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true); AI.isValid();
1624 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001625 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001626 const MDNode *Var = LiveUserVar[Reg];
1627 if (!Var)
1628 continue;
1629 // Reg is now clobbered.
1630 LiveUserVar[Reg] = 0;
1631
1632 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001633 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1634 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001635 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001636 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001637 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001638 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001639 const MachineInstr *Prev = History.back();
1640 // Sanity-check: Register assignments are terminated at the end of
1641 // their block.
1642 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1643 continue;
1644 // Is the variable still in Reg?
1645 if (!isDbgValueInDefinedReg(Prev) ||
1646 Prev->getOperand(0).getReg() != Reg)
1647 continue;
1648 // Var is clobbered. Make sure the next instruction gets a label.
1649 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001650 }
1651 }
Devang Patel002d54d2010-05-26 19:37:24 +00001652 }
Devang Patel002d54d2010-05-26 19:37:24 +00001653 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001654 }
1655
1656 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1657 I != E; ++I) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001658 SmallVectorImpl<const MachineInstr *> &History = I->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001659 if (History.empty())
1660 continue;
1661
1662 // Make sure the final register assignments are terminated.
1663 const MachineInstr *Prev = History.back();
1664 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1665 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001666 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001667 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001668 if (LastMI == PrevMBB->end())
1669 // Drop DBG_VALUE for empty range.
1670 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001671 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001672 // Terminate after LastMI.
1673 History.push_back(LastMI);
1674 }
1675 }
1676 // Request labels for the full history.
1677 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1678 const MachineInstr *MI = History[i];
1679 if (MI->isDebugValue())
1680 requestLabelBeforeInsn(MI);
1681 else
1682 requestLabelAfterInsn(MI);
1683 }
1684 }
Devang Patel002d54d2010-05-26 19:37:24 +00001685
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001686 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001687 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001688
1689 // Record beginning of function.
1690 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001691 DebugLoc FnStartDL =
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00001692 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
Eric Christopherfedfa442013-11-01 23:14:17 +00001693 recordSourceLine(
1694 FnStartDL.getLine(), FnStartDL.getCol(),
1695 FnStartDL.getScope(MF->getFunction()->getContext()),
1696 // We'd like to list the prologue as "not statements" but GDB behaves
1697 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1698 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001699 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001700}
1701
Devang Patel7e623022011-08-10 20:55:27 +00001702void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001703 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1704 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001705 // Variables with positive arg numbers are parameters.
1706 if (unsigned ArgNum = DV.getArgNumber()) {
1707 // Keep all parameters in order at the start of the variable list to ensure
1708 // function types are correct (no out-of-order parameters)
1709 //
1710 // This could be improved by only doing it for optimized builds (unoptimized
1711 // builds have the right order to begin with), searching from the back (this
1712 // would catch the unoptimized case quickly), or doing a binary search
1713 // rather than linear search.
1714 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1715 while (I != Vars.end()) {
1716 unsigned CurNum = (*I)->getVariable().getArgNumber();
1717 // A local (non-parameter) variable has been found, insert immediately
1718 // before it.
1719 if (CurNum == 0)
1720 break;
1721 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001722 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001723 break;
David Blaikieb272a752013-06-06 22:28:26 +00001724 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001725 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001726 Vars.insert(I, Var);
1727 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001728 }
1729
1730 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001731}
1732
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001733// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001734void DwarfDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001735 // Every beginFunction(MF) call should be followed by an endFunction(MF) call,
1736 // though the beginFunction may not be called at all.
1737 // We should handle both cases.
1738 if (CurFn == 0)
1739 CurFn = MF;
1740 else
1741 assert(CurFn == MF);
1742 assert(CurFn != 0);
1743
1744 if (!MMI->hasDebugInfo() || LScopes.empty()) {
1745 CurFn = 0;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001746 return;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001747 }
Devang Patel2904aa92009-11-12 19:02:56 +00001748
Devang Patel7e623022011-08-10 20:55:27 +00001749 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001750 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001751 // Assumes in correct section after the entry point.
1752 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher1a972152014-01-29 23:05:43 +00001753
Eric Christopher4287a492013-12-09 23:57:44 +00001754 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Manman Ren4e042a62013-02-05 21:52:47 +00001755 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001756
Devang Patel7e623022011-08-10 20:55:27 +00001757 SmallPtrSet<const MDNode *, 16> ProcessedVars;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001758 collectVariableInfo(ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001759
Devang Patel3acc70e2011-08-15 22:04:40 +00001760 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001761 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001762 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001763
Devang Patel7e623022011-08-10 20:55:27 +00001764 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001765 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1766 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1767 LexicalScope *AScope = AList[i];
1768 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001769 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001770 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001771 DIArray Variables = SP.getVariables();
1772 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1773 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001774 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001775 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001776 // Check that DbgVariable for DV wasn't created earlier, when
1777 // findAbstractVariable() was called for inlined instance of DV.
1778 LLVMContext &Ctx = DV->getContext();
1779 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1780 if (AbstractVariables.lookup(CleanDV))
1781 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001782 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001783 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001784 }
1785 }
Devang Patel44403472011-08-12 18:10:19 +00001786 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001787 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001788 }
Eric Christopher6a841382012-11-19 22:42:10 +00001789
Devang Patel3acc70e2011-08-15 22:04:40 +00001790 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001791 if (!CurFn->getTarget().Options.DisableFramePointerElim(*CurFn))
Eric Christopherbb69a272012-08-24 01:14:27 +00001792 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001793
Eric Christopher1a972152014-01-29 23:05:43 +00001794 // Add the range of this function to the list of ranges for the CU.
1795 RangeSpan Span(FunctionBeginSym, FunctionEndSym);
Chandler Carruth002da5d2014-03-02 04:08:41 +00001796 TheCU->addRange(std::move(Span));
Eric Christopher1a972152014-01-29 23:05:43 +00001797
Bill Wendling2b128d72009-05-20 23:19:06 +00001798 // Clear debug info
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001799 for (ScopeVariablesMap::iterator I = ScopeVariables.begin(),
1800 E = ScopeVariables.end();
1801 I != E; ++I)
Devang Patel7e623022011-08-10 20:55:27 +00001802 DeleteContainerPointers(I->second);
1803 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001804 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001805 UserVariables.clear();
1806 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001807 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001808 LabelsBeforeInsn.clear();
1809 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001810 PrevLabel = NULL;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001811 CurFn = 0;
Bill Wendling2b128d72009-05-20 23:19:06 +00001812}
1813
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001814// Register a source line with debug info. Returns the unique label that was
1815// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001816void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1817 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001818 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001819 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001820 unsigned Src = 1;
Diego Novillo282450d2014-03-03 18:53:17 +00001821 unsigned Discriminator = 0;
Dan Gohman50849c62010-05-05 23:41:32 +00001822 if (S) {
1823 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001824
Dan Gohman50849c62010-05-05 23:41:32 +00001825 if (Scope.isCompileUnit()) {
1826 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001827 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001828 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001829 } else if (Scope.isFile()) {
1830 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001831 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001832 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001833 } else if (Scope.isSubprogram()) {
1834 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001835 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001836 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001837 } else if (Scope.isLexicalBlockFile()) {
1838 DILexicalBlockFile DBF(S);
1839 Fn = DBF.getFilename();
1840 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001841 } else if (Scope.isLexicalBlock()) {
1842 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001843 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001844 Dir = DB.getDirectory();
Diego Novillo282450d2014-03-03 18:53:17 +00001845 Discriminator = DB.getDiscriminator();
Dan Gohman50849c62010-05-05 23:41:32 +00001846 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001847 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001848
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001849 Src = getOrCreateSourceID(
1850 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001851 }
Diego Novillo282450d2014-03-03 18:53:17 +00001852 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1853 Discriminator, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001854}
1855
Bill Wendling806535f2009-05-20 23:22:40 +00001856//===----------------------------------------------------------------------===//
1857// Emit Methods
1858//===----------------------------------------------------------------------===//
1859
Manman Rence20d462013-10-29 22:57:10 +00001860// Compute the size and offset of a DIE. The offset is relative to start of the
1861// CU. It returns the offset after laying out the DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001862unsigned DwarfFile::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001863 // Get the children.
1864 const std::vector<DIE *> &Children = Die->getChildren();
1865
Bill Wendling480ff322009-05-20 23:21:38 +00001866 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001867 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001868
1869 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001870 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001871
1872 // Set DIE offset
1873 Die->setOffset(Offset);
1874
1875 // Start the size with the size of abbreviation code.
Logan Chien5b776b72014-02-22 14:00:39 +00001876 Offset += getULEB128Size(Die->getAbbrevNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001877
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001878 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001879 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001880
1881 // Size the DIE attribute values.
1882 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1883 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001884 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001885
1886 // Size the DIE children if any.
1887 if (!Children.empty()) {
Eric Christophere8f10722014-03-05 01:44:58 +00001888 assert(Abbrev.hasChildren() && "Children flag not set");
Bill Wendling480ff322009-05-20 23:21:38 +00001889
1890 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001891 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001892
1893 // End of children marker.
1894 Offset += sizeof(int8_t);
1895 }
1896
1897 Die->setSize(Offset - Die->getOffset());
1898 return Offset;
1899}
1900
Eric Christopherb088d2d2013-10-24 21:05:08 +00001901// Compute the size and offset for each DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001902void DwarfFile::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001903 // Offset from the first CU in the debug info section is 0 initially.
1904 unsigned SecOffset = 0;
1905
Eric Christopherb088d2d2013-10-24 21:05:08 +00001906 // Iterate over each compile unit and set the size and offsets for each
1907 // DIE within each compile unit. All offsets are CU relative.
Eric Christophera5a79422013-12-09 23:32:48 +00001908 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = CUs.begin(),
1909 E = CUs.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001910 I != E; ++I) {
Manman Rence20d462013-10-29 22:57:10 +00001911 (*I)->setDebugInfoOffset(SecOffset);
1912
1913 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001914 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
David Blaikie6b288cf2013-10-30 20:42:41 +00001915 (*I)->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001916
1917 // EndOffset here is CU-relative, after laying out
1918 // all of the CU DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001919 unsigned EndOffset = computeSizeAndOffset((*I)->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001920 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001921 }
Bill Wendling480ff322009-05-20 23:21:38 +00001922}
1923
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001924// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001925void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001926 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001927
Bill Wendling480ff322009-05-20 23:21:38 +00001928 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001929 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001930 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Eric Christopherd8667202013-12-30 17:22:27 +00001931 if (useSplitDwarf())
1932 DwarfInfoDWOSectionSym =
1933 emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001934 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001935 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001936 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001937 DwarfAbbrevDWOSectionSym = emitSectionSym(
1938 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
David Blaikie20474102014-02-25 22:46:44 +00001939 if (GenerateARangeSection)
1940 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001941
Eric Christopher74804332013-02-07 21:19:50 +00001942 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001943 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001944 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001945 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001946 DwarfGnuPubNamesSectionSym =
1947 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1948 DwarfGnuPubTypesSectionSym =
1949 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001950 } else if (HasDwarfPubSections) {
1951 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1952 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001953 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001954
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001955 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001956 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001957 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001958 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001959 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001960 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001961 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00001962 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001963 DwarfDebugRangeSectionSym =
1964 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001965
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001966 DwarfDebugLocSectionSym =
1967 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Bill Wendling480ff322009-05-20 23:21:38 +00001968}
1969
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001970// Recursively emits a debug information entry.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001971void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001972 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001973 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001974
Bill Wendling480ff322009-05-20 23:21:38 +00001975 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001976 if (Asm->isVerbose())
David Blaikieff3ab2c2013-12-05 01:01:41 +00001977 Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
1978 "] 0x" + Twine::utohexstr(Die->getOffset()) +
1979 ":0x" + Twine::utohexstr(Die->getSize()) + " " +
1980 dwarf::TagString(Abbrev.getTag()));
1981 Asm->EmitULEB128(Abbrev.getNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001982
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001983 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001984 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001985
1986 // Emit the DIE attribute values.
1987 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00001988 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
1989 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00001990 assert(Form && "Too many attributes for DIE (check abbreviation)");
1991
Eric Christopher13a1bb32014-03-06 00:00:49 +00001992 if (Asm->isVerbose()) {
Chris Lattner5adf9872010-01-24 18:54:17 +00001993 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Eric Christopher13a1bb32014-03-06 00:00:49 +00001994 if (Attr == dwarf::DW_AT_accessibility)
1995 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(
1996 cast<DIEInteger>(Values[i])->getValue()));
1997 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001998
Eric Christopherdd508382014-03-06 00:00:56 +00001999 // Emit an attribute using the defined form.
2000 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002001 }
2002
2003 // Emit the DIE children if any.
Eric Christophere8f10722014-03-05 01:44:58 +00002004 if (Abbrev.hasChildren()) {
Bill Wendling480ff322009-05-20 23:21:38 +00002005 const std::vector<DIE *> &Children = Die->getChildren();
2006
2007 for (unsigned j = 0, M = Children.size(); j < M; ++j)
David Blaikieff3ab2c2013-12-05 01:01:41 +00002008 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00002009
David Blaikie155f8812013-12-04 21:51:05 +00002010 Asm->OutStreamer.AddComment("End Of Children Mark");
Chris Lattner566cae92010-03-09 23:52:58 +00002011 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002012 }
2013}
2014
Eric Christophera2de8262012-12-15 00:04:07 +00002015// Emit the various dwarf units to the unit section USection with
2016// the abbreviations going into ASection.
David Blaikie03073f72013-12-06 22:14:48 +00002017void DwarfFile::emitUnits(DwarfDebug *DD, const MCSection *ASection,
2018 const MCSymbol *ASectionSym) {
Eric Christophera5a79422013-12-09 23:32:48 +00002019 for (SmallVectorImpl<DwarfUnit *>::iterator I = CUs.begin(), E = CUs.end();
2020 I != E; ++I) {
2021 DwarfUnit *TheU = *I;
David Blaikie2a80e442013-12-02 22:09:48 +00002022 DIE *Die = TheU->getUnitDie();
David Blaikie03073f72013-12-06 22:14:48 +00002023 const MCSection *USection = TheU->getSection();
2024 Asm->OutStreamer.SwitchSection(USection);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002025
Devang Patel1a0df9a2010-05-10 22:49:55 +00002026 // Emit the compile units header.
David Blaikie7d734602013-12-06 22:33:05 +00002027 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002028
Devang Patel1a0df9a2010-05-10 22:49:55 +00002029 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00002030 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00002031 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002032
David Blaikie2a80e442013-12-02 22:09:48 +00002033 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002034
David Blaikieff3ab2c2013-12-05 01:01:41 +00002035 DD->emitDIE(Die);
David Blaikie7d734602013-12-06 22:33:05 +00002036 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
Devang Patel1a0df9a2010-05-10 22:49:55 +00002037 }
Bill Wendling480ff322009-05-20 23:21:38 +00002038}
2039
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002040// Emit the debug info section.
2041void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00002042 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christophera2de8262012-12-15 00:04:07 +00002043
David Blaikie03073f72013-12-06 22:14:48 +00002044 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfAbbrevSection(),
Eric Christophera2de8262012-12-15 00:04:07 +00002045 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002046}
2047
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002048// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002049void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00002050 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00002051
2052 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00002053}
Bill Wendling480ff322009-05-20 23:21:38 +00002054
Eric Christopherf8194852013-12-05 18:06:10 +00002055void DwarfFile::emitAbbrevs(const MCSection *Section) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002056 // Check to see if it is worth the effort.
David Blaikie0504cda2013-12-05 07:43:55 +00002057 if (!Abbreviations.empty()) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002058 // Start the debug abbrev section.
2059 Asm->OutStreamer.SwitchSection(Section);
2060
Bill Wendling480ff322009-05-20 23:21:38 +00002061 // For each abbrevation.
David Blaikie0504cda2013-12-05 07:43:55 +00002062 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00002063 // Get abbreviation data
David Blaikie0504cda2013-12-05 07:43:55 +00002064 const DIEAbbrev *Abbrev = Abbreviations[i];
Bill Wendling480ff322009-05-20 23:21:38 +00002065
2066 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002067 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002068
2069 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002070 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002071 }
2072
2073 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002074 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002075 }
2076}
2077
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002078// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002079void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002080 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002081 Asm->OutStreamer.AddComment("Extended Op");
2082 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002083
Chris Lattner566cae92010-03-09 23:52:58 +00002084 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002085 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002086 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2087 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2088
2089 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002090
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002091 Asm->OutStreamer.EmitSymbolValue(
2092 Asm->GetTempSymbol("section_end", SectionEnd),
2093 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002094
2095 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002096 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2097 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002098 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002099 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002100}
2101
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002102// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002103void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002104 DwarfAccelTable AT(
2105 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Eric Christophera5a79422013-12-09 23:32:48 +00002106 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2107 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002108 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002109 DwarfUnit *TheU = *I;
David Blaikie2a80e442013-12-02 22:09:48 +00002110 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelNames();
David Blaikie2ea848b2013-11-19 22:51:04 +00002111 for (StringMap<std::vector<const DIE *> >::const_iterator
2112 GI = Names.begin(),
2113 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002114 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002115 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002116 const std::vector<const DIE *> &Entities = GI->second;
2117 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2118 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002119 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002120 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002121 }
2122 }
2123
2124 AT.FinalizeTable(Asm, "Names");
2125 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002126 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002127 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2128 Asm->OutStreamer.EmitLabel(SectionBegin);
2129
2130 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002131 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002132}
2133
Eric Christopher48fef592012-12-20 21:58:40 +00002134// Emit objective C classes and categories into a hashed accelerator table
2135// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002136void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002137 DwarfAccelTable AT(
2138 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Eric Christophera5a79422013-12-09 23:32:48 +00002139 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2140 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002141 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002142 DwarfUnit *TheU = *I;
David Blaikie2a80e442013-12-02 22:09:48 +00002143 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelObjC();
David Blaikie2ea848b2013-11-19 22:51:04 +00002144 for (StringMap<std::vector<const DIE *> >::const_iterator
2145 GI = Names.begin(),
2146 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002147 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002148 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002149 const std::vector<const DIE *> &Entities = GI->second;
2150 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2151 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002152 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002153 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002154 }
2155 }
2156
2157 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002158 Asm->OutStreamer.SwitchSection(
2159 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002160 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2161 Asm->OutStreamer.EmitLabel(SectionBegin);
2162
2163 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002164 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002165}
2166
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002167// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002168void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002169 DwarfAccelTable AT(
2170 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Eric Christophera5a79422013-12-09 23:32:48 +00002171 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2172 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002173 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002174 DwarfUnit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002175 const StringMap<std::vector<const DIE *> > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002176 TheU->getAccelNamespace();
David Blaikie2ea848b2013-11-19 22:51:04 +00002177 for (StringMap<std::vector<const DIE *> >::const_iterator
2178 GI = Names.begin(),
2179 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002180 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002181 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002182 const std::vector<const DIE *> &Entities = GI->second;
2183 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2184 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002185 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002186 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002187 }
2188 }
2189
2190 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002191 Asm->OutStreamer.SwitchSection(
2192 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002193 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2194 Asm->OutStreamer.EmitLabel(SectionBegin);
2195
2196 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002197 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002198}
2199
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002200// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002201void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002202 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002203 Atoms.push_back(
2204 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2205 Atoms.push_back(
2206 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2207 Atoms.push_back(
2208 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002209 DwarfAccelTable AT(Atoms);
Eric Christophera5a79422013-12-09 23:32:48 +00002210 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2211 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002212 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002213 DwarfUnit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002214 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002215 TheU->getAccelTypes();
David Blaikie2ea848b2013-11-19 22:51:04 +00002216 for (StringMap<
2217 std::vector<std::pair<const DIE *, unsigned> > >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002218 GI = Names.begin(),
2219 GE = Names.end();
2220 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002221 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002222 const std::vector<std::pair<const DIE *, unsigned> > &Entities =
2223 GI->second;
2224 for (std::vector<std::pair<const DIE *, unsigned> >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002225 DI = Entities.begin(),
2226 DE = Entities.end();
2227 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002228 AT.AddName(Name, DI->first, DI->second);
Eric Christopher4996c702011-11-07 09:24:32 +00002229 }
2230 }
2231
2232 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002233 Asm->OutStreamer.SwitchSection(
2234 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002235 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2236 Asm->OutStreamer.EmitLabel(SectionBegin);
2237
2238 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002239 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002240}
2241
Eric Christopherdd1a0122013-09-13 00:35:05 +00002242// Public name handling.
2243// The format for the various pubnames:
2244//
2245// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2246// for the DIE that is named.
2247//
2248// gnu pubnames - offset/index value/name tuples where the offset is the offset
2249// into the CU and the index value is computed according to the type of value
2250// for the DIE that is named.
2251//
2252// For type units the offset is the offset of the skeleton DIE. For split dwarf
2253// it's the offset within the debug_info/debug_types dwo section, however, the
2254// reference in the pubname header doesn't change.
2255
2256/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00002257static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002258 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002259 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2260
2261 // We could have a specification DIE that has our most of our knowledge,
2262 // look for that now.
2263 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2264 if (SpecVal) {
2265 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2266 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2267 Linkage = dwarf::GIEL_EXTERNAL;
2268 } else if (Die->findAttribute(dwarf::DW_AT_external))
2269 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002270
2271 switch (Die->getTag()) {
2272 case dwarf::DW_TAG_class_type:
2273 case dwarf::DW_TAG_structure_type:
2274 case dwarf::DW_TAG_union_type:
2275 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002276 return dwarf::PubIndexEntryDescriptor(
2277 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2278 ? dwarf::GIEL_STATIC
2279 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002280 case dwarf::DW_TAG_typedef:
2281 case dwarf::DW_TAG_base_type:
2282 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002283 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002284 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002285 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002286 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002287 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002288 case dwarf::DW_TAG_constant:
2289 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002290 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002291 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002292 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2293 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002294 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002295 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002296 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002297}
2298
Eric Christopher5f93bb92013-09-09 20:03:17 +00002299/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002300///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002301void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Eric Christopherdd1a0122013-09-13 00:35:05 +00002302 const MCSection *PSec =
2303 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2304 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002305
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002306 for (const auto &NU : CUMap) {
2307 DwarfCompileUnit *TheU = NU.second;
2308 if (auto Skeleton = static_cast<DwarfCompileUnit *>(TheU->getSkeleton()))
2309 TheU = Skeleton;
David Blaikie2a80e442013-12-02 22:09:48 +00002310 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002311
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002312 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002313 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002314
Eric Christopher39eebfa2013-09-30 23:14:16 +00002315 // Emit a label so we can reference the beginning of this pubname section.
2316 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002317 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubnames", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002318
Eric Christopherdd1a0122013-09-13 00:35:05 +00002319 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002320 Asm->OutStreamer.AddComment("Length of Public Names Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002321 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubnames_begin", ID);
2322 MCSymbol *EndLabel = Asm->GetTempSymbol("pubnames_end", ID);
2323 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002324
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002325 Asm->OutStreamer.EmitLabel(BeginLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002326
2327 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002328 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002329
2330 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002331 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002332
2333 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002334 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002335
Eric Christopherdd1a0122013-09-13 00:35:05 +00002336 // Emit the pubnames for this compilation unit.
David Blaikie4f623202013-12-06 19:38:49 +00002337 const StringMap<const DIE *> &Globals = getUnits()[ID]->getGlobalNames();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002338 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2339 GE = Globals.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002340 GI != GE; ++GI) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002341 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002342 const DIE *Entity = GI->second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002343
2344 Asm->OutStreamer.AddComment("DIE offset");
2345 Asm->EmitInt32(Entity->getOffset());
2346
Eric Christopherdd1a0122013-09-13 00:35:05 +00002347 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002348 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002349 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002350 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002351 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002352 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002353 }
2354
David Blaikie155f8812013-12-04 21:51:05 +00002355 Asm->OutStreamer.AddComment("External Name");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002356 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002357 }
2358
2359 Asm->OutStreamer.AddComment("End Mark");
2360 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002361 Asm->OutStreamer.EmitLabel(EndLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002362 }
2363}
2364
Eric Christopherdd1a0122013-09-13 00:35:05 +00002365void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher261d2342013-09-23 20:55:35 +00002366 const MCSection *PSec =
2367 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2368 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002369
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002370 for (const auto &NU : CUMap) {
2371 DwarfCompileUnit *TheU = NU.second;
2372 if (auto Skeleton = static_cast<DwarfCompileUnit *>(TheU->getSkeleton()))
2373 TheU = Skeleton;
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002374 unsigned ID = TheU->getUniqueID();
2375
Eric Christopher6abc9c52011-11-07 09:18:35 +00002376 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002377 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002378
2379 // Emit a label so we can reference the beginning of this pubtype section.
2380 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002381 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubtypes", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002382
2383 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002384 Asm->OutStreamer.AddComment("Length of Public Types Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002385 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubtypes_begin", ID);
2386 MCSymbol *EndLabel = Asm->GetTempSymbol("pubtypes_end", ID);
2387 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002388
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002389 Asm->OutStreamer.EmitLabel(BeginLabel);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002390
David Blaikie155f8812013-12-04 21:51:05 +00002391 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002392 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002393
Devang Patel1a0df9a2010-05-10 22:49:55 +00002394 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002395 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002396
Devang Patel1a0df9a2010-05-10 22:49:55 +00002397 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002398 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002399
Eric Christopher39eebfa2013-09-30 23:14:16 +00002400 // Emit the pubtypes.
David Blaikie4f623202013-12-06 19:38:49 +00002401 const StringMap<const DIE *> &Globals = getUnits()[ID]->getGlobalTypes();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002402 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2403 GE = Globals.end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002404 GI != GE; ++GI) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002405 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002406 const DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002407
David Blaikie155f8812013-12-04 21:51:05 +00002408 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002409 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002410
Eric Christopherdd1a0122013-09-13 00:35:05 +00002411 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002412 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002413 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002414 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002415 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002416 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002417 }
2418
David Blaikie155f8812013-12-04 21:51:05 +00002419 Asm->OutStreamer.AddComment("External Name");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002420
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002421 // Emit the name with a terminating null byte.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002422 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002423 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002424
Devang Patel1a0df9a2010-05-10 22:49:55 +00002425 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002426 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002427 Asm->OutStreamer.EmitLabel(EndLabel);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002428 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002429}
2430
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002431// Emit strings into a string section.
Eric Christopherf8194852013-12-05 18:06:10 +00002432void DwarfFile::emitStrings(const MCSection *StrSection,
Eric Christophera5a79422013-12-09 23:32:48 +00002433 const MCSection *OffsetSection = NULL,
2434 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002435
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002436 if (StringPool.empty())
2437 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002438
Chris Lattner3d72a672010-03-09 23:38:23 +00002439 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002440 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002441
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002442 // Get all of the string pool entries and put them in an array by their ID so
2443 // we can sort them.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002444 SmallVector<
2445 std::pair<unsigned, StringMapEntry<std::pair<MCSymbol *, unsigned> > *>,
2446 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002447
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002448 for (StringMap<std::pair<MCSymbol *, unsigned> >::iterator
2449 I = StringPool.begin(),
2450 E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002451 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002452 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002453
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002454 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002455
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002456 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002457 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002458 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002459
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002460 // Emit the string itself with a terminating null byte.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002461 Asm->OutStreamer.EmitBytes(
2462 StringRef(Entries[i].second->getKeyData(),
2463 Entries[i].second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002464 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002465
2466 // If we've got an offset section go ahead and emit that now as well.
2467 if (OffsetSection) {
2468 Asm->OutStreamer.SwitchSection(OffsetSection);
2469 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002470 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002471 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002472 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002473 offset += Entries[i].second->getKeyLength() + 1;
2474 }
2475 }
Bill Wendling480ff322009-05-20 23:21:38 +00002476}
2477
Eric Christopher65132a82013-11-19 09:11:26 +00002478// Emit addresses into the section given.
Eric Christopherf8194852013-12-05 18:06:10 +00002479void DwarfFile::emitAddresses(const MCSection *AddrSection) {
Eric Christopher962c9082013-01-15 23:56:56 +00002480
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002481 if (AddressPool.empty())
2482 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002483
2484 // Start the dwarf addr section.
2485 Asm->OutStreamer.SwitchSection(AddrSection);
2486
David Blaikiece1960f2013-07-08 17:51:28 +00002487 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002488 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002489
David Blaikief1a6dea2014-02-15 19:34:03 +00002490 for (AddrPool::iterator I = AddressPool.begin(), E = AddressPool.end();
Eric Christopher962c9082013-01-15 23:56:56 +00002491 I != E; ++I)
David Blaikief1a6dea2014-02-15 19:34:03 +00002492 Entries[I->second.Number] =
2493 I->second.TLS
2494 ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I->first)
2495 : MCSymbolRefExpr::Create(I->first, Asm->OutContext);
Eric Christopher962c9082013-01-15 23:56:56 +00002496
David Blaikief28703a2014-02-15 18:33:11 +00002497 for (unsigned i = 0, e = Entries.size(); i != e; ++i)
2498 Asm->OutStreamer.EmitValue(Entries[i],
2499 Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002500}
2501
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002502// Emit visible names into a debug str section.
2503void DwarfDebug::emitDebugStr() {
Eric Christopherf8194852013-12-05 18:06:10 +00002504 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002505 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2506}
2507
Eric Christopher9046f942013-07-02 21:36:07 +00002508// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002509void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002510 if (DotDebugLocEntries.empty())
2511 return;
2512
Eric Christopher4887c8f2013-03-29 23:34:06 +00002513 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002514 I = DotDebugLocEntries.begin(),
2515 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002516 I != E; ++I) {
2517 DotDebugLocEntry &Entry = *I;
2518 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002519 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002520 }
2521
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002522 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002523 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002524 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002525 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002526 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2527 unsigned index = 1;
Eric Christophereeb51952014-03-06 19:51:16 +00002528 for (SmallVectorImpl<DotDebugLocEntry>::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002529 I = DotDebugLocEntries.begin(),
2530 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002531 I != E; ++I, ++index) {
Eric Christophereeb51952014-03-06 19:51:16 +00002532 const DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002533 if (Entry.isMerged())
2534 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002535 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002536 Asm->OutStreamer.EmitIntValue(0, Size);
2537 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002538 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002539 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002540 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2541 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2542 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002543 Asm->OutStreamer.AddComment("Loc expr size");
2544 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2545 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2546 Asm->EmitLabelDifference(end, begin, 2);
2547 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002548 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002549 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002550 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2551 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002552 Asm->OutStreamer.AddComment("DW_OP_consts");
2553 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002554 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002555 } else {
2556 Asm->OutStreamer.AddComment("DW_OP_constu");
2557 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002558 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002559 }
Devang Pateled9fd452011-07-08 16:49:43 +00002560 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002561 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002562 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002563 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002564 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002565 else {
2566 // Complex address entry.
2567 unsigned N = DV.getNumAddrElements();
2568 unsigned i = 0;
2569 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002570 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002571 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002572 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002573 Asm->OutStreamer.AddComment("DW_OP_deref");
2574 Asm->EmitInt8(dwarf::DW_OP_deref);
2575 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2576 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2577 Asm->EmitSLEB128(DV.getAddrElement(1));
2578 } else {
2579 // If first address element is OpPlus then emit
2580 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002581 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2582 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002583 i = 2;
2584 }
2585 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002586 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002587 }
Eric Christopher6a841382012-11-19 22:42:10 +00002588
Devang Pateled9fd452011-07-08 16:49:43 +00002589 // Emit remaining complex address elements.
2590 for (; i < N; ++i) {
2591 uint64_t Element = DV.getAddrElement(i);
2592 if (Element == DIBuilder::OpPlus) {
2593 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2594 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002595 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002596 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002597 Asm->EmitInt8(dwarf::DW_OP_deref);
2598 } else
2599 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002600 }
Devang Patel3e021532011-04-28 02:22:40 +00002601 }
Devang Patel3e021532011-04-28 02:22:40 +00002602 }
Devang Pateled9fd452011-07-08 16:49:43 +00002603 // else ... ignore constant fp. There is not any good way to
2604 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002605 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002606 }
2607 }
Bill Wendling480ff322009-05-20 23:21:38 +00002608}
2609
Richard Mitton21101b32013-09-19 23:21:01 +00002610struct SymbolCUSorter {
2611 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2612 const MCStreamer &Streamer;
2613
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002614 bool operator()(const SymbolCU &A, const SymbolCU &B) {
Richard Mitton21101b32013-09-19 23:21:01 +00002615 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2616 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2617
2618 // Symbols with no order assigned should be placed at the end.
2619 // (e.g. section end labels)
2620 if (IA == 0)
2621 IA = (unsigned)(-1);
2622 if (IB == 0)
2623 IB = (unsigned)(-1);
2624 return IA < IB;
2625 }
2626};
2627
Eric Christophera5a79422013-12-09 23:32:48 +00002628static bool CUSort(const DwarfUnit *A, const DwarfUnit *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002629 return (A->getUniqueID() < B->getUniqueID());
Richard Mitton21101b32013-09-19 23:21:01 +00002630}
2631
2632struct ArangeSpan {
2633 const MCSymbol *Start, *End;
2634};
2635
2636// Emit a debug aranges section, containing a CU lookup for any
2637// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002638void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002639 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002640 Asm->OutStreamer.SwitchSection(
2641 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002642
Eric Christopher4287a492013-12-09 23:57:44 +00002643 typedef DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan> > SpansType;
Richard Mitton21101b32013-09-19 23:21:01 +00002644
2645 SpansType Spans;
2646
2647 // Build a list of sections used.
2648 std::vector<const MCSection *> Sections;
2649 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
2650 it++) {
2651 const MCSection *Section = it->first;
2652 Sections.push_back(Section);
2653 }
2654
2655 // Sort the sections into order.
2656 // This is only done to ensure consistent output order across different runs.
2657 std::sort(Sections.begin(), Sections.end(), SectionSort);
2658
2659 // Build a set of address spans, sorted by CU.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002660 for (size_t SecIdx = 0; SecIdx < Sections.size(); SecIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002661 const MCSection *Section = Sections[SecIdx];
2662 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2663 if (List.size() < 2)
2664 continue;
2665
2666 // Sort the symbols by offset within the section.
2667 SymbolCUSorter sorter(Asm->OutStreamer);
2668 std::sort(List.begin(), List.end(), sorter);
2669
2670 // If we have no section (e.g. common), just write out
2671 // individual spans for each symbol.
2672 if (Section == NULL) {
2673 for (size_t n = 0; n < List.size(); n++) {
2674 const SymbolCU &Cur = List[n];
2675
2676 ArangeSpan Span;
2677 Span.Start = Cur.Sym;
2678 Span.End = NULL;
2679 if (Cur.CU)
2680 Spans[Cur.CU].push_back(Span);
2681 }
2682 } else {
2683 // Build spans between each label.
2684 const MCSymbol *StartSym = List[0].Sym;
2685 for (size_t n = 1; n < List.size(); n++) {
2686 const SymbolCU &Prev = List[n - 1];
2687 const SymbolCU &Cur = List[n];
2688
2689 // Try and build the longest span we can within the same CU.
2690 if (Cur.CU != Prev.CU) {
2691 ArangeSpan Span;
2692 Span.Start = StartSym;
2693 Span.End = Cur.Sym;
2694 Spans[Prev.CU].push_back(Span);
2695 StartSym = Cur.Sym;
2696 }
2697 }
2698 }
2699 }
2700
Richard Mitton21101b32013-09-19 23:21:01 +00002701 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2702
2703 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00002704 std::vector<DwarfCompileUnit *> CUs;
Richard Mitton21101b32013-09-19 23:21:01 +00002705 for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
Eric Christopher4287a492013-12-09 23:57:44 +00002706 DwarfCompileUnit *CU = it->first;
Richard Mitton21101b32013-09-19 23:21:01 +00002707 CUs.push_back(CU);
2708 }
2709
2710 // Sort the CU list (again, to ensure consistent output order).
2711 std::sort(CUs.begin(), CUs.end(), CUSort);
2712
2713 // Emit an arange table for each CU we used.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002714 for (size_t CUIdx = 0; CUIdx < CUs.size(); CUIdx++) {
Eric Christopher4287a492013-12-09 23:57:44 +00002715 DwarfCompileUnit *CU = CUs[CUIdx];
Richard Mitton21101b32013-09-19 23:21:01 +00002716 std::vector<ArangeSpan> &List = Spans[CU];
2717
2718 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002719 unsigned ContentSize =
2720 sizeof(int16_t) + // DWARF ARange version number
2721 sizeof(int32_t) + // Offset of CU in the .debug_info section
2722 sizeof(int8_t) + // Pointer Size (in bytes)
2723 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002724
2725 unsigned TupleSize = PtrSize * 2;
2726
2727 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002728 unsigned Padding =
2729 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00002730
2731 ContentSize += Padding;
2732 ContentSize += (List.size() + 1) * TupleSize;
2733
2734 // For each compile unit, write the list of spans it covers.
2735 Asm->OutStreamer.AddComment("Length of ARange Set");
2736 Asm->EmitInt32(ContentSize);
2737 Asm->OutStreamer.AddComment("DWARF Arange version number");
2738 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2739 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Eric Christopherd8667202013-12-30 17:22:27 +00002740 Asm->EmitSectionOffset(CU->getLocalLabelBegin(), CU->getLocalSectionSym());
Richard Mitton21101b32013-09-19 23:21:01 +00002741 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2742 Asm->EmitInt8(PtrSize);
2743 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2744 Asm->EmitInt8(0);
2745
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002746 Asm->OutStreamer.EmitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00002747
2748 for (unsigned n = 0; n < List.size(); n++) {
2749 const ArangeSpan &Span = List[n];
2750 Asm->EmitLabelReference(Span.Start, PtrSize);
2751
2752 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002753 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002754 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002755 } else {
2756 // For symbols without an end marker (e.g. common), we
2757 // write a single arange entry containing just that one symbol.
2758 uint64_t Size = SymSize[Span.Start];
2759 if (Size == 0)
2760 Size = 1;
2761
2762 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2763 }
Richard Mitton21101b32013-09-19 23:21:01 +00002764 }
2765
2766 Asm->OutStreamer.AddComment("ARange terminator");
2767 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2768 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2769 }
Bill Wendling480ff322009-05-20 23:21:38 +00002770}
2771
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002772// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002773void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002774 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002775 Asm->OutStreamer.SwitchSection(
2776 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002777
Eric Christopher0f63d062013-12-03 00:45:45 +00002778 // Size for our labels.
2779 unsigned char Size = Asm->getDataLayout().getPointerSize();
2780
2781 // Grab the specific ranges for the compile units in the module.
Eric Christopher179fba12014-01-29 22:06:23 +00002782 for (MapVector<const MDNode *, DwarfCompileUnit *>::iterator
2783 I = CUMap.begin(),
2784 E = CUMap.end();
Eric Christopher0f63d062013-12-03 00:45:45 +00002785 I != E; ++I) {
Eric Christopher4287a492013-12-09 23:57:44 +00002786 DwarfCompileUnit *TheCU = I->second;
Eric Christopher0f63d062013-12-03 00:45:45 +00002787
2788 // Emit a symbol so we can find the beginning of our ranges.
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002789 Asm->OutStreamer.EmitLabel(TheCU->getLabelRange());
Eric Christopher0f63d062013-12-03 00:45:45 +00002790
2791 // Iterate over the misc ranges for the compile units in the module.
Eric Christopher270ba4a2013-12-04 19:06:58 +00002792 const SmallVectorImpl<RangeSpanList> &RangeLists = TheCU->getRangeLists();
Eric Christophera5a79422013-12-09 23:32:48 +00002793 for (SmallVectorImpl<RangeSpanList>::const_iterator I = RangeLists.begin(),
2794 E = RangeLists.end();
Eric Christopher0f63d062013-12-03 00:45:45 +00002795 I != E; ++I) {
Eric Christopher270ba4a2013-12-04 19:06:58 +00002796 const RangeSpanList &List = *I;
Eric Christopher0f63d062013-12-03 00:45:45 +00002797
Eric Christopherf8790642013-12-04 22:04:50 +00002798 // Emit our symbol so we can find the beginning of the range.
2799 Asm->OutStreamer.EmitLabel(List.getSym());
Eric Christopher0f63d062013-12-03 00:45:45 +00002800
2801 for (SmallVectorImpl<RangeSpan>::const_iterator
Eric Christopher270ba4a2013-12-04 19:06:58 +00002802 RI = List.getRanges().begin(),
2803 RE = List.getRanges().end();
2804 RI != RE; ++RI) {
2805 const RangeSpan &Range = *RI;
Eric Christopher0f63d062013-12-03 00:45:45 +00002806 const MCSymbol *Begin = Range.getStart();
2807 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002808 assert(Begin && "Range without a begin symbol?");
2809 assert(End && "Range without an end symbol?");
2810 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2811 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002812 }
2813
2814 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002815 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002816 Asm->OutStreamer.EmitIntValue(0, Size);
2817 }
Eric Christopher46e23432013-12-20 04:16:18 +00002818
2819 // Now emit a range for the CU itself.
Eric Christophera9a1d272014-02-27 07:44:45 +00002820 if (useCURanges() && TheCU->getRanges().size()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002821 Asm->OutStreamer.EmitLabel(
2822 Asm->GetTempSymbol("cu_ranges", TheCU->getUniqueID()));
2823 const SmallVectorImpl<RangeSpan> &Ranges = TheCU->getRanges();
2824 for (uint32_t i = 0, e = Ranges.size(); i != e; ++i) {
2825 RangeSpan Range = Ranges[i];
Eric Christopher46e23432013-12-20 04:16:18 +00002826 const MCSymbol *Begin = Range.getStart();
2827 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002828 assert(Begin && "Range without a begin symbol?");
2829 assert(End && "Range without an end symbol?");
2830 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2831 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher46e23432013-12-20 04:16:18 +00002832 }
2833 // And terminate the list with two 0 values.
2834 Asm->OutStreamer.EmitIntValue(0, Size);
2835 Asm->OutStreamer.EmitIntValue(0, Size);
2836 }
Devang Patel12563b32010-04-16 23:33:45 +00002837 }
Bill Wendling480ff322009-05-20 23:21:38 +00002838}
2839
Eric Christopherd692c1d2012-12-11 19:42:09 +00002840// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002841
David Blaikie38fe6342014-01-09 04:28:46 +00002842void DwarfDebug::initSkeletonUnit(const DwarfUnit *U, DIE *Die,
2843 DwarfUnit *NewU) {
2844 NewU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
2845 U->getCUNode().getSplitDebugFilename());
2846
2847 // Relocate to the beginning of the addr_base section, else 0 for the
2848 // beginning of the one for this compile unit.
2849 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherceec7b02014-01-11 00:23:18 +00002850 NewU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base, DwarfAddrSectionSym);
David Blaikie38fe6342014-01-09 04:28:46 +00002851 else
2852 NewU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
2853
2854 if (!CompilationDir.empty())
2855 NewU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2856
2857 addGnuPubAttributes(NewU, Die);
2858
2859 SkeletonHolder.addUnit(NewU);
2860}
2861
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002862// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2863// 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 +00002864// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher5090d572013-12-10 00:40:03 +00002865// TODO: Implement DW_AT_ranges_base.
Eric Christopher4287a492013-12-09 23:57:44 +00002866DwarfCompileUnit *DwarfDebug::constructSkeletonCU(const DwarfCompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002867
2868 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +00002869 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
David Blaikief645f962014-01-09 03:23:41 +00002870 CU->getUniqueID(), Die, CU->getCUNode(), Asm, this, &SkeletonHolder);
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002871 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
2872 DwarfInfoSectionSym);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002873
David Blaikie2494fdb2014-02-14 22:41:51 +00002874 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002875
David Blaikie38fe6342014-01-09 04:28:46 +00002876 initSkeletonUnit(CU, Die, NewCU);
Eric Christopherc8a310e2012-12-10 23:34:43 +00002877
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002878 return NewCU;
2879}
2880
David Blaikie15ed5eb2014-01-10 01:38:41 +00002881// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_dwo_name,
2882// DW_AT_addr_base.
David Blaikie15632ae2014-02-12 00:31:30 +00002883DwarfTypeUnit *DwarfDebug::constructSkeletonTU(DwarfTypeUnit *TU) {
David Blaikie60e63862014-02-14 23:58:13 +00002884 DwarfCompileUnit &CU = static_cast<DwarfCompileUnit &>(
2885 *SkeletonHolder.getUnits()[TU->getCU().getUniqueID()]);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002886
2887 DIE *Die = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie60e63862014-02-14 23:58:13 +00002888 DwarfTypeUnit *NewTU =
2889 new DwarfTypeUnit(TU->getUniqueID(), Die, CU, Asm, this, &SkeletonHolder);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002890 NewTU->setTypeSignature(TU->getTypeSignature());
2891 NewTU->setType(NULL);
2892 NewTU->initSection(
2893 Asm->getObjFileLowering().getDwarfTypesSection(TU->getTypeSignature()));
David Blaikie60e63862014-02-14 23:58:13 +00002894 CU.applyStmtList(*Die);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002895
2896 initSkeletonUnit(TU, Die, NewTU);
2897 return NewTU;
2898}
2899
Eric Christopherd692c1d2012-12-11 19:42:09 +00002900// Emit the .debug_info.dwo section for separated dwarf. This contains the
2901// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002902void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002903 assert(useSplitDwarf() && "No split dwarf debug info?");
David Blaikie03073f72013-12-06 22:14:48 +00002904 InfoHolder.emitUnits(this,
Eric Christopher3c5a1912012-12-19 22:02:53 +00002905 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2906 DwarfAbbrevDWOSectionSym);
2907}
2908
2909// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2910// abbreviations for the .debug_info.dwo section.
2911void DwarfDebug::emitDebugAbbrevDWO() {
2912 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00002913 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002914}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002915
2916// Emit the .debug_str.dwo section for separated dwarf. This contains the
2917// string section and is identical in format to traditional .debug_str
2918// sections.
2919void DwarfDebug::emitDebugStrDWO() {
2920 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002921 const MCSection *OffSec =
2922 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002923 const MCSymbol *StrSym = DwarfStrSectionSym;
2924 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2925 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002926}
David Blaikie409dd9c2013-11-19 23:08:21 +00002927
David Blaikie15632ae2014-02-12 00:31:30 +00002928void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikief645f962014-01-09 03:23:41 +00002929 StringRef Identifier, DIE *RefDie,
2930 DICompositeType CTy) {
David Blaikie322d79b2014-01-31 19:52:26 +00002931 // Flag the type unit reference as a declaration so that if it contains
2932 // members (implicit special members, static data member definitions, member
2933 // declarations for definitions in this CU, etc) consumers don't get confused
2934 // and think this is a full definition.
David Blaikie15632ae2014-02-12 00:31:30 +00002935 CU.addFlag(RefDie, dwarf::DW_AT_declaration);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002936
David Blaikie47f615e2013-12-17 23:32:35 +00002937 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
Chandler Carruthb587ab62014-01-20 08:07:07 +00002938 if (TU) {
David Blaikie15632ae2014-02-12 00:31:30 +00002939 CU.addDIETypeSignature(RefDie, *TU);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002940 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00002941 }
2942
Chandler Carruthb587ab62014-01-20 08:07:07 +00002943 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikied696fac2014-02-12 00:32:05 +00002944 DwarfTypeUnit *NewTU = new DwarfTypeUnit(InfoHolder.getUnits().size(),
2945 UnitDie, CU, Asm, this, &InfoHolder);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002946 TU = NewTU;
2947 InfoHolder.addUnit(NewTU);
2948
2949 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie15632ae2014-02-12 00:31:30 +00002950 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00002951
2952 MD5 Hash;
2953 Hash.update(Identifier);
2954 // ... take the least significant 8 bytes and return those. Our MD5
2955 // implementation always returns its results in little endian, swap bytes
2956 // appropriately.
2957 MD5::MD5Result Result;
2958 Hash.final(Result);
2959 uint64_t Signature = *reinterpret_cast<support::ulittle64_t *>(Result + 8);
2960 NewTU->setTypeSignature(Signature);
2961 if (useSplitDwarf())
2962 NewTU->setSkeleton(constructSkeletonTU(NewTU));
David Blaikie60e63862014-02-14 23:58:13 +00002963 else
2964 CU.applyStmtList(*UnitDie);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002965
2966 NewTU->setType(NewTU->createTypeDIE(CTy));
2967
2968 NewTU->initSection(
2969 useSplitDwarf()
2970 ? Asm->getObjFileLowering().getDwarfTypesDWOSection(Signature)
2971 : Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2972
David Blaikie15632ae2014-02-12 00:31:30 +00002973 CU.addDIETypeSignature(RefDie, *NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00002974}
David Blaikie4bd13b72014-03-07 18:49:45 +00002975
2976void DwarfDebug::attachLowHighPC(DwarfCompileUnit *Unit, DIE *D,
2977 MCSymbol *Begin, MCSymbol *End) {
2978 Unit->addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
2979 if (DwarfVersion < 4 || Triple(Asm->getTargetTriple()).isOSDarwin())
2980 Unit->addLabelAddress(D, dwarf::DW_AT_high_pc, End);
2981 else
2982 Unit->addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
2983}