module_32.c 9.4 KB

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