module.c 9.8 KB

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