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, ELFYAML::Symbol &S);
31 std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
32 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
33 ELFYAML::RelocationSection &S);
35 std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
36 ELFYAML::Relocation &R);
38 ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
39 ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
40 ErrorOr<ELFYAML::RawContentSection *>
41 dumpContentSection(const Elf_Shdr *Shdr);
42 ErrorOr<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
43 ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
44 ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
47 ELFDumper(const object::ELFFile<ELFT> &O);
48 ErrorOr<ELFYAML::Object *> dump();
54 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
58 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
59 auto Y = make_unique<ELFYAML::Object>();
62 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
63 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
64 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
65 Y->Header.Type = Obj.getHeader()->e_type;
66 Y->Header.Machine = Obj.getHeader()->e_machine;
67 Y->Header.Flags = Obj.getHeader()->e_flags;
68 Y->Header.Entry = Obj.getHeader()->e_entry;
71 for (const Elf_Shdr &Sec : Obj.sections()) {
72 switch (Sec.sh_type) {
77 // Do not dump these sections.
80 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
81 if (std::error_code EC = S.getError())
83 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
87 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
88 if (std::error_code EC = S.getError())
90 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
93 case ELF::SHT_GROUP: {
94 ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
95 if (std::error_code EC = G.getError())
97 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
100 case ELF::SHT_MIPS_ABIFLAGS: {
101 ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec);
102 if (std::error_code EC = G.getError())
104 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
107 case ELF::SHT_NOBITS: {
108 ErrorOr<ELFYAML::NoBitsSection *> S = dumpNoBitsSection(&Sec);
109 if (std::error_code EC = S.getError())
111 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
115 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
116 if (std::error_code EC = S.getError())
118 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
124 bool IsFirstSym = true;
125 for (const Elf_Sym &Sym : Obj.symbols()) {
132 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(&Sym, S))
135 switch (Sym.getBinding())
138 Y->Symbols.Local.push_back(S);
140 case ELF::STB_GLOBAL:
141 Y->Symbols.Global.push_back(S);
144 Y->Symbols.Weak.push_back(S);
147 llvm_unreachable("Unknown ELF symbol binding");
154 template <class ELFT>
155 std::error_code ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym,
156 ELFYAML::Symbol &S) {
157 S.Type = Sym->getType();
158 S.Value = Sym->st_value;
159 S.Size = Sym->st_size;
160 S.Other = Sym->st_other;
162 ErrorOr<StringRef> NameOrErr = Obj.getStaticSymbolName(Sym);
163 if (std::error_code EC = NameOrErr.getError())
165 S.Name = NameOrErr.get();
167 ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(&*Sym);
168 if (std::error_code EC = ShdrOrErr.getError())
170 const Elf_Shdr *Shdr = *ShdrOrErr;
172 return obj2yaml_error::success;
174 NameOrErr = Obj.getSectionName(Shdr);
175 if (std::error_code EC = NameOrErr.getError())
177 S.Section = NameOrErr.get();
179 return obj2yaml_error::success;
182 template <class ELFT>
183 template <class RelT>
184 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
186 ELFYAML::Relocation &R) {
187 R.Type = Rel->getType(Obj.isMips64EL());
188 R.Offset = Rel->r_offset;
191 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
193 return obj2yaml_error::success;
195 const Elf_Shdr *SymTab = NamePair.first;
196 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
197 if (std::error_code EC = StrTabSec.getError())
199 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
200 if (std::error_code EC = StrTabOrErr.getError())
202 StringRef StrTab = *StrTabOrErr;
204 ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab);
205 if (std::error_code EC = NameOrErr.getError())
207 R.Symbol = NameOrErr.get();
209 return obj2yaml_error::success;
212 template <class ELFT>
213 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
214 ELFYAML::Section &S) {
215 S.Type = Shdr->sh_type;
216 S.Flags = Shdr->sh_flags;
217 S.Address = Shdr->sh_addr;
218 S.AddressAlign = Shdr->sh_addralign;
220 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
221 if (std::error_code EC = NameOrErr.getError())
223 S.Name = NameOrErr.get();
225 if (Shdr->sh_link != ELF::SHN_UNDEF) {
226 ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
227 if (std::error_code EC = LinkSection.getError())
229 NameOrErr = Obj.getSectionName(*LinkSection);
230 if (std::error_code EC = NameOrErr.getError())
232 S.Link = NameOrErr.get();
235 return obj2yaml_error::success;
238 template <class ELFT>
240 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
241 ELFYAML::RelocationSection &S) {
242 if (std::error_code EC = dumpCommonSection(Shdr, S))
245 ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
246 if (std::error_code EC = InfoSection.getError())
249 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
250 if (std::error_code EC = NameOrErr.getError())
252 S.Info = NameOrErr.get();
254 return obj2yaml_error::success;
257 template <class ELFT>
258 ErrorOr<ELFYAML::RelocationSection *>
259 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
260 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
261 auto S = make_unique<ELFYAML::RelocationSection>();
263 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
266 for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
267 ELFYAML::Relocation R;
268 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
270 S->Relocations.push_back(R);
276 template <class ELFT>
277 ErrorOr<ELFYAML::RelocationSection *>
278 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
279 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
280 auto S = make_unique<ELFYAML::RelocationSection>();
282 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
285 for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
287 ELFYAML::Relocation R;
288 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
290 R.Addend = RI->r_addend;
291 S->Relocations.push_back(R);
297 template <class ELFT>
298 ErrorOr<ELFYAML::RawContentSection *>
299 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
300 auto S = make_unique<ELFYAML::RawContentSection>();
302 if (std::error_code EC = dumpCommonSection(Shdr, *S))
305 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
306 if (std::error_code EC = ContentOrErr.getError())
308 S->Content = yaml::BinaryRef(ContentOrErr.get());
309 S->Size = S->Content.binary_size();
314 template <class ELFT>
315 ErrorOr<ELFYAML::NoBitsSection *>
316 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
317 auto S = make_unique<ELFYAML::NoBitsSection>();
319 if (std::error_code EC = dumpCommonSection(Shdr, *S))
321 S->Size = Shdr->sh_size;
326 template <class ELFT>
327 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
328 auto S = make_unique<ELFYAML::Group>();
330 if (std::error_code EC = dumpCommonSection(Shdr, *S))
332 // Get sh_info which is the signature.
333 const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
334 ErrorOr<const Elf_Shdr *> Symtab = Obj.getSection(Shdr->sh_link);
335 if (std::error_code EC = Symtab.getError())
337 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection((*Symtab)->sh_link);
338 if (std::error_code EC = StrTabSec.getError())
340 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
341 if (std::error_code EC = StrTabOrErr.getError())
343 StringRef StrTab = *StrTabOrErr;
344 auto sectionContents = Obj.getSectionContents(Shdr);
345 if (std::error_code ec = sectionContents.getError())
347 ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
348 if (std::error_code EC = symbolName.getError())
350 S->Info = *symbolName;
351 const Elf_Word *groupMembers =
352 reinterpret_cast<const Elf_Word *>(sectionContents->data());
353 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
354 ELFYAML::SectionOrType s;
355 for (int i = 0; i < count; i++) {
356 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
357 s.sectionNameOrType = "GRP_COMDAT";
359 ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
360 if (std::error_code EC = sHdr.getError())
362 ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
363 if (std::error_code ec = sectionName.getError())
365 s.sectionNameOrType = *sectionName;
367 S->Members.push_back(s);
372 template <class ELFT>
373 ErrorOr<ELFYAML::MipsABIFlags *>
374 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
375 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
376 "Section type is not SHT_MIPS_ABIFLAGS");
377 auto S = make_unique<ELFYAML::MipsABIFlags>();
378 if (std::error_code EC = dumpCommonSection(Shdr, *S))
381 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
382 if (std::error_code EC = ContentOrErr.getError())
385 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
386 ContentOrErr.get().data());
387 S->Version = Flags->version;
388 S->ISALevel = Flags->isa_level;
389 S->ISARevision = Flags->isa_rev;
390 S->GPRSize = Flags->gpr_size;
391 S->CPR1Size = Flags->cpr1_size;
392 S->CPR2Size = Flags->cpr2_size;
393 S->FpABI = Flags->fp_abi;
394 S->ISAExtension = Flags->isa_ext;
395 S->ASEs = Flags->ases;
396 S->Flags1 = Flags->flags1;
397 S->Flags2 = Flags->flags2;
401 template <class ELFT>
402 static std::error_code elf2yaml(raw_ostream &Out,
403 const object::ELFFile<ELFT> &Obj) {
404 ELFDumper<ELFT> Dumper(Obj);
405 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
406 if (std::error_code EC = YAMLOrErr.getError())
409 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
410 yaml::Output Yout(Out);
413 return std::error_code();
416 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
417 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
418 return elf2yaml(Out, *ELFObj->getELFFile());
420 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
421 return elf2yaml(Out, *ELFObj->getELFFile());
423 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
424 return elf2yaml(Out, *ELFObj->getELFFile());
426 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
427 return elf2yaml(Out, *ELFObj->getELFFile());
429 return obj2yaml_error::unsupported_obj_file_format;