module-plts.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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/sort.h>
  10. #include <linux/moduleloader.h>
  11. #include <asm/cache.h>
  12. #include <asm/opcodes.h>
  13. #ifdef CONFIG_THUMB2_KERNEL
  14. #define PLT_ENT_LDR __opcode_to_mem_thumb32(0xf8dff000 | \
  15. (PLT_ENT_STRIDE - 4))
  16. #else
  17. #define PLT_ENT_LDR __opcode_to_mem_arm(0xe59ff000 | \
  18. (PLT_ENT_STRIDE - 8))
  19. #endif
  20. static const u32 fixed_plts[] = {
  21. #ifdef CONFIG_DYNAMIC_FTRACE
  22. FTRACE_ADDR,
  23. MCOUNT_ADDR,
  24. #endif
  25. };
  26. static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt)
  27. {
  28. int i;
  29. if (!ARRAY_SIZE(fixed_plts) || pltsec->plt_count)
  30. return;
  31. pltsec->plt_count = ARRAY_SIZE(fixed_plts);
  32. for (i = 0; i < ARRAY_SIZE(plt->ldr); ++i)
  33. plt->ldr[i] = PLT_ENT_LDR;
  34. BUILD_BUG_ON(sizeof(fixed_plts) > sizeof(plt->lit));
  35. memcpy(plt->lit, fixed_plts, sizeof(fixed_plts));
  36. }
  37. u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val)
  38. {
  39. struct mod_plt_sec *pltsec = !within_module_init(loc, mod) ?
  40. &mod->arch.core : &mod->arch.init;
  41. struct plt_entries *plt;
  42. int idx;
  43. /* cache the address, ELF header is available only during module load */
  44. if (!pltsec->plt_ent)
  45. pltsec->plt_ent = (struct plt_entries *)pltsec->plt->sh_addr;
  46. plt = pltsec->plt_ent;
  47. prealloc_fixed(pltsec, plt);
  48. for (idx = 0; idx < ARRAY_SIZE(fixed_plts); ++idx)
  49. if (plt->lit[idx] == val)
  50. return (u32)&plt->ldr[idx];
  51. idx = 0;
  52. /*
  53. * Look for an existing entry pointing to 'val'. Given that the
  54. * relocations are sorted, this will be the last entry we allocated.
  55. * (if one exists).
  56. */
  57. if (pltsec->plt_count > 0) {
  58. plt += (pltsec->plt_count - 1) / PLT_ENT_COUNT;
  59. idx = (pltsec->plt_count - 1) % PLT_ENT_COUNT;
  60. if (plt->lit[idx] == val)
  61. return (u32)&plt->ldr[idx];
  62. idx = (idx + 1) % PLT_ENT_COUNT;
  63. if (!idx)
  64. plt++;
  65. }
  66. pltsec->plt_count++;
  67. BUG_ON(pltsec->plt_count * PLT_ENT_SIZE > pltsec->plt->sh_size);
  68. if (!idx)
  69. /* Populate a new set of entries */
  70. *plt = (struct plt_entries){
  71. { [0 ... PLT_ENT_COUNT - 1] = PLT_ENT_LDR, },
  72. { val, }
  73. };
  74. else
  75. plt->lit[idx] = val;
  76. return (u32)&plt->ldr[idx];
  77. }
  78. #define cmp_3way(a,b) ((a) < (b) ? -1 : (a) > (b))
  79. static int cmp_rel(const void *a, const void *b)
  80. {
  81. const Elf32_Rel *x = a, *y = b;
  82. int i;
  83. /* sort by type and symbol index */
  84. i = cmp_3way(ELF32_R_TYPE(x->r_info), ELF32_R_TYPE(y->r_info));
  85. if (i == 0)
  86. i = cmp_3way(ELF32_R_SYM(x->r_info), ELF32_R_SYM(y->r_info));
  87. return i;
  88. }
  89. static bool is_zero_addend_relocation(Elf32_Addr base, const Elf32_Rel *rel)
  90. {
  91. u32 *tval = (u32 *)(base + rel->r_offset);
  92. /*
  93. * Do a bitwise compare on the raw addend rather than fully decoding
  94. * the offset and doing an arithmetic comparison.
  95. * Note that a zero-addend jump/call relocation is encoded taking the
  96. * PC bias into account, i.e., -8 for ARM and -4 for Thumb2.
  97. */
  98. switch (ELF32_R_TYPE(rel->r_info)) {
  99. u16 upper, lower;
  100. case R_ARM_THM_CALL:
  101. case R_ARM_THM_JUMP24:
  102. upper = __mem_to_opcode_thumb16(((u16 *)tval)[0]);
  103. lower = __mem_to_opcode_thumb16(((u16 *)tval)[1]);
  104. return (upper & 0x7ff) == 0x7ff && (lower & 0x2fff) == 0x2ffe;
  105. case R_ARM_CALL:
  106. case R_ARM_PC24:
  107. case R_ARM_JUMP24:
  108. return (__mem_to_opcode_arm(*tval) & 0xffffff) == 0xfffffe;
  109. }
  110. BUG();
  111. }
  112. static bool duplicate_rel(Elf32_Addr base, const Elf32_Rel *rel, int num)
  113. {
  114. const Elf32_Rel *prev;
  115. /*
  116. * Entries are sorted by type and symbol index. That means that,
  117. * if a duplicate entry exists, it must be in the preceding
  118. * slot.
  119. */
  120. if (!num)
  121. return false;
  122. prev = rel + num - 1;
  123. return cmp_rel(rel + num, prev) == 0 &&
  124. is_zero_addend_relocation(base, prev);
  125. }
  126. /* Count how many PLT entries we may need */
  127. static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base,
  128. const Elf32_Rel *rel, int num, Elf32_Word dstidx)
  129. {
  130. unsigned int ret = 0;
  131. const Elf32_Sym *s;
  132. int i;
  133. for (i = 0; i < num; i++) {
  134. switch (ELF32_R_TYPE(rel[i].r_info)) {
  135. case R_ARM_CALL:
  136. case R_ARM_PC24:
  137. case R_ARM_JUMP24:
  138. case R_ARM_THM_CALL:
  139. case R_ARM_THM_JUMP24:
  140. /*
  141. * We only have to consider branch targets that resolve
  142. * to symbols that are defined in a different section.
  143. * This is not simply a heuristic, it is a fundamental
  144. * limitation, since there is no guaranteed way to emit
  145. * PLT entries sufficiently close to the branch if the
  146. * section size exceeds the range of a branch
  147. * instruction. So ignore relocations against defined
  148. * symbols if they live in the same section as the
  149. * relocation target.
  150. */
  151. s = syms + ELF32_R_SYM(rel[i].r_info);
  152. if (s->st_shndx == dstidx)
  153. break;
  154. /*
  155. * Jump relocations with non-zero addends against
  156. * undefined symbols are supported by the ELF spec, but
  157. * do not occur in practice (e.g., 'jump n bytes past
  158. * the entry point of undefined function symbol f').
  159. * So we need to support them, but there is no need to
  160. * take them into consideration when trying to optimize
  161. * this code. So let's only check for duplicates when
  162. * the addend is zero. (Note that calls into the core
  163. * module via init PLT entries could involve section
  164. * relative symbol references with non-zero addends, for
  165. * which we may end up emitting duplicates, but the init
  166. * PLT is released along with the rest of the .init
  167. * region as soon as module loading completes.)
  168. */
  169. if (!is_zero_addend_relocation(base, rel + i) ||
  170. !duplicate_rel(base, rel, i))
  171. ret++;
  172. }
  173. }
  174. return ret;
  175. }
  176. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  177. char *secstrings, struct module *mod)
  178. {
  179. unsigned long core_plts = ARRAY_SIZE(fixed_plts);
  180. unsigned long init_plts = ARRAY_SIZE(fixed_plts);
  181. Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
  182. Elf32_Sym *syms = NULL;
  183. /*
  184. * To store the PLTs, we expand the .text section for core module code
  185. * and for initialization code.
  186. */
  187. for (s = sechdrs; s < sechdrs_end; ++s) {
  188. if (strcmp(".plt", secstrings + s->sh_name) == 0)
  189. mod->arch.core.plt = s;
  190. else if (strcmp(".init.plt", secstrings + s->sh_name) == 0)
  191. mod->arch.init.plt = s;
  192. else if (s->sh_type == SHT_SYMTAB)
  193. syms = (Elf32_Sym *)s->sh_addr;
  194. }
  195. if (!mod->arch.core.plt || !mod->arch.init.plt) {
  196. pr_err("%s: module PLT section(s) missing\n", mod->name);
  197. return -ENOEXEC;
  198. }
  199. if (!syms) {
  200. pr_err("%s: module symtab section missing\n", mod->name);
  201. return -ENOEXEC;
  202. }
  203. for (s = sechdrs + 1; s < sechdrs_end; ++s) {
  204. Elf32_Rel *rels = (void *)ehdr + s->sh_offset;
  205. int numrels = s->sh_size / sizeof(Elf32_Rel);
  206. Elf32_Shdr *dstsec = sechdrs + s->sh_info;
  207. if (s->sh_type != SHT_REL)
  208. continue;
  209. /* ignore relocations that operate on non-exec sections */
  210. if (!(dstsec->sh_flags & SHF_EXECINSTR))
  211. continue;
  212. /* sort by type and symbol index */
  213. sort(rels, numrels, sizeof(Elf32_Rel), cmp_rel, NULL);
  214. if (!module_init_layout_section(secstrings + dstsec->sh_name))
  215. core_plts += count_plts(syms, dstsec->sh_addr, rels,
  216. numrels, s->sh_info);
  217. else
  218. init_plts += count_plts(syms, dstsec->sh_addr, rels,
  219. numrels, s->sh_info);
  220. }
  221. mod->arch.core.plt->sh_type = SHT_NOBITS;
  222. mod->arch.core.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  223. mod->arch.core.plt->sh_addralign = L1_CACHE_BYTES;
  224. mod->arch.core.plt->sh_size = round_up(core_plts * PLT_ENT_SIZE,
  225. sizeof(struct plt_entries));
  226. mod->arch.core.plt_count = 0;
  227. mod->arch.core.plt_ent = NULL;
  228. mod->arch.init.plt->sh_type = SHT_NOBITS;
  229. mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  230. mod->arch.init.plt->sh_addralign = L1_CACHE_BYTES;
  231. mod->arch.init.plt->sh_size = round_up(init_plts * PLT_ENT_SIZE,
  232. sizeof(struct plt_entries));
  233. mod->arch.init.plt_count = 0;
  234. mod->arch.init.plt_ent = NULL;
  235. pr_debug("%s: plt=%x, init.plt=%x\n", __func__,
  236. mod->arch.core.plt->sh_size, mod->arch.init.plt->sh_size);
  237. return 0;
  238. }
  239. bool in_module_plt(unsigned long loc)
  240. {
  241. struct module *mod;
  242. bool ret;
  243. preempt_disable();
  244. mod = __module_text_address(loc);
  245. ret = mod && (loc - (u32)mod->arch.core.plt_ent < mod->arch.core.plt_count * PLT_ENT_SIZE ||
  246. loc - (u32)mod->arch.init.plt_ent < mod->arch.init.plt_count * PLT_ENT_SIZE);
  247. preempt_enable();
  248. return ret;
  249. }