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 typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
25 typedef typename object::ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
27 const object::ELFFile<ELFT> &Obj;
29 error_code dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S);
30 error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
32 error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
33 ELFYAML::Relocation &R);
35 ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
36 ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
37 ErrorOr<ELFYAML::RawContentSection *>
38 dumpContentSection(const Elf_Shdr *Shdr);
41 ELFDumper(const object::ELFFile<ELFT> &O);
42 ErrorOr<ELFYAML::Object *> dump();
48 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
52 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
53 auto Y = make_unique<ELFYAML::Object>();
56 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
57 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
58 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
59 Y->Header.Type = Obj.getHeader()->e_type;
60 Y->Header.Machine = Obj.getHeader()->e_machine;
61 Y->Header.Flags = Obj.getHeader()->e_flags;
62 Y->Header.Entry = Obj.getHeader()->e_entry;
65 for (const Elf_Shdr &Sec : Obj.sections()) {
66 switch (Sec.sh_type) {
71 // Do not dump these sections.
74 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
75 if (error_code EC = S.getError())
77 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
81 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
82 if (error_code EC = S.getError())
84 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
88 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
89 if (error_code EC = S.getError())
91 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
97 bool IsFirstSym = true;
98 for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) {
105 if (error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, S))
108 switch (SI->getBinding())
111 Y->Symbols.Local.push_back(S);
113 case ELF::STB_GLOBAL:
114 Y->Symbols.Global.push_back(S);
117 Y->Symbols.Weak.push_back(S);
120 llvm_unreachable("Unknown ELF symbol binding");
127 template <class ELFT>
128 error_code ELFDumper<ELFT>::dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S) {
129 S.Type = Sym->getType();
130 S.Value = Sym->st_value;
131 S.Size = Sym->st_size;
133 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
134 if (error_code EC = NameOrErr.getError())
136 S.Name = NameOrErr.get();
138 const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
140 return obj2yaml_error::success;
142 NameOrErr = Obj.getSectionName(Shdr);
143 if (error_code EC = NameOrErr.getError())
145 S.Section = NameOrErr.get();
147 return obj2yaml_error::success;
150 template <class ELFT>
151 template <class RelT>
152 error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
154 ELFYAML::Relocation &R) {
155 R.Type = Rel->getType(Obj.isMips64EL());
156 R.Offset = Rel->r_offset;
159 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
161 return obj2yaml_error::success;
163 ErrorOr<StringRef> NameOrErr =
164 Obj.getSymbolName(NamePair.first, NamePair.second);
165 if (error_code EC = NameOrErr.getError())
167 R.Symbol = NameOrErr.get();
169 return obj2yaml_error::success;
172 template <class ELFT>
173 error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
174 ELFYAML::Section &S) {
175 S.Type = Shdr->sh_type;
176 S.Flags = Shdr->sh_flags;
177 S.Address = Shdr->sh_addr;
178 S.AddressAlign = Shdr->sh_addralign;
180 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
181 if (error_code EC = NameOrErr.getError())
183 S.Name = NameOrErr.get();
185 if (Shdr->sh_link != ELF::SHN_UNDEF) {
186 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
187 NameOrErr = Obj.getSectionName(LinkSection);
188 if (error_code EC = NameOrErr.getError())
190 S.Link = NameOrErr.get();
193 if (Shdr->sh_info != ELF::SHN_UNDEF) {
194 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
195 NameOrErr = Obj.getSectionName(InfoSection);
196 if (error_code EC = NameOrErr.getError())
198 S.Info = NameOrErr.get();
201 return obj2yaml_error::success;
204 template <class ELFT>
205 ErrorOr<ELFYAML::RelocationSection *>
206 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
207 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
208 auto S = make_unique<ELFYAML::RelocationSection>();
210 if (error_code EC = dumpCommonSection(Shdr, *S))
213 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
215 ELFYAML::Relocation R;
216 if (error_code EC = dumpRelocation(Shdr, &*RI, R))
218 S->Relocations.push_back(R);
224 template <class ELFT>
225 ErrorOr<ELFYAML::RelocationSection *>
226 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
227 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
228 auto S = make_unique<ELFYAML::RelocationSection>();
230 if (error_code EC = dumpCommonSection(Shdr, *S))
233 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
235 ELFYAML::Relocation R;
236 if (error_code EC = dumpRelocation(Shdr, &*RI, R))
238 R.Addend = RI->r_addend;
239 S->Relocations.push_back(R);
245 template <class ELFT>
246 ErrorOr<ELFYAML::RawContentSection *>
247 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
248 auto S = make_unique<ELFYAML::RawContentSection>();
250 if (error_code EC = dumpCommonSection(Shdr, *S))
253 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
254 if (error_code EC = ContentOrErr.getError())
256 S->Content = object::yaml::BinaryRef(ContentOrErr.get());
261 template <class ELFT>
262 static error_code elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) {
263 ELFDumper<ELFT> Dumper(Obj);
264 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
265 if (error_code EC = YAMLOrErr.getError())
268 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
269 yaml::Output Yout(Out);
272 return object::object_error::success;
275 error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
276 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
277 return elf2yaml(Out, *ELFObj->getELFFile());
279 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
280 return elf2yaml(Out, *ELFObj->getELFFile());
282 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
283 return elf2yaml(Out, *ELFObj->getELFFile());
285 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
286 return elf2yaml(Out, *ELFObj->getELFFile());
288 return obj2yaml_error::unsupported_obj_file_format;