module.c 8.9 KB

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