1 //===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/Object/ELFObjectFile.h"
14 #include "llvm/Object/ELFYAML.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/YAMLTraits.h"
24 typedef object::Elf_Sym_Impl<ELFT> Elf_Sym;
25 typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
26 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
28 const object::ELFFile<ELFT> &Obj;
29 ArrayRef<Elf_Word> ShndxTable;
31 std::error_code dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
32 StringRef StrTable, ELFYAML::Symbol &S);
33 std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
34 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
35 ELFYAML::RelocationSection &S);
37 std::error_code dumpRelocation(const RelT *Rel, const Elf_Shdr *SymTab,
38 ELFYAML::Relocation &R);
40 ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
41 ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
42 ErrorOr<ELFYAML::RawContentSection *>
43 dumpContentSection(const Elf_Shdr *Shdr);
44 ErrorOr<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
45 ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
46 ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
49 ELFDumper(const object::ELFFile<ELFT> &O);
50 ErrorOr<ELFYAML::Object *> dump();
56 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
60 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
61 auto Y = make_unique<ELFYAML::Object>();
64 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
65 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
66 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
67 Y->Header.Type = Obj.getHeader()->e_type;
68 Y->Header.Machine = Obj.getHeader()->e_machine;
69 Y->Header.Flags = Obj.getHeader()->e_flags;
70 Y->Header.Entry = Obj.getHeader()->e_entry;
72 const Elf_Shdr *Symtab = nullptr;
75 for (const Elf_Shdr &Sec : Obj.sections()) {
76 switch (Sec.sh_type) {
80 // Do not dump these sections.
85 case ELF::SHT_SYMTAB_SHNDX: {
86 ErrorOr<ArrayRef<Elf_Word>> TableOrErr = Obj.getSHNDXTable(Sec);
87 if (std::error_code EC = TableOrErr.getError())
89 ShndxTable = *TableOrErr;
93 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
94 if (std::error_code EC = S.getError())
96 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
100 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
101 if (std::error_code EC = S.getError())
103 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
106 case ELF::SHT_GROUP: {
107 ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
108 if (std::error_code EC = G.getError())
110 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
113 case ELF::SHT_MIPS_ABIFLAGS: {
114 ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec);
115 if (std::error_code EC = G.getError())
117 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
120 case ELF::SHT_NOBITS: {
121 ErrorOr<ELFYAML::NoBitsSection *> S = dumpNoBitsSection(&Sec);
122 if (std::error_code EC = S.getError())
124 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
128 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
129 if (std::error_code EC = S.getError())
131 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
137 ErrorOr<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
138 if (std::error_code EC = StrTableOrErr.getError())
140 StringRef StrTable = *StrTableOrErr;
142 bool IsFirstSym = true;
143 for (const Elf_Sym &Sym : Obj.symbols(Symtab)) {
150 if (std::error_code EC =
151 ELFDumper<ELFT>::dumpSymbol(&Sym, Symtab, StrTable, S))
154 switch (Sym.getBinding())
157 Y->Symbols.Local.push_back(S);
159 case ELF::STB_GLOBAL:
160 Y->Symbols.Global.push_back(S);
163 Y->Symbols.Weak.push_back(S);
166 llvm_unreachable("Unknown ELF symbol binding");
173 template <class ELFT>
175 ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, const Elf_Shdr *SymTab,
176 StringRef StrTable, ELFYAML::Symbol &S) {
177 S.Type = Sym->getType();
178 S.Value = Sym->st_value;
179 S.Size = Sym->st_size;
180 S.Other = Sym->st_other;
182 ErrorOr<StringRef> NameOrErr = Sym->getName(StrTable);
183 if (std::error_code EC = NameOrErr.getError())
185 S.Name = NameOrErr.get();
187 ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(Sym, SymTab, ShndxTable);
188 if (std::error_code EC = ShdrOrErr.getError())
190 const Elf_Shdr *Shdr = *ShdrOrErr;
192 return obj2yaml_error::success;
194 NameOrErr = Obj.getSectionName(Shdr);
195 if (std::error_code EC = NameOrErr.getError())
197 S.Section = NameOrErr.get();
199 return obj2yaml_error::success;
202 template <class ELFT>
203 template <class RelT>
204 std::error_code ELFDumper<ELFT>::dumpRelocation(const RelT *Rel,
205 const Elf_Shdr *SymTab,
206 ELFYAML::Relocation &R) {
207 R.Type = Rel->getType(Obj.isMips64EL());
208 R.Offset = Rel->r_offset;
211 const Elf_Sym *Sym = Obj.getRelocationSymbol(Rel, SymTab);
212 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
213 if (std::error_code EC = StrTabSec.getError())
215 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
216 if (std::error_code EC = StrTabOrErr.getError())
218 StringRef StrTab = *StrTabOrErr;
220 ErrorOr<StringRef> NameOrErr = Sym->getName(StrTab);
221 if (std::error_code EC = NameOrErr.getError())
223 R.Symbol = NameOrErr.get();
225 return obj2yaml_error::success;
228 template <class ELFT>
229 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
230 ELFYAML::Section &S) {
231 S.Type = Shdr->sh_type;
232 S.Flags = Shdr->sh_flags;
233 S.Address = Shdr->sh_addr;
234 S.AddressAlign = Shdr->sh_addralign;
236 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
237 if (std::error_code EC = NameOrErr.getError())
239 S.Name = NameOrErr.get();
241 if (Shdr->sh_link != ELF::SHN_UNDEF) {
242 ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
243 if (std::error_code EC = LinkSection.getError())
245 NameOrErr = Obj.getSectionName(*LinkSection);
246 if (std::error_code EC = NameOrErr.getError())
248 S.Link = NameOrErr.get();
251 return obj2yaml_error::success;
254 template <class ELFT>
256 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
257 ELFYAML::RelocationSection &S) {
258 if (std::error_code EC = dumpCommonSection(Shdr, S))
261 ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
262 if (std::error_code EC = InfoSection.getError())
265 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
266 if (std::error_code EC = NameOrErr.getError())
268 S.Info = NameOrErr.get();
270 return obj2yaml_error::success;
273 template <class ELFT>
274 ErrorOr<ELFYAML::RelocationSection *>
275 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
276 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
277 auto S = make_unique<ELFYAML::RelocationSection>();
279 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
282 ErrorOr<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Shdr->sh_link);
283 if (std::error_code EC = SymTabOrErr.getError())
285 const Elf_Shdr *SymTab = *SymTabOrErr;
287 for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
288 ELFYAML::Relocation R;
289 if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
291 S->Relocations.push_back(R);
297 template <class ELFT>
298 ErrorOr<ELFYAML::RelocationSection *>
299 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
300 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
301 auto S = make_unique<ELFYAML::RelocationSection>();
303 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
306 ErrorOr<const Elf_Shdr *> SymTabOrErr = Obj.getSection(Shdr->sh_link);
307 if (std::error_code EC = SymTabOrErr.getError())
309 const Elf_Shdr *SymTab = *SymTabOrErr;
311 for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
313 ELFYAML::Relocation R;
314 if (std::error_code EC = dumpRelocation(&*RI, SymTab, R))
316 R.Addend = RI->r_addend;
317 S->Relocations.push_back(R);
323 template <class ELFT>
324 ErrorOr<ELFYAML::RawContentSection *>
325 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
326 auto S = make_unique<ELFYAML::RawContentSection>();
328 if (std::error_code EC = dumpCommonSection(Shdr, *S))
331 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
332 if (std::error_code EC = ContentOrErr.getError())
334 S->Content = yaml::BinaryRef(ContentOrErr.get());
335 S->Size = S->Content.binary_size();
340 template <class ELFT>
341 ErrorOr<ELFYAML::NoBitsSection *>
342 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
343 auto S = make_unique<ELFYAML::NoBitsSection>();
345 if (std::error_code EC = dumpCommonSection(Shdr, *S))
347 S->Size = Shdr->sh_size;
352 template <class ELFT>
353 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
354 auto S = make_unique<ELFYAML::Group>();
356 if (std::error_code EC = dumpCommonSection(Shdr, *S))
358 // Get sh_info which is the signature.
359 ErrorOr<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Shdr->sh_link);
360 if (std::error_code EC = SymtabOrErr.getError())
362 const Elf_Shdr *Symtab = *SymtabOrErr;
363 const Elf_Sym *symbol = Obj.getSymbol(Symtab, Shdr->sh_info);
364 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
365 if (std::error_code EC = StrTabOrErr.getError())
367 StringRef StrTab = *StrTabOrErr;
368 auto sectionContents = Obj.getSectionContents(Shdr);
369 if (std::error_code ec = sectionContents.getError())
371 ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
372 if (std::error_code EC = symbolName.getError())
374 S->Info = *symbolName;
375 const Elf_Word *groupMembers =
376 reinterpret_cast<const Elf_Word *>(sectionContents->data());
377 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
378 ELFYAML::SectionOrType s;
379 for (int i = 0; i < count; i++) {
380 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
381 s.sectionNameOrType = "GRP_COMDAT";
383 ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
384 if (std::error_code EC = sHdr.getError())
386 ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
387 if (std::error_code ec = sectionName.getError())
389 s.sectionNameOrType = *sectionName;
391 S->Members.push_back(s);
396 template <class ELFT>
397 ErrorOr<ELFYAML::MipsABIFlags *>
398 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
399 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
400 "Section type is not SHT_MIPS_ABIFLAGS");
401 auto S = make_unique<ELFYAML::MipsABIFlags>();
402 if (std::error_code EC = dumpCommonSection(Shdr, *S))
405 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
406 if (std::error_code EC = ContentOrErr.getError())
409 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
410 ContentOrErr.get().data());
411 S->Version = Flags->version;
412 S->ISALevel = Flags->isa_level;
413 S->ISARevision = Flags->isa_rev;
414 S->GPRSize = Flags->gpr_size;
415 S->CPR1Size = Flags->cpr1_size;
416 S->CPR2Size = Flags->cpr2_size;
417 S->FpABI = Flags->fp_abi;
418 S->ISAExtension = Flags->isa_ext;
419 S->ASEs = Flags->ases;
420 S->Flags1 = Flags->flags1;
421 S->Flags2 = Flags->flags2;
425 template <class ELFT>
426 static std::error_code elf2yaml(raw_ostream &Out,
427 const object::ELFFile<ELFT> &Obj) {
428 ELFDumper<ELFT> Dumper(Obj);
429 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
430 if (std::error_code EC = YAMLOrErr.getError())
433 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
434 yaml::Output Yout(Out);
437 return std::error_code();
440 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
441 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
442 return elf2yaml(Out, *ELFObj->getELFFile());
444 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
445 return elf2yaml(Out, *ELFObj->getELFFile());
447 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
448 return elf2yaml(Out, *ELFObj->getELFFile());
450 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
451 return elf2yaml(Out, *ELFObj->getELFFile());
453 return obj2yaml_error::unsupported_obj_file_format;