ELFDump.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_MUTABLE:
  229. outs() << "OPENBSD_MUTABLE ";
  230. break;
  231. case ELF::PT_OPENBSD_RANDOMIZE:
  232. outs() << "OPENBSD_RANDOMIZE ";
  233. break;
  234. case ELF::PT_OPENBSD_WXNEEDED:
  235. outs() << "OPENBSD_WXNEEDED ";
  236. break;
  237. case ELF::PT_PHDR:
  238. outs() << " PHDR ";
  239. break;
  240. case ELF::PT_TLS:
  241. outs() << " TLS ";
  242. break;
  243. default:
  244. outs() << " UNKNOWN ";
  245. }
  246. const char *Fmt = ELFT::Is64Bits ? "0x%016" PRIx64 " " : "0x%08" PRIx64 " ";
  247. outs() << "off " << format(Fmt, (uint64_t)Phdr.p_offset) << "vaddr "
  248. << format(Fmt, (uint64_t)Phdr.p_vaddr) << "paddr "
  249. << format(Fmt, (uint64_t)Phdr.p_paddr)
  250. << format("align 2**%u\n",
  251. countTrailingZeros<uint64_t>(Phdr.p_align))
  252. << " filesz " << format(Fmt, (uint64_t)Phdr.p_filesz)
  253. << "memsz " << format(Fmt, (uint64_t)Phdr.p_memsz) << "flags "
  254. << ((Phdr.p_flags & ELF::PF_R) ? "r" : "-")
  255. << ((Phdr.p_flags & ELF::PF_W) ? "w" : "-")
  256. << ((Phdr.p_flags & ELF::PF_X) ? "x" : "-") << "\n";
  257. }
  258. }
  259. template <class ELFT>
  260. static void printSymbolVersionDependency(StringRef FileName,
  261. const ELFFile<ELFT> &Obj,
  262. const typename ELFT::Shdr &Sec) {
  263. outs() << "\nVersion References:\n";
  264. auto WarningHandler = [&](const Twine &Msg) {
  265. reportWarning(Msg, FileName);
  266. return Error::success();
  267. };
  268. Expected<std::vector<VerNeed>> V =
  269. Obj.getVersionDependencies(Sec, WarningHandler);
  270. if (!V) {
  271. reportWarning(toString(V.takeError()), FileName);
  272. return;
  273. }
  274. raw_fd_ostream &OS = outs();
  275. for (const VerNeed &VN : *V) {
  276. OS << " required from " << VN.File << ":\n";
  277. for (const VernAux &Aux : VN.AuxV)
  278. OS << format(" 0x%08x 0x%02x %02u %s\n", Aux.Hash, Aux.Flags,
  279. Aux.Other, Aux.Name.c_str());
  280. }
  281. }
  282. template <class ELFT>
  283. static void printSymbolVersionDefinition(const typename ELFT::Shdr &Shdr,
  284. ArrayRef<uint8_t> Contents,
  285. StringRef StrTab) {
  286. outs() << "\nVersion definitions:\n";
  287. const uint8_t *Buf = Contents.data();
  288. uint32_t VerdefIndex = 1;
  289. // sh_info contains the number of entries in the SHT_GNU_verdef section. To
  290. // make the index column have consistent width, we should insert blank spaces
  291. // according to sh_info.
  292. uint16_t VerdefIndexWidth = std::to_string(Shdr.sh_info).size();
  293. while (Buf) {
  294. auto *Verdef = reinterpret_cast<const typename ELFT::Verdef *>(Buf);
  295. outs() << format_decimal(VerdefIndex++, VerdefIndexWidth) << " "
  296. << format("0x%02" PRIx16 " ", (uint16_t)Verdef->vd_flags)
  297. << format("0x%08" PRIx32 " ", (uint32_t)Verdef->vd_hash);
  298. const uint8_t *BufAux = Buf + Verdef->vd_aux;
  299. uint16_t VerdauxIndex = 0;
  300. while (BufAux) {
  301. auto *Verdaux = reinterpret_cast<const typename ELFT::Verdaux *>(BufAux);
  302. if (VerdauxIndex)
  303. outs() << std::string(VerdefIndexWidth + 17, ' ');
  304. outs() << StringRef(StrTab.drop_front(Verdaux->vda_name).data()) << '\n';
  305. BufAux = Verdaux->vda_next ? BufAux + Verdaux->vda_next : nullptr;
  306. ++VerdauxIndex;
  307. }
  308. Buf = Verdef->vd_next ? Buf + Verdef->vd_next : nullptr;
  309. }
  310. }
  311. template <class ELFT>
  312. static void printSymbolVersionInfo(const ELFFile<ELFT> &Elf,
  313. StringRef FileName) {
  314. ArrayRef<typename ELFT::Shdr> Sections =
  315. unwrapOrError(Elf.sections(), FileName);
  316. for (const typename ELFT::Shdr &Shdr : Sections) {
  317. if (Shdr.sh_type != ELF::SHT_GNU_verneed &&
  318. Shdr.sh_type != ELF::SHT_GNU_verdef)
  319. continue;
  320. ArrayRef<uint8_t> Contents =
  321. unwrapOrError(Elf.getSectionContents(Shdr), FileName);
  322. const typename ELFT::Shdr *StrTabSec =
  323. unwrapOrError(Elf.getSection(Shdr.sh_link), FileName);
  324. StringRef StrTab = unwrapOrError(Elf.getStringTable(*StrTabSec), FileName);
  325. if (Shdr.sh_type == ELF::SHT_GNU_verneed)
  326. printSymbolVersionDependency<ELFT>(FileName, Elf, Shdr);
  327. else
  328. printSymbolVersionDefinition<ELFT>(Shdr, Contents, StrTab);
  329. }
  330. }
  331. void objdump::printELFFileHeader(const object::ObjectFile *Obj) {
  332. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
  333. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  334. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
  335. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  336. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
  337. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  338. else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
  339. printProgramHeaders(ELFObj->getELFFile(), Obj->getFileName());
  340. }
  341. void objdump::printELFDynamicSection(const object::ObjectFile *Obj) {
  342. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
  343. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  344. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
  345. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  346. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
  347. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  348. else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
  349. printDynamicSection(ELFObj->getELFFile(), Obj->getFileName());
  350. }
  351. void objdump::printELFSymbolVersionInfo(const object::ObjectFile *Obj) {
  352. if (const auto *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
  353. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  354. else if (const auto *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
  355. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  356. else if (const auto *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
  357. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  358. else if (const auto *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
  359. printSymbolVersionInfo(ELFObj->getELFFile(), Obj->getFileName());
  360. }