blob: 73c83d897d99cbced44b3d8249fb0a00bdf487cc [file] [log] [blame]
Simon Atanasyanae6bb332014-05-14 05:07:47 +00001//===------ 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
18using namespace llvm;
19
20namespace {
21
22template <class ELFT>
23class ELFDumper {
Shankar Easwaran6fbbe202015-02-21 04:28:26 +000024 typedef object::Elf_Sym_Impl<ELFT> Elf_Sym;
Simon Atanasyanae6bb332014-05-14 05:07:47 +000025 typedef typename object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +000026 typedef typename object::ELFFile<ELFT>::Elf_Word Elf_Word;
Simon Atanasyanae6bb332014-05-14 05:07:47 +000027
28 const object::ELFFile<ELFT> &Obj;
29
Rafael Espindola719dc7c2015-06-29 12:38:31 +000030 std::error_code dumpSymbol(const Elf_Sym *Sym, bool IsDynamic,
31 ELFYAML::Symbol &S);
Rafael Espindola4453e42942014-06-13 03:07:50 +000032 std::error_code dumpCommonSection(const Elf_Shdr *Shdr, ELFYAML::Section &S);
33 std::error_code dumpCommonRelocationSection(const Elf_Shdr *Shdr,
34 ELFYAML::RelocationSection &S);
Simon Atanasyanae6bb332014-05-14 05:07:47 +000035 template <class RelT>
Rafael Espindola4453e42942014-06-13 03:07:50 +000036 std::error_code dumpRelocation(const Elf_Shdr *Shdr, const RelT *Rel,
37 ELFYAML::Relocation &R);
Simon Atanasyanae6bb332014-05-14 05:07:47 +000038
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);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +000043 ErrorOr<ELFYAML::Group *> dumpGroup(const Elf_Shdr *Shdr);
Simon Atanasyan04d9e652015-05-07 15:40:48 +000044 ErrorOr<ELFYAML::MipsABIFlags *> dumpMipsABIFlags(const Elf_Shdr *Shdr);
Simon Atanasyanae6bb332014-05-14 05:07:47 +000045
46public:
47 ELFDumper(const object::ELFFile<ELFT> &O);
48 ErrorOr<ELFYAML::Object *> dump();
49};
50
51}
52
53template <class ELFT>
54ELFDumper<ELFT>::ELFDumper(const object::ELFFile<ELFT> &O)
55 : Obj(O) {}
56
57template <class ELFT>
58ErrorOr<ELFYAML::Object *> ELFDumper<ELFT>::dump() {
59 auto Y = make_unique<ELFYAML::Object>();
60
61 // Dump header
62 Y->Header.Class = ELFYAML::ELF_ELFCLASS(Obj.getHeader()->getFileClass());
63 Y->Header.Data = ELFYAML::ELF_ELFDATA(Obj.getHeader()->getDataEncoding());
64 Y->Header.OSABI = Obj.getHeader()->e_ident[ELF::EI_OSABI];
65 Y->Header.Type = Obj.getHeader()->e_type;
66 Y->Header.Machine = Obj.getHeader()->e_machine;
67 Y->Header.Flags = Obj.getHeader()->e_flags;
68 Y->Header.Entry = Obj.getHeader()->e_entry;
69
70 // Dump sections
71 for (const Elf_Shdr &Sec : Obj.sections()) {
72 switch (Sec.sh_type) {
73 case ELF::SHT_NULL:
74 case ELF::SHT_SYMTAB:
75 case ELF::SHT_DYNSYM:
76 case ELF::SHT_STRTAB:
77 // Do not dump these sections.
78 break;
79 case ELF::SHT_RELA: {
80 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelaSection(&Sec);
Rafael Espindola4453e42942014-06-13 03:07:50 +000081 if (std::error_code EC = S.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +000082 return EC;
83 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
84 break;
85 }
86 case ELF::SHT_REL: {
87 ErrorOr<ELFYAML::RelocationSection *> S = dumpRelSection(&Sec);
Rafael Espindola4453e42942014-06-13 03:07:50 +000088 if (std::error_code EC = S.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +000089 return EC;
90 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
91 break;
92 }
Shankar Easwaran6fbbe202015-02-21 04:28:26 +000093 case ELF::SHT_GROUP: {
94 ErrorOr<ELFYAML::Group *> G = dumpGroup(&Sec);
95 if (std::error_code EC = G.getError())
96 return EC;
97 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
98 break;
99 }
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000100 case ELF::SHT_MIPS_ABIFLAGS: {
101 ErrorOr<ELFYAML::MipsABIFlags *> G = dumpMipsABIFlags(&Sec);
102 if (std::error_code EC = G.getError())
103 return EC;
104 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(G.get()));
105 break;
106 }
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000107 default: {
108 ErrorOr<ELFYAML::RawContentSection *> S = dumpContentSection(&Sec);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000109 if (std::error_code EC = S.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000110 return EC;
111 Y->Sections.push_back(std::unique_ptr<ELFYAML::Section>(S.get()));
112 }
113 }
114 }
115
116 // Dump symbols
117 bool IsFirstSym = true;
118 for (auto SI = Obj.begin_symbols(), SE = Obj.end_symbols(); SI != SE; ++SI) {
119 if (IsFirstSym) {
120 IsFirstSym = false;
121 continue;
122 }
123
124 ELFYAML::Symbol S;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000125 if (std::error_code EC = ELFDumper<ELFT>::dumpSymbol(SI, false, S))
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000126 return EC;
127
128 switch (SI->getBinding())
129 {
130 case ELF::STB_LOCAL:
131 Y->Symbols.Local.push_back(S);
132 break;
133 case ELF::STB_GLOBAL:
134 Y->Symbols.Global.push_back(S);
135 break;
136 case ELF::STB_WEAK:
137 Y->Symbols.Weak.push_back(S);
138 break;
139 default:
140 llvm_unreachable("Unknown ELF symbol binding");
141 }
142 }
143
144 return Y.release();
145}
146
147template <class ELFT>
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000148std::error_code ELFDumper<ELFT>::dumpSymbol(const Elf_Sym *Sym, bool IsDynamic,
Rafael Espindola4453e42942014-06-13 03:07:50 +0000149 ELFYAML::Symbol &S) {
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000150 S.Type = Sym->getType();
151 S.Value = Sym->st_value;
152 S.Size = Sym->st_size;
Simon Atanasyan60e1a792014-11-06 22:46:24 +0000153 S.Other = Sym->st_other;
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000154
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000155 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(Sym, IsDynamic);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000156 if (std::error_code EC = NameOrErr.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000157 return EC;
158 S.Name = NameOrErr.get();
159
160 const Elf_Shdr *Shdr = Obj.getSection(&*Sym);
161 if (!Shdr)
162 return obj2yaml_error::success;
163
164 NameOrErr = Obj.getSectionName(Shdr);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000165 if (std::error_code EC = NameOrErr.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000166 return EC;
167 S.Section = NameOrErr.get();
168
169 return obj2yaml_error::success;
170}
171
172template <class ELFT>
173template <class RelT>
Rafael Espindola4453e42942014-06-13 03:07:50 +0000174std::error_code ELFDumper<ELFT>::dumpRelocation(const Elf_Shdr *Shdr,
175 const RelT *Rel,
176 ELFYAML::Relocation &R) {
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000177 R.Type = Rel->getType(Obj.isMips64EL());
178 R.Offset = Rel->r_offset;
179 R.Addend = 0;
180
181 auto NamePair = Obj.getRelocationSymbol(Shdr, Rel);
182 if (!NamePair.first)
183 return obj2yaml_error::success;
184
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000185 const Elf_Shdr *SymTab = NamePair.first;
Rafael Espindola6a1bfb22015-06-29 14:39:25 +0000186 const Elf_Shdr *StrTabSec = Obj.getSection(SymTab->sh_link);
187 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
188 if (std::error_code EC = StrTabOrErr.getError())
189 return EC;
190 StringRef StrTab = *StrTabOrErr;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000191
192 ErrorOr<StringRef> NameOrErr = Obj.getSymbolName(StrTab, NamePair.second);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000193 if (std::error_code EC = NameOrErr.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000194 return EC;
195 R.Symbol = NameOrErr.get();
196
197 return obj2yaml_error::success;
198}
199
200template <class ELFT>
Rafael Espindola4453e42942014-06-13 03:07:50 +0000201std::error_code ELFDumper<ELFT>::dumpCommonSection(const Elf_Shdr *Shdr,
202 ELFYAML::Section &S) {
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000203 S.Type = Shdr->sh_type;
204 S.Flags = Shdr->sh_flags;
205 S.Address = Shdr->sh_addr;
206 S.AddressAlign = Shdr->sh_addralign;
207
208 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(Shdr);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000209 if (std::error_code EC = NameOrErr.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000210 return EC;
211 S.Name = NameOrErr.get();
212
213 if (Shdr->sh_link != ELF::SHN_UNDEF) {
214 if (const Elf_Shdr *LinkSection = Obj.getSection(Shdr->sh_link)) {
215 NameOrErr = Obj.getSectionName(LinkSection);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000216 if (std::error_code EC = NameOrErr.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000217 return EC;
218 S.Link = NameOrErr.get();
219 }
220 }
Simon Atanasyan87459932014-05-29 11:05:31 +0000221
222 return obj2yaml_error::success;
223}
224
225template <class ELFT>
Rafael Espindola4453e42942014-06-13 03:07:50 +0000226std::error_code
Simon Atanasyan87459932014-05-29 11:05:31 +0000227ELFDumper<ELFT>::dumpCommonRelocationSection(const Elf_Shdr *Shdr,
228 ELFYAML::RelocationSection &S) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000229 if (std::error_code EC = dumpCommonSection(Shdr, S))
Simon Atanasyan87459932014-05-29 11:05:31 +0000230 return EC;
231
232 if (const Elf_Shdr *InfoSection = Obj.getSection(Shdr->sh_info)) {
233 ErrorOr<StringRef> NameOrErr = Obj.getSectionName(InfoSection);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000234 if (std::error_code EC = NameOrErr.getError())
Simon Atanasyan87459932014-05-29 11:05:31 +0000235 return EC;
236 S.Info = NameOrErr.get();
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000237 }
Simon Atanasyan87459932014-05-29 11:05:31 +0000238
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000239 return obj2yaml_error::success;
240}
241
242template <class ELFT>
243ErrorOr<ELFYAML::RelocationSection *>
244ELFDumper<ELFT>::dumpRelSection(const Elf_Shdr *Shdr) {
245 assert(Shdr->sh_type == ELF::SHT_REL && "Section type is not SHT_REL");
246 auto S = make_unique<ELFYAML::RelocationSection>();
247
Rafael Espindola4453e42942014-06-13 03:07:50 +0000248 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000249 return EC;
250
251 for (auto RI = Obj.begin_rel(Shdr), RE = Obj.end_rel(Shdr); RI != RE;
252 ++RI) {
253 ELFYAML::Relocation R;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000254 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000255 return EC;
256 S->Relocations.push_back(R);
257 }
258
259 return S.release();
260}
261
262template <class ELFT>
263ErrorOr<ELFYAML::RelocationSection *>
264ELFDumper<ELFT>::dumpRelaSection(const Elf_Shdr *Shdr) {
265 assert(Shdr->sh_type == ELF::SHT_RELA && "Section type is not SHT_RELA");
266 auto S = make_unique<ELFYAML::RelocationSection>();
267
Rafael Espindola4453e42942014-06-13 03:07:50 +0000268 if (std::error_code EC = dumpCommonRelocationSection(Shdr, *S))
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000269 return EC;
270
271 for (auto RI = Obj.begin_rela(Shdr), RE = Obj.end_rela(Shdr); RI != RE;
272 ++RI) {
273 ELFYAML::Relocation R;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000274 if (std::error_code EC = dumpRelocation(Shdr, &*RI, R))
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000275 return EC;
276 R.Addend = RI->r_addend;
277 S->Relocations.push_back(R);
278 }
279
280 return S.release();
281}
282
283template <class ELFT>
284ErrorOr<ELFYAML::RawContentSection *>
285ELFDumper<ELFT>::dumpContentSection(const Elf_Shdr *Shdr) {
286 auto S = make_unique<ELFYAML::RawContentSection>();
287
Rafael Espindola4453e42942014-06-13 03:07:50 +0000288 if (std::error_code EC = dumpCommonSection(Shdr, *S))
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000289 return EC;
290
291 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000292 if (std::error_code EC = ContentOrErr.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000293 return EC;
Rafael Espindola97de4742014-07-03 02:01:39 +0000294 S->Content = yaml::BinaryRef(ContentOrErr.get());
Simon Atanasyanb83f3802014-05-16 16:01:00 +0000295 S->Size = S->Content.binary_size();
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000296
297 return S.release();
298}
299
300template <class ELFT>
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000301ErrorOr<ELFYAML::Group *> ELFDumper<ELFT>::dumpGroup(const Elf_Shdr *Shdr) {
302 auto S = make_unique<ELFYAML::Group>();
303
304 if (std::error_code EC = dumpCommonSection(Shdr, *S))
305 return EC;
306 // Get sh_info which is the signature.
307 const Elf_Sym *symbol = Obj.getSymbol(Shdr->sh_info);
308 const Elf_Shdr *symtab = Obj.getSection(Shdr->sh_link);
Rafael Espindola6a1bfb22015-06-29 14:39:25 +0000309 const Elf_Shdr *StrTabSec = Obj.getSection(symtab->sh_link);
310 ErrorOr<StringRef> StrTabOrErr = Obj.getStringTable(StrTabSec);
311 if (std::error_code EC = StrTabOrErr.getError())
312 return EC;
313 StringRef StrTab = *StrTabOrErr;
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000314 auto sectionContents = Obj.getSectionContents(Shdr);
315 if (std::error_code ec = sectionContents.getError())
316 return ec;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000317 ErrorOr<StringRef> symbolName = Obj.getSymbolName(StrTab, symbol);
Shankar Easwaran6fbbe202015-02-21 04:28:26 +0000318 if (std::error_code EC = symbolName.getError())
319 return EC;
320 S->Info = *symbolName;
321 const Elf_Word *groupMembers =
322 reinterpret_cast<const Elf_Word *>(sectionContents->data());
323 const long count = (Shdr->sh_size) / sizeof(Elf_Word);
324 ELFYAML::SectionOrType s;
325 for (int i = 0; i < count; i++) {
326 if (groupMembers[i] == llvm::ELF::GRP_COMDAT) {
327 s.sectionNameOrType = "GRP_COMDAT";
328 } else {
329 const Elf_Shdr *sHdr = Obj.getSection(groupMembers[i]);
330 ErrorOr<StringRef> sectionName = Obj.getSectionName(sHdr);
331 if (std::error_code ec = sectionName.getError())
332 return ec;
333 s.sectionNameOrType = *sectionName;
334 }
335 S->Members.push_back(s);
336 }
337 return S.release();
338}
339
340template <class ELFT>
Simon Atanasyan04d9e652015-05-07 15:40:48 +0000341ErrorOr<ELFYAML::MipsABIFlags *>
342ELFDumper<ELFT>::dumpMipsABIFlags(const Elf_Shdr *Shdr) {
343 assert(Shdr->sh_type == ELF::SHT_MIPS_ABIFLAGS &&
344 "Section type is not SHT_MIPS_ABIFLAGS");
345 auto S = make_unique<ELFYAML::MipsABIFlags>();
346 if (std::error_code EC = dumpCommonSection(Shdr, *S))
347 return EC;
348
349 ErrorOr<ArrayRef<uint8_t>> ContentOrErr = Obj.getSectionContents(Shdr);
350 if (std::error_code EC = ContentOrErr.getError())
351 return EC;
352
353 auto *Flags = reinterpret_cast<const object::Elf_Mips_ABIFlags<ELFT> *>(
354 ContentOrErr.get().data());
355 S->Version = Flags->version;
356 S->ISALevel = Flags->isa_level;
357 S->ISARevision = Flags->isa_rev;
358 S->GPRSize = Flags->gpr_size;
359 S->CPR1Size = Flags->cpr1_size;
360 S->CPR2Size = Flags->cpr2_size;
361 S->FpABI = Flags->fp_abi;
362 S->ISAExtension = Flags->isa_ext;
363 S->ASEs = Flags->ases;
364 S->Flags1 = Flags->flags1;
365 S->Flags2 = Flags->flags2;
366 return S.release();
367}
368
369template <class ELFT>
Rafael Espindola4453e42942014-06-13 03:07:50 +0000370static std::error_code elf2yaml(raw_ostream &Out,
371 const object::ELFFile<ELFT> &Obj) {
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000372 ELFDumper<ELFT> Dumper(Obj);
373 ErrorOr<ELFYAML::Object *> YAMLOrErr = Dumper.dump();
Rafael Espindola4453e42942014-06-13 03:07:50 +0000374 if (std::error_code EC = YAMLOrErr.getError())
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000375 return EC;
376
377 std::unique_ptr<ELFYAML::Object> YAML(YAMLOrErr.get());
378 yaml::Output Yout(Out);
379 Yout << *YAML;
380
Rui Ueyama7d099192015-06-09 15:20:42 +0000381 return std::error_code();
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000382}
383
Rafael Espindola4453e42942014-06-13 03:07:50 +0000384std::error_code elf2yaml(raw_ostream &Out, const object::ObjectFile &Obj) {
Simon Atanasyanae6bb332014-05-14 05:07:47 +0000385 if (const auto *ELFObj = dyn_cast<object::ELF32LEObjectFile>(&Obj))
386 return elf2yaml(Out, *ELFObj->getELFFile());
387
388 if (const auto *ELFObj = dyn_cast<object::ELF32BEObjectFile>(&Obj))
389 return elf2yaml(Out, *ELFObj->getELFFile());
390
391 if (const auto *ELFObj = dyn_cast<object::ELF64LEObjectFile>(&Obj))
392 return elf2yaml(Out, *ELFObj->getELFFile());
393
394 if (const auto *ELFObj = dyn_cast<object::ELF64BEObjectFile>(&Obj))
395 return elf2yaml(Out, *ELFObj->getELFFile());
396
397 return obj2yaml_error::unsupported_obj_file_format;
398}