blob: 7510bc5415e62c86d2c3ba31c611d5d20372462a [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
Daniel Jasper0f778692016-12-08 12:45:29 +000010#include "../lib/CodeGen/AsmPrinter/DwarfStringPool.h"
Greg Clayton3462a422016-12-08 01:03:48 +000011#include "DwarfGenerator.h"
Greg Clayton3462a422016-12-08 01:03:48 +000012#include "llvm/ADT/Triple.h"
13#include "llvm/CodeGen/AsmPrinter.h"
14#include "llvm/CodeGen/DIE.h"
15#include "llvm/DebugInfo/DWARF/DWARFContext.h"
16#include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
17#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
18#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
19#include "llvm/IR/LegacyPassManagers.h"
20#include "llvm/MC/MCAsmBackend.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCCodeEmitter.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCDwarf.h"
25#include "llvm/MC/MCInstrInfo.h"
26#include "llvm/MC/MCObjectFileInfo.h"
27#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSubtargetInfo.h"
30#include "llvm/MC/MCTargetOptionsCommandFlags.h"
31#include "llvm/PassAnalysisSupport.h"
32#include "llvm/Support/Dwarf.h"
33#include "llvm/Support/LEB128.h"
34#include "llvm/Support/TargetRegistry.h"
35#include "llvm/Support/raw_ostream.h"
36#include "llvm/Target/TargetMachine.h"
37#include "llvm/Target/TargetOptions.h"
38
39using namespace llvm;
40using namespace dwarf;
41
42namespace {} // end anonymous namespace
43
44//===----------------------------------------------------------------------===//
45/// dwarfgen::DIE implementation.
46//===----------------------------------------------------------------------===//
47unsigned dwarfgen::DIE::computeSizeAndOffsets(unsigned Offset) {
48 auto &DG = CU->getGenerator();
49 return Die->computeOffsetsAndAbbrevs(DG.getAsmPrinter(), DG.getAbbrevSet(),
50 Offset);
51}
52
53void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, uint64_t U) {
54 auto &DG = CU->getGenerator();
55 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
56 DIEInteger(U));
57}
58
59void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
60 StringRef String) {
61 auto &DG = CU->getGenerator();
62 if (Form == DW_FORM_string) {
63 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
64 new (DG.getAllocator()) DIEInlineString(String));
65 } else {
66 Die->addValue(
67 DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
68 DIEString(DG.getStringPool().getEntry(*DG.getAsmPrinter(), String)));
69 }
70}
71
72void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form,
73 dwarfgen::DIE &RefDie) {
74 auto &DG = CU->getGenerator();
75 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
76 DIEEntry(*RefDie.Die));
77}
78
79void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form, const void *P,
80 size_t S) {
81 auto &DG = CU->getGenerator();
82 DIEBlock *Block = new (DG.getAllocator()) DIEBlock;
83 for (size_t I = 0; I < S; ++I)
84 Block->addValue(DG.getAllocator(), (dwarf::Attribute)0,
85 dwarf::DW_FORM_data1, DIEInteger(((uint8_t *)P)[I]));
86
87 Block->ComputeSize(DG.getAsmPrinter());
88 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
89 Block);
90}
91
92void dwarfgen::DIE::addAttribute(uint16_t A, dwarf::Form Form) {
93 auto &DG = CU->getGenerator();
94 assert(Form == DW_FORM_flag_present);
95 Die->addValue(DG.getAllocator(), static_cast<dwarf::Attribute>(A), Form,
96 DIEInteger(1));
97}
98
99dwarfgen::DIE dwarfgen::DIE::addChild(dwarf::Tag Tag) {
100 auto &DG = CU->getGenerator();
101 return dwarfgen::DIE(CU,
102 &Die->addChild(llvm::DIE::get(DG.getAllocator(), Tag)));
103}
104
105dwarfgen::DIE dwarfgen::CompileUnit::getUnitDIE() {
106 return dwarfgen::DIE(this, &DU.getUnitDie());
107}
108
109//===----------------------------------------------------------------------===//
110/// dwarfgen::Generator implementation.
111//===----------------------------------------------------------------------===//
112
Greg Claytonb9032832016-12-08 16:57:04 +0000113dwarfgen::Generator::Generator()
114 : MAB(nullptr), MCE(nullptr), MS(nullptr), StringPool(nullptr),
115 Abbreviations(Allocator) {}
Greg Clayton3462a422016-12-08 01:03:48 +0000116dwarfgen::Generator::~Generator() = default;
117
118llvm::Expected<std::unique_ptr<dwarfgen::Generator>>
119dwarfgen::Generator::create(Triple TheTriple, uint16_t DwarfVersion) {
120 std::unique_ptr<dwarfgen::Generator> GenUP(new dwarfgen::Generator());
121 llvm::Error error = GenUP->init(TheTriple, DwarfVersion);
122 if (error)
123 return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(error));
124 return Expected<std::unique_ptr<dwarfgen::Generator>>(std::move(GenUP));
125}
126
127llvm::Error dwarfgen::Generator::init(Triple TheTriple, uint16_t V) {
128 Version = V;
129 std::string ErrorStr;
130 std::string TripleName;
131
132 // Get the target.
133 const Target *TheTarget =
134 TargetRegistry::lookupTarget(TripleName, TheTriple, ErrorStr);
135 if (!TheTarget)
136 return make_error<StringError>(ErrorStr, inconvertibleErrorCode());
137
138 TripleName = TheTriple.getTriple();
139
140 // Create all the MC Objects.
141 MRI.reset(TheTarget->createMCRegInfo(TripleName));
142 if (!MRI)
143 return make_error<StringError>(Twine("no register info for target ") +
144 TripleName,
145 inconvertibleErrorCode());
146
147 MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName));
148 if (!MAI)
149 return make_error<StringError>("no asm info for target " + TripleName,
150 inconvertibleErrorCode());
151
152 MOFI.reset(new MCObjectFileInfo);
153 MC.reset(new MCContext(MAI.get(), MRI.get(), MOFI.get()));
154 MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, CodeModel::Default, *MC);
155
156 MCTargetOptions Options;
157 MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "", Options);
158 if (!MAB)
159 return make_error<StringError>("no asm backend for target " + TripleName,
160 inconvertibleErrorCode());
161
162 MII.reset(TheTarget->createMCInstrInfo());
163 if (!MII)
164 return make_error<StringError>("no instr info info for target " +
165 TripleName,
166 inconvertibleErrorCode());
167
168 MSTI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
169 if (!MSTI)
170 return make_error<StringError>("no subtarget info for target " + TripleName,
171 inconvertibleErrorCode());
172
173 MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, *MC);
174 if (!MCE)
175 return make_error<StringError>("no code emitter for target " + TripleName,
176 inconvertibleErrorCode());
177
178 Stream = make_unique<raw_svector_ostream>(FileBytes);
179
180 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
181 MS = TheTarget->createMCObjectStreamer(
182 TheTriple, *MC, *MAB, *Stream, MCE, *MSTI, MCOptions.MCRelaxAll,
183 MCOptions.MCIncrementalLinkerCompatible,
184 /*DWARFMustBeAtTheEnd*/ false);
185 if (!MS)
186 return make_error<StringError>("no object streamer for target " +
187 TripleName,
188 inconvertibleErrorCode());
189
190 // Finally create the AsmPrinter we'll use to emit the DIEs.
191 TM.reset(TheTarget->createTargetMachine(TripleName, "", "", TargetOptions(),
192 None));
193 if (!TM)
194 return make_error<StringError>("no target machine for target " + TripleName,
195 inconvertibleErrorCode());
196
197 Asm.reset(TheTarget->createAsmPrinter(*TM, std::unique_ptr<MCStreamer>(MS)));
198 if (!Asm)
199 return make_error<StringError>("no asm printer for target " + TripleName,
200 inconvertibleErrorCode());
201
202 // Set the DWARF version correctly on all classes that we use.
203 MC->setDwarfVersion(Version);
204 Asm->setDwarfVersion(Version);
205
Benjamin Kramer9fcb7fe2016-12-09 13:12:30 +0000206 StringPool = llvm::make_unique<DwarfStringPool>(Allocator, *Asm, StringRef());
Greg Clayton3462a422016-12-08 01:03:48 +0000207
208 return Error::success();
209}
210
211StringRef dwarfgen::Generator::generate() {
212 // Offset from the first CU in the debug info section is 0 initially.
213 unsigned SecOffset = 0;
214
215 // Iterate over each compile unit and set the size and offsets for each
216 // DIE within each compile unit. All offsets are CU relative.
217 for (auto &CU : CompileUnits) {
218 // Set the absolute .debug_info offset for this compile unit.
219 CU->setOffset(SecOffset);
220 // The DIEs contain compile unit relative offsets.
221 unsigned CUOffset = 11;
222 CUOffset = CU->getUnitDIE().computeSizeAndOffsets(CUOffset);
223 // Update our absolute .debug_info offset.
224 SecOffset += CUOffset;
225 CU->setLength(CUOffset - 4);
226 }
227 Abbreviations.Emit(Asm.get(), MOFI->getDwarfAbbrevSection());
228 StringPool->emit(*Asm, MOFI->getDwarfStrSection());
229 MS->SwitchSection(MOFI->getDwarfInfoSection());
230 for (auto &CU : CompileUnits) {
231 uint16_t Version = CU->getVersion();
232 auto Length = CU->getLength();
233 MC->setDwarfVersion(Version);
234 assert(Length != -1U);
235 Asm->EmitInt32(Length);
236 Asm->EmitInt16(Version);
237 Asm->EmitInt32(0);
238 Asm->EmitInt8(CU->getAddressSize());
239 Asm->emitDwarfDIE(*CU->getUnitDIE().Die);
240 }
241
242 MS->Finish();
243 if (FileBytes.empty())
244 return StringRef();
245 return StringRef(FileBytes.data(), FileBytes.size());
246}
247
248bool dwarfgen::Generator::saveFile(StringRef Path) {
249 if (FileBytes.empty())
250 return false;
251 std::error_code EC;
252 raw_fd_ostream Strm(Path, EC, sys::fs::F_None);
253 if (EC)
254 return false;
255 Strm.write(FileBytes.data(), FileBytes.size());
256 Strm.close();
257 return true;
258}
259
260dwarfgen::CompileUnit &dwarfgen::Generator::addCompileUnit() {
261 CompileUnits.push_back(std::unique_ptr<CompileUnit>(
262 new CompileUnit(*this, Version, Asm->getPointerSize())));
263 return *CompileUnits.back();
264}