RelocationResolver.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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(Twine(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_ABS32:
  85. case ELF::R_BPF_64_ABS64:
  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_ABS32:
  95. return (S + LocData) & 0xFFFFFFFF;
  96. case ELF::R_BPF_64_ABS64:
  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. // Support both RELA and REL relocations. The caller is responsible
  290. // for supplying the correct values for LocData and Addend, i.e.
  291. // Addend == 0 for REL and LocData == 0 for RELA.
  292. assert((LocData == 0 || Addend == 0) &&
  293. "one of LocData and Addend must be 0");
  294. switch (Type) {
  295. case ELF::R_ARM_ABS32:
  296. return (S + LocData + Addend) & 0xFFFFFFFF;
  297. case ELF::R_ARM_REL32:
  298. return (S + LocData + Addend - Offset) & 0xFFFFFFFF;
  299. }
  300. llvm_unreachable("Invalid relocation type");
  301. }
  302. static bool supportsAVR(uint64_t Type) {
  303. switch (Type) {
  304. case ELF::R_AVR_16:
  305. case ELF::R_AVR_32:
  306. return true;
  307. default:
  308. return false;
  309. }
  310. }
  311. static uint64_t resolveAVR(uint64_t Type, uint64_t Offset, uint64_t S,
  312. uint64_t /*LocData*/, int64_t Addend) {
  313. switch (Type) {
  314. case ELF::R_AVR_16:
  315. return (S + Addend) & 0xFFFF;
  316. case ELF::R_AVR_32:
  317. return (S + Addend) & 0xFFFFFFFF;
  318. default:
  319. llvm_unreachable("Invalid relocation type");
  320. }
  321. }
  322. static bool supportsLanai(uint64_t Type) {
  323. return Type == ELF::R_LANAI_32;
  324. }
  325. static uint64_t resolveLanai(uint64_t Type, uint64_t Offset, uint64_t S,
  326. uint64_t /*LocData*/, int64_t Addend) {
  327. if (Type == ELF::R_LANAI_32)
  328. return (S + Addend) & 0xFFFFFFFF;
  329. llvm_unreachable("Invalid relocation type");
  330. }
  331. static bool supportsMips32(uint64_t Type) {
  332. switch (Type) {
  333. case ELF::R_MIPS_32:
  334. case ELF::R_MIPS_TLS_DTPREL32:
  335. return true;
  336. default:
  337. return false;
  338. }
  339. }
  340. static uint64_t resolveMips32(uint64_t Type, uint64_t Offset, uint64_t S,
  341. uint64_t LocData, int64_t /*Addend*/) {
  342. // FIXME: Take in account implicit addends to get correct results.
  343. if (Type == ELF::R_MIPS_32)
  344. return (S + LocData) & 0xFFFFFFFF;
  345. if (Type == ELF::R_MIPS_TLS_DTPREL32)
  346. return (S + LocData) & 0xFFFFFFFF;
  347. llvm_unreachable("Invalid relocation type");
  348. }
  349. static bool supportsSparc32(uint64_t Type) {
  350. switch (Type) {
  351. case ELF::R_SPARC_32:
  352. case ELF::R_SPARC_UA32:
  353. return true;
  354. default:
  355. return false;
  356. }
  357. }
  358. static uint64_t resolveSparc32(uint64_t Type, uint64_t Offset, uint64_t S,
  359. uint64_t LocData, int64_t Addend) {
  360. if (Type == ELF::R_SPARC_32 || Type == ELF::R_SPARC_UA32)
  361. return S + Addend;
  362. return LocData;
  363. }
  364. static bool supportsHexagon(uint64_t Type) {
  365. return Type == ELF::R_HEX_32;
  366. }
  367. static uint64_t resolveHexagon(uint64_t Type, uint64_t Offset, uint64_t S,
  368. uint64_t /*LocData*/, int64_t Addend) {
  369. if (Type == ELF::R_HEX_32)
  370. return S + Addend;
  371. llvm_unreachable("Invalid relocation type");
  372. }
  373. static bool supportsRISCV(uint64_t Type) {
  374. switch (Type) {
  375. case ELF::R_RISCV_NONE:
  376. case ELF::R_RISCV_32:
  377. case ELF::R_RISCV_32_PCREL:
  378. case ELF::R_RISCV_64:
  379. case ELF::R_RISCV_SET6:
  380. case ELF::R_RISCV_SUB6:
  381. case ELF::R_RISCV_ADD8:
  382. case ELF::R_RISCV_SUB8:
  383. case ELF::R_RISCV_ADD16:
  384. case ELF::R_RISCV_SUB16:
  385. case ELF::R_RISCV_ADD32:
  386. case ELF::R_RISCV_SUB32:
  387. case ELF::R_RISCV_ADD64:
  388. case ELF::R_RISCV_SUB64:
  389. return true;
  390. default:
  391. return false;
  392. }
  393. }
  394. static uint64_t resolveRISCV(uint64_t Type, uint64_t Offset, uint64_t S,
  395. uint64_t LocData, int64_t Addend) {
  396. int64_t RA = Addend;
  397. uint64_t A = LocData;
  398. switch (Type) {
  399. case ELF::R_RISCV_NONE:
  400. return LocData;
  401. case ELF::R_RISCV_32:
  402. return (S + RA) & 0xFFFFFFFF;
  403. case ELF::R_RISCV_32_PCREL:
  404. return (S + RA - Offset) & 0xFFFFFFFF;
  405. case ELF::R_RISCV_64:
  406. return S + RA;
  407. case ELF::R_RISCV_SET6:
  408. return (A & 0xC0) | ((S + RA) & 0x3F);
  409. case ELF::R_RISCV_SUB6:
  410. return (A & 0xC0) | (((A & 0x3F) - (S + RA)) & 0x3F);
  411. case ELF::R_RISCV_ADD8:
  412. return (A + (S + RA)) & 0xFF;
  413. case ELF::R_RISCV_SUB8:
  414. return (A - (S + RA)) & 0xFF;
  415. case ELF::R_RISCV_ADD16:
  416. return (A + (S + RA)) & 0xFFFF;
  417. case ELF::R_RISCV_SUB16:
  418. return (A - (S + RA)) & 0xFFFF;
  419. case ELF::R_RISCV_ADD32:
  420. return (A + (S + RA)) & 0xFFFFFFFF;
  421. case ELF::R_RISCV_SUB32:
  422. return (A - (S + RA)) & 0xFFFFFFFF;
  423. case ELF::R_RISCV_ADD64:
  424. return (A + (S + RA));
  425. case ELF::R_RISCV_SUB64:
  426. return (A - (S + RA));
  427. default:
  428. llvm_unreachable("Invalid relocation type");
  429. }
  430. }
  431. static bool supportsCOFFX86(uint64_t Type) {
  432. switch (Type) {
  433. case COFF::IMAGE_REL_I386_SECREL:
  434. case COFF::IMAGE_REL_I386_DIR32:
  435. return true;
  436. default:
  437. return false;
  438. }
  439. }
  440. static uint64_t resolveCOFFX86(uint64_t Type, uint64_t Offset, uint64_t S,
  441. uint64_t LocData, int64_t /*Addend*/) {
  442. switch (Type) {
  443. case COFF::IMAGE_REL_I386_SECREL:
  444. case COFF::IMAGE_REL_I386_DIR32:
  445. return (S + LocData) & 0xFFFFFFFF;
  446. default:
  447. llvm_unreachable("Invalid relocation type");
  448. }
  449. }
  450. static bool supportsCOFFX86_64(uint64_t Type) {
  451. switch (Type) {
  452. case COFF::IMAGE_REL_AMD64_SECREL:
  453. case COFF::IMAGE_REL_AMD64_ADDR64:
  454. return true;
  455. default:
  456. return false;
  457. }
  458. }
  459. static uint64_t resolveCOFFX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
  460. uint64_t LocData, int64_t /*Addend*/) {
  461. switch (Type) {
  462. case COFF::IMAGE_REL_AMD64_SECREL:
  463. return (S + LocData) & 0xFFFFFFFF;
  464. case COFF::IMAGE_REL_AMD64_ADDR64:
  465. return S + LocData;
  466. default:
  467. llvm_unreachable("Invalid relocation type");
  468. }
  469. }
  470. static bool supportsCOFFARM(uint64_t Type) {
  471. switch (Type) {
  472. case COFF::IMAGE_REL_ARM_SECREL:
  473. case COFF::IMAGE_REL_ARM_ADDR32:
  474. return true;
  475. default:
  476. return false;
  477. }
  478. }
  479. static uint64_t resolveCOFFARM(uint64_t Type, uint64_t Offset, uint64_t S,
  480. uint64_t LocData, int64_t /*Addend*/) {
  481. switch (Type) {
  482. case COFF::IMAGE_REL_ARM_SECREL:
  483. case COFF::IMAGE_REL_ARM_ADDR32:
  484. return (S + LocData) & 0xFFFFFFFF;
  485. default:
  486. llvm_unreachable("Invalid relocation type");
  487. }
  488. }
  489. static bool supportsCOFFARM64(uint64_t Type) {
  490. switch (Type) {
  491. case COFF::IMAGE_REL_ARM64_SECREL:
  492. case COFF::IMAGE_REL_ARM64_ADDR64:
  493. return true;
  494. default:
  495. return false;
  496. }
  497. }
  498. static uint64_t resolveCOFFARM64(uint64_t Type, uint64_t Offset, uint64_t S,
  499. uint64_t LocData, int64_t /*Addend*/) {
  500. switch (Type) {
  501. case COFF::IMAGE_REL_ARM64_SECREL:
  502. return (S + LocData) & 0xFFFFFFFF;
  503. case COFF::IMAGE_REL_ARM64_ADDR64:
  504. return S + LocData;
  505. default:
  506. llvm_unreachable("Invalid relocation type");
  507. }
  508. }
  509. static bool supportsMachOX86_64(uint64_t Type) {
  510. return Type == MachO::X86_64_RELOC_UNSIGNED;
  511. }
  512. static uint64_t resolveMachOX86_64(uint64_t Type, uint64_t Offset, uint64_t S,
  513. uint64_t LocData, int64_t /*Addend*/) {
  514. if (Type == MachO::X86_64_RELOC_UNSIGNED)
  515. return S;
  516. llvm_unreachable("Invalid relocation type");
  517. }
  518. static bool supportsWasm32(uint64_t Type) {
  519. switch (Type) {
  520. case wasm::R_WASM_FUNCTION_INDEX_LEB:
  521. case wasm::R_WASM_TABLE_INDEX_SLEB:
  522. case wasm::R_WASM_TABLE_INDEX_I32:
  523. case wasm::R_WASM_MEMORY_ADDR_LEB:
  524. case wasm::R_WASM_MEMORY_ADDR_SLEB:
  525. case wasm::R_WASM_MEMORY_ADDR_I32:
  526. case wasm::R_WASM_TYPE_INDEX_LEB:
  527. case wasm::R_WASM_GLOBAL_INDEX_LEB:
  528. case wasm::R_WASM_FUNCTION_OFFSET_I32:
  529. case wasm::R_WASM_SECTION_OFFSET_I32:
  530. case wasm::R_WASM_TAG_INDEX_LEB:
  531. case wasm::R_WASM_GLOBAL_INDEX_I32:
  532. case wasm::R_WASM_TABLE_NUMBER_LEB:
  533. case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
  534. return true;
  535. default:
  536. return false;
  537. }
  538. }
  539. static bool supportsWasm64(uint64_t Type) {
  540. switch (Type) {
  541. case wasm::R_WASM_MEMORY_ADDR_LEB64:
  542. case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  543. case wasm::R_WASM_MEMORY_ADDR_I64:
  544. case wasm::R_WASM_TABLE_INDEX_SLEB64:
  545. case wasm::R_WASM_TABLE_INDEX_I64:
  546. case wasm::R_WASM_FUNCTION_OFFSET_I64:
  547. return true;
  548. default:
  549. return supportsWasm32(Type);
  550. }
  551. }
  552. static uint64_t resolveWasm32(uint64_t Type, uint64_t Offset, uint64_t S,
  553. uint64_t LocData, int64_t /*Addend*/) {
  554. switch (Type) {
  555. case wasm::R_WASM_FUNCTION_INDEX_LEB:
  556. case wasm::R_WASM_TABLE_INDEX_SLEB:
  557. case wasm::R_WASM_TABLE_INDEX_I32:
  558. case wasm::R_WASM_MEMORY_ADDR_LEB:
  559. case wasm::R_WASM_MEMORY_ADDR_SLEB:
  560. case wasm::R_WASM_MEMORY_ADDR_I32:
  561. case wasm::R_WASM_TYPE_INDEX_LEB:
  562. case wasm::R_WASM_GLOBAL_INDEX_LEB:
  563. case wasm::R_WASM_FUNCTION_OFFSET_I32:
  564. case wasm::R_WASM_SECTION_OFFSET_I32:
  565. case wasm::R_WASM_TAG_INDEX_LEB:
  566. case wasm::R_WASM_GLOBAL_INDEX_I32:
  567. case wasm::R_WASM_TABLE_NUMBER_LEB:
  568. case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
  569. // For wasm section, its offset at 0 -- ignoring Value
  570. return LocData;
  571. default:
  572. llvm_unreachable("Invalid relocation type");
  573. }
  574. }
  575. static uint64_t resolveWasm64(uint64_t Type, uint64_t Offset, uint64_t S,
  576. uint64_t LocData, int64_t Addend) {
  577. switch (Type) {
  578. case wasm::R_WASM_MEMORY_ADDR_LEB64:
  579. case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  580. case wasm::R_WASM_MEMORY_ADDR_I64:
  581. case wasm::R_WASM_TABLE_INDEX_SLEB64:
  582. case wasm::R_WASM_TABLE_INDEX_I64:
  583. case wasm::R_WASM_FUNCTION_OFFSET_I64:
  584. // For wasm section, its offset at 0 -- ignoring Value
  585. return LocData;
  586. default:
  587. return resolveWasm32(Type, Offset, S, LocData, Addend);
  588. }
  589. }
  590. std::pair<SupportsRelocation, RelocationResolver>
  591. getRelocationResolver(const ObjectFile &Obj) {
  592. if (Obj.isCOFF()) {
  593. switch (Obj.getArch()) {
  594. case Triple::x86_64:
  595. return {supportsCOFFX86_64, resolveCOFFX86_64};
  596. case Triple::x86:
  597. return {supportsCOFFX86, resolveCOFFX86};
  598. case Triple::arm:
  599. case Triple::thumb:
  600. return {supportsCOFFARM, resolveCOFFARM};
  601. case Triple::aarch64:
  602. return {supportsCOFFARM64, resolveCOFFARM64};
  603. default:
  604. return {nullptr, nullptr};
  605. }
  606. } else if (Obj.isELF()) {
  607. if (Obj.getBytesInAddress() == 8) {
  608. switch (Obj.getArch()) {
  609. case Triple::x86_64:
  610. return {supportsX86_64, resolveX86_64};
  611. case Triple::aarch64:
  612. case Triple::aarch64_be:
  613. return {supportsAArch64, resolveAArch64};
  614. case Triple::bpfel:
  615. case Triple::bpfeb:
  616. return {supportsBPF, resolveBPF};
  617. case Triple::mips64el:
  618. case Triple::mips64:
  619. return {supportsMips64, resolveMips64};
  620. case Triple::ppc64le:
  621. case Triple::ppc64:
  622. return {supportsPPC64, resolvePPC64};
  623. case Triple::systemz:
  624. return {supportsSystemZ, resolveSystemZ};
  625. case Triple::sparcv9:
  626. return {supportsSparc64, resolveSparc64};
  627. case Triple::amdgcn:
  628. return {supportsAmdgpu, resolveAmdgpu};
  629. case Triple::riscv64:
  630. return {supportsRISCV, resolveRISCV};
  631. default:
  632. return {nullptr, nullptr};
  633. }
  634. }
  635. // 32-bit object file
  636. assert(Obj.getBytesInAddress() == 4 &&
  637. "Invalid word size in object file");
  638. switch (Obj.getArch()) {
  639. case Triple::x86:
  640. return {supportsX86, resolveX86};
  641. case Triple::ppcle:
  642. case Triple::ppc:
  643. return {supportsPPC32, resolvePPC32};
  644. case Triple::arm:
  645. case Triple::armeb:
  646. return {supportsARM, resolveARM};
  647. case Triple::avr:
  648. return {supportsAVR, resolveAVR};
  649. case Triple::lanai:
  650. return {supportsLanai, resolveLanai};
  651. case Triple::mipsel:
  652. case Triple::mips:
  653. return {supportsMips32, resolveMips32};
  654. case Triple::msp430:
  655. return {supportsMSP430, resolveMSP430};
  656. case Triple::sparc:
  657. return {supportsSparc32, resolveSparc32};
  658. case Triple::hexagon:
  659. return {supportsHexagon, resolveHexagon};
  660. case Triple::riscv32:
  661. return {supportsRISCV, resolveRISCV};
  662. default:
  663. return {nullptr, nullptr};
  664. }
  665. } else if (Obj.isMachO()) {
  666. if (Obj.getArch() == Triple::x86_64)
  667. return {supportsMachOX86_64, resolveMachOX86_64};
  668. return {nullptr, nullptr};
  669. } else if (Obj.isWasm()) {
  670. if (Obj.getArch() == Triple::wasm32)
  671. return {supportsWasm32, resolveWasm32};
  672. if (Obj.getArch() == Triple::wasm64)
  673. return {supportsWasm64, resolveWasm64};
  674. return {nullptr, nullptr};
  675. }
  676. llvm_unreachable("Invalid object file");
  677. }
  678. uint64_t resolveRelocation(RelocationResolver Resolver, const RelocationRef &R,
  679. uint64_t S, uint64_t LocData) {
  680. if (const ObjectFile *Obj = R.getObject()) {
  681. int64_t Addend = 0;
  682. if (Obj->isELF()) {
  683. auto GetRelSectionType = [&]() -> unsigned {
  684. if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
  685. return Elf32LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  686. if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
  687. return Elf64LEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  688. if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
  689. return Elf32BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  690. auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj);
  691. return Elf64BEObj->getRelSection(R.getRawDataRefImpl())->sh_type;
  692. };
  693. if (GetRelSectionType() == ELF::SHT_RELA) {
  694. Addend = getELFAddend(R);
  695. // RISCV relocations use both LocData and Addend.
  696. if (Obj->getArch() != Triple::riscv32 &&
  697. Obj->getArch() != Triple::riscv64)
  698. LocData = 0;
  699. }
  700. }
  701. return Resolver(R.getType(), R.getOffset(), S, LocData, Addend);
  702. }
  703. // Sometimes the caller might want to use its own specific implementation of
  704. // the resolver function. E.g. this is used by LLD when it resolves debug
  705. // relocations and assumes that all of them have the same computation (S + A).
  706. // The relocation R has no owner object in this case and we don't need to
  707. // provide Type and Offset fields. It is also assumed the DataRefImpl.p
  708. // contains the addend, provided by the caller.
  709. return Resolver(/*Type=*/0, /*Offset=*/0, S, LocData,
  710. R.getRawDataRefImpl().p);
  711. }
  712. } // namespace object
  713. } // namespace llvm