module_32.c 9.1 KB

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