blob: 2f14f5464fd531959e7c394e4e1cca5c388180d9 [file] [log] [blame]
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001//===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===//
Bill Wendling2f921f82009-05-15 09:23:25 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains support for writing dwarf debug info into asm files.
11//
12//===----------------------------------------------------------------------===//
Chris Lattnerb14490d2010-03-09 00:39:24 +000013
Bill Wendling2f921f82009-05-15 09:23:25 +000014#include "DwarfDebug.h"
David Blaikie37c52312014-10-04 15:49:50 +000015#include "ByteStreamer.h"
Eric Christopher45731982013-08-08 23:45:55 +000016#include "DIEHash.h"
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +000017#include "DebugLocEntry.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000018#include "DebugLocStream.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000019#include "DwarfCompileUnit.h"
20#include "DwarfExpression.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000021#include "DwarfFile.h"
David Blaikie2c86a722013-12-02 19:33:15 +000022#include "DwarfUnit.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000023#include "llvm/ADT/APInt.h"
24#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/DenseSet.h"
26#include "llvm/ADT/MapVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/ADT/STLExtras.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000028#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/StringRef.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/ADT/Triple.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000031#include "llvm/ADT/Twine.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000032#include "llvm/BinaryFormat/Dwarf.h"
Jonas Devlieghere855fc3b2018-01-29 14:52:41 +000033#include "llvm/CodeGen/AccelTable.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000034#include "llvm/CodeGen/AsmPrinter.h"
Frederic Risse541e0b2015-01-05 21:29:41 +000035#include "llvm/CodeGen/DIE.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000036#include "llvm/CodeGen/LexicalScopes.h"
37#include "llvm/CodeGen/MachineBasicBlock.h"
David Greene829b3e82009-08-19 21:52:55 +000038#include "llvm/CodeGen/MachineFunction.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000039#include "llvm/CodeGen/MachineInstr.h"
Bill Wendling2f921f82009-05-15 09:23:25 +000040#include "llvm/CodeGen/MachineModuleInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000041#include "llvm/CodeGen/MachineOperand.h"
Vedant Kumar5931b4e2018-10-05 20:37:17 +000042#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000043#include "llvm/CodeGen/TargetRegisterInfo.h"
44#include "llvm/CodeGen/TargetSubtargetInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000045#include "llvm/IR/Constants.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000046#include "llvm/IR/DebugInfoMetadata.h"
47#include "llvm/IR/DebugLoc.h"
48#include "llvm/IR/Function.h"
49#include "llvm/IR/GlobalVariable.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000050#include "llvm/IR/Module.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000051#include "llvm/MC/MCAsmInfo.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000052#include "llvm/MC/MCContext.h"
Frederic Rissa5ab8442015-08-07 15:14:08 +000053#include "llvm/MC/MCDwarf.h"
Chris Lattner4d2c0f92009-07-31 18:48:30 +000054#include "llvm/MC/MCSection.h"
Chris Lattner4b7dadb2009-08-19 05:49:37 +000055#include "llvm/MC/MCStreamer.h"
Chris Lattnere13c3722010-03-09 01:58:53 +000056#include "llvm/MC/MCSymbol.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000057#include "llvm/MC/MCTargetOptions.h"
58#include "llvm/MC/MachineLocation.h"
59#include "llvm/MC/SectionKind.h"
60#include "llvm/Pass.h"
61#include "llvm/Support/Casting.h"
Devang Patel6c74a872010-04-27 19:46:33 +000062#include "llvm/Support/CommandLine.h"
Daniel Dunbarcdf01b52009-10-13 06:47:08 +000063#include "llvm/Support/Debug.h"
64#include "llvm/Support/ErrorHandling.h"
Eric Christopher67646432013-07-26 17:02:41 +000065#include "llvm/Support/MD5.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000066#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000067#include "llvm/Support/Timer.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000068#include "llvm/Support/raw_ostream.h"
David Blaikie6054e652018-03-23 23:58:19 +000069#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000070#include "llvm/Target/TargetMachine.h"
71#include "llvm/Target/TargetOptions.h"
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +000072#include <algorithm>
73#include <cassert>
74#include <cstddef>
75#include <cstdint>
76#include <iterator>
77#include <string>
78#include <utility>
79#include <vector>
Eugene Zelenkoecefe5a2016-02-02 18:20:45 +000080
Bill Wendling2f921f82009-05-15 09:23:25 +000081using namespace llvm;
82
Chandler Carruth1b9dde02014-04-22 02:02:50 +000083#define DEBUG_TYPE "dwarfdebug"
84
Eric Christopher7f2b5512013-07-23 22:16:41 +000085static cl::opt<bool>
86DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
87 cl::desc("Disable debug info printing"));
Devang Patel6c74a872010-04-27 19:46:33 +000088
David Blaikie038e28a2017-07-31 21:48:42 +000089static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier(
90 "use-dwarf-ranges-base-address-specifier", cl::Hidden,
David Blaikiec2be8632017-08-01 14:50:50 +000091 cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
David Blaikie038e28a2017-07-31 21:48:42 +000092
Eric Christopher02dbadb2014-02-14 01:26:55 +000093static cl::opt<bool> GenerateARangeSection("generate-arange-section",
94 cl::Hidden,
95 cl::desc("Generate dwarf aranges"),
96 cl::init(false));
97
Pavel Labathf9adc202018-07-20 12:59:05 +000098static cl::opt<bool>
99 GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
100 cl::desc("Generate DWARF4 type units."),
101 cl::init(false));
102
David Blaikie488393f2017-05-12 01:13:45 +0000103static cl::opt<bool> SplitDwarfCrossCuReferences(
104 "split-dwarf-cross-cu-references", cl::Hidden,
105 cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
106
Eric Christopherf07ee3a2014-01-27 23:50:03 +0000107enum DefaultOnOff { Default, Enable, Disable };
Eric Christopher4996c702011-11-07 09:24:32 +0000108
Paul Robinsonac7fe5e2016-12-12 20:49:11 +0000109static cl::opt<DefaultOnOff> UnknownLocations(
110 "use-unknown-locations", cl::Hidden,
111 cl::desc("Make an absence of debug location information explicit."),
112 cl::values(clEnumVal(Default, "At top of block or after label"),
113 clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
114 cl::init(Default));
115
Pavel Labath6088c232018-04-04 14:42:14 +0000116static cl::opt<AccelTableKind> AccelTables(
117 "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."),
118 cl::values(clEnumValN(AccelTableKind::Default, "Default",
119 "Default for platform"),
120 clEnumValN(AccelTableKind::None, "Disable", "Disabled."),
121 clEnumValN(AccelTableKind::Apple, "Apple", "Apple"),
122 clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")),
123 cl::init(AccelTableKind::Default));
Eric Christopher20b76a72012-08-23 22:36:40 +0000124
Alexey Bataev0d6aead2018-02-20 15:28:08 +0000125static cl::opt<DefaultOnOff>
126DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden,
127 cl::desc("Use inlined strings rather than string section."),
128 cl::values(clEnumVal(Default, "Default for platform"),
129 clEnumVal(Enable, "Enabled"),
130 clEnumVal(Disable, "Disabled")),
131 cl::init(Default));
132
Alexey Bataev648ed2d2018-03-20 16:04:40 +0000133static cl::opt<bool>
Alexey Bataev858a7dd2018-03-20 20:21:38 +0000134 NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden,
135 cl::desc("Disable emission .debug_ranges section."),
136 cl::init(false));
137
Alexey Bataevbff36082018-03-23 13:35:54 +0000138static cl::opt<DefaultOnOff> DwarfSectionsAsReferences(
139 "dwarf-sections-as-references", cl::Hidden,
140 cl::desc("Use sections+offset as references rather than labels."),
141 cl::values(clEnumVal(Default, "Default for platform"),
142 clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
143 cl::init(Default));
144
Paul Robinson43d1e452016-04-18 22:41:41 +0000145enum LinkageNameOption {
146 DefaultLinkageNames,
147 AllLinkageNames,
148 AbstractLinkageNames
149};
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000150
Paul Robinson43d1e452016-04-18 22:41:41 +0000151static cl::opt<LinkageNameOption>
152 DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
153 cl::desc("Which DWARF linkage-name attributes to emit."),
154 cl::values(clEnumValN(DefaultLinkageNames, "Default",
155 "Default for platform"),
156 clEnumValN(AllLinkageNames, "All", "All"),
157 clEnumValN(AbstractLinkageNames, "Abstract",
Mehdi Amini732afdd2016-10-08 19:41:06 +0000158 "Abstract subprograms")),
Paul Robinson43d1e452016-04-18 22:41:41 +0000159 cl::init(DefaultLinkageNames));
Paul Robinson78046b42015-08-11 21:36:45 +0000160
Matthias Braun9f15a792016-11-18 19:43:18 +0000161static const char *const DWARFGroupName = "dwarf";
162static const char *const DWARFGroupDescription = "DWARF Emission";
163static const char *const DbgTimerName = "writer";
164static const char *const DbgTimerDescription = "DWARF Debug Writer";
Bill Wendlingfcc14142010-04-07 09:28:04 +0000165
Adrian Prantla63b8e82017-03-16 17:42:45 +0000166void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000167 BS.EmitInt8(
168 Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
169 : dwarf::OperationEncodingString(Op));
170}
171
Adrian Prantla63b8e82017-03-16 17:42:45 +0000172void DebugLocDwarfExpression::emitSigned(int64_t Value) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000173 BS.EmitSLEB128(Value, Twine(Value));
174}
175
Adrian Prantla63b8e82017-03-16 17:42:45 +0000176void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000177 BS.EmitULEB128(Value, Twine(Value));
178}
179
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000180bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
181 unsigned MachineReg) {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000182 // This information is not available while emitting .debug_loc entries.
183 return false;
184}
185
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000186bool DbgVariable::isBlockByrefVariable() const {
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000187 assert(getVariable() && "Invalid complex DbgVariable!");
188 return getVariable()->getType().resolve()->isBlockByrefStruct();
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000189}
190
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000191const DIType *DbgVariable::getType() const {
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000192 DIType *Ty = getVariable()->getType().resolve();
Devang Patelf20c4f72011-04-12 22:53:02 +0000193 // FIXME: isBlockByrefVariable should be reformulated in terms of complex
194 // addresses instead.
Duncan P. N. Exon Smith571baeb2015-04-14 01:59:58 +0000195 if (Ty->isBlockByrefStruct()) {
Devang Patelf20c4f72011-04-12 22:53:02 +0000196 /* Byref variables, in Blocks, are declared by the programmer as
197 "SomeType VarName;", but the compiler creates a
198 __Block_byref_x_VarName struct, and gives the variable VarName
199 either the struct, or a pointer to the struct, as its type. This
200 is necessary for various behind-the-scenes things the compiler
201 needs to do with by-reference variables in blocks.
Eric Christopher6a841382012-11-19 22:42:10 +0000202
Devang Patelf20c4f72011-04-12 22:53:02 +0000203 However, as far as the original *programmer* is concerned, the
204 variable should still have type 'SomeType', as originally declared.
Eric Christopher6a841382012-11-19 22:42:10 +0000205
Devang Patelf20c4f72011-04-12 22:53:02 +0000206 The following function dives into the __Block_byref_x_VarName
207 struct to find the original type of the variable. This will be
208 passed back to the code generating the type for the Debug
209 Information Entry for the variable 'VarName'. 'VarName' will then
210 have the original type 'SomeType' in its debug information.
Eric Christopher6a841382012-11-19 22:42:10 +0000211
Devang Patelf20c4f72011-04-12 22:53:02 +0000212 The original type 'SomeType' will be the type of the field named
213 'VarName' inside the __Block_byref_x_VarName struct.
Eric Christopher6a841382012-11-19 22:42:10 +0000214
Devang Patelf20c4f72011-04-12 22:53:02 +0000215 NOTE: In order for this to not completely fail on the debugger
216 side, the Debug Information Entry for the variable VarName needs to
217 have a DW_AT_location that tells the debugger how to unwind through
218 the pointers and __Block_byref_x_VarName struct to find the actual
219 value of the variable. The function addBlockByrefType does this. */
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000220 DIType *subType = Ty;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000221 uint16_t tag = Ty->getTag();
Eric Christopher6a841382012-11-19 22:42:10 +0000222
Eric Christopher9adc55f2013-09-04 19:53:21 +0000223 if (tag == dwarf::DW_TAG_pointer_type)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000224 subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
Eric Christopher6a841382012-11-19 22:42:10 +0000225
Duncan P. N. Exon Smith6ac940d2015-07-24 18:17:17 +0000226 auto Elements = cast<DICompositeType>(subType)->getElements();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000227 for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
Duncan P. N. Exon Smith099ea1c2015-07-24 18:58:32 +0000228 auto *DT = cast<DIDerivedType>(Elements[i]);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000229 if (getName() == DT->getName())
Duncan P. N. Exon Smith848af382015-04-20 18:20:03 +0000230 return resolve(DT->getBaseType());
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000231 }
Devang Patel6d9f9fe2010-08-09 21:01:39 +0000232 }
Devang Patelf20c4f72011-04-12 22:53:02 +0000233 return Ty;
234}
Bill Wendling2f921f82009-05-15 09:23:25 +0000235
Adrian Prantl67c24422017-02-17 19:42:32 +0000236ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
Adrian Prantl55030772017-03-22 16:50:16 +0000237 if (FrameIndexExprs.size() == 1)
238 return FrameIndexExprs;
239
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000240 assert(llvm::all_of(FrameIndexExprs,
241 [](const FrameIndexExpr &A) {
242 return A.Expr->isFragment();
243 }) &&
Adrian Prantl55030772017-03-22 16:50:16 +0000244 "multiple FI expressions without DW_OP_LLVM_fragment");
Fangrui Song0cac7262018-09-27 02:13:45 +0000245 llvm::sort(FrameIndexExprs,
Mandeep Singh Grange92f0cf2018-04-06 18:08:42 +0000246 [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
247 return A.Expr->getFragmentInfo()->OffsetInBits <
248 B.Expr->getFragmentInfo()->OffsetInBits;
249 });
Bjorn Steinbrinkd36bbe92017-10-10 07:46:17 +0000250
Adrian Prantl67c24422017-02-17 19:42:32 +0000251 return FrameIndexExprs;
252}
253
Bjorn Steinbrinkd36bbe92017-10-10 07:46:17 +0000254void DbgVariable::addMMIEntry(const DbgVariable &V) {
255 assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry");
256 assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry");
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000257 assert(V.getVariable() == getVariable() && "conflicting variable");
258 assert(V.getInlinedAt() == getInlinedAt() && "conflicting inlined-at location");
Bjorn Steinbrinkd36bbe92017-10-10 07:46:17 +0000259
260 assert(!FrameIndexExprs.empty() && "Expected an MMI entry");
261 assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry");
262
Daniel Jasper4d931202017-10-12 13:25:05 +0000263 // FIXME: This logic should not be necessary anymore, as we now have proper
264 // deduplication. However, without it, we currently run into the assertion
265 // below, which means that we are likely dealing with broken input, i.e. two
266 // non-fragment entries for the same variable at different frame indices.
267 if (FrameIndexExprs.size()) {
268 auto *Expr = FrameIndexExprs.back().Expr;
269 if (!Expr || !Expr->isFragment())
270 return;
271 }
272
Bjorn Steinbrinkd36bbe92017-10-10 07:46:17 +0000273 for (const auto &FIE : V.FrameIndexExprs)
274 // Ignore duplicate entries.
275 if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) {
276 return FIE.FI == Other.FI && FIE.Expr == Other.Expr;
277 }))
278 FrameIndexExprs.push_back(FIE);
279
280 assert((FrameIndexExprs.size() == 1 ||
281 llvm::all_of(FrameIndexExprs,
282 [](FrameIndexExpr &FIE) {
283 return FIE.Expr && FIE.Expr->isFragment();
284 })) &&
285 "conflicting locations for variable");
286}
287
Pavel Labathf9adc202018-07-20 12:59:05 +0000288static AccelTableKind computeAccelTableKind(unsigned DwarfVersion,
289 bool GenerateTypeUnits,
290 DebuggerKind Tuning,
291 const Triple &TT) {
292 // Honor an explicit request.
293 if (AccelTables != AccelTableKind::Default)
294 return AccelTables;
295
296 // Accelerator tables with type units are currently not supported.
297 if (GenerateTypeUnits)
298 return AccelTableKind::None;
299
300 // Accelerator tables get emitted if targetting DWARF v5 or LLDB. DWARF v5
301 // always implies debug_names. For lower standard versions we use apple
302 // accelerator tables on apple platforms and debug_names elsewhere.
303 if (DwarfVersion >= 5)
304 return AccelTableKind::Dwarf;
305 if (Tuning == DebuggerKind::LLDB)
306 return TT.isOSBinFormatMachO() ? AccelTableKind::Apple
307 : AccelTableKind::Dwarf;
308 return AccelTableKind::None;
309}
310
Chris Lattnerf0d6bd32010-04-05 05:11:15 +0000311DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
Reid Klecknerf9c275f2016-02-10 20:55:49 +0000312 : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
313 InfoHolder(A, "info_string", DIEValueAllocator),
Frederic Riss9412d632015-03-04 02:30:17 +0000314 SkeletonHolder(A, "skel_string", DIEValueAllocator),
Pavel Labatha7c457d2018-02-19 16:12:20 +0000315 IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
Eric Christopher364dbe02016-10-01 01:50:29 +0000316 const Triple &TT = Asm->TM.getTargetTriple();
Eric Christopherad9fe892012-04-02 17:58:52 +0000317
Paul Robinson6c27a2c2015-12-16 19:58:30 +0000318 // Make sure we know our "debugger tuning." The target option takes
Paul Robinsonb9de1062015-07-15 22:04:54 +0000319 // precedence; fall back to triple-based defaults.
Paul Robinson6c27a2c2015-12-16 19:58:30 +0000320 if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
321 DebuggerTuning = Asm->TM.Options.DebuggerTuning;
Dimitry Andric2c364212016-01-07 22:09:12 +0000322 else if (IsDarwin)
Paul Robinsonb9de1062015-07-15 22:04:54 +0000323 DebuggerTuning = DebuggerKind::LLDB;
324 else if (TT.isPS4CPU())
325 DebuggerTuning = DebuggerKind::SCE;
326 else
327 DebuggerTuning = DebuggerKind::GDB;
328
Eric Christopher68f22182018-05-18 03:13:08 +0000329 if (DwarfInlinedStrings == Default)
330 UseInlineStrings = TT.isNVPTX();
331 else
332 UseInlineStrings = DwarfInlinedStrings == Enable;
333
Alexey Bataev2a03d422018-06-29 14:23:28 +0000334 UseLocSection = !TT.isNVPTX();
335
David Blaikiec53e18d2016-05-24 21:19:28 +0000336 HasAppleExtensionAttributes = tuneForLLDB();
337
David Blaikie96b1ed52017-04-21 23:35:26 +0000338 // Handle split DWARF.
339 HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty();
Eric Christopher29424312012-11-12 22:22:20 +0000340
Paul Robinson43d1e452016-04-18 22:41:41 +0000341 // SCE defaults to linkage names only for abstract subprograms.
342 if (DwarfLinkageNames == DefaultLinkageNames)
343 UseAllLinkageNames = !tuneForSCE();
Paul Robinson78046b42015-08-11 21:36:45 +0000344 else
Paul Robinson43d1e452016-04-18 22:41:41 +0000345 UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
Paul Robinson78046b42015-08-11 21:36:45 +0000346
Eric Christopher4c5bff32014-06-19 06:22:08 +0000347 unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
Greg Claytone6543972016-11-23 23:30:37 +0000348 unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
Eric Christopher793c7472014-04-28 20:42:22 +0000349 : MMI->getModule()->getDwarfVersion();
Eric Christopher68f22182018-05-18 03:13:08 +0000350 // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2.
351 DwarfVersion =
352 TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION);
Manman Renac8062b2013-07-02 23:40:10 +0000353
Eric Christopher68f22182018-05-18 03:13:08 +0000354 UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX();
Alexey Bataev648ed2d2018-03-20 16:04:40 +0000355
Eric Christopher68f22182018-05-18 03:13:08 +0000356 // Use sections as references. Force for NVPTX.
357 if (DwarfSectionsAsReferences == Default)
358 UseSectionsAsReferences = TT.isNVPTX();
359 else
360 UseSectionsAsReferences = DwarfSectionsAsReferences == Enable;
Alexey Bataevbff36082018-03-23 13:35:54 +0000361
Jonas Devlieghere8acb74e2018-08-01 12:53:06 +0000362 // Don't generate type units for unsupported object file formats.
363 GenerateTypeUnits =
364 A->TM.getTargetTriple().isOSBinFormatELF() && GenerateDwarfTypeUnits;
Pavel Labathf9adc202018-07-20 12:59:05 +0000365
366 TheAccelTableKind = computeAccelTableKind(
367 DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple());
368
Paul Robinsonb9de1062015-07-15 22:04:54 +0000369 // Work around a GDB bug. GDB doesn't support the standard opcode;
370 // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
371 // is defined as of DWARF 3.
372 // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
373 // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
374 UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
Paul Robinson78cc0822015-03-04 20:55:11 +0000375
Adrian Prantl6323ddf2016-05-17 21:07:16 +0000376 // GDB does not fully support the DWARF 4 representation for bitfields.
377 UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB();
378
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000379 // The DWARF v5 string offsets table has - possibly shared - contributions
380 // from each compile and type unit each preceded by a header. The string
381 // offsets table used by the pre-DWARF v5 split-DWARF implementation uses
382 // a monolithic string offsets table without any header.
383 UseSegmentedStringOffsetsTable = DwarfVersion >= 5;
384
Lang Hames9ff69c82015-04-24 19:11:51 +0000385 Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
Bill Wendling2f921f82009-05-15 09:23:25 +0000386}
Bill Wendling2f921f82009-05-15 09:23:25 +0000387
Reid Klecknerdd2647e2014-04-30 20:34:31 +0000388// Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000389DwarfDebug::~DwarfDebug() = default;
Reid Klecknerdd2647e2014-04-30 20:34:31 +0000390
Eric Christopherd9843b32011-11-10 19:25:34 +0000391static bool isObjCClass(StringRef Name) {
392 return Name.startswith("+") || Name.startswith("-");
393}
394
395static bool hasObjCCategory(StringRef Name) {
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000396 if (!isObjCClass(Name))
397 return false;
Eric Christopherd9843b32011-11-10 19:25:34 +0000398
Benjamin Kramer260de742013-08-24 12:15:54 +0000399 return Name.find(") ") != StringRef::npos;
Eric Christopherd9843b32011-11-10 19:25:34 +0000400}
401
402static void getObjCClassCategory(StringRef In, StringRef &Class,
403 StringRef &Category) {
404 if (!hasObjCCategory(In)) {
405 Class = In.slice(In.find('[') + 1, In.find(' '));
406 Category = "";
407 return;
408 }
409
410 Class = In.slice(In.find('[') + 1, In.find('('));
411 Category = In.slice(In.find('[') + 1, In.find(' '));
Eric Christopherd9843b32011-11-10 19:25:34 +0000412}
413
414static StringRef getObjCMethodName(StringRef In) {
415 return In.slice(In.find(' ') + 1, In.find(']'));
416}
417
418// Add the various names to the Dwarf accelerator table names.
David Blaikie66cf14d2018-08-16 21:29:55 +0000419void DwarfDebug::addSubprogramNames(const DICompileUnit &CU,
420 const DISubprogram *SP, DIE &Die) {
421 if (getAccelTableKind() != AccelTableKind::Apple &&
422 CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None)
423 return;
424
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000425 if (!SP->isDefinition())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000426 return;
Pavel Labath889bf9f2018-04-09 08:41:57 +0000427
428 if (SP->getName() != "")
David Blaikie66cf14d2018-08-16 21:29:55 +0000429 addAccelName(CU, SP->getName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000430
Pavel Labath2a6afe52018-05-14 14:13:20 +0000431 // If the linkage name is different than the name, go ahead and output that as
432 // well into the name table. Only do that if we are going to actually emit
433 // that name.
434 if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() &&
435 (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP)))
David Blaikie66cf14d2018-08-16 21:29:55 +0000436 addAccelName(CU, SP->getLinkageName(), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000437
438 // If this is an Objective-C selector name add it to the ObjC accelerator
439 // too.
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000440 if (isObjCClass(SP->getName())) {
Eric Christopherd9843b32011-11-10 19:25:34 +0000441 StringRef Class, Category;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000442 getObjCClassCategory(SP->getName(), Class, Category);
David Blaikie66cf14d2018-08-16 21:29:55 +0000443 addAccelObjC(CU, Class, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000444 if (Category != "")
David Blaikie66cf14d2018-08-16 21:29:55 +0000445 addAccelObjC(CU, Category, Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000446 // Also add the base method name to the name table.
David Blaikie66cf14d2018-08-16 21:29:55 +0000447 addAccelName(CU, getObjCMethodName(SP->getName()), Die);
Eric Christopherd9843b32011-11-10 19:25:34 +0000448 }
449}
450
Manman Ren5b2f4b02013-09-11 19:40:28 +0000451/// Check whether we should create a DIE for the given Scope, return true
452/// if we don't create a DIE (the corresponding DIE is null).
Manman Ren2312ed32013-09-10 18:40:41 +0000453bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
454 if (Scope->isAbstractScope())
455 return false;
456
Manman Ren5b2f4b02013-09-11 19:40:28 +0000457 // We don't create a DIE if there is no Range.
Manman Ren2312ed32013-09-10 18:40:41 +0000458 const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
459 if (Ranges.empty())
460 return true;
461
462 if (Ranges.size() > 1)
463 return false;
464
Manman Ren5b2f4b02013-09-11 19:40:28 +0000465 // We don't create a DIE if we have a single Range and the end label
466 // is null.
David Blaikiecd4b8a22014-08-31 02:14:26 +0000467 return !getLabelAfterInsn(Ranges.front().second);
Manman Ren2312ed32013-09-10 18:40:41 +0000468}
469
Benjamin Kramerb7d33112016-08-06 11:13:10 +0000470template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
David Blaikie3a443c22014-11-04 22:12:25 +0000471 F(CU);
472 if (auto *SkelCU = CU.getSkeleton())
David Blaikiea01f2952016-08-24 18:29:49 +0000473 if (CU.getCUNode()->getSplitDebugInlining())
474 F(*SkelCU);
David Blaikie3a443c22014-11-04 22:12:25 +0000475}
476
David Blaikie488393f2017-05-12 01:13:45 +0000477bool DwarfDebug::shareAcrossDWOCUs() const {
478 return SplitDwarfCrossCuReferences;
479}
480
481void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU,
482 LexicalScope *Scope) {
David Blaikiee872a6e2014-04-29 15:58:35 +0000483 assert(Scope && Scope->getScopeNode());
484 assert(Scope->isAbstractScope());
485 assert(!Scope->getInlinedAt());
486
David Blaikieb4614682016-12-14 19:38:39 +0000487 auto *SP = cast<DISubprogram>(Scope->getScopeNode());
David Blaikiee872a6e2014-04-29 15:58:35 +0000488
David Blaikie1ea9db22014-05-21 23:14:12 +0000489 // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
490 // was inlined from another compile unit.
Mehdi Amini41812052017-05-29 06:25:30 +0000491 if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining())
492 // Avoid building the original CU if it won't be used
493 SrcCU.constructAbstractSubprogramScopeDIE(Scope);
494 else {
495 auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
496 if (auto *SkelCU = CU.getSkeleton()) {
497 (shareAcrossDWOCUs() ? CU : SrcCU)
498 .constructAbstractSubprogramScopeDIE(Scope);
499 if (CU.getCUNode()->getSplitDebugInlining())
500 SkelCU->constructAbstractSubprogramScopeDIE(Scope);
501 } else
502 CU.constructAbstractSubprogramScopeDIE(Scope);
David Blaikie488393f2017-05-12 01:13:45 +0000503 }
David Blaikiee872a6e2014-04-29 15:58:35 +0000504}
505
Vedant Kumar5931b4e2018-10-05 20:37:17 +0000506void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
507 DwarfCompileUnit &CU, DIE &ScopeDIE,
508 const MachineFunction &MF) {
509 // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if
510 // the subprogram is required to have one.
511 if (!SP.areAllCallsDescribed() || !SP.isDefinition())
512 return;
513
514 // Use DW_AT_call_all_calls to express that call site entries are present
515 // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls
516 // because one of its requirements is not met: call site entries for
517 // optimized-out calls are elided.
518 CU.addFlag(ScopeDIE, dwarf::DW_AT_call_all_calls);
519
520 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
521 assert(TII && "TargetInstrInfo not found: cannot label tail calls");
522
523 // Emit call site entries for each call or tail call in the function.
524 for (const MachineBasicBlock &MBB : MF) {
525 for (const MachineInstr &MI : MBB.instrs()) {
526 // Skip instructions which aren't calls. Both calls and tail-calling jump
527 // instructions (e.g TAILJMPd64) are classified correctly here.
528 if (!MI.isCall())
529 continue;
530
531 // TODO: Add support for targets with delay slots (see: beginInstruction).
532 if (MI.hasDelaySlot())
533 return;
534
535 // If this is a direct call, find the callee's subprogram.
536 const MachineOperand &CalleeOp = MI.getOperand(0);
537 if (!CalleeOp.isGlobal())
538 continue;
539 const Function *CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal());
540 if (!CalleeDecl || !CalleeDecl->getSubprogram())
541 continue;
542
543 // TODO: Omit call site entries for runtime calls (objc_msgSend, etc).
544 // TODO: Add support for indirect calls.
545
546 bool IsTail = TII->isTailCall(MI);
547
548 // For tail calls, no return PC information is needed. For regular calls,
549 // the return PC is needed to disambiguate paths in the call graph which
550 // could lead to some target function.
551 const MCSymbol *ReturnPC = IsTail ? nullptr : getLabelAfterInsn(&MI);
552
553 assert((IsTail || ReturnPC) && "Call without return PC information");
554 LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> "
555 << CalleeDecl->getName() << (IsTail ? " [tail]" : "")
556 << "\n");
557 CU.constructCallSiteEntryDIE(ScopeDIE, *CalleeDecl->getSubprogram(),
558 IsTail, ReturnPC);
559 }
560 }
561}
562
David Blaikieb3cee2f2017-05-25 18:50:28 +0000563void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const {
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000564 if (!U.hasDwarfPubSections())
David Blaikie3c842622013-12-04 21:31:26 +0000565 return;
566
David Blaikief9b6a552014-04-22 22:39:41 +0000567 U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
David Blaikie3c842622013-12-04 21:31:26 +0000568}
569
Eric Christopher4287a492013-12-09 23:57:44 +0000570// Create new DwarfCompileUnit for the given metadata node with tag
Eric Christopher48fef592012-12-20 21:58:40 +0000571// DW_TAG_compile_unit.
Duncan P. N. Exon Smith2fbe1352015-04-20 22:10:08 +0000572DwarfCompileUnit &
David Blaikie07963bd2017-05-26 18:52:56 +0000573DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) {
574 if (auto *CU = CUMap.lookup(DIUnit))
575 return *CU;
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000576 StringRef FN = DIUnit->getFilename();
577 CompilationDir = DIUnit->getDirectory();
Bill Wendling2b128d72009-05-20 23:19:06 +0000578
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +0000579 auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
David Blaikiebd579052014-04-28 21:14:27 +0000580 InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
David Blaikief9b6a552014-04-22 22:39:41 +0000581 DwarfCompileUnit &NewCU = *OwnedUnit;
David Blaikie92a2f8a2014-04-28 21:04:29 +0000582 DIE &Die = NewCU.getUnitDie();
David Blaikief9b6a552014-04-22 22:39:41 +0000583 InfoHolder.addUnit(std::move(OwnedUnit));
David Blaikie0b214e42016-03-24 18:37:08 +0000584 if (useSplitDwarf()) {
David Blaikiecafd9622014-11-02 08:51:37 +0000585 NewCU.setSkeleton(constructSkeletonCU(NewCU));
David Blaikie0b214e42016-03-24 18:37:08 +0000586 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikie96b1ed52017-04-21 23:35:26 +0000587 Asm->TM.Options.MCOptions.SplitDwarfFile);
David Blaikie0b214e42016-03-24 18:37:08 +0000588 }
David Blaikief9b6a552014-04-22 22:39:41 +0000589
David Blaikie89daf772017-07-28 03:06:25 +0000590 for (auto *IE : DIUnit->getImportedEntities())
591 NewCU.addImportedEntity(IE);
592
David Blaikie7ac51492014-03-20 17:05:45 +0000593 // LTO with assembly output shares a single line table amongst multiple CUs.
594 // To avoid the compilation directory being ambiguous, let the line table
595 // explicitly describe the directory of all files, never relying on the
596 // compilation directory.
Lang Hames9ff69c82015-04-24 19:11:51 +0000597 if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
Paul Robinsonb271f312018-03-29 17:16:41 +0000598 Asm->OutStreamer->emitDwarfFile0Directive(
599 CompilationDir, FN, NewCU.getMD5AsBytes(DIUnit->getFile()),
600 DIUnit->getSource(), NewCU.getUniqueID());
David Blaikie2666e242013-12-06 19:38:46 +0000601
Eric Christopher9ea300f2017-03-29 23:34:27 +0000602 StringRef Producer = DIUnit->getProducer();
603 StringRef Flags = DIUnit->getFlags();
Jonas Devliegherecaacedb2018-08-08 16:33:22 +0000604 if (!Flags.empty() && !useAppleExtensionAttributes()) {
Eric Christopher9ea300f2017-03-29 23:34:27 +0000605 std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
606 NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
607 } else
608 NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
609
David Blaikie92a2f8a2014-04-28 21:04:29 +0000610 NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000611 DIUnit->getSourceLanguage());
David Blaikie92a2f8a2014-04-28 21:04:29 +0000612 NewCU.addString(Die, dwarf::DW_AT_name, FN);
Eric Christopher52ce7182013-04-09 19:23:15 +0000613
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000614 // Add DW_str_offsets_base to the unit DIE, except for split units.
615 if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
616 NewCU.addStringOffsetsStart();
617
Eric Christopher52ce7182013-04-09 19:23:15 +0000618 if (!useSplitDwarf()) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000619 NewCU.initStmtList();
Eric Christophera51d3fc2013-09-27 22:50:48 +0000620
621 // If we're using split dwarf the compilation dir is going to be in the
622 // skeleton CU and so we don't need to duplicate it here.
623 if (!CompilationDir.empty())
David Blaikie92a2f8a2014-04-28 21:04:29 +0000624 NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
Eric Christophera51d3fc2013-09-27 22:50:48 +0000625
David Blaikie92a2f8a2014-04-28 21:04:29 +0000626 addGnuPubAttributes(NewCU, Die);
Eric Christopher52ce7182013-04-09 19:23:15 +0000627 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000628
David Blaikiec53e18d2016-05-24 21:19:28 +0000629 if (useAppleExtensionAttributes()) {
630 if (DIUnit->isOptimized())
631 NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
Bill Wendling2b128d72009-05-20 23:19:06 +0000632
David Blaikiec53e18d2016-05-24 21:19:28 +0000633 StringRef Flags = DIUnit->getFlags();
634 if (!Flags.empty())
635 NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
Eric Christopher6a841382012-11-19 22:42:10 +0000636
David Blaikiec53e18d2016-05-24 21:19:28 +0000637 if (unsigned RVer = DIUnit->getRuntimeVersion())
638 NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
639 dwarf::DW_FORM_data1, RVer);
640 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000641
David Blaikiecafd9622014-11-02 08:51:37 +0000642 if (useSplitDwarf())
Greg Clayton35630c32016-12-01 18:56:29 +0000643 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
David Blaikiecafd9622014-11-02 08:51:37 +0000644 else
Greg Clayton35630c32016-12-01 18:56:29 +0000645 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
Eric Christopherd039baa2013-12-30 03:40:32 +0000646
Adrian Prantldeef90d2015-09-14 22:10:22 +0000647 if (DIUnit->getDWOId()) {
Adrian Prantl77fefeb2015-09-22 23:21:00 +0000648 // This CU is either a clang module DWO or a skeleton CU.
Adrian Prantldeef90d2015-09-14 22:10:22 +0000649 NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
650 DIUnit->getDWOId());
Adrian Prantl77fefeb2015-09-22 23:21:00 +0000651 if (!DIUnit->getSplitDebugFilename().empty())
652 // This is a prefabricated skeleton CU.
653 NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
654 DIUnit->getSplitDebugFilename());
Adrian Prantldeef90d2015-09-14 22:10:22 +0000655 }
656
David Majnemer0a16c222016-08-11 21:15:00 +0000657 CUMap.insert({DIUnit, &NewCU});
658 CUDieMap.insert({&Die, &NewCU});
Devang Pateleb1bb4e2011-08-16 22:09:43 +0000659 return NewCU;
Devang Patel1a0df9a2010-05-10 22:49:55 +0000660}
661
David Blaikie8912df12014-08-31 05:41:15 +0000662void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000663 const DIImportedEntity *N) {
David Blaikie2195e132017-07-27 00:06:53 +0000664 if (isa<DILocalScope>(N->getScope()))
665 return;
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000666 if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
667 D->addChild(TheCU.constructImportedEntityDIE(N));
David Blaikief55abea2013-04-22 06:12:31 +0000668}
669
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000670/// Sort and unique GVEs by comparing their fragment offset.
671static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
672sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
Fangrui Song0cac7262018-09-27 02:13:45 +0000673 llvm::sort(
674 GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
675 // Sort order: first null exprs, then exprs without fragment
676 // info, then sort by fragment offset in bits.
677 // FIXME: Come up with a more comprehensive comparator so
678 // the sorting isn't non-deterministic, and so the following
679 // std::unique call works correctly.
680 if (!A.Expr || !B.Expr)
681 return !!B.Expr;
682 auto FragmentA = A.Expr->getFragmentInfo();
683 auto FragmentB = B.Expr->getFragmentInfo();
684 if (!FragmentA || !FragmentB)
685 return !!FragmentB;
686 return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
687 });
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000688 GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
689 [](DwarfCompileUnit::GlobalExpr A,
690 DwarfCompileUnit::GlobalExpr B) {
691 return A.Expr == B.Expr;
692 }),
693 GVEs.end());
694 return GVEs;
695}
696
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000697// Emit all Dwarf sections that should come prior to the content. Create
698// global DIEs and emit initial debug info sections. This is invoked by
699// the target AsmPrinter.
Eric Christopher58f41952012-11-19 22:42:15 +0000700void DwarfDebug::beginModule() {
Matthias Braun9f15a792016-11-18 19:43:18 +0000701 NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName,
702 DWARFGroupDescription, TimePassesIsEnabled);
Devang Patel6c74a872010-04-27 19:46:33 +0000703 if (DisableDebugInfoPrinting)
704 return;
705
Eric Christopher58f41952012-11-19 22:42:15 +0000706 const Module *M = MMI->getModule();
707
Peter Collingbourneb3269862016-06-01 02:58:40 +0000708 unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
709 M->debug_compile_units_end());
Adrian Prantl5992a722016-04-08 22:43:03 +0000710 // Tell MMI whether we have debug info.
711 MMI->setDebugInfoAvailability(NumDebugCUs > 0);
712 SingleCU = NumDebugCUs == 1;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000713 DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
714 GVMap;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000715 for (const GlobalVariable &Global : M->globals()) {
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000716 SmallVector<DIGlobalVariableExpression *, 1> GVs;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000717 Global.getDebugInfo(GVs);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000718 for (auto *GVE : GVs)
719 GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000720 }
721
Wolfgang Pieb456b5552018-01-26 18:52:58 +0000722 // Create the symbol that designates the start of the unit's contribution
723 // to the string offsets table. In a split DWARF scenario, only the skeleton
724 // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol).
725 if (useSegmentedStringOffsetsTable())
726 (useSplitDwarf() ? SkeletonHolder : InfoHolder)
727 .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base"));
728
Wolfgang Piebfcf38102018-07-12 18:18:21 +0000729 // Create the symbol that designates the start of the DWARF v5 range list
730 // table. It is located past the header and before the offsets table.
David Blaikiec4af8bf2018-10-20 07:36:39 +0000731 if (getDwarfVersion() >= 5) {
Wolfgang Piebfcf38102018-07-12 18:18:21 +0000732 (useSplitDwarf() ? SkeletonHolder : InfoHolder)
733 .setRnglistsTableBaseSym(Asm->createTempSymbol("rnglists_table_base"));
David Blaikiec4af8bf2018-10-20 07:36:39 +0000734 if (useSplitDwarf())
735 InfoHolder.setRnglistsTableBaseSym(
736 Asm->createTempSymbol("rnglists_dwo_table_base"));
737 }
Wolfgang Piebfcf38102018-07-12 18:18:21 +0000738
George Rimar425f7512018-09-20 09:17:36 +0000739 // Create the symbol that points to the first entry following the debug
740 // address table (.debug_addr) header.
741 AddrPool.setLabel(Asm->createTempSymbol("addr_table_base"));
742
Adrian Prantl5992a722016-04-08 22:43:03 +0000743 for (DICompileUnit *CUNode : M->debug_compile_units()) {
David Blaikie89daf772017-07-28 03:06:25 +0000744 // FIXME: Move local imported entities into a list attached to the
745 // subprogram, then this search won't be needed and a
746 // getImportedEntities().empty() test should go below with the rest.
747 bool HasNonLocalImportedEntities = llvm::any_of(
748 CUNode->getImportedEntities(), [](const DIImportedEntity *IE) {
749 return !isa<DILocalScope>(IE->getScope());
750 });
751
752 if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() &&
753 CUNode->getRetainedTypes().empty() &&
754 CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
David Blaikie07963bd2017-05-26 18:52:56 +0000755 continue;
756
757 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000758
759 // Global Variables.
Adrian Prantl2116dd32017-08-19 01:15:06 +0000760 for (auto *GVE : CUNode->getGlobalVariables()) {
761 // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
762 // already know about the variable and it isn't adding a constant
763 // expression.
764 auto &GVMapEntry = GVMap[GVE->getVariable()];
765 auto *Expr = GVE->getExpression();
766 if (!GVMapEntry.size() || (Expr && Expr->isConstant()))
767 GVMapEntry.push_back({nullptr, Expr});
768 }
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000769 DenseSet<DIGlobalVariable *> Processed;
770 for (auto *GVE : CUNode->getGlobalVariables()) {
771 DIGlobalVariable *GV = GVE->getVariable();
772 if (Processed.insert(GV).second)
773 CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
774 }
775
Duncan P. N. Exon Smith9928a902015-04-20 18:52:06 +0000776 for (auto *Ty : CUNode->getEnumTypes()) {
Manman Renbd1628a2014-07-28 23:04:20 +0000777 // The enum types array by design contains pointers to
778 // MDNodes rather than DIRefs. Unique them here.
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000779 CU.getOrCreateTypeDIE(cast<DIType>(Ty));
Manman Renbd1628a2014-07-28 23:04:20 +0000780 }
Duncan P. N. Exon Smith9928a902015-04-20 18:52:06 +0000781 for (auto *Ty : CUNode->getRetainedTypes()) {
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000782 // The retained types array by design contains pointers to
783 // MDNodes rather than DIRefs. Unique them here.
Amjad Aboud72da9392016-04-30 01:44:07 +0000784 if (DIType *RT = dyn_cast<DIType>(Ty))
Adrian Prantl75819ae2016-04-15 15:57:41 +0000785 // There is no point in force-emitting a forward declaration.
786 CU.getOrCreateTypeDIE(RT);
Adrian Prantl0aa1aa22014-03-18 02:35:03 +0000787 }
David Blaikief55abea2013-04-22 06:12:31 +0000788 // Emit imported_modules last so that the relevant context is already
789 // available.
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000790 for (auto *IE : CUNode->getImportedEntities())
Amjad Aboud72da9392016-04-30 01:44:07 +0000791 constructAndAddImportedEntityDIE(CU, IE);
David Blaikiedc69ebb2013-03-11 23:39:23 +0000792 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000793}
794
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000795void DwarfDebug::finishEntityDefinitions() {
796 for (const auto &Entity : ConcreteEntities) {
797 DIE *Die = Entity->getDIE();
798 assert(Die);
David Blaikieeb1a2722014-06-13 22:18:23 +0000799 // FIXME: Consider the time-space tradeoff of just storing the unit pointer
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000800 // in the ConcreteEntities list, rather than looking it up again here.
David Blaikieeb1a2722014-06-13 22:18:23 +0000801 // DIE::getUnit isn't simple - it walks parent pointers, etc.
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000802 DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie());
David Blaikieeb1a2722014-06-13 22:18:23 +0000803 assert(Unit);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000804 Unit->finishEntityDefinition(Entity.get());
David Blaikieeb1a2722014-06-13 22:18:23 +0000805 }
806}
807
David Blaikief7221ad2014-05-27 18:37:43 +0000808void DwarfDebug::finishSubprogramDefinitions() {
David Blaikie07963bd2017-05-26 18:52:56 +0000809 for (const DISubprogram *SP : ProcessedSPNodes) {
810 assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
811 forBothCUs(
812 getOrCreateDwarfCompileUnit(SP->getUnit()),
813 [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); });
814 }
Eric Christopher960ac372012-11-22 00:59:49 +0000815}
816
817void DwarfDebug::finalizeModuleInfo() {
Rafael Espindola063d7252015-03-10 16:58:10 +0000818 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
819
David Blaikie3c2fff32014-05-27 18:37:48 +0000820 finishSubprogramDefinitions();
821
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000822 finishEntityDefinitions();
David Blaikieeb1a2722014-06-13 22:18:23 +0000823
Mehdi Amini96ab48f2017-05-29 06:32:34 +0000824 // Include the DWO file name in the hash if there's more than one CU.
825 // This handles ThinLTO's situation where imported CUs may very easily be
826 // duplicate with the same CU partially imported into another ThinLTO unit.
827 StringRef DWOName;
828 if (CUMap.size() > 1)
829 DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile;
830
Eric Christopherad10cb52013-12-04 23:24:38 +0000831 // Handle anything that needs to be done on a per-unit basis after
832 // all other generation.
David Blaikie1998a2e2014-11-01 01:11:19 +0000833 for (const auto &P : CUMap) {
834 auto &TheCU = *P.second;
Alexey Bataevd4dd7212018-08-01 19:38:20 +0000835 if (TheCU.getCUNode()->isDebugDirectivesOnly())
836 continue;
Eric Christopher60eb7692013-08-12 20:27:48 +0000837 // Emit DW_AT_containing_type attribute to connect types with their
838 // vtable holding type.
David Blaikie1998a2e2014-11-01 01:11:19 +0000839 TheCU.constructContainingTypeDIEs();
Eric Christopher60eb7692013-08-12 20:27:48 +0000840
Eric Christopher46e23432013-12-20 04:16:18 +0000841 // Add CU specific attributes if we need to add any.
David Blaikie1998a2e2014-11-01 01:11:19 +0000842 // If we're splitting the dwarf out now that we've got the entire
843 // CU then add the dwo id to it.
844 auto *SkCU = TheCU.getSkeleton();
845 if (useSplitDwarf()) {
846 // Emit a unique identifier for this CU.
Mehdi Amini96ab48f2017-05-29 06:32:34 +0000847 uint64_t ID =
848 DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie());
Paul Robinson543c0e12018-05-22 17:27:31 +0000849 if (getDwarfVersion() >= 5) {
850 TheCU.setDWOId(ID);
851 SkCU->setDWOId(ID);
852 } else {
853 TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
854 dwarf::DW_FORM_data8, ID);
855 SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
856 dwarf::DW_FORM_data8, ID);
857 }
George Rimar425f7512018-09-20 09:17:36 +0000858
David Blaikiec4af8bf2018-10-20 07:36:39 +0000859 if (getDwarfVersion() < 5 && !SkeletonHolder.getRangeLists().empty()) {
Rafael Espindola063d7252015-03-10 16:58:10 +0000860 const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
David Blaikie1998a2e2014-11-01 01:11:19 +0000861 SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
Rafael Espindola063d7252015-03-10 16:58:10 +0000862 Sym, Sym);
863 }
David Blaikie1998a2e2014-11-01 01:11:19 +0000864 }
Eric Christopherd8667202013-12-30 17:22:27 +0000865
David Blaikie1998a2e2014-11-01 01:11:19 +0000866 // If we have code split among multiple sections or non-contiguous
867 // ranges of code then emit a DW_AT_ranges attribute on the unit that will
868 // remain in the .o file, otherwise add a DW_AT_low_pc.
869 // FIXME: We should use ranges allow reordering of code ala
870 // .subsections_via_symbols in mach-o. This would mean turning on
871 // ranges for all subprogram DIEs for mach-o.
David Blaikiea8be0a72014-11-01 01:15:24 +0000872 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
David Blaikie161dd3c2018-10-20 06:02:15 +0000873
874 // We don't keep track of which addresses are used in which CU so this
875 // is a bit pessimistic under LTO.
876 if (!AddrPool.isEmpty())
877 U.addAddrTableBase();
878
David Blaikie5b02a192014-11-03 23:10:59 +0000879 if (unsigned NumRanges = TheCU.getRanges().size()) {
Alexey Bataev858a7dd2018-03-20 20:21:38 +0000880 if (NumRanges > 1 && useRangesSection())
David Blaikie1998a2e2014-11-01 01:11:19 +0000881 // A DW_AT_low_pc attribute may also be specified in combination with
882 // DW_AT_ranges to specify the default base address for use in
883 // location lists (see Section 2.6.2) and range lists (see Section
884 // 2.17.3).
885 U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
David Blaikie5b02a192014-11-03 23:10:59 +0000886 else
David Blaikie72d03ef2015-05-02 02:31:49 +0000887 U.setBaseAddress(TheCU.getRanges().front().getStart());
David Blaikie5b02a192014-11-03 23:10:59 +0000888 U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
Eric Christopher60eb7692013-08-12 20:27:48 +0000889 }
Amjad Aboudd7cfb482016-01-07 14:28:20 +0000890
David Blaikiec4af8bf2018-10-20 07:36:39 +0000891 if (getDwarfVersion() >= 5 && U.hasRangeLists())
Wolfgang Pieb9ea65082018-07-26 22:48:52 +0000892 U.addRnglistsBase();
893
Amjad Aboudd7cfb482016-01-07 14:28:20 +0000894 auto *CUNode = cast<DICompileUnit>(P.first);
Amjad Aboud8bbce8a2016-02-01 14:09:41 +0000895 // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
896 if (CUNode->getMacros())
897 U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
898 U.getMacroLabelBegin(),
899 TLOF.getDwarfMacinfoSection()->getBeginSymbol());
Eric Christopher60eb7692013-08-12 20:27:48 +0000900 }
901
Adrian Prantl833ad372017-07-26 18:48:32 +0000902 // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
903 for (auto *CUNode : MMI->getModule()->debug_compile_units())
Adrian Prantl960e7662017-07-27 15:24:20 +0000904 if (CUNode->getDWOId())
Adrian Prantl833ad372017-07-26 18:48:32 +0000905 getOrCreateDwarfCompileUnit(CUNode);
906
Eric Christopher60eb7692013-08-12 20:27:48 +0000907 // Compute DIE offsets and sizes.
Eric Christopherc8a310e2012-12-10 23:34:43 +0000908 InfoHolder.computeSizeAndOffsets();
909 if (useSplitDwarf())
910 SkeletonHolder.computeSizeAndOffsets();
Eric Christopher960ac372012-11-22 00:59:49 +0000911}
912
Eric Christopheracdcbdb2012-11-27 22:43:45 +0000913// Emit all Dwarf sections that should come after the content.
Eric Christopher960ac372012-11-22 00:59:49 +0000914void DwarfDebug::endModule() {
Craig Toppere73658d2014-04-28 04:05:08 +0000915 assert(CurFn == nullptr);
916 assert(CurMI == nullptr);
Eric Christopher960ac372012-11-22 00:59:49 +0000917
David Blaikie0dc623c2014-10-24 17:53:38 +0000918 // If we aren't actually generating debug info (check beginModule -
919 // conditionalized on !DisableDebugInfoPrinting and the presence of the
920 // llvm.dbg.cu metadata node)
Rafael Espindolaf1a13f52015-03-11 00:51:37 +0000921 if (!MMI->hasDebugInfo())
Eric Christopherb4bef6d2013-11-19 09:04:36 +0000922 return;
Eric Christopher960ac372012-11-22 00:59:49 +0000923
Eric Christopher960ac372012-11-22 00:59:49 +0000924 // Finalize the debug info for the module.
925 finalizeModuleInfo();
Bill Wendling2b128d72009-05-20 23:19:06 +0000926
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000927 emitDebugStr();
Eric Christopher9cd26af2013-09-20 23:22:52 +0000928
Rafael Espindola063d7252015-03-10 16:58:10 +0000929 if (useSplitDwarf())
930 emitDebugLocDWO();
931 else
932 // Emit info into a debug loc section.
933 emitDebugLoc();
934
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000935 // Corresponding abbreviations into a abbrev section.
936 emitAbbreviations();
Eric Christopher95198f502012-11-27 22:43:42 +0000937
Rafael Espindolaf1a13f52015-03-11 00:51:37 +0000938 // Emit all the DIEs into a debug info section.
939 emitDebugInfo();
940
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000941 // Emit info into a debug aranges section.
Eric Christopher02dbadb2014-02-14 01:26:55 +0000942 if (GenerateARangeSection)
943 emitDebugARanges();
Eric Christopher95198f502012-11-27 22:43:42 +0000944
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000945 // Emit info into a debug ranges section.
946 emitDebugRanges();
Eric Christopher95198f502012-11-27 22:43:42 +0000947
Amjad Aboudd7cfb482016-01-07 14:28:20 +0000948 // Emit info into a debug macinfo section.
949 emitDebugMacinfo();
950
Eric Christopher9a8f5ed2013-11-19 09:04:50 +0000951 if (useSplitDwarf()) {
952 emitDebugStrDWO();
Eric Christopher9c2ecd92012-11-30 23:59:06 +0000953 emitDebugInfoDWO();
Eric Christopher3c5a1912012-12-19 22:02:53 +0000954 emitDebugAbbrevDWO();
David Blaikie4a2f95f2014-03-18 01:17:26 +0000955 emitDebugLineDWO();
David Blaikie32e09de2018-10-20 08:12:36 +0000956 emitDebugRangesDWO();
Rafael Espindola063d7252015-03-10 16:58:10 +0000957 }
Bill Wendling2b128d72009-05-20 23:19:06 +0000958
David Blaikie161dd3c2018-10-20 06:02:15 +0000959 emitDebugAddr();
960
Eric Christophera876b822012-08-23 07:32:06 +0000961 // Emit info into the dwarf accelerator table sections.
Pavel Labath6088c232018-04-04 14:42:14 +0000962 switch (getAccelTableKind()) {
963 case AccelTableKind::Apple:
Eric Christopher4996c702011-11-07 09:24:32 +0000964 emitAccelNames();
965 emitAccelObjC();
966 emitAccelNamespaces();
967 emitAccelTypes();
Pavel Labath6088c232018-04-04 14:42:14 +0000968 break;
969 case AccelTableKind::Dwarf:
970 emitAccelDebugNames();
971 break;
972 case AccelTableKind::None:
973 break;
974 case AccelTableKind::Default:
975 llvm_unreachable("Default should have already been resolved.");
Eric Christopher4996c702011-11-07 09:24:32 +0000976 }
Eric Christopher6a841382012-11-19 22:42:10 +0000977
Eric Christopher4b358182013-08-30 00:40:17 +0000978 // Emit the pubnames and pubtypes sections if requested.
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000979 emitDebugPubSections();
Devang Patel04d2f2d2009-11-24 01:14:22 +0000980
Devang Pateld0701282010-08-02 17:32:15 +0000981 // clean up.
David Blaikie488393f2017-05-12 01:13:45 +0000982 // FIXME: AbstractVariables.clear();
Bill Wendling2b128d72009-05-20 23:19:06 +0000983}
984
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000985void DwarfDebug::ensureAbstractEntityIsCreated(DwarfCompileUnit &CU,
986 const DINode *Node,
987 const MDNode *ScopeNode) {
988 if (CU.getExistingAbstractEntity(Node))
David Blaikie6f9e8672014-06-13 23:52:55 +0000989 return;
David Blaikie36408e72014-06-04 23:50:52 +0000990
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000991 CU.createAbstractEntity(Node, LScopes.getOrCreateAbstractScope(
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000992 cast<DILocalScope>(ScopeNode)));
David Blaikie36408e72014-06-04 23:50:52 +0000993}
994
Hsiangkai Wang2532ac82018-08-17 15:22:04 +0000995void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
996 const DINode *Node, const MDNode *ScopeNode) {
997 if (CU.getExistingAbstractEntity(Node))
David Blaikie6f9e8672014-06-13 23:52:55 +0000998 return;
David Blaikie36408e72014-06-04 23:50:52 +0000999
Duncan P. N. Exon Smith33af7a82015-03-30 23:21:21 +00001000 if (LexicalScope *Scope =
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001001 LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001002 CU.createAbstractEntity(Node, Scope);
David Blaikie36408e72014-06-04 23:50:52 +00001003}
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001004
Matthias Braunef331ef2016-11-30 23:48:50 +00001005// Collect variable information from side table maintained by MF.
1006void DwarfDebug::collectVariableInfoFromMFTable(
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001007 DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) {
1008 SmallDenseMap<InlinedEntity, DbgVariable *> MFVars;
Matthias Braunef331ef2016-11-30 23:48:50 +00001009 for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +00001010 if (!VI.Var)
Eric Christopherb4bef6d2013-11-19 09:04:36 +00001011 continue;
Duncan P. N. Exon Smith78a95272015-04-16 22:12:59 +00001012 assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
1013 "Expected inlined-at fields to agree");
1014
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001015 InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt());
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001016 Processed.insert(Var);
Benjamin Kramer2abfd6c72014-03-09 15:44:39 +00001017 LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
Jim Grosbacha8683bb2010-07-21 21:21:52 +00001018
Devang Patelcdb7d442009-11-10 23:20:04 +00001019 // If variable scope is not found then skip this variable.
Craig Topper353eda42014-04-24 06:44:33 +00001020 if (!Scope)
Devang Patelcdb7d442009-11-10 23:20:04 +00001021 continue;
Devang Patelf6eeaeb2009-11-10 23:06:00 +00001022
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001023 ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode());
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001024 auto RegVar = llvm::make_unique<DbgVariable>(
1025 cast<DILocalVariable>(Var.first), Var.second);
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001026 RegVar->initializeMMI(VI.Expr, VI.Slot);
Adrian Prantlbe662712017-07-25 23:32:59 +00001027 if (DbgVariable *DbgVar = MFVars.lookup(Var))
1028 DbgVar->addMMIEntry(*RegVar);
1029 else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) {
1030 MFVars.insert({Var, RegVar.get()});
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001031 ConcreteEntities.push_back(std::move(RegVar));
Adrian Prantlbe662712017-07-25 23:32:59 +00001032 }
Devang Patel475d32a2009-10-06 01:26:37 +00001033 }
Devang Patel490c8ab2010-05-20 19:57:06 +00001034}
Devang Patela3e9c9c2010-03-15 18:33:46 +00001035
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001036// Get .debug_loc entry for the instruction range starting at MI.
Adrian Prantle19e5ef2014-04-27 18:25:40 +00001037static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001038 const DIExpression *Expr = MI->getDebugExpression();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001039 assert(MI->getNumOperands() == 4);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001040 if (MI->getOperand(0).isReg()) {
Adrian Prantlbd6d2912017-08-02 17:19:13 +00001041 auto RegOp = MI->getOperand(0);
1042 auto Op1 = MI->getOperand(1);
Adrian Prantl418d1d12013-07-09 20:28:37 +00001043 // If the second operand is an immediate, this is a
1044 // register-indirect address.
Adrian Prantlbd6d2912017-08-02 17:19:13 +00001045 assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset");
1046 MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +00001047 return DebugLocEntry::Value(Expr, MLoc);
Devang Patel2442a892011-07-08 17:09:57 +00001048 }
1049 if (MI->getOperand(0).isImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +00001050 return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
Devang Patel2442a892011-07-08 17:09:57 +00001051 if (MI->getOperand(0).isFPImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +00001052 return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
Devang Patel2442a892011-07-08 17:09:57 +00001053 if (MI->getOperand(0).isCImm())
Duncan P. N. Exon Smith546c8be2015-04-17 16:33:37 +00001054 return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
Devang Patel2442a892011-07-08 17:09:57 +00001055
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001056 llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
Devang Patel2442a892011-07-08 17:09:57 +00001057}
1058
Adrian Prantl5f8f34e42018-05-01 15:54:18 +00001059/// If this and Next are describing different fragments of the same
Keno Fischerf8eb6a12016-01-16 01:11:33 +00001060/// variable, merge them by appending Next's values to the current
1061/// list of values.
1062/// Return true if the merge was successful.
1063bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) {
1064 if (Begin == Next.Begin) {
Keno Fischer6c1e47a2016-02-03 21:13:33 +00001065 auto *FirstExpr = cast<DIExpression>(Values[0].Expression);
1066 auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression);
Adrian Prantl941fa752016-12-05 18:04:47 +00001067 if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment())
Keno Fischer6c1e47a2016-02-03 21:13:33 +00001068 return false;
1069
Adrian Prantl941fa752016-12-05 18:04:47 +00001070 // We can only merge entries if none of the fragments overlap any others.
Keno Fischer6c1e47a2016-02-03 21:13:33 +00001071 // In doing so, we can take advantage of the fact that both lists are
1072 // sorted.
1073 for (unsigned i = 0, j = 0; i < Values.size(); ++i) {
1074 for (; j < Next.Values.size(); ++j) {
Bjorn Petterssona223f8152018-03-12 18:02:39 +00001075 int res = cast<DIExpression>(Values[i].Expression)->fragmentCmp(
Reid Klecknerf9c275f2016-02-10 20:55:49 +00001076 cast<DIExpression>(Next.Values[j].Expression));
Keno Fischer6c1e47a2016-02-03 21:13:33 +00001077 if (res == 0) // The two expressions overlap, we can't merge.
1078 return false;
1079 // Values[i] is entirely before Next.Values[j],
1080 // so go back to the next entry of Values.
1081 else if (res == -1)
1082 break;
1083 // Next.Values[j] is entirely before Values[i], so go on to the
1084 // next entry of Next.Values.
1085 }
Keno Fischerf8eb6a12016-01-16 01:11:33 +00001086 }
Keno Fischer6c1e47a2016-02-03 21:13:33 +00001087
1088 addValues(Next.Values);
1089 End = Next.End;
1090 return true;
Keno Fischerf8eb6a12016-01-16 01:11:33 +00001091 }
1092 return false;
1093}
1094
Adrian Prantlb1416832014-08-01 22:11:58 +00001095/// Build the location list for all DBG_VALUEs in the function that
1096/// describe the same variable. If the ranges of several independent
Adrian Prantl941fa752016-12-05 18:04:47 +00001097/// fragments of the same variable overlap partially, split them up and
Adrian Prantlb1416832014-08-01 22:11:58 +00001098/// combine the ranges. The resulting DebugLocEntries are will have
1099/// strict monotonically increasing begin addresses and will never
1100/// overlap.
1101//
1102// Input:
1103//
Adrian Prantl941fa752016-12-05 18:04:47 +00001104// Ranges History [var, loc, fragment ofs size]
1105// 0 | [x, (reg0, fragment 0, 32)]
1106// 1 | | [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry
Adrian Prantlb1416832014-08-01 22:11:58 +00001107// 2 | | ...
1108// 3 | [clobber reg0]
Adrian Prantl941fa752016-12-05 18:04:47 +00001109// 4 [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of
Eric Christopherffc5ff32015-02-17 20:02:28 +00001110// x.
Adrian Prantlb1416832014-08-01 22:11:58 +00001111//
1112// Output:
1113//
Adrian Prantl941fa752016-12-05 18:04:47 +00001114// [0-1] [x, (reg0, fragment 0, 32)]
1115// [1-3] [x, (reg0, fragment 0, 32), (reg1, fragment 32, 32)]
1116// [3-4] [x, (reg1, fragment 32, 32)]
1117// [4- ] [x, (mem, fragment 0, 64)]
David Blaikiee1a26a62014-08-05 23:14:16 +00001118void
1119DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
1120 const DbgValueHistoryMap::InstrRanges &Ranges) {
Adrian Prantlcaaf0532014-08-11 21:05:57 +00001121 SmallVector<DebugLocEntry::Value, 4> OpenRanges;
Adrian Prantlb1416832014-08-01 22:11:58 +00001122
1123 for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
1124 const MachineInstr *Begin = I->first;
1125 const MachineInstr *End = I->second;
1126 assert(Begin->isDebugValue() && "Invalid History entry");
1127
1128 // Check if a variable is inaccessible in this range.
Adrian Prantl5e1fa852014-08-12 21:55:58 +00001129 if (Begin->getNumOperands() > 1 &&
1130 Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
Adrian Prantlb1416832014-08-01 22:11:58 +00001131 OpenRanges.clear();
1132 continue;
1133 }
1134
Adrian Prantl941fa752016-12-05 18:04:47 +00001135 // If this fragment overlaps with any open ranges, truncate them.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001136 const DIExpression *DIExpr = Begin->getDebugExpression();
David Majnemer0a16c222016-08-11 21:15:00 +00001137 auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) {
Bjorn Petterssona223f8152018-03-12 18:02:39 +00001138 return DIExpr->fragmentsOverlap(R.getExpression());
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001139 });
Adrian Prantlb1416832014-08-01 22:11:58 +00001140 OpenRanges.erase(Last, OpenRanges.end());
1141
1142 const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
1143 assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
1144
1145 const MCSymbol *EndLabel;
1146 if (End != nullptr)
1147 EndLabel = getLabelAfterInsn(End);
1148 else if (std::next(I) == Ranges.end())
Rafael Espindola07c03d32015-03-05 02:05:42 +00001149 EndLabel = Asm->getFunctionEnd();
Adrian Prantlb1416832014-08-01 22:11:58 +00001150 else
1151 EndLabel = getLabelBeforeInsn(std::next(I)->first);
1152 assert(EndLabel && "Forgot label after instruction ending a range!");
1153
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001154 LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
Adrian Prantlb1416832014-08-01 22:11:58 +00001155
1156 auto Value = getDebugLocValue(Begin);
David Blaikiee1a26a62014-08-05 23:14:16 +00001157 DebugLocEntry Loc(StartLabel, EndLabel, Value);
Adrian Prantle09ee3f2014-08-11 20:59:28 +00001158 bool couldMerge = false;
1159
Adrian Prantl941fa752016-12-05 18:04:47 +00001160 // If this is a fragment, it may belong to the current DebugLocEntry.
1161 if (DIExpr->isFragment()) {
Adrian Prantle09ee3f2014-08-11 20:59:28 +00001162 // Add this value to the list of open ranges.
Adrian Prantlcaaf0532014-08-11 21:05:57 +00001163 OpenRanges.push_back(Value);
Adrian Prantle09ee3f2014-08-11 20:59:28 +00001164
Adrian Prantl941fa752016-12-05 18:04:47 +00001165 // Attempt to add the fragment to the last entry.
Adrian Prantle09ee3f2014-08-11 20:59:28 +00001166 if (!DebugLoc.empty())
1167 if (DebugLoc.back().MergeValues(Loc))
1168 couldMerge = true;
1169 }
1170
1171 if (!couldMerge) {
1172 // Need to add a new DebugLocEntry. Add all values from still
Adrian Prantl941fa752016-12-05 18:04:47 +00001173 // valid non-overlapping fragments.
Adrian Prantl1c6f2ec2014-08-11 21:06:00 +00001174 if (OpenRanges.size())
1175 Loc.addValues(OpenRanges);
1176
Adrian Prantlb1416832014-08-01 22:11:58 +00001177 DebugLoc.push_back(std::move(Loc));
1178 }
Adrian Prantle09ee3f2014-08-11 20:59:28 +00001179
1180 // Attempt to coalesce the ranges of two otherwise identical
1181 // DebugLocEntries.
1182 auto CurEntry = DebugLoc.rbegin();
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001183 LLVM_DEBUG({
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001184 dbgs() << CurEntry->getValues().size() << " Values:\n";
Adrian Prantl6f8c1b62015-05-26 20:06:51 +00001185 for (auto &Value : CurEntry->getValues())
Adrian Prantldba58fb2016-02-29 22:28:22 +00001186 Value.dump();
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001187 dbgs() << "-----\n";
1188 });
Adrian Prantl75707312015-05-26 20:06:48 +00001189
1190 auto PrevEntry = std::next(CurEntry);
1191 if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
1192 DebugLoc.pop_back();
Adrian Prantlb1416832014-08-01 22:11:58 +00001193 }
1194}
1195
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001196DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU,
1197 LexicalScope &Scope,
1198 const DINode *Node,
1199 const DILocation *Location,
1200 const MCSymbol *Sym) {
1201 ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode());
1202 if (isa<const DILocalVariable>(Node)) {
1203 ConcreteEntities.push_back(
1204 llvm::make_unique<DbgVariable>(cast<const DILocalVariable>(Node),
1205 Location));
1206 InfoHolder.addScopeVariable(&Scope,
1207 cast<DbgVariable>(ConcreteEntities.back().get()));
1208 } else if (isa<const DILabel>(Node)) {
1209 ConcreteEntities.push_back(
1210 llvm::make_unique<DbgLabel>(cast<const DILabel>(Node),
1211 Location, Sym));
1212 InfoHolder.addScopeLabel(&Scope,
1213 cast<DbgLabel>(ConcreteEntities.back().get()));
1214 }
1215 return ConcreteEntities.back().get();
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001216}
Adrian Prantlb1416832014-08-01 22:11:58 +00001217
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001218/// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
1219/// enclosing lexical scope. The check ensures there are no other instructions
1220/// in the same lexical scope preceding the DBG_VALUE and that its range is
1221/// either open or otherwise rolls off the end of the scope.
1222static bool validThroughout(LexicalScopes &LScopes,
1223 const MachineInstr *DbgValue,
1224 const MachineInstr *RangeEnd) {
1225 assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
1226 auto MBB = DbgValue->getParent();
1227 auto DL = DbgValue->getDebugLoc();
1228 auto *LScope = LScopes.findLexicalScope(DL);
1229 // Scope doesn't exist; this is a dead DBG_VALUE.
1230 if (!LScope)
Adrian Prantlfb2add22016-02-29 19:49:46 +00001231 return false;
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001232 auto &LSRange = LScope->getRanges();
1233 if (LSRange.size() == 0)
1234 return false;
1235
1236 // Determine if the DBG_VALUE is valid at the beginning of its lexical block.
1237 const MachineInstr *LScopeBegin = LSRange.front().first;
1238 // Early exit if the lexical scope begins outside of the current block.
1239 if (LScopeBegin->getParent() != MBB)
1240 return false;
1241 MachineBasicBlock::const_reverse_iterator Pred(DbgValue);
1242 for (++Pred; Pred != MBB->rend(); ++Pred) {
1243 if (Pred->getFlag(MachineInstr::FrameSetup))
1244 break;
1245 auto PredDL = Pred->getDebugLoc();
Adrian Prantl25422dc2017-06-20 21:08:52 +00001246 if (!PredDL || Pred->isMetaInstruction())
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001247 continue;
1248 // Check whether the instruction preceding the DBG_VALUE is in the same
1249 // (sub)scope as the DBG_VALUE.
Adrian Prantl25422dc2017-06-20 21:08:52 +00001250 if (DL->getScope() == PredDL->getScope())
1251 return false;
1252 auto *PredScope = LScopes.findLexicalScope(PredDL);
1253 if (!PredScope || LScope->dominates(PredScope))
Adrian Prantlfb2add22016-02-29 19:49:46 +00001254 return false;
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001255 }
1256
1257 // If the range of the DBG_VALUE is open-ended, report success.
1258 if (!RangeEnd)
1259 return true;
1260
1261 // Fail if there are instructions belonging to our scope in another block.
1262 const MachineInstr *LScopeEnd = LSRange.back().second;
1263 if (LScopeEnd->getParent() != MBB)
1264 return false;
1265
1266 // Single, constant DBG_VALUEs in the prologue are promoted to be live
1267 // throughout the function. This is a hack, presumably for DWARF v2 and not
1268 // necessarily correct. It would be much better to use a dbg.declare instead
1269 // if we know the constant is live throughout the scope.
1270 if (DbgValue->getOperand(0).isImm() && MBB->pred_empty())
1271 return true;
1272
1273 return false;
Adrian Prantlfb2add22016-02-29 19:49:46 +00001274}
1275
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001276// Find variables for each lexical scope.
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001277void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
1278 const DISubprogram *SP,
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001279 DenseSet<InlinedEntity> &Processed) {
Eric Christopher270a12c2013-07-03 21:37:03 +00001280 // Grab the variable info that was squirreled away in the MMI side-table.
David Blaikie488393f2017-05-12 01:13:45 +00001281 collectVariableInfoFromMFTable(TheCU, Processed);
Devang Patel490c8ab2010-05-20 19:57:06 +00001282
Alexey Samsonov0436caa2014-04-30 23:02:40 +00001283 for (const auto &I : DbgValues) {
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001284 InlinedEntity IV = I.first;
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001285 if (Processed.count(IV))
Devang Patel490c8ab2010-05-20 19:57:06 +00001286 continue;
1287
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001288 // Instruction ranges, specifying where IV is accessible.
Alexey Samsonovbb2990d2014-05-27 23:09:50 +00001289 const auto &Ranges = I.second;
1290 if (Ranges.empty())
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001291 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00001292
Craig Topper353eda42014-04-24 06:44:33 +00001293 LexicalScope *Scope = nullptr;
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001294 const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001295 if (const DILocation *IA = IV.second)
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001296 Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA);
Duncan P. N. Exon Smithb4749372015-02-17 00:02:27 +00001297 else
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001298 Scope = LScopes.findLexicalScope(LocalVar->getScope());
Devang Patel490c8ab2010-05-20 19:57:06 +00001299 // If variable scope is not found then skip this variable.
Devang Patelfbd6c452010-05-21 00:10:20 +00001300 if (!Scope)
Devang Patel490c8ab2010-05-20 19:57:06 +00001301 continue;
1302
Duncan P. N. Exon Smith62e0f452015-04-15 22:29:27 +00001303 Processed.insert(IV);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001304 DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU,
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001305 *Scope, LocalVar, IV.second));
Duncan P. N. Exon Smithe6cc5312015-06-21 16:50:43 +00001306
Alexey Samsonovbb2990d2014-05-27 23:09:50 +00001307 const MachineInstr *MInsn = Ranges.front().first;
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001308 assert(MInsn->isDebugValue() && "History must begin with debug value");
Jakob Stoklund Olesen9a624fa2011-03-26 02:19:36 +00001309
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001310 // Check if there is a single DBG_VALUE, valid throughout the var's scope.
Adrian Prantlfb2add22016-02-29 19:49:46 +00001311 if (Ranges.size() == 1 &&
Adrian Prantl274bcbc2017-06-16 22:40:04 +00001312 validThroughout(LScopes, MInsn, Ranges.front().second)) {
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001313 RegVar->initializeDbgValue(MInsn);
Devang Patel9fc11702010-05-25 23:40:22 +00001314 continue;
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001315 }
Alexey Bataev2a03d422018-06-29 14:23:28 +00001316 // Do not emit location lists if .debug_loc secton is disabled.
1317 if (!useLocSection())
1318 continue;
Devang Patel9fc11702010-05-25 23:40:22 +00001319
Eric Christopher59cc0712013-01-28 17:33:26 +00001320 // Handle multiple DBG_VALUE instructions describing one variable.
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001321 DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
Jakob Stoklund Olesen9c057ee2011-03-22 00:21:41 +00001322
Adrian Prantlb1416832014-08-01 22:11:58 +00001323 // Build the location list for this variable.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001324 SmallVector<DebugLocEntry, 8> Entries;
1325 buildLocationList(Entries, Ranges);
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001326
Adrian Prantl693e8de2016-02-29 17:06:46 +00001327 // If the variable has a DIBasicType, extract it. Basic types cannot have
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001328 // unique identifiers, so don't bother resolving the type with the
1329 // identifier map.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001330 const DIBasicType *BT = dyn_cast<DIBasicType>(
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001331 static_cast<const Metadata *>(LocalVar->getType()));
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001332
Adrian Prantl92da14b2015-03-02 22:02:33 +00001333 // Finalize the entry by lowering it into a DWARF bytestream.
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001334 for (auto &Entry : Entries)
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001335 Entry.finalize(*Asm, List, BT);
Devang Patela3e9c9c2010-03-15 18:33:46 +00001336 }
Devang Patele0a94bf2010-05-14 21:01:35 +00001337
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001338 // For each InlinedEntity collected from DBG_LABEL instructions, convert to
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001339 // DWARF-related DbgLabel.
1340 for (const auto &I : DbgLabels) {
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001341 InlinedEntity IL = I.first;
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001342 const MachineInstr *MI = I.second;
1343 if (MI == nullptr)
1344 continue;
1345
1346 LexicalScope *Scope = nullptr;
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001347 const DILabel *Label = cast<DILabel>(IL.first);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001348 // Get inlined DILocation if it is inlined label.
1349 if (const DILocation *IA = IL.second)
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001350 Scope = LScopes.findInlinedScope(Label->getScope(), IA);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001351 else
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001352 Scope = LScopes.findLexicalScope(Label->getScope());
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001353 // If label scope is not found then skip this label.
1354 if (!Scope)
1355 continue;
1356
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001357 Processed.insert(IL);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001358 /// At this point, the temporary label is created.
1359 /// Save the temporary label to DbgLabel entity to get the
1360 /// actually address when generating Dwarf DIE.
1361 MCSymbol *Sym = getLabelBeforeInsn(MI);
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001362 createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym);
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001363 }
1364
1365 // Collect info for variables/labels that were optimized out.
Shiva Chen2c864552018-05-09 02:40:45 +00001366 for (const DINode *DN : SP->getRetainedNodes()) {
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001367 if (!Processed.insert(InlinedEntity(DN, nullptr)).second)
1368 continue;
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001369 LexicalScope *Scope = nullptr;
Shiva Chen2c864552018-05-09 02:40:45 +00001370 if (auto *DV = dyn_cast<DILocalVariable>(DN)) {
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001371 Scope = LScopes.findLexicalScope(DV->getScope());
1372 } else if (auto *DL = dyn_cast<DILabel>(DN)) {
1373 Scope = LScopes.findLexicalScope(DL->getScope());
Shiva Chen2c864552018-05-09 02:40:45 +00001374 }
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001375
1376 if (Scope)
1377 createConcreteEntity(TheCU, *Scope, DN, nullptr);
Devang Patele0a94bf2010-05-14 21:01:35 +00001378 }
Devang Patel9fc11702010-05-25 23:40:22 +00001379}
Devang Patele0a94bf2010-05-14 21:01:35 +00001380
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001381// Process beginning of an instruction.
Devang Patelb5694e72010-10-26 17:49:02 +00001382void DwarfDebug::beginInstruction(const MachineInstr *MI) {
Reid Klecknerf9c275f2016-02-10 20:55:49 +00001383 DebugHandlerBase::beginInstruction(MI);
1384 assert(CurMI);
1385
Matthias Braunf1caa282017-12-15 22:22:58 +00001386 const auto *SP = MI->getMF()->getFunction().getSubprogram();
David Blaikie7f2b7172017-05-26 17:05:15 +00001387 if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
1388 return;
1389
Paul Robinson4fa7b57a2016-12-09 19:15:32 +00001390 // Check if source location changes, but ignore DBG_VALUE and CFI locations.
Paul Robinsonee88ed62018-02-14 17:35:52 +00001391 // If the instruction is part of the function frame setup code, do not emit
1392 // any line record, as there is no correspondence with any user code.
1393 if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup))
Paul Robinsonf428c9b2016-11-22 19:46:51 +00001394 return;
1395 const DebugLoc &DL = MI->getDebugLoc();
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001396 // When we emit a line-0 record, we don't update PrevInstLoc; so look at
1397 // the last line number actually emitted, to see if it was line 0.
1398 unsigned LastAsmLine =
1399 Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
Paul Robinson96de8c72016-11-29 22:41:16 +00001400
Vedant Kumar5931b4e2018-10-05 20:37:17 +00001401 // Request a label after the call in order to emit AT_return_pc information
1402 // in call site entries. TODO: Add support for targets with delay slots.
1403 if (SP->areAllCallsDescribed() && MI->isCall() && !MI->hasDelaySlot())
1404 requestLabelAfterInsn(MI);
1405
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001406 if (DL == PrevInstLoc) {
1407 // If we have an ongoing unspecified location, nothing to do here.
1408 if (!DL)
1409 return;
1410 // We have an explicit location, same as the previous location.
1411 // But we might be coming back to it after a line 0 record.
1412 if (LastAsmLine == 0 && DL.getLine() != 0) {
1413 // Reinstate the source location but not marked as a statement.
1414 const MDNode *Scope = DL.getScope();
1415 recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0);
Paul Robinson37a13dd2016-11-30 22:49:55 +00001416 }
1417 return;
1418 }
1419
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001420 if (!DL) {
1421 // We have an unspecified location, which might want to be line 0.
1422 // If we have already emitted a line-0 record, don't repeat it.
1423 if (LastAsmLine == 0)
1424 return;
1425 // If user said Don't Do That, don't do that.
1426 if (UnknownLocations == Disable)
1427 return;
Matthias Braunc7efb6f2018-10-11 23:37:58 +00001428 // See if we have a reason to emit a line-0 record now.
1429 // Reasons to emit a line-0 record include:
1430 // - User asked for it (UnknownLocations).
1431 // - Instruction has a label, so it's referenced from somewhere else,
1432 // possibly debug information; we want it to have a source location.
1433 // - Instruction is at the top of a block; we don't want to inherit the
1434 // location from the physically previous (maybe unrelated) block.
1435 if (UnknownLocations == Enable || PrevLabel ||
1436 (PrevInstBB && PrevInstBB != MI->getParent())) {
1437 // Preserve the file and column numbers, if we can, to save space in
1438 // the encoded line table.
1439 // Do not update PrevInstLoc, it remembers the last non-0 line.
1440 const MDNode *Scope = nullptr;
1441 unsigned Column = 0;
1442 if (PrevInstLoc) {
1443 Scope = PrevInstLoc.getScope();
1444 Column = PrevInstLoc.getCol();
1445 }
1446 recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
Paul Robinsonac7fe5e2016-12-12 20:49:11 +00001447 }
1448 return;
1449 }
1450
Matthias Braunc7efb6f2018-10-11 23:37:58 +00001451 // We have an explicit location, different from the previous location.
1452 // Don't repeat a line-0 record, but otherwise emit the new location.
1453 // (The new location might be an explicit line 0, which we do emit.)
1454 if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0)
1455 return;
1456 unsigned Flags = 0;
1457 if (DL == PrologEndLoc) {
1458 Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
1459 PrologEndLoc = DebugLoc();
1460 }
1461 // If the line changed, we call that a new statement; unless we went to
1462 // line 0 and came back, in which case it is not a new statement.
1463 unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
1464 if (DL.getLine() && DL.getLine() != OldLine)
1465 Flags |= DWARF2_FLAG_IS_STMT;
1466
1467 const MDNode *Scope = DL.getScope();
1468 recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
1469
1470 // If we're not at line 0, remember this location.
1471 if (DL.getLine())
1472 PrevInstLoc = DL;
Devang Patel75cc16c2009-10-01 20:31:14 +00001473}
1474
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001475static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
1476 // First known non-DBG_VALUE and non-frame setup location marks
1477 // the beginning of the function body.
1478 for (const auto &MBB : *MF)
1479 for (const auto &MI : MBB)
Adrian Prantlfb31da12017-05-22 20:47:09 +00001480 if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
Michael Kuperstein2b3c16c2015-10-08 07:48:49 +00001481 MI.getDebugLoc())
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001482 return MI.getDebugLoc();
1483 return DebugLoc();
1484}
1485
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001486// Gather pre-function debug information. Assumes being called immediately
1487// after the function entry point has been emitted.
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001488void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001489 CurFn = MF;
Eric Christopherfedfa442013-11-01 23:14:17 +00001490
Matthias Braunf1caa282017-12-15 22:22:58 +00001491 auto *SP = MF->getFunction().getSubprogram();
David Blaikie2c78f182017-05-25 23:11:28 +00001492 assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode());
David Blaikie07963bd2017-05-26 18:52:56 +00001493 if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
Adrian Prantl5992a722016-04-08 22:43:03 +00001494 return;
David Blaikie07963bd2017-05-26 18:52:56 +00001495
1496 DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
David Blaikie2c78f182017-05-25 23:11:28 +00001497
1498 // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
1499 // belongs to so that we add to the correct per-cu line table in the
1500 // non-asm case.
Lang Hames9ff69c82015-04-24 19:11:51 +00001501 if (Asm->OutStreamer->hasRawTextSupport())
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001502 // Use a single line table if we are generating assembly.
Lang Hames9ff69c82015-04-24 19:11:51 +00001503 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
Manman Ren9d4c7352013-05-21 00:57:22 +00001504 else
David Blaikie07963bd2017-05-26 18:52:56 +00001505 Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID());
Manman Ren4e042a62013-02-05 21:52:47 +00001506
Devang Patel34a66202011-05-11 19:22:19 +00001507 // Record beginning of function.
Alexey Samsonov8a86d6d2014-05-27 22:47:41 +00001508 PrologEndLoc = findPrologueEndLoc(MF);
David Blaikie2c78f182017-05-25 23:11:28 +00001509 if (PrologEndLoc) {
Saleem Abdulrasoolfc07c722015-01-24 20:19:45 +00001510 // We'd like to list the prologue as "not statements" but GDB behaves
1511 // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
David Blaikie2c78f182017-05-25 23:11:28 +00001512 auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
Duncan P. N. Exon Smithfd07a2a2015-03-30 21:32:28 +00001513 recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
Devang Patel34a66202011-05-11 19:22:19 +00001514 }
Bill Wendling2b128d72009-05-20 23:19:06 +00001515}
1516
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001517void DwarfDebug::skippedNonDebugFunction() {
1518 // If we don't have a subprogram for this function then there will be a hole
1519 // in the range information. Keep note of this by setting the previously used
1520 // section to nullptr.
1521 PrevCU = nullptr;
1522 CurFn = nullptr;
1523}
1524
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001525// Gather and emit post-function debug information.
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001526void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +00001527 const DISubprogram *SP = MF->getFunction().getSubprogram();
David Blaikieb2fbb4b2017-02-16 18:48:33 +00001528
David Blaikiee75f9632014-10-14 17:12:02 +00001529 assert(CurFn == MF &&
1530 "endFunction should be called with the same function as beginFunction");
Timur Iskhodzhanov1cd14442013-12-03 15:10:23 +00001531
Eric Christopher4287a492013-12-09 23:57:44 +00001532 // Set DwarfDwarfCompileUnitID in MCContext to default value.
Lang Hames9ff69c82015-04-24 19:11:51 +00001533 Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
Eric Christopher6a841382012-11-19 22:42:10 +00001534
Devang Patel3acc70e2011-08-15 22:04:40 +00001535 LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
David Blaikie3e3eb332016-12-15 23:17:52 +00001536 assert(!FnScope || SP == FnScope->getScopeNode());
Adrian Prantl75819ae2016-04-15 15:57:41 +00001537 DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit());
Alexey Bataevd4dd7212018-08-01 19:38:20 +00001538 if (TheCU.getCUNode()->isDebugDirectivesOnly()) {
1539 PrevLabel = nullptr;
1540 CurFn = nullptr;
1541 return;
1542 }
David Blaikie263a0082014-10-23 00:06:27 +00001543
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001544 DenseSet<InlinedEntity> Processed;
1545 collectEntityInfo(TheCU, SP, Processed);
Devang Patel3acc70e2011-08-15 22:04:40 +00001546
David Blaikie3a7ce252014-09-19 17:03:16 +00001547 // Add the range of this function to the list of ranges for the CU.
Rafael Espindola07c03d32015-03-05 02:05:42 +00001548 TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
David Blaikie3a7ce252014-09-19 17:03:16 +00001549
1550 // Under -gmlt, skip building the subprogram if there are no inlined
Dehao Chen1ce8d6c2017-01-19 00:44:11 +00001551 // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
1552 // is still needed as we need its source location.
Dehao Chen0944a8c2017-02-01 22:45:09 +00001553 if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
Dehao Chen1ce8d6c2017-01-19 00:44:11 +00001554 TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
David Blaikiee1c79742014-09-30 21:28:32 +00001555 LScopes.getAbstractScopesList().empty() && !IsDarwin) {
David Blaikie80e5b1e2014-10-24 17:57:34 +00001556 assert(InfoHolder.getScopeVariables().empty());
David Blaikie3a7ce252014-09-19 17:03:16 +00001557 PrevLabel = nullptr;
1558 CurFn = nullptr;
1559 return;
1560 }
1561
Adrian Prantl049d21c2014-10-13 20:44:58 +00001562#ifndef NDEBUG
1563 size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
1564#endif
Devang Patel7e623022011-08-10 20:55:27 +00001565 // Construct abstract scopes.
Benjamin Kramer15596c72014-03-07 19:09:39 +00001566 for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001567 auto *SP = cast<DISubprogram>(AScope->getScopeNode());
Shiva Chen2c864552018-05-09 02:40:45 +00001568 for (const DINode *DN : SP->getRetainedNodes()) {
Hsiangkai Wang760c1ab2018-09-06 02:22:06 +00001569 if (!Processed.insert(InlinedEntity(DN, nullptr)).second)
1570 continue;
1571
1572 const MDNode *Scope = nullptr;
1573 if (auto *DV = dyn_cast<DILocalVariable>(DN))
1574 Scope = DV->getScope();
1575 else if (auto *DL = dyn_cast<DILabel>(DN))
1576 Scope = DL->getScope();
1577 else
1578 llvm_unreachable("Unexpected DI type!");
1579
1580 // Collect info for variables/labels that were optimized out.
1581 ensureAbstractEntityIsCreated(TheCU, DN, Scope);
1582 assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
1583 && "ensureAbstractEntityIsCreated inserted abstract scopes");
Devang Patel5c0f85c2010-06-25 22:07:34 +00001584 }
David Blaikie488393f2017-05-12 01:13:45 +00001585 constructAbstractSubprogramScopeDIE(TheCU, AScope);
Bill Wendling2b128d72009-05-20 23:19:06 +00001586 }
Eric Christopher6a841382012-11-19 22:42:10 +00001587
David Blaikie38b74bf2016-12-15 23:37:38 +00001588 ProcessedSPNodes.insert(SP);
Vedant Kumar5931b4e2018-10-05 20:37:17 +00001589 DIE &ScopeDIE = TheCU.constructSubprogramScopeDIE(SP, FnScope);
David Blaikie3a443c22014-11-04 22:12:25 +00001590 if (auto *SkelCU = TheCU.getSkeleton())
David Blaikiea01f2952016-08-24 18:29:49 +00001591 if (!LScopes.getAbstractScopesList().empty() &&
1592 TheCU.getCUNode()->getSplitDebugInlining())
David Blaikie3e3eb332016-12-15 23:17:52 +00001593 SkelCU->constructSubprogramScopeDIE(SP, FnScope);
Devang Patel3acc70e2011-08-15 22:04:40 +00001594
Vedant Kumar5931b4e2018-10-05 20:37:17 +00001595 // Construct call site entries.
1596 constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF);
1597
Bill Wendling2b128d72009-05-20 23:19:06 +00001598 // Clear debug info
David Blaikie825bdd22014-05-21 22:41:17 +00001599 // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
1600 // DbgVariables except those that are also in AbstractVariables (since they
1601 // can be used cross-function)
David Blaikie80e5b1e2014-10-24 17:57:34 +00001602 InfoHolder.getScopeVariables().clear();
Hsiangkai Wang2532ac82018-08-17 15:22:04 +00001603 InfoHolder.getScopeLabels().clear();
Craig Topper353eda42014-04-24 06:44:33 +00001604 PrevLabel = nullptr;
1605 CurFn = nullptr;
Bill Wendling2b128d72009-05-20 23:19:06 +00001606}
1607
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001608// Register a source line with debug info. Returns the unique label that was
1609// emitted and which provides correspondence to the source line list.
Devang Patel34a66202011-05-11 19:22:19 +00001610void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
1611 unsigned Flags) {
Devang Patel2d9caf92009-11-25 17:36:49 +00001612 StringRef Fn;
Scott Linder16c7bda2018-02-23 23:01:06 +00001613 unsigned FileNo = 1;
Diego Novillo282450d2014-03-03 18:53:17 +00001614 unsigned Discriminator = 0;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001615 if (auto *Scope = cast_or_null<DIScope>(S)) {
Duncan P. N. Exon Smithb273d062015-04-16 01:37:00 +00001616 Fn = Scope->getFilename();
Paul Robinsonbb921372017-09-07 22:15:44 +00001617 if (Line != 0 && getDwarfVersion() >= 4)
1618 if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
Dehao Chen6e0c8442016-10-07 15:21:31 +00001619 Discriminator = LBF->getDiscriminator();
Dan Gohman50849c62010-05-05 23:41:32 +00001620
Lang Hames9ff69c82015-04-24 19:11:51 +00001621 unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
Scott Linder16c7bda2018-02-23 23:01:06 +00001622 FileNo = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
Paul Robinson612e89d2018-01-12 19:17:50 +00001623 .getOrCreateSourceID(Scope->getFile());
Dan Gohman50849c62010-05-05 23:41:32 +00001624 }
Scott Linder16c7bda2018-02-23 23:01:06 +00001625 Asm->OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
Lang Hames9ff69c82015-04-24 19:11:51 +00001626 Discriminator, Fn);
Bill Wendling2b128d72009-05-20 23:19:06 +00001627}
1628
Bill Wendling806535f2009-05-20 23:22:40 +00001629//===----------------------------------------------------------------------===//
1630// Emit Methods
1631//===----------------------------------------------------------------------===//
1632
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001633// Emit the debug info section.
1634void DwarfDebug::emitDebugInfo() {
Eric Christopherf8194852013-12-05 18:06:10 +00001635 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Rafael Espindola063d7252015-03-10 16:58:10 +00001636 Holder.emitUnits(/* UseOffsets */ false);
Eric Christopher9c2ecd92012-11-30 23:59:06 +00001637}
1638
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001639// Emit the abbreviation section.
Eric Christopher38371952012-11-20 23:30:11 +00001640void DwarfDebug::emitAbbreviations() {
Eric Christopherf8194852013-12-05 18:06:10 +00001641 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
David Blaikie0504cda2013-12-05 07:43:55 +00001642
1643 Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
Eric Christopher3c5a1912012-12-19 22:02:53 +00001644}
Bill Wendling480ff322009-05-20 23:21:38 +00001645
Wolfgang Pieb456b5552018-01-26 18:52:58 +00001646void DwarfDebug::emitStringOffsetsTableHeader() {
1647 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Pavel Labath7bfa5d62018-07-26 14:36:07 +00001648 Holder.getStringPool().emitStringOffsetsTableHeader(
1649 *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(),
1650 Holder.getStringOffsetsStartSym());
Wolfgang Pieb456b5552018-01-26 18:52:58 +00001651}
1652
Jonas Devliegheree699dfa2018-01-29 14:52:34 +00001653template <typename AccelTableT>
1654void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section,
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001655 StringRef TableName) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001656 Asm->OutStreamer->SwitchSection(Section);
Eric Christopher4996c702011-11-07 09:24:32 +00001657
1658 // Emit the full data.
Pavel Labatha7c457d2018-02-19 16:12:20 +00001659 emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol());
David Blaikie6741bb02014-09-11 21:12:48 +00001660}
1661
Pavel Labath6088c232018-04-04 14:42:14 +00001662void DwarfDebug::emitAccelDebugNames() {
Pavel Labatheadfac82018-04-09 14:38:53 +00001663 // Don't emit anything if we have no compilation units to index.
1664 if (getUnits().empty())
1665 return;
1666
Pavel Labath6088c232018-04-04 14:42:14 +00001667 emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits());
1668}
1669
David Blaikie6741bb02014-09-11 21:12:48 +00001670// Emit visible names into a hashed accelerator table section.
1671void DwarfDebug::emitAccelNames() {
1672 emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001673 "Names");
Eric Christopher4996c702011-11-07 09:24:32 +00001674}
1675
Eric Christopher48fef592012-12-20 21:58:40 +00001676// Emit objective C classes and categories into a hashed accelerator table
1677// section.
Eric Christopher4996c702011-11-07 09:24:32 +00001678void DwarfDebug::emitAccelObjC() {
David Blaikie6741bb02014-09-11 21:12:48 +00001679 emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001680 "ObjC");
Eric Christopher4996c702011-11-07 09:24:32 +00001681}
1682
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001683// Emit namespace dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00001684void DwarfDebug::emitAccelNamespaces() {
David Blaikie6741bb02014-09-11 21:12:48 +00001685 emitAccel(AccelNamespace,
1686 Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001687 "namespac");
Eric Christopher4996c702011-11-07 09:24:32 +00001688}
1689
Eric Christopheracdcbdb2012-11-27 22:43:45 +00001690// Emit type dies into a hashed accelerator table.
Eric Christopher4996c702011-11-07 09:24:32 +00001691void DwarfDebug::emitAccelTypes() {
David Blaikie6741bb02014-09-11 21:12:48 +00001692 emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
Rafael Espindola6b9998b2015-03-10 22:00:25 +00001693 "types");
Eric Christopher4996c702011-11-07 09:24:32 +00001694}
1695
Eric Christopherdd1a0122013-09-13 00:35:05 +00001696// Public name handling.
1697// The format for the various pubnames:
1698//
1699// dwarf pubnames - offset/name pairs where the offset is the offset into the CU
1700// for the DIE that is named.
1701//
1702// gnu pubnames - offset/index value/name tuples where the offset is the offset
1703// into the CU and the index value is computed according to the type of value
1704// for the DIE that is named.
1705//
1706// For type units the offset is the offset of the skeleton DIE. For split dwarf
1707// it's the offset within the debug_info/debug_types dwo section, however, the
1708// reference in the pubname header doesn't change.
1709
1710/// computeIndexValue - Compute the gdb index value for the DIE and CU.
Eric Christophera5a79422013-12-09 23:32:48 +00001711static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
Eric Christopher0fe676a2013-11-21 00:48:22 +00001712 const DIE *Die) {
David Blaikiea0e3c752017-02-03 00:44:18 +00001713 // Entities that ended up only in a Type Unit reference the CU instead (since
1714 // the pub entry has offsets within the CU there's no real offset that can be
1715 // provided anyway). As it happens all such entities (namespaces and types,
1716 // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
1717 // not to be true it would be necessary to persist this information from the
1718 // point at which the entry is added to the index data structure - since by
1719 // the time the index is built from that, the original type/namespace DIE in a
1720 // type unit has already been destroyed so it can't be queried for properties
1721 // like tag, etc.
1722 if (Die->getTag() == dwarf::DW_TAG_compile_unit)
1723 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
1724 dwarf::GIEL_EXTERNAL);
Eric Christopherd2b497b2013-10-16 01:37:49 +00001725 dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
1726
1727 // We could have a specification DIE that has our most of our knowledge,
1728 // look for that now.
Duncan P. N. Exon Smithe7e1d0c2015-05-27 22:14:58 +00001729 if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
1730 DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
David Blaikie8dbcc3f2014-04-25 19:33:43 +00001731 if (SpecDIE.findAttribute(dwarf::DW_AT_external))
Eric Christopherd2b497b2013-10-16 01:37:49 +00001732 Linkage = dwarf::GIEL_EXTERNAL;
1733 } else if (Die->findAttribute(dwarf::DW_AT_external))
1734 Linkage = dwarf::GIEL_EXTERNAL;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001735
1736 switch (Die->getTag()) {
1737 case dwarf::DW_TAG_class_type:
1738 case dwarf::DW_TAG_structure_type:
1739 case dwarf::DW_TAG_union_type:
1740 case dwarf::DW_TAG_enumeration_type:
Eric Christopher261d2342013-09-23 20:55:35 +00001741 return dwarf::PubIndexEntryDescriptor(
1742 dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
1743 ? dwarf::GIEL_STATIC
1744 : dwarf::GIEL_EXTERNAL);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001745 case dwarf::DW_TAG_typedef:
1746 case dwarf::DW_TAG_base_type:
1747 case dwarf::DW_TAG_subrange_type:
David Blaikie8dec4072013-09-19 20:40:26 +00001748 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001749 case dwarf::DW_TAG_namespace:
David Blaikie8dec4072013-09-19 20:40:26 +00001750 return dwarf::GIEK_TYPE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001751 case dwarf::DW_TAG_subprogram:
Eric Christopherccac5c42013-09-23 22:59:14 +00001752 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001753 case dwarf::DW_TAG_variable:
Eric Christopherccac5c42013-09-23 22:59:14 +00001754 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001755 case dwarf::DW_TAG_enumerator:
David Blaikie8dec4072013-09-19 20:40:26 +00001756 return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
1757 dwarf::GIEL_STATIC);
Eric Christopherdd1a0122013-09-13 00:35:05 +00001758 default:
David Blaikie8dec4072013-09-19 20:40:26 +00001759 return dwarf::GIEK_NONE;
Eric Christopherdd1a0122013-09-13 00:35:05 +00001760 }
Eric Christopherdd1a0122013-09-13 00:35:05 +00001761}
1762
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001763/// emitDebugPubSections - Emit visible names and types into debug pubnames and
1764/// pubtypes sections.
1765void DwarfDebug::emitDebugPubSections() {
David Blaikiec3d9e9e2014-03-06 01:42:00 +00001766 for (const auto &NU : CUMap) {
1767 DwarfCompileUnit *TheU = NU.second;
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001768 if (!TheU->hasDwarfPubSections())
David Blaikiece2f1cb2014-03-11 23:35:06 +00001769 continue;
1770
David Blaikie66cf14d2018-08-16 21:29:55 +00001771 bool GnuStyle = TheU->getCUNode()->getNameTableKind() ==
1772 DICompileUnit::DebugNameTableKind::GNU;
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001773
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001774 Asm->OutStreamer->SwitchSection(
1775 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
1776 : Asm->getObjFileLowering().getDwarfPubNamesSection());
1777 emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001778
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001779 Asm->OutStreamer->SwitchSection(
1780 GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
1781 : Asm->getObjFileLowering().getDwarfPubTypesSection());
1782 emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes());
Krzysztof Parzyszek228daa62013-02-12 18:00:14 +00001783 }
1784}
1785
Alexey Bataevbff36082018-03-23 13:35:54 +00001786void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) {
1787 if (useSectionsAsReferences())
1788 Asm->EmitDwarfOffset(CU.getSection()->getBeginSymbol(),
1789 CU.getDebugSectionOffset());
1790 else
1791 Asm->emitDwarfSymbolReference(CU.getLabelBegin());
1792}
1793
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001794void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
1795 DwarfCompileUnit *TheU,
1796 const StringMap<const DIE *> &Globals) {
1797 if (auto *Skeleton = TheU->getSkeleton())
1798 TheU = Skeleton;
Eric Christopher8b3737f2013-09-13 00:34:58 +00001799
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001800 // Emit the header.
1801 Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
1802 MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
1803 MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
1804 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
1805
1806 Asm->OutStreamer->EmitLabel(BeginLabel);
1807
1808 Asm->OutStreamer->AddComment("DWARF Version");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001809 Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION);
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001810
1811 Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
Alexey Bataevbff36082018-03-23 13:35:54 +00001812 emitSectionReference(*TheU);
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001813
1814 Asm->OutStreamer->AddComment("Compilation Unit Length");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001815 Asm->emitInt32(TheU->getLength());
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001816
1817 // Emit the pubnames for this compilation unit.
1818 for (const auto &GI : Globals) {
1819 const char *Name = GI.getKeyData();
1820 const DIE *Entity = GI.second;
1821
1822 Asm->OutStreamer->AddComment("DIE offset");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001823 Asm->emitInt32(Entity->getOffset());
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001824
1825 if (GnuStyle) {
1826 dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
1827 Asm->OutStreamer->AddComment(
1828 Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
1829 dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001830 Asm->emitInt8(Desc.toBits());
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001831 }
1832
1833 Asm->OutStreamer->AddComment("External Name");
1834 Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
1835 }
1836
1837 Asm->OutStreamer->AddComment("End Mark");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001838 Asm->emitInt32(0);
Peter Collingbourneb52e2362017-09-12 21:50:41 +00001839 Asm->OutStreamer->EmitLabel(EndLabel);
Devang Patel04d2f2d2009-11-24 01:14:22 +00001840}
1841
Amjad Aboudc0778412016-01-24 08:18:55 +00001842/// Emit null-terminated strings into a debug str section.
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001843void DwarfDebug::emitDebugStr() {
Wolfgang Pieb456b5552018-01-26 18:52:58 +00001844 MCSection *StringOffsetsSection = nullptr;
1845 if (useSegmentedStringOffsetsTable()) {
1846 emitStringOffsetsTableHeader();
1847 StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection();
1848 }
Eric Christopherf8194852013-12-05 18:06:10 +00001849 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Wolfgang Pieb456b5552018-01-26 18:52:58 +00001850 Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(),
1851 StringOffsetsSection, /* UseRelativeOffsets = */ true);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00001852}
1853
Eric Christopher29e874d2014-03-07 22:40:37 +00001854void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001855 const DebugLocStream::Entry &Entry) {
1856 auto &&Comments = DebugLocs.getComments(Entry);
1857 auto Comment = Comments.begin();
1858 auto End = Comments.end();
1859 for (uint8_t Byte : DebugLocs.getBytes(Entry))
Adrian Prantl92da14b2015-03-02 22:02:33 +00001860 Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
Adrian Prantlb1416832014-08-01 22:11:58 +00001861}
1862
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001863static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
Adrian Prantl92da14b2015-03-02 22:02:33 +00001864 const DebugLocEntry::Value &Value,
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001865 DwarfExpression &DwarfExpr) {
Adrian Prantl956484b2017-03-20 21:35:09 +00001866 auto *DIExpr = Value.getExpression();
1867 DIExpressionCursor ExprCursor(DIExpr);
1868 DwarfExpr.addFragmentOffset(DIExpr);
Adrian Prantlb1416832014-08-01 22:11:58 +00001869 // Regular entry.
Adrian Prantle19e5ef2014-04-27 18:25:40 +00001870 if (Value.isInt()) {
Duncan P. N. Exon Smithfba25d62015-04-17 16:28:58 +00001871 if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
1872 BT->getEncoding() == dwarf::DW_ATE_signed_char))
Adrian Prantla63b8e82017-03-16 17:42:45 +00001873 DwarfExpr.addSignedConstant(Value.getInt());
Adrian Prantl66f25952015-01-13 00:04:06 +00001874 else
Adrian Prantla63b8e82017-03-16 17:42:45 +00001875 DwarfExpr.addUnsignedConstant(Value.getInt());
Adrian Prantle19e5ef2014-04-27 18:25:40 +00001876 } else if (Value.isLocation()) {
Adrian Prantl956484b2017-03-20 21:35:09 +00001877 MachineLocation Location = Value.getLoc();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001878 if (Location.isIndirect())
1879 DwarfExpr.setMemoryLocationKind();
Adrian Prantl2049c0d2017-08-02 15:22:17 +00001880 DIExpressionCursor Cursor(DIExpr);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001881 const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
Adrian Prantlc12cee32017-04-19 23:42:25 +00001882 if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
Adrian Prantl956484b2017-03-20 21:35:09 +00001883 return;
1884 return DwarfExpr.addExpression(std::move(Cursor));
Adrian Prantl3e9c8872016-04-08 00:38:37 +00001885 } else if (Value.isConstantFP()) {
1886 APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
Adrian Prantla63b8e82017-03-16 17:42:45 +00001887 DwarfExpr.addUnsignedConstant(RawBytes);
Eric Christopher29e874d2014-03-07 22:40:37 +00001888 }
Adrian Prantla63b8e82017-03-16 17:42:45 +00001889 DwarfExpr.addExpression(std::move(ExprCursor));
Eric Christopher29e874d2014-03-07 22:40:37 +00001890}
1891
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001892void DebugLocEntry::finalize(const AsmPrinter &AP,
1893 DebugLocStream::ListBuilder &List,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00001894 const DIBasicType *BT) {
Duncan P. N. Exon Smith3a73d9e2015-06-21 16:54:56 +00001895 DebugLocStream::EntryBuilder Entry(List, Begin, End);
1896 BufferByteStreamer Streamer = Entry.getStreamer();
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001897 DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001898 const DebugLocEntry::Value &Value = Values[0];
Adrian Prantl941fa752016-12-05 18:04:47 +00001899 if (Value.isFragment()) {
1900 // Emit all fragments that belong to the same variable and range.
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00001901 assert(llvm::all_of(Values, [](DebugLocEntry::Value P) {
Adrian Prantl941fa752016-12-05 18:04:47 +00001902 return P.isFragment();
1903 }) && "all values are expected to be fragments");
Adrian Prantl92da14b2015-03-02 22:02:33 +00001904 assert(std::is_sorted(Values.begin(), Values.end()) &&
Adrian Prantl941fa752016-12-05 18:04:47 +00001905 "fragments are expected to be sorted");
Duncan P. N. Exon Smithc82570b2015-04-13 18:53:11 +00001906
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001907 for (auto Fragment : Values)
Petar Jovanovicd175aeb2018-06-28 04:50:40 +00001908 emitDebugLocValue(AP, BT, Fragment, DwarfExpr);
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001909
Adrian Prantl92da14b2015-03-02 22:02:33 +00001910 } else {
Adrian Prantl941fa752016-12-05 18:04:47 +00001911 assert(Values.size() == 1 && "only fragments may have >1 value");
Petar Jovanovicd175aeb2018-06-28 04:50:40 +00001912 emitDebugLocValue(AP, BT, Value, DwarfExpr);
Adrian Prantl92da14b2015-03-02 22:02:33 +00001913 }
Adrian Prantl8fafb8d2016-12-09 20:43:40 +00001914 DwarfExpr.finalize();
Adrian Prantl92da14b2015-03-02 22:02:33 +00001915}
1916
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001917void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
Duncan P. N. Exon Smith653c1092015-05-06 19:11:20 +00001918 // Emit the size.
Lang Hames9ff69c82015-04-24 19:11:51 +00001919 Asm->OutStreamer->AddComment("Loc expr size");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001920 Asm->emitInt16(DebugLocs.getBytes(Entry).size());
Duncan P. N. Exon Smith653c1092015-05-06 19:11:20 +00001921
David Blaikie0e84adc2014-04-01 16:17:41 +00001922 // Emit the entry.
1923 APByteStreamer Streamer(*Asm);
1924 emitDebugLocEntry(Streamer, Entry);
David Blaikie0e84adc2014-04-01 16:17:41 +00001925}
1926
Eric Christopher9046f942013-07-02 21:36:07 +00001927// Emit locations into the debug loc section.
Devang Patel930143b2009-11-21 02:48:08 +00001928void DwarfDebug::emitDebugLoc() {
David Blaikie07963bd2017-05-26 18:52:56 +00001929 if (DebugLocs.getLists().empty())
1930 return;
1931
Daniel Dunbarfd95b012011-03-16 22:16:39 +00001932 // Start the dwarf loc section.
Lang Hames9ff69c82015-04-24 19:11:51 +00001933 Asm->OutStreamer->SwitchSection(
David Blaikie94c1d7f2014-04-02 01:50:20 +00001934 Asm->getObjFileLowering().getDwarfLocSection());
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00001935 unsigned char Size = Asm->MAI->getCodePointerSize();
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001936 for (const auto &List : DebugLocs.getLists()) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001937 Asm->OutStreamer->EmitLabel(List.Label);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001938 const DwarfCompileUnit *CU = List.CU;
1939 for (const auto &Entry : DebugLocs.getEntries(List)) {
Eric Christopher384f3fe2014-03-20 19:16:16 +00001940 // Set up the range. This range is relative to the entry point of the
1941 // compile unit. This is a hard coded 0 for low_pc when we're emitting
1942 // ranges, or the DW_AT_low_pc on the compile unit otherwise.
David Blaikiece343492014-11-03 21:15:30 +00001943 if (auto *Base = CU->getBaseAddress()) {
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001944 Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
1945 Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
Eric Christopher384f3fe2014-03-20 19:16:16 +00001946 } else {
Lang Hames9ff69c82015-04-24 19:11:51 +00001947 Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
1948 Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
Eric Christopher384f3fe2014-03-20 19:16:16 +00001949 }
David Blaikie94c1d7f2014-04-02 01:50:20 +00001950
David Blaikie0e84adc2014-04-01 16:17:41 +00001951 emitDebugLocEntryLocation(Entry);
Devang Patel9fc11702010-05-25 23:40:22 +00001952 }
Lang Hames9ff69c82015-04-24 19:11:51 +00001953 Asm->OutStreamer->EmitIntValue(0, Size);
1954 Asm->OutStreamer->EmitIntValue(0, Size);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001955 }
1956}
1957
1958void DwarfDebug::emitDebugLocDWO() {
Lang Hames9ff69c82015-04-24 19:11:51 +00001959 Asm->OutStreamer->SwitchSection(
David Blaikie94c1d7f2014-04-02 01:50:20 +00001960 Asm->getObjFileLowering().getDwarfLocDWOSection());
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001961 for (const auto &List : DebugLocs.getLists()) {
Lang Hames9ff69c82015-04-24 19:11:51 +00001962 Asm->OutStreamer->EmitLabel(List.Label);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001963 for (const auto &Entry : DebugLocs.getEntries(List)) {
David Blaikie94c1d7f2014-04-02 01:50:20 +00001964 // Just always use start_length for now - at least that's one address
1965 // rather than two. We could get fancier and try to, say, reuse an
1966 // address we know we've emitted elsewhere (the start of the function?
1967 // The start of the CU or CU subrange that encloses this range?)
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001968 Asm->emitInt8(dwarf::DW_LLE_startx_length);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001969 unsigned idx = AddrPool.getIndex(Entry.BeginSym);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001970 Asm->EmitULEB128(idx);
Duncan P. N. Exon Smith364a3002015-04-17 21:34:47 +00001971 Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
David Blaikie94c1d7f2014-04-02 01:50:20 +00001972
1973 emitDebugLocEntryLocation(Entry);
David Blaikie9c550ac2014-03-25 01:44:02 +00001974 }
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00001975 Asm->emitInt8(dwarf::DW_LLE_end_of_list);
Devang Patel9fc11702010-05-25 23:40:22 +00001976 }
Bill Wendling480ff322009-05-20 23:21:38 +00001977}
1978
Richard Mitton21101b32013-09-19 23:21:01 +00001979struct ArangeSpan {
1980 const MCSymbol *Start, *End;
1981};
1982
1983// Emit a debug aranges section, containing a CU lookup for any
1984// address we can tie back to a CU.
Eric Christopher7b30f2e42012-11-21 00:34:35 +00001985void DwarfDebug::emitDebugARanges() {
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001986 // Provides a unique id per text section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001987 MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
Richard Mitton21101b32013-09-19 23:21:01 +00001988
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001989 // Filter labels by section.
1990 for (const SymbolCU &SCU : ArangeLabels) {
1991 if (SCU.Sym->isInSection()) {
1992 // Make a note of this symbol and it's section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +00001993 MCSection *Section = &SCU.Sym->getSection();
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00001994 if (!Section->getKind().isMetadata())
1995 SectionMap[Section].push_back(SCU);
1996 } else {
1997 // Some symbols (e.g. common/bss on mach-o) can have no section but still
1998 // appear in the output. This sucks as we rely on sections to build
1999 // arange spans. We can do it without, but it's icky.
2000 SectionMap[nullptr].push_back(SCU);
2001 }
2002 }
Richard Mitton21101b32013-09-19 23:21:01 +00002003
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00002004 DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
2005
Rafael Espindola4f4ef152015-03-09 22:08:37 +00002006 for (auto &I : SectionMap) {
Rafael Espindola78d947b2016-05-20 00:38:28 +00002007 MCSection *Section = I.first;
Rafael Espindola4f4ef152015-03-09 22:08:37 +00002008 SmallVector<SymbolCU, 8> &List = I.second;
Rafael Espindola78d947b2016-05-20 00:38:28 +00002009 if (List.size() < 1)
Richard Mitton21101b32013-09-19 23:21:01 +00002010 continue;
2011
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00002012 // If we have no section (e.g. common), just write out
2013 // individual spans for each symbol.
2014 if (!Section) {
2015 for (const SymbolCU &Cur : List) {
2016 ArangeSpan Span;
2017 Span.Start = Cur.Sym;
2018 Span.End = nullptr;
Rafael Espindola78d947b2016-05-20 00:38:28 +00002019 assert(Cur.CU);
2020 Spans[Cur.CU].push_back(Span);
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00002021 }
2022 continue;
2023 }
2024
Richard Mitton21101b32013-09-19 23:21:01 +00002025 // Sort the symbols by offset within the section.
Mandeep Singh Grang46d02de2018-02-25 19:52:34 +00002026 std::stable_sort(
Rafael Espindola0a78f8c2016-05-19 23:17:37 +00002027 List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) {
2028 unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
2029 unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
Benjamin Kramer571e2fe2014-03-07 19:41:22 +00002030
Rafael Espindola0a78f8c2016-05-19 23:17:37 +00002031 // Symbols with no order assigned should be placed at the end.
2032 // (e.g. section end labels)
2033 if (IA == 0)
2034 return false;
2035 if (IB == 0)
2036 return true;
2037 return IA < IB;
2038 });
Richard Mitton21101b32013-09-19 23:21:01 +00002039
Rafael Espindola78d947b2016-05-20 00:38:28 +00002040 // Insert a final terminator.
2041 List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
2042
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00002043 // Build spans between each label.
2044 const MCSymbol *StartSym = List[0].Sym;
2045 for (size_t n = 1, e = List.size(); n < e; n++) {
2046 const SymbolCU &Prev = List[n - 1];
2047 const SymbolCU &Cur = List[n];
Richard Mitton21101b32013-09-19 23:21:01 +00002048
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00002049 // Try and build the longest span we can within the same CU.
2050 if (Cur.CU != Prev.CU) {
2051 ArangeSpan Span;
2052 Span.Start = StartSym;
2053 Span.End = Cur.Sym;
Rafael Espindola78d947b2016-05-20 00:38:28 +00002054 assert(Prev.CU);
Rafael Espindola6ffb1d72015-02-02 19:22:51 +00002055 Spans[Prev.CU].push_back(Span);
2056 StartSym = Cur.Sym;
Richard Mitton21101b32013-09-19 23:21:01 +00002057 }
2058 }
2059 }
2060
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00002061 // Start the dwarf aranges section.
Lang Hames9ff69c82015-04-24 19:11:51 +00002062 Asm->OutStreamer->SwitchSection(
Rafael Espindolae8fd00d2015-02-26 22:02:02 +00002063 Asm->getObjFileLowering().getDwarfARangesSection());
2064
Konstantin Zhuravlyovdc77b2e2017-04-17 17:41:25 +00002065 unsigned PtrSize = Asm->MAI->getCodePointerSize();
Richard Mitton21101b32013-09-19 23:21:01 +00002066
2067 // Build a list of CUs used.
Eric Christopher4287a492013-12-09 23:57:44 +00002068 std::vector<DwarfCompileUnit *> CUs;
Benjamin Kramer15596c72014-03-07 19:09:39 +00002069 for (const auto &it : Spans) {
2070 DwarfCompileUnit *CU = it.first;
Richard Mitton21101b32013-09-19 23:21:01 +00002071 CUs.push_back(CU);
2072 }
2073
2074 // Sort the CU list (again, to ensure consistent output order).
Fangrui Song0cac7262018-09-27 02:13:45 +00002075 llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
2076 return A->getUniqueID() < B->getUniqueID();
2077 });
Richard Mitton21101b32013-09-19 23:21:01 +00002078
2079 // Emit an arange table for each CU we used.
Benjamin Kramer15596c72014-03-07 19:09:39 +00002080 for (DwarfCompileUnit *CU : CUs) {
Richard Mitton21101b32013-09-19 23:21:01 +00002081 std::vector<ArangeSpan> &List = Spans[CU];
2082
David Blaikie27e35f22014-11-02 01:21:43 +00002083 // Describe the skeleton CU's offset and length, not the dwo file's.
2084 if (auto *Skel = CU->getSkeleton())
2085 CU = Skel;
2086
Richard Mitton21101b32013-09-19 23:21:01 +00002087 // Emit size of content not including length itself.
Eric Christopherb4bef6d2013-11-19 09:04:36 +00002088 unsigned ContentSize =
2089 sizeof(int16_t) + // DWARF ARange version number
2090 sizeof(int32_t) + // Offset of CU in the .debug_info section
2091 sizeof(int8_t) + // Pointer Size (in bytes)
2092 sizeof(int8_t); // Segment Size (in bytes)
Richard Mitton21101b32013-09-19 23:21:01 +00002093
2094 unsigned TupleSize = PtrSize * 2;
2095
2096 // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
Benjamin Kramer8a68ab32014-01-07 19:28:14 +00002097 unsigned Padding =
2098 OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
Richard Mitton21101b32013-09-19 23:21:01 +00002099
2100 ContentSize += Padding;
2101 ContentSize += (List.size() + 1) * TupleSize;
2102
2103 // For each compile unit, write the list of spans it covers.
Lang Hames9ff69c82015-04-24 19:11:51 +00002104 Asm->OutStreamer->AddComment("Length of ARange Set");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00002105 Asm->emitInt32(ContentSize);
Lang Hames9ff69c82015-04-24 19:11:51 +00002106 Asm->OutStreamer->AddComment("DWARF Arange version number");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00002107 Asm->emitInt16(dwarf::DW_ARANGES_VERSION);
Lang Hames9ff69c82015-04-24 19:11:51 +00002108 Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
Alexey Bataevbff36082018-03-23 13:35:54 +00002109 emitSectionReference(*CU);
Lang Hames9ff69c82015-04-24 19:11:51 +00002110 Asm->OutStreamer->AddComment("Address Size (in bytes)");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00002111 Asm->emitInt8(PtrSize);
Lang Hames9ff69c82015-04-24 19:11:51 +00002112 Asm->OutStreamer->AddComment("Segment Size (in bytes)");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00002113 Asm->emitInt8(0);
Richard Mitton21101b32013-09-19 23:21:01 +00002114
Petr Hosekfaef3202016-06-01 01:59:58 +00002115 Asm->OutStreamer->emitFill(Padding, 0xff);
Richard Mitton21101b32013-09-19 23:21:01 +00002116
Benjamin Kramer15596c72014-03-07 19:09:39 +00002117 for (const ArangeSpan &Span : List) {
Richard Mitton21101b32013-09-19 23:21:01 +00002118 Asm->EmitLabelReference(Span.Start, PtrSize);
2119
2120 // Calculate the size as being from the span start to it's end.
Richard Mitton089ed892013-09-23 17:56:20 +00002121 if (Span.End) {
Richard Mitton21101b32013-09-19 23:21:01 +00002122 Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002123 } else {
2124 // For symbols without an end marker (e.g. common), we
2125 // write a single arange entry containing just that one symbol.
2126 uint64_t Size = SymSize[Span.Start];
2127 if (Size == 0)
2128 Size = 1;
2129
Lang Hames9ff69c82015-04-24 19:11:51 +00002130 Asm->OutStreamer->EmitIntValue(Size, PtrSize);
Richard Mitton089ed892013-09-23 17:56:20 +00002131 }
Richard Mitton21101b32013-09-19 23:21:01 +00002132 }
2133
Lang Hames9ff69c82015-04-24 19:11:51 +00002134 Asm->OutStreamer->AddComment("ARange terminator");
2135 Asm->OutStreamer->EmitIntValue(0, PtrSize);
2136 Asm->OutStreamer->EmitIntValue(0, PtrSize);
Richard Mitton21101b32013-09-19 23:21:01 +00002137 }
Bill Wendling480ff322009-05-20 23:21:38 +00002138}
2139
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002140/// Emit a single range list. We handle both DWARF v5 and earlier.
David Blaikiec4af8bf2018-10-20 07:36:39 +00002141static void emitRangeList(DwarfDebug &DD, AsmPrinter *Asm,
Wolfgang Piebe194f732018-07-10 00:10:11 +00002142 const RangeSpanList &List) {
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002143
David Blaikiec4af8bf2018-10-20 07:36:39 +00002144 auto DwarfVersion = DD.getDwarfVersion();
Wolfgang Piebe194f732018-07-10 00:10:11 +00002145 // Emit our symbol so we can find the beginning of the range.
2146 Asm->OutStreamer->EmitLabel(List.getSym());
2147 // Gather all the ranges that apply to the same section so they can share
2148 // a base address entry.
2149 MapVector<const MCSection *, std::vector<const RangeSpan *>> SectionRanges;
2150 // Size for our labels.
2151 auto Size = Asm->MAI->getCodePointerSize();
2152
2153 for (const RangeSpan &Range : List.getRanges())
2154 SectionRanges[&Range.getStart()->getSection()].push_back(&Range);
2155
David Blaikiec4af8bf2018-10-20 07:36:39 +00002156 const MCSymbol *CUBase = List.getBaseAddress();
Wolfgang Piebe194f732018-07-10 00:10:11 +00002157 bool BaseIsSet = false;
2158 for (const auto &P : SectionRanges) {
2159 // Don't bother with a base address entry if there's only one range in
2160 // this section in this range list - for example ranges for a CU will
2161 // usually consist of single regions from each of many sections
2162 // (-ffunction-sections, or just C++ inline functions) except under LTO
2163 // or optnone where there may be holes in a single CU's section
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002164 // contributions.
Wolfgang Piebe194f732018-07-10 00:10:11 +00002165 auto *Base = CUBase;
David Blaikie14cfa0d2018-10-20 09:16:49 +00002166 if (!Base && (P.second.size() > 1 || DwarfVersion < 5) &&
David Blaikied6614052018-07-18 18:04:42 +00002167 (UseDwarfRangesBaseAddressSpecifier || DwarfVersion >= 5)) {
Wolfgang Piebe194f732018-07-10 00:10:11 +00002168 BaseIsSet = true;
2169 // FIXME/use care: This may not be a useful base address if it's not
2170 // the lowest address/range in this object.
2171 Base = P.second.front()->getStart();
David Blaikied6614052018-07-18 18:04:42 +00002172 if (DwarfVersion >= 5) {
David Blaikiec4af8bf2018-10-20 07:36:39 +00002173 Asm->OutStreamer->AddComment("DW_RLE_base_addressx");
2174 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_base_addressx, 1);
2175 Asm->OutStreamer->AddComment(" base address index");
2176 Asm->EmitULEB128(DD.getAddressPool().getIndex(Base));
2177 } else {
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002178 Asm->OutStreamer->EmitIntValue(-1, Size);
David Blaikiec4af8bf2018-10-20 07:36:39 +00002179 Asm->OutStreamer->AddComment(" base address");
2180 Asm->OutStreamer->EmitSymbolValue(Base, Size);
2181 }
David Blaikied6614052018-07-18 18:04:42 +00002182 } else if (BaseIsSet && DwarfVersion < 5) {
Wolfgang Piebe194f732018-07-10 00:10:11 +00002183 BaseIsSet = false;
David Blaikied6614052018-07-18 18:04:42 +00002184 assert(!Base);
2185 Asm->OutStreamer->EmitIntValue(-1, Size);
Wolfgang Piebe194f732018-07-10 00:10:11 +00002186 Asm->OutStreamer->EmitIntValue(0, Size);
2187 }
2188
2189 for (const auto *RS : P.second) {
2190 const MCSymbol *Begin = RS->getStart();
2191 const MCSymbol *End = RS->getEnd();
2192 assert(Begin && "Range without a begin symbol?");
2193 assert(End && "Range without an end symbol?");
2194 if (Base) {
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002195 if (DwarfVersion >= 5) {
2196 // Emit DW_RLE_offset_pair when we have a base.
David Blaikied6614052018-07-18 18:04:42 +00002197 Asm->OutStreamer->AddComment("DW_RLE_offset_pair");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002198 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_offset_pair, 1);
David Blaikied6614052018-07-18 18:04:42 +00002199 Asm->OutStreamer->AddComment(" starting offset");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002200 Asm->EmitLabelDifferenceAsULEB128(Begin, Base);
David Blaikied6614052018-07-18 18:04:42 +00002201 Asm->OutStreamer->AddComment(" ending offset");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002202 Asm->EmitLabelDifferenceAsULEB128(End, Base);
2203 } else {
2204 Asm->EmitLabelDifference(Begin, Base, Size);
2205 Asm->EmitLabelDifference(End, Base, Size);
2206 }
2207 } else if (DwarfVersion >= 5) {
David Blaikiec4af8bf2018-10-20 07:36:39 +00002208 Asm->OutStreamer->AddComment("DW_RLE_startx_length");
2209 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_startx_length, 1);
2210 Asm->OutStreamer->AddComment(" start index");
2211 Asm->EmitULEB128(DD.getAddressPool().getIndex(Begin));
David Blaikied6614052018-07-18 18:04:42 +00002212 Asm->OutStreamer->AddComment(" length");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002213 Asm->EmitLabelDifferenceAsULEB128(End, Begin);
Wolfgang Piebe194f732018-07-10 00:10:11 +00002214 } else {
2215 Asm->OutStreamer->EmitSymbolValue(Begin, Size);
2216 Asm->OutStreamer->EmitSymbolValue(End, Size);
2217 }
2218 }
2219 }
David Blaikied6614052018-07-18 18:04:42 +00002220 if (DwarfVersion >= 5) {
2221 Asm->OutStreamer->AddComment("DW_RLE_end_of_list");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002222 Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_end_of_list, 1);
David Blaikied6614052018-07-18 18:04:42 +00002223 } else {
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002224 // Terminate the list with two 0 values.
2225 Asm->OutStreamer->EmitIntValue(0, Size);
2226 Asm->OutStreamer->EmitIntValue(0, Size);
2227 }
Wolfgang Piebe194f732018-07-10 00:10:11 +00002228}
2229
Wolfgang Pieb9ea65082018-07-26 22:48:52 +00002230// Emit the header of a DWARF 5 range list table. Returns the symbol that
2231// designates the end of the table for the caller to emit when the table is
2232// complete.
David Blaikiec4af8bf2018-10-20 07:36:39 +00002233static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm,
2234 const DwarfFile &Holder) {
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002235 // The length is described by a starting label right after the length field
2236 // and an end label.
2237 MCSymbol *TableStart = Asm->createTempSymbol("debug_rnglist_table_start");
2238 MCSymbol *TableEnd = Asm->createTempSymbol("debug_rnglist_table_end");
2239 // Build the range table header, which starts with the length field.
David Blaikiec4af8bf2018-10-20 07:36:39 +00002240 Asm->OutStreamer->AddComment("Length");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002241 Asm->EmitLabelDifference(TableEnd, TableStart, 4);
2242 Asm->OutStreamer->EmitLabel(TableStart);
2243 // Version number (DWARF v5 and later).
David Blaikiec4af8bf2018-10-20 07:36:39 +00002244 Asm->OutStreamer->AddComment("Version");
Wolfgang Pieb9ea65082018-07-26 22:48:52 +00002245 Asm->emitInt16(Asm->OutStreamer->getContext().getDwarfVersion());
David Blaikiec4af8bf2018-10-20 07:36:39 +00002246 Asm->OutStreamer->AddComment("Address size");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002247 Asm->emitInt8(Asm->MAI->getCodePointerSize());
David Blaikiec4af8bf2018-10-20 07:36:39 +00002248 Asm->OutStreamer->AddComment("Segment selector size");
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002249 Asm->emitInt8(0);
2250
David Blaikiec4af8bf2018-10-20 07:36:39 +00002251 MCSymbol *RnglistsTableBaseSym = Holder.getRnglistsTableBaseSym();
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002252
2253 // FIXME: Generate the offsets table and use DW_FORM_rnglistx with the
2254 // DW_AT_ranges attribute. Until then set the number of offsets to 0.
David Blaikiec4af8bf2018-10-20 07:36:39 +00002255 Asm->OutStreamer->AddComment("Offset entry count");
2256 Asm->emitInt32(Holder.getRangeLists().size());
2257 Asm->OutStreamer->EmitLabel(RnglistsTableBaseSym);
2258 for (const RangeSpanList &List : Holder.getRangeLists())
2259 Asm->EmitLabelDifference(List.getSym(), RnglistsTableBaseSym, 4);
Wolfgang Pieb9ea65082018-07-26 22:48:52 +00002260 return TableEnd;
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002261}
2262
David Blaikiec4af8bf2018-10-20 07:36:39 +00002263void emitDebugRangesImpl(DwarfDebug &DD, AsmPrinter *Asm,
2264 const DwarfFile &Holder, MCSymbol *TableEnd) {
2265 for (const RangeSpanList &List : Holder.getRangeLists())
2266 emitRangeList(DD, Asm, List);
2267
2268 if (TableEnd)
2269 Asm->OutStreamer->EmitLabel(TableEnd);
2270}
2271
Wolfgang Pieb9ea65082018-07-26 22:48:52 +00002272/// Emit address ranges into the .debug_ranges section or into the DWARF v5
2273/// .debug_rnglists section.
Devang Patel930143b2009-11-21 02:48:08 +00002274void DwarfDebug::emitDebugRanges() {
David Blaikie07963bd2017-05-26 18:52:56 +00002275 if (CUMap.empty())
2276 return;
2277
David Blaikiec4af8bf2018-10-20 07:36:39 +00002278 const auto &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Wolfgang Pieb9ea65082018-07-26 22:48:52 +00002279
David Blaikiec4af8bf2018-10-20 07:36:39 +00002280 if (Holder.getRangeLists().empty())
Alexey Bataevd4dd7212018-08-01 19:38:20 +00002281 return;
Alexey Bataevd4dd7212018-08-01 19:38:20 +00002282
David Blaikiec4af8bf2018-10-20 07:36:39 +00002283 assert(useRangesSection());
2284 assert(llvm::none_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
2285 return Pair.second->getCUNode()->isDebugDirectivesOnly();
2286 }));
Wolfgang Piebfcf38102018-07-12 18:18:21 +00002287
Bill Wendling480ff322009-05-20 23:21:38 +00002288 // Start the dwarf ranges section.
Wolfgang Pieb9ea65082018-07-26 22:48:52 +00002289 MCSymbol *TableEnd = nullptr;
2290 if (getDwarfVersion() >= 5) {
2291 Asm->OutStreamer->SwitchSection(
2292 Asm->getObjFileLowering().getDwarfRnglistsSection());
David Blaikiec4af8bf2018-10-20 07:36:39 +00002293 TableEnd = emitRnglistsTableHeader(Asm, Holder);
Wolfgang Pieb9ea65082018-07-26 22:48:52 +00002294 } else
2295 Asm->OutStreamer->SwitchSection(
2296 Asm->getObjFileLowering().getDwarfRangesSection());
Eric Christopher4751d702013-11-23 00:05:29 +00002297
David Blaikiec4af8bf2018-10-20 07:36:39 +00002298 emitDebugRangesImpl(*this, Asm, Holder, TableEnd);
Bill Wendling480ff322009-05-20 23:21:38 +00002299}
2300
David Blaikie32e09de2018-10-20 08:12:36 +00002301void DwarfDebug::emitDebugRangesDWO() {
2302 assert(useSplitDwarf());
2303
2304 if (CUMap.empty())
2305 return;
2306
2307 const auto &Holder = InfoHolder;
2308
2309 if (Holder.getRangeLists().empty())
2310 return;
2311
2312 assert(getDwarfVersion() >= 5);
2313 assert(useRangesSection());
2314 assert(llvm::none_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
2315 return Pair.second->getCUNode()->isDebugDirectivesOnly();
2316 }));
2317
2318 // Start the dwarf ranges section.
2319 Asm->OutStreamer->SwitchSection(
2320 Asm->getObjFileLowering().getDwarfRnglistsDWOSection());
2321 MCSymbol *TableEnd = emitRnglistsTableHeader(Asm, Holder);
2322
2323 emitDebugRangesImpl(*this, Asm, Holder, TableEnd);
2324}
2325
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002326void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002327 for (auto *MN : Nodes) {
2328 if (auto *M = dyn_cast<DIMacro>(MN))
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002329 emitMacro(*M);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002330 else if (auto *F = dyn_cast<DIMacroFile>(MN))
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002331 emitMacroFile(*F, U);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002332 else
2333 llvm_unreachable("Unexpected DI type!");
2334 }
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002335}
2336
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002337void DwarfDebug::emitMacro(DIMacro &M) {
2338 Asm->EmitULEB128(M.getMacinfoType());
2339 Asm->EmitULEB128(M.getLine());
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002340 StringRef Name = M.getName();
2341 StringRef Value = M.getValue();
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002342 Asm->OutStreamer->EmitBytes(Name);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002343 if (!Value.empty()) {
2344 // There should be one space between macro name and macro value.
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00002345 Asm->emitInt8(' ');
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002346 Asm->OutStreamer->EmitBytes(Value);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002347 }
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00002348 Asm->emitInt8('\0');
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002349}
2350
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002351void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002352 assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002353 Asm->EmitULEB128(dwarf::DW_MACINFO_start_file);
2354 Asm->EmitULEB128(F.getLine());
Paul Robinson612e89d2018-01-12 19:17:50 +00002355 Asm->EmitULEB128(U.getOrCreateSourceID(F.getFile()));
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002356 handleMacroNodes(F.getElements(), U);
2357 Asm->EmitULEB128(dwarf::DW_MACINFO_end_file);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002358}
2359
Amjad Aboudc0778412016-01-24 08:18:55 +00002360/// Emit macros into a debug macinfo section.
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002361void DwarfDebug::emitDebugMacinfo() {
David Blaikie07963bd2017-05-26 18:52:56 +00002362 if (CUMap.empty())
2363 return;
2364
Alexey Bataevd4dd7212018-08-01 19:38:20 +00002365 if (llvm::all_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
2366 return Pair.second->getCUNode()->isDebugDirectivesOnly();
2367 }))
2368 return;
2369
Amjad Aboud8bbce8a2016-02-01 14:09:41 +00002370 // Start the dwarf macinfo section.
2371 Asm->OutStreamer->SwitchSection(
2372 Asm->getObjFileLowering().getDwarfMacinfoSection());
2373
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002374 for (const auto &P : CUMap) {
2375 auto &TheCU = *P.second;
Alexey Bataevd4dd7212018-08-01 19:38:20 +00002376 if (TheCU.getCUNode()->isDebugDirectivesOnly())
2377 continue;
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002378 auto *SkCU = TheCU.getSkeleton();
2379 DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
2380 auto *CUNode = cast<DICompileUnit>(P.first);
Alexey Bataevbd7869442018-02-22 16:20:30 +00002381 DIMacroNodeArray Macros = CUNode->getMacros();
2382 if (!Macros.empty()) {
2383 Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin());
2384 handleMacroNodes(Macros, U);
2385 }
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002386 }
2387 Asm->OutStreamer->AddComment("End Of Macro List Mark");
Rafael Espindola4b4d85f2018-03-29 23:32:54 +00002388 Asm->emitInt8(0);
Amjad Aboudd7cfb482016-01-07 14:28:20 +00002389}
2390
Eric Christopherd692c1d2012-12-11 19:42:09 +00002391// DWARF5 Experimental Separate Dwarf emitters.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002392
David Blaikie65a74662014-04-25 18:26:14 +00002393void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002394 std::unique_ptr<DwarfCompileUnit> NewU) {
David Blaikiecafd9622014-11-02 08:51:37 +00002395 NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
David Blaikie96b1ed52017-04-21 23:35:26 +00002396 Asm->TM.Options.MCOptions.SplitDwarfFile);
David Blaikie38fe6342014-01-09 04:28:46 +00002397
David Blaikie38fe6342014-01-09 04:28:46 +00002398 if (!CompilationDir.empty())
David Blaikiecafd9622014-11-02 08:51:37 +00002399 NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
David Blaikie38fe6342014-01-09 04:28:46 +00002400
David Blaikief9b6a552014-04-22 22:39:41 +00002401 addGnuPubAttributes(*NewU, Die);
David Blaikie38fe6342014-01-09 04:28:46 +00002402
David Blaikief9b6a552014-04-22 22:39:41 +00002403 SkeletonHolder.addUnit(std::move(NewU));
David Blaikie38fe6342014-01-09 04:28:46 +00002404}
2405
David Blaikief9b6a552014-04-22 22:39:41 +00002406DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002407
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00002408 auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
David Blaikiebd579052014-04-28 21:14:27 +00002409 CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
David Blaikief9b6a552014-04-22 22:39:41 +00002410 DwarfCompileUnit &NewCU = *OwnedUnit;
Greg Clayton35630c32016-12-01 18:56:29 +00002411 NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
Eric Christopher4c7765f2013-01-17 03:00:04 +00002412
Rafael Espindola063d7252015-03-10 16:58:10 +00002413 NewCU.initStmtList();
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002414
Wolfgang Pieb456b5552018-01-26 18:52:58 +00002415 if (useSegmentedStringOffsetsTable())
2416 NewCU.addStringOffsetsStart();
2417
David Blaikie92a2f8a2014-04-28 21:04:29 +00002418 initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
Eric Christopherc8a310e2012-12-10 23:34:43 +00002419
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002420 return NewCU;
2421}
2422
Eric Christopherd692c1d2012-12-11 19:42:09 +00002423// Emit the .debug_info.dwo section for separated dwarf. This contains the
2424// compile units that would normally be in debug_info.
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002425void DwarfDebug::emitDebugInfoDWO() {
Eric Christophercdf218d2012-12-10 19:51:21 +00002426 assert(useSplitDwarf() && "No split dwarf debug info?");
Rafael Espindola063d7252015-03-10 16:58:10 +00002427 // Don't emit relocations into the dwo file.
2428 InfoHolder.emitUnits(/* UseOffsets */ true);
Eric Christopher3c5a1912012-12-19 22:02:53 +00002429}
2430
2431// Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
2432// abbreviations for the .debug_info.dwo section.
2433void DwarfDebug::emitDebugAbbrevDWO() {
2434 assert(useSplitDwarf() && "No split dwarf?");
David Blaikie0504cda2013-12-05 07:43:55 +00002435 InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
Eric Christopher9c2ecd92012-11-30 23:59:06 +00002436}
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002437
David Blaikie4a2f95f2014-03-18 01:17:26 +00002438void DwarfDebug::emitDebugLineDWO() {
2439 assert(useSplitDwarf() && "No split dwarf?");
Paul Robinson7cb26ad2018-03-27 21:28:59 +00002440 SplitTypeUnitFileTable.Emit(
2441 *Asm->OutStreamer, MCDwarfLineTableParams(),
David Blaikie4a2f95f2014-03-18 01:17:26 +00002442 Asm->getObjFileLowering().getDwarfLineDWOSection());
David Blaikie4a2f95f2014-03-18 01:17:26 +00002443}
2444
Wolfgang Pieb456b5552018-01-26 18:52:58 +00002445void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
2446 assert(useSplitDwarf() && "No split dwarf?");
Pavel Labath7bfa5d62018-07-26 14:36:07 +00002447 InfoHolder.getStringPool().emitStringOffsetsTableHeader(
2448 *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(),
2449 InfoHolder.getStringOffsetsStartSym());
Wolfgang Pieb456b5552018-01-26 18:52:58 +00002450}
2451
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002452// Emit the .debug_str.dwo section for separated dwarf. This contains the
2453// string section and is identical in format to traditional .debug_str
2454// sections.
2455void DwarfDebug::emitDebugStrDWO() {
Wolfgang Pieb456b5552018-01-26 18:52:58 +00002456 if (useSegmentedStringOffsetsTable())
2457 emitStringOffsetsTableHeaderDWO();
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002458 assert(useSplitDwarf() && "No split dwarf?");
Rafael Espindola0709a7b2015-05-21 19:20:38 +00002459 MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
Eric Christopher2cbd5762013-01-07 19:32:41 +00002460 InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
Wolfgang Pieb456b5552018-01-26 18:52:58 +00002461 OffSec, /* UseRelativeOffsets = */ false);
Eric Christopher3bf29fd2012-12-27 02:14:01 +00002462}
David Blaikie409dd9c2013-11-19 23:08:21 +00002463
David Blaikie161dd3c2018-10-20 06:02:15 +00002464// Emit address pool.
Victor Leschuk64e0c562018-08-01 05:48:06 +00002465void DwarfDebug::emitDebugAddr() {
Victor Leschuk64e0c562018-08-01 05:48:06 +00002466 AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
2467}
2468
David Blaikie47f4b822014-03-19 00:11:28 +00002469MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
2470 if (!useSplitDwarf())
2471 return nullptr;
Paul Robinsonb271f312018-03-29 17:16:41 +00002472 const DICompileUnit *DIUnit = CU.getCUNode();
2473 SplitTypeUnitFileTable.maybeSetRootFile(
2474 DIUnit->getDirectory(), DIUnit->getFilename(),
2475 CU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource());
David Blaikie47f4b822014-03-19 00:11:28 +00002476 return &SplitTypeUnitFileTable;
2477}
2478
Adrian Prantlee5feaf2015-07-15 17:01:41 +00002479uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
David Blaikief3de2ab2014-04-26 16:26:41 +00002480 MD5 Hash;
2481 Hash.update(Identifier);
2482 // ... take the least significant 8 bytes and return those. Our MD5
Zachary Turner82a0c972017-03-20 23:33:18 +00002483 // implementation always returns its results in little endian, so we actually
2484 // need the "high" word.
David Blaikief3de2ab2014-04-26 16:26:41 +00002485 MD5::MD5Result Result;
2486 Hash.final(Result);
Zachary Turner82a0c972017-03-20 23:33:18 +00002487 return Result.high();
David Blaikief3de2ab2014-04-26 16:26:41 +00002488}
2489
David Blaikie15632ae2014-02-12 00:31:30 +00002490void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
David Blaikie65a74662014-04-25 18:26:14 +00002491 StringRef Identifier, DIE &RefDie,
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002492 const DICompositeType *CTy) {
David Blaikiee12b49a2014-04-26 17:27:38 +00002493 // Fast path if we're building some type units and one has already used the
2494 // address pool we know we're going to throw away all this work anyway, so
2495 // don't bother building dependent types.
2496 if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
2497 return;
2498
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002499 auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
2500 if (!Ins.second) {
2501 CU.addDIETypeSignature(RefDie, Ins.first->second);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002502 return;
David Blaikie409dd9c2013-11-19 23:08:21 +00002503 }
2504
David Blaikiee12b49a2014-04-26 17:27:38 +00002505 bool TopLevelType = TypeUnitsUnderConstruction.empty();
2506 AddrPool.resetUsedFlag();
2507
Eugene Zelenko6e07bfd2017-08-17 21:26:39 +00002508 auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
2509 getDwoLineTable(CU));
David Blaikief9b6a552014-04-22 22:39:41 +00002510 DwarfTypeUnit &NewTU = *OwnedUnit;
David Blaikie92a2f8a2014-04-28 21:04:29 +00002511 DIE &UnitDie = NewTU.getUnitDie();
David Majnemer0a16c222016-08-11 21:15:00 +00002512 TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
Chandler Carruthb587ab62014-01-20 08:07:07 +00002513
David Blaikie92a2f8a2014-04-28 21:04:29 +00002514 NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
David Blaikie637cac42014-04-22 23:09:36 +00002515 CU.getLanguage());
Chandler Carruthb587ab62014-01-20 08:07:07 +00002516
David Blaikief3de2ab2014-04-26 16:26:41 +00002517 uint64_t Signature = makeTypeSignature(Identifier);
David Blaikief9b6a552014-04-22 22:39:41 +00002518 NewTU.setTypeSignature(Signature);
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002519 Ins.first->second = Signature;
David Blaikief3de2ab2014-04-26 16:26:41 +00002520
David Blaikie29459ae2014-07-25 17:11:58 +00002521 if (useSplitDwarf())
Greg Clayton35630c32016-12-01 18:56:29 +00002522 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
David Blaikie29459ae2014-07-25 17:11:58 +00002523 else {
Greg Clayton35630c32016-12-01 18:56:29 +00002524 NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
Paul Robinson7cb26ad2018-03-27 21:28:59 +00002525 // Non-split type units reuse the compile unit's line table.
2526 CU.applyStmtList(UnitDie);
David Blaikie29459ae2014-07-25 17:11:58 +00002527 }
Chandler Carruthb587ab62014-01-20 08:07:07 +00002528
Wolfgang Pieb456b5552018-01-26 18:52:58 +00002529 // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
2530 // units.
2531 if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
2532 NewTU.addStringOffsetsStart();
2533
David Blaikief3de2ab2014-04-26 16:26:41 +00002534 NewTU.setType(NewTU.createTypeDIE(CTy));
2535
David Blaikiee12b49a2014-04-26 17:27:38 +00002536 if (TopLevelType) {
2537 auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
2538 TypeUnitsUnderConstruction.clear();
2539
2540 // Types referencing entries in the address table cannot be placed in type
2541 // units.
2542 if (AddrPool.hasBeenUsed()) {
2543
2544 // Remove all the types built while building this type.
2545 // This is pessimistic as some of these types might not be dependent on
2546 // the type that used an address.
2547 for (const auto &TU : TypeUnitsToAdd)
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002548 TypeSignatures.erase(TU.second);
David Blaikiee12b49a2014-04-26 17:27:38 +00002549
2550 // Construct this type in the CU directly.
2551 // This is inefficient because all the dependent types will be rebuilt
2552 // from scratch, including building them in type units, discovering that
2553 // they depend on addresses, throwing them out and rebuilding them.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +00002554 CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
David Blaikiee12b49a2014-04-26 17:27:38 +00002555 return;
2556 }
2557
2558 // If the type wasn't dependent on fission addresses, finish adding the type
2559 // and all its dependent types.
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002560 for (auto &TU : TypeUnitsToAdd) {
2561 InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
2562 InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
2563 }
David Blaikiee12b49a2014-04-26 17:27:38 +00002564 }
Peter Collingbourne7c384cc2016-02-11 19:57:46 +00002565 CU.addDIETypeSignature(RefDie, Signature);
David Blaikie409dd9c2013-11-19 23:08:21 +00002566}
David Blaikie4bd13b72014-03-07 18:49:45 +00002567
Pavel Labath7f7e6062018-07-20 15:24:13 +00002568// Add the Name along with its companion DIE to the appropriate accelerator
2569// table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
2570// AccelTableKind::Apple, we use the table we got as an argument). If
2571// accelerator tables are disabled, this function does nothing.
2572template <typename DataT>
David Blaikie66cf14d2018-08-16 21:29:55 +00002573void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU,
2574 AccelTable<DataT> &AppleAccel, StringRef Name,
Pavel Labath7f7e6062018-07-20 15:24:13 +00002575 const DIE &Die) {
2576 if (getAccelTableKind() == AccelTableKind::None)
2577 return;
Pavel Labath3fb39c72018-04-18 12:11:59 +00002578
David Blaikie66cf14d2018-08-16 21:29:55 +00002579 if (getAccelTableKind() != AccelTableKind::Apple &&
2580 CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None)
2581 return;
2582
Pavel Labath3fb39c72018-04-18 12:11:59 +00002583 DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
Pavel Labath2f088112018-08-07 09:54:52 +00002584 DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name);
Pavel Labath3fb39c72018-04-18 12:11:59 +00002585
Pavel Labath6088c232018-04-04 14:42:14 +00002586 switch (getAccelTableKind()) {
2587 case AccelTableKind::Apple:
Pavel Labath7f7e6062018-07-20 15:24:13 +00002588 AppleAccel.addName(Ref, Die);
Pavel Labath6088c232018-04-04 14:42:14 +00002589 break;
2590 case AccelTableKind::Dwarf:
Pavel Labath7f7e6062018-07-20 15:24:13 +00002591 AccelDebugNames.addName(Ref, Die);
Pavel Labath6088c232018-04-04 14:42:14 +00002592 break;
Pavel Labath6088c232018-04-04 14:42:14 +00002593 case AccelTableKind::Default:
2594 llvm_unreachable("Default should have already been resolved.");
Pavel Labath7f7e6062018-07-20 15:24:13 +00002595 case AccelTableKind::None:
2596 llvm_unreachable("None handled above");
Pavel Labath6088c232018-04-04 14:42:14 +00002597 }
David Blaikie2406a0622014-04-23 23:37:35 +00002598}
David Blaikie0ee82b92014-04-24 00:53:32 +00002599
David Blaikie66cf14d2018-08-16 21:29:55 +00002600void DwarfDebug::addAccelName(const DICompileUnit &CU, StringRef Name,
2601 const DIE &Die) {
2602 addAccelNameImpl(CU, AccelNames, Name, Die);
Pavel Labath7f7e6062018-07-20 15:24:13 +00002603}
2604
David Blaikie66cf14d2018-08-16 21:29:55 +00002605void DwarfDebug::addAccelObjC(const DICompileUnit &CU, StringRef Name,
2606 const DIE &Die) {
Pavel Labath7f7e6062018-07-20 15:24:13 +00002607 // ObjC names go only into the Apple accelerator tables.
2608 if (getAccelTableKind() == AccelTableKind::Apple)
David Blaikie66cf14d2018-08-16 21:29:55 +00002609 addAccelNameImpl(CU, AccelObjC, Name, Die);
David Blaikie0ee82b92014-04-24 00:53:32 +00002610}
David Blaikieecf04152014-04-24 01:02:42 +00002611
David Blaikie66cf14d2018-08-16 21:29:55 +00002612void DwarfDebug::addAccelNamespace(const DICompileUnit &CU, StringRef Name,
2613 const DIE &Die) {
2614 addAccelNameImpl(CU, AccelNamespace, Name, Die);
David Blaikieecf04152014-04-24 01:02:42 +00002615}
David Blaikie18d33752014-04-24 01:23:49 +00002616
David Blaikie66cf14d2018-08-16 21:29:55 +00002617void DwarfDebug::addAccelType(const DICompileUnit &CU, StringRef Name,
2618 const DIE &Die, char Flags) {
2619 addAccelNameImpl(CU, AccelTypes, Name, Die);
David Blaikie18d33752014-04-24 01:23:49 +00002620}
Greg Claytone6543972016-11-23 23:30:37 +00002621
2622uint16_t DwarfDebug::getDwarfVersion() const {
2623 return Asm->OutStreamer->getContext().getDwarfVersion();
2624}