RelocationResolver.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. //===- RelocationResolver.cpp ------------------------------------*- 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. // This file defines utilities to resolve relocations in object files.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/RelocationResolver.h"
  13. namespace llvm {
  14. namespace object {
  15. static int64_t getELFAddend(RelocationRef R) {
  16. Expected<int64_t> AddendOrErr = ELFRelocationRef(R).getAddend();
  17. handleAllErrors(AddendOrErr.takeError(), [](const ErrorInfoBase &EI) {
  18. report_fatal_error(EI.message());
  19. });
  20. return *AddendOrErr;
  21. }
  22. static bool supportsX86_64(uint64_t Type) {
  23. switch (Type) {
  24. case ELF::R_X86_64_NONE:
  25. case ELF::R_X86_64_64:
  26. case ELF::R_X86_64_DTPOFF32:
  27. case ELF::R_X86_64_DTPOFF64:
  28. case ELF::R_X86_64_PC32:
  29. case ELF::R_X86_64_PC64:
  30. case ELF::R_X86_64_32:
  31. case ELF::R_X86_64_32S:
  32. return true;
  33. default:
  34. return false;
  35. }
  36. }
  37. static uint64_t resolveX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
  38. uint64_t LocData, int64_t Addend) {
  39. switch (Type) {
  40. case ELF::R_X86_64_NONE:
  41. return LocData;
  42. case ELF::R_X86_64_64:
  43. case ELF::R_X86_64_DTPOFF32:
  44. case ELF::R_X86_64_DTPOFF64:
  45. return S + Addend;
  46. case ELF::R_X86_64_PC32:
  47. case ELF::R_X86_64_PC64:
  48. return S + Addend - Offset;
  49. case ELF::R_X86_64_32:
  50. case ELF::R_X86_64_32S:
  51. return (S + Addend) & 0xFFFFFFFF;
  52. default:
  53. llvm_unreachable("Invalid relocation type");
  54. }
  55. }
  56. static bool supportsAArch64(uint64_t Type) {
  57. switch (Type) {
  58. case ELF::R_AARCH64_ABS32:
  59. case ELF::R_AARCH64_ABS64:
  60. case ELF::R_AARCH64_PREL32:
  61. case ELF::R_AARCH64_PREL64:
  62. return true;
  63. default:
  64. return false;
  65. }
  66. }
  67. static uint64_t resolveAArch64(uint64_t Type, uint64_t Offset, uint64_t S,
  68. uint64_t /*LocData*/, int64_t Addend) {
  69. switch (Type) {
  70. case ELF::R_AARCH64_ABS32:
  71. return (S + Addend) & 0xFFFFFFFF;
  72. case ELF::R_AARCH64_ABS64:
  73. return S + Addend;
  74. case ELF::R_AARCH64_PREL32:
  75. return (S + Addend - Offset) & 0xFFFFFFFF;
  76. case ELF::R_AARCH64_PREL64:
  77. return S + Addend - Offset;
  78. default:
  79. llvm_unreachable("Invalid relocation type");
  80. }
  81. }
  82. static bool supportsBPF(uint64_t Type) {
  83. switch (Type) {
  84. case ELF::R_BPF_64_32:
  85. case ELF::R_BPF_64_64:
  86. return true;
  87. default:
  88. return false;
  89. }
  90. }
  91. static uint64_t resolveBPF(uint64_t Type, uint64_t Offset, uint64_t S,
  92. uint64_t LocData, int64_t /*Addend*/) {
  93. switch (Type) {
  94. case ELF::R_BPF_64_32:
  95. return (S + LocData) & 0xFFFFFFFF;
  96. case ELF::R_BPF_64_64:
  97. return S + LocData;
  98. default:
  99. llvm_unreachable("Invalid relocation type");
  100. }
  101. }
  102. static bool supportsMips64(uint64_t Type) {
  103. switch (Type) {
  104. case ELF::R_MIPS_32:
  105. case ELF::R_MIPS_64:
  106. case ELF::R_MIPS_TLS_DTPREL64:
  107. case ELF::R_MIPS_PC32:
  108. return true;
  109. default:
  110. return false;
  111. }
  112. }
  113. static uint64_t resolveMips64(uint64_t Type, uint64_t Offset, uint64_t S,
  114. uint64_t /*LocData*/, int64_t Addend) {
  115. switch (Type) {
  116. case ELF::R_MIPS_32:
  117. return (S + Addend) & 0xFFFFFFFF;
  118. case ELF::R_MIPS_64:
  119. return S + Addend;
  120. case ELF::R_MIPS_TLS_DTPREL64:
  121. return S + Addend - 0x8000;
  122. case ELF::R_MIPS_PC32:
  123. return S + Addend - Offset;
  124. default:
  125. llvm_unreachable("Invalid relocation type");
  126. }
  127. }
  128. static bool supportsMSP430(uint64_t Type) {
  129. switch (Type) {
  130. case ELF::R_MSP430_32:
  131. case ELF::R_MSP430_16_BYTE:
  132. return true;
  133. default:
  134. return false;
  135. }
  136. }
  137. static uint64_t resolveMSP430(uint64_t Type, uint64_t Offset, uint64_t S,
  138. uint64_t /*LocData*/, int64_t Addend) {
  139. switch (Type) {
  140. case ELF::R_MSP430_32:
  141. return (S + Addend) & 0xFFFFFFFF;
  142. case ELF::R_MSP430_16_BYTE:
  143. return (S + Addend) & 0xFFFF;
  144. default:
  145. llvm_unreachable("Invalid relocation type");
  146. }
  147. }
  148. static bool supportsPPC64(uint64_t Type) {
  149. switch (Type) {
  150. case ELF::R_PPC64_ADDR32:
  151. case ELF::R_PPC64_ADDR64:
  152. case ELF::R_PPC64_REL32:
  153. case ELF::R_PPC64_REL64:
  154. return true;
  155. default:
  156. return false;
  157. }
  158. }
  159. static uint64_t resolvePPC64(uint64_t Type, uint64_t Offset, uint64_t S,
  160. uint64_t /*LocData*/, int64_t Addend) {
  161. switch (Type) {
  162. case ELF::R_PPC64_ADDR32:
  163. return (S + Addend) & 0xFFFFFFFF;
  164. case ELF::R_PPC64_ADDR64:
  165. return S + Addend;
  166. case ELF::R_PPC64_REL32:
  167. return (S + Addend - Offset) & 0xFFFFFFFF;
  168. case ELF::R_PPC64_REL64:
  169. return S + Addend - Offset;
  170. default:
  171. llvm_unreachable("Invalid relocation type");
  172. }
  173. }
  174. static bool supportsSystemZ(uint64_t Type) {
  175. switch (Type) {
  176. case ELF::R_390_32:
  177. case ELF::R_390_64:
  178. return true;
  179. default:
  180. return false;
  181. }
  182. }
  183. static uint64_t resolveSystemZ(uint64_t Type, uint64_t Offset, uint64_t S,
  184. uint64_t /*LocData*/, int64_t Addend) {
  185. switch (Type) {
  186. case ELF::R_390_32:
  187. return (S + Addend) & 0xFFFFFFFF;
  188. case ELF::R_390_64:
  189. return S + Addend;
  190. default:
  191. llvm_unreachable("Invalid relocation type");
  192. }
  193. }
  194. static bool supportsSparc64(uint64_t Type) {
  195. switch (Type) {
  196. case ELF::R_SPARC_32:
  197. case ELF::R_SPARC_64:
  198. case ELF::R_SPARC_UA32:
  199. case ELF::R_SPARC_UA64:
  200. return true;
  201. default:
  202. return false;
  203. }
  204. }
  205. static uint64_t resolveSparc64(uint64_t Type, uint64_t Offset, uint64_t S,
  206. uint64_t /*LocData*/, int64_t Addend) {
  207. switch (Type) {
  208. case ELF::R_SPARC_32:
  209. case ELF::R_SPARC_64:
  210. case ELF::R_SPARC_UA32:
  211. case ELF::R_SPARC_UA64:
  212. return S + Addend;
  213. default:
  214. llvm_unreachable("Invalid relocation type");
  215. }
  216. }
  217. static bool supportsAmdgpu(uint64_t Type) {
  218. switch (Type) {
  219. case ELF::R_AMDGPU_ABS32:
  220. case ELF::R_AMDGPU_ABS64:
  221. return true;
  222. default:
  223. return false;
  224. }
  225. }
  226. static uint64_t resolveAmdgpu(uint64_t Type, uint64_t Offset, uint64_t S,
  227. uint64_t /*LocData*/, int64_t Addend) {
  228. switch (Type) {
  229. case ELF::R_AMDGPU_ABS32:
  230. case ELF::R_AMDGPU_ABS64:
  231. return S + Addend;
  232. default:
  233. llvm_unreachable("Invalid relocation type");
  234. }
  235. }
  236. static bool supportsX86(uint64_t Type) {
  237. switch (Type) {
  238. case ELF::R_386_NONE:
  239. case ELF::R_386_32:
  240. case ELF::R_386_PC32:
  241. return true;
  242. default:
  243. return false;
  244. }
  245. }
  246. static uint64_t resolveX86(uint64_t Type, uint64_t Offset, uint64_t S,
  247. uint64_t LocData, int64_t /*Addend*/) {
  248. switch (Type) {
  249. case ELF::R_386_NONE:
  250. return LocData;
  251. case ELF::R_386_32:
  252. return S + LocData;
  253. case ELF::R_386_PC32:
  254. return S - Offset + LocData;
  255. default:
  256. llvm_unreachable("Invalid relocation type");
  257. }
  258. }
  259. static bool supportsPPC32(uint64_t Type) {
  260. switch (Type) {
  261. case ELF::R_PPC_ADDR32:
  262. case ELF::R_PPC_REL32:
  263. return true;
  264. default:
  265. return false;
  266. }
  267. }
  268. static uint64_t resolvePPC32(uint64_t Type, uint64_t Offset, uint64_t S,
  269. uint64_t /*LocData*/, int64_t Addend) {
  270. switch (Type) {
  271. case ELF::R_PPC_ADDR32:
  272. return (S + Addend) & 0xFFFFFFFF;
  273. case ELF::R_PPC_REL32:
  274. return (S + Addend - Offset) & 0xFFFFFFFF;
  275. }
  276. llvm_unreachable("Invalid relocation type");
  277. }
  278. static bool supportsARM(uint64_t Type) {
  279. switch (Type) {
  280. case ELF::R_ARM_ABS32:
  281. case ELF::R_ARM_REL32:
  282. return true;
  283. default:
  284. return false;
  285. }
  286. }
  287. static uint64_t resolveARM(uint64_t Type, uint64_t Offset, uint64_t S,
  288. uint64_t LocData, int64_t /*Addend*/) {
  289. switch (Type) {
  290. case ELF::R_ARM_ABS32:
  291. return (S + LocData) & 0xFFFFFFFF;
  292. case ELF::R_ARM_REL32:
  293. return (S + LocData - Offset) & 0xFFFFFFFF;
  294. }
  295. llvm_unreachable("Invalid relocation type");
  296. }
  297. static bool supportsAVR(uint64_t Type) {
  298. switch (Type) {
  299. case ELF::R_AVR_16:
  300. case ELF::R_AVR_32:
  301. return true;
  302. default:
  303. return false;
  304. }
  305. }
  306. static uint64_t resolveAVR(uint64_t Type, uint64_t Offset, uint64_t S,
  307. uint64_t /*LocData*/, int64_t Addend) {
  308. switch (Type) {
  309. case ELF::R_AVR_16:
  310. return (S + Addend) & 0xFFFF;
  311. case ELF::R_AVR_32:
  312. return (S + Addend) & 0xFFFFFFFF;
  313. default:
  314. llvm_unreachable("Invalid relocation type");
  315. }
  316. }
  317. static bool supportsLanai(uint64_t Type) {
  318. return Type == ELF::R_LANAI_32;
  319. }
  320. static uint64_t resolveLanai(uint64_t Type, uint64_t Offset, uint64_t S,
  321. uint64_t /*LocData*/, int64_t Addend) {
  322. if (Type == ELF::R_LANAI_32)
  323. return (S + Addend) & 0xFFFFFFFF;
  324. llvm_unreachable("Invalid relocation type");
  325. }
  326. static bool supportsMips32(uint64_t Type) {
  327. switch (Type) {
  328. case ELF::R_MIPS_32:
  329. case ELF::R_MIPS_TLS_DTPREL32:
  330. return true;
  331. default:
  332. return false;
  333. }
  334. }
  335. static uint64_t resolveMips32(uint64_t Type, uint64_t Offset, uint64_t S,
  336. uint64_t LocData, int64_t /*Addend*/) {
  337. // FIXME: Take in account implicit addends to get correct results.
  338. if (Type == ELF::R_MIPS_32)
  339. return (S + LocData) & 0xFFFFFFFF;
  340. if (Type == ELF::R_MIPS_TLS_DTPREL32)
  341. return (S + LocData) & 0xFFFFFFFF;
  342. llvm_unreachable("Invalid relocation type");
  343. }
  344. static bool supportsSparc32(uint64_t Type) {
  345. switch (Type) {
  346. case ELF::R_SPARC_32:
  347. case ELF::R_SPARC_UA32:
  348. return true;
  349. default:
  350. return false;
  351. }
  352. }
  353. static uint64_t resolveSparc32(uint64_t Type, uint64_t Offset, uint64_t S,
  354. uint64_t LocData, int64_t Addend) {
  355. if (Type == ELF::R_SPARC_32 || Type == ELF::R_SPARC_UA32)
  356. return S + Addend;
  357. return LocData;
  358. }
  359. static bool supportsHexagon(uint64_t Type) {
  360. return Type == ELF::R_HEX_32;
  361. }
  362. static uint64_t resolveHexagon(uint64_t Type, uint64_t Offset, uint64_t S,
  363. uint64_t /*LocData*/, int64_t Addend) {
  364. if (Type == ELF::R_HEX_32)
  365. return S + Addend;
  366. llvm_unreachable("Invalid relocation type");
  367. }
  368. static bool supportsRISCV(uint64_t Type) {
  369. switch (Type) {
  370. case ELF::R_RISCV_NONE:
  371. case ELF::R_RISCV_32:
  372. case ELF::R_RISCV_32_PCREL:
  373. case ELF::R_RISCV_64:
  374. case ELF::R_RISCV_SET6:
  375. case ELF::R_RISCV_SUB6:
  376. case ELF::R_RISCV_ADD8:
  377. case ELF::R_RISCV_SUB8:
  378. case ELF::R_RISCV_ADD16:
  379. case ELF::R_RISCV_SUB16:
  380. case ELF::R_RISCV_ADD32:
  381. case ELF::R_RISCV_SUB32:
  382. case ELF::R_RISCV_ADD64:
  383. case ELF::R_RISCV_SUB64:
  384. return true;
  385. default:
  386. return false;
  387. }
  388. }
  389. static uint64_t resolveRISCV(uint64_t Type, uint64_t Offset, uint64_t S,
  390. uint64_t LocData, int64_t Addend) {
  391. int64_t RA = Addend;
  392. uint64_t A = LocData;
  393. switch (Type) {
  394. case ELF::R_RISCV_NONE:
  395. return LocData;
  396. case ELF::R_RISCV_32:
  397. return (S + RA) & 0xFFFFFFFF;
  398. case ELF::R_RISCV_32_PCREL:
  399. return (S + RA - Offset) & 0xFFFFFFFF;
  400. case ELF::R_RISCV_64:
  401. return S + RA;
  402. case ELF::R_RISCV_SET6:
  403. return (A & 0xC0) | ((S + RA) & 0x3F);
  404. case ELF::R_RISCV_SUB6:
  405. return (A & 0xC0) | (((A & 0x3F) - (S + RA)) & 0x3F);
  406. case ELF::R_RISCV_ADD8:
  407. return (A + (S + RA)) & 0xFF;
  408. case ELF::R_RISCV_SUB8:
  409. return (A - (S + RA)) & 0xFF;
  410. case ELF::R_RISCV_ADD16:
  411. return (A + (S + RA)) & 0xFFFF;
  412. case ELF::R_RISCV_SUB16:
  413. return (A - (S + RA)) & 0xFFFF;
  414. case ELF::R_RISCV_ADD32:
  415. return (A + (S + RA)) & 0xFFFFFFFF;
  416. case ELF::R_RISCV_SUB32:
  417. return (A - (S + RA)) & 0xFFFFFFFF;
  418. case ELF::R_RISCV_ADD64:
  419. return (A + (S + RA));
  420. case ELF::R_RISCV_SUB64:
  421. return (A - (S + RA));
  422. default:
  423. llvm_unreachable("Invalid relocation type");
  424. }
  425. }
  426. static bool supportsCOFFX86(uint64_t Type) {
  427. switch (Type) {
  428. case COFF::IMAGE_REL_I386_SECREL:
  429. case COFF::IMAGE_REL_I386_DIR32:
  430. return true;
  431. default:
  432. return false;
  433. }
  434. }
  435. static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S,
  436. uint64_t LocData, int64_t /*Addend*/) {
  437. switch (Type) {
  438. case COFF::IMAGE_REL_I386_SECREL:
  439. case COFF::IMAGE_REL_I386_DIR32:
  440. return (S + LocData) & 0xFFFFFFFF;
  441. default:
  442. llvm_unreachable("Invalid relocation type");
  443. }
  444. }
  445. static bool supportsCOFFX86_64(uint64_t Type) {
  446. switch (Type) {
  447. case COFF::IMAGE_REL_AMD64_SECREL:
  448. case COFF::IMAGE_REL_AMD64_ADDR64:
  449. return true;
  450. default:
  451. return false;
  452. }
  453. }
  454. static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
  455. uint64_t LocData, int64_t /*Addend*/) {
  456. switch (Type) {
  457. case COFF::IMAGE_REL_AMD64_SECREL:
  458. return (S + LocData) & 0xFFFFFFFF;
  459. case COFF::IMAGE_REL_AMD64_ADDR64:
  460. return S + LocData;
  461. default:
  462. llvm_unreachable("Invalid relocation type");
  463. }
  464. }
  465. static bool supportsCOFFARM(uint64_t Type) {
  466. switch (Type) {
  467. case COFF::IMAGE_REL_ARM_SECREL:
  468. case COFF::IMAGE_REL_ARM_ADDR32:
  469. return true;
  470. default:
  471. return false;
  472. }
  473. }
  474. static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S,
  475. uint64_t LocData, int64_t /*Addend*/) {
  476. switch (Type) {
  477. case COFF::IMAGE_REL_ARM_SECREL:
  478. case COFF::IMAGE_REL_ARM_ADDR32:
  479. return (S + LocData) & 0xFFFFFFFF;
  480. default:
  481. llvm_unreachable("Invalid relocation type");
  482. }
  483. }
  484. static bool supportsCOFFARM64(uint64_t Type) {
  485. switch (Type) {
  486. case COFF::IMAGE_REL_ARM64_SECREL:
  487. case COFF::IMAGE_REL_ARM64_ADDR64:
  488. return true;
  489. default:
  490. return false;
  491. }
  492. }
  493. static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S,
  494. uint64_t LocData, int64_t /*Addend*/) {
  495. switch (Type) {
  496. case COFF::IMAGE_REL_ARM64_SECREL:
  497. return (S + LocData) & 0xFFFFFFFF;
  498. case COFF::IMAGE_REL_ARM64_ADDR64:
  499. return S + LocData;
  500. default:
  501. llvm_unreachable("Invalid relocation type");
  502. }
  503. }
  504. static bool supportsMachOX86_64(uint64_t Type) {
  505. return Type == MachO::X86_64_RELOC_UNSIGNED;
  506. }
  507. static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
  508. uint64_t LocData, int64_t /*Addend*/) {
  509. if (Type == MachO::X86_64_RELOC_UNSIGNED)
  510. return S;
  511. llvm_unreachable("Invalid relocation type");
  512. }
  513. static bool supportsWasm32(uint64_t Type) {
  514. switch (Type) {
  515. case wasm::R_WASM_FUNCTION_INDEX_LEB:
  516. case wasm::R_WASM_TABLE_INDEX_SLEB:
  517. case wasm::R_WASM_TABLE_INDEX_I32:
  518. case wasm::R_WASM_MEMORY_ADDR_LEB:
  519. case wasm::R_WASM_MEMORY_ADDR_SLEB:
  520. case wasm::R_WASM_MEMORY_ADDR_I32:
  521. case wasm::R_WASM_TYPE_INDEX_LEB:
  522. case wasm::R_WASM_GLOBAL_INDEX_LEB:
  523. case wasm::R_WASM_FUNCTION_OFFSET_I32:
  524. case wasm::R_WASM_SECTION_OFFSET_I32:
  525. case wasm::R_WASM_EVENT_INDEX_LEB:
  526. case wasm::R_WASM_GLOBAL_INDEX_I32:
  527. case wasm::R_WASM_TABLE_NUMBER_LEB:
  528. return true;
  529. default:
  530. return false;
  531. }
  532. }
  533. static bool supportsWasm64(uint64_t Type) {
  534. switch (Type) {
  535. case wasm::R_WASM_MEMORY_ADDR_LEB64:
  536. case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  537. case wasm::R_WASM_MEMORY_ADDR_I64:
  538. case wasm::R_WASM_TABLE_INDEX_SLEB64:
  539. case wasm::R_WASM_TABLE_INDEX_I64:
  540. case wasm::R_WASM_FUNCTION_OFFSET_I64:
  541. return true;
  542. default:
  543. return supportsWasm32(Type);
  544. }
  545. }
  546. static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S,
  547. uint64_t LocData, int64_t /*Addend*/) {
  548. switch (Type) {
  549. case wasm::R_WASM_FUNCTION_INDEX_LEB:
  550. case wasm::R_WASM_TABLE_INDEX_SLEB:
  551. case wasm::R_WASM_TABLE_INDEX_I32:
  552. case wasm::R_WASM_MEMORY_ADDR_LEB:
  553. case wasm::R_WASM_MEMORY_ADDR_SLEB:
  554. case wasm::R_WASM_MEMORY_ADDR_I32:
  555. case wasm::R_WASM_TYPE_INDEX_LEB:
  556. case wasm::R_WASM_GLOBAL_INDEX_LEB:
  557. case wasm::R_WASM_FUNCTION_OFFSET_I32:
  558. case wasm::R_WASM_SECTION_OFFSET_I32:
  559. case wasm::R_WASM_EVENT_INDEX_LEB:
  560. case wasm::R_WASM_GLOBAL_INDEX_I32:
  561. case wasm::R_WASM_TABLE_NUMBER_LEB:
  562. // For wasm section, its offset at 0 -- ignoring Value
  563. return LocData;
  564. default:
  565. llvm_unreachable("Invalid relocation type");
  566. }
  567. }
  568. static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S,
  569. uint64_t LocData, int64_t Addend) {
  570. switch (Type) {
  571. case wasm::R_WASM_MEMORY_ADDR_LEB64:
  572. case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  573. case wasm::R_WASM_MEMORY_ADDR_I64:
  574. case wasm::R_WASM_TABLE_INDEX_SLEB64:
  575. case wasm::R_WASM_TABLE_INDEX_I64:
  576. case wasm::R_WASM_FUNCTION_OFFSET_I64:
  577. // For wasm section, its offset at 0 -- ignoring Value
  578. return LocData;
  579. default:
  580. return resolveWasm32(Type, Offset, S, LocData, Addend);
  581. }
  582. }
  583. std::pair<SupportsRelocation, RelocationResolver>
  584. getRelocationResolver(const ObjectFile &Obj) {
  585. if (Obj.isCOFF()) {
  586. switch (Obj.getArch()) {
  587. case Triple::x86_64:
  588. return {supportsCOFFX86_64, resolveCOFFX86_64};
  589. case Triple::x86:
  590. return {supportsCOFFX86, resolveCOFFX86};
  591. case Triple::arm:
  592. case Triple::thumb:
  593. return {supportsCOFFARM, resolveCOFFARM};
  594. case Triple::aarch64:
  595. return {supportsCOFFARM64, resolveCOFFARM64};
  596. default:
  597. return {nullptr, nullptr};
  598. }
  599. } else if (Obj.isELF()) {
  600. if (Obj.getBytesInAddress() == 8) {
  601. switch (Obj.getArch()) {
  602. case Triple::x86_64:
  603. return {supportsX86_64, resolveX86_64};
  604. case Triple::aarch64:
  605. case Triple::aarch64_be:
  606. return {supportsAArch64, resolveAArch64};
  607. case Triple::bpfel:
  608. case Triple::bpfeb:
  609. return {supportsBPF, resolveBPF};
  610. case Triple::mips64el:
  611. case Triple::mips64:
  612. return {supportsMips64, resolveMips64};
  613. case Triple::ppc64le:
  614. case Triple::ppc64:
  615. return {supportsPPC64, resolvePPC64};
  616. case Triple::systemz:
  617. return {supportsSystemZ, resolveSystemZ};
  618. case Triple::sparcv9:
  619. return {supportsSparc64, resolveSparc64};
  620. case Triple::amdgcn:
  621. return {supportsAmdgpu, resolveAmdgpu};
  622. case Triple::riscv64:
  623. return {supportsRISCV, resolveRISCV};
  624. default:
  625. return {nullptr, nullptr};
  626. }
  627. }
  628. // 32-bit object file
  629. assert(Obj.getBytesInAddress() == 4 &&
  630. "Invalid word size in object file");
  631. switch (Obj.getArch()) {
  632. case Triple::x86:
  633. return {supportsX86, resolveX86};
  634. case Triple::ppcle:
  635. case Triple::ppc:
  636. return {supportsPPC32, resolvePPC32};
  637. case Triple::arm:
  638. case Triple::armeb:
  639. return {supportsARM, resolveARM};
  640. case Triple::avr:
  641. return {supportsAVR, resolveAVR};
  642. case Triple::lanai:
  643. return {supportsLanai, resolveLanai};
  644. case Triple::mipsel:
  645. case Triple::mips:
  646. return {supportsMips32, resolveMips32};
  647. case Triple::msp430:
  648. return {supportsMSP430, resolveMSP430};
  649. case Triple::sparc:
  650. return {supportsSparc32, resolveSparc32};
  651. case Triple::hexagon:
  652. return {supportsHexagon, resolveHexagon};
  653. case Triple::riscv32:
  654. return {supportsRISCV, resolveRISCV};
  655. default:
  656. return {nullptr, nullptr};
  657. }
  658. } else if (Obj.isMachO()) {
  659. if (Obj.getArch() == Triple::x86_64)
  660. return {supportsMachOX86_64, resolveMachOX86_64};
  661. return {nullptr, nullptr};
  662. } else if (Obj.isWasm()) {
  663. if (Obj.getArch() == Triple::wasm32)
  664. return {supportsWasm32, resolveWasm32};
  665. if (Obj.getArch() == Triple::wasm64)
  666. return {supportsWasm64, resolveWasm64};
  667. return {nullptr, nullptr};
  668. }
  669. llvm_unreachable("Invalid object file");
  670. }
  671. uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R,
  672. uint64_t S, uint64_t LocData) {
  673. if (const ObjectFile *Obj = R.getObject()) {
  674. int64_t Addend = 0;
  675. if (Obj->isELF()) {
  676. auto GetRelSectionType = [&]() -> unsigned {
  677. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
  678. return Elf32LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  679. if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
  680. return Elf64LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  681. if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
  682. return Elf32BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  683. auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj);
  684. return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  685. };
  686. if (GetRelSectionType() == ELF::SHT_RELA)
  687. Addend = getELFAddend(R);
  688. }
  689. return Resolver(R.getType(), R.getOffset(), S, LocData, Addend);
  690. }
  691. // Sometimes the caller might want to use its own specific implementation of
  692. // the resolver function. E.g. this is used by LLD when it resolves debug
  693. // relocations and assumes that all of them have the same computation (S + A).
  694. // The relocation R has no owner object in this case and we don't need to
  695. // provide Type and Offset fields. It is also assumed the DataRefImpl.p
  696. // contains the addend, provided by the caller.
  697. return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData,
  698. R.getRawDataRefImpl().p);
  699. }
  700. } // namespace object
  701. } // namespace llvm