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/BinaryFormat/Dwarf.h"
19 #include "llvm/ObjectYAML/DWARFYAML.h"
20 #include "llvm/Support/Errc.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/Host.h"
23 #include "llvm/Support/LEB128.h"
24 #include "llvm/Support/MathExtras.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SwapByteOrder.h"
27 #include "llvm/Support/YAMLTraits.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cstddef>
32 #include <cstdint>
33 #include <memory>
34 #include <string>
35 #include <vector>
36 
37 using namespace llvm;
38 
39 template <typename T>
40 static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
41   if (IsLittleEndian != sys::IsLittleEndianHost)
42     sys::swapByteOrder(Integer);
43   OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
44 }
45 
46 static Error writeVariableSizedInteger(uint64_t Integer, size_t Size,
47                                        raw_ostream &OS, bool IsLittleEndian) {
48   if (8 == Size)
49     writeInteger((uint64_t)Integer, OS, IsLittleEndian);
50   else if (4 == Size)
51     writeInteger((uint32_t)Integer, OS, IsLittleEndian);
52   else if (2 == Size)
53     writeInteger((uint16_t)Integer, OS, IsLittleEndian);
54   else if (1 == Size)
55     writeInteger((uint8_t)Integer, OS, IsLittleEndian);
56   else
57     return createStringError(errc::not_supported,
58                              "invalid integer write size: %zu", Size);
59 
60   return Error::success();
61 }
62 
63 static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
64   std::vector<uint8_t> FillData;
65   FillData.insert(FillData.begin(), Size, 0);
66   OS.write(reinterpret_cast<char *>(FillData.data()), Size);
67 }
68 
69 static void writeInitialLength(const DWARFYAML::InitialLength &Length,
70                                raw_ostream &OS, bool IsLittleEndian) {
71   writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
72   if (Length.isDWARF64())
73     writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
74 }
75 
76 static void writeInitialLength(const dwarf::DwarfFormat Format,
77                                const uint64_t Length, raw_ostream &OS,
78                                bool IsLittleEndian) {
79   bool IsDWARF64 = Format == dwarf::DWARF64;
80   if (IsDWARF64)
81     cantFail(writeVariableSizedInteger(dwarf::DW_LENGTH_DWARF64, 4, OS,
82                                        IsLittleEndian));
83   cantFail(
84       writeVariableSizedInteger(Length, IsDWARF64 ? 8 : 4, OS, IsLittleEndian));
85 }
86 
87 Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
88   for (auto Str : DI.DebugStrings) {
89     OS.write(Str.data(), Str.size());
90     OS.write('\0');
91   }
92 
93   return Error::success();
94 }
95 
96 Error DWARFYAML::emitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
97   uint64_t AbbrevCode = 0;
98   for (auto AbbrevDecl : DI.AbbrevDecls) {
99     AbbrevCode = AbbrevDecl.Code ? (uint64_t)*AbbrevDecl.Code : AbbrevCode + 1;
100     encodeULEB128(AbbrevCode, OS);
101     encodeULEB128(AbbrevDecl.Tag, OS);
102     OS.write(AbbrevDecl.Children);
103     for (auto Attr : AbbrevDecl.Attributes) {
104       encodeULEB128(Attr.Attribute, OS);
105       encodeULEB128(Attr.Form, OS);
106       if (Attr.Form == dwarf::DW_FORM_implicit_const)
107         encodeSLEB128(Attr.Value, OS);
108     }
109     encodeULEB128(0, OS);
110     encodeULEB128(0, OS);
111   }
112 
113   return Error::success();
114 }
115 
116 Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
117   for (auto Range : DI.ARanges) {
118     auto HeaderStart = OS.tell();
119     writeInitialLength(Range.Format, Range.Length, OS, DI.IsLittleEndian);
120     writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
121     if (Range.Format == dwarf::DWARF64)
122       writeInteger((uint64_t)Range.CuOffset, OS, DI.IsLittleEndian);
123     else
124       writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
125     writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
126     writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
127 
128     auto HeaderSize = OS.tell() - HeaderStart;
129     auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
130     ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
131 
132     for (auto Descriptor : Range.Descriptors) {
133       if (Error Err = writeVariableSizedInteger(
134               Descriptor.Address, Range.AddrSize, OS, DI.IsLittleEndian))
135         return createStringError(errc::not_supported,
136                                  "unable to write debug_aranges address: %s",
137                                  toString(std::move(Err)).c_str());
138       cantFail(writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
139                                          DI.IsLittleEndian));
140     }
141     ZeroFillBytes(OS, Range.AddrSize * 2);
142   }
143 
144   return Error::success();
145 }
146 
147 Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
148   const size_t RangesOffset = OS.tell();
149   uint64_t EntryIndex = 0;
150   for (auto DebugRanges : DI.DebugRanges) {
151     const size_t CurrOffset = OS.tell() - RangesOffset;
152     if (DebugRanges.Offset && (uint64_t)*DebugRanges.Offset < CurrOffset)
153       return createStringError(errc::invalid_argument,
154                                "'Offset' for 'debug_ranges' with index " +
155                                    Twine(EntryIndex) +
156                                    " must be greater than or equal to the "
157                                    "number of bytes written already (0x" +
158                                    Twine::utohexstr(CurrOffset) + ")");
159     if (DebugRanges.Offset)
160       ZeroFillBytes(OS, *DebugRanges.Offset - CurrOffset);
161 
162     uint8_t AddrSize;
163     if (DebugRanges.AddrSize)
164       AddrSize = *DebugRanges.AddrSize;
165     else
166       AddrSize = DI.Is64bit ? 8 : 4;
167     for (auto Entry : DebugRanges.Entries) {
168       if (Error Err = writeVariableSizedInteger(Entry.LowOffset, AddrSize, OS,
169                                                 DI.IsLittleEndian))
170         return createStringError(
171             errc::not_supported,
172             "unable to write debug_ranges address offset: %s",
173             toString(std::move(Err)).c_str());
174       cantFail(writeVariableSizedInteger(Entry.HighOffset, AddrSize, OS,
175                                          DI.IsLittleEndian));
176     }
177     ZeroFillBytes(OS, AddrSize * 2);
178     ++EntryIndex;
179   }
180 
181   return Error::success();
182 }
183 
184 Error DWARFYAML::emitPubSection(raw_ostream &OS,
185                                 const DWARFYAML::PubSection &Sect,
186                                 bool IsLittleEndian) {
187   writeInitialLength(Sect.Length, OS, IsLittleEndian);
188   writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
189   writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
190   writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
191   for (auto Entry : Sect.Entries) {
192     writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
193     if (Sect.IsGNUStyle)
194       writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
195     OS.write(Entry.Name.data(), Entry.Name.size());
196     OS.write('\0');
197   }
198 
199   return Error::success();
200 }
201 
202 namespace {
203 /// An extension of the DWARFYAML::ConstVisitor which writes compile
204 /// units and DIEs to a stream.
205 class DumpVisitor : public DWARFYAML::ConstVisitor {
206   raw_ostream &OS;
207 
208 protected:
209   void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
210     writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
211     writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
212     if(CU.Version >= 5) {
213       writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
214       writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
215       writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
216     }else {
217       writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
218       writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
219     }
220   }
221 
222   void onStartDIE(const DWARFYAML::Unit &CU,
223                   const DWARFYAML::Entry &DIE) override {
224     encodeULEB128(DIE.AbbrCode, OS);
225   }
226 
227   void onValue(const uint8_t U) override {
228     writeInteger(U, OS, DebugInfo.IsLittleEndian);
229   }
230 
231   void onValue(const uint16_t U) override {
232     writeInteger(U, OS, DebugInfo.IsLittleEndian);
233   }
234 
235   void onValue(const uint32_t U) override {
236     writeInteger(U, OS, DebugInfo.IsLittleEndian);
237   }
238 
239   void onValue(const uint64_t U, const bool LEB = false) override {
240     if (LEB)
241       encodeULEB128(U, OS);
242     else
243       writeInteger(U, OS, DebugInfo.IsLittleEndian);
244   }
245 
246   void onValue(const int64_t S, const bool LEB = false) override {
247     if (LEB)
248       encodeSLEB128(S, OS);
249     else
250       writeInteger(S, OS, DebugInfo.IsLittleEndian);
251   }
252 
253   void onValue(const StringRef String) override {
254     OS.write(String.data(), String.size());
255     OS.write('\0');
256   }
257 
258   void onValue(const MemoryBufferRef MBR) override {
259     OS.write(MBR.getBufferStart(), MBR.getBufferSize());
260   }
261 
262 public:
263   DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
264       : DWARFYAML::ConstVisitor(DI), OS(Out) {}
265 };
266 } // namespace
267 
268 Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
269   DumpVisitor Visitor(DI, OS);
270   Visitor.traverseDebugInfo();
271 
272   return Error::success();
273 }
274 
275 static void emitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
276   OS.write(File.Name.data(), File.Name.size());
277   OS.write('\0');
278   encodeULEB128(File.DirIdx, OS);
279   encodeULEB128(File.ModTime, OS);
280   encodeULEB128(File.Length, OS);
281 }
282 
283 Error DWARFYAML::emitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
284   for (const auto &LineTable : DI.DebugLines) {
285     writeInitialLength(LineTable.Format, LineTable.Length, OS,
286                        DI.IsLittleEndian);
287     uint64_t SizeOfPrologueLength = LineTable.Format == dwarf::DWARF64 ? 8 : 4;
288     writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
289     cantFail(writeVariableSizedInteger(
290         LineTable.PrologueLength, SizeOfPrologueLength, OS, DI.IsLittleEndian));
291     writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
292     if (LineTable.Version >= 4)
293       writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
294     writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
295     writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
296     writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
297     writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
298 
299     for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
300       writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
301 
302     for (auto IncludeDir : LineTable.IncludeDirs) {
303       OS.write(IncludeDir.data(), IncludeDir.size());
304       OS.write('\0');
305     }
306     OS.write('\0');
307 
308     for (auto File : LineTable.Files)
309       emitFileEntry(OS, File);
310     OS.write('\0');
311 
312     for (auto Op : LineTable.Opcodes) {
313       writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
314       if (Op.Opcode == 0) {
315         encodeULEB128(Op.ExtLen, OS);
316         writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
317         switch (Op.SubOpcode) {
318         case dwarf::DW_LNE_set_address:
319         case dwarf::DW_LNE_set_discriminator:
320           // TODO: Test this error.
321           if (Error Err = writeVariableSizedInteger(
322                   Op.Data, DI.CompileUnits[0].AddrSize, OS, DI.IsLittleEndian))
323             return Err;
324           break;
325         case dwarf::DW_LNE_define_file:
326           emitFileEntry(OS, Op.FileEntry);
327           break;
328         case dwarf::DW_LNE_end_sequence:
329           break;
330         default:
331           for (auto OpByte : Op.UnknownOpcodeData)
332             writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
333         }
334       } else if (Op.Opcode < LineTable.OpcodeBase) {
335         switch (Op.Opcode) {
336         case dwarf::DW_LNS_copy:
337         case dwarf::DW_LNS_negate_stmt:
338         case dwarf::DW_LNS_set_basic_block:
339         case dwarf::DW_LNS_const_add_pc:
340         case dwarf::DW_LNS_set_prologue_end:
341         case dwarf::DW_LNS_set_epilogue_begin:
342           break;
343 
344         case dwarf::DW_LNS_advance_pc:
345         case dwarf::DW_LNS_set_file:
346         case dwarf::DW_LNS_set_column:
347         case dwarf::DW_LNS_set_isa:
348           encodeULEB128(Op.Data, OS);
349           break;
350 
351         case dwarf::DW_LNS_advance_line:
352           encodeSLEB128(Op.SData, OS);
353           break;
354 
355         case dwarf::DW_LNS_fixed_advance_pc:
356           writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
357           break;
358 
359         default:
360           for (auto OpData : Op.StandardOpcodeData) {
361             encodeULEB128(OpData, OS);
362           }
363         }
364       }
365     }
366   }
367 
368   return Error::success();
369 }
370 
371 Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) {
372   for (const AddrTableEntry &TableEntry : DI.DebugAddr) {
373     uint8_t AddrSize;
374     if (TableEntry.AddrSize)
375       AddrSize = *TableEntry.AddrSize;
376     else
377       AddrSize = DI.Is64bit ? 8 : 4;
378 
379     uint64_t Length;
380     if (TableEntry.Length)
381       Length = (uint64_t)*TableEntry.Length;
382     else
383       // 2 (version) + 1 (address_size) + 1 (segment_selector_size) = 4
384       Length = 4 + (AddrSize + TableEntry.SegSelectorSize) *
385                        TableEntry.SegAddrPairs.size();
386 
387     writeInitialLength(TableEntry.Format, Length, OS, DI.IsLittleEndian);
388     writeInteger((uint16_t)TableEntry.Version, OS, DI.IsLittleEndian);
389     writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
390     writeInteger((uint8_t)TableEntry.SegSelectorSize, OS, DI.IsLittleEndian);
391 
392     for (const SegAddrPair &Pair : TableEntry.SegAddrPairs) {
393       if (TableEntry.SegSelectorSize != 0)
394         if (Error Err = writeVariableSizedInteger(Pair.Segment,
395                                                   TableEntry.SegSelectorSize,
396                                                   OS, DI.IsLittleEndian))
397           return createStringError(errc::not_supported,
398                                    "unable to write debug_addr segment: %s",
399                                    toString(std::move(Err)).c_str());
400       if (AddrSize != 0)
401         if (Error Err = writeVariableSizedInteger(Pair.Address, AddrSize, OS,
402                                                   DI.IsLittleEndian))
403           return createStringError(errc::not_supported,
404                                    "unable to write debug_addr address: %s",
405                                    toString(std::move(Err)).c_str());
406     }
407   }
408 
409   return Error::success();
410 }
411 
412 using EmitFuncType = Error (*)(raw_ostream &, const DWARFYAML::Data &);
413 
414 static Error
415 emitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
416                      StringRef Sec,
417                      StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
418   std::string Data;
419   raw_string_ostream DebugInfoStream(Data);
420   if (Error Err = EmitFunc(DebugInfoStream, DI))
421     return Err;
422   DebugInfoStream.flush();
423   if (!Data.empty())
424     OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
425 
426   return Error::success();
427 }
428 
429 namespace {
430 class DIEFixupVisitor : public DWARFYAML::Visitor {
431   uint64_t Length;
432 
433 public:
434   DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){};
435 
436 private:
437   virtual void onStartCompileUnit(DWARFYAML::Unit &CU) {
438     // Size of the unit header, excluding the length field itself.
439     Length = CU.Version >= 5 ? 8 : 7;
440   }
441 
442   virtual void onEndCompileUnit(DWARFYAML::Unit &CU) {
443     CU.Length.setLength(Length);
444   }
445 
446   virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) {
447     Length += getULEB128Size(DIE.AbbrCode);
448   }
449 
450   virtual void onValue(const uint8_t U) { Length += 1; }
451   virtual void onValue(const uint16_t U) { Length += 2; }
452   virtual void onValue(const uint32_t U) { Length += 4; }
453   virtual void onValue(const uint64_t U, const bool LEB = false) {
454     if (LEB)
455       Length += getULEB128Size(U);
456     else
457       Length += 8;
458   }
459   virtual void onValue(const int64_t S, const bool LEB = false) {
460     if (LEB)
461       Length += getSLEB128Size(S);
462     else
463       Length += 8;
464   }
465   virtual void onValue(const StringRef String) { Length += String.size() + 1; }
466 
467   virtual void onValue(const MemoryBufferRef MBR) {
468     Length += MBR.getBufferSize();
469   }
470 };
471 } // namespace
472 
473 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
474 DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups,
475                              bool IsLittleEndian) {
476   yaml::Input YIn(YAMLString);
477 
478   DWARFYAML::Data DI;
479   DI.IsLittleEndian = IsLittleEndian;
480   YIn >> DI;
481   if (YIn.error())
482     return errorCodeToError(YIn.error());
483 
484   if (ApplyFixups) {
485     DIEFixupVisitor DIFixer(DI);
486     DIFixer.traverseDebugInfo();
487   }
488 
489   StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
490   Error Err = emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, "debug_info",
491                                    DebugSections);
492   Err = joinErrors(std::move(Err),
493                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine,
494                                         "debug_line", DebugSections));
495   Err = joinErrors(std::move(Err),
496                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr,
497                                         "debug_str", DebugSections));
498   Err = joinErrors(std::move(Err),
499                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev,
500                                         "debug_abbrev", DebugSections));
501   Err = joinErrors(std::move(Err),
502                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges,
503                                         "debug_aranges", DebugSections));
504   Err = joinErrors(std::move(Err),
505                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges,
506                                         "debug_ranges", DebugSections));
507 
508   if (Err)
509     return std::move(Err);
510   return std::move(DebugSections);
511 }
512