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