[llvm-readelf/llvm-objdump] - Improve/refactor the implementation of SHT_LLVM_ADDRSIG...
[llvm-complete.git] / tools / obj2yaml / elf2yaml.cpp
blob2c17b9570e1ba50cce61eed1527d138e1f69a5bb
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 //===----------------------------------------------------------------------===//
9 #include "Error.h"
10 #include "llvm/ADT/DenseSet.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/Object/ELFObjectFile.h"
13 #include "llvm/ObjectYAML/ELFYAML.h"
14 #include "llvm/Support/DataExtractor.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/YAMLTraits.h"
18 using namespace llvm;
20 namespace {
22 template <class ELFT>
23 class ELFDumper {
24 typedef object::Elf_Sym_Impl<ELFT> Elf_Sym;
25 typedef typename ELFT::Dyn Elf_Dyn;
26 typedef typename ELFT::Shdr Elf_Shdr;
27 typedef typename ELFT::Word Elf_Word;
28 typedef typename ELFT::Rel Elf_Rel;
29 typedef typename ELFT::Rela Elf_Rela;
31 ArrayRef<Elf_Shdr> Sections;
32 ArrayRef<Elf_Sym> SymTable;
34 DenseMap<StringRef, uint32_t> UsedSectionNames;
35 std::vector<std::string> SectionNames;
37 DenseMap<StringRef, uint32_t> UsedSymbolNames;
38 std::vector<std::string> SymbolNames;
40 Expected<StringRef> getUniquedSectionName(const Elf_Shdr *Sec);
41 Expected<StringRef> getUniquedSymbolName(const Elf_Sym *Sym,
42 StringRef StrTable,
43 const Elf_Shdr *SymTab);
44 Expected<StringRef> getSymbolName(uint32_t SymtabNdx, uint32_t SymbolNdx);
46 const object::ELFFile<ELFT> &Obj;
47 ArrayRef<Elf_Word> ShndxTable;
49 Error dumpSymbols(const Elf_Shdr *Symtab,
50 std::vector<ELFYAML::Symbol> &Symbols);
51 Error dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
52 StringRef StrTable, ELFYAML::Symbol &S);
53 Error dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
54 Error dumpCommonRelocationSection(const Elf_Shdr *Shdr,
55 ELFYAML::RelocationSection &S);
56 template <class RelT>
57 Error dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
58 ELFYAML::Relocation &R);
60 Expected<ELFYAML::AddrsigSection *> dumpAddrsigSection(const Elf_Shdr *Shdr);
61 Expected<ELFYAML::DynamicSection *> dumpDynamicSection(const Elf_Shdr *Shdr);
62 Expected<ELFYAML::RelocationSection *> dumpRelocSection(const Elf_Shdr *Shdr);
63 Expected<ELFYAML::RawContentSection *>
64 dumpContentSection(const Elf_Shdr *Shdr);
65 Expected<ELFYAML::SymtabShndxSection *>
66 dumpSymtabShndxSection(const Elf_Shdr *Shdr);
67 Expected<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
68 Expected<ELFYAML::HashSection *> dumpHashSection(const Elf_Shdr *Shdr);
69 Expected<ELFYAML::VerdefSection *> dumpVerdefSection(const Elf_Shdr *Shdr);
70 Expected<ELFYAML::SymverSection *> dumpSymverSection(const Elf_Shdr *Shdr);
71 Expected<ELFYAML::VerneedSection *> dumpVerneedSection(const Elf_Shdr *Shdr);
72 Expected<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
73 Expected<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
74 Expected<ELFYAML::StackSizesSection *>
75 dumpStackSizesSection(const Elf_Shdr *Shdr);
77 Expected<ELFYAML::Section *> dumpSpecialSection(const Elf_Shdr *Shdr);
79 public:
80 ELFDumper(const object::ELFFile<ELFT> &O);
81 Expected<ELFYAML::Object *> dump();
86 template <class ELFT>
87 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
88 : Obj(O) {}
90 template <class ELFT>
91 Expected<StringRef>
92 ELFDumper<ELFT>::getUniquedSectionName(const Elf_Shdr *Sec) {
93 unsigned SecIndex = Sec - &Sections[0];
94 assert(&Sections[SecIndex] == Sec);
95 if (!SectionNames[SecIndex].empty())
96 return SectionNames[SecIndex];
98 auto NameOrErr = Obj.getSectionName(Sec);
99 if (!NameOrErr)
100 return NameOrErr;
101 StringRef Name = *NameOrErr;
102 std::string &Ret = SectionNames[SecIndex];
104 auto It = UsedSectionNames.insert({Name, 0});
105 if (!It.second)
106 Ret = (Name + " [" + Twine(++It.first->second) + "]").str();
107 else
108 Ret = Name;
109 return Ret;
112 template <class ELFT>
113 Expected<StringRef>
114 ELFDumper<ELFT>::getUniquedSymbolName(const Elf_Sym *Sym, StringRef StrTable,
115 const Elf_Shdr *SymTab) {
116 Expected<StringRef> SymbolNameOrErr = Sym->getName(StrTable);
117 if (!SymbolNameOrErr)
118 return SymbolNameOrErr;
119 StringRef Name = *SymbolNameOrErr;
120 if (Name.empty() && Sym->getType() == ELF::STT_SECTION) {
121 auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
122 if (!ShdrOrErr)
123 return ShdrOrErr.takeError();
124 return getUniquedSectionName(*ShdrOrErr);
127 // Symbols in .symtab can have duplicate names. For example, it is a common
128 // situation for local symbols in a relocatable object. Here we assign unique
129 // suffixes for such symbols so that we can differentiate them.
130 if (SymTab->sh_type == ELF::SHT_SYMTAB) {
131 unsigned Index = Sym - SymTable.data();
132 if (!SymbolNames[Index].empty())
133 return SymbolNames[Index];
135 auto It = UsedSymbolNames.insert({Name, 0});
136 if (!It.second)
137 SymbolNames[Index] =
138 (Name + " [" + Twine(++It.first->second) + "]").str();
139 else
140 SymbolNames[Index] = Name;
141 return SymbolNames[Index];
144 return Name;
147 template <class ELFT> Expected<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
148 auto Y = std::make_unique<ELFYAML::Object>();
150 // Dump header. We do not dump SHEntSize, SHOff, SHNum and SHStrNdx fields.
151 // When not explicitly set, the values are set by yaml2obj automatically
152 // and there is no need to dump them here.
153 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
154 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
155 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
156 Y->Header.ABIVersion = Obj.getHeader()->e_ident[ELF::EI_ABIVERSION];
157 Y->Header.Type = Obj.getHeader()->e_type;
158 Y->Header.Machine = Obj.getHeader()->e_machine;
159 Y->Header.Flags = Obj.getHeader()->e_flags;
160 Y->Header.Entry = Obj.getHeader()->e_entry;
162 // Dump sections
163 auto SectionsOrErr = Obj.sections();
164 if (!SectionsOrErr)
165 return SectionsOrErr.takeError();
166 Sections = *SectionsOrErr;
167 SectionNames.resize(Sections.size());
169 // Dump symbols. We need to do this early because other sections might want
170 // to access the deduplicated symbol names that we also create here.
171 const Elf_Shdr *SymTab = nullptr;
172 const Elf_Shdr *SymTabShndx = nullptr;
173 const Elf_Shdr *DynSymTab = nullptr;
175 for (const Elf_Shdr &Sec : Sections) {
176 if (Sec.sh_type == ELF::SHT_SYMTAB) {
177 SymTab = &Sec;
178 } else if (Sec.sh_type == ELF::SHT_DYNSYM) {
179 DynSymTab = &Sec;
180 } else if (Sec.sh_type == ELF::SHT_SYMTAB_SHNDX) {
181 // ABI allows us to have one SHT_SYMTAB_SHNDX for each symbol table.
182 // We only support having the SHT_SYMTAB_SHNDX for SHT_SYMTAB now.
183 if (SymTabShndx)
184 return createStringError(obj2yaml_error::not_implemented,
185 "multiple SHT_SYMTAB_SHNDX sections are not supported");
186 SymTabShndx = &Sec;
190 // We need to locate the SHT_SYMTAB_SHNDX section early, because it might be
191 // needed for dumping symbols.
192 if (SymTabShndx) {
193 if (!SymTab || SymTabShndx->sh_link != SymTab - Sections.begin())
194 return createStringError(
195 obj2yaml_error::not_implemented,
196 "only SHT_SYMTAB_SHNDX associated with SHT_SYMTAB are supported");
198 auto TableOrErr = Obj.getSHNDXTable(*SymTabShndx);
199 if (!TableOrErr)
200 return TableOrErr.takeError();
201 ShndxTable = *TableOrErr;
203 if (SymTab)
204 if (Error E = dumpSymbols(SymTab, Y->Symbols))
205 return std::move(E);
206 if (DynSymTab)
207 if (Error E = dumpSymbols(DynSymTab, Y->DynamicSymbols))
208 return std::move(E);
210 for (const Elf_Shdr &Sec : Sections) {
211 switch (Sec.sh_type) {
212 case ELF::SHT_DYNAMIC: {
213 Expected<ELFYAML::DynamicSection *> SecOrErr = dumpDynamicSection(&Sec);
214 if (!SecOrErr)
215 return SecOrErr.takeError();
216 Y->Sections.emplace_back(*SecOrErr);
217 break;
219 case ELF::SHT_STRTAB:
220 case ELF::SHT_SYMTAB:
221 case ELF::SHT_DYNSYM:
222 // Do not dump these sections.
223 break;
224 case ELF::SHT_SYMTAB_SHNDX: {
225 Expected<ELFYAML::SymtabShndxSection *> SecOrErr =
226 dumpSymtabShndxSection(&Sec);
227 if (!SecOrErr)
228 return SecOrErr.takeError();
229 Y->Sections.emplace_back(*SecOrErr);
230 break;
232 case ELF::SHT_REL:
233 case ELF::SHT_RELA: {
234 Expected<ELFYAML::RelocationSection *> SecOrErr = dumpRelocSection(&Sec);
235 if (!SecOrErr)
236 return SecOrErr.takeError();
237 Y->Sections.emplace_back(*SecOrErr);
238 break;
240 case ELF::SHT_GROUP: {
241 Expected<ELFYAML::Group *> GroupOrErr = dumpGroup(&Sec);
242 if (!GroupOrErr)
243 return GroupOrErr.takeError();
244 Y->Sections.emplace_back(*GroupOrErr);
245 break;
247 case ELF::SHT_MIPS_ABIFLAGS: {
248 Expected<ELFYAML::MipsABIFlags *> SecOrErr = dumpMipsABIFlags(&Sec);
249 if (!SecOrErr)
250 return SecOrErr.takeError();
251 Y->Sections.emplace_back(*SecOrErr);
252 break;
254 case ELF::SHT_NOBITS: {
255 Expected<ELFYAML::NoBitsSection *> SecOrErr = dumpNoBitsSection(&Sec);
256 if (!SecOrErr)
257 return SecOrErr.takeError();
258 Y->Sections.emplace_back(*SecOrErr);
259 break;
261 case ELF::SHT_HASH: {
262 Expected<ELFYAML::HashSection *> SecOrErr = dumpHashSection(&Sec);
263 if (!SecOrErr)
264 return SecOrErr.takeError();
265 Y->Sections.emplace_back(*SecOrErr);
266 break;
268 case ELF::SHT_GNU_verdef: {
269 Expected<ELFYAML::VerdefSection *> SecOrErr = dumpVerdefSection(&Sec);
270 if (!SecOrErr)
271 return SecOrErr.takeError();
272 Y->Sections.emplace_back(*SecOrErr);
273 break;
275 case ELF::SHT_GNU_versym: {
276 Expected<ELFYAML::SymverSection *> SecOrErr = dumpSymverSection(&Sec);
277 if (!SecOrErr)
278 return SecOrErr.takeError();
279 Y->Sections.emplace_back(*SecOrErr);
280 break;
282 case ELF::SHT_GNU_verneed: {
283 Expected<ELFYAML::VerneedSection *> SecOrErr = dumpVerneedSection(&Sec);
284 if (!SecOrErr)
285 return SecOrErr.takeError();
286 Y->Sections.emplace_back(*SecOrErr);
287 break;
289 case ELF::SHT_LLVM_ADDRSIG: {
290 Expected<ELFYAML::AddrsigSection *> SecOrErr = dumpAddrsigSection(&Sec);
291 if (!SecOrErr)
292 return SecOrErr.takeError();
293 Y->Sections.emplace_back(*SecOrErr);
294 break;
296 case ELF::SHT_NULL: {
297 // We only dump the SHT_NULL section at index 0 when it
298 // has at least one non-null field, because yaml2obj
299 // normally creates the zero section at index 0 implicitly.
300 if (&Sec == &Sections[0]) {
301 const uint8_t *Begin = reinterpret_cast<const uint8_t *>(&Sec);
302 const uint8_t *End = Begin + sizeof(Elf_Shdr);
303 if (std::find_if(Begin, End, [](uint8_t V) { return V != 0; }) == End)
304 break;
306 LLVM_FALLTHROUGH;
308 default: {
309 // Recognize some special SHT_PROGBITS sections by name.
310 if (Sec.sh_type == ELF::SHT_PROGBITS) {
311 Expected<ELFYAML::Section *> SpecialSecOrErr = dumpSpecialSection(&Sec);
312 if (!SpecialSecOrErr)
313 return SpecialSecOrErr.takeError();
314 if (*SpecialSecOrErr) {
315 Y->Sections.emplace_back(*SpecialSecOrErr);
316 break;
320 Expected<ELFYAML::RawContentSection *> SecOrErr =
321 dumpContentSection(&Sec);
322 if (!SecOrErr)
323 return SecOrErr.takeError();
324 Y->Sections.emplace_back(*SecOrErr);
329 return Y.release();
332 template <class ELFT>
333 Error ELFDumper<ELFT>::dumpSymbols(const Elf_Shdr *Symtab,
334 std::vector<ELFYAML::Symbol> &Symbols) {
335 if (!Symtab)
336 return Error::success();
338 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
339 if (!StrTableOrErr)
340 return StrTableOrErr.takeError();
341 StringRef StrTable = *StrTableOrErr;
343 auto SymtabOrErr = Obj.symbols(Symtab);
344 if (!SymtabOrErr)
345 return SymtabOrErr.takeError();
347 if (Symtab->sh_type == ELF::SHT_SYMTAB) {
348 SymTable = *SymtabOrErr;
349 SymbolNames.resize(SymTable.size());
352 for (const auto &Sym : (*SymtabOrErr).drop_front()) {
353 ELFYAML::Symbol S;
354 if (auto EC = dumpSymbol(&Sym, Symtab, StrTable, S))
355 return EC;
356 Symbols.push_back(S);
359 return Error::success();
362 template <class ELFT>
363 Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
364 StringRef StrTable, ELFYAML::Symbol &S) {
365 S.Type = Sym->getType();
366 S.Value = Sym->st_value;
367 S.Size = Sym->st_size;
368 S.Other = Sym->st_other;
369 S.Binding = Sym->getBinding();
371 Expected<StringRef> SymbolNameOrErr =
372 getUniquedSymbolName(Sym, StrTable, SymTab);
373 if (!SymbolNameOrErr)
374 return SymbolNameOrErr.takeError();
375 S.Name = SymbolNameOrErr.get();
377 if (Sym->st_shndx >= ELF::SHN_LORESERVE) {
378 S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx;
379 return Error::success();
382 auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
383 if (!ShdrOrErr)
384 return ShdrOrErr.takeError();
385 const Elf_Shdr *Shdr = *ShdrOrErr;
386 if (!Shdr)
387 return Error::success();
389 auto NameOrErr = getUniquedSectionName(Shdr);
390 if (!NameOrErr)
391 return NameOrErr.takeError();
392 S.Section = NameOrErr.get();
394 return Error::success();
397 template <class ELFT>
398 template <class RelT>
399 Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
400 ELFYAML::Relocation &R) {
401 R.Type = Rel->getType(Obj.isMips64EL());
402 R.Offset = Rel->r_offset;
403 R.Addend = 0;
405 auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab);
406 if (!SymOrErr)
407 return SymOrErr.takeError();
408 const Elf_Sym *Sym = *SymOrErr;
409 auto StrTabSec = Obj.getSection(SymTab->sh_link);
410 if (!StrTabSec)
411 return StrTabSec.takeError();
412 auto StrTabOrErr = Obj.getStringTable(*StrTabSec);
413 if (!StrTabOrErr)
414 return StrTabOrErr.takeError();
415 StringRef StrTab = *StrTabOrErr;
417 if (Sym) {
418 Expected<StringRef> NameOrErr = getUniquedSymbolName(Sym, StrTab, SymTab);
419 if (!NameOrErr)
420 return NameOrErr.takeError();
421 R.Symbol = NameOrErr.get();
422 } else {
423 // We have some edge cases of relocations without a symbol associated,
424 // e.g. an object containing the invalid (according to the System V
425 // ABI) R_X86_64_NONE reloc. Create a symbol with an empty name instead
426 // of crashing.
427 R.Symbol = "";
430 return Error::success();
433 template <class ELFT>
434 Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
435 ELFYAML::Section &S) {
436 // Dump fields. We do not dump the ShOffset field. When not explicitly
437 // set, the value is set by yaml2obj automatically.
438 S.Type = Shdr->sh_type;
439 if (Shdr->sh_flags)
440 S.Flags = static_cast<ELFYAML::ELF_SHF>(Shdr->sh_flags);
441 S.Address = Shdr->sh_addr;
442 S.AddressAlign = Shdr->sh_addralign;
443 if (Shdr->sh_entsize)
444 S.EntSize = static_cast<llvm::yaml::Hex64>(Shdr->sh_entsize);
446 auto NameOrErr = getUniquedSectionName(Shdr);
447 if (!NameOrErr)
448 return NameOrErr.takeError();
449 S.Name = NameOrErr.get();
451 if (Shdr->sh_link != ELF::SHN_UNDEF) {
452 auto LinkSection = Obj.getSection(Shdr->sh_link);
453 if (!LinkSection)
454 return make_error<StringError>(
455 "unable to resolve sh_link reference in section '" + S.Name +
456 "': " + toString(LinkSection.takeError()),
457 inconvertibleErrorCode());
459 NameOrErr = getUniquedSectionName(*LinkSection);
460 if (!NameOrErr)
461 return NameOrErr.takeError();
462 S.Link = NameOrErr.get();
465 return Error::success();
468 template <class ELFT>
469 Expected<ELFYAML::Section *>
470 ELFDumper<ELFT>::dumpSpecialSection(const Elf_Shdr *Shdr) {
471 auto NameOrErr = getUniquedSectionName(Shdr);
472 if (!NameOrErr)
473 return NameOrErr.takeError();
475 if (ELFYAML::StackSizesSection::nameMatches(*NameOrErr))
476 return dumpStackSizesSection(Shdr);
477 return nullptr;
480 template <class ELFT>
481 Error ELFDumper<ELFT>::dumpCommonRelocationSection(
482 const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S) {
483 if (Error E = dumpCommonSection(Shdr, S))
484 return E;
486 auto InfoSection = Obj.getSection(Shdr->sh_info);
487 if (!InfoSection)
488 return InfoSection.takeError();
490 auto NameOrErr = getUniquedSectionName(*InfoSection);
491 if (!NameOrErr)
492 return NameOrErr.takeError();
493 S.RelocatableSec = NameOrErr.get();
495 return Error::success();
498 template <class ELFT>
499 Expected<ELFYAML::StackSizesSection *>
500 ELFDumper<ELFT>::dumpStackSizesSection(const Elf_Shdr *Shdr) {
501 auto S = std::make_unique<ELFYAML::StackSizesSection>();
502 if (Error E = dumpCommonSection(Shdr, *S))
503 return std::move(E);
505 auto ContentOrErr = Obj.getSectionContents(Shdr);
506 if (!ContentOrErr)
507 return ContentOrErr.takeError();
509 ArrayRef<uint8_t> Content = *ContentOrErr;
510 DataExtractor Data(Content, Obj.isLE(), ELFT::Is64Bits ? 8 : 4);
512 std::vector<ELFYAML::StackSizeEntry> Entries;
513 DataExtractor::Cursor Cur(0);
514 while (Cur && Cur.tell() < Content.size()) {
515 uint64_t Address = Data.getAddress(Cur);
516 uint64_t Size = Data.getULEB128(Cur);
517 Entries.push_back({Address, Size});
520 if (Content.empty() || !Cur) {
521 // If .stack_sizes cannot be decoded, we dump it as an array of bytes.
522 consumeError(Cur.takeError());
523 S->Content = yaml::BinaryRef(Content);
524 } else {
525 S->Entries = std::move(Entries);
528 return S.release();
531 template <class ELFT>
532 Expected<ELFYAML::AddrsigSection *>
533 ELFDumper<ELFT>::dumpAddrsigSection(const Elf_Shdr *Shdr) {
534 auto S = std::make_unique<ELFYAML::AddrsigSection>();
535 if (Error E = dumpCommonSection(Shdr, *S))
536 return std::move(E);
538 auto ContentOrErr = Obj.getSectionContents(Shdr);
539 if (!ContentOrErr)
540 return ContentOrErr.takeError();
542 ArrayRef<uint8_t> Content = *ContentOrErr;
543 DataExtractor::Cursor Cur(0);
544 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0);
545 std::vector<ELFYAML::AddrsigSymbol> Symbols;
546 while (Cur && Cur.tell() < Content.size()) {
547 uint64_t SymNdx = Data.getULEB128(Cur);
548 if (!Cur)
549 break;
551 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, SymNdx);
552 if (!SymbolName || SymbolName->empty()) {
553 consumeError(SymbolName.takeError());
554 Symbols.emplace_back(SymNdx);
555 continue;
558 Symbols.emplace_back(*SymbolName);
561 if (Cur) {
562 S->Symbols = std::move(Symbols);
563 return S.release();
566 consumeError(Cur.takeError());
567 S->Content = yaml::BinaryRef(Content);
568 return S.release();
571 template <class ELFT>
572 Expected<ELFYAML::DynamicSection *>
573 ELFDumper<ELFT>::dumpDynamicSection(const Elf_Shdr *Shdr) {
574 auto S = std::make_unique<ELFYAML::DynamicSection>();
575 if (Error E = dumpCommonSection(Shdr, *S))
576 return std::move(E);
578 auto DynTagsOrErr = Obj.template getSectionContentsAsArray<Elf_Dyn>(Shdr);
579 if (!DynTagsOrErr)
580 return DynTagsOrErr.takeError();
582 for (const Elf_Dyn &Dyn : *DynTagsOrErr)
583 S->Entries.push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()});
585 return S.release();
588 template <class ELFT>
589 Expected<ELFYAML::RelocationSection *>
590 ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) {
591 auto S = std::make_unique<ELFYAML::RelocationSection>();
592 if (auto E = dumpCommonRelocationSection(Shdr, *S))
593 return std::move(E);
595 auto SymTabOrErr = Obj.getSection(Shdr->sh_link);
596 if (!SymTabOrErr)
597 return SymTabOrErr.takeError();
598 const Elf_Shdr *SymTab = *SymTabOrErr;
600 if (Shdr->sh_type == ELF::SHT_REL) {
601 auto Rels = Obj.rels(Shdr);
602 if (!Rels)
603 return Rels.takeError();
604 for (const Elf_Rel &Rel : *Rels) {
605 ELFYAML::Relocation R;
606 if (Error E = dumpRelocation(&Rel, SymTab, R))
607 return std::move(E);
608 S->Relocations.push_back(R);
610 } else {
611 auto Rels = Obj.relas(Shdr);
612 if (!Rels)
613 return Rels.takeError();
614 for (const Elf_Rela &Rel : *Rels) {
615 ELFYAML::Relocation R;
616 if (Error E = dumpRelocation(&Rel, SymTab, R))
617 return std::move(E);
618 R.Addend = Rel.r_addend;
619 S->Relocations.push_back(R);
623 return S.release();
626 template <class ELFT>
627 Expected<ELFYAML::RawContentSection *>
628 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
629 auto S = std::make_unique<ELFYAML::RawContentSection>();
630 if (Error E = dumpCommonSection(Shdr, *S))
631 return std::move(E);
633 unsigned SecIndex = Shdr - &Sections[0];
634 if (SecIndex != 0 || Shdr->sh_type != ELF::SHT_NULL) {
635 auto ContentOrErr = Obj.getSectionContents(Shdr);
636 if (!ContentOrErr)
637 return ContentOrErr.takeError();
638 ArrayRef<uint8_t> Content = *ContentOrErr;
639 if (!Content.empty())
640 S->Content = yaml::BinaryRef(Content);
641 } else {
642 S->Size = static_cast<llvm::yaml::Hex64>(Shdr->sh_size);
645 if (Shdr->sh_info)
646 S->Info = static_cast<llvm::yaml::Hex64>(Shdr->sh_info);
647 return S.release();
650 template <class ELFT>
651 Expected<ELFYAML::SymtabShndxSection *>
652 ELFDumper<ELFT>::dumpSymtabShndxSection(const Elf_Shdr *Shdr) {
653 auto S = std::make_unique<ELFYAML::SymtabShndxSection>();
654 if (Error E = dumpCommonSection(Shdr, *S))
655 return std::move(E);
657 auto EntriesOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr);
658 if (!EntriesOrErr)
659 return EntriesOrErr.takeError();
660 for (const Elf_Word &E : *EntriesOrErr)
661 S->Entries.push_back(E);
662 return S.release();
665 template <class ELFT>
666 Expected<ELFYAML::NoBitsSection *>
667 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
668 auto S = std::make_unique<ELFYAML::NoBitsSection>();
669 if (Error E = dumpCommonSection(Shdr, *S))
670 return std::move(E);
671 S->Size = Shdr->sh_size;
673 return S.release();
676 template <class ELFT>
677 Expected<ELFYAML::HashSection *>
678 ELFDumper<ELFT>::dumpHashSection(const Elf_Shdr *Shdr) {
679 auto S = std::make_unique<ELFYAML::HashSection>();
680 if (Error E = dumpCommonSection(Shdr, *S))
681 return std::move(E);
683 auto ContentOrErr = Obj.getSectionContents(Shdr);
684 if (!ContentOrErr)
685 return ContentOrErr.takeError();
687 ArrayRef<uint8_t> Content = *ContentOrErr;
688 if (Content.size() % 4 != 0 || Content.size() < 8) {
689 S->Content = yaml::BinaryRef(Content);
690 return S.release();
693 DataExtractor::Cursor Cur(0);
694 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0);
695 uint32_t NBucket = Data.getU32(Cur);
696 uint32_t NChain = Data.getU32(Cur);
697 if (Content.size() != (2 + NBucket + NChain) * 4) {
698 S->Content = yaml::BinaryRef(Content);
699 if (Cur)
700 return S.release();
701 llvm_unreachable("entries were not read correctly");
704 S->Bucket.emplace(NBucket);
705 for (uint32_t &V : *S->Bucket)
706 V = Data.getU32(Cur);
708 S->Chain.emplace(NChain);
709 for (uint32_t &V : *S->Chain)
710 V = Data.getU32(Cur);
712 if (Cur)
713 return S.release();
714 llvm_unreachable("entries were not read correctly");
717 template <class ELFT>
718 Expected<ELFYAML::VerdefSection *>
719 ELFDumper<ELFT>::dumpVerdefSection(const Elf_Shdr *Shdr) {
720 typedef typename ELFT::Verdef Elf_Verdef;
721 typedef typename ELFT::Verdaux Elf_Verdaux;
723 auto S = std::make_unique<ELFYAML::VerdefSection>();
724 if (Error E = dumpCommonSection(Shdr, *S))
725 return std::move(E);
727 S->Info = Shdr->sh_info;
729 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
730 if (!StringTableShdrOrErr)
731 return StringTableShdrOrErr.takeError();
733 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
734 if (!StringTableOrErr)
735 return StringTableOrErr.takeError();
737 auto Contents = Obj.getSectionContents(Shdr);
738 if (!Contents)
739 return Contents.takeError();
741 llvm::ArrayRef<uint8_t> Data = *Contents;
742 const uint8_t *Buf = Data.data();
743 while (Buf) {
744 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(Buf);
745 ELFYAML::VerdefEntry Entry;
746 Entry.Version = Verdef->vd_version;
747 Entry.Flags = Verdef->vd_flags;
748 Entry.VersionNdx = Verdef->vd_ndx;
749 Entry.Hash = Verdef->vd_hash;
751 const uint8_t *BufAux = Buf + Verdef->vd_aux;
752 while (BufAux) {
753 const Elf_Verdaux *Verdaux =
754 reinterpret_cast<const Elf_Verdaux *>(BufAux);
755 Entry.VerNames.push_back(
756 StringTableOrErr->drop_front(Verdaux->vda_name).data());
757 BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
760 S->Entries.push_back(Entry);
761 Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
764 return S.release();
767 template <class ELFT>
768 Expected<ELFYAML::SymverSection *>
769 ELFDumper<ELFT>::dumpSymverSection(const Elf_Shdr *Shdr) {
770 typedef typename ELFT::Half Elf_Half;
772 auto S = std::make_unique<ELFYAML::SymverSection>();
773 if (Error E = dumpCommonSection(Shdr, *S))
774 return std::move(E);
776 auto VersionsOrErr = Obj.template getSectionContentsAsArray<Elf_Half>(Shdr);
777 if (!VersionsOrErr)
778 return VersionsOrErr.takeError();
779 for (const Elf_Half &E : *VersionsOrErr)
780 S->Entries.push_back(E);
782 return S.release();
785 template <class ELFT>
786 Expected<ELFYAML::VerneedSection *>
787 ELFDumper<ELFT>::dumpVerneedSection(const Elf_Shdr *Shdr) {
788 typedef typename ELFT::Verneed Elf_Verneed;
789 typedef typename ELFT::Vernaux Elf_Vernaux;
791 auto S = std::make_unique<ELFYAML::VerneedSection>();
792 if (Error E = dumpCommonSection(Shdr, *S))
793 return std::move(E);
795 S->Info = Shdr->sh_info;
797 auto Contents = Obj.getSectionContents(Shdr);
798 if (!Contents)
799 return Contents.takeError();
801 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
802 if (!StringTableShdrOrErr)
803 return StringTableShdrOrErr.takeError();
805 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
806 if (!StringTableOrErr)
807 return StringTableOrErr.takeError();
809 llvm::ArrayRef<uint8_t> Data = *Contents;
810 const uint8_t *Buf = Data.data();
811 while (Buf) {
812 const Elf_Verneed *Verneed = reinterpret_cast<const Elf_Verneed *>(Buf);
814 ELFYAML::VerneedEntry Entry;
815 Entry.Version = Verneed->vn_version;
816 Entry.File =
817 StringRef(StringTableOrErr->drop_front(Verneed->vn_file).data());
819 const uint8_t *BufAux = Buf + Verneed->vn_aux;
820 while (BufAux) {
821 const Elf_Vernaux *Vernaux =
822 reinterpret_cast<const Elf_Vernaux *>(BufAux);
824 ELFYAML::VernauxEntry Aux;
825 Aux.Hash = Vernaux->vna_hash;
826 Aux.Flags = Vernaux->vna_flags;
827 Aux.Other = Vernaux->vna_other;
828 Aux.Name =
829 StringRef(StringTableOrErr->drop_front(Vernaux->vna_name).data());
831 Entry.AuxV.push_back(Aux);
832 BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr;
835 S->VerneedV.push_back(Entry);
836 Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr;
839 return S.release();
842 template <class ELFT>
843 Expected<StringRef> ELFDumper<ELFT>::getSymbolName(uint32_t SymtabNdx,
844 uint32_t SymbolNdx) {
845 auto SymtabOrErr = Obj.getSection(SymtabNdx);
846 if (!SymtabOrErr)
847 return SymtabOrErr.takeError();
849 const Elf_Shdr *Symtab = *SymtabOrErr;
850 auto SymOrErr = Obj.getSymbol(Symtab, SymbolNdx);
851 if (!SymOrErr)
852 return SymOrErr.takeError();
854 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
855 if (!StrTabOrErr)
856 return StrTabOrErr.takeError();
857 return getUniquedSymbolName(*SymOrErr, *StrTabOrErr, Symtab);
860 template <class ELFT>
861 Expected<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
862 auto S = std::make_unique<ELFYAML::Group>();
863 if (Error E = dumpCommonSection(Shdr, *S))
864 return std::move(E);
866 // Get symbol with index sh_info. This symbol's name is the signature of the group.
867 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, Shdr->sh_info);
868 if (!SymbolName)
869 return SymbolName.takeError();
870 S->Signature = *SymbolName;
872 auto MembersOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr);
873 if (!MembersOrErr)
874 return MembersOrErr.takeError();
876 for (Elf_Word Member : *MembersOrErr) {
877 if (Member == llvm::ELF::GRP_COMDAT) {
878 S->Members.push_back({"GRP_COMDAT"});
879 continue;
882 auto SHdrOrErr = Obj.getSection(Member);
883 if (!SHdrOrErr)
884 return SHdrOrErr.takeError();
885 auto NameOrErr = getUniquedSectionName(*SHdrOrErr);
886 if (!NameOrErr)
887 return NameOrErr.takeError();
888 S->Members.push_back({*NameOrErr});
890 return S.release();
893 template <class ELFT>
894 Expected<ELFYAML::MipsABIFlags *>
895 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
896 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
897 "Section type is not SHT_MIPS_ABIFLAGS");
898 auto S = std::make_unique<ELFYAML::MipsABIFlags>();
899 if (Error E = dumpCommonSection(Shdr, *S))
900 return std::move(E);
902 auto ContentOrErr = Obj.getSectionContents(Shdr);
903 if (!ContentOrErr)
904 return ContentOrErr.takeError();
906 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
907 ContentOrErr.get().data());
908 S->Version = Flags->version;
909 S->ISALevel = Flags->isa_level;
910 S->ISARevision = Flags->isa_rev;
911 S->GPRSize = Flags->gpr_size;
912 S->CPR1Size = Flags->cpr1_size;
913 S->CPR2Size = Flags->cpr2_size;
914 S->FpABI = Flags->fp_abi;
915 S->ISAExtension = Flags->isa_ext;
916 S->ASEs = Flags->ases;
917 S->Flags1 = Flags->flags1;
918 S->Flags2 = Flags->flags2;
919 return S.release();
922 template <class ELFT>
923 static Error elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) {
924 ELFDumper<ELFT> Dumper(Obj);
925 Expected<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
926 if (!YAMLOrErr)
927 return YAMLOrErr.takeError();
929 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
930 yaml::Output Yout(Out);
931 Yout << *YAML;
933 return Error::success();
936 Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
937 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
938 return elf2yaml(Out, *ELFObj->getELFFile());
940 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
941 return elf2yaml(Out, *ELFObj->getELFFile());
943 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
944 return elf2yaml(Out, *ELFObj->getELFFile());
946 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
947 return elf2yaml(Out, *ELFObj->getELFFile());
949 llvm_unreachable("unknown ELF file format");