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/Error.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Support/LEB128.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/SwapByteOrder.h"
26 #include "llvm/Support/YAMLTraits.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <cstddef>
31 #include <cstdint>
32 #include <memory>
33 #include <string>
34 #include <vector>
35 
36 using namespace llvm;
37 
38 template <typename T>
39 static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
40   if (IsLittleEndian != sys::IsLittleEndianHost)
41     sys::swapByteOrder(Integer);
42   OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
43 }
44 
45 static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
46                                       raw_ostream &OS, bool IsLittleEndian) {
47   if (8 == Size)
48     writeInteger((uint64_t)Integer, OS, IsLittleEndian);
49   else if (4 == Size)
50     writeInteger((uint32_t)Integer, OS, IsLittleEndian);
51   else if (2 == Size)
52     writeInteger((uint16_t)Integer, OS, IsLittleEndian);
53   else if (1 == Size)
54     writeInteger((uint8_t)Integer, OS, IsLittleEndian);
55   else
56     assert(false && "Invalid integer write size.");
57 }
58 
59 static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
60   std::vector<uint8_t> FillData;
61   FillData.insert(FillData.begin(), Size, 0);
62   OS.write(reinterpret_cast<char *>(FillData.data()), Size);
63 }
64 
65 static void writeInitialLength(const DWARFYAML::InitialLength &Length,
66                                raw_ostream &OS, bool IsLittleEndian) {
67   writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
68   if (Length.isDWARF64())
69     writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
70 }
71 
72 void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
73   for (auto Str : DI.DebugStrings) {
74     OS.write(Str.data(), Str.size());
75     OS.write('\0');
76   }
77 }
78 
79 void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
80   for (auto AbbrevDecl : DI.AbbrevDecls) {
81     encodeULEB128(AbbrevDecl.Code, OS);
82     encodeULEB128(AbbrevDecl.Tag, OS);
83     OS.write(AbbrevDecl.Children);
84     for (auto Attr : AbbrevDecl.Attributes) {
85       encodeULEB128(Attr.Attribute, OS);
86       encodeULEB128(Attr.Form, OS);
87       if (Attr.Form == dwarf::DW_FORM_implicit_const)
88         encodeSLEB128(Attr.Value, OS);
89     }
90     encodeULEB128(0, OS);
91     encodeULEB128(0, OS);
92   }
93 }
94 
95 void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
96   for (auto Range : DI.ARanges) {
97     auto HeaderStart = OS.tell();
98     if (Range.Format == dwarf::DWARF64) {
99       writeInteger((uint32_t)dwarf::DW_LENGTH_DWARF64, OS, DI.IsLittleEndian);
100       writeInteger((uint64_t)Range.Length, OS, DI.IsLittleEndian);
101     } else
102       writeInteger((uint32_t)Range.Length, OS, DI.IsLittleEndian);
103     writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
104     writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
105     writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
106     writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
107 
108     auto HeaderSize = OS.tell() - HeaderStart;
109     auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
110     ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
111 
112     for (auto Descriptor : Range.Descriptors) {
113       writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
114                                 DI.IsLittleEndian);
115       writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
116                                 DI.IsLittleEndian);
117     }
118     ZeroFillBytes(OS, Range.AddrSize * 2);
119   }
120 }
121 
122 void DWARFYAML::EmitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
123   const size_t RangesOffset = OS.tell();
124   for (auto DebugRanges : DI.DebugRanges) {
125     const size_t CurrOffset = OS.tell() - RangesOffset;
126     assert(DebugRanges.Offset <= CurrOffset);
127     if (DebugRanges.Offset > CurrOffset)
128       ZeroFillBytes(OS, DebugRanges.Offset - CurrOffset);
129     for (auto Entry : DebugRanges.Entries) {
130       writeVariableSizedInteger(Entry.LowOffset, DebugRanges.AddrSize, OS,
131                                 DI.IsLittleEndian);
132       writeVariableSizedInteger(Entry.HighOffset, DebugRanges.AddrSize, OS,
133                                 DI.IsLittleEndian);
134     }
135     ZeroFillBytes(OS, DebugRanges.AddrSize * 2);
136   }
137 }
138 
139 void DWARFYAML::EmitPubSection(raw_ostream &OS,
140                                const DWARFYAML::PubSection &Sect,
141                                bool IsLittleEndian) {
142   writeInitialLength(Sect.Length, OS, IsLittleEndian);
143   writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
144   writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
145   writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
146   for (auto Entry : Sect.Entries) {
147     writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
148     if (Sect.IsGNUStyle)
149       writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
150     OS.write(Entry.Name.data(), Entry.Name.size());
151     OS.write('\0');
152   }
153 }
154 
155 namespace {
156 /// An extension of the DWARFYAML::ConstVisitor which writes compile
157 /// units and DIEs to a stream.
158 class DumpVisitor : public DWARFYAML::ConstVisitor {
159   raw_ostream &OS;
160 
161 protected:
162   void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
163     writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
164     writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
165     if(CU.Version >= 5) {
166       writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
167       writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
168       writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
169     }else {
170       writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
171       writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
172     }
173   }
174 
175   void onStartDIE(const DWARFYAML::Unit &CU,
176                   const DWARFYAML::Entry &DIE) override {
177     encodeULEB128(DIE.AbbrCode, OS);
178   }
179 
180   void onValue(const uint8_t U) override {
181     writeInteger(U, OS, DebugInfo.IsLittleEndian);
182   }
183 
184   void onValue(const uint16_t U) override {
185     writeInteger(U, OS, DebugInfo.IsLittleEndian);
186   }
187 
188   void onValue(const uint32_t U) override {
189     writeInteger(U, OS, DebugInfo.IsLittleEndian);
190   }
191 
192   void onValue(const uint64_t U, const bool LEB = false) override {
193     if (LEB)
194       encodeULEB128(U, OS);
195     else
196       writeInteger(U, OS, DebugInfo.IsLittleEndian);
197   }
198 
199   void onValue(const int64_t S, const bool LEB = false) override {
200     if (LEB)
201       encodeSLEB128(S, OS);
202     else
203       writeInteger(S, OS, DebugInfo.IsLittleEndian);
204   }
205 
206   void onValue(const StringRef String) override {
207     OS.write(String.data(), String.size());
208     OS.write('\0');
209   }
210 
211   void onValue(const MemoryBufferRef MBR) override {
212     OS.write(MBR.getBufferStart(), MBR.getBufferSize());
213   }
214 
215 public:
216   DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
217       : DWARFYAML::ConstVisitor(DI), OS(Out) {}
218 };
219 } // namespace
220 
221 void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
222   DumpVisitor Visitor(DI, OS);
223   Visitor.traverseDebugInfo();
224 }
225 
226 static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
227   OS.write(File.Name.data(), File.Name.size());
228   OS.write('\0');
229   encodeULEB128(File.DirIdx, OS);
230   encodeULEB128(File.ModTime, OS);
231   encodeULEB128(File.Length, OS);
232 }
233 
234 void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
235   for (const auto &LineTable : DI.DebugLines) {
236     writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
237     uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
238     writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
239     writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
240                               OS, DI.IsLittleEndian);
241     writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
242     if (LineTable.Version >= 4)
243       writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
244     writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
245     writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
246     writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
247     writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
248 
249     for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
250       writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
251 
252     for (auto IncludeDir : LineTable.IncludeDirs) {
253       OS.write(IncludeDir.data(), IncludeDir.size());
254       OS.write('\0');
255     }
256     OS.write('\0');
257 
258     for (auto File : LineTable.Files)
259       EmitFileEntry(OS, File);
260     OS.write('\0');
261 
262     for (auto Op : LineTable.Opcodes) {
263       writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
264       if (Op.Opcode == 0) {
265         encodeULEB128(Op.ExtLen, OS);
266         writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
267         switch (Op.SubOpcode) {
268         case dwarf::DW_LNE_set_address:
269         case dwarf::DW_LNE_set_discriminator:
270           writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
271                                     DI.IsLittleEndian);
272           break;
273         case dwarf::DW_LNE_define_file:
274           EmitFileEntry(OS, Op.FileEntry);
275           break;
276         case dwarf::DW_LNE_end_sequence:
277           break;
278         default:
279           for (auto OpByte : Op.UnknownOpcodeData)
280             writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
281         }
282       } else if (Op.Opcode < LineTable.OpcodeBase) {
283         switch (Op.Opcode) {
284         case dwarf::DW_LNS_copy:
285         case dwarf::DW_LNS_negate_stmt:
286         case dwarf::DW_LNS_set_basic_block:
287         case dwarf::DW_LNS_const_add_pc:
288         case dwarf::DW_LNS_set_prologue_end:
289         case dwarf::DW_LNS_set_epilogue_begin:
290           break;
291 
292         case dwarf::DW_LNS_advance_pc:
293         case dwarf::DW_LNS_set_file:
294         case dwarf::DW_LNS_set_column:
295         case dwarf::DW_LNS_set_isa:
296           encodeULEB128(Op.Data, OS);
297           break;
298 
299         case dwarf::DW_LNS_advance_line:
300           encodeSLEB128(Op.SData, OS);
301           break;
302 
303         case dwarf::DW_LNS_fixed_advance_pc:
304           writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
305           break;
306 
307         default:
308           for (auto OpData : Op.StandardOpcodeData) {
309             encodeULEB128(OpData, OS);
310           }
311         }
312       }
313     }
314   }
315 }
316 
317 using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &);
318 
319 static void
320 EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
321                      StringRef Sec,
322                      StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
323   std::string Data;
324   raw_string_ostream DebugInfoStream(Data);
325   EmitFunc(DebugInfoStream, DI);
326   DebugInfoStream.flush();
327   if (!Data.empty())
328     OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
329 }
330 
331 namespace {
332 class DIEFixupVisitor : public DWARFYAML::Visitor {
333   uint64_t Length;
334 
335 public:
336   DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){};
337 
338 private:
339   virtual void onStartCompileUnit(DWARFYAML::Unit &CU) {
340     // Size of the unit header, excluding the length field itself.
341     Length = CU.Version >= 5 ? 8 : 7;
342   }
343 
344   virtual void onEndCompileUnit(DWARFYAML::Unit &CU) {
345     CU.Length.setLength(Length);
346   }
347 
348   virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) {
349     Length += getULEB128Size(DIE.AbbrCode);
350   }
351 
352   virtual void onValue(const uint8_t U) { Length += 1; }
353   virtual void onValue(const uint16_t U) { Length += 2; }
354   virtual void onValue(const uint32_t U) { Length += 4; }
355   virtual void onValue(const uint64_t U, const bool LEB = false) {
356     if (LEB)
357       Length += getULEB128Size(U);
358     else
359       Length += 8;
360   }
361   virtual void onValue(const int64_t S, const bool LEB = false) {
362     if (LEB)
363       Length += getSLEB128Size(S);
364     else
365       Length += 8;
366   }
367   virtual void onValue(const StringRef String) { Length += String.size() + 1; }
368 
369   virtual void onValue(const MemoryBufferRef MBR) {
370     Length += MBR.getBufferSize();
371   }
372 };
373 } // namespace
374 
375 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
376 DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups,
377                              bool IsLittleEndian) {
378   yaml::Input YIn(YAMLString);
379 
380   DWARFYAML::Data DI;
381   DI.IsLittleEndian = IsLittleEndian;
382   YIn >> DI;
383   if (YIn.error())
384     return errorCodeToError(YIn.error());
385 
386   if (ApplyFixups) {
387     DIEFixupVisitor DIFixer(DI);
388     DIFixer.traverseDebugInfo();
389   }
390 
391   StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
392   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
393                        DebugSections);
394   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
395                        DebugSections);
396   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
397                        DebugSections);
398   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
399                        DebugSections);
400   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
401                        DebugSections);
402   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugRanges, "debug_ranges",
403                        DebugSections);
404   return std::move(DebugSections);
405 }
406