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