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