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.begin_symbols(), SE = Obj.end_symbols(); 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 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 const Elf_Shdr *SymTab = NamePair.first;
186 const Elf_Shdr *StrTabSec = Obj.getSection(SymTab->sh_link);
187 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
188 if (std::error_code EC = StrTabOrErr.getError())
190 StringRef StrTab = *StrTabOrErr;
192 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(StrTab, NamePair.second);
193 if (std::error_code EC = NameOrErr.getError())
195 R.Symbol = NameOrErr.get();
197 return obj2yaml_error::success;
200 template <class ELFT>
201 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
202 ELFYAML::Section &S) {
203 S.Type = Shdr->sh_type;
204 S.Flags = Shdr->sh_flags;
205 S.Address = Shdr->sh_addr;
206 S.AddressAlign = Shdr->sh_addralign;
208 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
209 if (std::error_code EC = NameOrErr.getError())
211 S.Name = NameOrErr.get();
213 if (Shdr->sh_link != ELF::SHN_UNDEF) {
214 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
215 NameOrErr = Obj.getSectionName(LinkSection);
216 if (std::error_code EC = NameOrErr.getError())
218 S.Link = NameOrErr.get();
222 return obj2yaml_error::success;
225 template <class ELFT>
227 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
228 ELFYAML::RelocationSection &S) {
229 if (std::error_code EC = dumpCommonSection(Shdr, S))
232 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
233 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
234 if (std::error_code EC = NameOrErr.getError())
236 S.Info = NameOrErr.get();
239 return obj2yaml_error::success;
242 template <class ELFT>
243 ErrorOr<ELFYAML::RelocationSection *>
244 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
245 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
246 auto S = make_unique<ELFYAML::RelocationSection>();
248 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
251 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
253 ELFYAML::Relocation R;
254 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
256 S->Relocations.push_back(R);
262 template <class ELFT>
263 ErrorOr<ELFYAML::RelocationSection *>
264 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
265 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
266 auto S = make_unique<ELFYAML::RelocationSection>();
268 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
271 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
273 ELFYAML::Relocation R;
274 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
276 R.Addend = RI->r_addend;
277 S->Relocations.push_back(R);
283 template <class ELFT>
284 ErrorOr<ELFYAML::RawContentSection *>
285 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
286 auto S = make_unique<ELFYAML::RawContentSection>();
288 if (std::error_code EC = dumpCommonSection(Shdr, *S))
291 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
292 if (std::error_code EC = ContentOrErr.getError())
294 S->Content = yaml::BinaryRef(ContentOrErr.get());
295 S->Size = S->Content.binary_size();
300 template <class ELFT>
301 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
302 auto S = make_unique<ELFYAML::Group>();
304 if (std::error_code EC = dumpCommonSection(Shdr, *S))
306 // Get sh_info which is the signature.
307 const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
308 const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link);
309 const Elf_Shdr *StrTabSec = Obj.getSection(symtab->sh_link);
310 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
311 if (std::error_code EC = StrTabOrErr.getError())
313 StringRef StrTab = *StrTabOrErr;
314 auto sectionContents = Obj.getSectionContents(Shdr);
315 if (std::error_code ec = sectionContents.getError())
317 ErrorOr<StringRef> symbolName = Obj.getSymbolName(StrTab, symbol);
318 if (std::error_code EC = symbolName.getError())
320 S->Info = *symbolName;
321 const Elf_Word *groupMembers =
322 reinterpret_cast<const Elf_Word *>(sectionContents->data());
323 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
324 ELFYAML::SectionOrType s;
325 for (int i = 0; i < count; i++) {
326 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
327 s.sectionNameOrType = "GRP_COMDAT";
329 const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]);
330 ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr);
331 if (std::error_code ec = sectionName.getError())
333 s.sectionNameOrType = *sectionName;
335 S->Members.push_back(s);
340 template <class ELFT>
341 ErrorOr<ELFYAML::MipsABIFlags *>
342 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
343 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
344 "Section type is not SHT_MIPS_ABIFLAGS");
345 auto S = make_unique<ELFYAML::MipsABIFlags>();
346 if (std::error_code EC = dumpCommonSection(Shdr, *S))
349 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
350 if (std::error_code EC = ContentOrErr.getError())
353 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
354 ContentOrErr.get().data());
355 S->Version = Flags->version;
356 S->ISALevel = Flags->isa_level;
357 S->ISARevision = Flags->isa_rev;
358 S->GPRSize = Flags->gpr_size;
359 S->CPR1Size = Flags->cpr1_size;
360 S->CPR2Size = Flags->cpr2_size;
361 S->FpABI = Flags->fp_abi;
362 S->ISAExtension = Flags->isa_ext;
363 S->ASEs = Flags->ases;
364 S->Flags1 = Flags->flags1;
365 S->Flags2 = Flags->flags2;
369 template <class ELFT>
370 static std::error_code elf2yaml(raw_ostream &Out,
371 const object::ELFFile<ELFT> &Obj) {
372 ELFDumper<ELFT> Dumper(Obj);
373 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
374 if (std::error_code EC = YAMLOrErr.getError())
377 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
378 yaml::Output Yout(Out);
381 return std::error_code();
384 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
385 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
386 return elf2yaml(Out, *ELFObj->getELFFile());
388 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
389 return elf2yaml(Out, *ELFObj->getELFFile());
391 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
392 return elf2yaml(Out, *ELFObj->getELFFile());
394 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
395 return elf2yaml(Out, *ELFObj->getELFFile());
397 return obj2yaml_error::unsupported_obj_file_format;