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