blob: fa966d01c9d95dd815b9fa1052b4b2874daa4c12 [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"
Eric Christopher29e874d2014-03-07 22:40:37 +000015#include "ByteStreamer.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000016#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000017#include "DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000018#include "DIEHash.h"
Eric Christopher4996c702011-11-07 09:24:32 +000019#include "DwarfAccelTable.h"
David Blaikie2c86a722013-12-02 19:33:15 +000020#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Triple.h"
David Greene829b3e82009-08-19 21:52:55 +000025#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000028#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000030#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/Instructions.h"
32#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000033#include "llvm/IR/ValueHandle.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000035#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000036#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000037#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000038#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000039#include "llvm/Support/Debug.h"
David Majnemered89b5c2013-08-21 06:13:34 +000040#include "llvm/Support/Dwarf.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000041#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000042#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000043#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000044#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000045#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Support/Timer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000047#include "llvm/Target/TargetFrameLowering.h"
48#include "llvm/Target/TargetLoweringObjectFile.h"
49#include "llvm/Target/TargetMachine.h"
50#include "llvm/Target/TargetOptions.h"
51#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000052using namespace llvm;
53
Eric Christopher7f2b5512013-07-23 22:16:41 +000054static cl::opt<bool>
55DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
56 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000057
Eric Christopher7f2b5512013-07-23 22:16:41 +000058static cl::opt<bool> UnknownLocations(
59 "use-unknown-locations", cl::Hidden,
60 cl::desc("Make an absence of debug location information explicit."),
61 cl::init(false));
Dan Gohman7421ae42010-05-07 01:08:53 +000062
Eric Christopherb4bef6d2013-11-19 09:04:36 +000063static cl::opt<bool> GenerateCUHash("generate-cu-hash", cl::Hidden,
64 cl::desc("Add the CU hash as the dwo_id."),
Eric Christopher7924e0c2014-03-12 17:14:43 +000065 cl::init(true));
Eric Christopherd29614f2013-08-13 01:21:55 +000066
Eric Christopherdd1a0122013-09-13 00:35:05 +000067static cl::opt<bool>
68GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
69 cl::desc("Generate GNU-style pubnames and pubtypes"),
70 cl::init(false));
71
Eric Christopher02dbadb2014-02-14 01:26:55 +000072static cl::opt<bool> GenerateARangeSection("generate-arange-section",
73 cl::Hidden,
74 cl::desc("Generate dwarf aranges"),
75 cl::init(false));
76
Eric Christopher20b76a72012-08-23 22:36:40 +000077namespace {
Eric Christopherf07ee3a2014-01-27 23:50:03 +000078enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher20b76a72012-08-23 22:36:40 +000079}
Eric Christopher4996c702011-11-07 09:24:32 +000080
Eric Christopher7f2b5512013-07-23 22:16:41 +000081static cl::opt<DefaultOnOff>
82DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
83 cl::desc("Output prototype dwarf accelerator tables."),
84 cl::values(clEnumVal(Default, "Default for platform"),
85 clEnumVal(Enable, "Enabled"),
86 clEnumVal(Disable, "Disabled"), clEnumValEnd),
87 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +000088
Eric Christopher7f2b5512013-07-23 22:16:41 +000089static cl::opt<DefaultOnOff>
Eric Christopher7f2b5512013-07-23 22:16:41 +000090SplitDwarf("split-dwarf", cl::Hidden,
Eric Christopher5d008fe2013-12-04 23:24:28 +000091 cl::desc("Output DWARF5 split debug info."),
Eric Christopher7f2b5512013-07-23 22:16:41 +000092 cl::values(clEnumVal(Default, "Default for platform"),
93 clEnumVal(Enable, "Enabled"),
94 clEnumVal(Disable, "Disabled"), clEnumValEnd),
95 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +000096
Eric Christopher7da24882013-08-19 21:07:38 +000097static cl::opt<DefaultOnOff>
Eric Christopher4d36ca02013-08-26 23:24:35 +000098DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
99 cl::desc("Generate DWARF pubnames and pubtypes sections"),
100 cl::values(clEnumVal(Default, "Default for platform"),
101 clEnumVal(Enable, "Enabled"),
102 clEnumVal(Disable, "Disabled"), clEnumValEnd),
103 cl::init(Default));
Eric Christopher7da24882013-08-19 21:07:38 +0000104
Eric Christopher33ff6972013-11-21 23:46:41 +0000105static cl::opt<unsigned>
106DwarfVersionNumber("dwarf-version", cl::Hidden,
Eric Christophera5a79422013-12-09 23:32:48 +0000107 cl::desc("Generate DWARF for dwarf version."), cl::init(0));
Eric Christopher33ff6972013-11-21 23:46:41 +0000108
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000109static const char *const DWARFGroupName = "DWARF Emission";
110static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000111
Bill Wendling2f921f82009-05-15 09:23:25 +0000112//===----------------------------------------------------------------------===//
113
Bill Wendling2f921f82009-05-15 09:23:25 +0000114namespace llvm {
115
Manman Renbe5576f2013-10-08 19:07:44 +0000116/// resolve - Look in the DwarfDebug map for the MDNode that
117/// corresponds to the reference.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000118template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
Manman Renbe5576f2013-10-08 19:07:44 +0000119 return DD->resolve(Ref);
120}
121
Nick Lewycky019d2552011-07-29 03:49:23 +0000122DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000123 DIType Ty = Var.getType();
124 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
125 // addresses instead.
126 if (Var.isBlockByrefVariable()) {
127 /* Byref variables, in Blocks, are declared by the programmer as
128 "SomeType VarName;", but the compiler creates a
129 __Block_byref_x_VarName struct, and gives the variable VarName
130 either the struct, or a pointer to the struct, as its type. This
131 is necessary for various behind-the-scenes things the compiler
132 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000133
Devang Patelf20c4f72011-04-12 22:53:02 +0000134 However, as far as the original *programmer* is concerned, the
135 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000136
Devang Patelf20c4f72011-04-12 22:53:02 +0000137 The following function dives into the __Block_byref_x_VarName
138 struct to find the original type of the variable. This will be
139 passed back to the code generating the type for the Debug
140 Information Entry for the variable 'VarName'. 'VarName' will then
141 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000142
Devang Patelf20c4f72011-04-12 22:53:02 +0000143 The original type 'SomeType' will be the type of the field named
144 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000145
Devang Patelf20c4f72011-04-12 22:53:02 +0000146 NOTE: In order for this to not completely fail on the debugger
147 side, the Debug Information Entry for the variable VarName needs to
148 have a DW_AT_location that tells the debugger how to unwind through
149 the pointers and __Block_byref_x_VarName struct to find the actual
150 value of the variable. The function addBlockByrefType does this. */
151 DIType subType = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000152 uint16_t tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000153
Eric Christopher9adc55f2013-09-04 19:53:21 +0000154 if (tag == dwarf::DW_TAG_pointer_type)
Manman Renbe5576f2013-10-08 19:07:44 +0000155 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher6a841382012-11-19 22:42:10 +0000156
Eric Christopher9adc55f2013-09-04 19:53:21 +0000157 DIArray Elements = DICompositeType(subType).getTypeArray();
Devang Patelf20c4f72011-04-12 22:53:02 +0000158 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie5af2aca2013-11-18 23:57:26 +0000159 DIDerivedType DT(Elements.getElement(i));
Devang Patelf20c4f72011-04-12 22:53:02 +0000160 if (getName() == DT.getName())
Manman Renbe5576f2013-10-08 19:07:44 +0000161 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000162 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000163 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000164 return Ty;
165}
Bill Wendling2f921f82009-05-15 09:23:25 +0000166
Chris Lattnerf5d06362010-04-05 04:09:20 +0000167} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000168
Eric Christopher942f22c2014-01-11 00:28:12 +0000169/// Return Dwarf Version by checking module flags.
170static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Ren8bfde892013-07-16 23:21:16 +0000171 Value *Val = M->getModuleFlag("Dwarf Version");
172 if (!Val)
Eric Christophere31e0722013-09-04 22:21:24 +0000173 return dwarf::DWARF_VERSION;
Manman Ren8bfde892013-07-16 23:21:16 +0000174 return cast<ConstantInt>(Val)->getZExtValue();
Manman Renac8062b2013-07-02 23:40:10 +0000175}
176
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000177DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
David Blaikie0504cda2013-12-05 07:43:55 +0000178 : Asm(A), MMI(Asm->MMI), FirstCU(0), SourceIdMap(DIEValueAllocator),
David Blaikie2666e242013-12-06 19:38:46 +0000179 PrevLabel(NULL), GlobalRangeCount(0),
Eric Christopher1bca60d2014-01-23 22:55:47 +0000180 InfoHolder(A, "info_string", DIEValueAllocator), HasCURanges(false),
Eric Christopher2037caf2014-01-28 00:49:26 +0000181 UsedNonDefaultText(false),
David Blaikie0504cda2013-12-05 07:43:55 +0000182 SkeletonHolder(A, "skel_string", DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000183
Eric Christopher50effa02014-01-03 02:16:44 +0000184 DwarfInfoSectionSym = DwarfAbbrevSectionSym = DwarfStrSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000185 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher55863be2013-04-07 03:43:09 +0000186 DwarfAddrSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000187 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000188 FunctionBeginSym = FunctionEndSym = 0;
Eric Christophera5a79422013-12-09 23:32:48 +0000189 CurFn = 0;
190 CurMI = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000191
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000192 // Turn on accelerator tables for Darwin by default, pubnames by
193 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000194 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000195
Eric Christopher574b5c82013-08-19 21:41:38 +0000196 if (DwarfAccelTables == Default)
197 HasDwarfAccelTables = IsDarwin;
198 else
Eric Christopher5297df02013-08-26 20:58:35 +0000199 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000200
Eric Christophercdf218d2012-12-10 19:51:21 +0000201 if (SplitDwarf == Default)
202 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000203 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000204 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000205
Eric Christopher4d36ca02013-08-26 23:24:35 +0000206 if (DwarfPubSections == Default)
207 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000208 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000209 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000210
Eric Christopher942f22c2014-01-11 00:28:12 +0000211 DwarfVersion = DwarfVersionNumber
212 ? DwarfVersionNumber
213 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000214
Dan Gohman6e681a52010-06-18 15:56:31 +0000215 {
216 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000217 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000218 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000219}
Bill Wendling2f921f82009-05-15 09:23:25 +0000220
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000221// Switch to the specified MCSection and emit an assembler
222// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000223static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000224 const char *SymbolStem = 0) {
225 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000226 if (!SymbolStem)
227 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000228
229 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
230 Asm->OutStreamer.EmitLabel(TmpSym);
231 return TmpSym;
232}
233
Eric Christopherf8194852013-12-05 18:06:10 +0000234DwarfFile::~DwarfFile() {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000235 for (DwarfUnit *DU : CUs)
236 delete DU;
David Blaikie72f1a3e2013-11-23 01:17:34 +0000237}
238
Eric Christopherf8194852013-12-05 18:06:10 +0000239MCSymbol *DwarfFile::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000240 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000241}
242
Eric Christopherf8194852013-12-05 18:06:10 +0000243MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000244 std::pair<MCSymbol *, unsigned> &Entry =
245 StringPool.GetOrCreateValue(Str).getValue();
246 if (Entry.first)
247 return Entry.first;
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000248
249 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000250 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000251}
252
Eric Christopherf8194852013-12-05 18:06:10 +0000253unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000254 std::pair<MCSymbol *, unsigned> &Entry =
255 StringPool.GetOrCreateValue(Str).getValue();
256 if (Entry.first)
257 return Entry.second;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000258
259 Entry.second = NextStringPoolNumber++;
260 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
261 return Entry.second;
262}
263
David Blaikief1a6dea2014-02-15 19:34:03 +0000264unsigned DwarfFile::getAddrPoolIndex(const MCSymbol *Sym, bool TLS) {
265 std::pair<AddrPool::iterator, bool> P = AddressPool.insert(
266 std::make_pair(Sym, AddressPoolEntry(NextAddrPoolNumber, TLS)));
David Blaikiea67de2b2013-06-28 18:55:13 +0000267 if (P.second)
268 ++NextAddrPoolNumber;
David Blaikief1a6dea2014-02-15 19:34:03 +0000269 return P.first->second.Number;
Eric Christopher962c9082013-01-15 23:56:56 +0000270}
271
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000272// Define a unique number for the abbreviation.
273//
Eric Christopherf8194852013-12-05 18:06:10 +0000274void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000275 // Check the set for priors.
David Blaikie0504cda2013-12-05 07:43:55 +0000276 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000277
278 // If it's newly added.
279 if (InSet == &Abbrev) {
280 // Add to abbreviation list.
David Blaikie2d4e1122013-10-30 17:14:24 +0000281 Abbreviations.push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000282
283 // Assign the vector position + 1 as its number.
David Blaikie2d4e1122013-10-30 17:14:24 +0000284 Abbrev.setNumber(Abbreviations.size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000285 } else {
286 // Assign existing abbreviation number.
287 Abbrev.setNumber(InSet->getNumber());
288 }
289}
290
Eric Christopherd9843b32011-11-10 19:25:34 +0000291static bool isObjCClass(StringRef Name) {
292 return Name.startswith("+") || Name.startswith("-");
293}
294
295static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000296 if (!isObjCClass(Name))
297 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000298
Benjamin Kramer260de742013-08-24 12:15:54 +0000299 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000300}
301
302static void getObjCClassCategory(StringRef In, StringRef &Class,
303 StringRef &Category) {
304 if (!hasObjCCategory(In)) {
305 Class = In.slice(In.find('[') + 1, In.find(' '));
306 Category = "";
307 return;
308 }
309
310 Class = In.slice(In.find('[') + 1, In.find('('));
311 Category = In.slice(In.find('[') + 1, In.find(' '));
312 return;
313}
314
315static StringRef getObjCMethodName(StringRef In) {
316 return In.slice(In.find(' ') + 1, In.find(']'));
317}
318
Richard Mittonc2508242013-10-03 22:07:08 +0000319// Helper for sorting sections into a stable output order.
320static bool SectionSort(const MCSection *A, const MCSection *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000321 std::string LA = (A ? A->getLabelBeginName() : "");
322 std::string LB = (B ? B->getLabelBeginName() : "");
323 return LA < LB;
Richard Mittonc2508242013-10-03 22:07:08 +0000324}
325
Eric Christopherd9843b32011-11-10 19:25:34 +0000326// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000327// TODO: Determine whether or not we should add names for programs
328// that do not have a DW_AT_name or DW_AT_linkage_name field - this
329// is only slightly different than the lookup of non-standard ObjC names.
Eric Christophera5a79422013-12-09 23:32:48 +0000330static void addSubprogramNames(DwarfUnit *TheU, DISubprogram SP, DIE *Die) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000331 if (!SP.isDefinition())
332 return;
David Blaikie2a80e442013-12-02 22:09:48 +0000333 TheU->addAccelName(SP.getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000334
335 // If the linkage name is different than the name, go ahead and output
336 // that as well into the name table.
337 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
David Blaikie2a80e442013-12-02 22:09:48 +0000338 TheU->addAccelName(SP.getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000339
340 // If this is an Objective-C selector name add it to the ObjC accelerator
341 // too.
342 if (isObjCClass(SP.getName())) {
343 StringRef Class, Category;
344 getObjCClassCategory(SP.getName(), Class, Category);
David Blaikie2a80e442013-12-02 22:09:48 +0000345 TheU->addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000346 if (Category != "")
David Blaikie2a80e442013-12-02 22:09:48 +0000347 TheU->addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000348 // Also add the base method name to the name table.
David Blaikie2a80e442013-12-02 22:09:48 +0000349 TheU->addAccelName(getObjCMethodName(SP.getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000350 }
351}
352
Manman Ren3eb9dff2013-09-09 19:05:21 +0000353/// isSubprogramContext - Return true if Context is either a subprogram
354/// or another context nested inside a subprogram.
355bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
356 if (!Context)
357 return false;
358 DIDescriptor D(Context);
359 if (D.isSubprogram())
360 return true;
361 if (D.isType())
Manman Ren116868e2013-09-09 19:47:11 +0000362 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000363 return false;
364}
365
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000366// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
367// and DW_AT_high_pc attributes. If there are global variables in this
368// scope then create and insert DIEs for these variables.
Eric Christopher4287a492013-12-09 23:57:44 +0000369DIE *DwarfDebug::updateSubprogramScopeDIE(DwarfCompileUnit *SPCU,
370 DISubprogram SP) {
David Blaikie25bc7192013-11-15 23:13:08 +0000371 DIE *SPDie = SPCU->getDIE(SP);
Devang Patela37a95e2010-07-07 22:20:57 +0000372
Chris Lattner3a383cb2010-04-05 00:13:49 +0000373 assert(SPDie && "Unable to find subprogram DIE!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000374
Bill Wendlingf720bf62012-11-07 05:19:04 +0000375 // If we're updating an abstract DIE, then we will be adding the children and
376 // object pointer later on. But what we don't want to do is process the
377 // concrete DIE twice.
David Blaikie25bc7192013-11-15 23:13:08 +0000378 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000379 // Pick up abstract subprogram DIE.
David Blaikie2a80e442013-12-02 22:09:48 +0000380 SPDie =
381 SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000382 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000383 } else {
384 DISubprogram SPDecl = SP.getFunctionDeclaration();
385 if (!SPDecl.isSubprogram()) {
386 // There is not any need to generate specification DIE for a function
387 // defined at compile unit level. If a function is defined inside another
388 // function then gdb prefers the definition at top level and but does not
389 // expect specification DIE in parent function. So avoid creating
390 // specification DIE for a function defined inside a function.
Manman Renc50fa112013-10-10 18:40:01 +0000391 DIScope SPContext = resolve(SP.getContext());
392 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000393 !SPContext.isFile() && !isSubprogramContext(SPContext)) {
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000394 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
395
396 // Add arguments.
397 DICompositeType SPTy = SP.getType();
398 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000399 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000400 if (SPTag == dwarf::DW_TAG_subroutine_type)
Adrian Prantl69140d22014-02-25 22:27:14 +0000401 SPCU->constructSubprogramArguments(*SPDie, Args);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000402 DIE *SPDeclDie = SPDie;
David Blaikie2a80e442013-12-02 22:09:48 +0000403 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
404 *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000405 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000406 }
407 }
Devang Patela37a95e2010-07-07 22:20:57 +0000408 }
409
David Blaikie4bd13b72014-03-07 18:49:45 +0000410 attachLowHighPC(SPCU, SPDie, FunctionBeginSym, FunctionEndSym);
Eric Christopher0f63d062013-12-03 00:45:45 +0000411
Chris Lattner3a383cb2010-04-05 00:13:49 +0000412 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
413 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000414 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000415
Eric Christopherd9843b32011-11-10 19:25:34 +0000416 // Add name to the name table, we do this here because we're guaranteed
417 // to have concrete versions of our DW_TAG_subprogram nodes.
418 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000419
Chris Lattner3a383cb2010-04-05 00:13:49 +0000420 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000421}
422
Manman Ren5b2f4b02013-09-11 19:40:28 +0000423/// Check whether we should create a DIE for the given Scope, return true
424/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000425bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
426 if (Scope->isAbstractScope())
427 return false;
428
Manman Ren5b2f4b02013-09-11 19:40:28 +0000429 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000430 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
431 if (Ranges.empty())
432 return true;
433
434 if (Ranges.size() > 1)
435 return false;
436
Manman Ren5b2f4b02013-09-11 19:40:28 +0000437 // We don't create a DIE if we have a single Range and the end label
438 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000439 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
440 MCSymbol *End = getLabelAfterInsn(RI->second);
441 return !End;
442}
443
Eric Christophera5a79422013-12-09 23:32:48 +0000444static void addSectionLabel(AsmPrinter *Asm, DwarfUnit *U, DIE *D,
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000445 dwarf::Attribute A, const MCSymbol *L,
446 const MCSymbol *Sec) {
447 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
448 U->addSectionLabel(D, A, L);
449 else
450 U->addSectionDelta(D, A, L, Sec);
451}
452
Eric Christopher4287a492013-12-09 23:57:44 +0000453void DwarfDebug::addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
Eric Christopherbe2513e2013-12-03 00:45:59 +0000454 const SmallVectorImpl<InsnRange> &Range) {
455 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
456 // emitting it appropriately.
Eric Christopherf8790642013-12-04 22:04:50 +0000457 MCSymbol *RangeSym = Asm->GetTempSymbol("debug_ranges", GlobalRangeCount++);
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000458 addSectionLabel(Asm, TheCU, ScopeDIE, dwarf::DW_AT_ranges, RangeSym,
459 DwarfDebugRangeSectionSym);
460
Eric Christopherf8790642013-12-04 22:04:50 +0000461 RangeSpanList List(RangeSym);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000462 for (const InsnRange &R : Range) {
463 RangeSpan Span(getLabelBeforeInsn(R.first), getLabelAfterInsn(R.second));
Chandler Carruth002da5d2014-03-02 04:08:41 +0000464 List.addRange(std::move(Span));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000465 }
466
467 // Add the range list to the set of ranges to be emitted.
Chandler Carruth002da5d2014-03-02 04:08:41 +0000468 TheCU->addRangeList(std::move(List));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000469}
470
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000471// Construct new DW_TAG_lexical_block for this scope and attach
472// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher4287a492013-12-09 23:57:44 +0000473DIE *DwarfDebug::constructLexicalScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000474 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000475 if (isLexicalScopeDIENull(Scope))
476 return 0;
477
Devang Patel6c74a872010-04-27 19:46:33 +0000478 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
479 if (Scope->isAbstractScope())
480 return ScopeDIE;
481
Eric Christopher0f63d062013-12-03 00:45:45 +0000482 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherbe2513e2013-12-03 00:45:59 +0000483
Eric Christopherdc42ea82013-07-03 01:57:28 +0000484 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000485 if (ScopeRanges.size() > 1) {
Eric Christopherbe2513e2013-12-03 00:45:59 +0000486 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
Devang Patel6c74a872010-04-27 19:46:33 +0000487 return ScopeDIE;
488 }
489
Eric Christopherdc42ea82013-07-03 01:57:28 +0000490 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000491 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000492 MCSymbol *Start = getLabelBeforeInsn(RI->first);
493 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000494 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000495
Chris Lattnere13c3722010-03-09 01:58:53 +0000496 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
497 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000498
David Blaikie26ab6c62014-03-08 00:58:20 +0000499 attachLowHighPC(TheCU, ScopeDIE, Start, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000500
501 return ScopeDIE;
502}
503
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000504// This scope represents inlined body of a function. Construct DIE to
505// represent this concrete inlined copy of the function.
Eric Christopher4287a492013-12-09 23:57:44 +0000506DIE *DwarfDebug::constructInlinedScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000507 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000508 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopher1cdb63d2013-12-04 21:20:15 +0000509 assert(!ScopeRanges.empty() &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000510 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000511
Devang Patelf098ce22011-07-27 00:34:13 +0000512 if (!Scope->getScopeNode())
513 return NULL;
514 DIScope DS(Scope->getScopeNode());
515 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000516 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000517 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000518 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000519 return NULL;
520 }
521
Devang Patel73bc1722011-05-05 17:54:26 +0000522 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000523 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000524
Eric Christopherbe2513e2013-12-03 00:45:59 +0000525 // If we have multiple ranges, emit them into the range section.
526 if (ScopeRanges.size() > 1)
527 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
528 else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000529 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000530 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
531 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
532
533 if (StartLabel == 0 || EndLabel == 0)
534 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
535
536 assert(StartLabel->isDefined() &&
537 "Invalid starting label for an inlined scope!");
538 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
539
David Blaikie555e79a2014-03-07 22:00:56 +0000540 attachLowHighPC(TheCU, ScopeDIE, StartLabel, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000541 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000542
543 InlinedSubprogramDIEs.insert(OriginDIE);
544
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000545 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000546 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000547 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000548 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
549 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000550 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000551
Eric Christopher8dda5d02011-12-04 06:02:38 +0000552 // Add name to the name table, we do this here because we're guaranteed
553 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
554 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000555
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000556 return ScopeDIE;
557}
558
Eric Christopher4287a492013-12-09 23:57:44 +0000559DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
560 LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000561 SmallVectorImpl<DIE *> &Children) {
562 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000563
564 // Collect arguments for current function.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000565 if (LScopes.isCurrentFunctionScope(Scope)) {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000566 for (DbgVariable *ArgDV : CurrentFnArguments)
567 if (ArgDV)
Eric Christopher6a841382012-11-19 22:42:10 +0000568 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000569 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000570 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000571 if (ArgDV->isObjectPointer())
572 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000573 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000574
Adrian Prantl69140d22014-02-25 22:27:14 +0000575 // If this is a variadic function, add an unspecified parameter.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000576 DISubprogram SP(Scope->getScopeNode());
Adrian Prantl3f49c892014-02-25 19:57:42 +0000577 DIArray FnArgs = SP.getType().getTypeArray();
Eric Christopher73ffdb82014-02-26 02:50:56 +0000578 if (FnArgs.getElement(FnArgs.getNumElements() - 1)
579 .isUnspecifiedParameter()) {
Adrian Prantl3f49c892014-02-25 19:57:42 +0000580 DIE *Ellipsis = new DIE(dwarf::DW_TAG_unspecified_parameters);
581 Children.push_back(Ellipsis);
582 }
583 }
584
Eric Christopherf84354b2011-10-03 15:49:16 +0000585 // Collect lexical scope children first.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000586 for (DbgVariable *DV : ScopeVariables.lookup(Scope))
587 if (DIE *Variable = TheCU->constructVariableDIE(*DV,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000588 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000589 Children.push_back(Variable);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000590 if (DV->isObjectPointer())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000591 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000592 }
Benjamin Kramer15596c72014-03-07 19:09:39 +0000593 for (LexicalScope *LS : Scope->getChildren())
594 if (DIE *Nested = constructScopeDIE(TheCU, LS))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000595 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000596 return ObjectPointer;
597}
598
599// Construct a DIE for this scope.
Eric Christopher4287a492013-12-09 23:57:44 +0000600DIE *DwarfDebug::constructScopeDIE(DwarfCompileUnit *TheCU,
601 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000602 if (!Scope || !Scope->getScopeNode())
603 return NULL;
604
605 DIScope DS(Scope->getScopeNode());
606
607 SmallVector<DIE *, 8> Children;
608 DIE *ObjectPointer = NULL;
609 bool ChildrenCreated = false;
610
Manman Ren5b2f4b02013-09-11 19:40:28 +0000611 // We try to create the scope DIE first, then the children DIEs. This will
612 // avoid creating un-used children then removing them later when we find out
613 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000614 DIE *ScopeDIE = NULL;
615 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000616 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000617 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000618 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000619 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000620 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000621 // Note down abstract DIE.
622 if (ScopeDIE)
623 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000624 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000625 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000626 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000627 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000628 if (isLexicalScopeDIENull(Scope))
629 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000630
631 // We create children here when we know the scope DIE is not going to be
632 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000633 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
634 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000635
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000636 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000637 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000638 ImportedEntityMap::const_iterator> Range =
639 std::equal_range(
640 ScopesWithImportedEntities.begin(),
641 ScopesWithImportedEntities.end(),
642 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
643 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000644 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000645 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000646 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000647 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000648 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
649 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000650 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000651 }
Eric Christopher6a841382012-11-19 22:42:10 +0000652
Benjamin Kramer892daba2013-08-24 11:55:49 +0000653 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000654 assert(Children.empty() &&
655 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000656 return NULL;
657 }
Manman Ren2312ed32013-09-10 18:40:41 +0000658 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000659 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000660 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000661
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000662 // Add children
Benjamin Kramer15596c72014-03-07 19:09:39 +0000663 for (DIE *I : Children)
664 ScopeDIE->addChild(I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000665
Eric Christophere3417762012-09-12 23:36:19 +0000666 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000667 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000668
Eric Christopherd9843b32011-11-10 19:25:34 +0000669 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000670}
671
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000672// Look up the source id with the given directory and source file names.
673// If none currently exists, create a new id and insert it in the
674// SourceIds map. This can update DirectoryNames and SourceFileNames maps
675// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000676unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
677 unsigned CUID) {
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000678 // If we print assembly, we can't separate .file entries according to
Manman Ren1e427202013-03-07 01:42:00 +0000679 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000680
681 // FIXME: add a better feature test than hasRawTextSupport. Even better,
682 // extend .file to support this.
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000683 if (Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000684 CUID = 0;
685
Devang Patel871d0b12010-09-16 20:57:49 +0000686 // If FE did not provide a file name, then assume stdin.
David Blaikieedc17532014-03-14 16:33:32 +0000687 if (FileName.empty()) {
688 FileName = "<stdin>";
689 DirName = "";
690 }
Devang Patele01b75c2011-03-24 20:30:50 +0000691
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000692 // TODO: this might not belong here. See if we can factor this better.
693 if (DirName == CompilationDir)
694 DirName = "";
695
Manman Ren1e427202013-03-07 01:42:00 +0000696 // FileIDCUMap stores the current ID for the given compile unit.
697 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000698
Manman Ren1e427202013-03-07 01:42:00 +0000699 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000700 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000701 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000702 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000703 NamePair += DirName;
704 NamePair += '\0'; // Zero bytes are not allowed in paths.
705 NamePair += FileName;
706
707 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
708 if (Ent.getValue() != SrcId)
709 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000710
Manman Ren1e427202013-03-07 01:42:00 +0000711 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000712 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000713 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000714
715 return SrcId;
716}
717
Eric Christophera5a79422013-12-09 23:32:48 +0000718void DwarfDebug::addGnuPubAttributes(DwarfUnit *U, DIE *D) const {
David Blaikie3c842622013-12-04 21:31:26 +0000719 if (!GenerateGnuPubSections)
720 return;
721
David Blaikie47c254b2014-03-06 05:47:39 +0000722 U->addFlag(D, dwarf::DW_AT_GNU_pubnames);
David Blaikie3c842622013-12-04 21:31:26 +0000723}
724
Eric Christopher4287a492013-12-09 23:57:44 +0000725// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000726// DW_TAG_compile_unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000727DwarfCompileUnit *DwarfDebug::constructDwarfCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000728 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000729 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000730
731 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +0000732 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
733 InfoHolder.getUnits().size(), Die, DIUnit, Asm, this, &InfoHolder);
David Blaikie2666e242013-12-06 19:38:46 +0000734 InfoHolder.addUnit(NewCU);
735
Manman Ren1e427202013-03-07 01:42:00 +0000736 FileIDCUMap[NewCU->getUniqueID()] = 0;
Manman Ren1e427202013-03-07 01:42:00 +0000737
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000738 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000739 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
740 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000741 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000742
Eric Christopher52ce7182013-04-09 19:23:15 +0000743 if (!useSplitDwarf()) {
David Blaikie2494fdb2014-02-14 22:41:51 +0000744 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000745
746 // If we're using split dwarf the compilation dir is going to be in the
747 // skeleton CU and so we don't need to duplicate it here.
748 if (!CompilationDir.empty())
749 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
750
David Blaikie3c842622013-12-04 21:31:26 +0000751 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000752 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000753
Bill Wendling2b128d72009-05-20 23:19:06 +0000754 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000755 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000756
Devang Patel2d9caf92009-11-25 17:36:49 +0000757 StringRef Flags = DIUnit.getFlags();
758 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000759 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000760
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000761 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000762 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000763 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000764
Devang Patel1a0df9a2010-05-10 22:49:55 +0000765 if (!FirstCU)
766 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000767
Eric Christopherd4368fd2014-01-02 21:03:28 +0000768 if (useSplitDwarf()) {
769 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection(),
770 DwarfInfoDWOSectionSym);
Eric Christopherd8667202013-12-30 17:22:27 +0000771 NewCU->setSkeleton(constructSkeletonCU(NewCU));
Eric Christopherd4368fd2014-01-02 21:03:28 +0000772 } else
773 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
774 DwarfInfoSectionSym);
Eric Christopherd039baa2013-12-30 03:40:32 +0000775
David Blaikie5a152402013-11-15 23:52:02 +0000776 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000777 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000778 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000779}
780
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000781// Construct subprogram DIE.
Eric Christopher4287a492013-12-09 23:57:44 +0000782void DwarfDebug::constructSubprogramDIE(DwarfCompileUnit *TheCU,
783 const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000784 // FIXME: We should only call this routine once, however, during LTO if a
785 // program is defined in multiple CUs we could end up calling it out of
786 // beginModule as we walk the CUs.
787
Eric Christopher4287a492013-12-09 23:57:44 +0000788 DwarfCompileUnit *&CURef = SPMap[N];
Eric Christopherffbc4de2013-10-18 01:57:30 +0000789 if (CURef)
790 return;
791 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000792
Devang Patel80ae3492009-08-28 23:24:31 +0000793 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000794 if (!SP.isDefinition())
795 // This is a method declaration which will be handled while constructing
796 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000797 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000798
Devang Patel89543712011-08-15 17:24:54 +0000799 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000800
Eric Christopherba506db2013-09-09 20:03:20 +0000801 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000802 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000803}
804
Eric Christopher4287a492013-12-09 23:57:44 +0000805void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000806 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000807 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000808 assert(Module.Verify());
David Blaikie684fc532013-05-06 23:33:07 +0000809 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000810 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000811}
812
Eric Christopher4287a492013-12-09 23:57:44 +0000813void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
814 const MDNode *N, DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000815 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000816 assert(Module.Verify());
David Blaikie4dd2de72013-05-08 06:01:38 +0000817 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000818}
819
Eric Christopher4287a492013-12-09 23:57:44 +0000820void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000821 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000822 DIE *Context) {
823 assert(Module.Verify() &&
824 "Use one of the MDNode * overloads to handle invalid metadata");
825 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000826 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000827 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000828 DIE *EntityDie;
829 DIDescriptor Entity = Module.getEntity();
830 if (Entity.isNameSpace())
831 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
832 else if (Entity.isSubprogram())
833 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000834 else if (Entity.isType())
835 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000836 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000837 EntityDie = TheCU->getDIE(Entity);
David Blaikie7066f7b2014-03-12 16:51:06 +0000838 TheCU->addSourceLine(IMDie, Module.getLineNumber(),
839 Module.getContext().getFilename(),
840 Module.getContext().getDirectory());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000841 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000842 StringRef Name = Module.getName();
843 if (!Name.empty())
844 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000845 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000846}
847
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000848// Emit all Dwarf sections that should come prior to the content. Create
849// global DIEs and emit initial debug info sections. This is invoked by
850// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000851void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000852 if (DisableDebugInfoPrinting)
853 return;
854
Eric Christopher58f41952012-11-19 22:42:15 +0000855 const Module *M = MMI->getModule();
856
Nick Lewycky019d2552011-07-29 03:49:23 +0000857 // If module has named metadata anchors then use them, otherwise scan the
858 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000859 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000860 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000861 return;
Manman Ren60352032013-09-05 18:48:31 +0000862 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000863
David Blaikiedc69ebb2013-03-11 23:39:23 +0000864 // Emit initial sections so we can reference labels later.
865 emitSectionLabels();
866
Benjamin Kramer15596c72014-03-07 19:09:39 +0000867 for (MDNode *N : CU_Nodes->operands()) {
868 DICompileUnit CUNode(N);
Eric Christopher4287a492013-12-09 23:57:44 +0000869 DwarfCompileUnit *CU = constructDwarfCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000870 DIArray ImportedEntities = CUNode.getImportedEntities();
871 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000872 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000873 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
874 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000875 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000876 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000877 DIArray GVs = CUNode.getGlobalVariables();
878 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000879 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000880 DIArray SPs = CUNode.getSubprograms();
881 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
882 constructSubprogramDIE(CU, SPs.getElement(i));
883 DIArray EnumTypes = CUNode.getEnumTypes();
884 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
885 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
886 DIArray RetainedTypes = CUNode.getRetainedTypes();
887 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
888 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000889 // Emit imported_modules last so that the relevant context is already
890 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000891 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
892 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000893 }
Eric Christopher6a841382012-11-19 22:42:10 +0000894
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000895 // Tell MMI that we have debug info.
896 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000897
Bill Wendling2b128d72009-05-20 23:19:06 +0000898 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000899 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000900}
901
Eric Christopher960ac372012-11-22 00:59:49 +0000902// Attach DW_AT_inline attribute with inlined subprogram DIEs.
903void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000904 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000905 for (DIE *ISP : InlinedSubprogramDIEs)
David Blaikief2443192013-10-21 17:28:37 +0000906 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000907
908 for (const auto &AI : AbstractSPDies) {
909 DIE *ISP = AI.second;
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000910 if (InlinedSubprogramDIEs.count(ISP))
911 continue;
David Blaikief2443192013-10-21 17:28:37 +0000912 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000913 }
Eric Christopher960ac372012-11-22 00:59:49 +0000914}
915
916// Collect info for variables that were optimized out.
917void DwarfDebug::collectDeadVariables() {
918 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000919
920 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000921 for (MDNode *N : CU_Nodes->operands()) {
922 DICompileUnit TheCU(N);
Eric Christopher960ac372012-11-22 00:59:49 +0000923 DIArray Subprograms = TheCU.getSubprograms();
924 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000925 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000926 if (ProcessedSPNodes.count(SP) != 0)
927 continue;
928 if (!SP.isSubprogram())
929 continue;
930 if (!SP.isDefinition())
931 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000932 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +0000933 if (Variables.getNumElements() == 0)
934 continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000935
Eric Christopher735401c2012-11-27 00:13:51 +0000936 // Construct subprogram DIE and add variables DIEs.
Eric Christopher4287a492013-12-09 23:57:44 +0000937 DwarfCompileUnit *SPCU =
938 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +0000939 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +0000940 // FIXME: See the comment in constructSubprogramDIE about duplicate
941 // subprogram DIEs.
942 constructSubprogramDIE(SPCU, SP);
943 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +0000944 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
945 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +0000946 if (!DV.isVariable())
947 continue;
Manman Renb3388602013-10-05 01:43:03 +0000948 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000949 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +0000950 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +0000951 }
Eric Christopher960ac372012-11-22 00:59:49 +0000952 }
953 }
954 }
Eric Christopher960ac372012-11-22 00:59:49 +0000955}
956
957void DwarfDebug::finalizeModuleInfo() {
958 // Collect info for variables that were optimized out.
959 collectDeadVariables();
960
961 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
962 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000963
Eric Christopherad10cb52013-12-04 23:24:38 +0000964 // Handle anything that needs to be done on a per-unit basis after
965 // all other generation.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000966 for (DwarfUnit *TheU : getUnits()) {
Eric Christopher60eb7692013-08-12 20:27:48 +0000967 // Emit DW_AT_containing_type attribute to connect types with their
968 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +0000969 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000970
Eric Christopher46e23432013-12-20 04:16:18 +0000971 // Add CU specific attributes if we need to add any.
972 if (TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
973 // If we're splitting the dwarf out now that we've got the entire
Eric Christopherd8667202013-12-30 17:22:27 +0000974 // CU then add the dwo id to it.
975 DwarfCompileUnit *SkCU =
976 static_cast<DwarfCompileUnit *>(TheU->getSkeleton());
Eric Christopher46e23432013-12-20 04:16:18 +0000977 if (useSplitDwarf()) {
978 // This should be a unique identifier when we want to build .dwp files.
979 uint64_t ID = 0;
980 if (GenerateCUHash) {
Eric Christopher420569b2014-02-20 02:50:45 +0000981 DIEHash CUHash(Asm);
Eric Christopher46e23432013-12-20 04:16:18 +0000982 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
983 }
984 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
985 dwarf::DW_FORM_data8, ID);
Eric Christopher46e23432013-12-20 04:16:18 +0000986 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
987 dwarf::DW_FORM_data8, ID);
Eric Christopherd29614f2013-08-13 01:21:55 +0000988 }
Eric Christopherd8667202013-12-30 17:22:27 +0000989
Eric Christopher39cde8c2014-01-14 22:44:17 +0000990 // If we have code split among multiple sections or we've requested
991 // it then emit a DW_AT_ranges attribute on the unit that will remain
992 // in the .o file, otherwise add a DW_AT_low_pc.
Eric Christopherd8667202013-12-30 17:22:27 +0000993 // FIXME: Also add a high pc if we can.
Eric Christopher39cde8c2014-01-14 22:44:17 +0000994 // FIXME: We should use ranges if we have multiple compile units or
995 // allow reordering of code ala .subsections_via_symbols in mach-o.
Eric Christopherd8667202013-12-30 17:22:27 +0000996 DwarfCompileUnit *U = SkCU ? SkCU : static_cast<DwarfCompileUnit *>(TheU);
Eric Christopher8873ada2014-01-29 22:22:56 +0000997 if (useCURanges() && TheU->getRanges().size()) {
Eric Christopherd8667202013-12-30 17:22:27 +0000998 addSectionLabel(Asm, U, U->getUnitDie(), dwarf::DW_AT_ranges,
999 Asm->GetTempSymbol("cu_ranges", U->getUniqueID()),
1000 DwarfDebugRangeSectionSym);
Eric Christopher8873ada2014-01-29 22:22:56 +00001001
1002 // A DW_AT_low_pc attribute may also be specified in combination with
1003 // DW_AT_ranges to specify the default base address for use in location
1004 // lists (see Section 2.6.2) and range lists (see Section 2.17.3).
1005 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1006 0);
1007 } else
Eric Christophercf48ade2014-01-24 11:52:53 +00001008 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1009 0);
Eric Christopher60eb7692013-08-12 20:27:48 +00001010 }
1011 }
1012
1013 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001014 InfoHolder.computeSizeAndOffsets();
1015 if (useSplitDwarf())
1016 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001017}
1018
1019void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001020 // Filter labels by section.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001021 for (const SymbolCU &SCU : ArangeLabels) {
Richard Mitton21101b32013-09-19 23:21:01 +00001022 if (SCU.Sym->isInSection()) {
1023 // Make a note of this symbol and it's section.
1024 const MCSection *Section = &SCU.Sym->getSection();
1025 if (!Section->getKind().isMetadata())
1026 SectionMap[Section].push_back(SCU);
1027 } else {
1028 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1029 // appear in the output. This sucks as we rely on sections to build
1030 // arange spans. We can do it without, but it's icky.
1031 SectionMap[NULL].push_back(SCU);
1032 }
1033 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001034
Richard Mittonc2508242013-10-03 22:07:08 +00001035 // Build a list of sections used.
1036 std::vector<const MCSection *> Sections;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001037 for (const auto &it : SectionMap) {
1038 const MCSection *Section = it.first;
Richard Mittonc2508242013-10-03 22:07:08 +00001039 Sections.push_back(Section);
1040 }
1041
1042 // Sort the sections into order.
1043 // This is only done to ensure consistent output order across different runs.
1044 std::sort(Sections.begin(), Sections.end(), SectionSort);
1045
1046 // Add terminating symbols for each section.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001047 for (unsigned ID = 0, E = Sections.size(); ID != E; ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001048 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001049 MCSymbol *Sym = NULL;
1050
1051 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001052 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001053 // if we know the section name up-front. For user-created sections, the
Eric Christopher95531b62014-01-29 22:06:21 +00001054 // resulting label may not be valid to use as a label. (section names can
1055 // use a greater set of characters on some systems)
Richard Mittonc2508242013-10-03 22:07:08 +00001056 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001057 Asm->OutStreamer.SwitchSection(Section);
1058 Asm->OutStreamer.EmitLabel(Sym);
1059 }
1060
1061 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001062 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001063 }
Eric Christopher1bca60d2014-01-23 22:55:47 +00001064
Eric Christopher3a70d002014-03-14 20:53:43 +00001065 // For now only turn on CU ranges if we have -ffunction-sections enabled,
1066 // we've emitted a function into a unique section, or we're using LTO. If
1067 // we're using LTO then we can't know that any particular function in the
1068 // module is correlated to a particular CU and so we need to be conservative.
1069 // At this point all sections should be finalized except for dwarf sections.
1070 HasCURanges = UsedNonDefaultText || (CUMap.size() > 1) ||
Eric Christopher1bca60d2014-01-23 22:55:47 +00001071 TargetMachine::getFunctionSections();
Eric Christopher960ac372012-11-22 00:59:49 +00001072}
Bill Wendling2b128d72009-05-20 23:19:06 +00001073
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001074// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001075void DwarfDebug::endModule() {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001076 assert(CurFn == 0);
1077 assert(CurMI == 0);
Eric Christopher960ac372012-11-22 00:59:49 +00001078
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001079 if (!FirstCU)
1080 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001081
1082 // End any existing sections.
1083 // TODO: Does this need to happen?
1084 endSections();
1085
1086 // Finalize the debug info for the module.
1087 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001088
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001089 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001090
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001091 // Emit all the DIEs into a debug info section.
1092 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001093
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001094 // Corresponding abbreviations into a abbrev section.
1095 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001096
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001097 // Emit info into a debug loc section.
1098 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001099
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001100 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +00001101 if (GenerateARangeSection)
1102 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001103
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001104 // Emit info into a debug ranges section.
1105 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001106
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001107 if (useSplitDwarf()) {
1108 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001109 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001110 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001111 // Emit DWO addresses.
1112 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001113 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001114
Eric Christophera876b822012-08-23 07:32:06 +00001115 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001116 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001117 emitAccelNames();
1118 emitAccelObjC();
1119 emitAccelNamespaces();
1120 emitAccelTypes();
1121 }
Eric Christopher6a841382012-11-19 22:42:10 +00001122
Eric Christopher4b358182013-08-30 00:40:17 +00001123 // Emit the pubnames and pubtypes sections if requested.
1124 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001125 emitDebugPubNames(GenerateGnuPubSections);
1126 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001127 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001128
Devang Pateld0701282010-08-02 17:32:15 +00001129 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001130 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001131
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001132 // Reset these for the next Module if we have one.
1133 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001134}
1135
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001136// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001137DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001138 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001139 LLVMContext &Ctx = DV->getContext();
1140 // More then one inlined variable corresponds to one abstract variable.
1141 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001142 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001143 if (AbsDbgVariable)
1144 return AbsDbgVariable;
1145
Devang Patel7e623022011-08-10 20:55:27 +00001146 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001147 if (!Scope)
1148 return NULL;
1149
Manman Renb3388602013-10-05 01:43:03 +00001150 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001151 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001152 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001153 return AbsDbgVariable;
1154}
1155
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001156// If Var is a current function argument then add it to CurrentFnArguments list.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001157bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +00001158 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001159 return false;
1160 DIVariable DV = Var->getVariable();
1161 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1162 return false;
1163 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001164 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001165 return false;
1166
Devang Patel4ab660b2011-03-03 20:02:02 +00001167 size_t Size = CurrentFnArguments.size();
1168 if (Size == 0)
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001169 CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001170 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001171 // arguments does the function have at source level.
1172 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001173 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001174 CurrentFnArguments[ArgNo - 1] = Var;
1175 return true;
1176}
1177
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001178// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001179void DwarfDebug::collectVariableInfoFromMMITable(
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001180 SmallPtrSet<const MDNode *, 16> &Processed) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00001181 for (const auto &VI : MMI->getVariableDbgInfo()) {
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +00001182 if (!VI.Var)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001183 continue;
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +00001184 Processed.insert(VI.Var);
1185 DIVariable DV(VI.Var);
1186 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001187
Devang Patelcdb7d442009-11-10 23:20:04 +00001188 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001189 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001190 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001191
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +00001192 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VI.Loc);
Manman Renb3388602013-10-05 01:43:03 +00001193 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +00001194 RegVar->setFrameIndex(VI.Slot);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001195 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001196 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001197 if (AbsDbgVariable)
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +00001198 AbsDbgVariable->setFrameIndex(VI.Slot);
Devang Patel475d32a2009-10-06 01:26:37 +00001199 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001200}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001201
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001202// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1203// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001204static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001205 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001206 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1207 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001208 (MI->getOperand(1).isImm() ||
1209 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001210}
1211
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001212// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001213static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1214 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001215 const MCSymbol *SLabel,
1216 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001217 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001218
David Blaikie0252265b2013-06-16 20:34:15 +00001219 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001220 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001221 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001222 // If the second operand is an immediate, this is a
1223 // register-indirect address.
1224 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001225 MLoc.set(MI->getOperand(0).getReg());
1226 else
1227 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001228 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1229 }
1230 if (MI->getOperand(0).isImm())
1231 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1232 if (MI->getOperand(0).isFPImm())
1233 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1234 if (MI->getOperand(0).isCImm())
1235 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1236
Craig Topperee4dab52012-02-05 08:31:47 +00001237 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001238}
1239
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001240// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001241void
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001242DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001243
Eric Christopher270a12c2013-07-03 21:37:03 +00001244 // Grab the variable info that was squirreled away in the MMI side-table.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001245 collectVariableInfoFromMMITable(Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001246
Benjamin Kramer15596c72014-03-07 19:09:39 +00001247 for (const MDNode *Var : UserVariables) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001248 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001249 continue;
1250
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001251 // History contains relevant DBG_VALUE instructions for Var and instructions
1252 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001253 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001254 if (History.empty())
1255 continue;
1256 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001257
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001258 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001259 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001260 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001261 DISubprogram(DV.getContext()).describes(CurFn->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001262 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001263 else if (MDNode *IA = DV.getInlinedAt())
1264 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1265 else
1266 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001267 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001268 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001269 continue;
1270
1271 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001272 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001273 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001274 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001275 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001276 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001277 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001278 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001279
Eric Christophercc10d202012-10-08 20:48:54 +00001280 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001281 if (History.size() <= 1 ||
1282 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001283 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001284 continue;
1285 }
1286
Eric Christopher59cc0712013-01-28 17:33:26 +00001287 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001288 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001289
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001290 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1291 HI = History.begin(),
1292 HE = History.end();
1293 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001294 const MachineInstr *Begin = *HI;
1295 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001296
Devang Patele7181b52011-06-01 23:00:17 +00001297 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001298 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1299 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001300 continue;
1301
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001302 // Compute the range for a register location.
1303 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1304 const MCSymbol *SLabel = 0;
1305
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001306 if (HI + 1 == HE)
1307 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001308 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001309 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001310 else {
1311 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001312 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001313 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001314 if (End->isDebugValue())
1315 SLabel = getLabelBeforeInsn(End);
1316 else {
1317 // End is a normal instruction clobbering the range.
1318 SLabel = getLabelAfterInsn(End);
1319 assert(SLabel && "Forgot label after clobber instruction");
1320 ++HI;
1321 }
1322 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001323
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001324 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001325 DotDebugLocEntries.push_back(
1326 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001327 }
1328 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001329 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001330
1331 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001332 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1333 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1334 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1335 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001336 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001337 continue;
1338 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001339 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001340 }
Devang Patel9fc11702010-05-25 23:40:22 +00001341}
Devang Patele0a94bf2010-05-14 21:01:35 +00001342
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001343// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001344MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001345 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1346 assert(Label && "Didn't insert label before instruction");
1347 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001348}
1349
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001350// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001351MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001352 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001353}
1354
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001355// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001356void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001357 assert(CurMI == 0);
1358 CurMI = MI;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001359 // Check if source location changes, but ignore DBG_VALUE locations.
1360 if (!MI->isDebugValue()) {
1361 DebugLoc DL = MI->getDebugLoc();
1362 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001363 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001364 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001365 if (DL == PrologEndLoc) {
1366 Flags |= DWARF2_FLAG_PROLOGUE_END;
1367 PrologEndLoc = DebugLoc();
1368 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001369 if (PrologEndLoc.isUnknown())
1370 Flags |= DWARF2_FLAG_IS_STMT;
1371
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001372 if (!DL.isUnknown()) {
1373 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001374 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001375 } else
Devang Patel34a66202011-05-11 19:22:19 +00001376 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001377 }
Devang Patel9fc11702010-05-25 23:40:22 +00001378 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001379
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001380 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001381 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1382 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001383
1384 // No label needed.
1385 if (I == LabelsBeforeInsn.end())
1386 return;
1387
1388 // Label already assigned.
1389 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001390 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001391
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001392 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001393 PrevLabel = MMI->getContext().CreateTempSymbol();
1394 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001395 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001396 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001397}
1398
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001399// Process end of an instruction.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001400void DwarfDebug::endInstruction() {
1401 assert(CurMI != 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001402 // Don't create a new label after DBG_VALUE instructions.
1403 // They don't generate code.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001404 if (!CurMI->isDebugValue())
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001405 PrevLabel = 0;
1406
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001407 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001408 LabelsAfterInsn.find(CurMI);
1409 CurMI = 0;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001410
1411 // No label needed.
1412 if (I == LabelsAfterInsn.end())
1413 return;
1414
1415 // Label already assigned.
1416 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001417 return;
1418
1419 // We need a label after this instruction.
1420 if (!PrevLabel) {
1421 PrevLabel = MMI->getContext().CreateTempSymbol();
1422 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001423 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001424 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001425}
1426
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001427// Each LexicalScope has first instruction and last instruction to mark
1428// beginning and end of a scope respectively. Create an inverse map that list
1429// scopes starts (and ends) with an instruction. One instruction may start (or
1430// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001431void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001432 SmallVector<LexicalScope *, 4> WorkList;
1433 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001434 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001435 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001436
Craig Topper977e9cd2013-07-03 04:24:43 +00001437 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001438 if (!Children.empty())
Benjamin Kramer15596c72014-03-07 19:09:39 +00001439 WorkList.append(Children.begin(), Children.end());
Devang Patel7771b7c2010-01-20 02:05:23 +00001440
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001441 if (S->isAbstractScope())
1442 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001443
Benjamin Kramer15596c72014-03-07 19:09:39 +00001444 for (const InsnRange &R : S->getRanges()) {
1445 assert(R.first && "InsnRange does not have first instruction!");
1446 assert(R.second && "InsnRange does not have second instruction!");
1447 requestLabelBeforeInsn(R.first);
1448 requestLabelAfterInsn(R.second);
Devang Patel6c74a872010-04-27 19:46:33 +00001449 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001450 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001451}
1452
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001453// Gather pre-function debug information. Assumes being called immediately
1454// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001455void DwarfDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001456 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001457
1458 // If there's no debug info for the function we're not going to do anything.
1459 if (!MMI->hasDebugInfo())
1460 return;
1461
1462 // Grab the lexical scopes for the function, if we don't have any of those
1463 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001464 LScopes.initialize(*MF);
Eric Christopher4dd947a2014-03-14 20:53:49 +00001465 if (LScopes.empty()) {
1466 UsedNonDefaultText = true;
Eric Christopherfedfa442013-11-01 23:14:17 +00001467 return;
Eric Christopher4dd947a2014-03-14 20:53:49 +00001468 }
Eric Christopherfedfa442013-11-01 23:14:17 +00001469
1470 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1471
1472 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001473 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001474
Eric Christopher4287a492013-12-09 23:57:44 +00001475 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001476 // belongs to so that we add to the correct per-cu line table in the
1477 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001478 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001479 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren4e042a62013-02-05 21:52:47 +00001480 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001481 if (Asm->OutStreamer.hasRawTextSupport())
1482 // Use a single line table if we are generating assembly.
Manman Ren9d4c7352013-05-21 00:57:22 +00001483 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1484 else
1485 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001486
Eric Christopher2037caf2014-01-28 00:49:26 +00001487 // Check the current section against the standard text section. If different
1488 // keep track so that we will know when we're emitting functions into multiple
1489 // sections.
1490 if (Asm->getObjFileLowering().getTextSection() != Asm->getCurrentSection())
1491 UsedNonDefaultText = true;
1492
Eric Christopherfedfa442013-11-01 23:14:17 +00001493 // Emit a label for the function so that we have a beginning address.
1494 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001495 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001496 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001497
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001498 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001499 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001500 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001501
Eric Christopherfedfa442013-11-01 23:14:17 +00001502 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1503 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001504 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001505 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1506 II != IE; ++II) {
1507 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001508
Devang Patel002d54d2010-05-26 19:37:24 +00001509 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001510 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001511
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001512 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001513 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001514 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001515
1516 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001517 if (isDbgValueInDefinedReg(MI))
1518 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1519
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001520 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001521 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001522 if (History.empty()) {
1523 UserVariables.push_back(Var);
1524 // The first mention of a function argument gets the FunctionBeginSym
1525 // label, so arguments are visible when breaking at function entry.
1526 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001527 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001528 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001529 LabelsBeforeInsn[MI] = FunctionBeginSym;
1530 } else {
1531 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1532 const MachineInstr *Prev = History.back();
1533 if (Prev->isDebugValue()) {
1534 // Coalesce identical entries at the end of History.
1535 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001536 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001537 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001538 << "\t" << *Prev << "\t"
1539 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001540 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001541 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001542
1543 // Terminate old register assignments that don't reach MI;
1544 MachineFunction::const_iterator PrevMBB = Prev->getParent();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001545 if (PrevMBB != I && (!AtBlockEntry || std::next(PrevMBB) != I) &&
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001546 isDbgValueInDefinedReg(Prev)) {
1547 // Previous register assignment needs to terminate at the end of
1548 // its basic block.
1549 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001550 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001551 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001552 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001553 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001554 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001555 History.pop_back();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001556 } else if (std::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001557 // Terminate after LastMI.
1558 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001559 }
1560 }
1561 }
1562 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001563 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001564 // Not a DBG_VALUE instruction.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001565 if (!MI->isPosition())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001566 AtBlockEntry = false;
1567
Eric Christopher133195782012-10-04 20:46:14 +00001568 // First known non-DBG_VALUE and non-frame setup location marks
1569 // the beginning of the function body.
1570 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1571 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001572 PrologEndLoc = MI->getDebugLoc();
1573
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001574 // Check if the instruction clobbers any registers with debug vars.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001575 for (const MachineOperand &MO : MI->operands()) {
1576 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001577 continue;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001578 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
Eric Christopherfedfa442013-11-01 23:14:17 +00001579 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001580 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001581 const MDNode *Var = LiveUserVar[Reg];
1582 if (!Var)
1583 continue;
1584 // Reg is now clobbered.
1585 LiveUserVar[Reg] = 0;
1586
1587 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001588 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1589 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001590 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001591 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001592 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001593 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001594 const MachineInstr *Prev = History.back();
1595 // Sanity-check: Register assignments are terminated at the end of
1596 // their block.
1597 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1598 continue;
1599 // Is the variable still in Reg?
1600 if (!isDbgValueInDefinedReg(Prev) ||
1601 Prev->getOperand(0).getReg() != Reg)
1602 continue;
1603 // Var is clobbered. Make sure the next instruction gets a label.
1604 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001605 }
1606 }
Devang Patel002d54d2010-05-26 19:37:24 +00001607 }
Devang Patel002d54d2010-05-26 19:37:24 +00001608 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001609 }
1610
Benjamin Kramer15596c72014-03-07 19:09:39 +00001611 for (auto &I : DbgValues) {
1612 SmallVectorImpl<const MachineInstr *> &History = I.second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001613 if (History.empty())
1614 continue;
1615
1616 // Make sure the final register assignments are terminated.
1617 const MachineInstr *Prev = History.back();
1618 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1619 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001620 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001621 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001622 if (LastMI == PrevMBB->end())
1623 // Drop DBG_VALUE for empty range.
1624 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001625 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001626 // Terminate after LastMI.
1627 History.push_back(LastMI);
1628 }
1629 }
1630 // Request labels for the full history.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001631 for (const MachineInstr *MI : History) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001632 if (MI->isDebugValue())
1633 requestLabelBeforeInsn(MI);
1634 else
1635 requestLabelAfterInsn(MI);
1636 }
1637 }
Devang Patel002d54d2010-05-26 19:37:24 +00001638
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001639 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001640 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001641
1642 // Record beginning of function.
1643 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001644 DebugLoc FnStartDL =
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00001645 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
Eric Christopherfedfa442013-11-01 23:14:17 +00001646 recordSourceLine(
1647 FnStartDL.getLine(), FnStartDL.getCol(),
1648 FnStartDL.getScope(MF->getFunction()->getContext()),
1649 // We'd like to list the prologue as "not statements" but GDB behaves
1650 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1651 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001652 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001653}
1654
Devang Patel7e623022011-08-10 20:55:27 +00001655void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001656 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1657 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001658 // Variables with positive arg numbers are parameters.
1659 if (unsigned ArgNum = DV.getArgNumber()) {
1660 // Keep all parameters in order at the start of the variable list to ensure
1661 // function types are correct (no out-of-order parameters)
1662 //
1663 // This could be improved by only doing it for optimized builds (unoptimized
1664 // builds have the right order to begin with), searching from the back (this
1665 // would catch the unoptimized case quickly), or doing a binary search
1666 // rather than linear search.
1667 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1668 while (I != Vars.end()) {
1669 unsigned CurNum = (*I)->getVariable().getArgNumber();
1670 // A local (non-parameter) variable has been found, insert immediately
1671 // before it.
1672 if (CurNum == 0)
1673 break;
1674 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001675 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001676 break;
David Blaikieb272a752013-06-06 22:28:26 +00001677 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001678 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001679 Vars.insert(I, Var);
1680 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001681 }
1682
1683 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001684}
1685
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001686// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001687void DwarfDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001688 // Every beginFunction(MF) call should be followed by an endFunction(MF) call,
1689 // though the beginFunction may not be called at all.
1690 // We should handle both cases.
1691 if (CurFn == 0)
1692 CurFn = MF;
1693 else
1694 assert(CurFn == MF);
1695 assert(CurFn != 0);
1696
1697 if (!MMI->hasDebugInfo() || LScopes.empty()) {
1698 CurFn = 0;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001699 return;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001700 }
Devang Patel2904aa92009-11-12 19:02:56 +00001701
Devang Patel7e623022011-08-10 20:55:27 +00001702 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001703 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001704 // Assumes in correct section after the entry point.
1705 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher1a972152014-01-29 23:05:43 +00001706
Eric Christopher4287a492013-12-09 23:57:44 +00001707 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Manman Ren4e042a62013-02-05 21:52:47 +00001708 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001709
Devang Patel7e623022011-08-10 20:55:27 +00001710 SmallPtrSet<const MDNode *, 16> ProcessedVars;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001711 collectVariableInfo(ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001712
Devang Patel3acc70e2011-08-15 22:04:40 +00001713 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001714 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001715 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001716
Devang Patel7e623022011-08-10 20:55:27 +00001717 // Construct abstract scopes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001718 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Devang Patel44403472011-08-12 18:10:19 +00001719 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001720 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001721 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001722 DIArray Variables = SP.getVariables();
1723 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1724 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001725 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001726 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001727 // Check that DbgVariable for DV wasn't created earlier, when
1728 // findAbstractVariable() was called for inlined instance of DV.
1729 LLVMContext &Ctx = DV->getContext();
1730 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1731 if (AbstractVariables.lookup(CleanDV))
1732 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001733 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001734 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001735 }
1736 }
Devang Patel44403472011-08-12 18:10:19 +00001737 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001738 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001739 }
Eric Christopher6a841382012-11-19 22:42:10 +00001740
Devang Patel3acc70e2011-08-15 22:04:40 +00001741 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001742 if (!CurFn->getTarget().Options.DisableFramePointerElim(*CurFn))
Eric Christopherbb69a272012-08-24 01:14:27 +00001743 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001744
Eric Christopher1a972152014-01-29 23:05:43 +00001745 // Add the range of this function to the list of ranges for the CU.
1746 RangeSpan Span(FunctionBeginSym, FunctionEndSym);
Chandler Carruth002da5d2014-03-02 04:08:41 +00001747 TheCU->addRange(std::move(Span));
Eric Christopher1a972152014-01-29 23:05:43 +00001748
Bill Wendling2b128d72009-05-20 23:19:06 +00001749 // Clear debug info
Benjamin Kramer15596c72014-03-07 19:09:39 +00001750 for (auto &I : ScopeVariables)
1751 DeleteContainerPointers(I.second);
Devang Patel7e623022011-08-10 20:55:27 +00001752 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001753 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001754 UserVariables.clear();
1755 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001756 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001757 LabelsBeforeInsn.clear();
1758 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001759 PrevLabel = NULL;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001760 CurFn = 0;
Bill Wendling2b128d72009-05-20 23:19:06 +00001761}
1762
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001763// Register a source line with debug info. Returns the unique label that was
1764// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001765void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1766 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001767 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001768 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001769 unsigned Src = 1;
Diego Novillo282450d2014-03-03 18:53:17 +00001770 unsigned Discriminator = 0;
Dan Gohman50849c62010-05-05 23:41:32 +00001771 if (S) {
1772 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001773
Dan Gohman50849c62010-05-05 23:41:32 +00001774 if (Scope.isCompileUnit()) {
1775 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001776 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001777 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001778 } else if (Scope.isFile()) {
1779 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001780 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001781 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001782 } else if (Scope.isSubprogram()) {
1783 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001784 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001785 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001786 } else if (Scope.isLexicalBlockFile()) {
1787 DILexicalBlockFile DBF(S);
1788 Fn = DBF.getFilename();
1789 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001790 } else if (Scope.isLexicalBlock()) {
1791 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001792 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001793 Dir = DB.getDirectory();
Diego Novillo282450d2014-03-03 18:53:17 +00001794 Discriminator = DB.getDiscriminator();
Dan Gohman50849c62010-05-05 23:41:32 +00001795 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001796 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001797
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001798 Src = getOrCreateSourceID(
1799 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001800 }
Diego Novillo282450d2014-03-03 18:53:17 +00001801 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1802 Discriminator, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001803}
1804
Bill Wendling806535f2009-05-20 23:22:40 +00001805//===----------------------------------------------------------------------===//
1806// Emit Methods
1807//===----------------------------------------------------------------------===//
1808
Manman Rence20d462013-10-29 22:57:10 +00001809// Compute the size and offset of a DIE. The offset is relative to start of the
1810// CU. It returns the offset after laying out the DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001811unsigned DwarfFile::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001812 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001813 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001814
1815 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001816 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001817
1818 // Set DIE offset
1819 Die->setOffset(Offset);
1820
1821 // Start the size with the size of abbreviation code.
Logan Chien5b776b72014-02-22 14:00:39 +00001822 Offset += getULEB128Size(Die->getAbbrevNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001823
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001824 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001825 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001826
1827 // Size the DIE attribute values.
1828 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1829 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001830 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001831
Benjamin Kramer15596c72014-03-07 19:09:39 +00001832 // Get the children.
1833 const std::vector<DIE *> &Children = Die->getChildren();
1834
Bill Wendling480ff322009-05-20 23:21:38 +00001835 // Size the DIE children if any.
1836 if (!Children.empty()) {
Eric Christophere8f10722014-03-05 01:44:58 +00001837 assert(Abbrev.hasChildren() && "Children flag not set");
Bill Wendling480ff322009-05-20 23:21:38 +00001838
Benjamin Kramer15596c72014-03-07 19:09:39 +00001839 for (DIE *Child : Children)
1840 Offset = computeSizeAndOffset(Child, Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001841
1842 // End of children marker.
1843 Offset += sizeof(int8_t);
1844 }
1845
1846 Die->setSize(Offset - Die->getOffset());
1847 return Offset;
1848}
1849
Eric Christopherb088d2d2013-10-24 21:05:08 +00001850// Compute the size and offset for each DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001851void DwarfFile::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001852 // Offset from the first CU in the debug info section is 0 initially.
1853 unsigned SecOffset = 0;
1854
Eric Christopherb088d2d2013-10-24 21:05:08 +00001855 // Iterate over each compile unit and set the size and offsets for each
1856 // DIE within each compile unit. All offsets are CU relative.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001857 for (DwarfUnit *TheU : CUs) {
1858 TheU->setDebugInfoOffset(SecOffset);
Manman Rence20d462013-10-29 22:57:10 +00001859
1860 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001861 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
Benjamin Kramer15596c72014-03-07 19:09:39 +00001862 TheU->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001863
1864 // EndOffset here is CU-relative, after laying out
1865 // all of the CU DIE.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001866 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001867 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001868 }
Bill Wendling480ff322009-05-20 23:21:38 +00001869}
1870
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001871// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001872void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001873 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001874
Bill Wendling480ff322009-05-20 23:21:38 +00001875 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001876 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001877 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Eric Christopherd8667202013-12-30 17:22:27 +00001878 if (useSplitDwarf())
1879 DwarfInfoDWOSectionSym =
1880 emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001881 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001882 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001883 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001884 DwarfAbbrevDWOSectionSym = emitSectionSym(
1885 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
David Blaikie20474102014-02-25 22:46:44 +00001886 if (GenerateARangeSection)
1887 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001888
Eric Christopher74804332013-02-07 21:19:50 +00001889 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001890 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001891 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001892 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001893 DwarfGnuPubNamesSectionSym =
1894 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1895 DwarfGnuPubTypesSectionSym =
1896 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001897 } else if (HasDwarfPubSections) {
1898 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1899 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001900 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001901
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001902 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001903 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001904 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001905 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001906 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001907 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001908 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00001909 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001910 DwarfDebugRangeSectionSym =
1911 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001912
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001913 DwarfDebugLocSectionSym =
1914 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Bill Wendling480ff322009-05-20 23:21:38 +00001915}
1916
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001917// Recursively emits a debug information entry.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001918void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001919 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001920 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001921
Bill Wendling480ff322009-05-20 23:21:38 +00001922 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001923 if (Asm->isVerbose())
David Blaikieff3ab2c2013-12-05 01:01:41 +00001924 Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
1925 "] 0x" + Twine::utohexstr(Die->getOffset()) +
1926 ":0x" + Twine::utohexstr(Die->getSize()) + " " +
1927 dwarf::TagString(Abbrev.getTag()));
1928 Asm->EmitULEB128(Abbrev.getNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001929
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001930 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001931 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001932
1933 // Emit the DIE attribute values.
1934 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00001935 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
1936 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00001937 assert(Form && "Too many attributes for DIE (check abbreviation)");
1938
Eric Christopher13a1bb32014-03-06 00:00:49 +00001939 if (Asm->isVerbose()) {
Chris Lattner5adf9872010-01-24 18:54:17 +00001940 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Eric Christopher13a1bb32014-03-06 00:00:49 +00001941 if (Attr == dwarf::DW_AT_accessibility)
1942 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(
1943 cast<DIEInteger>(Values[i])->getValue()));
1944 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001945
Eric Christopherdd508382014-03-06 00:00:56 +00001946 // Emit an attribute using the defined form.
1947 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001948 }
1949
1950 // Emit the DIE children if any.
Eric Christophere8f10722014-03-05 01:44:58 +00001951 if (Abbrev.hasChildren()) {
Bill Wendling480ff322009-05-20 23:21:38 +00001952 const std::vector<DIE *> &Children = Die->getChildren();
1953
Benjamin Kramer15596c72014-03-07 19:09:39 +00001954 for (DIE *Child : Children)
1955 emitDIE(Child);
Bill Wendling480ff322009-05-20 23:21:38 +00001956
David Blaikie155f8812013-12-04 21:51:05 +00001957 Asm->OutStreamer.AddComment("End Of Children Mark");
Chris Lattner566cae92010-03-09 23:52:58 +00001958 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001959 }
1960}
1961
Eric Christophera2de8262012-12-15 00:04:07 +00001962// Emit the various dwarf units to the unit section USection with
1963// the abbreviations going into ASection.
David Blaikie03073f72013-12-06 22:14:48 +00001964void DwarfFile::emitUnits(DwarfDebug *DD, const MCSection *ASection,
1965 const MCSymbol *ASectionSym) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00001966 for (DwarfUnit *TheU : CUs) {
David Blaikie2a80e442013-12-02 22:09:48 +00001967 DIE *Die = TheU->getUnitDie();
David Blaikie03073f72013-12-06 22:14:48 +00001968 const MCSection *USection = TheU->getSection();
1969 Asm->OutStreamer.SwitchSection(USection);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001970
Devang Patel1a0df9a2010-05-10 22:49:55 +00001971 // Emit the compile units header.
David Blaikie7d734602013-12-06 22:33:05 +00001972 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001973
Devang Patel1a0df9a2010-05-10 22:49:55 +00001974 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00001975 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00001976 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001977
David Blaikie2a80e442013-12-02 22:09:48 +00001978 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001979
David Blaikieff3ab2c2013-12-05 01:01:41 +00001980 DD->emitDIE(Die);
David Blaikie7d734602013-12-06 22:33:05 +00001981 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001982 }
Bill Wendling480ff322009-05-20 23:21:38 +00001983}
1984
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001985// Emit the debug info section.
1986void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00001987 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christophera2de8262012-12-15 00:04:07 +00001988
David Blaikie03073f72013-12-06 22:14:48 +00001989 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfAbbrevSection(),
Eric Christophera2de8262012-12-15 00:04:07 +00001990 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001991}
1992
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001993// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00001994void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00001995 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00001996
1997 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00001998}
Bill Wendling480ff322009-05-20 23:21:38 +00001999
Eric Christopherf8194852013-12-05 18:06:10 +00002000void DwarfFile::emitAbbrevs(const MCSection *Section) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002001 // Check to see if it is worth the effort.
David Blaikie0504cda2013-12-05 07:43:55 +00002002 if (!Abbreviations.empty()) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002003 // Start the debug abbrev section.
2004 Asm->OutStreamer.SwitchSection(Section);
2005
Bill Wendling480ff322009-05-20 23:21:38 +00002006 // For each abbrevation.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002007 for (const DIEAbbrev *Abbrev : Abbreviations) {
Bill Wendling480ff322009-05-20 23:21:38 +00002008 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002009 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002010
2011 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002012 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002013 }
2014
2015 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002016 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002017 }
2018}
2019
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002020// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002021void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002022 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002023 Asm->OutStreamer.AddComment("Extended Op");
2024 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002025
Chris Lattner566cae92010-03-09 23:52:58 +00002026 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002027 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002028 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2029 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2030
2031 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002032
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002033 Asm->OutStreamer.EmitSymbolValue(
2034 Asm->GetTempSymbol("section_end", SectionEnd),
2035 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002036
2037 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002038 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2039 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002040 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002041 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002042}
2043
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002044// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002045void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002046 DwarfAccelTable AT(
2047 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002048 for (DwarfUnit *TheU : getUnits()) {
2049 for (const auto &GI : TheU->getAccelNames()) {
2050 StringRef Name = GI.getKey();
2051 for (const DIE *D : GI.second)
2052 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002053 }
2054 }
2055
2056 AT.FinalizeTable(Asm, "Names");
2057 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002058 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002059 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2060 Asm->OutStreamer.EmitLabel(SectionBegin);
2061
2062 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002063 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002064}
2065
Eric Christopher48fef592012-12-20 21:58:40 +00002066// Emit objective C classes and categories into a hashed accelerator table
2067// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002068void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002069 DwarfAccelTable AT(
2070 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002071 for (DwarfUnit *TheU : getUnits()) {
2072 for (const auto &GI : TheU->getAccelObjC()) {
2073 StringRef Name = GI.getKey();
2074 for (const DIE *D : GI.second)
2075 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002076 }
2077 }
2078
2079 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002080 Asm->OutStreamer.SwitchSection(
2081 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002082 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2083 Asm->OutStreamer.EmitLabel(SectionBegin);
2084
2085 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002086 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002087}
2088
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002089// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002090void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002091 DwarfAccelTable AT(
2092 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002093 for (DwarfUnit *TheU : getUnits()) {
2094 for (const auto &GI : TheU->getAccelNamespace()) {
2095 StringRef Name = GI.getKey();
2096 for (const DIE *D : GI.second)
2097 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002098 }
2099 }
2100
2101 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002102 Asm->OutStreamer.SwitchSection(
2103 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002104 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2105 Asm->OutStreamer.EmitLabel(SectionBegin);
2106
2107 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002108 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002109}
2110
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002111// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002112void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002113 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002114 Atoms.push_back(
2115 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2116 Atoms.push_back(
2117 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2118 Atoms.push_back(
2119 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002120 DwarfAccelTable AT(Atoms);
Benjamin Kramer15596c72014-03-07 19:09:39 +00002121 for (DwarfUnit *TheU : getUnits()) {
2122 for (const auto &GI : TheU->getAccelTypes()) {
2123 StringRef Name = GI.getKey();
2124 for (const auto &DI : GI.second)
2125 AT.AddName(Name, DI.first, DI.second);
Eric Christopher4996c702011-11-07 09:24:32 +00002126 }
2127 }
2128
2129 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002130 Asm->OutStreamer.SwitchSection(
2131 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002132 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2133 Asm->OutStreamer.EmitLabel(SectionBegin);
2134
2135 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002136 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002137}
2138
Eric Christopherdd1a0122013-09-13 00:35:05 +00002139// Public name handling.
2140// The format for the various pubnames:
2141//
2142// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2143// for the DIE that is named.
2144//
2145// gnu pubnames - offset/index value/name tuples where the offset is the offset
2146// into the CU and the index value is computed according to the type of value
2147// for the DIE that is named.
2148//
2149// For type units the offset is the offset of the skeleton DIE. For split dwarf
2150// it's the offset within the debug_info/debug_types dwo section, however, the
2151// reference in the pubname header doesn't change.
2152
2153/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00002154static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002155 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002156 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2157
2158 // We could have a specification DIE that has our most of our knowledge,
2159 // look for that now.
2160 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2161 if (SpecVal) {
2162 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2163 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2164 Linkage = dwarf::GIEL_EXTERNAL;
2165 } else if (Die->findAttribute(dwarf::DW_AT_external))
2166 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002167
2168 switch (Die->getTag()) {
2169 case dwarf::DW_TAG_class_type:
2170 case dwarf::DW_TAG_structure_type:
2171 case dwarf::DW_TAG_union_type:
2172 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002173 return dwarf::PubIndexEntryDescriptor(
2174 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2175 ? dwarf::GIEL_STATIC
2176 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002177 case dwarf::DW_TAG_typedef:
2178 case dwarf::DW_TAG_base_type:
2179 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002180 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002181 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002182 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002183 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002184 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002185 case dwarf::DW_TAG_constant:
2186 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002187 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002188 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002189 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2190 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002191 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002192 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002193 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002194}
2195
Eric Christopher5f93bb92013-09-09 20:03:17 +00002196/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002197///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002198void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Eric Christopherdd1a0122013-09-13 00:35:05 +00002199 const MCSection *PSec =
2200 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2201 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002202
David Blaikie0f55e832014-03-11 23:18:15 +00002203 emitDebugPubSection(GnuStyle, PSec, "Names", &DwarfUnit::getGlobalNames);
2204}
2205
2206void DwarfDebug::emitDebugPubSection(
2207 bool GnuStyle, const MCSection *PSec, StringRef Name,
2208 const StringMap<const DIE *> &(DwarfUnit::*Accessor)() const) {
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002209 for (const auto &NU : CUMap) {
2210 DwarfCompileUnit *TheU = NU.second;
David Blaikie55bb8ac2014-03-11 23:23:39 +00002211
2212 const auto &Globals = (TheU->*Accessor)();
2213
David Blaikiece2f1cb2014-03-11 23:35:06 +00002214 if (Globals.empty())
2215 continue;
2216
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002217 if (auto Skeleton = static_cast<DwarfCompileUnit *>(TheU->getSkeleton()))
2218 TheU = Skeleton;
David Blaikie2a80e442013-12-02 22:09:48 +00002219 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002220
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002221 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002222 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002223
Eric Christopherdd1a0122013-09-13 00:35:05 +00002224 // Emit the header.
David Blaikie0f55e832014-03-11 23:18:15 +00002225 Asm->OutStreamer.AddComment("Length of Public " + Name + " Info");
2226 MCSymbol *BeginLabel = Asm->GetTempSymbol("pub" + Name + "_begin", ID);
2227 MCSymbol *EndLabel = Asm->GetTempSymbol("pub" + Name + "_end", ID);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002228 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.
David Blaikie55bb8ac2014-03-11 23:23:39 +00002242 for (const auto &GI : Globals) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00002243 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 Blaikie0f55e832014-03-11 23:18:15 +00002272 emitDebugPubSection(GnuStyle, PSec, "Types", &DwarfUnit::getGlobalTypes);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002273}
2274
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002275// Emit strings into a string section.
Eric Christopherf8194852013-12-05 18:06:10 +00002276void DwarfFile::emitStrings(const MCSection *StrSection,
Eric Christophera5a79422013-12-09 23:32:48 +00002277 const MCSection *OffsetSection = NULL,
2278 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002279
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002280 if (StringPool.empty())
2281 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002282
Chris Lattner3d72a672010-03-09 23:38:23 +00002283 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002284 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002285
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002286 // Get all of the string pool entries and put them in an array by their ID so
2287 // we can sort them.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002288 SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64 > Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002289
Benjamin Kramer15596c72014-03-07 19:09:39 +00002290 for (const auto &I : StringPool)
2291 Entries.push_back(std::make_pair(I.second.second, &I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002292
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002293 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002294
Benjamin Kramer15596c72014-03-07 19:09:39 +00002295 for (const auto &Entry : Entries) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002296 // Emit a label for reference from debug information entries.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002297 Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002298
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002299 // Emit the string itself with a terminating null byte.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002300 Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
2301 Entry.second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002302 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002303
2304 // If we've got an offset section go ahead and emit that now as well.
2305 if (OffsetSection) {
2306 Asm->OutStreamer.SwitchSection(OffsetSection);
2307 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002308 unsigned size = 4; // FIXME: DWARF64 is 8.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002309 for (const auto &Entry : Entries) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002310 Asm->OutStreamer.EmitIntValue(offset, size);
Benjamin Kramer15596c72014-03-07 19:09:39 +00002311 offset += Entry.second->getKeyLength() + 1;
Eric Christopher2cbd5762013-01-07 19:32:41 +00002312 }
2313 }
Bill Wendling480ff322009-05-20 23:21:38 +00002314}
2315
Eric Christopher65132a82013-11-19 09:11:26 +00002316// Emit addresses into the section given.
Eric Christopherf8194852013-12-05 18:06:10 +00002317void DwarfFile::emitAddresses(const MCSection *AddrSection) {
Eric Christopher962c9082013-01-15 23:56:56 +00002318
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002319 if (AddressPool.empty())
2320 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002321
2322 // Start the dwarf addr section.
2323 Asm->OutStreamer.SwitchSection(AddrSection);
2324
David Blaikiece1960f2013-07-08 17:51:28 +00002325 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002326 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002327
Benjamin Kramer15596c72014-03-07 19:09:39 +00002328 for (const auto &I : AddressPool)
2329 Entries[I.second.Number] =
2330 I.second.TLS
2331 ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I.first)
2332 : MCSymbolRefExpr::Create(I.first, Asm->OutContext);
Eric Christopher962c9082013-01-15 23:56:56 +00002333
Benjamin Kramer15596c72014-03-07 19:09:39 +00002334 for (const MCExpr *Entry : Entries)
2335 Asm->OutStreamer.EmitValue(Entry, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002336}
2337
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002338// Emit visible names into a debug str section.
2339void DwarfDebug::emitDebugStr() {
Eric Christopherf8194852013-12-05 18:06:10 +00002340 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002341 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2342}
2343
Eric Christopher29e874d2014-03-07 22:40:37 +00002344void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
2345 const DotDebugLocEntry &Entry) {
2346 DIVariable DV(Entry.getVariable());
2347 if (Entry.isInt()) {
2348 DIBasicType BTy(DV.getType());
2349 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2350 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
2351 Streamer.EmitInt8(dwarf::DW_OP_consts, "DW_OP_consts");
2352 Streamer.EmitSLEB128(Entry.getInt());
2353 } else {
2354 Streamer.EmitInt8(dwarf::DW_OP_constu, "DW_OP_constu");
2355 Streamer.EmitULEB128(Entry.getInt());
2356 }
2357 } else if (Entry.isLocation()) {
2358 MachineLocation Loc = Entry.getLoc();
2359 if (!DV.hasComplexAddress())
2360 // Regular entry.
2361 Asm->EmitDwarfRegOp(Streamer, Loc, DV.isIndirect());
2362 else {
2363 // Complex address entry.
2364 unsigned N = DV.getNumAddrElements();
2365 unsigned i = 0;
2366 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
2367 if (Loc.getOffset()) {
2368 i = 2;
2369 Asm->EmitDwarfRegOp(Streamer, Loc, DV.isIndirect());
2370 Streamer.EmitInt8(dwarf::DW_OP_deref, "DW_OP_deref");
2371 Streamer.EmitInt8(dwarf::DW_OP_plus_uconst, "DW_OP_plus_uconst");
2372 Streamer.EmitSLEB128(DV.getAddrElement(1));
2373 } else {
2374 // If first address element is OpPlus then emit
2375 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
2376 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2377 Asm->EmitDwarfRegOp(Streamer, TLoc, DV.isIndirect());
2378 i = 2;
2379 }
2380 } else {
2381 Asm->EmitDwarfRegOp(Streamer, Loc, DV.isIndirect());
2382 }
2383
2384 // Emit remaining complex address elements.
2385 for (; i < N; ++i) {
2386 uint64_t Element = DV.getAddrElement(i);
2387 if (Element == DIBuilder::OpPlus) {
2388 Streamer.EmitInt8(dwarf::DW_OP_plus_uconst, "DW_OP_plus_uconst");
2389 Streamer.EmitULEB128(DV.getAddrElement(++i));
2390 } else if (Element == DIBuilder::OpDeref) {
2391 if (!Loc.isReg())
2392 Streamer.EmitInt8(dwarf::DW_OP_deref, "DW_OP_deref");
2393 } else
2394 llvm_unreachable("unknown Opcode found in complex address");
2395 }
2396 }
2397 }
2398 // else ... ignore constant fp. There is not any good way to
2399 // to represent them here in dwarf.
2400 // FIXME: ^
2401}
2402
Eric Christopher9046f942013-07-02 21:36:07 +00002403// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002404void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002405 if (DotDebugLocEntries.empty())
2406 return;
2407
Eric Christopher4887c8f2013-03-29 23:34:06 +00002408 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002409 I = DotDebugLocEntries.begin(),
2410 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002411 I != E; ++I) {
2412 DotDebugLocEntry &Entry = *I;
2413 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002414 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002415 }
2416
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002417 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002418 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002419 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002420 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002421 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2422 unsigned index = 1;
Eric Christophereeb51952014-03-06 19:51:16 +00002423 for (SmallVectorImpl<DotDebugLocEntry>::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002424 I = DotDebugLocEntries.begin(),
2425 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002426 I != E; ++I, ++index) {
Eric Christophereeb51952014-03-06 19:51:16 +00002427 const DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002428 if (Entry.isMerged())
2429 continue;
Eric Christopher29e874d2014-03-07 22:40:37 +00002430
Devang Patel9fc11702010-05-25 23:40:22 +00002431 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002432 Asm->OutStreamer.EmitIntValue(0, Size);
2433 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002434 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002435 } else {
Eric Christopher29e874d2014-03-07 22:40:37 +00002436 // Set up the range.
Eric Christopher25f06422013-07-03 22:40:18 +00002437 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2438 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002439 Asm->OutStreamer.AddComment("Loc expr size");
2440 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2441 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2442 Asm->EmitLabelDifference(end, begin, 2);
2443 Asm->OutStreamer.EmitLabel(begin);
Eric Christopher29e874d2014-03-07 22:40:37 +00002444 // Emit the entry.
2445 APByteStreamer Streamer(*Asm);
2446 emitDebugLocEntry(Streamer, Entry);
2447 // Close the range.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002448 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002449 }
2450 }
Bill Wendling480ff322009-05-20 23:21:38 +00002451}
2452
Richard Mitton21101b32013-09-19 23:21:01 +00002453struct ArangeSpan {
2454 const MCSymbol *Start, *End;
2455};
2456
2457// Emit a debug aranges section, containing a CU lookup for any
2458// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002459void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002460 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002461 Asm->OutStreamer.SwitchSection(
2462 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002463
Eric Christopher4287a492013-12-09 23:57:44 +00002464 typedef DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan> > SpansType;
Richard Mitton21101b32013-09-19 23:21:01 +00002465
2466 SpansType Spans;
2467
2468 // Build a list of sections used.
2469 std::vector<const MCSection *> Sections;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002470 for (const auto &it : SectionMap) {
2471 const MCSection *Section = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00002472 Sections.push_back(Section);
2473 }
2474
2475 // Sort the sections into order.
2476 // This is only done to ensure consistent output order across different runs.
2477 std::sort(Sections.begin(), Sections.end(), SectionSort);
2478
2479 // Build a set of address spans, sorted by CU.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002480 for (const MCSection *Section : Sections) {
Richard Mitton21101b32013-09-19 23:21:01 +00002481 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2482 if (List.size() < 2)
2483 continue;
2484
2485 // Sort the symbols by offset within the section.
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00002486 std::sort(List.begin(), List.end(),
2487 [&](const SymbolCU &A, const SymbolCU &B) {
2488 unsigned IA = A.Sym ? Asm->OutStreamer.GetSymbolOrder(A.Sym) : 0;
2489 unsigned IB = B.Sym ? Asm->OutStreamer.GetSymbolOrder(B.Sym) : 0;
2490
2491 // Symbols with no order assigned should be placed at the end.
2492 // (e.g. section end labels)
2493 if (IA == 0)
2494 return false;
2495 if (IB == 0)
2496 return true;
2497 return IA < IB;
2498 });
Richard Mitton21101b32013-09-19 23:21:01 +00002499
2500 // If we have no section (e.g. common), just write out
2501 // individual spans for each symbol.
2502 if (Section == NULL) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00002503 for (const SymbolCU &Cur : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00002504 ArangeSpan Span;
2505 Span.Start = Cur.Sym;
2506 Span.End = NULL;
2507 if (Cur.CU)
2508 Spans[Cur.CU].push_back(Span);
2509 }
2510 } else {
2511 // Build spans between each label.
2512 const MCSymbol *StartSym = List[0].Sym;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002513 for (size_t n = 1, e = List.size(); n < e; n++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002514 const SymbolCU &Prev = List[n - 1];
2515 const SymbolCU &Cur = List[n];
2516
2517 // Try and build the longest span we can within the same CU.
2518 if (Cur.CU != Prev.CU) {
2519 ArangeSpan Span;
2520 Span.Start = StartSym;
2521 Span.End = Cur.Sym;
2522 Spans[Prev.CU].push_back(Span);
2523 StartSym = Cur.Sym;
2524 }
2525 }
2526 }
2527 }
2528
Richard Mitton21101b32013-09-19 23:21:01 +00002529 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2530
2531 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00002532 std::vector<DwarfCompileUnit *> CUs;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002533 for (const auto &it : Spans) {
2534 DwarfCompileUnit *CU = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00002535 CUs.push_back(CU);
2536 }
2537
2538 // Sort the CU list (again, to ensure consistent output order).
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00002539 std::sort(CUs.begin(), CUs.end(), [](const DwarfUnit *A, const DwarfUnit *B) {
2540 return A->getUniqueID() < B->getUniqueID();
2541 });
Richard Mitton21101b32013-09-19 23:21:01 +00002542
2543 // Emit an arange table for each CU we used.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002544 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton21101b32013-09-19 23:21:01 +00002545 std::vector<ArangeSpan> &List = Spans[CU];
2546
2547 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002548 unsigned ContentSize =
2549 sizeof(int16_t) + // DWARF ARange version number
2550 sizeof(int32_t) + // Offset of CU in the .debug_info section
2551 sizeof(int8_t) + // Pointer Size (in bytes)
2552 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002553
2554 unsigned TupleSize = PtrSize * 2;
2555
2556 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002557 unsigned Padding =
2558 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00002559
2560 ContentSize += Padding;
2561 ContentSize += (List.size() + 1) * TupleSize;
2562
2563 // For each compile unit, write the list of spans it covers.
2564 Asm->OutStreamer.AddComment("Length of ARange Set");
2565 Asm->EmitInt32(ContentSize);
2566 Asm->OutStreamer.AddComment("DWARF Arange version number");
2567 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2568 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Eric Christopherd8667202013-12-30 17:22:27 +00002569 Asm->EmitSectionOffset(CU->getLocalLabelBegin(), CU->getLocalSectionSym());
Richard Mitton21101b32013-09-19 23:21:01 +00002570 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2571 Asm->EmitInt8(PtrSize);
2572 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2573 Asm->EmitInt8(0);
2574
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002575 Asm->OutStreamer.EmitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00002576
Benjamin Kramer15596c72014-03-07 19:09:39 +00002577 for (const ArangeSpan &Span : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00002578 Asm->EmitLabelReference(Span.Start, PtrSize);
2579
2580 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002581 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002582 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002583 } else {
2584 // For symbols without an end marker (e.g. common), we
2585 // write a single arange entry containing just that one symbol.
2586 uint64_t Size = SymSize[Span.Start];
2587 if (Size == 0)
2588 Size = 1;
2589
2590 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2591 }
Richard Mitton21101b32013-09-19 23:21:01 +00002592 }
2593
2594 Asm->OutStreamer.AddComment("ARange terminator");
2595 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2596 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2597 }
Bill Wendling480ff322009-05-20 23:21:38 +00002598}
2599
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002600// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002601void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002602 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002603 Asm->OutStreamer.SwitchSection(
2604 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002605
Eric Christopher0f63d062013-12-03 00:45:45 +00002606 // Size for our labels.
2607 unsigned char Size = Asm->getDataLayout().getPointerSize();
2608
2609 // Grab the specific ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002610 for (const auto &I : CUMap) {
2611 DwarfCompileUnit *TheCU = I.second;
Eric Christopher0f63d062013-12-03 00:45:45 +00002612
2613 // Emit a symbol so we can find the beginning of our ranges.
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002614 Asm->OutStreamer.EmitLabel(TheCU->getLabelRange());
Eric Christopher0f63d062013-12-03 00:45:45 +00002615
2616 // Iterate over the misc ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002617 for (const RangeSpanList &List : TheCU->getRangeLists()) {
Eric Christopherf8790642013-12-04 22:04:50 +00002618 // Emit our symbol so we can find the beginning of the range.
2619 Asm->OutStreamer.EmitLabel(List.getSym());
Eric Christopher0f63d062013-12-03 00:45:45 +00002620
Benjamin Kramer15596c72014-03-07 19:09:39 +00002621 for (const RangeSpan &Range : List.getRanges()) {
Eric Christopher0f63d062013-12-03 00:45:45 +00002622 const MCSymbol *Begin = Range.getStart();
2623 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002624 assert(Begin && "Range without a begin symbol?");
2625 assert(End && "Range without an end symbol?");
2626 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2627 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002628 }
2629
2630 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002631 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002632 Asm->OutStreamer.EmitIntValue(0, Size);
2633 }
Eric Christopher46e23432013-12-20 04:16:18 +00002634
2635 // Now emit a range for the CU itself.
Eric Christophera9a1d272014-02-27 07:44:45 +00002636 if (useCURanges() && TheCU->getRanges().size()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002637 Asm->OutStreamer.EmitLabel(
2638 Asm->GetTempSymbol("cu_ranges", TheCU->getUniqueID()));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002639 for (const RangeSpan &Range : TheCU->getRanges()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002640 const MCSymbol *Begin = Range.getStart();
2641 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002642 assert(Begin && "Range without a begin symbol?");
2643 assert(End && "Range without an end symbol?");
2644 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2645 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher46e23432013-12-20 04:16:18 +00002646 }
2647 // And terminate the list with two 0 values.
2648 Asm->OutStreamer.EmitIntValue(0, Size);
2649 Asm->OutStreamer.EmitIntValue(0, Size);
2650 }
Devang Patel12563b32010-04-16 23:33:45 +00002651 }
Bill Wendling480ff322009-05-20 23:21:38 +00002652}
2653
Eric Christopherd692c1d2012-12-11 19:42:09 +00002654// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002655
David Blaikie38fe6342014-01-09 04:28:46 +00002656void DwarfDebug::initSkeletonUnit(const DwarfUnit *U, DIE *Die,
2657 DwarfUnit *NewU) {
2658 NewU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
2659 U->getCUNode().getSplitDebugFilename());
2660
2661 // Relocate to the beginning of the addr_base section, else 0 for the
2662 // beginning of the one for this compile unit.
2663 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherceec7b02014-01-11 00:23:18 +00002664 NewU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base, DwarfAddrSectionSym);
David Blaikie38fe6342014-01-09 04:28:46 +00002665 else
2666 NewU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
2667
2668 if (!CompilationDir.empty())
2669 NewU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2670
2671 addGnuPubAttributes(NewU, Die);
2672
2673 SkeletonHolder.addUnit(NewU);
2674}
2675
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002676// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2677// 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 +00002678// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher5090d572013-12-10 00:40:03 +00002679// TODO: Implement DW_AT_ranges_base.
Eric Christopher4287a492013-12-09 23:57:44 +00002680DwarfCompileUnit *DwarfDebug::constructSkeletonCU(const DwarfCompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002681
2682 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +00002683 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
David Blaikief645f962014-01-09 03:23:41 +00002684 CU->getUniqueID(), Die, CU->getCUNode(), Asm, this, &SkeletonHolder);
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002685 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
2686 DwarfInfoSectionSym);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002687
David Blaikie2494fdb2014-02-14 22:41:51 +00002688 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002689
David Blaikie38fe6342014-01-09 04:28:46 +00002690 initSkeletonUnit(CU, Die, NewCU);
Eric Christopherc8a310e2012-12-10 23:34:43 +00002691
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002692 return NewCU;
2693}
2694
David Blaikie15ed5eb2014-01-10 01:38:41 +00002695// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_dwo_name,
2696// DW_AT_addr_base.
David Blaikie15632ae2014-02-12 00:31:30 +00002697DwarfTypeUnit *DwarfDebug::constructSkeletonTU(DwarfTypeUnit *TU) {
David Blaikie60e63862014-02-14 23:58:13 +00002698 DwarfCompileUnit &CU = static_cast<DwarfCompileUnit &>(
2699 *SkeletonHolder.getUnits()[TU->getCU().getUniqueID()]);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002700
2701 DIE *Die = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie60e63862014-02-14 23:58:13 +00002702 DwarfTypeUnit *NewTU =
2703 new DwarfTypeUnit(TU->getUniqueID(), Die, CU, Asm, this, &SkeletonHolder);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002704 NewTU->setTypeSignature(TU->getTypeSignature());
2705 NewTU->setType(NULL);
2706 NewTU->initSection(
2707 Asm->getObjFileLowering().getDwarfTypesSection(TU->getTypeSignature()));
David Blaikie60e63862014-02-14 23:58:13 +00002708 CU.applyStmtList(*Die);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002709
2710 initSkeletonUnit(TU, Die, NewTU);
2711 return NewTU;
2712}
2713
Eric Christopherd692c1d2012-12-11 19:42:09 +00002714// Emit the .debug_info.dwo section for separated dwarf. This contains the
2715// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002716void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002717 assert(useSplitDwarf() && "No split dwarf debug info?");
David Blaikie03073f72013-12-06 22:14:48 +00002718 InfoHolder.emitUnits(this,
Eric Christopher3c5a1912012-12-19 22:02:53 +00002719 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2720 DwarfAbbrevDWOSectionSym);
2721}
2722
2723// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2724// abbreviations for the .debug_info.dwo section.
2725void DwarfDebug::emitDebugAbbrevDWO() {
2726 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00002727 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002728}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002729
2730// Emit the .debug_str.dwo section for separated dwarf. This contains the
2731// string section and is identical in format to traditional .debug_str
2732// sections.
2733void DwarfDebug::emitDebugStrDWO() {
2734 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002735 const MCSection *OffSec =
2736 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002737 const MCSymbol *StrSym = DwarfStrSectionSym;
2738 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2739 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002740}
David Blaikie409dd9c2013-11-19 23:08:21 +00002741
David Blaikie15632ae2014-02-12 00:31:30 +00002742void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikief645f962014-01-09 03:23:41 +00002743 StringRef Identifier, DIE *RefDie,
2744 DICompositeType CTy) {
David Blaikie322d79b2014-01-31 19:52:26 +00002745 // Flag the type unit reference as a declaration so that if it contains
2746 // members (implicit special members, static data member definitions, member
2747 // declarations for definitions in this CU, etc) consumers don't get confused
2748 // and think this is a full definition.
David Blaikie15632ae2014-02-12 00:31:30 +00002749 CU.addFlag(RefDie, dwarf::DW_AT_declaration);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002750
David Blaikie47f615e2013-12-17 23:32:35 +00002751 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
Chandler Carruthb587ab62014-01-20 08:07:07 +00002752 if (TU) {
David Blaikie15632ae2014-02-12 00:31:30 +00002753 CU.addDIETypeSignature(RefDie, *TU);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002754 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00002755 }
2756
Chandler Carruthb587ab62014-01-20 08:07:07 +00002757 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikied696fac2014-02-12 00:32:05 +00002758 DwarfTypeUnit *NewTU = new DwarfTypeUnit(InfoHolder.getUnits().size(),
2759 UnitDie, CU, Asm, this, &InfoHolder);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002760 TU = NewTU;
2761 InfoHolder.addUnit(NewTU);
2762
2763 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie15632ae2014-02-12 00:31:30 +00002764 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00002765
2766 MD5 Hash;
2767 Hash.update(Identifier);
2768 // ... take the least significant 8 bytes and return those. Our MD5
2769 // implementation always returns its results in little endian, swap bytes
2770 // appropriately.
2771 MD5::MD5Result Result;
2772 Hash.final(Result);
2773 uint64_t Signature = *reinterpret_cast<support::ulittle64_t *>(Result + 8);
2774 NewTU->setTypeSignature(Signature);
2775 if (useSplitDwarf())
2776 NewTU->setSkeleton(constructSkeletonTU(NewTU));
David Blaikie60e63862014-02-14 23:58:13 +00002777 else
2778 CU.applyStmtList(*UnitDie);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002779
2780 NewTU->setType(NewTU->createTypeDIE(CTy));
2781
2782 NewTU->initSection(
2783 useSplitDwarf()
2784 ? Asm->getObjFileLowering().getDwarfTypesDWOSection(Signature)
2785 : Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2786
David Blaikie15632ae2014-02-12 00:31:30 +00002787 CU.addDIETypeSignature(RefDie, *NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00002788}
David Blaikie4bd13b72014-03-07 18:49:45 +00002789
2790void DwarfDebug::attachLowHighPC(DwarfCompileUnit *Unit, DIE *D,
2791 MCSymbol *Begin, MCSymbol *End) {
2792 Unit->addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
Adrian Prantl887e7072014-03-07 23:07:21 +00002793 if (DwarfVersion < 4)
David Blaikie4bd13b72014-03-07 18:49:45 +00002794 Unit->addLabelAddress(D, dwarf::DW_AT_high_pc, End);
2795 else
2796 Unit->addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
2797}