blob: e28bd5180190c0bfa650c6cfe4a64e19de7cb177 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000017#include "DIEHash.h"
Eric Christopher4996c702011-11-07 09:24:32 +000018#include "DwarfAccelTable.h"
David Blaikie2c86a722013-12-02 19:33:15 +000019#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/Triple.h"
David Greene829b3e82009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000027#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000032#include "llvm/IR/ValueHandle.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000033#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000034#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000035#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000036#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000037#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000038#include "llvm/Support/Debug.h"
David Majnemered89b5c2013-08-21 06:13:34 +000039#include "llvm/Support/Dwarf.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000040#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000041#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000042#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000043#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000045#include "llvm/Support/Timer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Target/TargetFrameLowering.h"
47#include "llvm/Target/TargetLoweringObjectFile.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetOptions.h"
50#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000051using namespace llvm;
52
Eric Christopher7f2b5512013-07-23 22:16:41 +000053static cl::opt<bool>
54DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
55 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000056
Eric Christopher7f2b5512013-07-23 22:16:41 +000057static cl::opt<bool> UnknownLocations(
58 "use-unknown-locations", cl::Hidden,
59 cl::desc("Make an absence of debug location information explicit."),
60 cl::init(false));
Dan Gohman7421ae42010-05-07 01:08:53 +000061
Eric Christopherb4bef6d2013-11-19 09:04:36 +000062static cl::opt<bool> GenerateCUHash("generate-cu-hash", cl::Hidden,
63 cl::desc("Add the CU hash as the dwo_id."),
64 cl::init(false));
Eric Christopherd29614f2013-08-13 01:21:55 +000065
Eric Christopherdd1a0122013-09-13 00:35:05 +000066static cl::opt<bool>
67GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
68 cl::desc("Generate GNU-style pubnames and pubtypes"),
69 cl::init(false));
70
Eric Christopher02dbadb2014-02-14 01:26:55 +000071static cl::opt<bool> GenerateARangeSection("generate-arange-section",
72 cl::Hidden,
73 cl::desc("Generate dwarf aranges"),
74 cl::init(false));
75
Eric Christopher20b76a72012-08-23 22:36:40 +000076namespace {
Eric Christopherf07ee3a2014-01-27 23:50:03 +000077enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher20b76a72012-08-23 22:36:40 +000078}
Eric Christopher4996c702011-11-07 09:24:32 +000079
Eric Christopher7f2b5512013-07-23 22:16:41 +000080static cl::opt<DefaultOnOff>
81DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
82 cl::desc("Output prototype dwarf accelerator tables."),
83 cl::values(clEnumVal(Default, "Default for platform"),
84 clEnumVal(Enable, "Enabled"),
85 clEnumVal(Disable, "Disabled"), clEnumValEnd),
86 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +000087
Eric Christopher7f2b5512013-07-23 22:16:41 +000088static cl::opt<DefaultOnOff>
Eric Christopher7f2b5512013-07-23 22:16:41 +000089SplitDwarf("split-dwarf", cl::Hidden,
Eric Christopher5d008fe2013-12-04 23:24:28 +000090 cl::desc("Output DWARF5 split debug info."),
Eric Christopher7f2b5512013-07-23 22:16:41 +000091 cl::values(clEnumVal(Default, "Default for platform"),
92 clEnumVal(Enable, "Enabled"),
93 clEnumVal(Disable, "Disabled"), clEnumValEnd),
94 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +000095
Eric Christopher7da24882013-08-19 21:07:38 +000096static cl::opt<DefaultOnOff>
Eric Christopher4d36ca02013-08-26 23:24:35 +000097DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
98 cl::desc("Generate DWARF pubnames and pubtypes sections"),
99 cl::values(clEnumVal(Default, "Default for platform"),
100 clEnumVal(Enable, "Enabled"),
101 clEnumVal(Disable, "Disabled"), clEnumValEnd),
102 cl::init(Default));
Eric Christopher7da24882013-08-19 21:07:38 +0000103
Eric Christopher33ff6972013-11-21 23:46:41 +0000104static cl::opt<unsigned>
105DwarfVersionNumber("dwarf-version", cl::Hidden,
Eric Christophera5a79422013-12-09 23:32:48 +0000106 cl::desc("Generate DWARF for dwarf version."), cl::init(0));
Eric Christopher33ff6972013-11-21 23:46:41 +0000107
Eric Christopher46e23432013-12-20 04:16:18 +0000108static cl::opt<bool>
109DwarfCURanges("generate-dwarf-cu-ranges", cl::Hidden,
110 cl::desc("Generate DW_AT_ranges for compile units"),
111 cl::init(false));
112
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000113static const char *const DWARFGroupName = "DWARF Emission";
114static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000115
Bill Wendling2f921f82009-05-15 09:23:25 +0000116//===----------------------------------------------------------------------===//
117
Bill Wendling2f921f82009-05-15 09:23:25 +0000118namespace llvm {
119
Manman Renbe5576f2013-10-08 19:07:44 +0000120/// resolve - Look in the DwarfDebug map for the MDNode that
121/// corresponds to the reference.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000122template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
Manman Renbe5576f2013-10-08 19:07:44 +0000123 return DD->resolve(Ref);
124}
125
Nick Lewycky019d2552011-07-29 03:49:23 +0000126DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000127 DIType Ty = Var.getType();
128 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
129 // addresses instead.
130 if (Var.isBlockByrefVariable()) {
131 /* Byref variables, in Blocks, are declared by the programmer as
132 "SomeType VarName;", but the compiler creates a
133 __Block_byref_x_VarName struct, and gives the variable VarName
134 either the struct, or a pointer to the struct, as its type. This
135 is necessary for various behind-the-scenes things the compiler
136 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000137
Devang Patelf20c4f72011-04-12 22:53:02 +0000138 However, as far as the original *programmer* is concerned, the
139 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000140
Devang Patelf20c4f72011-04-12 22:53:02 +0000141 The following function dives into the __Block_byref_x_VarName
142 struct to find the original type of the variable. This will be
143 passed back to the code generating the type for the Debug
144 Information Entry for the variable 'VarName'. 'VarName' will then
145 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000146
Devang Patelf20c4f72011-04-12 22:53:02 +0000147 The original type 'SomeType' will be the type of the field named
148 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000149
Devang Patelf20c4f72011-04-12 22:53:02 +0000150 NOTE: In order for this to not completely fail on the debugger
151 side, the Debug Information Entry for the variable VarName needs to
152 have a DW_AT_location that tells the debugger how to unwind through
153 the pointers and __Block_byref_x_VarName struct to find the actual
154 value of the variable. The function addBlockByrefType does this. */
155 DIType subType = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000156 uint16_t tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000157
Eric Christopher9adc55f2013-09-04 19:53:21 +0000158 if (tag == dwarf::DW_TAG_pointer_type)
Manman Renbe5576f2013-10-08 19:07:44 +0000159 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher6a841382012-11-19 22:42:10 +0000160
Eric Christopher9adc55f2013-09-04 19:53:21 +0000161 DIArray Elements = DICompositeType(subType).getTypeArray();
Devang Patelf20c4f72011-04-12 22:53:02 +0000162 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie5af2aca2013-11-18 23:57:26 +0000163 DIDerivedType DT(Elements.getElement(i));
Devang Patelf20c4f72011-04-12 22:53:02 +0000164 if (getName() == DT.getName())
Manman Renbe5576f2013-10-08 19:07:44 +0000165 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000166 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000167 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000168 return Ty;
169}
Bill Wendling2f921f82009-05-15 09:23:25 +0000170
Chris Lattnerf5d06362010-04-05 04:09:20 +0000171} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000172
Eric Christopher942f22c2014-01-11 00:28:12 +0000173/// Return Dwarf Version by checking module flags.
174static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Ren8bfde892013-07-16 23:21:16 +0000175 Value *Val = M->getModuleFlag("Dwarf Version");
176 if (!Val)
Eric Christophere31e0722013-09-04 22:21:24 +0000177 return dwarf::DWARF_VERSION;
Manman Ren8bfde892013-07-16 23:21:16 +0000178 return cast<ConstantInt>(Val)->getZExtValue();
Manman Renac8062b2013-07-02 23:40:10 +0000179}
180
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000181DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
David Blaikie0504cda2013-12-05 07:43:55 +0000182 : Asm(A), MMI(Asm->MMI), FirstCU(0), SourceIdMap(DIEValueAllocator),
David Blaikie2666e242013-12-06 19:38:46 +0000183 PrevLabel(NULL), GlobalRangeCount(0),
Eric Christopher1bca60d2014-01-23 22:55:47 +0000184 InfoHolder(A, "info_string", DIEValueAllocator), HasCURanges(false),
Eric Christopher2037caf2014-01-28 00:49:26 +0000185 UsedNonDefaultText(false),
David Blaikie0504cda2013-12-05 07:43:55 +0000186 SkeletonHolder(A, "skel_string", DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000187
Eric Christopher50effa02014-01-03 02:16:44 +0000188 DwarfInfoSectionSym = DwarfAbbrevSectionSym = DwarfStrSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000189 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher55863be2013-04-07 03:43:09 +0000190 DwarfAddrSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000191 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000192 FunctionBeginSym = FunctionEndSym = 0;
Eric Christophera5a79422013-12-09 23:32:48 +0000193 CurFn = 0;
194 CurMI = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000195
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000196 // Turn on accelerator tables for Darwin by default, pubnames by
197 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000198 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000199
Eric Christopher574b5c82013-08-19 21:41:38 +0000200 if (DwarfAccelTables == Default)
201 HasDwarfAccelTables = IsDarwin;
202 else
Eric Christopher5297df02013-08-26 20:58:35 +0000203 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000204
Eric Christophercdf218d2012-12-10 19:51:21 +0000205 if (SplitDwarf == Default)
206 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000207 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000208 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000209
Eric Christopher4d36ca02013-08-26 23:24:35 +0000210 if (DwarfPubSections == Default)
211 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000212 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000213 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000214
Eric Christopher942f22c2014-01-11 00:28:12 +0000215 DwarfVersion = DwarfVersionNumber
216 ? DwarfVersionNumber
217 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000218
Dan Gohman6e681a52010-06-18 15:56:31 +0000219 {
220 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000221 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000222 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000223}
Bill Wendling2f921f82009-05-15 09:23:25 +0000224
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000225// Switch to the specified MCSection and emit an assembler
226// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000227static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000228 const char *SymbolStem = 0) {
229 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000230 if (!SymbolStem)
231 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000232
233 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
234 Asm->OutStreamer.EmitLabel(TmpSym);
235 return TmpSym;
236}
237
Eric Christopherf8194852013-12-05 18:06:10 +0000238DwarfFile::~DwarfFile() {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000239 for (DwarfUnit *DU : CUs)
240 delete DU;
David Blaikie72f1a3e2013-11-23 01:17:34 +0000241}
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)
Adrian Prantl69140d22014-02-25 22:27:14 +0000405 SPCU->constructSubprogramArguments(*SPDie, Args);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000406 DIE *SPDeclDie = SPDie;
David Blaikie2a80e442013-12-02 22:09:48 +0000407 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
408 *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000409 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000410 }
411 }
Devang Patela37a95e2010-07-07 22:20:57 +0000412 }
413
David Blaikie4bd13b72014-03-07 18:49:45 +0000414 attachLowHighPC(SPCU, SPDie, FunctionBeginSym, FunctionEndSym);
Eric Christopher0f63d062013-12-03 00:45:45 +0000415
Chris Lattner3a383cb2010-04-05 00:13:49 +0000416 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
417 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000418 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000419
Eric Christopherd9843b32011-11-10 19:25:34 +0000420 // Add name to the name table, we do this here because we're guaranteed
421 // to have concrete versions of our DW_TAG_subprogram nodes.
422 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000423
Chris Lattner3a383cb2010-04-05 00:13:49 +0000424 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000425}
426
Manman Ren5b2f4b02013-09-11 19:40:28 +0000427/// Check whether we should create a DIE for the given Scope, return true
428/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000429bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
430 if (Scope->isAbstractScope())
431 return false;
432
Manman Ren5b2f4b02013-09-11 19:40:28 +0000433 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000434 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
435 if (Ranges.empty())
436 return true;
437
438 if (Ranges.size() > 1)
439 return false;
440
Manman Ren5b2f4b02013-09-11 19:40:28 +0000441 // We don't create a DIE if we have a single Range and the end label
442 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000443 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
444 MCSymbol *End = getLabelAfterInsn(RI->second);
445 return !End;
446}
447
Eric Christophera5a79422013-12-09 23:32:48 +0000448static void addSectionLabel(AsmPrinter *Asm, DwarfUnit *U, DIE *D,
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000449 dwarf::Attribute A, const MCSymbol *L,
450 const MCSymbol *Sec) {
451 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
452 U->addSectionLabel(D, A, L);
453 else
454 U->addSectionDelta(D, A, L, Sec);
455}
456
Eric Christopher4287a492013-12-09 23:57:44 +0000457void DwarfDebug::addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
Eric Christopherbe2513e2013-12-03 00:45:59 +0000458 const SmallVectorImpl<InsnRange> &Range) {
459 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
460 // emitting it appropriately.
Eric Christopherf8790642013-12-04 22:04:50 +0000461 MCSymbol *RangeSym = Asm->GetTempSymbol("debug_ranges", GlobalRangeCount++);
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000462 addSectionLabel(Asm, TheCU, ScopeDIE, dwarf::DW_AT_ranges, RangeSym,
463 DwarfDebugRangeSectionSym);
464
Eric Christopherf8790642013-12-04 22:04:50 +0000465 RangeSpanList List(RangeSym);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000466 for (const InsnRange &R : Range) {
467 RangeSpan Span(getLabelBeforeInsn(R.first), getLabelAfterInsn(R.second));
Chandler Carruth002da5d2014-03-02 04:08:41 +0000468 List.addRange(std::move(Span));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000469 }
470
471 // Add the range list to the set of ranges to be emitted.
Chandler Carruth002da5d2014-03-02 04:08:41 +0000472 TheCU->addRangeList(std::move(List));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000473}
474
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000475// Construct new DW_TAG_lexical_block for this scope and attach
476// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher4287a492013-12-09 23:57:44 +0000477DIE *DwarfDebug::constructLexicalScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000478 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000479 if (isLexicalScopeDIENull(Scope))
480 return 0;
481
Devang Patel6c74a872010-04-27 19:46:33 +0000482 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
483 if (Scope->isAbstractScope())
484 return ScopeDIE;
485
Eric Christopher0f63d062013-12-03 00:45:45 +0000486 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherbe2513e2013-12-03 00:45:59 +0000487
Eric Christopherdc42ea82013-07-03 01:57:28 +0000488 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000489 if (ScopeRanges.size() > 1) {
Eric Christopherbe2513e2013-12-03 00:45:59 +0000490 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
Devang Patel6c74a872010-04-27 19:46:33 +0000491 return ScopeDIE;
492 }
493
Eric Christopherdc42ea82013-07-03 01:57:28 +0000494 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000495 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000496 MCSymbol *Start = getLabelBeforeInsn(RI->first);
497 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000498 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000499
Chris Lattnere13c3722010-03-09 01:58:53 +0000500 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
501 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000502
Eric Christopher962c9082013-01-15 23:56:56 +0000503 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
504 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000505
506 return ScopeDIE;
507}
508
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000509// This scope represents inlined body of a function. Construct DIE to
510// represent this concrete inlined copy of the function.
Eric Christopher4287a492013-12-09 23:57:44 +0000511DIE *DwarfDebug::constructInlinedScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000512 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000513 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopher1cdb63d2013-12-04 21:20:15 +0000514 assert(!ScopeRanges.empty() &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000515 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000516
Devang Patelf098ce22011-07-27 00:34:13 +0000517 if (!Scope->getScopeNode())
518 return NULL;
519 DIScope DS(Scope->getScopeNode());
520 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000521 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000522 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000523 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000524 return NULL;
525 }
526
Devang Patel73bc1722011-05-05 17:54:26 +0000527 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000528 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000529
Eric Christopherbe2513e2013-12-03 00:45:59 +0000530 // If we have multiple ranges, emit them into the range section.
531 if (ScopeRanges.size() > 1)
532 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
533 else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000534 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000535 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
536 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
537
538 if (StartLabel == 0 || EndLabel == 0)
539 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
540
541 assert(StartLabel->isDefined() &&
542 "Invalid starting label for an inlined scope!");
543 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
544
Eric Christopher962c9082013-01-15 23:56:56 +0000545 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, StartLabel);
546 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000547 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000548
549 InlinedSubprogramDIEs.insert(OriginDIE);
550
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000551 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000552 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000553 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000554 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
555 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000556 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000557
Eric Christopher8dda5d02011-12-04 06:02:38 +0000558 // Add name to the name table, we do this here because we're guaranteed
559 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
560 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000561
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000562 return ScopeDIE;
563}
564
Eric Christopher4287a492013-12-09 23:57:44 +0000565DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
566 LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000567 SmallVectorImpl<DIE *> &Children) {
568 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000569
570 // Collect arguments for current function.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000571 if (LScopes.isCurrentFunctionScope(Scope)) {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000572 for (DbgVariable *ArgDV : CurrentFnArguments)
573 if (ArgDV)
Eric Christopher6a841382012-11-19 22:42:10 +0000574 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000575 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000576 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000577 if (ArgDV->isObjectPointer())
578 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000579 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000580
Adrian Prantl69140d22014-02-25 22:27:14 +0000581 // If this is a variadic function, add an unspecified parameter.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000582 DISubprogram SP(Scope->getScopeNode());
Adrian Prantl3f49c892014-02-25 19:57:42 +0000583 DIArray FnArgs = SP.getType().getTypeArray();
Eric Christopher73ffdb82014-02-26 02:50:56 +0000584 if (FnArgs.getElement(FnArgs.getNumElements() - 1)
585 .isUnspecifiedParameter()) {
Adrian Prantl3f49c892014-02-25 19:57:42 +0000586 DIE *Ellipsis = new DIE(dwarf::DW_TAG_unspecified_parameters);
587 Children.push_back(Ellipsis);
588 }
589 }
590
Eric Christopherf84354b2011-10-03 15:49:16 +0000591 // Collect lexical scope children first.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000592 for (DbgVariable *DV : ScopeVariables.lookup(Scope))
593 if (DIE *Variable = TheCU->constructVariableDIE(*DV,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000594 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000595 Children.push_back(Variable);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000596 if (DV->isObjectPointer())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000597 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000598 }
Benjamin Kramer15596c72014-03-07 19:09:39 +0000599 for (LexicalScope *LS : Scope->getChildren())
600 if (DIE *Nested = constructScopeDIE(TheCU, LS))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000601 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000602 return ObjectPointer;
603}
604
605// Construct a DIE for this scope.
Eric Christopher4287a492013-12-09 23:57:44 +0000606DIE *DwarfDebug::constructScopeDIE(DwarfCompileUnit *TheCU,
607 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000608 if (!Scope || !Scope->getScopeNode())
609 return NULL;
610
611 DIScope DS(Scope->getScopeNode());
612
613 SmallVector<DIE *, 8> Children;
614 DIE *ObjectPointer = NULL;
615 bool ChildrenCreated = false;
616
Manman Ren5b2f4b02013-09-11 19:40:28 +0000617 // We try to create the scope DIE first, then the children DIEs. This will
618 // avoid creating un-used children then removing them later when we find out
619 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000620 DIE *ScopeDIE = NULL;
621 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000622 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000623 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000624 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000625 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000626 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000627 // Note down abstract DIE.
628 if (ScopeDIE)
629 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000630 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000631 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000632 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000633 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000634 if (isLexicalScopeDIENull(Scope))
635 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000636
637 // We create children here when we know the scope DIE is not going to be
638 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000639 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
640 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000641
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000642 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000643 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000644 ImportedEntityMap::const_iterator> Range =
645 std::equal_range(
646 ScopesWithImportedEntities.begin(),
647 ScopesWithImportedEntities.end(),
648 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
649 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000650 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000651 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000652 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000653 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000654 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
655 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000656 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000657 }
Eric Christopher6a841382012-11-19 22:42:10 +0000658
Benjamin Kramer892daba2013-08-24 11:55:49 +0000659 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000660 assert(Children.empty() &&
661 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000662 return NULL;
663 }
Manman Ren2312ed32013-09-10 18:40:41 +0000664 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000665 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000666 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000667
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000668 // Add children
Benjamin Kramer15596c72014-03-07 19:09:39 +0000669 for (DIE *I : Children)
670 ScopeDIE->addChild(I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000671
Eric Christophere3417762012-09-12 23:36:19 +0000672 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000673 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000674
Eric Christopherd9843b32011-11-10 19:25:34 +0000675 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000676}
677
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000678// Look up the source id with the given directory and source file names.
679// If none currently exists, create a new id and insert it in the
680// SourceIds map. This can update DirectoryNames and SourceFileNames maps
681// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000682unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
683 unsigned CUID) {
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000684 // If we print assembly, we can't separate .file entries according to
Manman Ren1e427202013-03-07 01:42:00 +0000685 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000686
687 // FIXME: add a better feature test than hasRawTextSupport. Even better,
688 // extend .file to support this.
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000689 if (Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000690 CUID = 0;
691
Devang Patel871d0b12010-09-16 20:57:49 +0000692 // If FE did not provide a file name, then assume stdin.
693 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000694 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000695
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000696 // TODO: this might not belong here. See if we can factor this better.
697 if (DirName == CompilationDir)
698 DirName = "";
699
Manman Ren1e427202013-03-07 01:42:00 +0000700 // FileIDCUMap stores the current ID for the given compile unit.
701 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000702
Manman Ren1e427202013-03-07 01:42:00 +0000703 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000704 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000705 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000706 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000707 NamePair += DirName;
708 NamePair += '\0'; // Zero bytes are not allowed in paths.
709 NamePair += FileName;
710
711 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
712 if (Ent.getValue() != SrcId)
713 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000714
Manman Ren1e427202013-03-07 01:42:00 +0000715 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000716 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000717 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000718
719 return SrcId;
720}
721
Eric Christophera5a79422013-12-09 23:32:48 +0000722void DwarfDebug::addGnuPubAttributes(DwarfUnit *U, DIE *D) const {
David Blaikie3c842622013-12-04 21:31:26 +0000723 if (!GenerateGnuPubSections)
724 return;
725
David Blaikie47c254b2014-03-06 05:47:39 +0000726 U->addFlag(D, dwarf::DW_AT_GNU_pubnames);
David Blaikie3c842622013-12-04 21:31:26 +0000727}
728
Eric Christopher4287a492013-12-09 23:57:44 +0000729// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000730// DW_TAG_compile_unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000731DwarfCompileUnit *DwarfDebug::constructDwarfCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000732 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000733 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000734
735 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +0000736 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
737 InfoHolder.getUnits().size(), Die, DIUnit, Asm, this, &InfoHolder);
David Blaikie2666e242013-12-06 19:38:46 +0000738 InfoHolder.addUnit(NewCU);
739
Manman Ren1e427202013-03-07 01:42:00 +0000740 FileIDCUMap[NewCU->getUniqueID()] = 0;
Manman Ren1e427202013-03-07 01:42:00 +0000741
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000742 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000743 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
744 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000745 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000746
Eric Christopher52ce7182013-04-09 19:23:15 +0000747 if (!useSplitDwarf()) {
David Blaikie2494fdb2014-02-14 22:41:51 +0000748 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000749
750 // If we're using split dwarf the compilation dir is going to be in the
751 // skeleton CU and so we don't need to duplicate it here.
752 if (!CompilationDir.empty())
753 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
754
David Blaikie3c842622013-12-04 21:31:26 +0000755 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000756 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000757
Bill Wendling2b128d72009-05-20 23:19:06 +0000758 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000759 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000760
Devang Patel2d9caf92009-11-25 17:36:49 +0000761 StringRef Flags = DIUnit.getFlags();
762 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000763 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000764
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000765 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000766 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000767 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000768
Devang Patel1a0df9a2010-05-10 22:49:55 +0000769 if (!FirstCU)
770 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000771
Eric Christopherd4368fd2014-01-02 21:03:28 +0000772 if (useSplitDwarf()) {
773 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection(),
774 DwarfInfoDWOSectionSym);
Eric Christopherd8667202013-12-30 17:22:27 +0000775 NewCU->setSkeleton(constructSkeletonCU(NewCU));
Eric Christopherd4368fd2014-01-02 21:03:28 +0000776 } else
777 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
778 DwarfInfoSectionSym);
Eric Christopherd039baa2013-12-30 03:40:32 +0000779
David Blaikie5a152402013-11-15 23:52:02 +0000780 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000781 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000782 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000783}
784
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000785// Construct subprogram DIE.
Eric Christopher4287a492013-12-09 23:57:44 +0000786void DwarfDebug::constructSubprogramDIE(DwarfCompileUnit *TheCU,
787 const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000788 // FIXME: We should only call this routine once, however, during LTO if a
789 // program is defined in multiple CUs we could end up calling it out of
790 // beginModule as we walk the CUs.
791
Eric Christopher4287a492013-12-09 23:57:44 +0000792 DwarfCompileUnit *&CURef = SPMap[N];
Eric Christopherffbc4de2013-10-18 01:57:30 +0000793 if (CURef)
794 return;
795 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000796
Devang Patel80ae3492009-08-28 23:24:31 +0000797 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000798 if (!SP.isDefinition())
799 // This is a method declaration which will be handled while constructing
800 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000801 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000802
Devang Patel89543712011-08-15 17:24:54 +0000803 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000804
Eric Christopherba506db2013-09-09 20:03:20 +0000805 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000806 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000807}
808
Eric Christopher4287a492013-12-09 23:57:44 +0000809void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000810 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000811 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000812 assert(Module.Verify());
David Blaikie684fc532013-05-06 23:33:07 +0000813 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000814 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000815}
816
Eric Christopher4287a492013-12-09 23:57:44 +0000817void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
818 const MDNode *N, DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000819 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000820 assert(Module.Verify());
David Blaikie4dd2de72013-05-08 06:01:38 +0000821 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000822}
823
Eric Christopher4287a492013-12-09 23:57:44 +0000824void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000825 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000826 DIE *Context) {
827 assert(Module.Verify() &&
828 "Use one of the MDNode * overloads to handle invalid metadata");
829 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000830 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000831 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000832 DIE *EntityDie;
833 DIDescriptor Entity = Module.getEntity();
834 if (Entity.isNameSpace())
835 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
836 else if (Entity.isSubprogram())
837 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000838 else if (Entity.isType())
839 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000840 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000841 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000842 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
843 Module.getContext().getDirectory(),
844 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000845 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
846 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000847 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000848 StringRef Name = Module.getName();
849 if (!Name.empty())
850 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000851 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000852}
853
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000854// Emit all Dwarf sections that should come prior to the content. Create
855// global DIEs and emit initial debug info sections. This is invoked by
856// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000857void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000858 if (DisableDebugInfoPrinting)
859 return;
860
Eric Christopher58f41952012-11-19 22:42:15 +0000861 const Module *M = MMI->getModule();
862
Nick Lewycky019d2552011-07-29 03:49:23 +0000863 // If module has named metadata anchors then use them, otherwise scan the
864 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000865 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000866 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000867 return;
Manman Ren60352032013-09-05 18:48:31 +0000868 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000869
David Blaikiedc69ebb2013-03-11 23:39:23 +0000870 // Emit initial sections so we can reference labels later.
871 emitSectionLabels();
872
Benjamin Kramer15596c72014-03-07 19:09:39 +0000873 for (MDNode *N : CU_Nodes->operands()) {
874 DICompileUnit CUNode(N);
Eric Christopher4287a492013-12-09 23:57:44 +0000875 DwarfCompileUnit *CU = constructDwarfCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000876 DIArray ImportedEntities = CUNode.getImportedEntities();
877 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000878 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000879 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
880 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000881 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000882 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000883 DIArray GVs = CUNode.getGlobalVariables();
884 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000885 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000886 DIArray SPs = CUNode.getSubprograms();
887 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
888 constructSubprogramDIE(CU, SPs.getElement(i));
889 DIArray EnumTypes = CUNode.getEnumTypes();
890 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
891 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
892 DIArray RetainedTypes = CUNode.getRetainedTypes();
893 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
894 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000895 // Emit imported_modules last so that the relevant context is already
896 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000897 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
898 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000899 }
Eric Christopher6a841382012-11-19 22:42:10 +0000900
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000901 // Tell MMI that we have debug info.
902 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000903
Bill Wendling2b128d72009-05-20 23:19:06 +0000904 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000905 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000906}
907
Eric Christopher960ac372012-11-22 00:59:49 +0000908// Attach DW_AT_inline attribute with inlined subprogram DIEs.
909void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000910 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000911 for (DIE *ISP : InlinedSubprogramDIEs)
David Blaikief2443192013-10-21 17:28:37 +0000912 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000913
914 for (const auto &AI : AbstractSPDies) {
915 DIE *ISP = AI.second;
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000916 if (InlinedSubprogramDIEs.count(ISP))
917 continue;
David Blaikief2443192013-10-21 17:28:37 +0000918 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000919 }
Eric Christopher960ac372012-11-22 00:59:49 +0000920}
921
922// Collect info for variables that were optimized out.
923void DwarfDebug::collectDeadVariables() {
924 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000925
926 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000927 for (MDNode *N : CU_Nodes->operands()) {
928 DICompileUnit TheCU(N);
Eric Christopher960ac372012-11-22 00:59:49 +0000929 DIArray Subprograms = TheCU.getSubprograms();
930 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000931 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000932 if (ProcessedSPNodes.count(SP) != 0)
933 continue;
934 if (!SP.isSubprogram())
935 continue;
936 if (!SP.isDefinition())
937 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000938 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +0000939 if (Variables.getNumElements() == 0)
940 continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000941
Eric Christopher735401c2012-11-27 00:13:51 +0000942 // Construct subprogram DIE and add variables DIEs.
Eric Christopher4287a492013-12-09 23:57:44 +0000943 DwarfCompileUnit *SPCU =
944 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +0000945 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +0000946 // FIXME: See the comment in constructSubprogramDIE about duplicate
947 // subprogram DIEs.
948 constructSubprogramDIE(SPCU, SP);
949 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +0000950 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
951 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +0000952 if (!DV.isVariable())
953 continue;
Manman Renb3388602013-10-05 01:43:03 +0000954 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000955 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +0000956 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +0000957 }
Eric Christopher960ac372012-11-22 00:59:49 +0000958 }
959 }
960 }
Eric Christopher960ac372012-11-22 00:59:49 +0000961}
962
963void DwarfDebug::finalizeModuleInfo() {
964 // Collect info for variables that were optimized out.
965 collectDeadVariables();
966
967 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
968 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000969
Eric Christopherad10cb52013-12-04 23:24:38 +0000970 // Handle anything that needs to be done on a per-unit basis after
971 // all other generation.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000972 for (DwarfUnit *TheU : getUnits()) {
Eric Christopher60eb7692013-08-12 20:27:48 +0000973 // Emit DW_AT_containing_type attribute to connect types with their
974 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +0000975 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000976
Eric Christopher46e23432013-12-20 04:16:18 +0000977 // Add CU specific attributes if we need to add any.
978 if (TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
979 // If we're splitting the dwarf out now that we've got the entire
Eric Christopherd8667202013-12-30 17:22:27 +0000980 // CU then add the dwo id to it.
981 DwarfCompileUnit *SkCU =
982 static_cast<DwarfCompileUnit *>(TheU->getSkeleton());
Eric Christopher46e23432013-12-20 04:16:18 +0000983 if (useSplitDwarf()) {
984 // This should be a unique identifier when we want to build .dwp files.
985 uint64_t ID = 0;
986 if (GenerateCUHash) {
Eric Christopher420569b2014-02-20 02:50:45 +0000987 DIEHash CUHash(Asm);
Eric Christopher46e23432013-12-20 04:16:18 +0000988 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
989 }
990 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
991 dwarf::DW_FORM_data8, ID);
Eric Christopher46e23432013-12-20 04:16:18 +0000992 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
993 dwarf::DW_FORM_data8, ID);
Eric Christopherd29614f2013-08-13 01:21:55 +0000994 }
Eric Christopherd8667202013-12-30 17:22:27 +0000995
Eric Christopher39cde8c2014-01-14 22:44:17 +0000996 // If we have code split among multiple sections or we've requested
997 // it then emit a DW_AT_ranges attribute on the unit that will remain
998 // in the .o file, otherwise add a DW_AT_low_pc.
Eric Christopherd8667202013-12-30 17:22:27 +0000999 // FIXME: Also add a high pc if we can.
Eric Christopher39cde8c2014-01-14 22:44:17 +00001000 // FIXME: We should use ranges if we have multiple compile units or
1001 // allow reordering of code ala .subsections_via_symbols in mach-o.
Eric Christopherd8667202013-12-30 17:22:27 +00001002 DwarfCompileUnit *U = SkCU ? SkCU : static_cast<DwarfCompileUnit *>(TheU);
Eric Christopher8873ada2014-01-29 22:22:56 +00001003 if (useCURanges() && TheU->getRanges().size()) {
Eric Christopherd8667202013-12-30 17:22:27 +00001004 addSectionLabel(Asm, U, U->getUnitDie(), dwarf::DW_AT_ranges,
1005 Asm->GetTempSymbol("cu_ranges", U->getUniqueID()),
1006 DwarfDebugRangeSectionSym);
Eric Christopher8873ada2014-01-29 22:22:56 +00001007
1008 // A DW_AT_low_pc attribute may also be specified in combination with
1009 // DW_AT_ranges to specify the default base address for use in location
1010 // lists (see Section 2.6.2) and range lists (see Section 2.17.3).
1011 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1012 0);
1013 } else
Eric Christophercf48ade2014-01-24 11:52:53 +00001014 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1015 0);
Eric Christopher60eb7692013-08-12 20:27:48 +00001016 }
1017 }
1018
1019 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001020 InfoHolder.computeSizeAndOffsets();
1021 if (useSplitDwarf())
1022 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001023}
1024
1025void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001026 // Filter labels by section.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001027 for (const SymbolCU &SCU : ArangeLabels) {
Richard Mitton21101b32013-09-19 23:21:01 +00001028 if (SCU.Sym->isInSection()) {
1029 // Make a note of this symbol and it's section.
1030 const MCSection *Section = &SCU.Sym->getSection();
1031 if (!Section->getKind().isMetadata())
1032 SectionMap[Section].push_back(SCU);
1033 } else {
1034 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1035 // appear in the output. This sucks as we rely on sections to build
1036 // arange spans. We can do it without, but it's icky.
1037 SectionMap[NULL].push_back(SCU);
1038 }
1039 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001040
Richard Mittonc2508242013-10-03 22:07:08 +00001041 // Build a list of sections used.
1042 std::vector<const MCSection *> Sections;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001043 for (const auto &it : SectionMap) {
1044 const MCSection *Section = it.first;
Richard Mittonc2508242013-10-03 22:07:08 +00001045 Sections.push_back(Section);
1046 }
1047
1048 // Sort the sections into order.
1049 // This is only done to ensure consistent output order across different runs.
1050 std::sort(Sections.begin(), Sections.end(), SectionSort);
1051
1052 // Add terminating symbols for each section.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001053 for (unsigned ID = 0, E = Sections.size(); ID != E; ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001054 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001055 MCSymbol *Sym = NULL;
1056
1057 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001058 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001059 // if we know the section name up-front. For user-created sections, the
Eric Christopher95531b62014-01-29 22:06:21 +00001060 // resulting label may not be valid to use as a label. (section names can
1061 // use a greater set of characters on some systems)
Richard Mittonc2508242013-10-03 22:07:08 +00001062 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001063 Asm->OutStreamer.SwitchSection(Section);
1064 Asm->OutStreamer.EmitLabel(Sym);
1065 }
1066
1067 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001068 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001069 }
Eric Christopher1bca60d2014-01-23 22:55:47 +00001070
1071 // For now only turn on CU ranges if we've explicitly asked for it,
Eric Christopherfb8dd002014-01-29 22:06:27 +00001072 // we have -ffunction-sections enabled, we've emitted a function
1073 // into a unique section, or we're using LTO. If we're using LTO then
1074 // we can't know that any particular function in the module is correlated
1075 // to a particular CU and so we need to be conservative. At this point all
1076 // sections should be finalized except for dwarf sections.
1077 HasCURanges = DwarfCURanges || UsedNonDefaultText || (CUMap.size() > 1) ||
Eric Christopher1bca60d2014-01-23 22:55:47 +00001078 TargetMachine::getFunctionSections();
Eric Christopher960ac372012-11-22 00:59:49 +00001079}
Bill Wendling2b128d72009-05-20 23:19:06 +00001080
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001081// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001082void DwarfDebug::endModule() {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001083 assert(CurFn == 0);
1084 assert(CurMI == 0);
Eric Christopher960ac372012-11-22 00:59:49 +00001085
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001086 if (!FirstCU)
1087 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001088
1089 // End any existing sections.
1090 // TODO: Does this need to happen?
1091 endSections();
1092
1093 // Finalize the debug info for the module.
1094 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001095
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001096 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001097
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001098 // Emit all the DIEs into a debug info section.
1099 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001100
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001101 // Corresponding abbreviations into a abbrev section.
1102 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001103
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001104 // Emit info into a debug loc section.
1105 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001106
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001107 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +00001108 if (GenerateARangeSection)
1109 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001110
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001111 // Emit info into a debug ranges section.
1112 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001113
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001114 if (useSplitDwarf()) {
1115 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001116 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001117 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001118 // Emit DWO addresses.
1119 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001120 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001121
Eric Christophera876b822012-08-23 07:32:06 +00001122 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001123 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001124 emitAccelNames();
1125 emitAccelObjC();
1126 emitAccelNamespaces();
1127 emitAccelTypes();
1128 }
Eric Christopher6a841382012-11-19 22:42:10 +00001129
Eric Christopher4b358182013-08-30 00:40:17 +00001130 // Emit the pubnames and pubtypes sections if requested.
1131 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001132 emitDebugPubNames(GenerateGnuPubSections);
1133 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001134 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001135
Devang Pateld0701282010-08-02 17:32:15 +00001136 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001137 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001138
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001139 // Reset these for the next Module if we have one.
1140 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001141}
1142
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001143// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001144DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001145 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001146 LLVMContext &Ctx = DV->getContext();
1147 // More then one inlined variable corresponds to one abstract variable.
1148 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001149 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001150 if (AbsDbgVariable)
1151 return AbsDbgVariable;
1152
Devang Patel7e623022011-08-10 20:55:27 +00001153 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001154 if (!Scope)
1155 return NULL;
1156
Manman Renb3388602013-10-05 01:43:03 +00001157 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001158 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001159 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001160 return AbsDbgVariable;
1161}
1162
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001163// If Var is a current function argument then add it to CurrentFnArguments list.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001164bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +00001165 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001166 return false;
1167 DIVariable DV = Var->getVariable();
1168 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1169 return false;
1170 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001171 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001172 return false;
1173
Devang Patel4ab660b2011-03-03 20:02:02 +00001174 size_t Size = CurrentFnArguments.size();
1175 if (Size == 0)
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001176 CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001177 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001178 // arguments does the function have at source level.
1179 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001180 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001181 CurrentFnArguments[ArgNo - 1] = Var;
1182 return true;
1183}
1184
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001185// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001186void DwarfDebug::collectVariableInfoFromMMITable(
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001187 SmallPtrSet<const MDNode *, 16> &Processed) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00001188 for (const auto &VI : MMI->getVariableDbgInfo()) {
1189 const MDNode *Var = VI.first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001190 if (!Var)
1191 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001192 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001193 DIVariable DV(Var);
Benjamin Kramer15596c72014-03-07 19:09:39 +00001194 const std::pair<unsigned, DebugLoc> &VP = VI.second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001195
Devang Patel7e623022011-08-10 20:55:27 +00001196 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001197
Devang Patelcdb7d442009-11-10 23:20:04 +00001198 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001199 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001200 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001201
Devang Patele1c53f22010-05-20 16:36:41 +00001202 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001203 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001204 RegVar->setFrameIndex(VP.first);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001205 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001206 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001207 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001208 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001209 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001210}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001211
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001212// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1213// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001214static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001215 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001216 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1217 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001218 (MI->getOperand(1).isImm() ||
1219 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001220}
1221
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001222// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001223static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1224 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001225 const MCSymbol *SLabel,
1226 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001227 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001228
David Blaikie0252265b2013-06-16 20:34:15 +00001229 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001230 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001231 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001232 // If the second operand is an immediate, this is a
1233 // register-indirect address.
1234 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001235 MLoc.set(MI->getOperand(0).getReg());
1236 else
1237 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001238 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1239 }
1240 if (MI->getOperand(0).isImm())
1241 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1242 if (MI->getOperand(0).isFPImm())
1243 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1244 if (MI->getOperand(0).isCImm())
1245 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1246
Craig Topperee4dab52012-02-05 08:31:47 +00001247 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001248}
1249
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001250// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001251void
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001252DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001253
Eric Christopher270a12c2013-07-03 21:37:03 +00001254 // Grab the variable info that was squirreled away in the MMI side-table.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001255 collectVariableInfoFromMMITable(Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001256
Benjamin Kramer15596c72014-03-07 19:09:39 +00001257 for (const MDNode *Var : UserVariables) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001258 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001259 continue;
1260
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001261 // History contains relevant DBG_VALUE instructions for Var and instructions
1262 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001263 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001264 if (History.empty())
1265 continue;
1266 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001267
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001268 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001269 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001270 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001271 DISubprogram(DV.getContext()).describes(CurFn->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001272 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001273 else if (MDNode *IA = DV.getInlinedAt())
1274 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1275 else
1276 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001277 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001278 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001279 continue;
1280
1281 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001282 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001283 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001284 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001285 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001286 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001287 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001288 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001289
Eric Christophercc10d202012-10-08 20:48:54 +00001290 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001291 if (History.size() <= 1 ||
1292 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001293 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001294 continue;
1295 }
1296
Eric Christopher59cc0712013-01-28 17:33:26 +00001297 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001298 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001299
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001300 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1301 HI = History.begin(),
1302 HE = History.end();
1303 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001304 const MachineInstr *Begin = *HI;
1305 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001306
Devang Patele7181b52011-06-01 23:00:17 +00001307 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001308 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1309 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001310 continue;
1311
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001312 // Compute the range for a register location.
1313 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1314 const MCSymbol *SLabel = 0;
1315
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001316 if (HI + 1 == HE)
1317 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001318 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001319 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001320 else {
1321 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001322 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001323 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001324 if (End->isDebugValue())
1325 SLabel = getLabelBeforeInsn(End);
1326 else {
1327 // End is a normal instruction clobbering the range.
1328 SLabel = getLabelAfterInsn(End);
1329 assert(SLabel && "Forgot label after clobber instruction");
1330 ++HI;
1331 }
1332 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001333
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001334 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001335 DotDebugLocEntries.push_back(
1336 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001337 }
1338 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001339 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001340
1341 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001342 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1343 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1344 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1345 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001346 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001347 continue;
1348 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001349 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001350 }
Devang Patel9fc11702010-05-25 23:40:22 +00001351}
Devang Patele0a94bf2010-05-14 21:01:35 +00001352
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001353// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001354MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001355 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1356 assert(Label && "Didn't insert label before instruction");
1357 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001358}
1359
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001360// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001361MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001362 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001363}
1364
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001365// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001366void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001367 assert(CurMI == 0);
1368 CurMI = MI;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001369 // Check if source location changes, but ignore DBG_VALUE locations.
1370 if (!MI->isDebugValue()) {
1371 DebugLoc DL = MI->getDebugLoc();
1372 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001373 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001374 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001375 if (DL == PrologEndLoc) {
1376 Flags |= DWARF2_FLAG_PROLOGUE_END;
1377 PrologEndLoc = DebugLoc();
1378 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001379 if (PrologEndLoc.isUnknown())
1380 Flags |= DWARF2_FLAG_IS_STMT;
1381
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001382 if (!DL.isUnknown()) {
1383 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001384 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001385 } else
Devang Patel34a66202011-05-11 19:22:19 +00001386 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001387 }
Devang Patel9fc11702010-05-25 23:40:22 +00001388 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001389
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001390 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001391 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1392 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001393
1394 // No label needed.
1395 if (I == LabelsBeforeInsn.end())
1396 return;
1397
1398 // Label already assigned.
1399 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001400 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001401
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001402 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001403 PrevLabel = MMI->getContext().CreateTempSymbol();
1404 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001405 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001406 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001407}
1408
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001409// Process end of an instruction.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001410void DwarfDebug::endInstruction() {
1411 assert(CurMI != 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001412 // Don't create a new label after DBG_VALUE instructions.
1413 // They don't generate code.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001414 if (!CurMI->isDebugValue())
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001415 PrevLabel = 0;
1416
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001417 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001418 LabelsAfterInsn.find(CurMI);
1419 CurMI = 0;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001420
1421 // No label needed.
1422 if (I == LabelsAfterInsn.end())
1423 return;
1424
1425 // Label already assigned.
1426 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001427 return;
1428
1429 // We need a label after this instruction.
1430 if (!PrevLabel) {
1431 PrevLabel = MMI->getContext().CreateTempSymbol();
1432 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001433 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001434 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001435}
1436
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001437// Each LexicalScope has first instruction and last instruction to mark
1438// beginning and end of a scope respectively. Create an inverse map that list
1439// scopes starts (and ends) with an instruction. One instruction may start (or
1440// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001441void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001442 SmallVector<LexicalScope *, 4> WorkList;
1443 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001444 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001445 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001446
Craig Topper977e9cd2013-07-03 04:24:43 +00001447 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001448 if (!Children.empty())
Benjamin Kramer15596c72014-03-07 19:09:39 +00001449 WorkList.append(Children.begin(), Children.end());
Devang Patel7771b7c2010-01-20 02:05:23 +00001450
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001451 if (S->isAbstractScope())
1452 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001453
Benjamin Kramer15596c72014-03-07 19:09:39 +00001454 for (const InsnRange &R : S->getRanges()) {
1455 assert(R.first && "InsnRange does not have first instruction!");
1456 assert(R.second && "InsnRange does not have second instruction!");
1457 requestLabelBeforeInsn(R.first);
1458 requestLabelAfterInsn(R.second);
Devang Patel6c74a872010-04-27 19:46:33 +00001459 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001460 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001461}
1462
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001463// Gather pre-function debug information. Assumes being called immediately
1464// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001465void DwarfDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001466 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001467
1468 // If there's no debug info for the function we're not going to do anything.
1469 if (!MMI->hasDebugInfo())
1470 return;
1471
1472 // Grab the lexical scopes for the function, if we don't have any of those
1473 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001474 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001475 if (LScopes.empty())
1476 return;
1477
1478 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1479
1480 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001481 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001482
Eric Christopher4287a492013-12-09 23:57:44 +00001483 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001484 // belongs to so that we add to the correct per-cu line table in the
1485 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001486 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001487 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren4e042a62013-02-05 21:52:47 +00001488 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001489 if (Asm->OutStreamer.hasRawTextSupport())
1490 // Use a single line table if we are generating assembly.
Manman Ren9d4c7352013-05-21 00:57:22 +00001491 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1492 else
1493 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001494
Eric Christopher2037caf2014-01-28 00:49:26 +00001495 // Check the current section against the standard text section. If different
1496 // keep track so that we will know when we're emitting functions into multiple
1497 // sections.
1498 if (Asm->getObjFileLowering().getTextSection() != Asm->getCurrentSection())
1499 UsedNonDefaultText = true;
1500
Eric Christopherfedfa442013-11-01 23:14:17 +00001501 // Emit a label for the function so that we have a beginning address.
1502 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001503 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001504 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001505
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001506 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001507 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001508 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001509
Eric Christopherfedfa442013-11-01 23:14:17 +00001510 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1511 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001512 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001513 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1514 II != IE; ++II) {
1515 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001516
Devang Patel002d54d2010-05-26 19:37:24 +00001517 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001518 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001519
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001520 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001521 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001522 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001523
1524 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001525 if (isDbgValueInDefinedReg(MI))
1526 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1527
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001528 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001529 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001530 if (History.empty()) {
1531 UserVariables.push_back(Var);
1532 // The first mention of a function argument gets the FunctionBeginSym
1533 // label, so arguments are visible when breaking at function entry.
1534 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001535 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001536 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001537 LabelsBeforeInsn[MI] = FunctionBeginSym;
1538 } else {
1539 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1540 const MachineInstr *Prev = History.back();
1541 if (Prev->isDebugValue()) {
1542 // Coalesce identical entries at the end of History.
1543 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001544 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001545 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001546 << "\t" << *Prev << "\t"
1547 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001548 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001549 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001550
1551 // Terminate old register assignments that don't reach MI;
1552 MachineFunction::const_iterator PrevMBB = Prev->getParent();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001553 if (PrevMBB != I && (!AtBlockEntry || std::next(PrevMBB) != I) &&
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001554 isDbgValueInDefinedReg(Prev)) {
1555 // Previous register assignment needs to terminate at the end of
1556 // its basic block.
1557 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001558 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001559 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001560 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001561 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001562 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001563 History.pop_back();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001564 } else if (std::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001565 // Terminate after LastMI.
1566 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001567 }
1568 }
1569 }
1570 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001571 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001572 // Not a DBG_VALUE instruction.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001573 if (!MI->isPosition())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001574 AtBlockEntry = false;
1575
Eric Christopher133195782012-10-04 20:46:14 +00001576 // First known non-DBG_VALUE and non-frame setup location marks
1577 // the beginning of the function body.
1578 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1579 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001580 PrologEndLoc = MI->getDebugLoc();
1581
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001582 // Check if the instruction clobbers any registers with debug vars.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001583 for (const MachineOperand &MO : MI->operands()) {
1584 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001585 continue;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001586 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
Eric Christopherfedfa442013-11-01 23:14:17 +00001587 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001588 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001589 const MDNode *Var = LiveUserVar[Reg];
1590 if (!Var)
1591 continue;
1592 // Reg is now clobbered.
1593 LiveUserVar[Reg] = 0;
1594
1595 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001596 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1597 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001598 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001599 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001600 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001601 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001602 const MachineInstr *Prev = History.back();
1603 // Sanity-check: Register assignments are terminated at the end of
1604 // their block.
1605 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1606 continue;
1607 // Is the variable still in Reg?
1608 if (!isDbgValueInDefinedReg(Prev) ||
1609 Prev->getOperand(0).getReg() != Reg)
1610 continue;
1611 // Var is clobbered. Make sure the next instruction gets a label.
1612 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001613 }
1614 }
Devang Patel002d54d2010-05-26 19:37:24 +00001615 }
Devang Patel002d54d2010-05-26 19:37:24 +00001616 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001617 }
1618
Benjamin Kramer15596c72014-03-07 19:09:39 +00001619 for (auto &I : DbgValues) {
1620 SmallVectorImpl<const MachineInstr *> &History = I.second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001621 if (History.empty())
1622 continue;
1623
1624 // Make sure the final register assignments are terminated.
1625 const MachineInstr *Prev = History.back();
1626 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1627 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001628 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001629 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001630 if (LastMI == PrevMBB->end())
1631 // Drop DBG_VALUE for empty range.
1632 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001633 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001634 // Terminate after LastMI.
1635 History.push_back(LastMI);
1636 }
1637 }
1638 // Request labels for the full history.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001639 for (const MachineInstr *MI : History) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001640 if (MI->isDebugValue())
1641 requestLabelBeforeInsn(MI);
1642 else
1643 requestLabelAfterInsn(MI);
1644 }
1645 }
Devang Patel002d54d2010-05-26 19:37:24 +00001646
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001647 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001648 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001649
1650 // Record beginning of function.
1651 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001652 DebugLoc FnStartDL =
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00001653 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
Eric Christopherfedfa442013-11-01 23:14:17 +00001654 recordSourceLine(
1655 FnStartDL.getLine(), FnStartDL.getCol(),
1656 FnStartDL.getScope(MF->getFunction()->getContext()),
1657 // We'd like to list the prologue as "not statements" but GDB behaves
1658 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1659 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001660 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001661}
1662
Devang Patel7e623022011-08-10 20:55:27 +00001663void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001664 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1665 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001666 // Variables with positive arg numbers are parameters.
1667 if (unsigned ArgNum = DV.getArgNumber()) {
1668 // Keep all parameters in order at the start of the variable list to ensure
1669 // function types are correct (no out-of-order parameters)
1670 //
1671 // This could be improved by only doing it for optimized builds (unoptimized
1672 // builds have the right order to begin with), searching from the back (this
1673 // would catch the unoptimized case quickly), or doing a binary search
1674 // rather than linear search.
1675 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1676 while (I != Vars.end()) {
1677 unsigned CurNum = (*I)->getVariable().getArgNumber();
1678 // A local (non-parameter) variable has been found, insert immediately
1679 // before it.
1680 if (CurNum == 0)
1681 break;
1682 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001683 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001684 break;
David Blaikieb272a752013-06-06 22:28:26 +00001685 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001686 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001687 Vars.insert(I, Var);
1688 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001689 }
1690
1691 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001692}
1693
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001694// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001695void DwarfDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001696 // Every beginFunction(MF) call should be followed by an endFunction(MF) call,
1697 // though the beginFunction may not be called at all.
1698 // We should handle both cases.
1699 if (CurFn == 0)
1700 CurFn = MF;
1701 else
1702 assert(CurFn == MF);
1703 assert(CurFn != 0);
1704
1705 if (!MMI->hasDebugInfo() || LScopes.empty()) {
1706 CurFn = 0;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001707 return;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001708 }
Devang Patel2904aa92009-11-12 19:02:56 +00001709
Devang Patel7e623022011-08-10 20:55:27 +00001710 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001711 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001712 // Assumes in correct section after the entry point.
1713 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher1a972152014-01-29 23:05:43 +00001714
Eric Christopher4287a492013-12-09 23:57:44 +00001715 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Manman Ren4e042a62013-02-05 21:52:47 +00001716 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001717
Devang Patel7e623022011-08-10 20:55:27 +00001718 SmallPtrSet<const MDNode *, 16> ProcessedVars;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001719 collectVariableInfo(ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001720
Devang Patel3acc70e2011-08-15 22:04:40 +00001721 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001722 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001723 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001724
Devang Patel7e623022011-08-10 20:55:27 +00001725 // Construct abstract scopes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001726 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Devang Patel44403472011-08-12 18:10:19 +00001727 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001728 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001729 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001730 DIArray Variables = SP.getVariables();
1731 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1732 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001733 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001734 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001735 // Check that DbgVariable for DV wasn't created earlier, when
1736 // findAbstractVariable() was called for inlined instance of DV.
1737 LLVMContext &Ctx = DV->getContext();
1738 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1739 if (AbstractVariables.lookup(CleanDV))
1740 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001741 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001742 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001743 }
1744 }
Devang Patel44403472011-08-12 18:10:19 +00001745 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001746 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001747 }
Eric Christopher6a841382012-11-19 22:42:10 +00001748
Devang Patel3acc70e2011-08-15 22:04:40 +00001749 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001750 if (!CurFn->getTarget().Options.DisableFramePointerElim(*CurFn))
Eric Christopherbb69a272012-08-24 01:14:27 +00001751 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001752
Eric Christopher1a972152014-01-29 23:05:43 +00001753 // Add the range of this function to the list of ranges for the CU.
1754 RangeSpan Span(FunctionBeginSym, FunctionEndSym);
Chandler Carruth002da5d2014-03-02 04:08:41 +00001755 TheCU->addRange(std::move(Span));
Eric Christopher1a972152014-01-29 23:05:43 +00001756
Bill Wendling2b128d72009-05-20 23:19:06 +00001757 // Clear debug info
Benjamin Kramer15596c72014-03-07 19:09:39 +00001758 for (auto &I : ScopeVariables)
1759 DeleteContainerPointers(I.second);
Devang Patel7e623022011-08-10 20:55:27 +00001760 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001761 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001762 UserVariables.clear();
1763 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001764 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001765 LabelsBeforeInsn.clear();
1766 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001767 PrevLabel = NULL;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001768 CurFn = 0;
Bill Wendling2b128d72009-05-20 23:19:06 +00001769}
1770
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001771// Register a source line with debug info. Returns the unique label that was
1772// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001773void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1774 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001775 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001776 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001777 unsigned Src = 1;
Diego Novillo282450d2014-03-03 18:53:17 +00001778 unsigned Discriminator = 0;
Dan Gohman50849c62010-05-05 23:41:32 +00001779 if (S) {
1780 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001781
Dan Gohman50849c62010-05-05 23:41:32 +00001782 if (Scope.isCompileUnit()) {
1783 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001784 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001785 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001786 } else if (Scope.isFile()) {
1787 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001788 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001789 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001790 } else if (Scope.isSubprogram()) {
1791 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001792 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001793 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001794 } else if (Scope.isLexicalBlockFile()) {
1795 DILexicalBlockFile DBF(S);
1796 Fn = DBF.getFilename();
1797 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001798 } else if (Scope.isLexicalBlock()) {
1799 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001800 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001801 Dir = DB.getDirectory();
Diego Novillo282450d2014-03-03 18:53:17 +00001802 Discriminator = DB.getDiscriminator();
Dan Gohman50849c62010-05-05 23:41:32 +00001803 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001804 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001805
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001806 Src = getOrCreateSourceID(
1807 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001808 }
Diego Novillo282450d2014-03-03 18:53:17 +00001809 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1810 Discriminator, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001811}
1812
Bill Wendling806535f2009-05-20 23:22:40 +00001813//===----------------------------------------------------------------------===//
1814// Emit Methods
1815//===----------------------------------------------------------------------===//
1816
Manman Rence20d462013-10-29 22:57:10 +00001817// Compute the size and offset of a DIE. The offset is relative to start of the
1818// CU. It returns the offset after laying out the DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001819unsigned DwarfFile::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001820 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001821 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001822
1823 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001824 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001825
1826 // Set DIE offset
1827 Die->setOffset(Offset);
1828
1829 // Start the size with the size of abbreviation code.
Logan Chien5b776b72014-02-22 14:00:39 +00001830 Offset += getULEB128Size(Die->getAbbrevNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001831
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001832 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001833 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001834
1835 // Size the DIE attribute values.
1836 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1837 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001838 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001839
Benjamin Kramer15596c72014-03-07 19:09:39 +00001840 // Get the children.
1841 const std::vector<DIE *> &Children = Die->getChildren();
1842
Bill Wendling480ff322009-05-20 23:21:38 +00001843 // Size the DIE children if any.
1844 if (!Children.empty()) {
Eric Christophere8f10722014-03-05 01:44:58 +00001845 assert(Abbrev.hasChildren() && "Children flag not set");
Bill Wendling480ff322009-05-20 23:21:38 +00001846
Benjamin Kramer15596c72014-03-07 19:09:39 +00001847 for (DIE *Child : Children)
1848 Offset = computeSizeAndOffset(Child, Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001849
1850 // End of children marker.
1851 Offset += sizeof(int8_t);
1852 }
1853
1854 Die->setSize(Offset - Die->getOffset());
1855 return Offset;
1856}
1857
Eric Christopherb088d2d2013-10-24 21:05:08 +00001858// Compute the size and offset for each DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001859void DwarfFile::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001860 // Offset from the first CU in the debug info section is 0 initially.
1861 unsigned SecOffset = 0;
1862
Eric Christopherb088d2d2013-10-24 21:05:08 +00001863 // Iterate over each compile unit and set the size and offsets for each
1864 // DIE within each compile unit. All offsets are CU relative.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001865 for (DwarfUnit *TheU : CUs) {
1866 TheU->setDebugInfoOffset(SecOffset);
Manman Rence20d462013-10-29 22:57:10 +00001867
1868 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001869 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
Benjamin Kramer15596c72014-03-07 19:09:39 +00001870 TheU->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001871
1872 // EndOffset here is CU-relative, after laying out
1873 // all of the CU DIE.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001874 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001875 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001876 }
Bill Wendling480ff322009-05-20 23:21:38 +00001877}
1878
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001879// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001880void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001881 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001882
Bill Wendling480ff322009-05-20 23:21:38 +00001883 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001884 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001885 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Eric Christopherd8667202013-12-30 17:22:27 +00001886 if (useSplitDwarf())
1887 DwarfInfoDWOSectionSym =
1888 emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001889 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001890 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001891 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001892 DwarfAbbrevDWOSectionSym = emitSectionSym(
1893 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
David Blaikie20474102014-02-25 22:46:44 +00001894 if (GenerateARangeSection)
1895 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001896
Eric Christopher74804332013-02-07 21:19:50 +00001897 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001898 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001899 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001900 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001901 DwarfGnuPubNamesSectionSym =
1902 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1903 DwarfGnuPubTypesSectionSym =
1904 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001905 } else if (HasDwarfPubSections) {
1906 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1907 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001908 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001909
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001910 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001911 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001912 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001913 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001914 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001915 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001916 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00001917 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001918 DwarfDebugRangeSectionSym =
1919 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001920
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001921 DwarfDebugLocSectionSym =
1922 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Bill Wendling480ff322009-05-20 23:21:38 +00001923}
1924
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001925// Recursively emits a debug information entry.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001926void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001927 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001928 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001929
Bill Wendling480ff322009-05-20 23:21:38 +00001930 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001931 if (Asm->isVerbose())
David Blaikieff3ab2c2013-12-05 01:01:41 +00001932 Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
1933 "] 0x" + Twine::utohexstr(Die->getOffset()) +
1934 ":0x" + Twine::utohexstr(Die->getSize()) + " " +
1935 dwarf::TagString(Abbrev.getTag()));
1936 Asm->EmitULEB128(Abbrev.getNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001937
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001938 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001939 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001940
1941 // Emit the DIE attribute values.
1942 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00001943 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
1944 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00001945 assert(Form && "Too many attributes for DIE (check abbreviation)");
1946
Eric Christopher13a1bb32014-03-06 00:00:49 +00001947 if (Asm->isVerbose()) {
Chris Lattner5adf9872010-01-24 18:54:17 +00001948 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Eric Christopher13a1bb32014-03-06 00:00:49 +00001949 if (Attr == dwarf::DW_AT_accessibility)
1950 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(
1951 cast<DIEInteger>(Values[i])->getValue()));
1952 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001953
Eric Christopherdd508382014-03-06 00:00:56 +00001954 // Emit an attribute using the defined form.
1955 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001956 }
1957
1958 // Emit the DIE children if any.
Eric Christophere8f10722014-03-05 01:44:58 +00001959 if (Abbrev.hasChildren()) {
Bill Wendling480ff322009-05-20 23:21:38 +00001960 const std::vector<DIE *> &Children = Die->getChildren();
1961
Benjamin Kramer15596c72014-03-07 19:09:39 +00001962 for (DIE *Child : Children)
1963 emitDIE(Child);
Bill Wendling480ff322009-05-20 23:21:38 +00001964
David Blaikie155f8812013-12-04 21:51:05 +00001965 Asm->OutStreamer.AddComment("End Of Children Mark");
Chris Lattner566cae92010-03-09 23:52:58 +00001966 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001967 }
1968}
1969
Eric Christophera2de8262012-12-15 00:04:07 +00001970// Emit the various dwarf units to the unit section USection with
1971// the abbreviations going into ASection.
David Blaikie03073f72013-12-06 22:14:48 +00001972void DwarfFile::emitUnits(DwarfDebug *DD, const MCSection *ASection,
1973 const MCSymbol *ASectionSym) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00001974 for (DwarfUnit *TheU : CUs) {
David Blaikie2a80e442013-12-02 22:09:48 +00001975 DIE *Die = TheU->getUnitDie();
David Blaikie03073f72013-12-06 22:14:48 +00001976 const MCSection *USection = TheU->getSection();
1977 Asm->OutStreamer.SwitchSection(USection);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001978
Devang Patel1a0df9a2010-05-10 22:49:55 +00001979 // Emit the compile units header.
David Blaikie7d734602013-12-06 22:33:05 +00001980 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001981
Devang Patel1a0df9a2010-05-10 22:49:55 +00001982 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00001983 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00001984 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001985
David Blaikie2a80e442013-12-02 22:09:48 +00001986 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001987
David Blaikieff3ab2c2013-12-05 01:01:41 +00001988 DD->emitDIE(Die);
David Blaikie7d734602013-12-06 22:33:05 +00001989 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001990 }
Bill Wendling480ff322009-05-20 23:21:38 +00001991}
1992
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001993// Emit the debug info section.
1994void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00001995 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christophera2de8262012-12-15 00:04:07 +00001996
David Blaikie03073f72013-12-06 22:14:48 +00001997 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfAbbrevSection(),
Eric Christophera2de8262012-12-15 00:04:07 +00001998 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001999}
2000
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002001// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002002void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00002003 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00002004
2005 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00002006}
Bill Wendling480ff322009-05-20 23:21:38 +00002007
Eric Christopherf8194852013-12-05 18:06:10 +00002008void DwarfFile::emitAbbrevs(const MCSection *Section) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002009 // Check to see if it is worth the effort.
David Blaikie0504cda2013-12-05 07:43:55 +00002010 if (!Abbreviations.empty()) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002011 // Start the debug abbrev section.
2012 Asm->OutStreamer.SwitchSection(Section);
2013
Bill Wendling480ff322009-05-20 23:21:38 +00002014 // For each abbrevation.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002015 for (const DIEAbbrev *Abbrev : Abbreviations) {
Bill Wendling480ff322009-05-20 23:21:38 +00002016 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002017 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002018
2019 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002020 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002021 }
2022
2023 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002024 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002025 }
2026}
2027
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002028// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002029void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002030 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002031 Asm->OutStreamer.AddComment("Extended Op");
2032 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002033
Chris Lattner566cae92010-03-09 23:52:58 +00002034 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002035 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002036 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2037 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2038
2039 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002040
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002041 Asm->OutStreamer.EmitSymbolValue(
2042 Asm->GetTempSymbol("section_end", SectionEnd),
2043 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002044
2045 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002046 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2047 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002048 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002049 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002050}
2051
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002052// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002053void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002054 DwarfAccelTable AT(
2055 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002056 for (DwarfUnit *TheU : getUnits()) {
2057 for (const auto &GI : TheU->getAccelNames()) {
2058 StringRef Name = GI.getKey();
2059 for (const DIE *D : GI.second)
2060 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002061 }
2062 }
2063
2064 AT.FinalizeTable(Asm, "Names");
2065 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002066 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002067 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2068 Asm->OutStreamer.EmitLabel(SectionBegin);
2069
2070 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002071 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002072}
2073
Eric Christopher48fef592012-12-20 21:58:40 +00002074// Emit objective C classes and categories into a hashed accelerator table
2075// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002076void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002077 DwarfAccelTable AT(
2078 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002079 for (DwarfUnit *TheU : getUnits()) {
2080 for (const auto &GI : TheU->getAccelObjC()) {
2081 StringRef Name = GI.getKey();
2082 for (const DIE *D : GI.second)
2083 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002084 }
2085 }
2086
2087 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002088 Asm->OutStreamer.SwitchSection(
2089 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002090 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2091 Asm->OutStreamer.EmitLabel(SectionBegin);
2092
2093 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002094 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002095}
2096
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002097// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002098void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002099 DwarfAccelTable AT(
2100 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002101 for (DwarfUnit *TheU : getUnits()) {
2102 for (const auto &GI : TheU->getAccelNamespace()) {
2103 StringRef Name = GI.getKey();
2104 for (const DIE *D : GI.second)
2105 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002106 }
2107 }
2108
2109 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002110 Asm->OutStreamer.SwitchSection(
2111 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002112 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2113 Asm->OutStreamer.EmitLabel(SectionBegin);
2114
2115 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002116 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002117}
2118
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002119// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002120void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002121 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002122 Atoms.push_back(
2123 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2124 Atoms.push_back(
2125 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2126 Atoms.push_back(
2127 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002128 DwarfAccelTable AT(Atoms);
Benjamin Kramer15596c72014-03-07 19:09:39 +00002129 for (DwarfUnit *TheU : getUnits()) {
2130 for (const auto &GI : TheU->getAccelTypes()) {
2131 StringRef Name = GI.getKey();
2132 for (const auto &DI : GI.second)
2133 AT.AddName(Name, DI.first, DI.second);
Eric Christopher4996c702011-11-07 09:24:32 +00002134 }
2135 }
2136
2137 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002138 Asm->OutStreamer.SwitchSection(
2139 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002140 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2141 Asm->OutStreamer.EmitLabel(SectionBegin);
2142
2143 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002144 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002145}
2146
Eric Christopherdd1a0122013-09-13 00:35:05 +00002147// Public name handling.
2148// The format for the various pubnames:
2149//
2150// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2151// for the DIE that is named.
2152//
2153// gnu pubnames - offset/index value/name tuples where the offset is the offset
2154// into the CU and the index value is computed according to the type of value
2155// for the DIE that is named.
2156//
2157// For type units the offset is the offset of the skeleton DIE. For split dwarf
2158// it's the offset within the debug_info/debug_types dwo section, however, the
2159// reference in the pubname header doesn't change.
2160
2161/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00002162static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002163 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002164 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2165
2166 // We could have a specification DIE that has our most of our knowledge,
2167 // look for that now.
2168 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2169 if (SpecVal) {
2170 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2171 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2172 Linkage = dwarf::GIEL_EXTERNAL;
2173 } else if (Die->findAttribute(dwarf::DW_AT_external))
2174 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002175
2176 switch (Die->getTag()) {
2177 case dwarf::DW_TAG_class_type:
2178 case dwarf::DW_TAG_structure_type:
2179 case dwarf::DW_TAG_union_type:
2180 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002181 return dwarf::PubIndexEntryDescriptor(
2182 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2183 ? dwarf::GIEL_STATIC
2184 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002185 case dwarf::DW_TAG_typedef:
2186 case dwarf::DW_TAG_base_type:
2187 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002188 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002189 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002190 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002191 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002192 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002193 case dwarf::DW_TAG_constant:
2194 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002195 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002196 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002197 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2198 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002199 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002200 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002201 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002202}
2203
Eric Christopher5f93bb92013-09-09 20:03:17 +00002204/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002205///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002206void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Eric Christopherdd1a0122013-09-13 00:35:05 +00002207 const MCSection *PSec =
2208 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2209 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002210
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002211 for (const auto &NU : CUMap) {
2212 DwarfCompileUnit *TheU = NU.second;
2213 if (auto Skeleton = static_cast<DwarfCompileUnit *>(TheU->getSkeleton()))
2214 TheU = Skeleton;
David Blaikie2a80e442013-12-02 22:09:48 +00002215 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002216
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002217 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002218 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002219
Eric Christopher39eebfa2013-09-30 23:14:16 +00002220 // Emit a label so we can reference the beginning of this pubname section.
2221 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002222 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubnames", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002223
Eric Christopherdd1a0122013-09-13 00:35:05 +00002224 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002225 Asm->OutStreamer.AddComment("Length of Public Names Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002226 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubnames_begin", ID);
2227 MCSymbol *EndLabel = Asm->GetTempSymbol("pubnames_end", ID);
2228 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002229
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002230 Asm->OutStreamer.EmitLabel(BeginLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002231
2232 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002233 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002234
2235 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002236 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002237
2238 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002239 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002240
Eric Christopherdd1a0122013-09-13 00:35:05 +00002241 // Emit the pubnames for this compilation unit.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002242 for (const auto &GI : getUnits()[ID]->getGlobalNames()) {
2243 const char *Name = GI.getKeyData();
2244 const DIE *Entity = GI.second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002245
2246 Asm->OutStreamer.AddComment("DIE offset");
2247 Asm->EmitInt32(Entity->getOffset());
2248
Eric Christopherdd1a0122013-09-13 00:35:05 +00002249 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002250 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002251 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002252 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002253 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002254 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002255 }
2256
David Blaikie155f8812013-12-04 21:51:05 +00002257 Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer15596c72014-03-07 19:09:39 +00002258 Asm->OutStreamer.EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002259 }
2260
2261 Asm->OutStreamer.AddComment("End Mark");
2262 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002263 Asm->OutStreamer.EmitLabel(EndLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002264 }
2265}
2266
Eric Christopherdd1a0122013-09-13 00:35:05 +00002267void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher261d2342013-09-23 20:55:35 +00002268 const MCSection *PSec =
2269 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2270 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002271
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002272 for (const auto &NU : CUMap) {
2273 DwarfCompileUnit *TheU = NU.second;
2274 if (auto Skeleton = static_cast<DwarfCompileUnit *>(TheU->getSkeleton()))
2275 TheU = Skeleton;
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002276 unsigned ID = TheU->getUniqueID();
2277
Eric Christopher6abc9c52011-11-07 09:18:35 +00002278 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002279 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002280
2281 // Emit a label so we can reference the beginning of this pubtype section.
2282 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002283 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubtypes", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002284
2285 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002286 Asm->OutStreamer.AddComment("Length of Public Types Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002287 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubtypes_begin", ID);
2288 MCSymbol *EndLabel = Asm->GetTempSymbol("pubtypes_end", ID);
2289 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002290
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002291 Asm->OutStreamer.EmitLabel(BeginLabel);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002292
David Blaikie155f8812013-12-04 21:51:05 +00002293 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002294 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002295
Devang Patel1a0df9a2010-05-10 22:49:55 +00002296 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002297 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002298
Devang Patel1a0df9a2010-05-10 22:49:55 +00002299 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002300 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002301
Eric Christopher39eebfa2013-09-30 23:14:16 +00002302 // Emit the pubtypes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002303 for (const auto &GI : getUnits()[ID]->getGlobalTypes()) {
2304 const char *Name = GI.getKeyData();
2305 const DIE *Entity = GI.second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002306
David Blaikie155f8812013-12-04 21:51:05 +00002307 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002308 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002309
Eric Christopherdd1a0122013-09-13 00:35:05 +00002310 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002311 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002312 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002313 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002314 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002315 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002316 }
2317
David Blaikie155f8812013-12-04 21:51:05 +00002318 Asm->OutStreamer.AddComment("External Name");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002319
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002320 // Emit the name with a terminating null byte.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002321 Asm->OutStreamer.EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002322 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002323
Devang Patel1a0df9a2010-05-10 22:49:55 +00002324 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002325 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002326 Asm->OutStreamer.EmitLabel(EndLabel);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002327 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002328}
2329
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002330// Emit strings into a string section.
Eric Christopherf8194852013-12-05 18:06:10 +00002331void DwarfFile::emitStrings(const MCSection *StrSection,
Eric Christophera5a79422013-12-09 23:32:48 +00002332 const MCSection *OffsetSection = NULL,
2333 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002334
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002335 if (StringPool.empty())
2336 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002337
Chris Lattner3d72a672010-03-09 23:38:23 +00002338 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002339 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002340
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002341 // Get all of the string pool entries and put them in an array by their ID so
2342 // we can sort them.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002343 SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64 > Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002344
Benjamin Kramer15596c72014-03-07 19:09:39 +00002345 for (const auto &I : StringPool)
2346 Entries.push_back(std::make_pair(I.second.second, &I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002347
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002348 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002349
Benjamin Kramer15596c72014-03-07 19:09:39 +00002350 for (const auto &Entry : Entries) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002351 // Emit a label for reference from debug information entries.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002352 Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002353
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002354 // Emit the string itself with a terminating null byte.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002355 Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
2356 Entry.second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002357 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002358
2359 // If we've got an offset section go ahead and emit that now as well.
2360 if (OffsetSection) {
2361 Asm->OutStreamer.SwitchSection(OffsetSection);
2362 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002363 unsigned size = 4; // FIXME: DWARF64 is 8.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002364 for (const auto &Entry : Entries) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002365 Asm->OutStreamer.EmitIntValue(offset, size);
Benjamin Kramer15596c72014-03-07 19:09:39 +00002366 offset += Entry.second->getKeyLength() + 1;
Eric Christopher2cbd5762013-01-07 19:32:41 +00002367 }
2368 }
Bill Wendling480ff322009-05-20 23:21:38 +00002369}
2370
Eric Christopher65132a82013-11-19 09:11:26 +00002371// Emit addresses into the section given.
Eric Christopherf8194852013-12-05 18:06:10 +00002372void DwarfFile::emitAddresses(const MCSection *AddrSection) {
Eric Christopher962c9082013-01-15 23:56:56 +00002373
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002374 if (AddressPool.empty())
2375 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002376
2377 // Start the dwarf addr section.
2378 Asm->OutStreamer.SwitchSection(AddrSection);
2379
David Blaikiece1960f2013-07-08 17:51:28 +00002380 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002381 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002382
Benjamin Kramer15596c72014-03-07 19:09:39 +00002383 for (const auto &I : AddressPool)
2384 Entries[I.second.Number] =
2385 I.second.TLS
2386 ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I.first)
2387 : MCSymbolRefExpr::Create(I.first, Asm->OutContext);
Eric Christopher962c9082013-01-15 23:56:56 +00002388
Benjamin Kramer15596c72014-03-07 19:09:39 +00002389 for (const MCExpr *Entry : Entries)
2390 Asm->OutStreamer.EmitValue(Entry, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002391}
2392
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002393// Emit visible names into a debug str section.
2394void DwarfDebug::emitDebugStr() {
Eric Christopherf8194852013-12-05 18:06:10 +00002395 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002396 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2397}
2398
Eric Christopher9046f942013-07-02 21:36:07 +00002399// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002400void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002401 if (DotDebugLocEntries.empty())
2402 return;
2403
Eric Christopher4887c8f2013-03-29 23:34:06 +00002404 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002405 I = DotDebugLocEntries.begin(),
2406 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002407 I != E; ++I) {
2408 DotDebugLocEntry &Entry = *I;
2409 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002410 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002411 }
2412
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002413 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002414 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002415 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002416 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002417 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2418 unsigned index = 1;
Eric Christophereeb51952014-03-06 19:51:16 +00002419 for (SmallVectorImpl<DotDebugLocEntry>::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002420 I = DotDebugLocEntries.begin(),
2421 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002422 I != E; ++I, ++index) {
Eric Christophereeb51952014-03-06 19:51:16 +00002423 const DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002424 if (Entry.isMerged())
2425 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002426 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002427 Asm->OutStreamer.EmitIntValue(0, Size);
2428 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002429 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002430 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002431 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2432 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2433 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002434 Asm->OutStreamer.AddComment("Loc expr size");
2435 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2436 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2437 Asm->EmitLabelDifference(end, begin, 2);
2438 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002439 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002440 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002441 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2442 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002443 Asm->OutStreamer.AddComment("DW_OP_consts");
2444 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002445 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002446 } else {
2447 Asm->OutStreamer.AddComment("DW_OP_constu");
2448 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002449 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002450 }
Devang Pateled9fd452011-07-08 16:49:43 +00002451 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002452 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002453 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002454 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002455 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002456 else {
2457 // Complex address entry.
2458 unsigned N = DV.getNumAddrElements();
2459 unsigned i = 0;
2460 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002461 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002462 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002463 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002464 Asm->OutStreamer.AddComment("DW_OP_deref");
2465 Asm->EmitInt8(dwarf::DW_OP_deref);
2466 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2467 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2468 Asm->EmitSLEB128(DV.getAddrElement(1));
2469 } else {
2470 // If first address element is OpPlus then emit
2471 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002472 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2473 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002474 i = 2;
2475 }
2476 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002477 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002478 }
Eric Christopher6a841382012-11-19 22:42:10 +00002479
Devang Pateled9fd452011-07-08 16:49:43 +00002480 // Emit remaining complex address elements.
2481 for (; i < N; ++i) {
2482 uint64_t Element = DV.getAddrElement(i);
2483 if (Element == DIBuilder::OpPlus) {
2484 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2485 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002486 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002487 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002488 Asm->EmitInt8(dwarf::DW_OP_deref);
2489 } else
2490 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002491 }
Devang Patel3e021532011-04-28 02:22:40 +00002492 }
Devang Patel3e021532011-04-28 02:22:40 +00002493 }
Devang Pateled9fd452011-07-08 16:49:43 +00002494 // else ... ignore constant fp. There is not any good way to
2495 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002496 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002497 }
2498 }
Bill Wendling480ff322009-05-20 23:21:38 +00002499}
2500
Richard Mitton21101b32013-09-19 23:21:01 +00002501struct SymbolCUSorter {
2502 SymbolCUSorter(const MCStreamer &s) : Streamer(s) {}
2503 const MCStreamer &Streamer;
2504
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002505 bool operator()(const SymbolCU &A, const SymbolCU &B) {
Richard Mitton21101b32013-09-19 23:21:01 +00002506 unsigned IA = A.Sym ? Streamer.GetSymbolOrder(A.Sym) : 0;
2507 unsigned IB = B.Sym ? Streamer.GetSymbolOrder(B.Sym) : 0;
2508
2509 // Symbols with no order assigned should be placed at the end.
2510 // (e.g. section end labels)
2511 if (IA == 0)
2512 IA = (unsigned)(-1);
2513 if (IB == 0)
2514 IB = (unsigned)(-1);
2515 return IA < IB;
2516 }
2517};
2518
Eric Christophera5a79422013-12-09 23:32:48 +00002519static bool CUSort(const DwarfUnit *A, const DwarfUnit *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002520 return (A->getUniqueID() < B->getUniqueID());
Richard Mitton21101b32013-09-19 23:21:01 +00002521}
2522
2523struct ArangeSpan {
2524 const MCSymbol *Start, *End;
2525};
2526
2527// Emit a debug aranges section, containing a CU lookup for any
2528// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002529void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002530 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002531 Asm->OutStreamer.SwitchSection(
2532 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002533
Eric Christopher4287a492013-12-09 23:57:44 +00002534 typedef DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan> > SpansType;
Richard Mitton21101b32013-09-19 23:21:01 +00002535
2536 SpansType Spans;
2537
2538 // Build a list of sections used.
2539 std::vector<const MCSection *> Sections;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002540 for (const auto &it : SectionMap) {
2541 const MCSection *Section = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00002542 Sections.push_back(Section);
2543 }
2544
2545 // Sort the sections into order.
2546 // This is only done to ensure consistent output order across different runs.
2547 std::sort(Sections.begin(), Sections.end(), SectionSort);
2548
2549 // Build a set of address spans, sorted by CU.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002550 for (const MCSection *Section : Sections) {
Richard Mitton21101b32013-09-19 23:21:01 +00002551 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2552 if (List.size() < 2)
2553 continue;
2554
2555 // Sort the symbols by offset within the section.
2556 SymbolCUSorter sorter(Asm->OutStreamer);
2557 std::sort(List.begin(), List.end(), sorter);
2558
2559 // If we have no section (e.g. common), just write out
2560 // individual spans for each symbol.
2561 if (Section == NULL) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00002562 for (const SymbolCU &Cur : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00002563 ArangeSpan Span;
2564 Span.Start = Cur.Sym;
2565 Span.End = NULL;
2566 if (Cur.CU)
2567 Spans[Cur.CU].push_back(Span);
2568 }
2569 } else {
2570 // Build spans between each label.
2571 const MCSymbol *StartSym = List[0].Sym;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002572 for (size_t n = 1, e = List.size(); n < e; n++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002573 const SymbolCU &Prev = List[n - 1];
2574 const SymbolCU &Cur = List[n];
2575
2576 // Try and build the longest span we can within the same CU.
2577 if (Cur.CU != Prev.CU) {
2578 ArangeSpan Span;
2579 Span.Start = StartSym;
2580 Span.End = Cur.Sym;
2581 Spans[Prev.CU].push_back(Span);
2582 StartSym = Cur.Sym;
2583 }
2584 }
2585 }
2586 }
2587
Richard Mitton21101b32013-09-19 23:21:01 +00002588 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2589
2590 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00002591 std::vector<DwarfCompileUnit *> CUs;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002592 for (const auto &it : Spans) {
2593 DwarfCompileUnit *CU = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00002594 CUs.push_back(CU);
2595 }
2596
2597 // Sort the CU list (again, to ensure consistent output order).
2598 std::sort(CUs.begin(), CUs.end(), CUSort);
2599
2600 // Emit an arange table for each CU we used.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002601 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton21101b32013-09-19 23:21:01 +00002602 std::vector<ArangeSpan> &List = Spans[CU];
2603
2604 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002605 unsigned ContentSize =
2606 sizeof(int16_t) + // DWARF ARange version number
2607 sizeof(int32_t) + // Offset of CU in the .debug_info section
2608 sizeof(int8_t) + // Pointer Size (in bytes)
2609 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002610
2611 unsigned TupleSize = PtrSize * 2;
2612
2613 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002614 unsigned Padding =
2615 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00002616
2617 ContentSize += Padding;
2618 ContentSize += (List.size() + 1) * TupleSize;
2619
2620 // For each compile unit, write the list of spans it covers.
2621 Asm->OutStreamer.AddComment("Length of ARange Set");
2622 Asm->EmitInt32(ContentSize);
2623 Asm->OutStreamer.AddComment("DWARF Arange version number");
2624 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2625 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Eric Christopherd8667202013-12-30 17:22:27 +00002626 Asm->EmitSectionOffset(CU->getLocalLabelBegin(), CU->getLocalSectionSym());
Richard Mitton21101b32013-09-19 23:21:01 +00002627 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2628 Asm->EmitInt8(PtrSize);
2629 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2630 Asm->EmitInt8(0);
2631
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002632 Asm->OutStreamer.EmitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00002633
Benjamin Kramer15596c72014-03-07 19:09:39 +00002634 for (const ArangeSpan &Span : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00002635 Asm->EmitLabelReference(Span.Start, PtrSize);
2636
2637 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002638 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002639 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002640 } else {
2641 // For symbols without an end marker (e.g. common), we
2642 // write a single arange entry containing just that one symbol.
2643 uint64_t Size = SymSize[Span.Start];
2644 if (Size == 0)
2645 Size = 1;
2646
2647 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2648 }
Richard Mitton21101b32013-09-19 23:21:01 +00002649 }
2650
2651 Asm->OutStreamer.AddComment("ARange terminator");
2652 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2653 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2654 }
Bill Wendling480ff322009-05-20 23:21:38 +00002655}
2656
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002657// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002658void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002659 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002660 Asm->OutStreamer.SwitchSection(
2661 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002662
Eric Christopher0f63d062013-12-03 00:45:45 +00002663 // Size for our labels.
2664 unsigned char Size = Asm->getDataLayout().getPointerSize();
2665
2666 // Grab the specific ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002667 for (const auto &I : CUMap) {
2668 DwarfCompileUnit *TheCU = I.second;
Eric Christopher0f63d062013-12-03 00:45:45 +00002669
2670 // Emit a symbol so we can find the beginning of our ranges.
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002671 Asm->OutStreamer.EmitLabel(TheCU->getLabelRange());
Eric Christopher0f63d062013-12-03 00:45:45 +00002672
2673 // Iterate over the misc ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002674 for (const RangeSpanList &List : TheCU->getRangeLists()) {
Eric Christopherf8790642013-12-04 22:04:50 +00002675 // Emit our symbol so we can find the beginning of the range.
2676 Asm->OutStreamer.EmitLabel(List.getSym());
Eric Christopher0f63d062013-12-03 00:45:45 +00002677
Benjamin Kramer15596c72014-03-07 19:09:39 +00002678 for (const RangeSpan &Range : List.getRanges()) {
Eric Christopher0f63d062013-12-03 00:45:45 +00002679 const MCSymbol *Begin = Range.getStart();
2680 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002681 assert(Begin && "Range without a begin symbol?");
2682 assert(End && "Range without an end symbol?");
2683 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2684 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002685 }
2686
2687 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002688 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002689 Asm->OutStreamer.EmitIntValue(0, Size);
2690 }
Eric Christopher46e23432013-12-20 04:16:18 +00002691
2692 // Now emit a range for the CU itself.
Eric Christophera9a1d272014-02-27 07:44:45 +00002693 if (useCURanges() && TheCU->getRanges().size()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002694 Asm->OutStreamer.EmitLabel(
2695 Asm->GetTempSymbol("cu_ranges", TheCU->getUniqueID()));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002696 for (const RangeSpan &Range : TheCU->getRanges()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002697 const MCSymbol *Begin = Range.getStart();
2698 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002699 assert(Begin && "Range without a begin symbol?");
2700 assert(End && "Range without an end symbol?");
2701 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2702 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher46e23432013-12-20 04:16:18 +00002703 }
2704 // And terminate the list with two 0 values.
2705 Asm->OutStreamer.EmitIntValue(0, Size);
2706 Asm->OutStreamer.EmitIntValue(0, Size);
2707 }
Devang Patel12563b32010-04-16 23:33:45 +00002708 }
Bill Wendling480ff322009-05-20 23:21:38 +00002709}
2710
Eric Christopherd692c1d2012-12-11 19:42:09 +00002711// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002712
David Blaikie38fe6342014-01-09 04:28:46 +00002713void DwarfDebug::initSkeletonUnit(const DwarfUnit *U, DIE *Die,
2714 DwarfUnit *NewU) {
2715 NewU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
2716 U->getCUNode().getSplitDebugFilename());
2717
2718 // Relocate to the beginning of the addr_base section, else 0 for the
2719 // beginning of the one for this compile unit.
2720 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherceec7b02014-01-11 00:23:18 +00002721 NewU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base, DwarfAddrSectionSym);
David Blaikie38fe6342014-01-09 04:28:46 +00002722 else
2723 NewU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
2724
2725 if (!CompilationDir.empty())
2726 NewU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2727
2728 addGnuPubAttributes(NewU, Die);
2729
2730 SkeletonHolder.addUnit(NewU);
2731}
2732
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002733// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2734// 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 +00002735// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher5090d572013-12-10 00:40:03 +00002736// TODO: Implement DW_AT_ranges_base.
Eric Christopher4287a492013-12-09 23:57:44 +00002737DwarfCompileUnit *DwarfDebug::constructSkeletonCU(const DwarfCompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002738
2739 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +00002740 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
David Blaikief645f962014-01-09 03:23:41 +00002741 CU->getUniqueID(), Die, CU->getCUNode(), Asm, this, &SkeletonHolder);
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002742 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
2743 DwarfInfoSectionSym);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002744
David Blaikie2494fdb2014-02-14 22:41:51 +00002745 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002746
David Blaikie38fe6342014-01-09 04:28:46 +00002747 initSkeletonUnit(CU, Die, NewCU);
Eric Christopherc8a310e2012-12-10 23:34:43 +00002748
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002749 return NewCU;
2750}
2751
David Blaikie15ed5eb2014-01-10 01:38:41 +00002752// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_dwo_name,
2753// DW_AT_addr_base.
David Blaikie15632ae2014-02-12 00:31:30 +00002754DwarfTypeUnit *DwarfDebug::constructSkeletonTU(DwarfTypeUnit *TU) {
David Blaikie60e63862014-02-14 23:58:13 +00002755 DwarfCompileUnit &CU = static_cast<DwarfCompileUnit &>(
2756 *SkeletonHolder.getUnits()[TU->getCU().getUniqueID()]);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002757
2758 DIE *Die = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie60e63862014-02-14 23:58:13 +00002759 DwarfTypeUnit *NewTU =
2760 new DwarfTypeUnit(TU->getUniqueID(), Die, CU, Asm, this, &SkeletonHolder);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002761 NewTU->setTypeSignature(TU->getTypeSignature());
2762 NewTU->setType(NULL);
2763 NewTU->initSection(
2764 Asm->getObjFileLowering().getDwarfTypesSection(TU->getTypeSignature()));
David Blaikie60e63862014-02-14 23:58:13 +00002765 CU.applyStmtList(*Die);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002766
2767 initSkeletonUnit(TU, Die, NewTU);
2768 return NewTU;
2769}
2770
Eric Christopherd692c1d2012-12-11 19:42:09 +00002771// Emit the .debug_info.dwo section for separated dwarf. This contains the
2772// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002773void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002774 assert(useSplitDwarf() && "No split dwarf debug info?");
David Blaikie03073f72013-12-06 22:14:48 +00002775 InfoHolder.emitUnits(this,
Eric Christopher3c5a1912012-12-19 22:02:53 +00002776 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2777 DwarfAbbrevDWOSectionSym);
2778}
2779
2780// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2781// abbreviations for the .debug_info.dwo section.
2782void DwarfDebug::emitDebugAbbrevDWO() {
2783 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00002784 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002785}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002786
2787// Emit the .debug_str.dwo section for separated dwarf. This contains the
2788// string section and is identical in format to traditional .debug_str
2789// sections.
2790void DwarfDebug::emitDebugStrDWO() {
2791 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002792 const MCSection *OffSec =
2793 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002794 const MCSymbol *StrSym = DwarfStrSectionSym;
2795 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2796 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002797}
David Blaikie409dd9c2013-11-19 23:08:21 +00002798
David Blaikie15632ae2014-02-12 00:31:30 +00002799void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikief645f962014-01-09 03:23:41 +00002800 StringRef Identifier, DIE *RefDie,
2801 DICompositeType CTy) {
David Blaikie322d79b2014-01-31 19:52:26 +00002802 // Flag the type unit reference as a declaration so that if it contains
2803 // members (implicit special members, static data member definitions, member
2804 // declarations for definitions in this CU, etc) consumers don't get confused
2805 // and think this is a full definition.
David Blaikie15632ae2014-02-12 00:31:30 +00002806 CU.addFlag(RefDie, dwarf::DW_AT_declaration);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002807
David Blaikie47f615e2013-12-17 23:32:35 +00002808 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
Chandler Carruthb587ab62014-01-20 08:07:07 +00002809 if (TU) {
David Blaikie15632ae2014-02-12 00:31:30 +00002810 CU.addDIETypeSignature(RefDie, *TU);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002811 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00002812 }
2813
Chandler Carruthb587ab62014-01-20 08:07:07 +00002814 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikied696fac2014-02-12 00:32:05 +00002815 DwarfTypeUnit *NewTU = new DwarfTypeUnit(InfoHolder.getUnits().size(),
2816 UnitDie, CU, Asm, this, &InfoHolder);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002817 TU = NewTU;
2818 InfoHolder.addUnit(NewTU);
2819
2820 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie15632ae2014-02-12 00:31:30 +00002821 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00002822
2823 MD5 Hash;
2824 Hash.update(Identifier);
2825 // ... take the least significant 8 bytes and return those. Our MD5
2826 // implementation always returns its results in little endian, swap bytes
2827 // appropriately.
2828 MD5::MD5Result Result;
2829 Hash.final(Result);
2830 uint64_t Signature = *reinterpret_cast<support::ulittle64_t *>(Result + 8);
2831 NewTU->setTypeSignature(Signature);
2832 if (useSplitDwarf())
2833 NewTU->setSkeleton(constructSkeletonTU(NewTU));
David Blaikie60e63862014-02-14 23:58:13 +00002834 else
2835 CU.applyStmtList(*UnitDie);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002836
2837 NewTU->setType(NewTU->createTypeDIE(CTy));
2838
2839 NewTU->initSection(
2840 useSplitDwarf()
2841 ? Asm->getObjFileLowering().getDwarfTypesDWOSection(Signature)
2842 : Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2843
David Blaikie15632ae2014-02-12 00:31:30 +00002844 CU.addDIETypeSignature(RefDie, *NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00002845}
David Blaikie4bd13b72014-03-07 18:49:45 +00002846
2847void DwarfDebug::attachLowHighPC(DwarfCompileUnit *Unit, DIE *D,
2848 MCSymbol *Begin, MCSymbol *End) {
2849 Unit->addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
2850 if (DwarfVersion < 4 || Triple(Asm->getTargetTriple()).isOSDarwin())
2851 Unit->addLabelAddress(D, dwarf::DW_AT_high_pc, End);
2852 else
2853 Unit->addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
2854}