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