blob: 69f7d368ddc8bb12d31473988badff15029c0434 [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"
Chris Bieneman7d7364a2016-12-07 22:30:15 +000016#include "llvm/ObjectYAML/DWARFYAML.h"
17#include "llvm/Support/Error.h"
18#include "llvm/Support/LEB128.h"
19#include "llvm/Support/raw_ostream.h"
Chris Bieneman55de3a22016-12-22 21:58:03 +000020#include "llvm/Support/SwapByteOrder.h"
Chris Bieneman7d7364a2016-12-07 22:30:15 +000021
Chris Bienemane0e451d2016-12-22 22:44:27 +000022#include <algorithm>
23
Chris Bieneman7d7364a2016-12-07 22:30:15 +000024using namespace llvm;
25
Chris Bieneman55de3a22016-12-22 21:58:03 +000026template <typename T>
Benjamin Kramerefcf06f2017-02-11 11:06:55 +000027static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
Chris Bieneman55de3a22016-12-22 21:58:03 +000028 if (IsLittleEndian != sys::IsLittleEndianHost)
29 sys::swapByteOrder(Integer);
30 OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
31}
32
Benjamin Kramerefcf06f2017-02-11 11:06:55 +000033static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
34 raw_ostream &OS, bool IsLittleEndian) {
Chris Bieneman55de3a22016-12-22 21:58:03 +000035 if (8 == Size)
36 writeInteger((uint64_t)Integer, OS, IsLittleEndian);
37 else if (4 == Size)
38 writeInteger((uint32_t)Integer, OS, IsLittleEndian);
39 else if (2 == Size)
40 writeInteger((uint16_t)Integer, OS, IsLittleEndian);
41 else if (1 == Size)
42 writeInteger((uint8_t)Integer, OS, IsLittleEndian);
43 else
44 assert(false && "Invalid integer write size.");
45}
46
Benjamin Kramerefcf06f2017-02-11 11:06:55 +000047static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
Chris Bieneman313b3262016-12-09 00:26:44 +000048 std::vector<uint8_t> FillData;
49 FillData.insert(FillData.begin(), Size, 0);
50 OS.write(reinterpret_cast<char *>(FillData.data()), Size);
51}
52
Chris Bienemanfaf1feb2017-03-03 21:11:55 +000053void writeInitialLength(const DWARFYAML::InitialLength &Length, raw_ostream &OS,
54 bool IsLittleEndian) {
55 writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
56 if (Length.isDWARF64())
57 writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
58}
59
Chris Bieneman07088c12017-01-12 21:35:21 +000060void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
Chris Bieneman7d7364a2016-12-07 22:30:15 +000061 for (auto Str : DI.DebugStrings) {
62 OS.write(Str.data(), Str.size());
63 OS.write('\0');
64 }
65}
66
Chris Bieneman07088c12017-01-12 21:35:21 +000067void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
Chris Bieneman7d7364a2016-12-07 22:30:15 +000068 for (auto AbbrevDecl : DI.AbbrevDecls) {
69 encodeULEB128(AbbrevDecl.Code, OS);
70 encodeULEB128(AbbrevDecl.Tag, OS);
71 OS.write(AbbrevDecl.Children);
72 for (auto Attr : AbbrevDecl.Attributes) {
73 encodeULEB128(Attr.Attribute, OS);
74 encodeULEB128(Attr.Form, OS);
75 }
76 encodeULEB128(0, OS);
77 encodeULEB128(0, OS);
78 }
79}
Chris Bieneman313b3262016-12-09 00:26:44 +000080
Chris Bieneman07088c12017-01-12 21:35:21 +000081void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
Chris Bieneman313b3262016-12-09 00:26:44 +000082 for (auto Range : DI.ARanges) {
83 auto HeaderStart = OS.tell();
Chris Bienemanfaf1feb2017-03-03 21:11:55 +000084 writeInitialLength(Range.Length, OS, DI.IsLittleEndian);
Chris Bieneman55de3a22016-12-22 21:58:03 +000085 writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
86 writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
87 writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
88 writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
Chris Bieneman313b3262016-12-09 00:26:44 +000089
90 auto HeaderSize = OS.tell() - HeaderStart;
91 auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
92 ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
93
94 for (auto Descriptor : Range.Descriptors) {
Chris Bieneman55de3a22016-12-22 21:58:03 +000095 writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
96 DI.IsLittleEndian);
97 writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
98 DI.IsLittleEndian);
Chris Bieneman313b3262016-12-09 00:26:44 +000099 }
100 ZeroFillBytes(OS, Range.AddrSize * 2);
101 }
102}
Chris Bienemand9430942016-12-19 22:22:12 +0000103
Chris Bieneman07088c12017-01-12 21:35:21 +0000104void DWARFYAML::EmitPubSection(raw_ostream &OS,
105 const DWARFYAML::PubSection &Sect,
106 bool IsLittleEndian) {
Chris Bienemanfaf1feb2017-03-03 21:11:55 +0000107 writeInitialLength(Sect.Length, OS, IsLittleEndian);
Chris Bieneman55de3a22016-12-22 21:58:03 +0000108 writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
109 writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
110 writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
Chris Bienemand9430942016-12-19 22:22:12 +0000111 for (auto Entry : Sect.Entries) {
Chris Bieneman55de3a22016-12-22 21:58:03 +0000112 writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
Chris Bienemand9430942016-12-19 22:22:12 +0000113 if (Sect.IsGNUStyle)
Chris Bieneman55de3a22016-12-22 21:58:03 +0000114 writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
Chris Bienemand9430942016-12-19 22:22:12 +0000115 OS.write(Entry.Name.data(), Entry.Name.size());
116 OS.write('\0');
117 }
Chris Bienemane0e451d2016-12-22 22:44:27 +0000118}
119
Chris Bieneman07088c12017-01-12 21:35:21 +0000120void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
Chris Bienemane0e451d2016-12-22 22:44:27 +0000121
122 for (auto CU : DI.CompileUnits) {
Chris Bienemanfaf1feb2017-03-03 21:11:55 +0000123 writeInitialLength(CU.Length, OS, DI.IsLittleEndian);
Chris Bienemane0e451d2016-12-22 22:44:27 +0000124 writeInteger((uint16_t)CU.Version, OS, DI.IsLittleEndian);
125 writeInteger((uint32_t)CU.AbbrOffset, OS, DI.IsLittleEndian);
126 writeInteger((uint8_t)CU.AddrSize, OS, DI.IsLittleEndian);
127
128 auto FirstAbbrevCode = CU.Entries[0].AbbrCode;
129
130 for (auto Entry : CU.Entries) {
131 encodeULEB128(Entry.AbbrCode, OS);
132 if (Entry.AbbrCode == 0u)
133 continue;
134 bool Indirect = false;
135 assert(Entry.AbbrCode - FirstAbbrevCode < DI.AbbrevDecls.size() &&
136 "Out of range AbbCode");
137 auto &Abbrev = DI.AbbrevDecls[Entry.AbbrCode - FirstAbbrevCode];
138
139 auto FormVal = Entry.Values.begin();
140 auto AbbrForm = Abbrev.Attributes.begin();
141 for (;
142 FormVal != Entry.Values.end() && AbbrForm != Abbrev.Attributes.end();
143 ++FormVal, ++AbbrForm) {
144 dwarf::Form Form = AbbrForm->Form;
145 do {
Chris Bieneman7e984682016-12-22 22:58:07 +0000146 Indirect = false;
Chris Bienemane0e451d2016-12-22 22:44:27 +0000147 switch (Form) {
148 case dwarf::DW_FORM_addr:
149 writeVariableSizedInteger(FormVal->Value, CU.AddrSize, OS,
150 DI.IsLittleEndian);
151 break;
152 case dwarf::DW_FORM_ref_addr: {
153 // TODO: Handle DWARF32/DWARF64 after Line Table data is done
154 auto writeSize = CU.Version == 2 ? CU.AddrSize : 4;
155 writeVariableSizedInteger(FormVal->Value, writeSize, OS,
156 DI.IsLittleEndian);
157 break;
158 }
159 case dwarf::DW_FORM_exprloc:
160 case dwarf::DW_FORM_block:
161 encodeULEB128(FormVal->BlockData.size(), OS);
162 OS.write(reinterpret_cast<char *>(&FormVal->BlockData[0]),
163 FormVal->BlockData.size());
164 break;
165 case dwarf::DW_FORM_block1: {
166 auto writeSize = FormVal->BlockData.size();
167 writeInteger((uint8_t)writeSize, OS, DI.IsLittleEndian);
168 OS.write(reinterpret_cast<char *>(&FormVal->BlockData[0]),
169 FormVal->BlockData.size());
170 break;
171 }
172 case dwarf::DW_FORM_block2: {
173 auto writeSize = FormVal->BlockData.size();
174 writeInteger((uint16_t)writeSize, OS, DI.IsLittleEndian);
175 OS.write(reinterpret_cast<char *>(&FormVal->BlockData[0]),
176 FormVal->BlockData.size());
177 break;
178 }
179 case dwarf::DW_FORM_block4: {
180 auto writeSize = FormVal->BlockData.size();
181 writeInteger((uint32_t)writeSize, OS, DI.IsLittleEndian);
182 OS.write(reinterpret_cast<char *>(&FormVal->BlockData[0]),
183 FormVal->BlockData.size());
184 break;
185 }
186 case dwarf::DW_FORM_data1:
187 case dwarf::DW_FORM_ref1:
188 case dwarf::DW_FORM_flag:
189 writeInteger((uint8_t)FormVal->Value, OS, DI.IsLittleEndian);
190 break;
191 case dwarf::DW_FORM_data2:
192 case dwarf::DW_FORM_ref2:
193 writeInteger((uint16_t)FormVal->Value, OS, DI.IsLittleEndian);
194 break;
195 case dwarf::DW_FORM_data4:
196 case dwarf::DW_FORM_ref4:
197 writeInteger((uint32_t)FormVal->Value, OS, DI.IsLittleEndian);
198 break;
199 case dwarf::DW_FORM_data8:
200 case dwarf::DW_FORM_ref8:
201 writeInteger((uint64_t)FormVal->Value, OS, DI.IsLittleEndian);
202 break;
203 case dwarf::DW_FORM_sdata:
204 encodeSLEB128(FormVal->Value, OS);
205 break;
206 case dwarf::DW_FORM_udata:
207 case dwarf::DW_FORM_ref_udata:
208 encodeULEB128(FormVal->Value, OS);
209 break;
210 case dwarf::DW_FORM_string:
211 OS.write(FormVal->CStr.data(), FormVal->CStr.size());
212 OS.write('\0');
213 break;
214 case dwarf::DW_FORM_indirect:
215 encodeULEB128(FormVal->Value, OS);
216 Indirect = true;
217 Form = static_cast<dwarf::Form>((uint64_t)FormVal->Value);
218 ++FormVal;
219 break;
220 case dwarf::DW_FORM_strp:
221 case dwarf::DW_FORM_sec_offset:
222 case dwarf::DW_FORM_GNU_ref_alt:
223 case dwarf::DW_FORM_GNU_strp_alt:
224 case dwarf::DW_FORM_line_strp:
225 case dwarf::DW_FORM_strp_sup:
226 case dwarf::DW_FORM_ref_sup:
227 // TODO: Handle DWARF32/64
228 writeInteger((uint32_t)FormVal->Value, OS, DI.IsLittleEndian);
229 break;
230 case dwarf::DW_FORM_ref_sig8:
231 writeInteger((uint64_t)FormVal->Value, OS, DI.IsLittleEndian);
232 break;
233 case dwarf::DW_FORM_GNU_addr_index:
234 case dwarf::DW_FORM_GNU_str_index:
235 encodeULEB128(FormVal->Value, OS);
236 break;
237 default:
238 break;
239 }
240 } while (Indirect);
241 }
242 }
243 }
244}
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000245
Benjamin Kramerefcf06f2017-02-11 11:06:55 +0000246static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000247 OS.write(File.Name.data(), File.Name.size());
248 OS.write('\0');
249 encodeULEB128(File.DirIdx, OS);
250 encodeULEB128(File.ModTime, OS);
251 encodeULEB128(File.Length, OS);
252}
253
Chris Bieneman07088c12017-01-12 21:35:21 +0000254void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
Benjamin Kramerefcf06f2017-02-11 11:06:55 +0000255 for (const auto &LineTable : DI.DebugLines) {
Chris Bienemanfaf1feb2017-03-03 21:11:55 +0000256 writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
257 uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000258 writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
259 writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
260 OS, DI.IsLittleEndian);
261 writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
262 if (LineTable.Version >= 4)
263 writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
264 writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
265 writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
266 writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
267 writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
268
269 for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
270 writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
271
272 for (auto IncludeDir : LineTable.IncludeDirs) {
273 OS.write(IncludeDir.data(), IncludeDir.size());
274 OS.write('\0');
275 }
276 OS.write('\0');
277
278 for (auto File : LineTable.Files)
Chris Bieneman07088c12017-01-12 21:35:21 +0000279 EmitFileEntry(OS, File);
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000280 OS.write('\0');
281
282 for (auto Op : LineTable.Opcodes) {
283 writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
284 if (Op.Opcode == 0) {
285 encodeULEB128(Op.ExtLen, OS);
286 writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
287 switch (Op.SubOpcode) {
288 case dwarf::DW_LNE_set_address:
289 case dwarf::DW_LNE_set_discriminator:
290 writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
291 DI.IsLittleEndian);
292 break;
293 case dwarf::DW_LNE_define_file:
Chris Bieneman07088c12017-01-12 21:35:21 +0000294 EmitFileEntry(OS, Op.FileEntry);
Chris Bieneman1b7200d2017-01-10 06:22:49 +0000295 break;
296 case dwarf::DW_LNE_end_sequence:
297 break;
298 default:
299 for (auto OpByte : Op.UnknownOpcodeData)
300 writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
301 }
302 } else if (Op.Opcode < LineTable.OpcodeBase) {
303 switch (Op.Opcode) {
304 case dwarf::DW_LNS_copy:
305 case dwarf::DW_LNS_negate_stmt:
306 case dwarf::DW_LNS_set_basic_block:
307 case dwarf::DW_LNS_const_add_pc:
308 case dwarf::DW_LNS_set_prologue_end:
309 case dwarf::DW_LNS_set_epilogue_begin:
310 break;
311
312 case dwarf::DW_LNS_advance_pc:
313 case dwarf::DW_LNS_set_file:
314 case dwarf::DW_LNS_set_column:
315 case dwarf::DW_LNS_set_isa:
316 encodeULEB128(Op.Data, OS);
317 break;
318
319 case dwarf::DW_LNS_advance_line:
320 encodeSLEB128(Op.SData, OS);
321 break;
322
323 case dwarf::DW_LNS_fixed_advance_pc:
324 writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
325 break;
326
327 default:
328 for (auto OpData : Op.StandardOpcodeData) {
329 encodeULEB128(OpData, OS);
330 }
331 }
332 }
333 }
334 }
335}
Chris Bieneman2e752db2017-01-20 19:03:14 +0000336
337typedef void (*EmitFuncType)(raw_ostream &, const DWARFYAML::Data &);
338
Benjamin Kramerefcf06f2017-02-11 11:06:55 +0000339static void
340EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
341 StringRef Sec,
342 StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
Chris Bieneman2e752db2017-01-20 19:03:14 +0000343 std::string Data;
344 raw_string_ostream DebugInfoStream(Data);
345 EmitFunc(DebugInfoStream, DI);
346 DebugInfoStream.flush();
347 if (!Data.empty())
348 OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
349}
350
351Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
352DWARFYAML::EmitDebugSections(StringRef YAMLString,
353 bool IsLittleEndian) {
354 StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
355
356 yaml::Input YIn(YAMLString);
357
358 DWARFYAML::Data DI;
359 DI.IsLittleEndian = IsLittleEndian;
360 YIn >> DI;
361 if (YIn.error())
362 return errorCodeToError(YIn.error());
363
364 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
365 DebugSections);
366 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
367 DebugSections);
368 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
369 DebugSections);
370 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
371 DebugSections);
372 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
373 DebugSections);
374 return std::move(DebugSections);
375}