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;
71 const Elf_Shdr *Symtab = nullptr;
74 for (const Elf_Shdr &Sec : Obj.sections()) {
75 switch (Sec.sh_type) {
79 // Do not dump these sections.
85 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
86 if (std::error_code EC = S.getError())
88 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
92 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
93 if (std::error_code EC = S.getError())
95 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
98 case ELF::SHT_GROUP: {
99 ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
100 if (std::error_code EC = G.getError())
102 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
105 case ELF::SHT_MIPS_ABIFLAGS: {
106 ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec);
107 if (std::error_code EC = G.getError())
109 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
112 case ELF::SHT_NOBITS: {
113 ErrorOr<ELFYAML::NoBitsSection *> S = dumpNoBitsSection(&Sec);
114 if (std::error_code EC = S.getError())
116 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
120 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
121 if (std::error_code EC = S.getError())
123 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
129 ErrorOr<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
130 if (std::error_code EC = StrTableOrErr.getError())
132 StringRef StrTable = *StrTableOrErr;
134 bool IsFirstSym = true;
135 for (const Elf_Sym &Sym : Obj.symbols(Symtab)) {
142 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(&Sym, StrTable, S))
145 switch (Sym.getBinding())
148 Y->Symbols.Local.push_back(S);
150 case ELF::STB_GLOBAL:
151 Y->Symbols.Global.push_back(S);
154 Y->Symbols.Weak.push_back(S);
157 llvm_unreachable("Unknown ELF symbol binding");
164 template <class ELFT>
165 std::error_code ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym,
167 ELFYAML::Symbol &S) {
168 S.Type = Sym->getType();
169 S.Value = Sym->st_value;
170 S.Size = Sym->st_size;
171 S.Other = Sym->st_other;
173 ErrorOr<StringRef> NameOrErr = Sym->getName(StrTable);
174 if (std::error_code EC = NameOrErr.getError())
176 S.Name = NameOrErr.get();
178 ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(Sym);
179 if (std::error_code EC = ShdrOrErr.getError())
181 const Elf_Shdr *Shdr = *ShdrOrErr;
183 return obj2yaml_error::success;
185 NameOrErr = Obj.getSectionName(Shdr);
186 if (std::error_code EC = NameOrErr.getError())
188 S.Section = NameOrErr.get();
190 return obj2yaml_error::success;
193 template <class ELFT>
194 template <class RelT>
195 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
197 ELFYAML::Relocation &R) {
198 R.Type = Rel->getType(Obj.isMips64EL());
199 R.Offset = Rel->r_offset;
202 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
204 return obj2yaml_error::success;
206 const Elf_Shdr *SymTab = NamePair.first;
207 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
208 if (std::error_code EC = StrTabSec.getError())
210 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
211 if (std::error_code EC = StrTabOrErr.getError())
213 StringRef StrTab = *StrTabOrErr;
215 ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab);
216 if (std::error_code EC = NameOrErr.getError())
218 R.Symbol = NameOrErr.get();
220 return obj2yaml_error::success;
223 template <class ELFT>
224 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
225 ELFYAML::Section &S) {
226 S.Type = Shdr->sh_type;
227 S.Flags = Shdr->sh_flags;
228 S.Address = Shdr->sh_addr;
229 S.AddressAlign = Shdr->sh_addralign;
231 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
232 if (std::error_code EC = NameOrErr.getError())
234 S.Name = NameOrErr.get();
236 if (Shdr->sh_link != ELF::SHN_UNDEF) {
237 ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
238 if (std::error_code EC = LinkSection.getError())
240 NameOrErr = Obj.getSectionName(*LinkSection);
241 if (std::error_code EC = NameOrErr.getError())
243 S.Link = NameOrErr.get();
246 return obj2yaml_error::success;
249 template <class ELFT>
251 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
252 ELFYAML::RelocationSection &S) {
253 if (std::error_code EC = dumpCommonSection(Shdr, S))
256 ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
257 if (std::error_code EC = InfoSection.getError())
260 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
261 if (std::error_code EC = NameOrErr.getError())
263 S.Info = NameOrErr.get();
265 return obj2yaml_error::success;
268 template <class ELFT>
269 ErrorOr<ELFYAML::RelocationSection *>
270 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
271 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
272 auto S = make_unique<ELFYAML::RelocationSection>();
274 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
277 for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
278 ELFYAML::Relocation R;
279 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
281 S->Relocations.push_back(R);
287 template <class ELFT>
288 ErrorOr<ELFYAML::RelocationSection *>
289 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
290 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
291 auto S = make_unique<ELFYAML::RelocationSection>();
293 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
296 for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
298 ELFYAML::Relocation R;
299 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
301 R.Addend = RI->r_addend;
302 S->Relocations.push_back(R);
308 template <class ELFT>
309 ErrorOr<ELFYAML::RawContentSection *>
310 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
311 auto S = make_unique<ELFYAML::RawContentSection>();
313 if (std::error_code EC = dumpCommonSection(Shdr, *S))
316 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
317 if (std::error_code EC = ContentOrErr.getError())
319 S->Content = yaml::BinaryRef(ContentOrErr.get());
320 S->Size = S->Content.binary_size();
325 template <class ELFT>
326 ErrorOr<ELFYAML::NoBitsSection *>
327 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
328 auto S = make_unique<ELFYAML::NoBitsSection>();
330 if (std::error_code EC = dumpCommonSection(Shdr, *S))
332 S->Size = Shdr->sh_size;
337 template <class ELFT>
338 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
339 auto S = make_unique<ELFYAML::Group>();
341 if (std::error_code EC = dumpCommonSection(Shdr, *S))
343 // Get sh_info which is the signature.
344 ErrorOr<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Shdr->sh_link);
345 if (std::error_code EC = SymtabOrErr.getError())
347 const Elf_Shdr *Symtab = *SymtabOrErr;
348 const Elf_Sym *symbol = Obj.getSymbol(Symtab, Shdr->sh_info);
349 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
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;