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;
136 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
137 if (error_code EC = NameOrErr.getError())
139 S.Name = NameOrErr.get();
141 const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
143 return obj2yaml_error::success;
145 NameOrErr = Obj.getSectionName(Shdr);
146 if (error_code EC = NameOrErr.getError())
148 S.Section = NameOrErr.get();
150 return obj2yaml_error::success;
153 template <class ELFT>
154 template <class RelT>
155 error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
157 ELFYAML::Relocation &R) {
158 R.Type = Rel->getType(Obj.isMips64EL());
159 R.Offset = Rel->r_offset;
162 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
164 return obj2yaml_error::success;
166 ErrorOr<StringRef> NameOrErr =
167 Obj.getSymbolName(NamePair.first, NamePair.second);
168 if (error_code EC = NameOrErr.getError())
170 R.Symbol = NameOrErr.get();
172 return obj2yaml_error::success;
175 template <class ELFT>
176 error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
177 ELFYAML::Section &S) {
178 S.Type = Shdr->sh_type;
179 S.Flags = Shdr->sh_flags;
180 S.Address = Shdr->sh_addr;
181 S.AddressAlign = Shdr->sh_addralign;
183 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
184 if (error_code EC = NameOrErr.getError())
186 S.Name = NameOrErr.get();
188 if (Shdr->sh_link != ELF::SHN_UNDEF) {
189 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
190 NameOrErr = Obj.getSectionName(LinkSection);
191 if (error_code EC = NameOrErr.getError())
193 S.Link = NameOrErr.get();
197 return obj2yaml_error::success;
200 template <class ELFT>
202 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
203 ELFYAML::RelocationSection &S) {
204 if (error_code EC = dumpCommonSection(Shdr, S))
207 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
208 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
209 if (error_code EC = NameOrErr.getError())
211 S.Info = NameOrErr.get();
214 return obj2yaml_error::success;
217 template <class ELFT>
218 ErrorOr<ELFYAML::RelocationSection *>
219 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
220 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
221 auto S = make_unique<ELFYAML::RelocationSection>();
223 if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
226 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
228 ELFYAML::Relocation R;
229 if (error_code EC = dumpRelocation(Shdr, &*RI, R))
231 S->Relocations.push_back(R);
237 template <class ELFT>
238 ErrorOr<ELFYAML::RelocationSection *>
239 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
240 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
241 auto S = make_unique<ELFYAML::RelocationSection>();
243 if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
246 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
248 ELFYAML::Relocation R;
249 if (error_code EC = dumpRelocation(Shdr, &*RI, R))
251 R.Addend = RI->r_addend;
252 S->Relocations.push_back(R);
258 template <class ELFT>
259 ErrorOr<ELFYAML::RawContentSection *>
260 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
261 auto S = make_unique<ELFYAML::RawContentSection>();
263 if (error_code EC = dumpCommonSection(Shdr, *S))
266 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
267 if (error_code EC = ContentOrErr.getError())
269 S->Content = object::yaml::BinaryRef(ContentOrErr.get());
270 S->Size = S->Content.binary_size();
275 template <class ELFT>
276 static error_code elf2yaml(raw_ostream &Out, const object::ELFFile<ELFT> &Obj) {
277 ELFDumper<ELFT> Dumper(Obj);
278 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
279 if (error_code EC = YAMLOrErr.getError())
282 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
283 yaml::Output Yout(Out);
286 return object::object_error::success;
289 error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
290 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
291 return elf2yaml(Out, *ELFObj->getELFFile());
293 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
294 return elf2yaml(Out, *ELFObj->getELFFile());
296 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
297 return elf2yaml(Out, *ELFObj->getELFFile());
299 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
300 return elf2yaml(Out, *ELFObj->getELFFile());
302 return obj2yaml_error::unsupported_obj_file_format;