blob: 81934e1729778e9db34c3ccf22c53e4ee0c79fe8 [file] [log] [blame]
Bill Wendling2f921f82009-05-15 09:23:25 +00001//===-- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Devang Patel80ae3492009-08-28 23:24:31 +000014#define DEBUG_TYPE "dwarfdebug"
Bill Wendling2f921f82009-05-15 09:23:25 +000015#include "DwarfDebug.h"
Chris Lattner3f3fb972010-04-05 05:24:55 +000016#include "DIE.h"
Eric Christopher45731982013-08-08 23:45:55 +000017#include "DIEHash.h"
Eric Christopher4996c702011-11-07 09:24:32 +000018#include "DwarfAccelTable.h"
David Blaikie2c86a722013-12-02 19:33:15 +000019#include "DwarfUnit.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Statistic.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/Triple.h"
David Greene829b3e82009-08-19 21:52:55 +000024#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Chandler Carruth12664a02014-03-06 00:22:06 +000027#include "llvm/IR/DIBuilder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DataLayout.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000029#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Instructions.h"
31#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000032#include "llvm/IR/ValueHandle.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000033#include "llvm/MC/MCAsmInfo.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000034#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000035#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000036#include "llvm/MC/MCSymbol.h"
Devang Patel6c74a872010-04-27 19:46:33 +000037#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000038#include "llvm/Support/Debug.h"
David Majnemered89b5c2013-08-21 06:13:34 +000039#include "llvm/Support/Dwarf.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000040#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf5c834f2010-01-22 22:09:00 +000041#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000042#include "llvm/Support/LEB128.h"
Eric Christopher67646432013-07-26 17:02:41 +000043#include "llvm/Support/MD5.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000044#include "llvm/Support/Path.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000045#include "llvm/Support/Timer.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000046#include "llvm/Target/TargetFrameLowering.h"
47#include "llvm/Target/TargetLoweringObjectFile.h"
48#include "llvm/Target/TargetMachine.h"
49#include "llvm/Target/TargetOptions.h"
50#include "llvm/Target/TargetRegisterInfo.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000051using namespace llvm;
52
Eric Christopher7f2b5512013-07-23 22:16:41 +000053static cl::opt<bool>
54DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
55 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000056
Eric Christopher7f2b5512013-07-23 22:16:41 +000057static cl::opt<bool> UnknownLocations(
58 "use-unknown-locations", cl::Hidden,
59 cl::desc("Make an absence of debug location information explicit."),
60 cl::init(false));
Dan Gohman7421ae42010-05-07 01:08:53 +000061
Eric Christopherb4bef6d2013-11-19 09:04:36 +000062static cl::opt<bool> GenerateCUHash("generate-cu-hash", cl::Hidden,
63 cl::desc("Add the CU hash as the dwo_id."),
64 cl::init(false));
Eric Christopherd29614f2013-08-13 01:21:55 +000065
Eric Christopherdd1a0122013-09-13 00:35:05 +000066static cl::opt<bool>
67GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
68 cl::desc("Generate GNU-style pubnames and pubtypes"),
69 cl::init(false));
70
Eric Christopher02dbadb2014-02-14 01:26:55 +000071static cl::opt<bool> GenerateARangeSection("generate-arange-section",
72 cl::Hidden,
73 cl::desc("Generate dwarf aranges"),
74 cl::init(false));
75
Eric Christopher20b76a72012-08-23 22:36:40 +000076namespace {
Eric Christopherf07ee3a2014-01-27 23:50:03 +000077enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher20b76a72012-08-23 22:36:40 +000078}
Eric Christopher4996c702011-11-07 09:24:32 +000079
Eric Christopher7f2b5512013-07-23 22:16:41 +000080static cl::opt<DefaultOnOff>
81DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
82 cl::desc("Output prototype dwarf accelerator tables."),
83 cl::values(clEnumVal(Default, "Default for platform"),
84 clEnumVal(Enable, "Enabled"),
85 clEnumVal(Disable, "Disabled"), clEnumValEnd),
86 cl::init(Default));
Eric Christopher20b76a72012-08-23 22:36:40 +000087
Eric Christopher7f2b5512013-07-23 22:16:41 +000088static cl::opt<DefaultOnOff>
Eric Christopher7f2b5512013-07-23 22:16:41 +000089SplitDwarf("split-dwarf", cl::Hidden,
Eric Christopher5d008fe2013-12-04 23:24:28 +000090 cl::desc("Output DWARF5 split debug info."),
Eric Christopher7f2b5512013-07-23 22:16:41 +000091 cl::values(clEnumVal(Default, "Default for platform"),
92 clEnumVal(Enable, "Enabled"),
93 clEnumVal(Disable, "Disabled"), clEnumValEnd),
94 cl::init(Default));
Eric Christopher29424312012-11-12 22:22:20 +000095
Eric Christopher7da24882013-08-19 21:07:38 +000096static cl::opt<DefaultOnOff>
Eric Christopher4d36ca02013-08-26 23:24:35 +000097DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
98 cl::desc("Generate DWARF pubnames and pubtypes sections"),
99 cl::values(clEnumVal(Default, "Default for platform"),
100 clEnumVal(Enable, "Enabled"),
101 clEnumVal(Disable, "Disabled"), clEnumValEnd),
102 cl::init(Default));
Eric Christopher7da24882013-08-19 21:07:38 +0000103
Eric Christopher33ff6972013-11-21 23:46:41 +0000104static cl::opt<unsigned>
105DwarfVersionNumber("dwarf-version", cl::Hidden,
Eric Christophera5a79422013-12-09 23:32:48 +0000106 cl::desc("Generate DWARF for dwarf version."), cl::init(0));
Eric Christopher33ff6972013-11-21 23:46:41 +0000107
Eric Christopher46e23432013-12-20 04:16:18 +0000108static cl::opt<bool>
109DwarfCURanges("generate-dwarf-cu-ranges", cl::Hidden,
110 cl::desc("Generate DW_AT_ranges for compile units"),
111 cl::init(false));
112
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000113static const char *const DWARFGroupName = "DWARF Emission";
114static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000115
Bill Wendling2f921f82009-05-15 09:23:25 +0000116//===----------------------------------------------------------------------===//
117
Bill Wendling2f921f82009-05-15 09:23:25 +0000118namespace llvm {
119
Manman Renbe5576f2013-10-08 19:07:44 +0000120/// resolve - Look in the DwarfDebug map for the MDNode that
121/// corresponds to the reference.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000122template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
Manman Renbe5576f2013-10-08 19:07:44 +0000123 return DD->resolve(Ref);
124}
125
Nick Lewycky019d2552011-07-29 03:49:23 +0000126DIType DbgVariable::getType() const {
Devang Patelf20c4f72011-04-12 22:53:02 +0000127 DIType Ty = Var.getType();
128 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
129 // addresses instead.
130 if (Var.isBlockByrefVariable()) {
131 /* Byref variables, in Blocks, are declared by the programmer as
132 "SomeType VarName;", but the compiler creates a
133 __Block_byref_x_VarName struct, and gives the variable VarName
134 either the struct, or a pointer to the struct, as its type. This
135 is necessary for various behind-the-scenes things the compiler
136 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000137
Devang Patelf20c4f72011-04-12 22:53:02 +0000138 However, as far as the original *programmer* is concerned, the
139 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000140
Devang Patelf20c4f72011-04-12 22:53:02 +0000141 The following function dives into the __Block_byref_x_VarName
142 struct to find the original type of the variable. This will be
143 passed back to the code generating the type for the Debug
144 Information Entry for the variable 'VarName'. 'VarName' will then
145 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000146
Devang Patelf20c4f72011-04-12 22:53:02 +0000147 The original type 'SomeType' will be the type of the field named
148 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000149
Devang Patelf20c4f72011-04-12 22:53:02 +0000150 NOTE: In order for this to not completely fail on the debugger
151 side, the Debug Information Entry for the variable VarName needs to
152 have a DW_AT_location that tells the debugger how to unwind through
153 the pointers and __Block_byref_x_VarName struct to find the actual
154 value of the variable. The function addBlockByrefType does this. */
155 DIType subType = Ty;
Eric Christopher31b05762013-08-08 01:41:00 +0000156 uint16_t tag = Ty.getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000157
Eric Christopher9adc55f2013-09-04 19:53:21 +0000158 if (tag == dwarf::DW_TAG_pointer_type)
Manman Renbe5576f2013-10-08 19:07:44 +0000159 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher6a841382012-11-19 22:42:10 +0000160
Eric Christopher9adc55f2013-09-04 19:53:21 +0000161 DIArray Elements = DICompositeType(subType).getTypeArray();
Devang Patelf20c4f72011-04-12 22:53:02 +0000162 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikie5af2aca2013-11-18 23:57:26 +0000163 DIDerivedType DT(Elements.getElement(i));
Devang Patelf20c4f72011-04-12 22:53:02 +0000164 if (getName() == DT.getName())
Manman Renbe5576f2013-10-08 19:07:44 +0000165 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000166 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000167 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000168 return Ty;
169}
Bill Wendling2f921f82009-05-15 09:23:25 +0000170
Chris Lattnerf5d06362010-04-05 04:09:20 +0000171} // end llvm namespace
Bill Wendling2f921f82009-05-15 09:23:25 +0000172
Eric Christopher942f22c2014-01-11 00:28:12 +0000173/// Return Dwarf Version by checking module flags.
174static unsigned getDwarfVersionFromModule(const Module *M) {
Manman Ren8bfde892013-07-16 23:21:16 +0000175 Value *Val = M->getModuleFlag("Dwarf Version");
176 if (!Val)
Eric Christophere31e0722013-09-04 22:21:24 +0000177 return dwarf::DWARF_VERSION;
Manman Ren8bfde892013-07-16 23:21:16 +0000178 return cast<ConstantInt>(Val)->getZExtValue();
Manman Renac8062b2013-07-02 23:40:10 +0000179}
180
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000181DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
David Blaikie0504cda2013-12-05 07:43:55 +0000182 : Asm(A), MMI(Asm->MMI), FirstCU(0), SourceIdMap(DIEValueAllocator),
David Blaikie2666e242013-12-06 19:38:46 +0000183 PrevLabel(NULL), GlobalRangeCount(0),
Eric Christopher1bca60d2014-01-23 22:55:47 +0000184 InfoHolder(A, "info_string", DIEValueAllocator), HasCURanges(false),
Eric Christopher2037caf2014-01-28 00:49:26 +0000185 UsedNonDefaultText(false),
David Blaikie0504cda2013-12-05 07:43:55 +0000186 SkeletonHolder(A, "skel_string", DIEValueAllocator) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000187
Eric Christopher50effa02014-01-03 02:16:44 +0000188 DwarfInfoSectionSym = DwarfAbbrevSectionSym = DwarfStrSectionSym = 0;
Eric Christopher74804332013-02-07 21:19:50 +0000189 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = DwarfLineSectionSym = 0;
Eric Christopher55863be2013-04-07 03:43:09 +0000190 DwarfAddrSectionSym = 0;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000191 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = 0;
Devang Patel9fc11702010-05-25 23:40:22 +0000192 FunctionBeginSym = FunctionEndSym = 0;
Eric Christophera5a79422013-12-09 23:32:48 +0000193 CurFn = 0;
194 CurMI = 0;
Eric Christopherad9fe892012-04-02 17:58:52 +0000195
Adrian Prantl5bf1d002013-10-15 20:26:37 +0000196 // Turn on accelerator tables for Darwin by default, pubnames by
197 // default for non-Darwin, and handle split dwarf.
Eric Christopher203e12b2013-04-27 01:07:52 +0000198 bool IsDarwin = Triple(A->getTargetTriple()).isOSDarwin();
Eric Christopher4977f212012-08-23 22:36:36 +0000199
Eric Christopher574b5c82013-08-19 21:41:38 +0000200 if (DwarfAccelTables == Default)
201 HasDwarfAccelTables = IsDarwin;
202 else
Eric Christopher5297df02013-08-26 20:58:35 +0000203 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20b76a72012-08-23 22:36:40 +0000204
Eric Christophercdf218d2012-12-10 19:51:21 +0000205 if (SplitDwarf == Default)
206 HasSplitDwarf = false;
Eric Christopher29424312012-11-12 22:22:20 +0000207 else
Eric Christopher574b5c82013-08-19 21:41:38 +0000208 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopher29424312012-11-12 22:22:20 +0000209
Eric Christopher4d36ca02013-08-26 23:24:35 +0000210 if (DwarfPubSections == Default)
211 HasDwarfPubSections = !IsDarwin;
Eric Christopher574b5c82013-08-19 21:41:38 +0000212 else
Eric Christopher4d36ca02013-08-26 23:24:35 +0000213 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopher7da24882013-08-19 21:07:38 +0000214
Eric Christopher942f22c2014-01-11 00:28:12 +0000215 DwarfVersion = DwarfVersionNumber
216 ? DwarfVersionNumber
217 : getDwarfVersionFromModule(MMI->getModule());
Manman Renac8062b2013-07-02 23:40:10 +0000218
Dan Gohman6e681a52010-06-18 15:56:31 +0000219 {
220 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopher58f41952012-11-19 22:42:15 +0000221 beginModule();
Torok Edwinf8dba242010-04-07 10:44:46 +0000222 }
Bill Wendling2f921f82009-05-15 09:23:25 +0000223}
Bill Wendling2f921f82009-05-15 09:23:25 +0000224
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000225// Switch to the specified MCSection and emit an assembler
226// temporary label to it if SymbolStem is specified.
Eric Christopher7b30f2e42012-11-21 00:34:35 +0000227static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Eric Christophera7b61892011-11-07 09:18:38 +0000228 const char *SymbolStem = 0) {
229 Asm->OutStreamer.SwitchSection(Section);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000230 if (!SymbolStem)
231 return 0;
Eric Christophera7b61892011-11-07 09:18:38 +0000232
233 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
234 Asm->OutStreamer.EmitLabel(TmpSym);
235 return TmpSym;
236}
237
Eric Christopherf8194852013-12-05 18:06:10 +0000238DwarfFile::~DwarfFile() {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000239 for (DwarfUnit *DU : CUs)
240 delete DU;
David Blaikie72f1a3e2013-11-23 01:17:34 +0000241}
242
Eric Christopherf8194852013-12-05 18:06:10 +0000243MCSymbol *DwarfFile::getStringPoolSym() {
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000244 return Asm->GetTempSymbol(StringPref);
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000245}
246
Eric Christopherf8194852013-12-05 18:06:10 +0000247MCSymbol *DwarfFile::getStringPoolEntry(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000248 std::pair<MCSymbol *, unsigned> &Entry =
249 StringPool.GetOrCreateValue(Str).getValue();
250 if (Entry.first)
251 return Entry.first;
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000252
253 Entry.second = NextStringPoolNumber++;
Eric Christopher3bf29fd2012-12-27 02:14:01 +0000254 return Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
Chris Lattnerb7aa9522010-03-13 02:17:42 +0000255}
256
Eric Christopherf8194852013-12-05 18:06:10 +0000257unsigned DwarfFile::getStringPoolIndex(StringRef Str) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000258 std::pair<MCSymbol *, unsigned> &Entry =
259 StringPool.GetOrCreateValue(Str).getValue();
260 if (Entry.first)
261 return Entry.second;
Eric Christopher2cbd5762013-01-07 19:32:41 +0000262
263 Entry.second = NextStringPoolNumber++;
264 Entry.first = Asm->GetTempSymbol(StringPref, Entry.second);
265 return Entry.second;
266}
267
David Blaikief1a6dea2014-02-15 19:34:03 +0000268unsigned DwarfFile::getAddrPoolIndex(const MCSymbol *Sym, bool TLS) {
269 std::pair<AddrPool::iterator, bool> P = AddressPool.insert(
270 std::make_pair(Sym, AddressPoolEntry(NextAddrPoolNumber, TLS)));
David Blaikiea67de2b2013-06-28 18:55:13 +0000271 if (P.second)
272 ++NextAddrPoolNumber;
David Blaikief1a6dea2014-02-15 19:34:03 +0000273 return P.first->second.Number;
Eric Christopher962c9082013-01-15 23:56:56 +0000274}
275
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000276// Define a unique number for the abbreviation.
277//
Eric Christopherf8194852013-12-05 18:06:10 +0000278void DwarfFile::assignAbbrevNumber(DIEAbbrev &Abbrev) {
Bill Wendling2f921f82009-05-15 09:23:25 +0000279 // Check the set for priors.
David Blaikie0504cda2013-12-05 07:43:55 +0000280 DIEAbbrev *InSet = AbbreviationsSet.GetOrInsertNode(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000281
282 // If it's newly added.
283 if (InSet == &Abbrev) {
284 // Add to abbreviation list.
David Blaikie2d4e1122013-10-30 17:14:24 +0000285 Abbreviations.push_back(&Abbrev);
Bill Wendling2f921f82009-05-15 09:23:25 +0000286
287 // Assign the vector position + 1 as its number.
David Blaikie2d4e1122013-10-30 17:14:24 +0000288 Abbrev.setNumber(Abbreviations.size());
Bill Wendling2f921f82009-05-15 09:23:25 +0000289 } else {
290 // Assign existing abbreviation number.
291 Abbrev.setNumber(InSet->getNumber());
292 }
293}
294
Eric Christopherd9843b32011-11-10 19:25:34 +0000295static bool isObjCClass(StringRef Name) {
296 return Name.startswith("+") || Name.startswith("-");
297}
298
299static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000300 if (!isObjCClass(Name))
301 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000302
Benjamin Kramer260de742013-08-24 12:15:54 +0000303 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000304}
305
306static void getObjCClassCategory(StringRef In, StringRef &Class,
307 StringRef &Category) {
308 if (!hasObjCCategory(In)) {
309 Class = In.slice(In.find('[') + 1, In.find(' '));
310 Category = "";
311 return;
312 }
313
314 Class = In.slice(In.find('[') + 1, In.find('('));
315 Category = In.slice(In.find('[') + 1, In.find(' '));
316 return;
317}
318
319static StringRef getObjCMethodName(StringRef In) {
320 return In.slice(In.find(' ') + 1, In.find(']'));
321}
322
Richard Mittonc2508242013-10-03 22:07:08 +0000323// Helper for sorting sections into a stable output order.
324static bool SectionSort(const MCSection *A, const MCSection *B) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000325 std::string LA = (A ? A->getLabelBeginName() : "");
326 std::string LB = (B ? B->getLabelBeginName() : "");
327 return LA < LB;
Richard Mittonc2508242013-10-03 22:07:08 +0000328}
329
Eric Christopherd9843b32011-11-10 19:25:34 +0000330// Add the various names to the Dwarf accelerator table names.
Eric Christopher9cd26af2013-09-20 23:22:52 +0000331// TODO: Determine whether or not we should add names for programs
332// that do not have a DW_AT_name or DW_AT_linkage_name field - this
333// is only slightly different than the lookup of non-standard ObjC names.
Eric Christophera5a79422013-12-09 23:32:48 +0000334static void addSubprogramNames(DwarfUnit *TheU, DISubprogram SP, DIE *Die) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000335 if (!SP.isDefinition())
336 return;
David Blaikie2a80e442013-12-02 22:09:48 +0000337 TheU->addAccelName(SP.getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000338
339 // If the linkage name is different than the name, go ahead and output
340 // that as well into the name table.
341 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
David Blaikie2a80e442013-12-02 22:09:48 +0000342 TheU->addAccelName(SP.getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000343
344 // If this is an Objective-C selector name add it to the ObjC accelerator
345 // too.
346 if (isObjCClass(SP.getName())) {
347 StringRef Class, Category;
348 getObjCClassCategory(SP.getName(), Class, Category);
David Blaikie2a80e442013-12-02 22:09:48 +0000349 TheU->addAccelObjC(Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000350 if (Category != "")
David Blaikie2a80e442013-12-02 22:09:48 +0000351 TheU->addAccelObjC(Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000352 // Also add the base method name to the name table.
David Blaikie2a80e442013-12-02 22:09:48 +0000353 TheU->addAccelName(getObjCMethodName(SP.getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000354 }
355}
356
Manman Ren3eb9dff2013-09-09 19:05:21 +0000357/// isSubprogramContext - Return true if Context is either a subprogram
358/// or another context nested inside a subprogram.
359bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
360 if (!Context)
361 return false;
362 DIDescriptor D(Context);
363 if (D.isSubprogram())
364 return true;
365 if (D.isType())
Manman Ren116868e2013-09-09 19:47:11 +0000366 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren3eb9dff2013-09-09 19:05:21 +0000367 return false;
368}
369
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000370// Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
371// and DW_AT_high_pc attributes. If there are global variables in this
372// scope then create and insert DIEs for these variables.
Eric Christopher4287a492013-12-09 23:57:44 +0000373DIE *DwarfDebug::updateSubprogramScopeDIE(DwarfCompileUnit *SPCU,
374 DISubprogram SP) {
David Blaikie25bc7192013-11-15 23:13:08 +0000375 DIE *SPDie = SPCU->getDIE(SP);
Devang Patela37a95e2010-07-07 22:20:57 +0000376
Chris Lattner3a383cb2010-04-05 00:13:49 +0000377 assert(SPDie && "Unable to find subprogram DIE!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000378
Bill Wendlingf720bf62012-11-07 05:19:04 +0000379 // If we're updating an abstract DIE, then we will be adding the children and
380 // object pointer later on. But what we don't want to do is process the
381 // concrete DIE twice.
David Blaikie25bc7192013-11-15 23:13:08 +0000382 if (DIE *AbsSPDIE = AbstractSPDies.lookup(SP)) {
Bill Wendlingf720bf62012-11-07 05:19:04 +0000383 // Pick up abstract subprogram DIE.
David Blaikie2a80e442013-12-02 22:09:48 +0000384 SPDie =
385 SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram, *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000386 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_abstract_origin, AbsSPDIE);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000387 } else {
388 DISubprogram SPDecl = SP.getFunctionDeclaration();
389 if (!SPDecl.isSubprogram()) {
390 // There is not any need to generate specification DIE for a function
391 // defined at compile unit level. If a function is defined inside another
392 // function then gdb prefers the definition at top level and but does not
393 // expect specification DIE in parent function. So avoid creating
394 // specification DIE for a function defined inside a function.
Manman Renc50fa112013-10-10 18:40:01 +0000395 DIScope SPContext = resolve(SP.getContext());
396 if (SP.isDefinition() && !SPContext.isCompileUnit() &&
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000397 !SPContext.isFile() && !isSubprogramContext(SPContext)) {
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000398 SPCU->addFlag(SPDie, dwarf::DW_AT_declaration);
399
400 // Add arguments.
401 DICompositeType SPTy = SP.getType();
402 DIArray Args = SPTy.getTypeArray();
Eric Christopher31b05762013-08-08 01:41:00 +0000403 uint16_t SPTag = SPTy.getTag();
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000404 if (SPTag == dwarf::DW_TAG_subroutine_type)
Adrian Prantl69140d22014-02-25 22:27:14 +0000405 SPCU->constructSubprogramArguments(*SPDie, Args);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000406 DIE *SPDeclDie = SPDie;
David Blaikie2a80e442013-12-02 22:09:48 +0000407 SPDie = SPCU->createAndAddDIE(dwarf::DW_TAG_subprogram,
408 *SPCU->getUnitDie());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000409 SPCU->addDIEEntry(SPDie, dwarf::DW_AT_specification, SPDeclDie);
Bill Wendlingd9bb9b62012-11-07 04:42:18 +0000410 }
411 }
Devang Patela37a95e2010-07-07 22:20:57 +0000412 }
413
David Blaikie4bd13b72014-03-07 18:49:45 +0000414 attachLowHighPC(SPCU, SPDie, FunctionBeginSym, FunctionEndSym);
Eric Christopher0f63d062013-12-03 00:45:45 +0000415
Chris Lattner3a383cb2010-04-05 00:13:49 +0000416 const TargetRegisterInfo *RI = Asm->TM.getRegisterInfo();
417 MachineLocation Location(RI->getFrameRegister(*Asm->MF));
Devang Patelf20c4f72011-04-12 22:53:02 +0000418 SPCU->addAddress(SPDie, dwarf::DW_AT_frame_base, Location);
Devang Patel6efc8e52010-02-06 01:02:37 +0000419
Eric Christopherd9843b32011-11-10 19:25:34 +0000420 // Add name to the name table, we do this here because we're guaranteed
421 // to have concrete versions of our DW_TAG_subprogram nodes.
422 addSubprogramNames(SPCU, SP, SPDie);
Eric Christopher6a841382012-11-19 22:42:10 +0000423
Chris Lattner3a383cb2010-04-05 00:13:49 +0000424 return SPDie;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000425}
426
Manman Ren5b2f4b02013-09-11 19:40:28 +0000427/// Check whether we should create a DIE for the given Scope, return true
428/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000429bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
430 if (Scope->isAbstractScope())
431 return false;
432
Manman Ren5b2f4b02013-09-11 19:40:28 +0000433 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000434 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
435 if (Ranges.empty())
436 return true;
437
438 if (Ranges.size() > 1)
439 return false;
440
Manman Ren5b2f4b02013-09-11 19:40:28 +0000441 // We don't create a DIE if we have a single Range and the end label
442 // is null.
Manman Ren2312ed32013-09-10 18:40:41 +0000443 SmallVectorImpl<InsnRange>::const_iterator RI = Ranges.begin();
444 MCSymbol *End = getLabelAfterInsn(RI->second);
445 return !End;
446}
447
Eric Christophera5a79422013-12-09 23:32:48 +0000448static void addSectionLabel(AsmPrinter *Asm, DwarfUnit *U, DIE *D,
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000449 dwarf::Attribute A, const MCSymbol *L,
450 const MCSymbol *Sec) {
451 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
452 U->addSectionLabel(D, A, L);
453 else
454 U->addSectionDelta(D, A, L, Sec);
455}
456
Eric Christopher4287a492013-12-09 23:57:44 +0000457void DwarfDebug::addScopeRangeList(DwarfCompileUnit *TheCU, DIE *ScopeDIE,
Eric Christopherbe2513e2013-12-03 00:45:59 +0000458 const SmallVectorImpl<InsnRange> &Range) {
459 // Emit offset in .debug_range as a relocatable label. emitDIE will handle
460 // emitting it appropriately.
Eric Christopherf8790642013-12-04 22:04:50 +0000461 MCSymbol *RangeSym = Asm->GetTempSymbol("debug_ranges", GlobalRangeCount++);
Eric Christopherc31fe2d2013-12-05 00:36:17 +0000462 addSectionLabel(Asm, TheCU, ScopeDIE, dwarf::DW_AT_ranges, RangeSym,
463 DwarfDebugRangeSectionSym);
464
Eric Christopherf8790642013-12-04 22:04:50 +0000465 RangeSpanList List(RangeSym);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000466 for (const InsnRange &R : Range) {
467 RangeSpan Span(getLabelBeforeInsn(R.first), getLabelAfterInsn(R.second));
Chandler Carruth002da5d2014-03-02 04:08:41 +0000468 List.addRange(std::move(Span));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000469 }
470
471 // Add the range list to the set of ranges to be emitted.
Chandler Carruth002da5d2014-03-02 04:08:41 +0000472 TheCU->addRangeList(std::move(List));
Eric Christopherbe2513e2013-12-03 00:45:59 +0000473}
474
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000475// Construct new DW_TAG_lexical_block for this scope and attach
476// DW_AT_low_pc/DW_AT_high_pc labels.
Eric Christopher4287a492013-12-09 23:57:44 +0000477DIE *DwarfDebug::constructLexicalScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000478 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000479 if (isLexicalScopeDIENull(Scope))
480 return 0;
481
Devang Patel6c74a872010-04-27 19:46:33 +0000482 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_lexical_block);
483 if (Scope->isAbstractScope())
484 return ScopeDIE;
485
Eric Christopher0f63d062013-12-03 00:45:45 +0000486 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopherbe2513e2013-12-03 00:45:59 +0000487
Eric Christopherdc42ea82013-07-03 01:57:28 +0000488 // If we have multiple ranges, emit them into the range section.
Eric Christopher0f63d062013-12-03 00:45:45 +0000489 if (ScopeRanges.size() > 1) {
Eric Christopherbe2513e2013-12-03 00:45:59 +0000490 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
Devang Patel6c74a872010-04-27 19:46:33 +0000491 return ScopeDIE;
492 }
493
Eric Christopherdc42ea82013-07-03 01:57:28 +0000494 // Construct the address range for this DIE.
Eric Christopher0f63d062013-12-03 00:45:45 +0000495 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopher962c9082013-01-15 23:56:56 +0000496 MCSymbol *Start = getLabelBeforeInsn(RI->first);
497 MCSymbol *End = getLabelAfterInsn(RI->second);
Manman Ren2312ed32013-09-10 18:40:41 +0000498 assert(End && "End label should not be null!");
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000499
Chris Lattnere13c3722010-03-09 01:58:53 +0000500 assert(Start->isDefined() && "Invalid starting label for an inlined scope!");
501 assert(End->isDefined() && "Invalid end label for an inlined scope!");
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000502
Eric Christopher962c9082013-01-15 23:56:56 +0000503 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_low_pc, Start);
504 TheCU->addLabelAddress(ScopeDIE, dwarf::DW_AT_high_pc, End);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000505
506 return ScopeDIE;
507}
508
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000509// This scope represents inlined body of a function. Construct DIE to
510// represent this concrete inlined copy of the function.
Eric Christopher4287a492013-12-09 23:57:44 +0000511DIE *DwarfDebug::constructInlinedScopeDIE(DwarfCompileUnit *TheCU,
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000512 LexicalScope *Scope) {
Eric Christopher0f63d062013-12-03 00:45:45 +0000513 const SmallVectorImpl<InsnRange> &ScopeRanges = Scope->getRanges();
Eric Christopher1cdb63d2013-12-04 21:20:15 +0000514 assert(!ScopeRanges.empty() &&
Nick Lewycky654f5ce2011-10-26 22:55:33 +0000515 "LexicalScope does not have instruction markers!");
Devang Patel6c74a872010-04-27 19:46:33 +0000516
Devang Patelf098ce22011-07-27 00:34:13 +0000517 if (!Scope->getScopeNode())
518 return NULL;
519 DIScope DS(Scope->getScopeNode());
520 DISubprogram InlinedSP = getDISubprogram(DS);
Eric Christophere595bae2013-10-04 17:08:38 +0000521 DIE *OriginDIE = TheCU->getDIE(InlinedSP);
Devang Patelf098ce22011-07-27 00:34:13 +0000522 if (!OriginDIE) {
Bill Wendling10e0e2e2012-10-30 17:51:02 +0000523 DEBUG(dbgs() << "Unable to find original DIE for an inlined subprogram.");
Devang Patelf098ce22011-07-27 00:34:13 +0000524 return NULL;
525 }
526
Devang Patel73bc1722011-05-05 17:54:26 +0000527 DIE *ScopeDIE = new DIE(dwarf::DW_TAG_inlined_subroutine);
Manman Ren4c4b69c2013-10-11 23:58:05 +0000528 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_abstract_origin, OriginDIE);
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000529
Eric Christopherbe2513e2013-12-03 00:45:59 +0000530 // If we have multiple ranges, emit them into the range section.
531 if (ScopeRanges.size() > 1)
532 addScopeRangeList(TheCU, ScopeDIE, ScopeRanges);
533 else {
Eric Christopher0f63d062013-12-03 00:45:45 +0000534 SmallVectorImpl<InsnRange>::const_iterator RI = ScopeRanges.begin();
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000535 MCSymbol *StartLabel = getLabelBeforeInsn(RI->first);
536 MCSymbol *EndLabel = getLabelAfterInsn(RI->second);
537
538 if (StartLabel == 0 || EndLabel == 0)
539 llvm_unreachable("Unexpected Start and End labels for an inlined scope!");
540
541 assert(StartLabel->isDefined() &&
542 "Invalid starting label for an inlined scope!");
543 assert(EndLabel->isDefined() && "Invalid end label for an inlined scope!");
544
David Blaikie555e79a2014-03-07 22:00:56 +0000545 attachLowHighPC(TheCU, ScopeDIE, StartLabel, EndLabel);
Devang Patelf098ce22011-07-27 00:34:13 +0000546 }
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000547
548 InlinedSubprogramDIEs.insert(OriginDIE);
549
Eric Christopherf94eb2b2013-07-03 02:23:53 +0000550 // Add the call site information to the DIE.
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000551 DILocation DL(Scope->getInlinedAt());
David Blaikief2443192013-10-21 17:28:37 +0000552 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_file, None,
Manman Ren1e427202013-03-07 01:42:00 +0000553 getOrCreateSourceID(DL.getFilename(), DL.getDirectory(),
554 TheCU->getUniqueID()));
David Blaikief2443192013-10-21 17:28:37 +0000555 TheCU->addUInt(ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000556
Eric Christopher8dda5d02011-12-04 06:02:38 +0000557 // Add name to the name table, we do this here because we're guaranteed
558 // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
559 addSubprogramNames(TheCU, InlinedSP, ScopeDIE);
Eric Christopher6a841382012-11-19 22:42:10 +0000560
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000561 return ScopeDIE;
562}
563
Eric Christopher4287a492013-12-09 23:57:44 +0000564DIE *DwarfDebug::createScopeChildrenDIE(DwarfCompileUnit *TheCU,
565 LexicalScope *Scope,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000566 SmallVectorImpl<DIE *> &Children) {
567 DIE *ObjectPointer = NULL;
Devang Patel6c622ef2011-03-01 22:58:55 +0000568
569 // Collect arguments for current function.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000570 if (LScopes.isCurrentFunctionScope(Scope)) {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000571 for (DbgVariable *ArgDV : CurrentFnArguments)
572 if (ArgDV)
Eric Christopher6a841382012-11-19 22:42:10 +0000573 if (DIE *Arg =
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000574 TheCU->constructVariableDIE(*ArgDV, Scope->isAbstractScope())) {
Devang Patel6c622ef2011-03-01 22:58:55 +0000575 Children.push_back(Arg);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000576 if (ArgDV->isObjectPointer())
577 ObjectPointer = Arg;
Eric Christophere3417762012-09-12 23:36:19 +0000578 }
Devang Patel6c622ef2011-03-01 22:58:55 +0000579
Adrian Prantl69140d22014-02-25 22:27:14 +0000580 // If this is a variadic function, add an unspecified parameter.
Adrian Prantl3f49c892014-02-25 19:57:42 +0000581 DISubprogram SP(Scope->getScopeNode());
Adrian Prantl3f49c892014-02-25 19:57:42 +0000582 DIArray FnArgs = SP.getType().getTypeArray();
Eric Christopher73ffdb82014-02-26 02:50:56 +0000583 if (FnArgs.getElement(FnArgs.getNumElements() - 1)
584 .isUnspecifiedParameter()) {
Adrian Prantl3f49c892014-02-25 19:57:42 +0000585 DIE *Ellipsis = new DIE(dwarf::DW_TAG_unspecified_parameters);
586 Children.push_back(Ellipsis);
587 }
588 }
589
Eric Christopherf84354b2011-10-03 15:49:16 +0000590 // Collect lexical scope children first.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000591 for (DbgVariable *DV : ScopeVariables.lookup(Scope))
592 if (DIE *Variable = TheCU->constructVariableDIE(*DV,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000593 Scope->isAbstractScope())) {
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000594 Children.push_back(Variable);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000595 if (DV->isObjectPointer())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000596 ObjectPointer = Variable;
Eric Christopherc1c8a1b2012-09-21 22:18:52 +0000597 }
Benjamin Kramer15596c72014-03-07 19:09:39 +0000598 for (LexicalScope *LS : Scope->getChildren())
599 if (DIE *Nested = constructScopeDIE(TheCU, LS))
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000600 Children.push_back(Nested);
Manman Ren2312ed32013-09-10 18:40:41 +0000601 return ObjectPointer;
602}
603
604// Construct a DIE for this scope.
Eric Christopher4287a492013-12-09 23:57:44 +0000605DIE *DwarfDebug::constructScopeDIE(DwarfCompileUnit *TheCU,
606 LexicalScope *Scope) {
Manman Ren2312ed32013-09-10 18:40:41 +0000607 if (!Scope || !Scope->getScopeNode())
608 return NULL;
609
610 DIScope DS(Scope->getScopeNode());
611
612 SmallVector<DIE *, 8> Children;
613 DIE *ObjectPointer = NULL;
614 bool ChildrenCreated = false;
615
Manman Ren5b2f4b02013-09-11 19:40:28 +0000616 // We try to create the scope DIE first, then the children DIEs. This will
617 // avoid creating un-used children then removing them later when we find out
618 // the scope DIE is null.
Devang Patel3b548aa2010-03-08 20:52:55 +0000619 DIE *ScopeDIE = NULL;
620 if (Scope->getInlinedAt())
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000621 ScopeDIE = constructInlinedScopeDIE(TheCU, Scope);
Devang Patel3b548aa2010-03-08 20:52:55 +0000622 else if (DS.isSubprogram()) {
Devang Pateld10b2af2010-06-28 20:53:04 +0000623 ProcessedSPNodes.insert(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000624 if (Scope->isAbstractScope()) {
Eric Christophere595bae2013-10-04 17:08:38 +0000625 ScopeDIE = TheCU->getDIE(DS);
Devang Patela37a95e2010-07-07 22:20:57 +0000626 // Note down abstract DIE.
627 if (ScopeDIE)
628 AbstractSPDies.insert(std::make_pair(DS, ScopeDIE));
David Blaikiee26a3772013-11-18 23:59:04 +0000629 } else
David Blaikie25bc7192013-11-15 23:13:08 +0000630 ScopeDIE = updateSubprogramScopeDIE(TheCU, DISubprogram(DS));
David Blaikiee26a3772013-11-18 23:59:04 +0000631 } else {
Manman Ren5b2f4b02013-09-11 19:40:28 +0000632 // Early exit when we know the scope DIE is going to be null.
Manman Ren2312ed32013-09-10 18:40:41 +0000633 if (isLexicalScopeDIENull(Scope))
634 return NULL;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000635
636 // We create children here when we know the scope DIE is not going to be
637 // null and the children will be added to the scope DIE.
Manman Ren2312ed32013-09-10 18:40:41 +0000638 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
639 ChildrenCreated = true;
Manman Ren5b2f4b02013-09-11 19:40:28 +0000640
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000641 // There is no need to emit empty lexical block DIE.
David Blaikie684fc532013-05-06 23:33:07 +0000642 std::pair<ImportedEntityMap::const_iterator,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000643 ImportedEntityMap::const_iterator> Range =
644 std::equal_range(
645 ScopesWithImportedEntities.begin(),
646 ScopesWithImportedEntities.end(),
647 std::pair<const MDNode *, const MDNode *>(DS, (const MDNode *)0),
648 less_first());
David Blaikie684fc532013-05-06 23:33:07 +0000649 if (Children.empty() && Range.first == Range.second)
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000650 return NULL;
Devang Pateld2dfc5e2011-08-15 22:24:32 +0000651 ScopeDIE = constructLexicalScopeDIE(TheCU, Scope);
Manman Ren2312ed32013-09-10 18:40:41 +0000652 assert(ScopeDIE && "Scope DIE should not be null.");
Eric Christopher5fdd68e2013-06-24 23:20:02 +0000653 for (ImportedEntityMap::const_iterator i = Range.first; i != Range.second;
654 ++i)
David Blaikie4dd2de72013-05-08 06:01:38 +0000655 constructImportedEntityDIE(TheCU, i->second, ScopeDIE);
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000656 }
Eric Christopher6a841382012-11-19 22:42:10 +0000657
Benjamin Kramer892daba2013-08-24 11:55:49 +0000658 if (!ScopeDIE) {
Manman Ren2312ed32013-09-10 18:40:41 +0000659 assert(Children.empty() &&
660 "We create children only when the scope DIE is not null.");
Benjamin Kramer892daba2013-08-24 11:55:49 +0000661 return NULL;
662 }
Manman Ren2312ed32013-09-10 18:40:41 +0000663 if (!ChildrenCreated)
Manman Ren5b2f4b02013-09-11 19:40:28 +0000664 // We create children when the scope DIE is not null.
Manman Ren2312ed32013-09-10 18:40:41 +0000665 ObjectPointer = createScopeChildrenDIE(TheCU, Scope, Children);
Jim Grosbacha8683bb2010-07-21 21:21:52 +0000666
Devang Patel5f1b4cd2011-02-19 01:31:27 +0000667 // Add children
Benjamin Kramer15596c72014-03-07 19:09:39 +0000668 for (DIE *I : Children)
669 ScopeDIE->addChild(I);
Devang Patel04d2f2d2009-11-24 01:14:22 +0000670
Eric Christophere3417762012-09-12 23:36:19 +0000671 if (DS.isSubprogram() && ObjectPointer != NULL)
Manman Ren4c4b69c2013-10-11 23:58:05 +0000672 TheCU->addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, ObjectPointer);
Eric Christophere3417762012-09-12 23:36:19 +0000673
Eric Christopherd9843b32011-11-10 19:25:34 +0000674 return ScopeDIE;
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000675}
676
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000677// Look up the source id with the given directory and source file names.
678// If none currently exists, create a new id and insert it in the
679// SourceIds map. This can update DirectoryNames and SourceFileNames maps
680// as well.
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000681unsigned DwarfDebug::getOrCreateSourceID(StringRef FileName, StringRef DirName,
682 unsigned CUID) {
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000683 // If we print assembly, we can't separate .file entries according to
Manman Ren1e427202013-03-07 01:42:00 +0000684 // compile units. Thus all files will belong to the default compile unit.
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000685
686 // FIXME: add a better feature test than hasRawTextSupport. Even better,
687 // extend .file to support this.
Rafael Espindolab4eec1d2014-02-05 18:00:21 +0000688 if (Asm->OutStreamer.hasRawTextSupport())
Manman Ren1e427202013-03-07 01:42:00 +0000689 CUID = 0;
690
Devang Patel871d0b12010-09-16 20:57:49 +0000691 // If FE did not provide a file name, then assume stdin.
692 if (FileName.empty())
Manman Ren1e427202013-03-07 01:42:00 +0000693 return getOrCreateSourceID("<stdin>", StringRef(), CUID);
Devang Patele01b75c2011-03-24 20:30:50 +0000694
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000695 // TODO: this might not belong here. See if we can factor this better.
696 if (DirName == CompilationDir)
697 DirName = "";
698
Manman Ren1e427202013-03-07 01:42:00 +0000699 // FileIDCUMap stores the current ID for the given compile unit.
700 unsigned SrcId = FileIDCUMap[CUID] + 1;
Devang Patel871d0b12010-09-16 20:57:49 +0000701
Manman Ren1e427202013-03-07 01:42:00 +0000702 // We look up the CUID/file/dir by concatenating them with a zero byte.
Benjamin Kramer71b19732012-03-11 14:56:26 +0000703 SmallString<128> NamePair;
Manman Ren5b22f9f2013-04-06 01:02:38 +0000704 NamePair += utostr(CUID);
Manman Ren1e427202013-03-07 01:42:00 +0000705 NamePair += '\0';
Benjamin Kramer71b19732012-03-11 14:56:26 +0000706 NamePair += DirName;
707 NamePair += '\0'; // Zero bytes are not allowed in paths.
708 NamePair += FileName;
709
710 StringMapEntry<unsigned> &Ent = SourceIdMap.GetOrCreateValue(NamePair, SrcId);
711 if (Ent.getValue() != SrcId)
712 return Ent.getValue();
Bill Wendling2b128d72009-05-20 23:19:06 +0000713
Manman Ren1e427202013-03-07 01:42:00 +0000714 FileIDCUMap[CUID] = SrcId;
Rafael Espindola67c6ab82010-11-18 02:04:25 +0000715 // Print out a .file directive to specify files for .loc directives.
Manman Ren1e427202013-03-07 01:42:00 +0000716 Asm->OutStreamer.EmitDwarfFileDirective(SrcId, DirName, FileName, CUID);
Bill Wendling2b128d72009-05-20 23:19:06 +0000717
718 return SrcId;
719}
720
Eric Christophera5a79422013-12-09 23:32:48 +0000721void DwarfDebug::addGnuPubAttributes(DwarfUnit *U, DIE *D) const {
David Blaikie3c842622013-12-04 21:31:26 +0000722 if (!GenerateGnuPubSections)
723 return;
724
David Blaikie47c254b2014-03-06 05:47:39 +0000725 U->addFlag(D, dwarf::DW_AT_GNU_pubnames);
David Blaikie3c842622013-12-04 21:31:26 +0000726}
727
Eric Christopher4287a492013-12-09 23:57:44 +0000728// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000729// DW_TAG_compile_unit.
Eric Christopher4287a492013-12-09 23:57:44 +0000730DwarfCompileUnit *DwarfDebug::constructDwarfCompileUnit(DICompileUnit DIUnit) {
Devang Patel2d9caf92009-11-25 17:36:49 +0000731 StringRef FN = DIUnit.getFilename();
Nick Lewyckyd1ee7f82011-11-02 20:55:33 +0000732 CompilationDir = DIUnit.getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000733
734 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +0000735 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
736 InfoHolder.getUnits().size(), Die, DIUnit, Asm, this, &InfoHolder);
David Blaikie2666e242013-12-06 19:38:46 +0000737 InfoHolder.addUnit(NewCU);
738
Manman Ren1e427202013-03-07 01:42:00 +0000739 FileIDCUMap[NewCU->getUniqueID()] = 0;
Manman Ren1e427202013-03-07 01:42:00 +0000740
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000741 NewCU->addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
Devang Patelf20c4f72011-04-12 22:53:02 +0000742 NewCU->addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
743 DIUnit.getLanguage());
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000744 NewCU->addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000745
Eric Christopher52ce7182013-04-09 19:23:15 +0000746 if (!useSplitDwarf()) {
David Blaikie2494fdb2014-02-14 22:41:51 +0000747 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000748
749 // If we're using split dwarf the compilation dir is going to be in the
750 // skeleton CU and so we don't need to duplicate it here.
751 if (!CompilationDir.empty())
752 NewCU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
753
David Blaikie3c842622013-12-04 21:31:26 +0000754 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000755 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000756
Bill Wendling2b128d72009-05-20 23:19:06 +0000757 if (DIUnit.isOptimized())
Eric Christopherbb69a272012-08-24 01:14:27 +0000758 NewCU->addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000759
Devang Patel2d9caf92009-11-25 17:36:49 +0000760 StringRef Flags = DIUnit.getFlags();
761 if (!Flags.empty())
Nick Lewyckyd59c0ca2011-10-27 06:44:11 +0000762 NewCU->addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000763
Nick Lewycky479a8fe2011-10-17 23:27:36 +0000764 if (unsigned RVer = DIUnit.getRunTimeVersion())
Devang Patelf20c4f72011-04-12 22:53:02 +0000765 NewCU->addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000766 dwarf::DW_FORM_data1, RVer);
Bill Wendling2b128d72009-05-20 23:19:06 +0000767
Devang Patel1a0df9a2010-05-10 22:49:55 +0000768 if (!FirstCU)
769 FirstCU = NewCU;
Eric Christopher7a2cdf72013-02-05 07:31:55 +0000770
Eric Christopherd4368fd2014-01-02 21:03:28 +0000771 if (useSplitDwarf()) {
772 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection(),
773 DwarfInfoDWOSectionSym);
Eric Christopherd8667202013-12-30 17:22:27 +0000774 NewCU->setSkeleton(constructSkeletonCU(NewCU));
Eric Christopherd4368fd2014-01-02 21:03:28 +0000775 } else
776 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
777 DwarfInfoSectionSym);
Eric Christopherd039baa2013-12-30 03:40:32 +0000778
David Blaikie5a152402013-11-15 23:52:02 +0000779 CUMap.insert(std::make_pair(DIUnit, NewCU));
Manman Rence20d462013-10-29 22:57:10 +0000780 CUDieMap.insert(std::make_pair(Die, NewCU));
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000781 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000782}
783
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000784// Construct subprogram DIE.
Eric Christopher4287a492013-12-09 23:57:44 +0000785void DwarfDebug::constructSubprogramDIE(DwarfCompileUnit *TheCU,
786 const MDNode *N) {
Eric Christopherffbc4de2013-10-18 01:57:30 +0000787 // FIXME: We should only call this routine once, however, during LTO if a
788 // program is defined in multiple CUs we could end up calling it out of
789 // beginModule as we walk the CUs.
790
Eric Christopher4287a492013-12-09 23:57:44 +0000791 DwarfCompileUnit *&CURef = SPMap[N];
Eric Christopherffbc4de2013-10-18 01:57:30 +0000792 if (CURef)
793 return;
794 CURef = TheCU;
Rafael Espindola6cf4e832011-11-04 19:00:29 +0000795
Devang Patel80ae3492009-08-28 23:24:31 +0000796 DISubprogram SP(N);
Bill Wendling2b128d72009-05-20 23:19:06 +0000797 if (!SP.isDefinition())
798 // This is a method declaration which will be handled while constructing
799 // class type.
Devang Patel0751a282009-06-26 01:49:18 +0000800 return;
Bill Wendling2b128d72009-05-20 23:19:06 +0000801
Devang Patel89543712011-08-15 17:24:54 +0000802 DIE *SubprogramDie = TheCU->getOrCreateSubprogramDIE(SP);
Stuart Hastings4bd3dd92010-04-06 21:38:29 +0000803
Eric Christopherba506db2013-09-09 20:03:20 +0000804 // Expose as a global name.
Eric Christopher2c8b7902013-10-17 02:06:06 +0000805 TheCU->addGlobalName(SP.getName(), SubprogramDie, resolve(SP.getContext()));
Bill Wendling2b128d72009-05-20 23:19:06 +0000806}
807
Eric Christopher4287a492013-12-09 23:57:44 +0000808void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikief55abea2013-04-22 06:12:31 +0000809 const MDNode *N) {
David Blaikie1fd43652013-05-07 21:35:53 +0000810 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000811 assert(Module.Verify());
David Blaikie684fc532013-05-06 23:33:07 +0000812 if (DIE *D = TheCU->getOrCreateContextDIE(Module.getContext()))
David Blaikie4dd2de72013-05-08 06:01:38 +0000813 constructImportedEntityDIE(TheCU, Module, D);
David Blaikie684fc532013-05-06 23:33:07 +0000814}
815
Eric Christopher4287a492013-12-09 23:57:44 +0000816void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
817 const MDNode *N, DIE *Context) {
David Blaikie1fd43652013-05-07 21:35:53 +0000818 DIImportedEntity Module(N);
David Blaikie5e390e42014-02-04 01:23:52 +0000819 assert(Module.Verify());
David Blaikie4dd2de72013-05-08 06:01:38 +0000820 return constructImportedEntityDIE(TheCU, Module, Context);
David Blaikie684fc532013-05-06 23:33:07 +0000821}
822
Eric Christopher4287a492013-12-09 23:57:44 +0000823void DwarfDebug::constructImportedEntityDIE(DwarfCompileUnit *TheCU,
David Blaikie1fd43652013-05-07 21:35:53 +0000824 const DIImportedEntity &Module,
David Blaikie684fc532013-05-06 23:33:07 +0000825 DIE *Context) {
826 assert(Module.Verify() &&
827 "Use one of the MDNode * overloads to handle invalid metadata");
828 assert(Context && "Should always have a context for an imported_module");
David Blaikie1fd43652013-05-07 21:35:53 +0000829 DIE *IMDie = new DIE(Module.getTag());
David Blaikief55abea2013-04-22 06:12:31 +0000830 TheCU->insertDIE(Module, IMDie);
David Blaikie1fd43652013-05-07 21:35:53 +0000831 DIE *EntityDie;
832 DIDescriptor Entity = Module.getEntity();
833 if (Entity.isNameSpace())
834 EntityDie = TheCU->getOrCreateNameSpace(DINameSpace(Entity));
835 else if (Entity.isSubprogram())
836 EntityDie = TheCU->getOrCreateSubprogramDIE(DISubprogram(Entity));
David Blaikie3b6038b2013-05-08 06:01:41 +0000837 else if (Entity.isType())
838 EntityDie = TheCU->getOrCreateTypeDIE(DIType(Entity));
David Blaikie1fd43652013-05-07 21:35:53 +0000839 else
David Blaikie3b6038b2013-05-08 06:01:41 +0000840 EntityDie = TheCU->getDIE(Entity);
David Blaikief55abea2013-04-22 06:12:31 +0000841 unsigned FileID = getOrCreateSourceID(Module.getContext().getFilename(),
842 Module.getContext().getDirectory(),
843 TheCU->getUniqueID());
David Blaikief2443192013-10-21 17:28:37 +0000844 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_file, None, FileID);
845 TheCU->addUInt(IMDie, dwarf::DW_AT_decl_line, None, Module.getLineNumber());
Manman Ren4c4b69c2013-10-11 23:58:05 +0000846 TheCU->addDIEEntry(IMDie, dwarf::DW_AT_import, EntityDie);
David Blaikiee63d5d12013-05-20 22:50:35 +0000847 StringRef Name = Module.getName();
848 if (!Name.empty())
849 TheCU->addString(IMDie, dwarf::DW_AT_name, Name);
David Blaikie684fc532013-05-06 23:33:07 +0000850 Context->addChild(IMDie);
David Blaikief55abea2013-04-22 06:12:31 +0000851}
852
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000853// Emit all Dwarf sections that should come prior to the content. Create
854// global DIEs and emit initial debug info sections. This is invoked by
855// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000856void DwarfDebug::beginModule() {
Devang Patel6c74a872010-04-27 19:46:33 +0000857 if (DisableDebugInfoPrinting)
858 return;
859
Eric Christopher58f41952012-11-19 22:42:15 +0000860 const Module *M = MMI->getModule();
861
Nick Lewycky019d2552011-07-29 03:49:23 +0000862 // If module has named metadata anchors then use them, otherwise scan the
863 // module using debug info finder to collect debug info.
Devang Patele02e5852011-05-03 16:45:22 +0000864 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiedc69ebb2013-03-11 23:39:23 +0000865 if (!CU_Nodes)
Devang Patel07bb9ee2011-08-15 23:47:24 +0000866 return;
Manman Ren60352032013-09-05 18:48:31 +0000867 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patele02e5852011-05-03 16:45:22 +0000868
David Blaikiedc69ebb2013-03-11 23:39:23 +0000869 // Emit initial sections so we can reference labels later.
870 emitSectionLabels();
871
Benjamin Kramer15596c72014-03-07 19:09:39 +0000872 for (MDNode *N : CU_Nodes->operands()) {
873 DICompileUnit CUNode(N);
Eric Christopher4287a492013-12-09 23:57:44 +0000874 DwarfCompileUnit *CU = constructDwarfCompileUnit(CUNode);
David Blaikie1fd43652013-05-07 21:35:53 +0000875 DIArray ImportedEntities = CUNode.getImportedEntities();
876 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikie684fc532013-05-06 23:33:07 +0000877 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie1fd43652013-05-07 21:35:53 +0000878 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
879 ImportedEntities.getElement(i)));
David Blaikie684fc532013-05-06 23:33:07 +0000880 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramerb12cf012013-08-24 12:54:27 +0000881 ScopesWithImportedEntities.end(), less_first());
David Blaikiedc69ebb2013-03-11 23:39:23 +0000882 DIArray GVs = CUNode.getGlobalVariables();
883 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
David Blaikiea781b25b2013-11-17 21:55:13 +0000884 CU->createGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000885 DIArray SPs = CUNode.getSubprograms();
886 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
887 constructSubprogramDIE(CU, SPs.getElement(i));
888 DIArray EnumTypes = CUNode.getEnumTypes();
889 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
890 CU->getOrCreateTypeDIE(EnumTypes.getElement(i));
891 DIArray RetainedTypes = CUNode.getRetainedTypes();
892 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
893 CU->getOrCreateTypeDIE(RetainedTypes.getElement(i));
David Blaikief55abea2013-04-22 06:12:31 +0000894 // Emit imported_modules last so that the relevant context is already
895 // available.
David Blaikie1fd43652013-05-07 21:35:53 +0000896 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
897 constructImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiedc69ebb2013-03-11 23:39:23 +0000898 }
Eric Christopher6a841382012-11-19 22:42:10 +0000899
Chris Lattner7cfa70e2010-04-05 02:19:28 +0000900 // Tell MMI that we have debug info.
901 MMI->setDebugInfoAvailability(true);
Eric Christopher6a841382012-11-19 22:42:10 +0000902
Bill Wendling2b128d72009-05-20 23:19:06 +0000903 // Prime section data.
Richard Mitton21101b32013-09-19 23:21:01 +0000904 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendling2b128d72009-05-20 23:19:06 +0000905}
906
Eric Christopher960ac372012-11-22 00:59:49 +0000907// Attach DW_AT_inline attribute with inlined subprogram DIEs.
908void DwarfDebug::computeInlinedDIEs() {
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000909 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000910 for (DIE *ISP : InlinedSubprogramDIEs)
David Blaikief2443192013-10-21 17:28:37 +0000911 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Benjamin Kramer15596c72014-03-07 19:09:39 +0000912
913 for (const auto &AI : AbstractSPDies) {
914 DIE *ISP = AI.second;
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000915 if (InlinedSubprogramDIEs.count(ISP))
916 continue;
David Blaikief2443192013-10-21 17:28:37 +0000917 FirstCU->addUInt(ISP, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
Rafael Espindolae7cc8bf2011-11-12 01:57:54 +0000918 }
Eric Christopher960ac372012-11-22 00:59:49 +0000919}
920
921// Collect info for variables that were optimized out.
922void DwarfDebug::collectDeadVariables() {
923 const Module *M = MMI->getModule();
Eric Christopher960ac372012-11-22 00:59:49 +0000924
925 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
Benjamin Kramer15596c72014-03-07 19:09:39 +0000926 for (MDNode *N : CU_Nodes->operands()) {
927 DICompileUnit TheCU(N);
Eric Christopher960ac372012-11-22 00:59:49 +0000928 DIArray Subprograms = TheCU.getSubprograms();
929 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopher735401c2012-11-27 00:13:51 +0000930 DISubprogram SP(Subprograms.getElement(i));
Eric Christophera6c38a32013-10-15 23:31:38 +0000931 if (ProcessedSPNodes.count(SP) != 0)
932 continue;
933 if (!SP.isSubprogram())
934 continue;
935 if (!SP.isDefinition())
936 continue;
Eric Christopher735401c2012-11-27 00:13:51 +0000937 DIArray Variables = SP.getVariables();
Eric Christophera6c38a32013-10-15 23:31:38 +0000938 if (Variables.getNumElements() == 0)
939 continue;
Eric Christopher960ac372012-11-22 00:59:49 +0000940
Eric Christopher735401c2012-11-27 00:13:51 +0000941 // Construct subprogram DIE and add variables DIEs.
Eric Christopher4287a492013-12-09 23:57:44 +0000942 DwarfCompileUnit *SPCU =
943 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
Eric Christopher735401c2012-11-27 00:13:51 +0000944 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopherc798d8a2013-10-22 00:22:39 +0000945 // FIXME: See the comment in constructSubprogramDIE about duplicate
946 // subprogram DIEs.
947 constructSubprogramDIE(SPCU, SP);
948 DIE *SPDIE = SPCU->getDIE(SP);
Eric Christopher735401c2012-11-27 00:13:51 +0000949 for (unsigned vi = 0, ve = Variables.getNumElements(); vi != ve; ++vi) {
950 DIVariable DV(Variables.getElement(vi));
Eric Christophera6c38a32013-10-15 23:31:38 +0000951 if (!DV.isVariable())
952 continue;
Manman Renb3388602013-10-05 01:43:03 +0000953 DbgVariable NewVar(DV, NULL, this);
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000954 if (DIE *VariableDIE = SPCU->constructVariableDIE(NewVar, false))
Eric Christophera6c38a32013-10-15 23:31:38 +0000955 SPDIE->addChild(VariableDIE);
Eric Christopher735401c2012-11-27 00:13:51 +0000956 }
Eric Christopher960ac372012-11-22 00:59:49 +0000957 }
958 }
959 }
Eric Christopher960ac372012-11-22 00:59:49 +0000960}
961
962void DwarfDebug::finalizeModuleInfo() {
963 // Collect info for variables that were optimized out.
964 collectDeadVariables();
965
966 // Attach DW_AT_inline attribute with inlined subprogram DIEs.
967 computeInlinedDIEs();
Devang Patelf6eeaeb2009-11-10 23:06:00 +0000968
Eric Christopherad10cb52013-12-04 23:24:38 +0000969 // Handle anything that needs to be done on a per-unit basis after
970 // all other generation.
Benjamin Kramer15596c72014-03-07 19:09:39 +0000971 for (DwarfUnit *TheU : getUnits()) {
Eric Christopher60eb7692013-08-12 20:27:48 +0000972 // Emit DW_AT_containing_type attribute to connect types with their
973 // vtable holding type.
David Blaikie2a80e442013-12-02 22:09:48 +0000974 TheU->constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000975
Eric Christopher46e23432013-12-20 04:16:18 +0000976 // Add CU specific attributes if we need to add any.
977 if (TheU->getUnitDie()->getTag() == dwarf::DW_TAG_compile_unit) {
978 // If we're splitting the dwarf out now that we've got the entire
Eric Christopherd8667202013-12-30 17:22:27 +0000979 // CU then add the dwo id to it.
980 DwarfCompileUnit *SkCU =
981 static_cast<DwarfCompileUnit *>(TheU->getSkeleton());
Eric Christopher46e23432013-12-20 04:16:18 +0000982 if (useSplitDwarf()) {
983 // This should be a unique identifier when we want to build .dwp files.
984 uint64_t ID = 0;
985 if (GenerateCUHash) {
Eric Christopher420569b2014-02-20 02:50:45 +0000986 DIEHash CUHash(Asm);
Eric Christopher46e23432013-12-20 04:16:18 +0000987 ID = CUHash.computeCUSignature(*TheU->getUnitDie());
988 }
989 TheU->addUInt(TheU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
990 dwarf::DW_FORM_data8, ID);
Eric Christopher46e23432013-12-20 04:16:18 +0000991 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
992 dwarf::DW_FORM_data8, ID);
Eric Christopherd29614f2013-08-13 01:21:55 +0000993 }
Eric Christopherd8667202013-12-30 17:22:27 +0000994
Eric Christopher39cde8c2014-01-14 22:44:17 +0000995 // If we have code split among multiple sections or we've requested
996 // it then emit a DW_AT_ranges attribute on the unit that will remain
997 // in the .o file, otherwise add a DW_AT_low_pc.
Eric Christopherd8667202013-12-30 17:22:27 +0000998 // FIXME: Also add a high pc if we can.
Eric Christopher39cde8c2014-01-14 22:44:17 +0000999 // FIXME: We should use ranges if we have multiple compile units or
1000 // allow reordering of code ala .subsections_via_symbols in mach-o.
Eric Christopherd8667202013-12-30 17:22:27 +00001001 DwarfCompileUnit *U = SkCU ? SkCU : static_cast<DwarfCompileUnit *>(TheU);
Eric Christopher8873ada2014-01-29 22:22:56 +00001002 if (useCURanges() && TheU->getRanges().size()) {
Eric Christopherd8667202013-12-30 17:22:27 +00001003 addSectionLabel(Asm, U, U->getUnitDie(), dwarf::DW_AT_ranges,
1004 Asm->GetTempSymbol("cu_ranges", U->getUniqueID()),
1005 DwarfDebugRangeSectionSym);
Eric Christopher8873ada2014-01-29 22:22:56 +00001006
1007 // A DW_AT_low_pc attribute may also be specified in combination with
1008 // DW_AT_ranges to specify the default base address for use in location
1009 // lists (see Section 2.6.2) and range lists (see Section 2.17.3).
1010 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1011 0);
1012 } else
Eric Christophercf48ade2014-01-24 11:52:53 +00001013 U->addUInt(U->getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr,
1014 0);
Eric Christopher60eb7692013-08-12 20:27:48 +00001015 }
1016 }
1017
1018 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +00001019 InfoHolder.computeSizeAndOffsets();
1020 if (useSplitDwarf())
1021 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +00001022}
1023
1024void DwarfDebug::endSections() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001025 // Filter labels by section.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001026 for (const SymbolCU &SCU : ArangeLabels) {
Richard Mitton21101b32013-09-19 23:21:01 +00001027 if (SCU.Sym->isInSection()) {
1028 // Make a note of this symbol and it's section.
1029 const MCSection *Section = &SCU.Sym->getSection();
1030 if (!Section->getKind().isMetadata())
1031 SectionMap[Section].push_back(SCU);
1032 } else {
1033 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1034 // appear in the output. This sucks as we rely on sections to build
1035 // arange spans. We can do it without, but it's icky.
1036 SectionMap[NULL].push_back(SCU);
1037 }
1038 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001039
Richard Mittonc2508242013-10-03 22:07:08 +00001040 // Build a list of sections used.
1041 std::vector<const MCSection *> Sections;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001042 for (const auto &it : SectionMap) {
1043 const MCSection *Section = it.first;
Richard Mittonc2508242013-10-03 22:07:08 +00001044 Sections.push_back(Section);
1045 }
1046
1047 // Sort the sections into order.
1048 // This is only done to ensure consistent output order across different runs.
1049 std::sort(Sections.begin(), Sections.end(), SectionSort);
1050
1051 // Add terminating symbols for each section.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001052 for (unsigned ID = 0, E = Sections.size(); ID != E; ID++) {
Richard Mittonc2508242013-10-03 22:07:08 +00001053 const MCSection *Section = Sections[ID];
Richard Mitton21101b32013-09-19 23:21:01 +00001054 MCSymbol *Sym = NULL;
1055
1056 if (Section) {
Richard Mittonc2508242013-10-03 22:07:08 +00001057 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001058 // if we know the section name up-front. For user-created sections, the
Eric Christopher95531b62014-01-29 22:06:21 +00001059 // resulting label may not be valid to use as a label. (section names can
1060 // use a greater set of characters on some systems)
Richard Mittonc2508242013-10-03 22:07:08 +00001061 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton21101b32013-09-19 23:21:01 +00001062 Asm->OutStreamer.SwitchSection(Section);
1063 Asm->OutStreamer.EmitLabel(Sym);
1064 }
1065
1066 // Insert a final terminator.
Alexey Samsonov4436bf02013-10-03 08:54:43 +00001067 SectionMap[Section].push_back(SymbolCU(NULL, Sym));
Bill Wendling2b128d72009-05-20 23:19:06 +00001068 }
Eric Christopher1bca60d2014-01-23 22:55:47 +00001069
1070 // For now only turn on CU ranges if we've explicitly asked for it,
Eric Christopherfb8dd002014-01-29 22:06:27 +00001071 // we have -ffunction-sections enabled, we've emitted a function
1072 // into a unique section, or we're using LTO. If we're using LTO then
1073 // we can't know that any particular function in the module is correlated
1074 // to a particular CU and so we need to be conservative. At this point all
1075 // sections should be finalized except for dwarf sections.
1076 HasCURanges = DwarfCURanges || UsedNonDefaultText || (CUMap.size() > 1) ||
Eric Christopher1bca60d2014-01-23 22:55:47 +00001077 TargetMachine::getFunctionSections();
Eric Christopher960ac372012-11-22 00:59:49 +00001078}
Bill Wendling2b128d72009-05-20 23:19:06 +00001079
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001080// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +00001081void DwarfDebug::endModule() {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001082 assert(CurFn == 0);
1083 assert(CurMI == 0);
Eric Christopher960ac372012-11-22 00:59:49 +00001084
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001085 if (!FirstCU)
1086 return;
Eric Christopher960ac372012-11-22 00:59:49 +00001087
1088 // End any existing sections.
1089 // TODO: Does this need to happen?
1090 endSections();
1091
1092 // Finalize the debug info for the module.
1093 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +00001094
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001095 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +00001096
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001097 // Emit all the DIEs into a debug info section.
1098 emitDebugInfo();
Eric Christopher4c9b1192012-11-27 00:41:54 +00001099
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001100 // Corresponding abbreviations into a abbrev section.
1101 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +00001102
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001103 // Emit info into a debug loc section.
1104 emitDebugLoc();
Eric Christopher95198f502012-11-27 22:43:42 +00001105
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001106 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +00001107 if (GenerateARangeSection)
1108 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001109
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001110 // Emit info into a debug ranges section.
1111 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +00001112
Eric Christopher9a8f5ed2013-11-19 09:04:50 +00001113 if (useSplitDwarf()) {
1114 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001115 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +00001116 emitDebugAbbrevDWO();
Eric Christopher962c9082013-01-15 23:56:56 +00001117 // Emit DWO addresses.
1118 InfoHolder.emitAddresses(Asm->getObjFileLowering().getDwarfAddrSection());
Eric Christopher95198f502012-11-27 22:43:42 +00001119 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001120
Eric Christophera876b822012-08-23 07:32:06 +00001121 // Emit info into the dwarf accelerator table sections.
Eric Christopher20b76a72012-08-23 22:36:40 +00001122 if (useDwarfAccelTables()) {
Eric Christopher4996c702011-11-07 09:24:32 +00001123 emitAccelNames();
1124 emitAccelObjC();
1125 emitAccelNamespaces();
1126 emitAccelTypes();
1127 }
Eric Christopher6a841382012-11-19 22:42:10 +00001128
Eric Christopher4b358182013-08-30 00:40:17 +00001129 // Emit the pubnames and pubtypes sections if requested.
1130 if (HasDwarfPubSections) {
David Blaikie70a33202013-09-19 17:33:35 +00001131 emitDebugPubNames(GenerateGnuPubSections);
1132 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher4b358182013-08-30 00:40:17 +00001133 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00001134
Devang Pateld0701282010-08-02 17:32:15 +00001135 // clean up.
Devang Pateleb1bb4e2011-08-16 22:09:43 +00001136 SPMap.clear();
Eric Christopher8afd7b62012-12-10 19:51:18 +00001137
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001138 // Reset these for the next Module if we have one.
1139 FirstCU = NULL;
Bill Wendling2b128d72009-05-20 23:19:06 +00001140}
1141
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001142// Find abstract variable, if any, associated with Var.
Devang Patelbb23a4a2011-08-10 21:50:54 +00001143DbgVariable *DwarfDebug::findAbstractVariable(DIVariable &DV,
Chris Lattner915c5f92010-04-02 19:42:39 +00001144 DebugLoc ScopeLoc) {
Devang Patelbb23a4a2011-08-10 21:50:54 +00001145 LLVMContext &Ctx = DV->getContext();
1146 // More then one inlined variable corresponds to one abstract variable.
1147 DIVariable Var = cleanseInlinedVariable(DV, Ctx);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001148 DbgVariable *AbsDbgVariable = AbstractVariables.lookup(Var);
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001149 if (AbsDbgVariable)
1150 return AbsDbgVariable;
1151
Devang Patel7e623022011-08-10 20:55:27 +00001152 LexicalScope *Scope = LScopes.findAbstractScope(ScopeLoc.getScope(Ctx));
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001153 if (!Scope)
1154 return NULL;
1155
Manman Renb3388602013-10-05 01:43:03 +00001156 AbsDbgVariable = new DbgVariable(Var, NULL, this);
Devang Patel7e623022011-08-10 20:55:27 +00001157 addScopeVariable(Scope, AbsDbgVariable);
Devang Patelcfa8e9d2010-05-07 18:11:54 +00001158 AbstractVariables[Var] = AbsDbgVariable;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001159 return AbsDbgVariable;
1160}
1161
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001162// If Var is a current function argument then add it to CurrentFnArguments list.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001163bool DwarfDebug::addCurrentFnArgument(DbgVariable *Var, LexicalScope *Scope) {
Devang Patel7e623022011-08-10 20:55:27 +00001164 if (!LScopes.isCurrentFunctionScope(Scope))
Devang Patel6c622ef2011-03-01 22:58:55 +00001165 return false;
1166 DIVariable DV = Var->getVariable();
1167 if (DV.getTag() != dwarf::DW_TAG_arg_variable)
1168 return false;
1169 unsigned ArgNo = DV.getArgNumber();
Eric Christopher6a841382012-11-19 22:42:10 +00001170 if (ArgNo == 0)
Devang Patel6c622ef2011-03-01 22:58:55 +00001171 return false;
1172
Devang Patel4ab660b2011-03-03 20:02:02 +00001173 size_t Size = CurrentFnArguments.size();
1174 if (Size == 0)
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001175 CurrentFnArguments.resize(CurFn->getFunction()->arg_size());
Devang Patel63b3e762011-03-03 21:49:41 +00001176 // llvm::Function argument size is not good indicator of how many
Devang Patel34a7ab42011-03-03 20:08:10 +00001177 // arguments does the function have at source level.
1178 if (ArgNo > Size)
Devang Patel4ab660b2011-03-03 20:02:02 +00001179 CurrentFnArguments.resize(ArgNo * 2);
Devang Patel6c622ef2011-03-01 22:58:55 +00001180 CurrentFnArguments[ArgNo - 1] = Var;
1181 return true;
1182}
1183
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001184// Collect variable information from side table maintained by MMI.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001185void DwarfDebug::collectVariableInfoFromMMITable(
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001186 SmallPtrSet<const MDNode *, 16> &Processed) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00001187 for (const auto &VI : MMI->getVariableDbgInfo()) {
1188 const MDNode *Var = VI.first;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001189 if (!Var)
1190 continue;
Devang Patele0a94bf2010-05-14 21:01:35 +00001191 Processed.insert(Var);
Chris Lattner915c5f92010-04-02 19:42:39 +00001192 DIVariable DV(Var);
Benjamin Kramer15596c72014-03-07 19:09:39 +00001193 const std::pair<unsigned, DebugLoc> &VP = VI.second;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001194
Devang Patel7e623022011-08-10 20:55:27 +00001195 LexicalScope *Scope = LScopes.findLexicalScope(VP.second);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001196
Devang Patelcdb7d442009-11-10 23:20:04 +00001197 // If variable scope is not found then skip this variable.
Chris Lattner915c5f92010-04-02 19:42:39 +00001198 if (Scope == 0)
Devang Patelcdb7d442009-11-10 23:20:04 +00001199 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001200
Devang Patele1c53f22010-05-20 16:36:41 +00001201 DbgVariable *AbsDbgVariable = findAbstractVariable(DV, VP.second);
Manman Renb3388602013-10-05 01:43:03 +00001202 DbgVariable *RegVar = new DbgVariable(DV, AbsDbgVariable, this);
Devang Patel3e4a9652011-08-15 21:24:36 +00001203 RegVar->setFrameIndex(VP.first);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001204 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001205 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001206 if (AbsDbgVariable)
Devang Patel3e4a9652011-08-15 21:24:36 +00001207 AbsDbgVariable->setFrameIndex(VP.first);
Devang Patel475d32a2009-10-06 01:26:37 +00001208 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001209}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001210
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001211// Return true if debug value, encoded by DBG_VALUE instruction, is in a
1212// defined reg.
Devang Patel9fc11702010-05-25 23:40:22 +00001213static bool isDbgValueInDefinedReg(const MachineInstr *MI) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001214 assert(MI->isDebugValue() && "Invalid DBG_VALUE machine instruction!");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001215 return MI->getNumOperands() == 3 && MI->getOperand(0).isReg() &&
1216 MI->getOperand(0).getReg() &&
Adrian Prantl418d1d12013-07-09 20:28:37 +00001217 (MI->getOperand(1).isImm() ||
1218 (MI->getOperand(1).isReg() && MI->getOperand(1).getReg() == 0U));
Devang Patel9fc11702010-05-25 23:40:22 +00001219}
1220
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001221// Get .debug_loc entry for the instruction range starting at MI.
Eric Christopher6a841382012-11-19 22:42:10 +00001222static DotDebugLocEntry getDebugLocEntry(AsmPrinter *Asm,
1223 const MCSymbol *FLabel,
Devang Patel2442a892011-07-08 17:09:57 +00001224 const MCSymbol *SLabel,
1225 const MachineInstr *MI) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001226 const MDNode *Var = MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Devang Patel2442a892011-07-08 17:09:57 +00001227
David Blaikie0252265b2013-06-16 20:34:15 +00001228 assert(MI->getNumOperands() == 3);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001229 if (MI->getOperand(0).isReg()) {
Devang Patel2442a892011-07-08 17:09:57 +00001230 MachineLocation MLoc;
Adrian Prantl418d1d12013-07-09 20:28:37 +00001231 // If the second operand is an immediate, this is a
1232 // register-indirect address.
1233 if (!MI->getOperand(1).isImm())
Adrian Prantld4c0dd42013-04-26 21:57:17 +00001234 MLoc.set(MI->getOperand(0).getReg());
1235 else
1236 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001237 return DotDebugLocEntry(FLabel, SLabel, MLoc, Var);
1238 }
1239 if (MI->getOperand(0).isImm())
1240 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getImm());
1241 if (MI->getOperand(0).isFPImm())
1242 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getFPImm());
1243 if (MI->getOperand(0).isCImm())
1244 return DotDebugLocEntry(FLabel, SLabel, MI->getOperand(0).getCImm());
1245
Craig Topperee4dab52012-02-05 08:31:47 +00001246 llvm_unreachable("Unexpected 3 operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001247}
1248
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001249// Find variables for each lexical scope.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001250void
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001251DwarfDebug::collectVariableInfo(SmallPtrSet<const MDNode *, 16> &Processed) {
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001252
Eric Christopher270a12c2013-07-03 21:37:03 +00001253 // Grab the variable info that was squirreled away in the MMI side-table.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001254 collectVariableInfoFromMMITable(Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001255
Benjamin Kramer15596c72014-03-07 19:09:39 +00001256 for (const MDNode *Var : UserVariables) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001257 if (Processed.count(Var))
Devang Patel490c8ab2010-05-20 19:57:06 +00001258 continue;
1259
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001260 // History contains relevant DBG_VALUE instructions for Var and instructions
1261 // clobbering it.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001262 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001263 if (History.empty())
1264 continue;
1265 const MachineInstr *MInsn = History.front();
Devang Patel9fc11702010-05-25 23:40:22 +00001266
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001267 DIVariable DV(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001268 LexicalScope *Scope = NULL;
Devang Patel7a9dedf2010-05-27 20:25:04 +00001269 if (DV.getTag() == dwarf::DW_TAG_arg_variable &&
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001270 DISubprogram(DV.getContext()).describes(CurFn->getFunction()))
Devang Patel7e623022011-08-10 20:55:27 +00001271 Scope = LScopes.getCurrentFunctionScope();
David Blaikiedc69ebb2013-03-11 23:39:23 +00001272 else if (MDNode *IA = DV.getInlinedAt())
1273 Scope = LScopes.findInlinedScope(DebugLoc::getFromDILocation(IA));
1274 else
1275 Scope = LScopes.findLexicalScope(cast<MDNode>(DV->getOperand(1)));
Devang Patel490c8ab2010-05-20 19:57:06 +00001276 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001277 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001278 continue;
1279
1280 Processed.insert(DV);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001281 assert(MInsn->isDebugValue() && "History must begin with debug value");
Devang Patel99819b52011-08-15 19:01:20 +00001282 DbgVariable *AbsVar = findAbstractVariable(DV, MInsn->getDebugLoc());
Manman Renb3388602013-10-05 01:43:03 +00001283 DbgVariable *RegVar = new DbgVariable(DV, AbsVar, this);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001284 if (!addCurrentFnArgument(RegVar, Scope))
Devang Patel7e623022011-08-10 20:55:27 +00001285 addScopeVariable(Scope, RegVar);
Devang Patel99819b52011-08-15 19:01:20 +00001286 if (AbsVar)
Devang Patel3e4a9652011-08-15 21:24:36 +00001287 AbsVar->setMInsn(MInsn);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001288
Eric Christophercc10d202012-10-08 20:48:54 +00001289 // Simplify ranges that are fully coalesced.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001290 if (History.size() <= 1 ||
1291 (History.size() == 2 && MInsn->isIdenticalTo(History.back()))) {
Devang Patel3e4a9652011-08-15 21:24:36 +00001292 RegVar->setMInsn(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001293 continue;
1294 }
1295
Eric Christopher59cc0712013-01-28 17:33:26 +00001296 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001297 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001298
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001299 for (SmallVectorImpl<const MachineInstr *>::const_iterator
1300 HI = History.begin(),
1301 HE = History.end();
1302 HI != HE; ++HI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001303 const MachineInstr *Begin = *HI;
1304 assert(Begin->isDebugValue() && "Invalid History entry");
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001305
Devang Patele7181b52011-06-01 23:00:17 +00001306 // Check if DBG_VALUE is truncating a range.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001307 if (Begin->getNumOperands() > 1 && Begin->getOperand(0).isReg() &&
1308 !Begin->getOperand(0).getReg())
Devang Patele7181b52011-06-01 23:00:17 +00001309 continue;
1310
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001311 // Compute the range for a register location.
1312 const MCSymbol *FLabel = getLabelBeforeInsn(Begin);
1313 const MCSymbol *SLabel = 0;
1314
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001315 if (HI + 1 == HE)
1316 // If Begin is the last instruction in History then its value is valid
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001317 // until the end of the function.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001318 SLabel = FunctionEndSym;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001319 else {
1320 const MachineInstr *End = HI[1];
Eric Christopher6a841382012-11-19 22:42:10 +00001321 DEBUG(dbgs() << "DotDebugLoc Pair:\n"
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001322 << "\t" << *Begin << "\t" << *End << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001323 if (End->isDebugValue())
1324 SLabel = getLabelBeforeInsn(End);
1325 else {
1326 // End is a normal instruction clobbering the range.
1327 SLabel = getLabelAfterInsn(End);
1328 assert(SLabel && "Forgot label after clobber instruction");
1329 ++HI;
1330 }
1331 }
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001332
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001333 // The value is valid until the next DBG_VALUE or clobber.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001334 DotDebugLocEntries.push_back(
1335 getDebugLocEntry(Asm, FLabel, SLabel, Begin));
Devang Patel9fc11702010-05-25 23:40:22 +00001336 }
1337 DotDebugLocEntries.push_back(DotDebugLocEntry());
Devang Patela3e9c9c2010-03-15 18:33:46 +00001338 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001339
1340 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001341 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
1342 DIArray Variables = DISubprogram(FnScope->getScopeNode()).getVariables();
1343 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1344 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001345 if (!DV || !DV.isVariable() || !Processed.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001346 continue;
1347 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001348 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patele0a94bf2010-05-14 21:01:35 +00001349 }
Devang Patel9fc11702010-05-25 23:40:22 +00001350}
Devang Patele0a94bf2010-05-14 21:01:35 +00001351
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001352// Return Label preceding the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001353MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001354 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1355 assert(Label && "Didn't insert label before instruction");
1356 return Label;
Devang Patel9fc11702010-05-25 23:40:22 +00001357}
1358
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001359// Return Label immediately following the instruction.
Eric Christopher962c9082013-01-15 23:56:56 +00001360MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001361 return LabelsAfterInsn.lookup(MI);
Devang Patel475d32a2009-10-06 01:26:37 +00001362}
1363
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001364// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001365void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001366 assert(CurMI == 0);
1367 CurMI = MI;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001368 // Check if source location changes, but ignore DBG_VALUE locations.
1369 if (!MI->isDebugValue()) {
1370 DebugLoc DL = MI->getDebugLoc();
1371 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopheraec8a822012-04-05 20:39:05 +00001372 unsigned Flags = 0;
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001373 PrevInstLoc = DL;
Devang Patel34a66202011-05-11 19:22:19 +00001374 if (DL == PrologEndLoc) {
1375 Flags |= DWARF2_FLAG_PROLOGUE_END;
1376 PrologEndLoc = DebugLoc();
1377 }
Eric Christopheraec8a822012-04-05 20:39:05 +00001378 if (PrologEndLoc.isUnknown())
1379 Flags |= DWARF2_FLAG_IS_STMT;
1380
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001381 if (!DL.isUnknown()) {
1382 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel34a66202011-05-11 19:22:19 +00001383 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001384 } else
Devang Patel34a66202011-05-11 19:22:19 +00001385 recordSourceLine(0, 0, 0, 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001386 }
Devang Patel9fc11702010-05-25 23:40:22 +00001387 }
Devang Patel23b2ae62010-03-29 22:59:58 +00001388
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001389 // Insert labels where requested.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001390 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1391 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001392
1393 // No label needed.
1394 if (I == LabelsBeforeInsn.end())
1395 return;
1396
1397 // Label already assigned.
1398 if (I->second)
Devang Patel002d54d2010-05-26 19:37:24 +00001399 return;
Devang Patelbd477be2010-03-29 17:20:31 +00001400
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001401 if (!PrevLabel) {
Devang Patelacc32a52010-05-26 21:23:46 +00001402 PrevLabel = MMI->getContext().CreateTempSymbol();
1403 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel002d54d2010-05-26 19:37:24 +00001404 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001405 I->second = PrevLabel;
Devang Patel8db360d2009-10-06 01:50:42 +00001406}
1407
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001408// Process end of an instruction.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001409void DwarfDebug::endInstruction() {
1410 assert(CurMI != 0);
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001411 // Don't create a new label after DBG_VALUE instructions.
1412 // They don't generate code.
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001413 if (!CurMI->isDebugValue())
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001414 PrevLabel = 0;
1415
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001416 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001417 LabelsAfterInsn.find(CurMI);
1418 CurMI = 0;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001419
1420 // No label needed.
1421 if (I == LabelsAfterInsn.end())
1422 return;
1423
1424 // Label already assigned.
1425 if (I->second)
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001426 return;
1427
1428 // We need a label after this instruction.
1429 if (!PrevLabel) {
1430 PrevLabel = MMI->getContext().CreateTempSymbol();
1431 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel3ebd8932010-04-08 16:50:29 +00001432 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001433 I->second = PrevLabel;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001434}
1435
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001436// Each LexicalScope has first instruction and last instruction to mark
1437// beginning and end of a scope respectively. Create an inverse map that list
1438// scopes starts (and ends) with an instruction. One instruction may start (or
1439// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patel359b0132010-04-08 18:43:56 +00001440void DwarfDebug::identifyScopeMarkers() {
Devang Patel7e623022011-08-10 20:55:27 +00001441 SmallVector<LexicalScope *, 4> WorkList;
1442 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel7771b7c2010-01-20 02:05:23 +00001443 while (!WorkList.empty()) {
Devang Patel7e623022011-08-10 20:55:27 +00001444 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001445
Craig Topper977e9cd2013-07-03 04:24:43 +00001446 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001447 if (!Children.empty())
Benjamin Kramer15596c72014-03-07 19:09:39 +00001448 WorkList.append(Children.begin(), Children.end());
Devang Patel7771b7c2010-01-20 02:05:23 +00001449
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001450 if (S->isAbstractScope())
1451 continue;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001452
Benjamin Kramer15596c72014-03-07 19:09:39 +00001453 for (const InsnRange &R : S->getRanges()) {
1454 assert(R.first && "InsnRange does not have first instruction!");
1455 assert(R.second && "InsnRange does not have second instruction!");
1456 requestLabelBeforeInsn(R.first);
1457 requestLabelAfterInsn(R.second);
Devang Patel6c74a872010-04-27 19:46:33 +00001458 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001459 }
Devang Patel75cc16c2009-10-01 20:31:14 +00001460}
1461
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001462// Gather pre-function debug information. Assumes being called immediately
1463// after the function entry point has been emitted.
Chris Lattner76555b52010-01-26 23:18:02 +00001464void DwarfDebug::beginFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001465 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001466
1467 // If there's no debug info for the function we're not going to do anything.
1468 if (!MMI->hasDebugInfo())
1469 return;
1470
1471 // Grab the lexical scopes for the function, if we don't have any of those
1472 // then we're not going to be able to do anything.
Devang Patel7e623022011-08-10 20:55:27 +00001473 LScopes.initialize(*MF);
Eric Christopherfedfa442013-11-01 23:14:17 +00001474 if (LScopes.empty())
1475 return;
1476
1477 assert(UserVariables.empty() && DbgValues.empty() && "Maps weren't cleaned");
1478
1479 // Make sure that each lexical scope will have a begin/end label.
Devang Patel7e623022011-08-10 20:55:27 +00001480 identifyScopeMarkers();
Devang Patel4598eb62009-10-06 18:37:31 +00001481
Eric Christopher4287a492013-12-09 23:57:44 +00001482 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopherfedfa442013-11-01 23:14:17 +00001483 // belongs to so that we add to the correct per-cu line table in the
1484 // non-asm case.
Manman Ren4e042a62013-02-05 21:52:47 +00001485 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001486 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren4e042a62013-02-05 21:52:47 +00001487 assert(TheCU && "Unable to find compile unit!");
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001488 if (Asm->OutStreamer.hasRawTextSupport())
1489 // Use a single line table if we are generating assembly.
Manman Ren9d4c7352013-05-21 00:57:22 +00001490 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1491 else
1492 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001493
Eric Christopher2037caf2014-01-28 00:49:26 +00001494 // Check the current section against the standard text section. If different
1495 // keep track so that we will know when we're emitting functions into multiple
1496 // sections.
1497 if (Asm->getObjFileLowering().getTextSection() != Asm->getCurrentSection())
1498 UsedNonDefaultText = true;
1499
Eric Christopherfedfa442013-11-01 23:14:17 +00001500 // Emit a label for the function so that we have a beginning address.
1501 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendling2b128d72009-05-20 23:19:06 +00001502 // Assumes in correct section after the entry point.
Devang Patel6c74a872010-04-27 19:46:33 +00001503 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendling2b128d72009-05-20 23:19:06 +00001504
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001505 const TargetRegisterInfo *TRI = Asm->TM.getRegisterInfo();
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001506 // LiveUserVar - Map physreg numbers to the MDNode they contain.
Eric Christopherfedfa442013-11-01 23:14:17 +00001507 std::vector<const MDNode *> LiveUserVar(TRI->getNumRegs());
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001508
Eric Christopherfedfa442013-11-01 23:14:17 +00001509 for (MachineFunction::const_iterator I = MF->begin(), E = MF->end(); I != E;
1510 ++I) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001511 bool AtBlockEntry = true;
Devang Patel002d54d2010-05-26 19:37:24 +00001512 for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
1513 II != IE; ++II) {
1514 const MachineInstr *MI = II;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001515
Devang Patel002d54d2010-05-26 19:37:24 +00001516 if (MI->isDebugValue()) {
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001517 assert(MI->getNumOperands() > 1 && "Invalid machine instruction!");
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001518
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001519 // Keep track of user variables.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001520 const MDNode *Var =
Eric Christopherfedfa442013-11-01 23:14:17 +00001521 MI->getOperand(MI->getNumOperands() - 1).getMetadata();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001522
1523 // Variable is in a register, we need to check for clobbers.
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001524 if (isDbgValueInDefinedReg(MI))
1525 LiveUserVar[MI->getOperand(0).getReg()] = Var;
1526
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001527 // Check the history of this variable.
Eric Christopherfedfa442013-11-01 23:14:17 +00001528 SmallVectorImpl<const MachineInstr *> &History = DbgValues[Var];
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001529 if (History.empty()) {
1530 UserVariables.push_back(Var);
1531 // The first mention of a function argument gets the FunctionBeginSym
1532 // label, so arguments are visible when breaking at function entry.
1533 DIVariable DV(Var);
Manman Ren7504ed42013-07-08 18:33:29 +00001534 if (DV.isVariable() && DV.getTag() == dwarf::DW_TAG_arg_variable &&
David Blaikie5af2aca2013-11-18 23:57:26 +00001535 getDISubprogram(DV.getContext()).describes(MF->getFunction()))
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001536 LabelsBeforeInsn[MI] = FunctionBeginSym;
1537 } else {
1538 // We have seen this variable before. Try to coalesce DBG_VALUEs.
1539 const MachineInstr *Prev = History.back();
1540 if (Prev->isDebugValue()) {
1541 // Coalesce identical entries at the end of History.
1542 if (History.size() >= 2 &&
Devang Patelb7a328e2011-07-07 00:14:27 +00001543 Prev->isIdenticalTo(History[History.size() - 2])) {
Eric Christopher85a495e2012-10-08 20:48:49 +00001544 DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001545 << "\t" << *Prev << "\t"
1546 << *History[History.size() - 2] << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001547 History.pop_back();
Devang Patelb7a328e2011-07-07 00:14:27 +00001548 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001549
1550 // Terminate old register assignments that don't reach MI;
1551 MachineFunction::const_iterator PrevMBB = Prev->getParent();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001552 if (PrevMBB != I && (!AtBlockEntry || std::next(PrevMBB) != I) &&
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001553 isDbgValueInDefinedReg(Prev)) {
1554 // Previous register assignment needs to terminate at the end of
1555 // its basic block.
1556 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001557 PrevMBB->getLastNonDebugInstr();
Devang Patelb7a328e2011-07-07 00:14:27 +00001558 if (LastMI == PrevMBB->end()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001559 // Drop DBG_VALUE for empty range.
Eric Christopher85a495e2012-10-08 20:48:49 +00001560 DEBUG(dbgs() << "Dropping DBG_VALUE for empty range:\n"
Eric Christopherfedfa442013-11-01 23:14:17 +00001561 << "\t" << *Prev << "\n");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001562 History.pop_back();
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001563 } else if (std::next(PrevMBB) != PrevMBB->getParent()->end())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001564 // Terminate after LastMI.
1565 History.push_back(LastMI);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001566 }
1567 }
1568 }
1569 History.push_back(MI);
Devang Patel002d54d2010-05-26 19:37:24 +00001570 } else {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001571 // Not a DBG_VALUE instruction.
Rafael Espindolab1f25f12014-03-07 06:08:31 +00001572 if (!MI->isPosition())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001573 AtBlockEntry = false;
1574
Eric Christopher133195782012-10-04 20:46:14 +00001575 // First known non-DBG_VALUE and non-frame setup location marks
1576 // the beginning of the function body.
1577 if (!MI->getFlag(MachineInstr::FrameSetup) &&
1578 (PrologEndLoc.isUnknown() && !MI->getDebugLoc().isUnknown()))
Devang Patel34a66202011-05-11 19:22:19 +00001579 PrologEndLoc = MI->getDebugLoc();
1580
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001581 // Check if the instruction clobbers any registers with debug vars.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001582 for (const MachineOperand &MO : MI->operands()) {
1583 if (!MO.isReg() || !MO.isDef() || !MO.getReg())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001584 continue;
Benjamin Kramer15596c72014-03-07 19:09:39 +00001585 for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
Eric Christopherfedfa442013-11-01 23:14:17 +00001586 ++AI) {
Jakob Stoklund Olesen54038d72012-06-01 23:28:30 +00001587 unsigned Reg = *AI;
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001588 const MDNode *Var = LiveUserVar[Reg];
1589 if (!Var)
1590 continue;
1591 // Reg is now clobbered.
1592 LiveUserVar[Reg] = 0;
1593
1594 // Was MD last defined by a DBG_VALUE referring to Reg?
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001595 DbgValueHistoryMap::iterator HistI = DbgValues.find(Var);
1596 if (HistI == DbgValues.end())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001597 continue;
Eric Christopherfedfa442013-11-01 23:14:17 +00001598 SmallVectorImpl<const MachineInstr *> &History = HistI->second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001599 if (History.empty())
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001600 continue;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001601 const MachineInstr *Prev = History.back();
1602 // Sanity-check: Register assignments are terminated at the end of
1603 // their block.
1604 if (!Prev->isDebugValue() || Prev->getParent() != MI->getParent())
1605 continue;
1606 // Is the variable still in Reg?
1607 if (!isDbgValueInDefinedReg(Prev) ||
1608 Prev->getOperand(0).getReg() != Reg)
1609 continue;
1610 // Var is clobbered. Make sure the next instruction gets a label.
1611 History.push_back(MI);
Jakob Stoklund Olesenec0ac3c2011-03-22 22:33:08 +00001612 }
1613 }
Devang Patel002d54d2010-05-26 19:37:24 +00001614 }
Devang Patel002d54d2010-05-26 19:37:24 +00001615 }
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001616 }
1617
Benjamin Kramer15596c72014-03-07 19:09:39 +00001618 for (auto &I : DbgValues) {
1619 SmallVectorImpl<const MachineInstr *> &History = I.second;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001620 if (History.empty())
1621 continue;
1622
1623 // Make sure the final register assignments are terminated.
1624 const MachineInstr *Prev = History.back();
1625 if (Prev->isDebugValue() && isDbgValueInDefinedReg(Prev)) {
1626 const MachineBasicBlock *PrevMBB = Prev->getParent();
Eric Christopher6a841382012-11-19 22:42:10 +00001627 MachineBasicBlock::const_iterator LastMI =
Eric Christopherfedfa442013-11-01 23:14:17 +00001628 PrevMBB->getLastNonDebugInstr();
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001629 if (LastMI == PrevMBB->end())
1630 // Drop DBG_VALUE for empty range.
1631 History.pop_back();
David Blaikieea2605d2013-06-20 00:25:24 +00001632 else if (PrevMBB != &PrevMBB->getParent()->back()) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001633 // Terminate after LastMI.
1634 History.push_back(LastMI);
1635 }
1636 }
1637 // Request labels for the full history.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001638 for (const MachineInstr *MI : History) {
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001639 if (MI->isDebugValue())
1640 requestLabelBeforeInsn(MI);
1641 else
1642 requestLabelAfterInsn(MI);
1643 }
1644 }
Devang Patel002d54d2010-05-26 19:37:24 +00001645
Jakob Stoklund Olesen1886a4c2011-03-25 17:20:59 +00001646 PrevInstLoc = DebugLoc();
Devang Patel002d54d2010-05-26 19:37:24 +00001647 PrevLabel = FunctionBeginSym;
Devang Patel34a66202011-05-11 19:22:19 +00001648
1649 // Record beginning of function.
1650 if (!PrologEndLoc.isUnknown()) {
Eric Christopherfedfa442013-11-01 23:14:17 +00001651 DebugLoc FnStartDL =
Timur Iskhodzhanovf166f6c2014-01-30 01:39:17 +00001652 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
Eric Christopherfedfa442013-11-01 23:14:17 +00001653 recordSourceLine(
1654 FnStartDL.getLine(), FnStartDL.getCol(),
1655 FnStartDL.getScope(MF->getFunction()->getContext()),
1656 // We'd like to list the prologue as "not statements" but GDB behaves
1657 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1658 DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001659 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001660}
1661
Devang Patel7e623022011-08-10 20:55:27 +00001662void DwarfDebug::addScopeVariable(LexicalScope *LS, DbgVariable *Var) {
David Blaikie6f1a8062013-06-05 05:39:59 +00001663 SmallVectorImpl<DbgVariable *> &Vars = ScopeVariables[LS];
1664 DIVariable DV = Var->getVariable();
David Blaikie36d5d2f2013-06-06 21:04:51 +00001665 // Variables with positive arg numbers are parameters.
1666 if (unsigned ArgNum = DV.getArgNumber()) {
1667 // Keep all parameters in order at the start of the variable list to ensure
1668 // function types are correct (no out-of-order parameters)
1669 //
1670 // This could be improved by only doing it for optimized builds (unoptimized
1671 // builds have the right order to begin with), searching from the back (this
1672 // would catch the unoptimized case quickly), or doing a binary search
1673 // rather than linear search.
1674 SmallVectorImpl<DbgVariable *>::iterator I = Vars.begin();
1675 while (I != Vars.end()) {
1676 unsigned CurNum = (*I)->getVariable().getArgNumber();
1677 // A local (non-parameter) variable has been found, insert immediately
1678 // before it.
1679 if (CurNum == 0)
1680 break;
1681 // A later indexed parameter has been found, insert immediately before it.
David Blaikieb272a752013-06-06 22:28:26 +00001682 if (CurNum > ArgNum)
David Blaikie36d5d2f2013-06-06 21:04:51 +00001683 break;
David Blaikieb272a752013-06-06 22:28:26 +00001684 ++I;
David Blaikie6f1a8062013-06-05 05:39:59 +00001685 }
David Blaikie36d5d2f2013-06-06 21:04:51 +00001686 Vars.insert(I, Var);
1687 return;
David Blaikie6f1a8062013-06-05 05:39:59 +00001688 }
1689
1690 Vars.push_back(Var);
Devang Patel7e623022011-08-10 20:55:27 +00001691}
1692
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001693// Gather and emit post-function debug information.
NAKAMURA Takumib9271612013-12-03 13:15:54 +00001694void DwarfDebug::endFunction(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001695 // Every beginFunction(MF) call should be followed by an endFunction(MF) call,
1696 // though the beginFunction may not be called at all.
1697 // We should handle both cases.
1698 if (CurFn == 0)
1699 CurFn = MF;
1700 else
1701 assert(CurFn == MF);
1702 assert(CurFn != 0);
1703
1704 if (!MMI->hasDebugInfo() || LScopes.empty()) {
1705 CurFn = 0;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001706 return;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001707 }
Devang Patel2904aa92009-11-12 19:02:56 +00001708
Devang Patel7e623022011-08-10 20:55:27 +00001709 // Define end label for subprogram.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001710 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patel7e623022011-08-10 20:55:27 +00001711 // Assumes in correct section after the entry point.
1712 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Eric Christopher1a972152014-01-29 23:05:43 +00001713
Eric Christopher4287a492013-12-09 23:57:44 +00001714 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Manman Ren4e042a62013-02-05 21:52:47 +00001715 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001716
Devang Patel7e623022011-08-10 20:55:27 +00001717 SmallPtrSet<const MDNode *, 16> ProcessedVars;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001718 collectVariableInfo(ProcessedVars);
Eric Christopher6a841382012-11-19 22:42:10 +00001719
Devang Patel3acc70e2011-08-15 22:04:40 +00001720 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Eric Christopher4287a492013-12-09 23:57:44 +00001721 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Nick Lewycky654f5ce2011-10-26 22:55:33 +00001722 assert(TheCU && "Unable to find compile unit!");
Devang Patel3acc70e2011-08-15 22:04:40 +00001723
Devang Patel7e623022011-08-10 20:55:27 +00001724 // Construct abstract scopes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001725 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Devang Patel44403472011-08-12 18:10:19 +00001726 DISubprogram SP(AScope->getScopeNode());
Manman Ren7504ed42013-07-08 18:33:29 +00001727 if (SP.isSubprogram()) {
Devang Patel7e623022011-08-10 20:55:27 +00001728 // Collect info for variables that were optimized out.
Devang Patel59e27c52011-08-19 23:28:12 +00001729 DIArray Variables = SP.getVariables();
1730 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1731 DIVariable DV(Variables.getElement(i));
Manman Ren7504ed42013-07-08 18:33:29 +00001732 if (!DV || !DV.isVariable() || !ProcessedVars.insert(DV))
Devang Patel59e27c52011-08-19 23:28:12 +00001733 continue;
Alexey Samsonov39602782012-07-06 08:45:08 +00001734 // Check that DbgVariable for DV wasn't created earlier, when
1735 // findAbstractVariable() was called for inlined instance of DV.
1736 LLVMContext &Ctx = DV->getContext();
1737 DIVariable CleanDV = cleanseInlinedVariable(DV, Ctx);
1738 if (AbstractVariables.lookup(CleanDV))
1739 continue;
Devang Patel59e27c52011-08-19 23:28:12 +00001740 if (LexicalScope *Scope = LScopes.findAbstractScope(DV.getContext()))
Manman Renb3388602013-10-05 01:43:03 +00001741 addScopeVariable(Scope, new DbgVariable(DV, NULL, this));
Devang Patel5c0f85c2010-06-25 22:07:34 +00001742 }
1743 }
Devang Patel44403472011-08-12 18:10:19 +00001744 if (ProcessedSPNodes.count(AScope->getScopeNode()) == 0)
Manman Ren4213c392013-05-29 17:16:59 +00001745 constructScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001746 }
Eric Christopher6a841382012-11-19 22:42:10 +00001747
Devang Patel3acc70e2011-08-15 22:04:40 +00001748 DIE *CurFnDIE = constructScopeDIE(TheCU, FnScope);
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001749 if (!CurFn->getTarget().Options.DisableFramePointerElim(*CurFn))
Eric Christopherbb69a272012-08-24 01:14:27 +00001750 TheCU->addFlag(CurFnDIE, dwarf::DW_AT_APPLE_omit_frame_ptr);
Devang Patel3acc70e2011-08-15 22:04:40 +00001751
Eric Christopher1a972152014-01-29 23:05:43 +00001752 // Add the range of this function to the list of ranges for the CU.
1753 RangeSpan Span(FunctionBeginSym, FunctionEndSym);
Chandler Carruth002da5d2014-03-02 04:08:41 +00001754 TheCU->addRange(std::move(Span));
Eric Christopher1a972152014-01-29 23:05:43 +00001755
Bill Wendling2b128d72009-05-20 23:19:06 +00001756 // Clear debug info
Benjamin Kramer15596c72014-03-07 19:09:39 +00001757 for (auto &I : ScopeVariables)
1758 DeleteContainerPointers(I.second);
Devang Patel7e623022011-08-10 20:55:27 +00001759 ScopeVariables.clear();
Devang Patelad45d912011-04-22 18:09:57 +00001760 DeleteContainerPointers(CurrentFnArguments);
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001761 UserVariables.clear();
1762 DbgValues.clear();
Jeffrey Yasskin35b4e4f2010-03-12 17:45:06 +00001763 AbstractVariables.clear();
Devang Patel6c74a872010-04-27 19:46:33 +00001764 LabelsBeforeInsn.clear();
1765 LabelsAfterInsn.clear();
Devang Patel12563b32010-04-16 23:33:45 +00001766 PrevLabel = NULL;
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001767 CurFn = 0;
Bill Wendling2b128d72009-05-20 23:19:06 +00001768}
1769
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001770// Register a source line with debug info. Returns the unique label that was
1771// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001772void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1773 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001774 StringRef Fn;
Devang Patele01b75c2011-03-24 20:30:50 +00001775 StringRef Dir;
Dan Gohman50849c62010-05-05 23:41:32 +00001776 unsigned Src = 1;
Diego Novillo282450d2014-03-03 18:53:17 +00001777 unsigned Discriminator = 0;
Dan Gohman50849c62010-05-05 23:41:32 +00001778 if (S) {
1779 DIDescriptor Scope(S);
Devang Patel2089d162009-10-05 18:03:19 +00001780
Dan Gohman50849c62010-05-05 23:41:32 +00001781 if (Scope.isCompileUnit()) {
1782 DICompileUnit CU(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001783 Fn = CU.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001784 Dir = CU.getDirectory();
Devang Patelc4b69052010-10-28 17:30:52 +00001785 } else if (Scope.isFile()) {
1786 DIFile F(S);
Devang Patelc4b69052010-10-28 17:30:52 +00001787 Fn = F.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001788 Dir = F.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001789 } else if (Scope.isSubprogram()) {
1790 DISubprogram SP(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001791 Fn = SP.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001792 Dir = SP.getDirectory();
Eric Christopher6647b832011-10-11 22:59:11 +00001793 } else if (Scope.isLexicalBlockFile()) {
1794 DILexicalBlockFile DBF(S);
1795 Fn = DBF.getFilename();
1796 Dir = DBF.getDirectory();
Dan Gohman50849c62010-05-05 23:41:32 +00001797 } else if (Scope.isLexicalBlock()) {
1798 DILexicalBlock DB(S);
Dan Gohman50849c62010-05-05 23:41:32 +00001799 Fn = DB.getFilename();
Devang Patele01b75c2011-03-24 20:30:50 +00001800 Dir = DB.getDirectory();
Diego Novillo282450d2014-03-03 18:53:17 +00001801 Discriminator = DB.getDiscriminator();
Dan Gohman50849c62010-05-05 23:41:32 +00001802 } else
Craig Topperee4dab52012-02-05 08:31:47 +00001803 llvm_unreachable("Unexpected scope info");
Dan Gohman50849c62010-05-05 23:41:32 +00001804
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001805 Src = getOrCreateSourceID(
1806 Fn, Dir, Asm->OutStreamer.getContext().getDwarfCompileUnitID());
Dan Gohman50849c62010-05-05 23:41:32 +00001807 }
Diego Novillo282450d2014-03-03 18:53:17 +00001808 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1809 Discriminator, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001810}
1811
Bill Wendling806535f2009-05-20 23:22:40 +00001812//===----------------------------------------------------------------------===//
1813// Emit Methods
1814//===----------------------------------------------------------------------===//
1815
Manman Rence20d462013-10-29 22:57:10 +00001816// Compute the size and offset of a DIE. The offset is relative to start of the
1817// CU. It returns the offset after laying out the DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001818unsigned DwarfFile::computeSizeAndOffset(DIE *Die, unsigned Offset) {
Bill Wendling480ff322009-05-20 23:21:38 +00001819 // Record the abbreviation.
Devang Patel930143b2009-11-21 02:48:08 +00001820 assignAbbrevNumber(Die->getAbbrev());
Bill Wendling480ff322009-05-20 23:21:38 +00001821
1822 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001823 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001824
1825 // Set DIE offset
1826 Die->setOffset(Offset);
1827
1828 // Start the size with the size of abbreviation code.
Logan Chien5b776b72014-02-22 14:00:39 +00001829 Offset += getULEB128Size(Die->getAbbrevNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001830
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001831 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001832 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001833
1834 // Size the DIE attribute values.
1835 for (unsigned i = 0, N = Values.size(); i < N; ++i)
1836 // Size attribute value.
Chris Lattner5a00dea2010-04-05 00:18:22 +00001837 Offset += Values[i]->SizeOf(Asm, AbbrevData[i].getForm());
Bill Wendling480ff322009-05-20 23:21:38 +00001838
Benjamin Kramer15596c72014-03-07 19:09:39 +00001839 // Get the children.
1840 const std::vector<DIE *> &Children = Die->getChildren();
1841
Bill Wendling480ff322009-05-20 23:21:38 +00001842 // Size the DIE children if any.
1843 if (!Children.empty()) {
Eric Christophere8f10722014-03-05 01:44:58 +00001844 assert(Abbrev.hasChildren() && "Children flag not set");
Bill Wendling480ff322009-05-20 23:21:38 +00001845
Benjamin Kramer15596c72014-03-07 19:09:39 +00001846 for (DIE *Child : Children)
1847 Offset = computeSizeAndOffset(Child, Offset);
Bill Wendling480ff322009-05-20 23:21:38 +00001848
1849 // End of children marker.
1850 Offset += sizeof(int8_t);
1851 }
1852
1853 Die->setSize(Offset - Die->getOffset());
1854 return Offset;
1855}
1856
Eric Christopherb088d2d2013-10-24 21:05:08 +00001857// Compute the size and offset for each DIE.
Eric Christopherf8194852013-12-05 18:06:10 +00001858void DwarfFile::computeSizeAndOffsets() {
Manman Rence20d462013-10-29 22:57:10 +00001859 // Offset from the first CU in the debug info section is 0 initially.
1860 unsigned SecOffset = 0;
1861
Eric Christopherb088d2d2013-10-24 21:05:08 +00001862 // Iterate over each compile unit and set the size and offsets for each
1863 // DIE within each compile unit. All offsets are CU relative.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001864 for (DwarfUnit *TheU : CUs) {
1865 TheU->setDebugInfoOffset(SecOffset);
Manman Rence20d462013-10-29 22:57:10 +00001866
1867 // CU-relative offset is reset to 0 here.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001868 unsigned Offset = sizeof(int32_t) + // Length of Unit Info
Benjamin Kramer15596c72014-03-07 19:09:39 +00001869 TheU->getHeaderSize(); // Unit-specific headers
Manman Rence20d462013-10-29 22:57:10 +00001870
1871 // EndOffset here is CU-relative, after laying out
1872 // all of the CU DIE.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001873 unsigned EndOffset = computeSizeAndOffset(TheU->getUnitDie(), Offset);
Manman Rence20d462013-10-29 22:57:10 +00001874 SecOffset += EndOffset;
Devang Patel1a0df9a2010-05-10 22:49:55 +00001875 }
Bill Wendling480ff322009-05-20 23:21:38 +00001876}
1877
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001878// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001879void DwarfDebug::emitSectionLabels() {
Chris Lattner4b7dadb2009-08-19 05:49:37 +00001880 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarc418d6b2009-09-19 20:40:05 +00001881
Bill Wendling480ff322009-05-20 23:21:38 +00001882 // Dwarf sections base addresses.
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001883 DwarfInfoSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001884 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Eric Christopherd8667202013-12-30 17:22:27 +00001885 if (useSplitDwarf())
1886 DwarfInfoDWOSectionSym =
1887 emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001888 DwarfAbbrevSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001889 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
Eric Christopher3c5a1912012-12-19 22:02:53 +00001890 if (useSplitDwarf())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001891 DwarfAbbrevDWOSectionSym = emitSectionSym(
1892 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
David Blaikie20474102014-02-25 22:46:44 +00001893 if (GenerateARangeSection)
1894 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001895
Eric Christopher74804332013-02-07 21:19:50 +00001896 DwarfLineSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001897 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001898 emitSectionSym(Asm, TLOF.getDwarfLocSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001899 if (GenerateGnuPubSections) {
Eric Christopher39eebfa2013-09-30 23:14:16 +00001900 DwarfGnuPubNamesSectionSym =
1901 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1902 DwarfGnuPubTypesSectionSym =
1903 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christopher261d2342013-09-23 20:55:35 +00001904 } else if (HasDwarfPubSections) {
1905 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1906 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmanc89466f2013-09-04 04:39:38 +00001907 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001908
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001909 DwarfStrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001910 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001911 if (useSplitDwarf()) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001912 DwarfStrDWOSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001913 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher55863be2013-04-07 03:43:09 +00001914 DwarfAddrSectionSym =
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001915 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
Eric Christopher55863be2013-04-07 03:43:09 +00001916 }
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001917 DwarfDebugRangeSectionSym =
1918 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling480ff322009-05-20 23:21:38 +00001919
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001920 DwarfDebugLocSectionSym =
1921 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
Bill Wendling480ff322009-05-20 23:21:38 +00001922}
1923
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001924// Recursively emits a debug information entry.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001925void DwarfDebug::emitDIE(DIE *Die) {
Bill Wendling480ff322009-05-20 23:21:38 +00001926 // Get the abbreviation for this DIE.
David Blaikieff3ab2c2013-12-05 01:01:41 +00001927 const DIEAbbrev &Abbrev = Die->getAbbrev();
Bill Wendling480ff322009-05-20 23:21:38 +00001928
Bill Wendling480ff322009-05-20 23:21:38 +00001929 // Emit the code (index) for the abbreviation.
Chris Lattner7bde8c02010-04-04 18:52:31 +00001930 if (Asm->isVerbose())
David Blaikieff3ab2c2013-12-05 01:01:41 +00001931 Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
1932 "] 0x" + Twine::utohexstr(Die->getOffset()) +
1933 ":0x" + Twine::utohexstr(Die->getSize()) + " " +
1934 dwarf::TagString(Abbrev.getTag()));
1935 Asm->EmitULEB128(Abbrev.getNumber());
Bill Wendling480ff322009-05-20 23:21:38 +00001936
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001937 const SmallVectorImpl<DIEValue *> &Values = Die->getValues();
David Blaikieff3ab2c2013-12-05 01:01:41 +00001938 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling480ff322009-05-20 23:21:38 +00001939
1940 // Emit the DIE attribute values.
1941 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikief2443192013-10-21 17:28:37 +00001942 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
1943 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling480ff322009-05-20 23:21:38 +00001944 assert(Form && "Too many attributes for DIE (check abbreviation)");
1945
Eric Christopher13a1bb32014-03-06 00:00:49 +00001946 if (Asm->isVerbose()) {
Chris Lattner5adf9872010-01-24 18:54:17 +00001947 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Eric Christopher13a1bb32014-03-06 00:00:49 +00001948 if (Attr == dwarf::DW_AT_accessibility)
1949 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(
1950 cast<DIEInteger>(Values[i])->getValue()));
1951 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001952
Eric Christopherdd508382014-03-06 00:00:56 +00001953 // Emit an attribute using the defined form.
1954 Values[i]->EmitValue(Asm, Form);
Bill Wendling480ff322009-05-20 23:21:38 +00001955 }
1956
1957 // Emit the DIE children if any.
Eric Christophere8f10722014-03-05 01:44:58 +00001958 if (Abbrev.hasChildren()) {
Bill Wendling480ff322009-05-20 23:21:38 +00001959 const std::vector<DIE *> &Children = Die->getChildren();
1960
Benjamin Kramer15596c72014-03-07 19:09:39 +00001961 for (DIE *Child : Children)
1962 emitDIE(Child);
Bill Wendling480ff322009-05-20 23:21:38 +00001963
David Blaikie155f8812013-12-04 21:51:05 +00001964 Asm->OutStreamer.AddComment("End Of Children Mark");
Chris Lattner566cae92010-03-09 23:52:58 +00001965 Asm->EmitInt8(0);
Bill Wendling480ff322009-05-20 23:21:38 +00001966 }
1967}
1968
Eric Christophera2de8262012-12-15 00:04:07 +00001969// Emit the various dwarf units to the unit section USection with
1970// the abbreviations going into ASection.
David Blaikie03073f72013-12-06 22:14:48 +00001971void DwarfFile::emitUnits(DwarfDebug *DD, const MCSection *ASection,
1972 const MCSymbol *ASectionSym) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00001973 for (DwarfUnit *TheU : CUs) {
David Blaikie2a80e442013-12-02 22:09:48 +00001974 DIE *Die = TheU->getUnitDie();
David Blaikie03073f72013-12-06 22:14:48 +00001975 const MCSection *USection = TheU->getSection();
1976 Asm->OutStreamer.SwitchSection(USection);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001977
Devang Patel1a0df9a2010-05-10 22:49:55 +00001978 // Emit the compile units header.
David Blaikie7d734602013-12-06 22:33:05 +00001979 Asm->OutStreamer.EmitLabel(TheU->getLabelBegin());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001980
Devang Patel1a0df9a2010-05-10 22:49:55 +00001981 // Emit size of content not including length itself
David Blaikie6b288cf2013-10-30 20:42:41 +00001982 Asm->OutStreamer.AddComment("Length of Unit");
David Blaikie2a80e442013-12-02 22:09:48 +00001983 Asm->EmitInt32(TheU->getHeaderSize() + Die->getSize());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001984
David Blaikie2a80e442013-12-02 22:09:48 +00001985 TheU->emitHeader(ASection, ASectionSym);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001986
David Blaikieff3ab2c2013-12-05 01:01:41 +00001987 DD->emitDIE(Die);
David Blaikie7d734602013-12-06 22:33:05 +00001988 Asm->OutStreamer.EmitLabel(TheU->getLabelEnd());
Devang Patel1a0df9a2010-05-10 22:49:55 +00001989 }
Bill Wendling480ff322009-05-20 23:21:38 +00001990}
1991
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001992// Emit the debug info section.
1993void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00001994 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christophera2de8262012-12-15 00:04:07 +00001995
David Blaikie03073f72013-12-06 22:14:48 +00001996 Holder.emitUnits(this, Asm->getObjFileLowering().getDwarfAbbrevSection(),
Eric Christophera2de8262012-12-15 00:04:07 +00001997 DwarfAbbrevSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001998}
1999
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002000// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00002001void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00002002 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00002003
2004 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00002005}
Bill Wendling480ff322009-05-20 23:21:38 +00002006
Eric Christopherf8194852013-12-05 18:06:10 +00002007void DwarfFile::emitAbbrevs(const MCSection *Section) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002008 // Check to see if it is worth the effort.
David Blaikie0504cda2013-12-05 07:43:55 +00002009 if (!Abbreviations.empty()) {
Eric Christopher3c5a1912012-12-19 22:02:53 +00002010 // Start the debug abbrev section.
2011 Asm->OutStreamer.SwitchSection(Section);
2012
Bill Wendling480ff322009-05-20 23:21:38 +00002013 // For each abbrevation.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002014 for (const DIEAbbrev *Abbrev : Abbreviations) {
Bill Wendling480ff322009-05-20 23:21:38 +00002015 // Emit the abbrevations code (base 1 index.)
Chris Lattner9efd1182010-04-04 19:09:29 +00002016 Asm->EmitULEB128(Abbrev->getNumber(), "Abbreviation Code");
Bill Wendling480ff322009-05-20 23:21:38 +00002017
2018 // Emit the abbreviations data.
Chris Lattner3a383cb2010-04-05 00:13:49 +00002019 Abbrev->Emit(Asm);
Bill Wendling480ff322009-05-20 23:21:38 +00002020 }
2021
2022 // Mark end of abbreviations.
Chris Lattner9efd1182010-04-04 19:09:29 +00002023 Asm->EmitULEB128(0, "EOM(3)");
Bill Wendling480ff322009-05-20 23:21:38 +00002024 }
2025}
2026
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002027// Emit the last address of the section and the end of the line matrix.
Devang Patel930143b2009-11-21 02:48:08 +00002028void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling480ff322009-05-20 23:21:38 +00002029 // Define last address of section.
Chris Lattner566cae92010-03-09 23:52:58 +00002030 Asm->OutStreamer.AddComment("Extended Op");
2031 Asm->EmitInt8(0);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002032
Chris Lattner566cae92010-03-09 23:52:58 +00002033 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth5da3f052012-11-01 09:14:31 +00002034 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner566cae92010-03-09 23:52:58 +00002035 Asm->OutStreamer.AddComment("DW_LNE_set_address");
2036 Asm->EmitInt8(dwarf::DW_LNE_set_address);
2037
2038 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerb245dfb2010-03-10 01:17:49 +00002039
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002040 Asm->OutStreamer.EmitSymbolValue(
2041 Asm->GetTempSymbol("section_end", SectionEnd),
2042 Asm->getDataLayout().getPointerSize());
Bill Wendling480ff322009-05-20 23:21:38 +00002043
2044 // Mark end of matrix.
Chris Lattner566cae92010-03-09 23:52:58 +00002045 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
2046 Asm->EmitInt8(0);
Chris Lattnerf5c834f2010-01-22 22:09:00 +00002047 Asm->EmitInt8(1);
Chris Lattnerfa823552010-01-22 23:18:42 +00002048 Asm->EmitInt8(1);
Bill Wendling480ff322009-05-20 23:21:38 +00002049}
2050
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002051// Emit visible names into a hashed accelerator table section.
Eric Christopher4996c702011-11-07 09:24:32 +00002052void DwarfDebug::emitAccelNames() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002053 DwarfAccelTable AT(
2054 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002055 for (DwarfUnit *TheU : getUnits()) {
2056 for (const auto &GI : TheU->getAccelNames()) {
2057 StringRef Name = GI.getKey();
2058 for (const DIE *D : GI.second)
2059 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002060 }
2061 }
2062
2063 AT.FinalizeTable(Asm, "Names");
2064 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002065 Asm->getObjFileLowering().getDwarfAccelNamesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002066 MCSymbol *SectionBegin = Asm->GetTempSymbol("names_begin");
2067 Asm->OutStreamer.EmitLabel(SectionBegin);
2068
2069 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002070 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002071}
2072
Eric Christopher48fef592012-12-20 21:58:40 +00002073// Emit objective C classes and categories into a hashed accelerator table
2074// section.
Eric Christopher4996c702011-11-07 09:24:32 +00002075void DwarfDebug::emitAccelObjC() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002076 DwarfAccelTable AT(
2077 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002078 for (DwarfUnit *TheU : getUnits()) {
2079 for (const auto &GI : TheU->getAccelObjC()) {
2080 StringRef Name = GI.getKey();
2081 for (const DIE *D : GI.second)
2082 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002083 }
2084 }
2085
2086 AT.FinalizeTable(Asm, "ObjC");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002087 Asm->OutStreamer.SwitchSection(
2088 Asm->getObjFileLowering().getDwarfAccelObjCSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002089 MCSymbol *SectionBegin = Asm->GetTempSymbol("objc_begin");
2090 Asm->OutStreamer.EmitLabel(SectionBegin);
2091
2092 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002093 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002094}
2095
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002096// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002097void DwarfDebug::emitAccelNamespaces() {
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002098 DwarfAccelTable AT(
2099 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002100 for (DwarfUnit *TheU : getUnits()) {
2101 for (const auto &GI : TheU->getAccelNamespace()) {
2102 StringRef Name = GI.getKey();
2103 for (const DIE *D : GI.second)
2104 AT.AddName(Name, D);
Eric Christopher4996c702011-11-07 09:24:32 +00002105 }
2106 }
2107
2108 AT.FinalizeTable(Asm, "namespac");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002109 Asm->OutStreamer.SwitchSection(
2110 Asm->getObjFileLowering().getDwarfAccelNamespaceSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002111 MCSymbol *SectionBegin = Asm->GetTempSymbol("namespac_begin");
2112 Asm->OutStreamer.EmitLabel(SectionBegin);
2113
2114 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002115 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002116}
2117
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002118// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00002119void DwarfDebug::emitAccelTypes() {
Eric Christopher21bde872012-01-06 04:35:23 +00002120 std::vector<DwarfAccelTable::Atom> Atoms;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002121 Atoms.push_back(
2122 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4));
2123 Atoms.push_back(
2124 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2));
2125 Atoms.push_back(
2126 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1));
Eric Christopher21bde872012-01-06 04:35:23 +00002127 DwarfAccelTable AT(Atoms);
Benjamin Kramer15596c72014-03-07 19:09:39 +00002128 for (DwarfUnit *TheU : getUnits()) {
2129 for (const auto &GI : TheU->getAccelTypes()) {
2130 StringRef Name = GI.getKey();
2131 for (const auto &DI : GI.second)
2132 AT.AddName(Name, DI.first, DI.second);
Eric Christopher4996c702011-11-07 09:24:32 +00002133 }
2134 }
2135
2136 AT.FinalizeTable(Asm, "types");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002137 Asm->OutStreamer.SwitchSection(
2138 Asm->getObjFileLowering().getDwarfAccelTypesSection());
Eric Christopher4996c702011-11-07 09:24:32 +00002139 MCSymbol *SectionBegin = Asm->GetTempSymbol("types_begin");
2140 Asm->OutStreamer.EmitLabel(SectionBegin);
2141
2142 // Emit the full data.
Eric Christophere698f532012-12-20 21:58:36 +00002143 AT.Emit(Asm, SectionBegin, &InfoHolder);
Eric Christopher4996c702011-11-07 09:24:32 +00002144}
2145
Eric Christopherdd1a0122013-09-13 00:35:05 +00002146// Public name handling.
2147// The format for the various pubnames:
2148//
2149// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
2150// for the DIE that is named.
2151//
2152// gnu pubnames - offset/index value/name tuples where the offset is the offset
2153// into the CU and the index value is computed according to the type of value
2154// for the DIE that is named.
2155//
2156// For type units the offset is the offset of the skeleton DIE. For split dwarf
2157// it's the offset within the debug_info/debug_types dwo section, however, the
2158// reference in the pubname header doesn't change.
2159
2160/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00002161static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00002162 const DIE *Die) {
Eric Christopherd2b497b2013-10-16 01:37:49 +00002163 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
2164
2165 // We could have a specification DIE that has our most of our knowledge,
2166 // look for that now.
2167 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
2168 if (SpecVal) {
2169 DIE *SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
2170 if (SpecDIE->findAttribute(dwarf::DW_AT_external))
2171 Linkage = dwarf::GIEL_EXTERNAL;
2172 } else if (Die->findAttribute(dwarf::DW_AT_external))
2173 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002174
2175 switch (Die->getTag()) {
2176 case dwarf::DW_TAG_class_type:
2177 case dwarf::DW_TAG_structure_type:
2178 case dwarf::DW_TAG_union_type:
2179 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00002180 return dwarf::PubIndexEntryDescriptor(
2181 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
2182 ? dwarf::GIEL_STATIC
2183 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002184 case dwarf::DW_TAG_typedef:
2185 case dwarf::DW_TAG_base_type:
2186 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00002187 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002188 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00002189 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002190 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00002191 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002192 case dwarf::DW_TAG_constant:
2193 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00002194 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002195 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00002196 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
2197 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00002198 default:
David Blaikie8dec4072013-09-19 20:40:26 +00002199 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00002200 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00002201}
2202
Eric Christopher5f93bb92013-09-09 20:03:17 +00002203/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002204///
Eric Christopherdd1a0122013-09-13 00:35:05 +00002205void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Eric Christopherdd1a0122013-09-13 00:35:05 +00002206 const MCSection *PSec =
2207 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
2208 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002209
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002210 for (const auto &NU : CUMap) {
2211 DwarfCompileUnit *TheU = NU.second;
2212 if (auto Skeleton = static_cast<DwarfCompileUnit *>(TheU->getSkeleton()))
2213 TheU = Skeleton;
David Blaikie2a80e442013-12-02 22:09:48 +00002214 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002215
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002216 // Start the dwarf pubnames section.
Eric Christopher13b99d22013-09-10 21:49:37 +00002217 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002218
Eric Christopher39eebfa2013-09-30 23:14:16 +00002219 // Emit a label so we can reference the beginning of this pubname section.
2220 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002221 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubnames", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002222
Eric Christopherdd1a0122013-09-13 00:35:05 +00002223 // Emit the header.
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002224 Asm->OutStreamer.AddComment("Length of Public Names Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002225 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubnames_begin", ID);
2226 MCSymbol *EndLabel = Asm->GetTempSymbol("pubnames_end", ID);
2227 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002228
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002229 Asm->OutStreamer.EmitLabel(BeginLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002230
2231 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002232 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002233
2234 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002235 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002236
2237 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002238 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002239
Eric Christopherdd1a0122013-09-13 00:35:05 +00002240 // Emit the pubnames for this compilation unit.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002241 for (const auto &GI : getUnits()[ID]->getGlobalNames()) {
2242 const char *Name = GI.getKeyData();
2243 const DIE *Entity = GI.second;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002244
2245 Asm->OutStreamer.AddComment("DIE offset");
2246 Asm->EmitInt32(Entity->getOffset());
2247
Eric Christopherdd1a0122013-09-13 00:35:05 +00002248 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002249 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002250 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002251 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002252 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002253 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002254 }
2255
David Blaikie155f8812013-12-04 21:51:05 +00002256 Asm->OutStreamer.AddComment("External Name");
Benjamin Kramer15596c72014-03-07 19:09:39 +00002257 Asm->OutStreamer.EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002258 }
2259
2260 Asm->OutStreamer.AddComment("End Mark");
2261 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002262 Asm->OutStreamer.EmitLabel(EndLabel);
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00002263 }
2264}
2265
Eric Christopherdd1a0122013-09-13 00:35:05 +00002266void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christopher261d2342013-09-23 20:55:35 +00002267 const MCSection *PSec =
2268 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
2269 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher8b3737f2013-09-13 00:34:58 +00002270
David Blaikiec3d9e9e2014-03-06 01:42:00 +00002271 for (const auto &NU : CUMap) {
2272 DwarfCompileUnit *TheU = NU.second;
2273 if (auto Skeleton = static_cast<DwarfCompileUnit *>(TheU->getSkeleton()))
2274 TheU = Skeleton;
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002275 unsigned ID = TheU->getUniqueID();
2276
Eric Christopher6abc9c52011-11-07 09:18:35 +00002277 // Start the dwarf pubtypes section.
Eric Christopher8b3737f2013-09-13 00:34:58 +00002278 Asm->OutStreamer.SwitchSection(PSec);
Eric Christopher39eebfa2013-09-30 23:14:16 +00002279
2280 // Emit a label so we can reference the beginning of this pubtype section.
2281 if (GnuStyle)
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002282 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("gnu_pubtypes", ID));
Eric Christopher39eebfa2013-09-30 23:14:16 +00002283
2284 // Emit the header.
Devang Patel1a0df9a2010-05-10 22:49:55 +00002285 Asm->OutStreamer.AddComment("Length of Public Types Info");
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002286 MCSymbol *BeginLabel = Asm->GetTempSymbol("pubtypes_begin", ID);
2287 MCSymbol *EndLabel = Asm->GetTempSymbol("pubtypes_end", ID);
2288 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002289
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002290 Asm->OutStreamer.EmitLabel(BeginLabel);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002291
David Blaikie155f8812013-12-04 21:51:05 +00002292 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemered89b5c2013-08-21 06:13:34 +00002293 Asm->EmitInt16(dwarf::DW_PUBTYPES_VERSION);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002294
Devang Patel1a0df9a2010-05-10 22:49:55 +00002295 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
David Blaikie7d734602013-12-06 22:33:05 +00002296 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002297
Devang Patel1a0df9a2010-05-10 22:49:55 +00002298 Asm->OutStreamer.AddComment("Compilation Unit Length");
David Blaikie7d734602013-12-06 22:33:05 +00002299 Asm->EmitLabelDifference(TheU->getLabelEnd(), TheU->getLabelBegin(), 4);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002300
Eric Christopher39eebfa2013-09-30 23:14:16 +00002301 // Emit the pubtypes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002302 for (const auto &GI : getUnits()[ID]->getGlobalTypes()) {
2303 const char *Name = GI.getKeyData();
2304 const DIE *Entity = GI.second;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002305
David Blaikie155f8812013-12-04 21:51:05 +00002306 Asm->OutStreamer.AddComment("DIE offset");
Devang Patel1a0df9a2010-05-10 22:49:55 +00002307 Asm->EmitInt32(Entity->getOffset());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002308
Eric Christopherdd1a0122013-09-13 00:35:05 +00002309 if (GnuStyle) {
David Blaikie2a80e442013-12-02 22:09:48 +00002310 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikied0a869d2013-09-19 22:19:37 +00002311 Asm->OutStreamer.AddComment(
David Blaikieefd0bcb2013-09-20 00:33:15 +00002312 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie404d3042013-09-19 23:01:29 +00002313 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikied0a869d2013-09-19 22:19:37 +00002314 Asm->EmitInt8(Desc.toBits());
Eric Christopherdd1a0122013-09-13 00:35:05 +00002315 }
2316
David Blaikie155f8812013-12-04 21:51:05 +00002317 Asm->OutStreamer.AddComment("External Name");
Eric Christopher8b3737f2013-09-13 00:34:58 +00002318
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002319 // Emit the name with a terminating null byte.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002320 Asm->OutStreamer.EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
Devang Patel1a0df9a2010-05-10 22:49:55 +00002321 }
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002322
Devang Patel1a0df9a2010-05-10 22:49:55 +00002323 Asm->OutStreamer.AddComment("End Mark");
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002324 Asm->EmitInt32(0);
David Blaikieb7a1c4d2013-12-04 17:55:41 +00002325 Asm->OutStreamer.EmitLabel(EndLabel);
Devang Patel04d2f2d2009-11-24 01:14:22 +00002326 }
Devang Patel04d2f2d2009-11-24 01:14:22 +00002327}
2328
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002329// Emit strings into a string section.
Eric Christopherf8194852013-12-05 18:06:10 +00002330void DwarfFile::emitStrings(const MCSection *StrSection,
Eric Christophera5a79422013-12-09 23:32:48 +00002331 const MCSection *OffsetSection = NULL,
2332 const MCSymbol *StrSecSym = NULL) {
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002333
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002334 if (StringPool.empty())
2335 return;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002336
Chris Lattner3d72a672010-03-09 23:38:23 +00002337 // Start the dwarf str section.
Eric Christopher2cbd5762013-01-07 19:32:41 +00002338 Asm->OutStreamer.SwitchSection(StrSection);
Bill Wendling480ff322009-05-20 23:21:38 +00002339
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002340 // Get all of the string pool entries and put them in an array by their ID so
2341 // we can sort them.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002342 SmallVector<std::pair<unsigned, const StrPool::value_type *>, 64 > Entries;
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002343
Benjamin Kramer15596c72014-03-07 19:09:39 +00002344 for (const auto &I : StringPool)
2345 Entries.push_back(std::make_pair(I.second.second, &I));
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002346
Chris Lattnerb7aa9522010-03-13 02:17:42 +00002347 array_pod_sort(Entries.begin(), Entries.end());
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002348
Benjamin Kramer15596c72014-03-07 19:09:39 +00002349 for (const auto &Entry : Entries) {
Chris Lattner3d72a672010-03-09 23:38:23 +00002350 // Emit a label for reference from debug information entries.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002351 Asm->OutStreamer.EmitLabel(Entry.second->getValue().first);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00002352
Benjamin Kramer966ed1b2011-11-09 18:16:11 +00002353 // Emit the string itself with a terminating null byte.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002354 Asm->OutStreamer.EmitBytes(StringRef(Entry.second->getKeyData(),
2355 Entry.second->getKeyLength() + 1));
Bill Wendling480ff322009-05-20 23:21:38 +00002356 }
Eric Christopher2cbd5762013-01-07 19:32:41 +00002357
2358 // If we've got an offset section go ahead and emit that now as well.
2359 if (OffsetSection) {
2360 Asm->OutStreamer.SwitchSection(OffsetSection);
2361 unsigned offset = 0;
Eric Christopher962c9082013-01-15 23:56:56 +00002362 unsigned size = 4; // FIXME: DWARF64 is 8.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002363 for (const auto &Entry : Entries) {
Eric Christopherbf7bc492013-01-09 03:52:05 +00002364 Asm->OutStreamer.EmitIntValue(offset, size);
Benjamin Kramer15596c72014-03-07 19:09:39 +00002365 offset += Entry.second->getKeyLength() + 1;
Eric Christopher2cbd5762013-01-07 19:32:41 +00002366 }
2367 }
Bill Wendling480ff322009-05-20 23:21:38 +00002368}
2369
Eric Christopher65132a82013-11-19 09:11:26 +00002370// Emit addresses into the section given.
Eric Christopherf8194852013-12-05 18:06:10 +00002371void DwarfFile::emitAddresses(const MCSection *AddrSection) {
Eric Christopher962c9082013-01-15 23:56:56 +00002372
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002373 if (AddressPool.empty())
2374 return;
Eric Christopher962c9082013-01-15 23:56:56 +00002375
2376 // Start the dwarf addr section.
2377 Asm->OutStreamer.SwitchSection(AddrSection);
2378
David Blaikiece1960f2013-07-08 17:51:28 +00002379 // Order the address pool entries by ID
David Blaikieac569a62013-07-08 17:33:10 +00002380 SmallVector<const MCExpr *, 64> Entries(AddressPool.size());
Eric Christopher962c9082013-01-15 23:56:56 +00002381
Benjamin Kramer15596c72014-03-07 19:09:39 +00002382 for (const auto &I : AddressPool)
2383 Entries[I.second.Number] =
2384 I.second.TLS
2385 ? Asm->getObjFileLowering().getDebugThreadLocalSymbol(I.first)
2386 : MCSymbolRefExpr::Create(I.first, Asm->OutContext);
Eric Christopher962c9082013-01-15 23:56:56 +00002387
Benjamin Kramer15596c72014-03-07 19:09:39 +00002388 for (const MCExpr *Entry : Entries)
2389 Asm->OutStreamer.EmitValue(Entry, Asm->getDataLayout().getPointerSize());
Eric Christopher962c9082013-01-15 23:56:56 +00002390}
2391
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002392// Emit visible names into a debug str section.
2393void DwarfDebug::emitDebugStr() {
Eric Christopherf8194852013-12-05 18:06:10 +00002394 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002395 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
2396}
2397
Eric Christopher9046f942013-07-02 21:36:07 +00002398// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00002399void DwarfDebug::emitDebugLoc() {
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002400 if (DotDebugLocEntries.empty())
2401 return;
2402
Eric Christopher4887c8f2013-03-29 23:34:06 +00002403 for (SmallVectorImpl<DotDebugLocEntry>::iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002404 I = DotDebugLocEntries.begin(),
2405 E = DotDebugLocEntries.end();
Devang Patel116a9d72011-02-04 22:57:18 +00002406 I != E; ++I) {
2407 DotDebugLocEntry &Entry = *I;
2408 if (I + 1 != DotDebugLocEntries.end())
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002409 Entry.Merge(I + 1);
Devang Patel116a9d72011-02-04 22:57:18 +00002410 }
2411
Daniel Dunbarfd95b012011-03-16 22:16:39 +00002412 // Start the dwarf loc section.
Chris Lattner4b7dadb2009-08-19 05:49:37 +00002413 Asm->OutStreamer.SwitchSection(
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002414 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth5da3f052012-11-01 09:14:31 +00002415 unsigned char Size = Asm->getDataLayout().getPointerSize();
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002416 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", 0));
2417 unsigned index = 1;
Eric Christophereeb51952014-03-06 19:51:16 +00002418 for (SmallVectorImpl<DotDebugLocEntry>::const_iterator
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002419 I = DotDebugLocEntries.begin(),
2420 E = DotDebugLocEntries.end();
Devang Patel30265c42010-07-07 20:12:52 +00002421 I != E; ++I, ++index) {
Eric Christophereeb51952014-03-06 19:51:16 +00002422 const DotDebugLocEntry &Entry = *I;
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002423 if (Entry.isMerged())
2424 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00002425 if (Entry.isEmpty()) {
Eric Christopherce0cfce2013-01-09 01:35:34 +00002426 Asm->OutStreamer.EmitIntValue(0, Size);
2427 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patel6b9a9fe2010-05-26 23:55:23 +00002428 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("debug_loc", index));
Devang Patel9fc11702010-05-25 23:40:22 +00002429 } else {
Eric Christopher25f06422013-07-03 22:40:18 +00002430 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
2431 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
2432 DIVariable DV(Entry.getVariable());
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002433 Asm->OutStreamer.AddComment("Loc expr size");
2434 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
2435 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
2436 Asm->EmitLabelDifference(end, begin, 2);
2437 Asm->OutStreamer.EmitLabel(begin);
Devang Pateled9fd452011-07-08 16:49:43 +00002438 if (Entry.isInt()) {
Devang Patel324f8432011-06-01 22:03:25 +00002439 DIBasicType BTy(DV.getType());
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002440 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
2441 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
Devang Patel324f8432011-06-01 22:03:25 +00002442 Asm->OutStreamer.AddComment("DW_OP_consts");
2443 Asm->EmitInt8(dwarf::DW_OP_consts);
Devang Pateled9fd452011-07-08 16:49:43 +00002444 Asm->EmitSLEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002445 } else {
2446 Asm->OutStreamer.AddComment("DW_OP_constu");
2447 Asm->EmitInt8(dwarf::DW_OP_constu);
Devang Pateled9fd452011-07-08 16:49:43 +00002448 Asm->EmitULEB128(Entry.getInt());
Devang Patel324f8432011-06-01 22:03:25 +00002449 }
Devang Pateled9fd452011-07-08 16:49:43 +00002450 } else if (Entry.isLocation()) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002451 MachineLocation Loc = Entry.getLoc();
Eric Christopher6a841382012-11-19 22:42:10 +00002452 if (!DV.hasComplexAddress())
Devang Pateled9fd452011-07-08 16:49:43 +00002453 // Regular entry.
Eric Christopher614a89f2013-07-03 22:40:21 +00002454 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002455 else {
2456 // Complex address entry.
2457 unsigned N = DV.getNumAddrElements();
2458 unsigned i = 0;
2459 if (N >= 2 && DV.getAddrElement(0) == DIBuilder::OpPlus) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002460 if (Loc.getOffset()) {
Devang Pateled9fd452011-07-08 16:49:43 +00002461 i = 2;
Eric Christopher614a89f2013-07-03 22:40:21 +00002462 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002463 Asm->OutStreamer.AddComment("DW_OP_deref");
2464 Asm->EmitInt8(dwarf::DW_OP_deref);
2465 Asm->OutStreamer.AddComment("DW_OP_plus_uconst");
2466 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2467 Asm->EmitSLEB128(DV.getAddrElement(1));
2468 } else {
2469 // If first address element is OpPlus then emit
2470 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Eric Christopher614a89f2013-07-03 22:40:21 +00002471 MachineLocation TLoc(Loc.getReg(), DV.getAddrElement(1));
2472 Asm->EmitDwarfRegOp(TLoc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002473 i = 2;
2474 }
2475 } else {
Eric Christopher614a89f2013-07-03 22:40:21 +00002476 Asm->EmitDwarfRegOp(Loc, DV.isIndirect());
Devang Pateled9fd452011-07-08 16:49:43 +00002477 }
Eric Christopher6a841382012-11-19 22:42:10 +00002478
Devang Pateled9fd452011-07-08 16:49:43 +00002479 // Emit remaining complex address elements.
2480 for (; i < N; ++i) {
2481 uint64_t Element = DV.getAddrElement(i);
2482 if (Element == DIBuilder::OpPlus) {
2483 Asm->EmitInt8(dwarf::DW_OP_plus_uconst);
2484 Asm->EmitULEB128(DV.getAddrElement(++i));
Eric Christopher4d250522012-05-08 18:56:00 +00002485 } else if (Element == DIBuilder::OpDeref) {
Eric Christopher614a89f2013-07-03 22:40:21 +00002486 if (!Loc.isReg())
Eric Christopher4d250522012-05-08 18:56:00 +00002487 Asm->EmitInt8(dwarf::DW_OP_deref);
2488 } else
2489 llvm_unreachable("unknown Opcode found in complex address");
Devang Pateled9fd452011-07-08 16:49:43 +00002490 }
Devang Patel3e021532011-04-28 02:22:40 +00002491 }
Devang Patel3e021532011-04-28 02:22:40 +00002492 }
Devang Pateled9fd452011-07-08 16:49:43 +00002493 // else ... ignore constant fp. There is not any good way to
2494 // to represent them here in dwarf.
Rafael Espindolad23bfb82011-05-27 22:05:41 +00002495 Asm->OutStreamer.EmitLabel(end);
Devang Patel9fc11702010-05-25 23:40:22 +00002496 }
2497 }
Bill Wendling480ff322009-05-20 23:21:38 +00002498}
2499
Richard Mitton21101b32013-09-19 23:21:01 +00002500struct ArangeSpan {
2501 const MCSymbol *Start, *End;
2502};
2503
2504// Emit a debug aranges section, containing a CU lookup for any
2505// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00002506void DwarfDebug::emitDebugARanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002507 // Start the dwarf aranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002508 Asm->OutStreamer.SwitchSection(
2509 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton21101b32013-09-19 23:21:01 +00002510
Eric Christopher4287a492013-12-09 23:57:44 +00002511 typedef DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan> > SpansType;
Richard Mitton21101b32013-09-19 23:21:01 +00002512
2513 SpansType Spans;
2514
2515 // Build a list of sections used.
2516 std::vector<const MCSection *> Sections;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002517 for (const auto &it : SectionMap) {
2518 const MCSection *Section = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00002519 Sections.push_back(Section);
2520 }
2521
2522 // Sort the sections into order.
2523 // This is only done to ensure consistent output order across different runs.
2524 std::sort(Sections.begin(), Sections.end(), SectionSort);
2525
2526 // Build a set of address spans, sorted by CU.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002527 for (const MCSection *Section : Sections) {
Richard Mitton21101b32013-09-19 23:21:01 +00002528 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
2529 if (List.size() < 2)
2530 continue;
2531
2532 // Sort the symbols by offset within the section.
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00002533 std::sort(List.begin(), List.end(),
2534 [&](const SymbolCU &A, const SymbolCU &B) {
2535 unsigned IA = A.Sym ? Asm->OutStreamer.GetSymbolOrder(A.Sym) : 0;
2536 unsigned IB = B.Sym ? Asm->OutStreamer.GetSymbolOrder(B.Sym) : 0;
2537
2538 // Symbols with no order assigned should be placed at the end.
2539 // (e.g. section end labels)
2540 if (IA == 0)
2541 return false;
2542 if (IB == 0)
2543 return true;
2544 return IA < IB;
2545 });
Richard Mitton21101b32013-09-19 23:21:01 +00002546
2547 // If we have no section (e.g. common), just write out
2548 // individual spans for each symbol.
2549 if (Section == NULL) {
Benjamin Kramer15596c72014-03-07 19:09:39 +00002550 for (const SymbolCU &Cur : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00002551 ArangeSpan Span;
2552 Span.Start = Cur.Sym;
2553 Span.End = NULL;
2554 if (Cur.CU)
2555 Spans[Cur.CU].push_back(Span);
2556 }
2557 } else {
2558 // Build spans between each label.
2559 const MCSymbol *StartSym = List[0].Sym;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002560 for (size_t n = 1, e = List.size(); n < e; n++) {
Richard Mitton21101b32013-09-19 23:21:01 +00002561 const SymbolCU &Prev = List[n - 1];
2562 const SymbolCU &Cur = List[n];
2563
2564 // Try and build the longest span we can within the same CU.
2565 if (Cur.CU != Prev.CU) {
2566 ArangeSpan Span;
2567 Span.Start = StartSym;
2568 Span.End = Cur.Sym;
2569 Spans[Prev.CU].push_back(Span);
2570 StartSym = Cur.Sym;
2571 }
2572 }
2573 }
2574 }
2575
Richard Mitton21101b32013-09-19 23:21:01 +00002576 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
2577
2578 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00002579 std::vector<DwarfCompileUnit *> CUs;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002580 for (const auto &it : Spans) {
2581 DwarfCompileUnit *CU = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00002582 CUs.push_back(CU);
2583 }
2584
2585 // Sort the CU list (again, to ensure consistent output order).
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00002586 std::sort(CUs.begin(), CUs.end(), [](const DwarfUnit *A, const DwarfUnit *B) {
2587 return A->getUniqueID() < B->getUniqueID();
2588 });
Richard Mitton21101b32013-09-19 23:21:01 +00002589
2590 // Emit an arange table for each CU we used.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002591 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton21101b32013-09-19 23:21:01 +00002592 std::vector<ArangeSpan> &List = Spans[CU];
2593
2594 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002595 unsigned ContentSize =
2596 sizeof(int16_t) + // DWARF ARange version number
2597 sizeof(int32_t) + // Offset of CU in the .debug_info section
2598 sizeof(int8_t) + // Pointer Size (in bytes)
2599 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002600
2601 unsigned TupleSize = PtrSize * 2;
2602
2603 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002604 unsigned Padding =
2605 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00002606
2607 ContentSize += Padding;
2608 ContentSize += (List.size() + 1) * TupleSize;
2609
2610 // For each compile unit, write the list of spans it covers.
2611 Asm->OutStreamer.AddComment("Length of ARange Set");
2612 Asm->EmitInt32(ContentSize);
2613 Asm->OutStreamer.AddComment("DWARF Arange version number");
2614 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
2615 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Eric Christopherd8667202013-12-30 17:22:27 +00002616 Asm->EmitSectionOffset(CU->getLocalLabelBegin(), CU->getLocalSectionSym());
Richard Mitton21101b32013-09-19 23:21:01 +00002617 Asm->OutStreamer.AddComment("Address Size (in bytes)");
2618 Asm->EmitInt8(PtrSize);
2619 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
2620 Asm->EmitInt8(0);
2621
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002622 Asm->OutStreamer.EmitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00002623
Benjamin Kramer15596c72014-03-07 19:09:39 +00002624 for (const ArangeSpan &Span : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00002625 Asm->EmitLabelReference(Span.Start, PtrSize);
2626
2627 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002628 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002629 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002630 } else {
2631 // For symbols without an end marker (e.g. common), we
2632 // write a single arange entry containing just that one symbol.
2633 uint64_t Size = SymSize[Span.Start];
2634 if (Size == 0)
2635 Size = 1;
2636
2637 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
2638 }
Richard Mitton21101b32013-09-19 23:21:01 +00002639 }
2640
2641 Asm->OutStreamer.AddComment("ARange terminator");
2642 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2643 Asm->OutStreamer.EmitIntValue(0, PtrSize);
2644 }
Bill Wendling480ff322009-05-20 23:21:38 +00002645}
2646
Eric Christopheracdcbdb2012-11-27 22:43:45 +00002647// Emit visible names into a debug ranges section.
Devang Patel930143b2009-11-21 02:48:08 +00002648void DwarfDebug::emitDebugRanges() {
Bill Wendling480ff322009-05-20 23:21:38 +00002649 // Start the dwarf ranges section.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002650 Asm->OutStreamer.SwitchSection(
2651 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002652
Eric Christopher0f63d062013-12-03 00:45:45 +00002653 // Size for our labels.
2654 unsigned char Size = Asm->getDataLayout().getPointerSize();
2655
2656 // Grab the specific ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002657 for (const auto &I : CUMap) {
2658 DwarfCompileUnit *TheCU = I.second;
Eric Christopher0f63d062013-12-03 00:45:45 +00002659
2660 // Emit a symbol so we can find the beginning of our ranges.
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002661 Asm->OutStreamer.EmitLabel(TheCU->getLabelRange());
Eric Christopher0f63d062013-12-03 00:45:45 +00002662
2663 // Iterate over the misc ranges for the compile units in the module.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002664 for (const RangeSpanList &List : TheCU->getRangeLists()) {
Eric Christopherf8790642013-12-04 22:04:50 +00002665 // Emit our symbol so we can find the beginning of the range.
2666 Asm->OutStreamer.EmitLabel(List.getSym());
Eric Christopher0f63d062013-12-03 00:45:45 +00002667
Benjamin Kramer15596c72014-03-07 19:09:39 +00002668 for (const RangeSpan &Range : List.getRanges()) {
Eric Christopher0f63d062013-12-03 00:45:45 +00002669 const MCSymbol *Begin = Range.getStart();
2670 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002671 assert(Begin && "Range without a begin symbol?");
2672 assert(End && "Range without an end symbol?");
2673 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2674 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002675 }
2676
2677 // And terminate the list with two 0 values.
Eric Christopherce0cfce2013-01-09 01:35:34 +00002678 Asm->OutStreamer.EmitIntValue(0, Size);
Eric Christopher0f63d062013-12-03 00:45:45 +00002679 Asm->OutStreamer.EmitIntValue(0, Size);
2680 }
Eric Christopher46e23432013-12-20 04:16:18 +00002681
2682 // Now emit a range for the CU itself.
Eric Christophera9a1d272014-02-27 07:44:45 +00002683 if (useCURanges() && TheCU->getRanges().size()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002684 Asm->OutStreamer.EmitLabel(
2685 Asm->GetTempSymbol("cu_ranges", TheCU->getUniqueID()));
Benjamin Kramer15596c72014-03-07 19:09:39 +00002686 for (const RangeSpan &Range : TheCU->getRanges()) {
Eric Christopher46e23432013-12-20 04:16:18 +00002687 const MCSymbol *Begin = Range.getStart();
2688 const MCSymbol *End = Range.getEnd();
Eric Christopher565ab112013-12-20 04:34:22 +00002689 assert(Begin && "Range without a begin symbol?");
2690 assert(End && "Range without an end symbol?");
2691 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2692 Asm->OutStreamer.EmitSymbolValue(End, Size);
Eric Christopher46e23432013-12-20 04:16:18 +00002693 }
2694 // And terminate the list with two 0 values.
2695 Asm->OutStreamer.EmitIntValue(0, Size);
2696 Asm->OutStreamer.EmitIntValue(0, Size);
2697 }
Devang Patel12563b32010-04-16 23:33:45 +00002698 }
Bill Wendling480ff322009-05-20 23:21:38 +00002699}
2700
Eric Christopherd692c1d2012-12-11 19:42:09 +00002701// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002702
David Blaikie38fe6342014-01-09 04:28:46 +00002703void DwarfDebug::initSkeletonUnit(const DwarfUnit *U, DIE *Die,
2704 DwarfUnit *NewU) {
2705 NewU->addLocalString(Die, dwarf::DW_AT_GNU_dwo_name,
2706 U->getCUNode().getSplitDebugFilename());
2707
2708 // Relocate to the beginning of the addr_base section, else 0 for the
2709 // beginning of the one for this compile unit.
2710 if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
Eric Christopherceec7b02014-01-11 00:23:18 +00002711 NewU->addSectionLabel(Die, dwarf::DW_AT_GNU_addr_base, DwarfAddrSectionSym);
David Blaikie38fe6342014-01-09 04:28:46 +00002712 else
2713 NewU->addSectionOffset(Die, dwarf::DW_AT_GNU_addr_base, 0);
2714
2715 if (!CompilationDir.empty())
2716 NewU->addLocalString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
2717
2718 addGnuPubAttributes(NewU, Die);
2719
2720 SkeletonHolder.addUnit(NewU);
2721}
2722
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002723// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2724// 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 +00002725// DW_AT_ranges_base, DW_AT_addr_base.
Eric Christopher5090d572013-12-10 00:40:03 +00002726// TODO: Implement DW_AT_ranges_base.
Eric Christopher4287a492013-12-09 23:57:44 +00002727DwarfCompileUnit *DwarfDebug::constructSkeletonCU(const DwarfCompileUnit *CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002728
2729 DIE *Die = new DIE(dwarf::DW_TAG_compile_unit);
Eric Christopher4287a492013-12-09 23:57:44 +00002730 DwarfCompileUnit *NewCU = new DwarfCompileUnit(
David Blaikief645f962014-01-09 03:23:41 +00002731 CU->getUniqueID(), Die, CU->getCUNode(), Asm, this, &SkeletonHolder);
David Blaikie1ab7c2d2013-12-09 17:51:30 +00002732 NewCU->initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
2733 DwarfInfoSectionSym);
Eric Christopher4c7765f2013-01-17 03:00:04 +00002734
David Blaikie2494fdb2014-02-14 22:41:51 +00002735 NewCU->initStmtList(DwarfLineSectionSym);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002736
David Blaikie38fe6342014-01-09 04:28:46 +00002737 initSkeletonUnit(CU, Die, NewCU);
Eric Christopherc8a310e2012-12-10 23:34:43 +00002738
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002739 return NewCU;
2740}
2741
David Blaikie15ed5eb2014-01-10 01:38:41 +00002742// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_dwo_name,
2743// DW_AT_addr_base.
David Blaikie15632ae2014-02-12 00:31:30 +00002744DwarfTypeUnit *DwarfDebug::constructSkeletonTU(DwarfTypeUnit *TU) {
David Blaikie60e63862014-02-14 23:58:13 +00002745 DwarfCompileUnit &CU = static_cast<DwarfCompileUnit &>(
2746 *SkeletonHolder.getUnits()[TU->getCU().getUniqueID()]);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002747
2748 DIE *Die = new DIE(dwarf::DW_TAG_type_unit);
David Blaikie60e63862014-02-14 23:58:13 +00002749 DwarfTypeUnit *NewTU =
2750 new DwarfTypeUnit(TU->getUniqueID(), Die, CU, Asm, this, &SkeletonHolder);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002751 NewTU->setTypeSignature(TU->getTypeSignature());
2752 NewTU->setType(NULL);
2753 NewTU->initSection(
2754 Asm->getObjFileLowering().getDwarfTypesSection(TU->getTypeSignature()));
David Blaikie60e63862014-02-14 23:58:13 +00002755 CU.applyStmtList(*Die);
David Blaikie15ed5eb2014-01-10 01:38:41 +00002756
2757 initSkeletonUnit(TU, Die, NewTU);
2758 return NewTU;
2759}
2760
Eric Christopherd692c1d2012-12-11 19:42:09 +00002761// Emit the .debug_info.dwo section for separated dwarf. This contains the
2762// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002763void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002764 assert(useSplitDwarf() && "No split dwarf debug info?");
David Blaikie03073f72013-12-06 22:14:48 +00002765 InfoHolder.emitUnits(this,
Eric Christopher3c5a1912012-12-19 22:02:53 +00002766 Asm->getObjFileLowering().getDwarfAbbrevDWOSection(),
2767 DwarfAbbrevDWOSectionSym);
2768}
2769
2770// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2771// abbreviations for the .debug_info.dwo section.
2772void DwarfDebug::emitDebugAbbrevDWO() {
2773 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00002774 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002775}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002776
2777// Emit the .debug_str.dwo section for separated dwarf. This contains the
2778// string section and is identical in format to traditional .debug_str
2779// sections.
2780void DwarfDebug::emitDebugStrDWO() {
2781 assert(useSplitDwarf() && "No split dwarf?");
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002782 const MCSection *OffSec =
2783 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002784 const MCSymbol *StrSym = DwarfStrSectionSym;
2785 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
2786 OffSec, StrSym);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002787}
David Blaikie409dd9c2013-11-19 23:08:21 +00002788
David Blaikie15632ae2014-02-12 00:31:30 +00002789void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikief645f962014-01-09 03:23:41 +00002790 StringRef Identifier, DIE *RefDie,
2791 DICompositeType CTy) {
David Blaikie322d79b2014-01-31 19:52:26 +00002792 // Flag the type unit reference as a declaration so that if it contains
2793 // members (implicit special members, static data member definitions, member
2794 // declarations for definitions in this CU, etc) consumers don't get confused
2795 // and think this is a full definition.
David Blaikie15632ae2014-02-12 00:31:30 +00002796 CU.addFlag(RefDie, dwarf::DW_AT_declaration);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002797
David Blaikie47f615e2013-12-17 23:32:35 +00002798 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
Chandler Carruthb587ab62014-01-20 08:07:07 +00002799 if (TU) {
David Blaikie15632ae2014-02-12 00:31:30 +00002800 CU.addDIETypeSignature(RefDie, *TU);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002801 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00002802 }
2803
Chandler Carruthb587ab62014-01-20 08:07:07 +00002804 DIE *UnitDie = new DIE(dwarf::DW_TAG_type_unit);
David Blaikied696fac2014-02-12 00:32:05 +00002805 DwarfTypeUnit *NewTU = new DwarfTypeUnit(InfoHolder.getUnits().size(),
2806 UnitDie, CU, Asm, this, &InfoHolder);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002807 TU = NewTU;
2808 InfoHolder.addUnit(NewTU);
2809
2810 NewTU->addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie15632ae2014-02-12 00:31:30 +00002811 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00002812
2813 MD5 Hash;
2814 Hash.update(Identifier);
2815 // ... take the least significant 8 bytes and return those. Our MD5
2816 // implementation always returns its results in little endian, swap bytes
2817 // appropriately.
2818 MD5::MD5Result Result;
2819 Hash.final(Result);
2820 uint64_t Signature = *reinterpret_cast<support::ulittle64_t *>(Result + 8);
2821 NewTU->setTypeSignature(Signature);
2822 if (useSplitDwarf())
2823 NewTU->setSkeleton(constructSkeletonTU(NewTU));
David Blaikie60e63862014-02-14 23:58:13 +00002824 else
2825 CU.applyStmtList(*UnitDie);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002826
2827 NewTU->setType(NewTU->createTypeDIE(CTy));
2828
2829 NewTU->initSection(
2830 useSplitDwarf()
2831 ? Asm->getObjFileLowering().getDwarfTypesDWOSection(Signature)
2832 : Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2833
David Blaikie15632ae2014-02-12 00:31:30 +00002834 CU.addDIETypeSignature(RefDie, *NewTU);
David Blaikie409dd9c2013-11-19 23:08:21 +00002835}
David Blaikie4bd13b72014-03-07 18:49:45 +00002836
2837void DwarfDebug::attachLowHighPC(DwarfCompileUnit *Unit, DIE *D,
2838 MCSymbol *Begin, MCSymbol *End) {
2839 Unit->addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
2840 if (DwarfVersion < 4 || Triple(Asm->getTargetTriple()).isOSDarwin())
2841 Unit->addLabelAddress(D, dwarf::DW_AT_high_pc, End);
2842 else
2843 Unit->addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
2844}