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, bool IsDynamic,
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.symbol_begin(), SE = Obj.symbol_end(); SI != SE; ++SI) {
125 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, false, 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(const Elf_Sym *Sym, bool IsDynamic,
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, IsDynamic);
156 if (std::error_code EC = NameOrErr.getError())
158 S.Name = NameOrErr.get();
160 ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(&*Sym);
161 if (std::error_code EC = ShdrOrErr.getError())
163 const Elf_Shdr *Shdr = *ShdrOrErr;
165 return obj2yaml_error::success;
167 NameOrErr = Obj.getSectionName(Shdr);
168 if (std::error_code EC = NameOrErr.getError())
170 S.Section = NameOrErr.get();
172 return obj2yaml_error::success;
175 template <class ELFT>
176 template <class RelT>
177 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
179 ELFYAML::Relocation &R) {
180 R.Type = Rel->getType(Obj.isMips64EL());
181 R.Offset = Rel->r_offset;
184 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
186 return obj2yaml_error::success;
188 const Elf_Shdr *SymTab = NamePair.first;
189 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
190 if (std::error_code EC = StrTabSec.getError())
192 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
193 if (std::error_code EC = StrTabOrErr.getError())
195 StringRef StrTab = *StrTabOrErr;
197 ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab);
198 if (std::error_code EC = NameOrErr.getError())
200 R.Symbol = NameOrErr.get();
202 return obj2yaml_error::success;
205 template <class ELFT>
206 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
207 ELFYAML::Section &S) {
208 S.Type = Shdr->sh_type;
209 S.Flags = Shdr->sh_flags;
210 S.Address = Shdr->sh_addr;
211 S.AddressAlign = Shdr->sh_addralign;
213 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
214 if (std::error_code EC = NameOrErr.getError())
216 S.Name = NameOrErr.get();
218 if (Shdr->sh_link != ELF::SHN_UNDEF) {
219 ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
220 if (std::error_code EC = LinkSection.getError())
222 NameOrErr = Obj.getSectionName(*LinkSection);
223 if (std::error_code EC = NameOrErr.getError())
225 S.Link = NameOrErr.get();
228 return obj2yaml_error::success;
231 template <class ELFT>
233 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
234 ELFYAML::RelocationSection &S) {
235 if (std::error_code EC = dumpCommonSection(Shdr, S))
238 ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
239 if (std::error_code EC = InfoSection.getError())
242 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
243 if (std::error_code EC = NameOrErr.getError())
245 S.Info = NameOrErr.get();
247 return obj2yaml_error::success;
250 template <class ELFT>
251 ErrorOr<ELFYAML::RelocationSection *>
252 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
253 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
254 auto S = make_unique<ELFYAML::RelocationSection>();
256 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
259 for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
260 ELFYAML::Relocation R;
261 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
263 S->Relocations.push_back(R);
269 template <class ELFT>
270 ErrorOr<ELFYAML::RelocationSection *>
271 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
272 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
273 auto S = make_unique<ELFYAML::RelocationSection>();
275 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
278 for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
280 ELFYAML::Relocation R;
281 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
283 R.Addend = RI->r_addend;
284 S->Relocations.push_back(R);
290 template <class ELFT>
291 ErrorOr<ELFYAML::RawContentSection *>
292 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
293 auto S = make_unique<ELFYAML::RawContentSection>();
295 if (std::error_code EC = dumpCommonSection(Shdr, *S))
298 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
299 if (std::error_code EC = ContentOrErr.getError())
301 S->Content = yaml::BinaryRef(ContentOrErr.get());
302 S->Size = S->Content.binary_size();
307 template <class ELFT>
308 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
309 auto S = make_unique<ELFYAML::Group>();
311 if (std::error_code EC = dumpCommonSection(Shdr, *S))
313 // Get sh_info which is the signature.
314 const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
315 ErrorOr<const Elf_Shdr *> Symtab = Obj.getSection(Shdr->sh_link);
316 if (std::error_code EC = Symtab.getError())
318 ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection((*Symtab)->sh_link);
319 if (std::error_code EC = StrTabSec.getError())
321 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
322 if (std::error_code EC = StrTabOrErr.getError())
324 StringRef StrTab = *StrTabOrErr;
325 auto sectionContents = Obj.getSectionContents(Shdr);
326 if (std::error_code ec = sectionContents.getError())
328 ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
329 if (std::error_code EC = symbolName.getError())
331 S->Info = *symbolName;
332 const Elf_Word *groupMembers =
333 reinterpret_cast<const Elf_Word *>(sectionContents->data());
334 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
335 ELFYAML::SectionOrType s;
336 for (int i = 0; i < count; i++) {
337 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
338 s.sectionNameOrType = "GRP_COMDAT";
340 ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
341 if (std::error_code EC = sHdr.getError())
343 ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
344 if (std::error_code ec = sectionName.getError())
346 s.sectionNameOrType = *sectionName;
348 S->Members.push_back(s);
353 template <class ELFT>
354 ErrorOr<ELFYAML::MipsABIFlags *>
355 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
356 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
357 "Section type is not SHT_MIPS_ABIFLAGS");
358 auto S = make_unique<ELFYAML::MipsABIFlags>();
359 if (std::error_code EC = dumpCommonSection(Shdr, *S))
362 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
363 if (std::error_code EC = ContentOrErr.getError())
366 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
367 ContentOrErr.get().data());
368 S->Version = Flags->version;
369 S->ISALevel = Flags->isa_level;
370 S->ISARevision = Flags->isa_rev;
371 S->GPRSize = Flags->gpr_size;
372 S->CPR1Size = Flags->cpr1_size;
373 S->CPR2Size = Flags->cpr2_size;
374 S->FpABI = Flags->fp_abi;
375 S->ISAExtension = Flags->isa_ext;
376 S->ASEs = Flags->ases;
377 S->Flags1 = Flags->flags1;
378 S->Flags2 = Flags->flags2;
382 template <class ELFT>
383 static std::error_code elf2yaml(raw_ostream &Out,
384 const object::ELFFile<ELFT> &Obj) {
385 ELFDumper<ELFT> Dumper(Obj);
386 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
387 if (std::error_code EC = YAMLOrErr.getError())
390 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
391 yaml::Output Yout(Out);
394 return std::error_code();
397 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
398 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
399 return elf2yaml(Out, *ELFObj->getELFFile());
401 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
402 return elf2yaml(Out, *ELFObj->getELFFile());
404 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
405 return elf2yaml(Out, *ELFObj->getELFFile());
407 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
408 return elf2yaml(Out, *ELFObj->getELFFile());
410 return obj2yaml_error::unsupported_obj_file_format;