Return ErrorOr from getSection.
[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, bool IsDynamic,
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::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     default: {
108       ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&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     }
113     }
114   }
115
116   // Dump symbols
117   bool IsFirstSym = true;
118   for (auto SI = Obj.symbol_begin(), SE = Obj.symbol_end(); SI != SE; ++SI) {
119     if (IsFirstSym) {
120       IsFirstSym = false;
121       continue;
122     }
123
124     ELFYAML::Symbol S;
125     if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, false, S))
126       return EC;
127
128     switch (SI->getBinding())
129     {
130     case ELF::STB_LOCAL:
131       Y->Symbols.Local.push_back(S);
132       break;
133     case ELF::STB_GLOBAL:
134       Y->Symbols.Global.push_back(S);
135       break;
136     case ELF::STB_WEAK:
137       Y->Symbols.Weak.push_back(S);
138       break;
139     default:
140       llvm_unreachable("Unknown ELF symbol binding");
141     }
142   }
143
144   return Y.release();
145 }
146
147 template <class ELFT>
148 std::error_code ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, bool IsDynamic,
149                                             ELFYAML::Symbol &S) {
150   S.Type = Sym->getType();
151   S.Value = Sym->st_value;
152   S.Size = Sym->st_size;
153   S.Other = Sym->st_other;
154
155   ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym, IsDynamic);
156   if (std::error_code EC = NameOrErr.getError())
157     return EC;
158   S.Name = NameOrErr.get();
159
160   ErrorOr<const Elf_Shdr *> ShdrOrErr = Obj.getSection(&*Sym);
161   if (std::error_code EC = ShdrOrErr.getError())
162     return EC;
163   const Elf_Shdr *Shdr = *ShdrOrErr;
164   if (!Shdr)
165     return obj2yaml_error::success;
166
167   NameOrErr = Obj.getSectionName(Shdr);
168   if (std::error_code EC = NameOrErr.getError())
169     return EC;
170   S.Section = NameOrErr.get();
171
172   return obj2yaml_error::success;
173 }
174
175 template <class ELFT>
176 template <class RelT>
177 std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
178                                                 const RelT *Rel,
179                                                 ELFYAML::Relocation &R) {
180   R.Type = Rel->getType(Obj.isMips64EL());
181   R.Offset = Rel->r_offset;
182   R.Addend = 0;
183
184   auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
185   if (!NamePair.first)
186     return obj2yaml_error::success;
187
188   const Elf_Shdr *SymTab = NamePair.first;
189   ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection(SymTab->sh_link);
190   if (std::error_code EC = StrTabSec.getError())
191     return EC;
192   ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
193   if (std::error_code EC = StrTabOrErr.getError())
194     return EC;
195   StringRef StrTab = *StrTabOrErr;
196
197   ErrorOr<StringRef> NameOrErr = NamePair.second->getName(StrTab);
198   if (std::error_code EC = NameOrErr.getError())
199     return EC;
200   R.Symbol = NameOrErr.get();
201
202   return obj2yaml_error::success;
203 }
204
205 template <class ELFT>
206 std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
207                                                    ELFYAML::Section &S) {
208   S.Type = Shdr->sh_type;
209   S.Flags = Shdr->sh_flags;
210   S.Address = Shdr->sh_addr;
211   S.AddressAlign = Shdr->sh_addralign;
212
213   ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
214   if (std::error_code EC = NameOrErr.getError())
215     return EC;
216   S.Name = NameOrErr.get();
217
218   if (Shdr->sh_link != ELF::SHN_UNDEF) {
219     ErrorOr<const Elf_Shdr *> LinkSection = Obj.getSection(Shdr->sh_link);
220     if (std::error_code EC = LinkSection.getError())
221       return EC;
222     NameOrErr = Obj.getSectionName(*LinkSection);
223     if (std::error_code EC = NameOrErr.getError())
224       return EC;
225     S.Link = NameOrErr.get();
226   }
227
228   return obj2yaml_error::success;
229 }
230
231 template <class ELFT>
232 std::error_code
233 ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
234                                              ELFYAML::RelocationSection &S) {
235   if (std::error_code EC = dumpCommonSection(Shdr, S))
236     return EC;
237
238   ErrorOr<const Elf_Shdr *> InfoSection = Obj.getSection(Shdr->sh_info);
239   if (std::error_code EC = InfoSection.getError())
240     return EC;
241
242   ErrorOr<StringRef> NameOrErr = Obj.getSectionName(*InfoSection);
243   if (std::error_code EC = NameOrErr.getError())
244     return EC;
245   S.Info = NameOrErr.get();
246
247   return obj2yaml_error::success;
248 }
249
250 template <class ELFT>
251 ErrorOr<ELFYAML::RelocationSection *>
252 ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
253   assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
254   auto S = make_unique<ELFYAML::RelocationSection>();
255
256   if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
257     return EC;
258
259   for (auto RI = Obj.rel_begin(Shdr), RE = Obj.rel_end(Shdr); RI != RE; ++RI) {
260     ELFYAML::Relocation R;
261     if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
262       return EC;
263     S->Relocations.push_back(R);
264   }
265
266   return S.release();
267 }
268
269 template <class ELFT>
270 ErrorOr<ELFYAML::RelocationSection *>
271 ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
272   assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
273   auto S = make_unique<ELFYAML::RelocationSection>();
274
275   if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
276     return EC;
277
278   for (auto RI = Obj.rela_begin(Shdr), RE = Obj.rela_end(Shdr); RI != RE;
279        ++RI) {
280     ELFYAML::Relocation R;
281     if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
282       return EC;
283     R.Addend = RI->r_addend;
284     S->Relocations.push_back(R);
285   }
286
287   return S.release();
288 }
289
290 template <class ELFT>
291 ErrorOr<ELFYAML::RawContentSection *>
292 ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
293   auto S = make_unique<ELFYAML::RawContentSection>();
294
295   if (std::error_code EC = dumpCommonSection(Shdr, *S))
296     return EC;
297
298   ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
299   if (std::error_code EC = ContentOrErr.getError())
300     return EC;
301   S->Content = yaml::BinaryRef(ContentOrErr.get());
302   S->Size = S->Content.binary_size();
303
304   return S.release();
305 }
306
307 template <class ELFT>
308 ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
309   auto S = make_unique<ELFYAML::Group>();
310
311   if (std::error_code EC = dumpCommonSection(Shdr, *S))
312     return EC;
313   // Get sh_info which is the signature.
314   const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
315   ErrorOr<const Elf_Shdr *> Symtab = Obj.getSection(Shdr->sh_link);
316   if (std::error_code EC = Symtab.getError())
317     return EC;
318   ErrorOr<const Elf_Shdr *> StrTabSec = Obj.getSection((*Symtab)->sh_link);
319   if (std::error_code EC = StrTabSec.getError())
320     return EC;
321   ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(*StrTabSec);
322   if (std::error_code EC = StrTabOrErr.getError())
323     return EC;
324   StringRef StrTab = *StrTabOrErr;
325   auto sectionContents = Obj.getSectionContents(Shdr);
326   if (std::error_code ec = sectionContents.getError())
327     return ec;
328   ErrorOr<StringRef> symbolName = symbol->getName(StrTab);
329   if (std::error_code EC = symbolName.getError())
330     return EC;
331   S->Info = *symbolName;
332   const Elf_Word *groupMembers =
333       reinterpret_cast<const Elf_Word *>(sectionContents->data());
334   const long count = (Shdr->sh_size) / sizeof(Elf_Word);
335   ELFYAML::SectionOrType s;
336   for (int i = 0; i < count; i++) {
337     if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
338       s.sectionNameOrType = "GRP_COMDAT";
339     } else {
340       ErrorOr<const Elf_Shdr *> sHdr = Obj.getSection(groupMembers[i]);
341       if (std::error_code EC = sHdr.getError())
342         return EC;
343       ErrorOr<StringRef> sectionName = Obj.getSectionName(*sHdr);
344       if (std::error_code ec = sectionName.getError())
345         return ec;
346       s.sectionNameOrType = *sectionName;
347     }
348     S->Members.push_back(s);
349   }
350   return S.release();
351 }
352
353 template <class ELFT>
354 ErrorOr<ELFYAML::MipsABIFlags *>
355 ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
356   assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
357          "Section type is not SHT_MIPS_ABIFLAGS");
358   auto S = make_unique<ELFYAML::MipsABIFlags>();
359   if (std::error_code EC = dumpCommonSection(Shdr, *S))
360     return EC;
361
362   ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
363   if (std::error_code EC = ContentOrErr.getError())
364     return EC;
365
366   auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
367       ContentOrErr.get().data());
368   S->Version = Flags->version;
369   S->ISALevel = Flags->isa_level;
370   S->ISARevision = Flags->isa_rev;
371   S->GPRSize = Flags->gpr_size;
372   S->CPR1Size = Flags->cpr1_size;
373   S->CPR2Size = Flags->cpr2_size;
374   S->FpABI = Flags->fp_abi;
375   S->ISAExtension = Flags->isa_ext;
376   S->ASEs = Flags->ases;
377   S->Flags1 = Flags->flags1;
378   S->Flags2 = Flags->flags2;
379   return S.release();
380 }
381
382 template <class ELFT>
383 static std::error_code elf2yaml(raw_ostream &Out,
384                                 const object::ELFFile<ELFT> &Obj) {
385   ELFDumper<ELFT> Dumper(Obj);
386   ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
387   if (std::error_code EC = YAMLOrErr.getError())
388     return EC;
389
390   std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
391   yaml::Output Yout(Out);
392   Yout << *YAML;
393
394   return std::error_code();
395 }
396
397 std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
398   if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
399     return elf2yaml(Out, *ELFObj->getELFFile());
400
401   if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
402     return elf2yaml(Out, *ELFObj->getELFFile());
403
404   if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
405     return elf2yaml(Out, *ELFObj->getELFFile());
406
407   if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
408     return elf2yaml(Out, *ELFObj->getELFFile());
409
410   return obj2yaml_error::unsupported_obj_file_format;
411 }