ELF.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. //===- ELF.cpp - ELF object file implementation ---------------------------===//
  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. #include "llvm/Object/ELF.h"
  9. #include "llvm/BinaryFormat/ELF.h"
  10. #include "llvm/Support/LEB128.h"
  11. using namespace llvm;
  12. using namespace object;
  13. #define STRINGIFY_ENUM_CASE(ns, name) \
  14. case ns::name: \
  15. return #name;
  16. #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
  17. StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
  18. uint32_t Type) {
  19. switch (Machine) {
  20. case ELF::EM_X86_64:
  21. switch (Type) {
  22. #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
  23. default:
  24. break;
  25. }
  26. break;
  27. case ELF::EM_386:
  28. case ELF::EM_IAMCU:
  29. switch (Type) {
  30. #include "llvm/BinaryFormat/ELFRelocs/i386.def"
  31. default:
  32. break;
  33. }
  34. break;
  35. case ELF::EM_MIPS:
  36. switch (Type) {
  37. #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
  38. default:
  39. break;
  40. }
  41. break;
  42. case ELF::EM_AARCH64:
  43. switch (Type) {
  44. #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
  45. default:
  46. break;
  47. }
  48. break;
  49. case ELF::EM_ARM:
  50. switch (Type) {
  51. #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
  52. default:
  53. break;
  54. }
  55. break;
  56. case ELF::EM_ARC_COMPACT:
  57. case ELF::EM_ARC_COMPACT2:
  58. switch (Type) {
  59. #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
  60. default:
  61. break;
  62. }
  63. break;
  64. case ELF::EM_AVR:
  65. switch (Type) {
  66. #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
  67. default:
  68. break;
  69. }
  70. break;
  71. case ELF::EM_HEXAGON:
  72. switch (Type) {
  73. #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
  74. default:
  75. break;
  76. }
  77. break;
  78. case ELF::EM_LANAI:
  79. switch (Type) {
  80. #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
  81. default:
  82. break;
  83. }
  84. break;
  85. case ELF::EM_PPC:
  86. switch (Type) {
  87. #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
  88. default:
  89. break;
  90. }
  91. break;
  92. case ELF::EM_PPC64:
  93. switch (Type) {
  94. #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
  95. default:
  96. break;
  97. }
  98. break;
  99. case ELF::EM_RISCV:
  100. switch (Type) {
  101. #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
  102. default:
  103. break;
  104. }
  105. break;
  106. case ELF::EM_S390:
  107. switch (Type) {
  108. #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
  109. default:
  110. break;
  111. }
  112. break;
  113. case ELF::EM_SPARC:
  114. case ELF::EM_SPARC32PLUS:
  115. case ELF::EM_SPARCV9:
  116. switch (Type) {
  117. #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
  118. default:
  119. break;
  120. }
  121. break;
  122. case ELF::EM_AMDGPU:
  123. switch (Type) {
  124. #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
  125. default:
  126. break;
  127. }
  128. break;
  129. case ELF::EM_BPF:
  130. switch (Type) {
  131. #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
  132. default:
  133. break;
  134. }
  135. break;
  136. case ELF::EM_MSP430:
  137. switch (Type) {
  138. #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
  139. default:
  140. break;
  141. }
  142. break;
  143. case ELF::EM_VE:
  144. switch (Type) {
  145. #include "llvm/BinaryFormat/ELFRelocs/VE.def"
  146. default:
  147. break;
  148. }
  149. break;
  150. case ELF::EM_CSKY:
  151. switch (Type) {
  152. #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
  153. default:
  154. break;
  155. }
  156. break;
  157. default:
  158. break;
  159. }
  160. return "Unknown";
  161. }
  162. #undef ELF_RELOC
  163. uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
  164. switch (Machine) {
  165. case ELF::EM_X86_64:
  166. return ELF::R_X86_64_RELATIVE;
  167. case ELF::EM_386:
  168. case ELF::EM_IAMCU:
  169. return ELF::R_386_RELATIVE;
  170. case ELF::EM_MIPS:
  171. break;
  172. case ELF::EM_AARCH64:
  173. return ELF::R_AARCH64_RELATIVE;
  174. case ELF::EM_ARM:
  175. return ELF::R_ARM_RELATIVE;
  176. case ELF::EM_ARC_COMPACT:
  177. case ELF::EM_ARC_COMPACT2:
  178. return ELF::R_ARC_RELATIVE;
  179. case ELF::EM_AVR:
  180. break;
  181. case ELF::EM_HEXAGON:
  182. return ELF::R_HEX_RELATIVE;
  183. case ELF::EM_LANAI:
  184. break;
  185. case ELF::EM_PPC:
  186. break;
  187. case ELF::EM_PPC64:
  188. return ELF::R_PPC64_RELATIVE;
  189. case ELF::EM_RISCV:
  190. return ELF::R_RISCV_RELATIVE;
  191. case ELF::EM_S390:
  192. return ELF::R_390_RELATIVE;
  193. case ELF::EM_SPARC:
  194. case ELF::EM_SPARC32PLUS:
  195. case ELF::EM_SPARCV9:
  196. return ELF::R_SPARC_RELATIVE;
  197. case ELF::EM_CSKY:
  198. return ELF::R_CKCORE_RELATIVE;
  199. case ELF::EM_AMDGPU:
  200. break;
  201. case ELF::EM_BPF:
  202. break;
  203. default:
  204. break;
  205. }
  206. return 0;
  207. }
  208. StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
  209. switch (Machine) {
  210. case ELF::EM_ARM:
  211. switch (Type) {
  212. STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
  213. STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
  214. STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
  215. STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
  216. STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
  217. }
  218. break;
  219. case ELF::EM_HEXAGON:
  220. switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
  221. break;
  222. case ELF::EM_X86_64:
  223. switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
  224. break;
  225. case ELF::EM_MIPS:
  226. case ELF::EM_MIPS_RS3_LE:
  227. switch (Type) {
  228. STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
  229. STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
  230. STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
  231. STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
  232. }
  233. break;
  234. case ELF::EM_RISCV:
  235. switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
  236. break;
  237. default:
  238. break;
  239. }
  240. switch (Type) {
  241. STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
  242. STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
  243. STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
  244. STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
  245. STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
  246. STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
  247. STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
  248. STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
  249. STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
  250. STRINGIFY_ENUM_CASE(ELF, SHT_REL);
  251. STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
  252. STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
  253. STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
  254. STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
  255. STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
  256. STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
  257. STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
  258. STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
  259. STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
  260. STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
  261. STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
  262. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
  263. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
  264. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
  265. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
  266. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
  267. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
  268. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
  269. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
  270. STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
  271. STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
  272. STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
  273. STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
  274. STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
  275. STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
  276. default:
  277. return "Unknown";
  278. }
  279. }
  280. template <class ELFT>
  281. std::vector<typename ELFT::Rel>
  282. ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
  283. // This function decodes the contents of an SHT_RELR packed relocation
  284. // section.
  285. //
  286. // Proposal for adding SHT_RELR sections to generic-abi is here:
  287. // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
  288. //
  289. // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
  290. // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
  291. //
  292. // i.e. start with an address, followed by any number of bitmaps. The address
  293. // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
  294. // relocations each, at subsequent offsets following the last address entry.
  295. //
  296. // The bitmap entries must have 1 in the least significant bit. The assumption
  297. // here is that an address cannot have 1 in lsb. Odd addresses are not
  298. // supported.
  299. //
  300. // Excluding the least significant bit in the bitmap, each non-zero bit in
  301. // the bitmap represents a relocation to be applied to a corresponding machine
  302. // word that follows the base address word. The second least significant bit
  303. // represents the machine word immediately following the initial address, and
  304. // each bit that follows represents the next word, in linear order. As such,
  305. // a single bitmap can encode up to 31 relocations in a 32-bit object, and
  306. // 63 relocations in a 64-bit object.
  307. //
  308. // This encoding has a couple of interesting properties:
  309. // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
  310. // even means address, odd means bitmap.
  311. // 2. Just a simple list of addresses is a valid encoding.
  312. Elf_Rel Rel;
  313. Rel.r_info = 0;
  314. Rel.setType(getRelativeRelocationType(), false);
  315. std::vector<Elf_Rel> Relocs;
  316. // Word type: uint32_t for Elf32, and uint64_t for Elf64.
  317. typedef typename ELFT::uint Word;
  318. // Word size in number of bytes.
  319. const size_t WordSize = sizeof(Word);
  320. // Number of bits used for the relocation offsets bitmap.
  321. // These many relative relocations can be encoded in a single entry.
  322. const size_t NBits = 8*WordSize - 1;
  323. Word Base = 0;
  324. for (const Elf_Relr &R : relrs) {
  325. Word Entry = R;
  326. if ((Entry&1) == 0) {
  327. // Even entry: encodes the offset for next relocation.
  328. Rel.r_offset = Entry;
  329. Relocs.push_back(Rel);
  330. // Set base offset for subsequent bitmap entries.
  331. Base = Entry + WordSize;
  332. continue;
  333. }
  334. // Odd entry: encodes bitmap for relocations starting at base.
  335. Word Offset = Base;
  336. while (Entry != 0) {
  337. Entry >>= 1;
  338. if ((Entry&1) != 0) {
  339. Rel.r_offset = Offset;
  340. Relocs.push_back(Rel);
  341. }
  342. Offset += WordSize;
  343. }
  344. // Advance base offset by NBits words.
  345. Base += NBits * WordSize;
  346. }
  347. return Relocs;
  348. }
  349. template <class ELFT>
  350. Expected<std::vector<typename ELFT::Rela>>
  351. ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
  352. // This function reads relocations in Android's packed relocation format,
  353. // which is based on SLEB128 and delta encoding.
  354. Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
  355. if (!ContentsOrErr)
  356. return ContentsOrErr.takeError();
  357. const uint8_t *Cur = ContentsOrErr->begin();
  358. const uint8_t *End = ContentsOrErr->end();
  359. if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
  360. Cur[2] != 'S' || Cur[3] != '2')
  361. return createError("invalid packed relocation header");
  362. Cur += 4;
  363. const char *ErrStr = nullptr;
  364. auto ReadSLEB = [&]() -> int64_t {
  365. if (ErrStr)
  366. return 0;
  367. unsigned Len;
  368. int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
  369. Cur += Len;
  370. return Result;
  371. };
  372. uint64_t NumRelocs = ReadSLEB();
  373. uint64_t Offset = ReadSLEB();
  374. uint64_t Addend = 0;
  375. if (ErrStr)
  376. return createError(ErrStr);
  377. std::vector<Elf_Rela> Relocs;
  378. Relocs.reserve(NumRelocs);
  379. while (NumRelocs) {
  380. uint64_t NumRelocsInGroup = ReadSLEB();
  381. if (NumRelocsInGroup > NumRelocs)
  382. return createError("relocation group unexpectedly large");
  383. NumRelocs -= NumRelocsInGroup;
  384. uint64_t GroupFlags = ReadSLEB();
  385. bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
  386. bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
  387. bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
  388. bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
  389. uint64_t GroupOffsetDelta;
  390. if (GroupedByOffsetDelta)
  391. GroupOffsetDelta = ReadSLEB();
  392. uint64_t GroupRInfo;
  393. if (GroupedByInfo)
  394. GroupRInfo = ReadSLEB();
  395. if (GroupedByAddend && GroupHasAddend)
  396. Addend += ReadSLEB();
  397. if (!GroupHasAddend)
  398. Addend = 0;
  399. for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
  400. Elf_Rela R;
  401. Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
  402. R.r_offset = Offset;
  403. R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
  404. if (GroupHasAddend && !GroupedByAddend)
  405. Addend += ReadSLEB();
  406. R.r_addend = Addend;
  407. Relocs.push_back(R);
  408. if (ErrStr)
  409. return createError(ErrStr);
  410. }
  411. if (ErrStr)
  412. return createError(ErrStr);
  413. }
  414. return Relocs;
  415. }
  416. template <class ELFT>
  417. std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
  418. uint64_t Type) const {
  419. #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
  420. case value: \
  421. return #tag;
  422. #define DYNAMIC_TAG(n, v)
  423. switch (Arch) {
  424. case ELF::EM_AARCH64:
  425. switch (Type) {
  426. #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
  427. #include "llvm/BinaryFormat/DynamicTags.def"
  428. #undef AARCH64_DYNAMIC_TAG
  429. }
  430. break;
  431. case ELF::EM_HEXAGON:
  432. switch (Type) {
  433. #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
  434. #include "llvm/BinaryFormat/DynamicTags.def"
  435. #undef HEXAGON_DYNAMIC_TAG
  436. }
  437. break;
  438. case ELF::EM_MIPS:
  439. switch (Type) {
  440. #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
  441. #include "llvm/BinaryFormat/DynamicTags.def"
  442. #undef MIPS_DYNAMIC_TAG
  443. }
  444. break;
  445. case ELF::EM_PPC64:
  446. switch (Type) {
  447. #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
  448. #include "llvm/BinaryFormat/DynamicTags.def"
  449. #undef PPC64_DYNAMIC_TAG
  450. }
  451. break;
  452. }
  453. #undef DYNAMIC_TAG
  454. switch (Type) {
  455. // Now handle all dynamic tags except the architecture specific ones
  456. #define AARCH64_DYNAMIC_TAG(name, value)
  457. #define MIPS_DYNAMIC_TAG(name, value)
  458. #define HEXAGON_DYNAMIC_TAG(name, value)
  459. #define PPC64_DYNAMIC_TAG(name, value)
  460. // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
  461. #define DYNAMIC_TAG_MARKER(name, value)
  462. #define DYNAMIC_TAG(name, value) case value: return #name;
  463. #include "llvm/BinaryFormat/DynamicTags.def"
  464. #undef DYNAMIC_TAG
  465. #undef AARCH64_DYNAMIC_TAG
  466. #undef MIPS_DYNAMIC_TAG
  467. #undef HEXAGON_DYNAMIC_TAG
  468. #undef PPC64_DYNAMIC_TAG
  469. #undef DYNAMIC_TAG_MARKER
  470. #undef DYNAMIC_STRINGIFY_ENUM
  471. default:
  472. return "<unknown:>0x" + utohexstr(Type, true);
  473. }
  474. }
  475. template <class ELFT>
  476. std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
  477. return getDynamicTagAsString(getHeader().e_machine, Type);
  478. }
  479. template <class ELFT>
  480. Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
  481. ArrayRef<Elf_Dyn> Dyn;
  482. auto ProgramHeadersOrError = program_headers();
  483. if (!ProgramHeadersOrError)
  484. return ProgramHeadersOrError.takeError();
  485. for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
  486. if (Phdr.p_type == ELF::PT_DYNAMIC) {
  487. Dyn = makeArrayRef(
  488. reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
  489. Phdr.p_filesz / sizeof(Elf_Dyn));
  490. break;
  491. }
  492. }
  493. // If we can't find the dynamic section in the program headers, we just fall
  494. // back on the sections.
  495. if (Dyn.empty()) {
  496. auto SectionsOrError = sections();
  497. if (!SectionsOrError)
  498. return SectionsOrError.takeError();
  499. for (const Elf_Shdr &Sec : *SectionsOrError) {
  500. if (Sec.sh_type == ELF::SHT_DYNAMIC) {
  501. Expected<ArrayRef<Elf_Dyn>> DynOrError =
  502. getSectionContentsAsArray<Elf_Dyn>(Sec);
  503. if (!DynOrError)
  504. return DynOrError.takeError();
  505. Dyn = *DynOrError;
  506. break;
  507. }
  508. }
  509. if (!Dyn.data())
  510. return ArrayRef<Elf_Dyn>();
  511. }
  512. if (Dyn.empty())
  513. // TODO: this error is untested.
  514. return createError("invalid empty dynamic section");
  515. if (Dyn.back().d_tag != ELF::DT_NULL)
  516. // TODO: this error is untested.
  517. return createError("dynamic sections must be DT_NULL terminated");
  518. return Dyn;
  519. }
  520. template <class ELFT>
  521. Expected<const uint8_t *>
  522. ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const {
  523. auto ProgramHeadersOrError = program_headers();
  524. if (!ProgramHeadersOrError)
  525. return ProgramHeadersOrError.takeError();
  526. llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
  527. for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
  528. if (Phdr.p_type == ELF::PT_LOAD)
  529. LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
  530. auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
  531. const Elf_Phdr_Impl<ELFT> *B) {
  532. return A->p_vaddr < B->p_vaddr;
  533. };
  534. if (!llvm::is_sorted(LoadSegments, SortPred)) {
  535. if (Error E =
  536. WarnHandler("loadable segments are unsorted by virtual address"))
  537. return std::move(E);
  538. llvm::stable_sort(LoadSegments, SortPred);
  539. }
  540. const Elf_Phdr *const *I = llvm::upper_bound(
  541. LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
  542. return VAddr < Phdr->p_vaddr;
  543. });
  544. if (I == LoadSegments.begin())
  545. return createError("virtual address is not in any segment: 0x" +
  546. Twine::utohexstr(VAddr));
  547. --I;
  548. const Elf_Phdr &Phdr = **I;
  549. uint64_t Delta = VAddr - Phdr.p_vaddr;
  550. if (Delta >= Phdr.p_filesz)
  551. return createError("virtual address is not in any segment: 0x" +
  552. Twine::utohexstr(VAddr));
  553. uint64_t Offset = Phdr.p_offset + Delta;
  554. if (Offset >= getBufSize())
  555. return createError("can't map virtual address 0x" +
  556. Twine::utohexstr(VAddr) + " to the segment with index " +
  557. Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
  558. ": the segment ends at 0x" +
  559. Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
  560. ", which is greater than the file size (0x" +
  561. Twine::utohexstr(getBufSize()) + ")");
  562. return base() + Offset;
  563. }
  564. template class llvm::object::ELFFile<ELF32LE>;
  565. template class llvm::object::ELFFile<ELF32BE>;
  566. template class llvm::object::ELFFile<ELF64LE>;
  567. template class llvm::object::ELFFile<ELF64BE>;