module.c 10 KB

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