module.c 9.6 KB

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