ELFDump.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //===-- ELFDump.cpp - ELF-specific dumper -----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// This file implements the ELF-specific dumper for llvm-objdump.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "ELFDump.h"
  14. #include "llvm-objdump.h"
  15. #include "llvm/Demangle/Demangle.h"
  16. #include "llvm/Object/ELFObjectFile.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/MathExtras.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. using namespace llvm::object;
  22. using namespace llvm::objdump;
  23. template <class ELFT>
  24. static Expected<StringRef> getDynamicStrTab(const ELFFile<ELFT> &Elf) {
  25. auto DynamicEntriesOrError = Elf.dynamicEntries();
  26. if (!DynamicEntriesOrError)
  27. return DynamicEntriesOrError.takeError();
  28. for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) {
  29. if (Dyn.d_tag == ELF::DT_STRTAB) {
  30. auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr());
  31. if (!MappedAddrOrError)
  32. consumeError(MappedAddrOrError.takeError());
  33. return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError));
  34. }
  35. }
  36. // If the dynamic segment is not present, we fall back on the sections.
  37. auto SectionsOrError = Elf.sections();
  38. if (!SectionsOrError)
  39. return SectionsOrError.takeError();
  40. for (const typename ELFT::Shdr &Sec : *SectionsOrError) {
  41. if (Sec.sh_type == ELF::SHT_DYNSYM)
  42. return Elf.getStringTableForSymtab(Sec);
  43. }
  44. return createError("dynamic string table not found");
  45. }
  46. template <class ELFT>
  47. static Error getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
  48. const RelocationRef &RelRef,
  49. SmallVectorImpl<char> &Result) {
  50. const ELFFile<ELFT> &EF = Obj->getELFFile();
  51. DataRefImpl Rel = RelRef.getRawDataRefImpl();
  52. auto SecOrErr = EF.getSection(Rel.d.a);
  53. if (!SecOrErr)
  54. return SecOrErr.takeError();
  55. int64_t Addend = 0;
  56. // If there is no Symbol associated with the relocation, we set the undef
  57. // boolean value to 'true'. This will prevent us from calling functions that
  58. // requires the relocation to be associated with a symbol.
  59. //
  60. // In SHT_REL case we would need to read the addend from section data.
  61. // GNU objdump does not do that and we just follow for simplicity atm.
  62. bool Undef = false;
  63. if ((*SecOrErr)->sh_type == ELF::SHT_RELA) {
  64. const typename ELFT::Rela *ERela = Obj->getRela(Rel);
  65. Addend = ERela->r_addend;
  66. Undef = ERela->getSymbol(false) == 0;
  67. } else if ((*SecOrErr)->sh_type == ELF::SHT_REL) {
  68. const typename ELFT::Rel *ERel = Obj->getRel(Rel);
  69. Undef = ERel->getSymbol(false) == 0;
  70. } else {
  71. return make_error<BinaryError>();
  72. }
  73. // Default scheme is to print Target, as well as "+ <addend>" for nonzero
  74. // addend. Should be acceptable for all normal purposes.
  75. std::string FmtBuf;
  76. raw_string_ostream Fmt(FmtBuf);
  77. if (!Undef) {
  78. symbol_iterator SI = RelRef.getSymbol();
  79. Expected<const typename ELFT::Sym *> SymOrErr =
  80. Obj->getSymbol(SI->getRawDataRefImpl());
  81. // TODO: test this error.
  82. if (!SymOrErr)
  83. return SymOrErr.takeError();
  84. if ((*SymOrErr)->getType() == ELF::STT_SECTION) {
  85. Expected<section_iterator> SymSI = SI->getSection();
  86. if (!SymSI)
  87. return SymSI.takeError();
  88. const typename ELFT::Shdr *SymSec =
  89. Obj->getSection((*SymSI)->getRawDataRefImpl());
  90. auto SecName = EF.getSectionName(*SymSec);
  91. if (!SecName)
  92. return SecName.takeError();
  93. Fmt << *SecName;
  94. } else {
  95. Expected<StringRef> SymName = SI->getName();
  96. if (!SymName)
  97. return SymName.takeError();
  98. if (Demangle)
  99. Fmt << demangle(std::string(*SymName));
  100. else
  101. Fmt << *SymName;
  102. }
  103. } else {
  104. Fmt << "*ABS*";
  105. }
  106. if (Addend != 0) {
  107. Fmt << (Addend < 0
  108. ? "-"
  109. : "+") << format("0x%" PRIx64,
  110. (Addend < 0 ? -(uint64_t)Addend : (uint64_t)Addend));
  111. }
  112. Fmt.flush();
  113. Result.append(FmtBuf.begin(), FmtBuf.end());
  114. return Error::success();
  115. }
  116. Error objdump::getELFRelocationValueString(const ELFObjectFileBase *Obj,
  117. const RelocationRef &Rel,
  118. SmallVectorImpl<char> &Result) {
  119. if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
  120. return getRelocationValueString(ELF32LE, Rel, Result);
  121. if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
  122. return getRelocationValueString(ELF64LE, Rel, Result);
  123. if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
  124. return getRelocationValueString(ELF32BE, Rel, Result);
  125. auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
  126. return getRelocationValueString(ELF64BE, Rel, Result);
  127. }
  128. template <class ELFT>
  129. static uint64_t getSectionLMA(const ELFFile<ELFT> &Obj,
  130. const object::ELFSectionRef &Sec) {
  131. auto PhdrRangeOrErr = Obj.program_headers();
  132. if (!PhdrRangeOrErr)
  133. report_fatal_error(Twine(toString(PhdrRangeOrErr.takeError())));
  134. // Search for a PT_LOAD segment containing the requested section. Use this
  135. // segment's p_addr to calculate the section's LMA.
  136. for (const typename ELFT::Phdr &Phdr : *PhdrRangeOrErr)
  137. if ((Phdr.p_type == ELF::PT_LOAD) && (Phdr.p_vaddr <= Sec.getAddress()) &&
  138. (Phdr.p_vaddr + Phdr.p_memsz > Sec.getAddress()))
  139. return Sec.getAddress() - Phdr.p_vaddr + Phdr.p_paddr;
  140. // Return section's VMA if it isn't in a PT_LOAD segment.
  141. return Sec.getAddress();
  142. }
  143. uint64_t objdump::getELFSectionLMA(const object::ELFSectionRef &Sec) {
  144. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Sec.getObject()))
  145. return getSectionLMA(ELFObj->getELFFile(), Sec);
  146. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Sec.getObject()))
  147. return getSectionLMA(ELFObj->getELFFile(), Sec);
  148. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Sec.getObject()))
  149. return getSectionLMA(ELFObj->getELFFile(), Sec);
  150. const auto *ELFObj = cast<ELF64BEObjectFile>(Sec.getObject());
  151. return getSectionLMA(ELFObj->getELFFile(), Sec);
  152. }
  153. template <class ELFT>
  154. static void printDynamicSection(const ELFFile<ELFT> &Elf, StringRef Filename) {
  155. auto DynamicEntriesOrErr = Elf.dynamicEntries();
  156. if (!DynamicEntriesOrErr) {
  157. reportWarning(toString(DynamicEntriesOrErr.takeError()), Filename);
  158. return;
  159. }
  160. ArrayRef<typename ELFT::Dyn> DynamicEntries = *DynamicEntriesOrErr;
  161. // Find the maximum tag name length to format the value column properly.
  162. size_t MaxLen = 0;
  163. for (const typename ELFT::Dyn &Dyn : DynamicEntries)
  164. MaxLen = std::max(MaxLen, Elf.getDynamicTagAsString(Dyn.d_tag).size());
  165. std::string TagFmt = " %-" + std::to_string(MaxLen) + "s ";
  166. outs() << "\nDynamic Section:\n";
  167. for (const typename ELFT::Dyn &Dyn : DynamicEntries) {
  168. if (Dyn.d_tag == ELF::DT_NULL)
  169. continue;
  170. std::string Str = Elf.getDynamicTagAsString(Dyn.d_tag);
  171. outs() << format(TagFmt.c_str(), Str.c_str());
  172. const char *Fmt =
  173. ELFT::Is64Bits ? "0x%016" PRIx64 "\n" : "0x%08" PRIx64 "\n";
  174. if (Dyn.d_tag == ELF::DT_NEEDED || Dyn.d_tag == ELF::DT_RPATH ||
  175. Dyn.d_tag == ELF::DT_RUNPATH || Dyn.d_tag == ELF::DT_SONAME ||
  176. Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER) {
  177. Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
  178. if (StrTabOrErr) {
  179. const char *Data = StrTabOrErr.get().data();
  180. outs() << (Data + Dyn.d_un.d_val) << "\n";
  181. continue;
  182. }
  183. reportWarning(toString(StrTabOrErr.takeError()), Filename);
  184. consumeError(StrTabOrErr.takeError());
  185. }
  186. outs() << format(Fmt, (uint64_t)Dyn.d_un.d_val);
  187. }
  188. }
  189. template <class ELFT>
  190. static void printProgramHeaders(const ELFFile<ELFT> &Obj, StringRef FileName) {
  191. outs() << "\nProgram Header:\n";
  192. auto ProgramHeaderOrError = Obj.program_headers();
  193. if (!ProgramHeaderOrError) {
  194. reportWarning("unable to read program headers: " +
  195. toString(ProgramHeaderOrError.takeError()),
  196. FileName);
  197. return;
  198. }
  199. for (const typename ELFT::Phdr &Phdr : *ProgramHeaderOrError) {
  200. switch (Phdr.p_type) {
  201. case ELF::PT_DYNAMIC:
  202. outs() << " DYNAMIC ";
  203. break;
  204. case ELF::PT_GNU_EH_FRAME:
  205. outs() << "EH_FRAME ";
  206. break;
  207. case ELF::PT_GNU_RELRO:
  208. outs() << " RELRO ";
  209. break;
  210. case ELF::PT_GNU_PROPERTY:
  211. outs() << " PROPERTY ";
  212. break;
  213. case ELF::PT_GNU_STACK:
  214. outs() << " STACK ";
  215. break;
  216. case ELF::PT_INTERP:
  217. outs() << " INTERP ";
  218. break;
  219. case ELF::PT_LOAD:
  220. outs() << " LOAD ";
  221. break;
  222. case ELF::PT_NOTE:
  223. outs() << " NOTE ";
  224. break;
  225. case ELF::PT_OPENBSD_BOOTDATA:
  226. outs() << " OPENBSD_BOOTDATA ";
  227. break;
  228. case ELF::PT_OPENBSD_RANDOMIZE:
  229. outs() << " OPENBSD_RANDOMIZE ";
  230. break;
  231. case ELF::PT_OPENBSD_WXNEEDED:
  232. outs() << " OPENBSD_WXNEEDED ";
  233. break;
  234. case ELF::PT_PHDR:
  235. outs() << " PHDR ";
  236. break;
  237. case ELF::PT_TLS:
  238. outs() << " TLS ";
  239. break;
  240. default:
  241. outs() << " UNKNOWN ";
  242. }
  243. const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
  244. outs() << "off " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
  245. << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
  246. << format(Fmt, (uint64_t)Phdr.p_paddr)
  247. << format("align 2**%u\n",
  248. countTrailingZeros<uint64_t>(Phdr.p_align))
  249. << " filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
  250. << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
  251. << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
  252. << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
  253. << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
  254. }
  255. }
  256. template <class ELFT>
  257. static void printSymbolVersionDependency(ArrayRef<uint8_t> Contents,
  258. StringRef StrTab) {
  259. outs() << "\nVersion References:\n";
  260. const uint8_t *Buf = Contents.data();
  261. while (Buf) {
  262. auto *Verneed = reinterpret_cast<const typename ELFT::Verneed *>(Buf);
  263. outs() << " required from "
  264. << StringRef(StrTab.drop_front(Verneed->vn_file).data()) << ":\n";
  265. const uint8_t *BufAux = Buf + Verneed->vn_aux;
  266. while (BufAux) {
  267. auto *Vernaux = reinterpret_cast<const typename ELFT::Vernaux *>(BufAux);
  268. outs() << " "
  269. << format("0x%08" PRIx32 " ", (uint32_t)Vernaux->vna_hash)
  270. << format("0x%02" PRIx16 " ", (uint16_t)Vernaux->vna_flags)
  271. << format("%02" PRIu16 " ", (uint16_t)Vernaux->vna_other)
  272. << StringRef(StrTab.drop_front(Vernaux->vna_name).data()) << '\n';
  273. BufAux = Vernaux->vna_next ? BufAux + Vernaux->vna_next : nullptr;
  274. }
  275. Buf = Verneed->vn_next ? Buf + Verneed->vn_next : nullptr;
  276. }
  277. }
  278. template <class ELFT>
  279. static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr,
  280. ArrayRef<uint8_t> Contents,
  281. StringRef StrTab) {
  282. outs() << "\nVersion definitions:\n";
  283. const uint8_t *Buf = Contents.data();
  284. uint32_t VerdefIndex = 1;
  285. // sh_info contains the number of entries in the SHT_GNU_verdef section. To
  286. // make the index column have consistent width, we should insert blank spaces
  287. // according to sh_info.
  288. uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size();
  289. while (Buf) {
  290. auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf);
  291. outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " "
  292. << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags)
  293. << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash);
  294. const uint8_t *BufAux = Buf + Verdef->vd_aux;
  295. uint16_t VerdauxIndex = 0;
  296. while (BufAux) {
  297. auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux);
  298. if (VerdauxIndex)
  299. outs() << std::string(VerdefIndexWidth + 17, ' ');
  300. outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n';
  301. BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
  302. ++VerdauxIndex;
  303. }
  304. Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
  305. }
  306. }
  307. template <class ELFT>
  308. static void printSymbolVersionInfo(const ELFFile<ELFT> &Elf,
  309. StringRef FileName) {
  310. ArrayRef<typename ELFT::Shdr> Sections =
  311. unwrapOrError(Elf.sections(), FileName);
  312. for (const typename ELFT::Shdr &Shdr : Sections) {
  313. if (Shdr.sh_type != ELF::SHT_GNU_verneed &&
  314. Shdr.sh_type != ELF::SHT_GNU_verdef)
  315. continue;
  316. ArrayRef<uint8_t> Contents =
  317. unwrapOrError(Elf.getSectionContents(Shdr), FileName);
  318. const typename ELFT::Shdr *StrTabSec =
  319. unwrapOrError(Elf.getSection(Shdr.sh_link), FileName);
  320. StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName);
  321. if (Shdr.sh_type == ELF::SHT_GNU_verneed)
  322. printSymbolVersionDependency<ELFT>(Contents, StrTab);
  323. else
  324. printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
  325. }
  326. }
  327. void objdump::printELFFileHeader(const object::ObjectFile *Obj) {
  328. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
  329. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  330. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
  331. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  332. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
  333. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  334. else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
  335. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  336. }
  337. void objdump::printELFDynamicSection(const object::ObjectFile *Obj) {
  338. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
  339. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  340. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
  341. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  342. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
  343. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  344. else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
  345. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  346. }
  347. void objdump::printELFSymbolVersionInfo(const object::ObjectFile *Obj) {
  348. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
  349. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  350. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
  351. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  352. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
  353. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  354. else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
  355. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  356. }