module.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. static int apply_r_mips_none(struct module *me, u32 *location,
  50. u32 base, Elf_Addr v, bool rela)
  51. {
  52. return 0;
  53. }
  54. static int apply_r_mips_32(struct module *me, u32 *location,
  55. u32 base, Elf_Addr v, bool rela)
  56. {
  57. *location = base + v;
  58. return 0;
  59. }
  60. static int apply_r_mips_26(struct module *me, u32 *location,
  61. u32 base, Elf_Addr v, bool rela)
  62. {
  63. if (v % 4) {
  64. pr_err("module %s: dangerous R_MIPS_26 relocation\n",
  65. me->name);
  66. return -ENOEXEC;
  67. }
  68. if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
  69. pr_err("module %s: relocation overflow\n",
  70. me->name);
  71. return -ENOEXEC;
  72. }
  73. *location = (*location & ~0x03ffffff) |
  74. ((base + (v >> 2)) & 0x03ffffff);
  75. return 0;
  76. }
  77. static int apply_r_mips_hi16(struct module *me, u32 *location,
  78. u32 base, Elf_Addr v, bool rela)
  79. {
  80. struct mips_hi16 *n;
  81. if (rela) {
  82. *location = (*location & 0xffff0000) |
  83. ((((long long) v + 0x8000LL) >> 16) & 0xffff);
  84. return 0;
  85. }
  86. /*
  87. * We cannot relocate this one now because we don't know the value of
  88. * the carry we need to add. Save the information, and let LO16 do the
  89. * actual relocation.
  90. */
  91. n = kmalloc(sizeof *n, GFP_KERNEL);
  92. if (!n)
  93. return -ENOMEM;
  94. n->addr = (Elf_Addr *)location;
  95. n->value = v;
  96. n->next = me->arch.r_mips_hi16_list;
  97. me->arch.r_mips_hi16_list = n;
  98. return 0;
  99. }
  100. static void free_relocation_chain(struct mips_hi16 *l)
  101. {
  102. struct mips_hi16 *next;
  103. while (l) {
  104. next = l->next;
  105. kfree(l);
  106. l = next;
  107. }
  108. }
  109. static int apply_r_mips_lo16(struct module *me, u32 *location,
  110. u32 base, Elf_Addr v, bool rela)
  111. {
  112. unsigned long insnlo = base;
  113. struct mips_hi16 *l;
  114. Elf_Addr val, vallo;
  115. if (rela) {
  116. *location = (*location & 0xffff0000) | (v & 0xffff);
  117. return 0;
  118. }
  119. /* Sign extend the addend we extract from the lo insn. */
  120. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  121. if (me->arch.r_mips_hi16_list != NULL) {
  122. l = me->arch.r_mips_hi16_list;
  123. while (l != NULL) {
  124. struct mips_hi16 *next;
  125. unsigned long insn;
  126. /*
  127. * The value for the HI16 had best be the same.
  128. */
  129. if (v != l->value)
  130. goto out_danger;
  131. /*
  132. * Do the HI16 relocation. Note that we actually don't
  133. * need to know anything about the LO16 itself, except
  134. * where to find the low 16 bits of the addend needed
  135. * by the LO16.
  136. */
  137. insn = *l->addr;
  138. val = ((insn & 0xffff) << 16) + vallo;
  139. val += v;
  140. /*
  141. * Account for the sign extension that will happen in
  142. * the low bits.
  143. */
  144. val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
  145. insn = (insn & ~0xffff) | val;
  146. *l->addr = insn;
  147. next = l->next;
  148. kfree(l);
  149. l = next;
  150. }
  151. me->arch.r_mips_hi16_list = NULL;
  152. }
  153. /*
  154. * Ok, we're done with the HI16 relocs. Now deal with the LO16.
  155. */
  156. val = v + vallo;
  157. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  158. *location = insnlo;
  159. return 0;
  160. out_danger:
  161. free_relocation_chain(l);
  162. me->arch.r_mips_hi16_list = NULL;
  163. pr_err("module %s: dangerous R_MIPS_LO16 relocation\n", me->name);
  164. return -ENOEXEC;
  165. }
  166. static int apply_r_mips_pc(struct module *me, u32 *location, u32 base,
  167. Elf_Addr v, unsigned int bits)
  168. {
  169. unsigned long mask = GENMASK(bits - 1, 0);
  170. unsigned long se_bits;
  171. long offset;
  172. if (v % 4) {
  173. pr_err("module %s: dangerous R_MIPS_PC%u relocation\n",
  174. me->name, bits);
  175. return -ENOEXEC;
  176. }
  177. /* retrieve & sign extend implicit addend if any */
  178. offset = base & mask;
  179. offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
  180. offset += ((long)v - (long)location) >> 2;
  181. /* check the sign bit onwards are identical - ie. we didn't overflow */
  182. se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
  183. if ((offset & ~mask) != (se_bits & ~mask)) {
  184. pr_err("module %s: relocation overflow\n", me->name);
  185. return -ENOEXEC;
  186. }
  187. *location = (*location & ~mask) | (offset & mask);
  188. return 0;
  189. }
  190. static int apply_r_mips_pc16(struct module *me, u32 *location,
  191. u32 base, Elf_Addr v, bool rela)
  192. {
  193. return apply_r_mips_pc(me, location, base, v, 16);
  194. }
  195. static int apply_r_mips_pc21(struct module *me, u32 *location,
  196. u32 base, Elf_Addr v, bool rela)
  197. {
  198. return apply_r_mips_pc(me, location, base, v, 21);
  199. }
  200. static int apply_r_mips_pc26(struct module *me, u32 *location,
  201. u32 base, Elf_Addr v, bool rela)
  202. {
  203. return apply_r_mips_pc(me, location, base, v, 26);
  204. }
  205. static int apply_r_mips_64(struct module *me, u32 *location,
  206. u32 base, Elf_Addr v, bool rela)
  207. {
  208. if (WARN_ON(!rela))
  209. return -EINVAL;
  210. *(Elf_Addr *)location = v;
  211. return 0;
  212. }
  213. static int apply_r_mips_higher(struct module *me, u32 *location,
  214. u32 base, Elf_Addr v, bool rela)
  215. {
  216. if (WARN_ON(!rela))
  217. return -EINVAL;
  218. *location = (*location & 0xffff0000) |
  219. ((((long long)v + 0x80008000LL) >> 32) & 0xffff);
  220. return 0;
  221. }
  222. static int apply_r_mips_highest(struct module *me, u32 *location,
  223. u32 base, Elf_Addr v, bool rela)
  224. {
  225. if (WARN_ON(!rela))
  226. return -EINVAL;
  227. *location = (*location & 0xffff0000) |
  228. ((((long long)v + 0x800080008000LL) >> 48) & 0xffff);
  229. return 0;
  230. }
  231. /**
  232. * reloc_handler() - Apply a particular relocation to a module
  233. * @me: the module to apply the reloc to
  234. * @location: the address at which the reloc is to be applied
  235. * @base: the existing value at location for REL-style; 0 for RELA-style
  236. * @v: the value of the reloc, with addend for RELA-style
  237. *
  238. * Each implemented reloc_handler function applies a particular type of
  239. * relocation to the module @me. Relocs that may be found in either REL or RELA
  240. * variants can be handled by making use of the @base & @v parameters which are
  241. * set to values which abstract the difference away from the particular reloc
  242. * implementations.
  243. *
  244. * Return: 0 upon success, else -ERRNO
  245. */
  246. typedef int (*reloc_handler)(struct module *me, u32 *location,
  247. u32 base, Elf_Addr v, bool rela);
  248. /* The handlers for known reloc types */
  249. static reloc_handler reloc_handlers[] = {
  250. [R_MIPS_NONE] = apply_r_mips_none,
  251. [R_MIPS_32] = apply_r_mips_32,
  252. [R_MIPS_26] = apply_r_mips_26,
  253. [R_MIPS_HI16] = apply_r_mips_hi16,
  254. [R_MIPS_LO16] = apply_r_mips_lo16,
  255. [R_MIPS_PC16] = apply_r_mips_pc16,
  256. [R_MIPS_64] = apply_r_mips_64,
  257. [R_MIPS_HIGHER] = apply_r_mips_higher,
  258. [R_MIPS_HIGHEST] = apply_r_mips_highest,
  259. [R_MIPS_PC21_S2] = apply_r_mips_pc21,
  260. [R_MIPS_PC26_S2] = apply_r_mips_pc26,
  261. };
  262. static int __apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
  263. unsigned int symindex, unsigned int relsec,
  264. struct module *me, bool rela)
  265. {
  266. union {
  267. Elf_Mips_Rel *rel;
  268. Elf_Mips_Rela *rela;
  269. } r;
  270. reloc_handler handler;
  271. Elf_Sym *sym;
  272. u32 *location, base;
  273. unsigned int i, type;
  274. Elf_Addr v;
  275. int err = 0;
  276. size_t reloc_sz;
  277. pr_debug("Applying relocate section %u to %u\n", relsec,
  278. sechdrs[relsec].sh_info);
  279. r.rel = (void *)sechdrs[relsec].sh_addr;
  280. reloc_sz = rela ? sizeof(*r.rela) : sizeof(*r.rel);
  281. me->arch.r_mips_hi16_list = NULL;
  282. for (i = 0; i < sechdrs[relsec].sh_size / reloc_sz; i++) {
  283. /* This is where to make the change */
  284. location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  285. + r.rel->r_offset;
  286. /* This is the symbol it is referring to */
  287. sym = (Elf_Sym *)sechdrs[symindex].sh_addr
  288. + ELF_MIPS_R_SYM(*r.rel);
  289. if (sym->st_value >= -MAX_ERRNO) {
  290. /* Ignore unresolved weak symbol */
  291. if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
  292. continue;
  293. pr_warn("%s: Unknown symbol %s\n",
  294. me->name, strtab + sym->st_name);
  295. err = -ENOENT;
  296. goto out;
  297. }
  298. type = ELF_MIPS_R_TYPE(*r.rel);
  299. if (type < ARRAY_SIZE(reloc_handlers))
  300. handler = reloc_handlers[type];
  301. else
  302. handler = NULL;
  303. if (!handler) {
  304. pr_err("%s: Unknown relocation type %u\n",
  305. me->name, type);
  306. err = -EINVAL;
  307. goto out;
  308. }
  309. if (rela) {
  310. v = sym->st_value + r.rela->r_addend;
  311. base = 0;
  312. r.rela = &r.rela[1];
  313. } else {
  314. v = sym->st_value;
  315. base = *location;
  316. r.rel = &r.rel[1];
  317. }
  318. err = handler(me, location, base, v, rela);
  319. if (err)
  320. goto out;
  321. }
  322. out:
  323. /*
  324. * Normally the hi16 list should be deallocated at this point. A
  325. * malformed binary however could contain a series of R_MIPS_HI16
  326. * relocations not followed by a R_MIPS_LO16 relocation, or if we hit
  327. * an error processing a reloc we might have gotten here before
  328. * reaching the R_MIPS_LO16. In either case, free up the list and
  329. * return an error.
  330. */
  331. if (me->arch.r_mips_hi16_list) {
  332. free_relocation_chain(me->arch.r_mips_hi16_list);
  333. me->arch.r_mips_hi16_list = NULL;
  334. err = err ?: -ENOEXEC;
  335. }
  336. return err;
  337. }
  338. int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
  339. unsigned int symindex, unsigned int relsec,
  340. struct module *me)
  341. {
  342. return __apply_relocate(sechdrs, strtab, symindex, relsec, me, false);
  343. }
  344. #ifdef CONFIG_MODULES_USE_ELF_RELA
  345. int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
  346. unsigned int symindex, unsigned int relsec,
  347. struct module *me)
  348. {
  349. return __apply_relocate(sechdrs, strtab, symindex, relsec, me, true);
  350. }
  351. #endif /* CONFIG_MODULES_USE_ELF_RELA */
  352. /* Given an address, look for it in the module exception tables. */
  353. const struct exception_table_entry *search_module_dbetables(unsigned long addr)
  354. {
  355. unsigned long flags;
  356. const struct exception_table_entry *e = NULL;
  357. struct mod_arch_specific *dbe;
  358. spin_lock_irqsave(&dbe_lock, flags);
  359. list_for_each_entry(dbe, &dbe_list, dbe_list) {
  360. e = search_extable(dbe->dbe_start,
  361. dbe->dbe_end - dbe->dbe_start, addr);
  362. if (e)
  363. break;
  364. }
  365. spin_unlock_irqrestore(&dbe_lock, flags);
  366. /* Now, if we found one, we are running inside it now, hence
  367. we cannot unload the module, hence no refcnt needed. */
  368. return e;
  369. }
  370. /* Put in dbe list if necessary. */
  371. int module_finalize(const Elf_Ehdr *hdr,
  372. const Elf_Shdr *sechdrs,
  373. struct module *me)
  374. {
  375. const Elf_Shdr *s;
  376. char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
  377. /* Make jump label nops. */
  378. jump_label_apply_nops(me);
  379. INIT_LIST_HEAD(&me->arch.dbe_list);
  380. for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
  381. if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
  382. continue;
  383. me->arch.dbe_start = (void *)s->sh_addr;
  384. me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
  385. spin_lock_irq(&dbe_lock);
  386. list_add(&me->arch.dbe_list, &dbe_list);
  387. spin_unlock_irq(&dbe_lock);
  388. }
  389. return 0;
  390. }
  391. void module_arch_cleanup(struct module *mod)
  392. {
  393. spin_lock_irq(&dbe_lock);
  394. list_del(&mod->arch.dbe_list);
  395. spin_unlock_irq(&dbe_lock);
  396. }