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