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;
30 std::error_code dumpSymbol(const Elf_Sym *Sym, StringRef StrTable,
32 std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
33 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
34 ELFYAML::RelocationSection &S);
36 std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
37 ELFYAML::Relocation &R);
39 ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
40 ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
41 ErrorOr<ELFYAML::RawContentSection *>
42 dumpContentSection(const Elf_Shdr *Shdr);
43 ErrorOr<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
44 ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
45 ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
48 ELFDumper(const object::ELFFile<ELFT> &O);
49 ErrorOr<ELFYAML::Object *> dump();
55 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
59 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
60 auto Y = make_unique<ELFYAML::Object>();
63 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
64 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
65 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
66 Y->Header.Type = Obj.getHeader()->e_type;
67 Y->Header.Machine = Obj.getHeader()->e_machine;
68 Y->Header.Flags = Obj.getHeader()->e_flags;
69 Y->Header.Entry = Obj.getHeader()->e_entry;
72 for (const Elf_Shdr &Sec : Obj.sections()) {
73 switch (Sec.sh_type) {
78 // Do not dump these sections.
81 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
82 if (std::error_code EC = S.getError())
84 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
88 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
89 if (std::error_code EC = S.getError())
91 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
94 case ELF::SHT_GROUP: {
95 ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
96 if (std::error_code EC = G.getError())
98 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
101 case ELF::SHT_MIPS_ABIFLAGS: {
102 ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec);
103 if (std::error_code EC = G.getError())
105 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
108 case ELF::SHT_NOBITS: {
109 ErrorOr<ELFYAML::NoBitsSection *> S = dumpNoBitsSection(&Sec);
110 if (std::error_code EC = S.getError())
112 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
116 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
117 if (std::error_code EC = S.getError())
119 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
125 const Elf_Shdr *Symtab = Obj.getDotSymtabSec();
126 ErrorOr<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
127 if (std::error_code EC = StrTableOrErr.getError())
129 StringRef StrTable = *StrTableOrErr;
131 bool IsFirstSym = true;
132 for (const Elf_Sym &Sym : Obj.symbols()) {
139 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(&Sym, StrTable, S))
142 switch (Sym.getBinding())
145 Y->Symbols.Local.push_back(S);
147 case ELF::STB_GLOBAL:
148 Y->Symbols.Global.push_back(S);
151 Y->Symbols.Weak.push_back(S);
154 llvm_unreachable("Unknown ELF symbol binding");
161 template <class ELFT>
162 std::error_code ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym,
164 ELFYAML::Symbol &S) {
165 S.Type = Sym->getType();
166 S.Value = Sym->st_value;
167 S.Size = Sym->st_size;
168 S.Other = Sym->st_other;
170 ErrorOr<StringRef> NameOrErr = Sym->getName(StrTable);
171 if (std::error_code EC = NameOrErr.getError())
173 S.Name = NameOrErr.get();
175 ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(&*Sym);
176 if (std::error_code EC = ShdrOrErr.getError())
178 const Elf_Shdr *Shdr = *ShdrOrErr;
180 return obj2yaml_error::success;
182 NameOrErr = Obj.getSectionName(Shdr);
183 if (std::error_code EC = NameOrErr.getError())
185 S.Section = NameOrErr.get();
187 return obj2yaml_error::success;
190 template <class ELFT>
191 template <class RelT>
192 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
194 ELFYAML::Relocation &R) {
195 R.Type = Rel->getType(Obj.isMips64EL());
196 R.Offset = Rel->r_offset;
199 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
201 return obj2yaml_error::success;
203 const Elf_Shdr *SymTab = NamePair.first;
204 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
205 if (std::error_code EC = StrTabSec.getError())
207 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
208 if (std::error_code EC = StrTabOrErr.getError())
210 StringRef StrTab = *StrTabOrErr;
212 ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab);
213 if (std::error_code EC = NameOrErr.getError())
215 R.Symbol = NameOrErr.get();
217 return obj2yaml_error::success;
220 template <class ELFT>
221 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
222 ELFYAML::Section &S) {
223 S.Type = Shdr->sh_type;
224 S.Flags = Shdr->sh_flags;
225 S.Address = Shdr->sh_addr;
226 S.AddressAlign = Shdr->sh_addralign;
228 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
229 if (std::error_code EC = NameOrErr.getError())
231 S.Name = NameOrErr.get();
233 if (Shdr->sh_link != ELF::SHN_UNDEF) {
234 ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
235 if (std::error_code EC = LinkSection.getError())
237 NameOrErr = Obj.getSectionName(*LinkSection);
238 if (std::error_code EC = NameOrErr.getError())
240 S.Link = NameOrErr.get();
243 return obj2yaml_error::success;
246 template <class ELFT>
248 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
249 ELFYAML::RelocationSection &S) {
250 if (std::error_code EC = dumpCommonSection(Shdr, S))
253 ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
254 if (std::error_code EC = InfoSection.getError())
257 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
258 if (std::error_code EC = NameOrErr.getError())
260 S.Info = NameOrErr.get();
262 return obj2yaml_error::success;
265 template <class ELFT>
266 ErrorOr<ELFYAML::RelocationSection *>
267 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
268 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
269 auto S = make_unique<ELFYAML::RelocationSection>();
271 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
274 for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
275 ELFYAML::Relocation R;
276 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
278 S->Relocations.push_back(R);
284 template <class ELFT>
285 ErrorOr<ELFYAML::RelocationSection *>
286 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
287 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
288 auto S = make_unique<ELFYAML::RelocationSection>();
290 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
293 for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
295 ELFYAML::Relocation R;
296 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
298 R.Addend = RI->r_addend;
299 S->Relocations.push_back(R);
305 template <class ELFT>
306 ErrorOr<ELFYAML::RawContentSection *>
307 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
308 auto S = make_unique<ELFYAML::RawContentSection>();
310 if (std::error_code EC = dumpCommonSection(Shdr, *S))
313 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
314 if (std::error_code EC = ContentOrErr.getError())
316 S->Content = yaml::BinaryRef(ContentOrErr.get());
317 S->Size = S->Content.binary_size();
322 template <class ELFT>
323 ErrorOr<ELFYAML::NoBitsSection *>
324 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
325 auto S = make_unique<ELFYAML::NoBitsSection>();
327 if (std::error_code EC = dumpCommonSection(Shdr, *S))
329 S->Size = Shdr->sh_size;
334 template <class ELFT>
335 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
336 auto S = make_unique<ELFYAML::Group>();
338 if (std::error_code EC = dumpCommonSection(Shdr, *S))
340 // Get sh_info which is the signature.
341 ErrorOr<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Shdr->sh_link);
342 if (std::error_code EC = SymtabOrErr.getError())
344 const Elf_Shdr *Symtab = *SymtabOrErr;
345 const Elf_Sym *symbol = Obj.getSymbol(Symtab, Shdr->sh_info);
346 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(Symtab->sh_link);
347 if (std::error_code EC = StrTabSec.getError())
349 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
350 if (std::error_code EC = StrTabOrErr.getError())
352 StringRef StrTab = *StrTabOrErr;
353 auto sectionContents = Obj.getSectionContents(Shdr);
354 if (std::error_code ec = sectionContents.getError())
356 ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
357 if (std::error_code EC = symbolName.getError())
359 S->Info = *symbolName;
360 const Elf_Word *groupMembers =
361 reinterpret_cast<const Elf_Word *>(sectionContents->data());
362 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
363 ELFYAML::SectionOrType s;
364 for (int i = 0; i < count; i++) {
365 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
366 s.sectionNameOrType = "GRP_COMDAT";
368 ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
369 if (std::error_code EC = sHdr.getError())
371 ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
372 if (std::error_code ec = sectionName.getError())
374 s.sectionNameOrType = *sectionName;
376 S->Members.push_back(s);
381 template <class ELFT>
382 ErrorOr<ELFYAML::MipsABIFlags *>
383 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
384 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
385 "Section type is not SHT_MIPS_ABIFLAGS");
386 auto S = make_unique<ELFYAML::MipsABIFlags>();
387 if (std::error_code EC = dumpCommonSection(Shdr, *S))
390 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
391 if (std::error_code EC = ContentOrErr.getError())
394 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
395 ContentOrErr.get().data());
396 S->Version = Flags->version;
397 S->ISALevel = Flags->isa_level;
398 S->ISARevision = Flags->isa_rev;
399 S->GPRSize = Flags->gpr_size;
400 S->CPR1Size = Flags->cpr1_size;
401 S->CPR2Size = Flags->cpr2_size;
402 S->FpABI = Flags->fp_abi;
403 S->ISAExtension = Flags->isa_ext;
404 S->ASEs = Flags->ases;
405 S->Flags1 = Flags->flags1;
406 S->Flags2 = Flags->flags2;
410 template <class ELFT>
411 static std::error_code elf2yaml(raw_ostream &Out,
412 const object::ELFFile<ELFT> &Obj) {
413 ELFDumper<ELFT> Dumper(Obj);
414 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
415 if (std::error_code EC = YAMLOrErr.getError())
418 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
419 yaml::Output Yout(Out);
422 return std::error_code();
425 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
426 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
427 return elf2yaml(Out, *ELFObj->getELFFile());
429 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
430 return elf2yaml(Out, *ELFObj->getELFFile());
432 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
433 return elf2yaml(Out, *ELFObj->getELFFile());
435 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
436 return elf2yaml(Out, *ELFObj->getELFFile());
438 return obj2yaml_error::unsupported_obj_file_format;