1//===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
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#include "obj2yaml.h"
10#include "llvm/ADT/STLExtras.h"
11#include "llvm/ADT/Twine.h"
12#include "llvm/DebugInfo/DWARF/DWARFContext.h"
13#include "llvm/Object/ELFObjectFile.h"
14#include "llvm/ObjectYAML/DWARFYAML.h"
15#include "llvm/ObjectYAML/ELFYAML.h"
16#include "llvm/Support/DataExtractor.h"
17#include "llvm/Support/Errc.h"
18#include "llvm/Support/ErrorHandling.h"
19#include "llvm/Support/YAMLTraits.h"
20#include <optional>
21
22using namespace llvm;
23
24namespace {
25
26template <class ELFT>
27class ELFDumper {
28 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
29
30 ArrayRef<Elf_Shdr> Sections;
31 ArrayRef<Elf_Sym> SymTable;
32
33 DenseMap<StringRef, uint32_t> UsedSectionNames;
34 std::vector<std::string> SectionNames;
35 std::optional<uint32_t> ShStrTabIndex;
36
37 DenseMap<StringRef, uint32_t> UsedSymbolNames;
38 std::vector<std::string> SymbolNames;
39
40 BumpPtrAllocator StringAllocator;
41
42 Expected<StringRef> getUniquedSectionName(const Elf_Shdr &Sec);
43 Expected<StringRef> getUniquedSymbolName(const Elf_Sym *Sym,
44 StringRef StrTable,
45 const Elf_Shdr *SymTab);
46 Expected<StringRef> getSymbolName(uint32_t SymtabNdx, uint32_t SymbolNdx);
47
48 const object::ELFFile<ELFT> &Obj;
49 std::unique_ptr<DWARFContext> DWARFCtx;
50
51 DenseMap<const Elf_Shdr *, ArrayRef<Elf_Word>> ShndxTables;
52
53 Expected<std::vector<ELFYAML::ProgramHeader>>
54 dumpProgramHeaders(ArrayRef<std::unique_ptr<ELFYAML::Chunk>> Sections);
55
56 std::optional<DWARFYAML::Data>
57 dumpDWARFSections(std::vector<std::unique_ptr<ELFYAML::Chunk>> &Sections);
58
59 Error dumpSymbols(const Elf_Shdr *Symtab,
60 std::optional<std::vector<ELFYAML::Symbol>> &Symbols);
61 Error dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
62 StringRef StrTable, ELFYAML::Symbol &S);
63 Expected<std::vector<std::unique_ptr<ELFYAML::Chunk>>> dumpSections();
64 Error dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
65 Error dumpCommonRelocationSection(const Elf_Shdr *Shdr,
66 ELFYAML::RelocationSection &S);
67 template <class RelT>
68 Error dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
69 ELFYAML::Relocation &R);
70
71 Expected<ELFYAML::AddrsigSection *> dumpAddrsigSection(const Elf_Shdr *Shdr);
72 Expected<ELFYAML::LinkerOptionsSection *>
73 dumpLinkerOptionsSection(const Elf_Shdr *Shdr);
74 Expected<ELFYAML::DependentLibrariesSection *>
75 dumpDependentLibrariesSection(const Elf_Shdr *Shdr);
76 Expected<ELFYAML::CallGraphProfileSection *>
77 dumpCallGraphProfileSection(const Elf_Shdr *Shdr);
78 Expected<ELFYAML::DynamicSection *> dumpDynamicSection(const Elf_Shdr *Shdr);
79 Expected<ELFYAML::RelocationSection *> dumpRelocSection(const Elf_Shdr *Shdr);
80 Expected<ELFYAML::RelrSection *> dumpRelrSection(const Elf_Shdr *Shdr);
81 Expected<ELFYAML::RawContentSection *>
82 dumpContentSection(const Elf_Shdr *Shdr);
83 Expected<ELFYAML::SymtabShndxSection *>
84 dumpSymtabShndxSection(const Elf_Shdr *Shdr);
85 Expected<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
86 Expected<ELFYAML::HashSection *> dumpHashSection(const Elf_Shdr *Shdr);
87 Expected<ELFYAML::NoteSection *> dumpNoteSection(const Elf_Shdr *Shdr);
88 Expected<ELFYAML::GnuHashSection *> dumpGnuHashSection(const Elf_Shdr *Shdr);
89 Expected<ELFYAML::VerdefSection *> dumpVerdefSection(const Elf_Shdr *Shdr);
90 Expected<ELFYAML::SymverSection *> dumpSymverSection(const Elf_Shdr *Shdr);
91 Expected<ELFYAML::VerneedSection *> dumpVerneedSection(const Elf_Shdr *Shdr);
92 Expected<ELFYAML::GroupSection *> dumpGroupSection(const Elf_Shdr *Shdr);
93 Expected<ELFYAML::ARMIndexTableSection *>
94 dumpARMIndexTableSection(const Elf_Shdr *Shdr);
95 Expected<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
96 Expected<ELFYAML::StackSizesSection *>
97 dumpStackSizesSection(const Elf_Shdr *Shdr);
98 Expected<ELFYAML::BBAddrMapSection *>
99 dumpBBAddrMapSection(const Elf_Shdr *Shdr);
100 Expected<ELFYAML::RawContentSection *>
101 dumpPlaceholderSection(const Elf_Shdr *Shdr);
102
103 bool shouldPrintSection(const ELFYAML::Section &S, const Elf_Shdr &SHdr,
104 std::optional<DWARFYAML::Data> DWARF);
105
106public:
107 ELFDumper(const object::ELFFile<ELFT> &O, std::unique_ptr<DWARFContext> DCtx);
108 Expected<ELFYAML::Object *> dump();
109};
110
111}
112
113template <class ELFT>
114ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O,
115 std::unique_ptr<DWARFContext> DCtx)
116 : Obj(O), DWARFCtx(std::move(DCtx)) {}
117
118template <class ELFT>
119Expected<StringRef>
120ELFDumper<ELFT>::getUniquedSectionName(const Elf_Shdr &Sec) {
121 unsigned SecIndex = &Sec - &Sections[0];
122 if (!SectionNames[SecIndex].empty())
123 return SectionNames[SecIndex];
124
125 auto NameOrErr = Obj.getSectionName(Sec);
126 if (!NameOrErr)
127 return NameOrErr;
128 StringRef Name = *NameOrErr;
129 // In some specific cases we might have more than one section without a
130 // name (sh_name == 0). It normally doesn't happen, but when we have this case
131 // it doesn't make sense to uniquify their names and add noise to the output.
132 if (Name.empty())
133 return "";
134
135 std::string &Ret = SectionNames[SecIndex];
136
137 auto It = UsedSectionNames.insert(KV: {Name, 0});
138 if (!It.second)
139 Ret = ELFYAML::appendUniqueSuffix(Name, Msg: Twine(++It.first->second));
140 else
141 Ret = std::string(Name);
142 return Ret;
143}
144
145template <class ELFT>
146Expected<StringRef>
147ELFDumper<ELFT>::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable,
148 const Elf_Shdr *SymTab) {
149 Expected<StringRef> SymbolNameOrErr = Sym->getName(StrTable);
150 if (!SymbolNameOrErr)
151 return SymbolNameOrErr;
152 StringRef Name = *SymbolNameOrErr;
153 if (Name.empty() && Sym->getType() == ELF::STT_SECTION) {
154 Expected<const Elf_Shdr *> ShdrOrErr =
155 Obj.getSection(*Sym, SymTab, ShndxTables.lookup(SymTab));
156 if (!ShdrOrErr)
157 return ShdrOrErr.takeError();
158 // The null section has no name.
159 return (*ShdrOrErr == nullptr) ? "" : getUniquedSectionName(Sec: **ShdrOrErr);
160 }
161
162 // Symbols in .symtab can have duplicate names. For example, it is a common
163 // situation for local symbols in a relocatable object. Here we assign unique
164 // suffixes for such symbols so that we can differentiate them.
165 if (SymTab->sh_type == ELF::SHT_SYMTAB) {
166 unsigned Index = Sym - SymTable.data();
167 if (!SymbolNames[Index].empty())
168 return SymbolNames[Index];
169
170 auto It = UsedSymbolNames.insert(KV: {Name, 0});
171 if (!It.second)
172 SymbolNames[Index] =
173 ELFYAML::appendUniqueSuffix(Name, Msg: Twine(++It.first->second));
174 else
175 SymbolNames[Index] = std::string(Name);
176 return SymbolNames[Index];
177 }
178
179 return Name;
180}
181
182template <class ELFT>
183bool ELFDumper<ELFT>::shouldPrintSection(const ELFYAML::Section &S,
184 const Elf_Shdr &SHdr,
185 std::optional<DWARFYAML::Data> DWARF) {
186 // We only print the SHT_NULL section at index 0 when it
187 // has at least one non-null field, because yaml2obj
188 // normally creates the zero section at index 0 implicitly.
189 if (S.Type == ELF::SHT_NULL && (&SHdr == &Sections[0])) {
190 const uint8_t *Begin = reinterpret_cast<const uint8_t *>(&SHdr);
191 const uint8_t *End = Begin + sizeof(Elf_Shdr);
192 return std::any_of(Begin, End, [](uint8_t V) { return V != 0; });
193 }
194
195 // Normally we use "DWARF:" to describe contents of DWARF sections. Sometimes
196 // the content of DWARF sections can be successfully parsed into the "DWARF:"
197 // entry but their section headers may have special flags, entry size, address
198 // alignment, etc. We will preserve the header for them under such
199 // circumstances.
200 StringRef SecName = S.Name.substr(Start: 1);
201 if (DWARF && DWARF->getNonEmptySectionNames().count(key: SecName)) {
202 if (const ELFYAML::RawContentSection *RawSec =
203 dyn_cast<const ELFYAML::RawContentSection>(Val: &S)) {
204 if (RawSec->Type != ELF::SHT_PROGBITS || RawSec->Link || RawSec->Info ||
205 RawSec->AddressAlign != yaml::Hex64{1} || RawSec->Address ||
206 RawSec->EntSize)
207 return true;
208
209 ELFYAML::ELF_SHF ShFlags = RawSec->Flags.value_or(u: ELFYAML::ELF_SHF(0));
210
211 if (SecName == "debug_str")
212 return ShFlags != ELFYAML::ELF_SHF(ELF::SHF_MERGE | ELF::SHF_STRINGS);
213
214 return ShFlags != ELFYAML::ELF_SHF{0};
215 }
216 }
217
218 // Normally we use "Symbols:" and "DynamicSymbols:" to describe contents of
219 // symbol tables. We also build and emit corresponding string tables
220 // implicitly. But sometimes it is important to preserve positions and virtual
221 // addresses of allocatable sections, e.g. for creating program headers.
222 // Generally we are trying to reduce noise in the YAML output. Because
223 // of that we do not print non-allocatable versions of such sections and
224 // assume they are placed at the end.
225 // We also dump symbol tables when the Size field is set. It happens when they
226 // are empty, which should not normally happen.
227 if (S.Type == ELF::SHT_STRTAB || S.Type == ELF::SHT_SYMTAB ||
228 S.Type == ELF::SHT_DYNSYM) {
229 return S.Size || S.Flags.value_or(u: ELFYAML::ELF_SHF(0)) & ELF::SHF_ALLOC;
230 }
231
232 return true;
233}
234
235template <class ELFT>
236static void dumpSectionOffsets(const typename ELFT::Ehdr &Header,
237 ArrayRef<ELFYAML::ProgramHeader> Phdrs,
238 std::vector<std::unique_ptr<ELFYAML::Chunk>> &V,
239 ArrayRef<typename ELFT::Shdr> S) {
240 if (V.empty())
241 return;
242
243 uint64_t ExpectedOffset;
244 if (Header.e_phoff > 0)
245 ExpectedOffset = Header.e_phoff + Header.e_phentsize * Header.e_phnum;
246 else
247 ExpectedOffset = sizeof(typename ELFT::Ehdr);
248
249 for (const std::unique_ptr<ELFYAML::Chunk> &C : ArrayRef(V).drop_front()) {
250 ELFYAML::Section &Sec = *cast<ELFYAML::Section>(Val: C.get());
251 const typename ELFT::Shdr &SecHdr = S[Sec.OriginalSecNdx];
252
253 ExpectedOffset = alignTo(ExpectedOffset,
254 SecHdr.sh_addralign ? SecHdr.sh_addralign : 1uLL);
255
256 // We only set the "Offset" field when it can't be naturally derived
257 // from the offset and size of the previous section. This reduces
258 // the noise in the YAML output.
259 if (SecHdr.sh_offset != ExpectedOffset)
260 Sec.Offset = (yaml::Hex64)SecHdr.sh_offset;
261
262 if (Sec.Type == ELF::SHT_NOBITS &&
263 !ELFYAML::shouldAllocateFileSpace(Phdrs,
264 S: *cast<ELFYAML::NoBitsSection>(Val: &Sec)))
265 ExpectedOffset = SecHdr.sh_offset;
266 else
267 ExpectedOffset = SecHdr.sh_offset + SecHdr.sh_size;
268 }
269}
270
271template <class ELFT> Expected<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
272 auto Y = std::make_unique<ELFYAML::Object>();
273
274 // Dump header. We do not dump EPh* and ESh* fields. When not explicitly set,
275 // the values are set by yaml2obj automatically and there is no need to dump
276 // them here.
277 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader().getFileClass());
278 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader().getDataEncoding());
279 Y->Header.OSABI = Obj.getHeader().e_ident[ELF::EI_OSABI];
280 Y->Header.ABIVersion = Obj.getHeader().e_ident[ELF::EI_ABIVERSION];
281 Y->Header.Type = Obj.getHeader().e_type;
282 if (Obj.getHeader().e_machine != 0)
283 Y->Header.Machine = ELFYAML::ELF_EM(Obj.getHeader().e_machine);
284 if (Obj.getHeader().e_flags != 0)
285 Y->Header.Flags = ELFYAML::ELF_EF(Obj.getHeader().e_flags);
286 Y->Header.Entry = Obj.getHeader().e_entry;
287
288 // Dump sections
289 auto SectionsOrErr = Obj.sections();
290 if (!SectionsOrErr)
291 return SectionsOrErr.takeError();
292 Sections = *SectionsOrErr;
293 SectionNames.resize(Sections.size());
294
295 if (Sections.size() > 0) {
296 ShStrTabIndex = Obj.getHeader().e_shstrndx;
297 if (*ShStrTabIndex == ELF::SHN_XINDEX)
298 ShStrTabIndex = Sections[0].sh_link;
299 // TODO: Set EShStrndx if the value doesn't represent a real section.
300 }
301
302 // Normally an object that does not have sections has e_shnum == 0.
303 // Also, e_shnum might be 0, when the number of entries in the section
304 // header table is larger than or equal to SHN_LORESERVE (0xff00). In this
305 // case the real number of entries is held in the sh_size member of the
306 // initial entry. We have a section header table when `e_shoff` is not 0.
307 if (Obj.getHeader().e_shoff != 0 && Obj.getHeader().e_shnum == 0)
308 Y->Header.EShNum = 0;
309
310 // Dump symbols. We need to do this early because other sections might want
311 // to access the deduplicated symbol names that we also create here.
312 const Elf_Shdr *SymTab = nullptr;
313 const Elf_Shdr *DynSymTab = nullptr;
314
315 for (const Elf_Shdr &Sec : Sections) {
316 if (Sec.sh_type == ELF::SHT_SYMTAB) {
317 SymTab = &Sec;
318 } else if (Sec.sh_type == ELF::SHT_DYNSYM) {
319 DynSymTab = &Sec;
320 } else if (Sec.sh_type == ELF::SHT_SYMTAB_SHNDX) {
321 // We need to locate SHT_SYMTAB_SHNDX sections early, because they
322 // might be needed for dumping symbols.
323 if (Expected<ArrayRef<Elf_Word>> TableOrErr = Obj.getSHNDXTable(Sec)) {
324 // The `getSHNDXTable` calls the `getSection` internally when validates
325 // the symbol table section linked to the SHT_SYMTAB_SHNDX section.
326 const Elf_Shdr *LinkedSymTab = cantFail(Obj.getSection(Sec.sh_link));
327 if (!ShndxTables.insert({LinkedSymTab, *TableOrErr}).second)
328 return createStringError(
329 EC: errc::invalid_argument,
330 S: "multiple SHT_SYMTAB_SHNDX sections are "
331 "linked to the same symbol table with index " +
332 Twine(Sec.sh_link));
333 } else {
334 return createStringError(errc::invalid_argument,
335 "unable to read extended section indexes: " +
336 toString(TableOrErr.takeError()));
337 }
338 }
339 }
340
341 if (SymTab)
342 if (Error E = dumpSymbols(Symtab: SymTab, Symbols&: Y->Symbols))
343 return std::move(E);
344
345 if (DynSymTab)
346 if (Error E = dumpSymbols(Symtab: DynSymTab, Symbols&: Y->DynamicSymbols))
347 return std::move(E);
348
349 // We dump all sections first. It is simple and allows us to verify that all
350 // sections are valid and also to generalize the code. But we are not going to
351 // keep all of them in the final output (see comments for
352 // 'shouldPrintSection()'). Undesired chunks will be removed later.
353 Expected<std::vector<std::unique_ptr<ELFYAML::Chunk>>> ChunksOrErr =
354 dumpSections();
355 if (!ChunksOrErr)
356 return ChunksOrErr.takeError();
357 std::vector<std::unique_ptr<ELFYAML::Chunk>> Chunks = std::move(*ChunksOrErr);
358
359 std::vector<ELFYAML::Section *> OriginalOrder;
360 if (!Chunks.empty())
361 for (const std::unique_ptr<ELFYAML::Chunk> &C :
362 ArrayRef(Chunks).drop_front())
363 OriginalOrder.push_back(x: cast<ELFYAML::Section>(Val: C.get()));
364
365 // Sometimes the order of sections in the section header table does not match
366 // their actual order. Here we sort sections by the file offset.
367 llvm::stable_sort(Chunks, [&](const std::unique_ptr<ELFYAML::Chunk> &A,
368 const std::unique_ptr<ELFYAML::Chunk> &B) {
369 return Sections[cast<ELFYAML::Section>(Val: A.get())->OriginalSecNdx].sh_offset <
370 Sections[cast<ELFYAML::Section>(Val: B.get())->OriginalSecNdx].sh_offset;
371 });
372
373 // Dump program headers.
374 Expected<std::vector<ELFYAML::ProgramHeader>> PhdrsOrErr =
375 dumpProgramHeaders(Sections: Chunks);
376 if (!PhdrsOrErr)
377 return PhdrsOrErr.takeError();
378 Y->ProgramHeaders = std::move(*PhdrsOrErr);
379
380 dumpSectionOffsets<ELFT>(Obj.getHeader(), Y->ProgramHeaders, Chunks,
381 Sections);
382
383 // Dump DWARF sections.
384 Y->DWARF = dumpDWARFSections(Sections&: Chunks);
385
386 // We emit the "SectionHeaderTable" key when the order of sections in the
387 // sections header table doesn't match the file order.
388 const bool SectionsSorted =
389 llvm::is_sorted(Chunks, [&](const std::unique_ptr<ELFYAML::Chunk> &A,
390 const std::unique_ptr<ELFYAML::Chunk> &B) {
391 return cast<ELFYAML::Section>(Val: A.get())->OriginalSecNdx <
392 cast<ELFYAML::Section>(Val: B.get())->OriginalSecNdx;
393 });
394 if (!SectionsSorted) {
395 std::unique_ptr<ELFYAML::SectionHeaderTable> SHT =
396 std::make_unique<ELFYAML::SectionHeaderTable>(/*IsImplicit=*/args: false);
397 SHT->Sections.emplace();
398 for (ELFYAML::Section *S : OriginalOrder)
399 SHT->Sections->push_back(x: {.Name: S->Name});
400 Chunks.push_back(x: std::move(SHT));
401 }
402
403 llvm::erase_if(Chunks, [this, &Y](const std::unique_ptr<ELFYAML::Chunk> &C) {
404 if (isa<ELFYAML::SectionHeaderTable>(Val: *C))
405 return false;
406
407 const ELFYAML::Section &S = cast<ELFYAML::Section>(Val&: *C);
408 return !shouldPrintSection(S, SHdr: Sections[S.OriginalSecNdx], DWARF: Y->DWARF);
409 });
410
411 // The section header string table by default is assumed to be called
412 // ".shstrtab" and be in its own unique section. However, it's possible for it
413 // to be called something else and shared with another section. If the name
414 // isn't the default, provide this in the YAML.
415 if (ShStrTabIndex && *ShStrTabIndex != ELF::SHN_UNDEF &&
416 *ShStrTabIndex < Sections.size()) {
417 StringRef ShStrtabName;
418 if (SymTab && SymTab->sh_link == *ShStrTabIndex) {
419 // Section header string table is shared with the symbol table. Use that
420 // section's name (usually .strtab).
421 ShStrtabName = cantFail(Obj.getSectionName(Sections[SymTab->sh_link]));
422 } else if (DynSymTab && DynSymTab->sh_link == *ShStrTabIndex) {
423 // Section header string table is shared with the dynamic symbol table.
424 // Use that section's name (usually .dynstr).
425 ShStrtabName = cantFail(Obj.getSectionName(Sections[DynSymTab->sh_link]));
426 } else {
427 // Otherwise, the section name potentially needs uniquifying.
428 ShStrtabName = cantFail(getUniquedSectionName(Sec: Sections[*ShStrTabIndex]));
429 }
430 if (ShStrtabName != ".shstrtab")
431 Y->Header.SectionHeaderStringTable = ShStrtabName;
432 }
433
434 Y->Chunks = std::move(Chunks);
435 return Y.release();
436}
437
438template <class ELFT>
439static bool isInSegment(const ELFYAML::Section &Sec,
440 const typename ELFT::Shdr &SHdr,
441 const typename ELFT::Phdr &Phdr) {
442 if (Sec.Type == ELF::SHT_NULL)
443 return false;
444
445 // A section is within a segment when its location in a file is within the
446 // [p_offset, p_offset + p_filesz] region.
447 bool FileOffsetsMatch =
448 SHdr.sh_offset >= Phdr.p_offset &&
449 (SHdr.sh_offset + SHdr.sh_size <= Phdr.p_offset + Phdr.p_filesz);
450
451 bool VirtualAddressesMatch = SHdr.sh_addr >= Phdr.p_vaddr &&
452 SHdr.sh_addr <= Phdr.p_vaddr + Phdr.p_memsz;
453
454 if (FileOffsetsMatch) {
455 // An empty section on the edges of a program header can be outside of the
456 // virtual address space of the segment. This means it is not included in
457 // the segment and we should ignore it.
458 if (SHdr.sh_size == 0 && (SHdr.sh_offset == Phdr.p_offset ||
459 SHdr.sh_offset == Phdr.p_offset + Phdr.p_filesz))
460 return VirtualAddressesMatch;
461 return true;
462 }
463
464 // SHT_NOBITS sections usually occupy no physical space in a file. Such
465 // sections belong to a segment when they reside in the segment's virtual
466 // address space.
467 if (Sec.Type != ELF::SHT_NOBITS)
468 return false;
469 return VirtualAddressesMatch;
470}
471
472template <class ELFT>
473Expected<std::vector<ELFYAML::ProgramHeader>>
474ELFDumper<ELFT>::dumpProgramHeaders(
475 ArrayRef<std::unique_ptr<ELFYAML::Chunk>> Chunks) {
476 std::vector<ELFYAML::ProgramHeader> Ret;
477 Expected<typename ELFT::PhdrRange> PhdrsOrErr = Obj.program_headers();
478 if (!PhdrsOrErr)
479 return PhdrsOrErr.takeError();
480
481 for (const typename ELFT::Phdr &Phdr : *PhdrsOrErr) {
482 ELFYAML::ProgramHeader PH;
483 PH.Type = Phdr.p_type;
484 PH.Flags = Phdr.p_flags;
485 PH.VAddr = Phdr.p_vaddr;
486 PH.PAddr = Phdr.p_paddr;
487 PH.Offset = Phdr.p_offset;
488
489 // yaml2obj sets the alignment of a segment to 1 by default.
490 // We do not print the default alignment to reduce noise in the output.
491 if (Phdr.p_align != 1)
492 PH.Align = static_cast<llvm::yaml::Hex64>(Phdr.p_align);
493
494 // Here we match sections with segments.
495 // It is not possible to have a non-Section chunk, because
496 // obj2yaml does not create Fill chunks.
497 for (const std::unique_ptr<ELFYAML::Chunk> &C : Chunks) {
498 ELFYAML::Section &S = cast<ELFYAML::Section>(Val&: *C);
499 if (isInSegment<ELFT>(S, Sections[S.OriginalSecNdx], Phdr)) {
500 if (!PH.FirstSec)
501 PH.FirstSec = S.Name;
502 PH.LastSec = S.Name;
503 PH.Chunks.push_back(x: C.get());
504 }
505 }
506
507 Ret.push_back(x: PH);
508 }
509
510 return Ret;
511}
512
513template <class ELFT>
514std::optional<DWARFYAML::Data> ELFDumper<ELFT>::dumpDWARFSections(
515 std::vector<std::unique_ptr<ELFYAML::Chunk>> &Sections) {
516 DWARFYAML::Data DWARF;
517 for (std::unique_ptr<ELFYAML::Chunk> &C : Sections) {
518 if (!C->Name.starts_with(Prefix: ".debug_"))
519 continue;
520
521 if (ELFYAML::RawContentSection *RawSec =
522 dyn_cast<ELFYAML::RawContentSection>(Val: C.get())) {
523 // FIXME: The dumpDebug* functions should take the content as stored in
524 // RawSec. Currently, they just use the last section with the matching
525 // name, which defeats this attempt to skip reading a section header
526 // string table with the same name as a DWARF section.
527 if (ShStrTabIndex && RawSec->OriginalSecNdx == *ShStrTabIndex)
528 continue;
529 Error Err = Error::success();
530 cantFail(Err: std::move(Err));
531
532 if (RawSec->Name == ".debug_aranges")
533 Err = dumpDebugARanges(DCtx&: *DWARFCtx, Y&: DWARF);
534 else if (RawSec->Name == ".debug_str")
535 Err = dumpDebugStrings(DCtx&: *DWARFCtx, Y&: DWARF);
536 else if (RawSec->Name == ".debug_ranges")
537 Err = dumpDebugRanges(DCtx&: *DWARFCtx, Y&: DWARF);
538 else if (RawSec->Name == ".debug_addr")
539 Err = dumpDebugAddr(DCtx&: *DWARFCtx, Y&: DWARF);
540 else
541 continue;
542
543 // If the DWARF section cannot be successfully parsed, emit raw content
544 // instead of an entry in the DWARF section of the YAML.
545 if (Err)
546 consumeError(Err: std::move(Err));
547 else
548 RawSec->Content.reset();
549 }
550 }
551
552 if (DWARF.getNonEmptySectionNames().empty())
553 return std::nullopt;
554 return DWARF;
555}
556
557template <class ELFT>
558Expected<ELFYAML::RawContentSection *>
559ELFDumper<ELFT>::dumpPlaceholderSection(const Elf_Shdr *Shdr) {
560 auto S = std::make_unique<ELFYAML::RawContentSection>();
561 if (Error E = dumpCommonSection(Shdr, S&: *S.get()))
562 return std::move(E);
563
564 // Normally symbol tables should not be empty. We dump the "Size"
565 // key when they are.
566 if ((Shdr->sh_type == ELF::SHT_SYMTAB || Shdr->sh_type == ELF::SHT_DYNSYM) &&
567 !Shdr->sh_size)
568 S->Size.emplace();
569
570 return S.release();
571}
572
573template <class ELFT>
574Expected<std::vector<std::unique_ptr<ELFYAML::Chunk>>>
575ELFDumper<ELFT>::dumpSections() {
576 std::vector<std::unique_ptr<ELFYAML::Chunk>> Ret;
577 auto Add = [&](Expected<ELFYAML::Chunk *> SecOrErr) -> Error {
578 if (!SecOrErr)
579 return SecOrErr.takeError();
580 Ret.emplace_back(args&: *SecOrErr);
581 return Error::success();
582 };
583
584 auto GetDumper = [this](unsigned Type)
585 -> std::function<Expected<ELFYAML::Chunk *>(const Elf_Shdr *)> {
586 if (Obj.getHeader().e_machine == ELF::EM_ARM && Type == ELF::SHT_ARM_EXIDX)
587 return [this](const Elf_Shdr *S) { return dumpARMIndexTableSection(Shdr: S); };
588
589 if (Obj.getHeader().e_machine == ELF::EM_MIPS &&
590 Type == ELF::SHT_MIPS_ABIFLAGS)
591 return [this](const Elf_Shdr *S) { return dumpMipsABIFlags(Shdr: S); };
592
593 switch (Type) {
594 case ELF::SHT_DYNAMIC:
595 return [this](const Elf_Shdr *S) { return dumpDynamicSection(Shdr: S); };
596 case ELF::SHT_SYMTAB_SHNDX:
597 return [this](const Elf_Shdr *S) { return dumpSymtabShndxSection(Shdr: S); };
598 case ELF::SHT_REL:
599 case ELF::SHT_RELA:
600 case ELF::SHT_CREL:
601 return [this](const Elf_Shdr *S) { return dumpRelocSection(Shdr: S); };
602 case ELF::SHT_RELR:
603 return [this](const Elf_Shdr *S) { return dumpRelrSection(Shdr: S); };
604 case ELF::SHT_GROUP:
605 return [this](const Elf_Shdr *S) { return dumpGroupSection(Shdr: S); };
606 case ELF::SHT_NOBITS:
607 return [this](const Elf_Shdr *S) { return dumpNoBitsSection(Shdr: S); };
608 case ELF::SHT_NOTE:
609 return [this](const Elf_Shdr *S) { return dumpNoteSection(Shdr: S); };
610 case ELF::SHT_HASH:
611 return [this](const Elf_Shdr *S) { return dumpHashSection(Shdr: S); };
612 case ELF::SHT_GNU_HASH:
613 return [this](const Elf_Shdr *S) { return dumpGnuHashSection(Shdr: S); };
614 case ELF::SHT_GNU_verdef:
615 return [this](const Elf_Shdr *S) { return dumpVerdefSection(Shdr: S); };
616 case ELF::SHT_GNU_versym:
617 return [this](const Elf_Shdr *S) { return dumpSymverSection(Shdr: S); };
618 case ELF::SHT_GNU_verneed:
619 return [this](const Elf_Shdr *S) { return dumpVerneedSection(Shdr: S); };
620 case ELF::SHT_LLVM_ADDRSIG:
621 return [this](const Elf_Shdr *S) { return dumpAddrsigSection(Shdr: S); };
622 case ELF::SHT_LLVM_LINKER_OPTIONS:
623 return [this](const Elf_Shdr *S) { return dumpLinkerOptionsSection(Shdr: S); };
624 case ELF::SHT_LLVM_DEPENDENT_LIBRARIES:
625 return [this](const Elf_Shdr *S) {
626 return dumpDependentLibrariesSection(Shdr: S);
627 };
628 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
629 return
630 [this](const Elf_Shdr *S) { return dumpCallGraphProfileSection(Shdr: S); };
631 case ELF::SHT_LLVM_BB_ADDR_MAP:
632 return [this](const Elf_Shdr *S) { return dumpBBAddrMapSection(Shdr: S); };
633 case ELF::SHT_STRTAB:
634 case ELF::SHT_SYMTAB:
635 case ELF::SHT_DYNSYM:
636 // The contents of these sections are described by other parts of the YAML
637 // file. But we still want to dump them, because their properties can be
638 // important. See comments for 'shouldPrintSection()' for more details.
639 return [this](const Elf_Shdr *S) { return dumpPlaceholderSection(Shdr: S); };
640 default:
641 return nullptr;
642 }
643 };
644
645 for (const Elf_Shdr &Sec : Sections) {
646 // We have dedicated dumping functions for most of the section types.
647 // Try to use one of them first.
648 if (std::function<Expected<ELFYAML::Chunk *>(const Elf_Shdr *)> DumpFn =
649 GetDumper(Sec.sh_type)) {
650 if (Error E = Add(DumpFn(&Sec)))
651 return std::move(E);
652 continue;
653 }
654
655 // Recognize some special SHT_PROGBITS sections by name.
656 if (Sec.sh_type == ELF::SHT_PROGBITS) {
657 auto NameOrErr = Obj.getSectionName(Sec);
658 if (!NameOrErr)
659 return NameOrErr.takeError();
660
661 if (ELFYAML::StackSizesSection::nameMatches(Name: *NameOrErr)) {
662 if (Error E = Add(dumpStackSizesSection(Shdr: &Sec)))
663 return std::move(E);
664 continue;
665 }
666 }
667
668 if (Error E = Add(dumpContentSection(Shdr: &Sec)))
669 return std::move(E);
670 }
671
672 return std::move(Ret);
673}
674
675template <class ELFT>
676Error ELFDumper<ELFT>::dumpSymbols(
677 const Elf_Shdr *Symtab,
678 std::optional<std::vector<ELFYAML::Symbol>> &Symbols) {
679 if (!Symtab)
680 return Error::success();
681
682 auto SymtabOrErr = Obj.symbols(Symtab);
683 if (!SymtabOrErr)
684 return SymtabOrErr.takeError();
685
686 if (SymtabOrErr->empty())
687 return Error::success();
688
689 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
690 if (!StrTableOrErr)
691 return StrTableOrErr.takeError();
692
693 if (Symtab->sh_type == ELF::SHT_SYMTAB) {
694 SymTable = *SymtabOrErr;
695 SymbolNames.resize(SymTable.size());
696 }
697
698 Symbols.emplace();
699 for (const auto &Sym : (*SymtabOrErr).drop_front()) {
700 ELFYAML::Symbol S;
701 if (auto EC = dumpSymbol(Sym: &Sym, SymTab: Symtab, StrTable: *StrTableOrErr, S))
702 return EC;
703 Symbols->push_back(x: S);
704 }
705
706 return Error::success();
707}
708
709template <class ELFT>
710Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
711 StringRef StrTable, ELFYAML::Symbol &S) {
712 S.Type = Sym->getType();
713 if (Sym->st_value)
714 S.Value = (yaml::Hex64)Sym->st_value;
715 if (Sym->st_size)
716 S.Size = (yaml::Hex64)Sym->st_size;
717 S.Other = Sym->st_other;
718 S.Binding = Sym->getBinding();
719
720 Expected<StringRef> SymbolNameOrErr =
721 getUniquedSymbolName(Sym, StrTable, SymTab);
722 if (!SymbolNameOrErr)
723 return SymbolNameOrErr.takeError();
724 S.Name = SymbolNameOrErr.get();
725
726 if (Sym->st_shndx >= ELF::SHN_LORESERVE) {
727 S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx;
728 return Error::success();
729 }
730
731 auto ShdrOrErr = Obj.getSection(*Sym, SymTab, ShndxTables.lookup(SymTab));
732 if (!ShdrOrErr)
733 return ShdrOrErr.takeError();
734 const Elf_Shdr *Shdr = *ShdrOrErr;
735 if (!Shdr)
736 return Error::success();
737
738 auto NameOrErr = getUniquedSectionName(Sec: *Shdr);
739 if (!NameOrErr)
740 return NameOrErr.takeError();
741 S.Section = NameOrErr.get();
742
743 return Error::success();
744}
745
746template <class ELFT>
747template <class RelT>
748Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
749 ELFYAML::Relocation &R) {
750 R.Type = Rel->getType(Obj.isMips64EL());
751 R.Offset = Rel->r_offset;
752 R.Addend = 0;
753
754 auto SymOrErr = Obj.getRelocationSymbol(*Rel, SymTab);
755 if (!SymOrErr)
756 return SymOrErr.takeError();
757
758 // We have might have a relocation with symbol index 0,
759 // e.g. R_X86_64_NONE or R_X86_64_GOTPC32.
760 const Elf_Sym *Sym = *SymOrErr;
761 if (!Sym)
762 return Error::success();
763
764 auto StrTabSec = Obj.getSection(SymTab->sh_link);
765 if (!StrTabSec)
766 return StrTabSec.takeError();
767 auto StrTabOrErr = Obj.getStringTable(**StrTabSec);
768 if (!StrTabOrErr)
769 return StrTabOrErr.takeError();
770
771 Expected<StringRef> NameOrErr =
772 getUniquedSymbolName(Sym, StrTable: *StrTabOrErr, SymTab);
773 if (!NameOrErr)
774 return NameOrErr.takeError();
775 R.Symbol = NameOrErr.get();
776
777 return Error::success();
778}
779
780template <class ELFT>
781Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
782 ELFYAML::Section &S) {
783 // Dump fields. We do not dump the ShOffset field. When not explicitly
784 // set, the value is set by yaml2obj automatically.
785 S.Type = Shdr->sh_type;
786 if (Shdr->sh_flags)
787 S.Flags = static_cast<ELFYAML::ELF_SHF>(Shdr->sh_flags);
788 if (Shdr->sh_addr)
789 S.Address = static_cast<uint64_t>(Shdr->sh_addr);
790 S.AddressAlign = Shdr->sh_addralign;
791
792 S.OriginalSecNdx = Shdr - &Sections[0];
793
794 Expected<StringRef> NameOrErr = getUniquedSectionName(Sec: *Shdr);
795 if (!NameOrErr)
796 return NameOrErr.takeError();
797 S.Name = NameOrErr.get();
798
799 if (Shdr->sh_entsize != ELFYAML::getDefaultShEntSize<ELFT>(
800 Obj.getHeader().e_machine, S.Type, S.Name))
801 S.EntSize = static_cast<llvm::yaml::Hex64>(Shdr->sh_entsize);
802
803 if (Shdr->sh_link != ELF::SHN_UNDEF) {
804 Expected<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
805 if (!LinkSection)
806 return make_error<StringError>(
807 "unable to resolve sh_link reference in section '" + S.Name +
808 "': " + toString(LinkSection.takeError()),
809 inconvertibleErrorCode());
810
811 NameOrErr = getUniquedSectionName(Sec: **LinkSection);
812 if (!NameOrErr)
813 return NameOrErr.takeError();
814 S.Link = NameOrErr.get();
815 }
816
817 return Error::success();
818}
819
820template <class ELFT>
821Error ELFDumper<ELFT>::dumpCommonRelocationSection(
822 const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S) {
823 if (Error E = dumpCommonSection(Shdr, S))
824 return E;
825
826 // Having a zero sh_info field is normal: .rela.dyn is a dynamic
827 // relocation section that normally has no value in this field.
828 if (!Shdr->sh_info)
829 return Error::success();
830
831 auto InfoSection = Obj.getSection(Shdr->sh_info);
832 if (!InfoSection)
833 return InfoSection.takeError();
834
835 Expected<StringRef> NameOrErr = getUniquedSectionName(Sec: **InfoSection);
836 if (!NameOrErr)
837 return NameOrErr.takeError();
838 S.RelocatableSec = NameOrErr.get();
839
840 return Error::success();
841}
842
843template <class ELFT>
844Expected<ELFYAML::StackSizesSection *>
845ELFDumper<ELFT>::dumpStackSizesSection(const Elf_Shdr *Shdr) {
846 auto S = std::make_unique<ELFYAML::StackSizesSection>();
847 if (Error E = dumpCommonSection(Shdr, S&: *S))
848 return std::move(E);
849
850 auto ContentOrErr = Obj.getSectionContents(*Shdr);
851 if (!ContentOrErr)
852 return ContentOrErr.takeError();
853
854 ArrayRef<uint8_t> Content = *ContentOrErr;
855 unsigned AddressSize = ELFT::Is64Bits ? 8 : 4;
856 DataExtractor Data(Content, Obj.isLE());
857
858 std::vector<ELFYAML::StackSizeEntry> Entries;
859 DataExtractor::Cursor Cur(0);
860 while (Cur && Cur.tell() < Content.size()) {
861 uint64_t Address = Data.getUnsigned(C&: Cur, Size: AddressSize);
862 uint64_t Size = Data.getULEB128(C&: Cur);
863 Entries.push_back(x: {.Address: Address, .Size: Size});
864 }
865
866 if (Content.empty() || !Cur) {
867 // If .stack_sizes cannot be decoded, we dump it as an array of bytes.
868 consumeError(Err: Cur.takeError());
869 S->Content = yaml::BinaryRef(Content);
870 } else {
871 S->Entries = std::move(Entries);
872 }
873
874 return S.release();
875}
876
877template <class ELFT>
878Expected<ELFYAML::BBAddrMapSection *>
879ELFDumper<ELFT>::dumpBBAddrMapSection(const Elf_Shdr *Shdr) {
880 auto S = std::make_unique<ELFYAML::BBAddrMapSection>();
881 if (Error E = dumpCommonSection(Shdr, S&: *S))
882 return std::move(E);
883
884 auto ContentOrErr = Obj.getSectionContents(*Shdr);
885 if (!ContentOrErr)
886 return ContentOrErr.takeError();
887
888 ArrayRef<uint8_t> Content = *ContentOrErr;
889 if (Content.empty())
890 return S.release();
891
892 if (Error E = BBAddrMapYAML::decodePayload(Content, Endian: ELFT::Endianness,
893 AddressSize: ELFT::Is64Bits ? 8 : 4, Entries&: S->Entries,
894 PGOAnalyses&: S->PGOAnalyses))
895 return std::move(E);
896
897 // If the section cannot be decoded, we dump it as an array of bytes.
898 if (!S->Entries)
899 S->Content = yaml::BinaryRef(Content);
900
901 return S.release();
902}
903
904template <class ELFT>
905Expected<ELFYAML::AddrsigSection *>
906ELFDumper<ELFT>::dumpAddrsigSection(const Elf_Shdr *Shdr) {
907 auto S = std::make_unique<ELFYAML::AddrsigSection>();
908 if (Error E = dumpCommonSection(Shdr, S&: *S))
909 return std::move(E);
910
911 auto ContentOrErr = Obj.getSectionContents(*Shdr);
912 if (!ContentOrErr)
913 return ContentOrErr.takeError();
914
915 ArrayRef<uint8_t> Content = *ContentOrErr;
916 DataExtractor::Cursor Cur(0);
917 DataExtractor Data(Content, Obj.isLE());
918 std::vector<ELFYAML::YAMLFlowString> Symbols;
919 while (Cur && Cur.tell() < Content.size()) {
920 uint64_t SymNdx = Data.getULEB128(C&: Cur);
921 if (!Cur)
922 break;
923
924 Expected<StringRef> SymbolName = getSymbolName(SymtabNdx: Shdr->sh_link, SymbolNdx: SymNdx);
925 if (!SymbolName || SymbolName->empty()) {
926 consumeError(Err: SymbolName.takeError());
927 Symbols.emplace_back(
928 args: StringRef(std::to_string(val: SymNdx)).copy(A&: StringAllocator));
929 continue;
930 }
931
932 Symbols.emplace_back(args&: *SymbolName);
933 }
934
935 if (Cur) {
936 S->Symbols = std::move(Symbols);
937 return S.release();
938 }
939
940 consumeError(Err: Cur.takeError());
941 S->Content = yaml::BinaryRef(Content);
942 return S.release();
943}
944
945template <class ELFT>
946Expected<ELFYAML::LinkerOptionsSection *>
947ELFDumper<ELFT>::dumpLinkerOptionsSection(const Elf_Shdr *Shdr) {
948 auto S = std::make_unique<ELFYAML::LinkerOptionsSection>();
949 if (Error E = dumpCommonSection(Shdr, S&: *S))
950 return std::move(E);
951
952 auto ContentOrErr = Obj.getSectionContents(*Shdr);
953 if (!ContentOrErr)
954 return ContentOrErr.takeError();
955
956 ArrayRef<uint8_t> Content = *ContentOrErr;
957 if (Content.empty() || Content.back() != 0) {
958 S->Content = Content;
959 return S.release();
960 }
961
962 SmallVector<StringRef, 16> Strings;
963 toStringRef(Input: Content.drop_back()).split(A&: Strings, Separator: '\0');
964 if (Strings.size() % 2 != 0) {
965 S->Content = Content;
966 return S.release();
967 }
968
969 S->Options.emplace();
970 for (size_t I = 0, E = Strings.size(); I != E; I += 2)
971 S->Options->push_back(x: {.Key: Strings[I], .Value: Strings[I + 1]});
972
973 return S.release();
974}
975
976template <class ELFT>
977Expected<ELFYAML::DependentLibrariesSection *>
978ELFDumper<ELFT>::dumpDependentLibrariesSection(const Elf_Shdr *Shdr) {
979 auto DL = std::make_unique<ELFYAML::DependentLibrariesSection>();
980 if (Error E = dumpCommonSection(Shdr, S&: *DL))
981 return std::move(E);
982
983 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
984 if (!ContentOrErr)
985 return ContentOrErr.takeError();
986
987 ArrayRef<uint8_t> Content = *ContentOrErr;
988 if (!Content.empty() && Content.back() != 0) {
989 DL->Content = Content;
990 return DL.release();
991 }
992
993 DL->Libs.emplace();
994 for (const uint8_t *I = Content.begin(), *E = Content.end(); I < E;) {
995 StringRef Lib((const char *)I);
996 DL->Libs->emplace_back(args&: Lib);
997 I += Lib.size() + 1;
998 }
999
1000 return DL.release();
1001}
1002
1003template <class ELFT>
1004Expected<ELFYAML::CallGraphProfileSection *>
1005ELFDumper<ELFT>::dumpCallGraphProfileSection(const Elf_Shdr *Shdr) {
1006 auto S = std::make_unique<ELFYAML::CallGraphProfileSection>();
1007 if (Error E = dumpCommonSection(Shdr, S&: *S))
1008 return std::move(E);
1009
1010 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
1011 if (!ContentOrErr)
1012 return ContentOrErr.takeError();
1013 ArrayRef<uint8_t> Content = *ContentOrErr;
1014 const uint32_t SizeOfEntry = ELFYAML::getDefaultShEntSize<ELFT>(
1015 Obj.getHeader().e_machine, S->Type, S->Name);
1016 // Dump the section by using the Content key when it is truncated.
1017 // There is no need to create either "Content" or "Entries" fields when the
1018 // section is empty.
1019 if (Content.empty() || Content.size() % SizeOfEntry != 0) {
1020 if (!Content.empty())
1021 S->Content = yaml::BinaryRef(Content);
1022 return S.release();
1023 }
1024
1025 std::vector<ELFYAML::CallGraphEntryWeight> Entries(Content.size() /
1026 SizeOfEntry);
1027 DataExtractor Data(Content, Obj.isLE());
1028 DataExtractor::Cursor Cur(0);
1029 auto ReadEntry = [&](ELFYAML::CallGraphEntryWeight &E) {
1030 E.Weight = Data.getU64(C&: Cur);
1031 if (!Cur) {
1032 consumeError(Err: Cur.takeError());
1033 return false;
1034 }
1035 return true;
1036 };
1037
1038 for (ELFYAML::CallGraphEntryWeight &E : Entries) {
1039 if (ReadEntry(E))
1040 continue;
1041 S->Content = yaml::BinaryRef(Content);
1042 return S.release();
1043 }
1044
1045 S->Entries = std::move(Entries);
1046 return S.release();
1047}
1048
1049template <class ELFT>
1050Expected<ELFYAML::DynamicSection *>
1051ELFDumper<ELFT>::dumpDynamicSection(const Elf_Shdr *Shdr) {
1052 auto S = std::make_unique<ELFYAML::DynamicSection>();
1053 if (Error E = dumpCommonSection(Shdr, S&: *S))
1054 return std::move(E);
1055
1056 auto DynTagsOrErr = Obj.template getSectionContentsAsArray<Elf_Dyn>(*Shdr);
1057 if (!DynTagsOrErr)
1058 return DynTagsOrErr.takeError();
1059
1060 S->Entries.emplace();
1061 for (const Elf_Dyn &Dyn : *DynTagsOrErr)
1062 S->Entries->push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()});
1063
1064 return S.release();
1065}
1066
1067template <class ELFT>
1068Expected<ELFYAML::RelocationSection *>
1069ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) {
1070 auto S = std::make_unique<ELFYAML::RelocationSection>();
1071 if (auto E = dumpCommonRelocationSection(Shdr, S&: *S))
1072 return std::move(E);
1073
1074 auto SymTabOrErr = Obj.getSection(Shdr->sh_link);
1075 if (!SymTabOrErr)
1076 return SymTabOrErr.takeError();
1077
1078 if (Shdr->sh_size != 0)
1079 S->Relocations.emplace();
1080
1081 std::vector<Elf_Rel> Rels;
1082 std::vector<Elf_Rela> Relas;
1083 if (Shdr->sh_type == ELF::SHT_CREL) {
1084 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
1085 if (!ContentOrErr)
1086 return ContentOrErr.takeError();
1087 auto Crel = Obj.decodeCrel(*ContentOrErr);
1088 if (!Crel)
1089 return Crel.takeError();
1090 Rels = std::move(Crel->first);
1091 Relas = std::move(Crel->second);
1092 } else if (Shdr->sh_type == ELF::SHT_REL) {
1093 auto R = Obj.rels(*Shdr);
1094 if (!R)
1095 return R.takeError();
1096 Rels = std::move(*R);
1097 } else {
1098 auto R = Obj.relas(*Shdr);
1099 if (!R)
1100 return R.takeError();
1101 Relas = std::move(*R);
1102 }
1103
1104 for (const Elf_Rel &Rel : Rels) {
1105 ELFYAML::Relocation R;
1106 if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
1107 return std::move(E);
1108 S->Relocations->push_back(x: R);
1109 }
1110 for (const Elf_Rela &Rel : Relas) {
1111 ELFYAML::Relocation R;
1112 if (Error E = dumpRelocation(&Rel, *SymTabOrErr, R))
1113 return std::move(E);
1114 R.Addend = Rel.r_addend;
1115 S->Relocations->push_back(x: R);
1116 }
1117
1118 return S.release();
1119}
1120
1121template <class ELFT>
1122Expected<ELFYAML::RelrSection *>
1123ELFDumper<ELFT>::dumpRelrSection(const Elf_Shdr *Shdr) {
1124 auto S = std::make_unique<ELFYAML::RelrSection>();
1125 if (auto E = dumpCommonSection(Shdr, S&: *S))
1126 return std::move(E);
1127
1128 if (Expected<ArrayRef<Elf_Relr>> Relrs = Obj.relrs(*Shdr)) {
1129 S->Entries.emplace();
1130 for (Elf_Relr Rel : *Relrs)
1131 S->Entries->emplace_back(Rel);
1132 return S.release();
1133 } else {
1134 // Ignore. We are going to dump the data as raw content below.
1135 consumeError(Relrs.takeError());
1136 }
1137
1138 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
1139 if (!ContentOrErr)
1140 return ContentOrErr.takeError();
1141 S->Content = *ContentOrErr;
1142 return S.release();
1143}
1144
1145template <class ELFT>
1146Expected<ELFYAML::RawContentSection *>
1147ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
1148 auto S = std::make_unique<ELFYAML::RawContentSection>();
1149 if (Error E = dumpCommonSection(Shdr, S&: *S))
1150 return std::move(E);
1151
1152 unsigned SecIndex = Shdr - &Sections[0];
1153 if (SecIndex != 0 || Shdr->sh_type != ELF::SHT_NULL) {
1154 auto ContentOrErr = Obj.getSectionContents(*Shdr);
1155 if (!ContentOrErr)
1156 return ContentOrErr.takeError();
1157 ArrayRef<uint8_t> Content = *ContentOrErr;
1158 if (!Content.empty())
1159 S->Content = yaml::BinaryRef(Content);
1160 } else {
1161 S->Size = static_cast<llvm::yaml::Hex64>(Shdr->sh_size);
1162 }
1163
1164 if (Shdr->sh_info)
1165 S->Info = static_cast<llvm::yaml::Hex64>(Shdr->sh_info);
1166 return S.release();
1167}
1168
1169template <class ELFT>
1170Expected<ELFYAML::SymtabShndxSection *>
1171ELFDumper<ELFT>::dumpSymtabShndxSection(const Elf_Shdr *Shdr) {
1172 auto S = std::make_unique<ELFYAML::SymtabShndxSection>();
1173 if (Error E = dumpCommonSection(Shdr, S&: *S))
1174 return std::move(E);
1175
1176 auto EntriesOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(*Shdr);
1177 if (!EntriesOrErr)
1178 return EntriesOrErr.takeError();
1179
1180 S->Entries.emplace();
1181 llvm::append_range(*S->Entries, *EntriesOrErr);
1182 return S.release();
1183}
1184
1185template <class ELFT>
1186Expected<ELFYAML::NoBitsSection *>
1187ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
1188 auto S = std::make_unique<ELFYAML::NoBitsSection>();
1189 if (Error E = dumpCommonSection(Shdr, S&: *S))
1190 return std::move(E);
1191 if (Shdr->sh_size)
1192 S->Size = static_cast<llvm::yaml::Hex64>(Shdr->sh_size);
1193 return S.release();
1194}
1195
1196template <class ELFT>
1197Expected<ELFYAML::NoteSection *>
1198ELFDumper<ELFT>::dumpNoteSection(const Elf_Shdr *Shdr) {
1199 auto S = std::make_unique<ELFYAML::NoteSection>();
1200 if (Error E = dumpCommonSection(Shdr, S&: *S))
1201 return std::move(E);
1202
1203 auto ContentOrErr = Obj.getSectionContents(*Shdr);
1204 if (!ContentOrErr)
1205 return ContentOrErr.takeError();
1206
1207 std::vector<ELFYAML::NoteEntry> Entries;
1208 ArrayRef<uint8_t> Content = *ContentOrErr;
1209 size_t Align = std::max<size_t>(Shdr->sh_addralign, 4);
1210 while (!Content.empty()) {
1211 if (Content.size() < sizeof(Elf_Nhdr)) {
1212 S->Content = yaml::BinaryRef(*ContentOrErr);
1213 return S.release();
1214 }
1215
1216 const Elf_Nhdr *Header = reinterpret_cast<const Elf_Nhdr *>(Content.data());
1217 if (Content.size() < Header->getSize(Align)) {
1218 S->Content = yaml::BinaryRef(*ContentOrErr);
1219 return S.release();
1220 }
1221
1222 Elf_Note Note(*Header);
1223 Entries.push_back(
1224 {Note.getName(), Note.getDesc(Align), (ELFYAML::ELF_NT)Note.getType()});
1225
1226 Content = Content.drop_front(N: Header->getSize(Align));
1227 }
1228
1229 S->Notes = std::move(Entries);
1230 return S.release();
1231}
1232
1233template <class ELFT>
1234Expected<ELFYAML::HashSection *>
1235ELFDumper<ELFT>::dumpHashSection(const Elf_Shdr *Shdr) {
1236 auto S = std::make_unique<ELFYAML::HashSection>();
1237 if (Error E = dumpCommonSection(Shdr, S&: *S))
1238 return std::move(E);
1239
1240 auto ContentOrErr = Obj.getSectionContents(*Shdr);
1241 if (!ContentOrErr)
1242 return ContentOrErr.takeError();
1243
1244 ArrayRef<uint8_t> Content = *ContentOrErr;
1245 if (Content.size() % 4 != 0 || Content.size() < 8) {
1246 S->Content = yaml::BinaryRef(Content);
1247 return S.release();
1248 }
1249
1250 DataExtractor::Cursor Cur(0);
1251 DataExtractor Data(Content, Obj.isLE());
1252 uint64_t NBucket = Data.getU32(C&: Cur);
1253 uint64_t NChain = Data.getU32(C&: Cur);
1254 if (Content.size() != (2 + NBucket + NChain) * 4) {
1255 S->Content = yaml::BinaryRef(Content);
1256 if (Cur)
1257 return S.release();
1258 llvm_unreachable("entries were not read correctly");
1259 }
1260
1261 S->Bucket.emplace(args&: NBucket);
1262 for (uint32_t &V : *S->Bucket)
1263 V = Data.getU32(C&: Cur);
1264
1265 S->Chain.emplace(args&: NChain);
1266 for (uint32_t &V : *S->Chain)
1267 V = Data.getU32(C&: Cur);
1268
1269 if (Cur)
1270 return S.release();
1271 llvm_unreachable("entries were not read correctly");
1272}
1273
1274template <class ELFT>
1275Expected<ELFYAML::GnuHashSection *>
1276ELFDumper<ELFT>::dumpGnuHashSection(const Elf_Shdr *Shdr) {
1277 auto S = std::make_unique<ELFYAML::GnuHashSection>();
1278 if (Error E = dumpCommonSection(Shdr, S&: *S))
1279 return std::move(E);
1280
1281 auto ContentOrErr = Obj.getSectionContents(*Shdr);
1282 if (!ContentOrErr)
1283 return ContentOrErr.takeError();
1284
1285 unsigned AddrSize = ELFT::Is64Bits ? 8 : 4;
1286 ArrayRef<uint8_t> Content = *ContentOrErr;
1287 DataExtractor Data(Content, Obj.isLE());
1288
1289 ELFYAML::GnuHashHeader Header;
1290 DataExtractor::Cursor Cur(0);
1291 uint64_t NBuckets = Data.getU32(C&: Cur);
1292 Header.SymNdx = Data.getU32(C&: Cur);
1293 uint64_t MaskWords = Data.getU32(C&: Cur);
1294 Header.Shift2 = Data.getU32(C&: Cur);
1295
1296 // Set just the raw binary content if we were unable to read the header
1297 // or when the section data is truncated or malformed.
1298 uint64_t Size = Data.getData().size() - Cur.tell();
1299 if (!Cur || (Size < MaskWords * AddrSize + NBuckets * 4) ||
1300 (Size % 4 != 0)) {
1301 consumeError(Err: Cur.takeError());
1302 S->Content = yaml::BinaryRef(Content);
1303 return S.release();
1304 }
1305
1306 S->Header = Header;
1307
1308 S->BloomFilter.emplace(args&: MaskWords);
1309 for (llvm::yaml::Hex64 &Val : *S->BloomFilter)
1310 Val = Data.getUnsigned(C&: Cur, Size: AddrSize);
1311
1312 S->HashBuckets.emplace(args&: NBuckets);
1313 for (llvm::yaml::Hex32 &Val : *S->HashBuckets)
1314 Val = Data.getU32(C&: Cur);
1315
1316 S->HashValues.emplace(args: (Data.getData().size() - Cur.tell()) / 4);
1317 for (llvm::yaml::Hex32 &Val : *S->HashValues)
1318 Val = Data.getU32(C&: Cur);
1319
1320 if (Cur)
1321 return S.release();
1322 llvm_unreachable("GnuHashSection was not read correctly");
1323}
1324
1325template <class ELFT>
1326Expected<ELFYAML::VerdefSection *>
1327ELFDumper<ELFT>::dumpVerdefSection(const Elf_Shdr *Shdr) {
1328 auto S = std::make_unique<ELFYAML::VerdefSection>();
1329 if (Error E = dumpCommonSection(Shdr, S&: *S))
1330 return std::move(E);
1331
1332 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
1333 if (!StringTableShdrOrErr)
1334 return StringTableShdrOrErr.takeError();
1335
1336 auto StringTableOrErr = Obj.getStringTable(**StringTableShdrOrErr);
1337 if (!StringTableOrErr)
1338 return StringTableOrErr.takeError();
1339
1340 auto Contents = Obj.getSectionContents(*Shdr);
1341 if (!Contents)
1342 return Contents.takeError();
1343
1344 S->Entries.emplace();
1345
1346 llvm::ArrayRef<uint8_t> Data = *Contents;
1347 const uint8_t *Buf = Data.data();
1348 while (Buf) {
1349 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(Buf);
1350 ELFYAML::VerdefEntry Entry;
1351 if (Verdef->vd_version != 1)
1352 return createStringError(EC: errc::invalid_argument,
1353 S: "invalid SHT_GNU_verdef section version: " +
1354 Twine(Verdef->vd_version));
1355
1356 if (Verdef->vd_flags != 0)
1357 Entry.Flags = Verdef->vd_flags;
1358
1359 if (Verdef->vd_ndx != 0)
1360 Entry.VersionNdx = Verdef->vd_ndx;
1361
1362 if (Verdef->vd_hash != 0)
1363 Entry.Hash = Verdef->vd_hash;
1364
1365 if (Verdef->vd_aux != sizeof(Elf_Verdef))
1366 Entry.VDAux = Verdef->vd_aux;
1367
1368 const uint8_t *BufAux = Buf + Verdef->vd_aux;
1369 if (BufAux > Data.end())
1370 return createStringError(
1371 EC: errc::invalid_argument,
1372 S: "corrupted section: vd_aux value " + Twine(Verdef->vd_aux) +
1373 " in section verdef points past end of the section");
1374 while (BufAux) {
1375 const Elf_Verdaux *Verdaux =
1376 reinterpret_cast<const Elf_Verdaux *>(BufAux);
1377 Entry.VerNames.push_back(
1378 StringTableOrErr->drop_front(Verdaux->vda_name).data());
1379 BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
1380 }
1381
1382 S->Entries->push_back(x: Entry);
1383 Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
1384 }
1385
1386 if (Shdr->sh_info != S->Entries->size())
1387 S->Info = (llvm::yaml::Hex64)Shdr->sh_info;
1388
1389 return S.release();
1390}
1391
1392template <class ELFT>
1393Expected<ELFYAML::SymverSection *>
1394ELFDumper<ELFT>::dumpSymverSection(const Elf_Shdr *Shdr) {
1395 auto S = std::make_unique<ELFYAML::SymverSection>();
1396 if (Error E = dumpCommonSection(Shdr, S&: *S))
1397 return std::move(E);
1398
1399 auto VersionsOrErr = Obj.template getSectionContentsAsArray<Elf_Half>(*Shdr);
1400 if (!VersionsOrErr)
1401 return VersionsOrErr.takeError();
1402
1403 S->Entries.emplace();
1404 llvm::append_range(*S->Entries, *VersionsOrErr);
1405
1406 return S.release();
1407}
1408
1409template <class ELFT>
1410Expected<ELFYAML::VerneedSection *>
1411ELFDumper<ELFT>::dumpVerneedSection(const Elf_Shdr *Shdr) {
1412 auto S = std::make_unique<ELFYAML::VerneedSection>();
1413 if (Error E = dumpCommonSection(Shdr, S&: *S))
1414 return std::move(E);
1415
1416 auto Contents = Obj.getSectionContents(*Shdr);
1417 if (!Contents)
1418 return Contents.takeError();
1419
1420 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
1421 if (!StringTableShdrOrErr)
1422 return StringTableShdrOrErr.takeError();
1423
1424 auto StringTableOrErr = Obj.getStringTable(**StringTableShdrOrErr);
1425 if (!StringTableOrErr)
1426 return StringTableOrErr.takeError();
1427
1428 S->VerneedV.emplace();
1429
1430 llvm::ArrayRef<uint8_t> Data = *Contents;
1431 const uint8_t *Buf = Data.data();
1432 while (Buf) {
1433 const Elf_Verneed *Verneed = reinterpret_cast<const Elf_Verneed *>(Buf);
1434
1435 ELFYAML::VerneedEntry Entry;
1436 Entry.Version = Verneed->vn_version;
1437 Entry.File =
1438 StringRef(StringTableOrErr->drop_front(Verneed->vn_file).data());
1439
1440 const uint8_t *BufAux = Buf + Verneed->vn_aux;
1441 while (BufAux) {
1442 const Elf_Vernaux *Vernaux =
1443 reinterpret_cast<const Elf_Vernaux *>(BufAux);
1444
1445 ELFYAML::VernauxEntry Aux;
1446 Aux.Hash = Vernaux->vna_hash;
1447 Aux.Flags = Vernaux->vna_flags;
1448 Aux.Other = Vernaux->vna_other;
1449 Aux.Name =
1450 StringRef(StringTableOrErr->drop_front(Vernaux->vna_name).data());
1451
1452 Entry.AuxV.push_back(x: Aux);
1453 BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr;
1454 }
1455
1456 S->VerneedV->push_back(x: Entry);
1457 Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr;
1458 }
1459
1460 if (Shdr->sh_info != S->VerneedV->size())
1461 S->Info = (llvm::yaml::Hex64)Shdr->sh_info;
1462
1463 return S.release();
1464}
1465
1466template <class ELFT>
1467Expected<StringRef> ELFDumper<ELFT>::getSymbolName(uint32_t SymtabNdx,
1468 uint32_t SymbolNdx) {
1469 auto SymtabOrErr = Obj.getSection(SymtabNdx);
1470 if (!SymtabOrErr)
1471 return SymtabOrErr.takeError();
1472
1473 const Elf_Shdr *Symtab = *SymtabOrErr;
1474 auto SymOrErr = Obj.getSymbol(Symtab, SymbolNdx);
1475 if (!SymOrErr)
1476 return SymOrErr.takeError();
1477
1478 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
1479 if (!StrTabOrErr)
1480 return StrTabOrErr.takeError();
1481 return getUniquedSymbolName(Sym: *SymOrErr, StrTable: *StrTabOrErr, SymTab: Symtab);
1482}
1483
1484template <class ELFT>
1485Expected<ELFYAML::GroupSection *>
1486ELFDumper<ELFT>::dumpGroupSection(const Elf_Shdr *Shdr) {
1487 auto S = std::make_unique<ELFYAML::GroupSection>();
1488 if (Error E = dumpCommonSection(Shdr, S&: *S))
1489 return std::move(E);
1490
1491 // Get symbol with index sh_info. This symbol's name is the signature of the group.
1492 Expected<StringRef> SymbolName = getSymbolName(SymtabNdx: Shdr->sh_link, SymbolNdx: Shdr->sh_info);
1493 if (!SymbolName)
1494 return SymbolName.takeError();
1495 S->Signature = *SymbolName;
1496
1497 auto MembersOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(*Shdr);
1498 if (!MembersOrErr)
1499 return MembersOrErr.takeError();
1500
1501 S->Members.emplace();
1502 for (Elf_Word Member : *MembersOrErr) {
1503 if (Member == llvm::ELF::GRP_COMDAT) {
1504 S->Members->push_back(x: {.sectionNameOrType: "GRP_COMDAT"});
1505 continue;
1506 }
1507
1508 Expected<const Elf_Shdr *> SHdrOrErr = Obj.getSection(Member);
1509 if (!SHdrOrErr)
1510 return SHdrOrErr.takeError();
1511 Expected<StringRef> NameOrErr = getUniquedSectionName(Sec: **SHdrOrErr);
1512 if (!NameOrErr)
1513 return NameOrErr.takeError();
1514 S->Members->push_back(x: {.sectionNameOrType: *NameOrErr});
1515 }
1516 return S.release();
1517}
1518
1519template <class ELFT>
1520Expected<ELFYAML::ARMIndexTableSection *>
1521ELFDumper<ELFT>::dumpARMIndexTableSection(const Elf_Shdr *Shdr) {
1522 auto S = std::make_unique<ELFYAML::ARMIndexTableSection>();
1523 if (Error E = dumpCommonSection(Shdr, S&: *S))
1524 return std::move(E);
1525
1526 Expected<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(*Shdr);
1527 if (!ContentOrErr)
1528 return ContentOrErr.takeError();
1529
1530 if (ContentOrErr->size() % (sizeof(Elf_Word) * 2) != 0) {
1531 S->Content = yaml::BinaryRef(*ContentOrErr);
1532 return S.release();
1533 }
1534
1535 ArrayRef<Elf_Word> Words(
1536 reinterpret_cast<const Elf_Word *>(ContentOrErr->data()),
1537 ContentOrErr->size() / sizeof(Elf_Word));
1538
1539 S->Entries.emplace();
1540 for (size_t I = 0, E = Words.size(); I != E; I += 2)
1541 S->Entries->push_back(x: {.Offset: (yaml::Hex32)Words[I], .Value: (yaml::Hex32)Words[I + 1]});
1542
1543 return S.release();
1544}
1545
1546template <class ELFT>
1547Expected<ELFYAML::MipsABIFlags *>
1548ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
1549 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
1550 "Section type is not SHT_MIPS_ABIFLAGS");
1551 auto S = std::make_unique<ELFYAML::MipsABIFlags>();
1552 if (Error E = dumpCommonSection(Shdr, S&: *S))
1553 return std::move(E);
1554
1555 auto ContentOrErr = Obj.getSectionContents(*Shdr);
1556 if (!ContentOrErr)
1557 return ContentOrErr.takeError();
1558
1559 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
1560 ContentOrErr.get().data());
1561 S->Version = Flags->version;
1562 S->ISALevel = Flags->isa_level;
1563 S->ISARevision = Flags->isa_rev;
1564 S->GPRSize = Flags->gpr_size;
1565 S->CPR1Size = Flags->cpr1_size;
1566 S->CPR2Size = Flags->cpr2_size;
1567 S->FpABI = Flags->fp_abi;
1568 S->ISAExtension = Flags->isa_ext;
1569 S->ASEs = Flags->ases;
1570 S->Flags1 = Flags->flags1;
1571 S->Flags2 = Flags->flags2;
1572 return S.release();
1573}
1574
1575template <class ELFT>
1576static Error elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj,
1577 std::unique_ptr<DWARFContext> DWARFCtx) {
1578 ELFDumper<ELFT> Dumper(Obj, std::move(DWARFCtx));
1579 Expected<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
1580 if (!YAMLOrErr)
1581 return YAMLOrErr.takeError();
1582
1583 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
1584 yaml::Output Yout(Out);
1585 Yout << *YAML;
1586
1587 return Error::success();
1588}
1589
1590Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
1591 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
1592 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(Val: &Obj))
1593 return elf2yaml(Out, Obj: ELFObj->getELFFile(), DWARFCtx: std::move(DWARFCtx));
1594
1595 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(Val: &Obj))
1596 return elf2yaml(Out, Obj: ELFObj->getELFFile(), DWARFCtx: std::move(DWARFCtx));
1597
1598 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(Val: &Obj))
1599 return elf2yaml(Out, Obj: ELFObj->getELFFile(), DWARFCtx: std::move(DWARFCtx));
1600
1601 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(Val: &Obj))
1602 return elf2yaml(Out, Obj: ELFObj->getELFFile(), DWARFCtx: std::move(DWARFCtx));
1603
1604 llvm_unreachable("unknown ELF file format");
1605}
1606