module-plts.c 7.5 KB

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