module.c 7.3 KB

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