107088c10SChris Bieneman //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===// 207088c10SChris Bieneman // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 607088c10SChris Bieneman // 707088c10SChris Bieneman //===----------------------------------------------------------------------===// 807088c10SChris Bieneman /// 907088c10SChris Bieneman /// \file 105f8f34e4SAdrian Prantl /// The DWARF component of yaml2obj. Provided as library code for tests. 1107088c10SChris Bieneman /// 1207088c10SChris Bieneman //===----------------------------------------------------------------------===// 1307088c10SChris Bieneman 1407088c10SChris Bieneman #include "llvm/ObjectYAML/DWARFEmitter.h" 15c4cf250cSXing GUO #include "llvm/ADT/ArrayRef.h" 1628082ab0SEugene Zelenko #include "llvm/ADT/StringMap.h" 1728082ab0SEugene Zelenko #include "llvm/ADT/StringRef.h" 18929edd8bSXing GUO #include "llvm/BinaryFormat/Dwarf.h" 1907088c10SChris Bieneman #include "llvm/ObjectYAML/DWARFYAML.h" 20e4344e14SXing GUO #include "llvm/Support/Errc.h" 2107088c10SChris Bieneman #include "llvm/Support/Error.h" 2228082ab0SEugene Zelenko #include "llvm/Support/Host.h" 2307088c10SChris Bieneman #include "llvm/Support/LEB128.h" 2428082ab0SEugene Zelenko #include "llvm/Support/MathExtras.h" 2528082ab0SEugene Zelenko #include "llvm/Support/MemoryBuffer.h" 268f9ca561SXing GUO #include "llvm/Support/SourceMgr.h" 2707088c10SChris Bieneman #include "llvm/Support/SwapByteOrder.h" 2828082ab0SEugene Zelenko #include "llvm/Support/YAMLTraits.h" 296bda14b3SChandler Carruth #include "llvm/Support/raw_ostream.h" 3007088c10SChris Bieneman #include <algorithm> 3128082ab0SEugene Zelenko #include <cassert> 3228082ab0SEugene Zelenko #include <cstddef> 3328082ab0SEugene Zelenko #include <cstdint> 3428082ab0SEugene Zelenko #include <memory> 3528082ab0SEugene Zelenko #include <string> 3628082ab0SEugene Zelenko #include <vector> 3707088c10SChris Bieneman 3807088c10SChris Bieneman using namespace llvm; 3907088c10SChris Bieneman 4007088c10SChris Bieneman template <typename T> 41efcf06f5SBenjamin Kramer static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) { 4207088c10SChris Bieneman if (IsLittleEndian != sys::IsLittleEndianHost) 4307088c10SChris Bieneman sys::swapByteOrder(Integer); 4407088c10SChris Bieneman OS.write(reinterpret_cast<char *>(&Integer), sizeof(T)); 4507088c10SChris Bieneman } 4607088c10SChris Bieneman 479aaa32cfSXing GUO static Error writeVariableSizedInteger(uint64_t Integer, size_t Size, 48efcf06f5SBenjamin Kramer raw_ostream &OS, bool IsLittleEndian) { 4907088c10SChris Bieneman if (8 == Size) 5007088c10SChris Bieneman writeInteger((uint64_t)Integer, OS, IsLittleEndian); 5107088c10SChris Bieneman else if (4 == Size) 5207088c10SChris Bieneman writeInteger((uint32_t)Integer, OS, IsLittleEndian); 5307088c10SChris Bieneman else if (2 == Size) 5407088c10SChris Bieneman writeInteger((uint16_t)Integer, OS, IsLittleEndian); 5507088c10SChris Bieneman else if (1 == Size) 5607088c10SChris Bieneman writeInteger((uint8_t)Integer, OS, IsLittleEndian); 5707088c10SChris Bieneman else 589aaa32cfSXing GUO return createStringError(errc::not_supported, 599aaa32cfSXing GUO "invalid integer write size: %zu", Size); 609aaa32cfSXing GUO 619aaa32cfSXing GUO return Error::success(); 6207088c10SChris Bieneman } 6307088c10SChris Bieneman 64efcf06f5SBenjamin Kramer static void ZeroFillBytes(raw_ostream &OS, size_t Size) { 6507088c10SChris Bieneman std::vector<uint8_t> FillData; 6607088c10SChris Bieneman FillData.insert(FillData.begin(), Size, 0); 6707088c10SChris Bieneman OS.write(reinterpret_cast<char *>(FillData.data()), Size); 6807088c10SChris Bieneman } 6907088c10SChris Bieneman 7049a49fe8SBenjamin Kramer static void writeInitialLength(const DWARFYAML::InitialLength &Length, 7149a49fe8SBenjamin Kramer raw_ostream &OS, bool IsLittleEndian) { 72faf1feb5SChris Bieneman writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian); 73faf1feb5SChris Bieneman if (Length.isDWARF64()) 74faf1feb5SChris Bieneman writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian); 75faf1feb5SChris Bieneman } 76faf1feb5SChris Bieneman 77613c4a87SXing GUO static void writeInitialLength(const dwarf::DwarfFormat Format, 78613c4a87SXing GUO const uint64_t Length, raw_ostream &OS, 79613c4a87SXing GUO bool IsLittleEndian) { 80613c4a87SXing GUO bool IsDWARF64 = Format == dwarf::DWARF64; 81613c4a87SXing GUO if (IsDWARF64) 829aaa32cfSXing GUO cantFail(writeVariableSizedInteger(dwarf::DW_LENGTH_DWARF64, 4, OS, 839aaa32cfSXing GUO IsLittleEndian)); 849aaa32cfSXing GUO cantFail( 859aaa32cfSXing GUO writeVariableSizedInteger(Length, IsDWARF64 ? 8 : 4, OS, IsLittleEndian)); 86613c4a87SXing GUO } 87613c4a87SXing GUO 88367d0d4cSXing GUO static void writeDWARFOffset(uint64_t Offset, dwarf::DwarfFormat Format, 89367d0d4cSXing GUO raw_ostream &OS, bool IsLittleEndian) { 90367d0d4cSXing GUO cantFail(writeVariableSizedInteger(Offset, Format == dwarf::DWARF64 ? 8 : 4, 91367d0d4cSXing GUO OS, IsLittleEndian)); 92367d0d4cSXing GUO } 93367d0d4cSXing GUO 949939f231SXing GUO Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) { 9507088c10SChris Bieneman for (auto Str : DI.DebugStrings) { 9607088c10SChris Bieneman OS.write(Str.data(), Str.size()); 9707088c10SChris Bieneman OS.write('\0'); 9807088c10SChris Bieneman } 999939f231SXing GUO 1009939f231SXing GUO return Error::success(); 10107088c10SChris Bieneman } 10207088c10SChris Bieneman 1039939f231SXing GUO Error DWARFYAML::emitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) { 104d261a1c0SXing GUO uint64_t AbbrevCode = 0; 10507088c10SChris Bieneman for (auto AbbrevDecl : DI.AbbrevDecls) { 106d261a1c0SXing GUO AbbrevCode = AbbrevDecl.Code ? (uint64_t)*AbbrevDecl.Code : AbbrevCode + 1; 107d261a1c0SXing GUO encodeULEB128(AbbrevCode, OS); 10807088c10SChris Bieneman encodeULEB128(AbbrevDecl.Tag, OS); 10907088c10SChris Bieneman OS.write(AbbrevDecl.Children); 11007088c10SChris Bieneman for (auto Attr : AbbrevDecl.Attributes) { 11107088c10SChris Bieneman encodeULEB128(Attr.Attribute, OS); 11207088c10SChris Bieneman encodeULEB128(Attr.Form, OS); 113bcf513f2SChris Bieneman if (Attr.Form == dwarf::DW_FORM_implicit_const) 114bcf513f2SChris Bieneman encodeSLEB128(Attr.Value, OS); 11507088c10SChris Bieneman } 11607088c10SChris Bieneman encodeULEB128(0, OS); 11707088c10SChris Bieneman encodeULEB128(0, OS); 11807088c10SChris Bieneman } 1199939f231SXing GUO 12038907b69SXing GUO // The abbreviations for a given compilation unit end with an entry consisting 12138907b69SXing GUO // of a 0 byte for the abbreviation code. 12238907b69SXing GUO OS.write_zeros(1); 12338907b69SXing GUO 1249939f231SXing GUO return Error::success(); 12507088c10SChris Bieneman } 12607088c10SChris Bieneman 1279939f231SXing GUO Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) { 12807088c10SChris Bieneman for (auto Range : DI.ARanges) { 12907088c10SChris Bieneman auto HeaderStart = OS.tell(); 130*006f6f8aSXing GUO 131*006f6f8aSXing GUO uint8_t AddrSize; 132*006f6f8aSXing GUO if (Range.AddrSize) 133*006f6f8aSXing GUO AddrSize = *Range.AddrSize; 134*006f6f8aSXing GUO else 135*006f6f8aSXing GUO AddrSize = DI.Is64BitAddrSize ? 8 : 4; 136*006f6f8aSXing GUO 137613c4a87SXing GUO writeInitialLength(Range.Format, Range.Length, OS, DI.IsLittleEndian); 13807088c10SChris Bieneman writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian); 139367d0d4cSXing GUO writeDWARFOffset(Range.CuOffset, Range.Format, OS, DI.IsLittleEndian); 140*006f6f8aSXing GUO writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian); 14107088c10SChris Bieneman writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian); 14207088c10SChris Bieneman 14307088c10SChris Bieneman auto HeaderSize = OS.tell() - HeaderStart; 144*006f6f8aSXing GUO auto FirstDescriptor = alignTo(HeaderSize, AddrSize * 2); 14507088c10SChris Bieneman ZeroFillBytes(OS, FirstDescriptor - HeaderSize); 14607088c10SChris Bieneman 14707088c10SChris Bieneman for (auto Descriptor : Range.Descriptors) { 148*006f6f8aSXing GUO if (Error Err = writeVariableSizedInteger(Descriptor.Address, AddrSize, 149*006f6f8aSXing GUO OS, DI.IsLittleEndian)) 1509aaa32cfSXing GUO return createStringError(errc::not_supported, 1519aaa32cfSXing GUO "unable to write debug_aranges address: %s", 1529aaa32cfSXing GUO toString(std::move(Err)).c_str()); 153*006f6f8aSXing GUO cantFail(writeVariableSizedInteger(Descriptor.Length, AddrSize, OS, 1549aaa32cfSXing GUO DI.IsLittleEndian)); 15507088c10SChris Bieneman } 156*006f6f8aSXing GUO ZeroFillBytes(OS, AddrSize * 2); 15707088c10SChris Bieneman } 1589939f231SXing GUO 1599939f231SXing GUO return Error::success(); 16007088c10SChris Bieneman } 16107088c10SChris Bieneman 1629939f231SXing GUO Error DWARFYAML::emitDebugRanges(raw_ostream &OS, const DWARFYAML::Data &DI) { 1636025fc22SGreg Clayton const size_t RangesOffset = OS.tell(); 164e4344e14SXing GUO uint64_t EntryIndex = 0; 1656e73f12aSGreg Clayton for (auto DebugRanges : DI.DebugRanges) { 1666025fc22SGreg Clayton const size_t CurrOffset = OS.tell() - RangesOffset; 16799c23354SXing GUO if (DebugRanges.Offset && (uint64_t)*DebugRanges.Offset < CurrOffset) 168e4344e14SXing GUO return createStringError(errc::invalid_argument, 169e4344e14SXing GUO "'Offset' for 'debug_ranges' with index " + 170e4344e14SXing GUO Twine(EntryIndex) + 171e4344e14SXing GUO " must be greater than or equal to the " 172e4344e14SXing GUO "number of bytes written already (0x" + 173e4344e14SXing GUO Twine::utohexstr(CurrOffset) + ")"); 17499c23354SXing GUO if (DebugRanges.Offset) 17599c23354SXing GUO ZeroFillBytes(OS, *DebugRanges.Offset - CurrOffset); 176ff9c1ae2SXing GUO 177ff9c1ae2SXing GUO uint8_t AddrSize; 178ff9c1ae2SXing GUO if (DebugRanges.AddrSize) 179ff9c1ae2SXing GUO AddrSize = *DebugRanges.AddrSize; 180ff9c1ae2SXing GUO else 181830a7c2aSXing GUO AddrSize = DI.Is64BitAddrSize ? 8 : 4; 1826e73f12aSGreg Clayton for (auto Entry : DebugRanges.Entries) { 1839aaa32cfSXing GUO if (Error Err = writeVariableSizedInteger(Entry.LowOffset, AddrSize, OS, 1849aaa32cfSXing GUO DI.IsLittleEndian)) 1859aaa32cfSXing GUO return createStringError( 1869aaa32cfSXing GUO errc::not_supported, 1879aaa32cfSXing GUO "unable to write debug_ranges address offset: %s", 1889aaa32cfSXing GUO toString(std::move(Err)).c_str()); 1899aaa32cfSXing GUO cantFail(writeVariableSizedInteger(Entry.HighOffset, AddrSize, OS, 1909aaa32cfSXing GUO DI.IsLittleEndian)); 1916025fc22SGreg Clayton } 192ff9c1ae2SXing GUO ZeroFillBytes(OS, AddrSize * 2); 193e4344e14SXing GUO ++EntryIndex; 1946025fc22SGreg Clayton } 1959939f231SXing GUO 1969939f231SXing GUO return Error::success(); 1976025fc22SGreg Clayton } 1986025fc22SGreg Clayton 1999939f231SXing GUO Error DWARFYAML::emitPubSection(raw_ostream &OS, 20007088c10SChris Bieneman const DWARFYAML::PubSection &Sect, 201b6f08b7cSXing GUO bool IsLittleEndian, bool IsGNUPubSec) { 202faf1feb5SChris Bieneman writeInitialLength(Sect.Length, OS, IsLittleEndian); 20307088c10SChris Bieneman writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian); 20407088c10SChris Bieneman writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian); 20507088c10SChris Bieneman writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian); 20607088c10SChris Bieneman for (auto Entry : Sect.Entries) { 20707088c10SChris Bieneman writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian); 208b6f08b7cSXing GUO if (IsGNUPubSec) 20993bc571dSXing GUO writeInteger((uint8_t)Entry.Descriptor, OS, IsLittleEndian); 21007088c10SChris Bieneman OS.write(Entry.Name.data(), Entry.Name.size()); 21107088c10SChris Bieneman OS.write('\0'); 21207088c10SChris Bieneman } 2139939f231SXing GUO 2149939f231SXing GUO return Error::success(); 21507088c10SChris Bieneman } 21607088c10SChris Bieneman 21792874d28SXing GUO static Expected<uint64_t> writeDIE(ArrayRef<DWARFYAML::Abbrev> AbbrevDecls, 21892874d28SXing GUO const DWARFYAML::Unit &Unit, 21992874d28SXing GUO const DWARFYAML::Entry &Entry, 22092874d28SXing GUO raw_ostream &OS, bool IsLittleEndian) { 22192874d28SXing GUO uint64_t EntryBegin = OS.tell(); 22292874d28SXing GUO encodeULEB128(Entry.AbbrCode, OS); 22392874d28SXing GUO uint32_t AbbrCode = Entry.AbbrCode; 22492874d28SXing GUO if (AbbrCode == 0 || Entry.Values.empty()) 22592874d28SXing GUO return OS.tell() - EntryBegin; 22607088c10SChris Bieneman 22792874d28SXing GUO if (AbbrCode > AbbrevDecls.size()) 22892874d28SXing GUO return createStringError( 22992874d28SXing GUO errc::invalid_argument, 23092874d28SXing GUO "abbrev code must be less than or equal to the number of " 23192874d28SXing GUO "entries in abbreviation table"); 23292874d28SXing GUO const DWARFYAML::Abbrev &Abbrev = AbbrevDecls[AbbrCode - 1]; 23392874d28SXing GUO auto FormVal = Entry.Values.begin(); 23492874d28SXing GUO auto AbbrForm = Abbrev.Attributes.begin(); 23592874d28SXing GUO for (; FormVal != Entry.Values.end() && AbbrForm != Abbrev.Attributes.end(); 23692874d28SXing GUO ++FormVal, ++AbbrForm) { 23792874d28SXing GUO dwarf::Form Form = AbbrForm->Form; 23892874d28SXing GUO bool Indirect; 23992874d28SXing GUO do { 24092874d28SXing GUO Indirect = false; 24192874d28SXing GUO switch (Form) { 24292874d28SXing GUO case dwarf::DW_FORM_addr: 24392874d28SXing GUO // TODO: Test this error. 244bbb057c4SXing GUO if (Error Err = writeVariableSizedInteger( 245bbb057c4SXing GUO FormVal->Value, Unit.FormParams.AddrSize, OS, IsLittleEndian)) 24692874d28SXing GUO return std::move(Err); 24792874d28SXing GUO break; 24892874d28SXing GUO case dwarf::DW_FORM_ref_addr: 24992874d28SXing GUO // TODO: Test this error. 25092874d28SXing GUO if (Error Err = writeVariableSizedInteger( 251bbb057c4SXing GUO FormVal->Value, Unit.FormParams.getRefAddrByteSize(), OS, 252bbb057c4SXing GUO IsLittleEndian)) 25392874d28SXing GUO return std::move(Err); 25492874d28SXing GUO break; 25592874d28SXing GUO case dwarf::DW_FORM_exprloc: 25692874d28SXing GUO case dwarf::DW_FORM_block: 25792874d28SXing GUO encodeULEB128(FormVal->BlockData.size(), OS); 25892874d28SXing GUO OS.write((const char *)FormVal->BlockData.data(), 25992874d28SXing GUO FormVal->BlockData.size()); 26092874d28SXing GUO break; 26192874d28SXing GUO case dwarf::DW_FORM_block1: { 26292874d28SXing GUO writeInteger((uint8_t)FormVal->BlockData.size(), OS, IsLittleEndian); 26392874d28SXing GUO OS.write((const char *)FormVal->BlockData.data(), 26492874d28SXing GUO FormVal->BlockData.size()); 26592874d28SXing GUO break; 26607088c10SChris Bieneman } 26792874d28SXing GUO case dwarf::DW_FORM_block2: { 26892874d28SXing GUO writeInteger((uint16_t)FormVal->BlockData.size(), OS, IsLittleEndian); 26992874d28SXing GUO OS.write((const char *)FormVal->BlockData.data(), 27092874d28SXing GUO FormVal->BlockData.size()); 27192874d28SXing GUO break; 27207088c10SChris Bieneman } 27392874d28SXing GUO case dwarf::DW_FORM_block4: { 27492874d28SXing GUO writeInteger((uint32_t)FormVal->BlockData.size(), OS, IsLittleEndian); 27592874d28SXing GUO OS.write((const char *)FormVal->BlockData.data(), 27692874d28SXing GUO FormVal->BlockData.size()); 27792874d28SXing GUO break; 27807088c10SChris Bieneman } 27992874d28SXing GUO case dwarf::DW_FORM_strx: 28092874d28SXing GUO case dwarf::DW_FORM_addrx: 28192874d28SXing GUO case dwarf::DW_FORM_rnglistx: 28292874d28SXing GUO case dwarf::DW_FORM_loclistx: 28392874d28SXing GUO case dwarf::DW_FORM_udata: 28492874d28SXing GUO case dwarf::DW_FORM_ref_udata: 28592874d28SXing GUO case dwarf::DW_FORM_GNU_addr_index: 28692874d28SXing GUO case dwarf::DW_FORM_GNU_str_index: 28792874d28SXing GUO encodeULEB128(FormVal->Value, OS); 28892874d28SXing GUO break; 28992874d28SXing GUO case dwarf::DW_FORM_data1: 29092874d28SXing GUO case dwarf::DW_FORM_ref1: 29192874d28SXing GUO case dwarf::DW_FORM_flag: 29292874d28SXing GUO case dwarf::DW_FORM_strx1: 29392874d28SXing GUO case dwarf::DW_FORM_addrx1: 29492874d28SXing GUO writeInteger((uint8_t)FormVal->Value, OS, IsLittleEndian); 29592874d28SXing GUO break; 29692874d28SXing GUO case dwarf::DW_FORM_data2: 29792874d28SXing GUO case dwarf::DW_FORM_ref2: 29892874d28SXing GUO case dwarf::DW_FORM_strx2: 29992874d28SXing GUO case dwarf::DW_FORM_addrx2: 30092874d28SXing GUO writeInteger((uint16_t)FormVal->Value, OS, IsLittleEndian); 30192874d28SXing GUO break; 30292874d28SXing GUO case dwarf::DW_FORM_data4: 30392874d28SXing GUO case dwarf::DW_FORM_ref4: 30492874d28SXing GUO case dwarf::DW_FORM_ref_sup4: 30592874d28SXing GUO case dwarf::DW_FORM_strx4: 30692874d28SXing GUO case dwarf::DW_FORM_addrx4: 30792874d28SXing GUO writeInteger((uint32_t)FormVal->Value, OS, IsLittleEndian); 30892874d28SXing GUO break; 30992874d28SXing GUO case dwarf::DW_FORM_data8: 31092874d28SXing GUO case dwarf::DW_FORM_ref8: 31192874d28SXing GUO case dwarf::DW_FORM_ref_sup8: 31292874d28SXing GUO case dwarf::DW_FORM_ref_sig8: 31392874d28SXing GUO writeInteger((uint64_t)FormVal->Value, OS, IsLittleEndian); 31492874d28SXing GUO break; 31592874d28SXing GUO case dwarf::DW_FORM_sdata: 31692874d28SXing GUO encodeSLEB128(FormVal->Value, OS); 31792874d28SXing GUO break; 31892874d28SXing GUO case dwarf::DW_FORM_string: 31992874d28SXing GUO OS.write(FormVal->CStr.data(), FormVal->CStr.size()); 32007088c10SChris Bieneman OS.write('\0'); 32192874d28SXing GUO break; 32292874d28SXing GUO case dwarf::DW_FORM_indirect: 32392874d28SXing GUO encodeULEB128(FormVal->Value, OS); 32492874d28SXing GUO Indirect = true; 32592874d28SXing GUO Form = static_cast<dwarf::Form>((uint64_t)FormVal->Value); 32692874d28SXing GUO ++FormVal; 32792874d28SXing GUO break; 32892874d28SXing GUO case dwarf::DW_FORM_strp: 32992874d28SXing GUO case dwarf::DW_FORM_sec_offset: 33092874d28SXing GUO case dwarf::DW_FORM_GNU_ref_alt: 33192874d28SXing GUO case dwarf::DW_FORM_GNU_strp_alt: 33292874d28SXing GUO case dwarf::DW_FORM_line_strp: 33392874d28SXing GUO case dwarf::DW_FORM_strp_sup: 334bbb057c4SXing GUO cantFail(writeVariableSizedInteger( 335bbb057c4SXing GUO FormVal->Value, Unit.FormParams.getDwarfOffsetByteSize(), OS, 336bbb057c4SXing GUO IsLittleEndian)); 33792874d28SXing GUO break; 33892874d28SXing GUO default: 33992874d28SXing GUO break; 34092874d28SXing GUO } 34192874d28SXing GUO } while (Indirect); 34207088c10SChris Bieneman } 3438f471f72SChris Bieneman 34492874d28SXing GUO return OS.tell() - EntryBegin; 34507088c10SChris Bieneman } 3468f471f72SChris Bieneman 3479939f231SXing GUO Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) { 34892874d28SXing GUO for (const DWARFYAML::Unit &Unit : DI.CompileUnits) { 34992874d28SXing GUO uint64_t Length = 3; // sizeof(version) + sizeof(address_size) 350bbb057c4SXing GUO Length += Unit.FormParams.Version >= 5 ? 1 : 0; // sizeof(unit_type) 35192874d28SXing GUO Length += 352bbb057c4SXing GUO Unit.FormParams.getDwarfOffsetByteSize(); // sizeof(debug_abbrev_offset) 35392874d28SXing GUO 35492874d28SXing GUO // Since the length of the current compilation unit is undetermined yet, we 35592874d28SXing GUO // firstly write the content of the compilation unit to a buffer to 35692874d28SXing GUO // calculate it and then serialize the buffer content to the actual output 35792874d28SXing GUO // stream. 35892874d28SXing GUO std::string EntryBuffer; 35992874d28SXing GUO raw_string_ostream EntryBufferOS(EntryBuffer); 36092874d28SXing GUO 36192874d28SXing GUO for (const DWARFYAML::Entry &Entry : Unit.Entries) { 36292874d28SXing GUO if (Expected<uint64_t> EntryLength = writeDIE( 36392874d28SXing GUO DI.AbbrevDecls, Unit, Entry, EntryBufferOS, DI.IsLittleEndian)) 36492874d28SXing GUO Length += *EntryLength; 36592874d28SXing GUO else 36692874d28SXing GUO return EntryLength.takeError(); 36792874d28SXing GUO } 36892874d28SXing GUO 36992874d28SXing GUO // If the length is specified in the YAML description, we use it instead of 37092874d28SXing GUO // the actual length. 37192874d28SXing GUO if (Unit.Length) 37292874d28SXing GUO Length = *Unit.Length; 37392874d28SXing GUO 374bbb057c4SXing GUO writeInitialLength(Unit.FormParams.Format, Length, OS, DI.IsLittleEndian); 375bbb057c4SXing GUO writeInteger((uint16_t)Unit.FormParams.Version, OS, DI.IsLittleEndian); 376bbb057c4SXing GUO if (Unit.FormParams.Version >= 5) { 37792874d28SXing GUO writeInteger((uint8_t)Unit.Type, OS, DI.IsLittleEndian); 378bbb057c4SXing GUO writeInteger((uint8_t)Unit.FormParams.AddrSize, OS, DI.IsLittleEndian); 379bbb057c4SXing GUO writeDWARFOffset(Unit.AbbrOffset, Unit.FormParams.Format, OS, 380bbb057c4SXing GUO DI.IsLittleEndian); 38192874d28SXing GUO } else { 382bbb057c4SXing GUO writeDWARFOffset(Unit.AbbrOffset, Unit.FormParams.Format, OS, 383bbb057c4SXing GUO DI.IsLittleEndian); 384bbb057c4SXing GUO writeInteger((uint8_t)Unit.FormParams.AddrSize, OS, DI.IsLittleEndian); 38592874d28SXing GUO } 38692874d28SXing GUO 38792874d28SXing GUO OS.write(EntryBuffer.data(), EntryBuffer.size()); 38892874d28SXing GUO } 38992874d28SXing GUO 39092874d28SXing GUO return Error::success(); 39107088c10SChris Bieneman } 39207088c10SChris Bieneman 39367b4afc4SXing GUO static void emitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) { 39407088c10SChris Bieneman OS.write(File.Name.data(), File.Name.size()); 39507088c10SChris Bieneman OS.write('\0'); 39607088c10SChris Bieneman encodeULEB128(File.DirIdx, OS); 39707088c10SChris Bieneman encodeULEB128(File.ModTime, OS); 39807088c10SChris Bieneman encodeULEB128(File.Length, OS); 39907088c10SChris Bieneman } 40007088c10SChris Bieneman 4019939f231SXing GUO Error DWARFYAML::emitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) { 402efcf06f5SBenjamin Kramer for (const auto &LineTable : DI.DebugLines) { 4030431e4bcSXing GUO writeInitialLength(LineTable.Format, LineTable.Length, OS, 4040431e4bcSXing GUO DI.IsLittleEndian); 4050431e4bcSXing GUO uint64_t SizeOfPrologueLength = LineTable.Format == dwarf::DWARF64 ? 8 : 4; 40607088c10SChris Bieneman writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian); 4079aaa32cfSXing GUO cantFail(writeVariableSizedInteger( 4089aaa32cfSXing GUO LineTable.PrologueLength, SizeOfPrologueLength, OS, DI.IsLittleEndian)); 40907088c10SChris Bieneman writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian); 41007088c10SChris Bieneman if (LineTable.Version >= 4) 41107088c10SChris Bieneman writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian); 41207088c10SChris Bieneman writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian); 41307088c10SChris Bieneman writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian); 41407088c10SChris Bieneman writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian); 41507088c10SChris Bieneman writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian); 41607088c10SChris Bieneman 41707088c10SChris Bieneman for (auto OpcodeLength : LineTable.StandardOpcodeLengths) 41807088c10SChris Bieneman writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian); 41907088c10SChris Bieneman 42007088c10SChris Bieneman for (auto IncludeDir : LineTable.IncludeDirs) { 42107088c10SChris Bieneman OS.write(IncludeDir.data(), IncludeDir.size()); 42207088c10SChris Bieneman OS.write('\0'); 42307088c10SChris Bieneman } 42407088c10SChris Bieneman OS.write('\0'); 42507088c10SChris Bieneman 42607088c10SChris Bieneman for (auto File : LineTable.Files) 42767b4afc4SXing GUO emitFileEntry(OS, File); 42807088c10SChris Bieneman OS.write('\0'); 42907088c10SChris Bieneman 43007088c10SChris Bieneman for (auto Op : LineTable.Opcodes) { 43107088c10SChris Bieneman writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian); 43207088c10SChris Bieneman if (Op.Opcode == 0) { 43307088c10SChris Bieneman encodeULEB128(Op.ExtLen, OS); 43407088c10SChris Bieneman writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian); 43507088c10SChris Bieneman switch (Op.SubOpcode) { 43607088c10SChris Bieneman case dwarf::DW_LNE_set_address: 43707088c10SChris Bieneman case dwarf::DW_LNE_set_discriminator: 4389aaa32cfSXing GUO // TODO: Test this error. 4399aaa32cfSXing GUO if (Error Err = writeVariableSizedInteger( 440bbb057c4SXing GUO Op.Data, DI.CompileUnits[0].FormParams.AddrSize, OS, 441bbb057c4SXing GUO DI.IsLittleEndian)) 4429aaa32cfSXing GUO return Err; 44307088c10SChris Bieneman break; 44407088c10SChris Bieneman case dwarf::DW_LNE_define_file: 44567b4afc4SXing GUO emitFileEntry(OS, Op.FileEntry); 44607088c10SChris Bieneman break; 44707088c10SChris Bieneman case dwarf::DW_LNE_end_sequence: 44807088c10SChris Bieneman break; 44907088c10SChris Bieneman default: 45007088c10SChris Bieneman for (auto OpByte : Op.UnknownOpcodeData) 45107088c10SChris Bieneman writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian); 45207088c10SChris Bieneman } 45307088c10SChris Bieneman } else if (Op.Opcode < LineTable.OpcodeBase) { 45407088c10SChris Bieneman switch (Op.Opcode) { 45507088c10SChris Bieneman case dwarf::DW_LNS_copy: 45607088c10SChris Bieneman case dwarf::DW_LNS_negate_stmt: 45707088c10SChris Bieneman case dwarf::DW_LNS_set_basic_block: 45807088c10SChris Bieneman case dwarf::DW_LNS_const_add_pc: 45907088c10SChris Bieneman case dwarf::DW_LNS_set_prologue_end: 46007088c10SChris Bieneman case dwarf::DW_LNS_set_epilogue_begin: 46107088c10SChris Bieneman break; 46207088c10SChris Bieneman 46307088c10SChris Bieneman case dwarf::DW_LNS_advance_pc: 46407088c10SChris Bieneman case dwarf::DW_LNS_set_file: 46507088c10SChris Bieneman case dwarf::DW_LNS_set_column: 46607088c10SChris Bieneman case dwarf::DW_LNS_set_isa: 46707088c10SChris Bieneman encodeULEB128(Op.Data, OS); 46807088c10SChris Bieneman break; 46907088c10SChris Bieneman 47007088c10SChris Bieneman case dwarf::DW_LNS_advance_line: 47107088c10SChris Bieneman encodeSLEB128(Op.SData, OS); 47207088c10SChris Bieneman break; 47307088c10SChris Bieneman 47407088c10SChris Bieneman case dwarf::DW_LNS_fixed_advance_pc: 47507088c10SChris Bieneman writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian); 47607088c10SChris Bieneman break; 47707088c10SChris Bieneman 47807088c10SChris Bieneman default: 47907088c10SChris Bieneman for (auto OpData : Op.StandardOpcodeData) { 48007088c10SChris Bieneman encodeULEB128(OpData, OS); 48107088c10SChris Bieneman } 48207088c10SChris Bieneman } 48307088c10SChris Bieneman } 48407088c10SChris Bieneman } 48507088c10SChris Bieneman } 4869939f231SXing GUO 4879939f231SXing GUO return Error::success(); 48807088c10SChris Bieneman } 4892e752db4SChris Bieneman 4908aaeaddeSXing GUO Error DWARFYAML::emitDebugAddr(raw_ostream &OS, const Data &DI) { 4918aaeaddeSXing GUO for (const AddrTableEntry &TableEntry : DI.DebugAddr) { 4928aaeaddeSXing GUO uint8_t AddrSize; 4938aaeaddeSXing GUO if (TableEntry.AddrSize) 4948aaeaddeSXing GUO AddrSize = *TableEntry.AddrSize; 4958aaeaddeSXing GUO else 496830a7c2aSXing GUO AddrSize = DI.Is64BitAddrSize ? 8 : 4; 4978aaeaddeSXing GUO 4988aaeaddeSXing GUO uint64_t Length; 4998aaeaddeSXing GUO if (TableEntry.Length) 5008aaeaddeSXing GUO Length = (uint64_t)*TableEntry.Length; 5018aaeaddeSXing GUO else 5028aaeaddeSXing GUO // 2 (version) + 1 (address_size) + 1 (segment_selector_size) = 4 5038aaeaddeSXing GUO Length = 4 + (AddrSize + TableEntry.SegSelectorSize) * 5048aaeaddeSXing GUO TableEntry.SegAddrPairs.size(); 5058aaeaddeSXing GUO 5068aaeaddeSXing GUO writeInitialLength(TableEntry.Format, Length, OS, DI.IsLittleEndian); 5078aaeaddeSXing GUO writeInteger((uint16_t)TableEntry.Version, OS, DI.IsLittleEndian); 5088aaeaddeSXing GUO writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian); 5098aaeaddeSXing GUO writeInteger((uint8_t)TableEntry.SegSelectorSize, OS, DI.IsLittleEndian); 5108aaeaddeSXing GUO 5118aaeaddeSXing GUO for (const SegAddrPair &Pair : TableEntry.SegAddrPairs) { 5128aaeaddeSXing GUO if (TableEntry.SegSelectorSize != 0) 5139aaa32cfSXing GUO if (Error Err = writeVariableSizedInteger(Pair.Segment, 5149aaa32cfSXing GUO TableEntry.SegSelectorSize, 5159aaa32cfSXing GUO OS, DI.IsLittleEndian)) 5169aaa32cfSXing GUO return createStringError(errc::not_supported, 5179aaa32cfSXing GUO "unable to write debug_addr segment: %s", 5189aaa32cfSXing GUO toString(std::move(Err)).c_str()); 5198aaeaddeSXing GUO if (AddrSize != 0) 5209aaa32cfSXing GUO if (Error Err = writeVariableSizedInteger(Pair.Address, AddrSize, OS, 5219aaa32cfSXing GUO DI.IsLittleEndian)) 5229aaa32cfSXing GUO return createStringError(errc::not_supported, 5239aaa32cfSXing GUO "unable to write debug_addr address: %s", 5249aaa32cfSXing GUO toString(std::move(Err)).c_str()); 5258aaeaddeSXing GUO } 5268aaeaddeSXing GUO } 5278aaeaddeSXing GUO 5288aaeaddeSXing GUO return Error::success(); 5298aaeaddeSXing GUO } 5308aaeaddeSXing GUO 53110478c9cSXing GUO Error DWARFYAML::emitDebugStrOffsets(raw_ostream &OS, const Data &DI) { 53210478c9cSXing GUO assert(DI.DebugStrOffsets && "unexpected emitDebugStrOffsets() call"); 53310478c9cSXing GUO for (const DWARFYAML::StringOffsetsTable &Table : *DI.DebugStrOffsets) { 53410478c9cSXing GUO uint64_t Length; 53510478c9cSXing GUO if (Table.Length) 53610478c9cSXing GUO Length = *Table.Length; 53710478c9cSXing GUO else 53810478c9cSXing GUO // sizeof(version) + sizeof(padding) = 4 53910478c9cSXing GUO Length = 54010478c9cSXing GUO 4 + Table.Offsets.size() * (Table.Format == dwarf::DWARF64 ? 8 : 4); 54110478c9cSXing GUO 54210478c9cSXing GUO writeInitialLength(Table.Format, Length, OS, DI.IsLittleEndian); 54310478c9cSXing GUO writeInteger((uint16_t)Table.Version, OS, DI.IsLittleEndian); 54410478c9cSXing GUO writeInteger((uint16_t)Table.Padding, OS, DI.IsLittleEndian); 54510478c9cSXing GUO 546367d0d4cSXing GUO for (uint64_t Offset : Table.Offsets) 547367d0d4cSXing GUO writeDWARFOffset(Offset, Table.Format, OS, DI.IsLittleEndian); 54810478c9cSXing GUO } 54910478c9cSXing GUO 55010478c9cSXing GUO return Error::success(); 55110478c9cSXing GUO } 55210478c9cSXing GUO 5536784d82dSXing GUO static Error checkOperandCount(StringRef EncodingString, 554a997e6edSXing GUO ArrayRef<yaml::Hex64> Values, 555a997e6edSXing GUO uint64_t ExpectedOperands) { 556a997e6edSXing GUO if (Values.size() != ExpectedOperands) 557a997e6edSXing GUO return createStringError( 558a997e6edSXing GUO errc::invalid_argument, 559a997e6edSXing GUO "invalid number (%zu) of operands for the operator: %s, %" PRIu64 560a997e6edSXing GUO " expected", 561a997e6edSXing GUO Values.size(), EncodingString.str().c_str(), ExpectedOperands); 562a997e6edSXing GUO 563a997e6edSXing GUO return Error::success(); 564a997e6edSXing GUO } 565a997e6edSXing GUO 566a997e6edSXing GUO static Error writeListEntryAddress(StringRef EncodingName, raw_ostream &OS, 567a997e6edSXing GUO uint64_t Addr, uint8_t AddrSize, 568a997e6edSXing GUO bool IsLittleEndian) { 569a997e6edSXing GUO if (Error Err = writeVariableSizedInteger(Addr, AddrSize, OS, IsLittleEndian)) 570a997e6edSXing GUO return createStringError(errc::invalid_argument, 571a997e6edSXing GUO "unable to write address for the operator %s: %s", 572a997e6edSXing GUO EncodingName.str().c_str(), 573a997e6edSXing GUO toString(std::move(Err)).c_str()); 574a997e6edSXing GUO 575a997e6edSXing GUO return Error::success(); 576a997e6edSXing GUO } 577a997e6edSXing GUO 578c4cf250cSXing GUO static Expected<uint64_t> writeListEntry(raw_ostream &OS, 579c4cf250cSXing GUO const DWARFYAML::RnglistEntry &Entry, 580c4cf250cSXing GUO uint8_t AddrSize, 581c4cf250cSXing GUO bool IsLittleEndian) { 5821ab3d6c8SXing GUO uint64_t BeginOffset = OS.tell(); 5831ab3d6c8SXing GUO writeInteger((uint8_t)Entry.Operator, OS, IsLittleEndian); 5841ab3d6c8SXing GUO 585a997e6edSXing GUO StringRef EncodingName = dwarf::RangeListEncodingString(Entry.Operator); 5861ab3d6c8SXing GUO 587a997e6edSXing GUO auto CheckOperands = [&](uint64_t ExpectedOperands) -> Error { 5886784d82dSXing GUO return checkOperandCount(EncodingName, Entry.Values, ExpectedOperands); 5891ab3d6c8SXing GUO }; 5901ab3d6c8SXing GUO 5911ab3d6c8SXing GUO auto WriteAddress = [&](uint64_t Addr) -> Error { 592a997e6edSXing GUO return writeListEntryAddress(EncodingName, OS, Addr, AddrSize, 593a997e6edSXing GUO IsLittleEndian); 5941ab3d6c8SXing GUO }; 5951ab3d6c8SXing GUO 5961ab3d6c8SXing GUO switch (Entry.Operator) { 5971ab3d6c8SXing GUO case dwarf::DW_RLE_end_of_list: 5981ab3d6c8SXing GUO if (Error Err = CheckOperands(0)) 5991ab3d6c8SXing GUO return std::move(Err); 6001ab3d6c8SXing GUO break; 6011ab3d6c8SXing GUO case dwarf::DW_RLE_base_addressx: 6021ab3d6c8SXing GUO if (Error Err = CheckOperands(1)) 6031ab3d6c8SXing GUO return std::move(Err); 6041ab3d6c8SXing GUO encodeULEB128(Entry.Values[0], OS); 6051ab3d6c8SXing GUO break; 6061ab3d6c8SXing GUO case dwarf::DW_RLE_startx_endx: 6071ab3d6c8SXing GUO case dwarf::DW_RLE_startx_length: 6081ab3d6c8SXing GUO case dwarf::DW_RLE_offset_pair: 6091ab3d6c8SXing GUO if (Error Err = CheckOperands(2)) 6101ab3d6c8SXing GUO return std::move(Err); 6111ab3d6c8SXing GUO encodeULEB128(Entry.Values[0], OS); 6121ab3d6c8SXing GUO encodeULEB128(Entry.Values[1], OS); 6131ab3d6c8SXing GUO break; 6141ab3d6c8SXing GUO case dwarf::DW_RLE_base_address: 6151ab3d6c8SXing GUO if (Error Err = CheckOperands(1)) 6161ab3d6c8SXing GUO return std::move(Err); 6171ab3d6c8SXing GUO if (Error Err = WriteAddress(Entry.Values[0])) 6181ab3d6c8SXing GUO return std::move(Err); 6191ab3d6c8SXing GUO break; 6201ab3d6c8SXing GUO case dwarf::DW_RLE_start_end: 6211ab3d6c8SXing GUO if (Error Err = CheckOperands(2)) 6221ab3d6c8SXing GUO return std::move(Err); 6231ab3d6c8SXing GUO if (Error Err = WriteAddress(Entry.Values[0])) 6241ab3d6c8SXing GUO return std::move(Err); 6251ab3d6c8SXing GUO cantFail(WriteAddress(Entry.Values[1])); 6261ab3d6c8SXing GUO break; 6271ab3d6c8SXing GUO case dwarf::DW_RLE_start_length: 6281ab3d6c8SXing GUO if (Error Err = CheckOperands(2)) 6291ab3d6c8SXing GUO return std::move(Err); 6301ab3d6c8SXing GUO if (Error Err = WriteAddress(Entry.Values[0])) 6311ab3d6c8SXing GUO return std::move(Err); 6321ab3d6c8SXing GUO encodeULEB128(Entry.Values[1], OS); 6331ab3d6c8SXing GUO break; 6341ab3d6c8SXing GUO } 6351ab3d6c8SXing GUO 6361ab3d6c8SXing GUO return OS.tell() - BeginOffset; 6371ab3d6c8SXing GUO } 6381ab3d6c8SXing GUO 639c4cf250cSXing GUO template <typename EntryType> 640c4cf250cSXing GUO Error writeDWARFLists(raw_ostream &OS, 641c4cf250cSXing GUO ArrayRef<DWARFYAML::ListTable<EntryType>> Tables, 642c4cf250cSXing GUO bool IsLittleEndian, bool Is64BitAddrSize) { 643c4cf250cSXing GUO for (const DWARFYAML::ListTable<EntryType> &Table : Tables) { 6441ab3d6c8SXing GUO // sizeof(version) + sizeof(address_size) + sizeof(segment_selector_size) + 6451ab3d6c8SXing GUO // sizeof(offset_entry_count) = 8 6461ab3d6c8SXing GUO uint64_t Length = 8; 6471ab3d6c8SXing GUO 6481ab3d6c8SXing GUO uint8_t AddrSize; 6491ab3d6c8SXing GUO if (Table.AddrSize) 6501ab3d6c8SXing GUO AddrSize = *Table.AddrSize; 6511ab3d6c8SXing GUO else 652c4cf250cSXing GUO AddrSize = Is64BitAddrSize ? 8 : 4; 6531ab3d6c8SXing GUO 654c4cf250cSXing GUO // Since the length of the current range/location lists entry is 655c4cf250cSXing GUO // undetermined yet, we firstly write the content of the range/location 656c4cf250cSXing GUO // lists to a buffer to calculate the length and then serialize the buffer 657c4cf250cSXing GUO // content to the actual output stream. 6581ab3d6c8SXing GUO std::string ListBuffer; 6591ab3d6c8SXing GUO raw_string_ostream ListBufferOS(ListBuffer); 6601ab3d6c8SXing GUO 661c4cf250cSXing GUO // Offsets holds offsets for each range/location list. The i-th element is 662c4cf250cSXing GUO // the offset from the beginning of the first range/location list to the 663c4cf250cSXing GUO // location of the i-th range list. 6641ab3d6c8SXing GUO std::vector<uint64_t> Offsets; 6651ab3d6c8SXing GUO 666c4cf250cSXing GUO for (const DWARFYAML::ListEntries<EntryType> &List : Table.Lists) { 6671ab3d6c8SXing GUO Offsets.push_back(ListBufferOS.tell()); 66822ec861dSXing GUO if (List.Content) { 66922ec861dSXing GUO List.Content->writeAsBinary(ListBufferOS, UINT64_MAX); 67022ec861dSXing GUO Length += List.Content->binary_size(); 67122ec861dSXing GUO } else if (List.Entries) { 67222ec861dSXing GUO for (const EntryType &Entry : *List.Entries) { 6731ab3d6c8SXing GUO Expected<uint64_t> EntrySize = 674c4cf250cSXing GUO writeListEntry(ListBufferOS, Entry, AddrSize, IsLittleEndian); 6751ab3d6c8SXing GUO if (!EntrySize) 6761ab3d6c8SXing GUO return EntrySize.takeError(); 6771ab3d6c8SXing GUO Length += *EntrySize; 6781ab3d6c8SXing GUO } 6791ab3d6c8SXing GUO } 68022ec861dSXing GUO } 6811ab3d6c8SXing GUO 6821ab3d6c8SXing GUO // If the offset_entry_count field isn't specified, yaml2obj will infer it 6831ab3d6c8SXing GUO // from the 'Offsets' field in the YAML description. If the 'Offsets' field 6841ab3d6c8SXing GUO // isn't specified either, yaml2obj will infer it from the auto-generated 6851ab3d6c8SXing GUO // offsets. 6861ab3d6c8SXing GUO uint32_t OffsetEntryCount; 6871ab3d6c8SXing GUO if (Table.OffsetEntryCount) 6881ab3d6c8SXing GUO OffsetEntryCount = *Table.OffsetEntryCount; 6891ab3d6c8SXing GUO else 6901ab3d6c8SXing GUO OffsetEntryCount = Table.Offsets ? Table.Offsets->size() : Offsets.size(); 6911ab3d6c8SXing GUO uint64_t OffsetsSize = 6921ab3d6c8SXing GUO OffsetEntryCount * (Table.Format == dwarf::DWARF64 ? 8 : 4); 6931ab3d6c8SXing GUO Length += OffsetsSize; 6941ab3d6c8SXing GUO 6951ab3d6c8SXing GUO // If the length is specified in the YAML description, we use it instead of 6961ab3d6c8SXing GUO // the actual length. 6971ab3d6c8SXing GUO if (Table.Length) 6981ab3d6c8SXing GUO Length = *Table.Length; 6991ab3d6c8SXing GUO 700c4cf250cSXing GUO writeInitialLength(Table.Format, Length, OS, IsLittleEndian); 701c4cf250cSXing GUO writeInteger((uint16_t)Table.Version, OS, IsLittleEndian); 702c4cf250cSXing GUO writeInteger((uint8_t)AddrSize, OS, IsLittleEndian); 703c4cf250cSXing GUO writeInteger((uint8_t)Table.SegSelectorSize, OS, IsLittleEndian); 704c4cf250cSXing GUO writeInteger((uint32_t)OffsetEntryCount, OS, IsLittleEndian); 7051ab3d6c8SXing GUO 7061ab3d6c8SXing GUO auto EmitOffsets = [&](ArrayRef<uint64_t> Offsets, uint64_t OffsetsSize) { 707367d0d4cSXing GUO for (uint64_t Offset : Offsets) 708367d0d4cSXing GUO writeDWARFOffset(OffsetsSize + Offset, Table.Format, OS, 709367d0d4cSXing GUO IsLittleEndian); 7101ab3d6c8SXing GUO }; 7111ab3d6c8SXing GUO 7121ab3d6c8SXing GUO if (Table.Offsets) 7131ab3d6c8SXing GUO EmitOffsets(ArrayRef<uint64_t>((const uint64_t *)Table.Offsets->data(), 7141ab3d6c8SXing GUO Table.Offsets->size()), 7151ab3d6c8SXing GUO 0); 7161ab3d6c8SXing GUO else 7171ab3d6c8SXing GUO EmitOffsets(Offsets, OffsetsSize); 7181ab3d6c8SXing GUO 7191ab3d6c8SXing GUO OS.write(ListBuffer.data(), ListBuffer.size()); 7201ab3d6c8SXing GUO } 7211ab3d6c8SXing GUO 7221ab3d6c8SXing GUO return Error::success(); 7231ab3d6c8SXing GUO } 7241ab3d6c8SXing GUO 725c4cf250cSXing GUO Error DWARFYAML::emitDebugRnglists(raw_ostream &OS, const Data &DI) { 726c4cf250cSXing GUO assert(DI.DebugRnglists && "unexpected emitDebugRnglists() call"); 727c4cf250cSXing GUO return writeDWARFLists<DWARFYAML::RnglistEntry>( 728c4cf250cSXing GUO OS, *DI.DebugRnglists, DI.IsLittleEndian, DI.Is64BitAddrSize); 729c4cf250cSXing GUO } 730c4cf250cSXing GUO 7319939f231SXing GUO using EmitFuncType = Error (*)(raw_ostream &, const DWARFYAML::Data &); 7322e752db4SChris Bieneman 7339939f231SXing GUO static Error 73467b4afc4SXing GUO emitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc, 735efcf06f5SBenjamin Kramer StringRef Sec, 7362e752db4SChris Bieneman StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) { 7372e752db4SChris Bieneman std::string Data; 7382e752db4SChris Bieneman raw_string_ostream DebugInfoStream(Data); 7399939f231SXing GUO if (Error Err = EmitFunc(DebugInfoStream, DI)) 7409939f231SXing GUO return Err; 7412e752db4SChris Bieneman DebugInfoStream.flush(); 7422e752db4SChris Bieneman if (!Data.empty()) 7432e752db4SChris Bieneman OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data); 7449939f231SXing GUO 7459939f231SXing GUO return Error::success(); 7462e752db4SChris Bieneman } 7472e752db4SChris Bieneman 7485c709edaSJonas Devlieghere Expected<StringMap<std::unique_ptr<MemoryBuffer>>> 74992874d28SXing GUO DWARFYAML::emitDebugSections(StringRef YAMLString, bool IsLittleEndian) { 7508f9ca561SXing GUO auto CollectDiagnostic = [](const SMDiagnostic &Diag, void *DiagContext) { 7518f9ca561SXing GUO *static_cast<SMDiagnostic *>(DiagContext) = Diag; 7528f9ca561SXing GUO }; 7538f9ca561SXing GUO 7548f9ca561SXing GUO SMDiagnostic GeneratedDiag; 7558f9ca561SXing GUO yaml::Input YIn(YAMLString, /*Ctxt=*/nullptr, CollectDiagnostic, 7568f9ca561SXing GUO &GeneratedDiag); 7572e752db4SChris Bieneman 7582e752db4SChris Bieneman DWARFYAML::Data DI; 7592e752db4SChris Bieneman DI.IsLittleEndian = IsLittleEndian; 7602e752db4SChris Bieneman YIn >> DI; 7612e752db4SChris Bieneman if (YIn.error()) 7628f9ca561SXing GUO return createStringError(YIn.error(), GeneratedDiag.getMessage()); 7632e752db4SChris Bieneman 7645c709edaSJonas Devlieghere StringMap<std::unique_ptr<MemoryBuffer>> DebugSections; 7659939f231SXing GUO Error Err = emitDebugSectionImpl(DI, &DWARFYAML::emitDebugInfo, "debug_info", 7662e752db4SChris Bieneman DebugSections); 7679939f231SXing GUO Err = joinErrors(std::move(Err), 7689939f231SXing GUO emitDebugSectionImpl(DI, &DWARFYAML::emitDebugLine, 7699939f231SXing GUO "debug_line", DebugSections)); 7709939f231SXing GUO Err = joinErrors(std::move(Err), 7719939f231SXing GUO emitDebugSectionImpl(DI, &DWARFYAML::emitDebugStr, 7729939f231SXing GUO "debug_str", DebugSections)); 7739939f231SXing GUO Err = joinErrors(std::move(Err), 7749939f231SXing GUO emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAbbrev, 7759939f231SXing GUO "debug_abbrev", DebugSections)); 7769939f231SXing GUO Err = joinErrors(std::move(Err), 7779939f231SXing GUO emitDebugSectionImpl(DI, &DWARFYAML::emitDebugAranges, 7789939f231SXing GUO "debug_aranges", DebugSections)); 7799939f231SXing GUO Err = joinErrors(std::move(Err), 7809939f231SXing GUO emitDebugSectionImpl(DI, &DWARFYAML::emitDebugRanges, 7819939f231SXing GUO "debug_ranges", DebugSections)); 7829939f231SXing GUO 7839939f231SXing GUO if (Err) 7849939f231SXing GUO return std::move(Err); 785c55cf4afSBill Wendling return std::move(DebugSections); 7862e752db4SChris Bieneman } 787