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