module_32.c 10 KB

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