module-plts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014-2017 Linaro Ltd. <ard.biesheuvel@linaro.org>
  4. */
  5. #include <linux/elf.h>
  6. #include <linux/ftrace.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleloader.h>
  10. #include <linux/sort.h>
  11. static struct plt_entry __get_adrp_add_pair(u64 dst, u64 pc,
  12. enum aarch64_insn_register reg)
  13. {
  14. u32 adrp, add;
  15. adrp = aarch64_insn_gen_adr(pc, dst, reg, AARCH64_INSN_ADR_TYPE_ADRP);
  16. add = aarch64_insn_gen_add_sub_imm(reg, reg, dst % SZ_4K,
  17. AARCH64_INSN_VARIANT_64BIT,
  18. AARCH64_INSN_ADSB_ADD);
  19. return (struct plt_entry){ cpu_to_le32(adrp), cpu_to_le32(add) };
  20. }
  21. struct plt_entry get_plt_entry(u64 dst, void *pc)
  22. {
  23. struct plt_entry plt;
  24. static u32 br;
  25. if (!br)
  26. br = aarch64_insn_gen_branch_reg(AARCH64_INSN_REG_16,
  27. AARCH64_INSN_BRANCH_NOLINK);
  28. plt = __get_adrp_add_pair(dst, (u64)pc, AARCH64_INSN_REG_16);
  29. plt.br = cpu_to_le32(br);
  30. return plt;
  31. }
  32. static bool plt_entries_equal(const struct plt_entry *a,
  33. const struct plt_entry *b)
  34. {
  35. u64 p, q;
  36. /*
  37. * Check whether both entries refer to the same target:
  38. * do the cheapest checks first.
  39. * If the 'add' or 'br' opcodes are different, then the target
  40. * cannot be the same.
  41. */
  42. if (a->add != b->add || a->br != b->br)
  43. return false;
  44. p = ALIGN_DOWN((u64)a, SZ_4K);
  45. q = ALIGN_DOWN((u64)b, SZ_4K);
  46. /*
  47. * If the 'adrp' opcodes are the same then we just need to check
  48. * that they refer to the same 4k region.
  49. */
  50. if (a->adrp == b->adrp && p == q)
  51. return true;
  52. return (p + aarch64_insn_adrp_get_offset(le32_to_cpu(a->adrp))) ==
  53. (q + aarch64_insn_adrp_get_offset(le32_to_cpu(b->adrp)));
  54. }
  55. u64 module_emit_plt_entry(struct module *mod, Elf64_Shdr *sechdrs,
  56. void *loc, const Elf64_Rela *rela,
  57. Elf64_Sym *sym)
  58. {
  59. struct mod_plt_sec *pltsec = !within_module_init((unsigned long)loc, mod) ?
  60. &mod->arch.core : &mod->arch.init;
  61. struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;
  62. int i = pltsec->plt_num_entries;
  63. int j = i - 1;
  64. u64 val = sym->st_value + rela->r_addend;
  65. if (is_forbidden_offset_for_adrp(&plt[i].adrp))
  66. i++;
  67. plt[i] = get_plt_entry(val, &plt[i]);
  68. /*
  69. * Check if the entry we just created is a duplicate. Given that the
  70. * relocations are sorted, this will be the last entry we allocated.
  71. * (if one exists).
  72. */
  73. if (j >= 0 && plt_entries_equal(plt + i, plt + j))
  74. return (u64)&plt[j];
  75. pltsec->plt_num_entries += i - j;
  76. if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
  77. return 0;
  78. return (u64)&plt[i];
  79. }
  80. #ifdef CONFIG_ARM64_ERRATUM_843419
  81. u64 module_emit_veneer_for_adrp(struct module *mod, Elf64_Shdr *sechdrs,
  82. void *loc, u64 val)
  83. {
  84. struct mod_plt_sec *pltsec = !within_module_init((unsigned long)loc, mod) ?
  85. &mod->arch.core : &mod->arch.init;
  86. struct plt_entry *plt = (struct plt_entry *)sechdrs[pltsec->plt_shndx].sh_addr;
  87. int i = pltsec->plt_num_entries++;
  88. u32 br;
  89. int rd;
  90. if (WARN_ON(pltsec->plt_num_entries > pltsec->plt_max_entries))
  91. return 0;
  92. if (is_forbidden_offset_for_adrp(&plt[i].adrp))
  93. i = pltsec->plt_num_entries++;
  94. /* get the destination register of the ADRP instruction */
  95. rd = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RD,
  96. le32_to_cpup((__le32 *)loc));
  97. br = aarch64_insn_gen_branch_imm((u64)&plt[i].br, (u64)loc + 4,
  98. AARCH64_INSN_BRANCH_NOLINK);
  99. plt[i] = __get_adrp_add_pair(val, (u64)&plt[i], rd);
  100. plt[i].br = cpu_to_le32(br);
  101. return (u64)&plt[i];
  102. }
  103. #endif
  104. #define cmp_3way(a, b) ((a) < (b) ? -1 : (a) > (b))
  105. static int cmp_rela(const void *a, const void *b)
  106. {
  107. const Elf64_Rela *x = a, *y = b;
  108. int i;
  109. /* sort by type, symbol index and addend */
  110. i = cmp_3way(ELF64_R_TYPE(x->r_info), ELF64_R_TYPE(y->r_info));
  111. if (i == 0)
  112. i = cmp_3way(ELF64_R_SYM(x->r_info), ELF64_R_SYM(y->r_info));
  113. if (i == 0)
  114. i = cmp_3way(x->r_addend, y->r_addend);
  115. return i;
  116. }
  117. static bool duplicate_rel(const Elf64_Rela *rela, int num)
  118. {
  119. /*
  120. * Entries are sorted by type, symbol index and addend. That means
  121. * that, if a duplicate entry exists, it must be in the preceding
  122. * slot.
  123. */
  124. return num > 0 && cmp_rela(rela + num, rela + num - 1) == 0;
  125. }
  126. static unsigned int count_plts(Elf64_Sym *syms, Elf64_Rela *rela, int num,
  127. Elf64_Word dstidx, Elf_Shdr *dstsec)
  128. {
  129. unsigned int ret = 0;
  130. Elf64_Sym *s;
  131. int i;
  132. for (i = 0; i < num; i++) {
  133. u64 min_align;
  134. switch (ELF64_R_TYPE(rela[i].r_info)) {
  135. case R_AARCH64_JUMP26:
  136. case R_AARCH64_CALL26:
  137. /*
  138. * We only have to consider branch targets that resolve
  139. * to symbols that are defined in a different section.
  140. * This is not simply a heuristic, it is a fundamental
  141. * limitation, since there is no guaranteed way to emit
  142. * PLT entries sufficiently close to the branch if the
  143. * section size exceeds the range of a branch
  144. * instruction. So ignore relocations against defined
  145. * symbols if they live in the same section as the
  146. * relocation target.
  147. */
  148. s = syms + ELF64_R_SYM(rela[i].r_info);
  149. if (s->st_shndx == dstidx)
  150. break;
  151. /*
  152. * Jump relocations with non-zero addends against
  153. * undefined symbols are supported by the ELF spec, but
  154. * do not occur in practice (e.g., 'jump n bytes past
  155. * the entry point of undefined function symbol f').
  156. * So we need to support them, but there is no need to
  157. * take them into consideration when trying to optimize
  158. * this code. So let's only check for duplicates when
  159. * the addend is zero: this allows us to record the PLT
  160. * entry address in the symbol table itself, rather than
  161. * having to search the list for duplicates each time we
  162. * emit one.
  163. */
  164. if (rela[i].r_addend != 0 || !duplicate_rel(rela, i))
  165. ret++;
  166. break;
  167. case R_AARCH64_ADR_PREL_PG_HI21_NC:
  168. case R_AARCH64_ADR_PREL_PG_HI21:
  169. if (!cpus_have_final_cap(ARM64_WORKAROUND_843419))
  170. break;
  171. /*
  172. * Determine the minimal safe alignment for this ADRP
  173. * instruction: the section alignment at which it is
  174. * guaranteed not to appear at a vulnerable offset.
  175. *
  176. * This comes down to finding the least significant zero
  177. * bit in bits [11:3] of the section offset, and
  178. * increasing the section's alignment so that the
  179. * resulting address of this instruction is guaranteed
  180. * to equal the offset in that particular bit (as well
  181. * as all less significant bits). This ensures that the
  182. * address modulo 4 KB != 0xfff8 or 0xfffc (which would
  183. * have all ones in bits [11:3])
  184. */
  185. min_align = 2ULL << ffz(rela[i].r_offset | 0x7);
  186. /*
  187. * Allocate veneer space for each ADRP that may appear
  188. * at a vulnerable offset nonetheless. At relocation
  189. * time, some of these will remain unused since some
  190. * ADRP instructions can be patched to ADR instructions
  191. * instead.
  192. */
  193. if (min_align > SZ_4K)
  194. ret++;
  195. else
  196. dstsec->sh_addralign = max(dstsec->sh_addralign,
  197. min_align);
  198. break;
  199. }
  200. }
  201. if (cpus_have_final_cap(ARM64_WORKAROUND_843419)) {
  202. /*
  203. * Add some slack so we can skip PLT slots that may trigger
  204. * the erratum due to the placement of the ADRP instruction.
  205. */
  206. ret += DIV_ROUND_UP(ret, (SZ_4K / sizeof(struct plt_entry)));
  207. }
  208. return ret;
  209. }
  210. static bool branch_rela_needs_plt(Elf64_Sym *syms, Elf64_Rela *rela,
  211. Elf64_Word dstidx)
  212. {
  213. Elf64_Sym *s = syms + ELF64_R_SYM(rela->r_info);
  214. if (s->st_shndx == dstidx)
  215. return false;
  216. return ELF64_R_TYPE(rela->r_info) == R_AARCH64_JUMP26 ||
  217. ELF64_R_TYPE(rela->r_info) == R_AARCH64_CALL26;
  218. }
  219. /* Group branch PLT relas at the front end of the array. */
  220. static int partition_branch_plt_relas(Elf64_Sym *syms, Elf64_Rela *rela,
  221. int numrels, Elf64_Word dstidx)
  222. {
  223. int i = 0, j = numrels - 1;
  224. while (i < j) {
  225. if (branch_rela_needs_plt(syms, &rela[i], dstidx))
  226. i++;
  227. else if (branch_rela_needs_plt(syms, &rela[j], dstidx))
  228. swap(rela[i], rela[j]);
  229. else
  230. j--;
  231. }
  232. return i;
  233. }
  234. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  235. char *secstrings, struct module *mod)
  236. {
  237. unsigned long core_plts = 0;
  238. unsigned long init_plts = 0;
  239. Elf64_Sym *syms = NULL;
  240. Elf_Shdr *pltsec, *tramp = NULL, *init_tramp = NULL;
  241. int i;
  242. /*
  243. * Find the empty .plt section so we can expand it to store the PLT
  244. * entries. Record the symtab address as well.
  245. */
  246. for (i = 0; i < ehdr->e_shnum; i++) {
  247. if (!strcmp(secstrings + sechdrs[i].sh_name, ".plt"))
  248. mod->arch.core.plt_shndx = i;
  249. else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt"))
  250. mod->arch.init.plt_shndx = i;
  251. else if (!strcmp(secstrings + sechdrs[i].sh_name,
  252. ".text.ftrace_trampoline"))
  253. tramp = sechdrs + i;
  254. else if (!strcmp(secstrings + sechdrs[i].sh_name,
  255. ".init.text.ftrace_trampoline"))
  256. init_tramp = sechdrs + i;
  257. else if (sechdrs[i].sh_type == SHT_SYMTAB)
  258. syms = (Elf64_Sym *)sechdrs[i].sh_addr;
  259. }
  260. if (!mod->arch.core.plt_shndx || !mod->arch.init.plt_shndx) {
  261. pr_err("%s: module PLT section(s) missing\n", mod->name);
  262. return -ENOEXEC;
  263. }
  264. if (!syms) {
  265. pr_err("%s: module symtab section missing\n", mod->name);
  266. return -ENOEXEC;
  267. }
  268. for (i = 0; i < ehdr->e_shnum; i++) {
  269. Elf64_Rela *rels = (void *)ehdr + sechdrs[i].sh_offset;
  270. int nents, numrels = sechdrs[i].sh_size / sizeof(Elf64_Rela);
  271. Elf64_Shdr *dstsec = sechdrs + sechdrs[i].sh_info;
  272. if (sechdrs[i].sh_type != SHT_RELA)
  273. continue;
  274. /* ignore relocations that operate on non-exec sections */
  275. if (!(dstsec->sh_flags & SHF_EXECINSTR))
  276. continue;
  277. /*
  278. * sort branch relocations requiring a PLT by type, symbol index
  279. * and addend
  280. */
  281. nents = partition_branch_plt_relas(syms, rels, numrels,
  282. sechdrs[i].sh_info);
  283. if (nents)
  284. sort(rels, nents, sizeof(Elf64_Rela), cmp_rela, NULL);
  285. if (!module_init_layout_section(secstrings + dstsec->sh_name))
  286. core_plts += count_plts(syms, rels, numrels,
  287. sechdrs[i].sh_info, dstsec);
  288. else
  289. init_plts += count_plts(syms, rels, numrels,
  290. sechdrs[i].sh_info, dstsec);
  291. }
  292. pltsec = sechdrs + mod->arch.core.plt_shndx;
  293. pltsec->sh_type = SHT_NOBITS;
  294. pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  295. pltsec->sh_addralign = L1_CACHE_BYTES;
  296. pltsec->sh_size = (core_plts + 1) * sizeof(struct plt_entry);
  297. mod->arch.core.plt_num_entries = 0;
  298. mod->arch.core.plt_max_entries = core_plts;
  299. pltsec = sechdrs + mod->arch.init.plt_shndx;
  300. pltsec->sh_type = SHT_NOBITS;
  301. pltsec->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  302. pltsec->sh_addralign = L1_CACHE_BYTES;
  303. pltsec->sh_size = (init_plts + 1) * sizeof(struct plt_entry);
  304. mod->arch.init.plt_num_entries = 0;
  305. mod->arch.init.plt_max_entries = init_plts;
  306. if (tramp) {
  307. tramp->sh_type = SHT_NOBITS;
  308. tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  309. tramp->sh_addralign = __alignof__(struct plt_entry);
  310. tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry);
  311. }
  312. if (init_tramp) {
  313. init_tramp->sh_type = SHT_NOBITS;
  314. init_tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  315. init_tramp->sh_addralign = __alignof__(struct plt_entry);
  316. init_tramp->sh_size = NR_FTRACE_PLTS * sizeof(struct plt_entry);
  317. }
  318. return 0;
  319. }