ELF.cpp 21 KB

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