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 std::error_code dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S);
30 std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
31 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
32 ELFYAML::RelocationSection &S);
34 std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
35 ELFYAML::Relocation &R);
37 ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
38 ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
39 ErrorOr<ELFYAML::RawContentSection *>
40 dumpContentSection(const Elf_Shdr *Shdr);
43 ELFDumper(const object::ELFFile<ELFT> &O);
44 ErrorOr<ELFYAML::Object *> dump();
50 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
54 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
55 auto Y = make_unique<ELFYAML::Object>();
58 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
59 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
60 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
61 Y->Header.Type = Obj.getHeader()->e_type;
62 Y->Header.Machine = Obj.getHeader()->e_machine;
63 Y->Header.Flags = Obj.getHeader()->e_flags;
64 Y->Header.Entry = Obj.getHeader()->e_entry;
67 for (const Elf_Shdr &Sec : Obj.sections()) {
68 switch (Sec.sh_type) {
73 // Do not dump these sections.
76 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
77 if (std::error_code EC = S.getError())
79 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
83 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
84 if (std::error_code EC = S.getError())
86 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
89 // FIXME: Support SHT_GROUP section format.
91 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
92 if (std::error_code EC = S.getError())
94 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
100 bool IsFirstSym = true;
101 for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) {
108 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, S))
111 switch (SI->getBinding())
114 Y->Symbols.Local.push_back(S);
116 case ELF::STB_GLOBAL:
117 Y->Symbols.Global.push_back(S);
120 Y->Symbols.Weak.push_back(S);
123 llvm_unreachable("Unknown ELF symbol binding");
130 template <class ELFT>
131 std::error_code ELFDumper<ELFT>::dumpSymbol(Elf_Sym_Iter Sym,
132 ELFYAML::Symbol &S) {
133 S.Type = Sym->getType();
134 S.Value = Sym->st_value;
135 S.Size = Sym->st_size;
136 S.Other = Sym->st_other;
138 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
139 if (std::error_code EC = NameOrErr.getError())
141 S.Name = NameOrErr.get();
143 const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
145 return obj2yaml_error::success;
147 NameOrErr = Obj.getSectionName(Shdr);
148 if (std::error_code EC = NameOrErr.getError())
150 S.Section = NameOrErr.get();
152 return obj2yaml_error::success;
155 template <class ELFT>
156 template <class RelT>
157 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
159 ELFYAML::Relocation &R) {
160 R.Type = Rel->getType(Obj.isMips64EL());
161 R.Offset = Rel->r_offset;
164 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
166 return obj2yaml_error::success;
168 ErrorOr<StringRef> NameOrErr =
169 Obj.getSymbolName(NamePair.first, NamePair.second);
170 if (std::error_code EC = NameOrErr.getError())
172 R.Symbol = NameOrErr.get();
174 return obj2yaml_error::success;
177 template <class ELFT>
178 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
179 ELFYAML::Section &S) {
180 S.Type = Shdr->sh_type;
181 S.Flags = Shdr->sh_flags;
182 S.Address = Shdr->sh_addr;
183 S.AddressAlign = Shdr->sh_addralign;
185 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
186 if (std::error_code EC = NameOrErr.getError())
188 S.Name = NameOrErr.get();
190 if (Shdr->sh_link != ELF::SHN_UNDEF) {
191 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
192 NameOrErr = Obj.getSectionName(LinkSection);
193 if (std::error_code EC = NameOrErr.getError())
195 S.Link = NameOrErr.get();
199 return obj2yaml_error::success;
202 template <class ELFT>
204 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
205 ELFYAML::RelocationSection &S) {
206 if (std::error_code EC = dumpCommonSection(Shdr, S))
209 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
210 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
211 if (std::error_code EC = NameOrErr.getError())
213 S.Info = NameOrErr.get();
216 return obj2yaml_error::success;
219 template <class ELFT>
220 ErrorOr<ELFYAML::RelocationSection *>
221 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
222 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
223 auto S = make_unique<ELFYAML::RelocationSection>();
225 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
228 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
230 ELFYAML::Relocation R;
231 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
233 S->Relocations.push_back(R);
239 template <class ELFT>
240 ErrorOr<ELFYAML::RelocationSection *>
241 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
242 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
243 auto S = make_unique<ELFYAML::RelocationSection>();
245 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
248 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
250 ELFYAML::Relocation R;
251 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
253 R.Addend = RI->r_addend;
254 S->Relocations.push_back(R);
260 template <class ELFT>
261 ErrorOr<ELFYAML::RawContentSection *>
262 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
263 auto S = make_unique<ELFYAML::RawContentSection>();
265 if (std::error_code EC = dumpCommonSection(Shdr, *S))
268 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
269 if (std::error_code EC = ContentOrErr.getError())
271 S->Content = yaml::BinaryRef(ContentOrErr.get());
272 S->Size = S->Content.binary_size();
277 template <class ELFT>
278 static std::error_code elf2yaml(raw_ostream &Out,
279 const object::ELFFile<ELFT> &Obj) {
280 ELFDumper<ELFT> Dumper(Obj);
281 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
282 if (std::error_code EC = YAMLOrErr.getError())
285 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
286 yaml::Output Yout(Out);
289 return object::object_error::success;
292 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
293 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
294 return elf2yaml(Out, *ELFObj->getELFFile());
296 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
297 return elf2yaml(Out, *ELFObj->getELFFile());
299 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
300 return elf2yaml(Out, *ELFObj->getELFFile());
302 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
303 return elf2yaml(Out, *ELFObj->getELFFile());
305 return obj2yaml_error::unsupported_obj_file_format;