module.c 11 KB

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