module.c 11 KB

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