elf2yaml.cpp 14 KB

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