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