module_32.c 8.6 KB

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