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   return Visitor.traverseDebugInfo();
271 }
272 
273 static void emitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
274   OS.write(File.Name.data(), File.Name.size());
275   OS.write('\0');
276   encodeULEB128(File.DirIdx, OS);
277   encodeULEB128(File.ModTime, OS);
278   encodeULEB128(File.Length, OS);
279 }
280 
281 Error DWARFYAML::emitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
282   for (const auto &LineTable : DI.DebugLines) {
283     writeInitialLength(LineTable.Format, LineTable.Length, OS,
284                        DI.IsLittleEndian);
285     uint64_t SizeOfPrologueLength = LineTable.Format == dwarf::DWARF64 ? 8 : 4;
286     writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
287     cantFail(writeVariableSizedInteger(
288         LineTable.PrologueLength, SizeOfPrologueLength, OS, DI.IsLittleEndian));
289     writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
290     if (LineTable.Version >= 4)
291       writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
292     writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
293     writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
294     writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
295     writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
296 
297     for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
298       writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
299 
300     for (auto IncludeDir : LineTable.IncludeDirs) {
301       OS.write(IncludeDir.data(), IncludeDir.size());
302       OS.write('\0');
303     }
304     OS.write('\0');
305 
306     for (auto File : LineTable.Files)
307       emitFileEntry(OS, File);
308     OS.write('\0');
309 
310     for (auto Op : LineTable.Opcodes) {
311       writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
312       if (Op.Opcode == 0) {
313         encodeULEB128(Op.ExtLen, OS);
314         writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
315         switch (Op.SubOpcode) {
316         case dwarf::DW_LNE_set_address:
317         case dwarf::DW_LNE_set_discriminator:
318           // TODO: Test this error.
319           if (Error Err = writeVariableSizedInteger(
320                   Op.Data, DI.CompileUnits[0].AddrSize, OS, DI.IsLittleEndian))
321             return Err;
322           break;
323         case dwarf::DW_LNE_define_file:
324           emitFileEntry(OS, Op.FileEntry);
325           break;
326         case dwarf::DW_LNE_end_sequence:
327           break;
328         default:
329           for (auto OpByte : Op.UnknownOpcodeData)
330             writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
331         }
332       } else if (Op.Opcode < LineTable.OpcodeBase) {
333         switch (Op.Opcode) {
334         case dwarf::DW_LNS_copy:
335         case dwarf::DW_LNS_negate_stmt:
336         case dwarf::DW_LNS_set_basic_block:
337         case dwarf::DW_LNS_const_add_pc:
338         case dwarf::DW_LNS_set_prologue_end:
339         case dwarf::DW_LNS_set_epilogue_begin:
340           break;
341 
342         case dwarf::DW_LNS_advance_pc:
343         case dwarf::DW_LNS_set_file:
344         case dwarf::DW_LNS_set_column:
345         case dwarf::DW_LNS_set_isa:
346           encodeULEB128(Op.Data, OS);
347           break;
348 
349         case dwarf::DW_LNS_advance_line:
350           encodeSLEB128(Op.SData, OS);
351           break;
352 
353         case dwarf::DW_LNS_fixed_advance_pc:
354           writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
355           break;
356 
357         default:
358           for (auto OpData : Op.StandardOpcodeData) {
359             encodeULEB128(OpData, OS);
360           }
361         }
362       }
363     }
364   }
365 
366   return Error::success();
367 }
368 
369 Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) {
370   for (const AddrTableEntry &TableEntry : DI.DebugAddr) {
371     uint8_t AddrSize;
372     if (TableEntry.AddrSize)
373       AddrSize = *TableEntry.AddrSize;
374     else
375       AddrSize = DI.Is64bit ? 8 : 4;
376 
377     uint64_t Length;
378     if (TableEntry.Length)
379       Length = (uint64_t)*TableEntry.Length;
380     else
381       // 2 (version) + 1 (address_size) + 1 (segment_selector_size) = 4
382       Length = 4 + (AddrSize + TableEntry.SegSelectorSize) *
383                        TableEntry.SegAddrPairs.size();
384 
385     writeInitialLength(TableEntry.Format, Length, OS, DI.IsLittleEndian);
386     writeInteger((uint16_t)TableEntry.Version, OS, DI.IsLittleEndian);
387     writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
388     writeInteger((uint8_t)TableEntry.SegSelectorSize, OS, DI.IsLittleEndian);
389 
390     for (const SegAddrPair &Pair : TableEntry.SegAddrPairs) {
391       if (TableEntry.SegSelectorSize != 0)
392         if (Error Err = writeVariableSizedInteger(Pair.Segment,
393                                                   TableEntry.SegSelectorSize,
394                                                   OS, DI.IsLittleEndian))
395           return createStringError(errc::not_supported,
396                                    "unable to write debug_addr segment: %s",
397                                    toString(std::move(Err)).c_str());
398       if (AddrSize != 0)
399         if (Error Err = writeVariableSizedInteger(Pair.Address, AddrSize, OS,
400                                                   DI.IsLittleEndian))
401           return createStringError(errc::not_supported,
402                                    "unable to write debug_addr address: %s",
403                                    toString(std::move(Err)).c_str());
404     }
405   }
406 
407   return Error::success();
408 }
409 
410 using EmitFuncType = Error (*)(raw_ostream &, const DWARFYAML::Data &);
411 
412 static Error
413 emitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
414                      StringRef Sec,
415                      StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
416   std::string Data;
417   raw_string_ostream DebugInfoStream(Data);
418   if (Error Err = EmitFunc(DebugInfoStream, DI))
419     return Err;
420   DebugInfoStream.flush();
421   if (!Data.empty())
422     OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
423 
424   return Error::success();
425 }
426 
427 namespace {
428 class DIEFixupVisitor : public DWARFYAML::Visitor {
429   uint64_t Length;
430 
431 public:
432   DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){};
433 
434 private:
435   virtual void onStartCompileUnit(DWARFYAML::Unit &CU) {
436     // Size of the unit header, excluding the length field itself.
437     Length = CU.Version >= 5 ? 8 : 7;
438   }
439 
440   virtual void onEndCompileUnit(DWARFYAML::Unit &CU) {
441     CU.Length.setLength(Length);
442   }
443 
444   virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) {
445     Length += getULEB128Size(DIE.AbbrCode);
446   }
447 
448   virtual void onValue(const uint8_t U) { Length += 1; }
449   virtual void onValue(const uint16_t U) { Length += 2; }
450   virtual void onValue(const uint32_t U) { Length += 4; }
451   virtual void onValue(const uint64_t U, const bool LEB = false) {
452     if (LEB)
453       Length += getULEB128Size(U);
454     else
455       Length += 8;
456   }
457   virtual void onValue(const int64_t S, const bool LEB = false) {
458     if (LEB)
459       Length += getSLEB128Size(S);
460     else
461       Length += 8;
462   }
463   virtual void onValue(const StringRef String) { Length += String.size() + 1; }
464 
465   virtual void onValue(const MemoryBufferRef MBR) {
466     Length += MBR.getBufferSize();
467   }
468 };
469 } // namespace
470 
471 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
472 DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups,
473                              bool IsLittleEndian) {
474   yaml::Input YIn(YAMLString);
475 
476   DWARFYAML::Data DI;
477   DI.IsLittleEndian = IsLittleEndian;
478   YIn >> DI;
479   if (YIn.error())
480     return errorCodeToError(YIn.error());
481 
482   if (ApplyFixups) {
483     DIEFixupVisitor DIFixer(DI);
484     if (Error Err = DIFixer.traverseDebugInfo())
485       return std::move(Err);
486   }
487 
488   StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
489   Error Err = emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, "debug_info",
490                                    DebugSections);
491   Err = joinErrors(std::move(Err),
492                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine,
493                                         "debug_line", DebugSections));
494   Err = joinErrors(std::move(Err),
495                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr,
496                                         "debug_str", DebugSections));
497   Err = joinErrors(std::move(Err),
498                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev,
499                                         "debug_abbrev", DebugSections));
500   Err = joinErrors(std::move(Err),
501                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges,
502                                         "debug_aranges", DebugSections));
503   Err = joinErrors(std::move(Err),
504                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges,
505                                         "debug_ranges", DebugSections));
506 
507   if (Err)
508     return std::move(Err);
509   return std::move(DebugSections);
510 }
511