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