blob: 7f9f03ef722aa3fcf36158e57f0baa149766b714 [file] [log] [blame]
Daniel Jasper0f778692016-12-08 12:45:29 +00001//===--- unittests/DebugInfo/DWARF/DwarfGenerator.cpp -----------*- C++ -*-===//
Greg Clayton3462a422016-12-08 01:03:48 +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#include "DwarfGenerator.h"
Chandler Carruth9a67b072017-06-06 11:06:56 +000011#include "../lib/CodeGen/AsmPrinter/DwarfStringPool.h"
Greg Clayton3462a422016-12-08 01:03:48 +000012#include "llvm/ADT/Triple.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000013#include "llvm/BinaryFormat/Dwarf.h"
Greg Clayton3462a422016-12-08 01:03:48 +000014#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/CodeGen/DIE.h"
Greg Clayton3462a422016-12-08 01:03:48 +000016#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
17#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
Greg Clayton3462a422016-12-08 01:03:48 +000018#include "llvm/MC/MCAsmBackend.h"
19#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCCodeEmitter.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCDwarf.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCObjectFileInfo.h"
Peter Collingbournef7b81db2018-05-18 18:26:45 +000025#include "llvm/MC/MCObjectWriter.h"
Greg Clayton3462a422016-12-08 01:03:48 +000026#include "llvm/MC/MCRegisterInfo.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSubtargetInfo.h"
David Blaikie4333f972018-04-11 18:49:37 +000029#include "llvm/MC/MCTargetOptionsCommandFlags.inc"
Greg Clayton3462a422016-12-08 01:03:48 +000030#include "llvm/PassAnalysisSupport.h"
Greg Clayton3462a422016-12-08 01:03:48 +000031#include "llvm/Support/TargetRegistry.h"
32#include "llvm/Support/raw_ostream.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetOptions.h"
35
36using namespace llvm;
37using namespace dwarf;
38
39namespace {} // end anonymous namespace
40
41//===----------------------------------------------------------------------===//
42/// dwarfgen::DIE implementation.
43//===----------------------------------------------------------------------===//
44unsigned dwarfgen::DIE::computeSizeAndOffsets(unsigned Offset) {
45 auto &DG = CU->getGenerator();
46 return Die->computeOffsetsAndAbbrevs(DG.getAsmPrinter(), DG.getAbbrevSet(),
47 Offset);
48}
49
50void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, uint64_t U) {
51 auto &DG = CU->getGenerator();
52 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
53 DIEInteger(U));
54}
55
56void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
Pavel Labath78ab6592018-07-25 15:33:32 +000057 const MCExpr &Expr) {
58 auto &DG = CU->getGenerator();
59 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
60 DIEExpr(&Expr));
61}
62
63void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
Greg Clayton3462a422016-12-08 01:03:48 +000064 StringRef String) {
65 auto &DG = CU->getGenerator();
Pavel Labath78ab6592018-07-25 15:33:32 +000066 switch (Form) {
67 case DW_FORM_string:
Greg Clayton3462a422016-12-08 01:03:48 +000068 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
Benjamin Kramereedc4052016-12-09 13:33:41 +000069 new (DG.getAllocator())
70 DIEInlineString(String, DG.getAllocator()));
Pavel Labath78ab6592018-07-25 15:33:32 +000071 break;
72
73 case DW_FORM_strp:
74 case DW_FORM_GNU_str_index:
75 case DW_FORM_strx:
76 case DW_FORM_strx1:
77 case DW_FORM_strx2:
78 case DW_FORM_strx3:
79 case DW_FORM_strx4:
Greg Clayton3462a422016-12-08 01:03:48 +000080 Die->addValue(
81 DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
82 DIEString(DG.getStringPool().getEntry(*DG.getAsmPrinter(), String)));
Pavel Labath78ab6592018-07-25 15:33:32 +000083 break;
84
85 default:
86 llvm_unreachable("Unhandled form!");
Greg Clayton3462a422016-12-08 01:03:48 +000087 }
88}
89
90void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
91 dwarfgen::DIE &RefDie) {
92 auto &DG = CU->getGenerator();
93 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
94 DIEEntry(*RefDie.Die));
95}
96
97void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, const void *P,
98 size_t S) {
99 auto &DG = CU->getGenerator();
100 DIEBlock *Block = new (DG.getAllocator()) DIEBlock;
101 for (size_t I = 0; I < S; ++I)
Saleem Abdulrasoolecf13bb2016-12-27 18:35:24 +0000102 Block->addValue(
103 DG.getAllocator(), (dwarf::Attribute)0, dwarf::DW_FORM_data1,
104 DIEInteger(
105 (const_cast<uint8_t *>(static_cast<const uint8_t *>(P)))[I]));
Greg Clayton3462a422016-12-08 01:03:48 +0000106
107 Block->ComputeSize(DG.getAsmPrinter());
108 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
109 Block);
110}
111
112void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form) {
113 auto &DG = CU->getGenerator();
114 assert(Form == DW_FORM_flag_present);
115 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
116 DIEInteger(1));
117}
118
119dwarfgen::DIE dwarfgen::DIE::addChild(dwarf::Tag Tag) {
120 auto &DG = CU->getGenerator();
121 return dwarfgen::DIE(CU,
122 &Die->addChild(llvm::DIE::get(DG.getAllocator(), Tag)));
123}
124
125dwarfgen::DIE dwarfgen::CompileUnit::getUnitDIE() {
126 return dwarfgen::DIE(this, &DU.getUnitDie());
127}
128
129//===----------------------------------------------------------------------===//
James Hendersona3acf992018-05-10 10:51:33 +0000130/// dwarfgen::LineTable implementation.
131//===----------------------------------------------------------------------===//
132DWARFDebugLine::Prologue dwarfgen::LineTable::createBasicPrologue() const {
133 DWARFDebugLine::Prologue P;
134 switch (Version) {
135 case 2:
136 case 3:
137 P.TotalLength = 41;
138 P.PrologueLength = 35;
139 break;
140 case 4:
141 P.TotalLength = 42;
142 P.PrologueLength = 36;
143 break;
144 case 5:
145 P.TotalLength = 47;
146 P.PrologueLength = 39;
147 P.FormParams.AddrSize = AddrSize;
148 break;
149 default:
150 llvm_unreachable("unsupported version");
151 }
152 if (Format == DWARF64) {
153 P.TotalLength += 4;
154 P.FormParams.Format = DWARF64;
155 }
156 P.FormParams.Version = Version;
157 P.MinInstLength = 1;
158 P.MaxOpsPerInst = 1;
159 P.DefaultIsStmt = 1;
160 P.LineBase = -5;
161 P.LineRange = 14;
162 P.OpcodeBase = 13;
163 P.StandardOpcodeLengths = {0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1};
164 P.IncludeDirectories.push_back(DWARFFormValue(DW_FORM_string));
165 P.IncludeDirectories.back().setPValue("a dir");
166 P.FileNames.push_back(DWARFDebugLine::FileNameEntry());
167 P.FileNames.back().Name.setPValue("a file");
168 P.FileNames.back().Name.setForm(DW_FORM_string);
169 return P;
170}
171
172void dwarfgen::LineTable::setPrologue(DWARFDebugLine::Prologue NewPrologue) {
173 Prologue = NewPrologue;
174 CustomPrologue.clear();
175}
176
177void dwarfgen::LineTable::setCustomPrologue(
178 ArrayRef<ValueAndLength> NewPrologue) {
179 Prologue.reset();
180 CustomPrologue = NewPrologue;
181}
182
183void dwarfgen::LineTable::addByte(uint8_t Value) {
184 Contents.push_back({Value, Byte});
185}
186
187void dwarfgen::LineTable::addStandardOpcode(uint8_t Opcode,
188 ArrayRef<ValueAndLength> Operands) {
189 Contents.push_back({Opcode, Byte});
190 Contents.insert(Contents.end(), Operands.begin(), Operands.end());
191}
192
193void dwarfgen::LineTable::addExtendedOpcode(uint64_t Length, uint8_t Opcode,
194 ArrayRef<ValueAndLength> Operands) {
195 Contents.push_back({0, Byte});
196 Contents.push_back({Length, ULEB});
197 Contents.push_back({Opcode, Byte});
198 Contents.insert(Contents.end(), Operands.begin(), Operands.end());
199}
200
201void dwarfgen::LineTable::generate(MCContext &MC, AsmPrinter &Asm) const {
202 MC.setDwarfVersion(Version);
203
204 MCSymbol *EndSymbol = nullptr;
205 if (!CustomPrologue.empty()) {
206 writeData(CustomPrologue, Asm);
207 } else if (!Prologue) {
208 EndSymbol = writeDefaultPrologue(Asm);
209 } else {
210 writePrologue(Asm);
211 }
212
213 writeData(Contents, Asm);
214 if (EndSymbol != nullptr)
215 Asm.OutStreamer->EmitLabel(EndSymbol);
216}
217
218void dwarfgen::LineTable::writeData(ArrayRef<ValueAndLength> Data,
219 AsmPrinter &Asm) const {
220 for (auto Entry : Data) {
221 switch (Entry.Length) {
222 case Byte:
223 case Half:
224 case Long:
225 case Quad:
226 Asm.OutStreamer->EmitIntValue(Entry.Value, Entry.Length);
Roman Lebedeve1b0e292018-05-10 14:16:41 +0000227 continue;
James Hendersona3acf992018-05-10 10:51:33 +0000228 case ULEB:
229 Asm.EmitULEB128(Entry.Value);
Roman Lebedeve1b0e292018-05-10 14:16:41 +0000230 continue;
James Hendersona3acf992018-05-10 10:51:33 +0000231 case SLEB:
232 Asm.EmitSLEB128(Entry.Value);
Roman Lebedeve1b0e292018-05-10 14:16:41 +0000233 continue;
James Hendersona3acf992018-05-10 10:51:33 +0000234 }
Roman Lebedeve1b0e292018-05-10 14:16:41 +0000235 llvm_unreachable("unsupported ValueAndLength Length value");
James Hendersona3acf992018-05-10 10:51:33 +0000236 }
237}
238
239MCSymbol *dwarfgen::LineTable::writeDefaultPrologue(AsmPrinter &Asm) const {
240 MCSymbol *UnitStart = Asm.createTempSymbol("line_unit_start");
241 MCSymbol *UnitEnd = Asm.createTempSymbol("line_unit_end");
242 if (Format == DwarfFormat::DWARF64) {
243 Asm.emitInt32(0xffffffff);
244 Asm.EmitLabelDifference(UnitEnd, UnitStart, 8);
245 } else {
246 Asm.EmitLabelDifference(UnitEnd, UnitStart, 4);
247 }
248 Asm.OutStreamer->EmitLabel(UnitStart);
249 Asm.emitInt16(Version);
250 if (Version == 5) {
251 Asm.emitInt8(AddrSize);
252 Asm.emitInt8(SegSize);
253 }
254
255 MCSymbol *PrologueStart = Asm.createTempSymbol("line_prologue_start");
256 MCSymbol *PrologueEnd = Asm.createTempSymbol("line_prologue_end");
257 Asm.EmitLabelDifference(PrologueEnd, PrologueStart,
258 Format == DwarfFormat::DWARF64 ? 8 : 4);
259 Asm.OutStreamer->EmitLabel(PrologueStart);
260
261 DWARFDebugLine::Prologue DefaultPrologue = createBasicPrologue();
262 writeProloguePayload(DefaultPrologue, Asm);
263 Asm.OutStreamer->EmitLabel(PrologueEnd);
264 return UnitEnd;
265}
266
267void dwarfgen::LineTable::writePrologue(AsmPrinter &Asm) const {
268 if (Format == DwarfFormat::DWARF64) {
269 Asm.emitInt32(0xffffffff);
270 Asm.emitInt64(Prologue->TotalLength);
271 } else {
272 Asm.emitInt32(Prologue->TotalLength);
273 }
274 Asm.emitInt16(Prologue->getVersion());
275 if (Version == 5) {
276 Asm.emitInt8(Prologue->getAddressSize());
277 Asm.emitInt8(Prologue->SegSelectorSize);
278 }
279 if (Format == DwarfFormat::DWARF64)
280 Asm.emitInt64(Prologue->PrologueLength);
281 else
282 Asm.emitInt32(Prologue->PrologueLength);
283
284 writeProloguePayload(*Prologue, Asm);
285}
286
287static void writeCString(StringRef Str, AsmPrinter &Asm) {
288 Asm.OutStreamer->EmitBytes(Str);
289 Asm.emitInt8(0);
290}
291
292static void writeV2IncludeAndFileTable(const DWARFDebugLine::Prologue &Prologue,
293 AsmPrinter &Asm) {
294 for (auto Include : Prologue.IncludeDirectories) {
295 assert(Include.getAsCString() && "expected a string form for include dir");
296 writeCString(*Include.getAsCString(), Asm);
297 }
298 Asm.emitInt8(0);
299
300 for (auto File : Prologue.FileNames) {
301 assert(File.Name.getAsCString() && "expected a string form for file name");
302 writeCString(*File.Name.getAsCString(), Asm);
303 Asm.EmitULEB128(File.DirIdx);
304 Asm.EmitULEB128(File.ModTime);
305 Asm.EmitULEB128(File.Length);
306 }
307 Asm.emitInt8(0);
308}
309
310static void writeV5IncludeAndFileTable(const DWARFDebugLine::Prologue &Prologue,
311 AsmPrinter &Asm) {
312 Asm.emitInt8(1); // directory_entry_format_count.
313 // TODO: Add support for other content descriptions - we currently only
314 // support a single DW_LNCT_path/DW_FORM_string.
315 Asm.EmitULEB128(DW_LNCT_path);
316 Asm.EmitULEB128(DW_FORM_string);
317 Asm.EmitULEB128(Prologue.IncludeDirectories.size());
318 for (auto Include : Prologue.IncludeDirectories) {
319 assert(Include.getAsCString() && "expected a string form for include dir");
320 writeCString(*Include.getAsCString(), Asm);
321 }
322
323 Asm.emitInt8(1); // file_name_entry_format_count.
324 Asm.EmitULEB128(DW_LNCT_path);
325 Asm.EmitULEB128(DW_FORM_string);
326 Asm.EmitULEB128(Prologue.FileNames.size());
327 for (auto File : Prologue.FileNames) {
328 assert(File.Name.getAsCString() && "expected a string form for file name");
329 writeCString(*File.Name.getAsCString(), Asm);
330 }
331}
332
333void dwarfgen::LineTable::writeProloguePayload(
334 const DWARFDebugLine::Prologue &Prologue, AsmPrinter &Asm) const {
335 Asm.emitInt8(Prologue.MinInstLength);
336 if (Version >= 4)
337 Asm.emitInt8(Prologue.MaxOpsPerInst);
338 Asm.emitInt8(Prologue.DefaultIsStmt);
339 Asm.emitInt8(Prologue.LineBase);
340 Asm.emitInt8(Prologue.LineRange);
341 Asm.emitInt8(Prologue.OpcodeBase);
342 for (auto Length : Prologue.StandardOpcodeLengths) {
343 Asm.emitInt8(Length);
344 }
345
346 if (Version < 5)
347 writeV2IncludeAndFileTable(Prologue, Asm);
348 else
349 writeV5IncludeAndFileTable(Prologue, Asm);
350}
351
352//===----------------------------------------------------------------------===//
Greg Clayton3462a422016-12-08 01:03:48 +0000353/// dwarfgen::Generator implementation.
354//===----------------------------------------------------------------------===//
355
Greg Claytonb9032832016-12-08 16:57:04 +0000356dwarfgen::Generator::Generator()
357 : MAB(nullptr), MCE(nullptr), MS(nullptr), StringPool(nullptr),
358 Abbreviations(Allocator) {}
Greg Clayton3462a422016-12-08 01:03:48 +0000359dwarfgen::Generator::~Generator() = default;
360
361llvm::Expected<std::unique_ptr<dwarfgen::Generator>>
362dwarfgen::Generator::create(Triple TheTriple, uint16_t DwarfVersion) {
363 std::unique_ptr<dwarfgen::Generator> GenUP(new dwarfgen::Generator());
364 llvm::Error error = GenUP->init(TheTriple, DwarfVersion);
365 if (error)
366 return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(error));
367 return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(GenUP));
368}
369
370llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) {
371 Version = V;
372 std::string ErrorStr;
373 std::string TripleName;
374
375 // Get the target.
376 const Target *TheTarget =
377 TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
378 if (!TheTarget)
379 return make_error<StringError>(ErrorStr, inconvertibleErrorCode());
380
381 TripleName = TheTriple.getTriple();
382
383 // Create all the MC Objects.
384 MRI.reset(TheTarget->createMCRegInfo(TripleName));
385 if (!MRI)
386 return make_error<StringError>(Twine("no register info for target ") +
387 TripleName,
388 inconvertibleErrorCode());
389
390 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
391 if (!MAI)
392 return make_error<StringError>("no asm info for target " + TripleName,
393 inconvertibleErrorCode());
394
395 MOFI.reset(new MCObjectFileInfo);
396 MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
Rafael Espindola9f929952017-08-02 20:32:26 +0000397 MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, *MC);
Greg Clayton3462a422016-12-08 01:03:48 +0000398
Alex Bradburyb22f7512018-01-03 08:53:05 +0000399 MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
400 if (!MSTI)
401 return make_error<StringError>("no subtarget info for target " + TripleName,
402 inconvertibleErrorCode());
403
David Blaikieeef5c232017-11-27 19:55:16 +0000404 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
Alex Bradburyb22f7512018-01-03 08:53:05 +0000405 MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
Greg Clayton3462a422016-12-08 01:03:48 +0000406 if (!MAB)
407 return make_error<StringError>("no asm backend for target " + TripleName,
408 inconvertibleErrorCode());
409
410 MII.reset(TheTarget->createMCInstrInfo());
411 if (!MII)
412 return make_error<StringError>("no instr info info for target " +
413 TripleName,
414 inconvertibleErrorCode());
415
Greg Clayton3462a422016-12-08 01:03:48 +0000416 MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
417 if (!MCE)
418 return make_error<StringError>("no code emitter for target " + TripleName,
419 inconvertibleErrorCode());
420
421 Stream = make_unique<raw_svector_ostream>(FileBytes);
422
Greg Clayton3462a422016-12-08 01:03:48 +0000423 MS = TheTarget->createMCObjectStreamer(
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000424 TheTriple, *MC, std::unique_ptr<MCAsmBackend>(MAB),
425 MAB->createObjectWriter(*Stream), std::unique_ptr<MCCodeEmitter>(MCE),
426 *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
Greg Clayton3462a422016-12-08 01:03:48 +0000427 /*DWARFMustBeAtTheEnd*/ false);
428 if (!MS)
429 return make_error<StringError>("no object streamer for target " +
430 TripleName,
431 inconvertibleErrorCode());
432
433 // Finally create the AsmPrinter we'll use to emit the DIEs.
434 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
435 None));
436 if (!TM)
437 return make_error<StringError>("no target machine for target " + TripleName,
438 inconvertibleErrorCode());
439
440 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
441 if (!Asm)
442 return make_error<StringError>("no asm printer for target " + TripleName,
443 inconvertibleErrorCode());
444
445 // Set the DWARF version correctly on all classes that we use.
446 MC->setDwarfVersion(Version);
447 Asm->setDwarfVersion(Version);
448
Benjamin Kramer9fcb7fe2016-12-09 13:12:30 +0000449 StringPool = llvm::make_unique<DwarfStringPool>(Allocator, *Asm, StringRef());
Pavel Labath78ab6592018-07-25 15:33:32 +0000450 StringOffsetsStartSym = Asm->createTempSymbol("str_offsets_base");
Greg Clayton3462a422016-12-08 01:03:48 +0000451
452 return Error::success();
453}
454
455StringRef dwarfgen::Generator::generate() {
456 // Offset from the first CU in the debug info section is 0 initially.
457 unsigned SecOffset = 0;
458
459 // Iterate over each compile unit and set the size and offsets for each
460 // DIE within each compile unit. All offsets are CU relative.
461 for (auto &CU : CompileUnits) {
462 // Set the absolute .debug_info offset for this compile unit.
463 CU->setOffset(SecOffset);
464 // The DIEs contain compile unit relative offsets.
465 unsigned CUOffset = 11;
466 CUOffset = CU->getUnitDIE().computeSizeAndOffsets(CUOffset);
467 // Update our absolute .debug_info offset.
468 SecOffset += CUOffset;
469 CU->setLength(CUOffset - 4);
470 }
471 Abbreviations.Emit(Asm.get(), MOFI->getDwarfAbbrevSection());
Pavel Labath78ab6592018-07-25 15:33:32 +0000472
473 StringPool->emitStringOffsetsTableHeader(*Asm, MOFI->getDwarfStrOffSection(),
474 StringOffsetsStartSym);
475 StringPool->emit(*Asm, MOFI->getDwarfStrSection(),
476 MOFI->getDwarfStrOffSection());
477
Greg Clayton3462a422016-12-08 01:03:48 +0000478 MS->SwitchSection(MOFI->getDwarfInfoSection());
479 for (auto &CU : CompileUnits) {
480 uint16_t Version = CU->getVersion();
481 auto Length = CU->getLength();
482 MC->setDwarfVersion(Version);
483 assert(Length != -1U);
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000484 Asm->emitInt32(Length);
485 Asm->emitInt16(Version);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000486 if (Version <= 4) {
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000487 Asm->emitInt32(0);
488 Asm->emitInt8(CU->getAddressSize());
Paul Robinsoncddd6042017-02-28 20:24:55 +0000489 } else {
Rafael Espindola4b4d85f2018-03-29 23:32:54 +0000490 Asm->emitInt8(dwarf::DW_UT_compile);
491 Asm->emitInt8(CU->getAddressSize());
492 Asm->emitInt32(0);
Paul Robinsoncddd6042017-02-28 20:24:55 +0000493 }
Greg Clayton3462a422016-12-08 01:03:48 +0000494 Asm->emitDwarfDIE(*CU->getUnitDIE().Die);
495 }
496
James Hendersona3acf992018-05-10 10:51:33 +0000497 MS->SwitchSection(MOFI->getDwarfLineSection());
498 for (auto &LT : LineTables)
499 LT->generate(*MC, *Asm);
500
Greg Clayton3462a422016-12-08 01:03:48 +0000501 MS->Finish();
502 if (FileBytes.empty())
503 return StringRef();
504 return StringRef(FileBytes.data(), FileBytes.size());
505}
506
507bool dwarfgen::Generator::saveFile(StringRef Path) {
508 if (FileBytes.empty())
509 return false;
510 std::error_code EC;
511 raw_fd_ostream Strm(Path, EC, sys::fs::F_None);
512 if (EC)
513 return false;
514 Strm.write(FileBytes.data(), FileBytes.size());
515 Strm.close();
516 return true;
517}
518
519dwarfgen::CompileUnit &dwarfgen::Generator::addCompileUnit() {
James Hendersona3acf992018-05-10 10:51:33 +0000520 CompileUnits.push_back(
521 make_unique<CompileUnit>(*this, Version, Asm->getPointerSize()));
Greg Clayton3462a422016-12-08 01:03:48 +0000522 return *CompileUnits.back();
523}
James Hendersona3acf992018-05-10 10:51:33 +0000524
525dwarfgen::LineTable &dwarfgen::Generator::addLineTable(DwarfFormat Format) {
526 LineTables.push_back(
James Henderson198a87c2018-05-10 14:36:24 +0000527 make_unique<LineTable>(Version, Format, Asm->getPointerSize()));
James Hendersona3acf992018-05-10 10:51:33 +0000528 return *LineTables.back();
529}