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            "Offset should be greater than or equal to the bytes that we have "
128            "written");
129     if (DebugRanges.Offset > CurrOffset)
130       ZeroFillBytes(OS, DebugRanges.Offset - CurrOffset);
131     for (auto Entry : DebugRanges.Entries) {
132       writeVariableSizedInteger(Entry.LowOffset, DebugRanges.AddrSize, OS,
133                                 DI.IsLittleEndian);
134       writeVariableSizedInteger(Entry.HighOffset, DebugRanges.AddrSize, OS,
135                                 DI.IsLittleEndian);
136     }
137     ZeroFillBytes(OS, DebugRanges.AddrSize * 2);
138   }
139 }
140 
141 void DWARFYAML::EmitPubSection(raw_ostream &OS,
142                                const DWARFYAML::PubSection &Sect,
143                                bool IsLittleEndian) {
144   writeInitialLength(Sect.Length, OS, IsLittleEndian);
145   writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
146   writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
147   writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
148   for (auto Entry : Sect.Entries) {
149     writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
150     if (Sect.IsGNUStyle)
151       writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
152     OS.write(Entry.Name.data(), Entry.Name.size());
153     OS.write('\0');
154   }
155 }
156 
157 namespace {
158 /// An extension of the DWARFYAML::ConstVisitor which writes compile
159 /// units and DIEs to a stream.
160 class DumpVisitor : public DWARFYAML::ConstVisitor {
161   raw_ostream &OS;
162 
163 protected:
164   void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
165     writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
166     writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
167     if(CU.Version >= 5) {
168       writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
169       writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
170       writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
171     }else {
172       writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
173       writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
174     }
175   }
176 
177   void onStartDIE(const DWARFYAML::Unit &CU,
178                   const DWARFYAML::Entry &DIE) override {
179     encodeULEB128(DIE.AbbrCode, OS);
180   }
181 
182   void onValue(const uint8_t U) override {
183     writeInteger(U, OS, DebugInfo.IsLittleEndian);
184   }
185 
186   void onValue(const uint16_t U) override {
187     writeInteger(U, OS, DebugInfo.IsLittleEndian);
188   }
189 
190   void onValue(const uint32_t U) override {
191     writeInteger(U, OS, DebugInfo.IsLittleEndian);
192   }
193 
194   void onValue(const uint64_t U, const bool LEB = false) override {
195     if (LEB)
196       encodeULEB128(U, OS);
197     else
198       writeInteger(U, OS, DebugInfo.IsLittleEndian);
199   }
200 
201   void onValue(const int64_t S, const bool LEB = false) override {
202     if (LEB)
203       encodeSLEB128(S, OS);
204     else
205       writeInteger(S, OS, DebugInfo.IsLittleEndian);
206   }
207 
208   void onValue(const StringRef String) override {
209     OS.write(String.data(), String.size());
210     OS.write('\0');
211   }
212 
213   void onValue(const MemoryBufferRef MBR) override {
214     OS.write(MBR.getBufferStart(), MBR.getBufferSize());
215   }
216 
217 public:
218   DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
219       : DWARFYAML::ConstVisitor(DI), OS(Out) {}
220 };
221 } // namespace
222 
223 void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
224   DumpVisitor Visitor(DI, OS);
225   Visitor.traverseDebugInfo();
226 }
227 
228 static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
229   OS.write(File.Name.data(), File.Name.size());
230   OS.write('\0');
231   encodeULEB128(File.DirIdx, OS);
232   encodeULEB128(File.ModTime, OS);
233   encodeULEB128(File.Length, OS);
234 }
235 
236 void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
237   for (const auto &LineTable : DI.DebugLines) {
238     writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
239     uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
240     writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
241     writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
242                               OS, DI.IsLittleEndian);
243     writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
244     if (LineTable.Version >= 4)
245       writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
246     writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
247     writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
248     writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
249     writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
250 
251     for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
252       writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
253 
254     for (auto IncludeDir : LineTable.IncludeDirs) {
255       OS.write(IncludeDir.data(), IncludeDir.size());
256       OS.write('\0');
257     }
258     OS.write('\0');
259 
260     for (auto File : LineTable.Files)
261       EmitFileEntry(OS, File);
262     OS.write('\0');
263 
264     for (auto Op : LineTable.Opcodes) {
265       writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
266       if (Op.Opcode == 0) {
267         encodeULEB128(Op.ExtLen, OS);
268         writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
269         switch (Op.SubOpcode) {
270         case dwarf::DW_LNE_set_address:
271         case dwarf::DW_LNE_set_discriminator:
272           writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
273                                     DI.IsLittleEndian);
274           break;
275         case dwarf::DW_LNE_define_file:
276           EmitFileEntry(OS, Op.FileEntry);
277           break;
278         case dwarf::DW_LNE_end_sequence:
279           break;
280         default:
281           for (auto OpByte : Op.UnknownOpcodeData)
282             writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
283         }
284       } else if (Op.Opcode < LineTable.OpcodeBase) {
285         switch (Op.Opcode) {
286         case dwarf::DW_LNS_copy:
287         case dwarf::DW_LNS_negate_stmt:
288         case dwarf::DW_LNS_set_basic_block:
289         case dwarf::DW_LNS_const_add_pc:
290         case dwarf::DW_LNS_set_prologue_end:
291         case dwarf::DW_LNS_set_epilogue_begin:
292           break;
293 
294         case dwarf::DW_LNS_advance_pc:
295         case dwarf::DW_LNS_set_file:
296         case dwarf::DW_LNS_set_column:
297         case dwarf::DW_LNS_set_isa:
298           encodeULEB128(Op.Data, OS);
299           break;
300 
301         case dwarf::DW_LNS_advance_line:
302           encodeSLEB128(Op.SData, OS);
303           break;
304 
305         case dwarf::DW_LNS_fixed_advance_pc:
306           writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
307           break;
308 
309         default:
310           for (auto OpData : Op.StandardOpcodeData) {
311             encodeULEB128(OpData, OS);
312           }
313         }
314       }
315     }
316   }
317 }
318 
319 using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &);
320 
321 static void
322 EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
323                      StringRef Sec,
324                      StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
325   std::string Data;
326   raw_string_ostream DebugInfoStream(Data);
327   EmitFunc(DebugInfoStream, DI);
328   DebugInfoStream.flush();
329   if (!Data.empty())
330     OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
331 }
332 
333 namespace {
334 class DIEFixupVisitor : public DWARFYAML::Visitor {
335   uint64_t Length;
336 
337 public:
338   DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){};
339 
340 private:
341   virtual void onStartCompileUnit(DWARFYAML::Unit &CU) {
342     // Size of the unit header, excluding the length field itself.
343     Length = CU.Version >= 5 ? 8 : 7;
344   }
345 
346   virtual void onEndCompileUnit(DWARFYAML::Unit &CU) {
347     CU.Length.setLength(Length);
348   }
349 
350   virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) {
351     Length += getULEB128Size(DIE.AbbrCode);
352   }
353 
354   virtual void onValue(const uint8_t U) { Length += 1; }
355   virtual void onValue(const uint16_t U) { Length += 2; }
356   virtual void onValue(const uint32_t U) { Length += 4; }
357   virtual void onValue(const uint64_t U, const bool LEB = false) {
358     if (LEB)
359       Length += getULEB128Size(U);
360     else
361       Length += 8;
362   }
363   virtual void onValue(const int64_t S, const bool LEB = false) {
364     if (LEB)
365       Length += getSLEB128Size(S);
366     else
367       Length += 8;
368   }
369   virtual void onValue(const StringRef String) { Length += String.size() + 1; }
370 
371   virtual void onValue(const MemoryBufferRef MBR) {
372     Length += MBR.getBufferSize();
373   }
374 };
375 } // namespace
376 
377 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
378 DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups,
379                              bool IsLittleEndian) {
380   yaml::Input YIn(YAMLString);
381 
382   DWARFYAML::Data DI;
383   DI.IsLittleEndian = IsLittleEndian;
384   YIn >> DI;
385   if (YIn.error())
386     return errorCodeToError(YIn.error());
387 
388   if (ApplyFixups) {
389     DIEFixupVisitor DIFixer(DI);
390     DIFixer.traverseDebugInfo();
391   }
392 
393   StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
394   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
395                        DebugSections);
396   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
397                        DebugSections);
398   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
399                        DebugSections);
400   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
401                        DebugSections);
402   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
403                        DebugSections);
404   EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugRanges, "debug_ranges",
405                        DebugSections);
406   return std::move(DebugSections);
407 }
408