2c6fc1bb09cacd1ba4b57d85d7165336d4c09fc3
[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 object::Elf_Sym_Impl<ELFT> Elf_Sym;
25   typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
26   typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
27
28   const object::ELFFile<ELFT> &Obj;
29
30   std::error_code dumpSymbol(const Elf_Sym *Sym, StringRef StrTable,
31                              ELFYAML::Symbol &S);
32   std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
33   std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
34                                               ELFYAML::RelocationSection &S);
35   template <class RelT>
36   std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
37                                  ELFYAML::Relocation &R);
38
39   ErrorOr<ELFYAML::RelocationSection *> dumpRelSection(const Elf_Shdr *Shdr);
40   ErrorOr<ELFYAML::RelocationSection *> dumpRelaSection(const Elf_Shdr *Shdr);
41   ErrorOr<ELFYAML::RawContentSection *>
42   dumpContentSection(const Elf_Shdr *Shdr);
43   ErrorOr<ELFYAML::NoBitsSection *> dumpNoBitsSection(const Elf_Shdr *Shdr);
44   ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
45   ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
46
47 public:
48   ELFDumper(const object::ELFFile<ELFT> &O);
49   ErrorOr<ELFYAML::Object *> dump();
50 };
51
52 }
53
54 template <class ELFT>
55 ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
56     : Obj(O) {}
57
58 template <class ELFT>
59 ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
60   auto Y = make_unique<ELFYAML::Object>();
61
62   // Dump header
63   Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
64   Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
65   Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
66   Y->Header.Type = Obj.getHeader()->e_type;
67   Y->Header.Machine = Obj.getHeader()->e_machine;
68   Y->Header.Flags = Obj.getHeader()->e_flags;
69   Y->Header.Entry = Obj.getHeader()->e_entry;
70
71   // Dump sections
72   for (const Elf_Shdr &Sec : Obj.sections()) {
73     switch (Sec.sh_type) {
74     case ELF::SHT_NULL:
75     case ELF::SHT_SYMTAB:
76     case ELF::SHT_DYNSYM:
77     case ELF::SHT_STRTAB:
78       // Do not dump these sections.
79       break;
80     case ELF::SHT_RELA: {
81       ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
82       if (std::error_code EC = S.getError())
83         return EC;
84       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
85       break;
86     }
87     case ELF::SHT_REL: {
88       ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
89       if (std::error_code EC = S.getError())
90         return EC;
91       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
92       break;
93     }
94     case ELF::SHT_GROUP: {
95       ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
96       if (std::error_code EC = G.getError())
97         return EC;
98       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
99       break;
100     }
101     case ELF::SHT_MIPS_ABIFLAGS: {
102       ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec);
103       if (std::error_code EC = G.getError())
104         return EC;
105       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
106       break;
107     }
108     case ELF::SHT_NOBITS: {
109       ErrorOr<ELFYAML::NoBitsSection *> S = dumpNoBitsSection(&Sec);
110       if (std::error_code EC = S.getError())
111         return EC;
112       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
113       break;
114     }
115     default: {
116       ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
117       if (std::error_code EC = S.getError())
118         return EC;
119       Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
120     }
121     }
122   }
123
124   // Dump symbols
125   const Elf_Shdr *Symtab = Obj.getDotSymtabSec();
126   ErrorOr<StringRef> StrTableOrErr = Obj.getStringTableForSymtab(*Symtab);
127   if (std::error_code EC = StrTableOrErr.getError())
128     return EC;
129   StringRef StrTable = *StrTableOrErr;
130
131   bool IsFirstSym = true;
132   for (const Elf_Sym &Sym : Obj.symbols()) {
133     if (IsFirstSym) {
134       IsFirstSym = false;
135       continue;
136     }
137
138     ELFYAML::Symbol S;
139     if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(&Sym, StrTable, S))
140       return EC;
141
142     switch (Sym.getBinding())
143     {
144     case ELF::STB_LOCAL:
145       Y->Symbols.Local.push_back(S);
146       break;
147     case ELF::STB_GLOBAL:
148       Y->Symbols.Global.push_back(S);
149       break;
150     case ELF::STB_WEAK:
151       Y->Symbols.Weak.push_back(S);
152       break;
153     default:
154       llvm_unreachable("Unknown ELF symbol binding");
155     }
156   }
157
158   return Y.release();
159 }
160
161 template <class ELFT>
162 std::error_code ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym,
163                                             StringRef StrTable,
164                                             ELFYAML::Symbol &S) {
165   S.Type = Sym->getType();
166   S.Value = Sym->st_value;
167   S.Size = Sym->st_size;
168   S.Other = Sym->st_other;
169
170   ErrorOr<StringRef> NameOrErr = Sym->getName(StrTable);
171   if (std::error_code EC = NameOrErr.getError())
172     return EC;
173   S.Name = NameOrErr.get();
174
175   ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(&*Sym);
176   if (std::error_code EC = ShdrOrErr.getError())
177     return EC;
178   const Elf_Shdr *Shdr = *ShdrOrErr;
179   if (!Shdr)
180     return obj2yaml_error::success;
181
182   NameOrErr = Obj.getSectionName(Shdr);
183   if (std::error_code EC = NameOrErr.getError())
184     return EC;
185   S.Section = NameOrErr.get();
186
187   return obj2yaml_error::success;
188 }
189
190 template <class ELFT>
191 template <class RelT>
192 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
193                                                 const RelT *Rel,
194                                                 ELFYAML::Relocation &R) {
195   R.Type = Rel->getType(Obj.isMips64EL());
196   R.Offset = Rel->r_offset;
197   R.Addend = 0;
198
199   auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
200   if (!NamePair.first)
201     return obj2yaml_error::success;
202
203   const Elf_Shdr *SymTab = NamePair.first;
204   ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
205   if (std::error_code EC = StrTabSec.getError())
206     return EC;
207   ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
208   if (std::error_code EC = StrTabOrErr.getError())
209     return EC;
210   StringRef StrTab = *StrTabOrErr;
211
212   ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab);
213   if (std::error_code EC = NameOrErr.getError())
214     return EC;
215   R.Symbol = NameOrErr.get();
216
217   return obj2yaml_error::success;
218 }
219
220 template <class ELFT>
221 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
222                                                    ELFYAML::Section &S) {
223   S.Type = Shdr->sh_type;
224   S.Flags = Shdr->sh_flags;
225   S.Address = Shdr->sh_addr;
226   S.AddressAlign = Shdr->sh_addralign;
227
228   ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
229   if (std::error_code EC = NameOrErr.getError())
230     return EC;
231   S.Name = NameOrErr.get();
232
233   if (Shdr->sh_link != ELF::SHN_UNDEF) {
234     ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
235     if (std::error_code EC = LinkSection.getError())
236       return EC;
237     NameOrErr = Obj.getSectionName(*LinkSection);
238     if (std::error_code EC = NameOrErr.getError())
239       return EC;
240     S.Link = NameOrErr.get();
241   }
242
243   return obj2yaml_error::success;
244 }
245
246 template <class ELFT>
247 std::error_code
248 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
249                                              ELFYAML::RelocationSection &S) {
250   if (std::error_code EC = dumpCommonSection(Shdr, S))
251     return EC;
252
253   ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
254   if (std::error_code EC = InfoSection.getError())
255     return EC;
256
257   ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
258   if (std::error_code EC = NameOrErr.getError())
259     return EC;
260   S.Info = NameOrErr.get();
261
262   return obj2yaml_error::success;
263 }
264
265 template <class ELFT>
266 ErrorOr<ELFYAML::RelocationSection *>
267 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
268   assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
269   auto S = make_unique<ELFYAML::RelocationSection>();
270
271   if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
272     return EC;
273
274   for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
275     ELFYAML::Relocation R;
276     if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
277       return EC;
278     S->Relocations.push_back(R);
279   }
280
281   return S.release();
282 }
283
284 template <class ELFT>
285 ErrorOr<ELFYAML::RelocationSection *>
286 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
287   assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
288   auto S = make_unique<ELFYAML::RelocationSection>();
289
290   if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
291     return EC;
292
293   for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
294        ++RI) {
295     ELFYAML::Relocation R;
296     if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
297       return EC;
298     R.Addend = RI->r_addend;
299     S->Relocations.push_back(R);
300   }
301
302   return S.release();
303 }
304
305 template <class ELFT>
306 ErrorOr<ELFYAML::RawContentSection *>
307 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
308   auto S = make_unique<ELFYAML::RawContentSection>();
309
310   if (std::error_code EC = dumpCommonSection(Shdr, *S))
311     return EC;
312
313   ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
314   if (std::error_code EC = ContentOrErr.getError())
315     return EC;
316   S->Content = yaml::BinaryRef(ContentOrErr.get());
317   S->Size = S->Content.binary_size();
318
319   return S.release();
320 }
321
322 template <class ELFT>
323 ErrorOr<ELFYAML::NoBitsSection *>
324 ELFDumper<ELFT>::dumpNoBitsSection(const Elf_Shdr *Shdr) {
325   auto S = make_unique<ELFYAML::NoBitsSection>();
326
327   if (std::error_code EC = dumpCommonSection(Shdr, *S))
328     return EC;
329   S->Size = Shdr->sh_size;
330
331   return S.release();
332 }
333
334 template <class ELFT>
335 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
336   auto S = make_unique<ELFYAML::Group>();
337
338   if (std::error_code EC = dumpCommonSection(Shdr, *S))
339     return EC;
340   // Get sh_info which is the signature.
341   ErrorOr<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Shdr->sh_link);
342   if (std::error_code EC = SymtabOrErr.getError())
343     return EC;
344   const Elf_Shdr *Symtab = *SymtabOrErr;
345   const Elf_Sym *symbol = Obj.getSymbol(Symtab, Shdr->sh_info);
346   ErrorOr<StringRef> StrTabOrErr = Obj.getStringTableForSymtab(*Symtab);
347   if (std::error_code EC = StrTabOrErr.getError())
348     return EC;
349   StringRef StrTab = *StrTabOrErr;
350   auto sectionContents = Obj.getSectionContents(Shdr);
351   if (std::error_code ec = sectionContents.getError())
352     return ec;
353   ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
354   if (std::error_code EC = symbolName.getError())
355     return EC;
356   S->Info = *symbolName;
357   const Elf_Word *groupMembers =
358       reinterpret_cast<const Elf_Word *>(sectionContents->data());
359   const long count = (Shdr->sh_size) / sizeof(Elf_Word);
360   ELFYAML::SectionOrType s;
361   for (int i = 0; i < count; i++) {
362     if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
363       s.sectionNameOrType = "GRP_COMDAT";
364     } else {
365       ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
366       if (std::error_code EC = sHdr.getError())
367         return EC;
368       ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
369       if (std::error_code ec = sectionName.getError())
370         return ec;
371       s.sectionNameOrType = *sectionName;
372     }
373     S->Members.push_back(s);
374   }
375   return S.release();
376 }
377
378 template <class ELFT>
379 ErrorOr<ELFYAML::MipsABIFlags *>
380 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
381   assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
382          "Section type is not SHT_MIPS_ABIFLAGS");
383   auto S = make_unique<ELFYAML::MipsABIFlags>();
384   if (std::error_code EC = dumpCommonSection(Shdr, *S))
385     return EC;
386
387   ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
388   if (std::error_code EC = ContentOrErr.getError())
389     return EC;
390
391   auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
392       ContentOrErr.get().data());
393   S->Version = Flags->version;
394   S->ISALevel = Flags->isa_level;
395   S->ISARevision = Flags->isa_rev;
396   S->GPRSize = Flags->gpr_size;
397   S->CPR1Size = Flags->cpr1_size;
398   S->CPR2Size = Flags->cpr2_size;
399   S->FpABI = Flags->fp_abi;
400   S->ISAExtension = Flags->isa_ext;
401   S->ASEs = Flags->ases;
402   S->Flags1 = Flags->flags1;
403   S->Flags2 = Flags->flags2;
404   return S.release();
405 }
406
407 template <class ELFT>
408 static std::error_code elf2yaml(raw_ostream &Out,
409                                 const object::ELFFile<ELFT> &Obj) {
410   ELFDumper<ELFT> Dumper(Obj);
411   ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
412   if (std::error_code EC = YAMLOrErr.getError())
413     return EC;
414
415   std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
416   yaml::Output Yout(Out);
417   Yout << *YAML;
418
419   return std::error_code();
420 }
421
422 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
423   if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
424     return elf2yaml(Out, *ELFObj->getELFFile());
425
426   if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
427     return elf2yaml(Out, *ELFObj->getELFFile());
428
429   if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
430     return elf2yaml(Out, *ELFObj->getELFFile());
431
432   if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
433     return elf2yaml(Out, *ELFObj->getELFFile());
434
435   return obj2yaml_error::unsupported_obj_file_format;
436 }