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 Elf_Shdr *Shdr, const RelT *Rel,
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 Elf_Shdr *Shdr,
206 ELFYAML::Relocation &R) {
207 R.Type = Rel->getType(Obj.isMips64EL());
208 R.Offset = Rel->r_offset;
211 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
213 return obj2yaml_error::success;
215 const Elf_Shdr *SymTab = NamePair.first;
216 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
217 if (std::error_code EC = StrTabSec.getError())
219 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
220 if (std::error_code EC = StrTabOrErr.getError())
222 StringRef StrTab = *StrTabOrErr;
224 ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab);
225 if (std::error_code EC = NameOrErr.getError())
227 R.Symbol = NameOrErr.get();
229 return obj2yaml_error::success;
232 template <class ELFT>
233 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
234 ELFYAML::Section &S) {
235 S.Type = Shdr->sh_type;
236 S.Flags = Shdr->sh_flags;
237 S.Address = Shdr->sh_addr;
238 S.AddressAlign = Shdr->sh_addralign;
240 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
241 if (std::error_code EC = NameOrErr.getError())
243 S.Name = NameOrErr.get();
245 if (Shdr->sh_link != ELF::SHN_UNDEF) {
246 ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
247 if (std::error_code EC = LinkSection.getError())
249 NameOrErr = Obj.getSectionName(*LinkSection);
250 if (std::error_code EC = NameOrErr.getError())
252 S.Link = NameOrErr.get();
255 return obj2yaml_error::success;
258 template <class ELFT>
260 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
261 ELFYAML::RelocationSection &S) {
262 if (std::error_code EC = dumpCommonSection(Shdr, S))
265 ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
266 if (std::error_code EC = InfoSection.getError())
269 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
270 if (std::error_code EC = NameOrErr.getError())
272 S.Info = NameOrErr.get();
274 return obj2yaml_error::success;
277 template <class ELFT>
278 ErrorOr<ELFYAML::RelocationSection *>
279 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
280 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
281 auto S = make_unique<ELFYAML::RelocationSection>();
283 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
286 for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
287 ELFYAML::Relocation R;
288 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
290 S->Relocations.push_back(R);
296 template <class ELFT>
297 ErrorOr<ELFYAML::RelocationSection *>
298 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
299 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
300 auto S = make_unique<ELFYAML::RelocationSection>();
302 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
305 for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
307 ELFYAML::Relocation R;
308 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
310 R.Addend = RI->r_addend;
311 S->Relocations.push_back(R);
317 template <class ELFT>
318 ErrorOr<ELFYAML::RawContentSection *>
319 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
320 auto S = make_unique<ELFYAML::RawContentSection>();
322 if (std::error_code EC = dumpCommonSection(Shdr, *S))
325 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
326 if (std::error_code EC = ContentOrErr.getError())
328 S->Content = yaml::BinaryRef(ContentOrErr.get());
329 S->Size = S->Content.binary_size();
334 template <class ELFT>
335 ErrorOr<ELFYAML::NoBitsSection *>
336 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
337 auto S = make_unique<ELFYAML::NoBitsSection>();
339 if (std::error_code EC = dumpCommonSection(Shdr, *S))
341 S->Size = Shdr->sh_size;
346 template <class ELFT>
347 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
348 auto S = make_unique<ELFYAML::Group>();
350 if (std::error_code EC = dumpCommonSection(Shdr, *S))
352 // Get sh_info which is the signature.
353 ErrorOr<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Shdr->sh_link);
354 if (std::error_code EC = SymtabOrErr.getError())
356 const Elf_Shdr *Symtab = *SymtabOrErr;
357 const Elf_Sym *symbol = Obj.getSymbol(Symtab, Shdr->sh_info);
358 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
359 if (std::error_code EC = StrTabOrErr.getError())
361 StringRef StrTab = *StrTabOrErr;
362 auto sectionContents = Obj.getSectionContents(Shdr);
363 if (std::error_code ec = sectionContents.getError())
365 ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
366 if (std::error_code EC = symbolName.getError())
368 S->Info = *symbolName;
369 const Elf_Word *groupMembers =
370 reinterpret_cast<const Elf_Word *>(sectionContents->data());
371 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
372 ELFYAML::SectionOrType s;
373 for (int i = 0; i < count; i++) {
374 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
375 s.sectionNameOrType = "GRP_COMDAT";
377 ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
378 if (std::error_code EC = sHdr.getError())
380 ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
381 if (std::error_code ec = sectionName.getError())
383 s.sectionNameOrType = *sectionName;
385 S->Members.push_back(s);
390 template <class ELFT>
391 ErrorOr<ELFYAML::MipsABIFlags *>
392 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
393 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
394 "Section type is not SHT_MIPS_ABIFLAGS");
395 auto S = make_unique<ELFYAML::MipsABIFlags>();
396 if (std::error_code EC = dumpCommonSection(Shdr, *S))
399 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
400 if (std::error_code EC = ContentOrErr.getError())
403 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
404 ContentOrErr.get().data());
405 S->Version = Flags->version;
406 S->ISALevel = Flags->isa_level;
407 S->ISARevision = Flags->isa_rev;
408 S->GPRSize = Flags->gpr_size;
409 S->CPR1Size = Flags->cpr1_size;
410 S->CPR2Size = Flags->cpr2_size;
411 S->FpABI = Flags->fp_abi;
412 S->ISAExtension = Flags->isa_ext;
413 S->ASEs = Flags->ases;
414 S->Flags1 = Flags->flags1;
415 S->Flags2 = Flags->flags2;
419 template <class ELFT>
420 static std::error_code elf2yaml(raw_ostream &Out,
421 const object::ELFFile<ELFT> &Obj) {
422 ELFDumper<ELFT> Dumper(Obj);
423 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
424 if (std::error_code EC = YAMLOrErr.getError())
427 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
428 yaml::Output Yout(Out);
431 return std::error_code();
434 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
435 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
436 return elf2yaml(Out, *ELFObj->getELFFile());
438 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
439 return elf2yaml(Out, *ELFObj->getELFFile());
441 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
442 return elf2yaml(Out, *ELFObj->getELFFile());
444 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
445 return elf2yaml(Out, *ELFObj->getELFFile());
447 return obj2yaml_error::unsupported_obj_file_format;