module.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  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. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. *
  16. * Copyright (C) 2001 Rusty Russell.
  17. * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
  18. * Copyright (C) 2005 Thiemo Seufer
  19. */
  20. #undef DEBUG
  21. #include <linux/moduleloader.h>
  22. #include <linux/elf.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/slab.h>
  25. #include <linux/fs.h>
  26. #include <linux/string.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/spinlock.h>
  30. #include <asm/pgtable.h> /* MODULE_START */
  31. struct mips_hi16 {
  32. struct mips_hi16 *next;
  33. Elf_Addr *addr;
  34. Elf_Addr value;
  35. };
  36. static struct mips_hi16 *mips_hi16_list;
  37. static LIST_HEAD(dbe_list);
  38. static DEFINE_SPINLOCK(dbe_lock);
  39. void *module_alloc(unsigned long size)
  40. {
  41. #ifdef MODULE_START
  42. struct vm_struct *area;
  43. size = PAGE_ALIGN(size);
  44. if (!size)
  45. return NULL;
  46. area = __get_vm_area(size, VM_ALLOC, MODULE_START, MODULE_END);
  47. if (!area)
  48. return NULL;
  49. return __vmalloc_area(area, GFP_KERNEL, PAGE_KERNEL);
  50. #else
  51. if (size == 0)
  52. return NULL;
  53. return vmalloc(size);
  54. #endif
  55. }
  56. /* Free memory returned from module_alloc */
  57. void module_free(struct module *mod, void *module_region)
  58. {
  59. vfree(module_region);
  60. /* FIXME: If module_region == mod->init_region, trim exception
  61. table entries. */
  62. }
  63. int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
  64. char *secstrings, struct module *mod)
  65. {
  66. return 0;
  67. }
  68. static int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
  69. {
  70. return 0;
  71. }
  72. static int apply_r_mips_32_rel(struct module *me, u32 *location, Elf_Addr v)
  73. {
  74. *location += v;
  75. return 0;
  76. }
  77. static int apply_r_mips_32_rela(struct module *me, u32 *location, Elf_Addr v)
  78. {
  79. *location = v;
  80. return 0;
  81. }
  82. static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
  83. {
  84. if (v % 4) {
  85. printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
  86. return -ENOEXEC;
  87. }
  88. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  89. printk(KERN_ERR
  90. "module %s: relocation overflow\n",
  91. me->name);
  92. return -ENOEXEC;
  93. }
  94. *location = (*location & ~0x03ffffff) |
  95. ((*location + (v >> 2)) & 0x03ffffff);
  96. return 0;
  97. }
  98. static int apply_r_mips_26_rela(struct module *me, u32 *location, Elf_Addr v)
  99. {
  100. if (v % 4) {
  101. printk(KERN_ERR "module %s: dangerous relocation\n", me->name);
  102. return -ENOEXEC;
  103. }
  104. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  105. printk(KERN_ERR
  106. "module %s: relocation overflow\n",
  107. me->name);
  108. return -ENOEXEC;
  109. }
  110. *location = (*location & ~0x03ffffff) | ((v >> 2) & 0x03ffffff);
  111. return 0;
  112. }
  113. static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
  114. {
  115. struct mips_hi16 *n;
  116. /*
  117. * We cannot relocate this one now because we don't know the value of
  118. * the carry we need to add. Save the information, and let LO16 do the
  119. * actual relocation.
  120. */
  121. n = kmalloc(sizeof *n, GFP_KERNEL);
  122. if (!n)
  123. return -ENOMEM;
  124. n->addr = (Elf_Addr *)location;
  125. n->value = v;
  126. n->next = mips_hi16_list;
  127. mips_hi16_list = n;
  128. return 0;
  129. }
  130. static int apply_r_mips_hi16_rela(struct module *me, u32 *location, Elf_Addr v)
  131. {
  132. *location = (*location & 0xffff0000) |
  133. ((((long long) v + 0x8000LL) >> 16) & 0xffff);
  134. return 0;
  135. }
  136. static int apply_r_mips_lo16_rel(struct module *me, u32 *location, Elf_Addr v)
  137. {
  138. unsigned long insnlo = *location;
  139. Elf_Addr val, vallo;
  140. /* Sign extend the addend we extract from the lo insn. */
  141. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  142. if (mips_hi16_list != NULL) {
  143. struct mips_hi16 *l;
  144. l = mips_hi16_list;
  145. while (l != NULL) {
  146. struct mips_hi16 *next;
  147. unsigned long insn;
  148. /*
  149. * The value for the HI16 had best be the same.
  150. */
  151. if (v != l->value)
  152. goto out_danger;
  153. /*
  154. * Do the HI16 relocation. Note that we actually don't
  155. * need to know anything about the LO16 itself, except
  156. * where to find the low 16 bits of the addend needed
  157. * by the LO16.
  158. */
  159. insn = *l->addr;
  160. val = ((insn & 0xffff) << 16) + vallo;
  161. val += v;
  162. /*
  163. * Account for the sign extension that will happen in
  164. * the low bits.
  165. */
  166. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  167. insn = (insn & ~0xffff) | val;
  168. *l->addr = insn;
  169. next = l->next;
  170. kfree(l);
  171. l = next;
  172. }
  173. mips_hi16_list = NULL;
  174. }
  175. /*
  176. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  177. */
  178. val = v + vallo;
  179. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  180. *location = insnlo;
  181. return 0;
  182. out_danger:
  183. printk(KERN_ERR "module %s: dangerous " "relocation\n", me->name);
  184. return -ENOEXEC;
  185. }
  186. static int apply_r_mips_lo16_rela(struct module *me, u32 *location, Elf_Addr v)
  187. {
  188. *location = (*location & 0xffff0000) | (v & 0xffff);
  189. return 0;
  190. }
  191. static int apply_r_mips_64_rela(struct module *me, u32 *location, Elf_Addr v)
  192. {
  193. *(Elf_Addr *)location = v;
  194. return 0;
  195. }
  196. static int apply_r_mips_higher_rela(struct module *me, u32 *location,
  197. Elf_Addr v)
  198. {
  199. *location = (*location & 0xffff0000) |
  200. ((((long long) v + 0x80008000LL) >> 32) & 0xffff);
  201. return 0;
  202. }
  203. static int apply_r_mips_highest_rela(struct module *me, u32 *location,
  204. Elf_Addr v)
  205. {
  206. *location = (*location & 0xffff0000) |
  207. ((((long long) v + 0x800080008000LL) >> 48) & 0xffff);
  208. return 0;
  209. }
  210. static int (*reloc_handlers_rel[]) (struct module *me, u32 *location,
  211. Elf_Addr v) = {
  212. [R_MIPS_NONE] = apply_r_mips_none,
  213. [R_MIPS_32] = apply_r_mips_32_rel,
  214. [R_MIPS_26] = apply_r_mips_26_rel,
  215. [R_MIPS_HI16] = apply_r_mips_hi16_rel,
  216. [R_MIPS_LO16] = apply_r_mips_lo16_rel
  217. };
  218. static int (*reloc_handlers_rela[]) (struct module *me, u32 *location,
  219. Elf_Addr v) = {
  220. [R_MIPS_NONE] = apply_r_mips_none,
  221. [R_MIPS_32] = apply_r_mips_32_rela,
  222. [R_MIPS_26] = apply_r_mips_26_rela,
  223. [R_MIPS_HI16] = apply_r_mips_hi16_rela,
  224. [R_MIPS_LO16] = apply_r_mips_lo16_rela,
  225. [R_MIPS_64] = apply_r_mips_64_rela,
  226. [R_MIPS_HIGHER] = apply_r_mips_higher_rela,
  227. [R_MIPS_HIGHEST] = apply_r_mips_highest_rela
  228. };
  229. int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
  230. unsigned int symindex, unsigned int relsec,
  231. struct module *me)
  232. {
  233. Elf_Mips_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  234. Elf_Sym *sym;
  235. u32 *location;
  236. unsigned int i;
  237. Elf_Addr v;
  238. int res;
  239. pr_debug("Applying relocate section %u to %u\n", relsec,
  240. sechdrs[relsec].sh_info);
  241. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  242. /* This is where to make the change */
  243. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  244. + rel[i].r_offset;
  245. /* This is the symbol it is referring to */
  246. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  247. + ELF_MIPS_R_SYM(rel[i]);
  248. if (!sym->st_value) {
  249. /* Ignore unresolved weak symbol */
  250. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  251. continue;
  252. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  253. me->name, strtab + sym->st_name);
  254. return -ENOENT;
  255. }
  256. v = sym->st_value;
  257. res = reloc_handlers_rel[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
  258. if (res)
  259. return res;
  260. }
  261. return 0;
  262. }
  263. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  264. unsigned int symindex, unsigned int relsec,
  265. struct module *me)
  266. {
  267. Elf_Mips_Rela *rel = (void *) sechdrs[relsec].sh_addr;
  268. Elf_Sym *sym;
  269. u32 *location;
  270. unsigned int i;
  271. Elf_Addr v;
  272. int res;
  273. pr_debug("Applying relocate section %u to %u\n", relsec,
  274. sechdrs[relsec].sh_info);
  275. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  276. /* This is where to make the change */
  277. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  278. + rel[i].r_offset;
  279. /* This is the symbol it is referring to */
  280. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  281. + ELF_MIPS_R_SYM(rel[i]);
  282. if (!sym->st_value) {
  283. /* Ignore unresolved weak symbol */
  284. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  285. continue;
  286. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  287. me->name, strtab + sym->st_name);
  288. return -ENOENT;
  289. }
  290. v = sym->st_value + rel[i].r_addend;
  291. res = reloc_handlers_rela[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
  292. if (res)
  293. return res;
  294. }
  295. return 0;
  296. }
  297. /* Given an address, look for it in the module exception tables. */
  298. const struct exception_table_entry *search_module_dbetables(unsigned long addr)
  299. {
  300. unsigned long flags;
  301. const struct exception_table_entry *e = NULL;
  302. struct mod_arch_specific *dbe;
  303. spin_lock_irqsave(&dbe_lock, flags);
  304. list_for_each_entry(dbe, &dbe_list, dbe_list) {
  305. e = search_extable(dbe->dbe_start, dbe->dbe_end - 1, addr);
  306. if (e)
  307. break;
  308. }
  309. spin_unlock_irqrestore(&dbe_lock, flags);
  310. /* Now, if we found one, we are running inside it now, hence
  311. we cannot unload the module, hence no refcnt needed. */
  312. return e;
  313. }
  314. /* Put in dbe list if necessary. */
  315. int module_finalize(const Elf_Ehdr *hdr,
  316. const Elf_Shdr *sechdrs,
  317. struct module *me)
  318. {
  319. const Elf_Shdr *s;
  320. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  321. INIT_LIST_HEAD(&me->arch.dbe_list);
  322. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  323. if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
  324. continue;
  325. me->arch.dbe_start = (void *)s->sh_addr;
  326. me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
  327. spin_lock_irq(&dbe_lock);
  328. list_add(&me->arch.dbe_list, &dbe_list);
  329. spin_unlock_irq(&dbe_lock);
  330. }
  331. return 0;
  332. }
  333. void module_arch_cleanup(struct module *mod)
  334. {
  335. spin_lock_irq(&dbe_lock);
  336. list_del(&mod->arch.dbe_list);
  337. spin_unlock_irq(&dbe_lock);
  338. }