blob: c49c2249cdfefe1291c4cb90093554d33f7009bc [file] [log] [blame]
Chris Bieneman07088c12017-01-12 21:35:21 +00001//===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===//
Chris Bieneman7d7364a2016-12-07 22:30:15 +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/// \file
Chris Bieneman07088c12017-01-12 21:35:21 +000011/// \brief The DWARF component of yaml2obj. Provided as library code for tests.
Chris Bieneman7d7364a2016-12-07 22:30:15 +000012///
13//===----------------------------------------------------------------------===//
14
Chris Bieneman07088c12017-01-12 21:35:21 +000015#include "llvm/ObjectYAML/DWARFEmitter.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000016#include "DWARFVisitor.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
Chris Bieneman7d7364a2016-12-07 22:30:15 +000019#include "llvm/ObjectYAML/DWARFYAML.h"
20#include "llvm/Support/Error.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000021#include "llvm/Support/Host.h"
Chris Bieneman7d7364a2016-12-07 22:30:15 +000022#include "llvm/Support/LEB128.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000023#include "llvm/Support/MathExtras.h"
24#include "llvm/Support/MemoryBuffer.h"
Chris Bieneman55de3a22016-12-22 21:58:03 +000025#include "llvm/Support/SwapByteOrder.h"
Eugene Zelenko28082ab2017-07-01 01:35:55 +000026#include "llvm/Support/YAMLTraits.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000027#include "llvm/Support/raw_ostream.h"
Chris Bienemane0e451d2016-12-22 22:44:27 +000028#include <algorithm>
Eugene Zelenko28082ab2017-07-01 01:35:55 +000029#include <cassert>
30#include <cstddef>
31#include <cstdint>
32#include <memory>
33#include <string>
34#include <vector>
Chris Bienemane0e451d2016-12-22 22:44:27 +000035
Chris Bieneman7d7364a2016-12-07 22:30:15 +000036using namespace llvm;
37
Chris Bieneman55de3a22016-12-22 21:58:03 +000038template <typename T>
Benjamin Kramerefcf06f2017-02-11 11:06:55 +000039static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
Chris Bieneman55de3a22016-12-22 21:58:03 +000040 if (IsLittleEndian != sys::IsLittleEndianHost)
41 sys::swapByteOrder(Integer);
42 OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
43}
44
Benjamin Kramerefcf06f2017-02-11 11:06:55 +000045static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
46 raw_ostream &OS, bool IsLittleEndian) {
Chris Bieneman55de3a22016-12-22 21:58:03 +000047 if (8 == Size)
48 writeInteger((uint64_t)Integer, OS, IsLittleEndian);
49 else if (4 == Size)
50 writeInteger((uint32_t)Integer, OS, IsLittleEndian);
51 else if (2 == Size)
52 writeInteger((uint16_t)Integer, OS, IsLittleEndian);
53 else if (1 == Size)
54 writeInteger((uint8_t)Integer, OS, IsLittleEndian);
55 else
56 assert(false && "Invalid integer write size.");
57}
58
Benjamin Kramerefcf06f2017-02-11 11:06:55 +000059static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
Chris Bieneman313b3262016-12-09 00:26:44 +000060 std::vector<uint8_t> FillData;
61 FillData.insert(FillData.begin(), Size, 0);
62 OS.write(reinterpret_cast<char *>(FillData.data()), Size);
63}
64
Benjamin Kramer49a49fe2017-08-20 13:03:48 +000065static void writeInitialLength(const DWARFYAML::InitialLength &Length,
66 raw_ostream &OS, bool IsLittleEndian) {
Chris Bienemanfaf1feb2017-03-03 21:11:55 +000067 writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
68 if (Length.isDWARF64())
69 writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
70}
71
Chris Bieneman07088c12017-01-12 21:35:21 +000072void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
Chris Bieneman7d7364a2016-12-07 22:30:15 +000073 for (auto Str : DI.DebugStrings) {
74 OS.write(Str.data(), Str.size());
75 OS.write('\0');
76 }
77}
78
Chris Bieneman07088c12017-01-12 21:35:21 +000079void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
Chris Bieneman7d7364a2016-12-07 22:30:15 +000080 for (auto AbbrevDecl : DI.AbbrevDecls) {
81 encodeULEB128(AbbrevDecl.Code, OS);
82 encodeULEB128(AbbrevDecl.Tag, OS);
83 OS.write(AbbrevDecl.Children);
84 for (auto Attr : AbbrevDecl.Attributes) {
85 encodeULEB128(Attr.Attribute, OS);
86 encodeULEB128(Attr.Form, OS);
Chris Bienemanbcf513f2017-03-06 23:22:49 +000087 if (Attr.Form == dwarf::DW_FORM_implicit_const)
88 encodeSLEB128(Attr.Value, OS);
Chris Bieneman7d7364a2016-12-07 22:30:15 +000089 }
90 encodeULEB128(0, OS);
91 encodeULEB128(0, OS);
92 }
93}
Chris Bieneman313b3262016-12-09 00:26:44 +000094
Chris Bieneman07088c12017-01-12 21:35:21 +000095void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
Chris Bieneman313b3262016-12-09 00:26:44 +000096 for (auto Range : DI.ARanges) {
97 auto HeaderStart = OS.tell();
Chris Bienemanfaf1feb2017-03-03 21:11:55 +000098 writeInitialLength(Range.Length, OS, DI.IsLittleEndian);
Chris Bieneman55de3a22016-12-22 21:58:03 +000099 writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
100 writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
101 writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
102 writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
Chris Bieneman313b3262016-12-09 00:26:44 +0000103
104 auto HeaderSize = OS.tell() - HeaderStart;
105 auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
106 ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
107
108 for (auto Descriptor : Range.Descriptors) {
Chris Bieneman55de3a22016-12-22 21:58:03 +0000109 writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
110 DI.IsLittleEndian);
111 writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
112 DI.IsLittleEndian);
Chris Bieneman313b3262016-12-09 00:26:44 +0000113 }
114 ZeroFillBytes(OS, Range.AddrSize * 2);
115 }
116}
Chris Bienemand9430942016-12-19 22:22:12 +0000117
Chris Bieneman07088c12017-01-12 21:35:21 +0000118void DWARFYAML::EmitPubSection(raw_ostream &OS,
119 const DWARFYAML::PubSection &Sect,
120 bool IsLittleEndian) {
Chris Bienemanfaf1feb2017-03-03 21:11:55 +0000121 writeInitialLength(Sect.Length, OS, IsLittleEndian);
Chris Bieneman55de3a22016-12-22 21:58:03 +0000122 writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
123 writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
124 writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
Chris Bienemand9430942016-12-19 22:22:12 +0000125 for (auto Entry : Sect.Entries) {
Chris Bieneman55de3a22016-12-22 21:58:03 +0000126 writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
Chris Bienemand9430942016-12-19 22:22:12 +0000127 if (Sect.IsGNUStyle)
Chris Bieneman55de3a22016-12-22 21:58:03 +0000128 writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
Chris Bienemand9430942016-12-19 22:22:12 +0000129 OS.write(Entry.Name.data(), Entry.Name.size());
130 OS.write('\0');
131 }
Chris Bienemane0e451d2016-12-22 22:44:27 +0000132}
133
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000134namespace {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000135/// \brief An extension of the DWARFYAML::ConstVisitor which writes compile
136/// units and DIEs to a stream.
137class DumpVisitor : public DWARFYAML::ConstVisitor {
138 raw_ostream &OS;
Chris Bienemane0e451d2016-12-22 22:44:27 +0000139
Chris Bieneman8f471f72017-03-06 20:52:12 +0000140protected:
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000141 void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000142 writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
143 writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
Chris Bienemanb3ca7112017-03-07 18:50:58 +0000144 if(CU.Version >= 5) {
145 writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
146 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
147 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
148 }else {
149 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
150 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
151 }
152
Chris Bienemane0e451d2016-12-22 22:44:27 +0000153 }
Chris Bieneman8f471f72017-03-06 20:52:12 +0000154
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000155 void onStartDIE(const DWARFYAML::Unit &CU,
156 const DWARFYAML::Entry &DIE) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000157 encodeULEB128(DIE.AbbrCode, OS);
158 }
159
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000160 void onValue(const uint8_t U) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000161 writeInteger(U, OS, DebugInfo.IsLittleEndian);
162 }
163
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000164 void onValue(const uint16_t U) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000165 writeInteger(U, OS, DebugInfo.IsLittleEndian);
166 }
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000167
168 void onValue(const uint32_t U) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000169 writeInteger(U, OS, DebugInfo.IsLittleEndian);
170 }
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000171
172 void onValue(const uint64_t U, const bool LEB = false) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000173 if (LEB)
174 encodeULEB128(U, OS);
175 else
176 writeInteger(U, OS, DebugInfo.IsLittleEndian);
177 }
178
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000179 void onValue(const int64_t S, const bool LEB = false) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000180 if (LEB)
181 encodeSLEB128(S, OS);
182 else
183 writeInteger(S, OS, DebugInfo.IsLittleEndian);
184 }
185
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000186 void onValue(const StringRef String) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000187 OS.write(String.data(), String.size());
188 OS.write('\0');
189 }
190
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000191 void onValue(const MemoryBufferRef MBR) override {
Chris Bieneman8f471f72017-03-06 20:52:12 +0000192 OS.write(MBR.getBufferStart(), MBR.getBufferSize());
193 }
194
195public:
196 DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
197 : DWARFYAML::ConstVisitor(DI), OS(Out) {}
198};
Benjamin Kramer49a49fe2017-08-20 13:03:48 +0000199} // namespace
Chris Bieneman8f471f72017-03-06 20:52:12 +0000200
201void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
202 DumpVisitor Visitor(DI, OS);
203 Visitor.traverseDebugInfo();
Chris Bienemane0e451d2016-12-22 22:44:27 +0000204}
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000205
Benjamin Kramerefcf06f2017-02-11 11:06:55 +0000206static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000207 OS.write(File.Name.data(), File.Name.size());
208 OS.write('\0');
209 encodeULEB128(File.DirIdx, OS);
210 encodeULEB128(File.ModTime, OS);
211 encodeULEB128(File.Length, OS);
212}
213
Chris Bieneman07088c12017-01-12 21:35:21 +0000214void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
Benjamin Kramerefcf06f2017-02-11 11:06:55 +0000215 for (const auto &LineTable : DI.DebugLines) {
Chris Bienemanfaf1feb2017-03-03 21:11:55 +0000216 writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
217 uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000218 writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
219 writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
220 OS, DI.IsLittleEndian);
221 writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
222 if (LineTable.Version >= 4)
223 writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
224 writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
225 writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
226 writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
227 writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
228
229 for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
230 writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
231
232 for (auto IncludeDir : LineTable.IncludeDirs) {
233 OS.write(IncludeDir.data(), IncludeDir.size());
234 OS.write('\0');
235 }
236 OS.write('\0');
237
238 for (auto File : LineTable.Files)
Chris Bieneman07088c12017-01-12 21:35:21 +0000239 EmitFileEntry(OS, File);
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000240 OS.write('\0');
241
242 for (auto Op : LineTable.Opcodes) {
243 writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
244 if (Op.Opcode == 0) {
245 encodeULEB128(Op.ExtLen, OS);
246 writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
247 switch (Op.SubOpcode) {
248 case dwarf::DW_LNE_set_address:
249 case dwarf::DW_LNE_set_discriminator:
250 writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
251 DI.IsLittleEndian);
252 break;
253 case dwarf::DW_LNE_define_file:
Chris Bieneman07088c12017-01-12 21:35:21 +0000254 EmitFileEntry(OS, Op.FileEntry);
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000255 break;
256 case dwarf::DW_LNE_end_sequence:
257 break;
258 default:
259 for (auto OpByte : Op.UnknownOpcodeData)
260 writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
261 }
262 } else if (Op.Opcode < LineTable.OpcodeBase) {
263 switch (Op.Opcode) {
264 case dwarf::DW_LNS_copy:
265 case dwarf::DW_LNS_negate_stmt:
266 case dwarf::DW_LNS_set_basic_block:
267 case dwarf::DW_LNS_const_add_pc:
268 case dwarf::DW_LNS_set_prologue_end:
269 case dwarf::DW_LNS_set_epilogue_begin:
270 break;
271
272 case dwarf::DW_LNS_advance_pc:
273 case dwarf::DW_LNS_set_file:
274 case dwarf::DW_LNS_set_column:
275 case dwarf::DW_LNS_set_isa:
276 encodeULEB128(Op.Data, OS);
277 break;
278
279 case dwarf::DW_LNS_advance_line:
280 encodeSLEB128(Op.SData, OS);
281 break;
282
283 case dwarf::DW_LNS_fixed_advance_pc:
284 writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
285 break;
286
287 default:
288 for (auto OpData : Op.StandardOpcodeData) {
289 encodeULEB128(OpData, OS);
290 }
291 }
292 }
293 }
294 }
295}
Chris Bieneman2e752db2017-01-20 19:03:14 +0000296
Eugene Zelenko28082ab2017-07-01 01:35:55 +0000297using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &);
Chris Bieneman2e752db2017-01-20 19:03:14 +0000298
Benjamin Kramerefcf06f2017-02-11 11:06:55 +0000299static void
300EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
301 StringRef Sec,
302 StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
Chris Bieneman2e752db2017-01-20 19:03:14 +0000303 std::string Data;
304 raw_string_ostream DebugInfoStream(Data);
305 EmitFunc(DebugInfoStream, DI);
306 DebugInfoStream.flush();
307 if (!Data.empty())
308 OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
309}
310
311Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
312DWARFYAML::EmitDebugSections(StringRef YAMLString,
313 bool IsLittleEndian) {
314 StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
315
316 yaml::Input YIn(YAMLString);
317
318 DWARFYAML::Data DI;
319 DI.IsLittleEndian = IsLittleEndian;
320 YIn >> DI;
321 if (YIn.error())
322 return errorCodeToError(YIn.error());
323
324 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
325 DebugSections);
326 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
327 DebugSections);
328 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
329 DebugSections);
330 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
331 DebugSections);
332 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
333 DebugSections);
334 return std::move(DebugSections);
335}