module.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/cache.h>
  23. #if 0
  24. #define DEBUGP printk
  25. #else
  26. #define DEBUGP(fmt , ...)
  27. #endif
  28. LIST_HEAD(module_bug_list);
  29. void *module_alloc(unsigned long size)
  30. {
  31. if (size == 0)
  32. return NULL;
  33. return vmalloc(size);
  34. }
  35. /* Free memory returned from module_alloc */
  36. void module_free(struct module *mod, void *module_region)
  37. {
  38. vfree(module_region);
  39. /* FIXME: If module_region == mod->init_region, trim exception
  40. table entries. */
  41. }
  42. /* Count how many different relocations (different symbol, different
  43. addend) */
  44. static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
  45. {
  46. unsigned int i, j, ret = 0;
  47. /* Sure, this is order(n^2), but it's usually short, and not
  48. time critical */
  49. for (i = 0; i < num; i++) {
  50. for (j = 0; j < i; j++) {
  51. /* If this addend appeared before, it's
  52. already been counted */
  53. if (ELF32_R_SYM(rela[i].r_info)
  54. == ELF32_R_SYM(rela[j].r_info)
  55. && rela[i].r_addend == rela[j].r_addend)
  56. break;
  57. }
  58. if (j == i) ret++;
  59. }
  60. return ret;
  61. }
  62. /* Get the potential trampolines size required of the init and
  63. non-init sections */
  64. static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
  65. const Elf32_Shdr *sechdrs,
  66. const char *secstrings,
  67. int is_init)
  68. {
  69. unsigned long ret = 0;
  70. unsigned i;
  71. /* Everything marked ALLOC (this includes the exported
  72. symbols) */
  73. for (i = 1; i < hdr->e_shnum; i++) {
  74. /* If it's called *.init*, and we're not init, we're
  75. not interested */
  76. if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != 0)
  77. != is_init)
  78. continue;
  79. /* We don't want to look at debug sections. */
  80. if (strstr(secstrings + sechdrs[i].sh_name, ".debug") != 0)
  81. continue;
  82. if (sechdrs[i].sh_type == SHT_RELA) {
  83. DEBUGP("Found relocations in section %u\n", i);
  84. DEBUGP("Ptr: %p. Number: %u\n",
  85. (void *)hdr + sechdrs[i].sh_offset,
  86. sechdrs[i].sh_size / sizeof(Elf32_Rela));
  87. ret += count_relocs((void *)hdr
  88. + sechdrs[i].sh_offset,
  89. sechdrs[i].sh_size
  90. / sizeof(Elf32_Rela))
  91. * sizeof(struct ppc_plt_entry);
  92. }
  93. }
  94. return ret;
  95. }
  96. int module_frob_arch_sections(Elf32_Ehdr *hdr,
  97. Elf32_Shdr *sechdrs,
  98. char *secstrings,
  99. struct module *me)
  100. {
  101. unsigned int i;
  102. /* Find .plt and .init.plt sections */
  103. for (i = 0; i < hdr->e_shnum; i++) {
  104. if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
  105. me->arch.init_plt_section = i;
  106. else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
  107. me->arch.core_plt_section = i;
  108. }
  109. if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
  110. printk("Module doesn't contain .plt or .init.plt sections.\n");
  111. return -ENOEXEC;
  112. }
  113. /* Override their sizes */
  114. sechdrs[me->arch.core_plt_section].sh_size
  115. = get_plt_size(hdr, sechdrs, secstrings, 0);
  116. sechdrs[me->arch.init_plt_section].sh_size
  117. = get_plt_size(hdr, sechdrs, secstrings, 1);
  118. return 0;
  119. }
  120. int apply_relocate(Elf32_Shdr *sechdrs,
  121. const char *strtab,
  122. unsigned int symindex,
  123. unsigned int relsec,
  124. struct module *module)
  125. {
  126. printk(KERN_ERR "%s: Non-ADD RELOCATION unsupported\n",
  127. module->name);
  128. return -ENOEXEC;
  129. }
  130. static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
  131. {
  132. if (entry->jump[0] == 0x3d600000 + ((val + 0x8000) >> 16)
  133. && entry->jump[1] == 0x396b0000 + (val & 0xffff))
  134. return 1;
  135. return 0;
  136. }
  137. /* Set up a trampoline in the PLT to bounce us to the distant function */
  138. static uint32_t do_plt_call(void *location,
  139. Elf32_Addr val,
  140. Elf32_Shdr *sechdrs,
  141. struct module *mod)
  142. {
  143. struct ppc_plt_entry *entry;
  144. DEBUGP("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
  145. /* Init, or core PLT? */
  146. if (location >= mod->module_core
  147. && location < mod->module_core + mod->core_size)
  148. entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
  149. else
  150. entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
  151. /* Find this entry, or if that fails, the next avail. entry */
  152. while (entry->jump[0]) {
  153. if (entry_matches(entry, val)) return (uint32_t)entry;
  154. entry++;
  155. }
  156. /* Stolen from Paul Mackerras as well... */
  157. entry->jump[0] = 0x3d600000+((val+0x8000)>>16); /* lis r11,sym@ha */
  158. entry->jump[1] = 0x396b0000 + (val&0xffff); /* addi r11,r11,sym@l*/
  159. entry->jump[2] = 0x7d6903a6; /* mtctr r11 */
  160. entry->jump[3] = 0x4e800420; /* bctr */
  161. DEBUGP("Initialized plt for 0x%x at %p\n", val, entry);
  162. return (uint32_t)entry;
  163. }
  164. int apply_relocate_add(Elf32_Shdr *sechdrs,
  165. const char *strtab,
  166. unsigned int symindex,
  167. unsigned int relsec,
  168. struct module *module)
  169. {
  170. unsigned int i;
  171. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  172. Elf32_Sym *sym;
  173. uint32_t *location;
  174. uint32_t value;
  175. DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
  176. sechdrs[relsec].sh_info);
  177. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
  178. /* This is where to make the change */
  179. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  180. + rela[i].r_offset;
  181. /* This is the symbol it is referring to. Note that all
  182. undefined symbols have been resolved. */
  183. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  184. + ELF32_R_SYM(rela[i].r_info);
  185. /* `Everything is relative'. */
  186. value = sym->st_value + rela[i].r_addend;
  187. switch (ELF32_R_TYPE(rela[i].r_info)) {
  188. case R_PPC_ADDR32:
  189. /* Simply set it */
  190. *(uint32_t *)location = value;
  191. break;
  192. case R_PPC_ADDR16_LO:
  193. /* Low half of the symbol */
  194. *(uint16_t *)location = value;
  195. break;
  196. case R_PPC_ADDR16_HA:
  197. /* Sign-adjusted lower 16 bits: PPC ELF ABI says:
  198. (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
  199. This is the same, only sane.
  200. */
  201. *(uint16_t *)location = (value + 0x8000) >> 16;
  202. break;
  203. case R_PPC_REL24:
  204. if ((int)(value - (uint32_t)location) < -0x02000000
  205. || (int)(value - (uint32_t)location) >= 0x02000000)
  206. value = do_plt_call(location, value,
  207. sechdrs, module);
  208. /* Only replace bits 2 through 26 */
  209. DEBUGP("REL24 value = %08X. location = %08X\n",
  210. value, (uint32_t)location);
  211. DEBUGP("Location before: %08X.\n",
  212. *(uint32_t *)location);
  213. *(uint32_t *)location
  214. = (*(uint32_t *)location & ~0x03fffffc)
  215. | ((value - (uint32_t)location)
  216. & 0x03fffffc);
  217. DEBUGP("Location after: %08X.\n",
  218. *(uint32_t *)location);
  219. DEBUGP("ie. jump to %08X+%08X = %08X\n",
  220. *(uint32_t *)location & 0x03fffffc,
  221. (uint32_t)location,
  222. (*(uint32_t *)location & 0x03fffffc)
  223. + (uint32_t)location);
  224. break;
  225. case R_PPC_REL32:
  226. /* 32-bit relative jump. */
  227. *(uint32_t *)location = value - (uint32_t)location;
  228. break;
  229. default:
  230. printk("%s: unknown ADD relocation: %u\n",
  231. module->name,
  232. ELF32_R_TYPE(rela[i].r_info));
  233. return -ENOEXEC;
  234. }
  235. }
  236. return 0;
  237. }
  238. int module_finalize(const Elf_Ehdr *hdr,
  239. const Elf_Shdr *sechdrs,
  240. struct module *me)
  241. {
  242. char *secstrings;
  243. unsigned int i;
  244. me->arch.bug_table = NULL;
  245. me->arch.num_bugs = 0;
  246. /* Find the __bug_table section, if present */
  247. secstrings = (char *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  248. for (i = 1; i < hdr->e_shnum; i++) {
  249. if (strcmp(secstrings+sechdrs[i].sh_name, "__bug_table"))
  250. continue;
  251. me->arch.bug_table = (void *) sechdrs[i].sh_addr;
  252. me->arch.num_bugs = sechdrs[i].sh_size / sizeof(struct bug_entry);
  253. break;
  254. }
  255. /*
  256. * Strictly speaking this should have a spinlock to protect against
  257. * traversals, but since we only traverse on BUG()s, a spinlock
  258. * could potentially lead to deadlock and thus be counter-productive.
  259. */
  260. list_add(&me->arch.bug_list, &module_bug_list);
  261. return 0;
  262. }
  263. void module_arch_cleanup(struct module *mod)
  264. {
  265. list_del(&mod->arch.bug_list);
  266. }
  267. struct bug_entry *module_find_bug(unsigned long bugaddr)
  268. {
  269. struct mod_arch_specific *mod;
  270. unsigned int i;
  271. struct bug_entry *bug;
  272. list_for_each_entry(mod, &module_bug_list, bug_list) {
  273. bug = mod->bug_table;
  274. for (i = 0; i < mod->num_bugs; ++i, ++bug)
  275. if (bugaddr == bug->bug_addr)
  276. return bug;
  277. }
  278. return NULL;
  279. }