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