module_32.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Kernel module help for PPC.
  2. Copyright (C) 2001 Rusty Russell.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/moduleloader.h>
  18. #include <linux/elf.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/fs.h>
  21. #include <linux/string.h>
  22. #include <linux/kernel.h>
  23. #include <linux/ftrace.h>
  24. #include <linux/cache.h>
  25. #include <linux/bug.h>
  26. #include <linux/sort.h>
  27. #include <asm/setup.h>
  28. /* Count how many different relocations (different symbol, different
  29. addend) */
  30. static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
  31. {
  32. unsigned int i, r_info, r_addend, _count_relocs;
  33. _count_relocs = 0;
  34. r_info = 0;
  35. r_addend = 0;
  36. for (i = 0; i < num; i++)
  37. /* Only count 24-bit relocs, others don't need stubs */
  38. if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
  39. (r_info != ELF32_R_SYM(rela[i].r_info) ||
  40. r_addend != rela[i].r_addend)) {
  41. _count_relocs++;
  42. r_info = ELF32_R_SYM(rela[i].r_info);
  43. r_addend = rela[i].r_addend;
  44. }
  45. #ifdef CONFIG_DYNAMIC_FTRACE
  46. _count_relocs++; /* add one for ftrace_caller */
  47. #endif
  48. return _count_relocs;
  49. }
  50. static int relacmp(const void *_x, const void *_y)
  51. {
  52. const Elf32_Rela *x, *y;
  53. y = (Elf32_Rela *)_x;
  54. x = (Elf32_Rela *)_y;
  55. /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
  56. * make the comparison cheaper/faster. It won't affect the sorting or
  57. * the counting algorithms' performance
  58. */
  59. if (x->r_info < y->r_info)
  60. return -1;
  61. else if (x->r_info > y->r_info)
  62. return 1;
  63. else if (x->r_addend < y->r_addend)
  64. return -1;
  65. else if (x->r_addend > y->r_addend)
  66. return 1;
  67. else
  68. return 0;
  69. }
  70. static void relaswap(void *_x, void *_y, int size)
  71. {
  72. uint32_t *x, *y, tmp;
  73. int i;
  74. y = (uint32_t *)_x;
  75. x = (uint32_t *)_y;
  76. for (i = 0; i < sizeof(Elf32_Rela) / sizeof(uint32_t); i++) {
  77. tmp = x[i];
  78. x[i] = y[i];
  79. y[i] = tmp;
  80. }
  81. }
  82. /* Get the potential trampolines size required of the init and
  83. non-init sections */
  84. static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
  85. const Elf32_Shdr *sechdrs,
  86. const char *secstrings,
  87. int is_init)
  88. {
  89. unsigned long ret = 0;
  90. unsigned i;
  91. /* Everything marked ALLOC (this includes the exported
  92. symbols) */
  93. for (i = 1; i < hdr->e_shnum; i++) {
  94. /* If it's called *.init*, and we're not init, we're
  95. not interested */
  96. if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
  97. != is_init)
  98. continue;
  99. /* We don't want to look at debug sections. */
  100. if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
  101. continue;
  102. if (sechdrs[i].sh_type == SHT_RELA) {
  103. pr_debug("Found relocations in section %u\n", i);
  104. pr_debug("Ptr: %p. Number: %u\n",
  105. (void *)hdr + sechdrs[i].sh_offset,
  106. sechdrs[i].sh_size / sizeof(Elf32_Rela));
  107. /* Sort the relocation information based on a symbol and
  108. * addend key. This is a stable O(n*log n) complexity
  109. * alogrithm but it will reduce the complexity of
  110. * count_relocs() to linear complexity O(n)
  111. */
  112. sort((void *)hdr + sechdrs[i].sh_offset,
  113. sechdrs[i].sh_size / sizeof(Elf32_Rela),
  114. sizeof(Elf32_Rela), relacmp, relaswap);
  115. ret += count_relocs((void *)hdr
  116. + sechdrs[i].sh_offset,
  117. sechdrs[i].sh_size
  118. / sizeof(Elf32_Rela))
  119. * sizeof(struct ppc_plt_entry);
  120. }
  121. }
  122. return ret;
  123. }
  124. int module_frob_arch_sections(Elf32_Ehdr *hdr,
  125. Elf32_Shdr *sechdrs,
  126. char *secstrings,
  127. struct module *me)
  128. {
  129. unsigned int i;
  130. /* Find .plt and .init.plt sections */
  131. for (i = 0; i < hdr->e_shnum; i++) {
  132. if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
  133. me->arch.init_plt_section = i;
  134. else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
  135. me->arch.core_plt_section = i;
  136. }
  137. if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
  138. pr_err("Module doesn't contain .plt or .init.plt sections.\n");
  139. return -ENOEXEC;
  140. }
  141. /* Override their sizes */
  142. sechdrs[me->arch.core_plt_section].sh_size
  143. = get_plt_size(hdr, sechdrs, secstrings, 0);
  144. sechdrs[me->arch.init_plt_section].sh_size
  145. = get_plt_size(hdr, sechdrs, secstrings, 1);
  146. return 0;
  147. }
  148. static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
  149. {
  150. if (entry->jump[0] == 0x3d800000 + ((val + 0x8000) >> 16)
  151. && entry->jump[1] == 0x398c0000 + (val & 0xffff))
  152. return 1;
  153. return 0;
  154. }
  155. /* Set up a trampoline in the PLT to bounce us to the distant function */
  156. static uint32_t do_plt_call(void *location,
  157. Elf32_Addr val,
  158. Elf32_Shdr *sechdrs,
  159. struct module *mod)
  160. {
  161. struct ppc_plt_entry *entry;
  162. pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
  163. /* Init, or core PLT? */
  164. if (location >= mod->module_core
  165. && location < mod->module_core + mod->core_size)
  166. entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
  167. else
  168. entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
  169. /* Find this entry, or if that fails, the next avail. entry */
  170. while (entry->jump[0]) {
  171. if (entry_matches(entry, val)) return (uint32_t)entry;
  172. entry++;
  173. }
  174. entry->jump[0] = 0x3d800000+((val+0x8000)>>16); /* lis r12,sym@ha */
  175. entry->jump[1] = 0x398c0000 + (val&0xffff); /* addi r12,r12,sym@l*/
  176. entry->jump[2] = 0x7d8903a6; /* mtctr r12 */
  177. entry->jump[3] = 0x4e800420; /* bctr */
  178. pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
  179. return (uint32_t)entry;
  180. }
  181. int apply_relocate_add(Elf32_Shdr *sechdrs,
  182. const char *strtab,
  183. unsigned int symindex,
  184. unsigned int relsec,
  185. struct module *module)
  186. {
  187. unsigned int i;
  188. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  189. Elf32_Sym *sym;
  190. uint32_t *location;
  191. uint32_t value;
  192. pr_debug("Applying ADD relocate section %u to %u\n", relsec,
  193. sechdrs[relsec].sh_info);
  194. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  195. /* This is where to make the change */
  196. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  197. + rela[i].r_offset;
  198. /* This is the symbol it is referring to. Note that all
  199. undefined symbols have been resolved. */
  200. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  201. + ELF32_R_SYM(rela[i].r_info);
  202. /* `Everything is relative'. */
  203. value = sym->st_value + rela[i].r_addend;
  204. switch (ELF32_R_TYPE(rela[i].r_info)) {
  205. case R_PPC_ADDR32:
  206. /* Simply set it */
  207. *(uint32_t *)location = value;
  208. break;
  209. case R_PPC_ADDR16_LO:
  210. /* Low half of the symbol */
  211. *(uint16_t *)location = value;
  212. break;
  213. case R_PPC_ADDR16_HI:
  214. /* Higher half of the symbol */
  215. *(uint16_t *)location = (value >> 16);
  216. break;
  217. case R_PPC_ADDR16_HA:
  218. /* Sign-adjusted lower 16 bits: PPC ELF ABI says:
  219. (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
  220. This is the same, only sane.
  221. */
  222. *(uint16_t *)location = (value + 0x8000) >> 16;
  223. break;
  224. case R_PPC_REL24:
  225. if ((int)(value - (uint32_t)location) < -0x02000000
  226. || (int)(value - (uint32_t)location) >= 0x02000000)
  227. value = do_plt_call(location, value,
  228. sechdrs, module);
  229. /* Only replace bits 2 through 26 */
  230. pr_debug("REL24 value = %08X. location = %08X\n",
  231. value, (uint32_t)location);
  232. pr_debug("Location before: %08X.\n",
  233. *(uint32_t *)location);
  234. *(uint32_t *)location
  235. = (*(uint32_t *)location & ~0x03fffffc)
  236. | ((value - (uint32_t)location)
  237. & 0x03fffffc);
  238. pr_debug("Location after: %08X.\n",
  239. *(uint32_t *)location);
  240. pr_debug("ie. jump to %08X+%08X = %08X\n",
  241. *(uint32_t *)location & 0x03fffffc,
  242. (uint32_t)location,
  243. (*(uint32_t *)location & 0x03fffffc)
  244. + (uint32_t)location);
  245. break;
  246. case R_PPC_REL32:
  247. /* 32-bit relative jump. */
  248. *(uint32_t *)location = value - (uint32_t)location;
  249. break;
  250. default:
  251. pr_err("%s: unknown ADD relocation: %u\n",
  252. module->name,
  253. ELF32_R_TYPE(rela[i].r_info));
  254. return -ENOEXEC;
  255. }
  256. }
  257. #ifdef CONFIG_DYNAMIC_FTRACE
  258. module->arch.tramp =
  259. do_plt_call(module->module_core,
  260. (unsigned long)ftrace_caller,
  261. sechdrs, module);
  262. #endif
  263. return 0;
  264. }