aot_reloc_arm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "aot_reloc.h"
  6. #define R_ARM_CALL 28 /* PC relative 24 bit (BL, BLX). */
  7. #define R_ARM_JMP24 29 /* PC relative 24 bit (B/BL<cond>). */
  8. #define R_ARM_ABS32 2 /* Direct 32 bit */
  9. #define R_ARM_MOVW_ABS_NC 43
  10. #define R_ARM_MOVT_ABS 44
  11. /* clang-format off */
  12. void __adddf3(void);
  13. void __addsf3(void);
  14. void __aeabi_d2f(void);
  15. void __aeabi_d2iz(void);
  16. void __aeabi_d2lz(void);
  17. void __aeabi_d2uiz(void);
  18. void __aeabi_d2ulz(void);
  19. void __aeabi_dadd(void);
  20. void __aeabi_dcmpeq(void);
  21. void __aeabi_dcmpge(void);
  22. void __aeabi_dcmpgt(void);
  23. void __aeabi_dcmple(void);
  24. void __aeabi_dcmplt(void);
  25. void __aeabi_dcmpun(void);
  26. void __aeabi_ddiv(void);
  27. void __aeabi_dmul(void);
  28. void __aeabi_dsub(void);
  29. void __aeabi_f2d(void);
  30. void __aeabi_f2iz(void);
  31. void __aeabi_f2lz(void);
  32. void __aeabi_f2ulz(void);
  33. void __aeabi_fadd(void);
  34. void __aeabi_fcmpeq(void);
  35. void __aeabi_fcmpge(void);
  36. void __aeabi_fcmpgt(void);
  37. void __aeabi_fcmple(void);
  38. void __aeabi_fcmplt(void);
  39. void __aeabi_fcmpun(void);
  40. void __aeabi_fdiv(void);
  41. void __aeabi_fmul(void);
  42. void __aeabi_fsub(void);
  43. void __aeabi_i2d(void);
  44. void __aeabi_i2f(void);
  45. void __aeabi_idiv(void);
  46. void __aeabi_idivmod(void);
  47. void __aeabi_l2d(void);
  48. void __aeabi_l2f(void);
  49. void __aeabi_ldivmod(void);
  50. void __aeabi_memclr(void);
  51. void __aeabi_memcpy(void);
  52. void __aeabi_memmove(void);
  53. void __aeabi_memset(void);
  54. void __aeabi_ui2d(void);
  55. void __aeabi_ui2f(void);
  56. void __aeabi_uidiv(void);
  57. void __aeabi_uidivmod(void);
  58. void __aeabi_ul2d(void);
  59. void __aeabi_ul2f(void);
  60. void __aeabi_uldivmod(void);
  61. void __clzsi2(void);
  62. void __divdf3(void);
  63. void __divdi3(void);
  64. void __divsf3(void);
  65. void __divsi3(void);
  66. void __eqdf2(void);
  67. void __eqsf2(void);
  68. void __extendsfdf2(void);
  69. void __fixdfdi(void);
  70. void __fixdfsi(void);
  71. void __fixsfdi(void);
  72. void __fixsfsi(void);
  73. void __fixunsdfdi(void);
  74. void __fixunsdfsi(void);
  75. void __fixunssfdi(void);
  76. void __floatdidf(void);
  77. void __floatdisf(void);
  78. void __floatsidf(void);
  79. void __floatsisf(void);
  80. void __floatundidf(void);
  81. void __floatundisf(void);
  82. void __floatunsidf(void);
  83. void __floatunsisf(void);
  84. void __gedf2(void);
  85. void __gesf2(void);
  86. void __gtdf2(void);
  87. void __gtsf2(void);
  88. void __ledf2(void);
  89. void __lesf2(void);
  90. void __ltdf2(void);
  91. void __ltsf2(void);
  92. void __moddi3(void);
  93. void __modsi3(void);
  94. void __muldf3(void);
  95. void __mulsf3(void);
  96. void __nedf2(void);
  97. void __nesf2(void);
  98. void __subdf3(void);
  99. void __subsf3(void);
  100. void __truncdfsf2(void);
  101. void __udivdi3(void);
  102. void __udivmoddi4(void);
  103. void __udivsi3(void);
  104. void __umoddi3(void);
  105. void __umodsi3(void);
  106. void __unorddf2(void);
  107. void __unordsf2(void);
  108. /* clang-format on */
  109. static SymbolMap target_sym_map[] = {
  110. /* clang-format off */
  111. REG_COMMON_SYMBOLS
  112. /* compiler-rt symbols that come from compiler(e.g. gcc) */
  113. REG_SYM(__adddf3),
  114. REG_SYM(__addsf3),
  115. /* clang-format on */
  116. REG_SYM(__aeabi_d2f),
  117. REG_SYM(__aeabi_d2iz),
  118. REG_SYM(__aeabi_d2lz),
  119. REG_SYM(__aeabi_d2uiz),
  120. REG_SYM(__aeabi_d2ulz),
  121. REG_SYM(__aeabi_dadd),
  122. REG_SYM(__aeabi_dcmpeq),
  123. REG_SYM(__aeabi_dcmpge),
  124. REG_SYM(__aeabi_dcmpgt),
  125. REG_SYM(__aeabi_dcmple),
  126. REG_SYM(__aeabi_dcmplt),
  127. REG_SYM(__aeabi_dcmpun),
  128. REG_SYM(__aeabi_ddiv),
  129. REG_SYM(__aeabi_dmul),
  130. REG_SYM(__aeabi_dsub),
  131. REG_SYM(__aeabi_f2d),
  132. REG_SYM(__aeabi_f2iz),
  133. REG_SYM(__aeabi_f2lz),
  134. REG_SYM(__aeabi_f2ulz),
  135. REG_SYM(__aeabi_fadd),
  136. REG_SYM(__aeabi_fcmpeq),
  137. REG_SYM(__aeabi_fcmpge),
  138. REG_SYM(__aeabi_fcmpgt),
  139. REG_SYM(__aeabi_fcmple),
  140. REG_SYM(__aeabi_fcmplt),
  141. REG_SYM(__aeabi_fcmpun),
  142. REG_SYM(__aeabi_fdiv),
  143. REG_SYM(__aeabi_fmul),
  144. REG_SYM(__aeabi_fsub),
  145. REG_SYM(__aeabi_i2d),
  146. REG_SYM(__aeabi_i2f),
  147. REG_SYM(__aeabi_idiv),
  148. REG_SYM(__aeabi_idivmod),
  149. REG_SYM(__aeabi_l2d),
  150. REG_SYM(__aeabi_l2f),
  151. REG_SYM(__aeabi_ldivmod),
  152. REG_SYM(__aeabi_memclr),
  153. REG_SYM(__aeabi_memcpy),
  154. REG_SYM(__aeabi_memmove),
  155. REG_SYM(__aeabi_memset),
  156. REG_SYM(__aeabi_ui2d),
  157. REG_SYM(__aeabi_ui2f),
  158. REG_SYM(__aeabi_uidiv),
  159. REG_SYM(__aeabi_uidivmod),
  160. REG_SYM(__aeabi_ul2d),
  161. REG_SYM(__aeabi_ul2f),
  162. REG_SYM(__aeabi_uldivmod),
  163. REG_SYM(__clzsi2),
  164. REG_SYM(__divdf3),
  165. REG_SYM(__divdi3),
  166. REG_SYM(__divsf3),
  167. REG_SYM(__divsi3),
  168. REG_SYM(__eqdf2),
  169. REG_SYM(__eqsf2),
  170. REG_SYM(__extendsfdf2),
  171. REG_SYM(__fixdfdi),
  172. REG_SYM(__fixdfsi),
  173. REG_SYM(__fixsfdi),
  174. REG_SYM(__fixsfsi),
  175. REG_SYM(__fixunsdfdi),
  176. REG_SYM(__fixunsdfsi),
  177. REG_SYM(__fixunssfdi),
  178. REG_SYM(__floatdidf),
  179. REG_SYM(__floatdisf),
  180. REG_SYM(__floatsidf),
  181. REG_SYM(__floatsisf),
  182. REG_SYM(__floatundidf),
  183. REG_SYM(__floatundisf),
  184. REG_SYM(__floatunsidf),
  185. REG_SYM(__floatunsisf),
  186. REG_SYM(__gedf2),
  187. REG_SYM(__gesf2),
  188. REG_SYM(__gtdf2),
  189. REG_SYM(__gtsf2),
  190. REG_SYM(__ledf2),
  191. REG_SYM(__lesf2),
  192. REG_SYM(__ltdf2),
  193. REG_SYM(__ltsf2),
  194. REG_SYM(__moddi3),
  195. REG_SYM(__modsi3),
  196. REG_SYM(__muldf3),
  197. REG_SYM(__mulsf3),
  198. REG_SYM(__nedf2),
  199. REG_SYM(__nesf2),
  200. REG_SYM(__subdf3),
  201. REG_SYM(__subsf3),
  202. REG_SYM(__truncdfsf2),
  203. REG_SYM(__udivdi3),
  204. REG_SYM(__udivmoddi4),
  205. REG_SYM(__udivsi3),
  206. REG_SYM(__umoddi3),
  207. REG_SYM(__umodsi3),
  208. REG_SYM(__unorddf2),
  209. REG_SYM(__unordsf2),
  210. };
  211. static void
  212. set_error_buf(char *error_buf, uint32 error_buf_size, const char *string)
  213. {
  214. if (error_buf != NULL)
  215. snprintf(error_buf, error_buf_size, "%s", string);
  216. }
  217. SymbolMap *
  218. get_target_symbol_map(uint32 *sym_num)
  219. {
  220. *sym_num = sizeof(target_sym_map) / sizeof(SymbolMap);
  221. return target_sym_map;
  222. }
  223. #define BUILD_TARGET_ARM_DEFAULT "armv4"
  224. void
  225. get_current_target(char *target_buf, uint32 target_buf_size)
  226. {
  227. const char *s = BUILD_TARGET;
  228. size_t s_size = sizeof(BUILD_TARGET);
  229. char *d = target_buf;
  230. /* Set to "armv4" by default if sub version isn't specified */
  231. if (strcmp(s, "ARM") == 0) {
  232. s = BUILD_TARGET_ARM_DEFAULT;
  233. s_size = sizeof(BUILD_TARGET_ARM_DEFAULT);
  234. }
  235. if (target_buf_size < s_size) {
  236. s_size = target_buf_size;
  237. }
  238. while (--s_size) {
  239. if (*s >= 'A' && *s <= 'Z')
  240. *d++ = *s++ + 'a' - 'A';
  241. else
  242. *d++ = *s++;
  243. }
  244. /* Ensure the string is null byte ('\0') terminated */
  245. *d = '\0';
  246. }
  247. #undef BUILD_TARGET_ARM_DEFAULT
  248. uint32
  249. get_plt_item_size(void)
  250. {
  251. /* 8 bytes instructions and 4 bytes symbol address */
  252. return 12;
  253. }
  254. uint32
  255. get_plt_table_size()
  256. {
  257. return get_plt_item_size() * (sizeof(target_sym_map) / sizeof(SymbolMap));
  258. }
  259. void
  260. init_plt_table(uint8 *plt)
  261. {
  262. uint32 i, num = sizeof(target_sym_map) / sizeof(SymbolMap);
  263. for (i = 0; i < num; i++) {
  264. uint32 *p = (uint32 *)plt;
  265. /* ldr pc, [pc] */
  266. *p++ = 0xe59ff000;
  267. /* nop */
  268. *p++ = 0xe1a00000;
  269. /* symbol addr */
  270. *p++ = (uint32)(uintptr_t)target_sym_map[i].symbol_addr;
  271. plt += get_plt_item_size();
  272. }
  273. }
  274. static bool
  275. check_reloc_offset(uint32 target_section_size, uint64 reloc_offset,
  276. uint32 reloc_data_size, char *error_buf,
  277. uint32 error_buf_size)
  278. {
  279. if (!(reloc_offset < (uint64)target_section_size
  280. && reloc_offset + reloc_data_size <= (uint64)target_section_size)) {
  281. set_error_buf(error_buf, error_buf_size,
  282. "AOT module load failed: invalid relocation offset.");
  283. return false;
  284. }
  285. return true;
  286. }
  287. bool
  288. apply_relocation(AOTModule *module, uint8 *target_section_addr,
  289. uint32 target_section_size, uint64 reloc_offset,
  290. int64 reloc_addend, uint32 reloc_type, void *symbol_addr,
  291. int32 symbol_index, char *error_buf, uint32 error_buf_size)
  292. {
  293. switch (reloc_type) {
  294. case R_ARM_CALL:
  295. case R_ARM_JMP24:
  296. {
  297. intptr_t result;
  298. int32 RESULT_MASK = 0x03FFFFFE;
  299. int32 insn = *(int32 *)(target_section_addr + reloc_offset);
  300. /* Initial addend: sign_extend(insn[23:0] << 2) */
  301. int32 initial_addend =
  302. ((insn & 0xFFFFFF) << 2) | ((insn & 0x800000) ? 0xFC000000 : 0);
  303. CHECK_RELOC_OFFSET(sizeof(int32));
  304. if (symbol_index < 0) {
  305. /* Symbol address itself is an AOT function.
  306. * Apply relocation with the symbol directly.
  307. * Suppose the symbol address is in +-32MB relative
  308. * to the relocation address.
  309. */
  310. /* operation: ((S + A) | T) - P where S is symbol address and T
  311. * is 0 */
  312. result =
  313. (intptr_t)((uintptr_t)symbol_addr + (intptr_t)reloc_addend
  314. - (uintptr_t)(target_section_addr
  315. + reloc_offset));
  316. }
  317. else {
  318. if (reloc_addend > 0) {
  319. set_error_buf(
  320. error_buf, error_buf_size,
  321. "AOT module load failed: relocate to plt table "
  322. "with reloc addend larger than 0 is unsupported.");
  323. return false;
  324. }
  325. /* Symbol address is not an AOT function,
  326. * but a function of runtime or native. Its address is
  327. * beyond of the +-32MB space. Apply relocation with
  328. * the PLT which branch to the target symbol address.
  329. */
  330. /* operation: ((S + A) | T) - P where S is PLT address and T is
  331. * 0 */
  332. uint8 *plt = (uint8 *)module->code + module->code_size
  333. - get_plt_table_size()
  334. + get_plt_item_size() * symbol_index;
  335. result = (intptr_t)((uintptr_t)plt + (intptr_t)reloc_addend
  336. - (uintptr_t)(target_section_addr
  337. + reloc_offset));
  338. }
  339. result += initial_addend;
  340. /* Check overflow: +-32MB */
  341. if (result > (32 * BH_MB) || result < (-32 * BH_MB)) {
  342. set_error_buf(error_buf, error_buf_size,
  343. "AOT module load failed: "
  344. "target address out of range.");
  345. return false;
  346. }
  347. *(int32 *)(target_section_addr + reloc_offset) =
  348. (int32)((insn & 0xff000000)
  349. | (((int32)result & RESULT_MASK) >> 2));
  350. break;
  351. }
  352. case R_ARM_ABS32:
  353. {
  354. intptr_t initial_addend;
  355. /* (S + A) | T where T is 0 */
  356. CHECK_RELOC_OFFSET(sizeof(void *));
  357. initial_addend =
  358. *(intptr_t *)(target_section_addr + (uint32)reloc_offset);
  359. *(uintptr_t *)(target_section_addr + reloc_offset) =
  360. (uintptr_t)symbol_addr + initial_addend
  361. + (intptr_t)reloc_addend;
  362. break;
  363. }
  364. case R_ARM_MOVW_ABS_NC:
  365. case R_ARM_MOVT_ABS:
  366. {
  367. uintptr_t *loc;
  368. uintptr_t addr;
  369. CHECK_RELOC_OFFSET(sizeof(void *));
  370. loc = (uintptr_t *)(target_section_addr + (uint32)reloc_offset);
  371. addr = (uintptr_t)symbol_addr + (intptr_t)reloc_addend;
  372. if (reloc_type == R_ARM_MOVT_ABS) {
  373. addr >>= 16;
  374. }
  375. *loc = ((*loc) & 0xfff0f000) | ((addr << 4) & 0x000f0000)
  376. | (addr & 0x00000fff);
  377. break;
  378. }
  379. default:
  380. if (error_buf != NULL)
  381. snprintf(error_buf, error_buf_size,
  382. "Load relocation section failed: "
  383. "invalid relocation type %d.",
  384. reloc_type);
  385. return false;
  386. }
  387. return true;
  388. }