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);
31 error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
32 ELFYAML::RelocationSection &S);
34 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 (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 (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 (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 (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 error_code ELFDumper<ELFT>::dumpSymbol(Elf_Sym_Iter Sym, ELFYAML::Symbol &S) {
132 S.Type = Sym->getType();
133 S.Value = Sym->st_value;
134 S.Size = Sym->st_size;
135 S.Visibility = Sym->st_other & 0x3;
137 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
138 if (error_code EC = NameOrErr.getError())
140 S.Name = NameOrErr.get();
142 const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
144 return obj2yaml_error::success;
146 NameOrErr = Obj.getSectionName(Shdr);
147 if (error_code EC = NameOrErr.getError())
149 S.Section = NameOrErr.get();
151 return obj2yaml_error::success;
154 template <class ELFT>
155 template <class RelT>
156 error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
158 ELFYAML::Relocation &R) {
159 R.Type = Rel->getType(Obj.isMips64EL());
160 R.Offset = Rel->r_offset;
163 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
165 return obj2yaml_error::success;
167 ErrorOr<StringRef> NameOrErr =
168 Obj.getSymbolName(NamePair.first, NamePair.second);
169 if (error_code EC = NameOrErr.getError())
171 R.Symbol = NameOrErr.get();
173 return obj2yaml_error::success;
176 template <class ELFT>
177 error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
178 ELFYAML::Section &S) {
179 S.Type = Shdr->sh_type;
180 S.Flags = Shdr->sh_flags;
181 S.Address = Shdr->sh_addr;
182 S.AddressAlign = Shdr->sh_addralign;
184 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
185 if (error_code EC = NameOrErr.getError())
187 S.Name = NameOrErr.get();
189 if (Shdr->sh_link != ELF::SHN_UNDEF) {
190 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
191 NameOrErr = Obj.getSectionName(LinkSection);
192 if (error_code EC = NameOrErr.getError())
194 S.Link = NameOrErr.get();
198 return obj2yaml_error::success;
201 template <class ELFT>
203 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
204 ELFYAML::RelocationSection &S) {
205 if (error_code EC = dumpCommonSection(Shdr, S))
208 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
209 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
210 if (error_code EC = NameOrErr.getError())
212 S.Info = NameOrErr.get();
215 return obj2yaml_error::success;
218 template <class ELFT>
219 ErrorOr<ELFYAML::RelocationSection *>
220 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
221 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
222 auto S = make_unique<ELFYAML::RelocationSection>();
224 if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
227 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
229 ELFYAML::Relocation R;
230 if (error_code EC = dumpRelocation(Shdr, &*RI, R))
232 S->Relocations.push_back(R);
238 template <class ELFT>
239 ErrorOr<ELFYAML::RelocationSection *>
240 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
241 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
242 auto S = make_unique<ELFYAML::RelocationSection>();
244 if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
247 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
249 ELFYAML::Relocation R;
250 if (error_code EC = dumpRelocation(Shdr, &*RI, R))
252 R.Addend = RI->r_addend;
253 S->Relocations.push_back(R);
259 template <class ELFT>
260 ErrorOr<ELFYAML::RawContentSection *>
261 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
262 auto S = make_unique<ELFYAML::RawContentSection>();
264 if (error_code EC = dumpCommonSection(Shdr, *S))
267 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
268 if (error_code EC = ContentOrErr.getError())
270 S->Content = object::yaml::BinaryRef(ContentOrErr.get());
271 S->Size = S->Content.binary_size();
276 template <class ELFT>
277 static error_code elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) {
278 ELFDumper<ELFT> Dumper(Obj);
279 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
280 if (error_code EC = YAMLOrErr.getError())
283 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
284 yaml::Output Yout(Out);
287 return object::object_error::success;
290 error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
291 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
292 return elf2yaml(Out, *ELFObj->getELFFile());
294 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
295 return elf2yaml(Out, *ELFObj->getELFFile());
297 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
298 return elf2yaml(Out, *ELFObj->getELFFile());
300 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
301 return elf2yaml(Out, *ELFObj->getELFFile());
303 return obj2yaml_error::unsupported_obj_file_format;