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