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 using EmitFuncType = Error (*)(raw_ostream &, const DWARFYAML::Data &);
356 
357 static Error
358 emitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
359                      StringRef Sec,
360                      StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
361   std::string Data;
362   raw_string_ostream DebugInfoStream(Data);
363   if (Error Err = EmitFunc(DebugInfoStream, DI))
364     return Err;
365   DebugInfoStream.flush();
366   if (!Data.empty())
367     OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
368 
369   return Error::success();
370 }
371 
372 namespace {
373 class DIEFixupVisitor : public DWARFYAML::Visitor {
374   uint64_t Length;
375 
376 public:
377   DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){};
378 
379 private:
380   virtual void onStartCompileUnit(DWARFYAML::Unit &CU) {
381     // Size of the unit header, excluding the length field itself.
382     Length = CU.Version >= 5 ? 8 : 7;
383   }
384 
385   virtual void onEndCompileUnit(DWARFYAML::Unit &CU) {
386     CU.Length.setLength(Length);
387   }
388 
389   virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) {
390     Length += getULEB128Size(DIE.AbbrCode);
391   }
392 
393   virtual void onValue(const uint8_t U) { Length += 1; }
394   virtual void onValue(const uint16_t U) { Length += 2; }
395   virtual void onValue(const uint32_t U) { Length += 4; }
396   virtual void onValue(const uint64_t U, const bool LEB = false) {
397     if (LEB)
398       Length += getULEB128Size(U);
399     else
400       Length += 8;
401   }
402   virtual void onValue(const int64_t S, const bool LEB = false) {
403     if (LEB)
404       Length += getSLEB128Size(S);
405     else
406       Length += 8;
407   }
408   virtual void onValue(const StringRef String) { Length += String.size() + 1; }
409 
410   virtual void onValue(const MemoryBufferRef MBR) {
411     Length += MBR.getBufferSize();
412   }
413 };
414 } // namespace
415 
416 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
417 DWARFYAML::emitDebugSections(StringRef YAMLString, bool ApplyFixups,
418                              bool IsLittleEndian) {
419   yaml::Input YIn(YAMLString);
420 
421   DWARFYAML::Data DI;
422   DI.IsLittleEndian = IsLittleEndian;
423   YIn >> DI;
424   if (YIn.error())
425     return errorCodeToError(YIn.error());
426 
427   if (ApplyFixups) {
428     DIEFixupVisitor DIFixer(DI);
429     DIFixer.traverseDebugInfo();
430   }
431 
432   StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
433   Error Err = emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, "debug_info",
434                                    DebugSections);
435   Err = joinErrors(std::move(Err),
436                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine,
437                                         "debug_line", DebugSections));
438   Err = joinErrors(std::move(Err),
439                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr,
440                                         "debug_str", DebugSections));
441   Err = joinErrors(std::move(Err),
442                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev,
443                                         "debug_abbrev", DebugSections));
444   Err = joinErrors(std::move(Err),
445                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges,
446                                         "debug_aranges", DebugSections));
447   Err = joinErrors(std::move(Err),
448                    emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges,
449                                         "debug_ranges", DebugSections));
450 
451   if (Err)
452     return std::move(Err);
453   return std::move(DebugSections);
454 }
455