module.c 9.9 KB

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