[elf2yaml][ELF] Move Info field to the RelocationSection structure. This
[oota-llvm.git] / tools / obj2yaml / elf2yaml.cpp
1 //===------ utils/elf2yaml.cpp - obj2yaml conversion tool -------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "Error.h"
11 #include "obj2yaml.h"
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"
17
18 using namespace llvm;
19
20 namespace {
21
22 template <class ELFT>
23 class ELFDumper {
24   typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
25   typedef typename object::ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
26
27   const object::ELFFile<ELFT> &Obj;
28
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);
33   template <class RelT>
34   error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
35                             ELFYAML::Relocation &R);
36
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);
41
42 public:
43   ELFDumper(const object::ELFFile<ELFT> &O);
44   ErrorOr<ELFYAML::Object *> dump();
45 };
46
47 }
48
49 template <class ELFT>
50 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
51     : Obj(O) {}
52
53 template <class ELFT>
54 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
55   auto Y = make_unique<ELFYAML::Object>();
56
57   // Dump header
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;
65
66   // Dump sections
67   for (const Elf_Shdr &Sec : Obj.sections()) {
68     switch (Sec.sh_type) {
69     case ELF::SHT_NULL:
70     case ELF::SHT_SYMTAB:
71     case ELF::SHT_DYNSYM:
72     case ELF::SHT_STRTAB:
73       // Do not dump these sections.
74       break;
75     case ELF::SHT_RELA: {
76       ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
77       if (error_code EC = S.getError())
78         return EC;
79       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
80       break;
81     }
82     case ELF::SHT_REL: {
83       ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
84       if (error_code EC = S.getError())
85         return EC;
86       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
87       break;
88     }
89     // FIXME: Support SHT_GROUP section format.
90     default: {
91       ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
92       if (error_code EC = S.getError())
93         return EC;
94       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
95     }
96     }
97   }
98
99   // Dump symbols
100   bool IsFirstSym = true;
101   for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) {
102     if (IsFirstSym) {
103       IsFirstSym = false;
104       continue;
105     }
106
107     ELFYAML::Symbol S;
108     if (error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, S))
109       return EC;
110
111     switch (SI->getBinding())
112     {
113     case ELF::STB_LOCAL:
114       Y->Symbols.Local.push_back(S);
115       break;
116     case ELF::STB_GLOBAL:
117       Y->Symbols.Global.push_back(S);
118       break;
119     case ELF::STB_WEAK:
120       Y->Symbols.Weak.push_back(S);
121       break;
122     default:
123       llvm_unreachable("Unknown ELF symbol binding");
124     }
125   }
126
127   return Y.release();
128 }
129
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
136   ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
137   if (error_code EC = NameOrErr.getError())
138     return EC;
139   S.Name = NameOrErr.get();
140
141   const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
142   if (!Shdr)
143     return obj2yaml_error::success;
144
145   NameOrErr = Obj.getSectionName(Shdr);
146   if (error_code EC = NameOrErr.getError())
147     return EC;
148   S.Section = NameOrErr.get();
149
150   return obj2yaml_error::success;
151 }
152
153 template <class ELFT>
154 template <class RelT>
155 error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
156                                            const RelT *Rel,
157                                            ELFYAML::Relocation &R) {
158   R.Type = Rel->getType(Obj.isMips64EL());
159   R.Offset = Rel->r_offset;
160   R.Addend = 0;
161
162   auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
163   if (!NamePair.first)
164     return obj2yaml_error::success;
165
166   ErrorOr<StringRef> NameOrErr =
167       Obj.getSymbolName(NamePair.first, NamePair.second);
168   if (error_code EC = NameOrErr.getError())
169     return EC;
170   R.Symbol = NameOrErr.get();
171
172   return obj2yaml_error::success;
173 }
174
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;
182
183   ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
184   if (error_code EC = NameOrErr.getError())
185     return EC;
186   S.Name = NameOrErr.get();
187
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())
192         return EC;
193       S.Link = NameOrErr.get();
194     }
195   }
196
197   return obj2yaml_error::success;
198 }
199
200 template <class ELFT>
201 error_code
202 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
203                                              ELFYAML::RelocationSection &S) {
204   if (error_code EC = dumpCommonSection(Shdr, S))
205     return EC;
206
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())
210       return EC;
211     S.Info = NameOrErr.get();
212   }
213
214   return obj2yaml_error::success;
215 }
216
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>();
222
223   if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
224     return EC;
225
226   for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
227        ++RI) {
228     ELFYAML::Relocation R;
229     if (error_code EC = dumpRelocation(Shdr, &*RI, R))
230       return EC;
231     S->Relocations.push_back(R);
232   }
233
234   return S.release();
235 }
236
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>();
242
243   if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
244     return EC;
245
246   for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
247        ++RI) {
248     ELFYAML::Relocation R;
249     if (error_code EC = dumpRelocation(Shdr, &*RI, R))
250       return EC;
251     R.Addend = RI->r_addend;
252     S->Relocations.push_back(R);
253   }
254
255   return S.release();
256 }
257
258 template <class ELFT>
259 ErrorOr<ELFYAML::RawContentSection *>
260 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
261   auto S = make_unique<ELFYAML::RawContentSection>();
262
263   if (error_code EC = dumpCommonSection(Shdr, *S))
264     return EC;
265
266   ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
267   if (error_code EC = ContentOrErr.getError())
268     return EC;
269   S->Content = object::yaml::BinaryRef(ContentOrErr.get());
270   S->Size = S->Content.binary_size();
271
272   return S.release();
273 }
274
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())
280     return EC;
281
282   std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
283   yaml::Output Yout(Out);
284   Yout << *YAML;
285
286   return object::object_error::success;
287 }
288
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());
292
293   if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
294     return elf2yaml(Out, *ELFObj->getELFFile());
295
296   if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
297     return elf2yaml(Out, *ELFObj->getELFFile());
298
299   if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
300     return elf2yaml(Out, *ELFObj->getELFFile());
301
302   return obj2yaml_error::unsupported_obj_file_format;
303 }