module.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 LIST_HEAD(dbe_list);
  38. static DEFINE_SPINLOCK(dbe_lock);
  39. #ifdef MODULE_START
  40. void *module_alloc(unsigned long size)
  41. {
  42. return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
  43. GFP_KERNEL, PAGE_KERNEL, -1,
  44. __builtin_return_address(0));
  45. }
  46. #endif
  47. int apply_r_mips_none(struct module *me, u32 *location, Elf_Addr v)
  48. {
  49. return 0;
  50. }
  51. static int apply_r_mips_32_rel(struct module *me, u32 *location, Elf_Addr v)
  52. {
  53. *location += v;
  54. return 0;
  55. }
  56. static int apply_r_mips_26_rel(struct module *me, u32 *location, Elf_Addr v)
  57. {
  58. if (v % 4) {
  59. pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
  60. me->name);
  61. return -ENOEXEC;
  62. }
  63. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  64. printk(KERN_ERR
  65. "module %s: relocation overflow\n",
  66. me->name);
  67. return -ENOEXEC;
  68. }
  69. *location = (*location & ~0x03ffffff) |
  70. ((*location + (v >> 2)) & 0x03ffffff);
  71. return 0;
  72. }
  73. static int apply_r_mips_hi16_rel(struct module *me, u32 *location, Elf_Addr v)
  74. {
  75. struct mips_hi16 *n;
  76. /*
  77. * We cannot relocate this one now because we don't know the value of
  78. * the carry we need to add. Save the information, and let LO16 do the
  79. * actual relocation.
  80. */
  81. n = kmalloc(sizeof *n, GFP_KERNEL);
  82. if (!n)
  83. return -ENOMEM;
  84. n->addr = (Elf_Addr *)location;
  85. n->value = v;
  86. n->next = me->arch.r_mips_hi16_list;
  87. me->arch.r_mips_hi16_list = n;
  88. return 0;
  89. }
  90. static void free_relocation_chain(struct mips_hi16 *l)
  91. {
  92. struct mips_hi16 *next;
  93. while (l) {
  94. next = l->next;
  95. kfree(l);
  96. l = next;
  97. }
  98. }
  99. static int apply_r_mips_lo16_rel(struct module *me, u32 *location, Elf_Addr v)
  100. {
  101. unsigned long insnlo = *location;
  102. struct mips_hi16 *l;
  103. Elf_Addr val, vallo;
  104. /* Sign extend the addend we extract from the lo insn. */
  105. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  106. if (me->arch.r_mips_hi16_list != NULL) {
  107. l = me->arch.r_mips_hi16_list;
  108. while (l != NULL) {
  109. struct mips_hi16 *next;
  110. unsigned long insn;
  111. /*
  112. * The value for the HI16 had best be the same.
  113. */
  114. if (v != l->value)
  115. goto out_danger;
  116. /*
  117. * Do the HI16 relocation. Note that we actually don't
  118. * need to know anything about the LO16 itself, except
  119. * where to find the low 16 bits of the addend needed
  120. * by the LO16.
  121. */
  122. insn = *l->addr;
  123. val = ((insn & 0xffff) << 16) + vallo;
  124. val += v;
  125. /*
  126. * Account for the sign extension that will happen in
  127. * the low bits.
  128. */
  129. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  130. insn = (insn & ~0xffff) | val;
  131. *l->addr = insn;
  132. next = l->next;
  133. kfree(l);
  134. l = next;
  135. }
  136. me->arch.r_mips_hi16_list = NULL;
  137. }
  138. /*
  139. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  140. */
  141. val = v + vallo;
  142. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  143. *location = insnlo;
  144. return 0;
  145. out_danger:
  146. free_relocation_chain(l);
  147. me->arch.r_mips_hi16_list = NULL;
  148. pr_err("module %s: dangerous R_MIPS_LO16 REL relocation\n", me->name);
  149. return -ENOEXEC;
  150. }
  151. static int (*reloc_handlers_rel[]) (struct module *me, u32 *location,
  152. Elf_Addr v) = {
  153. [R_MIPS_NONE] = apply_r_mips_none,
  154. [R_MIPS_32] = apply_r_mips_32_rel,
  155. [R_MIPS_26] = apply_r_mips_26_rel,
  156. [R_MIPS_HI16] = apply_r_mips_hi16_rel,
  157. [R_MIPS_LO16] = apply_r_mips_lo16_rel
  158. };
  159. int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
  160. unsigned int symindex, unsigned int relsec,
  161. struct module *me)
  162. {
  163. Elf_Mips_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  164. Elf_Sym *sym;
  165. u32 *location;
  166. unsigned int i;
  167. Elf_Addr v;
  168. int res;
  169. pr_debug("Applying relocate section %u to %u\n", relsec,
  170. sechdrs[relsec].sh_info);
  171. me->arch.r_mips_hi16_list = NULL;
  172. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  173. /* This is where to make the change */
  174. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  175. + rel[i].r_offset;
  176. /* This is the symbol it is referring to */
  177. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  178. + ELF_MIPS_R_SYM(rel[i]);
  179. if (IS_ERR_VALUE(sym->st_value)) {
  180. /* Ignore unresolved weak symbol */
  181. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  182. continue;
  183. printk(KERN_WARNING "%s: Unknown symbol %s\n",
  184. me->name, strtab + sym->st_name);
  185. return -ENOENT;
  186. }
  187. v = sym->st_value;
  188. res = reloc_handlers_rel[ELF_MIPS_R_TYPE(rel[i])](me, location, v);
  189. if (res)
  190. return res;
  191. }
  192. /*
  193. * Normally the hi16 list should be deallocated at this point. A
  194. * malformed binary however could contain a series of R_MIPS_HI16
  195. * relocations not followed by a R_MIPS_LO16 relocation. In that
  196. * case, free up the list and return an error.
  197. */
  198. if (me->arch.r_mips_hi16_list) {
  199. free_relocation_chain(me->arch.r_mips_hi16_list);
  200. me->arch.r_mips_hi16_list = NULL;
  201. return -ENOEXEC;
  202. }
  203. return 0;
  204. }
  205. /* Given an address, look for it in the module exception tables. */
  206. const struct exception_table_entry *search_module_dbetables(unsigned long addr)
  207. {
  208. unsigned long flags;
  209. const struct exception_table_entry *e = NULL;
  210. struct mod_arch_specific *dbe;
  211. spin_lock_irqsave(&dbe_lock, flags);
  212. list_for_each_entry(dbe, &dbe_list, dbe_list) {
  213. e = search_extable(dbe->dbe_start, dbe->dbe_end - 1, addr);
  214. if (e)
  215. break;
  216. }
  217. spin_unlock_irqrestore(&dbe_lock, flags);
  218. /* Now, if we found one, we are running inside it now, hence
  219. we cannot unload the module, hence no refcnt needed. */
  220. return e;
  221. }
  222. /* Put in dbe list if necessary. */
  223. int module_finalize(const Elf_Ehdr *hdr,
  224. const Elf_Shdr *sechdrs,
  225. struct module *me)
  226. {
  227. const Elf_Shdr *s;
  228. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  229. /* Make jump label nops. */
  230. jump_label_apply_nops(me);
  231. INIT_LIST_HEAD(&me->arch.dbe_list);
  232. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  233. if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
  234. continue;
  235. me->arch.dbe_start = (void *)s->sh_addr;
  236. me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
  237. spin_lock_irq(&dbe_lock);
  238. list_add(&me->arch.dbe_list, &dbe_list);
  239. spin_unlock_irq(&dbe_lock);
  240. }
  241. return 0;
  242. }
  243. void module_arch_cleanup(struct module *mod)
  244. {
  245. spin_lock_irq(&dbe_lock);
  246. list_del(&mod->arch.dbe_list);
  247. spin_unlock_irq(&dbe_lock);
  248. }