ELF.cpp 20 KB

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