blob: aa1f79f260aaba3a0476f2f8f24b9d81fc1be98f [file] [log] [blame]
Bill Wendling0310d762009-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 Lattner6cde3e62010-03-09 00:39:24 +000013
Bill Wendling0310d762009-05-15 09:23:25 +000014#include "DwarfDebug.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080015#include "ByteStreamer.h"
Eric Christopher0d27ca12013-08-08 23:45:55 +000016#include "DIEHash.h"
Stephen Hinesebe69fe2015-03-23 12:10:34 -070017#include "DwarfCompileUnit.h"
18#include "DwarfExpression.h"
Stephen Hines36b56882014-04-23 16:57:46 -070019#include "DwarfUnit.h"
Chandler Carruthd04a8d42012-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"
Stephen Hinesebe69fe2015-03-23 12:10:34 -070024#include "llvm/CodeGen/DIE.h"
David Greeneb2c66fc2009-08-19 21:52:55 +000025#include "llvm/CodeGen/MachineFunction.h"
Bill Wendling0310d762009-05-15 09:23:25 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000027#include "llvm/IR/Constants.h"
Stephen Hines36b56882014-04-23 16:57:46 -070028#include "llvm/IR/DIBuilder.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000029#include "llvm/IR/DataLayout.h"
Stephen Hines36b56882014-04-23 16:57:46 -070030#include "llvm/IR/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000031#include "llvm/IR/Instructions.h"
32#include "llvm/IR/Module.h"
Stephen Hines36b56882014-04-23 16:57:46 -070033#include "llvm/IR/ValueHandle.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000034#include "llvm/MC/MCAsmInfo.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000035#include "llvm/MC/MCSection.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000036#include "llvm/MC/MCStreamer.h"
Chris Lattnerb7db7332010-03-09 01:58:53 +000037#include "llvm/MC/MCSymbol.h"
Devang Pateleac9c072010-04-27 19:46:33 +000038#include "llvm/Support/CommandLine.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000039#include "llvm/Support/Debug.h"
David Majnemerd7d43dc2013-08-21 06:13:34 +000040#include "llvm/Support/Dwarf.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080041#include "llvm/Support/Endian.h"
Daniel Dunbar6e4bdfc2009-10-13 06:47:08 +000042#include "llvm/Support/ErrorHandling.h"
Chris Lattner0ad9c912010-01-22 22:09:00 +000043#include "llvm/Support/FormattedStream.h"
Stephen Hines36b56882014-04-23 16:57:46 -070044#include "llvm/Support/LEB128.h"
Eric Christopher3dee5752013-07-26 17:02:41 +000045#include "llvm/Support/MD5.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000046#include "llvm/Support/Path.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000047#include "llvm/Support/Timer.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000048#include "llvm/Target/TargetFrameLowering.h"
49#include "llvm/Target/TargetLoweringObjectFile.h"
50#include "llvm/Target/TargetMachine.h"
51#include "llvm/Target/TargetOptions.h"
52#include "llvm/Target/TargetRegisterInfo.h"
Stephen Hines37ed9c12014-12-01 14:51:49 -080053#include "llvm/Target/TargetSubtargetInfo.h"
Bill Wendling0310d762009-05-15 09:23:25 +000054using namespace llvm;
55
Stephen Hinesdce4a402014-05-29 02:49:00 -070056#define DEBUG_TYPE "dwarfdebug"
57
Eric Christopher6e61e0b2013-07-23 22:16:41 +000058static cl::opt<bool>
59DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
60 cl::desc("Disable debug info printing"));
Devang Pateleac9c072010-04-27 19:46:33 +000061
Eric Christopher6e61e0b2013-07-23 22:16:41 +000062static cl::opt<bool> UnknownLocations(
63 "use-unknown-locations", cl::Hidden,
64 cl::desc("Make an absence of debug location information explicit."),
65 cl::init(false));
Dan Gohman281d65d2010-05-07 01:08:53 +000066
Eric Christopher6e61e0b2013-07-23 22:16:41 +000067static cl::opt<bool>
Eric Christopher8f1a9292013-09-13 00:35:05 +000068GenerateGnuPubSections("generate-gnu-dwarf-pub-sections", cl::Hidden,
69 cl::desc("Generate GNU-style pubnames and pubtypes"),
70 cl::init(false));
71
Stephen Hines36b56882014-04-23 16:57:46 -070072static cl::opt<bool> GenerateARangeSection("generate-arange-section",
73 cl::Hidden,
74 cl::desc("Generate dwarf aranges"),
75 cl::init(false));
76
Eric Christopher20f47ab2012-08-23 22:36:40 +000077namespace {
Stephen Hines36b56882014-04-23 16:57:46 -070078enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher20f47ab2012-08-23 22:36:40 +000079}
Eric Christopher09ac3d82011-11-07 09:24:32 +000080
Eric Christopher6e61e0b2013-07-23 22:16:41 +000081static cl::opt<DefaultOnOff>
82DwarfAccelTables("dwarf-accel-tables", cl::Hidden,
83 cl::desc("Output prototype dwarf accelerator tables."),
84 cl::values(clEnumVal(Default, "Default for platform"),
85 clEnumVal(Enable, "Enabled"),
86 clEnumVal(Disable, "Disabled"), clEnumValEnd),
87 cl::init(Default));
Eric Christopher20f47ab2012-08-23 22:36:40 +000088
Eric Christopher6e61e0b2013-07-23 22:16:41 +000089static cl::opt<DefaultOnOff>
Eric Christopher6e61e0b2013-07-23 22:16:41 +000090SplitDwarf("split-dwarf", cl::Hidden,
Stephen Hines36b56882014-04-23 16:57:46 -070091 cl::desc("Output DWARF5 split debug info."),
Eric Christopher6e61e0b2013-07-23 22:16:41 +000092 cl::values(clEnumVal(Default, "Default for platform"),
93 clEnumVal(Enable, "Enabled"),
94 clEnumVal(Disable, "Disabled"), clEnumValEnd),
95 cl::init(Default));
Eric Christopherf5b6dcd2012-11-12 22:22:20 +000096
Eric Christopherf04e4ef2013-08-19 21:07:38 +000097static cl::opt<DefaultOnOff>
Eric Christopher63ae7c92013-08-26 23:24:35 +000098DwarfPubSections("generate-dwarf-pub-sections", cl::Hidden,
99 cl::desc("Generate DWARF pubnames and pubtypes sections"),
100 cl::values(clEnumVal(Default, "Default for platform"),
101 clEnumVal(Enable, "Enabled"),
102 clEnumVal(Disable, "Disabled"), clEnumValEnd),
103 cl::init(Default));
Eric Christopherf04e4ef2013-08-19 21:07:38 +0000104
Benjamin Kramer0b6962f2013-08-24 12:54:27 +0000105static const char *const DWARFGroupName = "DWARF Emission";
106static const char *const DbgTimerName = "DWARF Debug Writer";
Bill Wendling5f017e82010-04-07 09:28:04 +0000107
Bill Wendling0310d762009-05-15 09:23:25 +0000108//===----------------------------------------------------------------------===//
109
Manman Ren8199f0b2013-10-08 19:07:44 +0000110/// resolve - Look in the DwarfDebug map for the MDNode that
111/// corresponds to the reference.
Stephen Hines36b56882014-04-23 16:57:46 -0700112template <typename T> T DbgVariable::resolve(DIRef<T> Ref) const {
Manman Ren8199f0b2013-10-08 19:07:44 +0000113 return DD->resolve(Ref);
114}
115
Stephen Hines36b56882014-04-23 16:57:46 -0700116bool DbgVariable::isBlockByrefVariable() const {
117 assert(Var.isVariable() && "Invalid complex DbgVariable!");
118 return Var.isBlockByrefVariable(DD->getTypeIdentifierMap());
119}
120
Nick Lewycky3bbb6f72011-07-29 03:49:23 +0000121DIType DbgVariable::getType() const {
Stephen Hines36b56882014-04-23 16:57:46 -0700122 DIType Ty = Var.getType().resolve(DD->getTypeIdentifierMap());
Devang Patel3cbee302011-04-12 22:53:02 +0000123 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
124 // addresses instead.
Stephen Hines36b56882014-04-23 16:57:46 -0700125 if (Var.isBlockByrefVariable(DD->getTypeIdentifierMap())) {
Devang Patel3cbee302011-04-12 22:53:02 +0000126 /* Byref variables, in Blocks, are declared by the programmer as
127 "SomeType VarName;", but the compiler creates a
128 __Block_byref_x_VarName struct, and gives the variable VarName
129 either the struct, or a pointer to the struct, as its type. This
130 is necessary for various behind-the-scenes things the compiler
131 needs to do with by-reference variables in blocks.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000132
Devang Patel3cbee302011-04-12 22:53:02 +0000133 However, as far as the original *programmer* is concerned, the
134 variable should still have type 'SomeType', as originally declared.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000135
Devang Patel3cbee302011-04-12 22:53:02 +0000136 The following function dives into the __Block_byref_x_VarName
137 struct to find the original type of the variable. This will be
138 passed back to the code generating the type for the Debug
139 Information Entry for the variable 'VarName'. 'VarName' will then
140 have the original type 'SomeType' in its debug information.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000141
Devang Patel3cbee302011-04-12 22:53:02 +0000142 The original type 'SomeType' will be the type of the field named
143 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000144
Devang Patel3cbee302011-04-12 22:53:02 +0000145 NOTE: In order for this to not completely fail on the debugger
146 side, the Debug Information Entry for the variable VarName needs to
147 have a DW_AT_location that tells the debugger how to unwind through
148 the pointers and __Block_byref_x_VarName struct to find the actual
149 value of the variable. The function addBlockByrefType does this. */
150 DIType subType = Ty;
Eric Christopher31667622013-08-08 01:41:00 +0000151 uint16_t tag = Ty.getTag();
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000152
Eric Christopher4dc211a2013-09-04 19:53:21 +0000153 if (tag == dwarf::DW_TAG_pointer_type)
Manman Ren8199f0b2013-10-08 19:07:44 +0000154 subType = resolve(DIDerivedType(Ty).getTypeDerivedFrom());
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000155
Stephen Hines37ed9c12014-12-01 14:51:49 -0800156 DIArray Elements = DICompositeType(subType).getElements();
Devang Patel3cbee302011-04-12 22:53:02 +0000157 for (unsigned i = 0, N = Elements.getNumElements(); i < N; ++i) {
David Blaikied6dffb42013-11-18 23:57:26 +0000158 DIDerivedType DT(Elements.getElement(i));
Devang Patel3cbee302011-04-12 22:53:02 +0000159 if (getName() == DT.getName())
Manman Ren8199f0b2013-10-08 19:07:44 +0000160 return (resolve(DT.getTypeDerivedFrom()));
Devang Patel8bd11de2010-08-09 21:01:39 +0000161 }
Devang Patel8bd11de2010-08-09 21:01:39 +0000162 }
Devang Patel3cbee302011-04-12 22:53:02 +0000163 return Ty;
164}
Bill Wendling0310d762009-05-15 09:23:25 +0000165
Stephen Hinesdce4a402014-05-29 02:49:00 -0700166static LLVM_CONSTEXPR DwarfAccelTable::Atom TypeAtoms[] = {
167 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset, dwarf::DW_FORM_data4),
168 DwarfAccelTable::Atom(dwarf::DW_ATOM_die_tag, dwarf::DW_FORM_data2),
169 DwarfAccelTable::Atom(dwarf::DW_ATOM_type_flags, dwarf::DW_FORM_data1)};
Manman Ren0e6783f2013-07-02 23:40:10 +0000170
Chris Lattner49cd6642010-04-05 05:11:15 +0000171DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800172 : Asm(A), MMI(Asm->MMI), PrevLabel(nullptr), GlobalRangeCount(0),
173 InfoHolder(A, *this, "info_string", DIEValueAllocator),
Stephen Hines36b56882014-04-23 16:57:46 -0700174 UsedNonDefaultText(false),
Stephen Hines37ed9c12014-12-01 14:51:49 -0800175 SkeletonHolder(A, *this, "skel_string", DIEValueAllocator),
176 IsDarwin(Triple(A->getTargetTriple()).isOSDarwin()),
Stephen Hinesdce4a402014-05-29 02:49:00 -0700177 AccelNames(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
178 dwarf::DW_FORM_data4)),
179 AccelObjC(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
180 dwarf::DW_FORM_data4)),
181 AccelNamespace(DwarfAccelTable::Atom(dwarf::DW_ATOM_die_offset,
182 dwarf::DW_FORM_data4)),
183 AccelTypes(TypeAtoms) {
Jim Grosbach1e20b962010-07-21 21:21:52 +0000184
Stephen Hinesdce4a402014-05-29 02:49:00 -0700185 DwarfInfoSectionSym = DwarfAbbrevSectionSym = DwarfStrSectionSym = nullptr;
186 DwarfDebugRangeSectionSym = DwarfDebugLocSectionSym = nullptr;
187 DwarfLineSectionSym = nullptr;
188 DwarfAddrSectionSym = nullptr;
189 DwarfAbbrevDWOSectionSym = DwarfStrDWOSectionSym = nullptr;
190 FunctionBeginSym = FunctionEndSym = nullptr;
191 CurFn = nullptr;
192 CurMI = nullptr;
Eric Christopher60777d82012-04-02 17:58:52 +0000193
Adrian Prantl73df4b92013-10-15 20:26:37 +0000194 // Turn on accelerator tables for Darwin by default, pubnames by
195 // default for non-Darwin, and handle split dwarf.
Eric Christopher05bae3b2013-08-19 21:41:38 +0000196 if (DwarfAccelTables == Default)
197 HasDwarfAccelTables = IsDarwin;
198 else
Eric Christopher03be5e52013-08-26 20:58:35 +0000199 HasDwarfAccelTables = DwarfAccelTables == Enable;
Eric Christopher20f47ab2012-08-23 22:36:40 +0000200
Eric Christopher4daaed12012-12-10 19:51:21 +0000201 if (SplitDwarf == Default)
202 HasSplitDwarf = false;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000203 else
Eric Christopher05bae3b2013-08-19 21:41:38 +0000204 HasSplitDwarf = SplitDwarf == Enable;
Eric Christopherf5b6dcd2012-11-12 22:22:20 +0000205
Eric Christopher63ae7c92013-08-26 23:24:35 +0000206 if (DwarfPubSections == Default)
207 HasDwarfPubSections = !IsDarwin;
Eric Christopher05bae3b2013-08-19 21:41:38 +0000208 else
Eric Christopher63ae7c92013-08-26 23:24:35 +0000209 HasDwarfPubSections = DwarfPubSections == Enable;
Eric Christopherf04e4ef2013-08-19 21:07:38 +0000210
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700211 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700212 DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
213 : MMI->getModule()->getDwarfVersion();
Manman Ren0e6783f2013-07-02 23:40:10 +0000214
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700215 Asm->OutStreamer.getContext().setDwarfVersion(DwarfVersion);
216
Dan Gohman03c3dc72010-06-18 15:56:31 +0000217 {
218 NamedRegionTimer T(DbgTimerName, DWARFGroupName, TimePassesIsEnabled);
Eric Christopherc4639d62012-11-19 22:42:15 +0000219 beginModule();
Torok Edwin9c421072010-04-07 10:44:46 +0000220 }
Bill Wendling0310d762009-05-15 09:23:25 +0000221}
Bill Wendling0310d762009-05-15 09:23:25 +0000222
Stephen Hinesdce4a402014-05-29 02:49:00 -0700223// Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
224DwarfDebug::~DwarfDebug() { }
225
Eric Christopherb6dc8652012-11-27 22:43:45 +0000226// Switch to the specified MCSection and emit an assembler
227// temporary label to it if SymbolStem is specified.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +0000228static MCSymbol *emitSectionSym(AsmPrinter *Asm, const MCSection *Section,
Stephen Hinesdce4a402014-05-29 02:49:00 -0700229 const char *SymbolStem = nullptr) {
Eric Christopherd8a87522011-11-07 09:18:38 +0000230 Asm->OutStreamer.SwitchSection(Section);
Stephen Hines36b56882014-04-23 16:57:46 -0700231 if (!SymbolStem)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700232 return nullptr;
Eric Christopherd8a87522011-11-07 09:18:38 +0000233
234 MCSymbol *TmpSym = Asm->GetTempSymbol(SymbolStem);
235 Asm->OutStreamer.EmitLabel(TmpSym);
236 return TmpSym;
237}
238
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000239static bool isObjCClass(StringRef Name) {
240 return Name.startswith("+") || Name.startswith("-");
241}
242
243static bool hasObjCCategory(StringRef Name) {
Stephen Hines36b56882014-04-23 16:57:46 -0700244 if (!isObjCClass(Name))
245 return false;
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000246
Benjamin Kramer4321d4e2013-08-24 12:15:54 +0000247 return Name.find(") ") != StringRef::npos;
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000248}
249
250static void getObjCClassCategory(StringRef In, StringRef &Class,
251 StringRef &Category) {
252 if (!hasObjCCategory(In)) {
253 Class = In.slice(In.find('[') + 1, In.find(' '));
254 Category = "";
255 return;
256 }
257
258 Class = In.slice(In.find('[') + 1, In.find('('));
259 Category = In.slice(In.find('[') + 1, In.find(' '));
260 return;
261}
262
263static StringRef getObjCMethodName(StringRef In) {
264 return In.slice(In.find(' ') + 1, In.find(']'));
265}
266
Richard Mitton7c9659a2013-10-03 22:07:08 +0000267// Helper for sorting sections into a stable output order.
268static bool SectionSort(const MCSection *A, const MCSection *B) {
Stephen Hines36b56882014-04-23 16:57:46 -0700269 std::string LA = (A ? A->getLabelBeginName() : "");
270 std::string LB = (B ? B->getLabelBeginName() : "");
271 return LA < LB;
Richard Mitton7c9659a2013-10-03 22:07:08 +0000272}
273
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000274// Add the various names to the Dwarf accelerator table names.
Eric Christopher9e0b08d2013-09-20 23:22:52 +0000275// TODO: Determine whether or not we should add names for programs
276// that do not have a DW_AT_name or DW_AT_linkage_name field - this
277// is only slightly different than the lookup of non-standard ObjC names.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700278void DwarfDebug::addSubprogramNames(DISubprogram SP, DIE &Die) {
Stephen Hines36b56882014-04-23 16:57:46 -0700279 if (!SP.isDefinition())
280 return;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700281 addAccelName(SP.getName(), Die);
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000282
283 // If the linkage name is different than the name, go ahead and output
284 // that as well into the name table.
285 if (SP.getLinkageName() != "" && SP.getName() != SP.getLinkageName())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700286 addAccelName(SP.getLinkageName(), Die);
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000287
288 // If this is an Objective-C selector name add it to the ObjC accelerator
289 // too.
290 if (isObjCClass(SP.getName())) {
291 StringRef Class, Category;
292 getObjCClassCategory(SP.getName(), Class, Category);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700293 addAccelObjC(Class, Die);
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000294 if (Category != "")
Stephen Hinesdce4a402014-05-29 02:49:00 -0700295 addAccelObjC(Category, Die);
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000296 // Also add the base method name to the name table.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700297 addAccelName(getObjCMethodName(SP.getName()), Die);
Eric Christopher0ffe2b42011-11-10 19:25:34 +0000298 }
299}
300
Manman Ren02d29672013-09-09 19:05:21 +0000301/// isSubprogramContext - Return true if Context is either a subprogram
302/// or another context nested inside a subprogram.
303bool DwarfDebug::isSubprogramContext(const MDNode *Context) {
304 if (!Context)
305 return false;
306 DIDescriptor D(Context);
307 if (D.isSubprogram())
308 return true;
309 if (D.isType())
Manman Ren2c9905a2013-09-09 19:47:11 +0000310 return isSubprogramContext(resolve(DIType(Context).getContext()));
Manman Ren02d29672013-09-09 19:05:21 +0000311 return false;
312}
313
Manman Ren995de6c2013-09-11 19:40:28 +0000314/// Check whether we should create a DIE for the given Scope, return true
315/// if we don't create a DIE (the corresponding DIE is null).
Manman Renfd2210a2013-09-10 18:40:41 +0000316bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
317 if (Scope->isAbstractScope())
318 return false;
319
Manman Ren995de6c2013-09-11 19:40:28 +0000320 // We don't create a DIE if there is no Range.
Manman Renfd2210a2013-09-10 18:40:41 +0000321 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
322 if (Ranges.empty())
323 return true;
324
325 if (Ranges.size() > 1)
326 return false;
327
Manman Ren995de6c2013-09-11 19:40:28 +0000328 // We don't create a DIE if we have a single Range and the end label
329 // is null.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800330 return !getLabelAfterInsn(Ranges.front().second);
Manman Renfd2210a2013-09-10 18:40:41 +0000331}
332
Stephen Hines37ed9c12014-12-01 14:51:49 -0800333template <typename Func> void forBothCUs(DwarfCompileUnit &CU, Func F) {
334 F(CU);
335 if (auto *SkelCU = CU.getSkeleton())
336 F(*SkelCU);
Stephen Hines36b56882014-04-23 16:57:46 -0700337}
338
Stephen Hines37ed9c12014-12-01 14:51:49 -0800339void DwarfDebug::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700340 assert(Scope && Scope->getScopeNode());
341 assert(Scope->isAbstractScope());
342 assert(!Scope->getInlinedAt());
343
Stephen Hines37ed9c12014-12-01 14:51:49 -0800344 const MDNode *SP = Scope->getScopeNode();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700345
346 ProcessedSPNodes.insert(SP);
347
Stephen Hinesdce4a402014-05-29 02:49:00 -0700348 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
349 // was inlined from another compile unit.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800350 auto &CU = SPMap[SP];
351 forBothCUs(*CU, [&](DwarfCompileUnit &CU) {
352 CU.constructAbstractSubprogramScopeDIE(Scope);
353 });
Stephen Hinesdce4a402014-05-29 02:49:00 -0700354}
355
356void DwarfDebug::addGnuPubAttributes(DwarfUnit &U, DIE &D) const {
Stephen Hines36b56882014-04-23 16:57:46 -0700357 if (!GenerateGnuPubSections)
358 return;
Rafael Espindola5e195a42013-10-05 16:42:21 +0000359
Stephen Hinesdce4a402014-05-29 02:49:00 -0700360 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000361}
362
Stephen Hines36b56882014-04-23 16:57:46 -0700363// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher72c16552012-12-20 21:58:40 +0000364// DW_TAG_compile_unit.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700365DwarfCompileUnit &DwarfDebug::constructDwarfCompileUnit(DICompileUnit DIUnit) {
Devang Patel65dbc902009-11-25 17:36:49 +0000366 StringRef FN = DIUnit.getFilename();
Nick Lewycky6c1a7032011-11-02 20:55:33 +0000367 CompilationDir = DIUnit.getDirectory();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000368
Stephen Hinesdce4a402014-05-29 02:49:00 -0700369 auto OwnedUnit = make_unique<DwarfCompileUnit>(
370 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
371 DwarfCompileUnit &NewCU = *OwnedUnit;
372 DIE &Die = NewCU.getUnitDie();
373 InfoHolder.addUnit(std::move(OwnedUnit));
Stephen Hines37ed9c12014-12-01 14:51:49 -0800374 if (useSplitDwarf())
375 NewCU.setSkeleton(constructSkeletonCU(NewCU));
Manman Ren3de61b42013-03-07 01:42:00 +0000376
Stephen Hines36b56882014-04-23 16:57:46 -0700377 // LTO with assembly output shares a single line table amongst multiple CUs.
378 // To avoid the compilation directory being ambiguous, let the line table
379 // explicitly describe the directory of all files, never relying on the
380 // compilation directory.
381 if (!Asm->OutStreamer.hasRawTextSupport() || SingleCU)
382 Asm->OutStreamer.getContext().setMCLineTableCompilationDir(
Stephen Hinesdce4a402014-05-29 02:49:00 -0700383 NewCU.getUniqueID(), CompilationDir);
Manman Ren3de61b42013-03-07 01:42:00 +0000384
Stephen Hinesdce4a402014-05-29 02:49:00 -0700385 NewCU.addString(Die, dwarf::DW_AT_producer, DIUnit.getProducer());
386 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
387 DIUnit.getLanguage());
388 NewCU.addString(Die, dwarf::DW_AT_name, FN);
Eric Christophere0828ec2013-04-09 19:23:15 +0000389
Eric Christophere0828ec2013-04-09 19:23:15 +0000390 if (!useSplitDwarf()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700391 NewCU.initStmtList(DwarfLineSectionSym);
Eric Christopher0b15e982013-09-27 22:50:48 +0000392
393 // If we're using split dwarf the compilation dir is going to be in the
394 // skeleton CU and so we don't need to duplicate it here.
395 if (!CompilationDir.empty())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700396 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher0b15e982013-09-27 22:50:48 +0000397
Stephen Hines36b56882014-04-23 16:57:46 -0700398 addGnuPubAttributes(NewCU, Die);
Eric Christophere0828ec2013-04-09 19:23:15 +0000399 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000400
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000401 if (DIUnit.isOptimized())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700402 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000403
Devang Patel65dbc902009-11-25 17:36:49 +0000404 StringRef Flags = DIUnit.getFlags();
405 if (!Flags.empty())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700406 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000407
Nick Lewyckyd5d52132011-10-17 23:27:36 +0000408 if (unsigned RVer = DIUnit.getRunTimeVersion())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700409 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
410 dwarf::DW_FORM_data1, RVer);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000411
Stephen Hines37ed9c12014-12-01 14:51:49 -0800412 if (useSplitDwarf())
Stephen Hinesdce4a402014-05-29 02:49:00 -0700413 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoDWOSection(),
414 DwarfInfoDWOSectionSym);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800415 else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700416 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
417 DwarfInfoSectionSym);
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000418
Stephen Hinesdce4a402014-05-29 02:49:00 -0700419 CUMap.insert(std::make_pair(DIUnit, &NewCU));
420 CUDieMap.insert(std::make_pair(&Die, &NewCU));
Devang Patel94c7ddb2011-08-16 22:09:43 +0000421 return NewCU;
Devang Patel163a9f72010-05-10 22:49:55 +0000422}
423
Stephen Hines37ed9c12014-12-01 14:51:49 -0800424void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
425 const MDNode *N) {
David Blaikie20d9e412013-05-07 21:35:53 +0000426 DIImportedEntity Module(N);
Stephen Hines36b56882014-04-23 16:57:46 -0700427 assert(Module.Verify());
Stephen Hinesdce4a402014-05-29 02:49:00 -0700428 if (DIE *D = TheCU.getOrCreateContextDIE(Module.getContext()))
Stephen Hines37ed9c12014-12-01 14:51:49 -0800429 D->addChild(TheCU.constructImportedEntityDIE(Module));
David Blaikiec462db62013-04-22 06:12:31 +0000430}
431
Eric Christopherb6dc8652012-11-27 22:43:45 +0000432// Emit all Dwarf sections that should come prior to the content. Create
433// global DIEs and emit initial debug info sections. This is invoked by
434// the target AsmPrinter.
Eric Christopherc4639d62012-11-19 22:42:15 +0000435void DwarfDebug::beginModule() {
Devang Pateleac9c072010-04-27 19:46:33 +0000436 if (DisableDebugInfoPrinting)
437 return;
438
Eric Christopherc4639d62012-11-19 22:42:15 +0000439 const Module *M = MMI->getModule();
440
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700441 FunctionDIs = makeSubprogramMap(*M);
442
Devang Patel30692ab2011-05-03 16:45:22 +0000443 NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu");
David Blaikiec0ec8a42013-03-11 23:39:23 +0000444 if (!CU_Nodes)
Devang Patel02e603f2011-08-15 23:47:24 +0000445 return;
Manman Renbc660712013-09-05 18:48:31 +0000446 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
Devang Patel30692ab2011-05-03 16:45:22 +0000447
David Blaikiec0ec8a42013-03-11 23:39:23 +0000448 // Emit initial sections so we can reference labels later.
449 emitSectionLabels();
450
Stephen Hines36b56882014-04-23 16:57:46 -0700451 SingleCU = CU_Nodes->getNumOperands() == 1;
452
453 for (MDNode *N : CU_Nodes->operands()) {
454 DICompileUnit CUNode(N);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700455 DwarfCompileUnit &CU = constructDwarfCompileUnit(CUNode);
David Blaikie20d9e412013-05-07 21:35:53 +0000456 DIArray ImportedEntities = CUNode.getImportedEntities();
457 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
David Blaikied2e0f7e2013-05-06 23:33:07 +0000458 ScopesWithImportedEntities.push_back(std::make_pair(
David Blaikie20d9e412013-05-07 21:35:53 +0000459 DIImportedEntity(ImportedEntities.getElement(i)).getContext(),
460 ImportedEntities.getElement(i)));
David Blaikied2e0f7e2013-05-06 23:33:07 +0000461 std::sort(ScopesWithImportedEntities.begin(),
Benjamin Kramer0b6962f2013-08-24 12:54:27 +0000462 ScopesWithImportedEntities.end(), less_first());
David Blaikiec0ec8a42013-03-11 23:39:23 +0000463 DIArray GVs = CUNode.getGlobalVariables();
464 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800465 CU.getOrCreateGlobalVariableDIE(DIGlobalVariable(GVs.getElement(i)));
David Blaikiec0ec8a42013-03-11 23:39:23 +0000466 DIArray SPs = CUNode.getSubprograms();
467 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
Stephen Hinesdce4a402014-05-29 02:49:00 -0700468 SPMap.insert(std::make_pair(SPs.getElement(i), &CU));
David Blaikiec0ec8a42013-03-11 23:39:23 +0000469 DIArray EnumTypes = CUNode.getEnumTypes();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800470 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i) {
471 DIType Ty(EnumTypes.getElement(i));
472 // The enum types array by design contains pointers to
473 // MDNodes rather than DIRefs. Unique them here.
474 DIType UniqueTy(resolve(Ty.getRef()));
475 CU.getOrCreateTypeDIE(UniqueTy);
476 }
David Blaikiec0ec8a42013-03-11 23:39:23 +0000477 DIArray RetainedTypes = CUNode.getRetainedTypes();
Stephen Hines36b56882014-04-23 16:57:46 -0700478 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i) {
479 DIType Ty(RetainedTypes.getElement(i));
480 // The retained types array by design contains pointers to
481 // MDNodes rather than DIRefs. Unique them here.
482 DIType UniqueTy(resolve(Ty.getRef()));
Stephen Hinesdce4a402014-05-29 02:49:00 -0700483 CU.getOrCreateTypeDIE(UniqueTy);
Stephen Hines36b56882014-04-23 16:57:46 -0700484 }
David Blaikiec462db62013-04-22 06:12:31 +0000485 // Emit imported_modules last so that the relevant context is already
486 // available.
David Blaikie20d9e412013-05-07 21:35:53 +0000487 for (unsigned i = 0, e = ImportedEntities.getNumElements(); i != e; ++i)
Stephen Hines37ed9c12014-12-01 14:51:49 -0800488 constructAndAddImportedEntityDIE(CU, ImportedEntities.getElement(i));
David Blaikiec0ec8a42013-03-11 23:39:23 +0000489 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000490
Chris Lattnerd850ac72010-04-05 02:19:28 +0000491 // Tell MMI that we have debug info.
492 MMI->setDebugInfoAvailability(true);
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000493}
494
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700495void DwarfDebug::finishVariableDefinitions() {
496 for (const auto &Var : ConcreteVariables) {
497 DIE *VariableDie = Var->getDIE();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800498 assert(VariableDie);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700499 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
500 // in the ConcreteVariables list, rather than looking it up again here.
501 // DIE::getUnit isn't simple - it walks parent pointers, etc.
502 DwarfCompileUnit *Unit = lookupUnit(VariableDie->getUnit());
503 assert(Unit);
504 DbgVariable *AbsVar = getExistingAbstractVariable(Var->getVariable());
505 if (AbsVar && AbsVar->getDIE()) {
506 Unit->addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin,
507 *AbsVar->getDIE());
508 } else
509 Unit->applyVariableAttributes(*Var, *VariableDie);
510 }
511}
512
Stephen Hinesdce4a402014-05-29 02:49:00 -0700513void DwarfDebug::finishSubprogramDefinitions() {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800514 for (const auto &P : SPMap)
515 forBothCUs(*P.second, [&](DwarfCompileUnit &CU) {
516 CU.finishSubprogramDefinition(DISubprogram(P.first));
517 });
Eric Christopher4117bec2012-11-22 00:59:49 +0000518}
519
Stephen Hinesdce4a402014-05-29 02:49:00 -0700520
Eric Christopher4117bec2012-11-22 00:59:49 +0000521// Collect info for variables that were optimized out.
522void DwarfDebug::collectDeadVariables() {
523 const Module *M = MMI->getModule();
Eric Christopher4117bec2012-11-22 00:59:49 +0000524
525 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
Stephen Hines36b56882014-04-23 16:57:46 -0700526 for (MDNode *N : CU_Nodes->operands()) {
527 DICompileUnit TheCU(N);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700528 // Construct subprogram DIE and add variables DIEs.
529 DwarfCompileUnit *SPCU =
530 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
531 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopher4117bec2012-11-22 00:59:49 +0000532 DIArray Subprograms = TheCU.getSubprograms();
533 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopherbdab8002012-11-27 00:13:51 +0000534 DISubprogram SP(Subprograms.getElement(i));
Eric Christopherc6228242013-10-15 23:31:38 +0000535 if (ProcessedSPNodes.count(SP) != 0)
536 continue;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800537 SPCU->collectDeadVariables(SP);
Eric Christopher4117bec2012-11-22 00:59:49 +0000538 }
539 }
540 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000541}
542
543void DwarfDebug::finalizeModuleInfo() {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700544 finishSubprogramDefinitions();
545
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700546 finishVariableDefinitions();
547
Eric Christopher4117bec2012-11-22 00:59:49 +0000548 // Collect info for variables that were optimized out.
549 collectDeadVariables();
550
Stephen Hines36b56882014-04-23 16:57:46 -0700551 // Handle anything that needs to be done on a per-unit basis after
552 // all other generation.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800553 for (const auto &P : CUMap) {
554 auto &TheCU = *P.second;
Eric Christopherf962c022013-08-12 20:27:48 +0000555 // Emit DW_AT_containing_type attribute to connect types with their
556 // vtable holding type.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800557 TheCU.constructContainingTypeDIEs();
Eric Christopherf962c022013-08-12 20:27:48 +0000558
Stephen Hines36b56882014-04-23 16:57:46 -0700559 // Add CU specific attributes if we need to add any.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800560 // If we're splitting the dwarf out now that we've got the entire
561 // CU then add the dwo id to it.
562 auto *SkCU = TheCU.getSkeleton();
563 if (useSplitDwarf()) {
564 // Emit a unique identifier for this CU.
565 uint64_t ID = DIEHash(Asm).computeCUSignature(TheCU.getUnitDie());
566 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
567 dwarf::DW_FORM_data8, ID);
568 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
569 dwarf::DW_FORM_data8, ID);
Stephen Hines36b56882014-04-23 16:57:46 -0700570
Stephen Hines37ed9c12014-12-01 14:51:49 -0800571 // We don't keep track of which addresses are used in which CU so this
572 // is a bit pessimistic under LTO.
573 if (!AddrPool.isEmpty())
574 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
575 DwarfAddrSectionSym, DwarfAddrSectionSym);
576 if (!SkCU->getRangeLists().empty())
577 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
578 DwarfDebugRangeSectionSym,
579 DwarfDebugRangeSectionSym);
580 }
Stephen Hines36b56882014-04-23 16:57:46 -0700581
Stephen Hines37ed9c12014-12-01 14:51:49 -0800582 // If we have code split among multiple sections or non-contiguous
583 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
584 // remain in the .o file, otherwise add a DW_AT_low_pc.
585 // FIXME: We should use ranges allow reordering of code ala
586 // .subsections_via_symbols in mach-o. This would mean turning on
587 // ranges for all subprogram DIEs for mach-o.
588 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
589 if (unsigned NumRanges = TheCU.getRanges().size()) {
590 if (NumRanges > 1)
591 // A DW_AT_low_pc attribute may also be specified in combination with
592 // DW_AT_ranges to specify the default base address for use in
593 // location lists (see Section 2.6.2) and range lists (see Section
594 // 2.17.3).
595 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
596 else
597 TheCU.setBaseAddress(TheCU.getRanges().front().getStart());
598 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
Eric Christopherf962c022013-08-12 20:27:48 +0000599 }
600 }
601
602 // Compute DIE offsets and sizes.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000603 InfoHolder.computeSizeAndOffsets();
604 if (useSplitDwarf())
605 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher4117bec2012-11-22 00:59:49 +0000606}
607
Eric Christopherb6dc8652012-11-27 22:43:45 +0000608// Emit all Dwarf sections that should come after the content.
Eric Christopher4117bec2012-11-22 00:59:49 +0000609void DwarfDebug::endModule() {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700610 assert(CurFn == nullptr);
611 assert(CurMI == nullptr);
Eric Christopher4117bec2012-11-22 00:59:49 +0000612
Stephen Hines37ed9c12014-12-01 14:51:49 -0800613 // If we aren't actually generating debug info (check beginModule -
614 // conditionalized on !DisableDebugInfoPrinting and the presence of the
615 // llvm.dbg.cu metadata node)
616 if (!DwarfInfoSectionSym)
Stephen Hines36b56882014-04-23 16:57:46 -0700617 return;
Eric Christopher4117bec2012-11-22 00:59:49 +0000618
Eric Christopher4117bec2012-11-22 00:59:49 +0000619 // Finalize the debug info for the module.
620 finalizeModuleInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000621
Stephen Hines36b56882014-04-23 16:57:46 -0700622 emitDebugStr();
Eric Christopher9e0b08d2013-09-20 23:22:52 +0000623
Stephen Hines36b56882014-04-23 16:57:46 -0700624 // Emit all the DIEs into a debug info section.
625 emitDebugInfo();
Eric Christopher74802f72012-11-27 00:41:54 +0000626
Stephen Hines36b56882014-04-23 16:57:46 -0700627 // Corresponding abbreviations into a abbrev section.
628 emitAbbreviations();
Eric Christopher42885022012-11-27 22:43:42 +0000629
Stephen Hines36b56882014-04-23 16:57:46 -0700630 // Emit info into a debug aranges section.
631 if (GenerateARangeSection)
Eric Christopher42885022012-11-27 22:43:42 +0000632 emitDebugARanges();
633
Stephen Hines36b56882014-04-23 16:57:46 -0700634 // Emit info into a debug ranges section.
635 emitDebugRanges();
Eric Christopher42885022012-11-27 22:43:42 +0000636
Stephen Hines36b56882014-04-23 16:57:46 -0700637 if (useSplitDwarf()) {
638 emitDebugStrDWO();
Eric Christopher98e237f2012-11-30 23:59:06 +0000639 emitDebugInfoDWO();
Eric Christopher6eebe472012-12-19 22:02:53 +0000640 emitDebugAbbrevDWO();
Stephen Hines36b56882014-04-23 16:57:46 -0700641 emitDebugLineDWO();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700642 emitDebugLocDWO();
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000643 // Emit DWO addresses.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700644 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
Stephen Hines36b56882014-04-23 16:57:46 -0700645 } else
646 // Emit info into a debug loc section.
647 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000648
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000649 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000650 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000651 emitAccelNames();
652 emitAccelObjC();
653 emitAccelNamespaces();
654 emitAccelTypes();
655 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000656
Eric Christopher26123542013-08-30 00:40:17 +0000657 // Emit the pubnames and pubtypes sections if requested.
658 if (HasDwarfPubSections) {
David Blaikiee56a4d92013-09-19 17:33:35 +0000659 emitDebugPubNames(GenerateGnuPubSections);
660 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher26123542013-08-30 00:40:17 +0000661 }
Devang Patel193f7202009-11-24 01:14:22 +0000662
Devang Patele9a1cca2010-08-02 17:32:15 +0000663 // clean up.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000664 SPMap.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700665 AbstractVariables.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000666}
667
Eric Christopherb6dc8652012-11-27 22:43:45 +0000668// Find abstract variable, if any, associated with Var.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700669DbgVariable *DwarfDebug::getExistingAbstractVariable(const DIVariable &DV,
670 DIVariable &Cleansed) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000671 LLVMContext &Ctx = DV->getContext();
672 // More then one inlined variable corresponds to one abstract variable.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700673 // FIXME: This duplication of variables when inlining should probably be
674 // removed. It's done to allow each DIVariable to describe its location
675 // because the DebugLoc on the dbg.value/declare isn't accurate. We should
676 // make it accurate then remove this duplication/cleansing stuff.
677 Cleansed = cleanseInlinedVariable(DV, Ctx);
678 auto I = AbstractVariables.find(Cleansed);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700679 if (I != AbstractVariables.end())
680 return I->second.get();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700681 return nullptr;
682}
Devang Patel53bb5c92009-11-10 23:06:00 +0000683
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700684DbgVariable *DwarfDebug::getExistingAbstractVariable(const DIVariable &DV) {
685 DIVariable Cleansed;
686 return getExistingAbstractVariable(DV, Cleansed);
687}
Devang Patel53bb5c92009-11-10 23:06:00 +0000688
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700689void DwarfDebug::createAbstractVariable(const DIVariable &Var,
690 LexicalScope *Scope) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800691 auto AbsDbgVariable = make_unique<DbgVariable>(Var, DIExpression(), this);
692 InfoHolder.addScopeVariable(Scope, AbsDbgVariable.get());
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700693 AbstractVariables[Var] = std::move(AbsDbgVariable);
694}
695
696void DwarfDebug::ensureAbstractVariableIsCreated(const DIVariable &DV,
697 const MDNode *ScopeNode) {
698 DIVariable Cleansed = DV;
699 if (getExistingAbstractVariable(DV, Cleansed))
700 return;
701
702 createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(ScopeNode));
703}
704
705void
706DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(const DIVariable &DV,
707 const MDNode *ScopeNode) {
708 DIVariable Cleansed = DV;
709 if (getExistingAbstractVariable(DV, Cleansed))
710 return;
711
712 if (LexicalScope *Scope = LScopes.findAbstractScope(ScopeNode))
713 createAbstractVariable(Cleansed, Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +0000714}
715
Eric Christopherb6dc8652012-11-27 22:43:45 +0000716// Collect variable information from side table maintained by MMI.
Stephen Hines36b56882014-04-23 16:57:46 -0700717void DwarfDebug::collectVariableInfoFromMMITable(
Stephen Hines37ed9c12014-12-01 14:51:49 -0800718 SmallPtrSetImpl<const MDNode *> &Processed) {
Stephen Hines36b56882014-04-23 16:57:46 -0700719 for (const auto &VI : MMI->getVariableDbgInfo()) {
720 if (!VI.Var)
721 continue;
722 Processed.insert(VI.Var);
Stephen Hines36b56882014-04-23 16:57:46 -0700723 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000724
Devang Patelfb0ee432009-11-10 23:20:04 +0000725 // If variable scope is not found then skip this variable.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700726 if (!Scope)
Devang Patelfb0ee432009-11-10 23:20:04 +0000727 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000728
Stephen Hines37ed9c12014-12-01 14:51:49 -0800729 DIVariable DV(VI.Var);
730 DIExpression Expr(VI.Expr);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700731 ensureAbstractVariableIsCreatedIfScoped(DV, Scope->getScopeNode());
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700732 auto RegVar = make_unique<DbgVariable>(DV, Expr, this, VI.Slot);
733 if (InfoHolder.addScopeVariable(Scope, RegVar.get()))
734 ConcreteVariables.push_back(std::move(RegVar));
Devang Patele717faa2009-10-06 01:26:37 +0000735 }
Devang Patelee432862010-05-20 19:57:06 +0000736}
Devang Patel90a48ad2010-03-15 18:33:46 +0000737
Eric Christopherb6dc8652012-11-27 22:43:45 +0000738// Get .debug_loc entry for the instruction range starting at MI.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700739static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800740 const MDNode *Expr = MI->getDebugExpression();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700741 const MDNode *Var = MI->getDebugVariable();
Devang Patel90b40412011-07-08 17:09:57 +0000742
Stephen Hines37ed9c12014-12-01 14:51:49 -0800743 assert(MI->getNumOperands() == 4);
Adrian Prantl35176402013-07-09 20:28:37 +0000744 if (MI->getOperand(0).isReg()) {
Devang Patel90b40412011-07-08 17:09:57 +0000745 MachineLocation MLoc;
Adrian Prantl35176402013-07-09 20:28:37 +0000746 // If the second operand is an immediate, this is a
747 // register-indirect address.
748 if (!MI->getOperand(1).isImm())
Adrian Prantl13131e62013-04-26 21:57:17 +0000749 MLoc.set(MI->getOperand(0).getReg());
750 else
751 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800752 return DebugLocEntry::Value(Var, Expr, MLoc);
Devang Patel90b40412011-07-08 17:09:57 +0000753 }
754 if (MI->getOperand(0).isImm())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800755 return DebugLocEntry::Value(Var, Expr, MI->getOperand(0).getImm());
Devang Patel90b40412011-07-08 17:09:57 +0000756 if (MI->getOperand(0).isFPImm())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800757 return DebugLocEntry::Value(Var, Expr, MI->getOperand(0).getFPImm());
Devang Patel90b40412011-07-08 17:09:57 +0000758 if (MI->getOperand(0).isCImm())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800759 return DebugLocEntry::Value(Var, Expr, MI->getOperand(0).getCImm());
Devang Patel90b40412011-07-08 17:09:57 +0000760
Stephen Hines37ed9c12014-12-01 14:51:49 -0800761 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000762}
763
Stephen Hines37ed9c12014-12-01 14:51:49 -0800764/// Determine whether two variable pieces overlap.
765static bool piecesOverlap(DIExpression P1, DIExpression P2) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700766 if (!P1.isBitPiece() || !P2.isBitPiece())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800767 return true;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700768 unsigned l1 = P1.getBitPieceOffset();
769 unsigned l2 = P2.getBitPieceOffset();
770 unsigned r1 = l1 + P1.getBitPieceSize();
771 unsigned r2 = l2 + P2.getBitPieceSize();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800772 // True where [l1,r1[ and [r1,r2[ overlap.
773 return (l1 < r2) && (l2 < r1);
774}
775
776/// Build the location list for all DBG_VALUEs in the function that
777/// describe the same variable. If the ranges of several independent
778/// pieces of the same variable overlap partially, split them up and
779/// combine the ranges. The resulting DebugLocEntries are will have
780/// strict monotonically increasing begin addresses and will never
781/// overlap.
782//
783// Input:
784//
785// Ranges History [var, loc, piece ofs size]
786// 0 | [x, (reg0, piece 0, 32)]
787// 1 | | [x, (reg1, piece 32, 32)] <- IsPieceOfPrevEntry
788// 2 | | ...
789// 3 | [clobber reg0]
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700790// 4 [x, (mem, piece 0, 64)] <- overlapping with both previous pieces of
791// x.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800792//
793// Output:
794//
795// [0-1] [x, (reg0, piece 0, 32)]
796// [1-3] [x, (reg0, piece 0, 32), (reg1, piece 32, 32)]
797// [3-4] [x, (reg1, piece 32, 32)]
798// [4- ] [x, (mem, piece 0, 64)]
799void
800DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
801 const DbgValueHistoryMap::InstrRanges &Ranges) {
802 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
803
804 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
805 const MachineInstr *Begin = I->first;
806 const MachineInstr *End = I->second;
807 assert(Begin->isDebugValue() && "Invalid History entry");
808
809 // Check if a variable is inaccessible in this range.
810 if (Begin->getNumOperands() > 1 &&
811 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
812 OpenRanges.clear();
813 continue;
814 }
815
816 // If this piece overlaps with any open ranges, truncate them.
817 DIExpression DIExpr = Begin->getDebugExpression();
818 auto Last = std::remove_if(OpenRanges.begin(), OpenRanges.end(),
819 [&](DebugLocEntry::Value R) {
820 return piecesOverlap(DIExpr, R.getExpression());
821 });
822 OpenRanges.erase(Last, OpenRanges.end());
823
824 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
825 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
826
827 const MCSymbol *EndLabel;
828 if (End != nullptr)
829 EndLabel = getLabelAfterInsn(End);
830 else if (std::next(I) == Ranges.end())
831 EndLabel = FunctionEndSym;
832 else
833 EndLabel = getLabelBeforeInsn(std::next(I)->first);
834 assert(EndLabel && "Forgot label after instruction ending a range!");
835
836 DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
837
838 auto Value = getDebugLocValue(Begin);
839 DebugLocEntry Loc(StartLabel, EndLabel, Value);
840 bool couldMerge = false;
841
842 // If this is a piece, it may belong to the current DebugLocEntry.
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700843 if (DIExpr.isBitPiece()) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800844 // Add this value to the list of open ranges.
845 OpenRanges.push_back(Value);
846
847 // Attempt to add the piece to the last entry.
848 if (!DebugLoc.empty())
849 if (DebugLoc.back().MergeValues(Loc))
850 couldMerge = true;
851 }
852
853 if (!couldMerge) {
854 // Need to add a new DebugLocEntry. Add all values from still
855 // valid non-overlapping pieces.
856 if (OpenRanges.size())
857 Loc.addValues(OpenRanges);
858
859 DebugLoc.push_back(std::move(Loc));
860 }
861
862 // Attempt to coalesce the ranges of two otherwise identical
863 // DebugLocEntries.
864 auto CurEntry = DebugLoc.rbegin();
865 auto PrevEntry = std::next(CurEntry);
866 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
867 DebugLoc.pop_back();
868
869 DEBUG({
870 dbgs() << CurEntry->getValues().size() << " Values:\n";
871 for (auto Value : CurEntry->getValues()) {
872 Value.getVariable()->dump();
873 Value.getExpression()->dump();
874 }
875 dbgs() << "-----\n";
876 });
877 }
878}
879
880
Eric Christopherb6dc8652012-11-27 22:43:45 +0000881// Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000882void
Stephen Hines37ed9c12014-12-01 14:51:49 -0800883DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU, DISubprogram SP,
884 SmallPtrSetImpl<const MDNode *> &Processed) {
Eric Christopher4e976452013-07-03 21:37:03 +0000885 // Grab the variable info that was squirreled away in the MMI side-table.
Stephen Hines36b56882014-04-23 16:57:46 -0700886 collectVariableInfoFromMMITable(Processed);
Devang Patelee432862010-05-20 19:57:06 +0000887
Stephen Hinesdce4a402014-05-29 02:49:00 -0700888 for (const auto &I : DbgValues) {
889 DIVariable DV(I.first);
890 if (Processed.count(DV))
Devang Patelee432862010-05-20 19:57:06 +0000891 continue;
892
Stephen Hinesdce4a402014-05-29 02:49:00 -0700893 // Instruction ranges, specifying where DV is accessible.
894 const auto &Ranges = I.second;
895 if (Ranges.empty())
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000896 continue;
Devang Patelc3f5f782010-05-25 23:40:22 +0000897
Stephen Hinesdce4a402014-05-29 02:49:00 -0700898 LexicalScope *Scope = nullptr;
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700899 if (MDNode *IA = DV.getInlinedAt())
900 Scope = LScopes.findInlinedScope(DV.getContext(), IA);
901 else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700902 Scope = LScopes.findLexicalScope(DV.getContext());
Devang Patelee432862010-05-20 19:57:06 +0000903 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +0000904 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +0000905 continue;
906
907 Processed.insert(DV);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700908 const MachineInstr *MInsn = Ranges.front().first;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000909 assert(MInsn->isDebugValue() && "History must begin with debug value");
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700910 ensureAbstractVariableIsCreatedIfScoped(DV, Scope->getScopeNode());
911 ConcreteVariables.push_back(make_unique<DbgVariable>(MInsn, this));
912 DbgVariable *RegVar = ConcreteVariables.back().get();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800913 InfoHolder.addScopeVariable(Scope, RegVar);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000914
Stephen Hinesdce4a402014-05-29 02:49:00 -0700915 // Check if the first DBG_VALUE is valid for the rest of the function.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700916 if (Ranges.size() == 1 && Ranges.front().second == nullptr)
Devang Patelc3f5f782010-05-25 23:40:22 +0000917 continue;
Devang Patelc3f5f782010-05-25 23:40:22 +0000918
Eric Christopher498703b2013-01-28 17:33:26 +0000919 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000920 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000921
Stephen Hines36b56882014-04-23 16:57:46 -0700922 DotDebugLocEntries.resize(DotDebugLocEntries.size() + 1);
923 DebugLocList &LocList = DotDebugLocEntries.back();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800924 LocList.CU = &TheCU;
Stephen Hines36b56882014-04-23 16:57:46 -0700925 LocList.Label =
926 Asm->GetTempSymbol("debug_loc", DotDebugLocEntries.size() - 1);
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +0000927
Stephen Hines37ed9c12014-12-01 14:51:49 -0800928 // Build the location list for this variable.
929 buildLocationList(LocList.List, Ranges);
Devang Patel90a48ad2010-03-15 18:33:46 +0000930 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000931
932 // Collect info for variables that were optimized out.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800933 DIArray Variables = SP.getVariables();
Devang Patel93d39be2011-08-19 23:28:12 +0000934 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
935 DIVariable DV(Variables.getElement(i));
Stephen Hinesdce4a402014-05-29 02:49:00 -0700936 assert(DV.isVariable());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800937 if (!Processed.insert(DV).second)
Devang Patel93d39be2011-08-19 23:28:12 +0000938 continue;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700939 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext())) {
940 ensureAbstractVariableIsCreatedIfScoped(DV, Scope->getScopeNode());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800941 DIExpression NoExpr;
942 ConcreteVariables.push_back(make_unique<DbgVariable>(DV, NoExpr, this));
943 InfoHolder.addScopeVariable(Scope, ConcreteVariables.back().get());
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700944 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000945 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000946}
Devang Patel98e1cac2010-05-14 21:01:35 +0000947
Eric Christopherb6dc8652012-11-27 22:43:45 +0000948// Return Label preceding the instruction.
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000949MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000950 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
951 assert(Label && "Didn't insert label before instruction");
952 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +0000953}
954
Eric Christopherb6dc8652012-11-27 22:43:45 +0000955// Return Label immediately following the instruction.
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000956MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000957 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +0000958}
959
Eric Christopherb6dc8652012-11-27 22:43:45 +0000960// Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +0000961void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700962 assert(CurMI == nullptr);
Stephen Hines36b56882014-04-23 16:57:46 -0700963 CurMI = MI;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000964 // Check if source location changes, but ignore DBG_VALUE locations.
965 if (!MI->isDebugValue()) {
966 DebugLoc DL = MI->getDebugLoc();
967 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +0000968 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000969 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +0000970 if (DL == PrologEndLoc) {
971 Flags |= DWARF2_FLAG_PROLOGUE_END;
972 PrologEndLoc = DebugLoc();
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700973 Flags |= DWARF2_FLAG_IS_STMT;
Devang Patel4243e672011-05-11 19:22:19 +0000974 }
Stephen Hinesebe69fe2015-03-23 12:10:34 -0700975 if (DL.getLine() !=
976 Asm->OutStreamer.getContext().getCurrentDwarfLoc().getLine())
Eric Christopher60b35f42012-04-05 20:39:05 +0000977 Flags |= DWARF2_FLAG_IS_STMT;
978
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000979 if (!DL.isUnknown()) {
980 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +0000981 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000982 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -0700983 recordSourceLine(0, 0, nullptr, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000984 }
Devang Patelc3f5f782010-05-25 23:40:22 +0000985 }
Devang Patelaead63c2010-03-29 22:59:58 +0000986
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000987 // Insert labels where requested.
Stephen Hines36b56882014-04-23 16:57:46 -0700988 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
989 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000990
991 // No label needed.
992 if (I == LabelsBeforeInsn.end())
993 return;
994
995 // Label already assigned.
996 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +0000997 return;
Devang Patel553881b2010-03-29 17:20:31 +0000998
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +0000999 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001000 PrevLabel = MMI->getContext().CreateTempSymbol();
1001 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001002 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001003 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001004}
1005
Eric Christopherb6dc8652012-11-27 22:43:45 +00001006// Process end of an instruction.
Stephen Hines36b56882014-04-23 16:57:46 -07001007void DwarfDebug::endInstruction() {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001008 assert(CurMI != nullptr);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001009 // Don't create a new label after DBG_VALUE instructions.
1010 // They don't generate code.
Stephen Hines36b56882014-04-23 16:57:46 -07001011 if (!CurMI->isDebugValue())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001012 PrevLabel = nullptr;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001013
Stephen Hines36b56882014-04-23 16:57:46 -07001014 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1015 LabelsAfterInsn.find(CurMI);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001016 CurMI = nullptr;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001017
1018 // No label needed.
1019 if (I == LabelsAfterInsn.end())
1020 return;
1021
1022 // Label already assigned.
1023 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001024 return;
1025
1026 // We need a label after this instruction.
1027 if (!PrevLabel) {
1028 PrevLabel = MMI->getContext().CreateTempSymbol();
1029 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001030 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001031 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001032}
1033
Eric Christopherb6dc8652012-11-27 22:43:45 +00001034// Each LexicalScope has first instruction and last instruction to mark
1035// beginning and end of a scope respectively. Create an inverse map that list
1036// scopes starts (and ends) with an instruction. One instruction may start (or
1037// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001038void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001039 SmallVector<LexicalScope *, 4> WorkList;
1040 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001041 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001042 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001043
Craig Topper9f43ac62013-07-03 04:24:43 +00001044 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001045 if (!Children.empty())
Stephen Hines36b56882014-04-23 16:57:46 -07001046 WorkList.append(Children.begin(), Children.end());
Devang Patel42aafd72010-01-20 02:05:23 +00001047
Devang Patel53bb5c92009-11-10 23:06:00 +00001048 if (S->isAbstractScope())
1049 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001050
Stephen Hines36b56882014-04-23 16:57:46 -07001051 for (const InsnRange &R : S->getRanges()) {
1052 assert(R.first && "InsnRange does not have first instruction!");
1053 assert(R.second && "InsnRange does not have second instruction!");
1054 requestLabelBeforeInsn(R.first);
1055 requestLabelAfterInsn(R.second);
Devang Pateleac9c072010-04-27 19:46:33 +00001056 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001057 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001058}
1059
Stephen Hinesdce4a402014-05-29 02:49:00 -07001060static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1061 // First known non-DBG_VALUE and non-frame setup location marks
1062 // the beginning of the function body.
1063 for (const auto &MBB : *MF)
1064 for (const auto &MI : MBB)
1065 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001066 !MI.getDebugLoc().isUnknown()) {
1067 // Did the target forget to set the FrameSetup flag for CFI insns?
1068 assert(!MI.isCFIInstruction() &&
1069 "First non-frame-setup instruction is a CFI instruction.");
Stephen Hinesdce4a402014-05-29 02:49:00 -07001070 return MI.getDebugLoc();
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001071 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001072 return DebugLoc();
1073}
1074
Eric Christopherb6dc8652012-11-27 22:43:45 +00001075// Gather pre-function debug information. Assumes being called immediately
1076// after the function entry point has been emitted.
Chris Lattnereec791a2010-01-26 23:18:02 +00001077void DwarfDebug::beginFunction(const MachineFunction *MF) {
Stephen Hines36b56882014-04-23 16:57:46 -07001078 CurFn = MF;
Eric Christopher31ed50c2013-11-01 23:14:17 +00001079
1080 // If there's no debug info for the function we're not going to do anything.
1081 if (!MMI->hasDebugInfo())
1082 return;
1083
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001084 auto DI = FunctionDIs.find(MF->getFunction());
1085 if (DI == FunctionDIs.end())
1086 return;
1087
Eric Christopher31ed50c2013-11-01 23:14:17 +00001088 // Grab the lexical scopes for the function, if we don't have any of those
1089 // then we're not going to be able to do anything.
Devang Patelbf47fdb2011-08-10 20:55:27 +00001090 LScopes.initialize(*MF);
Eric Christopher31ed50c2013-11-01 23:14:17 +00001091 if (LScopes.empty())
1092 return;
1093
Stephen Hinesdce4a402014-05-29 02:49:00 -07001094 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
Eric Christopher31ed50c2013-11-01 23:14:17 +00001095
1096 // Make sure that each lexical scope will have a begin/end label.
Devang Patelbf47fdb2011-08-10 20:55:27 +00001097 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001098
Stephen Hines36b56882014-04-23 16:57:46 -07001099 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopher31ed50c2013-11-01 23:14:17 +00001100 // belongs to so that we add to the correct per-cu line table in the
1101 // non-asm case.
Manman Ren43213cf2013-02-05 21:52:47 +00001102 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001103 // FnScope->getScopeNode() and DI->second should represent the same function,
1104 // though they may not be the same MDNode due to inline functions merged in
1105 // LTO where the debug info metadata still differs (either due to distinct
1106 // written differences - two versions of a linkonce_odr function
1107 // written/copied into two separate files, or some sub-optimal metadata that
1108 // isn't structurally identical (see: file path/name info from clang, which
1109 // includes the directory of the cpp file being built, even when the file name
1110 // is absolute (such as an <> lookup header)))
Stephen Hines36b56882014-04-23 16:57:46 -07001111 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren43213cf2013-02-05 21:52:47 +00001112 assert(TheCU && "Unable to find compile unit!");
Stephen Hines36b56882014-04-23 16:57:46 -07001113 if (Asm->OutStreamer.hasRawTextSupport())
1114 // Use a single line table if we are generating assembly.
Manman Ren01cb18e2013-05-21 00:57:22 +00001115 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1116 else
1117 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren43213cf2013-02-05 21:52:47 +00001118
Eric Christopher31ed50c2013-11-01 23:14:17 +00001119 // Emit a label for the function so that we have a beginning address.
1120 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001121 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001122 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001123
Stephen Hinesdce4a402014-05-29 02:49:00 -07001124 // Calculate history for local variables.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001125 calculateDbgValueHistory(MF, Asm->MF->getSubtarget().getRegisterInfo(),
Stephen Hines37ed9c12014-12-01 14:51:49 -08001126 DbgValues);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001127
Stephen Hinesdce4a402014-05-29 02:49:00 -07001128 // Request labels for the full history.
1129 for (const auto &I : DbgValues) {
1130 const auto &Ranges = I.second;
1131 if (Ranges.empty())
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001132 continue;
1133
Stephen Hinesdce4a402014-05-29 02:49:00 -07001134 // The first mention of a function argument gets the FunctionBeginSym
1135 // label, so arguments are visible when breaking at function entry.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001136 DIVariable DIVar(Ranges.front().first->getDebugVariable());
1137 if (DIVar.isVariable() && DIVar.getTag() == dwarf::DW_TAG_arg_variable &&
1138 getDISubprogram(DIVar.getContext()).describes(MF->getFunction())) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001139 LabelsBeforeInsn[Ranges.front().first] = FunctionBeginSym;
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001140 if (Ranges.front().first->getDebugExpression().isBitPiece()) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001141 // Mark all non-overlapping initial pieces.
1142 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
1143 DIExpression Piece = I->first->getDebugExpression();
1144 if (std::all_of(Ranges.begin(), I,
1145 [&](DbgValueHistoryMap::InstrRange Pred) {
1146 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
1147 }))
1148 LabelsBeforeInsn[I->first] = FunctionBeginSym;
1149 else
1150 break;
1151 }
1152 }
1153 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001154
1155 for (const auto &Range : Ranges) {
1156 requestLabelBeforeInsn(Range.first);
1157 if (Range.second)
1158 requestLabelAfterInsn(Range.second);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001159 }
1160 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001161
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001162 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001163 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001164
1165 // Record beginning of function.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001166 PrologEndLoc = findPrologueEndLoc(MF);
Devang Patel4243e672011-05-11 19:22:19 +00001167 if (!PrologEndLoc.isUnknown()) {
Eric Christopher31ed50c2013-11-01 23:14:17 +00001168 DebugLoc FnStartDL =
Stephen Hines36b56882014-04-23 16:57:46 -07001169 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001170
1171 // We'd like to list the prologue as "not statements" but GDB behaves
1172 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1173 recordSourceLine(FnStartDL.getLine(), FnStartDL.getCol(),
1174 FnStartDL.getScope(MF->getFunction()->getContext()),
1175 DWARF2_FLAG_IS_STMT);
Devang Patel4243e672011-05-11 19:22:19 +00001176 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001177}
1178
Eric Christopherb6dc8652012-11-27 22:43:45 +00001179// Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +00001180void DwarfDebug::endFunction(const MachineFunction *MF) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001181 assert(CurFn == MF &&
1182 "endFunction should be called with the same function as beginFunction");
Stephen Hines36b56882014-04-23 16:57:46 -07001183
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001184 if (!MMI->hasDebugInfo() || LScopes.empty() ||
1185 !FunctionDIs.count(MF->getFunction())) {
Stephen Hines36b56882014-04-23 16:57:46 -07001186 // If we don't have a lexical scope for this function then there will
1187 // be a hole in the range information. Keep note of this by setting the
1188 // previously used section to nullptr.
Stephen Hines36b56882014-04-23 16:57:46 -07001189 PrevCU = nullptr;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001190 CurFn = nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -07001191 return;
1192 }
Devang Patel70d75ca2009-11-12 19:02:56 +00001193
Devang Patelbf47fdb2011-08-10 20:55:27 +00001194 // Define end label for subprogram.
Stephen Hines36b56882014-04-23 16:57:46 -07001195 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001196 // Assumes in correct section after the entry point.
1197 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Stephen Hines36b56882014-04-23 16:57:46 -07001198
1199 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Manman Ren43213cf2013-02-05 21:52:47 +00001200 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001201
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001202 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001203 DISubprogram SP(FnScope->getScopeNode());
1204 DwarfCompileUnit &TheCU = *SPMap.lookup(SP);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001205
Stephen Hines37ed9c12014-12-01 14:51:49 -08001206 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1207 collectVariableInfo(TheCU, SP, ProcessedVars);
1208
1209 // Add the range of this function to the list of ranges for the CU.
1210 TheCU.addRange(RangeSpan(FunctionBeginSym, FunctionEndSym));
1211
1212 // Under -gmlt, skip building the subprogram if there are no inlined
1213 // subroutines inside it.
1214 if (TheCU.getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly &&
1215 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1216 assert(InfoHolder.getScopeVariables().empty());
1217 assert(DbgValues.empty());
1218 // FIXME: This wouldn't be true in LTO with a -g (with inlining) CU followed
1219 // by a -gmlt CU. Add a test and remove this assertion.
1220 assert(AbstractVariables.empty());
1221 LabelsBeforeInsn.clear();
1222 LabelsAfterInsn.clear();
1223 PrevLabel = nullptr;
1224 CurFn = nullptr;
1225 return;
1226 }
1227
1228#ifndef NDEBUG
1229 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1230#endif
Devang Patelbf47fdb2011-08-10 20:55:27 +00001231 // Construct abstract scopes.
Stephen Hines36b56882014-04-23 16:57:46 -07001232 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Devang Patelcd9f6c52011-08-12 18:10:19 +00001233 DISubprogram SP(AScope->getScopeNode());
Stephen Hines37ed9c12014-12-01 14:51:49 -08001234 assert(SP.isSubprogram());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001235 // Collect info for variables that were optimized out.
1236 DIArray Variables = SP.getVariables();
1237 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1238 DIVariable DV(Variables.getElement(i));
1239 assert(DV && DV.isVariable());
Stephen Hines37ed9c12014-12-01 14:51:49 -08001240 if (!ProcessedVars.insert(DV).second)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001241 continue;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001242 ensureAbstractVariableIsCreated(DV, DV.getContext());
Stephen Hines37ed9c12014-12-01 14:51:49 -08001243 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1244 && "ensureAbstractVariableIsCreated inserted abstract scopes");
Devang Patel78e127d2010-06-25 22:07:34 +00001245 }
Stephen Hines37ed9c12014-12-01 14:51:49 -08001246 constructAbstractSubprogramScopeDIE(AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001247 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001248
Stephen Hines37ed9c12014-12-01 14:51:49 -08001249 TheCU.constructSubprogramScopeDIE(FnScope);
1250 if (auto *SkelCU = TheCU.getSkeleton())
1251 if (!LScopes.getAbstractScopesList().empty())
1252 SkelCU->constructSubprogramScopeDIE(FnScope);
Stephen Hines36b56882014-04-23 16:57:46 -07001253
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001254 // Clear debug info
Stephen Hinesdce4a402014-05-29 02:49:00 -07001255 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1256 // DbgVariables except those that are also in AbstractVariables (since they
1257 // can be used cross-function)
Stephen Hines37ed9c12014-12-01 14:51:49 -08001258 InfoHolder.getScopeVariables().clear();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001259 DbgValues.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001260 LabelsBeforeInsn.clear();
1261 LabelsAfterInsn.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001262 PrevLabel = nullptr;
1263 CurFn = nullptr;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001264}
1265
Eric Christopherb6dc8652012-11-27 22:43:45 +00001266// Register a source line with debug info. Returns the unique label that was
1267// emitted and which provides correspondence to the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001268void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1269 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001270 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001271 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001272 unsigned Src = 1;
Stephen Hines36b56882014-04-23 16:57:46 -07001273 unsigned Discriminator = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001274 if (DIScope Scope = DIScope(S)) {
1275 assert(Scope.isScope());
1276 Fn = Scope.getFilename();
1277 Dir = Scope.getDirectory();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001278 if (Scope.isLexicalBlockFile())
1279 Discriminator = DILexicalBlockFile(S).getDiscriminator();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001280
Stephen Hines36b56882014-04-23 16:57:46 -07001281 unsigned CUID = Asm->OutStreamer.getContext().getDwarfCompileUnitID();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001282 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1283 .getOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001284 }
Stephen Hines36b56882014-04-23 16:57:46 -07001285 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1286 Discriminator, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001287}
1288
Bill Wendling829e67b2009-05-20 23:22:40 +00001289//===----------------------------------------------------------------------===//
1290// Emit Methods
1291//===----------------------------------------------------------------------===//
1292
Eric Christopherb6dc8652012-11-27 22:43:45 +00001293// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001294void DwarfDebug::emitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001295 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001296
Bill Wendling94d04b82009-05-20 23:21:38 +00001297 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001298 DwarfInfoSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001299 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001300 if (useSplitDwarf()) {
Stephen Hines36b56882014-04-23 16:57:46 -07001301 DwarfInfoDWOSectionSym =
1302 emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001303 DwarfTypesDWOSectionSym = emitSectionSym(
1304 Asm, TLOF.getDwarfTypesDWOSection(), "section_types_dwo");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001305 }
Stephen Hines36b56882014-04-23 16:57:46 -07001306 DwarfAbbrevSectionSym =
1307 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
1308 if (useSplitDwarf())
1309 DwarfAbbrevDWOSectionSym = emitSectionSym(
1310 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
1311 if (GenerateARangeSection)
1312 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001313
Eric Christopher7a0103c2013-02-07 21:19:50 +00001314 DwarfLineSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001315 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christophercdeaae42013-09-23 20:55:35 +00001316 if (GenerateGnuPubSections) {
Eric Christophera6d84152013-09-30 23:14:16 +00001317 DwarfGnuPubNamesSectionSym =
1318 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1319 DwarfGnuPubTypesSectionSym =
1320 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christophercdeaae42013-09-23 20:55:35 +00001321 } else if (HasDwarfPubSections) {
1322 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1323 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmandc42d032013-09-04 04:39:38 +00001324 }
Eric Christopher8f1a9292013-09-13 00:35:05 +00001325
Jim Grosbach1e20b962010-07-21 21:21:52 +00001326 DwarfStrSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001327 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher9a9e73b2013-04-07 03:43:09 +00001328 if (useSplitDwarf()) {
Eric Christopher64f824c2012-12-27 02:14:01 +00001329 DwarfStrDWOSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001330 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher9a9e73b2013-04-07 03:43:09 +00001331 DwarfAddrSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001332 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
1333 DwarfDebugLocSectionSym =
1334 emitSectionSym(Asm, TLOF.getDwarfLocDWOSection(), "skel_loc");
1335 } else
1336 DwarfDebugLocSectionSym =
1337 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
1338 DwarfDebugRangeSectionSym =
1339 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001340}
1341
Eric Christopherb6dc8652012-11-27 22:43:45 +00001342// Recursively emits a debug information entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001343void DwarfDebug::emitDIE(DIE &Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001344 // Get the abbreviation for this DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001345 const DIEAbbrev &Abbrev = Die.getAbbrev();
Bill Wendling94d04b82009-05-20 23:21:38 +00001346
Bill Wendling94d04b82009-05-20 23:21:38 +00001347 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001348 if (Asm->isVerbose())
Stephen Hines36b56882014-04-23 16:57:46 -07001349 Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
Stephen Hinesdce4a402014-05-29 02:49:00 -07001350 "] 0x" + Twine::utohexstr(Die.getOffset()) +
1351 ":0x" + Twine::utohexstr(Die.getSize()) + " " +
Stephen Hines36b56882014-04-23 16:57:46 -07001352 dwarf::TagString(Abbrev.getTag()));
1353 Asm->EmitULEB128(Abbrev.getNumber());
Bill Wendling94d04b82009-05-20 23:21:38 +00001354
Stephen Hinesdce4a402014-05-29 02:49:00 -07001355 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
Stephen Hines36b56882014-04-23 16:57:46 -07001356 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling94d04b82009-05-20 23:21:38 +00001357
1358 // Emit the DIE attribute values.
1359 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikie770530b2013-10-21 17:28:37 +00001360 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
1361 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling94d04b82009-05-20 23:21:38 +00001362 assert(Form && "Too many attributes for DIE (check abbreviation)");
1363
Stephen Hines36b56882014-04-23 16:57:46 -07001364 if (Asm->isVerbose()) {
Chris Lattnera8013622010-01-24 18:54:17 +00001365 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Stephen Hines36b56882014-04-23 16:57:46 -07001366 if (Attr == dwarf::DW_AT_accessibility)
1367 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(
1368 cast<DIEInteger>(Values[i])->getValue()));
1369 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001370
Stephen Hines36b56882014-04-23 16:57:46 -07001371 // Emit an attribute using the defined form.
1372 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001373 }
1374
1375 // Emit the DIE children if any.
Stephen Hines36b56882014-04-23 16:57:46 -07001376 if (Abbrev.hasChildren()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001377 for (auto &Child : Die.getChildren())
1378 emitDIE(*Child);
Bill Wendling94d04b82009-05-20 23:21:38 +00001379
Stephen Hines36b56882014-04-23 16:57:46 -07001380 Asm->OutStreamer.AddComment("End Of Children Mark");
Chris Lattner233f52b2010-03-09 23:52:58 +00001381 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001382 }
1383}
1384
Eric Christopher98e237f2012-11-30 23:59:06 +00001385// Emit the debug info section.
1386void DwarfDebug::emitDebugInfo() {
Stephen Hines36b56882014-04-23 16:57:46 -07001387 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopherb1e66d02012-12-15 00:04:07 +00001388
Stephen Hines37ed9c12014-12-01 14:51:49 -08001389 Holder.emitUnits(DwarfAbbrevSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00001390}
1391
Eric Christopherb6dc8652012-11-27 22:43:45 +00001392// Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +00001393void DwarfDebug::emitAbbreviations() {
Stephen Hines36b56882014-04-23 16:57:46 -07001394 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1395
1396 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher6eebe472012-12-19 22:02:53 +00001397}
Bill Wendling94d04b82009-05-20 23:21:38 +00001398
Eric Christopherb6dc8652012-11-27 22:43:45 +00001399// Emit the last address of the section and the end of the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001400void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001401 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001402 Asm->OutStreamer.AddComment("Extended Op");
1403 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001404
Chris Lattner233f52b2010-03-09 23:52:58 +00001405 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001406 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001407 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1408 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1409
1410 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001411
Stephen Hines36b56882014-04-23 16:57:46 -07001412 Asm->OutStreamer.EmitSymbolValue(
1413 Asm->GetTempSymbol("section_end", SectionEnd),
1414 Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00001415
1416 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001417 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1418 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001419 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001420 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001421}
1422
Stephen Hines37ed9c12014-12-01 14:51:49 -08001423void DwarfDebug::emitAccel(DwarfAccelTable &Accel, const MCSection *Section,
1424 StringRef TableName, StringRef SymName) {
1425 Accel.FinalizeTable(Asm, TableName);
1426 Asm->OutStreamer.SwitchSection(Section);
1427 auto *SectionBegin = Asm->GetTempSymbol(SymName);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001428 Asm->OutStreamer.EmitLabel(SectionBegin);
1429
1430 // Emit the full data.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001431 Accel.Emit(Asm, SectionBegin, this, DwarfStrSectionSym);
1432}
1433
1434// Emit visible names into a hashed accelerator table section.
1435void DwarfDebug::emitAccelNames() {
1436 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1437 "Names", "names_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001438}
1439
Eric Christopher72c16552012-12-20 21:58:40 +00001440// Emit objective C classes and categories into a hashed accelerator table
1441// section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001442void DwarfDebug::emitAccelObjC() {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001443 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1444 "ObjC", "objc_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001445}
1446
Eric Christopherb6dc8652012-11-27 22:43:45 +00001447// Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001448void DwarfDebug::emitAccelNamespaces() {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001449 emitAccel(AccelNamespace,
1450 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
1451 "namespac", "namespac_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001452}
1453
Eric Christopherb6dc8652012-11-27 22:43:45 +00001454// Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001455void DwarfDebug::emitAccelTypes() {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001456 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1457 "types", "types_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001458}
1459
Eric Christopher8f1a9292013-09-13 00:35:05 +00001460// Public name handling.
1461// The format for the various pubnames:
1462//
1463// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1464// for the DIE that is named.
1465//
1466// gnu pubnames - offset/index value/name tuples where the offset is the offset
1467// into the CU and the index value is computed according to the type of value
1468// for the DIE that is named.
1469//
1470// For type units the offset is the offset of the skeleton DIE. For split dwarf
1471// it's the offset within the debug_info/debug_types dwo section, however, the
1472// reference in the pubname header doesn't change.
1473
1474/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Stephen Hines36b56882014-04-23 16:57:46 -07001475static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
1476 const DIE *Die) {
Eric Christophera486f552013-10-16 01:37:49 +00001477 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1478
1479 // We could have a specification DIE that has our most of our knowledge,
1480 // look for that now.
1481 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
1482 if (SpecVal) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001483 DIE &SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
1484 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
Eric Christophera486f552013-10-16 01:37:49 +00001485 Linkage = dwarf::GIEL_EXTERNAL;
1486 } else if (Die->findAttribute(dwarf::DW_AT_external))
1487 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopher8f1a9292013-09-13 00:35:05 +00001488
1489 switch (Die->getTag()) {
1490 case dwarf::DW_TAG_class_type:
1491 case dwarf::DW_TAG_structure_type:
1492 case dwarf::DW_TAG_union_type:
1493 case dwarf::DW_TAG_enumeration_type:
Eric Christophercdeaae42013-09-23 20:55:35 +00001494 return dwarf::PubIndexEntryDescriptor(
1495 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1496 ? dwarf::GIEL_STATIC
1497 : dwarf::GIEL_EXTERNAL);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001498 case dwarf::DW_TAG_typedef:
1499 case dwarf::DW_TAG_base_type:
1500 case dwarf::DW_TAG_subrange_type:
David Blaikie9599f512013-09-19 20:40:26 +00001501 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001502 case dwarf::DW_TAG_namespace:
David Blaikie9599f512013-09-19 20:40:26 +00001503 return dwarf::GIEK_TYPE;
Eric Christopher8f1a9292013-09-13 00:35:05 +00001504 case dwarf::DW_TAG_subprogram:
Eric Christophera31a9752013-09-23 22:59:14 +00001505 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001506 case dwarf::DW_TAG_variable:
Eric Christophera31a9752013-09-23 22:59:14 +00001507 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001508 case dwarf::DW_TAG_enumerator:
David Blaikie9599f512013-09-19 20:40:26 +00001509 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1510 dwarf::GIEL_STATIC);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001511 default:
David Blaikie9599f512013-09-19 20:40:26 +00001512 return dwarf::GIEK_NONE;
Eric Christopher8f1a9292013-09-13 00:35:05 +00001513 }
Eric Christopher8f1a9292013-09-13 00:35:05 +00001514}
1515
Eric Christopher57479322013-09-09 20:03:17 +00001516/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001517///
Eric Christopher8f1a9292013-09-13 00:35:05 +00001518void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Eric Christopher8f1a9292013-09-13 00:35:05 +00001519 const MCSection *PSec =
1520 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1521 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001522
Stephen Hines37ed9c12014-12-01 14:51:49 -08001523 emitDebugPubSection(GnuStyle, PSec, "Names",
1524 &DwarfCompileUnit::getGlobalNames);
Stephen Hines36b56882014-04-23 16:57:46 -07001525}
1526
1527void DwarfDebug::emitDebugPubSection(
1528 bool GnuStyle, const MCSection *PSec, StringRef Name,
Stephen Hines37ed9c12014-12-01 14:51:49 -08001529 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) {
Stephen Hines36b56882014-04-23 16:57:46 -07001530 for (const auto &NU : CUMap) {
1531 DwarfCompileUnit *TheU = NU.second;
1532
1533 const auto &Globals = (TheU->*Accessor)();
1534
1535 if (Globals.empty())
1536 continue;
1537
Stephen Hines37ed9c12014-12-01 14:51:49 -08001538 if (auto *Skeleton = TheU->getSkeleton())
Stephen Hines36b56882014-04-23 16:57:46 -07001539 TheU = Skeleton;
1540 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001541
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001542 // Start the dwarf pubnames section.
Eric Christopher21a6a502013-09-10 21:49:37 +00001543 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001544
Eric Christopher8f1a9292013-09-13 00:35:05 +00001545 // Emit the header.
Stephen Hines36b56882014-04-23 16:57:46 -07001546 Asm->OutStreamer.AddComment("Length of Public " + Name + " Info");
1547 MCSymbol *BeginLabel = Asm->GetTempSymbol("pub" + Name + "_begin", ID);
1548 MCSymbol *EndLabel = Asm->GetTempSymbol("pub" + Name + "_end", ID);
1549 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001550
Stephen Hines36b56882014-04-23 16:57:46 -07001551 Asm->OutStreamer.EmitLabel(BeginLabel);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001552
1553 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemerd7d43dc2013-08-21 06:13:34 +00001554 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001555
1556 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Stephen Hines36b56882014-04-23 16:57:46 -07001557 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001558
1559 Asm->OutStreamer.AddComment("Compilation Unit Length");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001560 Asm->EmitInt32(TheU->getLength());
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001561
Eric Christopher8f1a9292013-09-13 00:35:05 +00001562 // Emit the pubnames for this compilation unit.
Stephen Hines36b56882014-04-23 16:57:46 -07001563 for (const auto &GI : Globals) {
1564 const char *Name = GI.getKeyData();
1565 const DIE *Entity = GI.second;
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001566
1567 Asm->OutStreamer.AddComment("DIE offset");
1568 Asm->EmitInt32(Entity->getOffset());
1569
Eric Christopher8f1a9292013-09-13 00:35:05 +00001570 if (GnuStyle) {
Stephen Hines36b56882014-04-23 16:57:46 -07001571 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikie18a6ade2013-09-19 22:19:37 +00001572 Asm->OutStreamer.AddComment(
David Blaikieac3f0162013-09-20 00:33:15 +00001573 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie994c37f2013-09-19 23:01:29 +00001574 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikie18a6ade2013-09-19 22:19:37 +00001575 Asm->EmitInt8(Desc.toBits());
Eric Christopher8f1a9292013-09-13 00:35:05 +00001576 }
1577
Stephen Hines36b56882014-04-23 16:57:46 -07001578 Asm->OutStreamer.AddComment("External Name");
1579 Asm->OutStreamer.EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001580 }
1581
1582 Asm->OutStreamer.AddComment("End Mark");
1583 Asm->EmitInt32(0);
Stephen Hines36b56882014-04-23 16:57:46 -07001584 Asm->OutStreamer.EmitLabel(EndLabel);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001585 }
1586}
1587
Eric Christopher8f1a9292013-09-13 00:35:05 +00001588void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christophercdeaae42013-09-23 20:55:35 +00001589 const MCSection *PSec =
1590 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1591 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher5704d642013-09-13 00:34:58 +00001592
Stephen Hines37ed9c12014-12-01 14:51:49 -08001593 emitDebugPubSection(GnuStyle, PSec, "Types",
1594 &DwarfCompileUnit::getGlobalTypes);
Devang Patel193f7202009-11-24 01:14:22 +00001595}
1596
Eric Christopher64f824c2012-12-27 02:14:01 +00001597// Emit visible names into a debug str section.
1598void DwarfDebug::emitDebugStr() {
Stephen Hines36b56882014-04-23 16:57:46 -07001599 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher64f824c2012-12-27 02:14:01 +00001600 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1601}
1602
Stephen Hines37ed9c12014-12-01 14:51:49 -08001603/// Emits an optimal (=sorted) sequence of DW_OP_pieces.
1604void DwarfDebug::emitLocPieces(ByteStreamer &Streamer,
1605 const DITypeIdentifierMap &Map,
1606 ArrayRef<DebugLocEntry::Value> Values) {
1607 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value P) {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001608 return P.isBitPiece();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001609 }) && "all values are expected to be pieces");
1610 assert(std::is_sorted(Values.begin(), Values.end()) &&
1611 "pieces are expected to be sorted");
1612
1613 unsigned Offset = 0;
1614 for (auto Piece : Values) {
1615 DIExpression Expr = Piece.getExpression();
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001616 unsigned PieceOffset = Expr.getBitPieceOffset();
1617 unsigned PieceSize = Expr.getBitPieceSize();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001618 assert(Offset <= PieceOffset && "overlapping or duplicate pieces");
1619 if (Offset < PieceOffset) {
1620 // The DWARF spec seriously mandates pieces with no locations for gaps.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001621 Asm->EmitDwarfOpPiece(Streamer, PieceOffset-Offset);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001622 Offset += PieceOffset-Offset;
1623 }
Stephen Hines37ed9c12014-12-01 14:51:49 -08001624 Offset += PieceSize;
1625
Stephen Hines37ed9c12014-12-01 14:51:49 -08001626#ifndef NDEBUG
1627 DIVariable Var = Piece.getVariable();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001628 unsigned VarSize = Var.getSizeInBits(Map);
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001629 assert(PieceSize+PieceOffset <= VarSize
Stephen Hines37ed9c12014-12-01 14:51:49 -08001630 && "piece is larger than or outside of variable");
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001631 assert(PieceSize != VarSize
Stephen Hines37ed9c12014-12-01 14:51:49 -08001632 && "piece covers entire variable");
1633#endif
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001634 emitDebugLocValue(Streamer, Piece, PieceOffset);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001635 }
1636}
1637
1638
Stephen Hines36b56882014-04-23 16:57:46 -07001639void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
1640 const DebugLocEntry &Entry) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001641 const DebugLocEntry::Value Value = Entry.getValues()[0];
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001642 if (Value.isBitPiece())
Stephen Hines37ed9c12014-12-01 14:51:49 -08001643 // Emit all pieces that belong to the same variable and range.
1644 return emitLocPieces(Streamer, TypeIdentifierMap, Entry.getValues());
1645
1646 assert(Entry.getValues().size() == 1 && "only pieces may have >1 value");
1647 emitDebugLocValue(Streamer, Value);
1648}
1649
1650void DwarfDebug::emitDebugLocValue(ByteStreamer &Streamer,
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001651 const DebugLocEntry::Value &Value,
1652 unsigned PieceOffsetInBits) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001653 DIVariable DV = Value.getVariable();
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001654 DebugLocDwarfExpression DwarfExpr(*Asm, Streamer);
1655
Stephen Hines37ed9c12014-12-01 14:51:49 -08001656 // Regular entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001657 if (Value.isInt()) {
Stephen Hines36b56882014-04-23 16:57:46 -07001658 DIBasicType BTy(resolve(DV.getType()));
1659 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001660 BTy.getEncoding() == dwarf::DW_ATE_signed_char))
1661 DwarfExpr.AddSignedConstant(Value.getInt());
1662 else
1663 DwarfExpr.AddUnsignedConstant(Value.getInt());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001664 } else if (Value.isLocation()) {
1665 MachineLocation Loc = Value.getLoc();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001666 DIExpression Expr = Value.getExpression();
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001667 if (!Expr || (Expr.getNumElements() == 0))
Stephen Hines36b56882014-04-23 16:57:46 -07001668 // Regular entry.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001669 Asm->EmitDwarfRegOp(Streamer, Loc);
Stephen Hines36b56882014-04-23 16:57:46 -07001670 else {
1671 // Complex address entry.
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001672 if (Loc.getOffset()) {
1673 DwarfExpr.AddMachineRegIndirect(Loc.getReg(), Loc.getOffset());
1674 DwarfExpr.AddExpression(Expr.begin(), Expr.end(), PieceOffsetInBits);
1675 } else
1676 DwarfExpr.AddMachineRegExpression(Expr, Loc.getReg(),
1677 PieceOffsetInBits);
Stephen Hines36b56882014-04-23 16:57:46 -07001678 }
1679 }
1680 // else ... ignore constant fp. There is not any good way to
1681 // to represent them here in dwarf.
1682 // FIXME: ^
1683}
1684
1685void DwarfDebug::emitDebugLocEntryLocation(const DebugLocEntry &Entry) {
1686 Asm->OutStreamer.AddComment("Loc expr size");
1687 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1688 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1689 Asm->EmitLabelDifference(end, begin, 2);
1690 Asm->OutStreamer.EmitLabel(begin);
1691 // Emit the entry.
1692 APByteStreamer Streamer(*Asm);
1693 emitDebugLocEntry(Streamer, Entry);
1694 // Close the range.
1695 Asm->OutStreamer.EmitLabel(end);
1696}
1697
Eric Christopherd3b98532013-07-02 21:36:07 +00001698// Emit locations into the debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001699void DwarfDebug::emitDebugLoc() {
Daniel Dunbar83320a02011-03-16 22:16:39 +00001700 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001701 Asm->OutStreamer.SwitchSection(
Stephen Hines36b56882014-04-23 16:57:46 -07001702 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001703 unsigned char Size = Asm->getDataLayout().getPointerSize();
Stephen Hines36b56882014-04-23 16:57:46 -07001704 for (const auto &DebugLoc : DotDebugLocEntries) {
1705 Asm->OutStreamer.EmitLabel(DebugLoc.Label);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001706 const DwarfCompileUnit *CU = DebugLoc.CU;
Stephen Hines36b56882014-04-23 16:57:46 -07001707 for (const auto &Entry : DebugLoc.List) {
1708 // Set up the range. This range is relative to the entry point of the
1709 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1710 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001711 if (auto *Base = CU->getBaseAddress()) {
Stephen Hines36b56882014-04-23 16:57:46 -07001712 Asm->EmitLabelDifference(Entry.getBeginSym(), Base, Size);
1713 Asm->EmitLabelDifference(Entry.getEndSym(), Base, Size);
1714 } else {
1715 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
1716 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
Devang Patelc26f5442011-04-28 02:22:40 +00001717 }
Stephen Hines36b56882014-04-23 16:57:46 -07001718
1719 emitDebugLocEntryLocation(Entry);
Devang Patelc3f5f782010-05-25 23:40:22 +00001720 }
Stephen Hines36b56882014-04-23 16:57:46 -07001721 Asm->OutStreamer.EmitIntValue(0, Size);
1722 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patelc3f5f782010-05-25 23:40:22 +00001723 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001724}
1725
Stephen Hines36b56882014-04-23 16:57:46 -07001726void DwarfDebug::emitDebugLocDWO() {
1727 Asm->OutStreamer.SwitchSection(
1728 Asm->getObjFileLowering().getDwarfLocDWOSection());
1729 for (const auto &DebugLoc : DotDebugLocEntries) {
1730 Asm->OutStreamer.EmitLabel(DebugLoc.Label);
1731 for (const auto &Entry : DebugLoc.List) {
1732 // Just always use start_length for now - at least that's one address
1733 // rather than two. We could get fancier and try to, say, reuse an
1734 // address we know we've emitted elsewhere (the start of the function?
1735 // The start of the CU or CU subrange that encloses this range?)
1736 Asm->EmitInt8(dwarf::DW_LLE_start_length_entry);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001737 unsigned idx = AddrPool.getIndex(Entry.getBeginSym());
Stephen Hines36b56882014-04-23 16:57:46 -07001738 Asm->EmitULEB128(idx);
1739 Asm->EmitLabelDifference(Entry.getEndSym(), Entry.getBeginSym(), 4);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001740
Stephen Hines36b56882014-04-23 16:57:46 -07001741 emitDebugLocEntryLocation(Entry);
1742 }
1743 Asm->EmitInt8(dwarf::DW_LLE_end_of_list_entry);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001744 }
Richard Mitton5cc319a2013-09-19 23:21:01 +00001745}
1746
1747struct ArangeSpan {
1748 const MCSymbol *Start, *End;
1749};
1750
1751// Emit a debug aranges section, containing a CU lookup for any
1752// address we can tie back to a CU.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001753void DwarfDebug::emitDebugARanges() {
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001754 // Provides a unique id per text section.
1755 DenseMap<const MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001756
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001757 // Prime section data.
1758 SectionMap[Asm->getObjFileLowering().getTextSection()];
Richard Mitton5cc319a2013-09-19 23:21:01 +00001759
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001760 // Filter labels by section.
1761 for (const SymbolCU &SCU : ArangeLabels) {
1762 if (SCU.Sym->isInSection()) {
1763 // Make a note of this symbol and it's section.
1764 const MCSection *Section = &SCU.Sym->getSection();
1765 if (!Section->getKind().isMetadata())
1766 SectionMap[Section].push_back(SCU);
1767 } else {
1768 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1769 // appear in the output. This sucks as we rely on sections to build
1770 // arange spans. We can do it without, but it's icky.
1771 SectionMap[nullptr].push_back(SCU);
1772 }
1773 }
Richard Mitton5cc319a2013-09-19 23:21:01 +00001774
1775 // Build a list of sections used.
1776 std::vector<const MCSection *> Sections;
Stephen Hines36b56882014-04-23 16:57:46 -07001777 for (const auto &it : SectionMap) {
1778 const MCSection *Section = it.first;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001779 Sections.push_back(Section);
1780 }
1781
1782 // Sort the sections into order.
1783 // This is only done to ensure consistent output order across different runs.
1784 std::sort(Sections.begin(), Sections.end(), SectionSort);
1785
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001786 // Add terminating symbols for each section.
1787 for (unsigned ID = 0, E = Sections.size(); ID != E; ID++) {
1788 const MCSection *Section = Sections[ID];
1789 MCSymbol *Sym = nullptr;
1790
1791 if (Section) {
1792 // We can't call MCSection::getLabelEndName, as it's only safe to do so
1793 // if we know the section name up-front. For user-created sections, the
1794 // resulting label may not be valid to use as a label. (section names can
1795 // use a greater set of characters on some systems)
1796 Sym = Asm->GetTempSymbol("debug_end", ID);
1797 Asm->OutStreamer.SwitchSection(Section);
1798 Asm->OutStreamer.EmitLabel(Sym);
1799 }
1800
1801 // Insert a final terminator.
1802 SectionMap[Section].push_back(SymbolCU(nullptr, Sym));
1803 }
1804
1805 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
1806
Stephen Hines36b56882014-04-23 16:57:46 -07001807 for (const MCSection *Section : Sections) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001808 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
1809 if (List.size() < 2)
1810 continue;
1811
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001812 // If we have no section (e.g. common), just write out
1813 // individual spans for each symbol.
1814 if (!Section) {
1815 for (const SymbolCU &Cur : List) {
1816 ArangeSpan Span;
1817 Span.Start = Cur.Sym;
1818 Span.End = nullptr;
1819 if (Cur.CU)
1820 Spans[Cur.CU].push_back(Span);
1821 }
1822 continue;
1823 }
1824
Richard Mitton5cc319a2013-09-19 23:21:01 +00001825 // Sort the symbols by offset within the section.
Stephen Hines36b56882014-04-23 16:57:46 -07001826 std::sort(List.begin(), List.end(),
1827 [&](const SymbolCU &A, const SymbolCU &B) {
1828 unsigned IA = A.Sym ? Asm->OutStreamer.GetSymbolOrder(A.Sym) : 0;
1829 unsigned IB = B.Sym ? Asm->OutStreamer.GetSymbolOrder(B.Sym) : 0;
1830
1831 // Symbols with no order assigned should be placed at the end.
1832 // (e.g. section end labels)
1833 if (IA == 0)
1834 return false;
1835 if (IB == 0)
1836 return true;
1837 return IA < IB;
1838 });
Richard Mitton5cc319a2013-09-19 23:21:01 +00001839
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001840 // Build spans between each label.
1841 const MCSymbol *StartSym = List[0].Sym;
1842 for (size_t n = 1, e = List.size(); n < e; n++) {
1843 const SymbolCU &Prev = List[n - 1];
1844 const SymbolCU &Cur = List[n];
Richard Mitton5cc319a2013-09-19 23:21:01 +00001845
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001846 // Try and build the longest span we can within the same CU.
1847 if (Cur.CU != Prev.CU) {
1848 ArangeSpan Span;
1849 Span.Start = StartSym;
1850 Span.End = Cur.Sym;
1851 Spans[Prev.CU].push_back(Span);
1852 StartSym = Cur.Sym;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001853 }
1854 }
1855 }
1856
Stephen Hinesebe69fe2015-03-23 12:10:34 -07001857 // Start the dwarf aranges section.
1858 Asm->OutStreamer.SwitchSection(
1859 Asm->getObjFileLowering().getDwarfARangesSection());
1860
Richard Mitton5cc319a2013-09-19 23:21:01 +00001861 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
1862
1863 // Build a list of CUs used.
Stephen Hines36b56882014-04-23 16:57:46 -07001864 std::vector<DwarfCompileUnit *> CUs;
1865 for (const auto &it : Spans) {
1866 DwarfCompileUnit *CU = it.first;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001867 CUs.push_back(CU);
1868 }
1869
1870 // Sort the CU list (again, to ensure consistent output order).
Stephen Hines36b56882014-04-23 16:57:46 -07001871 std::sort(CUs.begin(), CUs.end(), [](const DwarfUnit *A, const DwarfUnit *B) {
1872 return A->getUniqueID() < B->getUniqueID();
1873 });
Richard Mitton5cc319a2013-09-19 23:21:01 +00001874
1875 // Emit an arange table for each CU we used.
Stephen Hines36b56882014-04-23 16:57:46 -07001876 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001877 std::vector<ArangeSpan> &List = Spans[CU];
1878
Stephen Hines37ed9c12014-12-01 14:51:49 -08001879 // Describe the skeleton CU's offset and length, not the dwo file's.
1880 if (auto *Skel = CU->getSkeleton())
1881 CU = Skel;
1882
Richard Mitton5cc319a2013-09-19 23:21:01 +00001883 // Emit size of content not including length itself.
Stephen Hines36b56882014-04-23 16:57:46 -07001884 unsigned ContentSize =
1885 sizeof(int16_t) + // DWARF ARange version number
1886 sizeof(int32_t) + // Offset of CU in the .debug_info section
1887 sizeof(int8_t) + // Pointer Size (in bytes)
1888 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton5cc319a2013-09-19 23:21:01 +00001889
1890 unsigned TupleSize = PtrSize * 2;
1891
1892 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Stephen Hines36b56882014-04-23 16:57:46 -07001893 unsigned Padding =
1894 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001895
1896 ContentSize += Padding;
1897 ContentSize += (List.size() + 1) * TupleSize;
1898
1899 // For each compile unit, write the list of spans it covers.
1900 Asm->OutStreamer.AddComment("Length of ARange Set");
1901 Asm->EmitInt32(ContentSize);
1902 Asm->OutStreamer.AddComment("DWARF Arange version number");
1903 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
1904 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001905 Asm->EmitSectionOffset(CU->getLabelBegin(), CU->getSectionSym());
Richard Mitton5cc319a2013-09-19 23:21:01 +00001906 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1907 Asm->EmitInt8(PtrSize);
1908 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
1909 Asm->EmitInt8(0);
1910
Stephen Hines36b56882014-04-23 16:57:46 -07001911 Asm->OutStreamer.EmitFill(Padding, 0xff);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001912
Stephen Hines36b56882014-04-23 16:57:46 -07001913 for (const ArangeSpan &Span : List) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001914 Asm->EmitLabelReference(Span.Start, PtrSize);
1915
1916 // Calculate the size as being from the span start to it's end.
Richard Mittoneb46def2013-09-23 17:56:20 +00001917 if (Span.End) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001918 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mittoneb46def2013-09-23 17:56:20 +00001919 } else {
1920 // For symbols without an end marker (e.g. common), we
1921 // write a single arange entry containing just that one symbol.
1922 uint64_t Size = SymSize[Span.Start];
1923 if (Size == 0)
1924 Size = 1;
1925
1926 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
1927 }
Richard Mitton5cc319a2013-09-19 23:21:01 +00001928 }
1929
1930 Asm->OutStreamer.AddComment("ARange terminator");
1931 Asm->OutStreamer.EmitIntValue(0, PtrSize);
1932 Asm->OutStreamer.EmitIntValue(0, PtrSize);
1933 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001934}
1935
Eric Christopherb6dc8652012-11-27 22:43:45 +00001936// Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001937void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001938 // Start the dwarf ranges section.
Stephen Hines36b56882014-04-23 16:57:46 -07001939 Asm->OutStreamer.SwitchSection(
1940 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001941
Stephen Hines36b56882014-04-23 16:57:46 -07001942 // Size for our labels.
1943 unsigned char Size = Asm->getDataLayout().getPointerSize();
1944
1945 // Grab the specific ranges for the compile units in the module.
1946 for (const auto &I : CUMap) {
1947 DwarfCompileUnit *TheCU = I.second;
1948
Stephen Hines37ed9c12014-12-01 14:51:49 -08001949 if (auto *Skel = TheCU->getSkeleton())
1950 TheCU = Skel;
1951
Stephen Hines36b56882014-04-23 16:57:46 -07001952 // Iterate over the misc ranges for the compile units in the module.
1953 for (const RangeSpanList &List : TheCU->getRangeLists()) {
1954 // Emit our symbol so we can find the beginning of the range.
1955 Asm->OutStreamer.EmitLabel(List.getSym());
1956
1957 for (const RangeSpan &Range : List.getRanges()) {
1958 const MCSymbol *Begin = Range.getStart();
1959 const MCSymbol *End = Range.getEnd();
1960 assert(Begin && "Range without a begin symbol?");
1961 assert(End && "Range without an end symbol?");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001962 if (auto *Base = TheCU->getBaseAddress()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001963 Asm->EmitLabelDifference(Begin, Base, Size);
1964 Asm->EmitLabelDifference(End, Base, Size);
1965 } else {
1966 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
1967 Asm->OutStreamer.EmitSymbolValue(End, Size);
1968 }
Stephen Hines36b56882014-04-23 16:57:46 -07001969 }
1970
1971 // And terminate the list with two 0 values.
1972 Asm->OutStreamer.EmitIntValue(0, Size);
1973 Asm->OutStreamer.EmitIntValue(0, Size);
1974 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001975 }
1976}
1977
Eric Christopher0b944ee2012-12-11 19:42:09 +00001978// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher98e237f2012-11-30 23:59:06 +00001979
Stephen Hinesdce4a402014-05-29 02:49:00 -07001980void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
1981 std::unique_ptr<DwarfUnit> NewU) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001982 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
1983 U.getCUNode().getSplitDebugFilename());
Eric Christopher98e237f2012-11-30 23:59:06 +00001984
1985 if (!CompilationDir.empty())
Stephen Hines37ed9c12014-12-01 14:51:49 -08001986 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher98e237f2012-11-30 23:59:06 +00001987
Stephen Hinesdce4a402014-05-29 02:49:00 -07001988 addGnuPubAttributes(*NewU, Die);
Eric Christophera6d84152013-09-30 23:14:16 +00001989
Stephen Hinesdce4a402014-05-29 02:49:00 -07001990 SkeletonHolder.addUnit(std::move(NewU));
Stephen Hines36b56882014-04-23 16:57:46 -07001991}
Eric Christopher8f1a9292013-09-13 00:35:05 +00001992
Stephen Hines36b56882014-04-23 16:57:46 -07001993// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
1994// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
1995// DW_AT_addr_base, DW_AT_ranges_base.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001996DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
Eric Christopher451c71d2013-10-01 00:43:36 +00001997
Stephen Hinesdce4a402014-05-29 02:49:00 -07001998 auto OwnedUnit = make_unique<DwarfCompileUnit>(
1999 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
2000 DwarfCompileUnit &NewCU = *OwnedUnit;
2001 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
2002 DwarfInfoSectionSym);
Stephen Hines36b56882014-04-23 16:57:46 -07002003
Stephen Hinesdce4a402014-05-29 02:49:00 -07002004 NewCU.initStmtList(DwarfLineSectionSym);
Stephen Hines36b56882014-04-23 16:57:46 -07002005
Stephen Hinesdce4a402014-05-29 02:49:00 -07002006 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002007
Eric Christopher98e237f2012-11-30 23:59:06 +00002008 return NewCU;
2009}
2010
Eric Christopher0b944ee2012-12-11 19:42:09 +00002011// Emit the .debug_info.dwo section for separated dwarf. This contains the
2012// compile units that would normally be in debug_info.
Eric Christopher98e237f2012-11-30 23:59:06 +00002013void DwarfDebug::emitDebugInfoDWO() {
Eric Christopher4daaed12012-12-10 19:51:21 +00002014 assert(useSplitDwarf() && "No split dwarf debug info?");
Stephen Hines36b56882014-04-23 16:57:46 -07002015 // Don't pass an abbrev symbol, using a constant zero instead so as not to
2016 // emit relocations into the dwo file.
Stephen Hines37ed9c12014-12-01 14:51:49 -08002017 InfoHolder.emitUnits(/* AbbrevSymbol */ nullptr);
Eric Christopher6eebe472012-12-19 22:02:53 +00002018}
2019
2020// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2021// abbreviations for the .debug_info.dwo section.
2022void DwarfDebug::emitDebugAbbrevDWO() {
2023 assert(useSplitDwarf() && "No split dwarf?");
Stephen Hines36b56882014-04-23 16:57:46 -07002024 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
2025}
2026
2027void DwarfDebug::emitDebugLineDWO() {
2028 assert(useSplitDwarf() && "No split dwarf?");
2029 Asm->OutStreamer.SwitchSection(
2030 Asm->getObjFileLowering().getDwarfLineDWOSection());
2031 SplitTypeUnitFileTable.Emit(Asm->OutStreamer);
Eric Christopher98e237f2012-11-30 23:59:06 +00002032}
Eric Christopher64f824c2012-12-27 02:14:01 +00002033
2034// Emit the .debug_str.dwo section for separated dwarf. This contains the
2035// string section and is identical in format to traditional .debug_str
2036// sections.
2037void DwarfDebug::emitDebugStrDWO() {
2038 assert(useSplitDwarf() && "No split dwarf?");
Stephen Hines36b56882014-04-23 16:57:46 -07002039 const MCSection *OffSec =
2040 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002041 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
Stephen Hines37ed9c12014-12-01 14:51:49 -08002042 OffSec);
Eric Christopher64f824c2012-12-27 02:14:01 +00002043}
Stephen Hines36b56882014-04-23 16:57:46 -07002044
2045MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
2046 if (!useSplitDwarf())
2047 return nullptr;
2048 if (SingleCU)
2049 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode().getDirectory());
2050 return &SplitTypeUnitFileTable;
2051}
2052
Stephen Hinesdce4a402014-05-29 02:49:00 -07002053static uint64_t makeTypeSignature(StringRef Identifier) {
Stephen Hines36b56882014-04-23 16:57:46 -07002054 MD5 Hash;
2055 Hash.update(Identifier);
2056 // ... take the least significant 8 bytes and return those. Our MD5
2057 // implementation always returns its results in little endian, swap bytes
2058 // appropriately.
2059 MD5::MD5Result Result;
2060 Hash.final(Result);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002061 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
2062}
Stephen Hines36b56882014-04-23 16:57:46 -07002063
Stephen Hinesdce4a402014-05-29 02:49:00 -07002064void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
2065 StringRef Identifier, DIE &RefDie,
2066 DICompositeType CTy) {
2067 // Fast path if we're building some type units and one has already used the
2068 // address pool we know we're going to throw away all this work anyway, so
2069 // don't bother building dependent types.
2070 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
2071 return;
Stephen Hines36b56882014-04-23 16:57:46 -07002072
Stephen Hinesdce4a402014-05-29 02:49:00 -07002073 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
2074 if (TU) {
2075 CU.addDIETypeSignature(RefDie, *TU);
2076 return;
2077 }
2078
2079 bool TopLevelType = TypeUnitsUnderConstruction.empty();
2080 AddrPool.resetUsedFlag();
2081
Stephen Hines37ed9c12014-12-01 14:51:49 -08002082 auto OwnedUnit = make_unique<DwarfTypeUnit>(
2083 InfoHolder.getUnits().size() + TypeUnitsUnderConstruction.size(), CU, Asm,
2084 this, &InfoHolder, getDwoLineTable(CU));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002085 DwarfTypeUnit &NewTU = *OwnedUnit;
2086 DIE &UnitDie = NewTU.getUnitDie();
2087 TU = &NewTU;
2088 TypeUnitsUnderConstruction.push_back(
2089 std::make_pair(std::move(OwnedUnit), CTy));
2090
2091 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
2092 CU.getLanguage());
2093
2094 uint64_t Signature = makeTypeSignature(Identifier);
2095 NewTU.setTypeSignature(Signature);
2096
Stephen Hines37ed9c12014-12-01 14:51:49 -08002097 if (useSplitDwarf())
2098 NewTU.initSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
2099 else {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002100 CU.applyStmtList(UnitDie);
Stephen Hines37ed9c12014-12-01 14:51:49 -08002101 NewTU.initSection(
2102 Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2103 }
Stephen Hines36b56882014-04-23 16:57:46 -07002104
Stephen Hinesdce4a402014-05-29 02:49:00 -07002105 NewTU.setType(NewTU.createTypeDIE(CTy));
2106
2107 if (TopLevelType) {
2108 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2109 TypeUnitsUnderConstruction.clear();
2110
2111 // Types referencing entries in the address table cannot be placed in type
2112 // units.
2113 if (AddrPool.hasBeenUsed()) {
2114
2115 // Remove all the types built while building this type.
2116 // This is pessimistic as some of these types might not be dependent on
2117 // the type that used an address.
2118 for (const auto &TU : TypeUnitsToAdd)
2119 DwarfTypeUnits.erase(TU.second);
2120
2121 // Construct this type in the CU directly.
2122 // This is inefficient because all the dependent types will be rebuilt
2123 // from scratch, including building them in type units, discovering that
2124 // they depend on addresses, throwing them out and rebuilding them.
2125 CU.constructTypeDIE(RefDie, CTy);
2126 return;
2127 }
2128
2129 // If the type wasn't dependent on fission addresses, finish adding the type
2130 // and all its dependent types.
Stephen Hines37ed9c12014-12-01 14:51:49 -08002131 for (auto &TU : TypeUnitsToAdd)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002132 InfoHolder.addUnit(std::move(TU.first));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002133 }
2134 CU.addDIETypeSignature(RefDie, NewTU);
Stephen Hines36b56882014-04-23 16:57:46 -07002135}
2136
Stephen Hinesdce4a402014-05-29 02:49:00 -07002137// Accelerator table mutators - add each name along with its companion
2138// DIE to the proper table while ensuring that the name that we're going
2139// to reference is in the string table. We do this since the names we
2140// add may not only be identical to the names in the DIE.
2141void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
2142 if (!useDwarfAccelTables())
2143 return;
2144 AccelNames.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2145 &Die);
2146}
2147
2148void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2149 if (!useDwarfAccelTables())
2150 return;
2151 AccelObjC.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2152 &Die);
2153}
2154
2155void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
2156 if (!useDwarfAccelTables())
2157 return;
2158 AccelNamespace.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2159 &Die);
2160}
2161
2162void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2163 if (!useDwarfAccelTables())
2164 return;
2165 AccelTypes.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2166 &Die);
Stephen Hines36b56882014-04-23 16:57:46 -07002167}