module.c 9.9 KB

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