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_Sym_Iter Elf_Sym_Iter;
27 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
29 const object::ELFFile<ELFT> &Obj;
31 std::error_code dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S);
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::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()));
108 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
109 if (std::error_code EC = S.getError())
111 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
117 bool IsFirstSym = true;
118 for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) {
125 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, S))
128 switch (SI->getBinding())
131 Y->Symbols.Local.push_back(S);
133 case ELF::STB_GLOBAL:
134 Y->Symbols.Global.push_back(S);
137 Y->Symbols.Weak.push_back(S);
140 llvm_unreachable("Unknown ELF symbol binding");
147 template <class ELFT>
148 std::error_code ELFDumper<ELFT>::dumpSymbol(Elf_Sym_Iter Sym,
149 ELFYAML::Symbol &S) {
150 S.Type = Sym->getType();
151 S.Value = Sym->st_value;
152 S.Size = Sym->st_size;
153 S.Other = Sym->st_other;
155 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
156 if (std::error_code EC = NameOrErr.getError())
158 S.Name = NameOrErr.get();
160 const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
162 return obj2yaml_error::success;
164 NameOrErr = Obj.getSectionName(Shdr);
165 if (std::error_code EC = NameOrErr.getError())
167 S.Section = NameOrErr.get();
169 return obj2yaml_error::success;
172 template <class ELFT>
173 template <class RelT>
174 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
176 ELFYAML::Relocation &R) {
177 R.Type = Rel->getType(Obj.isMips64EL());
178 R.Offset = Rel->r_offset;
181 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
183 return obj2yaml_error::success;
185 ErrorOr<StringRef> NameOrErr =
186 Obj.getSymbolName(NamePair.first, NamePair.second);
187 if (std::error_code EC = NameOrErr.getError())
189 R.Symbol = NameOrErr.get();
191 return obj2yaml_error::success;
194 template <class ELFT>
195 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
196 ELFYAML::Section &S) {
197 S.Type = Shdr->sh_type;
198 S.Flags = Shdr->sh_flags;
199 S.Address = Shdr->sh_addr;
200 S.AddressAlign = Shdr->sh_addralign;
202 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
203 if (std::error_code EC = NameOrErr.getError())
205 S.Name = NameOrErr.get();
207 if (Shdr->sh_link != ELF::SHN_UNDEF) {
208 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
209 NameOrErr = Obj.getSectionName(LinkSection);
210 if (std::error_code EC = NameOrErr.getError())
212 S.Link = NameOrErr.get();
216 return obj2yaml_error::success;
219 template <class ELFT>
221 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
222 ELFYAML::RelocationSection &S) {
223 if (std::error_code EC = dumpCommonSection(Shdr, S))
226 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
227 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
228 if (std::error_code EC = NameOrErr.getError())
230 S.Info = NameOrErr.get();
233 return obj2yaml_error::success;
236 template <class ELFT>
237 ErrorOr<ELFYAML::RelocationSection *>
238 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
239 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
240 auto S = make_unique<ELFYAML::RelocationSection>();
242 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
245 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
247 ELFYAML::Relocation R;
248 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
250 S->Relocations.push_back(R);
256 template <class ELFT>
257 ErrorOr<ELFYAML::RelocationSection *>
258 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
259 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
260 auto S = make_unique<ELFYAML::RelocationSection>();
262 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
265 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
267 ELFYAML::Relocation R;
268 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
270 R.Addend = RI->r_addend;
271 S->Relocations.push_back(R);
277 template <class ELFT>
278 ErrorOr<ELFYAML::RawContentSection *>
279 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
280 auto S = make_unique<ELFYAML::RawContentSection>();
282 if (std::error_code EC = dumpCommonSection(Shdr, *S))
285 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
286 if (std::error_code EC = ContentOrErr.getError())
288 S->Content = yaml::BinaryRef(ContentOrErr.get());
289 S->Size = S->Content.binary_size();
294 template <class ELFT>
295 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
296 auto S = make_unique<ELFYAML::Group>();
298 if (std::error_code EC = dumpCommonSection(Shdr, *S))
300 // Get sh_info which is the signature.
301 const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
302 const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link);
303 auto sectionContents = Obj.getSectionContents(Shdr);
304 if (std::error_code ec = sectionContents.getError())
306 ErrorOr<StringRef> symbolName = Obj.getSymbolName(symtab, symbol);
307 if (std::error_code EC = symbolName.getError())
309 S->Info = *symbolName;
310 const Elf_Word *groupMembers =
311 reinterpret_cast<const Elf_Word *>(sectionContents->data());
312 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
313 ELFYAML::SectionOrType s;
314 for (int i = 0; i < count; i++) {
315 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
316 s.sectionNameOrType = "GRP_COMDAT";
318 const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]);
319 ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr);
320 if (std::error_code ec = sectionName.getError())
322 s.sectionNameOrType = *sectionName;
324 S->Members.push_back(s);
329 template <class ELFT>
330 ErrorOr<ELFYAML::MipsABIFlags *>
331 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
332 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
333 "Section type is not SHT_MIPS_ABIFLAGS");
334 auto S = make_unique<ELFYAML::MipsABIFlags>();
335 if (std::error_code EC = dumpCommonSection(Shdr, *S))
338 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
339 if (std::error_code EC = ContentOrErr.getError())
342 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
343 ContentOrErr.get().data());
344 S->Version = Flags->version;
345 S->ISALevel = Flags->isa_level;
346 S->ISARevision = Flags->isa_rev;
347 S->GPRSize = Flags->gpr_size;
348 S->CPR1Size = Flags->cpr1_size;
349 S->CPR2Size = Flags->cpr2_size;
350 S->FpABI = Flags->fp_abi;
351 S->ISAExtension = Flags->isa_ext;
352 S->ASEs = Flags->ases;
353 S->Flags1 = Flags->flags1;
354 S->Flags2 = Flags->flags2;
358 template <class ELFT>
359 static std::error_code elf2yaml(raw_ostream &Out,
360 const object::ELFFile<ELFT> &Obj) {
361 ELFDumper<ELFT> Dumper(Obj);
362 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
363 if (std::error_code EC = YAMLOrErr.getError())
366 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
367 yaml::Output Yout(Out);
370 return std::error_code();
373 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
374 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
375 return elf2yaml(Out, *ELFObj->getELFFile());
377 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
378 return elf2yaml(Out, *ELFObj->getELFFile());
380 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
381 return elf2yaml(Out, *ELFObj->getELFFile());
383 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
384 return elf2yaml(Out, *ELFObj->getELFFile());
386 return obj2yaml_error::unsupported_obj_file_format;