module.c 9.4 KB

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