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 ([email protected])
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/slab.h>
174e6a05feSThiemo Seufer #include <linux/fs.h>
184e6a05feSThiemo Seufer #include <linux/string.h>
194e6a05feSThiemo Seufer #include <linux/kernel.h>
201da177e4SLinus Torvalds #include <linux/spinlock.h>
2194bb0c1aSDavid Daney #include <linux/jump_label.h>
22*ad6eb1ecSArnd Bergmann #include <asm/jump_label.h>
231da177e4SLinus Torvalds
244e6a05feSThiemo Seufer struct mips_hi16 {
254e6a05feSThiemo Seufer struct mips_hi16 *next;
264e6a05feSThiemo Seufer Elf_Addr *addr;
274e6a05feSThiemo Seufer Elf_Addr value;
284e6a05feSThiemo Seufer };
294e6a05feSThiemo Seufer
301da177e4SLinus Torvalds static LIST_HEAD(dbe_list);
311da177e4SLinus Torvalds static DEFINE_SPINLOCK(dbe_lock);
321da177e4SLinus Torvalds
apply_r_mips_32(u32 * location,u32 base,Elf_Addr v)33049a68efSAlexander Lobakin static void apply_r_mips_32(u32 *location, u32 base, Elf_Addr v)
344e6a05feSThiemo Seufer {
35430d0b88SPaul Burton *location = base + v;
364e6a05feSThiemo Seufer }
374e6a05feSThiemo Seufer
apply_r_mips_26(struct module * me,u32 * location,u32 base,Elf_Addr v)38049a68efSAlexander Lobakin static int apply_r_mips_26(struct module *me, u32 *location, u32 base,
39049a68efSAlexander Lobakin Elf_Addr v)
404e6a05feSThiemo Seufer {
414e6a05feSThiemo Seufer if (v % 4) {
42430d0b88SPaul Burton pr_err("module %s: dangerous R_MIPS_26 relocation\n",
436f9fdeb6SRalf Baechle me->name);
444e6a05feSThiemo Seufer return -ENOEXEC;
454e6a05feSThiemo Seufer }
464e6a05feSThiemo Seufer
474e6a05feSThiemo Seufer if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
4855d791f3SSteven J. Hill pr_err("module %s: relocation overflow\n",
494e6a05feSThiemo Seufer me->name);
504e6a05feSThiemo Seufer return -ENOEXEC;
514e6a05feSThiemo Seufer }
524e6a05feSThiemo Seufer
534e6a05feSThiemo Seufer *location = (*location & ~0x03ffffff) |
54430d0b88SPaul Burton ((base + (v >> 2)) & 0x03ffffff);
554e6a05feSThiemo Seufer
564e6a05feSThiemo Seufer return 0;
574e6a05feSThiemo Seufer }
584e6a05feSThiemo Seufer
apply_r_mips_hi16(struct module * me,u32 * location,Elf_Addr v,bool rela)59049a68efSAlexander Lobakin static int apply_r_mips_hi16(struct module *me, u32 *location, Elf_Addr v,
60049a68efSAlexander Lobakin bool rela)
614e6a05feSThiemo Seufer {
624e6a05feSThiemo Seufer struct mips_hi16 *n;
634e6a05feSThiemo Seufer
64430d0b88SPaul Burton if (rela) {
65430d0b88SPaul Burton *location = (*location & 0xffff0000) |
66430d0b88SPaul Burton ((((long long) v + 0x8000LL) >> 16) & 0xffff);
67430d0b88SPaul Burton return 0;
68430d0b88SPaul Burton }
69430d0b88SPaul Burton
704e6a05feSThiemo Seufer /*
714e6a05feSThiemo Seufer * We cannot relocate this one now because we don't know the value of
724e6a05feSThiemo Seufer * the carry we need to add. Save the information, and let LO16 do the
734e6a05feSThiemo Seufer * actual relocation.
744e6a05feSThiemo Seufer */
754e6a05feSThiemo Seufer n = kmalloc(sizeof *n, GFP_KERNEL);
764e6a05feSThiemo Seufer if (!n)
774e6a05feSThiemo Seufer return -ENOMEM;
784e6a05feSThiemo Seufer
794e6a05feSThiemo Seufer n->addr = (Elf_Addr *)location;
804e6a05feSThiemo Seufer n->value = v;
81861667dcSRalf Baechle n->next = me->arch.r_mips_hi16_list;
82861667dcSRalf Baechle me->arch.r_mips_hi16_list = n;
834e6a05feSThiemo Seufer
844e6a05feSThiemo Seufer return 0;
854e6a05feSThiemo Seufer }
864e6a05feSThiemo Seufer
free_relocation_chain(struct mips_hi16 * l)87c54de490SRalf Baechle static void free_relocation_chain(struct mips_hi16 *l)
88c54de490SRalf Baechle {
89c54de490SRalf Baechle struct mips_hi16 *next;
90c54de490SRalf Baechle
91c54de490SRalf Baechle while (l) {
92c54de490SRalf Baechle next = l->next;
93c54de490SRalf Baechle kfree(l);
94c54de490SRalf Baechle l = next;
95c54de490SRalf Baechle }
96c54de490SRalf Baechle }
97c54de490SRalf Baechle
apply_r_mips_lo16(struct module * me,u32 * location,u32 base,Elf_Addr v,bool rela)98430d0b88SPaul Burton static int apply_r_mips_lo16(struct module *me, u32 *location,
99430d0b88SPaul Burton u32 base, Elf_Addr v, bool rela)
1004e6a05feSThiemo Seufer {
101430d0b88SPaul Burton unsigned long insnlo = base;
102c54de490SRalf Baechle struct mips_hi16 *l;
1034e6a05feSThiemo Seufer Elf_Addr val, vallo;
1044e6a05feSThiemo Seufer
105430d0b88SPaul Burton if (rela) {
106430d0b88SPaul Burton *location = (*location & 0xffff0000) | (v & 0xffff);
107430d0b88SPaul Burton return 0;
108430d0b88SPaul Burton }
109430d0b88SPaul Burton
1104e6a05feSThiemo Seufer /* Sign extend the addend we extract from the lo insn. */
1114e6a05feSThiemo Seufer vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
1124e6a05feSThiemo Seufer
113861667dcSRalf Baechle if (me->arch.r_mips_hi16_list != NULL) {
114861667dcSRalf Baechle l = me->arch.r_mips_hi16_list;
1154e6a05feSThiemo Seufer while (l != NULL) {
116c54de490SRalf Baechle struct mips_hi16 *next;
1174e6a05feSThiemo Seufer unsigned long insn;
1184e6a05feSThiemo Seufer
1194e6a05feSThiemo Seufer /*
1204e6a05feSThiemo Seufer * The value for the HI16 had best be the same.
1214e6a05feSThiemo Seufer */
1224e6a05feSThiemo Seufer if (v != l->value)
1234e6a05feSThiemo Seufer goto out_danger;
1244e6a05feSThiemo Seufer
1254e6a05feSThiemo Seufer /*
1264e6a05feSThiemo Seufer * Do the HI16 relocation. Note that we actually don't
1274e6a05feSThiemo Seufer * need to know anything about the LO16 itself, except
1284e6a05feSThiemo Seufer * where to find the low 16 bits of the addend needed
1294e6a05feSThiemo Seufer * by the LO16.
1304e6a05feSThiemo Seufer */
1314e6a05feSThiemo Seufer insn = *l->addr;
1324e6a05feSThiemo Seufer val = ((insn & 0xffff) << 16) + vallo;
1334e6a05feSThiemo Seufer val += v;
1344e6a05feSThiemo Seufer
1354e6a05feSThiemo Seufer /*
1364e6a05feSThiemo Seufer * Account for the sign extension that will happen in
1374e6a05feSThiemo Seufer * the low bits.
1384e6a05feSThiemo Seufer */
1394e6a05feSThiemo Seufer val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
1404e6a05feSThiemo Seufer
1414e6a05feSThiemo Seufer insn = (insn & ~0xffff) | val;
1424e6a05feSThiemo Seufer *l->addr = insn;
1434e6a05feSThiemo Seufer
1444e6a05feSThiemo Seufer next = l->next;
1454e6a05feSThiemo Seufer kfree(l);
1464e6a05feSThiemo Seufer l = next;
1474e6a05feSThiemo Seufer }
1484e6a05feSThiemo Seufer
149861667dcSRalf Baechle me->arch.r_mips_hi16_list = NULL;
1504e6a05feSThiemo Seufer }
1514e6a05feSThiemo Seufer
1524e6a05feSThiemo Seufer /*
1534e6a05feSThiemo Seufer * Ok, we're done with the HI16 relocs. Now deal with the LO16.
1544e6a05feSThiemo Seufer */
1554e6a05feSThiemo Seufer val = v + vallo;
1564e6a05feSThiemo Seufer insnlo = (insnlo & ~0xffff) | (val & 0xffff);
1574e6a05feSThiemo Seufer *location = insnlo;
1584e6a05feSThiemo Seufer
1594e6a05feSThiemo Seufer return 0;
1604e6a05feSThiemo Seufer
1614e6a05feSThiemo Seufer out_danger:
162c54de490SRalf Baechle free_relocation_chain(l);
163c54de490SRalf Baechle me->arch.r_mips_hi16_list = NULL;
164d3cac35cSRalf Baechle
165430d0b88SPaul Burton pr_err("module %s: dangerous R_MIPS_LO16 relocation\n", me->name);
1664e6a05feSThiemo Seufer
1674e6a05feSThiemo Seufer return -ENOEXEC;
1684e6a05feSThiemo Seufer }
1694e6a05feSThiemo Seufer
apply_r_mips_pc(struct module * me,u32 * location,u32 base,Elf_Addr v,unsigned int bits)170430d0b88SPaul Burton static int apply_r_mips_pc(struct module *me, u32 *location, u32 base,
171430d0b88SPaul Burton Elf_Addr v, unsigned int bits)
1725d3c7925SPaul Burton {
1735d3c7925SPaul Burton unsigned long mask = GENMASK(bits - 1, 0);
1745d3c7925SPaul Burton unsigned long se_bits;
1755d3c7925SPaul Burton long offset;
1765d3c7925SPaul Burton
1775d3c7925SPaul Burton if (v % 4) {
178430d0b88SPaul Burton pr_err("module %s: dangerous R_MIPS_PC%u relocation\n",
1795d3c7925SPaul Burton me->name, bits);
1805d3c7925SPaul Burton return -ENOEXEC;
1815d3c7925SPaul Burton }
1825d3c7925SPaul Burton
183430d0b88SPaul Burton /* retrieve & sign extend implicit addend if any */
184430d0b88SPaul Burton offset = base & mask;
1855d3c7925SPaul Burton offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
1865d3c7925SPaul Burton
1875d3c7925SPaul Burton offset += ((long)v - (long)location) >> 2;
1885d3c7925SPaul Burton
1895d3c7925SPaul Burton /* check the sign bit onwards are identical - ie. we didn't overflow */
1905d3c7925SPaul Burton se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
1915d3c7925SPaul Burton if ((offset & ~mask) != (se_bits & ~mask)) {
1925d3c7925SPaul Burton pr_err("module %s: relocation overflow\n", me->name);
1935d3c7925SPaul Burton return -ENOEXEC;
1945d3c7925SPaul Burton }
1955d3c7925SPaul Burton
1965d3c7925SPaul Burton *location = (*location & ~mask) | (offset & mask);
1975d3c7925SPaul Burton
1985d3c7925SPaul Burton return 0;
1995d3c7925SPaul Burton }
2005d3c7925SPaul Burton
apply_r_mips_pc16(struct module * me,u32 * location,u32 base,Elf_Addr v)201049a68efSAlexander Lobakin static int apply_r_mips_pc16(struct module *me, u32 *location, u32 base,
202049a68efSAlexander Lobakin Elf_Addr v)
2035d3c7925SPaul Burton {
204430d0b88SPaul Burton return apply_r_mips_pc(me, location, base, v, 16);
2055d3c7925SPaul Burton }
2065d3c7925SPaul Burton
apply_r_mips_pc21(struct module * me,u32 * location,u32 base,Elf_Addr v)207049a68efSAlexander Lobakin static int apply_r_mips_pc21(struct module *me, u32 *location, u32 base,
208049a68efSAlexander Lobakin Elf_Addr v)
2095d3c7925SPaul Burton {
210430d0b88SPaul Burton return apply_r_mips_pc(me, location, base, v, 21);
2115d3c7925SPaul Burton }
2125d3c7925SPaul Burton
apply_r_mips_pc26(struct module * me,u32 * location,u32 base,Elf_Addr v)213049a68efSAlexander Lobakin static int apply_r_mips_pc26(struct module *me, u32 *location, u32 base,
214049a68efSAlexander Lobakin Elf_Addr v)
2155d3c7925SPaul Burton {
216430d0b88SPaul Burton return apply_r_mips_pc(me, location, base, v, 26);
2175d3c7925SPaul Burton }
2185d3c7925SPaul Burton
apply_r_mips_64(u32 * location,Elf_Addr v,bool rela)219049a68efSAlexander Lobakin static int apply_r_mips_64(u32 *location, Elf_Addr v, bool rela)
220430d0b88SPaul Burton {
221430d0b88SPaul Burton if (WARN_ON(!rela))
222430d0b88SPaul Burton return -EINVAL;
223430d0b88SPaul Burton
224430d0b88SPaul Burton *(Elf_Addr *)location = v;
225430d0b88SPaul Burton
226430d0b88SPaul Burton return 0;
227430d0b88SPaul Burton }
228430d0b88SPaul Burton
apply_r_mips_higher(u32 * location,Elf_Addr v,bool rela)229049a68efSAlexander Lobakin static int apply_r_mips_higher(u32 *location, Elf_Addr v, bool rela)
230430d0b88SPaul Burton {
231430d0b88SPaul Burton if (WARN_ON(!rela))
232430d0b88SPaul Burton return -EINVAL;
233430d0b88SPaul Burton
234430d0b88SPaul Burton *location = (*location & 0xffff0000) |
235430d0b88SPaul Burton ((((long long)v + 0x80008000LL) >> 32) & 0xffff);
236430d0b88SPaul Burton
237430d0b88SPaul Burton return 0;
238430d0b88SPaul Burton }
239430d0b88SPaul Burton
apply_r_mips_highest(u32 * location,Elf_Addr v,bool rela)240049a68efSAlexander Lobakin static int apply_r_mips_highest(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 + 0x800080008000LL) >> 48) & 0xffff);
247430d0b88SPaul Burton
248430d0b88SPaul Burton return 0;
249430d0b88SPaul Burton }
250430d0b88SPaul Burton
251430d0b88SPaul Burton /**
252430d0b88SPaul Burton * reloc_handler() - Apply a particular relocation to a module
253049a68efSAlexander Lobakin * @type: type of the relocation to apply
254430d0b88SPaul Burton * @me: the module to apply the reloc to
255430d0b88SPaul Burton * @location: the address at which the reloc is to be applied
256430d0b88SPaul Burton * @base: the existing value at location for REL-style; 0 for RELA-style
257430d0b88SPaul Burton * @v: the value of the reloc, with addend for RELA-style
258049a68efSAlexander Lobakin * @rela: indication of is this a RELA (true) or REL (false) relocation
259430d0b88SPaul Burton *
260049a68efSAlexander Lobakin * Each implemented relocation function applies a particular type of
261430d0b88SPaul Burton * relocation to the module @me. Relocs that may be found in either REL or RELA
262430d0b88SPaul Burton * variants can be handled by making use of the @base & @v parameters which are
263430d0b88SPaul Burton * set to values which abstract the difference away from the particular reloc
264430d0b88SPaul Burton * implementations.
265430d0b88SPaul Burton *
266430d0b88SPaul Burton * Return: 0 upon success, else -ERRNO
267430d0b88SPaul Burton */
reloc_handler(u32 type,struct module * me,u32 * location,u32 base,Elf_Addr v,bool rela)268049a68efSAlexander Lobakin static int reloc_handler(u32 type, struct module *me, u32 *location, u32 base,
269049a68efSAlexander Lobakin Elf_Addr v, bool rela)
270049a68efSAlexander Lobakin {
271049a68efSAlexander Lobakin switch (type) {
272049a68efSAlexander Lobakin case R_MIPS_NONE:
273049a68efSAlexander Lobakin break;
274049a68efSAlexander Lobakin case R_MIPS_32:
275049a68efSAlexander Lobakin apply_r_mips_32(location, base, v);
276049a68efSAlexander Lobakin break;
277049a68efSAlexander Lobakin case R_MIPS_26:
278049a68efSAlexander Lobakin return apply_r_mips_26(me, location, base, v);
279049a68efSAlexander Lobakin case R_MIPS_HI16:
280049a68efSAlexander Lobakin return apply_r_mips_hi16(me, location, v, rela);
281049a68efSAlexander Lobakin case R_MIPS_LO16:
282049a68efSAlexander Lobakin return apply_r_mips_lo16(me, location, base, v, rela);
283049a68efSAlexander Lobakin case R_MIPS_PC16:
284049a68efSAlexander Lobakin return apply_r_mips_pc16(me, location, base, v);
285049a68efSAlexander Lobakin case R_MIPS_PC21_S2:
286049a68efSAlexander Lobakin return apply_r_mips_pc21(me, location, base, v);
287049a68efSAlexander Lobakin case R_MIPS_PC26_S2:
288049a68efSAlexander Lobakin return apply_r_mips_pc26(me, location, base, v);
289049a68efSAlexander Lobakin case R_MIPS_64:
290049a68efSAlexander Lobakin return apply_r_mips_64(location, v, rela);
291049a68efSAlexander Lobakin case R_MIPS_HIGHER:
292049a68efSAlexander Lobakin return apply_r_mips_higher(location, v, rela);
293049a68efSAlexander Lobakin case R_MIPS_HIGHEST:
294049a68efSAlexander Lobakin return apply_r_mips_highest(location, v, rela);
295049a68efSAlexander Lobakin default:
296049a68efSAlexander Lobakin pr_err("%s: Unknown relocation type %u\n", me->name, type);
297049a68efSAlexander Lobakin return -EINVAL;
298049a68efSAlexander Lobakin }
299430d0b88SPaul Burton
300049a68efSAlexander Lobakin return 0;
301049a68efSAlexander Lobakin }
3024e6a05feSThiemo Seufer
__apply_relocate(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me,bool rela)303430d0b88SPaul Burton static int __apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
3044e6a05feSThiemo Seufer unsigned int symindex, unsigned int relsec,
305430d0b88SPaul Burton struct module *me, bool rela)
3064e6a05feSThiemo Seufer {
307430d0b88SPaul Burton union {
308430d0b88SPaul Burton Elf_Mips_Rel *rel;
309430d0b88SPaul Burton Elf_Mips_Rela *rela;
310430d0b88SPaul Burton } r;
3114e6a05feSThiemo Seufer Elf_Sym *sym;
312430d0b88SPaul Burton u32 *location, base;
31304211a57SPaul Burton unsigned int i, type;
3144e6a05feSThiemo Seufer Elf_Addr v;
315351b0940SPaul Burton int err = 0;
316430d0b88SPaul Burton size_t reloc_sz;
3174e6a05feSThiemo Seufer
3184e6a05feSThiemo Seufer pr_debug("Applying relocate section %u to %u\n", relsec,
3194e6a05feSThiemo Seufer sechdrs[relsec].sh_info);
3204e6a05feSThiemo Seufer
321430d0b88SPaul Burton r.rel = (void *)sechdrs[relsec].sh_addr;
322430d0b88SPaul Burton reloc_sz = rela ? sizeof(*r.rela) : sizeof(*r.rel);
323861667dcSRalf Baechle me->arch.r_mips_hi16_list = NULL;
324430d0b88SPaul Burton for (i = 0; i < sechdrs[relsec].sh_size / reloc_sz; i++) {
3254e6a05feSThiemo Seufer /* This is where to make the change */
3264e6a05feSThiemo Seufer location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
327430d0b88SPaul Burton + r.rel->r_offset;
3284e6a05feSThiemo Seufer /* This is the symbol it is referring to */
3294e6a05feSThiemo Seufer sym = (Elf_Sym *)sechdrs[symindex].sh_addr
330430d0b88SPaul Burton + ELF_MIPS_R_SYM(*r.rel);
331ba837d38SAndrzej Hajda if (sym->st_value >= -MAX_ERRNO) {
332f3bf07b9SAtsushi Nemoto /* Ignore unresolved weak symbol */
333f3bf07b9SAtsushi Nemoto if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
334f3bf07b9SAtsushi Nemoto continue;
33555d791f3SSteven J. Hill pr_warn("%s: Unknown symbol %s\n",
3364e6a05feSThiemo Seufer me->name, strtab + sym->st_name);
337351b0940SPaul Burton err = -ENOENT;
338351b0940SPaul Burton goto out;
3394e6a05feSThiemo Seufer }
3404e6a05feSThiemo Seufer
341430d0b88SPaul Burton type = ELF_MIPS_R_TYPE(*r.rel);
34204211a57SPaul Burton
343430d0b88SPaul Burton if (rela) {
344430d0b88SPaul Burton v = sym->st_value + r.rela->r_addend;
345430d0b88SPaul Burton base = 0;
346430d0b88SPaul Burton r.rela = &r.rela[1];
347430d0b88SPaul Burton } else {
34804211a57SPaul Burton v = sym->st_value;
349430d0b88SPaul Burton base = *location;
350430d0b88SPaul Burton r.rel = &r.rel[1];
3514e6a05feSThiemo Seufer }
3524e6a05feSThiemo Seufer
353049a68efSAlexander Lobakin err = reloc_handler(type, me, location, base, v, rela);
354351b0940SPaul Burton if (err)
355351b0940SPaul Burton goto out;
3564e6a05feSThiemo Seufer }
3574e6a05feSThiemo Seufer
358351b0940SPaul Burton out:
359c54de490SRalf Baechle /*
360c54de490SRalf Baechle * Normally the hi16 list should be deallocated at this point. A
361c54de490SRalf Baechle * malformed binary however could contain a series of R_MIPS_HI16
362351b0940SPaul Burton * relocations not followed by a R_MIPS_LO16 relocation, or if we hit
363351b0940SPaul Burton * an error processing a reloc we might have gotten here before
364351b0940SPaul Burton * reaching the R_MIPS_LO16. In either case, free up the list and
365351b0940SPaul Burton * return an error.
366c54de490SRalf Baechle */
367c54de490SRalf Baechle if (me->arch.r_mips_hi16_list) {
368c54de490SRalf Baechle free_relocation_chain(me->arch.r_mips_hi16_list);
369c54de490SRalf Baechle me->arch.r_mips_hi16_list = NULL;
370351b0940SPaul Burton err = err ?: -ENOEXEC;
371c54de490SRalf Baechle }
372c54de490SRalf Baechle
373351b0940SPaul Burton return err;
3744e6a05feSThiemo Seufer }
3754e6a05feSThiemo Seufer
apply_relocate(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)376430d0b88SPaul Burton int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
377430d0b88SPaul Burton unsigned int symindex, unsigned int relsec,
378430d0b88SPaul Burton struct module *me)
379430d0b88SPaul Burton {
380430d0b88SPaul Burton return __apply_relocate(sechdrs, strtab, symindex, relsec, me, false);
381430d0b88SPaul Burton }
382430d0b88SPaul Burton
383430d0b88SPaul 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)384430d0b88SPaul Burton int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
385430d0b88SPaul Burton unsigned int symindex, unsigned int relsec,
386430d0b88SPaul Burton struct module *me)
387430d0b88SPaul Burton {
388430d0b88SPaul Burton return __apply_relocate(sechdrs, strtab, symindex, relsec, me, true);
389430d0b88SPaul Burton }
390430d0b88SPaul Burton #endif /* CONFIG_MODULES_USE_ELF_RELA */
391430d0b88SPaul Burton
3921da177e4SLinus Torvalds /* Given an address, look for it in the module exception tables. */
search_module_dbetables(unsigned long addr)3931da177e4SLinus Torvalds const struct exception_table_entry *search_module_dbetables(unsigned long addr)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds unsigned long flags;
3961da177e4SLinus Torvalds const struct exception_table_entry *e = NULL;
3971da177e4SLinus Torvalds struct mod_arch_specific *dbe;
3981da177e4SLinus Torvalds
3991da177e4SLinus Torvalds spin_lock_irqsave(&dbe_lock, flags);
4001da177e4SLinus Torvalds list_for_each_entry(dbe, &dbe_list, dbe_list) {
401a94c33ddSThomas Meyer e = search_extable(dbe->dbe_start,
402a94c33ddSThomas Meyer dbe->dbe_end - dbe->dbe_start, addr);
4031da177e4SLinus Torvalds if (e)
4041da177e4SLinus Torvalds break;
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds spin_unlock_irqrestore(&dbe_lock, flags);
4071da177e4SLinus Torvalds
4081da177e4SLinus Torvalds /* Now, if we found one, we are running inside it now, hence
4091da177e4SLinus Torvalds we cannot unload the module, hence no refcnt needed. */
4101da177e4SLinus Torvalds return e;
4111da177e4SLinus Torvalds }
4121da177e4SLinus Torvalds
4133a4fa0a2SRobert P. J. Day /* Put in dbe list if necessary. */
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)4141da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr,
4151da177e4SLinus Torvalds const Elf_Shdr *sechdrs,
4161da177e4SLinus Torvalds struct module *me)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds const Elf_Shdr *s;
4191da177e4SLinus Torvalds char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
4201da177e4SLinus Torvalds
421fdfd4289SArd Biesheuvel if (IS_ENABLED(CONFIG_JUMP_LABEL))
42294bb0c1aSDavid Daney jump_label_apply_nops(me);
42394bb0c1aSDavid Daney
4241da177e4SLinus Torvalds INIT_LIST_HEAD(&me->arch.dbe_list);
4251da177e4SLinus Torvalds for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
4261da177e4SLinus Torvalds if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
4271da177e4SLinus Torvalds continue;
4281da177e4SLinus Torvalds me->arch.dbe_start = (void *)s->sh_addr;
4291da177e4SLinus Torvalds me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
4301da177e4SLinus Torvalds spin_lock_irq(&dbe_lock);
4311da177e4SLinus Torvalds list_add(&me->arch.dbe_list, &dbe_list);
4321da177e4SLinus Torvalds spin_unlock_irq(&dbe_lock);
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds return 0;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds
module_arch_cleanup(struct module * mod)4371da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds spin_lock_irq(&dbe_lock);
4401da177e4SLinus Torvalds list_del(&mod->arch.dbe_list);
4411da177e4SLinus Torvalds spin_unlock_irq(&dbe_lock);
4421da177e4SLinus Torvalds }
443