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