xref: /openbmc/linux/arch/mips/kernel/module.c (revision fdfd4289)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24e6a05feSThiemo Seufer /*
34e6a05feSThiemo Seufer  *
44e6a05feSThiemo Seufer  *  Copyright (C) 2001 Rusty Russell.
54e6a05feSThiemo Seufer  *  Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
64e6a05feSThiemo Seufer  *  Copyright (C) 2005 Thiemo Seufer
74e6a05feSThiemo Seufer  */
84e6a05feSThiemo Seufer 
94e6a05feSThiemo Seufer #undef DEBUG
104e6a05feSThiemo Seufer 
119f3b8081SPaul Gortmaker #include <linux/extable.h>
124e6a05feSThiemo Seufer #include <linux/moduleloader.h>
134e6a05feSThiemo Seufer #include <linux/elf.h>
1427ac792cSAndrea Righi #include <linux/mm.h>
15761845f0SRalf Baechle #include <linux/numa.h>
164e6a05feSThiemo Seufer #include <linux/vmalloc.h>
174e6a05feSThiemo Seufer #include <linux/slab.h>
184e6a05feSThiemo Seufer #include <linux/fs.h>
194e6a05feSThiemo Seufer #include <linux/string.h>
204e6a05feSThiemo Seufer #include <linux/kernel.h>
211da177e4SLinus Torvalds #include <linux/spinlock.h>
2294bb0c1aSDavid Daney #include <linux/jump_label.h>
2394bb0c1aSDavid Daney 
24*fdfd4289SArd Biesheuvel extern void jump_label_apply_nops(struct module *mod);
251da177e4SLinus Torvalds 
264e6a05feSThiemo Seufer struct mips_hi16 {
274e6a05feSThiemo Seufer 	struct mips_hi16 *next;
284e6a05feSThiemo Seufer 	Elf_Addr *addr;
294e6a05feSThiemo Seufer 	Elf_Addr value;
304e6a05feSThiemo Seufer };
314e6a05feSThiemo Seufer 
321da177e4SLinus Torvalds static LIST_HEAD(dbe_list);
331da177e4SLinus Torvalds static DEFINE_SPINLOCK(dbe_lock);
341da177e4SLinus Torvalds 
3566574cc0SJonas Bonn #ifdef MODULE_START
module_alloc(unsigned long size)364e6a05feSThiemo Seufer void *module_alloc(unsigned long size)
374e6a05feSThiemo Seufer {
38d0a21265SDavid Rientjes 	return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
39cb9e3c29SAndrey Ryabinin 				GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
40d0a21265SDavid Rientjes 				__builtin_return_address(0));
4166574cc0SJonas Bonn }
42656be92fSAtsushi Nemoto #endif
434e6a05feSThiemo Seufer 
apply_r_mips_32(u32 * location,u32 base,Elf_Addr v)44049a68efSAlexander Lobakin static void apply_r_mips_32(u32 *location, u32 base, Elf_Addr v)
454e6a05feSThiemo Seufer {
46430d0b88SPaul Burton 	*location = base + v;
474e6a05feSThiemo Seufer }
484e6a05feSThiemo Seufer 
apply_r_mips_26(struct module * me,u32 * location,u32 base,Elf_Addr v)49049a68efSAlexander Lobakin static int apply_r_mips_26(struct module *me, u32 *location, u32 base,
50049a68efSAlexander Lobakin 			   Elf_Addr v)
514e6a05feSThiemo Seufer {
524e6a05feSThiemo Seufer 	if (v % 4) {
53430d0b88SPaul Burton 		pr_err("module %s: dangerous R_MIPS_26 relocation\n",
546f9fdeb6SRalf Baechle 		       me->name);
554e6a05feSThiemo Seufer 		return -ENOEXEC;
564e6a05feSThiemo Seufer 	}
574e6a05feSThiemo Seufer 
584e6a05feSThiemo Seufer 	if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
5955d791f3SSteven J. Hill 		pr_err("module %s: relocation overflow\n",
604e6a05feSThiemo Seufer 		       me->name);
614e6a05feSThiemo Seufer 		return -ENOEXEC;
624e6a05feSThiemo Seufer 	}
634e6a05feSThiemo Seufer 
644e6a05feSThiemo Seufer 	*location = (*location & ~0x03ffffff) |
65430d0b88SPaul Burton 		    ((base + (v >> 2)) & 0x03ffffff);
664e6a05feSThiemo Seufer 
674e6a05feSThiemo Seufer 	return 0;
684e6a05feSThiemo Seufer }
694e6a05feSThiemo Seufer 
apply_r_mips_hi16(struct module * me,u32 * location,Elf_Addr v,bool rela)70049a68efSAlexander Lobakin static int apply_r_mips_hi16(struct module *me, u32 *location, Elf_Addr v,
71049a68efSAlexander Lobakin 			     bool rela)
724e6a05feSThiemo Seufer {
734e6a05feSThiemo Seufer 	struct mips_hi16 *n;
744e6a05feSThiemo Seufer 
75430d0b88SPaul Burton 	if (rela) {
76430d0b88SPaul Burton 		*location = (*location & 0xffff0000) |
77430d0b88SPaul Burton 			    ((((long long) v + 0x8000LL) >> 16) & 0xffff);
78430d0b88SPaul Burton 		return 0;
79430d0b88SPaul Burton 	}
80430d0b88SPaul Burton 
814e6a05feSThiemo Seufer 	/*
824e6a05feSThiemo Seufer 	 * We cannot relocate this one now because we don't know the value of
834e6a05feSThiemo Seufer 	 * the carry we need to add.  Save the information, and let LO16 do the
844e6a05feSThiemo Seufer 	 * actual relocation.
854e6a05feSThiemo Seufer 	 */
864e6a05feSThiemo Seufer 	n = kmalloc(sizeof *n, GFP_KERNEL);
874e6a05feSThiemo Seufer 	if (!n)
884e6a05feSThiemo Seufer 		return -ENOMEM;
894e6a05feSThiemo Seufer 
904e6a05feSThiemo Seufer 	n->addr = (Elf_Addr *)location;
914e6a05feSThiemo Seufer 	n->value = v;
92861667dcSRalf Baechle 	n->next = me->arch.r_mips_hi16_list;
93861667dcSRalf Baechle 	me->arch.r_mips_hi16_list = n;
944e6a05feSThiemo Seufer 
954e6a05feSThiemo Seufer 	return 0;
964e6a05feSThiemo Seufer }
974e6a05feSThiemo Seufer 
free_relocation_chain(struct mips_hi16 * l)98c54de490SRalf Baechle static void free_relocation_chain(struct mips_hi16 *l)
99c54de490SRalf Baechle {
100c54de490SRalf Baechle 	struct mips_hi16 *next;
101c54de490SRalf Baechle 
102c54de490SRalf Baechle 	while (l) {
103c54de490SRalf Baechle 		next = l->next;
104c54de490SRalf Baechle 		kfree(l);
105c54de490SRalf Baechle 		l = next;
106c54de490SRalf Baechle 	}
107c54de490SRalf Baechle }
108c54de490SRalf Baechle 
apply_r_mips_lo16(struct module * me,u32 * location,u32 base,Elf_Addr v,bool rela)109430d0b88SPaul Burton static int apply_r_mips_lo16(struct module *me, u32 *location,
110430d0b88SPaul Burton 			     u32 base, Elf_Addr v, bool rela)
1114e6a05feSThiemo Seufer {
112430d0b88SPaul Burton 	unsigned long insnlo = base;
113c54de490SRalf Baechle 	struct mips_hi16 *l;
1144e6a05feSThiemo Seufer 	Elf_Addr val, vallo;
1154e6a05feSThiemo Seufer 
116430d0b88SPaul Burton 	if (rela) {
117430d0b88SPaul Burton 		*location = (*location & 0xffff0000) | (v & 0xffff);
118430d0b88SPaul Burton 		return 0;
119430d0b88SPaul Burton 	}
120430d0b88SPaul Burton 
1214e6a05feSThiemo Seufer 	/* Sign extend the addend we extract from the lo insn.	*/
1224e6a05feSThiemo Seufer 	vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1234e6a05feSThiemo Seufer 
124861667dcSRalf Baechle 	if (me->arch.r_mips_hi16_list != NULL) {
125861667dcSRalf Baechle 		l = me->arch.r_mips_hi16_list;
1264e6a05feSThiemo Seufer 		while (l != NULL) {
127c54de490SRalf Baechle 			struct mips_hi16 *next;
1284e6a05feSThiemo Seufer 			unsigned long insn;
1294e6a05feSThiemo Seufer 
1304e6a05feSThiemo Seufer 			/*
1314e6a05feSThiemo Seufer 			 * The value for the HI16 had best be the same.
1324e6a05feSThiemo Seufer 			 */
1334e6a05feSThiemo Seufer 			if (v != l->value)
1344e6a05feSThiemo Seufer 				goto out_danger;
1354e6a05feSThiemo Seufer 
1364e6a05feSThiemo Seufer 			/*
1374e6a05feSThiemo Seufer 			 * Do the HI16 relocation.  Note that we actually don't
1384e6a05feSThiemo Seufer 			 * need to know anything about the LO16 itself, except
1394e6a05feSThiemo Seufer 			 * where to find the low 16 bits of the addend needed
1404e6a05feSThiemo Seufer 			 * by the LO16.
1414e6a05feSThiemo Seufer 			 */
1424e6a05feSThiemo Seufer 			insn = *l->addr;
1434e6a05feSThiemo Seufer 			val = ((insn & 0xffff) << 16) + vallo;
1444e6a05feSThiemo Seufer 			val += v;
1454e6a05feSThiemo Seufer 
1464e6a05feSThiemo Seufer 			/*
1474e6a05feSThiemo Seufer 			 * Account for the sign extension that will happen in
1484e6a05feSThiemo Seufer 			 * the low bits.
1494e6a05feSThiemo Seufer 			 */
1504e6a05feSThiemo Seufer 			val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
1514e6a05feSThiemo Seufer 
1524e6a05feSThiemo Seufer 			insn = (insn & ~0xffff) | val;
1534e6a05feSThiemo Seufer 			*l->addr = insn;
1544e6a05feSThiemo Seufer 
1554e6a05feSThiemo Seufer 			next = l->next;
1564e6a05feSThiemo Seufer 			kfree(l);
1574e6a05feSThiemo Seufer 			l = next;
1584e6a05feSThiemo Seufer 		}
1594e6a05feSThiemo Seufer 
160861667dcSRalf Baechle 		me->arch.r_mips_hi16_list = NULL;
1614e6a05feSThiemo Seufer 	}
1624e6a05feSThiemo Seufer 
1634e6a05feSThiemo Seufer 	/*
1644e6a05feSThiemo Seufer 	 * Ok, we're done with the HI16 relocs.	 Now deal with the LO16.
1654e6a05feSThiemo Seufer 	 */
1664e6a05feSThiemo Seufer 	val = v + vallo;
1674e6a05feSThiemo Seufer 	insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1684e6a05feSThiemo Seufer 	*location = insnlo;
1694e6a05feSThiemo Seufer 
1704e6a05feSThiemo Seufer 	return 0;
1714e6a05feSThiemo Seufer 
1724e6a05feSThiemo Seufer out_danger:
173c54de490SRalf Baechle 	free_relocation_chain(l);
174c54de490SRalf Baechle 	me->arch.r_mips_hi16_list = NULL;
175d3cac35cSRalf Baechle 
176430d0b88SPaul Burton 	pr_err("module %s: dangerous R_MIPS_LO16 relocation\n", me->name);
1774e6a05feSThiemo Seufer 
1784e6a05feSThiemo Seufer 	return -ENOEXEC;
1794e6a05feSThiemo Seufer }
1804e6a05feSThiemo Seufer 
apply_r_mips_pc(struct module * me,u32 * location,u32 base,Elf_Addr v,unsigned int bits)181430d0b88SPaul Burton static int apply_r_mips_pc(struct module *me, u32 *location, u32 base,
182430d0b88SPaul Burton 			   Elf_Addr v, unsigned int bits)
1835d3c7925SPaul Burton {
1845d3c7925SPaul Burton 	unsigned long mask = GENMASK(bits - 1, 0);
1855d3c7925SPaul Burton 	unsigned long se_bits;
1865d3c7925SPaul Burton 	long offset;
1875d3c7925SPaul Burton 
1885d3c7925SPaul Burton 	if (v % 4) {
189430d0b88SPaul Burton 		pr_err("module %s: dangerous R_MIPS_PC%u relocation\n",
1905d3c7925SPaul Burton 		       me->name, bits);
1915d3c7925SPaul Burton 		return -ENOEXEC;
1925d3c7925SPaul Burton 	}
1935d3c7925SPaul Burton 
194430d0b88SPaul Burton 	/* retrieve & sign extend implicit addend if any */
195430d0b88SPaul Burton 	offset = base & mask;
1965d3c7925SPaul Burton 	offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
1975d3c7925SPaul Burton 
1985d3c7925SPaul Burton 	offset += ((long)v - (long)location) >> 2;
1995d3c7925SPaul Burton 
2005d3c7925SPaul Burton 	/* check the sign bit onwards are identical - ie. we didn't overflow */
2015d3c7925SPaul Burton 	se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
2025d3c7925SPaul Burton 	if ((offset & ~mask) != (se_bits & ~mask)) {
2035d3c7925SPaul Burton 		pr_err("module %s: relocation overflow\n", me->name);
2045d3c7925SPaul Burton 		return -ENOEXEC;
2055d3c7925SPaul Burton 	}
2065d3c7925SPaul Burton 
2075d3c7925SPaul Burton 	*location = (*location & ~mask) | (offset & mask);
2085d3c7925SPaul Burton 
2095d3c7925SPaul Burton 	return 0;
2105d3c7925SPaul Burton }
2115d3c7925SPaul Burton 
apply_r_mips_pc16(struct module * me,u32 * location,u32 base,Elf_Addr v)212049a68efSAlexander Lobakin static int apply_r_mips_pc16(struct module *me, u32 *location, u32 base,
213049a68efSAlexander Lobakin 			     Elf_Addr v)
2145d3c7925SPaul Burton {
215430d0b88SPaul Burton 	return apply_r_mips_pc(me, location, base, v, 16);
2165d3c7925SPaul Burton }
2175d3c7925SPaul Burton 
apply_r_mips_pc21(struct module * me,u32 * location,u32 base,Elf_Addr v)218049a68efSAlexander Lobakin static int apply_r_mips_pc21(struct module *me, u32 *location, u32 base,
219049a68efSAlexander Lobakin 			     Elf_Addr v)
2205d3c7925SPaul Burton {
221430d0b88SPaul Burton 	return apply_r_mips_pc(me, location, base, v, 21);
2225d3c7925SPaul Burton }
2235d3c7925SPaul Burton 
apply_r_mips_pc26(struct module * me,u32 * location,u32 base,Elf_Addr v)224049a68efSAlexander Lobakin static int apply_r_mips_pc26(struct module *me, u32 *location, u32 base,
225049a68efSAlexander Lobakin 			     Elf_Addr v)
2265d3c7925SPaul Burton {
227430d0b88SPaul Burton 	return apply_r_mips_pc(me, location, base, v, 26);
2285d3c7925SPaul Burton }
2295d3c7925SPaul Burton 
apply_r_mips_64(u32 * location,Elf_Addr v,bool rela)230049a68efSAlexander Lobakin static int apply_r_mips_64(u32 *location, Elf_Addr v, bool rela)
231430d0b88SPaul Burton {
232430d0b88SPaul Burton 	if (WARN_ON(!rela))
233430d0b88SPaul Burton 		return -EINVAL;
234430d0b88SPaul Burton 
235430d0b88SPaul Burton 	*(Elf_Addr *)location = v;
236430d0b88SPaul Burton 
237430d0b88SPaul Burton 	return 0;
238430d0b88SPaul Burton }
239430d0b88SPaul Burton 
apply_r_mips_higher(u32 * location,Elf_Addr v,bool rela)240049a68efSAlexander Lobakin static int apply_r_mips_higher(u32 *location, Elf_Addr v, bool rela)
241430d0b88SPaul Burton {
242430d0b88SPaul Burton 	if (WARN_ON(!rela))
243430d0b88SPaul Burton 		return -EINVAL;
244430d0b88SPaul Burton 
245430d0b88SPaul Burton 	*location = (*location & 0xffff0000) |
246430d0b88SPaul Burton 		    ((((long long)v + 0x80008000LL) >> 32) & 0xffff);
247430d0b88SPaul Burton 
248430d0b88SPaul Burton 	return 0;
249430d0b88SPaul Burton }
250430d0b88SPaul Burton 
apply_r_mips_highest(u32 * location,Elf_Addr v,bool rela)251049a68efSAlexander Lobakin static int apply_r_mips_highest(u32 *location, Elf_Addr v, bool rela)
252430d0b88SPaul Burton {
253430d0b88SPaul Burton 	if (WARN_ON(!rela))
254430d0b88SPaul Burton 		return -EINVAL;
255430d0b88SPaul Burton 
256430d0b88SPaul Burton 	*location = (*location & 0xffff0000) |
257430d0b88SPaul Burton 		    ((((long long)v + 0x800080008000LL) >> 48) & 0xffff);
258430d0b88SPaul Burton 
259430d0b88SPaul Burton 	return 0;
260430d0b88SPaul Burton }
261430d0b88SPaul Burton 
262430d0b88SPaul Burton /**
263430d0b88SPaul Burton  * reloc_handler() - Apply a particular relocation to a module
264049a68efSAlexander Lobakin  * @type: type of the relocation to apply
265430d0b88SPaul Burton  * @me: the module to apply the reloc to
266430d0b88SPaul Burton  * @location: the address at which the reloc is to be applied
267430d0b88SPaul Burton  * @base: the existing value at location for REL-style; 0 for RELA-style
268430d0b88SPaul Burton  * @v: the value of the reloc, with addend for RELA-style
269049a68efSAlexander Lobakin  * @rela: indication of is this a RELA (true) or REL (false) relocation
270430d0b88SPaul Burton  *
271049a68efSAlexander Lobakin  * Each implemented relocation function applies a particular type of
272430d0b88SPaul Burton  * relocation to the module @me. Relocs that may be found in either REL or RELA
273430d0b88SPaul Burton  * variants can be handled by making use of the @base & @v parameters which are
274430d0b88SPaul Burton  * set to values which abstract the difference away from the particular reloc
275430d0b88SPaul Burton  * implementations.
276430d0b88SPaul Burton  *
277430d0b88SPaul Burton  * Return: 0 upon success, else -ERRNO
278430d0b88SPaul Burton  */
reloc_handler(u32 type,struct module * me,u32 * location,u32 base,Elf_Addr v,bool rela)279049a68efSAlexander Lobakin static int reloc_handler(u32 type, struct module *me, u32 *location, u32 base,
280049a68efSAlexander Lobakin 			 Elf_Addr v, bool rela)
281049a68efSAlexander Lobakin {
282049a68efSAlexander Lobakin 	switch (type) {
283049a68efSAlexander Lobakin 	case R_MIPS_NONE:
284049a68efSAlexander Lobakin 		break;
285049a68efSAlexander Lobakin 	case R_MIPS_32:
286049a68efSAlexander Lobakin 		apply_r_mips_32(location, base, v);
287049a68efSAlexander Lobakin 		break;
288049a68efSAlexander Lobakin 	case R_MIPS_26:
289049a68efSAlexander Lobakin 		return apply_r_mips_26(me, location, base, v);
290049a68efSAlexander Lobakin 	case R_MIPS_HI16:
291049a68efSAlexander Lobakin 		return apply_r_mips_hi16(me, location, v, rela);
292049a68efSAlexander Lobakin 	case R_MIPS_LO16:
293049a68efSAlexander Lobakin 		return apply_r_mips_lo16(me, location, base, v, rela);
294049a68efSAlexander Lobakin 	case R_MIPS_PC16:
295049a68efSAlexander Lobakin 		return apply_r_mips_pc16(me, location, base, v);
296049a68efSAlexander Lobakin 	case R_MIPS_PC21_S2:
297049a68efSAlexander Lobakin 		return apply_r_mips_pc21(me, location, base, v);
298049a68efSAlexander Lobakin 	case R_MIPS_PC26_S2:
299049a68efSAlexander Lobakin 		return apply_r_mips_pc26(me, location, base, v);
300049a68efSAlexander Lobakin 	case R_MIPS_64:
301049a68efSAlexander Lobakin 		return apply_r_mips_64(location, v, rela);
302049a68efSAlexander Lobakin 	case R_MIPS_HIGHER:
303049a68efSAlexander Lobakin 		return apply_r_mips_higher(location, v, rela);
304049a68efSAlexander Lobakin 	case R_MIPS_HIGHEST:
305049a68efSAlexander Lobakin 		return apply_r_mips_highest(location, v, rela);
306049a68efSAlexander Lobakin 	default:
307049a68efSAlexander Lobakin 		pr_err("%s: Unknown relocation type %u\n", me->name, type);
308049a68efSAlexander Lobakin 		return -EINVAL;
309049a68efSAlexander Lobakin 	}
310430d0b88SPaul Burton 
311049a68efSAlexander Lobakin 	return 0;
312049a68efSAlexander Lobakin }
3134e6a05feSThiemo Seufer 
__apply_relocate(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me,bool rela)314430d0b88SPaul Burton static int __apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
3154e6a05feSThiemo Seufer 			    unsigned int symindex, unsigned int relsec,
316430d0b88SPaul Burton 			    struct module *me, bool rela)
3174e6a05feSThiemo Seufer {
318430d0b88SPaul Burton 	union {
319430d0b88SPaul Burton 		Elf_Mips_Rel *rel;
320430d0b88SPaul Burton 		Elf_Mips_Rela *rela;
321430d0b88SPaul Burton 	} r;
3224e6a05feSThiemo Seufer 	Elf_Sym *sym;
323430d0b88SPaul Burton 	u32 *location, base;
32404211a57SPaul Burton 	unsigned int i, type;
3254e6a05feSThiemo Seufer 	Elf_Addr v;
326351b0940SPaul Burton 	int err = 0;
327430d0b88SPaul Burton 	size_t reloc_sz;
3284e6a05feSThiemo Seufer 
3294e6a05feSThiemo Seufer 	pr_debug("Applying relocate section %u to %u\n", relsec,
3304e6a05feSThiemo Seufer 	       sechdrs[relsec].sh_info);
3314e6a05feSThiemo Seufer 
332430d0b88SPaul Burton 	r.rel = (void *)sechdrs[relsec].sh_addr;
333430d0b88SPaul Burton 	reloc_sz = rela ? sizeof(*r.rela) : sizeof(*r.rel);
334861667dcSRalf Baechle 	me->arch.r_mips_hi16_list = NULL;
335430d0b88SPaul Burton 	for (i = 0; i < sechdrs[relsec].sh_size / reloc_sz; i++) {
3364e6a05feSThiemo Seufer 		/* This is where to make the change */
3374e6a05feSThiemo Seufer 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
338430d0b88SPaul Burton 			+ r.rel->r_offset;
3394e6a05feSThiemo Seufer 		/* This is the symbol it is referring to */
3404e6a05feSThiemo Seufer 		sym = (Elf_Sym *)sechdrs[symindex].sh_addr
341430d0b88SPaul Burton 			+ ELF_MIPS_R_SYM(*r.rel);
342ba837d38SAndrzej Hajda 		if (sym->st_value >= -MAX_ERRNO) {
343f3bf07b9SAtsushi Nemoto 			/* Ignore unresolved weak symbol */
344f3bf07b9SAtsushi Nemoto 			if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
345f3bf07b9SAtsushi Nemoto 				continue;
34655d791f3SSteven J. Hill 			pr_warn("%s: Unknown symbol %s\n",
3474e6a05feSThiemo Seufer 				me->name, strtab + sym->st_name);
348351b0940SPaul Burton 			err = -ENOENT;
349351b0940SPaul Burton 			goto out;
3504e6a05feSThiemo Seufer 		}
3514e6a05feSThiemo Seufer 
352430d0b88SPaul Burton 		type = ELF_MIPS_R_TYPE(*r.rel);
35304211a57SPaul Burton 
354430d0b88SPaul Burton 		if (rela) {
355430d0b88SPaul Burton 			v = sym->st_value + r.rela->r_addend;
356430d0b88SPaul Burton 			base = 0;
357430d0b88SPaul Burton 			r.rela = &r.rela[1];
358430d0b88SPaul Burton 		} else {
35904211a57SPaul Burton 			v = sym->st_value;
360430d0b88SPaul Burton 			base = *location;
361430d0b88SPaul Burton 			r.rel = &r.rel[1];
3624e6a05feSThiemo Seufer 		}
3634e6a05feSThiemo Seufer 
364049a68efSAlexander Lobakin 		err = reloc_handler(type, me, location, base, v, rela);
365351b0940SPaul Burton 		if (err)
366351b0940SPaul Burton 			goto out;
3674e6a05feSThiemo Seufer 	}
3684e6a05feSThiemo Seufer 
369351b0940SPaul Burton out:
370c54de490SRalf Baechle 	/*
371c54de490SRalf Baechle 	 * Normally the hi16 list should be deallocated at this point. A
372c54de490SRalf Baechle 	 * malformed binary however could contain a series of R_MIPS_HI16
373351b0940SPaul Burton 	 * relocations not followed by a R_MIPS_LO16 relocation, or if we hit
374351b0940SPaul Burton 	 * an error processing a reloc we might have gotten here before
375351b0940SPaul Burton 	 * reaching the R_MIPS_LO16. In either case, free up the list and
376351b0940SPaul Burton 	 * return an error.
377c54de490SRalf Baechle 	 */
378c54de490SRalf Baechle 	if (me->arch.r_mips_hi16_list) {
379c54de490SRalf Baechle 		free_relocation_chain(me->arch.r_mips_hi16_list);
380c54de490SRalf Baechle 		me->arch.r_mips_hi16_list = NULL;
381351b0940SPaul Burton 		err = err ?: -ENOEXEC;
382c54de490SRalf Baechle 	}
383c54de490SRalf Baechle 
384351b0940SPaul Burton 	return err;
3854e6a05feSThiemo Seufer }
3864e6a05feSThiemo Seufer 
apply_relocate(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)387430d0b88SPaul Burton int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
388430d0b88SPaul Burton 		   unsigned int symindex, unsigned int relsec,
389430d0b88SPaul Burton 		   struct module *me)
390430d0b88SPaul Burton {
391430d0b88SPaul Burton 	return __apply_relocate(sechdrs, strtab, symindex, relsec, me, false);
392430d0b88SPaul Burton }
393430d0b88SPaul Burton 
394430d0b88SPaul Burton #ifdef CONFIG_MODULES_USE_ELF_RELA
apply_relocate_add(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)395430d0b88SPaul Burton int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
396430d0b88SPaul Burton 		       unsigned int symindex, unsigned int relsec,
397430d0b88SPaul Burton 		       struct module *me)
398430d0b88SPaul Burton {
399430d0b88SPaul Burton 	return __apply_relocate(sechdrs, strtab, symindex, relsec, me, true);
400430d0b88SPaul Burton }
401430d0b88SPaul Burton #endif /* CONFIG_MODULES_USE_ELF_RELA */
402430d0b88SPaul Burton 
4031da177e4SLinus Torvalds /* Given an address, look for it in the module exception tables. */
search_module_dbetables(unsigned long addr)4041da177e4SLinus Torvalds const struct exception_table_entry *search_module_dbetables(unsigned long addr)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	unsigned long flags;
4071da177e4SLinus Torvalds 	const struct exception_table_entry *e = NULL;
4081da177e4SLinus Torvalds 	struct mod_arch_specific *dbe;
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds 	spin_lock_irqsave(&dbe_lock, flags);
4111da177e4SLinus Torvalds 	list_for_each_entry(dbe, &dbe_list, dbe_list) {
412a94c33ddSThomas Meyer 		e = search_extable(dbe->dbe_start,
413a94c33ddSThomas Meyer 				   dbe->dbe_end - dbe->dbe_start, addr);
4141da177e4SLinus Torvalds 		if (e)
4151da177e4SLinus Torvalds 			break;
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dbe_lock, flags);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* Now, if we found one, we are running inside it now, hence
4201da177e4SLinus Torvalds 	   we cannot unload the module, hence no refcnt needed. */
4211da177e4SLinus Torvalds 	return e;
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4243a4fa0a2SRobert P. J. Day /* Put in dbe list if necessary. */
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)4251da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr,
4261da177e4SLinus Torvalds 		    const Elf_Shdr *sechdrs,
4271da177e4SLinus Torvalds 		    struct module *me)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	const Elf_Shdr *s;
4301da177e4SLinus Torvalds 	char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
4311da177e4SLinus Torvalds 
432*fdfd4289SArd Biesheuvel 	if (IS_ENABLED(CONFIG_JUMP_LABEL))
43394bb0c1aSDavid Daney 		jump_label_apply_nops(me);
43494bb0c1aSDavid Daney 
4351da177e4SLinus Torvalds 	INIT_LIST_HEAD(&me->arch.dbe_list);
4361da177e4SLinus Torvalds 	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
4371da177e4SLinus Torvalds 		if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
4381da177e4SLinus Torvalds 			continue;
4391da177e4SLinus Torvalds 		me->arch.dbe_start = (void *)s->sh_addr;
4401da177e4SLinus Torvalds 		me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
4411da177e4SLinus Torvalds 		spin_lock_irq(&dbe_lock);
4421da177e4SLinus Torvalds 		list_add(&me->arch.dbe_list, &dbe_list);
4431da177e4SLinus Torvalds 		spin_unlock_irq(&dbe_lock);
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds 	return 0;
4461da177e4SLinus Torvalds }
4471da177e4SLinus Torvalds 
module_arch_cleanup(struct module * mod)4481da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod)
4491da177e4SLinus Torvalds {
4501da177e4SLinus Torvalds 	spin_lock_irq(&dbe_lock);
4511da177e4SLinus Torvalds 	list_del(&mod->arch.dbe_list);
4521da177e4SLinus Torvalds 	spin_unlock_irq(&dbe_lock);
4531da177e4SLinus Torvalds }
454