1 //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// The DWARF component of yaml2obj. Provided as library code for tests.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/ObjectYAML/DWARFEmitter.h"
15 #include "DWARFVisitor.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ObjectYAML/DWARFYAML.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/LEB128.h"
22 #include "llvm/Support/MathExtras.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/SwapByteOrder.h"
25 #include "llvm/Support/YAMLTraits.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <cstddef>
30 #include <cstdint>
31 #include <memory>
32 #include <string>
33 #include <vector>
34
35 using namespace llvm;
36
37 template <typename T>
writeInteger(T Integer,raw_ostream & OS,bool IsLittleEndian)38 static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
39 if (IsLittleEndian != sys::IsLittleEndianHost)
40 sys::swapByteOrder(Integer);
41 OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
42 }
43
writeVariableSizedInteger(uint64_t Integer,size_t Size,raw_ostream & OS,bool IsLittleEndian)44 static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
45 raw_ostream &OS, bool IsLittleEndian) {
46 if (8 == Size)
47 writeInteger((uint64_t)Integer, OS, IsLittleEndian);
48 else if (4 == Size)
49 writeInteger((uint32_t)Integer, OS, IsLittleEndian);
50 else if (2 == Size)
51 writeInteger((uint16_t)Integer, OS, IsLittleEndian);
52 else if (1 == Size)
53 writeInteger((uint8_t)Integer, OS, IsLittleEndian);
54 else
55 assert(false && "Invalid integer write size.");
56 }
57
ZeroFillBytes(raw_ostream & OS,size_t Size)58 static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
59 std::vector<uint8_t> FillData;
60 FillData.insert(FillData.begin(), Size, 0);
61 OS.write(reinterpret_cast<char *>(FillData.data()), Size);
62 }
63
writeInitialLength(const DWARFYAML::InitialLength & Length,raw_ostream & OS,bool IsLittleEndian)64 static void writeInitialLength(const DWARFYAML::InitialLength &Length,
65 raw_ostream &OS, bool IsLittleEndian) {
66 writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
67 if (Length.isDWARF64())
68 writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
69 }
70
EmitDebugStr(raw_ostream & OS,const DWARFYAML::Data & DI)71 void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
72 for (auto Str : DI.DebugStrings) {
73 OS.write(Str.data(), Str.size());
74 OS.write('\0');
75 }
76 }
77
EmitDebugAbbrev(raw_ostream & OS,const DWARFYAML::Data & DI)78 void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
79 for (auto AbbrevDecl : DI.AbbrevDecls) {
80 encodeULEB128(AbbrevDecl.Code, OS);
81 // XXX BINARYEN This is a terminator.
82 if (!AbbrevDecl.Code) {
83 continue;
84 }
85 encodeULEB128(AbbrevDecl.Tag, OS);
86 OS.write(AbbrevDecl.Children);
87 for (auto Attr : AbbrevDecl.Attributes) {
88 encodeULEB128(Attr.Attribute, OS);
89 encodeULEB128(Attr.Form, OS);
90 if (Attr.Form == dwarf::DW_FORM_implicit_const)
91 encodeSLEB128(Attr.Value, OS);
92 }
93 encodeULEB128(0, OS);
94 encodeULEB128(0, OS);
95 }
96 }
97
EmitDebugAranges(raw_ostream & OS,const DWARFYAML::Data & DI)98 void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
99 for (auto Range : DI.ARanges) {
100 auto HeaderStart = OS.tell();
101 writeInitialLength(Range.Length, OS, DI.IsLittleEndian);
102 writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
103 writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
104 writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
105 writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
106
107 auto HeaderSize = OS.tell() - HeaderStart;
108 auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
109 ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
110
111 for (auto Descriptor : Range.Descriptors) {
112 writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
113 DI.IsLittleEndian);
114 writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
115 DI.IsLittleEndian);
116 }
117 ZeroFillBytes(OS, Range.AddrSize * 2);
118 }
119 }
120
121 // XXX BINARYEN
EmitDebugRanges(raw_ostream & OS,const DWARFYAML::Data & DI)122 void DWARFYAML::EmitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
123 // As DwarfStreamer.cpp says, "The debug_range section
124 // format is totally trivial, consisting just of pairs of address
125 // sized addresses describing the ranges." and apparently it ends
126 // with a null termination of a pair of zeros
127 for (auto Range : DI.Ranges) {
128 writeInteger((uint32_t)Range.Start, OS, DI.IsLittleEndian);
129 writeInteger((uint32_t)Range.End, OS, DI.IsLittleEndian);
130 }
131 }
132
133 // XXX BINARYEN
EmitDebugLoc(raw_ostream & OS,const DWARFYAML::Data & DI)134 void DWARFYAML::EmitDebugLoc(raw_ostream &OS, const DWARFYAML::Data &DI) {
135 for (auto Loc : DI.Locs) {
136 writeInteger((uint32_t)Loc.Start, OS, DI.IsLittleEndian);
137 writeInteger((uint32_t)Loc.End, OS, DI.IsLittleEndian);
138 if (Loc.Start == 0 && Loc.End == 0) {
139 // End of a list.
140 continue;
141 }
142 if (Loc.Start != -1) {
143 writeInteger((uint16_t)Loc.Location.size(), OS, DI.IsLittleEndian);
144 for (auto x : Loc.Location) {
145 writeInteger((uint8_t)x, OS, DI.IsLittleEndian);
146 }
147 }
148 }
149 }
150
EmitPubSection(raw_ostream & OS,const DWARFYAML::PubSection & Sect,bool IsLittleEndian)151 void DWARFYAML::EmitPubSection(raw_ostream &OS,
152 const DWARFYAML::PubSection &Sect,
153 bool IsLittleEndian) {
154 writeInitialLength(Sect.Length, OS, IsLittleEndian);
155 writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
156 writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
157 writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
158 for (auto Entry : Sect.Entries) {
159 writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
160 if (Sect.IsGNUStyle)
161 writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
162 OS.write(Entry.Name.data(), Entry.Name.size());
163 OS.write('\0');
164 }
165 }
166
167 namespace {
168 /// An extension of the DWARFYAML::ConstVisitor which writes compile
169 /// units and DIEs to a stream.
170 class DumpVisitor : public DWARFYAML::ConstVisitor {
171 raw_ostream &OS;
172
173 size_t StartPos; // XXX BINARYEN
174
175 protected:
onStartCompileUnit(const DWARFYAML::Unit & CU)176 void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
177 writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
178 StartPos = OS.tell(); // XXX BINARYEN
179 writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
180 if(CU.Version >= 5) {
181 writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
182 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
183 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
184 }else {
185 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
186 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
187 }
188 }
189
190 // XXX BINARYEN Make sure we emit the right size. We should not change the
191 // size as we only modify relocatable fields like addresses, and such fields
192 // have a fixed size, so any change is a bug.
onEndCompileUnit(const DWARFYAML::Unit & CU)193 void onEndCompileUnit(const DWARFYAML::Unit &CU) {
194 size_t EndPos = OS.tell();
195 if (EndPos - StartPos != CU.Length.getLength()) {
196 llvm_unreachable("compile unit size was incorrect");
197 }
198 }
199
onStartDIE(const DWARFYAML::Unit & CU,const DWARFYAML::Entry & DIE)200 void onStartDIE(const DWARFYAML::Unit &CU,
201 const DWARFYAML::Entry &DIE) override {
202 encodeULEB128(DIE.AbbrCode, OS);
203 }
204
onValue(const uint8_t U)205 void onValue(const uint8_t U) override {
206 writeInteger(U, OS, DebugInfo.IsLittleEndian);
207 }
208
onValue(const uint16_t U)209 void onValue(const uint16_t U) override {
210 writeInteger(U, OS, DebugInfo.IsLittleEndian);
211 }
212
onValue(const uint32_t U)213 void onValue(const uint32_t U) override {
214 writeInteger(U, OS, DebugInfo.IsLittleEndian);
215 }
216
onValue(const uint64_t U,const bool LEB=false)217 void onValue(const uint64_t U, const bool LEB = false) override {
218 if (LEB)
219 encodeULEB128(U, OS);
220 else
221 writeInteger(U, OS, DebugInfo.IsLittleEndian);
222 }
223
onValue(const int64_t S,const bool LEB=false)224 void onValue(const int64_t S, const bool LEB = false) override {
225 if (LEB)
226 encodeSLEB128(S, OS);
227 else
228 writeInteger(S, OS, DebugInfo.IsLittleEndian);
229 }
230
onValue(const StringRef String)231 void onValue(const StringRef String) override {
232 OS.write(String.data(), String.size());
233 OS.write('\0');
234 }
235
onValue(const MemoryBufferRef MBR)236 void onValue(const MemoryBufferRef MBR) override {
237 OS.write(MBR.getBufferStart(), MBR.getBufferSize());
238 }
239
240 public:
DumpVisitor(const DWARFYAML::Data & DI,raw_ostream & Out)241 DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
242 : DWARFYAML::ConstVisitor(DI), OS(Out) {}
243 };
244 } // namespace
245
EmitDebugInfo(raw_ostream & OS,const DWARFYAML::Data & DI)246 void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
247 DumpVisitor Visitor(DI, OS);
248 Visitor.traverseDebugInfo();
249 }
250
EmitFileEntry(raw_ostream & OS,const DWARFYAML::File & File)251 static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
252 OS.write(File.Name.data(), File.Name.size());
253 OS.write('\0');
254 encodeULEB128(File.DirIdx, OS);
255 encodeULEB128(File.ModTime, OS);
256 encodeULEB128(File.Length, OS);
257 }
258
259 // XXX BINARYEN: Refactor to an *Internal method that allows us to optionally
260 // compute the new lengths.
EmitDebugLineInternal(raw_ostream & RealOS,const DWARFYAML::Data & DI,std::vector<size_t> * computedLengths)261 static void EmitDebugLineInternal(raw_ostream &RealOS,
262 const DWARFYAML::Data &DI,
263 std::vector<size_t>* computedLengths) {
264 for (auto &LineTable : DI.DebugLines) {
265 // XXX BINARYEN We need to update each line table's length. Write to a
266 // temp stream first, then get the size from that.
267 std::string Buffer;
268 raw_string_ostream OS(Buffer);
269
270 // XXX BINARYEN writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
271
272 uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
273 writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
274 writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
275 OS, DI.IsLittleEndian);
276 writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
277 if (LineTable.Version >= 4)
278 writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
279 writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
280 writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
281 writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
282 writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
283
284 for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
285 writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
286
287 for (auto IncludeDir : LineTable.IncludeDirs) {
288 OS.write(IncludeDir.data(), IncludeDir.size());
289 OS.write('\0');
290 }
291 OS.write('\0');
292
293 for (auto File : LineTable.Files)
294 EmitFileEntry(OS, File);
295 OS.write('\0');
296
297 for (auto Op : LineTable.Opcodes) {
298 writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
299 if (Op.Opcode == 0) {
300 encodeULEB128(Op.ExtLen, OS);
301 writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
302 switch (Op.SubOpcode) {
303 case dwarf::DW_LNE_set_address:
304 case dwarf::DW_LNE_set_discriminator:
305 writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
306 DI.IsLittleEndian);
307 break;
308 case dwarf::DW_LNE_define_file:
309 EmitFileEntry(OS, Op.FileEntry);
310 break;
311 case dwarf::DW_LNE_end_sequence:
312 break;
313 default:
314 for (auto OpByte : Op.UnknownOpcodeData)
315 writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
316 }
317 } else if (Op.Opcode < LineTable.OpcodeBase) {
318 switch (Op.Opcode) {
319 case dwarf::DW_LNS_copy:
320 case dwarf::DW_LNS_negate_stmt:
321 case dwarf::DW_LNS_set_basic_block:
322 case dwarf::DW_LNS_const_add_pc:
323 case dwarf::DW_LNS_set_prologue_end:
324 case dwarf::DW_LNS_set_epilogue_begin:
325 break;
326
327 case dwarf::DW_LNS_advance_pc:
328 case dwarf::DW_LNS_set_file:
329 case dwarf::DW_LNS_set_column:
330 case dwarf::DW_LNS_set_isa:
331 encodeULEB128(Op.Data, OS);
332 break;
333
334 case dwarf::DW_LNS_advance_line:
335 encodeSLEB128(Op.SData, OS);
336 break;
337
338 case dwarf::DW_LNS_fixed_advance_pc:
339 writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
340 break;
341
342 default:
343 for (auto OpData : Op.StandardOpcodeData) {
344 encodeULEB128(OpData, OS);
345 }
346 }
347 }
348 }
349 // XXX BINARYEN Write to the actual stream, with the proper size.
350 // We assume for now that the length fits in 32 bits.
351 size_t Size = OS.str().size();
352 if (Size >= UINT32_MAX) {
353 llvm_unreachable("Table is too big");
354 }
355 if (computedLengths) {
356 computedLengths->push_back(Size);
357 }
358 writeInteger((uint32_t)Size, RealOS, DI.IsLittleEndian);
359 RealOS << OS.str();
360 }
361 }
362
EmitDebugLine(raw_ostream & RealOS,const DWARFYAML::Data & DI)363 void DWARFYAML::EmitDebugLine(raw_ostream &RealOS, const DWARFYAML::Data &DI) {
364 EmitDebugLineInternal(RealOS, DI, nullptr);
365 }
366
ComputeDebugLine(Data & DI,std::vector<size_t> & computedLengths)367 void DWARFYAML::ComputeDebugLine(Data &DI,
368 std::vector<size_t>& computedLengths) {
369 // TODO: Avoid writing out the data, or at least cache it so we don't need to
370 // do it again later.
371 std::string buffer;
372 llvm::raw_string_ostream tempStream(buffer);
373 EmitDebugLineInternal(tempStream, DI, &computedLengths);
374 }
375
376 using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &);
377
378 static void
EmitDebugSectionImpl(const DWARFYAML::Data & DI,EmitFuncType EmitFunc,StringRef Sec,StringMap<std::unique_ptr<MemoryBuffer>> & OutputBuffers)379 EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
380 StringRef Sec,
381 StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
382 std::string Data;
383 raw_string_ostream DebugInfoStream(Data);
384 EmitFunc(DebugInfoStream, DI);
385 DebugInfoStream.flush();
386 if (!Data.empty())
387 OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
388 }
389
390 namespace {
391 class DIEFixupVisitor : public DWARFYAML::Visitor {
392 uint64_t Length;
393
394 public:
DIEFixupVisitor(DWARFYAML::Data & DI)395 DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){};
396
397 private:
onStartCompileUnit(DWARFYAML::Unit & CU)398 virtual void onStartCompileUnit(DWARFYAML::Unit &CU) { Length = 7; }
399
onEndCompileUnit(DWARFYAML::Unit & CU)400 virtual void onEndCompileUnit(DWARFYAML::Unit &CU) {
401 CU.Length.setLength(Length);
402 }
403
onStartDIE(DWARFYAML::Unit & CU,DWARFYAML::Entry & DIE)404 virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) {
405 Length += getULEB128Size(DIE.AbbrCode);
406 }
407
onValue(const uint8_t U)408 virtual void onValue(const uint8_t U) { Length += 1; }
onValue(const uint16_t U)409 virtual void onValue(const uint16_t U) { Length += 2; }
onValue(const uint32_t U)410 virtual void onValue(const uint32_t U) { Length += 4; }
onValue(const uint64_t U,const bool LEB=false)411 virtual void onValue(const uint64_t U, const bool LEB = false) {
412 if (LEB)
413 Length += getULEB128Size(U);
414 else
415 Length += 8;
416 }
onValue(const int64_t S,const bool LEB=false)417 virtual void onValue(const int64_t S, const bool LEB = false) {
418 if (LEB)
419 Length += getSLEB128Size(S);
420 else
421 Length += 8;
422 }
onValue(const StringRef String)423 virtual void onValue(const StringRef String) { Length += String.size() + 1; }
424
onValue(const MemoryBufferRef MBR)425 virtual void onValue(const MemoryBufferRef MBR) {
426 Length += MBR.getBufferSize();
427 }
428 };
429 } // namespace
430
431 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
EmitDebugSections(StringRef YAMLString,bool ApplyFixups,bool IsLittleEndian)432 DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups,
433 bool IsLittleEndian) {
434 yaml::Input YIn(YAMLString);
435
436 DWARFYAML::Data DI;
437 DI.IsLittleEndian = IsLittleEndian;
438 YIn >> DI;
439 if (YIn.error())
440 return errorCodeToError(YIn.error());
441
442 if (ApplyFixups) {
443 DIEFixupVisitor DIFixer(DI);
444 DIFixer.traverseDebugInfo();
445 }
446
447 StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
448 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
449 DebugSections);
450 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
451 DebugSections);
452 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
453 DebugSections);
454 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
455 DebugSections);
456 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
457 DebugSections);
458 return std::move(DebugSections);
459 }
460
461 // XXX BINARYEN <--
462 namespace llvm {
463 namespace DWARFYAML {
464 StringMap<std::unique_ptr<MemoryBuffer>>
EmitDebugSections(llvm::DWARFYAML::Data & DI,bool ApplyFixups)465 EmitDebugSections(llvm::DWARFYAML::Data &DI, bool ApplyFixups) {
466 if (ApplyFixups) {
467 DIEFixupVisitor DIFixer(DI);
468 DIFixer.traverseDebugInfo();
469 }
470
471 StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
472 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
473 DebugSections);
474 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
475 DebugSections);
476 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
477 DebugSections);
478 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
479 DebugSections);
480 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
481 DebugSections);
482 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugRanges, "debug_ranges",
483 DebugSections); // XXX BINARYEN
484 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLoc, "debug_loc",
485 DebugSections); // XXX BINARYEN
486 return std::move(DebugSections);
487 }
488 } // namespace DWARFYAML
489 } // namespace llvm
490 // XXX BINARYEN -->
491