Use an enum class.
[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   S.Visibility = Sym->st_other & 0x3;
136
137   ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym);
138   if (error_code EC = NameOrErr.getError())
139     return EC;
140   S.Name = NameOrErr.get();
141
142   const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
143   if (!Shdr)
144     return obj2yaml_error::success;
145
146   NameOrErr = Obj.getSectionName(Shdr);
147   if (error_code EC = NameOrErr.getError())
148     return EC;
149   S.Section = NameOrErr.get();
150
151   return obj2yaml_error::success;
152 }
153
154 template <class ELFT>
155 template <class RelT>
156 error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
157                                            const RelT *Rel,
158                                            ELFYAML::Relocation &R) {
159   R.Type = Rel->getType(Obj.isMips64EL());
160   R.Offset = Rel->r_offset;
161   R.Addend = 0;
162
163   auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
164   if (!NamePair.first)
165     return obj2yaml_error::success;
166
167   ErrorOr<StringRef> NameOrErr =
168       Obj.getSymbolName(NamePair.first, NamePair.second);
169   if (error_code EC = NameOrErr.getError())
170     return EC;
171   R.Symbol = NameOrErr.get();
172
173   return obj2yaml_error::success;
174 }
175
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;
183
184   ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
185   if (error_code EC = NameOrErr.getError())
186     return EC;
187   S.Name = NameOrErr.get();
188
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())
193         return EC;
194       S.Link = NameOrErr.get();
195     }
196   }
197
198   return obj2yaml_error::success;
199 }
200
201 template <class ELFT>
202 error_code
203 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
204                                              ELFYAML::RelocationSection &S) {
205   if (error_code EC = dumpCommonSection(Shdr, S))
206     return EC;
207
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())
211       return EC;
212     S.Info = NameOrErr.get();
213   }
214
215   return obj2yaml_error::success;
216 }
217
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>();
223
224   if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
225     return EC;
226
227   for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
228        ++RI) {
229     ELFYAML::Relocation R;
230     if (error_code EC = dumpRelocation(Shdr, &*RI, R))
231       return EC;
232     S->Relocations.push_back(R);
233   }
234
235   return S.release();
236 }
237
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>();
243
244   if (error_code EC = dumpCommonRelocationSection(Shdr, *S))
245     return EC;
246
247   for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
248        ++RI) {
249     ELFYAML::Relocation R;
250     if (error_code EC = dumpRelocation(Shdr, &*RI, R))
251       return EC;
252     R.Addend = RI->r_addend;
253     S->Relocations.push_back(R);
254   }
255
256   return S.release();
257 }
258
259 template <class ELFT>
260 ErrorOr<ELFYAML::RawContentSection *>
261 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
262   auto S = make_unique<ELFYAML::RawContentSection>();
263
264   if (error_code EC = dumpCommonSection(Shdr, *S))
265     return EC;
266
267   ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
268   if (error_code EC = ContentOrErr.getError())
269     return EC;
270   S->Content = object::yaml::BinaryRef(ContentOrErr.get());
271   S->Size = S->Content.binary_size();
272
273   return S.release();
274 }
275
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())
281     return EC;
282
283   std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
284   yaml::Output Yout(Out);
285   Yout << *YAML;
286
287   return object::object_error::success;
288 }
289
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());
293
294   if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
295     return elf2yaml(Out, *ELFObj->getELFFile());
296
297   if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
298     return elf2yaml(Out, *ELFObj->getELFFile());
299
300   if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
301     return elf2yaml(Out, *ELFObj->getELFFile());
302
303   return obj2yaml_error::unsupported_obj_file_format;
304 }