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