blob: 9428fea7e1471cf4d30b4b5eecc538783f0f4110 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000017#include "DIEHash.h"
Eric Christopher4996c702011-11-07 09:24:32 +000018#include "DwarfAccelTable.h"
David Blaikie2c86a722013-12-02 19:33:15 +000019#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/Triple.h"
David Greene829b3e82009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/DIBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Constants.h"
29#include "llvm/IR/DataLayout.h"
30#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000032#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000033#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000034#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000035#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000036#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000037#include "llvm/Support/Debug.h"
David Majnemered89b5c2013-08-21 06:13:34 +000038#include "llvm/Support/Dwarf.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000039#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000040#include "llvm/Support/FormattedStream.h"
Eric Christopher67646432013-07-26 17:02:41 +000041#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000042#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000043#include "llvm/Support/Timer.h"
44#include "llvm/Support/ValueHandle.h"
45#include "llvm/Target/TargetFrameLowering.h"
46#include "llvm/Target/TargetLoweringObjectFile.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetOptions.h"
49#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000050using namespace llvm;
51
Eric Christopher7f2b5512013-07-23 22:16:41 +000052static cl::opt<bool>
53DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
54 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000055
Eric Christopher7f2b5512013-07-23 22:16:41 +000056static cl::opt<bool> UnknownLocations(
57 "use-unknown-locations", cl::Hidden,
58 cl::desc("Make an absence of debug location information explicit."),
59 cl::init(false));
Dan Gohman7421ae42010-05-07 01:08:53 +000060
Eric Christopherb4bef6d2013-11-19 09:04:36 +000061static cl::opt<bool> GenerateCUHash("generate-cu-hash", cl::Hidden,
62 cl::desc("Add the CU hash as the dwo_id."),
63 cl::init(false));
Eric Christopherd29614f2013-08-13 01:21:55 +000064
Eric Christopherdd1a0122013-09-13 00:35:05 +000065static cl::opt<bool>
66GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
67 cl::desc("Generate GNU-style pubnames and pubtypes"),
68 cl::init(false));
69
Eric Christopher02dbadb2014-02-14 01:26:55 +000070static cl::opt<bool> GenerateARangeSection("generate-arange-section",
71 cl::Hidden,
72 cl::desc("Generate dwarf aranges"),
73 cl::init(false));
74
Eric Christopher20b76a72012-08-23 22:36:40 +000075namespace {
Eric Christopherf07ee3a2014-01-27 23:50:03 +000076enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher20b76a72012-08-23 22:36:40 +000077}
Eric Christopher4996c702011-11-07 09:24:32 +000078
Eric Christopher7f2b5512013-07-23 22:16:41 +000079static cl::opt<DefaultOnOff>
80DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
81 cl::desc("Output prototype dwarf accelerator tables."),
82 cl::values(clEnumVal(Default, "Default for platform"),
83 clEnumVal(Enable, "Enabled"),
84 clEnumVal(Disable, "Disabled"), clEnumValEnd),
85 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +000086
Eric Christopher7f2b5512013-07-23 22:16:41 +000087static cl::opt<DefaultOnOff>
Eric Christopher7f2b5512013-07-23 22:16:41 +000088SplitDwarf("split-dwarf", cl::Hidden,
Eric Christopher5d008fe2013-12-04 23:24:28 +000089 cl::desc("Output DWARF5 split debug info."),
Eric Christopher7f2b5512013-07-23 22:16:41 +000090 cl::values(clEnumVal(Default, "Default for platform"),
91 clEnumVal(Enable, "Enabled"),
92 clEnumVal(Disable, "Disabled"), clEnumValEnd),
93 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +000094
Eric Christopher7da24882013-08-19 21:07:38 +000095static cl::opt<DefaultOnOff>
Eric Christopher4d36ca02013-08-26 23:24:35 +000096DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
97 cl::desc("Generate DWARF pubnames and pubtypes sections"),
98 cl::values(clEnumVal(Default, "Default for platform"),
99 clEnumVal(Enable, "Enabled"),
100 clEnumVal(Disable, "Disabled"), clEnumValEnd),
101 cl::init(Default));
Eric Christopher7da24882013-08-19 21:07:38 +0000102
Eric Christopher33ff6972013-11-21 23:46:41 +0000103static cl::opt<unsigned>
104DwarfVersionNumber("dwarf-version", cl::Hidden,
Eric Christophera5a79422013-12-09 23:32:48 +0000105 cl::desc("Generate DWARF for dwarf version."), cl::init(0));
Eric Christopher33ff6972013-11-21 23:46:41 +0000106
Eric Christopher46e23432013-12-20 04:16:18 +0000107static cl::opt<bool>
108DwarfCURanges("generate-dwarf-cu-ranges", cl::Hidden,
109 cl::desc("Generate DW_AT_ranges for compile units"),
110 cl::init(false));
111
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000112static const char *const DWARFGroupName = "DWARF Emission";
113static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000114
Bill Wendling2f921f82009-05-15 09:23:25 +0000115//===----------------------------------------------------------------------===//
116
Bill Wendling2f921f82009-05-15 09:23:25 +0000117namespace llvm {
118
Manman Renbe5576f2013-10-08 19:07:44 +0000119/// resolve - Look in the DwarfDebug map for the MDNode that
120/// corresponds to the reference.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000121template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
Manman Renbe5576f2013-10-08 19:07:44 +0000122 return DD->resolve(Ref);
123}
124
Nick Lewycky019d2552011-07-29 03:49:23 +0000125DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000126 DIType Ty = Var.getType();
127 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
128 // addresses instead.
129 if (Var.isBlockByrefVariable()) {
130 /* Byref variables, in Blocks, are declared by the programmer as
131 "SomeType VarName;", but the compiler creates a
132 __Block_byref_x_VarName struct, and gives the variable VarName
133 either the struct, or a pointer to the struct, as its type. This
134 is necessary for various behind-the-scenes things the compiler
135 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000136
Devang Patelf20c4f72011-04-12 22:53:02 +0000137 However, as far as the original *programmer* is concerned, the
138 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000139
Devang Patelf20c4f72011-04-12 22:53:02 +0000140 The following function dives into the __Block_byref_x_VarName
141 struct to find the original type of the variable. This will be
142 passed back to the code generating the type for the Debug
143 Information Entry for the variable 'VarName'. 'VarName' will then
144 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000145
Devang Patelf20c4f72011-04-12 22:53:02 +0000146 The original type 'SomeType' will be the type of the field named
147 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000148
Devang Patelf20c4f72011-04-12 22:53:02 +0000149 NOTE: In order for this to not completely fail on the debugger
150 side, the Debug Information Entry for the variable VarName needs to
151 have a DW_AT_location that tells the debugger how to unwind through
152 the pointers and __Block_byref_x_VarName struct to find the actual
153 value of the variable. The function addBlockByrefType does this. */
154 DIType subType = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000155 uint16_t tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000156
Eric Christopher9adc55f2013-09-04 19:53:21 +0000157 if (tag == dwarf::DW_TAG_pointer_type)
Manman Renbe5576f2013-10-08 19:07:44 +0000158 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher6a841382012-11-19 22:42:10 +0000159
Eric Christopher9adc55f2013-09-04 19:53:21 +0000160 DIArray Elements = DICompositeType(subType).getTypeArray();
Devang Patelf20c4f72011-04-12 22:53:02 +0000161 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie5af2aca2013-11-18 23:57:26 +0000162 DIDerivedType DT(Elements.getElement(i));
Devang Patelf20c4f72011-04-12 22:53:02 +0000163 if (getName() == DT.getName())
Manman Renbe5576f2013-10-08 19:07:44 +0000164 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000165 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000166 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000167 return Ty;
168}
Bill Wendling2f921f82009-05-15 09:23:25 +0000169
Chris Lattnerf5d06362010-04-05 04:09:20 +0000170} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000171
Eric Christopher942f22c2014-01-11 00:28:12 +0000172/// Return Dwarf Version by checking module flags.
173static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Ren8bfde892013-07-16 23:21:16 +0000174 Value *Val = M->getModuleFlag("Dwarf Version");
175 if (!Val)
Eric Christophere31e0722013-09-04 22:21:24 +0000176 return dwarf::DWARF_VERSION;
Manman Ren8bfde892013-07-16 23:21:16 +0000177 return cast<ConstantInt>(Val)->getZExtValue();
Manman Renac8062b2013-07-02 23:40:10 +0000178}
179
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000180DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
David Blaikie0504cda2013-12-05 07:43:55 +0000181 : Asm(A), MMI(Asm->MMI), FirstCU(0), SourceIdMap(DIEValueAllocator),
David Blaikie2666e242013-12-06 19:38:46 +0000182 PrevLabel(NULL), GlobalRangeCount(0),
Eric Christopher1bca60d2014-01-23 22:55:47 +0000183 InfoHolder(A, "info_string", DIEValueAllocator), HasCURanges(false),
Eric Christopher2037caf2014-01-28 00:49:26 +0000184 UsedNonDefaultText(false),
David Blaikie0504cda2013-12-05 07:43:55 +0000185 SkeletonHolder(A, "skel_string", DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000186
Eric Christopher50effa02014-01-03 02:16:44 +0000187 DwarfInfoSectionSym = DwarfAbbrevSectionSym = DwarfStrSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000188 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher55863be2013-04-07 03:43:09 +0000189 DwarfAddrSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000190 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000191 FunctionBeginSym = FunctionEndSym = 0;
Eric Christophera5a79422013-12-09 23:32:48 +0000192 CurFn = 0;
193 CurMI = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000194
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000195 // Turn on accelerator tables for Darwin by default, pubnames by
196 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000197 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000198
Eric Christopher574b5c82013-08-19 21:41:38 +0000199 if (DwarfAccelTables == Default)
200 HasDwarfAccelTables = IsDarwin;
201 else
Eric Christopher5297df02013-08-26 20:58:35 +0000202 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000203
Eric Christophercdf218d2012-12-10 19:51:21 +0000204 if (SplitDwarf == Default)
205 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000206 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000207 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000208
Eric Christopher4d36ca02013-08-26 23:24:35 +0000209 if (DwarfPubSections == Default)
210 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000211 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000212 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000213
Eric Christopher942f22c2014-01-11 00:28:12 +0000214 DwarfVersion = DwarfVersionNumber
215 ? DwarfVersionNumber
216 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000217
Dan Gohman6e681a52010-06-18 15:56:31 +0000218 {
219 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000220 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000221 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000222}
Bill Wendling2f921f82009-05-15 09:23:25 +0000223
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000224// Switch to the specified MCSection and emit an assembler
225// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000226static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000227 const char *SymbolStem = 0) {
228 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000229 if (!SymbolStem)
230 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000231
232 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
233 Asm->OutStreamer.EmitLabel(TmpSym);
234 return TmpSym;
235}
236
Eric Christopherf8194852013-12-05 18:06:10 +0000237DwarfFile::~DwarfFile() {
Eric Christophera5a79422013-12-09 23:32:48 +0000238 for (SmallVectorImpl<DwarfUnit *>::iterator I = CUs.begin(), E = CUs.end();
239 I != E; ++I)
David Blaikie72f1a3e2013-11-23 01:17:34 +0000240 delete *I;
241}
242
Eric Christopherf8194852013-12-05 18:06:10 +0000243MCSymbol *DwarfFile::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000244 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000245}
246
Eric Christopherf8194852013-12-05 18:06:10 +0000247MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000248 std::pair<MCSymbol *, unsigned> &Entry =
249 StringPool.GetOrCreateValue(Str).getValue();
250 if (Entry.first)
251 return Entry.first;
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000252
253 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000254 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000255}
256
Eric Christopherf8194852013-12-05 18:06:10 +0000257unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000258 std::pair<MCSymbol *, unsigned> &Entry =
259 StringPool.GetOrCreateValue(Str).getValue();
260 if (Entry.first)
261 return Entry.second;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000262
263 Entry.second = NextStringPoolNumber++;
264 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
265 return Entry.second;
266}
267
David Blaikief1a6dea2014-02-15 19:34:03 +0000268unsigned DwarfFile::getAddrPoolIndex(const MCSymbol *Sym, bool TLS) {
269 std::pair<AddrPool::iterator, bool> P = AddressPool.insert(
270 std::make_pair(Sym, AddressPoolEntry(NextAddrPoolNumber, TLS)));
David Blaikiea67de2b2013-06-28 18:55:13 +0000271 if (P.second)
272 ++NextAddrPoolNumber;
David Blaikief1a6dea2014-02-15 19:34:03 +0000273 return P.first->second.Number;
Eric Christopher962c9082013-01-15 23:56:56 +0000274}
275
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000276// Define a unique number for the abbreviation.
277//
Eric Christopherf8194852013-12-05 18:06:10 +0000278void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000279 // Check the set for priors.
David Blaikie0504cda2013-12-05 07:43:55 +0000280 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000281
282 // If it's newly added.
283 if (InSet == &Abbrev) {
284 // Add to abbreviation list.
David Blaikie2d4e1122013-10-30 17:14:24 +0000285 Abbreviations.push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000286
287 // Assign the vector position + 1 as its number.
David Blaikie2d4e1122013-10-30 17:14:24 +0000288 Abbrev.setNumber(Abbreviations.size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000289 } else {
290 // Assign existing abbreviation number.
291 Abbrev.setNumber(InSet->getNumber());
292 }
293}
294
Eric Christopherd9843b32011-11-10 19:25:34 +0000295static bool isObjCClass(StringRef Name) {
296 return Name.startswith("+") || Name.startswith("-");
297}
298
299static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000300 if (!isObjCClass(Name))
301 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000302
Benjamin Kramer260de742013-08-24 12:15:54 +0000303 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000304}
305
306static void getObjCClassCategory(StringRef In, StringRef &Class,
307 StringRef &Category) {
308 if (!hasObjCCategory(In)) {
309 Class = In.slice(In.find('[') + 1, In.find(' '));
310 Category = "";
311 return;
312 }
313
314 Class = In.slice(In.find('[') + 1, In.find('('));
315 Category = In.slice(In.find('[') + 1, In.find(' '));
316 return;
317}
318
319static StringRef getObjCMethodName(StringRef In) {
320 return In.slice(In.find(' ') + 1, In.find(']'));
321}
322
Richard Mittonc2508242013-10-03 22:07:08 +0000323// Helper for sorting sections into a stable output order.
324static bool SectionSort(const MCSection *A, const MCSection *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000325 std::string LA = (A ? A->getLabelBeginName() : "");
326 std::string LB = (B ? B->getLabelBeginName() : "");
327 return LA < LB;
Richard Mittonc2508242013-10-03 22:07:08 +0000328}
329
Eric Christopherd9843b32011-11-10 19:25:34 +0000330// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000331// TODO: Determine whether or not we should add names for programs
332// that do not have a DW_AT_name or DW_AT_linkage_name field - this
333// is only slightly different than the lookup of non-standard ObjC names.
Eric Christophera5a79422013-12-09 23:32:48 +0000334static void addSubprogramNames(DwarfUnit *TheU, DISubprogram SP, DIE *Die) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000335 if (!SP.isDefinition())
336 return;
David Blaikie2a80e442013-12-02 22:09:48 +0000337 TheU->addAccelName(SP.getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000338
339 // If the linkage name is different than the name, go ahead and output
340 // that as well into the name table.
341 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
David Blaikie2a80e442013-12-02 22:09:48 +0000342 TheU->addAccelName(SP.getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000343
344 // If this is an Objective-C selector name add it to the ObjC accelerator
345 // too.
346 if (isObjCClass(SP.getName())) {
347 StringRef Class, Category;
348 getObjCClassCategory(SP.getName(), Class, Category);
David Blaikie2a80e442013-12-02 22:09:48 +0000349 TheU->addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000350 if (Category != "")
David Blaikie2a80e442013-12-02 22:09:48 +0000351 TheU->addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000352 // Also add the base method name to the name table.
David Blaikie2a80e442013-12-02 22:09:48 +0000353 TheU->addAccelName(getObjCMethodName(SP.getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000354 }
355}
356
Manman Ren3eb9dff2013-09-09 19:05:21 +0000357/// isSubprogramContext - Return true if Context is either a subprogram
358/// or another context nested inside a subprogram.
359bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
360 if (!Context)
361 return false;
362 DIDescriptor D(Context);
363 if (D.isSubprogram())
364 return true;
365 if (D.isType())
Manman Ren116868e2013-09-09 19:47:11 +0000366 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000367 return false;
368}
369
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000370// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
371// and DW_AT_high_pc attributes. If there are global variables in this
372// scope then create and insert DIEs for these variables.
Eric Christopher4287a492013-12-09 23:57:44 +0000373DIE *DwarfDebug::updateSubprogramScopeDIE(DwarfCompileUnit *SPCU,
374 DISubprogram SP) {
David Blaikie25bc7192013-11-15 23:13:08 +0000375 DIE *SPDie = SPCU->getDIE(SP);
Devang Patela37a95e2010-07-07 22:20:57 +0000376
Chris Lattner3a383cb2010-04-05 00:13:49 +0000377 assert(SPDie && "Unable to find subprogram DIE!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000378
Bill Wendlingf720bf62012-11-07 05:19:04 +0000379 // If we're updating an abstract DIE, then we will be adding the children and
380 // object pointer later on. But what we don't want to do is process the
381 // concrete DIE twice.
David Blaikie25bc7192013-11-15 23:13:08 +0000382 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000383 // Pick up abstract subprogram DIE.
David Blaikie2a80e442013-12-02 22:09:48 +0000384 SPDie =
385 SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000386 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000387 } else {
388 DISubprogram SPDecl = SP.getFunctionDeclaration();
389 if (!SPDecl.isSubprogram()) {
390 // There is not any need to generate specification DIE for a function
391 // defined at compile unit level. If a function is defined inside another
392 // function then gdb prefers the definition at top level and but does not
393 // expect specification DIE in parent function. So avoid creating
394 // specification DIE for a function defined inside a function.
Manman Renc50fa112013-10-10 18:40:01 +0000395 DIScope SPContext = resolve(SP.getContext());
396 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000397 !SPContext.isFile() && !isSubprogramContext(SPContext)) {
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000398 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
399
400 // Add arguments.
401 DICompositeType SPTy = SP.getType();
402 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000403 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000404 if (SPTag == dwarf::DW_TAG_subroutine_type)
405 for (unsigned i = 1, N = Args.getNumElements(); i < N; ++i) {
Manman Ren4a841a82013-10-29 01:03:01 +0000406 DIE *Arg =
407 SPCU->createAndAddDIE(dwarf::DW_TAG_formal_parameter, *SPDie);
David Blaikie5af2aca2013-11-18 23:57:26 +0000408 DIType ATy(Args.getElement(i));
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000409 SPCU->addType(Arg, ATy);
410 if (ATy.isArtificial())
411 SPCU->addFlag(Arg, dwarf::DW_AT_artificial);
412 if (ATy.isObjectPointer())
Manman Ren4c4b69c2013-10-11 23:58:05 +0000413 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_object_pointer, Arg);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000414 }
415 DIE *SPDeclDie = SPDie;
David Blaikie2a80e442013-12-02 22:09:48 +0000416 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
417 *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000418 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000419 }
420 }
Devang Patela37a95e2010-07-07 22:20:57 +0000421 }
422
David Blaikieb7a1c4d2013-12-04 17:55:41 +0000423 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_low_pc, FunctionBeginSym);
424 SPCU->addLabelAddress(SPDie, dwarf::DW_AT_high_pc, FunctionEndSym);
Eric Christopher0f63d062013-12-03 00:45:45 +0000425
Chris Lattner3a383cb2010-04-05 00:13:49 +0000426 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
427 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000428 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000429
Eric Christopherd9843b32011-11-10 19:25:34 +0000430 // Add name to the name table, we do this here because we're guaranteed
431 // to have concrete versions of our DW_TAG_subprogram nodes.
432 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000433
Chris Lattner3a383cb2010-04-05 00:13:49 +0000434 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000435}
436
Manman Ren5b2f4b02013-09-11 19:40:28 +0000437/// Check whether we should create a DIE for the given Scope, return true
438/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000439bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
440 if (Scope->isAbstractScope())
441 return false;
442
Manman Ren5b2f4b02013-09-11 19:40:28 +0000443 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000444 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
445 if (Ranges.empty())
446 return true;
447
448 if (Ranges.size() > 1)
449 return false;
450
Manman Ren5b2f4b02013-09-11 19:40:28 +0000451 // We don't create a DIE if we have a single Range and the end label
452 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000453 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
454 MCSymbol *End = getLabelAfterInsn(RI->second);
455 return !End;
456}
457
Eric Christophera5a79422013-12-09 23:32:48 +0000458static void addSectionLabel(AsmPrinter *Asm, DwarfUnit *U, DIE *D,
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000459 dwarf::Attribute A, const MCSymbol *L,
460 const MCSymbol *Sec) {
461 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
462 U->addSectionLabel(D, A, L);
463 else
464 U->addSectionDelta(D, A, L, Sec);
465}
466
Eric Christopher4287a492013-12-09 23:57:44 +0000467void DwarfDebug::addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
Eric Christopherbe2513e2013-12-03 00:45:59 +0000468 const SmallVectorImpl<InsnRange> &Range) {
469 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
470 // emitting it appropriately.
Eric Christopherf8790642013-12-04 22:04:50 +0000471 MCSymbol *RangeSym = Asm->GetTempSymbol("debug_ranges", GlobalRangeCount++);
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000472 addSectionLabel(Asm, TheCU, ScopeDIE, dwarf::DW_AT_ranges, RangeSym,
473 DwarfDebugRangeSectionSym);
474
Eric Christopherf8790642013-12-04 22:04:50 +0000475 RangeSpanList List(RangeSym);
Eric Christopherbe2513e2013-12-03 00:45:59 +0000476 for (SmallVectorImpl<InsnRange>::const_iterator RI = Range.begin(),
477 RE = Range.end();
478 RI != RE; ++RI) {
479 RangeSpan Span(getLabelBeforeInsn(RI->first),
480 getLabelAfterInsn(RI->second));
Eric Christopher270ba4a2013-12-04 19:06:58 +0000481 List.addRange(llvm_move(Span));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000482 }
483
484 // Add the range list to the set of ranges to be emitted.
Eric Christopher270ba4a2013-12-04 19:06:58 +0000485 TheCU->addRangeList(llvm_move(List));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000486}
487
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000488// Construct new DW_TAG_lexical_block for this scope and attach
489// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher4287a492013-12-09 23:57:44 +0000490DIE *DwarfDebug::constructLexicalScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000491 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000492 if (isLexicalScopeDIENull(Scope))
493 return 0;
494
Devang Patel6c74a872010-04-27 19:46:33 +0000495 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
496 if (Scope->isAbstractScope())
497 return ScopeDIE;
498
Eric Christopher0f63d062013-12-03 00:45:45 +0000499 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherbe2513e2013-12-03 00:45:59 +0000500
Eric Christopherdc42ea82013-07-03 01:57:28 +0000501 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000502 if (ScopeRanges.size() > 1) {
Eric Christopherbe2513e2013-12-03 00:45:59 +0000503 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
Devang Patel6c74a872010-04-27 19:46:33 +0000504 return ScopeDIE;
505 }
506
Eric Christopherdc42ea82013-07-03 01:57:28 +0000507 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000508 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000509 MCSymbol *Start = getLabelBeforeInsn(RI->first);
510 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000511 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000512
Chris Lattnere13c3722010-03-09 01:58:53 +0000513 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
514 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000515
Eric Christopher962c9082013-01-15 23:56:56 +0000516 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
517 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000518
519 return ScopeDIE;
520}
521
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000522// This scope represents inlined body of a function. Construct DIE to
523// represent this concrete inlined copy of the function.
Eric Christopher4287a492013-12-09 23:57:44 +0000524DIE *DwarfDebug::constructInlinedScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000525 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000526 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopher1cdb63d2013-12-04 21:20:15 +0000527 assert(!ScopeRanges.empty() &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000528 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000529
Devang Patelf098ce22011-07-27 00:34:13 +0000530 if (!Scope->getScopeNode())
531 return NULL;
532 DIScope DS(Scope->getScopeNode());
533 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000534 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000535 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000536 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000537 return NULL;
538 }
539
Devang Patel73bc1722011-05-05 17:54:26 +0000540 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000541 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000542
Eric Christopherbe2513e2013-12-03 00:45:59 +0000543 // If we have multiple ranges, emit them into the range section.
544 if (ScopeRanges.size() > 1)
545 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
546 else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000547 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000548 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
549 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
550
551 if (StartLabel == 0 || EndLabel == 0)
552 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
553
554 assert(StartLabel->isDefined() &&
555 "Invalid starting label for an inlined scope!");
556 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
557
Eric Christopher962c9082013-01-15 23:56:56 +0000558 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
559 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000560 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000561
562 InlinedSubprogramDIEs.insert(OriginDIE);
563
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000564 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000565 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000566 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000567 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
568 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000569 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000570
Eric Christopher8dda5d02011-12-04 06:02:38 +0000571 // Add name to the name table, we do this here because we're guaranteed
572 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
573 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000574
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000575 return ScopeDIE;
576}
577
Eric Christopher4287a492013-12-09 23:57:44 +0000578DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
579 LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000580 SmallVectorImpl<DIE *> &Children) {
581 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000582
583 // Collect arguments for current function.
Devang Patel7e623022011-08-10 20:55:27 +0000584 if (LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +0000585 for (unsigned i = 0, N = CurrentFnArguments.size(); i < N; ++i)
586 if (DbgVariable *ArgDV = CurrentFnArguments[i])
Eric Christopher6a841382012-11-19 22:42:10 +0000587 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000588 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000589 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000590 if (ArgDV->isObjectPointer())
591 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000592 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000593
Eric Christopherf84354b2011-10-03 15:49:16 +0000594 // Collect lexical scope children first.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000595 const SmallVectorImpl<DbgVariable *> &Variables =
596 ScopeVariables.lookup(Scope);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000597 for (unsigned i = 0, N = Variables.size(); i < N; ++i)
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000598 if (DIE *Variable = TheCU->constructVariableDIE(*Variables[i],
599 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000600 Children.push_back(Variable);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000601 if (Variables[i]->isObjectPointer())
602 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000603 }
Craig Topper977e9cd2013-07-03 04:24:43 +0000604 const SmallVectorImpl<LexicalScope *> &Scopes = Scope->getChildren();
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000605 for (unsigned j = 0, M = Scopes.size(); j < M; ++j)
Devang Patel3acc70e2011-08-15 22:04:40 +0000606 if (DIE *Nested = constructScopeDIE(TheCU, Scopes[j]))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000607 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000608 return ObjectPointer;
609}
610
611// Construct a DIE for this scope.
Eric Christopher4287a492013-12-09 23:57:44 +0000612DIE *DwarfDebug::constructScopeDIE(DwarfCompileUnit *TheCU,
613 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000614 if (!Scope || !Scope->getScopeNode())
615 return NULL;
616
617 DIScope DS(Scope->getScopeNode());
618
619 SmallVector<DIE *, 8> Children;
620 DIE *ObjectPointer = NULL;
621 bool ChildrenCreated = false;
622
Manman Ren5b2f4b02013-09-11 19:40:28 +0000623 // We try to create the scope DIE first, then the children DIEs. This will
624 // avoid creating un-used children then removing them later when we find out
625 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000626 DIE *ScopeDIE = NULL;
627 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000628 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000629 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000630 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000631 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000632 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000633 // Note down abstract DIE.
634 if (ScopeDIE)
635 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000636 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000637 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000638 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000639 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000640 if (isLexicalScopeDIENull(Scope))
641 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000642
643 // We create children here when we know the scope DIE is not going to be
644 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000645 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
646 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000647
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000648 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000649 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000650 ImportedEntityMap::const_iterator> Range =
651 std::equal_range(
652 ScopesWithImportedEntities.begin(),
653 ScopesWithImportedEntities.end(),
654 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
655 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000656 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000657 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000658 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000659 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000660 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
661 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000662 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000663 }
Eric Christopher6a841382012-11-19 22:42:10 +0000664
Benjamin Kramer892daba2013-08-24 11:55:49 +0000665 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000666 assert(Children.empty() &&
667 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000668 return NULL;
669 }
Manman Ren2312ed32013-09-10 18:40:41 +0000670 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000671 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000672 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000673
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000674 // Add children
Craig Topperd8e43652013-07-03 04:17:25 +0000675 for (SmallVectorImpl<DIE *>::iterator I = Children.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000676 E = Children.end();
677 I != E; ++I)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000678 ScopeDIE->addChild(*I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000679
Eric Christophere3417762012-09-12 23:36:19 +0000680 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000681 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000682
Eric Christopherd9843b32011-11-10 19:25:34 +0000683 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000684}
685
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000686// Look up the source id with the given directory and source file names.
687// If none currently exists, create a new id and insert it in the
688// SourceIds map. This can update DirectoryNames and SourceFileNames maps
689// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000690unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
691 unsigned CUID) {
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000692 // If we print assembly, we can't separate .file entries according to
Manman Ren1e427202013-03-07 01:42:00 +0000693 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000694
695 // FIXME: add a better feature test than hasRawTextSupport. Even better,
696 // extend .file to support this.
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000697 if (Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000698 CUID = 0;
699
Devang Patel871d0b12010-09-16 20:57:49 +0000700 // If FE did not provide a file name, then assume stdin.
701 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000702 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000703
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000704 // TODO: this might not belong here. See if we can factor this better.
705 if (DirName == CompilationDir)
706 DirName = "";
707
Manman Ren1e427202013-03-07 01:42:00 +0000708 // FileIDCUMap stores the current ID for the given compile unit.
709 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000710
Manman Ren1e427202013-03-07 01:42:00 +0000711 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000712 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000713 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000714 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000715 NamePair += DirName;
716 NamePair += '\0'; // Zero bytes are not allowed in paths.
717 NamePair += FileName;
718
719 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
720 if (Ent.getValue() != SrcId)
721 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000722
Manman Ren1e427202013-03-07 01:42:00 +0000723 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000724 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000725 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000726
727 return SrcId;
728}
729
Eric Christophera5a79422013-12-09 23:32:48 +0000730void DwarfDebug::addGnuPubAttributes(DwarfUnit *U, DIE *D) const {
David Blaikie3c842622013-12-04 21:31:26 +0000731 if (!GenerateGnuPubSections)
732 return;
733
734 addSectionLabel(Asm, U, D, dwarf::DW_AT_GNU_pubnames,
735 Asm->GetTempSymbol("gnu_pubnames", U->getUniqueID()),
736 DwarfGnuPubNamesSectionSym);
737
738 addSectionLabel(Asm, U, D, dwarf::DW_AT_GNU_pubtypes,
739 Asm->GetTempSymbol("gnu_pubtypes", U->getUniqueID()),
740 DwarfGnuPubTypesSectionSym);
741}
742
Eric Christopher4287a492013-12-09 23:57:44 +0000743// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000744// DW_TAG_compile_unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000745DwarfCompileUnit *DwarfDebug::constructDwarfCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000746 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000747 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000748
749 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +0000750 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
751 InfoHolder.getUnits().size(), Die, DIUnit, Asm, this, &InfoHolder);
David Blaikie2666e242013-12-06 19:38:46 +0000752 InfoHolder.addUnit(NewCU);
753
Manman Ren1e427202013-03-07 01:42:00 +0000754 FileIDCUMap[NewCU->getUniqueID()] = 0;
Manman Ren1e427202013-03-07 01:42:00 +0000755
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000756 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000757 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
758 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000759 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000760
Manman Ren9d4c7352013-05-21 00:57:22 +0000761
Eric Christopher52ce7182013-04-09 19:23:15 +0000762 if (!useSplitDwarf()) {
David Blaikie2494fdb2014-02-14 22:41:51 +0000763 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000764
765 // If we're using split dwarf the compilation dir is going to be in the
766 // skeleton CU and so we don't need to duplicate it here.
767 if (!CompilationDir.empty())
768 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
769
David Blaikie3c842622013-12-04 21:31:26 +0000770 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000771 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000772
Bill Wendling2b128d72009-05-20 23:19:06 +0000773 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000774 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000775
Devang Patel2d9caf92009-11-25 17:36:49 +0000776 StringRef Flags = DIUnit.getFlags();
777 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000778 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000779
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000780 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000781 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000782 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000783
Devang Patel1a0df9a2010-05-10 22:49:55 +0000784 if (!FirstCU)
785 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000786
Eric Christopherd4368fd2014-01-02 21:03:28 +0000787 if (useSplitDwarf()) {
788 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection(),
789 DwarfInfoDWOSectionSym);
Eric Christopherd8667202013-12-30 17:22:27 +0000790 NewCU->setSkeleton(constructSkeletonCU(NewCU));
Eric Christopherd4368fd2014-01-02 21:03:28 +0000791 } else
792 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
793 DwarfInfoSectionSym);
Eric Christopherd039baa2013-12-30 03:40:32 +0000794
David Blaikie5a152402013-11-15 23:52:02 +0000795 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000796 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000797 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000798}
799
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000800// Construct subprogram DIE.
Eric Christopher4287a492013-12-09 23:57:44 +0000801void DwarfDebug::constructSubprogramDIE(DwarfCompileUnit *TheCU,
802 const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000803 // FIXME: We should only call this routine once, however, during LTO if a
804 // program is defined in multiple CUs we could end up calling it out of
805 // beginModule as we walk the CUs.
806
Eric Christopher4287a492013-12-09 23:57:44 +0000807 DwarfCompileUnit *&CURef = SPMap[N];
Eric Christopherffbc4de2013-10-18 01:57:30 +0000808 if (CURef)
809 return;
810 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000811
Devang Patel80ae3492009-08-28 23:24:31 +0000812 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000813 if (!SP.isDefinition())
814 // This is a method declaration which will be handled while constructing
815 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000816 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000817
Devang Patel89543712011-08-15 17:24:54 +0000818 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000819
Eric Christopherba506db2013-09-09 20:03:20 +0000820 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000821 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000822}
823
Eric Christopher4287a492013-12-09 23:57:44 +0000824void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000825 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000826 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000827 assert(Module.Verify());
David Blaikie684fc532013-05-06 23:33:07 +0000828 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000829 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000830}
831
Eric Christopher4287a492013-12-09 23:57:44 +0000832void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
833 const MDNode *N, DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000834 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000835 assert(Module.Verify());
David Blaikie4dd2de72013-05-08 06:01:38 +0000836 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000837}
838
Eric Christopher4287a492013-12-09 23:57:44 +0000839void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000840 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000841 DIE *Context) {
842 assert(Module.Verify() &&
843 "Use one of the MDNode * overloads to handle invalid metadata");
844 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000845 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000846 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000847 DIE *EntityDie;
848 DIDescriptor Entity = Module.getEntity();
849 if (Entity.isNameSpace())
850 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
851 else if (Entity.isSubprogram())
852 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000853 else if (Entity.isType())
854 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000855 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000856 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000857 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
858 Module.getContext().getDirectory(),
859 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000860 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
861 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000862 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000863 StringRef Name = Module.getName();
864 if (!Name.empty())
865 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000866 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000867}
868
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000869// Emit all Dwarf sections that should come prior to the content. Create
870// global DIEs and emit initial debug info sections. This is invoked by
871// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000872void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000873 if (DisableDebugInfoPrinting)
874 return;
875
Eric Christopher58f41952012-11-19 22:42:15 +0000876 const Module *M = MMI->getModule();
877
Nick Lewycky019d2552011-07-29 03:49:23 +0000878 // If module has named metadata anchors then use them, otherwise scan the
879 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000880 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000881 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000882 return;
Manman Ren60352032013-09-05 18:48:31 +0000883 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000884
David Blaikiedc69ebb2013-03-11 23:39:23 +0000885 // Emit initial sections so we can reference labels later.
886 emitSectionLabels();
887
888 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
889 DICompileUnit CUNode(CU_Nodes->getOperand(i));
Eric Christopher4287a492013-12-09 23:57:44 +0000890 DwarfCompileUnit *CU = constructDwarfCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000891 DIArray ImportedEntities = CUNode.getImportedEntities();
892 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000893 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000894 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
895 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000896 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000897 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000898 DIArray GVs = CUNode.getGlobalVariables();
899 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000900 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000901 DIArray SPs = CUNode.getSubprograms();
902 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
903 constructSubprogramDIE(CU, SPs.getElement(i));
904 DIArray EnumTypes = CUNode.getEnumTypes();
905 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
906 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
907 DIArray RetainedTypes = CUNode.getRetainedTypes();
908 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
909 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000910 // Emit imported_modules last so that the relevant context is already
911 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000912 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
913 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000914 }
Eric Christopher6a841382012-11-19 22:42:10 +0000915
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000916 // Tell MMI that we have debug info.
917 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000918
Bill Wendling2b128d72009-05-20 23:19:06 +0000919 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000920 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000921}
922
Eric Christopher960ac372012-11-22 00:59:49 +0000923// Attach DW_AT_inline attribute with inlined subprogram DIEs.
924void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000925 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
926 for (SmallPtrSet<DIE *, 4>::iterator AI = InlinedSubprogramDIEs.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000927 AE = InlinedSubprogramDIEs.end();
928 AI != AE; ++AI) {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000929 DIE *ISP = *AI;
David Blaikief2443192013-10-21 17:28:37 +0000930 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000931 }
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000932 for (DenseMap<const MDNode *, DIE *>::iterator AI = AbstractSPDies.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000933 AE = AbstractSPDies.end();
934 AI != AE; ++AI) {
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000935 DIE *ISP = AI->second;
936 if (InlinedSubprogramDIEs.count(ISP))
937 continue;
David Blaikief2443192013-10-21 17:28:37 +0000938 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000939 }
Eric Christopher960ac372012-11-22 00:59:49 +0000940}
941
942// Collect info for variables that were optimized out.
943void DwarfDebug::collectDeadVariables() {
944 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000945
946 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
947 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
948 DICompileUnit TheCU(CU_Nodes->getOperand(i));
949 DIArray Subprograms = TheCU.getSubprograms();
950 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000951 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000952 if (ProcessedSPNodes.count(SP) != 0)
953 continue;
954 if (!SP.isSubprogram())
955 continue;
956 if (!SP.isDefinition())
957 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000958 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +0000959 if (Variables.getNumElements() == 0)
960 continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000961
Eric Christopher735401c2012-11-27 00:13:51 +0000962 // Construct subprogram DIE and add variables DIEs.
Eric Christopher4287a492013-12-09 23:57:44 +0000963 DwarfCompileUnit *SPCU =
964 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +0000965 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +0000966 // FIXME: See the comment in constructSubprogramDIE about duplicate
967 // subprogram DIEs.
968 constructSubprogramDIE(SPCU, SP);
969 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +0000970 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
971 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +0000972 if (!DV.isVariable())
973 continue;
Manman Renb3388602013-10-05 01:43:03 +0000974 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000975 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +0000976 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +0000977 }
Eric Christopher960ac372012-11-22 00:59:49 +0000978 }
979 }
980 }
Eric Christopher960ac372012-11-22 00:59:49 +0000981}
982
983void DwarfDebug::finalizeModuleInfo() {
984 // Collect info for variables that were optimized out.
985 collectDeadVariables();
986
987 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
988 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000989
Eric Christopherad10cb52013-12-04 23:24:38 +0000990 // Handle anything that needs to be done on a per-unit basis after
991 // all other generation.
Eric Christophera5a79422013-12-09 23:32:48 +0000992 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
993 E = getUnits().end();
David Blaikiefd1eff52013-11-26 19:14:34 +0000994 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +0000995 DwarfUnit *TheU = *I;
Eric Christopher60eb7692013-08-12 20:27:48 +0000996 // Emit DW_AT_containing_type attribute to connect types with their
997 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +0000998 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000999
Eric Christopher46e23432013-12-20 04:16:18 +00001000 // Add CU specific attributes if we need to add any.
1001 if (TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
1002 // If we're splitting the dwarf out now that we've got the entire
Eric Christopherd8667202013-12-30 17:22:27 +00001003 // CU then add the dwo id to it.
1004 DwarfCompileUnit *SkCU =
1005 static_cast<DwarfCompileUnit *>(TheU->getSkeleton());
Eric Christopher46e23432013-12-20 04:16:18 +00001006 if (useSplitDwarf()) {
1007 // This should be a unique identifier when we want to build .dwp files.
1008 uint64_t ID = 0;
1009 if (GenerateCUHash) {
Eric Christopher420569b2014-02-20 02:50:45 +00001010 DIEHash CUHash(Asm);
Eric Christopher46e23432013-12-20 04:16:18 +00001011 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
1012 }
1013 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1014 dwarf::DW_FORM_data8, ID);
Eric Christopher46e23432013-12-20 04:16:18 +00001015 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
1016 dwarf::DW_FORM_data8, ID);
Eric Christopherd29614f2013-08-13 01:21:55 +00001017 }
Eric Christopherd8667202013-12-30 17:22:27 +00001018
Eric Christopher39cde8c2014-01-14 22:44:17 +00001019 // If we have code split among multiple sections or we've requested
1020 // it then emit a DW_AT_ranges attribute on the unit that will remain
1021 // in the .o file, otherwise add a DW_AT_low_pc.
Eric Christopherd8667202013-12-30 17:22:27 +00001022 // FIXME: Also add a high pc if we can.
Eric Christopher39cde8c2014-01-14 22:44:17 +00001023 // FIXME: We should use ranges if we have multiple compile units or
1024 // allow reordering of code ala .subsections_via_symbols in mach-o.
Eric Christopherd8667202013-12-30 17:22:27 +00001025 DwarfCompileUnit *U = SkCU ? SkCU : static_cast<DwarfCompileUnit *>(TheU);
Eric Christopher8873ada2014-01-29 22:22:56 +00001026 if (useCURanges() && TheU->getRanges().size()) {
Eric Christopherd8667202013-12-30 17:22:27 +00001027 addSectionLabel(Asm, U, U->getUnitDie(), dwarf::DW_AT_ranges,
1028 Asm->GetTempSymbol("cu_ranges", U->getUniqueID()),
1029 DwarfDebugRangeSectionSym);
Eric Christopher8873ada2014-01-29 22:22:56 +00001030
1031 // A DW_AT_low_pc attribute may also be specified in combination with
1032 // DW_AT_ranges to specify the default base address for use in location
1033 // lists (see Section 2.6.2) and range lists (see Section 2.17.3).
1034 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1035 0);
1036 } else
Eric Christophercf48ade2014-01-24 11:52:53 +00001037 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1038 0);
Eric Christopher60eb7692013-08-12 20:27:48 +00001039 }
1040 }
1041
1042 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001043 InfoHolder.computeSizeAndOffsets();
1044 if (useSplitDwarf())
1045 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001046}
1047
1048void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001049 // Filter labels by section.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001050 for (size_t n = 0; n < ArangeLabels.size(); n++) {
1051 const SymbolCU &SCU = ArangeLabels[n];
Richard Mitton21101b32013-09-19 23:21:01 +00001052 if (SCU.Sym->isInSection()) {
1053 // Make a note of this symbol and it's section.
1054 const MCSection *Section = &SCU.Sym->getSection();
1055 if (!Section->getKind().isMetadata())
1056 SectionMap[Section].push_back(SCU);
1057 } else {
1058 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1059 // appear in the output. This sucks as we rely on sections to build
1060 // arange spans. We can do it without, but it's icky.
1061 SectionMap[NULL].push_back(SCU);
1062 }
1063 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001064
Richard Mittonc2508242013-10-03 22:07:08 +00001065 // Build a list of sections used.
1066 std::vector<const MCSection *> Sections;
Richard Mitton21101b32013-09-19 23:21:01 +00001067 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
1068 it++) {
1069 const MCSection *Section = it->first;
Richard Mittonc2508242013-10-03 22:07:08 +00001070 Sections.push_back(Section);
1071 }
1072
1073 // Sort the sections into order.
1074 // This is only done to ensure consistent output order across different runs.
1075 std::sort(Sections.begin(), Sections.end(), SectionSort);
1076
1077 // Add terminating symbols for each section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001078 for (unsigned ID = 0; ID < Sections.size(); ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001079 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001080 MCSymbol *Sym = NULL;
1081
1082 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001083 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001084 // if we know the section name up-front. For user-created sections, the
Eric Christopher95531b62014-01-29 22:06:21 +00001085 // resulting label may not be valid to use as a label. (section names can
1086 // use a greater set of characters on some systems)
Richard Mittonc2508242013-10-03 22:07:08 +00001087 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001088 Asm->OutStreamer.SwitchSection(Section);
1089 Asm->OutStreamer.EmitLabel(Sym);
1090 }
1091
1092 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001093 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001094 }
Eric Christopher1bca60d2014-01-23 22:55:47 +00001095
1096 // For now only turn on CU ranges if we've explicitly asked for it,
Eric Christopherfb8dd002014-01-29 22:06:27 +00001097 // we have -ffunction-sections enabled, we've emitted a function
1098 // into a unique section, or we're using LTO. If we're using LTO then
1099 // we can't know that any particular function in the module is correlated
1100 // to a particular CU and so we need to be conservative. At this point all
1101 // sections should be finalized except for dwarf sections.
1102 HasCURanges = DwarfCURanges || UsedNonDefaultText || (CUMap.size() > 1) ||
Eric Christopher1bca60d2014-01-23 22:55:47 +00001103 TargetMachine::getFunctionSections();
Eric Christopher960ac372012-11-22 00:59:49 +00001104}
Bill Wendling2b128d72009-05-20 23:19:06 +00001105
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001106// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001107void DwarfDebug::endModule() {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001108 assert(CurFn == 0);
1109 assert(CurMI == 0);
Eric Christopher960ac372012-11-22 00:59:49 +00001110
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001111 if (!FirstCU)
1112 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001113
1114 // End any existing sections.
1115 // TODO: Does this need to happen?
1116 endSections();
1117
1118 // Finalize the debug info for the module.
1119 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001120
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001121 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001122
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001123 // Emit all the DIEs into a debug info section.
1124 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001125
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001126 // Corresponding abbreviations into a abbrev section.
1127 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001128
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001129 // Emit info into a debug loc section.
1130 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001131
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001132 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +00001133 if (GenerateARangeSection)
1134 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001135
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001136 // Emit info into a debug ranges section.
1137 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001138
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001139 if (useSplitDwarf()) {
1140 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001141 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001142 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001143 // Emit DWO addresses.
1144 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001145 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001146
Eric Christophera876b822012-08-23 07:32:06 +00001147 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001148 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001149 emitAccelNames();
1150 emitAccelObjC();
1151 emitAccelNamespaces();
1152 emitAccelTypes();
1153 }
Eric Christopher6a841382012-11-19 22:42:10 +00001154
Eric Christopher4b358182013-08-30 00:40:17 +00001155 // Emit the pubnames and pubtypes sections if requested.
1156 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001157 emitDebugPubNames(GenerateGnuPubSections);
1158 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001159 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001160
Devang Pateld0701282010-08-02 17:32:15 +00001161 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001162 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001163
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001164 // Reset these for the next Module if we have one.
1165 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001166}
1167
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001168// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001169DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001170 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001171 LLVMContext &Ctx = DV->getContext();
1172 // More then one inlined variable corresponds to one abstract variable.
1173 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001174 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001175 if (AbsDbgVariable)
1176 return AbsDbgVariable;
1177
Devang Patel7e623022011-08-10 20:55:27 +00001178 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001179 if (!Scope)
1180 return NULL;
1181
Manman Renb3388602013-10-05 01:43:03 +00001182 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001183 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001184 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001185 return AbsDbgVariable;
1186}
1187
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001188// If Var is a current function argument then add it to CurrentFnArguments list.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001189bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +00001190 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001191 return false;
1192 DIVariable DV = Var->getVariable();
1193 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1194 return false;
1195 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001196 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001197 return false;
1198
Devang Patel4ab660b2011-03-03 20:02:02 +00001199 size_t Size = CurrentFnArguments.size();
1200 if (Size == 0)
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001201 CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001202 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001203 // arguments does the function have at source level.
1204 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001205 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001206 CurrentFnArguments[ArgNo - 1] = Var;
1207 return true;
1208}
1209
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001210// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001211void DwarfDebug::collectVariableInfoFromMMITable(
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001212 SmallPtrSet<const MDNode *, 16> &Processed) {
Devang Patel475d32a2009-10-06 01:26:37 +00001213 MachineModuleInfo::VariableDbgInfoMapTy &VMap = MMI->getVariableDbgInfo();
1214 for (MachineModuleInfo::VariableDbgInfoMapTy::iterator VI = VMap.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001215 VE = VMap.end();
1216 VI != VE; ++VI) {
Devang Patel32cc43c2010-05-07 20:54:48 +00001217 const MDNode *Var = VI->first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001218 if (!Var)
1219 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001220 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001221 DIVariable DV(Var);
1222 const std::pair<unsigned, DebugLoc> &VP = VI->second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001223
Devang Patel7e623022011-08-10 20:55:27 +00001224 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001225
Devang Patelcdb7d442009-11-10 23:20:04 +00001226 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001227 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001228 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001229
Devang Patele1c53f22010-05-20 16:36:41 +00001230 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001231 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001232 RegVar->setFrameIndex(VP.first);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001233 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001234 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001235 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001236 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001237 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001238}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001239
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001240// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1241// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001242static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001243 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001244 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1245 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001246 (MI->getOperand(1).isImm() ||
1247 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001248}
1249
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001250// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001251static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1252 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001253 const MCSymbol *SLabel,
1254 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001255 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001256
David Blaikie0252265b2013-06-16 20:34:15 +00001257 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001258 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001259 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001260 // If the second operand is an immediate, this is a
1261 // register-indirect address.
1262 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001263 MLoc.set(MI->getOperand(0).getReg());
1264 else
1265 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001266 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1267 }
1268 if (MI->getOperand(0).isImm())
1269 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1270 if (MI->getOperand(0).isFPImm())
1271 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1272 if (MI->getOperand(0).isCImm())
1273 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1274
Craig Topperee4dab52012-02-05 08:31:47 +00001275 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001276}
1277
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001278// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001279void
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001280DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001281
Eric Christopher270a12c2013-07-03 21:37:03 +00001282 // Grab the variable info that was squirreled away in the MMI side-table.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001283 collectVariableInfoFromMMITable(Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001284
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001285 for (SmallVectorImpl<const MDNode *>::const_iterator
1286 UVI = UserVariables.begin(),
1287 UVE = UserVariables.end();
1288 UVI != UVE; ++UVI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001289 const MDNode *Var = *UVI;
1290 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001291 continue;
1292
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001293 // History contains relevant DBG_VALUE instructions for Var and instructions
1294 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001295 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001296 if (History.empty())
1297 continue;
1298 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001299
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001300 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001301 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001302 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001303 DISubprogram(DV.getContext()).describes(CurFn->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001304 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001305 else if (MDNode *IA = DV.getInlinedAt())
1306 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1307 else
1308 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001309 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001310 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001311 continue;
1312
1313 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001314 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001315 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001316 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001317 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001318 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001319 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001320 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001321
Eric Christophercc10d202012-10-08 20:48:54 +00001322 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001323 if (History.size() <= 1 ||
1324 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001325 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001326 continue;
1327 }
1328
Eric Christopher59cc0712013-01-28 17:33:26 +00001329 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001330 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001331
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001332 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1333 HI = History.begin(),
1334 HE = History.end();
1335 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001336 const MachineInstr *Begin = *HI;
1337 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001338
Devang Patele7181b52011-06-01 23:00:17 +00001339 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001340 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1341 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001342 continue;
1343
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001344 // Compute the range for a register location.
1345 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1346 const MCSymbol *SLabel = 0;
1347
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001348 if (HI + 1 == HE)
1349 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001350 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001351 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001352 else {
1353 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001354 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001355 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001356 if (End->isDebugValue())
1357 SLabel = getLabelBeforeInsn(End);
1358 else {
1359 // End is a normal instruction clobbering the range.
1360 SLabel = getLabelAfterInsn(End);
1361 assert(SLabel && "Forgot label after clobber instruction");
1362 ++HI;
1363 }
1364 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001365
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001366 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001367 DotDebugLocEntries.push_back(
1368 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001369 }
1370 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001371 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001372
1373 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001374 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1375 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1376 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1377 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001378 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001379 continue;
1380 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001381 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001382 }
Devang Patel9fc11702010-05-25 23:40:22 +00001383}
Devang Patele0a94bf2010-05-14 21:01:35 +00001384
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001385// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001386MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001387 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1388 assert(Label && "Didn't insert label before instruction");
1389 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001390}
1391
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001392// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001393MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001394 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001395}
1396
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001397// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001398void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001399 assert(CurMI == 0);
1400 CurMI = MI;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001401 // Check if source location changes, but ignore DBG_VALUE locations.
1402 if (!MI->isDebugValue()) {
1403 DebugLoc DL = MI->getDebugLoc();
1404 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001405 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001406 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001407 if (DL == PrologEndLoc) {
1408 Flags |= DWARF2_FLAG_PROLOGUE_END;
1409 PrologEndLoc = DebugLoc();
1410 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001411 if (PrologEndLoc.isUnknown())
1412 Flags |= DWARF2_FLAG_IS_STMT;
1413
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001414 if (!DL.isUnknown()) {
1415 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001416 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001417 } else
Devang Patel34a66202011-05-11 19:22:19 +00001418 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001419 }
Devang Patel9fc11702010-05-25 23:40:22 +00001420 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001421
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001422 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001423 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1424 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001425
1426 // No label needed.
1427 if (I == LabelsBeforeInsn.end())
1428 return;
1429
1430 // Label already assigned.
1431 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001432 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001433
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001434 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001435 PrevLabel = MMI->getContext().CreateTempSymbol();
1436 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001437 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001438 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001439}
1440
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001441// Process end of an instruction.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001442void DwarfDebug::endInstruction() {
1443 assert(CurMI != 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001444 // Don't create a new label after DBG_VALUE instructions.
1445 // They don't generate code.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001446 if (!CurMI->isDebugValue())
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001447 PrevLabel = 0;
1448
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001449 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001450 LabelsAfterInsn.find(CurMI);
1451 CurMI = 0;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001452
1453 // No label needed.
1454 if (I == LabelsAfterInsn.end())
1455 return;
1456
1457 // Label already assigned.
1458 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001459 return;
1460
1461 // We need a label after this instruction.
1462 if (!PrevLabel) {
1463 PrevLabel = MMI->getContext().CreateTempSymbol();
1464 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001465 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001466 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001467}
1468
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001469// Each LexicalScope has first instruction and last instruction to mark
1470// beginning and end of a scope respectively. Create an inverse map that list
1471// scopes starts (and ends) with an instruction. One instruction may start (or
1472// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001473void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001474 SmallVector<LexicalScope *, 4> WorkList;
1475 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001476 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001477 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001478
Craig Topper977e9cd2013-07-03 04:24:43 +00001479 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001480 if (!Children.empty())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001481 for (SmallVectorImpl<LexicalScope *>::const_iterator
1482 SI = Children.begin(),
1483 SE = Children.end();
1484 SI != SE; ++SI)
Devang Patel7771b7c2010-01-20 02:05:23 +00001485 WorkList.push_back(*SI);
1486
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001487 if (S->isAbstractScope())
1488 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001489
Craig Topper977e9cd2013-07-03 04:24:43 +00001490 const SmallVectorImpl<InsnRange> &Ranges = S->getRanges();
Devang Patel6c74a872010-04-27 19:46:33 +00001491 if (Ranges.empty())
1492 continue;
Craig Topperd8e43652013-07-03 04:17:25 +00001493 for (SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin(),
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001494 RE = Ranges.end();
1495 RI != RE; ++RI) {
Devang Patel7e623022011-08-10 20:55:27 +00001496 assert(RI->first && "InsnRange does not have first instruction!");
1497 assert(RI->second && "InsnRange does not have second instruction!");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001498 requestLabelBeforeInsn(RI->first);
1499 requestLabelAfterInsn(RI->second);
Devang Patel6c74a872010-04-27 19:46:33 +00001500 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001501 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001502}
1503
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001504// Gather pre-function debug information. Assumes being called immediately
1505// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001506void DwarfDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001507 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001508
1509 // If there's no debug info for the function we're not going to do anything.
1510 if (!MMI->hasDebugInfo())
1511 return;
1512
1513 // Grab the lexical scopes for the function, if we don't have any of those
1514 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001515 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001516 if (LScopes.empty())
1517 return;
1518
1519 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1520
1521 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001522 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001523
Eric Christopher4287a492013-12-09 23:57:44 +00001524 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001525 // belongs to so that we add to the correct per-cu line table in the
1526 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001527 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001528 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren4e042a62013-02-05 21:52:47 +00001529 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001530 if (Asm->OutStreamer.hasRawTextSupport())
1531 // Use a single line table if we are generating assembly.
Manman Ren9d4c7352013-05-21 00:57:22 +00001532 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1533 else
1534 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001535
Eric Christopher2037caf2014-01-28 00:49:26 +00001536 // Check the current section against the standard text section. If different
1537 // keep track so that we will know when we're emitting functions into multiple
1538 // sections.
1539 if (Asm->getObjFileLowering().getTextSection() != Asm->getCurrentSection())
1540 UsedNonDefaultText = true;
1541
Eric Christopherfedfa442013-11-01 23:14:17 +00001542 // Emit a label for the function so that we have a beginning address.
1543 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001544 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001545 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001546
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001547 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001548 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001549 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001550
Eric Christopherfedfa442013-11-01 23:14:17 +00001551 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1552 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001553 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001554 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1555 II != IE; ++II) {
1556 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001557
Devang Patel002d54d2010-05-26 19:37:24 +00001558 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001559 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001560
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001561 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001562 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001563 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001564
1565 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001566 if (isDbgValueInDefinedReg(MI))
1567 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1568
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001569 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001570 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001571 if (History.empty()) {
1572 UserVariables.push_back(Var);
1573 // The first mention of a function argument gets the FunctionBeginSym
1574 // label, so arguments are visible when breaking at function entry.
1575 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001576 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001577 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001578 LabelsBeforeInsn[MI] = FunctionBeginSym;
1579 } else {
1580 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1581 const MachineInstr *Prev = History.back();
1582 if (Prev->isDebugValue()) {
1583 // Coalesce identical entries at the end of History.
1584 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001585 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001586 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001587 << "\t" << *Prev << "\t"
1588 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001589 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001590 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001591
1592 // Terminate old register assignments that don't reach MI;
1593 MachineFunction::const_iterator PrevMBB = Prev->getParent();
1594 if (PrevMBB != I && (!AtBlockEntry || llvm::next(PrevMBB) != I) &&
1595 isDbgValueInDefinedReg(Prev)) {
1596 // Previous register assignment needs to terminate at the end of
1597 // its basic block.
1598 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001599 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001600 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001601 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001602 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001603 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001604 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001605 } else if (llvm::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001606 // Terminate after LastMI.
1607 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001608 }
1609 }
1610 }
1611 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001612 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001613 // Not a DBG_VALUE instruction.
1614 if (!MI->isLabel())
1615 AtBlockEntry = false;
1616
Eric Christopher133195782012-10-04 20:46:14 +00001617 // First known non-DBG_VALUE and non-frame setup location marks
1618 // the beginning of the function body.
1619 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1620 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001621 PrologEndLoc = MI->getDebugLoc();
1622
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001623 // Check if the instruction clobbers any registers with debug vars.
1624 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
Eric Christopherfedfa442013-11-01 23:14:17 +00001625 MOE = MI->operands_end();
1626 MOI != MOE; ++MOI) {
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001627 if (!MOI->isReg() || !MOI->isDef() || !MOI->getReg())
1628 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001629 for (MCRegAliasIterator AI(MOI->getReg(), TRI, true); AI.isValid();
1630 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001631 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001632 const MDNode *Var = LiveUserVar[Reg];
1633 if (!Var)
1634 continue;
1635 // Reg is now clobbered.
1636 LiveUserVar[Reg] = 0;
1637
1638 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001639 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1640 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001641 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001642 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001643 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001644 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001645 const MachineInstr *Prev = History.back();
1646 // Sanity-check: Register assignments are terminated at the end of
1647 // their block.
1648 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1649 continue;
1650 // Is the variable still in Reg?
1651 if (!isDbgValueInDefinedReg(Prev) ||
1652 Prev->getOperand(0).getReg() != Reg)
1653 continue;
1654 // Var is clobbered. Make sure the next instruction gets a label.
1655 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001656 }
1657 }
Devang Patel002d54d2010-05-26 19:37:24 +00001658 }
Devang Patel002d54d2010-05-26 19:37:24 +00001659 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001660 }
1661
1662 for (DbgValueHistoryMap::iterator I = DbgValues.begin(), E = DbgValues.end();
1663 I != E; ++I) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001664 SmallVectorImpl<const MachineInstr *> &History = I->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001665 if (History.empty())
1666 continue;
1667
1668 // Make sure the final register assignments are terminated.
1669 const MachineInstr *Prev = History.back();
1670 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1671 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001672 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001673 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001674 if (LastMI == PrevMBB->end())
1675 // Drop DBG_VALUE for empty range.
1676 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001677 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001678 // Terminate after LastMI.
1679 History.push_back(LastMI);
1680 }
1681 }
1682 // Request labels for the full history.
1683 for (unsigned i = 0, e = History.size(); i != e; ++i) {
1684 const MachineInstr *MI = History[i];
1685 if (MI->isDebugValue())
1686 requestLabelBeforeInsn(MI);
1687 else
1688 requestLabelAfterInsn(MI);
1689 }
1690 }
Devang Patel002d54d2010-05-26 19:37:24 +00001691
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001692 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001693 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001694
1695 // Record beginning of function.
1696 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001697 DebugLoc FnStartDL =
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00001698 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
Eric Christopherfedfa442013-11-01 23:14:17 +00001699 recordSourceLine(
1700 FnStartDL.getLine(), FnStartDL.getCol(),
1701 FnStartDL.getScope(MF->getFunction()->getContext()),
1702 // We'd like to list the prologue as "not statements" but GDB behaves
1703 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1704 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001705 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001706}
1707
Devang Patel7e623022011-08-10 20:55:27 +00001708void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001709 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1710 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001711 // Variables with positive arg numbers are parameters.
1712 if (unsigned ArgNum = DV.getArgNumber()) {
1713 // Keep all parameters in order at the start of the variable list to ensure
1714 // function types are correct (no out-of-order parameters)
1715 //
1716 // This could be improved by only doing it for optimized builds (unoptimized
1717 // builds have the right order to begin with), searching from the back (this
1718 // would catch the unoptimized case quickly), or doing a binary search
1719 // rather than linear search.
1720 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1721 while (I != Vars.end()) {
1722 unsigned CurNum = (*I)->getVariable().getArgNumber();
1723 // A local (non-parameter) variable has been found, insert immediately
1724 // before it.
1725 if (CurNum == 0)
1726 break;
1727 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001728 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001729 break;
David Blaikieb272a752013-06-06 22:28:26 +00001730 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001731 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001732 Vars.insert(I, Var);
1733 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001734 }
1735
1736 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001737}
1738
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001739// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001740void DwarfDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001741 // Every beginFunction(MF) call should be followed by an endFunction(MF) call,
1742 // though the beginFunction may not be called at all.
1743 // We should handle both cases.
1744 if (CurFn == 0)
1745 CurFn = MF;
1746 else
1747 assert(CurFn == MF);
1748 assert(CurFn != 0);
1749
1750 if (!MMI->hasDebugInfo() || LScopes.empty()) {
1751 CurFn = 0;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001752 return;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001753 }
Devang Patel2904aa92009-11-12 19:02:56 +00001754
Devang Patel7e623022011-08-10 20:55:27 +00001755 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001756 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001757 // Assumes in correct section after the entry point.
1758 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher1a972152014-01-29 23:05:43 +00001759
Eric Christopher4287a492013-12-09 23:57:44 +00001760 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Manman Ren4e042a62013-02-05 21:52:47 +00001761 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001762
Devang Patel7e623022011-08-10 20:55:27 +00001763 SmallPtrSet<const MDNode *, 16> ProcessedVars;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001764 collectVariableInfo(ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001765
Devang Patel3acc70e2011-08-15 22:04:40 +00001766 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001767 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001768 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001769
Devang Patel7e623022011-08-10 20:55:27 +00001770 // Construct abstract scopes.
Devang Patel44403472011-08-12 18:10:19 +00001771 ArrayRef<LexicalScope *> AList = LScopes.getAbstractScopesList();
1772 for (unsigned i = 0, e = AList.size(); i != e; ++i) {
1773 LexicalScope *AScope = AList[i];
1774 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001775 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001776 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001777 DIArray Variables = SP.getVariables();
1778 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1779 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001780 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001781 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001782 // Check that DbgVariable for DV wasn't created earlier, when
1783 // findAbstractVariable() was called for inlined instance of DV.
1784 LLVMContext &Ctx = DV->getContext();
1785 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1786 if (AbstractVariables.lookup(CleanDV))
1787 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001788 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001789 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001790 }
1791 }
Devang Patel44403472011-08-12 18:10:19 +00001792 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001793 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001794 }
Eric Christopher6a841382012-11-19 22:42:10 +00001795
Devang Patel3acc70e2011-08-15 22:04:40 +00001796 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001797 if (!CurFn->getTarget().Options.DisableFramePointerElim(*CurFn))
Eric Christopherbb69a272012-08-24 01:14:27 +00001798 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001799
Eric Christopher1a972152014-01-29 23:05:43 +00001800 // Add the range of this function to the list of ranges for the CU.
1801 RangeSpan Span(FunctionBeginSym, FunctionEndSym);
1802 TheCU->addRange(llvm_move(Span));
1803
Bill Wendling2b128d72009-05-20 23:19:06 +00001804 // Clear debug info
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001805 for (ScopeVariablesMap::iterator I = ScopeVariables.begin(),
1806 E = ScopeVariables.end();
1807 I != E; ++I)
Devang Patel7e623022011-08-10 20:55:27 +00001808 DeleteContainerPointers(I->second);
1809 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001810 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001811 UserVariables.clear();
1812 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001813 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001814 LabelsBeforeInsn.clear();
1815 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001816 PrevLabel = NULL;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001817 CurFn = 0;
Bill Wendling2b128d72009-05-20 23:19:06 +00001818}
1819
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001820// Register a source line with debug info. Returns the unique label that was
1821// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001822void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1823 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001824 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001825 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001826 unsigned Src = 1;
1827 if (S) {
1828 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001829
Dan Gohman50849c62010-05-05 23:41:32 +00001830 if (Scope.isCompileUnit()) {
1831 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001832 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001833 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001834 } else if (Scope.isFile()) {
1835 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001836 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001837 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001838 } else if (Scope.isSubprogram()) {
1839 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001840 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001841 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001842 } else if (Scope.isLexicalBlockFile()) {
1843 DILexicalBlockFile DBF(S);
1844 Fn = DBF.getFilename();
1845 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001846 } else if (Scope.isLexicalBlock()) {
1847 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001848 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001849 Dir = DB.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001850 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001851 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001852
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001853 Src = getOrCreateSourceID(
1854 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001855 }
Nick Lewycky019d2552011-07-29 03:49:23 +00001856 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0, 0, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001857}
1858
Bill Wendling806535f2009-05-20 23:22:40 +00001859//===----------------------------------------------------------------------===//
1860// Emit Methods
1861//===----------------------------------------------------------------------===//
1862
Manman Rence20d462013-10-29 22:57:10 +00001863// Compute the size and offset of a DIE. The offset is relative to start of the
1864// CU. It returns the offset after laying out the DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001865unsigned DwarfFile::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001866 // Get the children.
1867 const std::vector<DIE *> &Children = Die->getChildren();
1868
Bill Wendling480ff322009-05-20 23:21:38 +00001869 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001870 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001871
1872 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001873 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001874
1875 // Set DIE offset
1876 Die->setOffset(Offset);
1877
1878 // Start the size with the size of abbreviation code.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001879 Offset += MCAsmInfo::getULEB128Size(Die->getAbbrevNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001880
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001881 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001882 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001883
1884 // Size the DIE attribute values.
1885 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1886 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001887 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001888
1889 // Size the DIE children if any.
1890 if (!Children.empty()) {
David Blaikieff3ab2c2013-12-05 01:01:41 +00001891 assert(Abbrev.getChildrenFlag() == dwarf::DW_CHILDREN_yes &&
Bill Wendling480ff322009-05-20 23:21:38 +00001892 "Children flag not set");
1893
1894 for (unsigned j = 0, M = Children.size(); j < M; ++j)
Eric Christopher1f0cbb82012-11-20 22:14:13 +00001895 Offset = computeSizeAndOffset(Children[j], Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001896
1897 // End of children marker.
1898 Offset += sizeof(int8_t);
1899 }
1900
1901 Die->setSize(Offset - Die->getOffset());
1902 return Offset;
1903}
1904
Eric Christopherb088d2d2013-10-24 21:05:08 +00001905// Compute the size and offset for each DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001906void DwarfFile::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001907 // Offset from the first CU in the debug info section is 0 initially.
1908 unsigned SecOffset = 0;
1909
Eric Christopherb088d2d2013-10-24 21:05:08 +00001910 // Iterate over each compile unit and set the size and offsets for each
1911 // DIE within each compile unit. All offsets are CU relative.
Eric Christophera5a79422013-12-09 23:32:48 +00001912 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = CUs.begin(),
1913 E = CUs.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001914 I != E; ++I) {
Manman Rence20d462013-10-29 22:57:10 +00001915 (*I)->setDebugInfoOffset(SecOffset);
1916
1917 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001918 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
David Blaikie6b288cf2013-10-30 20:42:41 +00001919 (*I)->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001920
1921 // EndOffset here is CU-relative, after laying out
1922 // all of the CU DIE.
David Blaikie2a80e442013-12-02 22:09:48 +00001923 unsigned EndOffset = computeSizeAndOffset((*I)->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001924 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001925 }
Bill Wendling480ff322009-05-20 23:21:38 +00001926}
1927
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001928// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001929void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001930 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001931
Bill Wendling480ff322009-05-20 23:21:38 +00001932 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001933 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001934 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Eric Christopherd8667202013-12-30 17:22:27 +00001935 if (useSplitDwarf())
1936 DwarfInfoDWOSectionSym =
1937 emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001938 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001939 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001940 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001941 DwarfAbbrevDWOSectionSym = emitSectionSym(
1942 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001943 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001944
Chris Lattner6629ca92010-04-04 22:59:04 +00001945 if (const MCSection *MacroInfo = TLOF.getDwarfMacroInfoSection())
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001946 emitSectionSym(Asm, MacroInfo);
Bill Wendling480ff322009-05-20 23:21:38 +00001947
Eric Christopher74804332013-02-07 21:19:50 +00001948 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001949 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001950 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001951 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001952 DwarfGnuPubNamesSectionSym =
1953 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1954 DwarfGnuPubTypesSectionSym =
1955 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001956 } else if (HasDwarfPubSections) {
1957 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1958 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001959 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001960
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001961 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001962 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001963 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001964 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001965 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001966 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001967 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00001968 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001969 DwarfDebugRangeSectionSym =
1970 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001971
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001972 DwarfDebugLocSectionSym =
1973 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Bill Wendling480ff322009-05-20 23:21:38 +00001974}
1975
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001976// Recursively emits a debug information entry.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001977void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001978 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001979 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001980
Bill Wendling480ff322009-05-20 23:21:38 +00001981 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001982 if (Asm->isVerbose())
David Blaikieff3ab2c2013-12-05 01:01:41 +00001983 Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
1984 "] 0x" + Twine::utohexstr(Die->getOffset()) +
1985 ":0x" + Twine::utohexstr(Die->getSize()) + " " +
1986 dwarf::TagString(Abbrev.getTag()));
1987 Asm->EmitULEB128(Abbrev.getNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001988
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001989 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001990 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001991
1992 // Emit the DIE attribute values.
1993 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00001994 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
1995 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00001996 assert(Form && "Too many attributes for DIE (check abbreviation)");
1997
Chris Lattner7bde8c02010-04-04 18:52:31 +00001998 if (Asm->isVerbose())
Chris Lattner5adf9872010-01-24 18:54:17 +00001999 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002000
Bill Wendling480ff322009-05-20 23:21:38 +00002001 switch (Attr) {
Manman Rence20d462013-10-29 22:57:10 +00002002 case dwarf::DW_AT_abstract_origin:
2003 case dwarf::DW_AT_type:
2004 case dwarf::DW_AT_friend:
2005 case dwarf::DW_AT_specification:
2006 case dwarf::DW_AT_import:
2007 case dwarf::DW_AT_containing_type: {
2008 DIEEntry *E = cast<DIEEntry>(Values[i]);
2009 DIE *Origin = E->getEntry();
2010 unsigned Addr = Origin->getOffset();
2011 if (Form == dwarf::DW_FORM_ref_addr) {
2012 assert(!useSplitDwarf() && "TODO: dwo files can't have relocations.");
2013 // For DW_FORM_ref_addr, output the offset from beginning of debug info
2014 // section. Origin->getOffset() returns the offset from start of the
2015 // compile unit.
Eric Christopher4287a492013-12-09 23:57:44 +00002016 DwarfCompileUnit *CU = CUDieMap.lookup(Origin->getUnit());
Manman Rence20d462013-10-29 22:57:10 +00002017 assert(CU && "CUDie should belong to a CU.");
2018 Addr += CU->getDebugInfoOffset();
2019 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
David Blaikie03073f72013-12-06 22:14:48 +00002020 Asm->EmitLabelPlusOffset(CU->getSectionSym(), Addr,
Manman Rence20d462013-10-29 22:57:10 +00002021 DIEEntry::getRefAddrSize(Asm));
2022 else
David Blaikie03073f72013-12-06 22:14:48 +00002023 Asm->EmitLabelOffsetDifference(CU->getSectionSym(), Addr,
2024 CU->getSectionSym(),
Manman Ren251a1bd2013-10-29 23:14:15 +00002025 DIEEntry::getRefAddrSize(Asm));
Manman Rence20d462013-10-29 22:57:10 +00002026 } else {
Manman Ren4dbdc902013-10-31 17:54:35 +00002027 // Make sure Origin belong to the same CU.
David Blaikie409dd9c2013-11-19 23:08:21 +00002028 assert(Die->getUnit() == Origin->getUnit() &&
Manman Ren4dbdc902013-10-31 17:54:35 +00002029 "The referenced DIE should belong to the same CU in ref4");
Manman Rence20d462013-10-29 22:57:10 +00002030 Asm->EmitInt32(Addr);
2031 }
2032 break;
2033 }
Devang Patel9fc11702010-05-25 23:40:22 +00002034 case dwarf::DW_AT_location: {
Nick Lewycky33da3362012-06-22 01:25:12 +00002035 if (DIELabel *L = dyn_cast<DIELabel>(Values[i])) {
2036 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherd0b82ae2013-11-16 00:18:40 +00002037 Asm->EmitSectionOffset(L->getValue(), DwarfDebugLocSectionSym);
Nick Lewycky33da3362012-06-22 01:25:12 +00002038 else
Ulrich Weigand396ba8b2013-07-02 18:46:26 +00002039 Asm->EmitLabelDifference(L->getValue(), DwarfDebugLocSectionSym, 4);
Nick Lewycky33da3362012-06-22 01:25:12 +00002040 } else {
Devang Patel9fc11702010-05-25 23:40:22 +00002041 Values[i]->EmitValue(Asm, Form);
Nick Lewycky33da3362012-06-22 01:25:12 +00002042 }
Devang Patel9fc11702010-05-25 23:40:22 +00002043 break;
2044 }
Devang Patela1bd5a12010-09-29 19:08:08 +00002045 case dwarf::DW_AT_accessibility: {
2046 if (Asm->isVerbose()) {
2047 DIEInteger *V = cast<DIEInteger>(Values[i]);
2048 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(V->getValue()));
2049 }
2050 Values[i]->EmitValue(Asm, Form);
2051 break;
2052 }
Bill Wendling480ff322009-05-20 23:21:38 +00002053 default:
2054 // Emit an attribute using the defined form.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002055 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00002056 break;
2057 }
Bill Wendling480ff322009-05-20 23:21:38 +00002058 }
2059
2060 // Emit the DIE children if any.
David Blaikieff3ab2c2013-12-05 01:01:41 +00002061 if (Abbrev.getChildrenFlag() == dwarf::DW_CHILDREN_yes) {
Bill Wendling480ff322009-05-20 23:21:38 +00002062 const std::vector<DIE *> &Children = Die->getChildren();
2063
2064 for (unsigned j = 0, M = Children.size(); j < M; ++j)
David Blaikieff3ab2c2013-12-05 01:01:41 +00002065 emitDIE(Children[j]);
Bill Wendling480ff322009-05-20 23:21:38 +00002066
David Blaikie155f8812013-12-04 21:51:05 +00002067 Asm->OutStreamer.AddComment("End Of Children Mark");
Chris Lattner566cae92010-03-09 23:52:58 +00002068 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00002069 }
2070}
2071
Eric Christophera2de8262012-12-15 00:04:07 +00002072// Emit the various dwarf units to the unit section USection with
2073// the abbreviations going into ASection.
David Blaikie03073f72013-12-06 22:14:48 +00002074void DwarfFile::emitUnits(DwarfDebug *DD, const MCSection *ASection,
2075 const MCSymbol *ASectionSym) {
Eric Christophera5a79422013-12-09 23:32:48 +00002076 for (SmallVectorImpl<DwarfUnit *>::iterator I = CUs.begin(), E = CUs.end();
2077 I != E; ++I) {
2078 DwarfUnit *TheU = *I;
David Blaikie2a80e442013-12-02 22:09:48 +00002079 DIE *Die = TheU->getUnitDie();
David Blaikie03073f72013-12-06 22:14:48 +00002080 const MCSection *USection = TheU->getSection();
2081 Asm->OutStreamer.SwitchSection(USection);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002082
Devang Patel1a0df9a2010-05-10 22:49:55 +00002083 // Emit the compile units header.
David Blaikie7d734602013-12-06 22:33:05 +00002084 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002085
Devang Patel1a0df9a2010-05-10 22:49:55 +00002086 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00002087 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00002088 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002089
David Blaikie2a80e442013-12-02 22:09:48 +00002090 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002091
David Blaikieff3ab2c2013-12-05 01:01:41 +00002092 DD->emitDIE(Die);
David Blaikie7d734602013-12-06 22:33:05 +00002093 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
Devang Patel1a0df9a2010-05-10 22:49:55 +00002094 }
Bill Wendling480ff322009-05-20 23:21:38 +00002095}
2096
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002097// Emit the debug info section.
2098void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00002099 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christophera2de8262012-12-15 00:04:07 +00002100
David Blaikie03073f72013-12-06 22:14:48 +00002101 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfAbbrevSection(),
Eric Christophera2de8262012-12-15 00:04:07 +00002102 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002103}
2104
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002105// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002106void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00002107 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00002108
2109 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00002110}
Bill Wendling480ff322009-05-20 23:21:38 +00002111
Eric Christopherf8194852013-12-05 18:06:10 +00002112void DwarfFile::emitAbbrevs(const MCSection *Section) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002113 // Check to see if it is worth the effort.
David Blaikie0504cda2013-12-05 07:43:55 +00002114 if (!Abbreviations.empty()) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002115 // Start the debug abbrev section.
2116 Asm->OutStreamer.SwitchSection(Section);
2117
Bill Wendling480ff322009-05-20 23:21:38 +00002118 // For each abbrevation.
David Blaikie0504cda2013-12-05 07:43:55 +00002119 for (unsigned i = 0, N = Abbreviations.size(); i < N; ++i) {
Bill Wendling480ff322009-05-20 23:21:38 +00002120 // Get abbreviation data
David Blaikie0504cda2013-12-05 07:43:55 +00002121 const DIEAbbrev *Abbrev = Abbreviations[i];
Bill Wendling480ff322009-05-20 23:21:38 +00002122
2123 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002124 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002125
2126 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002127 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002128 }
2129
2130 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002131 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002132 }
2133}
2134
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002135// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002136void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002137 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002138 Asm->OutStreamer.AddComment("Extended Op");
2139 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002140
Chris Lattner566cae92010-03-09 23:52:58 +00002141 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002142 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002143 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2144 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2145
2146 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002147
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002148 Asm->OutStreamer.EmitSymbolValue(
2149 Asm->GetTempSymbol("section_end", SectionEnd),
2150 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002151
2152 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002153 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2154 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002155 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002156 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002157}
2158
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002159// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002160void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002161 DwarfAccelTable AT(
2162 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Eric Christophera5a79422013-12-09 23:32:48 +00002163 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2164 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002165 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002166 DwarfUnit *TheU = *I;
David Blaikie2a80e442013-12-02 22:09:48 +00002167 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelNames();
David Blaikie2ea848b2013-11-19 22:51:04 +00002168 for (StringMap<std::vector<const DIE *> >::const_iterator
2169 GI = Names.begin(),
2170 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002171 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002172 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002173 const std::vector<const DIE *> &Entities = GI->second;
2174 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2175 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002176 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002177 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002178 }
2179 }
2180
2181 AT.FinalizeTable(Asm, "Names");
2182 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002183 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002184 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2185 Asm->OutStreamer.EmitLabel(SectionBegin);
2186
2187 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002188 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002189}
2190
Eric Christopher48fef592012-12-20 21:58:40 +00002191// Emit objective C classes and categories into a hashed accelerator table
2192// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002193void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002194 DwarfAccelTable AT(
2195 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Eric Christophera5a79422013-12-09 23:32:48 +00002196 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2197 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002198 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002199 DwarfUnit *TheU = *I;
David Blaikie2a80e442013-12-02 22:09:48 +00002200 const StringMap<std::vector<const DIE *> > &Names = TheU->getAccelObjC();
David Blaikie2ea848b2013-11-19 22:51:04 +00002201 for (StringMap<std::vector<const DIE *> >::const_iterator
2202 GI = Names.begin(),
2203 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002204 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002205 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002206 const std::vector<const DIE *> &Entities = GI->second;
2207 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2208 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002209 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002210 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002211 }
2212 }
2213
2214 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002215 Asm->OutStreamer.SwitchSection(
2216 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002217 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2218 Asm->OutStreamer.EmitLabel(SectionBegin);
2219
2220 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002221 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002222}
2223
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002224// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002225void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002226 DwarfAccelTable AT(
2227 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Eric Christophera5a79422013-12-09 23:32:48 +00002228 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2229 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002230 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002231 DwarfUnit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002232 const StringMap<std::vector<const DIE *> > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002233 TheU->getAccelNamespace();
David Blaikie2ea848b2013-11-19 22:51:04 +00002234 for (StringMap<std::vector<const DIE *> >::const_iterator
2235 GI = Names.begin(),
2236 GE = Names.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002237 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002238 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002239 const std::vector<const DIE *> &Entities = GI->second;
2240 for (std::vector<const DIE *>::const_iterator DI = Entities.begin(),
2241 DE = Entities.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002242 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002243 AT.AddName(Name, *DI);
Eric Christopher4996c702011-11-07 09:24:32 +00002244 }
2245 }
2246
2247 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002248 Asm->OutStreamer.SwitchSection(
2249 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002250 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2251 Asm->OutStreamer.EmitLabel(SectionBegin);
2252
2253 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002254 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002255}
2256
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002257// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002258void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002259 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002260 Atoms.push_back(
2261 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2262 Atoms.push_back(
2263 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2264 Atoms.push_back(
2265 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002266 DwarfAccelTable AT(Atoms);
Eric Christophera5a79422013-12-09 23:32:48 +00002267 for (SmallVectorImpl<DwarfUnit *>::const_iterator I = getUnits().begin(),
2268 E = getUnits().end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002269 I != E; ++I) {
Eric Christophera5a79422013-12-09 23:32:48 +00002270 DwarfUnit *TheU = *I;
David Blaikie2ea848b2013-11-19 22:51:04 +00002271 const StringMap<std::vector<std::pair<const DIE *, unsigned> > > &Names =
David Blaikie2a80e442013-12-02 22:09:48 +00002272 TheU->getAccelTypes();
David Blaikie2ea848b2013-11-19 22:51:04 +00002273 for (StringMap<
2274 std::vector<std::pair<const DIE *, unsigned> > >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002275 GI = Names.begin(),
2276 GE = Names.end();
2277 GI != GE; ++GI) {
Benjamin Kramer63e39eb2013-05-11 18:24:28 +00002278 StringRef Name = GI->getKey();
David Blaikie2ea848b2013-11-19 22:51:04 +00002279 const std::vector<std::pair<const DIE *, unsigned> > &Entities =
2280 GI->second;
2281 for (std::vector<std::pair<const DIE *, unsigned> >::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002282 DI = Entities.begin(),
2283 DE = Entities.end();
2284 DI != DE; ++DI)
David Blaikie2ea848b2013-11-19 22:51:04 +00002285 AT.AddName(Name, DI->first, DI->second);
Eric Christopher4996c702011-11-07 09:24:32 +00002286 }
2287 }
2288
2289 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002290 Asm->OutStreamer.SwitchSection(
2291 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002292 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2293 Asm->OutStreamer.EmitLabel(SectionBegin);
2294
2295 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002296 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002297}
2298
Eric Christopherdd1a0122013-09-13 00:35:05 +00002299// Public name handling.
2300// The format for the various pubnames:
2301//
2302// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2303// for the DIE that is named.
2304//
2305// gnu pubnames - offset/index value/name tuples where the offset is the offset
2306// into the CU and the index value is computed according to the type of value
2307// for the DIE that is named.
2308//
2309// For type units the offset is the offset of the skeleton DIE. For split dwarf
2310// it's the offset within the debug_info/debug_types dwo section, however, the
2311// reference in the pubname header doesn't change.
2312
2313/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00002314static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002315 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002316 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2317
2318 // We could have a specification DIE that has our most of our knowledge,
2319 // look for that now.
2320 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2321 if (SpecVal) {
2322 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2323 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2324 Linkage = dwarf::GIEL_EXTERNAL;
2325 } else if (Die->findAttribute(dwarf::DW_AT_external))
2326 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002327
2328 switch (Die->getTag()) {
2329 case dwarf::DW_TAG_class_type:
2330 case dwarf::DW_TAG_structure_type:
2331 case dwarf::DW_TAG_union_type:
2332 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002333 return dwarf::PubIndexEntryDescriptor(
2334 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2335 ? dwarf::GIEL_STATIC
2336 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002337 case dwarf::DW_TAG_typedef:
2338 case dwarf::DW_TAG_base_type:
2339 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002340 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002341 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002342 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002343 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002344 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002345 case dwarf::DW_TAG_constant:
2346 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002347 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002348 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002349 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2350 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002351 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002352 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002353 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002354}
2355
Eric Christopher5f93bb92013-09-09 20:03:17 +00002356/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002357///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002358void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Eric Christopherdd1a0122013-09-13 00:35:05 +00002359 const MCSection *PSec =
2360 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2361 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002362
David Blaikie4f623202013-12-06 19:38:49 +00002363 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christophera5a79422013-12-09 23:32:48 +00002364 const SmallVectorImpl<DwarfUnit *> &Units = Holder.getUnits();
David Blaikie4f623202013-12-06 19:38:49 +00002365 for (unsigned i = 0; i != Units.size(); ++i) {
Eric Christophera5a79422013-12-09 23:32:48 +00002366 DwarfUnit *TheU = Units[i];
David Blaikie2a80e442013-12-02 22:09:48 +00002367 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002368
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002369 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002370 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002371
Eric Christopher39eebfa2013-09-30 23:14:16 +00002372 // Emit a label so we can reference the beginning of this pubname section.
2373 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002374 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubnames", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002375
Eric Christopherdd1a0122013-09-13 00:35:05 +00002376 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002377 Asm->OutStreamer.AddComment("Length of Public Names Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002378 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubnames_begin", ID);
2379 MCSymbol *EndLabel = Asm->GetTempSymbol("pubnames_end", ID);
2380 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002381
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002382 Asm->OutStreamer.EmitLabel(BeginLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002383
2384 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002385 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002386
2387 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002388 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002389
2390 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002391 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002392
Eric Christopherdd1a0122013-09-13 00:35:05 +00002393 // Emit the pubnames for this compilation unit.
David Blaikie4f623202013-12-06 19:38:49 +00002394 const StringMap<const DIE *> &Globals = getUnits()[ID]->getGlobalNames();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002395 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2396 GE = Globals.end();
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002397 GI != GE; ++GI) {
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002398 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002399 const DIE *Entity = GI->second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002400
2401 Asm->OutStreamer.AddComment("DIE offset");
2402 Asm->EmitInt32(Entity->getOffset());
2403
Eric Christopherdd1a0122013-09-13 00:35:05 +00002404 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002405 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002406 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002407 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002408 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002409 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002410 }
2411
David Blaikie155f8812013-12-04 21:51:05 +00002412 Asm->OutStreamer.AddComment("External Name");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002413 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002414 }
2415
2416 Asm->OutStreamer.AddComment("End Mark");
2417 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002418 Asm->OutStreamer.EmitLabel(EndLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002419 }
2420}
2421
Eric Christopherdd1a0122013-09-13 00:35:05 +00002422void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher261d2342013-09-23 20:55:35 +00002423 const MCSection *PSec =
2424 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2425 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002426
David Blaikie4f623202013-12-06 19:38:49 +00002427 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christophera5a79422013-12-09 23:32:48 +00002428 const SmallVectorImpl<DwarfUnit *> &Units = Holder.getUnits();
David Blaikie4f623202013-12-06 19:38:49 +00002429 for (unsigned i = 0; i != Units.size(); ++i) {
Eric Christophera5a79422013-12-09 23:32:48 +00002430 DwarfUnit *TheU = Units[i];
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002431 unsigned ID = TheU->getUniqueID();
2432
Eric Christopher6abc9c52011-11-07 09:18:35 +00002433 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002434 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002435
2436 // Emit a label so we can reference the beginning of this pubtype section.
2437 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002438 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubtypes", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002439
2440 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002441 Asm->OutStreamer.AddComment("Length of Public Types Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002442 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubtypes_begin", ID);
2443 MCSymbol *EndLabel = Asm->GetTempSymbol("pubtypes_end", ID);
2444 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002445
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002446 Asm->OutStreamer.EmitLabel(BeginLabel);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002447
David Blaikie155f8812013-12-04 21:51:05 +00002448 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002449 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002450
Devang Patel1a0df9a2010-05-10 22:49:55 +00002451 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002452 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002453
Devang Patel1a0df9a2010-05-10 22:49:55 +00002454 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002455 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002456
Eric Christopher39eebfa2013-09-30 23:14:16 +00002457 // Emit the pubtypes.
David Blaikie4f623202013-12-06 19:38:49 +00002458 const StringMap<const DIE *> &Globals = getUnits()[ID]->getGlobalTypes();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002459 for (StringMap<const DIE *>::const_iterator GI = Globals.begin(),
2460 GE = Globals.end();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002461 GI != GE; ++GI) {
Devang Patel1a0df9a2010-05-10 22:49:55 +00002462 const char *Name = GI->getKeyData();
Eric Christopher0fe676a2013-11-21 00:48:22 +00002463 const DIE *Entity = GI->second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002464
David Blaikie155f8812013-12-04 21:51:05 +00002465 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002466 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002467
Eric Christopherdd1a0122013-09-13 00:35:05 +00002468 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002469 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002470 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002471 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002472 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002473 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002474 }
2475
David Blaikie155f8812013-12-04 21:51:05 +00002476 Asm->OutStreamer.AddComment("External Name");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002477
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002478 // Emit the name with a terminating null byte.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002479 Asm->OutStreamer.EmitBytes(StringRef(Name, GI->getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002480 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002481
Devang Patel1a0df9a2010-05-10 22:49:55 +00002482 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002483 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002484 Asm->OutStreamer.EmitLabel(EndLabel);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002485 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002486}
2487
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002488// Emit strings into a string section.
Eric Christopherf8194852013-12-05 18:06:10 +00002489void DwarfFile::emitStrings(const MCSection *StrSection,
Eric Christophera5a79422013-12-09 23:32:48 +00002490 const MCSection *OffsetSection = NULL,
2491 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002492
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002493 if (StringPool.empty())
2494 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002495
Chris Lattner3d72a672010-03-09 23:38:23 +00002496 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002497 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002498
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002499 // Get all of the string pool entries and put them in an array by their ID so
2500 // we can sort them.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002501 SmallVector<
2502 std::pair<unsigned, StringMapEntry<std::pair<MCSymbol *, unsigned> > *>,
2503 64> Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002504
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002505 for (StringMap<std::pair<MCSymbol *, unsigned> >::iterator
2506 I = StringPool.begin(),
2507 E = StringPool.end();
Eric Christopher48fef592012-12-20 21:58:40 +00002508 I != E; ++I)
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002509 Entries.push_back(std::make_pair(I->second.second, &*I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002510
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002511 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002512
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002513 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002514 // Emit a label for reference from debug information entries.
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002515 Asm->OutStreamer.EmitLabel(Entries[i].second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002516
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002517 // Emit the string itself with a terminating null byte.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002518 Asm->OutStreamer.EmitBytes(
2519 StringRef(Entries[i].second->getKeyData(),
2520 Entries[i].second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002521 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002522
2523 // If we've got an offset section go ahead and emit that now as well.
2524 if (OffsetSection) {
2525 Asm->OutStreamer.SwitchSection(OffsetSection);
2526 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002527 unsigned size = 4; // FIXME: DWARF64 is 8.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002528 for (unsigned i = 0, e = Entries.size(); i != e; ++i) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002529 Asm->OutStreamer.EmitIntValue(offset, size);
Eric Christopher2cbd5762013-01-07 19:32:41 +00002530 offset += Entries[i].second->getKeyLength() + 1;
2531 }
2532 }
Bill Wendling480ff322009-05-20 23:21:38 +00002533}
2534
Eric Christopher65132a82013-11-19 09:11:26 +00002535// Emit addresses into the section given.
Eric Christopherf8194852013-12-05 18:06:10 +00002536void DwarfFile::emitAddresses(const MCSection *AddrSection) {
Eric Christopher962c9082013-01-15 23:56:56 +00002537
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002538 if (AddressPool.empty())
2539 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002540
2541 // Start the dwarf addr section.
2542 Asm->OutStreamer.SwitchSection(AddrSection);
2543
David Blaikiece1960f2013-07-08 17:51:28 +00002544 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002545 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002546
David Blaikief1a6dea2014-02-15 19:34:03 +00002547 for (AddrPool::iterator I = AddressPool.begin(), E = AddressPool.end();
Eric Christopher962c9082013-01-15 23:56:56 +00002548 I != E; ++I)
David Blaikief1a6dea2014-02-15 19:34:03 +00002549 Entries[I->second.Number] =
2550 I->second.TLS
2551 ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I->first)
2552 : MCSymbolRefExpr::Create(I->first, Asm->OutContext);
Eric Christopher962c9082013-01-15 23:56:56 +00002553
David Blaikief28703a2014-02-15 18:33:11 +00002554 for (unsigned i = 0, e = Entries.size(); i != e; ++i)
2555 Asm->OutStreamer.EmitValue(Entries[i],
2556 Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002557}
2558
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002559// Emit visible names into a debug str section.
2560void DwarfDebug::emitDebugStr() {
Eric Christopherf8194852013-12-05 18:06:10 +00002561 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002562 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2563}
2564
Eric Christopher9046f942013-07-02 21:36:07 +00002565// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002566void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002567 if (DotDebugLocEntries.empty())
2568 return;
2569
Eric Christopher4887c8f2013-03-29 23:34:06 +00002570 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002571 I = DotDebugLocEntries.begin(),
2572 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002573 I != E; ++I) {
2574 DotDebugLocEntry &Entry = *I;
2575 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002576 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002577 }
2578
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002579 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002580 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002581 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002582 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002583 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2584 unsigned index = 1;
Eric Christopher4887c8f2013-03-29 23:34:06 +00002585 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002586 I = DotDebugLocEntries.begin(),
2587 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002588 I != E; ++I, ++index) {
Devang Patel116a9d72011-02-04 22:57:18 +00002589 DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002590 if (Entry.isMerged())
2591 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002592 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002593 Asm->OutStreamer.EmitIntValue(0, Size);
2594 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002595 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002596 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002597 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2598 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2599 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002600 Asm->OutStreamer.AddComment("Loc expr size");
2601 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2602 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2603 Asm->EmitLabelDifference(end, begin, 2);
2604 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002605 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002606 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002607 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2608 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002609 Asm->OutStreamer.AddComment("DW_OP_consts");
2610 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002611 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002612 } else {
2613 Asm->OutStreamer.AddComment("DW_OP_constu");
2614 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002615 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002616 }
Devang Pateled9fd452011-07-08 16:49:43 +00002617 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002618 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002619 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002620 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002621 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002622 else {
2623 // Complex address entry.
2624 unsigned N = DV.getNumAddrElements();
2625 unsigned i = 0;
2626 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002627 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002628 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002629 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002630 Asm->OutStreamer.AddComment("DW_OP_deref");
2631 Asm->EmitInt8(dwarf::DW_OP_deref);
2632 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2633 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2634 Asm->EmitSLEB128(DV.getAddrElement(1));
2635 } else {
2636 // If first address element is OpPlus then emit
2637 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002638 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2639 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002640 i = 2;
2641 }
2642 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002643 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002644 }
Eric Christopher6a841382012-11-19 22:42:10 +00002645
Devang Pateled9fd452011-07-08 16:49:43 +00002646 // Emit remaining complex address elements.
2647 for (; i < N; ++i) {
2648 uint64_t Element = DV.getAddrElement(i);
2649 if (Element == DIBuilder::OpPlus) {
2650 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2651 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002652 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002653 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002654 Asm->EmitInt8(dwarf::DW_OP_deref);
2655 } else
2656 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002657 }
Devang Patel3e021532011-04-28 02:22:40 +00002658 }
Devang Patel3e021532011-04-28 02:22:40 +00002659 }
Devang Pateled9fd452011-07-08 16:49:43 +00002660 // else ... ignore constant fp. There is not any good way to
2661 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002662 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002663 }
2664 }
Bill Wendling480ff322009-05-20 23:21:38 +00002665}
2666
Richard Mitton21101b32013-09-19 23:21:01 +00002667struct SymbolCUSorter {
2668 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2669 const MCStreamer &Streamer;
2670
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002671 bool operator()(const SymbolCU &A, const SymbolCU &B) {
Richard Mitton21101b32013-09-19 23:21:01 +00002672 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2673 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2674
2675 // Symbols with no order assigned should be placed at the end.
2676 // (e.g. section end labels)
2677 if (IA == 0)
2678 IA = (unsigned)(-1);
2679 if (IB == 0)
2680 IB = (unsigned)(-1);
2681 return IA < IB;
2682 }
2683};
2684
Eric Christophera5a79422013-12-09 23:32:48 +00002685static bool CUSort(const DwarfUnit *A, const DwarfUnit *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002686 return (A->getUniqueID() < B->getUniqueID());
Richard Mitton21101b32013-09-19 23:21:01 +00002687}
2688
2689struct ArangeSpan {
2690 const MCSymbol *Start, *End;
2691};
2692
2693// Emit a debug aranges section, containing a CU lookup for any
2694// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002695void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002696 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002697 Asm->OutStreamer.SwitchSection(
2698 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002699
Eric Christopher4287a492013-12-09 23:57:44 +00002700 typedef DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan> > SpansType;
Richard Mitton21101b32013-09-19 23:21:01 +00002701
2702 SpansType Spans;
2703
2704 // Build a list of sections used.
2705 std::vector<const MCSection *> Sections;
2706 for (SectionMapType::iterator it = SectionMap.begin(); it != SectionMap.end();
2707 it++) {
2708 const MCSection *Section = it->first;
2709 Sections.push_back(Section);
2710 }
2711
2712 // Sort the sections into order.
2713 // This is only done to ensure consistent output order across different runs.
2714 std::sort(Sections.begin(), Sections.end(), SectionSort);
2715
2716 // Build a set of address spans, sorted by CU.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002717 for (size_t SecIdx = 0; SecIdx < Sections.size(); SecIdx++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002718 const MCSection *Section = Sections[SecIdx];
2719 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2720 if (List.size() < 2)
2721 continue;
2722
2723 // Sort the symbols by offset within the section.
2724 SymbolCUSorter sorter(Asm->OutStreamer);
2725 std::sort(List.begin(), List.end(), sorter);
2726
2727 // If we have no section (e.g. common), just write out
2728 // individual spans for each symbol.
2729 if (Section == NULL) {
2730 for (size_t n = 0; n < List.size(); n++) {
2731 const SymbolCU &Cur = List[n];
2732
2733 ArangeSpan Span;
2734 Span.Start = Cur.Sym;
2735 Span.End = NULL;
2736 if (Cur.CU)
2737 Spans[Cur.CU].push_back(Span);
2738 }
2739 } else {
2740 // Build spans between each label.
2741 const MCSymbol *StartSym = List[0].Sym;
2742 for (size_t n = 1; n < List.size(); n++) {
2743 const SymbolCU &Prev = List[n - 1];
2744 const SymbolCU &Cur = List[n];
2745
2746 // Try and build the longest span we can within the same CU.
2747 if (Cur.CU != Prev.CU) {
2748 ArangeSpan Span;
2749 Span.Start = StartSym;
2750 Span.End = Cur.Sym;
2751 Spans[Prev.CU].push_back(Span);
2752 StartSym = Cur.Sym;
2753 }
2754 }
2755 }
2756 }
2757
Richard Mitton21101b32013-09-19 23:21:01 +00002758 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2759
2760 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00002761 std::vector<DwarfCompileUnit *> CUs;
Richard Mitton21101b32013-09-19 23:21:01 +00002762 for (SpansType::iterator it = Spans.begin(); it != Spans.end(); it++) {
Eric Christopher4287a492013-12-09 23:57:44 +00002763 DwarfCompileUnit *CU = it->first;
Richard Mitton21101b32013-09-19 23:21:01 +00002764 CUs.push_back(CU);
2765 }
2766
2767 // Sort the CU list (again, to ensure consistent output order).
2768 std::sort(CUs.begin(), CUs.end(), CUSort);
2769
2770 // Emit an arange table for each CU we used.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002771 for (size_t CUIdx = 0; CUIdx < CUs.size(); CUIdx++) {
Eric Christopher4287a492013-12-09 23:57:44 +00002772 DwarfCompileUnit *CU = CUs[CUIdx];
Richard Mitton21101b32013-09-19 23:21:01 +00002773 std::vector<ArangeSpan> &List = Spans[CU];
2774
2775 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002776 unsigned ContentSize =
2777 sizeof(int16_t) + // DWARF ARange version number
2778 sizeof(int32_t) + // Offset of CU in the .debug_info section
2779 sizeof(int8_t) + // Pointer Size (in bytes)
2780 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002781
2782 unsigned TupleSize = PtrSize * 2;
2783
2784 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002785 unsigned Padding =
2786 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00002787
2788 ContentSize += Padding;
2789 ContentSize += (List.size() + 1) * TupleSize;
2790
2791 // For each compile unit, write the list of spans it covers.
2792 Asm->OutStreamer.AddComment("Length of ARange Set");
2793 Asm->EmitInt32(ContentSize);
2794 Asm->OutStreamer.AddComment("DWARF Arange version number");
2795 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2796 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Eric Christopherd8667202013-12-30 17:22:27 +00002797 Asm->EmitSectionOffset(CU->getLocalLabelBegin(), CU->getLocalSectionSym());
Richard Mitton21101b32013-09-19 23:21:01 +00002798 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2799 Asm->EmitInt8(PtrSize);
2800 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2801 Asm->EmitInt8(0);
2802
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002803 Asm->OutStreamer.EmitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00002804
2805 for (unsigned n = 0; n < List.size(); n++) {
2806 const ArangeSpan &Span = List[n];
2807 Asm->EmitLabelReference(Span.Start, PtrSize);
2808
2809 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002810 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002811 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002812 } else {
2813 // For symbols without an end marker (e.g. common), we
2814 // write a single arange entry containing just that one symbol.
2815 uint64_t Size = SymSize[Span.Start];
2816 if (Size == 0)
2817 Size = 1;
2818
2819 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2820 }
Richard Mitton21101b32013-09-19 23:21:01 +00002821 }
2822
2823 Asm->OutStreamer.AddComment("ARange terminator");
2824 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2825 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2826 }
Bill Wendling480ff322009-05-20 23:21:38 +00002827}
2828
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002829// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002830void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002831 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002832 Asm->OutStreamer.SwitchSection(
2833 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002834
Eric Christopher0f63d062013-12-03 00:45:45 +00002835 // Size for our labels.
2836 unsigned char Size = Asm->getDataLayout().getPointerSize();
2837
2838 // Grab the specific ranges for the compile units in the module.
Eric Christopher179fba12014-01-29 22:06:23 +00002839 for (MapVector<const MDNode *, DwarfCompileUnit *>::iterator
2840 I = CUMap.begin(),
2841 E = CUMap.end();
Eric Christopher0f63d062013-12-03 00:45:45 +00002842 I != E; ++I) {
Eric Christopher4287a492013-12-09 23:57:44 +00002843 DwarfCompileUnit *TheCU = I->second;
Eric Christopher0f63d062013-12-03 00:45:45 +00002844
2845 // Emit a symbol so we can find the beginning of our ranges.
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002846 Asm->OutStreamer.EmitLabel(TheCU->getLabelRange());
Eric Christopher0f63d062013-12-03 00:45:45 +00002847
2848 // Iterate over the misc ranges for the compile units in the module.
Eric Christopher270ba4a2013-12-04 19:06:58 +00002849 const SmallVectorImpl<RangeSpanList> &RangeLists = TheCU->getRangeLists();
Eric Christophera5a79422013-12-09 23:32:48 +00002850 for (SmallVectorImpl<RangeSpanList>::const_iterator I = RangeLists.begin(),
2851 E = RangeLists.end();
Eric Christopher0f63d062013-12-03 00:45:45 +00002852 I != E; ++I) {
Eric Christopher270ba4a2013-12-04 19:06:58 +00002853 const RangeSpanList &List = *I;
Eric Christopher0f63d062013-12-03 00:45:45 +00002854
Eric Christopherf8790642013-12-04 22:04:50 +00002855 // Emit our symbol so we can find the beginning of the range.
2856 Asm->OutStreamer.EmitLabel(List.getSym());
Eric Christopher0f63d062013-12-03 00:45:45 +00002857
2858 for (SmallVectorImpl<RangeSpan>::const_iterator
Eric Christopher270ba4a2013-12-04 19:06:58 +00002859 RI = List.getRanges().begin(),
2860 RE = List.getRanges().end();
2861 RI != RE; ++RI) {
2862 const RangeSpan &Range = *RI;
Eric Christopher0f63d062013-12-03 00:45:45 +00002863 const MCSymbol *Begin = Range.getStart();
2864 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002865 assert(Begin && "Range without a begin symbol?");
2866 assert(End && "Range without an end symbol?");
2867 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2868 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002869 }
2870
2871 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002872 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002873 Asm->OutStreamer.EmitIntValue(0, Size);
2874 }
Eric Christopher46e23432013-12-20 04:16:18 +00002875
2876 // Now emit a range for the CU itself.
Eric Christopher1ad84572014-01-15 00:04:29 +00002877 if (useCURanges()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002878 Asm->OutStreamer.EmitLabel(
2879 Asm->GetTempSymbol("cu_ranges", TheCU->getUniqueID()));
2880 const SmallVectorImpl<RangeSpan> &Ranges = TheCU->getRanges();
2881 for (uint32_t i = 0, e = Ranges.size(); i != e; ++i) {
2882 RangeSpan Range = Ranges[i];
Eric Christopher46e23432013-12-20 04:16:18 +00002883 const MCSymbol *Begin = Range.getStart();
2884 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002885 assert(Begin && "Range without a begin symbol?");
2886 assert(End && "Range without an end symbol?");
2887 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2888 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher46e23432013-12-20 04:16:18 +00002889 }
2890 // And terminate the list with two 0 values.
2891 Asm->OutStreamer.EmitIntValue(0, Size);
2892 Asm->OutStreamer.EmitIntValue(0, Size);
2893 }
Devang Patel12563b32010-04-16 23:33:45 +00002894 }
Bill Wendling480ff322009-05-20 23:21:38 +00002895}
2896
Eric Christopherd692c1d2012-12-11 19:42:09 +00002897// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002898
David Blaikie38fe6342014-01-09 04:28:46 +00002899void DwarfDebug::initSkeletonUnit(const DwarfUnit *U, DIE *Die,
2900 DwarfUnit *NewU) {
2901 NewU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
2902 U->getCUNode().getSplitDebugFilename());
2903
2904 // Relocate to the beginning of the addr_base section, else 0 for the
2905 // beginning of the one for this compile unit.
2906 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherceec7b02014-01-11 00:23:18 +00002907 NewU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base, DwarfAddrSectionSym);
David Blaikie38fe6342014-01-09 04:28:46 +00002908 else
2909 NewU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
2910
2911 if (!CompilationDir.empty())
2912 NewU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2913
2914 addGnuPubAttributes(NewU, Die);
2915
2916 SkeletonHolder.addUnit(NewU);
2917}
2918
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002919// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2920// 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 +00002921// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher5090d572013-12-10 00:40:03 +00002922// TODO: Implement DW_AT_ranges_base.
Eric Christopher4287a492013-12-09 23:57:44 +00002923DwarfCompileUnit *DwarfDebug::constructSkeletonCU(const DwarfCompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002924
2925 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +00002926 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
David Blaikief645f962014-01-09 03:23:41 +00002927 CU->getUniqueID(), Die, CU->getCUNode(), Asm, this, &SkeletonHolder);
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002928 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
2929 DwarfInfoSectionSym);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002930
David Blaikie2494fdb2014-02-14 22:41:51 +00002931 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002932
David Blaikie38fe6342014-01-09 04:28:46 +00002933 initSkeletonUnit(CU, Die, NewCU);
Eric Christopherc8a310e2012-12-10 23:34:43 +00002934
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002935 return NewCU;
2936}
2937
David Blaikie15ed5eb2014-01-10 01:38:41 +00002938// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_dwo_name,
2939// DW_AT_addr_base.
David Blaikie15632ae2014-02-12 00:31:30 +00002940DwarfTypeUnit *DwarfDebug::constructSkeletonTU(DwarfTypeUnit *TU) {
David Blaikie60e63862014-02-14 23:58:13 +00002941 DwarfCompileUnit &CU = static_cast<DwarfCompileUnit &>(
2942 *SkeletonHolder.getUnits()[TU->getCU().getUniqueID()]);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002943
2944 DIE *Die = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie60e63862014-02-14 23:58:13 +00002945 DwarfTypeUnit *NewTU =
2946 new DwarfTypeUnit(TU->getUniqueID(), Die, CU, Asm, this, &SkeletonHolder);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002947 NewTU->setTypeSignature(TU->getTypeSignature());
2948 NewTU->setType(NULL);
2949 NewTU->initSection(
2950 Asm->getObjFileLowering().getDwarfTypesSection(TU->getTypeSignature()));
David Blaikie60e63862014-02-14 23:58:13 +00002951 CU.applyStmtList(*Die);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002952
2953 initSkeletonUnit(TU, Die, NewTU);
2954 return NewTU;
2955}
2956
Eric Christopherd692c1d2012-12-11 19:42:09 +00002957// Emit the .debug_info.dwo section for separated dwarf. This contains the
2958// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002959void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002960 assert(useSplitDwarf() && "No split dwarf debug info?");
David Blaikie03073f72013-12-06 22:14:48 +00002961 InfoHolder.emitUnits(this,
Eric Christopher3c5a1912012-12-19 22:02:53 +00002962 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2963 DwarfAbbrevDWOSectionSym);
2964}
2965
2966// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2967// abbreviations for the .debug_info.dwo section.
2968void DwarfDebug::emitDebugAbbrevDWO() {
2969 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00002970 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002971}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002972
2973// Emit the .debug_str.dwo section for separated dwarf. This contains the
2974// string section and is identical in format to traditional .debug_str
2975// sections.
2976void DwarfDebug::emitDebugStrDWO() {
2977 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002978 const MCSection *OffSec =
2979 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002980 const MCSymbol *StrSym = DwarfStrSectionSym;
2981 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2982 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002983}
David Blaikie409dd9c2013-11-19 23:08:21 +00002984
David Blaikie15632ae2014-02-12 00:31:30 +00002985void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikief645f962014-01-09 03:23:41 +00002986 StringRef Identifier, DIE *RefDie,
2987 DICompositeType CTy) {
David Blaikie322d79b2014-01-31 19:52:26 +00002988 // Flag the type unit reference as a declaration so that if it contains
2989 // members (implicit special members, static data member definitions, member
2990 // declarations for definitions in this CU, etc) consumers don't get confused
2991 // and think this is a full definition.
David Blaikie15632ae2014-02-12 00:31:30 +00002992 CU.addFlag(RefDie, dwarf::DW_AT_declaration);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002993
David Blaikie47f615e2013-12-17 23:32:35 +00002994 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
Chandler Carruthb587ab62014-01-20 08:07:07 +00002995 if (TU) {
David Blaikie15632ae2014-02-12 00:31:30 +00002996 CU.addDIETypeSignature(RefDie, *TU);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002997 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00002998 }
2999
Chandler Carruthb587ab62014-01-20 08:07:07 +00003000 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikied696fac2014-02-12 00:32:05 +00003001 DwarfTypeUnit *NewTU = new DwarfTypeUnit(InfoHolder.getUnits().size(),
3002 UnitDie, CU, Asm, this, &InfoHolder);
Chandler Carruthb587ab62014-01-20 08:07:07 +00003003 TU = NewTU;
3004 InfoHolder.addUnit(NewTU);
3005
3006 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie15632ae2014-02-12 00:31:30 +00003007 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00003008
3009 MD5 Hash;
3010 Hash.update(Identifier);
3011 // ... take the least significant 8 bytes and return those. Our MD5
3012 // implementation always returns its results in little endian, swap bytes
3013 // appropriately.
3014 MD5::MD5Result Result;
3015 Hash.final(Result);
3016 uint64_t Signature = *reinterpret_cast<support::ulittle64_t *>(Result + 8);
3017 NewTU->setTypeSignature(Signature);
3018 if (useSplitDwarf())
3019 NewTU->setSkeleton(constructSkeletonTU(NewTU));
David Blaikie60e63862014-02-14 23:58:13 +00003020 else
3021 CU.applyStmtList(*UnitDie);
Chandler Carruthb587ab62014-01-20 08:07:07 +00003022
3023 NewTU->setType(NewTU->createTypeDIE(CTy));
3024
3025 NewTU->initSection(
3026 useSplitDwarf()
3027 ? Asm->getObjFileLowering().getDwarfTypesDWOSection(Signature)
3028 : Asm->getObjFileLowering().getDwarfTypesSection(Signature));
3029
David Blaikie15632ae2014-02-12 00:31:30 +00003030 CU.addDIETypeSignature(RefDie, *NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00003031}