blob: 230ea4651823631335ccda2866af90d9ac41b5e0 [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
16#include "ByteStreamer.h"
17#include "DwarfCompileUnit.h"
Chris Lattner74e41f92010-04-05 05:24:55 +000018#include "DIE.h"
Eric Christopher0d27ca12013-08-08 23:45:55 +000019#include "DIEHash.h"
Stephen Hines36b56882014-04-23 16:57:46 -070020#include "DwarfUnit.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/ADT/STLExtras.h"
22#include "llvm/ADT/Statistic.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/Triple.h"
David 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);
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000493
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000494 // Prime section data.
Richard Mitton5cc319a2013-09-19 23:21:01 +0000495 SectionMap[Asm->getObjFileLowering().getTextSection()];
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000496}
497
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700498void DwarfDebug::finishVariableDefinitions() {
499 for (const auto &Var : ConcreteVariables) {
500 DIE *VariableDie = Var->getDIE();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800501 assert(VariableDie);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700502 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
503 // in the ConcreteVariables list, rather than looking it up again here.
504 // DIE::getUnit isn't simple - it walks parent pointers, etc.
505 DwarfCompileUnit *Unit = lookupUnit(VariableDie->getUnit());
506 assert(Unit);
507 DbgVariable *AbsVar = getExistingAbstractVariable(Var->getVariable());
508 if (AbsVar && AbsVar->getDIE()) {
509 Unit->addDIEEntry(*VariableDie, dwarf::DW_AT_abstract_origin,
510 *AbsVar->getDIE());
511 } else
512 Unit->applyVariableAttributes(*Var, *VariableDie);
513 }
514}
515
Stephen Hinesdce4a402014-05-29 02:49:00 -0700516void DwarfDebug::finishSubprogramDefinitions() {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800517 for (const auto &P : SPMap)
518 forBothCUs(*P.second, [&](DwarfCompileUnit &CU) {
519 CU.finishSubprogramDefinition(DISubprogram(P.first));
520 });
Eric Christopher4117bec2012-11-22 00:59:49 +0000521}
522
Stephen Hinesdce4a402014-05-29 02:49:00 -0700523
Eric Christopher4117bec2012-11-22 00:59:49 +0000524// Collect info for variables that were optimized out.
525void DwarfDebug::collectDeadVariables() {
526 const Module *M = MMI->getModule();
Eric Christopher4117bec2012-11-22 00:59:49 +0000527
528 if (NamedMDNode *CU_Nodes = M->getNamedMetadata("llvm.dbg.cu")) {
Stephen Hines36b56882014-04-23 16:57:46 -0700529 for (MDNode *N : CU_Nodes->operands()) {
530 DICompileUnit TheCU(N);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700531 // Construct subprogram DIE and add variables DIEs.
532 DwarfCompileUnit *SPCU =
533 static_cast<DwarfCompileUnit *>(CUMap.lookup(TheCU));
534 assert(SPCU && "Unable to find Compile Unit!");
Eric Christopher4117bec2012-11-22 00:59:49 +0000535 DIArray Subprograms = TheCU.getSubprograms();
536 for (unsigned i = 0, e = Subprograms.getNumElements(); i != e; ++i) {
Eric Christopherbdab8002012-11-27 00:13:51 +0000537 DISubprogram SP(Subprograms.getElement(i));
Eric Christopherc6228242013-10-15 23:31:38 +0000538 if (ProcessedSPNodes.count(SP) != 0)
539 continue;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800540 SPCU->collectDeadVariables(SP);
Eric Christopher4117bec2012-11-22 00:59:49 +0000541 }
542 }
543 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000544}
545
546void DwarfDebug::finalizeModuleInfo() {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700547 finishSubprogramDefinitions();
548
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700549 finishVariableDefinitions();
550
Eric Christopher4117bec2012-11-22 00:59:49 +0000551 // Collect info for variables that were optimized out.
552 collectDeadVariables();
553
Stephen Hines36b56882014-04-23 16:57:46 -0700554 // Handle anything that needs to be done on a per-unit basis after
555 // all other generation.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800556 for (const auto &P : CUMap) {
557 auto &TheCU = *P.second;
Eric Christopherf962c022013-08-12 20:27:48 +0000558 // Emit DW_AT_containing_type attribute to connect types with their
559 // vtable holding type.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800560 TheCU.constructContainingTypeDIEs();
Eric Christopherf962c022013-08-12 20:27:48 +0000561
Stephen Hines36b56882014-04-23 16:57:46 -0700562 // Add CU specific attributes if we need to add any.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800563 // If we're splitting the dwarf out now that we've got the entire
564 // CU then add the dwo id to it.
565 auto *SkCU = TheCU.getSkeleton();
566 if (useSplitDwarf()) {
567 // Emit a unique identifier for this CU.
568 uint64_t ID = DIEHash(Asm).computeCUSignature(TheCU.getUnitDie());
569 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
570 dwarf::DW_FORM_data8, ID);
571 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
572 dwarf::DW_FORM_data8, ID);
Stephen Hines36b56882014-04-23 16:57:46 -0700573
Stephen Hines37ed9c12014-12-01 14:51:49 -0800574 // We don't keep track of which addresses are used in which CU so this
575 // is a bit pessimistic under LTO.
576 if (!AddrPool.isEmpty())
577 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_addr_base,
578 DwarfAddrSectionSym, DwarfAddrSectionSym);
579 if (!SkCU->getRangeLists().empty())
580 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
581 DwarfDebugRangeSectionSym,
582 DwarfDebugRangeSectionSym);
583 }
Stephen Hines36b56882014-04-23 16:57:46 -0700584
Stephen Hines37ed9c12014-12-01 14:51:49 -0800585 // If we have code split among multiple sections or non-contiguous
586 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
587 // remain in the .o file, otherwise add a DW_AT_low_pc.
588 // FIXME: We should use ranges allow reordering of code ala
589 // .subsections_via_symbols in mach-o. This would mean turning on
590 // ranges for all subprogram DIEs for mach-o.
591 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
592 if (unsigned NumRanges = TheCU.getRanges().size()) {
593 if (NumRanges > 1)
594 // A DW_AT_low_pc attribute may also be specified in combination with
595 // DW_AT_ranges to specify the default base address for use in
596 // location lists (see Section 2.6.2) and range lists (see Section
597 // 2.17.3).
598 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
599 else
600 TheCU.setBaseAddress(TheCU.getRanges().front().getStart());
601 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
Eric Christopherf962c022013-08-12 20:27:48 +0000602 }
603 }
604
605 // Compute DIE offsets and sizes.
Eric Christopher0e3e9b72012-12-10 23:34:43 +0000606 InfoHolder.computeSizeAndOffsets();
607 if (useSplitDwarf())
608 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher4117bec2012-11-22 00:59:49 +0000609}
610
611void DwarfDebug::endSections() {
Stephen Hines36b56882014-04-23 16:57:46 -0700612 // Filter labels by section.
613 for (const SymbolCU &SCU : ArangeLabels) {
Richard Mitton5cc319a2013-09-19 23:21:01 +0000614 if (SCU.Sym->isInSection()) {
615 // Make a note of this symbol and it's section.
616 const MCSection *Section = &SCU.Sym->getSection();
617 if (!Section->getKind().isMetadata())
618 SectionMap[Section].push_back(SCU);
619 } else {
620 // Some symbols (e.g. common/bss on mach-o) can have no section but still
621 // appear in the output. This sucks as we rely on sections to build
622 // arange spans. We can do it without, but it's icky.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700623 SectionMap[nullptr].push_back(SCU);
Richard Mitton5cc319a2013-09-19 23:21:01 +0000624 }
625 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000626
Richard Mitton7c9659a2013-10-03 22:07:08 +0000627 // Build a list of sections used.
628 std::vector<const MCSection *> Sections;
Stephen Hines36b56882014-04-23 16:57:46 -0700629 for (const auto &it : SectionMap) {
630 const MCSection *Section = it.first;
Richard Mitton7c9659a2013-10-03 22:07:08 +0000631 Sections.push_back(Section);
632 }
633
634 // Sort the sections into order.
635 // This is only done to ensure consistent output order across different runs.
636 std::sort(Sections.begin(), Sections.end(), SectionSort);
637
638 // Add terminating symbols for each section.
Stephen Hines36b56882014-04-23 16:57:46 -0700639 for (unsigned ID = 0, E = Sections.size(); ID != E; ID++) {
Richard Mitton7c9659a2013-10-03 22:07:08 +0000640 const MCSection *Section = Sections[ID];
Stephen Hinesdce4a402014-05-29 02:49:00 -0700641 MCSymbol *Sym = nullptr;
Richard Mitton5cc319a2013-09-19 23:21:01 +0000642
643 if (Section) {
Richard Mitton7c9659a2013-10-03 22:07:08 +0000644 // We can't call MCSection::getLabelEndName, as it's only safe to do so
Stephen Hines36b56882014-04-23 16:57:46 -0700645 // if we know the section name up-front. For user-created sections, the
646 // resulting label may not be valid to use as a label. (section names can
647 // use a greater set of characters on some systems)
Richard Mitton7c9659a2013-10-03 22:07:08 +0000648 Sym = Asm->GetTempSymbol("debug_end", ID);
Richard Mitton5cc319a2013-09-19 23:21:01 +0000649 Asm->OutStreamer.SwitchSection(Section);
650 Asm->OutStreamer.EmitLabel(Sym);
651 }
652
653 // Insert a final terminator.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700654 SectionMap[Section].push_back(SymbolCU(nullptr, Sym));
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000655 }
Eric Christopher4117bec2012-11-22 00:59:49 +0000656}
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000657
Eric Christopherb6dc8652012-11-27 22:43:45 +0000658// Emit all Dwarf sections that should come after the content.
Eric Christopher4117bec2012-11-22 00:59:49 +0000659void DwarfDebug::endModule() {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700660 assert(CurFn == nullptr);
661 assert(CurMI == nullptr);
Eric Christopher4117bec2012-11-22 00:59:49 +0000662
Stephen Hines37ed9c12014-12-01 14:51:49 -0800663 // If we aren't actually generating debug info (check beginModule -
664 // conditionalized on !DisableDebugInfoPrinting and the presence of the
665 // llvm.dbg.cu metadata node)
666 if (!DwarfInfoSectionSym)
Stephen Hines36b56882014-04-23 16:57:46 -0700667 return;
Eric Christopher4117bec2012-11-22 00:59:49 +0000668
669 // End any existing sections.
670 // TODO: Does this need to happen?
671 endSections();
672
673 // Finalize the debug info for the module.
674 finalizeModuleInfo();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000675
Stephen Hines36b56882014-04-23 16:57:46 -0700676 emitDebugStr();
Eric Christopher9e0b08d2013-09-20 23:22:52 +0000677
Stephen Hines36b56882014-04-23 16:57:46 -0700678 // Emit all the DIEs into a debug info section.
679 emitDebugInfo();
Eric Christopher74802f72012-11-27 00:41:54 +0000680
Stephen Hines36b56882014-04-23 16:57:46 -0700681 // Corresponding abbreviations into a abbrev section.
682 emitAbbreviations();
Eric Christopher42885022012-11-27 22:43:42 +0000683
Stephen Hines36b56882014-04-23 16:57:46 -0700684 // Emit info into a debug aranges section.
685 if (GenerateARangeSection)
Eric Christopher42885022012-11-27 22:43:42 +0000686 emitDebugARanges();
687
Stephen Hines36b56882014-04-23 16:57:46 -0700688 // Emit info into a debug ranges section.
689 emitDebugRanges();
Eric Christopher42885022012-11-27 22:43:42 +0000690
Stephen Hines36b56882014-04-23 16:57:46 -0700691 if (useSplitDwarf()) {
692 emitDebugStrDWO();
Eric Christopher98e237f2012-11-30 23:59:06 +0000693 emitDebugInfoDWO();
Eric Christopher6eebe472012-12-19 22:02:53 +0000694 emitDebugAbbrevDWO();
Stephen Hines36b56882014-04-23 16:57:46 -0700695 emitDebugLineDWO();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700696 emitDebugLocDWO();
Eric Christopher72f7bfb2013-01-15 23:56:56 +0000697 // Emit DWO addresses.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700698 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
Stephen Hines36b56882014-04-23 16:57:46 -0700699 } else
700 // Emit info into a debug loc section.
701 emitDebugLoc();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000702
Eric Christopher9d9f5a52012-08-23 07:32:06 +0000703 // Emit info into the dwarf accelerator table sections.
Eric Christopher20f47ab2012-08-23 22:36:40 +0000704 if (useDwarfAccelTables()) {
Eric Christopher09ac3d82011-11-07 09:24:32 +0000705 emitAccelNames();
706 emitAccelObjC();
707 emitAccelNamespaces();
708 emitAccelTypes();
709 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +0000710
Eric Christopher26123542013-08-30 00:40:17 +0000711 // Emit the pubnames and pubtypes sections if requested.
712 if (HasDwarfPubSections) {
David Blaikiee56a4d92013-09-19 17:33:35 +0000713 emitDebugPubNames(GenerateGnuPubSections);
714 emitDebugPubTypes(GenerateGnuPubSections);
Eric Christopher26123542013-08-30 00:40:17 +0000715 }
Devang Patel193f7202009-11-24 01:14:22 +0000716
Devang Patele9a1cca2010-08-02 17:32:15 +0000717 // clean up.
Devang Patel94c7ddb2011-08-16 22:09:43 +0000718 SPMap.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700719 AbstractVariables.clear();
Bill Wendlingf0fb9872009-05-20 23:19:06 +0000720}
721
Eric Christopherb6dc8652012-11-27 22:43:45 +0000722// Find abstract variable, if any, associated with Var.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700723DbgVariable *DwarfDebug::getExistingAbstractVariable(const DIVariable &DV,
724 DIVariable &Cleansed) {
Devang Patelb549bcf2011-08-10 21:50:54 +0000725 LLVMContext &Ctx = DV->getContext();
726 // More then one inlined variable corresponds to one abstract variable.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700727 // FIXME: This duplication of variables when inlining should probably be
728 // removed. It's done to allow each DIVariable to describe its location
729 // because the DebugLoc on the dbg.value/declare isn't accurate. We should
730 // make it accurate then remove this duplication/cleansing stuff.
731 Cleansed = cleanseInlinedVariable(DV, Ctx);
732 auto I = AbstractVariables.find(Cleansed);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700733 if (I != AbstractVariables.end())
734 return I->second.get();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700735 return nullptr;
736}
Devang Patel53bb5c92009-11-10 23:06:00 +0000737
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700738DbgVariable *DwarfDebug::getExistingAbstractVariable(const DIVariable &DV) {
739 DIVariable Cleansed;
740 return getExistingAbstractVariable(DV, Cleansed);
741}
Devang Patel53bb5c92009-11-10 23:06:00 +0000742
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700743void DwarfDebug::createAbstractVariable(const DIVariable &Var,
744 LexicalScope *Scope) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800745 auto AbsDbgVariable = make_unique<DbgVariable>(Var, DIExpression(), this);
746 InfoHolder.addScopeVariable(Scope, AbsDbgVariable.get());
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700747 AbstractVariables[Var] = std::move(AbsDbgVariable);
748}
749
750void DwarfDebug::ensureAbstractVariableIsCreated(const DIVariable &DV,
751 const MDNode *ScopeNode) {
752 DIVariable Cleansed = DV;
753 if (getExistingAbstractVariable(DV, Cleansed))
754 return;
755
756 createAbstractVariable(Cleansed, LScopes.getOrCreateAbstractScope(ScopeNode));
757}
758
759void
760DwarfDebug::ensureAbstractVariableIsCreatedIfScoped(const DIVariable &DV,
761 const MDNode *ScopeNode) {
762 DIVariable Cleansed = DV;
763 if (getExistingAbstractVariable(DV, Cleansed))
764 return;
765
766 if (LexicalScope *Scope = LScopes.findAbstractScope(ScopeNode))
767 createAbstractVariable(Cleansed, Scope);
Devang Patel53bb5c92009-11-10 23:06:00 +0000768}
769
Eric Christopherb6dc8652012-11-27 22:43:45 +0000770// Collect variable information from side table maintained by MMI.
Stephen Hines36b56882014-04-23 16:57:46 -0700771void DwarfDebug::collectVariableInfoFromMMITable(
Stephen Hines37ed9c12014-12-01 14:51:49 -0800772 SmallPtrSetImpl<const MDNode *> &Processed) {
Stephen Hines36b56882014-04-23 16:57:46 -0700773 for (const auto &VI : MMI->getVariableDbgInfo()) {
774 if (!VI.Var)
775 continue;
776 Processed.insert(VI.Var);
Stephen Hines36b56882014-04-23 16:57:46 -0700777 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
Jim Grosbach1e20b962010-07-21 21:21:52 +0000778
Devang Patelfb0ee432009-11-10 23:20:04 +0000779 // If variable scope is not found then skip this variable.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700780 if (!Scope)
Devang Patelfb0ee432009-11-10 23:20:04 +0000781 continue;
Devang Patel53bb5c92009-11-10 23:06:00 +0000782
Stephen Hines37ed9c12014-12-01 14:51:49 -0800783 DIVariable DV(VI.Var);
784 DIExpression Expr(VI.Expr);
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700785 ensureAbstractVariableIsCreatedIfScoped(DV, Scope->getScopeNode());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800786 ConcreteVariables.push_back(make_unique<DbgVariable>(DV, Expr, this));
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700787 DbgVariable *RegVar = ConcreteVariables.back().get();
Stephen Hines36b56882014-04-23 16:57:46 -0700788 RegVar->setFrameIndex(VI.Slot);
Stephen Hines37ed9c12014-12-01 14:51:49 -0800789 InfoHolder.addScopeVariable(Scope, RegVar);
Devang Patele717faa2009-10-06 01:26:37 +0000790 }
Devang Patelee432862010-05-20 19:57:06 +0000791}
Devang Patel90a48ad2010-03-15 18:33:46 +0000792
Eric Christopherb6dc8652012-11-27 22:43:45 +0000793// Get .debug_loc entry for the instruction range starting at MI.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700794static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
Stephen Hines37ed9c12014-12-01 14:51:49 -0800795 const MDNode *Expr = MI->getDebugExpression();
Stephen Hinesdce4a402014-05-29 02:49:00 -0700796 const MDNode *Var = MI->getDebugVariable();
Devang Patel90b40412011-07-08 17:09:57 +0000797
Stephen Hines37ed9c12014-12-01 14:51:49 -0800798 assert(MI->getNumOperands() == 4);
Adrian Prantl35176402013-07-09 20:28:37 +0000799 if (MI->getOperand(0).isReg()) {
Devang Patel90b40412011-07-08 17:09:57 +0000800 MachineLocation MLoc;
Adrian Prantl35176402013-07-09 20:28:37 +0000801 // If the second operand is an immediate, this is a
802 // register-indirect address.
803 if (!MI->getOperand(1).isImm())
Adrian Prantl13131e62013-04-26 21:57:17 +0000804 MLoc.set(MI->getOperand(0).getReg());
805 else
806 MLoc.set(MI->getOperand(0).getReg(), MI->getOperand(1).getImm());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800807 return DebugLocEntry::Value(Var, Expr, MLoc);
Devang Patel90b40412011-07-08 17:09:57 +0000808 }
809 if (MI->getOperand(0).isImm())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800810 return DebugLocEntry::Value(Var, Expr, MI->getOperand(0).getImm());
Devang Patel90b40412011-07-08 17:09:57 +0000811 if (MI->getOperand(0).isFPImm())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800812 return DebugLocEntry::Value(Var, Expr, MI->getOperand(0).getFPImm());
Devang Patel90b40412011-07-08 17:09:57 +0000813 if (MI->getOperand(0).isCImm())
Stephen Hines37ed9c12014-12-01 14:51:49 -0800814 return DebugLocEntry::Value(Var, Expr, MI->getOperand(0).getCImm());
Devang Patel90b40412011-07-08 17:09:57 +0000815
Stephen Hines37ed9c12014-12-01 14:51:49 -0800816 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
Devang Patel90b40412011-07-08 17:09:57 +0000817}
818
Stephen Hines37ed9c12014-12-01 14:51:49 -0800819/// Determine whether two variable pieces overlap.
820static bool piecesOverlap(DIExpression P1, DIExpression P2) {
821 if (!P1.isVariablePiece() || !P2.isVariablePiece())
822 return true;
823 unsigned l1 = P1.getPieceOffset();
824 unsigned l2 = P2.getPieceOffset();
825 unsigned r1 = l1 + P1.getPieceSize();
826 unsigned r2 = l2 + P2.getPieceSize();
827 // True where [l1,r1[ and [r1,r2[ overlap.
828 return (l1 < r2) && (l2 < r1);
829}
830
831/// Build the location list for all DBG_VALUEs in the function that
832/// describe the same variable. If the ranges of several independent
833/// pieces of the same variable overlap partially, split them up and
834/// combine the ranges. The resulting DebugLocEntries are will have
835/// strict monotonically increasing begin addresses and will never
836/// overlap.
837//
838// Input:
839//
840// Ranges History [var, loc, piece ofs size]
841// 0 | [x, (reg0, piece 0, 32)]
842// 1 | | [x, (reg1, piece 32, 32)] <- IsPieceOfPrevEntry
843// 2 | | ...
844// 3 | [clobber reg0]
845// 4 [x, (mem, piece 0, 64)] <- overlapping with both previous pieces of x.
846//
847// Output:
848//
849// [0-1] [x, (reg0, piece 0, 32)]
850// [1-3] [x, (reg0, piece 0, 32), (reg1, piece 32, 32)]
851// [3-4] [x, (reg1, piece 32, 32)]
852// [4- ] [x, (mem, piece 0, 64)]
853void
854DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
855 const DbgValueHistoryMap::InstrRanges &Ranges) {
856 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
857
858 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
859 const MachineInstr *Begin = I->first;
860 const MachineInstr *End = I->second;
861 assert(Begin->isDebugValue() && "Invalid History entry");
862
863 // Check if a variable is inaccessible in this range.
864 if (Begin->getNumOperands() > 1 &&
865 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
866 OpenRanges.clear();
867 continue;
868 }
869
870 // If this piece overlaps with any open ranges, truncate them.
871 DIExpression DIExpr = Begin->getDebugExpression();
872 auto Last = std::remove_if(OpenRanges.begin(), OpenRanges.end(),
873 [&](DebugLocEntry::Value R) {
874 return piecesOverlap(DIExpr, R.getExpression());
875 });
876 OpenRanges.erase(Last, OpenRanges.end());
877
878 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
879 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
880
881 const MCSymbol *EndLabel;
882 if (End != nullptr)
883 EndLabel = getLabelAfterInsn(End);
884 else if (std::next(I) == Ranges.end())
885 EndLabel = FunctionEndSym;
886 else
887 EndLabel = getLabelBeforeInsn(std::next(I)->first);
888 assert(EndLabel && "Forgot label after instruction ending a range!");
889
890 DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
891
892 auto Value = getDebugLocValue(Begin);
893 DebugLocEntry Loc(StartLabel, EndLabel, Value);
894 bool couldMerge = false;
895
896 // If this is a piece, it may belong to the current DebugLocEntry.
897 if (DIExpr.isVariablePiece()) {
898 // Add this value to the list of open ranges.
899 OpenRanges.push_back(Value);
900
901 // Attempt to add the piece to the last entry.
902 if (!DebugLoc.empty())
903 if (DebugLoc.back().MergeValues(Loc))
904 couldMerge = true;
905 }
906
907 if (!couldMerge) {
908 // Need to add a new DebugLocEntry. Add all values from still
909 // valid non-overlapping pieces.
910 if (OpenRanges.size())
911 Loc.addValues(OpenRanges);
912
913 DebugLoc.push_back(std::move(Loc));
914 }
915
916 // Attempt to coalesce the ranges of two otherwise identical
917 // DebugLocEntries.
918 auto CurEntry = DebugLoc.rbegin();
919 auto PrevEntry = std::next(CurEntry);
920 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
921 DebugLoc.pop_back();
922
923 DEBUG({
924 dbgs() << CurEntry->getValues().size() << " Values:\n";
925 for (auto Value : CurEntry->getValues()) {
926 Value.getVariable()->dump();
927 Value.getExpression()->dump();
928 }
929 dbgs() << "-----\n";
930 });
931 }
932}
933
934
Eric Christopherb6dc8652012-11-27 22:43:45 +0000935// Find variables for each lexical scope.
Jim Grosbach1e20b962010-07-21 21:21:52 +0000936void
Stephen Hines37ed9c12014-12-01 14:51:49 -0800937DwarfDebug::collectVariableInfo(DwarfCompileUnit &TheCU, DISubprogram SP,
938 SmallPtrSetImpl<const MDNode *> &Processed) {
Eric Christopher4e976452013-07-03 21:37:03 +0000939 // Grab the variable info that was squirreled away in the MMI side-table.
Stephen Hines36b56882014-04-23 16:57:46 -0700940 collectVariableInfoFromMMITable(Processed);
Devang Patelee432862010-05-20 19:57:06 +0000941
Stephen Hinesdce4a402014-05-29 02:49:00 -0700942 for (const auto &I : DbgValues) {
943 DIVariable DV(I.first);
944 if (Processed.count(DV))
Devang Patelee432862010-05-20 19:57:06 +0000945 continue;
946
Stephen Hinesdce4a402014-05-29 02:49:00 -0700947 // Instruction ranges, specifying where DV is accessible.
948 const auto &Ranges = I.second;
949 if (Ranges.empty())
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000950 continue;
Devang Patelc3f5f782010-05-25 23:40:22 +0000951
Stephen Hinesdce4a402014-05-29 02:49:00 -0700952 LexicalScope *Scope = nullptr;
Stephen Hines37ed9c12014-12-01 14:51:49 -0800953 if (MDNode *IA = DV.getInlinedAt()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700954 DebugLoc DL = DebugLoc::getFromDILocation(IA);
955 Scope = LScopes.findInlinedScope(DebugLoc::get(
956 DL.getLine(), DL.getCol(), DV.getContext(), IA));
957 } else
958 Scope = LScopes.findLexicalScope(DV.getContext());
Devang Patelee432862010-05-20 19:57:06 +0000959 // If variable scope is not found then skip this variable.
Devang Patelc0c5a262010-05-21 00:10:20 +0000960 if (!Scope)
Devang Patelee432862010-05-20 19:57:06 +0000961 continue;
962
963 Processed.insert(DV);
Stephen Hinesdce4a402014-05-29 02:49:00 -0700964 const MachineInstr *MInsn = Ranges.front().first;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000965 assert(MInsn->isDebugValue() && "History must begin with debug value");
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700966 ensureAbstractVariableIsCreatedIfScoped(DV, Scope->getScopeNode());
967 ConcreteVariables.push_back(make_unique<DbgVariable>(MInsn, this));
968 DbgVariable *RegVar = ConcreteVariables.back().get();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800969 InfoHolder.addScopeVariable(Scope, RegVar);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000970
Stephen Hinesdce4a402014-05-29 02:49:00 -0700971 // Check if the first DBG_VALUE is valid for the rest of the function.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700972 if (Ranges.size() == 1 && Ranges.front().second == nullptr)
Devang Patelc3f5f782010-05-25 23:40:22 +0000973 continue;
Devang Patelc3f5f782010-05-25 23:40:22 +0000974
Eric Christopher498703b2013-01-28 17:33:26 +0000975 // Handle multiple DBG_VALUE instructions describing one variable.
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +0000976 RegVar->setDotDebugLocOffset(DotDebugLocEntries.size());
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +0000977
Stephen Hines36b56882014-04-23 16:57:46 -0700978 DotDebugLocEntries.resize(DotDebugLocEntries.size() + 1);
979 DebugLocList &LocList = DotDebugLocEntries.back();
Stephen Hines37ed9c12014-12-01 14:51:49 -0800980 LocList.CU = &TheCU;
Stephen Hines36b56882014-04-23 16:57:46 -0700981 LocList.Label =
982 Asm->GetTempSymbol("debug_loc", DotDebugLocEntries.size() - 1);
Jakob Stoklund Olesene17232e2011-03-22 00:21:41 +0000983
Stephen Hines37ed9c12014-12-01 14:51:49 -0800984 // Build the location list for this variable.
985 buildLocationList(LocList.List, Ranges);
Devang Patel90a48ad2010-03-15 18:33:46 +0000986 }
Devang Patel98e1cac2010-05-14 21:01:35 +0000987
988 // Collect info for variables that were optimized out.
Stephen Hines37ed9c12014-12-01 14:51:49 -0800989 DIArray Variables = SP.getVariables();
Devang Patel93d39be2011-08-19 23:28:12 +0000990 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
991 DIVariable DV(Variables.getElement(i));
Stephen Hinesdce4a402014-05-29 02:49:00 -0700992 assert(DV.isVariable());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800993 if (!Processed.insert(DV).second)
Devang Patel93d39be2011-08-19 23:28:12 +0000994 continue;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700995 if (LexicalScope *Scope = LScopes.findLexicalScope(DV.getContext())) {
996 ensureAbstractVariableIsCreatedIfScoped(DV, Scope->getScopeNode());
Stephen Hines37ed9c12014-12-01 14:51:49 -0800997 DIExpression NoExpr;
998 ConcreteVariables.push_back(make_unique<DbgVariable>(DV, NoExpr, this));
999 InfoHolder.addScopeVariable(Scope, ConcreteVariables.back().get());
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001000 }
Devang Patel98e1cac2010-05-14 21:01:35 +00001001 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001002}
Devang Patel98e1cac2010-05-14 21:01:35 +00001003
Eric Christopherb6dc8652012-11-27 22:43:45 +00001004// Return Label preceding the instruction.
Eric Christopher72f7bfb2013-01-15 23:56:56 +00001005MCSymbol *DwarfDebug::getLabelBeforeInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001006 MCSymbol *Label = LabelsBeforeInsn.lookup(MI);
1007 assert(Label && "Didn't insert label before instruction");
1008 return Label;
Devang Patelc3f5f782010-05-25 23:40:22 +00001009}
1010
Eric Christopherb6dc8652012-11-27 22:43:45 +00001011// Return Label immediately following the instruction.
Eric Christopher72f7bfb2013-01-15 23:56:56 +00001012MCSymbol *DwarfDebug::getLabelAfterInsn(const MachineInstr *MI) {
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001013 return LabelsAfterInsn.lookup(MI);
Devang Patele717faa2009-10-06 01:26:37 +00001014}
1015
Eric Christopherb6dc8652012-11-27 22:43:45 +00001016// Process beginning of an instruction.
Devang Patelcbbe2872010-10-26 17:49:02 +00001017void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001018 assert(CurMI == nullptr);
Stephen Hines36b56882014-04-23 16:57:46 -07001019 CurMI = MI;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001020 // Check if source location changes, but ignore DBG_VALUE locations.
1021 if (!MI->isDebugValue()) {
1022 DebugLoc DL = MI->getDebugLoc();
1023 if (DL != PrevInstLoc && (!DL.isUnknown() || UnknownLocations)) {
Eric Christopher60b35f42012-04-05 20:39:05 +00001024 unsigned Flags = 0;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001025 PrevInstLoc = DL;
Devang Patel4243e672011-05-11 19:22:19 +00001026 if (DL == PrologEndLoc) {
1027 Flags |= DWARF2_FLAG_PROLOGUE_END;
1028 PrologEndLoc = DebugLoc();
1029 }
Eric Christopher60b35f42012-04-05 20:39:05 +00001030 if (PrologEndLoc.isUnknown())
1031 Flags |= DWARF2_FLAG_IS_STMT;
1032
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001033 if (!DL.isUnknown()) {
1034 const MDNode *Scope = DL.getScope(Asm->MF->getFunction()->getContext());
Devang Patel4243e672011-05-11 19:22:19 +00001035 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001036 } else
Stephen Hinesdce4a402014-05-29 02:49:00 -07001037 recordSourceLine(0, 0, nullptr, 0);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001038 }
Devang Patelc3f5f782010-05-25 23:40:22 +00001039 }
Devang Patelaead63c2010-03-29 22:59:58 +00001040
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001041 // Insert labels where requested.
Stephen Hines36b56882014-04-23 16:57:46 -07001042 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1043 LabelsBeforeInsn.find(MI);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001044
1045 // No label needed.
1046 if (I == LabelsBeforeInsn.end())
1047 return;
1048
1049 // Label already assigned.
1050 if (I->second)
Devang Patelb2b31a62010-05-26 19:37:24 +00001051 return;
Devang Patel553881b2010-03-29 17:20:31 +00001052
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001053 if (!PrevLabel) {
Devang Patel77051f52010-05-26 21:23:46 +00001054 PrevLabel = MMI->getContext().CreateTempSymbol();
1055 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patelb2b31a62010-05-26 19:37:24 +00001056 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001057 I->second = PrevLabel;
Devang Patel0d20ac82009-10-06 01:50:42 +00001058}
1059
Eric Christopherb6dc8652012-11-27 22:43:45 +00001060// Process end of an instruction.
Stephen Hines36b56882014-04-23 16:57:46 -07001061void DwarfDebug::endInstruction() {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001062 assert(CurMI != nullptr);
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001063 // Don't create a new label after DBG_VALUE instructions.
1064 // They don't generate code.
Stephen Hines36b56882014-04-23 16:57:46 -07001065 if (!CurMI->isDebugValue())
Stephen Hinesdce4a402014-05-29 02:49:00 -07001066 PrevLabel = nullptr;
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001067
Stephen Hines36b56882014-04-23 16:57:46 -07001068 DenseMap<const MachineInstr *, MCSymbol *>::iterator I =
1069 LabelsAfterInsn.find(CurMI);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001070 CurMI = nullptr;
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001071
1072 // No label needed.
1073 if (I == LabelsAfterInsn.end())
1074 return;
1075
1076 // Label already assigned.
1077 if (I->second)
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001078 return;
1079
1080 // We need a label after this instruction.
1081 if (!PrevLabel) {
1082 PrevLabel = MMI->getContext().CreateTempSymbol();
1083 Asm->OutStreamer.EmitLabel(PrevLabel);
Devang Patel1c246352010-04-08 16:50:29 +00001084 }
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001085 I->second = PrevLabel;
Devang Patel53bb5c92009-11-10 23:06:00 +00001086}
1087
Eric Christopherb6dc8652012-11-27 22:43:45 +00001088// Each LexicalScope has first instruction and last instruction to mark
1089// beginning and end of a scope respectively. Create an inverse map that list
1090// scopes starts (and ends) with an instruction. One instruction may start (or
1091// end) multiple scopes. Ignore scopes that are not reachable.
Devang Patele37b0c62010-04-08 18:43:56 +00001092void DwarfDebug::identifyScopeMarkers() {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001093 SmallVector<LexicalScope *, 4> WorkList;
1094 WorkList.push_back(LScopes.getCurrentFunctionScope());
Devang Patel42aafd72010-01-20 02:05:23 +00001095 while (!WorkList.empty()) {
Devang Patelbf47fdb2011-08-10 20:55:27 +00001096 LexicalScope *S = WorkList.pop_back_val();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001097
Craig Topper9f43ac62013-07-03 04:24:43 +00001098 const SmallVectorImpl<LexicalScope *> &Children = S->getChildren();
Jim Grosbach1e20b962010-07-21 21:21:52 +00001099 if (!Children.empty())
Stephen Hines36b56882014-04-23 16:57:46 -07001100 WorkList.append(Children.begin(), Children.end());
Devang Patel42aafd72010-01-20 02:05:23 +00001101
Devang Patel53bb5c92009-11-10 23:06:00 +00001102 if (S->isAbstractScope())
1103 continue;
Jim Grosbach1e20b962010-07-21 21:21:52 +00001104
Stephen Hines36b56882014-04-23 16:57:46 -07001105 for (const InsnRange &R : S->getRanges()) {
1106 assert(R.first && "InsnRange does not have first instruction!");
1107 assert(R.second && "InsnRange does not have second instruction!");
1108 requestLabelBeforeInsn(R.first);
1109 requestLabelAfterInsn(R.second);
Devang Pateleac9c072010-04-27 19:46:33 +00001110 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001111 }
Devang Patelaf9e8472009-10-01 20:31:14 +00001112}
1113
Stephen Hinesdce4a402014-05-29 02:49:00 -07001114static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1115 // First known non-DBG_VALUE and non-frame setup location marks
1116 // the beginning of the function body.
1117 for (const auto &MBB : *MF)
1118 for (const auto &MI : MBB)
1119 if (!MI.isDebugValue() && !MI.getFlag(MachineInstr::FrameSetup) &&
1120 !MI.getDebugLoc().isUnknown())
1121 return MI.getDebugLoc();
1122 return DebugLoc();
1123}
1124
Eric Christopherb6dc8652012-11-27 22:43:45 +00001125// Gather pre-function debug information. Assumes being called immediately
1126// after the function entry point has been emitted.
Chris Lattnereec791a2010-01-26 23:18:02 +00001127void DwarfDebug::beginFunction(const MachineFunction *MF) {
Stephen Hines36b56882014-04-23 16:57:46 -07001128 CurFn = MF;
Eric Christopher31ed50c2013-11-01 23:14:17 +00001129
1130 // If there's no debug info for the function we're not going to do anything.
1131 if (!MMI->hasDebugInfo())
1132 return;
1133
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001134 auto DI = FunctionDIs.find(MF->getFunction());
1135 if (DI == FunctionDIs.end())
1136 return;
1137
Eric Christopher31ed50c2013-11-01 23:14:17 +00001138 // Grab the lexical scopes for the function, if we don't have any of those
1139 // then we're not going to be able to do anything.
Devang Patelbf47fdb2011-08-10 20:55:27 +00001140 LScopes.initialize(*MF);
Eric Christopher31ed50c2013-11-01 23:14:17 +00001141 if (LScopes.empty())
1142 return;
1143
Stephen Hinesdce4a402014-05-29 02:49:00 -07001144 assert(DbgValues.empty() && "DbgValues map wasn't cleaned!");
Eric Christopher31ed50c2013-11-01 23:14:17 +00001145
1146 // Make sure that each lexical scope will have a begin/end label.
Devang Patelbf47fdb2011-08-10 20:55:27 +00001147 identifyScopeMarkers();
Devang Patel60b35bd2009-10-06 18:37:31 +00001148
Stephen Hines36b56882014-04-23 16:57:46 -07001149 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
Eric Christopher31ed50c2013-11-01 23:14:17 +00001150 // belongs to so that we add to the correct per-cu line table in the
1151 // non-asm case.
Manman Ren43213cf2013-02-05 21:52:47 +00001152 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001153 // FnScope->getScopeNode() and DI->second should represent the same function,
1154 // though they may not be the same MDNode due to inline functions merged in
1155 // LTO where the debug info metadata still differs (either due to distinct
1156 // written differences - two versions of a linkonce_odr function
1157 // written/copied into two separate files, or some sub-optimal metadata that
1158 // isn't structurally identical (see: file path/name info from clang, which
1159 // includes the directory of the cpp file being built, even when the file name
1160 // is absolute (such as an <> lookup header)))
Stephen Hines36b56882014-04-23 16:57:46 -07001161 DwarfCompileUnit *TheCU = SPMap.lookup(FnScope->getScopeNode());
Manman Ren43213cf2013-02-05 21:52:47 +00001162 assert(TheCU && "Unable to find compile unit!");
Stephen Hines36b56882014-04-23 16:57:46 -07001163 if (Asm->OutStreamer.hasRawTextSupport())
1164 // Use a single line table if we are generating assembly.
Manman Ren01cb18e2013-05-21 00:57:22 +00001165 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
1166 else
1167 Asm->OutStreamer.getContext().setDwarfCompileUnitID(TheCU->getUniqueID());
Manman Ren43213cf2013-02-05 21:52:47 +00001168
Eric Christopher31ed50c2013-11-01 23:14:17 +00001169 // Emit a label for the function so that we have a beginning address.
1170 FunctionBeginSym = Asm->GetTempSymbol("func_begin", Asm->getFunctionNumber());
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001171 // Assumes in correct section after the entry point.
Devang Pateleac9c072010-04-27 19:46:33 +00001172 Asm->OutStreamer.EmitLabel(FunctionBeginSym);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001173
Stephen Hinesdce4a402014-05-29 02:49:00 -07001174 // Calculate history for local variables.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001175 calculateDbgValueHistory(MF, Asm->TM.getSubtargetImpl()->getRegisterInfo(),
1176 DbgValues);
Jakob Stoklund Olesen28cf1152011-03-22 22:33:08 +00001177
Stephen Hinesdce4a402014-05-29 02:49:00 -07001178 // Request labels for the full history.
1179 for (const auto &I : DbgValues) {
1180 const auto &Ranges = I.second;
1181 if (Ranges.empty())
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001182 continue;
1183
Stephen Hinesdce4a402014-05-29 02:49:00 -07001184 // The first mention of a function argument gets the FunctionBeginSym
1185 // label, so arguments are visible when breaking at function entry.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001186 DIVariable DIVar(Ranges.front().first->getDebugVariable());
1187 if (DIVar.isVariable() && DIVar.getTag() == dwarf::DW_TAG_arg_variable &&
1188 getDISubprogram(DIVar.getContext()).describes(MF->getFunction())) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001189 LabelsBeforeInsn[Ranges.front().first] = FunctionBeginSym;
Stephen Hines37ed9c12014-12-01 14:51:49 -08001190 if (Ranges.front().first->getDebugExpression().isVariablePiece()) {
1191 // Mark all non-overlapping initial pieces.
1192 for (auto I = Ranges.begin(); I != Ranges.end(); ++I) {
1193 DIExpression Piece = I->first->getDebugExpression();
1194 if (std::all_of(Ranges.begin(), I,
1195 [&](DbgValueHistoryMap::InstrRange Pred) {
1196 return !piecesOverlap(Piece, Pred.first->getDebugExpression());
1197 }))
1198 LabelsBeforeInsn[I->first] = FunctionBeginSym;
1199 else
1200 break;
1201 }
1202 }
1203 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001204
1205 for (const auto &Range : Ranges) {
1206 requestLabelBeforeInsn(Range.first);
1207 if (Range.second)
1208 requestLabelAfterInsn(Range.second);
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001209 }
1210 }
Devang Patelb2b31a62010-05-26 19:37:24 +00001211
Jakob Stoklund Olesen15a3ea02011-03-25 17:20:59 +00001212 PrevInstLoc = DebugLoc();
Devang Patelb2b31a62010-05-26 19:37:24 +00001213 PrevLabel = FunctionBeginSym;
Devang Patel4243e672011-05-11 19:22:19 +00001214
1215 // Record beginning of function.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001216 PrologEndLoc = findPrologueEndLoc(MF);
Devang Patel4243e672011-05-11 19:22:19 +00001217 if (!PrologEndLoc.isUnknown()) {
Eric Christopher31ed50c2013-11-01 23:14:17 +00001218 DebugLoc FnStartDL =
Stephen Hines36b56882014-04-23 16:57:46 -07001219 PrologEndLoc.getFnDebugLoc(MF->getFunction()->getContext());
Eric Christopher31ed50c2013-11-01 23:14:17 +00001220 recordSourceLine(
1221 FnStartDL.getLine(), FnStartDL.getCol(),
1222 FnStartDL.getScope(MF->getFunction()->getContext()),
1223 // We'd like to list the prologue as "not statements" but GDB behaves
1224 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
1225 DWARF2_FLAG_IS_STMT);
Devang Patel4243e672011-05-11 19:22:19 +00001226 }
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001227}
1228
Eric Christopherb6dc8652012-11-27 22:43:45 +00001229// Gather and emit post-function debug information.
Chris Lattnereec791a2010-01-26 23:18:02 +00001230void DwarfDebug::endFunction(const MachineFunction *MF) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001231 assert(CurFn == MF &&
1232 "endFunction should be called with the same function as beginFunction");
Stephen Hines36b56882014-04-23 16:57:46 -07001233
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001234 if (!MMI->hasDebugInfo() || LScopes.empty() ||
1235 !FunctionDIs.count(MF->getFunction())) {
Stephen Hines36b56882014-04-23 16:57:46 -07001236 // If we don't have a lexical scope for this function then there will
1237 // be a hole in the range information. Keep note of this by setting the
1238 // previously used section to nullptr.
Stephen Hines36b56882014-04-23 16:57:46 -07001239 PrevCU = nullptr;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001240 CurFn = nullptr;
Stephen Hines36b56882014-04-23 16:57:46 -07001241 return;
1242 }
Devang Patel70d75ca2009-11-12 19:02:56 +00001243
Devang Patelbf47fdb2011-08-10 20:55:27 +00001244 // Define end label for subprogram.
Stephen Hines36b56882014-04-23 16:57:46 -07001245 FunctionEndSym = Asm->GetTempSymbol("func_end", Asm->getFunctionNumber());
Devang Patelbf47fdb2011-08-10 20:55:27 +00001246 // Assumes in correct section after the entry point.
1247 Asm->OutStreamer.EmitLabel(FunctionEndSym);
Stephen Hines36b56882014-04-23 16:57:46 -07001248
1249 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Manman Ren43213cf2013-02-05 21:52:47 +00001250 Asm->OutStreamer.getContext().setDwarfCompileUnitID(0);
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001251
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001252 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001253 DISubprogram SP(FnScope->getScopeNode());
1254 DwarfCompileUnit &TheCU = *SPMap.lookup(SP);
Devang Pateld0b5a5e2011-08-15 22:04:40 +00001255
Stephen Hines37ed9c12014-12-01 14:51:49 -08001256 SmallPtrSet<const MDNode *, 16> ProcessedVars;
1257 collectVariableInfo(TheCU, SP, ProcessedVars);
1258
1259 // Add the range of this function to the list of ranges for the CU.
1260 TheCU.addRange(RangeSpan(FunctionBeginSym, FunctionEndSym));
1261
1262 // Under -gmlt, skip building the subprogram if there are no inlined
1263 // subroutines inside it.
1264 if (TheCU.getCUNode().getEmissionKind() == DIBuilder::LineTablesOnly &&
1265 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
1266 assert(InfoHolder.getScopeVariables().empty());
1267 assert(DbgValues.empty());
1268 // FIXME: This wouldn't be true in LTO with a -g (with inlining) CU followed
1269 // by a -gmlt CU. Add a test and remove this assertion.
1270 assert(AbstractVariables.empty());
1271 LabelsBeforeInsn.clear();
1272 LabelsAfterInsn.clear();
1273 PrevLabel = nullptr;
1274 CurFn = nullptr;
1275 return;
1276 }
1277
1278#ifndef NDEBUG
1279 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1280#endif
Devang Patelbf47fdb2011-08-10 20:55:27 +00001281 // Construct abstract scopes.
Stephen Hines36b56882014-04-23 16:57:46 -07001282 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Devang Patelcd9f6c52011-08-12 18:10:19 +00001283 DISubprogram SP(AScope->getScopeNode());
Stephen Hines37ed9c12014-12-01 14:51:49 -08001284 assert(SP.isSubprogram());
Stephen Hinesdce4a402014-05-29 02:49:00 -07001285 // Collect info for variables that were optimized out.
1286 DIArray Variables = SP.getVariables();
1287 for (unsigned i = 0, e = Variables.getNumElements(); i != e; ++i) {
1288 DIVariable DV(Variables.getElement(i));
1289 assert(DV && DV.isVariable());
Stephen Hines37ed9c12014-12-01 14:51:49 -08001290 if (!ProcessedVars.insert(DV).second)
Stephen Hinesdce4a402014-05-29 02:49:00 -07001291 continue;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -07001292 ensureAbstractVariableIsCreated(DV, DV.getContext());
Stephen Hines37ed9c12014-12-01 14:51:49 -08001293 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1294 && "ensureAbstractVariableIsCreated inserted abstract scopes");
Devang Patel78e127d2010-06-25 22:07:34 +00001295 }
Stephen Hines37ed9c12014-12-01 14:51:49 -08001296 constructAbstractSubprogramScopeDIE(AScope);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001297 }
Eric Christopher0f1c7f62012-11-19 22:42:10 +00001298
Stephen Hines37ed9c12014-12-01 14:51:49 -08001299 TheCU.constructSubprogramScopeDIE(FnScope);
1300 if (auto *SkelCU = TheCU.getSkeleton())
1301 if (!LScopes.getAbstractScopesList().empty())
1302 SkelCU->constructSubprogramScopeDIE(FnScope);
Stephen Hines36b56882014-04-23 16:57:46 -07001303
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001304 // Clear debug info
Stephen Hinesdce4a402014-05-29 02:49:00 -07001305 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1306 // DbgVariables except those that are also in AbstractVariables (since they
1307 // can be used cross-function)
Stephen Hines37ed9c12014-12-01 14:51:49 -08001308 InfoHolder.getScopeVariables().clear();
Jakob Stoklund Olesenadb877d2011-03-26 02:19:36 +00001309 DbgValues.clear();
Devang Pateleac9c072010-04-27 19:46:33 +00001310 LabelsBeforeInsn.clear();
1311 LabelsAfterInsn.clear();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001312 PrevLabel = nullptr;
1313 CurFn = nullptr;
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001314}
1315
Eric Christopherb6dc8652012-11-27 22:43:45 +00001316// Register a source line with debug info. Returns the unique label that was
1317// emitted and which provides correspondence to the source line list.
Devang Patel4243e672011-05-11 19:22:19 +00001318void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1319 unsigned Flags) {
Devang Patel65dbc902009-11-25 17:36:49 +00001320 StringRef Fn;
Devang Patel23670e52011-03-24 20:30:50 +00001321 StringRef Dir;
Dan Gohman1cc0d622010-05-05 23:41:32 +00001322 unsigned Src = 1;
Stephen Hines36b56882014-04-23 16:57:46 -07001323 unsigned Discriminator = 0;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001324 if (DIScope Scope = DIScope(S)) {
1325 assert(Scope.isScope());
1326 Fn = Scope.getFilename();
1327 Dir = Scope.getDirectory();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001328 if (Scope.isLexicalBlockFile())
1329 Discriminator = DILexicalBlockFile(S).getDiscriminator();
Dan Gohman1cc0d622010-05-05 23:41:32 +00001330
Stephen Hines36b56882014-04-23 16:57:46 -07001331 unsigned CUID = Asm->OutStreamer.getContext().getDwarfCompileUnitID();
Stephen Hinesdce4a402014-05-29 02:49:00 -07001332 Src = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
1333 .getOrCreateSourceID(Fn, Dir);
Dan Gohman1cc0d622010-05-05 23:41:32 +00001334 }
Stephen Hines36b56882014-04-23 16:57:46 -07001335 Asm->OutStreamer.EmitDwarfLocDirective(Src, Line, Col, Flags, 0,
1336 Discriminator, Fn);
Bill Wendlingf0fb9872009-05-20 23:19:06 +00001337}
1338
Bill Wendling829e67b2009-05-20 23:22:40 +00001339//===----------------------------------------------------------------------===//
1340// Emit Methods
1341//===----------------------------------------------------------------------===//
1342
Eric Christopherb6dc8652012-11-27 22:43:45 +00001343// Emit initial Dwarf sections with a label at the start of each one.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001344void DwarfDebug::emitSectionLabels() {
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001345 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001346
Bill Wendling94d04b82009-05-20 23:21:38 +00001347 // Dwarf sections base addresses.
Jim Grosbach1e20b962010-07-21 21:21:52 +00001348 DwarfInfoSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001349 emitSectionSym(Asm, TLOF.getDwarfInfoSection(), "section_info");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001350 if (useSplitDwarf()) {
Stephen Hines36b56882014-04-23 16:57:46 -07001351 DwarfInfoDWOSectionSym =
1352 emitSectionSym(Asm, TLOF.getDwarfInfoDWOSection(), "section_info_dwo");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001353 DwarfTypesDWOSectionSym =
1354 emitSectionSym(Asm, TLOF.getDwarfTypesDWOSection(), "section_types_dwo");
1355 }
Stephen Hines36b56882014-04-23 16:57:46 -07001356 DwarfAbbrevSectionSym =
1357 emitSectionSym(Asm, TLOF.getDwarfAbbrevSection(), "section_abbrev");
1358 if (useSplitDwarf())
1359 DwarfAbbrevDWOSectionSym = emitSectionSym(
1360 Asm, TLOF.getDwarfAbbrevDWOSection(), "section_abbrev_dwo");
1361 if (GenerateARangeSection)
1362 emitSectionSym(Asm, TLOF.getDwarfARangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001363
Eric Christopher7a0103c2013-02-07 21:19:50 +00001364 DwarfLineSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001365 emitSectionSym(Asm, TLOF.getDwarfLineSection(), "section_line");
Eric Christophercdeaae42013-09-23 20:55:35 +00001366 if (GenerateGnuPubSections) {
Eric Christophera6d84152013-09-30 23:14:16 +00001367 DwarfGnuPubNamesSectionSym =
1368 emitSectionSym(Asm, TLOF.getDwarfGnuPubNamesSection());
1369 DwarfGnuPubTypesSectionSym =
1370 emitSectionSym(Asm, TLOF.getDwarfGnuPubTypesSection());
Eric Christophercdeaae42013-09-23 20:55:35 +00001371 } else if (HasDwarfPubSections) {
1372 emitSectionSym(Asm, TLOF.getDwarfPubNamesSection());
1373 emitSectionSym(Asm, TLOF.getDwarfPubTypesSection());
Michael Gottesmandc42d032013-09-04 04:39:38 +00001374 }
Eric Christopher8f1a9292013-09-13 00:35:05 +00001375
Jim Grosbach1e20b962010-07-21 21:21:52 +00001376 DwarfStrSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001377 emitSectionSym(Asm, TLOF.getDwarfStrSection(), "info_string");
Eric Christopher9a9e73b2013-04-07 03:43:09 +00001378 if (useSplitDwarf()) {
Eric Christopher64f824c2012-12-27 02:14:01 +00001379 DwarfStrDWOSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001380 emitSectionSym(Asm, TLOF.getDwarfStrDWOSection(), "skel_string");
Eric Christopher9a9e73b2013-04-07 03:43:09 +00001381 DwarfAddrSectionSym =
Stephen Hines36b56882014-04-23 16:57:46 -07001382 emitSectionSym(Asm, TLOF.getDwarfAddrSection(), "addr_sec");
1383 DwarfDebugLocSectionSym =
1384 emitSectionSym(Asm, TLOF.getDwarfLocDWOSection(), "skel_loc");
1385 } else
1386 DwarfDebugLocSectionSym =
1387 emitSectionSym(Asm, TLOF.getDwarfLocSection(), "section_debug_loc");
1388 DwarfDebugRangeSectionSym =
1389 emitSectionSym(Asm, TLOF.getDwarfRangesSection(), "debug_range");
Bill Wendling94d04b82009-05-20 23:21:38 +00001390}
1391
Eric Christopherb6dc8652012-11-27 22:43:45 +00001392// Recursively emits a debug information entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001393void DwarfDebug::emitDIE(DIE &Die) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001394 // Get the abbreviation for this DIE.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001395 const DIEAbbrev &Abbrev = Die.getAbbrev();
Bill Wendling94d04b82009-05-20 23:21:38 +00001396
Bill Wendling94d04b82009-05-20 23:21:38 +00001397 // Emit the code (index) for the abbreviation.
Chris Lattner3f53c832010-04-04 18:52:31 +00001398 if (Asm->isVerbose())
Stephen Hines36b56882014-04-23 16:57:46 -07001399 Asm->OutStreamer.AddComment("Abbrev [" + Twine(Abbrev.getNumber()) +
Stephen Hinesdce4a402014-05-29 02:49:00 -07001400 "] 0x" + Twine::utohexstr(Die.getOffset()) +
1401 ":0x" + Twine::utohexstr(Die.getSize()) + " " +
Stephen Hines36b56882014-04-23 16:57:46 -07001402 dwarf::TagString(Abbrev.getTag()));
1403 Asm->EmitULEB128(Abbrev.getNumber());
Bill Wendling94d04b82009-05-20 23:21:38 +00001404
Stephen Hinesdce4a402014-05-29 02:49:00 -07001405 const SmallVectorImpl<DIEValue *> &Values = Die.getValues();
Stephen Hines36b56882014-04-23 16:57:46 -07001406 const SmallVectorImpl<DIEAbbrevData> &AbbrevData = Abbrev.getData();
Bill Wendling94d04b82009-05-20 23:21:38 +00001407
1408 // Emit the DIE attribute values.
1409 for (unsigned i = 0, N = Values.size(); i < N; ++i) {
David Blaikie770530b2013-10-21 17:28:37 +00001410 dwarf::Attribute Attr = AbbrevData[i].getAttribute();
1411 dwarf::Form Form = AbbrevData[i].getForm();
Bill Wendling94d04b82009-05-20 23:21:38 +00001412 assert(Form && "Too many attributes for DIE (check abbreviation)");
1413
Stephen Hines36b56882014-04-23 16:57:46 -07001414 if (Asm->isVerbose()) {
Chris Lattnera8013622010-01-24 18:54:17 +00001415 Asm->OutStreamer.AddComment(dwarf::AttributeString(Attr));
Stephen Hines36b56882014-04-23 16:57:46 -07001416 if (Attr == dwarf::DW_AT_accessibility)
1417 Asm->OutStreamer.AddComment(dwarf::AccessibilityString(
1418 cast<DIEInteger>(Values[i])->getValue()));
1419 }
Jim Grosbach1e20b962010-07-21 21:21:52 +00001420
Stephen Hines36b56882014-04-23 16:57:46 -07001421 // Emit an attribute using the defined form.
1422 Values[i]->EmitValue(Asm, Form);
Bill Wendling94d04b82009-05-20 23:21:38 +00001423 }
1424
1425 // Emit the DIE children if any.
Stephen Hines36b56882014-04-23 16:57:46 -07001426 if (Abbrev.hasChildren()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001427 for (auto &Child : Die.getChildren())
1428 emitDIE(*Child);
Bill Wendling94d04b82009-05-20 23:21:38 +00001429
Stephen Hines36b56882014-04-23 16:57:46 -07001430 Asm->OutStreamer.AddComment("End Of Children Mark");
Chris Lattner233f52b2010-03-09 23:52:58 +00001431 Asm->EmitInt8(0);
Bill Wendling94d04b82009-05-20 23:21:38 +00001432 }
1433}
1434
Eric Christopher98e237f2012-11-30 23:59:06 +00001435// Emit the debug info section.
1436void DwarfDebug::emitDebugInfo() {
Stephen Hines36b56882014-04-23 16:57:46 -07001437 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopherb1e66d02012-12-15 00:04:07 +00001438
Stephen Hines37ed9c12014-12-01 14:51:49 -08001439 Holder.emitUnits(DwarfAbbrevSectionSym);
Eric Christopher98e237f2012-11-30 23:59:06 +00001440}
1441
Eric Christopherb6dc8652012-11-27 22:43:45 +00001442// Emit the abbreviation section.
Eric Christopher7dc68db2012-11-20 23:30:11 +00001443void DwarfDebug::emitAbbreviations() {
Stephen Hines36b56882014-04-23 16:57:46 -07001444 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
1445
1446 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher6eebe472012-12-19 22:02:53 +00001447}
Bill Wendling94d04b82009-05-20 23:21:38 +00001448
Eric Christopherb6dc8652012-11-27 22:43:45 +00001449// Emit the last address of the section and the end of the line matrix.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001450void DwarfDebug::emitEndOfLineMatrix(unsigned SectionEnd) {
Bill Wendling94d04b82009-05-20 23:21:38 +00001451 // Define last address of section.
Chris Lattner233f52b2010-03-09 23:52:58 +00001452 Asm->OutStreamer.AddComment("Extended Op");
1453 Asm->EmitInt8(0);
Jim Grosbach1e20b962010-07-21 21:21:52 +00001454
Chris Lattner233f52b2010-03-09 23:52:58 +00001455 Asm->OutStreamer.AddComment("Op size");
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001456 Asm->EmitInt8(Asm->getDataLayout().getPointerSize() + 1);
Chris Lattner233f52b2010-03-09 23:52:58 +00001457 Asm->OutStreamer.AddComment("DW_LNE_set_address");
1458 Asm->EmitInt8(dwarf::DW_LNE_set_address);
1459
1460 Asm->OutStreamer.AddComment("Section end label");
Chris Lattnerd85fc6e2010-03-10 01:17:49 +00001461
Stephen Hines36b56882014-04-23 16:57:46 -07001462 Asm->OutStreamer.EmitSymbolValue(
1463 Asm->GetTempSymbol("section_end", SectionEnd),
1464 Asm->getDataLayout().getPointerSize());
Bill Wendling94d04b82009-05-20 23:21:38 +00001465
1466 // Mark end of matrix.
Chris Lattner233f52b2010-03-09 23:52:58 +00001467 Asm->OutStreamer.AddComment("DW_LNE_end_sequence");
1468 Asm->EmitInt8(0);
Chris Lattner0ad9c912010-01-22 22:09:00 +00001469 Asm->EmitInt8(1);
Chris Lattner894d75a2010-01-22 23:18:42 +00001470 Asm->EmitInt8(1);
Bill Wendling94d04b82009-05-20 23:21:38 +00001471}
1472
Stephen Hines37ed9c12014-12-01 14:51:49 -08001473void DwarfDebug::emitAccel(DwarfAccelTable &Accel, const MCSection *Section,
1474 StringRef TableName, StringRef SymName) {
1475 Accel.FinalizeTable(Asm, TableName);
1476 Asm->OutStreamer.SwitchSection(Section);
1477 auto *SectionBegin = Asm->GetTempSymbol(SymName);
Eric Christopher09ac3d82011-11-07 09:24:32 +00001478 Asm->OutStreamer.EmitLabel(SectionBegin);
1479
1480 // Emit the full data.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001481 Accel.Emit(Asm, SectionBegin, this, DwarfStrSectionSym);
1482}
1483
1484// Emit visible names into a hashed accelerator table section.
1485void DwarfDebug::emitAccelNames() {
1486 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
1487 "Names", "names_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001488}
1489
Eric Christopher72c16552012-12-20 21:58:40 +00001490// Emit objective C classes and categories into a hashed accelerator table
1491// section.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001492void DwarfDebug::emitAccelObjC() {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001493 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
1494 "ObjC", "objc_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001495}
1496
Eric Christopherb6dc8652012-11-27 22:43:45 +00001497// Emit namespace dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001498void DwarfDebug::emitAccelNamespaces() {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001499 emitAccel(AccelNamespace,
1500 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
1501 "namespac", "namespac_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001502}
1503
Eric Christopherb6dc8652012-11-27 22:43:45 +00001504// Emit type dies into a hashed accelerator table.
Eric Christopher09ac3d82011-11-07 09:24:32 +00001505void DwarfDebug::emitAccelTypes() {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001506 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
1507 "types", "types_begin");
Eric Christopher09ac3d82011-11-07 09:24:32 +00001508}
1509
Eric Christopher8f1a9292013-09-13 00:35:05 +00001510// Public name handling.
1511// The format for the various pubnames:
1512//
1513// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1514// for the DIE that is named.
1515//
1516// gnu pubnames - offset/index value/name tuples where the offset is the offset
1517// into the CU and the index value is computed according to the type of value
1518// for the DIE that is named.
1519//
1520// For type units the offset is the offset of the skeleton DIE. For split dwarf
1521// it's the offset within the debug_info/debug_types dwo section, however, the
1522// reference in the pubname header doesn't change.
1523
1524/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Stephen Hines36b56882014-04-23 16:57:46 -07001525static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
1526 const DIE *Die) {
Eric Christophera486f552013-10-16 01:37:49 +00001527 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1528
1529 // We could have a specification DIE that has our most of our knowledge,
1530 // look for that now.
1531 DIEValue *SpecVal = Die->findAttribute(dwarf::DW_AT_specification);
1532 if (SpecVal) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001533 DIE &SpecDIE = cast<DIEEntry>(SpecVal)->getEntry();
1534 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
Eric Christophera486f552013-10-16 01:37:49 +00001535 Linkage = dwarf::GIEL_EXTERNAL;
1536 } else if (Die->findAttribute(dwarf::DW_AT_external))
1537 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopher8f1a9292013-09-13 00:35:05 +00001538
1539 switch (Die->getTag()) {
1540 case dwarf::DW_TAG_class_type:
1541 case dwarf::DW_TAG_structure_type:
1542 case dwarf::DW_TAG_union_type:
1543 case dwarf::DW_TAG_enumeration_type:
Eric Christophercdeaae42013-09-23 20:55:35 +00001544 return dwarf::PubIndexEntryDescriptor(
1545 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1546 ? dwarf::GIEL_STATIC
1547 : dwarf::GIEL_EXTERNAL);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001548 case dwarf::DW_TAG_typedef:
1549 case dwarf::DW_TAG_base_type:
1550 case dwarf::DW_TAG_subrange_type:
David Blaikie9599f512013-09-19 20:40:26 +00001551 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001552 case dwarf::DW_TAG_namespace:
David Blaikie9599f512013-09-19 20:40:26 +00001553 return dwarf::GIEK_TYPE;
Eric Christopher8f1a9292013-09-13 00:35:05 +00001554 case dwarf::DW_TAG_subprogram:
Eric Christophera31a9752013-09-23 22:59:14 +00001555 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001556 case dwarf::DW_TAG_constant:
1557 case dwarf::DW_TAG_variable:
Eric Christophera31a9752013-09-23 22:59:14 +00001558 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001559 case dwarf::DW_TAG_enumerator:
David Blaikie9599f512013-09-19 20:40:26 +00001560 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1561 dwarf::GIEL_STATIC);
Eric Christopher8f1a9292013-09-13 00:35:05 +00001562 default:
David Blaikie9599f512013-09-19 20:40:26 +00001563 return dwarf::GIEK_NONE;
Eric Christopher8f1a9292013-09-13 00:35:05 +00001564 }
Eric Christopher8f1a9292013-09-13 00:35:05 +00001565}
1566
Eric Christopher57479322013-09-09 20:03:17 +00001567/// emitDebugPubNames - Emit visible names into a debug pubnames section.
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001568///
Eric Christopher8f1a9292013-09-13 00:35:05 +00001569void DwarfDebug::emitDebugPubNames(bool GnuStyle) {
Eric Christopher8f1a9292013-09-13 00:35:05 +00001570 const MCSection *PSec =
1571 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1572 : Asm->getObjFileLowering().getDwarfPubNamesSection();
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001573
Stephen Hines37ed9c12014-12-01 14:51:49 -08001574 emitDebugPubSection(GnuStyle, PSec, "Names",
1575 &DwarfCompileUnit::getGlobalNames);
Stephen Hines36b56882014-04-23 16:57:46 -07001576}
1577
1578void DwarfDebug::emitDebugPubSection(
1579 bool GnuStyle, const MCSection *PSec, StringRef Name,
Stephen Hines37ed9c12014-12-01 14:51:49 -08001580 const StringMap<const DIE *> &(DwarfCompileUnit::*Accessor)() const) {
Stephen Hines36b56882014-04-23 16:57:46 -07001581 for (const auto &NU : CUMap) {
1582 DwarfCompileUnit *TheU = NU.second;
1583
1584 const auto &Globals = (TheU->*Accessor)();
1585
1586 if (Globals.empty())
1587 continue;
1588
Stephen Hines37ed9c12014-12-01 14:51:49 -08001589 if (auto *Skeleton = TheU->getSkeleton())
Stephen Hines36b56882014-04-23 16:57:46 -07001590 TheU = Skeleton;
1591 unsigned ID = TheU->getUniqueID();
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001592
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001593 // Start the dwarf pubnames section.
Eric Christopher21a6a502013-09-10 21:49:37 +00001594 Asm->OutStreamer.SwitchSection(PSec);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001595
Eric Christopher8f1a9292013-09-13 00:35:05 +00001596 // Emit the header.
Stephen Hines36b56882014-04-23 16:57:46 -07001597 Asm->OutStreamer.AddComment("Length of Public " + Name + " Info");
1598 MCSymbol *BeginLabel = Asm->GetTempSymbol("pub" + Name + "_begin", ID);
1599 MCSymbol *EndLabel = Asm->GetTempSymbol("pub" + Name + "_end", ID);
1600 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001601
Stephen Hines36b56882014-04-23 16:57:46 -07001602 Asm->OutStreamer.EmitLabel(BeginLabel);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001603
1604 Asm->OutStreamer.AddComment("DWARF Version");
David Majnemerd7d43dc2013-08-21 06:13:34 +00001605 Asm->EmitInt16(dwarf::DW_PUBNAMES_VERSION);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001606
1607 Asm->OutStreamer.AddComment("Offset of Compilation Unit Info");
Stephen Hines36b56882014-04-23 16:57:46 -07001608 Asm->EmitSectionOffset(TheU->getLabelBegin(), TheU->getSectionSym());
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001609
1610 Asm->OutStreamer.AddComment("Compilation Unit Length");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001611 Asm->EmitInt32(TheU->getLength());
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001612
Eric Christopher8f1a9292013-09-13 00:35:05 +00001613 // Emit the pubnames for this compilation unit.
Stephen Hines36b56882014-04-23 16:57:46 -07001614 for (const auto &GI : Globals) {
1615 const char *Name = GI.getKeyData();
1616 const DIE *Entity = GI.second;
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001617
1618 Asm->OutStreamer.AddComment("DIE offset");
1619 Asm->EmitInt32(Entity->getOffset());
1620
Eric Christopher8f1a9292013-09-13 00:35:05 +00001621 if (GnuStyle) {
Stephen Hines36b56882014-04-23 16:57:46 -07001622 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
David Blaikie18a6ade2013-09-19 22:19:37 +00001623 Asm->OutStreamer.AddComment(
David Blaikieac3f0162013-09-20 00:33:15 +00001624 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
David Blaikie994c37f2013-09-19 23:01:29 +00001625 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
David Blaikie18a6ade2013-09-19 22:19:37 +00001626 Asm->EmitInt8(Desc.toBits());
Eric Christopher8f1a9292013-09-13 00:35:05 +00001627 }
1628
Stephen Hines36b56882014-04-23 16:57:46 -07001629 Asm->OutStreamer.AddComment("External Name");
1630 Asm->OutStreamer.EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001631 }
1632
1633 Asm->OutStreamer.AddComment("End Mark");
1634 Asm->EmitInt32(0);
Stephen Hines36b56882014-04-23 16:57:46 -07001635 Asm->OutStreamer.EmitLabel(EndLabel);
Krzysztof Parzyszekc5ef7ee2013-02-12 18:00:14 +00001636 }
1637}
1638
Eric Christopher8f1a9292013-09-13 00:35:05 +00001639void DwarfDebug::emitDebugPubTypes(bool GnuStyle) {
Eric Christophercdeaae42013-09-23 20:55:35 +00001640 const MCSection *PSec =
1641 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1642 : Asm->getObjFileLowering().getDwarfPubTypesSection();
Eric Christopher5704d642013-09-13 00:34:58 +00001643
Stephen Hines37ed9c12014-12-01 14:51:49 -08001644 emitDebugPubSection(GnuStyle, PSec, "Types",
1645 &DwarfCompileUnit::getGlobalTypes);
Devang Patel193f7202009-11-24 01:14:22 +00001646}
1647
Eric Christopher64f824c2012-12-27 02:14:01 +00001648// Emit visible names into a debug str section.
1649void DwarfDebug::emitDebugStr() {
Stephen Hines36b56882014-04-23 16:57:46 -07001650 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Eric Christopher64f824c2012-12-27 02:14:01 +00001651 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection());
1652}
1653
Stephen Hines37ed9c12014-12-01 14:51:49 -08001654/// Emits an optimal (=sorted) sequence of DW_OP_pieces.
1655void DwarfDebug::emitLocPieces(ByteStreamer &Streamer,
1656 const DITypeIdentifierMap &Map,
1657 ArrayRef<DebugLocEntry::Value> Values) {
1658 assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value P) {
1659 return P.isVariablePiece();
1660 }) && "all values are expected to be pieces");
1661 assert(std::is_sorted(Values.begin(), Values.end()) &&
1662 "pieces are expected to be sorted");
1663
1664 unsigned Offset = 0;
1665 for (auto Piece : Values) {
1666 DIExpression Expr = Piece.getExpression();
1667 unsigned PieceOffset = Expr.getPieceOffset();
1668 unsigned PieceSize = Expr.getPieceSize();
1669 assert(Offset <= PieceOffset && "overlapping or duplicate pieces");
1670 if (Offset < PieceOffset) {
1671 // The DWARF spec seriously mandates pieces with no locations for gaps.
1672 Asm->EmitDwarfOpPiece(Streamer, (PieceOffset-Offset)*8);
1673 Offset += PieceOffset-Offset;
1674 }
1675
1676 Offset += PieceSize;
1677
1678 const unsigned SizeOfByte = 8;
1679#ifndef NDEBUG
1680 DIVariable Var = Piece.getVariable();
1681 assert(!Var.isIndirect() && "indirect address for piece");
1682 unsigned VarSize = Var.getSizeInBits(Map);
1683 assert(PieceSize+PieceOffset <= VarSize/SizeOfByte
1684 && "piece is larger than or outside of variable");
1685 assert(PieceSize*SizeOfByte != VarSize
1686 && "piece covers entire variable");
1687#endif
1688 if (Piece.isLocation() && Piece.getLoc().isReg())
1689 Asm->EmitDwarfRegOpPiece(Streamer,
1690 Piece.getLoc(),
1691 PieceSize*SizeOfByte);
1692 else {
1693 emitDebugLocValue(Streamer, Piece);
1694 Asm->EmitDwarfOpPiece(Streamer, PieceSize*SizeOfByte);
1695 }
1696 }
1697}
1698
1699
Stephen Hines36b56882014-04-23 16:57:46 -07001700void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
1701 const DebugLocEntry &Entry) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07001702 const DebugLocEntry::Value Value = Entry.getValues()[0];
Stephen Hines37ed9c12014-12-01 14:51:49 -08001703 if (Value.isVariablePiece())
1704 // Emit all pieces that belong to the same variable and range.
1705 return emitLocPieces(Streamer, TypeIdentifierMap, Entry.getValues());
1706
1707 assert(Entry.getValues().size() == 1 && "only pieces may have >1 value");
1708 emitDebugLocValue(Streamer, Value);
1709}
1710
1711void DwarfDebug::emitDebugLocValue(ByteStreamer &Streamer,
1712 const DebugLocEntry::Value &Value) {
1713 DIVariable DV = Value.getVariable();
1714 // Regular entry.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001715 if (Value.isInt()) {
Stephen Hines36b56882014-04-23 16:57:46 -07001716 DIBasicType BTy(resolve(DV.getType()));
1717 if (BTy.Verify() && (BTy.getEncoding() == dwarf::DW_ATE_signed ||
1718 BTy.getEncoding() == dwarf::DW_ATE_signed_char)) {
1719 Streamer.EmitInt8(dwarf::DW_OP_consts, "DW_OP_consts");
Stephen Hinesdce4a402014-05-29 02:49:00 -07001720 Streamer.EmitSLEB128(Value.getInt());
Stephen Hines36b56882014-04-23 16:57:46 -07001721 } else {
1722 Streamer.EmitInt8(dwarf::DW_OP_constu, "DW_OP_constu");
Stephen Hinesdce4a402014-05-29 02:49:00 -07001723 Streamer.EmitULEB128(Value.getInt());
Stephen Hines36b56882014-04-23 16:57:46 -07001724 }
Stephen Hinesdce4a402014-05-29 02:49:00 -07001725 } else if (Value.isLocation()) {
1726 MachineLocation Loc = Value.getLoc();
Stephen Hines37ed9c12014-12-01 14:51:49 -08001727 DIExpression Expr = Value.getExpression();
1728 if (!Expr)
Stephen Hines36b56882014-04-23 16:57:46 -07001729 // Regular entry.
1730 Asm->EmitDwarfRegOp(Streamer, Loc, DV.isIndirect());
1731 else {
1732 // Complex address entry.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001733 unsigned N = Expr.getNumElements();
Stephen Hines36b56882014-04-23 16:57:46 -07001734 unsigned i = 0;
Stephen Hines37ed9c12014-12-01 14:51:49 -08001735 if (N >= 2 && Expr.getElement(0) == dwarf::DW_OP_plus) {
Stephen Hines36b56882014-04-23 16:57:46 -07001736 if (Loc.getOffset()) {
1737 i = 2;
1738 Asm->EmitDwarfRegOp(Streamer, Loc, DV.isIndirect());
1739 Streamer.EmitInt8(dwarf::DW_OP_deref, "DW_OP_deref");
1740 Streamer.EmitInt8(dwarf::DW_OP_plus_uconst, "DW_OP_plus_uconst");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001741 Streamer.EmitSLEB128(Expr.getElement(1));
Stephen Hines36b56882014-04-23 16:57:46 -07001742 } else {
1743 // If first address element is OpPlus then emit
1744 // DW_OP_breg + Offset instead of DW_OP_reg + Offset.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001745 MachineLocation TLoc(Loc.getReg(), Expr.getElement(1));
Stephen Hines36b56882014-04-23 16:57:46 -07001746 Asm->EmitDwarfRegOp(Streamer, TLoc, DV.isIndirect());
1747 i = 2;
1748 }
1749 } else {
1750 Asm->EmitDwarfRegOp(Streamer, Loc, DV.isIndirect());
1751 }
1752
1753 // Emit remaining complex address elements.
1754 for (; i < N; ++i) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08001755 uint64_t Element = Expr.getElement(i);
1756 if (Element == dwarf::DW_OP_plus) {
Stephen Hines36b56882014-04-23 16:57:46 -07001757 Streamer.EmitInt8(dwarf::DW_OP_plus_uconst, "DW_OP_plus_uconst");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001758 Streamer.EmitULEB128(Expr.getElement(++i));
1759 } else if (Element == dwarf::DW_OP_deref) {
Stephen Hines36b56882014-04-23 16:57:46 -07001760 if (!Loc.isReg())
1761 Streamer.EmitInt8(dwarf::DW_OP_deref, "DW_OP_deref");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001762 } else if (Element == dwarf::DW_OP_piece) {
1763 i += 3;
1764 // handled in emitDebugLocEntry.
Stephen Hines36b56882014-04-23 16:57:46 -07001765 } else
1766 llvm_unreachable("unknown Opcode found in complex address");
1767 }
1768 }
1769 }
1770 // else ... ignore constant fp. There is not any good way to
1771 // to represent them here in dwarf.
1772 // FIXME: ^
1773}
1774
1775void DwarfDebug::emitDebugLocEntryLocation(const DebugLocEntry &Entry) {
1776 Asm->OutStreamer.AddComment("Loc expr size");
1777 MCSymbol *begin = Asm->OutStreamer.getContext().CreateTempSymbol();
1778 MCSymbol *end = Asm->OutStreamer.getContext().CreateTempSymbol();
1779 Asm->EmitLabelDifference(end, begin, 2);
1780 Asm->OutStreamer.EmitLabel(begin);
1781 // Emit the entry.
1782 APByteStreamer Streamer(*Asm);
1783 emitDebugLocEntry(Streamer, Entry);
1784 // Close the range.
1785 Asm->OutStreamer.EmitLabel(end);
1786}
1787
Eric Christopherd3b98532013-07-02 21:36:07 +00001788// Emit locations into the debug loc section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001789void DwarfDebug::emitDebugLoc() {
Daniel Dunbar83320a02011-03-16 22:16:39 +00001790 // Start the dwarf loc section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +00001791 Asm->OutStreamer.SwitchSection(
Stephen Hines36b56882014-04-23 16:57:46 -07001792 Asm->getObjFileLowering().getDwarfLocSection());
Chandler Carruth426c2bf2012-11-01 09:14:31 +00001793 unsigned char Size = Asm->getDataLayout().getPointerSize();
Stephen Hines36b56882014-04-23 16:57:46 -07001794 for (const auto &DebugLoc : DotDebugLocEntries) {
1795 Asm->OutStreamer.EmitLabel(DebugLoc.Label);
Stephen Hines37ed9c12014-12-01 14:51:49 -08001796 const DwarfCompileUnit *CU = DebugLoc.CU;
Stephen Hines36b56882014-04-23 16:57:46 -07001797 for (const auto &Entry : DebugLoc.List) {
1798 // Set up the range. This range is relative to the entry point of the
1799 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1800 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
Stephen Hines37ed9c12014-12-01 14:51:49 -08001801 if (auto *Base = CU->getBaseAddress()) {
Stephen Hines36b56882014-04-23 16:57:46 -07001802 Asm->EmitLabelDifference(Entry.getBeginSym(), Base, Size);
1803 Asm->EmitLabelDifference(Entry.getEndSym(), Base, Size);
1804 } else {
1805 Asm->OutStreamer.EmitSymbolValue(Entry.getBeginSym(), Size);
1806 Asm->OutStreamer.EmitSymbolValue(Entry.getEndSym(), Size);
Devang Patelc26f5442011-04-28 02:22:40 +00001807 }
Stephen Hines36b56882014-04-23 16:57:46 -07001808
1809 emitDebugLocEntryLocation(Entry);
Devang Patelc3f5f782010-05-25 23:40:22 +00001810 }
Stephen Hines36b56882014-04-23 16:57:46 -07001811 Asm->OutStreamer.EmitIntValue(0, Size);
1812 Asm->OutStreamer.EmitIntValue(0, Size);
Devang Patelc3f5f782010-05-25 23:40:22 +00001813 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001814}
1815
Stephen Hines36b56882014-04-23 16:57:46 -07001816void DwarfDebug::emitDebugLocDWO() {
1817 Asm->OutStreamer.SwitchSection(
1818 Asm->getObjFileLowering().getDwarfLocDWOSection());
1819 for (const auto &DebugLoc : DotDebugLocEntries) {
1820 Asm->OutStreamer.EmitLabel(DebugLoc.Label);
1821 for (const auto &Entry : DebugLoc.List) {
1822 // Just always use start_length for now - at least that's one address
1823 // rather than two. We could get fancier and try to, say, reuse an
1824 // address we know we've emitted elsewhere (the start of the function?
1825 // The start of the CU or CU subrange that encloses this range?)
1826 Asm->EmitInt8(dwarf::DW_LLE_start_length_entry);
Stephen Hinesdce4a402014-05-29 02:49:00 -07001827 unsigned idx = AddrPool.getIndex(Entry.getBeginSym());
Stephen Hines36b56882014-04-23 16:57:46 -07001828 Asm->EmitULEB128(idx);
1829 Asm->EmitLabelDifference(Entry.getEndSym(), Entry.getBeginSym(), 4);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001830
Stephen Hines36b56882014-04-23 16:57:46 -07001831 emitDebugLocEntryLocation(Entry);
1832 }
1833 Asm->EmitInt8(dwarf::DW_LLE_end_of_list_entry);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001834 }
Richard Mitton5cc319a2013-09-19 23:21:01 +00001835}
1836
1837struct ArangeSpan {
1838 const MCSymbol *Start, *End;
1839};
1840
1841// Emit a debug aranges section, containing a CU lookup for any
1842// address we can tie back to a CU.
Eric Christopher7ee5f5d2012-11-21 00:34:35 +00001843void DwarfDebug::emitDebugARanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001844 // Start the dwarf aranges section.
Stephen Hines36b56882014-04-23 16:57:46 -07001845 Asm->OutStreamer.SwitchSection(
1846 Asm->getObjFileLowering().getDwarfARangesSection());
Richard Mitton5cc319a2013-09-19 23:21:01 +00001847
Stephen Hinesdce4a402014-05-29 02:49:00 -07001848 typedef DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> SpansType;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001849
1850 SpansType Spans;
1851
1852 // Build a list of sections used.
1853 std::vector<const MCSection *> Sections;
Stephen Hines36b56882014-04-23 16:57:46 -07001854 for (const auto &it : SectionMap) {
1855 const MCSection *Section = it.first;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001856 Sections.push_back(Section);
1857 }
1858
1859 // Sort the sections into order.
1860 // This is only done to ensure consistent output order across different runs.
1861 std::sort(Sections.begin(), Sections.end(), SectionSort);
1862
1863 // Build a set of address spans, sorted by CU.
Stephen Hines36b56882014-04-23 16:57:46 -07001864 for (const MCSection *Section : Sections) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001865 SmallVector<SymbolCU, 8> &List = SectionMap[Section];
1866 if (List.size() < 2)
1867 continue;
1868
1869 // Sort the symbols by offset within the section.
Stephen Hines36b56882014-04-23 16:57:46 -07001870 std::sort(List.begin(), List.end(),
1871 [&](const SymbolCU &A, const SymbolCU &B) {
1872 unsigned IA = A.Sym ? Asm->OutStreamer.GetSymbolOrder(A.Sym) : 0;
1873 unsigned IB = B.Sym ? Asm->OutStreamer.GetSymbolOrder(B.Sym) : 0;
1874
1875 // Symbols with no order assigned should be placed at the end.
1876 // (e.g. section end labels)
1877 if (IA == 0)
1878 return false;
1879 if (IB == 0)
1880 return true;
1881 return IA < IB;
1882 });
Richard Mitton5cc319a2013-09-19 23:21:01 +00001883
1884 // If we have no section (e.g. common), just write out
1885 // individual spans for each symbol.
Stephen Hinesdce4a402014-05-29 02:49:00 -07001886 if (!Section) {
Stephen Hines36b56882014-04-23 16:57:46 -07001887 for (const SymbolCU &Cur : List) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001888 ArangeSpan Span;
1889 Span.Start = Cur.Sym;
Stephen Hinesdce4a402014-05-29 02:49:00 -07001890 Span.End = nullptr;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001891 if (Cur.CU)
1892 Spans[Cur.CU].push_back(Span);
1893 }
1894 } else {
1895 // Build spans between each label.
1896 const MCSymbol *StartSym = List[0].Sym;
Stephen Hines36b56882014-04-23 16:57:46 -07001897 for (size_t n = 1, e = List.size(); n < e; n++) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001898 const SymbolCU &Prev = List[n - 1];
1899 const SymbolCU &Cur = List[n];
1900
1901 // Try and build the longest span we can within the same CU.
1902 if (Cur.CU != Prev.CU) {
1903 ArangeSpan Span;
1904 Span.Start = StartSym;
1905 Span.End = Cur.Sym;
1906 Spans[Prev.CU].push_back(Span);
1907 StartSym = Cur.Sym;
1908 }
1909 }
1910 }
1911 }
1912
Richard Mitton5cc319a2013-09-19 23:21:01 +00001913 unsigned PtrSize = Asm->getDataLayout().getPointerSize();
1914
1915 // Build a list of CUs used.
Stephen Hines36b56882014-04-23 16:57:46 -07001916 std::vector<DwarfCompileUnit *> CUs;
1917 for (const auto &it : Spans) {
1918 DwarfCompileUnit *CU = it.first;
Richard Mitton5cc319a2013-09-19 23:21:01 +00001919 CUs.push_back(CU);
1920 }
1921
1922 // Sort the CU list (again, to ensure consistent output order).
Stephen Hines36b56882014-04-23 16:57:46 -07001923 std::sort(CUs.begin(), CUs.end(), [](const DwarfUnit *A, const DwarfUnit *B) {
1924 return A->getUniqueID() < B->getUniqueID();
1925 });
Richard Mitton5cc319a2013-09-19 23:21:01 +00001926
1927 // Emit an arange table for each CU we used.
Stephen Hines36b56882014-04-23 16:57:46 -07001928 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001929 std::vector<ArangeSpan> &List = Spans[CU];
1930
Stephen Hines37ed9c12014-12-01 14:51:49 -08001931 // Describe the skeleton CU's offset and length, not the dwo file's.
1932 if (auto *Skel = CU->getSkeleton())
1933 CU = Skel;
1934
Richard Mitton5cc319a2013-09-19 23:21:01 +00001935 // Emit size of content not including length itself.
Stephen Hines36b56882014-04-23 16:57:46 -07001936 unsigned ContentSize =
1937 sizeof(int16_t) + // DWARF ARange version number
1938 sizeof(int32_t) + // Offset of CU in the .debug_info section
1939 sizeof(int8_t) + // Pointer Size (in bytes)
1940 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton5cc319a2013-09-19 23:21:01 +00001941
1942 unsigned TupleSize = PtrSize * 2;
1943
1944 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Stephen Hines36b56882014-04-23 16:57:46 -07001945 unsigned Padding =
1946 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001947
1948 ContentSize += Padding;
1949 ContentSize += (List.size() + 1) * TupleSize;
1950
1951 // For each compile unit, write the list of spans it covers.
1952 Asm->OutStreamer.AddComment("Length of ARange Set");
1953 Asm->EmitInt32(ContentSize);
1954 Asm->OutStreamer.AddComment("DWARF Arange version number");
1955 Asm->EmitInt16(dwarf::DW_ARANGES_VERSION);
1956 Asm->OutStreamer.AddComment("Offset Into Debug Info Section");
Stephen Hines37ed9c12014-12-01 14:51:49 -08001957 Asm->EmitSectionOffset(CU->getLabelBegin(), CU->getSectionSym());
Richard Mitton5cc319a2013-09-19 23:21:01 +00001958 Asm->OutStreamer.AddComment("Address Size (in bytes)");
1959 Asm->EmitInt8(PtrSize);
1960 Asm->OutStreamer.AddComment("Segment Size (in bytes)");
1961 Asm->EmitInt8(0);
1962
Stephen Hines36b56882014-04-23 16:57:46 -07001963 Asm->OutStreamer.EmitFill(Padding, 0xff);
Richard Mitton5cc319a2013-09-19 23:21:01 +00001964
Stephen Hines36b56882014-04-23 16:57:46 -07001965 for (const ArangeSpan &Span : List) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001966 Asm->EmitLabelReference(Span.Start, PtrSize);
1967
1968 // Calculate the size as being from the span start to it's end.
Richard Mittoneb46def2013-09-23 17:56:20 +00001969 if (Span.End) {
Richard Mitton5cc319a2013-09-19 23:21:01 +00001970 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mittoneb46def2013-09-23 17:56:20 +00001971 } else {
1972 // For symbols without an end marker (e.g. common), we
1973 // write a single arange entry containing just that one symbol.
1974 uint64_t Size = SymSize[Span.Start];
1975 if (Size == 0)
1976 Size = 1;
1977
1978 Asm->OutStreamer.EmitIntValue(Size, PtrSize);
1979 }
Richard Mitton5cc319a2013-09-19 23:21:01 +00001980 }
1981
1982 Asm->OutStreamer.AddComment("ARange terminator");
1983 Asm->OutStreamer.EmitIntValue(0, PtrSize);
1984 Asm->OutStreamer.EmitIntValue(0, PtrSize);
1985 }
Bill Wendling94d04b82009-05-20 23:21:38 +00001986}
1987
Eric Christopherb6dc8652012-11-27 22:43:45 +00001988// Emit visible names into a debug ranges section.
Devang Patel2c4ceb12009-11-21 02:48:08 +00001989void DwarfDebug::emitDebugRanges() {
Bill Wendling94d04b82009-05-20 23:21:38 +00001990 // Start the dwarf ranges section.
Stephen Hines36b56882014-04-23 16:57:46 -07001991 Asm->OutStreamer.SwitchSection(
1992 Asm->getObjFileLowering().getDwarfRangesSection());
Bill Wendling94d04b82009-05-20 23:21:38 +00001993
Stephen Hines36b56882014-04-23 16:57:46 -07001994 // Size for our labels.
1995 unsigned char Size = Asm->getDataLayout().getPointerSize();
1996
1997 // Grab the specific ranges for the compile units in the module.
1998 for (const auto &I : CUMap) {
1999 DwarfCompileUnit *TheCU = I.second;
2000
Stephen Hines37ed9c12014-12-01 14:51:49 -08002001 if (auto *Skel = TheCU->getSkeleton())
2002 TheCU = Skel;
2003
Stephen Hines36b56882014-04-23 16:57:46 -07002004 // Iterate over the misc ranges for the compile units in the module.
2005 for (const RangeSpanList &List : TheCU->getRangeLists()) {
2006 // Emit our symbol so we can find the beginning of the range.
2007 Asm->OutStreamer.EmitLabel(List.getSym());
2008
2009 for (const RangeSpan &Range : List.getRanges()) {
2010 const MCSymbol *Begin = Range.getStart();
2011 const MCSymbol *End = Range.getEnd();
2012 assert(Begin && "Range without a begin symbol?");
2013 assert(End && "Range without an end symbol?");
Stephen Hines37ed9c12014-12-01 14:51:49 -08002014 if (auto *Base = TheCU->getBaseAddress()) {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002015 Asm->EmitLabelDifference(Begin, Base, Size);
2016 Asm->EmitLabelDifference(End, Base, Size);
2017 } else {
2018 Asm->OutStreamer.EmitSymbolValue(Begin, Size);
2019 Asm->OutStreamer.EmitSymbolValue(End, Size);
2020 }
Stephen Hines36b56882014-04-23 16:57:46 -07002021 }
2022
2023 // And terminate the list with two 0 values.
2024 Asm->OutStreamer.EmitIntValue(0, Size);
2025 Asm->OutStreamer.EmitIntValue(0, Size);
2026 }
Bill Wendling94d04b82009-05-20 23:21:38 +00002027 }
2028}
2029
Eric Christopher0b944ee2012-12-11 19:42:09 +00002030// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher98e237f2012-11-30 23:59:06 +00002031
Stephen Hinesdce4a402014-05-29 02:49:00 -07002032void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
2033 std::unique_ptr<DwarfUnit> NewU) {
Stephen Hines37ed9c12014-12-01 14:51:49 -08002034 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
2035 U.getCUNode().getSplitDebugFilename());
Eric Christopher98e237f2012-11-30 23:59:06 +00002036
2037 if (!CompilationDir.empty())
Stephen Hines37ed9c12014-12-01 14:51:49 -08002038 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christopher98e237f2012-11-30 23:59:06 +00002039
Stephen Hinesdce4a402014-05-29 02:49:00 -07002040 addGnuPubAttributes(*NewU, Die);
Eric Christophera6d84152013-09-30 23:14:16 +00002041
Stephen Hinesdce4a402014-05-29 02:49:00 -07002042 SkeletonHolder.addUnit(std::move(NewU));
Stephen Hines36b56882014-04-23 16:57:46 -07002043}
Eric Christopher8f1a9292013-09-13 00:35:05 +00002044
Stephen Hines36b56882014-04-23 16:57:46 -07002045// This DIE has the following attributes: DW_AT_comp_dir, DW_AT_stmt_list,
2046// DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_dwo_name, DW_AT_dwo_id,
2047// DW_AT_addr_base, DW_AT_ranges_base.
Stephen Hinesdce4a402014-05-29 02:49:00 -07002048DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
Eric Christopher451c71d2013-10-01 00:43:36 +00002049
Stephen Hinesdce4a402014-05-29 02:49:00 -07002050 auto OwnedUnit = make_unique<DwarfCompileUnit>(
2051 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
2052 DwarfCompileUnit &NewCU = *OwnedUnit;
2053 NewCU.initSection(Asm->getObjFileLowering().getDwarfInfoSection(),
2054 DwarfInfoSectionSym);
Stephen Hines36b56882014-04-23 16:57:46 -07002055
Stephen Hinesdce4a402014-05-29 02:49:00 -07002056 NewCU.initStmtList(DwarfLineSectionSym);
Stephen Hines36b56882014-04-23 16:57:46 -07002057
Stephen Hinesdce4a402014-05-29 02:49:00 -07002058 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
Eric Christopher0e3e9b72012-12-10 23:34:43 +00002059
Eric Christopher98e237f2012-11-30 23:59:06 +00002060 return NewCU;
2061}
2062
Eric Christopher0b944ee2012-12-11 19:42:09 +00002063// Emit the .debug_info.dwo section for separated dwarf. This contains the
2064// compile units that would normally be in debug_info.
Eric Christopher98e237f2012-11-30 23:59:06 +00002065void DwarfDebug::emitDebugInfoDWO() {
Eric Christopher4daaed12012-12-10 19:51:21 +00002066 assert(useSplitDwarf() && "No split dwarf debug info?");
Stephen Hines36b56882014-04-23 16:57:46 -07002067 // Don't pass an abbrev symbol, using a constant zero instead so as not to
2068 // emit relocations into the dwo file.
Stephen Hines37ed9c12014-12-01 14:51:49 -08002069 InfoHolder.emitUnits(/* AbbrevSymbol */ nullptr);
Eric Christopher6eebe472012-12-19 22:02:53 +00002070}
2071
2072// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2073// abbreviations for the .debug_info.dwo section.
2074void DwarfDebug::emitDebugAbbrevDWO() {
2075 assert(useSplitDwarf() && "No split dwarf?");
Stephen Hines36b56882014-04-23 16:57:46 -07002076 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
2077}
2078
2079void DwarfDebug::emitDebugLineDWO() {
2080 assert(useSplitDwarf() && "No split dwarf?");
2081 Asm->OutStreamer.SwitchSection(
2082 Asm->getObjFileLowering().getDwarfLineDWOSection());
2083 SplitTypeUnitFileTable.Emit(Asm->OutStreamer);
Eric Christopher98e237f2012-11-30 23:59:06 +00002084}
Eric Christopher64f824c2012-12-27 02:14:01 +00002085
2086// Emit the .debug_str.dwo section for separated dwarf. This contains the
2087// string section and is identical in format to traditional .debug_str
2088// sections.
2089void DwarfDebug::emitDebugStrDWO() {
2090 assert(useSplitDwarf() && "No split dwarf?");
Stephen Hines36b56882014-04-23 16:57:46 -07002091 const MCSection *OffSec =
2092 Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopherdd8e9f32013-01-07 19:32:41 +00002093 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
Stephen Hines37ed9c12014-12-01 14:51:49 -08002094 OffSec);
Eric Christopher64f824c2012-12-27 02:14:01 +00002095}
Stephen Hines36b56882014-04-23 16:57:46 -07002096
2097MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
2098 if (!useSplitDwarf())
2099 return nullptr;
2100 if (SingleCU)
2101 SplitTypeUnitFileTable.setCompilationDir(CU.getCUNode().getDirectory());
2102 return &SplitTypeUnitFileTable;
2103}
2104
Stephen Hinesdce4a402014-05-29 02:49:00 -07002105static uint64_t makeTypeSignature(StringRef Identifier) {
Stephen Hines36b56882014-04-23 16:57:46 -07002106 MD5 Hash;
2107 Hash.update(Identifier);
2108 // ... take the least significant 8 bytes and return those. Our MD5
2109 // implementation always returns its results in little endian, swap bytes
2110 // appropriately.
2111 MD5::MD5Result Result;
2112 Hash.final(Result);
Stephen Hinesdce4a402014-05-29 02:49:00 -07002113 return *reinterpret_cast<support::ulittle64_t *>(Result + 8);
2114}
Stephen Hines36b56882014-04-23 16:57:46 -07002115
Stephen Hinesdce4a402014-05-29 02:49:00 -07002116void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
2117 StringRef Identifier, DIE &RefDie,
2118 DICompositeType CTy) {
2119 // Fast path if we're building some type units and one has already used the
2120 // address pool we know we're going to throw away all this work anyway, so
2121 // don't bother building dependent types.
2122 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
2123 return;
Stephen Hines36b56882014-04-23 16:57:46 -07002124
Stephen Hinesdce4a402014-05-29 02:49:00 -07002125 const DwarfTypeUnit *&TU = DwarfTypeUnits[CTy];
2126 if (TU) {
2127 CU.addDIETypeSignature(RefDie, *TU);
2128 return;
2129 }
2130
2131 bool TopLevelType = TypeUnitsUnderConstruction.empty();
2132 AddrPool.resetUsedFlag();
2133
Stephen Hines37ed9c12014-12-01 14:51:49 -08002134 auto OwnedUnit = make_unique<DwarfTypeUnit>(
2135 InfoHolder.getUnits().size() + TypeUnitsUnderConstruction.size(), CU, Asm,
2136 this, &InfoHolder, getDwoLineTable(CU));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002137 DwarfTypeUnit &NewTU = *OwnedUnit;
2138 DIE &UnitDie = NewTU.getUnitDie();
2139 TU = &NewTU;
2140 TypeUnitsUnderConstruction.push_back(
2141 std::make_pair(std::move(OwnedUnit), CTy));
2142
2143 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
2144 CU.getLanguage());
2145
2146 uint64_t Signature = makeTypeSignature(Identifier);
2147 NewTU.setTypeSignature(Signature);
2148
Stephen Hines37ed9c12014-12-01 14:51:49 -08002149 if (useSplitDwarf())
2150 NewTU.initSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
2151 else {
Stephen Hinesdce4a402014-05-29 02:49:00 -07002152 CU.applyStmtList(UnitDie);
Stephen Hines37ed9c12014-12-01 14:51:49 -08002153 NewTU.initSection(
2154 Asm->getObjFileLowering().getDwarfTypesSection(Signature));
2155 }
Stephen Hines36b56882014-04-23 16:57:46 -07002156
Stephen Hinesdce4a402014-05-29 02:49:00 -07002157 NewTU.setType(NewTU.createTypeDIE(CTy));
2158
2159 if (TopLevelType) {
2160 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2161 TypeUnitsUnderConstruction.clear();
2162
2163 // Types referencing entries in the address table cannot be placed in type
2164 // units.
2165 if (AddrPool.hasBeenUsed()) {
2166
2167 // Remove all the types built while building this type.
2168 // This is pessimistic as some of these types might not be dependent on
2169 // the type that used an address.
2170 for (const auto &TU : TypeUnitsToAdd)
2171 DwarfTypeUnits.erase(TU.second);
2172
2173 // Construct this type in the CU directly.
2174 // This is inefficient because all the dependent types will be rebuilt
2175 // from scratch, including building them in type units, discovering that
2176 // they depend on addresses, throwing them out and rebuilding them.
2177 CU.constructTypeDIE(RefDie, CTy);
2178 return;
2179 }
2180
2181 // If the type wasn't dependent on fission addresses, finish adding the type
2182 // and all its dependent types.
Stephen Hines37ed9c12014-12-01 14:51:49 -08002183 for (auto &TU : TypeUnitsToAdd)
Stephen Hinesdce4a402014-05-29 02:49:00 -07002184 InfoHolder.addUnit(std::move(TU.first));
Stephen Hinesdce4a402014-05-29 02:49:00 -07002185 }
2186 CU.addDIETypeSignature(RefDie, NewTU);
Stephen Hines36b56882014-04-23 16:57:46 -07002187}
2188
Stephen Hinesdce4a402014-05-29 02:49:00 -07002189// Accelerator table mutators - add each name along with its companion
2190// DIE to the proper table while ensuring that the name that we're going
2191// to reference is in the string table. We do this since the names we
2192// add may not only be identical to the names in the DIE.
2193void DwarfDebug::addAccelName(StringRef Name, const DIE &Die) {
2194 if (!useDwarfAccelTables())
2195 return;
2196 AccelNames.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2197 &Die);
2198}
2199
2200void DwarfDebug::addAccelObjC(StringRef Name, const DIE &Die) {
2201 if (!useDwarfAccelTables())
2202 return;
2203 AccelObjC.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2204 &Die);
2205}
2206
2207void DwarfDebug::addAccelNamespace(StringRef Name, const DIE &Die) {
2208 if (!useDwarfAccelTables())
2209 return;
2210 AccelNamespace.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2211 &Die);
2212}
2213
2214void DwarfDebug::addAccelType(StringRef Name, const DIE &Die, char Flags) {
2215 if (!useDwarfAccelTables())
2216 return;
2217 AccelTypes.AddName(Name, InfoHolder.getStringPool().getSymbol(*Asm, Name),
2218 &Die);
Stephen Hines36b56882014-04-23 16:57:46 -07002219}