module.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * Copyright (C) 2017 Zihao Yu
  5. */
  6. #include <linux/elf.h>
  7. #include <linux/err.h>
  8. #include <linux/errno.h>
  9. #include <linux/hashtable.h>
  10. #include <linux/kernel.h>
  11. #include <linux/log2.h>
  12. #include <linux/moduleloader.h>
  13. #include <linux/sizes.h>
  14. #include <linux/pgtable.h>
  15. #include <asm/alternative.h>
  16. #include <asm/sections.h>
  17. struct used_bucket {
  18. struct list_head head;
  19. struct hlist_head *bucket;
  20. };
  21. struct relocation_head {
  22. struct hlist_node node;
  23. struct list_head rel_entry;
  24. void *location;
  25. };
  26. struct relocation_entry {
  27. struct list_head head;
  28. Elf_Addr value;
  29. unsigned int type;
  30. };
  31. struct relocation_handlers {
  32. int (*reloc_handler)(struct module *me, void *location, Elf_Addr v);
  33. int (*accumulate_handler)(struct module *me, void *location,
  34. long buffer);
  35. };
  36. /*
  37. * The auipc+jalr instruction pair can reach any PC-relative offset
  38. * in the range [-2^31 - 2^11, 2^31 - 2^11)
  39. */
  40. static bool riscv_insn_valid_32bit_offset(ptrdiff_t val)
  41. {
  42. #ifdef CONFIG_32BIT
  43. return true;
  44. #else
  45. return (-(1L << 31) - (1L << 11)) <= val && val < ((1L << 31) - (1L << 11));
  46. #endif
  47. }
  48. static int riscv_insn_rmw(void *location, u32 keep, u32 set)
  49. {
  50. __le16 *parcel = location;
  51. u32 insn = (u32)le16_to_cpu(parcel[0]) | (u32)le16_to_cpu(parcel[1]) << 16;
  52. insn &= keep;
  53. insn |= set;
  54. parcel[0] = cpu_to_le16(insn);
  55. parcel[1] = cpu_to_le16(insn >> 16);
  56. return 0;
  57. }
  58. static int riscv_insn_rvc_rmw(void *location, u16 keep, u16 set)
  59. {
  60. __le16 *parcel = location;
  61. u16 insn = le16_to_cpu(*parcel);
  62. insn &= keep;
  63. insn |= set;
  64. *parcel = cpu_to_le16(insn);
  65. return 0;
  66. }
  67. static int apply_r_riscv_32_rela(struct module *me, void *location, Elf_Addr v)
  68. {
  69. if (v != (u32)v) {
  70. pr_err("%s: value %016llx out of range for 32-bit field\n",
  71. me->name, (long long)v);
  72. return -EINVAL;
  73. }
  74. *(u32 *)location = v;
  75. return 0;
  76. }
  77. static int apply_r_riscv_64_rela(struct module *me, void *location, Elf_Addr v)
  78. {
  79. *(u64 *)location = v;
  80. return 0;
  81. }
  82. static int apply_r_riscv_branch_rela(struct module *me, void *location,
  83. Elf_Addr v)
  84. {
  85. ptrdiff_t offset = (void *)v - location;
  86. u32 imm12 = (offset & 0x1000) << (31 - 12);
  87. u32 imm11 = (offset & 0x800) >> (11 - 7);
  88. u32 imm10_5 = (offset & 0x7e0) << (30 - 10);
  89. u32 imm4_1 = (offset & 0x1e) << (11 - 4);
  90. return riscv_insn_rmw(location, 0x1fff07f, imm12 | imm11 | imm10_5 | imm4_1);
  91. }
  92. static int apply_r_riscv_jal_rela(struct module *me, void *location,
  93. Elf_Addr v)
  94. {
  95. ptrdiff_t offset = (void *)v - location;
  96. u32 imm20 = (offset & 0x100000) << (31 - 20);
  97. u32 imm19_12 = (offset & 0xff000);
  98. u32 imm11 = (offset & 0x800) << (20 - 11);
  99. u32 imm10_1 = (offset & 0x7fe) << (30 - 10);
  100. return riscv_insn_rmw(location, 0xfff, imm20 | imm19_12 | imm11 | imm10_1);
  101. }
  102. static int apply_r_riscv_rvc_branch_rela(struct module *me, void *location,
  103. Elf_Addr v)
  104. {
  105. ptrdiff_t offset = (void *)v - location;
  106. u16 imm8 = (offset & 0x100) << (12 - 8);
  107. u16 imm7_6 = (offset & 0xc0) >> (6 - 5);
  108. u16 imm5 = (offset & 0x20) >> (5 - 2);
  109. u16 imm4_3 = (offset & 0x18) << (12 - 5);
  110. u16 imm2_1 = (offset & 0x6) << (12 - 10);
  111. return riscv_insn_rvc_rmw(location, 0xe383,
  112. imm8 | imm7_6 | imm5 | imm4_3 | imm2_1);
  113. }
  114. static int apply_r_riscv_rvc_jump_rela(struct module *me, void *location,
  115. Elf_Addr v)
  116. {
  117. ptrdiff_t offset = (void *)v - location;
  118. u16 imm11 = (offset & 0x800) << (12 - 11);
  119. u16 imm10 = (offset & 0x400) >> (10 - 8);
  120. u16 imm9_8 = (offset & 0x300) << (12 - 11);
  121. u16 imm7 = (offset & 0x80) >> (7 - 6);
  122. u16 imm6 = (offset & 0x40) << (12 - 11);
  123. u16 imm5 = (offset & 0x20) >> (5 - 2);
  124. u16 imm4 = (offset & 0x10) << (12 - 5);
  125. u16 imm3_1 = (offset & 0xe) << (12 - 10);
  126. return riscv_insn_rvc_rmw(location, 0xe003,
  127. imm11 | imm10 | imm9_8 | imm7 | imm6 | imm5 | imm4 | imm3_1);
  128. }
  129. static int apply_r_riscv_pcrel_hi20_rela(struct module *me, void *location,
  130. Elf_Addr v)
  131. {
  132. ptrdiff_t offset = (void *)v - location;
  133. if (!riscv_insn_valid_32bit_offset(offset)) {
  134. pr_err(
  135. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  136. me->name, (long long)v, location);
  137. return -EINVAL;
  138. }
  139. return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
  140. }
  141. static int apply_r_riscv_pcrel_lo12_i_rela(struct module *me, void *location,
  142. Elf_Addr v)
  143. {
  144. /*
  145. * v is the lo12 value to fill. It is calculated before calling this
  146. * handler.
  147. */
  148. return riscv_insn_rmw(location, 0xfffff, (v & 0xfff) << 20);
  149. }
  150. static int apply_r_riscv_pcrel_lo12_s_rela(struct module *me, void *location,
  151. Elf_Addr v)
  152. {
  153. /*
  154. * v is the lo12 value to fill. It is calculated before calling this
  155. * handler.
  156. */
  157. u32 imm11_5 = (v & 0xfe0) << (31 - 11);
  158. u32 imm4_0 = (v & 0x1f) << (11 - 4);
  159. return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
  160. }
  161. static int apply_r_riscv_hi20_rela(struct module *me, void *location,
  162. Elf_Addr v)
  163. {
  164. if (IS_ENABLED(CONFIG_CMODEL_MEDLOW)) {
  165. pr_err(
  166. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  167. me->name, (long long)v, location);
  168. return -EINVAL;
  169. }
  170. return riscv_insn_rmw(location, 0xfff, ((s32)v + 0x800) & 0xfffff000);
  171. }
  172. static int apply_r_riscv_lo12_i_rela(struct module *me, void *location,
  173. Elf_Addr v)
  174. {
  175. /* Skip medlow checking because of filtering by HI20 already */
  176. s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
  177. s32 lo12 = ((s32)v - hi20);
  178. return riscv_insn_rmw(location, 0xfffff, (lo12 & 0xfff) << 20);
  179. }
  180. static int apply_r_riscv_lo12_s_rela(struct module *me, void *location,
  181. Elf_Addr v)
  182. {
  183. /* Skip medlow checking because of filtering by HI20 already */
  184. s32 hi20 = ((s32)v + 0x800) & 0xfffff000;
  185. s32 lo12 = ((s32)v - hi20);
  186. u32 imm11_5 = (lo12 & 0xfe0) << (31 - 11);
  187. u32 imm4_0 = (lo12 & 0x1f) << (11 - 4);
  188. return riscv_insn_rmw(location, 0x1fff07f, imm11_5 | imm4_0);
  189. }
  190. static int apply_r_riscv_got_hi20_rela(struct module *me, void *location,
  191. Elf_Addr v)
  192. {
  193. ptrdiff_t offset = (void *)v - location;
  194. /* Always emit the got entry */
  195. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
  196. offset = (void *)module_emit_got_entry(me, v) - location;
  197. } else {
  198. pr_err(
  199. "%s: can not generate the GOT entry for symbol = %016llx from PC = %p\n",
  200. me->name, (long long)v, location);
  201. return -EINVAL;
  202. }
  203. return riscv_insn_rmw(location, 0xfff, (offset + 0x800) & 0xfffff000);
  204. }
  205. static int apply_r_riscv_call_plt_rela(struct module *me, void *location,
  206. Elf_Addr v)
  207. {
  208. ptrdiff_t offset = (void *)v - location;
  209. u32 hi20, lo12;
  210. if (!riscv_insn_valid_32bit_offset(offset)) {
  211. /* Only emit the plt entry if offset over 32-bit range */
  212. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
  213. offset = (void *)module_emit_plt_entry(me, v) - location;
  214. } else {
  215. pr_err(
  216. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  217. me->name, (long long)v, location);
  218. return -EINVAL;
  219. }
  220. }
  221. hi20 = (offset + 0x800) & 0xfffff000;
  222. lo12 = (offset - hi20) & 0xfff;
  223. riscv_insn_rmw(location, 0xfff, hi20);
  224. return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
  225. }
  226. static int apply_r_riscv_call_rela(struct module *me, void *location,
  227. Elf_Addr v)
  228. {
  229. ptrdiff_t offset = (void *)v - location;
  230. u32 hi20, lo12;
  231. if (!riscv_insn_valid_32bit_offset(offset)) {
  232. pr_err(
  233. "%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  234. me->name, (long long)v, location);
  235. return -EINVAL;
  236. }
  237. hi20 = (offset + 0x800) & 0xfffff000;
  238. lo12 = (offset - hi20) & 0xfff;
  239. riscv_insn_rmw(location, 0xfff, hi20);
  240. return riscv_insn_rmw(location + 4, 0xfffff, lo12 << 20);
  241. }
  242. static int apply_r_riscv_relax_rela(struct module *me, void *location,
  243. Elf_Addr v)
  244. {
  245. return 0;
  246. }
  247. static int apply_r_riscv_align_rela(struct module *me, void *location,
  248. Elf_Addr v)
  249. {
  250. pr_err(
  251. "%s: The unexpected relocation type 'R_RISCV_ALIGN' from PC = %p\n",
  252. me->name, location);
  253. return -EINVAL;
  254. }
  255. static int apply_r_riscv_add8_rela(struct module *me, void *location, Elf_Addr v)
  256. {
  257. *(u8 *)location += (u8)v;
  258. return 0;
  259. }
  260. static int apply_r_riscv_add16_rela(struct module *me, void *location,
  261. Elf_Addr v)
  262. {
  263. *(u16 *)location += (u16)v;
  264. return 0;
  265. }
  266. static int apply_r_riscv_add32_rela(struct module *me, void *location,
  267. Elf_Addr v)
  268. {
  269. *(u32 *)location += (u32)v;
  270. return 0;
  271. }
  272. static int apply_r_riscv_add64_rela(struct module *me, void *location,
  273. Elf_Addr v)
  274. {
  275. *(u64 *)location += (u64)v;
  276. return 0;
  277. }
  278. static int apply_r_riscv_sub8_rela(struct module *me, void *location, Elf_Addr v)
  279. {
  280. *(u8 *)location -= (u8)v;
  281. return 0;
  282. }
  283. static int apply_r_riscv_sub16_rela(struct module *me, void *location,
  284. Elf_Addr v)
  285. {
  286. *(u16 *)location -= (u16)v;
  287. return 0;
  288. }
  289. static int apply_r_riscv_sub32_rela(struct module *me, void *location,
  290. Elf_Addr v)
  291. {
  292. *(u32 *)location -= (u32)v;
  293. return 0;
  294. }
  295. static int apply_r_riscv_sub64_rela(struct module *me, void *location,
  296. Elf_Addr v)
  297. {
  298. *(u64 *)location -= (u64)v;
  299. return 0;
  300. }
  301. static int dynamic_linking_not_supported(struct module *me, void *location,
  302. Elf_Addr v)
  303. {
  304. pr_err("%s: Dynamic linking not supported in kernel modules PC = %p\n",
  305. me->name, location);
  306. return -EINVAL;
  307. }
  308. static int tls_not_supported(struct module *me, void *location, Elf_Addr v)
  309. {
  310. pr_err("%s: Thread local storage not supported in kernel modules PC = %p\n",
  311. me->name, location);
  312. return -EINVAL;
  313. }
  314. static int apply_r_riscv_sub6_rela(struct module *me, void *location, Elf_Addr v)
  315. {
  316. u8 *byte = location;
  317. u8 value = v;
  318. *byte = (*byte - (value & 0x3f)) & 0x3f;
  319. return 0;
  320. }
  321. static int apply_r_riscv_set6_rela(struct module *me, void *location, Elf_Addr v)
  322. {
  323. u8 *byte = location;
  324. u8 value = v;
  325. *byte = (*byte & 0xc0) | (value & 0x3f);
  326. return 0;
  327. }
  328. static int apply_r_riscv_set8_rela(struct module *me, void *location, Elf_Addr v)
  329. {
  330. *(u8 *)location = (u8)v;
  331. return 0;
  332. }
  333. static int apply_r_riscv_set16_rela(struct module *me, void *location,
  334. Elf_Addr v)
  335. {
  336. *(u16 *)location = (u16)v;
  337. return 0;
  338. }
  339. static int apply_r_riscv_set32_rela(struct module *me, void *location,
  340. Elf_Addr v)
  341. {
  342. *(u32 *)location = (u32)v;
  343. return 0;
  344. }
  345. static int apply_r_riscv_32_pcrel_rela(struct module *me, void *location,
  346. Elf_Addr v)
  347. {
  348. *(u32 *)location = v - (uintptr_t)location;
  349. return 0;
  350. }
  351. static int apply_r_riscv_plt32_rela(struct module *me, void *location,
  352. Elf_Addr v)
  353. {
  354. ptrdiff_t offset = (void *)v - location;
  355. if (!riscv_insn_valid_32bit_offset(offset)) {
  356. /* Only emit the plt entry if offset over 32-bit range */
  357. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)) {
  358. offset = (void *)module_emit_plt_entry(me, v) - location;
  359. } else {
  360. pr_err("%s: target %016llx can not be addressed by the 32-bit offset from PC = %p\n",
  361. me->name, (long long)v, location);
  362. return -EINVAL;
  363. }
  364. }
  365. *(u32 *)location = (u32)offset;
  366. return 0;
  367. }
  368. static int apply_r_riscv_set_uleb128(struct module *me, void *location, Elf_Addr v)
  369. {
  370. *(long *)location = v;
  371. return 0;
  372. }
  373. static int apply_r_riscv_sub_uleb128(struct module *me, void *location, Elf_Addr v)
  374. {
  375. *(long *)location -= v;
  376. return 0;
  377. }
  378. static int apply_6_bit_accumulation(struct module *me, void *location, long buffer)
  379. {
  380. u8 *byte = location;
  381. u8 value = buffer;
  382. if (buffer > 0x3f) {
  383. pr_err("%s: value %ld out of range for 6-bit relocation.\n",
  384. me->name, buffer);
  385. return -EINVAL;
  386. }
  387. *byte = (*byte & 0xc0) | (value & 0x3f);
  388. return 0;
  389. }
  390. static int apply_8_bit_accumulation(struct module *me, void *location, long buffer)
  391. {
  392. if (buffer > U8_MAX) {
  393. pr_err("%s: value %ld out of range for 8-bit relocation.\n",
  394. me->name, buffer);
  395. return -EINVAL;
  396. }
  397. *(u8 *)location = (u8)buffer;
  398. return 0;
  399. }
  400. static int apply_16_bit_accumulation(struct module *me, void *location, long buffer)
  401. {
  402. if (buffer > U16_MAX) {
  403. pr_err("%s: value %ld out of range for 16-bit relocation.\n",
  404. me->name, buffer);
  405. return -EINVAL;
  406. }
  407. *(u16 *)location = (u16)buffer;
  408. return 0;
  409. }
  410. static int apply_32_bit_accumulation(struct module *me, void *location, long buffer)
  411. {
  412. if (buffer > U32_MAX) {
  413. pr_err("%s: value %ld out of range for 32-bit relocation.\n",
  414. me->name, buffer);
  415. return -EINVAL;
  416. }
  417. *(u32 *)location = (u32)buffer;
  418. return 0;
  419. }
  420. static int apply_64_bit_accumulation(struct module *me, void *location, long buffer)
  421. {
  422. *(u64 *)location = (u64)buffer;
  423. return 0;
  424. }
  425. static int apply_uleb128_accumulation(struct module *me, void *location, long buffer)
  426. {
  427. /*
  428. * ULEB128 is a variable length encoding. Encode the buffer into
  429. * the ULEB128 data format.
  430. */
  431. u8 *p = location;
  432. while (buffer != 0) {
  433. u8 value = buffer & 0x7f;
  434. buffer >>= 7;
  435. value |= (!!buffer) << 7;
  436. *p++ = value;
  437. }
  438. return 0;
  439. }
  440. /*
  441. * Relocations defined in the riscv-elf-psabi-doc.
  442. * This handles static linking only.
  443. */
  444. static const struct relocation_handlers reloc_handlers[] = {
  445. [R_RISCV_32] = { .reloc_handler = apply_r_riscv_32_rela },
  446. [R_RISCV_64] = { .reloc_handler = apply_r_riscv_64_rela },
  447. [R_RISCV_RELATIVE] = { .reloc_handler = dynamic_linking_not_supported },
  448. [R_RISCV_COPY] = { .reloc_handler = dynamic_linking_not_supported },
  449. [R_RISCV_JUMP_SLOT] = { .reloc_handler = dynamic_linking_not_supported },
  450. [R_RISCV_TLS_DTPMOD32] = { .reloc_handler = dynamic_linking_not_supported },
  451. [R_RISCV_TLS_DTPMOD64] = { .reloc_handler = dynamic_linking_not_supported },
  452. [R_RISCV_TLS_DTPREL32] = { .reloc_handler = dynamic_linking_not_supported },
  453. [R_RISCV_TLS_DTPREL64] = { .reloc_handler = dynamic_linking_not_supported },
  454. [R_RISCV_TLS_TPREL32] = { .reloc_handler = dynamic_linking_not_supported },
  455. [R_RISCV_TLS_TPREL64] = { .reloc_handler = dynamic_linking_not_supported },
  456. /* 12-15 undefined */
  457. [R_RISCV_BRANCH] = { .reloc_handler = apply_r_riscv_branch_rela },
  458. [R_RISCV_JAL] = { .reloc_handler = apply_r_riscv_jal_rela },
  459. [R_RISCV_CALL] = { .reloc_handler = apply_r_riscv_call_rela },
  460. [R_RISCV_CALL_PLT] = { .reloc_handler = apply_r_riscv_call_plt_rela },
  461. [R_RISCV_GOT_HI20] = { .reloc_handler = apply_r_riscv_got_hi20_rela },
  462. [R_RISCV_TLS_GOT_HI20] = { .reloc_handler = tls_not_supported },
  463. [R_RISCV_TLS_GD_HI20] = { .reloc_handler = tls_not_supported },
  464. [R_RISCV_PCREL_HI20] = { .reloc_handler = apply_r_riscv_pcrel_hi20_rela },
  465. [R_RISCV_PCREL_LO12_I] = { .reloc_handler = apply_r_riscv_pcrel_lo12_i_rela },
  466. [R_RISCV_PCREL_LO12_S] = { .reloc_handler = apply_r_riscv_pcrel_lo12_s_rela },
  467. [R_RISCV_HI20] = { .reloc_handler = apply_r_riscv_hi20_rela },
  468. [R_RISCV_LO12_I] = { .reloc_handler = apply_r_riscv_lo12_i_rela },
  469. [R_RISCV_LO12_S] = { .reloc_handler = apply_r_riscv_lo12_s_rela },
  470. [R_RISCV_TPREL_HI20] = { .reloc_handler = tls_not_supported },
  471. [R_RISCV_TPREL_LO12_I] = { .reloc_handler = tls_not_supported },
  472. [R_RISCV_TPREL_LO12_S] = { .reloc_handler = tls_not_supported },
  473. [R_RISCV_TPREL_ADD] = { .reloc_handler = tls_not_supported },
  474. [R_RISCV_ADD8] = { .reloc_handler = apply_r_riscv_add8_rela,
  475. .accumulate_handler = apply_8_bit_accumulation },
  476. [R_RISCV_ADD16] = { .reloc_handler = apply_r_riscv_add16_rela,
  477. .accumulate_handler = apply_16_bit_accumulation },
  478. [R_RISCV_ADD32] = { .reloc_handler = apply_r_riscv_add32_rela,
  479. .accumulate_handler = apply_32_bit_accumulation },
  480. [R_RISCV_ADD64] = { .reloc_handler = apply_r_riscv_add64_rela,
  481. .accumulate_handler = apply_64_bit_accumulation },
  482. [R_RISCV_SUB8] = { .reloc_handler = apply_r_riscv_sub8_rela,
  483. .accumulate_handler = apply_8_bit_accumulation },
  484. [R_RISCV_SUB16] = { .reloc_handler = apply_r_riscv_sub16_rela,
  485. .accumulate_handler = apply_16_bit_accumulation },
  486. [R_RISCV_SUB32] = { .reloc_handler = apply_r_riscv_sub32_rela,
  487. .accumulate_handler = apply_32_bit_accumulation },
  488. [R_RISCV_SUB64] = { .reloc_handler = apply_r_riscv_sub64_rela,
  489. .accumulate_handler = apply_64_bit_accumulation },
  490. /* 41-42 reserved for future standard use */
  491. [R_RISCV_ALIGN] = { .reloc_handler = apply_r_riscv_align_rela },
  492. [R_RISCV_RVC_BRANCH] = { .reloc_handler = apply_r_riscv_rvc_branch_rela },
  493. [R_RISCV_RVC_JUMP] = { .reloc_handler = apply_r_riscv_rvc_jump_rela },
  494. /* 46-50 reserved for future standard use */
  495. [R_RISCV_RELAX] = { .reloc_handler = apply_r_riscv_relax_rela },
  496. [R_RISCV_SUB6] = { .reloc_handler = apply_r_riscv_sub6_rela,
  497. .accumulate_handler = apply_6_bit_accumulation },
  498. [R_RISCV_SET6] = { .reloc_handler = apply_r_riscv_set6_rela,
  499. .accumulate_handler = apply_6_bit_accumulation },
  500. [R_RISCV_SET8] = { .reloc_handler = apply_r_riscv_set8_rela,
  501. .accumulate_handler = apply_8_bit_accumulation },
  502. [R_RISCV_SET16] = { .reloc_handler = apply_r_riscv_set16_rela,
  503. .accumulate_handler = apply_16_bit_accumulation },
  504. [R_RISCV_SET32] = { .reloc_handler = apply_r_riscv_set32_rela,
  505. .accumulate_handler = apply_32_bit_accumulation },
  506. [R_RISCV_32_PCREL] = { .reloc_handler = apply_r_riscv_32_pcrel_rela },
  507. [R_RISCV_IRELATIVE] = { .reloc_handler = dynamic_linking_not_supported },
  508. [R_RISCV_PLT32] = { .reloc_handler = apply_r_riscv_plt32_rela },
  509. [R_RISCV_SET_ULEB128] = { .reloc_handler = apply_r_riscv_set_uleb128,
  510. .accumulate_handler = apply_uleb128_accumulation },
  511. [R_RISCV_SUB_ULEB128] = { .reloc_handler = apply_r_riscv_sub_uleb128,
  512. .accumulate_handler = apply_uleb128_accumulation },
  513. /* 62-191 reserved for future standard use */
  514. /* 192-255 nonstandard ABI extensions */
  515. };
  516. static void
  517. process_accumulated_relocations(struct module *me,
  518. struct hlist_head **relocation_hashtable,
  519. struct list_head *used_buckets_list)
  520. {
  521. /*
  522. * Only ADD/SUB/SET/ULEB128 should end up here.
  523. *
  524. * Each bucket may have more than one relocation location. All
  525. * relocations for a location are stored in a list in a bucket.
  526. *
  527. * Relocations are applied to a temp variable before being stored to the
  528. * provided location to check for overflow. This also allows ULEB128 to
  529. * properly decide how many entries are needed before storing to
  530. * location. The final value is stored into location using the handler
  531. * for the last relocation to an address.
  532. *
  533. * Three layers of indexing:
  534. * - Each of the buckets in use
  535. * - Groups of relocations in each bucket by location address
  536. * - Each relocation entry for a location address
  537. */
  538. struct used_bucket *bucket_iter;
  539. struct used_bucket *bucket_iter_tmp;
  540. struct relocation_head *rel_head_iter;
  541. struct hlist_node *rel_head_iter_tmp;
  542. struct relocation_entry *rel_entry_iter;
  543. struct relocation_entry *rel_entry_iter_tmp;
  544. int curr_type;
  545. void *location;
  546. long buffer;
  547. list_for_each_entry_safe(bucket_iter, bucket_iter_tmp,
  548. used_buckets_list, head) {
  549. hlist_for_each_entry_safe(rel_head_iter, rel_head_iter_tmp,
  550. bucket_iter->bucket, node) {
  551. buffer = 0;
  552. location = rel_head_iter->location;
  553. list_for_each_entry_safe(rel_entry_iter,
  554. rel_entry_iter_tmp,
  555. &rel_head_iter->rel_entry,
  556. head) {
  557. curr_type = rel_entry_iter->type;
  558. reloc_handlers[curr_type].reloc_handler(
  559. me, &buffer, rel_entry_iter->value);
  560. kfree(rel_entry_iter);
  561. }
  562. reloc_handlers[curr_type].accumulate_handler(
  563. me, location, buffer);
  564. kfree(rel_head_iter);
  565. }
  566. kfree(bucket_iter);
  567. }
  568. kvfree(*relocation_hashtable);
  569. }
  570. static int add_relocation_to_accumulate(struct module *me, int type,
  571. void *location,
  572. unsigned int hashtable_bits, Elf_Addr v,
  573. struct hlist_head *relocation_hashtable,
  574. struct list_head *used_buckets_list)
  575. {
  576. struct relocation_entry *entry;
  577. struct relocation_head *rel_head;
  578. struct hlist_head *current_head;
  579. struct used_bucket *bucket;
  580. unsigned long hash;
  581. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  582. if (!entry)
  583. return -ENOMEM;
  584. INIT_LIST_HEAD(&entry->head);
  585. entry->type = type;
  586. entry->value = v;
  587. hash = hash_min((uintptr_t)location, hashtable_bits);
  588. current_head = &relocation_hashtable[hash];
  589. /*
  590. * Search for the relocation_head for the relocations that happen at the
  591. * provided location
  592. */
  593. bool found = false;
  594. struct relocation_head *rel_head_iter;
  595. hlist_for_each_entry(rel_head_iter, current_head, node) {
  596. if (rel_head_iter->location == location) {
  597. found = true;
  598. rel_head = rel_head_iter;
  599. break;
  600. }
  601. }
  602. /*
  603. * If there has not yet been any relocations at the provided location,
  604. * create a relocation_head for that location and populate it with this
  605. * relocation_entry.
  606. */
  607. if (!found) {
  608. rel_head = kmalloc(sizeof(*rel_head), GFP_KERNEL);
  609. if (!rel_head) {
  610. kfree(entry);
  611. return -ENOMEM;
  612. }
  613. INIT_LIST_HEAD(&rel_head->rel_entry);
  614. rel_head->location = location;
  615. INIT_HLIST_NODE(&rel_head->node);
  616. if (!current_head->first) {
  617. bucket =
  618. kmalloc(sizeof(struct used_bucket), GFP_KERNEL);
  619. if (!bucket) {
  620. kfree(entry);
  621. kfree(rel_head);
  622. return -ENOMEM;
  623. }
  624. INIT_LIST_HEAD(&bucket->head);
  625. bucket->bucket = current_head;
  626. list_add(&bucket->head, used_buckets_list);
  627. }
  628. hlist_add_head(&rel_head->node, current_head);
  629. }
  630. /* Add relocation to head of discovered rel_head */
  631. list_add_tail(&entry->head, &rel_head->rel_entry);
  632. return 0;
  633. }
  634. static unsigned int
  635. initialize_relocation_hashtable(unsigned int num_relocations,
  636. struct hlist_head **relocation_hashtable)
  637. {
  638. /* Can safely assume that bits is not greater than sizeof(long) */
  639. unsigned long hashtable_size = roundup_pow_of_two(num_relocations);
  640. /*
  641. * When hashtable_size == 1, hashtable_bits == 0.
  642. * This is valid because the hashing algorithm returns 0 in this case.
  643. */
  644. unsigned int hashtable_bits = ilog2(hashtable_size);
  645. /*
  646. * Double size of hashtable if num_relocations * 1.25 is greater than
  647. * hashtable_size.
  648. */
  649. int should_double_size = ((num_relocations + (num_relocations >> 2)) > (hashtable_size));
  650. hashtable_bits += should_double_size;
  651. hashtable_size <<= should_double_size;
  652. /* Number of relocations may be large, so kvmalloc it */
  653. *relocation_hashtable = kvmalloc_array(hashtable_size,
  654. sizeof(**relocation_hashtable),
  655. GFP_KERNEL);
  656. if (!*relocation_hashtable)
  657. return 0;
  658. __hash_init(*relocation_hashtable, hashtable_size);
  659. return hashtable_bits;
  660. }
  661. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  662. unsigned int symindex, unsigned int relsec,
  663. struct module *me)
  664. {
  665. Elf_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  666. int (*handler)(struct module *me, void *location, Elf_Addr v);
  667. Elf_Sym *sym;
  668. void *location;
  669. unsigned int i, type;
  670. unsigned int j_idx = 0;
  671. Elf_Addr v;
  672. int res;
  673. unsigned int num_relocations = sechdrs[relsec].sh_size / sizeof(*rel);
  674. struct hlist_head *relocation_hashtable;
  675. unsigned int hashtable_bits;
  676. LIST_HEAD(used_buckets_list);
  677. hashtable_bits = initialize_relocation_hashtable(num_relocations,
  678. &relocation_hashtable);
  679. if (!relocation_hashtable)
  680. return -ENOMEM;
  681. pr_debug("Applying relocate section %u to %u\n", relsec,
  682. sechdrs[relsec].sh_info);
  683. for (i = 0; i < num_relocations; i++) {
  684. /* This is where to make the change */
  685. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  686. + rel[i].r_offset;
  687. /* This is the symbol it is referring to */
  688. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  689. + ELF_RISCV_R_SYM(rel[i].r_info);
  690. if (IS_ERR_VALUE(sym->st_value)) {
  691. /* Ignore unresolved weak symbol */
  692. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  693. continue;
  694. pr_warn("%s: Unknown symbol %s\n",
  695. me->name, strtab + sym->st_name);
  696. return -ENOENT;
  697. }
  698. type = ELF_RISCV_R_TYPE(rel[i].r_info);
  699. if (type < ARRAY_SIZE(reloc_handlers))
  700. handler = reloc_handlers[type].reloc_handler;
  701. else
  702. handler = NULL;
  703. if (!handler) {
  704. pr_err("%s: Unknown relocation type %u\n",
  705. me->name, type);
  706. return -EINVAL;
  707. }
  708. v = sym->st_value + rel[i].r_addend;
  709. if (type == R_RISCV_PCREL_LO12_I || type == R_RISCV_PCREL_LO12_S) {
  710. unsigned int j = j_idx;
  711. bool found = false;
  712. do {
  713. unsigned long hi20_loc =
  714. sechdrs[sechdrs[relsec].sh_info].sh_addr
  715. + rel[j].r_offset;
  716. u32 hi20_type = ELF_RISCV_R_TYPE(rel[j].r_info);
  717. /* Find the corresponding HI20 relocation entry */
  718. if (hi20_loc == sym->st_value
  719. && (hi20_type == R_RISCV_PCREL_HI20
  720. || hi20_type == R_RISCV_GOT_HI20)) {
  721. s32 hi20, lo12;
  722. Elf_Sym *hi20_sym =
  723. (Elf_Sym *)sechdrs[symindex].sh_addr
  724. + ELF_RISCV_R_SYM(rel[j].r_info);
  725. unsigned long hi20_sym_val =
  726. hi20_sym->st_value
  727. + rel[j].r_addend;
  728. /* Calculate lo12 */
  729. size_t offset = hi20_sym_val - hi20_loc;
  730. if (IS_ENABLED(CONFIG_MODULE_SECTIONS)
  731. && hi20_type == R_RISCV_GOT_HI20) {
  732. offset = module_emit_got_entry(
  733. me, hi20_sym_val);
  734. offset = offset - hi20_loc;
  735. }
  736. hi20 = (offset + 0x800) & 0xfffff000;
  737. lo12 = offset - hi20;
  738. v = lo12;
  739. found = true;
  740. break;
  741. }
  742. j++;
  743. if (j == num_relocations)
  744. j = 0;
  745. } while (j_idx != j);
  746. if (!found) {
  747. pr_err(
  748. "%s: Can not find HI20 relocation information\n",
  749. me->name);
  750. return -EINVAL;
  751. }
  752. /* Record the previous j-loop end index */
  753. j_idx = j;
  754. }
  755. if (reloc_handlers[type].accumulate_handler)
  756. res = add_relocation_to_accumulate(me, type, location,
  757. hashtable_bits, v,
  758. relocation_hashtable,
  759. &used_buckets_list);
  760. else
  761. res = handler(me, location, v);
  762. if (res)
  763. return res;
  764. }
  765. process_accumulated_relocations(me, &relocation_hashtable,
  766. &used_buckets_list);
  767. return 0;
  768. }
  769. int module_finalize(const Elf_Ehdr *hdr,
  770. const Elf_Shdr *sechdrs,
  771. struct module *me)
  772. {
  773. const Elf_Shdr *s;
  774. s = find_section(hdr, sechdrs, ".alternative");
  775. if (s)
  776. apply_module_alternatives((void *)s->sh_addr, s->sh_size);
  777. return 0;
  778. }