[MIParser] Set RegClassOrRegBank during instruction parsing
[llvm-complete.git] / tools / obj2yaml / elf2yaml.cpp
blob35c33960c3737b15580f32eb64b2b06571b6ddb1
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;
204 if (SymTab) {
205 Y->Symbols.emplace();
206 if (Error E = dumpSymbols(SymTab, *Y->Symbols))
207 return std::move(E);
210 if (DynSymTab)
211 if (Error E = dumpSymbols(DynSymTab, Y->DynamicSymbols))
212 return std::move(E);
214 for (const Elf_Shdr &Sec : Sections) {
215 switch (Sec.sh_type) {
216 case ELF::SHT_DYNAMIC: {
217 Expected<ELFYAML::DynamicSection *> SecOrErr = dumpDynamicSection(&Sec);
218 if (!SecOrErr)
219 return SecOrErr.takeError();
220 Y->Sections.emplace_back(*SecOrErr);
221 break;
223 case ELF::SHT_STRTAB:
224 case ELF::SHT_SYMTAB:
225 case ELF::SHT_DYNSYM:
226 // Do not dump these sections.
227 break;
228 case ELF::SHT_SYMTAB_SHNDX: {
229 Expected<ELFYAML::SymtabShndxSection *> SecOrErr =
230 dumpSymtabShndxSection(&Sec);
231 if (!SecOrErr)
232 return SecOrErr.takeError();
233 Y->Sections.emplace_back(*SecOrErr);
234 break;
236 case ELF::SHT_REL:
237 case ELF::SHT_RELA: {
238 Expected<ELFYAML::RelocationSection *> SecOrErr = dumpRelocSection(&Sec);
239 if (!SecOrErr)
240 return SecOrErr.takeError();
241 Y->Sections.emplace_back(*SecOrErr);
242 break;
244 case ELF::SHT_GROUP: {
245 Expected<ELFYAML::Group *> GroupOrErr = dumpGroup(&Sec);
246 if (!GroupOrErr)
247 return GroupOrErr.takeError();
248 Y->Sections.emplace_back(*GroupOrErr);
249 break;
251 case ELF::SHT_MIPS_ABIFLAGS: {
252 Expected<ELFYAML::MipsABIFlags *> SecOrErr = dumpMipsABIFlags(&Sec);
253 if (!SecOrErr)
254 return SecOrErr.takeError();
255 Y->Sections.emplace_back(*SecOrErr);
256 break;
258 case ELF::SHT_NOBITS: {
259 Expected<ELFYAML::NoBitsSection *> SecOrErr = dumpNoBitsSection(&Sec);
260 if (!SecOrErr)
261 return SecOrErr.takeError();
262 Y->Sections.emplace_back(*SecOrErr);
263 break;
265 case ELF::SHT_HASH: {
266 Expected<ELFYAML::HashSection *> SecOrErr = dumpHashSection(&Sec);
267 if (!SecOrErr)
268 return SecOrErr.takeError();
269 Y->Sections.emplace_back(*SecOrErr);
270 break;
272 case ELF::SHT_GNU_verdef: {
273 Expected<ELFYAML::VerdefSection *> SecOrErr = dumpVerdefSection(&Sec);
274 if (!SecOrErr)
275 return SecOrErr.takeError();
276 Y->Sections.emplace_back(*SecOrErr);
277 break;
279 case ELF::SHT_GNU_versym: {
280 Expected<ELFYAML::SymverSection *> SecOrErr = dumpSymverSection(&Sec);
281 if (!SecOrErr)
282 return SecOrErr.takeError();
283 Y->Sections.emplace_back(*SecOrErr);
284 break;
286 case ELF::SHT_GNU_verneed: {
287 Expected<ELFYAML::VerneedSection *> SecOrErr = dumpVerneedSection(&Sec);
288 if (!SecOrErr)
289 return SecOrErr.takeError();
290 Y->Sections.emplace_back(*SecOrErr);
291 break;
293 case ELF::SHT_LLVM_ADDRSIG: {
294 Expected<ELFYAML::AddrsigSection *> SecOrErr = dumpAddrsigSection(&Sec);
295 if (!SecOrErr)
296 return SecOrErr.takeError();
297 Y->Sections.emplace_back(*SecOrErr);
298 break;
300 case ELF::SHT_NULL: {
301 // We only dump the SHT_NULL section at index 0 when it
302 // has at least one non-null field, because yaml2obj
303 // normally creates the zero section at index 0 implicitly.
304 if (&Sec == &Sections[0]) {
305 const uint8_t *Begin = reinterpret_cast<const uint8_t *>(&Sec);
306 const uint8_t *End = Begin + sizeof(Elf_Shdr);
307 if (std::find_if(Begin, End, [](uint8_t V) { return V != 0; }) == End)
308 break;
310 LLVM_FALLTHROUGH;
312 default: {
313 // Recognize some special SHT_PROGBITS sections by name.
314 if (Sec.sh_type == ELF::SHT_PROGBITS) {
315 Expected<ELFYAML::Section *> SpecialSecOrErr = dumpSpecialSection(&Sec);
316 if (!SpecialSecOrErr)
317 return SpecialSecOrErr.takeError();
318 if (*SpecialSecOrErr) {
319 Y->Sections.emplace_back(*SpecialSecOrErr);
320 break;
324 Expected<ELFYAML::RawContentSection *> SecOrErr =
325 dumpContentSection(&Sec);
326 if (!SecOrErr)
327 return SecOrErr.takeError();
328 Y->Sections.emplace_back(*SecOrErr);
333 return Y.release();
336 template <class ELFT>
337 Error ELFDumper<ELFT>::dumpSymbols(const Elf_Shdr *Symtab,
338 std::vector<ELFYAML::Symbol> &Symbols) {
339 if (!Symtab)
340 return Error::success();
342 auto StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
343 if (!StrTableOrErr)
344 return StrTableOrErr.takeError();
345 StringRef StrTable = *StrTableOrErr;
347 auto SymtabOrErr = Obj.symbols(Symtab);
348 if (!SymtabOrErr)
349 return SymtabOrErr.takeError();
351 if (Symtab->sh_type == ELF::SHT_SYMTAB) {
352 SymTable = *SymtabOrErr;
353 SymbolNames.resize(SymTable.size());
356 for (const auto &Sym : (*SymtabOrErr).drop_front()) {
357 ELFYAML::Symbol S;
358 if (auto EC = dumpSymbol(&Sym, Symtab, StrTable, S))
359 return EC;
360 Symbols.push_back(S);
363 return Error::success();
366 template <class ELFT>
367 Error ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
368 StringRef StrTable, ELFYAML::Symbol &S) {
369 S.Type = Sym->getType();
370 S.Value = Sym->st_value;
371 S.Size = Sym->st_size;
372 S.Other = Sym->st_other;
373 S.Binding = Sym->getBinding();
375 Expected<StringRef> SymbolNameOrErr =
376 getUniquedSymbolName(Sym, StrTable, SymTab);
377 if (!SymbolNameOrErr)
378 return SymbolNameOrErr.takeError();
379 S.Name = SymbolNameOrErr.get();
381 if (Sym->st_shndx >= ELF::SHN_LORESERVE) {
382 S.Index = (ELFYAML::ELF_SHN)Sym->st_shndx;
383 return Error::success();
386 auto ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
387 if (!ShdrOrErr)
388 return ShdrOrErr.takeError();
389 const Elf_Shdr *Shdr = *ShdrOrErr;
390 if (!Shdr)
391 return Error::success();
393 auto NameOrErr = getUniquedSectionName(Shdr);
394 if (!NameOrErr)
395 return NameOrErr.takeError();
396 S.Section = NameOrErr.get();
398 return Error::success();
401 template <class ELFT>
402 template <class RelT>
403 Error ELFDumper<ELFT>::dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
404 ELFYAML::Relocation &R) {
405 R.Type = Rel->getType(Obj.isMips64EL());
406 R.Offset = Rel->r_offset;
407 R.Addend = 0;
409 auto SymOrErr = Obj.getRelocationSymbol(Rel, SymTab);
410 if (!SymOrErr)
411 return SymOrErr.takeError();
412 const Elf_Sym *Sym = *SymOrErr;
413 auto StrTabSec = Obj.getSection(SymTab->sh_link);
414 if (!StrTabSec)
415 return StrTabSec.takeError();
416 auto StrTabOrErr = Obj.getStringTable(*StrTabSec);
417 if (!StrTabOrErr)
418 return StrTabOrErr.takeError();
419 StringRef StrTab = *StrTabOrErr;
421 if (Sym) {
422 Expected<StringRef> NameOrErr = getUniquedSymbolName(Sym, StrTab, SymTab);
423 if (!NameOrErr)
424 return NameOrErr.takeError();
425 R.Symbol = NameOrErr.get();
426 } else {
427 // We have some edge cases of relocations without a symbol associated,
428 // e.g. an object containing the invalid (according to the System V
429 // ABI) R_X86_64_NONE reloc. Create a symbol with an empty name instead
430 // of crashing.
431 R.Symbol = "";
434 return Error::success();
437 template <class ELFT>
438 Error ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
439 ELFYAML::Section &S) {
440 // Dump fields. We do not dump the ShOffset field. When not explicitly
441 // set, the value is set by yaml2obj automatically.
442 S.Type = Shdr->sh_type;
443 if (Shdr->sh_flags)
444 S.Flags = static_cast<ELFYAML::ELF_SHF>(Shdr->sh_flags);
445 S.Address = Shdr->sh_addr;
446 S.AddressAlign = Shdr->sh_addralign;
447 if (Shdr->sh_entsize)
448 S.EntSize = static_cast<llvm::yaml::Hex64>(Shdr->sh_entsize);
450 auto NameOrErr = getUniquedSectionName(Shdr);
451 if (!NameOrErr)
452 return NameOrErr.takeError();
453 S.Name = NameOrErr.get();
455 if (Shdr->sh_link != ELF::SHN_UNDEF) {
456 auto LinkSection = Obj.getSection(Shdr->sh_link);
457 if (!LinkSection)
458 return make_error<StringError>(
459 "unable to resolve sh_link reference in section '" + S.Name +
460 "': " + toString(LinkSection.takeError()),
461 inconvertibleErrorCode());
463 NameOrErr = getUniquedSectionName(*LinkSection);
464 if (!NameOrErr)
465 return NameOrErr.takeError();
466 S.Link = NameOrErr.get();
469 return Error::success();
472 template <class ELFT>
473 Expected<ELFYAML::Section *>
474 ELFDumper<ELFT>::dumpSpecialSection(const Elf_Shdr *Shdr) {
475 auto NameOrErr = getUniquedSectionName(Shdr);
476 if (!NameOrErr)
477 return NameOrErr.takeError();
479 if (ELFYAML::StackSizesSection::nameMatches(*NameOrErr))
480 return dumpStackSizesSection(Shdr);
481 return nullptr;
484 template <class ELFT>
485 Error ELFDumper<ELFT>::dumpCommonRelocationSection(
486 const Elf_Shdr *Shdr, ELFYAML::RelocationSection &S) {
487 if (Error E = dumpCommonSection(Shdr, S))
488 return E;
490 auto InfoSection = Obj.getSection(Shdr->sh_info);
491 if (!InfoSection)
492 return InfoSection.takeError();
494 auto NameOrErr = getUniquedSectionName(*InfoSection);
495 if (!NameOrErr)
496 return NameOrErr.takeError();
497 S.RelocatableSec = NameOrErr.get();
499 return Error::success();
502 template <class ELFT>
503 Expected<ELFYAML::StackSizesSection *>
504 ELFDumper<ELFT>::dumpStackSizesSection(const Elf_Shdr *Shdr) {
505 auto S = std::make_unique<ELFYAML::StackSizesSection>();
506 if (Error E = dumpCommonSection(Shdr, *S))
507 return std::move(E);
509 auto ContentOrErr = Obj.getSectionContents(Shdr);
510 if (!ContentOrErr)
511 return ContentOrErr.takeError();
513 ArrayRef<uint8_t> Content = *ContentOrErr;
514 DataExtractor Data(Content, Obj.isLE(), ELFT::Is64Bits ? 8 : 4);
516 std::vector<ELFYAML::StackSizeEntry> Entries;
517 DataExtractor::Cursor Cur(0);
518 while (Cur && Cur.tell() < Content.size()) {
519 uint64_t Address = Data.getAddress(Cur);
520 uint64_t Size = Data.getULEB128(Cur);
521 Entries.push_back({Address, Size});
524 if (Content.empty() || !Cur) {
525 // If .stack_sizes cannot be decoded, we dump it as an array of bytes.
526 consumeError(Cur.takeError());
527 S->Content = yaml::BinaryRef(Content);
528 } else {
529 S->Entries = std::move(Entries);
532 return S.release();
535 template <class ELFT>
536 Expected<ELFYAML::AddrsigSection *>
537 ELFDumper<ELFT>::dumpAddrsigSection(const Elf_Shdr *Shdr) {
538 auto S = std::make_unique<ELFYAML::AddrsigSection>();
539 if (Error E = dumpCommonSection(Shdr, *S))
540 return std::move(E);
542 auto ContentOrErr = Obj.getSectionContents(Shdr);
543 if (!ContentOrErr)
544 return ContentOrErr.takeError();
546 ArrayRef<uint8_t> Content = *ContentOrErr;
547 DataExtractor::Cursor Cur(0);
548 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0);
549 std::vector<ELFYAML::AddrsigSymbol> Symbols;
550 while (Cur && Cur.tell() < Content.size()) {
551 uint64_t SymNdx = Data.getULEB128(Cur);
552 if (!Cur)
553 break;
555 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, SymNdx);
556 if (!SymbolName || SymbolName->empty()) {
557 consumeError(SymbolName.takeError());
558 Symbols.emplace_back(SymNdx);
559 continue;
562 Symbols.emplace_back(*SymbolName);
565 if (Cur) {
566 S->Symbols = std::move(Symbols);
567 return S.release();
570 consumeError(Cur.takeError());
571 S->Content = yaml::BinaryRef(Content);
572 return S.release();
575 template <class ELFT>
576 Expected<ELFYAML::DynamicSection *>
577 ELFDumper<ELFT>::dumpDynamicSection(const Elf_Shdr *Shdr) {
578 auto S = std::make_unique<ELFYAML::DynamicSection>();
579 if (Error E = dumpCommonSection(Shdr, *S))
580 return std::move(E);
582 auto DynTagsOrErr = Obj.template getSectionContentsAsArray<Elf_Dyn>(Shdr);
583 if (!DynTagsOrErr)
584 return DynTagsOrErr.takeError();
586 for (const Elf_Dyn &Dyn : *DynTagsOrErr)
587 S->Entries.push_back({(ELFYAML::ELF_DYNTAG)Dyn.getTag(), Dyn.getVal()});
589 return S.release();
592 template <class ELFT>
593 Expected<ELFYAML::RelocationSection *>
594 ELFDumper<ELFT>::dumpRelocSection(const Elf_Shdr *Shdr) {
595 auto S = std::make_unique<ELFYAML::RelocationSection>();
596 if (auto E = dumpCommonRelocationSection(Shdr, *S))
597 return std::move(E);
599 auto SymTabOrErr = Obj.getSection(Shdr->sh_link);
600 if (!SymTabOrErr)
601 return SymTabOrErr.takeError();
602 const Elf_Shdr *SymTab = *SymTabOrErr;
604 if (Shdr->sh_type == ELF::SHT_REL) {
605 auto Rels = Obj.rels(Shdr);
606 if (!Rels)
607 return Rels.takeError();
608 for (const Elf_Rel &Rel : *Rels) {
609 ELFYAML::Relocation R;
610 if (Error E = dumpRelocation(&Rel, SymTab, R))
611 return std::move(E);
612 S->Relocations.push_back(R);
614 } else {
615 auto Rels = Obj.relas(Shdr);
616 if (!Rels)
617 return Rels.takeError();
618 for (const Elf_Rela &Rel : *Rels) {
619 ELFYAML::Relocation R;
620 if (Error E = dumpRelocation(&Rel, SymTab, R))
621 return std::move(E);
622 R.Addend = Rel.r_addend;
623 S->Relocations.push_back(R);
627 return S.release();
630 template <class ELFT>
631 Expected<ELFYAML::RawContentSection *>
632 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
633 auto S = std::make_unique<ELFYAML::RawContentSection>();
634 if (Error E = dumpCommonSection(Shdr, *S))
635 return std::move(E);
637 unsigned SecIndex = Shdr - &Sections[0];
638 if (SecIndex != 0 || Shdr->sh_type != ELF::SHT_NULL) {
639 auto ContentOrErr = Obj.getSectionContents(Shdr);
640 if (!ContentOrErr)
641 return ContentOrErr.takeError();
642 ArrayRef<uint8_t> Content = *ContentOrErr;
643 if (!Content.empty())
644 S->Content = yaml::BinaryRef(Content);
645 } else {
646 S->Size = static_cast<llvm::yaml::Hex64>(Shdr->sh_size);
649 if (Shdr->sh_info)
650 S->Info = static_cast<llvm::yaml::Hex64>(Shdr->sh_info);
651 return S.release();
654 template <class ELFT>
655 Expected<ELFYAML::SymtabShndxSection *>
656 ELFDumper<ELFT>::dumpSymtabShndxSection(const Elf_Shdr *Shdr) {
657 auto S = std::make_unique<ELFYAML::SymtabShndxSection>();
658 if (Error E = dumpCommonSection(Shdr, *S))
659 return std::move(E);
661 auto EntriesOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr);
662 if (!EntriesOrErr)
663 return EntriesOrErr.takeError();
664 for (const Elf_Word &E : *EntriesOrErr)
665 S->Entries.push_back(E);
666 return S.release();
669 template <class ELFT>
670 Expected<ELFYAML::NoBitsSection *>
671 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
672 auto S = std::make_unique<ELFYAML::NoBitsSection>();
673 if (Error E = dumpCommonSection(Shdr, *S))
674 return std::move(E);
675 S->Size = Shdr->sh_size;
677 return S.release();
680 template <class ELFT>
681 Expected<ELFYAML::HashSection *>
682 ELFDumper<ELFT>::dumpHashSection(const Elf_Shdr *Shdr) {
683 auto S = std::make_unique<ELFYAML::HashSection>();
684 if (Error E = dumpCommonSection(Shdr, *S))
685 return std::move(E);
687 auto ContentOrErr = Obj.getSectionContents(Shdr);
688 if (!ContentOrErr)
689 return ContentOrErr.takeError();
691 ArrayRef<uint8_t> Content = *ContentOrErr;
692 if (Content.size() % 4 != 0 || Content.size() < 8) {
693 S->Content = yaml::BinaryRef(Content);
694 return S.release();
697 DataExtractor::Cursor Cur(0);
698 DataExtractor Data(Content, Obj.isLE(), /*AddressSize=*/0);
699 uint32_t NBucket = Data.getU32(Cur);
700 uint32_t NChain = Data.getU32(Cur);
701 if (Content.size() != (2 + NBucket + NChain) * 4) {
702 S->Content = yaml::BinaryRef(Content);
703 if (Cur)
704 return S.release();
705 llvm_unreachable("entries were not read correctly");
708 S->Bucket.emplace(NBucket);
709 for (uint32_t &V : *S->Bucket)
710 V = Data.getU32(Cur);
712 S->Chain.emplace(NChain);
713 for (uint32_t &V : *S->Chain)
714 V = Data.getU32(Cur);
716 if (Cur)
717 return S.release();
718 llvm_unreachable("entries were not read correctly");
721 template <class ELFT>
722 Expected<ELFYAML::VerdefSection *>
723 ELFDumper<ELFT>::dumpVerdefSection(const Elf_Shdr *Shdr) {
724 typedef typename ELFT::Verdef Elf_Verdef;
725 typedef typename ELFT::Verdaux Elf_Verdaux;
727 auto S = std::make_unique<ELFYAML::VerdefSection>();
728 if (Error E = dumpCommonSection(Shdr, *S))
729 return std::move(E);
731 S->Info = Shdr->sh_info;
733 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
734 if (!StringTableShdrOrErr)
735 return StringTableShdrOrErr.takeError();
737 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
738 if (!StringTableOrErr)
739 return StringTableOrErr.takeError();
741 auto Contents = Obj.getSectionContents(Shdr);
742 if (!Contents)
743 return Contents.takeError();
745 llvm::ArrayRef<uint8_t> Data = *Contents;
746 const uint8_t *Buf = Data.data();
747 while (Buf) {
748 const Elf_Verdef *Verdef = reinterpret_cast<const Elf_Verdef *>(Buf);
749 ELFYAML::VerdefEntry Entry;
750 Entry.Version = Verdef->vd_version;
751 Entry.Flags = Verdef->vd_flags;
752 Entry.VersionNdx = Verdef->vd_ndx;
753 Entry.Hash = Verdef->vd_hash;
755 const uint8_t *BufAux = Buf + Verdef->vd_aux;
756 while (BufAux) {
757 const Elf_Verdaux *Verdaux =
758 reinterpret_cast<const Elf_Verdaux *>(BufAux);
759 Entry.VerNames.push_back(
760 StringTableOrErr->drop_front(Verdaux->vda_name).data());
761 BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
764 S->Entries.push_back(Entry);
765 Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
768 return S.release();
771 template <class ELFT>
772 Expected<ELFYAML::SymverSection *>
773 ELFDumper<ELFT>::dumpSymverSection(const Elf_Shdr *Shdr) {
774 typedef typename ELFT::Half Elf_Half;
776 auto S = std::make_unique<ELFYAML::SymverSection>();
777 if (Error E = dumpCommonSection(Shdr, *S))
778 return std::move(E);
780 auto VersionsOrErr = Obj.template getSectionContentsAsArray<Elf_Half>(Shdr);
781 if (!VersionsOrErr)
782 return VersionsOrErr.takeError();
783 for (const Elf_Half &E : *VersionsOrErr)
784 S->Entries.push_back(E);
786 return S.release();
789 template <class ELFT>
790 Expected<ELFYAML::VerneedSection *>
791 ELFDumper<ELFT>::dumpVerneedSection(const Elf_Shdr *Shdr) {
792 typedef typename ELFT::Verneed Elf_Verneed;
793 typedef typename ELFT::Vernaux Elf_Vernaux;
795 auto S = std::make_unique<ELFYAML::VerneedSection>();
796 if (Error E = dumpCommonSection(Shdr, *S))
797 return std::move(E);
799 S->Info = Shdr->sh_info;
801 auto Contents = Obj.getSectionContents(Shdr);
802 if (!Contents)
803 return Contents.takeError();
805 auto StringTableShdrOrErr = Obj.getSection(Shdr->sh_link);
806 if (!StringTableShdrOrErr)
807 return StringTableShdrOrErr.takeError();
809 auto StringTableOrErr = Obj.getStringTable(*StringTableShdrOrErr);
810 if (!StringTableOrErr)
811 return StringTableOrErr.takeError();
813 llvm::ArrayRef<uint8_t> Data = *Contents;
814 const uint8_t *Buf = Data.data();
815 while (Buf) {
816 const Elf_Verneed *Verneed = reinterpret_cast<const Elf_Verneed *>(Buf);
818 ELFYAML::VerneedEntry Entry;
819 Entry.Version = Verneed->vn_version;
820 Entry.File =
821 StringRef(StringTableOrErr->drop_front(Verneed->vn_file).data());
823 const uint8_t *BufAux = Buf + Verneed->vn_aux;
824 while (BufAux) {
825 const Elf_Vernaux *Vernaux =
826 reinterpret_cast<const Elf_Vernaux *>(BufAux);
828 ELFYAML::VernauxEntry Aux;
829 Aux.Hash = Vernaux->vna_hash;
830 Aux.Flags = Vernaux->vna_flags;
831 Aux.Other = Vernaux->vna_other;
832 Aux.Name =
833 StringRef(StringTableOrErr->drop_front(Vernaux->vna_name).data());
835 Entry.AuxV.push_back(Aux);
836 BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr;
839 S->VerneedV.push_back(Entry);
840 Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr;
843 return S.release();
846 template <class ELFT>
847 Expected<StringRef> ELFDumper<ELFT>::getSymbolName(uint32_t SymtabNdx,
848 uint32_t SymbolNdx) {
849 auto SymtabOrErr = Obj.getSection(SymtabNdx);
850 if (!SymtabOrErr)
851 return SymtabOrErr.takeError();
853 const Elf_Shdr *Symtab = *SymtabOrErr;
854 auto SymOrErr = Obj.getSymbol(Symtab, SymbolNdx);
855 if (!SymOrErr)
856 return SymOrErr.takeError();
858 auto StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
859 if (!StrTabOrErr)
860 return StrTabOrErr.takeError();
861 return getUniquedSymbolName(*SymOrErr, *StrTabOrErr, Symtab);
864 template <class ELFT>
865 Expected<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
866 auto S = std::make_unique<ELFYAML::Group>();
867 if (Error E = dumpCommonSection(Shdr, *S))
868 return std::move(E);
870 // Get symbol with index sh_info. This symbol's name is the signature of the group.
871 Expected<StringRef> SymbolName = getSymbolName(Shdr->sh_link, Shdr->sh_info);
872 if (!SymbolName)
873 return SymbolName.takeError();
874 S->Signature = *SymbolName;
876 auto MembersOrErr = Obj.template getSectionContentsAsArray<Elf_Word>(Shdr);
877 if (!MembersOrErr)
878 return MembersOrErr.takeError();
880 for (Elf_Word Member : *MembersOrErr) {
881 if (Member == llvm::ELF::GRP_COMDAT) {
882 S->Members.push_back({"GRP_COMDAT"});
883 continue;
886 auto SHdrOrErr = Obj.getSection(Member);
887 if (!SHdrOrErr)
888 return SHdrOrErr.takeError();
889 auto NameOrErr = getUniquedSectionName(*SHdrOrErr);
890 if (!NameOrErr)
891 return NameOrErr.takeError();
892 S->Members.push_back({*NameOrErr});
894 return S.release();
897 template <class ELFT>
898 Expected<ELFYAML::MipsABIFlags *>
899 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
900 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
901 "Section type is not SHT_MIPS_ABIFLAGS");
902 auto S = std::make_unique<ELFYAML::MipsABIFlags>();
903 if (Error E = dumpCommonSection(Shdr, *S))
904 return std::move(E);
906 auto ContentOrErr = Obj.getSectionContents(Shdr);
907 if (!ContentOrErr)
908 return ContentOrErr.takeError();
910 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
911 ContentOrErr.get().data());
912 S->Version = Flags->version;
913 S->ISALevel = Flags->isa_level;
914 S->ISARevision = Flags->isa_rev;
915 S->GPRSize = Flags->gpr_size;
916 S->CPR1Size = Flags->cpr1_size;
917 S->CPR2Size = Flags->cpr2_size;
918 S->FpABI = Flags->fp_abi;
919 S->ISAExtension = Flags->isa_ext;
920 S->ASEs = Flags->ases;
921 S->Flags1 = Flags->flags1;
922 S->Flags2 = Flags->flags2;
923 return S.release();
926 template <class ELFT>
927 static Error elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) {
928 ELFDumper<ELFT> Dumper(Obj);
929 Expected<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
930 if (!YAMLOrErr)
931 return YAMLOrErr.takeError();
933 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
934 yaml::Output Yout(Out);
935 Yout << *YAML;
937 return Error::success();
940 Error elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
941 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
942 return elf2yaml(Out, *ELFObj->getELFFile());
944 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
945 return elf2yaml(Out, *ELFObj->getELFFile());
947 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
948 return elf2yaml(Out, *ELFObj->getELFFile());
950 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
951 return elf2yaml(Out, *ELFObj->getELFFile());
953 llvm_unreachable("unknown ELF file format");