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