module-plts.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2014 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. u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val)
  29. {
  30. struct plt_entries *plt = (struct plt_entries *)mod->arch.plt->sh_addr;
  31. int idx = 0;
  32. /*
  33. * Look for an existing entry pointing to 'val'. Given that the
  34. * relocations are sorted, this will be the last entry we allocated.
  35. * (if one exists).
  36. */
  37. if (mod->arch.plt_count > 0) {
  38. plt += (mod->arch.plt_count - 1) / PLT_ENT_COUNT;
  39. idx = (mod->arch.plt_count - 1) % PLT_ENT_COUNT;
  40. if (plt->lit[idx] == val)
  41. return (u32)&plt->ldr[idx];
  42. idx = (idx + 1) % PLT_ENT_COUNT;
  43. if (!idx)
  44. plt++;
  45. }
  46. mod->arch.plt_count++;
  47. BUG_ON(mod->arch.plt_count * PLT_ENT_SIZE > mod->arch.plt->sh_size);
  48. if (!idx)
  49. /* Populate a new set of entries */
  50. *plt = (struct plt_entries){
  51. { [0 ... PLT_ENT_COUNT - 1] = PLT_ENT_LDR, },
  52. { val, }
  53. };
  54. else
  55. plt->lit[idx] = val;
  56. return (u32)&plt->ldr[idx];
  57. }
  58. #define cmp_3way(a,b) ((a) < (b) ? -1 : (a) > (b))
  59. static int cmp_rel(const void *a, const void *b)
  60. {
  61. const Elf32_Rel *x = a, *y = b;
  62. int i;
  63. /* sort by type and symbol index */
  64. i = cmp_3way(ELF32_R_TYPE(x->r_info), ELF32_R_TYPE(y->r_info));
  65. if (i == 0)
  66. i = cmp_3way(ELF32_R_SYM(x->r_info), ELF32_R_SYM(y->r_info));
  67. return i;
  68. }
  69. static bool is_zero_addend_relocation(Elf32_Addr base, const Elf32_Rel *rel)
  70. {
  71. u32 *tval = (u32 *)(base + rel->r_offset);
  72. /*
  73. * Do a bitwise compare on the raw addend rather than fully decoding
  74. * the offset and doing an arithmetic comparison.
  75. * Note that a zero-addend jump/call relocation is encoded taking the
  76. * PC bias into account, i.e., -8 for ARM and -4 for Thumb2.
  77. */
  78. switch (ELF32_R_TYPE(rel->r_info)) {
  79. u16 upper, lower;
  80. case R_ARM_THM_CALL:
  81. case R_ARM_THM_JUMP24:
  82. upper = __mem_to_opcode_thumb16(((u16 *)tval)[0]);
  83. lower = __mem_to_opcode_thumb16(((u16 *)tval)[1]);
  84. return (upper & 0x7ff) == 0x7ff && (lower & 0x2fff) == 0x2ffe;
  85. case R_ARM_CALL:
  86. case R_ARM_PC24:
  87. case R_ARM_JUMP24:
  88. return (__mem_to_opcode_arm(*tval) & 0xffffff) == 0xfffffe;
  89. }
  90. BUG();
  91. }
  92. static bool duplicate_rel(Elf32_Addr base, const Elf32_Rel *rel, int num)
  93. {
  94. const Elf32_Rel *prev;
  95. /*
  96. * Entries are sorted by type and symbol index. That means that,
  97. * if a duplicate entry exists, it must be in the preceding
  98. * slot.
  99. */
  100. if (!num)
  101. return false;
  102. prev = rel + num - 1;
  103. return cmp_rel(rel + num, prev) == 0 &&
  104. is_zero_addend_relocation(base, prev);
  105. }
  106. /* Count how many PLT entries we may need */
  107. static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base,
  108. const Elf32_Rel *rel, int num)
  109. {
  110. unsigned int ret = 0;
  111. const Elf32_Sym *s;
  112. int i;
  113. for (i = 0; i < num; i++) {
  114. switch (ELF32_R_TYPE(rel[i].r_info)) {
  115. case R_ARM_CALL:
  116. case R_ARM_PC24:
  117. case R_ARM_JUMP24:
  118. case R_ARM_THM_CALL:
  119. case R_ARM_THM_JUMP24:
  120. /*
  121. * We only have to consider branch targets that resolve
  122. * to undefined symbols. This is not simply a heuristic,
  123. * it is a fundamental limitation, since the PLT itself
  124. * is part of the module, and needs to be within range
  125. * as well, so modules can never grow beyond that limit.
  126. */
  127. s = syms + ELF32_R_SYM(rel[i].r_info);
  128. if (s->st_shndx != SHN_UNDEF)
  129. break;
  130. /*
  131. * Jump relocations with non-zero addends against
  132. * undefined symbols are supported by the ELF spec, but
  133. * do not occur in practice (e.g., 'jump n bytes past
  134. * the entry point of undefined function symbol f').
  135. * So we need to support them, but there is no need to
  136. * take them into consideration when trying to optimize
  137. * this code. So let's only check for duplicates when
  138. * the addend is zero.
  139. */
  140. if (!is_zero_addend_relocation(base, rel + i) ||
  141. !duplicate_rel(base, rel, i))
  142. ret++;
  143. }
  144. }
  145. return ret;
  146. }
  147. int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
  148. char *secstrings, struct module *mod)
  149. {
  150. unsigned long plts = 0;
  151. Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
  152. Elf32_Sym *syms = NULL;
  153. /*
  154. * To store the PLTs, we expand the .text section for core module code
  155. * and for initialization code.
  156. */
  157. for (s = sechdrs; s < sechdrs_end; ++s) {
  158. if (strcmp(".plt", secstrings + s->sh_name) == 0)
  159. mod->arch.plt = s;
  160. else if (s->sh_type == SHT_SYMTAB)
  161. syms = (Elf32_Sym *)s->sh_addr;
  162. }
  163. if (!mod->arch.plt) {
  164. pr_err("%s: module PLT section missing\n", mod->name);
  165. return -ENOEXEC;
  166. }
  167. if (!syms) {
  168. pr_err("%s: module symtab section missing\n", mod->name);
  169. return -ENOEXEC;
  170. }
  171. for (s = sechdrs + 1; s < sechdrs_end; ++s) {
  172. Elf32_Rel *rels = (void *)ehdr + s->sh_offset;
  173. int numrels = s->sh_size / sizeof(Elf32_Rel);
  174. Elf32_Shdr *dstsec = sechdrs + s->sh_info;
  175. if (s->sh_type != SHT_REL)
  176. continue;
  177. /* ignore relocations that operate on non-exec sections */
  178. if (!(dstsec->sh_flags & SHF_EXECINSTR))
  179. continue;
  180. /* sort by type and symbol index */
  181. sort(rels, numrels, sizeof(Elf32_Rel), cmp_rel, NULL);
  182. plts += count_plts(syms, dstsec->sh_addr, rels, numrels);
  183. }
  184. mod->arch.plt->sh_type = SHT_NOBITS;
  185. mod->arch.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
  186. mod->arch.plt->sh_addralign = L1_CACHE_BYTES;
  187. mod->arch.plt->sh_size = round_up(plts * PLT_ENT_SIZE,
  188. sizeof(struct plt_entries));
  189. mod->arch.plt_count = 0;
  190. pr_debug("%s: plt=%x\n", __func__, mod->arch.plt->sh_size);
  191. return 0;
  192. }