module.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/extable.h>
  22. #include <linux/moduleloader.h>
  23. #include <linux/elf.h>
  24. #include <linux/mm.h>
  25. #include <linux/numa.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/slab.h>
  28. #include <linux/fs.h>
  29. #include <linux/string.h>
  30. #include <linux/kernel.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/jump_label.h>
  33. #include <asm/pgtable.h> /* MODULE_START */
  34. struct mips_hi16 {
  35. struct mips_hi16 *next;
  36. Elf_Addr *addr;
  37. Elf_Addr value;
  38. };
  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, 0, NUMA_NO_NODE,
  46. __builtin_return_address(0));
  47. }
  48. #endif
  49. 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_26_rel(struct module *me, u32 *location, Elf_Addr v)
  59. {
  60. if (v % 4) {
  61. pr_err("module %s: dangerous R_MIPS_26 REL relocation\n",
  62. me->name);
  63. return -ENOEXEC;
  64. }
  65. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  66. pr_err("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 apply_r_mips_pc_rel(struct module *me, u32 *location, Elf_Addr v,
  153. unsigned bits)
  154. {
  155. unsigned long mask = GENMASK(bits - 1, 0);
  156. unsigned long se_bits;
  157. long offset;
  158. if (v % 4) {
  159. pr_err("module %s: dangerous R_MIPS_PC%u REL relocation\n",
  160. me->name, bits);
  161. return -ENOEXEC;
  162. }
  163. /* retrieve & sign extend implicit addend */
  164. offset = *location & mask;
  165. offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
  166. offset += ((long)v - (long)location) >> 2;
  167. /* check the sign bit onwards are identical - ie. we didn't overflow */
  168. se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
  169. if ((offset & ~mask) != (se_bits & ~mask)) {
  170. pr_err("module %s: relocation overflow\n", me->name);
  171. return -ENOEXEC;
  172. }
  173. *location = (*location & ~mask) | (offset & mask);
  174. return 0;
  175. }
  176. static int apply_r_mips_pc16_rel(struct module *me, u32 *location, Elf_Addr v)
  177. {
  178. return apply_r_mips_pc_rel(me, location, v, 16);
  179. }
  180. static int apply_r_mips_pc21_rel(struct module *me, u32 *location, Elf_Addr v)
  181. {
  182. return apply_r_mips_pc_rel(me, location, v, 21);
  183. }
  184. static int apply_r_mips_pc26_rel(struct module *me, u32 *location, Elf_Addr v)
  185. {
  186. return apply_r_mips_pc_rel(me, location, v, 26);
  187. }
  188. static int (*reloc_handlers_rel[]) (struct module *me, u32 *location,
  189. Elf_Addr v) = {
  190. [R_MIPS_NONE] = apply_r_mips_none,
  191. [R_MIPS_32] = apply_r_mips_32_rel,
  192. [R_MIPS_26] = apply_r_mips_26_rel,
  193. [R_MIPS_HI16] = apply_r_mips_hi16_rel,
  194. [R_MIPS_LO16] = apply_r_mips_lo16_rel,
  195. [R_MIPS_PC16] = apply_r_mips_pc16_rel,
  196. [R_MIPS_PC21_S2] = apply_r_mips_pc21_rel,
  197. [R_MIPS_PC26_S2] = apply_r_mips_pc26_rel,
  198. };
  199. int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
  200. unsigned int symindex, unsigned int relsec,
  201. struct module *me)
  202. {
  203. Elf_Mips_Rel *rel = (void *) sechdrs[relsec].sh_addr;
  204. int (*handler)(struct module *me, u32 *location, Elf_Addr v);
  205. Elf_Sym *sym;
  206. u32 *location;
  207. unsigned int i, type;
  208. Elf_Addr v;
  209. int res;
  210. pr_debug("Applying relocate section %u to %u\n", relsec,
  211. sechdrs[relsec].sh_info);
  212. me->arch.r_mips_hi16_list = NULL;
  213. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  214. /* This is where to make the change */
  215. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  216. + rel[i].r_offset;
  217. /* This is the symbol it is referring to */
  218. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  219. + ELF_MIPS_R_SYM(rel[i]);
  220. if (sym->st_value >= -MAX_ERRNO) {
  221. /* Ignore unresolved weak symbol */
  222. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  223. continue;
  224. pr_warn("%s: Unknown symbol %s\n",
  225. me->name, strtab + sym->st_name);
  226. return -ENOENT;
  227. }
  228. type = ELF_MIPS_R_TYPE(rel[i]);
  229. if (type < ARRAY_SIZE(reloc_handlers_rel))
  230. handler = reloc_handlers_rel[type];
  231. else
  232. handler = NULL;
  233. if (!handler) {
  234. pr_err("%s: Unknown relocation type %u\n",
  235. me->name, type);
  236. return -EINVAL;
  237. }
  238. v = sym->st_value;
  239. res = handler(me, location, v);
  240. if (res)
  241. return res;
  242. }
  243. /*
  244. * Normally the hi16 list should be deallocated at this point. A
  245. * malformed binary however could contain a series of R_MIPS_HI16
  246. * relocations not followed by a R_MIPS_LO16 relocation. In that
  247. * case, free up the list and return an error.
  248. */
  249. if (me->arch.r_mips_hi16_list) {
  250. free_relocation_chain(me->arch.r_mips_hi16_list);
  251. me->arch.r_mips_hi16_list = NULL;
  252. return -ENOEXEC;
  253. }
  254. return 0;
  255. }
  256. /* Given an address, look for it in the module exception tables. */
  257. const struct exception_table_entry *search_module_dbetables(unsigned long addr)
  258. {
  259. unsigned long flags;
  260. const struct exception_table_entry *e = NULL;
  261. struct mod_arch_specific *dbe;
  262. spin_lock_irqsave(&dbe_lock, flags);
  263. list_for_each_entry(dbe, &dbe_list, dbe_list) {
  264. e = search_extable(dbe->dbe_start, dbe->dbe_end - 1, addr);
  265. if (e)
  266. break;
  267. }
  268. spin_unlock_irqrestore(&dbe_lock, flags);
  269. /* Now, if we found one, we are running inside it now, hence
  270. we cannot unload the module, hence no refcnt needed. */
  271. return e;
  272. }
  273. /* Put in dbe list if necessary. */
  274. int module_finalize(const Elf_Ehdr *hdr,
  275. const Elf_Shdr *sechdrs,
  276. struct module *me)
  277. {
  278. const Elf_Shdr *s;
  279. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  280. /* Make jump label nops. */
  281. jump_label_apply_nops(me);
  282. INIT_LIST_HEAD(&me->arch.dbe_list);
  283. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  284. if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
  285. continue;
  286. me->arch.dbe_start = (void *)s->sh_addr;
  287. me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
  288. spin_lock_irq(&dbe_lock);
  289. list_add(&me->arch.dbe_list, &dbe_list);
  290. spin_unlock_irq(&dbe_lock);
  291. }
  292. return 0;
  293. }
  294. void module_arch_cleanup(struct module *mod)
  295. {
  296. spin_lock_irq(&dbe_lock);
  297. list_del(&mod->arch.dbe_list);
  298. spin_unlock_irq(&dbe_lock);
  299. }