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 "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/BinaryFormat/Dwarf.h"
20 #include "llvm/ObjectYAML/DWARFYAML.h"
21 #include "llvm/Support/Errc.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/Host.h"
24 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/SwapByteOrder.h"
29 #include "llvm/Support/YAMLTraits.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <algorithm>
32 #include <cassert>
33 #include <cstddef>
34 #include <cstdint>
35 #include <memory>
36 #include <string>
37 #include <vector>
38 
39 using namespace llvm;
40 
41 template <typename T>
42 static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
43   if (IsLittleEndian != sys::IsLittleEndianHost)
44     sys::swapByteOrder(Integer);
45   OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
46 }
47 
48 static Error writeVariableSizedInteger(uint64_t Integer, size_t Size,
49                                        raw_ostream &OS, bool IsLittleEndian) {
50   if (8 == Size)
51     writeInteger((uint64_t)Integer, OS, IsLittleEndian);
52   else if (4 == Size)
53     writeInteger((uint32_t)Integer, OS, IsLittleEndian);
54   else if (2 == Size)
55     writeInteger((uint16_t)Integer, OS, IsLittleEndian);
56   else if (1 == Size)
57     writeInteger((uint8_t)Integer, OS, IsLittleEndian);
58   else
59     return createStringError(errc::not_supported,
60                              "invalid integer write size: %zu", Size);
61 
62   return Error::success();
63 }
64 
65 static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
66   std::vector<uint8_t> FillData;
67   FillData.insert(FillData.begin(), Size, 0);
68   OS.write(reinterpret_cast<char *>(FillData.data()), Size);
69 }
70 
71 static void writeInitialLength(const dwarf::DwarfFormat Format,
72                                const uint64_t Length, raw_ostream &OS,
73                                bool IsLittleEndian) {
74   bool IsDWARF64 = Format == dwarf::DWARF64;
75   if (IsDWARF64)
76     cantFail(writeVariableSizedInteger(dwarf::DW_LENGTH_DWARF64, 4, OS,
77                                        IsLittleEndian));
78   cantFail(
79       writeVariableSizedInteger(Length, IsDWARF64 ? 8 : 4, OS, IsLittleEndian));
80 }
81 
82 static void writeDWARFOffset(uint64_t Offset, dwarf::DwarfFormat Format,
83                              raw_ostream &OS, bool IsLittleEndian) {
84   cantFail(writeVariableSizedInteger(Offset, Format == dwarf::DWARF64 ? 8 : 4,
85                                      OS, IsLittleEndian));
86 }
87 
88 Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
89   for (StringRef Str : *DI.DebugStrings) {
90     OS.write(Str.data(), Str.size());
91     OS.write('\0');
92   }
93 
94   return Error::success();
95 }
96 
97 StringRef DWARFYAML::Data::getAbbrevTableContentByIndex(uint64_t Index) const {
98   assert(Index < DebugAbbrev.size() &&
99          "Index should be less than the size of DebugAbbrev array");
100   auto It = AbbrevTableContents.find(Index);
101   if (It != AbbrevTableContents.cend())
102     return It->second;
103 
104   std::string AbbrevTableBuffer;
105   raw_string_ostream OS(AbbrevTableBuffer);
106 
107   uint64_t AbbrevCode = 0;
108   for (const DWARFYAML::Abbrev &AbbrevDecl : DebugAbbrev[Index].Table) {
109     AbbrevCode = AbbrevDecl.Code ? (uint64_t)*AbbrevDecl.Code : AbbrevCode + 1;
110     encodeULEB128(AbbrevCode, OS);
111     encodeULEB128(AbbrevDecl.Tag, OS);
112     OS.write(AbbrevDecl.Children);
113     for (auto Attr : AbbrevDecl.Attributes) {
114       encodeULEB128(Attr.Attribute, OS);
115       encodeULEB128(Attr.Form, OS);
116       if (Attr.Form == dwarf::DW_FORM_implicit_const)
117         encodeSLEB128(Attr.Value, OS);
118     }
119     encodeULEB128(0, OS);
120     encodeULEB128(0, OS);
121   }
122 
123   // The abbreviations for a given compilation unit end with an entry
124   // consisting of a 0 byte for the abbreviation code.
125   OS.write_zeros(1);
126 
127   AbbrevTableContents.insert({Index, AbbrevTableBuffer});
128 
129   return AbbrevTableContents[Index];
130 }
131 
132 Error DWARFYAML::emitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
133   for (uint64_t I = 0; I < DI.DebugAbbrev.size(); ++I) {
134     StringRef AbbrevTableContent = DI.getAbbrevTableContentByIndex(I);
135     OS.write(AbbrevTableContent.data(), AbbrevTableContent.size());
136   }
137 
138   return Error::success();
139 }
140 
141 Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
142   assert(DI.DebugAranges && "unexpected emitDebugAranges() call");
143   for (auto Range : *DI.DebugAranges) {
144     uint8_t AddrSize;
145     if (Range.AddrSize)
146       AddrSize = *Range.AddrSize;
147     else
148       AddrSize = DI.Is64BitAddrSize ? 8 : 4;
149 
150     uint64_t Length = 4; // sizeof(version) 2 + sizeof(address_size) 1 +
151                          // sizeof(segment_selector_size) 1
152     Length +=
153         Range.Format == dwarf::DWARF64 ? 8 : 4; // sizeof(debug_info_offset)
154 
155     const uint64_t HeaderLength =
156         Length + (Range.Format == dwarf::DWARF64
157                       ? 12
158                       : 4); // sizeof(unit_header) = 12 (DWARF64) or 4 (DWARF32)
159     const uint64_t PaddedHeaderLength = alignTo(HeaderLength, AddrSize * 2);
160 
161     if (Range.Length) {
162       Length = *Range.Length;
163     } else {
164       Length += PaddedHeaderLength - HeaderLength;
165       Length += AddrSize * 2 * (Range.Descriptors.size() + 1);
166     }
167 
168     writeInitialLength(Range.Format, Length, OS, DI.IsLittleEndian);
169     writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
170     writeDWARFOffset(Range.CuOffset, Range.Format, OS, DI.IsLittleEndian);
171     writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
172     writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
173     ZeroFillBytes(OS, PaddedHeaderLength - HeaderLength);
174 
175     for (auto Descriptor : Range.Descriptors) {
176       if (Error Err = writeVariableSizedInteger(Descriptor.Address, AddrSize,
177                                                 OS, DI.IsLittleEndian))
178         return createStringError(errc::not_supported,
179                                  "unable to write debug_aranges address: %s",
180                                  toString(std::move(Err)).c_str());
181       cantFail(writeVariableSizedInteger(Descriptor.Length, AddrSize, OS,
182                                          DI.IsLittleEndian));
183     }
184     ZeroFillBytes(OS, AddrSize * 2);
185   }
186 
187   return Error::success();
188 }
189 
190 Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) {
191   const size_t RangesOffset = OS.tell();
192   uint64_t EntryIndex = 0;
193   for (auto DebugRanges : *DI.DebugRanges) {
194     const size_t CurrOffset = OS.tell() - RangesOffset;
195     if (DebugRanges.Offset && (uint64_t)*DebugRanges.Offset < CurrOffset)
196       return createStringError(errc::invalid_argument,
197                                "'Offset' for 'debug_ranges' with index " +
198                                    Twine(EntryIndex) +
199                                    " must be greater than or equal to the "
200                                    "number of bytes written already (0x" +
201                                    Twine::utohexstr(CurrOffset) + ")");
202     if (DebugRanges.Offset)
203       ZeroFillBytes(OS, *DebugRanges.Offset - CurrOffset);
204 
205     uint8_t AddrSize;
206     if (DebugRanges.AddrSize)
207       AddrSize = *DebugRanges.AddrSize;
208     else
209       AddrSize = DI.Is64BitAddrSize ? 8 : 4;
210     for (auto Entry : DebugRanges.Entries) {
211       if (Error Err = writeVariableSizedInteger(Entry.LowOffset, AddrSize, OS,
212                                                 DI.IsLittleEndian))
213         return createStringError(
214             errc::not_supported,
215             "unable to write debug_ranges address offset: %s",
216             toString(std::move(Err)).c_str());
217       cantFail(writeVariableSizedInteger(Entry.HighOffset, AddrSize, OS,
218                                          DI.IsLittleEndian));
219     }
220     ZeroFillBytes(OS, AddrSize * 2);
221     ++EntryIndex;
222   }
223 
224   return Error::success();
225 }
226 
227 static Error emitPubSection(raw_ostream &OS, const DWARFYAML::PubSection &Sect,
228                             bool IsLittleEndian, bool IsGNUPubSec = false) {
229   writeInitialLength(Sect.Format, Sect.Length, OS, IsLittleEndian);
230   writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
231   writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
232   writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
233   for (auto Entry : Sect.Entries) {
234     writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
235     if (IsGNUPubSec)
236       writeInteger((uint8_t)Entry.Descriptor, OS, IsLittleEndian);
237     OS.write(Entry.Name.data(), Entry.Name.size());
238     OS.write('\0');
239   }
240 
241   return Error::success();
242 }
243 
244 Error DWARFYAML::emitDebugPubnames(raw_ostream &OS, const Data &DI) {
245   assert(DI.PubNames && "unexpected emitDebugPubnames() call");
246   return emitPubSection(OS, *DI.PubNames, DI.IsLittleEndian);
247 }
248 
249 Error DWARFYAML::emitDebugPubtypes(raw_ostream &OS, const Data &DI) {
250   assert(DI.PubTypes && "unexpected emitDebugPubtypes() call");
251   return emitPubSection(OS, *DI.PubTypes, DI.IsLittleEndian);
252 }
253 
254 Error DWARFYAML::emitDebugGNUPubnames(raw_ostream &OS, const Data &DI) {
255   assert(DI.GNUPubNames && "unexpected emitDebugGNUPubnames() call");
256   return emitPubSection(OS, *DI.GNUPubNames, DI.IsLittleEndian,
257                         /*IsGNUStyle=*/true);
258 }
259 
260 Error DWARFYAML::emitDebugGNUPubtypes(raw_ostream &OS, const Data &DI) {
261   assert(DI.GNUPubTypes && "unexpected emitDebugGNUPubtypes() call");
262   return emitPubSection(OS, *DI.GNUPubTypes, DI.IsLittleEndian,
263                         /*IsGNUStyle=*/true);
264 }
265 
266 static Expected<uint64_t> writeDIE(const DWARFYAML::Data &DI, uint64_t CUIndex,
267                                    uint64_t AbbrevTableID,
268                                    const dwarf::FormParams &Params,
269                                    const DWARFYAML::Entry &Entry,
270                                    raw_ostream &OS, bool IsLittleEndian) {
271   uint64_t EntryBegin = OS.tell();
272   encodeULEB128(Entry.AbbrCode, OS);
273   uint32_t AbbrCode = Entry.AbbrCode;
274   if (AbbrCode == 0 || Entry.Values.empty())
275     return OS.tell() - EntryBegin;
276 
277   Expected<DWARFYAML::Data::AbbrevTableInfo> AbbrevTableInfoOrErr =
278       DI.getAbbrevTableInfoByID(AbbrevTableID);
279   if (!AbbrevTableInfoOrErr)
280     return createStringError(errc::invalid_argument,
281                              toString(AbbrevTableInfoOrErr.takeError()) +
282                                  " for compilation unit with index " +
283                                  utostr(CUIndex));
284 
285   ArrayRef<DWARFYAML::Abbrev> AbbrevDecls(
286       DI.DebugAbbrev[AbbrevTableInfoOrErr->Index].Table);
287 
288   if (AbbrCode > AbbrevDecls.size())
289     return createStringError(
290         errc::invalid_argument,
291         "abbrev code must be less than or equal to the number of "
292         "entries in abbreviation table");
293   const DWARFYAML::Abbrev &Abbrev = AbbrevDecls[AbbrCode - 1];
294   auto FormVal = Entry.Values.begin();
295   auto AbbrForm = Abbrev.Attributes.begin();
296   for (; FormVal != Entry.Values.end() && AbbrForm != Abbrev.Attributes.end();
297        ++FormVal, ++AbbrForm) {
298     dwarf::Form Form = AbbrForm->Form;
299     bool Indirect;
300     do {
301       Indirect = false;
302       switch (Form) {
303       case dwarf::DW_FORM_addr:
304         // TODO: Test this error.
305         if (Error Err = writeVariableSizedInteger(
306                 FormVal->Value, Params.AddrSize, OS, IsLittleEndian))
307           return std::move(Err);
308         break;
309       case dwarf::DW_FORM_ref_addr:
310         // TODO: Test this error.
311         if (Error Err = writeVariableSizedInteger(FormVal->Value,
312                                                   Params.getRefAddrByteSize(),
313                                                   OS, IsLittleEndian))
314           return std::move(Err);
315         break;
316       case dwarf::DW_FORM_exprloc:
317       case dwarf::DW_FORM_block:
318         encodeULEB128(FormVal->BlockData.size(), OS);
319         OS.write((const char *)FormVal->BlockData.data(),
320                  FormVal->BlockData.size());
321         break;
322       case dwarf::DW_FORM_block1: {
323         writeInteger((uint8_t)FormVal->BlockData.size(), OS, IsLittleEndian);
324         OS.write((const char *)FormVal->BlockData.data(),
325                  FormVal->BlockData.size());
326         break;
327       }
328       case dwarf::DW_FORM_block2: {
329         writeInteger((uint16_t)FormVal->BlockData.size(), OS, IsLittleEndian);
330         OS.write((const char *)FormVal->BlockData.data(),
331                  FormVal->BlockData.size());
332         break;
333       }
334       case dwarf::DW_FORM_block4: {
335         writeInteger((uint32_t)FormVal->BlockData.size(), OS, IsLittleEndian);
336         OS.write((const char *)FormVal->BlockData.data(),
337                  FormVal->BlockData.size());
338         break;
339       }
340       case dwarf::DW_FORM_strx:
341       case dwarf::DW_FORM_addrx:
342       case dwarf::DW_FORM_rnglistx:
343       case dwarf::DW_FORM_loclistx:
344       case dwarf::DW_FORM_udata:
345       case dwarf::DW_FORM_ref_udata:
346       case dwarf::DW_FORM_GNU_addr_index:
347       case dwarf::DW_FORM_GNU_str_index:
348         encodeULEB128(FormVal->Value, OS);
349         break;
350       case dwarf::DW_FORM_data1:
351       case dwarf::DW_FORM_ref1:
352       case dwarf::DW_FORM_flag:
353       case dwarf::DW_FORM_strx1:
354       case dwarf::DW_FORM_addrx1:
355         writeInteger((uint8_t)FormVal->Value, OS, IsLittleEndian);
356         break;
357       case dwarf::DW_FORM_data2:
358       case dwarf::DW_FORM_ref2:
359       case dwarf::DW_FORM_strx2:
360       case dwarf::DW_FORM_addrx2:
361         writeInteger((uint16_t)FormVal->Value, OS, IsLittleEndian);
362         break;
363       case dwarf::DW_FORM_data4:
364       case dwarf::DW_FORM_ref4:
365       case dwarf::DW_FORM_ref_sup4:
366       case dwarf::DW_FORM_strx4:
367       case dwarf::DW_FORM_addrx4:
368         writeInteger((uint32_t)FormVal->Value, OS, IsLittleEndian);
369         break;
370       case dwarf::DW_FORM_data8:
371       case dwarf::DW_FORM_ref8:
372       case dwarf::DW_FORM_ref_sup8:
373       case dwarf::DW_FORM_ref_sig8:
374         writeInteger((uint64_t)FormVal->Value, OS, IsLittleEndian);
375         break;
376       case dwarf::DW_FORM_sdata:
377         encodeSLEB128(FormVal->Value, OS);
378         break;
379       case dwarf::DW_FORM_string:
380         OS.write(FormVal->CStr.data(), FormVal->CStr.size());
381         OS.write('\0');
382         break;
383       case dwarf::DW_FORM_indirect:
384         encodeULEB128(FormVal->Value, OS);
385         Indirect = true;
386         Form = static_cast<dwarf::Form>((uint64_t)FormVal->Value);
387         ++FormVal;
388         break;
389       case dwarf::DW_FORM_strp:
390       case dwarf::DW_FORM_sec_offset:
391       case dwarf::DW_FORM_GNU_ref_alt:
392       case dwarf::DW_FORM_GNU_strp_alt:
393       case dwarf::DW_FORM_line_strp:
394       case dwarf::DW_FORM_strp_sup:
395         cantFail(writeVariableSizedInteger(FormVal->Value,
396                                            Params.getDwarfOffsetByteSize(), OS,
397                                            IsLittleEndian));
398         break;
399       default:
400         break;
401       }
402     } while (Indirect);
403   }
404 
405   return OS.tell() - EntryBegin;
406 }
407 
408 Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
409   for (uint64_t I = 0; I < DI.CompileUnits.size(); ++I) {
410     const DWARFYAML::Unit &Unit = DI.CompileUnits[I];
411     uint8_t AddrSize;
412     if (Unit.AddrSize)
413       AddrSize = *Unit.AddrSize;
414     else
415       AddrSize = DI.Is64BitAddrSize ? 8 : 4;
416     dwarf::FormParams Params = {Unit.Version, AddrSize, Unit.Format};
417     uint64_t Length = 3; // sizeof(version) + sizeof(address_size)
418     Length += Unit.Version >= 5 ? 1 : 0;       // sizeof(unit_type)
419     Length += Params.getDwarfOffsetByteSize(); // sizeof(debug_abbrev_offset)
420 
421     // Since the length of the current compilation unit is undetermined yet, we
422     // firstly write the content of the compilation unit to a buffer to
423     // calculate it and then serialize the buffer content to the actual output
424     // stream.
425     std::string EntryBuffer;
426     raw_string_ostream EntryBufferOS(EntryBuffer);
427 
428     uint64_t AbbrevTableID = Unit.AbbrevTableID.getValueOr(I);
429     for (const DWARFYAML::Entry &Entry : Unit.Entries) {
430       if (Expected<uint64_t> EntryLength =
431               writeDIE(DI, I, AbbrevTableID, Params, Entry, EntryBufferOS,
432                        DI.IsLittleEndian))
433         Length += *EntryLength;
434       else
435         return EntryLength.takeError();
436     }
437 
438     // If the length is specified in the YAML description, we use it instead of
439     // the actual length.
440     if (Unit.Length)
441       Length = *Unit.Length;
442 
443     writeInitialLength(Unit.Format, Length, OS, DI.IsLittleEndian);
444     writeInteger((uint16_t)Unit.Version, OS, DI.IsLittleEndian);
445 
446     uint64_t AbbrevTableOffset = 0;
447     if (Unit.AbbrOffset) {
448       AbbrevTableOffset = *Unit.AbbrOffset;
449     } else {
450       if (Expected<DWARFYAML::Data::AbbrevTableInfo> AbbrevTableInfoOrErr =
451               DI.getAbbrevTableInfoByID(AbbrevTableID)) {
452         AbbrevTableOffset = AbbrevTableInfoOrErr->Offset;
453       } else {
454         // The current compilation unit may not have DIEs and it will not be
455         // able to find the associated abbrev table. We consume the error and
456         // assign 0 to the debug_abbrev_offset in such circumstances.
457         consumeError(AbbrevTableInfoOrErr.takeError());
458       }
459     }
460 
461     if (Unit.Version >= 5) {
462       writeInteger((uint8_t)Unit.Type, OS, DI.IsLittleEndian);
463       writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
464       writeDWARFOffset(AbbrevTableOffset, Unit.Format, OS, DI.IsLittleEndian);
465     } else {
466       writeDWARFOffset(AbbrevTableOffset, Unit.Format, OS, DI.IsLittleEndian);
467       writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
468     }
469 
470     OS.write(EntryBuffer.data(), EntryBuffer.size());
471   }
472 
473   return Error::success();
474 }
475 
476 static void emitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
477   OS.write(File.Name.data(), File.Name.size());
478   OS.write('\0');
479   encodeULEB128(File.DirIdx, OS);
480   encodeULEB128(File.ModTime, OS);
481   encodeULEB128(File.Length, OS);
482 }
483 
484 static void writeLineTableOpcode(const DWARFYAML::LineTableOpcode &Op,
485                                  uint8_t OpcodeBase, uint8_t AddrSize,
486                                  raw_ostream &OS, bool IsLittleEndian) {
487   writeInteger((uint8_t)Op.Opcode, OS, IsLittleEndian);
488   if (Op.Opcode == 0) {
489     encodeULEB128(Op.ExtLen, OS);
490     writeInteger((uint8_t)Op.SubOpcode, OS, IsLittleEndian);
491     switch (Op.SubOpcode) {
492     case dwarf::DW_LNE_set_address:
493       cantFail(
494           writeVariableSizedInteger(Op.Data, AddrSize, OS, IsLittleEndian));
495       break;
496     case dwarf::DW_LNE_define_file:
497       emitFileEntry(OS, Op.FileEntry);
498       break;
499     case dwarf::DW_LNE_set_discriminator:
500       encodeULEB128(Op.Data, OS);
501       break;
502     case dwarf::DW_LNE_end_sequence:
503       break;
504     default:
505       for (auto OpByte : Op.UnknownOpcodeData)
506         writeInteger((uint8_t)OpByte, OS, IsLittleEndian);
507     }
508   } else if (Op.Opcode < OpcodeBase) {
509     switch (Op.Opcode) {
510     case dwarf::DW_LNS_copy:
511     case dwarf::DW_LNS_negate_stmt:
512     case dwarf::DW_LNS_set_basic_block:
513     case dwarf::DW_LNS_const_add_pc:
514     case dwarf::DW_LNS_set_prologue_end:
515     case dwarf::DW_LNS_set_epilogue_begin:
516       break;
517 
518     case dwarf::DW_LNS_advance_pc:
519     case dwarf::DW_LNS_set_file:
520     case dwarf::DW_LNS_set_column:
521     case dwarf::DW_LNS_set_isa:
522       encodeULEB128(Op.Data, OS);
523       break;
524 
525     case dwarf::DW_LNS_advance_line:
526       encodeSLEB128(Op.SData, OS);
527       break;
528 
529     case dwarf::DW_LNS_fixed_advance_pc:
530       writeInteger((uint16_t)Op.Data, OS, IsLittleEndian);
531       break;
532 
533     default:
534       for (auto OpData : Op.StandardOpcodeData) {
535         encodeULEB128(OpData, OS);
536       }
537     }
538   }
539 }
540 
541 Error DWARFYAML::emitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
542   for (const DWARFYAML::LineTable &LineTable : DI.DebugLines) {
543     // Buffer holds the bytes following the header_length (or prologue_length in
544     // DWARFv2) field to the end of the line number program itself.
545     std::string Buffer;
546     raw_string_ostream BufferOS(Buffer);
547 
548     writeInteger(LineTable.MinInstLength, BufferOS, DI.IsLittleEndian);
549     // TODO: Add support for emitting DWARFv5 line table.
550     if (LineTable.Version >= 4)
551       writeInteger(LineTable.MaxOpsPerInst, BufferOS, DI.IsLittleEndian);
552     writeInteger(LineTable.DefaultIsStmt, BufferOS, DI.IsLittleEndian);
553     writeInteger(LineTable.LineBase, BufferOS, DI.IsLittleEndian);
554     writeInteger(LineTable.LineRange, BufferOS, DI.IsLittleEndian);
555     writeInteger(LineTable.OpcodeBase, BufferOS, DI.IsLittleEndian);
556 
557     for (uint8_t OpcodeLength : LineTable.StandardOpcodeLengths)
558       writeInteger(OpcodeLength, BufferOS, DI.IsLittleEndian);
559 
560     for (StringRef IncludeDir : LineTable.IncludeDirs) {
561       BufferOS.write(IncludeDir.data(), IncludeDir.size());
562       BufferOS.write('\0');
563     }
564     BufferOS.write('\0');
565 
566     for (const DWARFYAML::File &File : LineTable.Files)
567       emitFileEntry(BufferOS, File);
568     BufferOS.write('\0');
569 
570     uint64_t HeaderLength =
571         LineTable.PrologueLength ? *LineTable.PrologueLength : Buffer.size();
572 
573     for (const DWARFYAML::LineTableOpcode &Op : LineTable.Opcodes)
574       writeLineTableOpcode(Op, LineTable.OpcodeBase, DI.Is64BitAddrSize ? 8 : 4,
575                            BufferOS, DI.IsLittleEndian);
576 
577     uint64_t Length;
578     if (LineTable.Length) {
579       Length = *LineTable.Length;
580     } else {
581       Length = 2; // sizeof(version)
582       Length +=
583           (LineTable.Format == dwarf::DWARF64 ? 8 : 4); // sizeof(header_length)
584       Length += Buffer.size();
585     }
586 
587     writeInitialLength(LineTable.Format, Length, OS, DI.IsLittleEndian);
588     writeInteger(LineTable.Version, OS, DI.IsLittleEndian);
589     writeDWARFOffset(HeaderLength, LineTable.Format, OS, DI.IsLittleEndian);
590     OS.write(Buffer.data(), Buffer.size());
591   }
592 
593   return Error::success();
594 }
595 
596 Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) {
597   for (const AddrTableEntry &TableEntry : *DI.DebugAddr) {
598     uint8_t AddrSize;
599     if (TableEntry.AddrSize)
600       AddrSize = *TableEntry.AddrSize;
601     else
602       AddrSize = DI.Is64BitAddrSize ? 8 : 4;
603 
604     uint64_t Length;
605     if (TableEntry.Length)
606       Length = (uint64_t)*TableEntry.Length;
607     else
608       // 2 (version) + 1 (address_size) + 1 (segment_selector_size) = 4
609       Length = 4 + (AddrSize + TableEntry.SegSelectorSize) *
610                        TableEntry.SegAddrPairs.size();
611 
612     writeInitialLength(TableEntry.Format, Length, OS, DI.IsLittleEndian);
613     writeInteger((uint16_t)TableEntry.Version, OS, DI.IsLittleEndian);
614     writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
615     writeInteger((uint8_t)TableEntry.SegSelectorSize, OS, DI.IsLittleEndian);
616 
617     for (const SegAddrPair &Pair : TableEntry.SegAddrPairs) {
618       if (TableEntry.SegSelectorSize != 0)
619         if (Error Err = writeVariableSizedInteger(Pair.Segment,
620                                                   TableEntry.SegSelectorSize,
621                                                   OS, DI.IsLittleEndian))
622           return createStringError(errc::not_supported,
623                                    "unable to write debug_addr segment: %s",
624                                    toString(std::move(Err)).c_str());
625       if (AddrSize != 0)
626         if (Error Err = writeVariableSizedInteger(Pair.Address, AddrSize, OS,
627                                                   DI.IsLittleEndian))
628           return createStringError(errc::not_supported,
629                                    "unable to write debug_addr address: %s",
630                                    toString(std::move(Err)).c_str());
631     }
632   }
633 
634   return Error::success();
635 }
636 
637 Error DWARFYAML::emitDebugStrOffsets(raw_ostream &OS, const Data &DI) {
638   assert(DI.DebugStrOffsets && "unexpected emitDebugStrOffsets() call");
639   for (const DWARFYAML::StringOffsetsTable &Table : *DI.DebugStrOffsets) {
640     uint64_t Length;
641     if (Table.Length)
642       Length = *Table.Length;
643     else
644       // sizeof(version) + sizeof(padding) = 4
645       Length =
646           4 + Table.Offsets.size() * (Table.Format == dwarf::DWARF64 ? 8 : 4);
647 
648     writeInitialLength(Table.Format, Length, OS, DI.IsLittleEndian);
649     writeInteger((uint16_t)Table.Version, OS, DI.IsLittleEndian);
650     writeInteger((uint16_t)Table.Padding, OS, DI.IsLittleEndian);
651 
652     for (uint64_t Offset : Table.Offsets)
653       writeDWARFOffset(Offset, Table.Format, OS, DI.IsLittleEndian);
654   }
655 
656   return Error::success();
657 }
658 
659 static Error checkOperandCount(StringRef EncodingString,
660                                ArrayRef<yaml::Hex64> Values,
661                                uint64_t ExpectedOperands) {
662   if (Values.size() != ExpectedOperands)
663     return createStringError(
664         errc::invalid_argument,
665         "invalid number (%zu) of operands for the operator: %s, %" PRIu64
666         " expected",
667         Values.size(), EncodingString.str().c_str(), ExpectedOperands);
668 
669   return Error::success();
670 }
671 
672 static Error writeListEntryAddress(StringRef EncodingName, raw_ostream &OS,
673                                    uint64_t Addr, uint8_t AddrSize,
674                                    bool IsLittleEndian) {
675   if (Error Err = writeVariableSizedInteger(Addr, AddrSize, OS, IsLittleEndian))
676     return createStringError(errc::invalid_argument,
677                              "unable to write address for the operator %s: %s",
678                              EncodingName.str().c_str(),
679                              toString(std::move(Err)).c_str());
680 
681   return Error::success();
682 }
683 
684 static Expected<uint64_t>
685 writeDWARFExpression(raw_ostream &OS,
686                      const DWARFYAML::DWARFOperation &Operation,
687                      uint8_t AddrSize, bool IsLittleEndian) {
688   auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error {
689     return checkOperandCount(dwarf::OperationEncodingString(Operation.Operator),
690                              Operation.Values, ExpectedOperands);
691   };
692 
693   uint64_t ExpressionBegin = OS.tell();
694   writeInteger((uint8_t)Operation.Operator, OS, IsLittleEndian);
695   switch (Operation.Operator) {
696   case dwarf::DW_OP_consts:
697     if (Error Err = CheckOperands(1))
698       return std::move(Err);
699     encodeSLEB128(Operation.Values[0], OS);
700     break;
701   case dwarf::DW_OP_stack_value:
702     if (Error Err = CheckOperands(0))
703       return std::move(Err);
704     break;
705   default:
706     StringRef EncodingStr = dwarf::OperationEncodingString(Operation.Operator);
707     return createStringError(errc::not_supported,
708                              "DWARF expression: " +
709                                  (EncodingStr.empty()
710                                       ? "0x" + utohexstr(Operation.Operator)
711                                       : EncodingStr) +
712                                  " is not supported");
713   }
714   return OS.tell() - ExpressionBegin;
715 }
716 
717 static Expected<uint64_t> writeListEntry(raw_ostream &OS,
718                                          const DWARFYAML::RnglistEntry &Entry,
719                                          uint8_t AddrSize,
720                                          bool IsLittleEndian) {
721   uint64_t BeginOffset = OS.tell();
722   writeInteger((uint8_t)Entry.Operator, OS, IsLittleEndian);
723 
724   StringRef EncodingName = dwarf::RangeListEncodingString(Entry.Operator);
725 
726   auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error {
727     return checkOperandCount(EncodingName, Entry.Values, ExpectedOperands);
728   };
729 
730   auto WriteAddress = [&](uint64_t Addr) -> Error {
731     return writeListEntryAddress(EncodingName, OS, Addr, AddrSize,
732                                  IsLittleEndian);
733   };
734 
735   switch (Entry.Operator) {
736   case dwarf::DW_RLE_end_of_list:
737     if (Error Err = CheckOperands(0))
738       return std::move(Err);
739     break;
740   case dwarf::DW_RLE_base_addressx:
741     if (Error Err = CheckOperands(1))
742       return std::move(Err);
743     encodeULEB128(Entry.Values[0], OS);
744     break;
745   case dwarf::DW_RLE_startx_endx:
746   case dwarf::DW_RLE_startx_length:
747   case dwarf::DW_RLE_offset_pair:
748     if (Error Err = CheckOperands(2))
749       return std::move(Err);
750     encodeULEB128(Entry.Values[0], OS);
751     encodeULEB128(Entry.Values[1], OS);
752     break;
753   case dwarf::DW_RLE_base_address:
754     if (Error Err = CheckOperands(1))
755       return std::move(Err);
756     if (Error Err = WriteAddress(Entry.Values[0]))
757       return std::move(Err);
758     break;
759   case dwarf::DW_RLE_start_end:
760     if (Error Err = CheckOperands(2))
761       return std::move(Err);
762     if (Error Err = WriteAddress(Entry.Values[0]))
763       return std::move(Err);
764     cantFail(WriteAddress(Entry.Values[1]));
765     break;
766   case dwarf::DW_RLE_start_length:
767     if (Error Err = CheckOperands(2))
768       return std::move(Err);
769     if (Error Err = WriteAddress(Entry.Values[0]))
770       return std::move(Err);
771     encodeULEB128(Entry.Values[1], OS);
772     break;
773   }
774 
775   return OS.tell() - BeginOffset;
776 }
777 
778 static Expected<uint64_t> writeListEntry(raw_ostream &OS,
779                                          const DWARFYAML::LoclistEntry &Entry,
780                                          uint8_t AddrSize,
781                                          bool IsLittleEndian) {
782   uint64_t BeginOffset = OS.tell();
783   writeInteger((uint8_t)Entry.Operator, OS, IsLittleEndian);
784 
785   StringRef EncodingName = dwarf::LocListEncodingString(Entry.Operator);
786 
787   auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error {
788     return checkOperandCount(EncodingName, Entry.Values, ExpectedOperands);
789   };
790 
791   auto WriteAddress = [&](uint64_t Addr) -> Error {
792     return writeListEntryAddress(EncodingName, OS, Addr, AddrSize,
793                                  IsLittleEndian);
794   };
795 
796   auto WriteDWARFOperations = [&]() -> Error {
797     std::string OpBuffer;
798     raw_string_ostream OpBufferOS(OpBuffer);
799     uint64_t DescriptionsLength = 0;
800 
801     for (const DWARFYAML::DWARFOperation &Op : Entry.Descriptions) {
802       if (Expected<uint64_t> OpSize =
803               writeDWARFExpression(OpBufferOS, Op, AddrSize, IsLittleEndian))
804         DescriptionsLength += *OpSize;
805       else
806         return OpSize.takeError();
807     }
808 
809     if (Entry.DescriptionsLength)
810       DescriptionsLength = *Entry.DescriptionsLength;
811     else
812       DescriptionsLength = OpBuffer.size();
813 
814     encodeULEB128(DescriptionsLength, OS);
815     OS.write(OpBuffer.data(), OpBuffer.size());
816 
817     return Error::success();
818   };
819 
820   switch (Entry.Operator) {
821   case dwarf::DW_LLE_end_of_list:
822     if (Error Err = CheckOperands(0))
823       return std::move(Err);
824     break;
825   case dwarf::DW_LLE_base_addressx:
826     if (Error Err = CheckOperands(1))
827       return std::move(Err);
828     encodeULEB128(Entry.Values[0], OS);
829     break;
830   case dwarf::DW_LLE_startx_endx:
831   case dwarf::DW_LLE_startx_length:
832   case dwarf::DW_LLE_offset_pair:
833     if (Error Err = CheckOperands(2))
834       return std::move(Err);
835     encodeULEB128(Entry.Values[0], OS);
836     encodeULEB128(Entry.Values[1], OS);
837     if (Error Err = WriteDWARFOperations())
838       return std::move(Err);
839     break;
840   case dwarf::DW_LLE_default_location:
841     if (Error Err = CheckOperands(0))
842       return std::move(Err);
843     if (Error Err = WriteDWARFOperations())
844       return std::move(Err);
845     break;
846   case dwarf::DW_LLE_base_address:
847     if (Error Err = CheckOperands(1))
848       return std::move(Err);
849     if (Error Err = WriteAddress(Entry.Values[0]))
850       return std::move(Err);
851     break;
852   case dwarf::DW_LLE_start_end:
853     if (Error Err = CheckOperands(2))
854       return std::move(Err);
855     if (Error Err = WriteAddress(Entry.Values[0]))
856       return std::move(Err);
857     cantFail(WriteAddress(Entry.Values[1]));
858     if (Error Err = WriteDWARFOperations())
859       return std::move(Err);
860     break;
861   case dwarf::DW_LLE_start_length:
862     if (Error Err = CheckOperands(2))
863       return std::move(Err);
864     if (Error Err = WriteAddress(Entry.Values[0]))
865       return std::move(Err);
866     encodeULEB128(Entry.Values[1], OS);
867     if (Error Err = WriteDWARFOperations())
868       return std::move(Err);
869     break;
870   }
871 
872   return OS.tell() - BeginOffset;
873 }
874 
875 template <typename EntryType>
876 static Error writeDWARFLists(raw_ostream &OS,
877                              ArrayRef<DWARFYAML::ListTable<EntryType>> Tables,
878                              bool IsLittleEndian, bool Is64BitAddrSize) {
879   for (const DWARFYAML::ListTable<EntryType> &Table : Tables) {
880     // sizeof(version) + sizeof(address_size) + sizeof(segment_selector_size) +
881     // sizeof(offset_entry_count) = 8
882     uint64_t Length = 8;
883 
884     uint8_t AddrSize;
885     if (Table.AddrSize)
886       AddrSize = *Table.AddrSize;
887     else
888       AddrSize = Is64BitAddrSize ? 8 : 4;
889 
890     // Since the length of the current range/location lists entry is
891     // undetermined yet, we firstly write the content of the range/location
892     // lists to a buffer to calculate the length and then serialize the buffer
893     // content to the actual output stream.
894     std::string ListBuffer;
895     raw_string_ostream ListBufferOS(ListBuffer);
896 
897     // Offsets holds offsets for each range/location list. The i-th element is
898     // the offset from the beginning of the first range/location list to the
899     // location of the i-th range list.
900     std::vector<uint64_t> Offsets;
901 
902     for (const DWARFYAML::ListEntries<EntryType> &List : Table.Lists) {
903       Offsets.push_back(ListBufferOS.tell());
904       if (List.Content) {
905         List.Content->writeAsBinary(ListBufferOS, UINT64_MAX);
906         Length += List.Content->binary_size();
907       } else if (List.Entries) {
908         for (const EntryType &Entry : *List.Entries) {
909           Expected<uint64_t> EntrySize =
910               writeListEntry(ListBufferOS, Entry, AddrSize, IsLittleEndian);
911           if (!EntrySize)
912             return EntrySize.takeError();
913           Length += *EntrySize;
914         }
915       }
916     }
917 
918     // If the offset_entry_count field isn't specified, yaml2obj will infer it
919     // from the 'Offsets' field in the YAML description. If the 'Offsets' field
920     // isn't specified either, yaml2obj will infer it from the auto-generated
921     // offsets.
922     uint32_t OffsetEntryCount;
923     if (Table.OffsetEntryCount)
924       OffsetEntryCount = *Table.OffsetEntryCount;
925     else
926       OffsetEntryCount = Table.Offsets ? Table.Offsets->size() : Offsets.size();
927     uint64_t OffsetsSize =
928         OffsetEntryCount * (Table.Format == dwarf::DWARF64 ? 8 : 4);
929     Length += OffsetsSize;
930 
931     // If the length is specified in the YAML description, we use it instead of
932     // the actual length.
933     if (Table.Length)
934       Length = *Table.Length;
935 
936     writeInitialLength(Table.Format, Length, OS, IsLittleEndian);
937     writeInteger((uint16_t)Table.Version, OS, IsLittleEndian);
938     writeInteger((uint8_t)AddrSize, OS, IsLittleEndian);
939     writeInteger((uint8_t)Table.SegSelectorSize, OS, IsLittleEndian);
940     writeInteger((uint32_t)OffsetEntryCount, OS, IsLittleEndian);
941 
942     auto EmitOffsets = [&](ArrayRef<uint64_t> Offsets, uint64_t OffsetsSize) {
943       for (uint64_t Offset : Offsets)
944         writeDWARFOffset(OffsetsSize + Offset, Table.Format, OS,
945                          IsLittleEndian);
946     };
947 
948     if (Table.Offsets)
949       EmitOffsets(ArrayRef<uint64_t>((const uint64_t *)Table.Offsets->data(),
950                                      Table.Offsets->size()),
951                   0);
952     else if (OffsetEntryCount != 0)
953       EmitOffsets(Offsets, OffsetsSize);
954 
955     OS.write(ListBuffer.data(), ListBuffer.size());
956   }
957 
958   return Error::success();
959 }
960 
961 Error DWARFYAML::emitDebugRnglists(raw_ostream &OS, const Data &DI) {
962   assert(DI.DebugRnglists && "unexpected emitDebugRnglists() call");
963   return writeDWARFLists<DWARFYAML::RnglistEntry>(
964       OS, *DI.DebugRnglists, DI.IsLittleEndian, DI.Is64BitAddrSize);
965 }
966 
967 Error DWARFYAML::emitDebugLoclists(raw_ostream &OS, const Data &DI) {
968   assert(DI.DebugLoclists && "unexpected emitDebugRnglists() call");
969   return writeDWARFLists<DWARFYAML::LoclistEntry>(
970       OS, *DI.DebugLoclists, DI.IsLittleEndian, DI.Is64BitAddrSize);
971 }
972 
973 std::function<Error(raw_ostream &, const DWARFYAML::Data &)>
974 DWARFYAML::getDWARFEmitterByName(StringRef SecName) {
975   auto EmitFunc =
976       StringSwitch<
977           std::function<Error(raw_ostream &, const DWARFYAML::Data &)>>(SecName)
978           .Case("debug_abbrev", DWARFYAML::emitDebugAbbrev)
979           .Case("debug_addr", DWARFYAML::emitDebugAddr)
980           .Case("debug_aranges", DWARFYAML::emitDebugAranges)
981           .Case("debug_gnu_pubnames", DWARFYAML::emitDebugGNUPubnames)
982           .Case("debug_gnu_pubtypes", DWARFYAML::emitDebugGNUPubtypes)
983           .Case("debug_info", DWARFYAML::emitDebugInfo)
984           .Case("debug_line", DWARFYAML::emitDebugLine)
985           .Case("debug_loclists", DWARFYAML::emitDebugLoclists)
986           .Case("debug_pubnames", DWARFYAML::emitDebugPubnames)
987           .Case("debug_pubtypes", DWARFYAML::emitDebugPubtypes)
988           .Case("debug_ranges", DWARFYAML::emitDebugRanges)
989           .Case("debug_rnglists", DWARFYAML::emitDebugRnglists)
990           .Case("debug_str", DWARFYAML::emitDebugStr)
991           .Case("debug_str_offsets", DWARFYAML::emitDebugStrOffsets)
992           .Default([&](raw_ostream &, const DWARFYAML::Data &) {
993             return createStringError(errc::not_supported,
994                                      SecName + " is not supported");
995           });
996 
997   return EmitFunc;
998 }
999 
1000 static Error
1001 emitDebugSectionImpl(const DWARFYAML::Data &DI, StringRef Sec,
1002                      StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
1003   std::string Data;
1004   raw_string_ostream DebugInfoStream(Data);
1005 
1006   auto EmitFunc = DWARFYAML::getDWARFEmitterByName(Sec);
1007 
1008   if (Error Err = EmitFunc(DebugInfoStream, DI))
1009     return Err;
1010   DebugInfoStream.flush();
1011   if (!Data.empty())
1012     OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
1013 
1014   return Error::success();
1015 }
1016 
1017 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
1018 DWARFYAML::emitDebugSections(StringRef YAMLString, bool IsLittleEndian,
1019                              bool Is64BitAddrSize) {
1020   auto CollectDiagnostic = [](const SMDiagnostic &Diag, void *DiagContext) {
1021     *static_cast<SMDiagnostic *>(DiagContext) = Diag;
1022   };
1023 
1024   SMDiagnostic GeneratedDiag;
1025   yaml::Input YIn(YAMLString, /*Ctxt=*/nullptr, CollectDiagnostic,
1026                   &GeneratedDiag);
1027 
1028   DWARFYAML::Data DI;
1029   DI.IsLittleEndian = IsLittleEndian;
1030   DI.Is64BitAddrSize = Is64BitAddrSize;
1031 
1032   YIn >> DI;
1033   if (YIn.error())
1034     return createStringError(YIn.error(), GeneratedDiag.getMessage());
1035 
1036   StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
1037   Error Err = Error::success();
1038   cantFail(std::move(Err));
1039 
1040   for (StringRef SecName : DI.getNonEmptySectionNames())
1041     Err = joinErrors(std::move(Err),
1042                      emitDebugSectionImpl(DI, SecName, DebugSections));
1043 
1044   if (Err)
1045     return std::move(Err);
1046   return std::move(DebugSections);
1047 }
1048