18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (C) 2001 Rusty Russell. 58c2ecf20Sopenharmony_ci * Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org) 68c2ecf20Sopenharmony_ci * Copyright (C) 2005 Thiemo Seufer 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ci#undef DEBUG 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/extable.h> 128c2ecf20Sopenharmony_ci#include <linux/moduleloader.h> 138c2ecf20Sopenharmony_ci#include <linux/elf.h> 148c2ecf20Sopenharmony_ci#include <linux/mm.h> 158c2ecf20Sopenharmony_ci#include <linux/numa.h> 168c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 178c2ecf20Sopenharmony_ci#include <linux/slab.h> 188c2ecf20Sopenharmony_ci#include <linux/fs.h> 198c2ecf20Sopenharmony_ci#include <linux/string.h> 208c2ecf20Sopenharmony_ci#include <linux/kernel.h> 218c2ecf20Sopenharmony_ci#include <linux/spinlock.h> 228c2ecf20Sopenharmony_ci#include <linux/jump_label.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_cistruct mips_hi16 { 268c2ecf20Sopenharmony_ci struct mips_hi16 *next; 278c2ecf20Sopenharmony_ci Elf_Addr *addr; 288c2ecf20Sopenharmony_ci Elf_Addr value; 298c2ecf20Sopenharmony_ci}; 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cistatic LIST_HEAD(dbe_list); 328c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(dbe_lock); 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#ifdef MODULE_START 358c2ecf20Sopenharmony_civoid *module_alloc(unsigned long size) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END, 388c2ecf20Sopenharmony_ci GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, 398c2ecf20Sopenharmony_ci __builtin_return_address(0)); 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci#endif 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic int apply_r_mips_none(struct module *me, u32 *location, 448c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci return 0; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_cistatic int apply_r_mips_32(struct module *me, u32 *location, 508c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 518c2ecf20Sopenharmony_ci{ 528c2ecf20Sopenharmony_ci *location = base + v; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci return 0; 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic int apply_r_mips_26(struct module *me, u32 *location, 588c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci if (v % 4) { 618c2ecf20Sopenharmony_ci pr_err("module %s: dangerous R_MIPS_26 relocation\n", 628c2ecf20Sopenharmony_ci me->name); 638c2ecf20Sopenharmony_ci return -ENOEXEC; 648c2ecf20Sopenharmony_ci } 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) { 678c2ecf20Sopenharmony_ci pr_err("module %s: relocation overflow\n", 688c2ecf20Sopenharmony_ci me->name); 698c2ecf20Sopenharmony_ci return -ENOEXEC; 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci *location = (*location & ~0x03ffffff) | 738c2ecf20Sopenharmony_ci ((base + (v >> 2)) & 0x03ffffff); 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci return 0; 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic int apply_r_mips_hi16(struct module *me, u32 *location, 798c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 808c2ecf20Sopenharmony_ci{ 818c2ecf20Sopenharmony_ci struct mips_hi16 *n; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (rela) { 848c2ecf20Sopenharmony_ci *location = (*location & 0xffff0000) | 858c2ecf20Sopenharmony_ci ((((long long) v + 0x8000LL) >> 16) & 0xffff); 868c2ecf20Sopenharmony_ci return 0; 878c2ecf20Sopenharmony_ci } 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci /* 908c2ecf20Sopenharmony_ci * We cannot relocate this one now because we don't know the value of 918c2ecf20Sopenharmony_ci * the carry we need to add. Save the information, and let LO16 do the 928c2ecf20Sopenharmony_ci * actual relocation. 938c2ecf20Sopenharmony_ci */ 948c2ecf20Sopenharmony_ci n = kmalloc(sizeof *n, GFP_KERNEL); 958c2ecf20Sopenharmony_ci if (!n) 968c2ecf20Sopenharmony_ci return -ENOMEM; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci n->addr = (Elf_Addr *)location; 998c2ecf20Sopenharmony_ci n->value = v; 1008c2ecf20Sopenharmony_ci n->next = me->arch.r_mips_hi16_list; 1018c2ecf20Sopenharmony_ci me->arch.r_mips_hi16_list = n; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci return 0; 1048c2ecf20Sopenharmony_ci} 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_cistatic void free_relocation_chain(struct mips_hi16 *l) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci struct mips_hi16 *next; 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci while (l) { 1118c2ecf20Sopenharmony_ci next = l->next; 1128c2ecf20Sopenharmony_ci kfree(l); 1138c2ecf20Sopenharmony_ci l = next; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci} 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_cistatic int apply_r_mips_lo16(struct module *me, u32 *location, 1188c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci unsigned long insnlo = base; 1218c2ecf20Sopenharmony_ci struct mips_hi16 *l; 1228c2ecf20Sopenharmony_ci Elf_Addr val, vallo; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci if (rela) { 1258c2ecf20Sopenharmony_ci *location = (*location & 0xffff0000) | (v & 0xffff); 1268c2ecf20Sopenharmony_ci return 0; 1278c2ecf20Sopenharmony_ci } 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci /* Sign extend the addend we extract from the lo insn. */ 1308c2ecf20Sopenharmony_ci vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (me->arch.r_mips_hi16_list != NULL) { 1338c2ecf20Sopenharmony_ci l = me->arch.r_mips_hi16_list; 1348c2ecf20Sopenharmony_ci while (l != NULL) { 1358c2ecf20Sopenharmony_ci struct mips_hi16 *next; 1368c2ecf20Sopenharmony_ci unsigned long insn; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci /* 1398c2ecf20Sopenharmony_ci * The value for the HI16 had best be the same. 1408c2ecf20Sopenharmony_ci */ 1418c2ecf20Sopenharmony_ci if (v != l->value) 1428c2ecf20Sopenharmony_ci goto out_danger; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci /* 1458c2ecf20Sopenharmony_ci * Do the HI16 relocation. Note that we actually don't 1468c2ecf20Sopenharmony_ci * need to know anything about the LO16 itself, except 1478c2ecf20Sopenharmony_ci * where to find the low 16 bits of the addend needed 1488c2ecf20Sopenharmony_ci * by the LO16. 1498c2ecf20Sopenharmony_ci */ 1508c2ecf20Sopenharmony_ci insn = *l->addr; 1518c2ecf20Sopenharmony_ci val = ((insn & 0xffff) << 16) + vallo; 1528c2ecf20Sopenharmony_ci val += v; 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci /* 1558c2ecf20Sopenharmony_ci * Account for the sign extension that will happen in 1568c2ecf20Sopenharmony_ci * the low bits. 1578c2ecf20Sopenharmony_ci */ 1588c2ecf20Sopenharmony_ci val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci insn = (insn & ~0xffff) | val; 1618c2ecf20Sopenharmony_ci *l->addr = insn; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci next = l->next; 1648c2ecf20Sopenharmony_ci kfree(l); 1658c2ecf20Sopenharmony_ci l = next; 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci me->arch.r_mips_hi16_list = NULL; 1698c2ecf20Sopenharmony_ci } 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci /* 1728c2ecf20Sopenharmony_ci * Ok, we're done with the HI16 relocs. Now deal with the LO16. 1738c2ecf20Sopenharmony_ci */ 1748c2ecf20Sopenharmony_ci val = v + vallo; 1758c2ecf20Sopenharmony_ci insnlo = (insnlo & ~0xffff) | (val & 0xffff); 1768c2ecf20Sopenharmony_ci *location = insnlo; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci return 0; 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ciout_danger: 1818c2ecf20Sopenharmony_ci free_relocation_chain(l); 1828c2ecf20Sopenharmony_ci me->arch.r_mips_hi16_list = NULL; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci pr_err("module %s: dangerous R_MIPS_LO16 relocation\n", me->name); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci return -ENOEXEC; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic int apply_r_mips_pc(struct module *me, u32 *location, u32 base, 1908c2ecf20Sopenharmony_ci Elf_Addr v, unsigned int bits) 1918c2ecf20Sopenharmony_ci{ 1928c2ecf20Sopenharmony_ci unsigned long mask = GENMASK(bits - 1, 0); 1938c2ecf20Sopenharmony_ci unsigned long se_bits; 1948c2ecf20Sopenharmony_ci long offset; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci if (v % 4) { 1978c2ecf20Sopenharmony_ci pr_err("module %s: dangerous R_MIPS_PC%u relocation\n", 1988c2ecf20Sopenharmony_ci me->name, bits); 1998c2ecf20Sopenharmony_ci return -ENOEXEC; 2008c2ecf20Sopenharmony_ci } 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* retrieve & sign extend implicit addend if any */ 2038c2ecf20Sopenharmony_ci offset = base & mask; 2048c2ecf20Sopenharmony_ci offset |= (offset & BIT(bits - 1)) ? ~mask : 0; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci offset += ((long)v - (long)location) >> 2; 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci /* check the sign bit onwards are identical - ie. we didn't overflow */ 2098c2ecf20Sopenharmony_ci se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0; 2108c2ecf20Sopenharmony_ci if ((offset & ~mask) != (se_bits & ~mask)) { 2118c2ecf20Sopenharmony_ci pr_err("module %s: relocation overflow\n", me->name); 2128c2ecf20Sopenharmony_ci return -ENOEXEC; 2138c2ecf20Sopenharmony_ci } 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci *location = (*location & ~mask) | (offset & mask); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci return 0; 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic int apply_r_mips_pc16(struct module *me, u32 *location, 2218c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci return apply_r_mips_pc(me, location, base, v, 16); 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic int apply_r_mips_pc21(struct module *me, u32 *location, 2278c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 2288c2ecf20Sopenharmony_ci{ 2298c2ecf20Sopenharmony_ci return apply_r_mips_pc(me, location, base, v, 21); 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic int apply_r_mips_pc26(struct module *me, u32 *location, 2338c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 2348c2ecf20Sopenharmony_ci{ 2358c2ecf20Sopenharmony_ci return apply_r_mips_pc(me, location, base, v, 26); 2368c2ecf20Sopenharmony_ci} 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_cistatic int apply_r_mips_64(struct module *me, u32 *location, 2398c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 2408c2ecf20Sopenharmony_ci{ 2418c2ecf20Sopenharmony_ci if (WARN_ON(!rela)) 2428c2ecf20Sopenharmony_ci return -EINVAL; 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci *(Elf_Addr *)location = v; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci return 0; 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_cistatic int apply_r_mips_higher(struct module *me, u32 *location, 2508c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci if (WARN_ON(!rela)) 2538c2ecf20Sopenharmony_ci return -EINVAL; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci *location = (*location & 0xffff0000) | 2568c2ecf20Sopenharmony_ci ((((long long)v + 0x80008000LL) >> 32) & 0xffff); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci return 0; 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic int apply_r_mips_highest(struct module *me, u32 *location, 2628c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela) 2638c2ecf20Sopenharmony_ci{ 2648c2ecf20Sopenharmony_ci if (WARN_ON(!rela)) 2658c2ecf20Sopenharmony_ci return -EINVAL; 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci *location = (*location & 0xffff0000) | 2688c2ecf20Sopenharmony_ci ((((long long)v + 0x800080008000LL) >> 48) & 0xffff); 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci return 0; 2718c2ecf20Sopenharmony_ci} 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ci/** 2748c2ecf20Sopenharmony_ci * reloc_handler() - Apply a particular relocation to a module 2758c2ecf20Sopenharmony_ci * @me: the module to apply the reloc to 2768c2ecf20Sopenharmony_ci * @location: the address at which the reloc is to be applied 2778c2ecf20Sopenharmony_ci * @base: the existing value at location for REL-style; 0 for RELA-style 2788c2ecf20Sopenharmony_ci * @v: the value of the reloc, with addend for RELA-style 2798c2ecf20Sopenharmony_ci * 2808c2ecf20Sopenharmony_ci * Each implemented reloc_handler function applies a particular type of 2818c2ecf20Sopenharmony_ci * relocation to the module @me. Relocs that may be found in either REL or RELA 2828c2ecf20Sopenharmony_ci * variants can be handled by making use of the @base & @v parameters which are 2838c2ecf20Sopenharmony_ci * set to values which abstract the difference away from the particular reloc 2848c2ecf20Sopenharmony_ci * implementations. 2858c2ecf20Sopenharmony_ci * 2868c2ecf20Sopenharmony_ci * Return: 0 upon success, else -ERRNO 2878c2ecf20Sopenharmony_ci */ 2888c2ecf20Sopenharmony_citypedef int (*reloc_handler)(struct module *me, u32 *location, 2898c2ecf20Sopenharmony_ci u32 base, Elf_Addr v, bool rela); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci/* The handlers for known reloc types */ 2928c2ecf20Sopenharmony_cistatic reloc_handler reloc_handlers[] = { 2938c2ecf20Sopenharmony_ci [R_MIPS_NONE] = apply_r_mips_none, 2948c2ecf20Sopenharmony_ci [R_MIPS_32] = apply_r_mips_32, 2958c2ecf20Sopenharmony_ci [R_MIPS_26] = apply_r_mips_26, 2968c2ecf20Sopenharmony_ci [R_MIPS_HI16] = apply_r_mips_hi16, 2978c2ecf20Sopenharmony_ci [R_MIPS_LO16] = apply_r_mips_lo16, 2988c2ecf20Sopenharmony_ci [R_MIPS_PC16] = apply_r_mips_pc16, 2998c2ecf20Sopenharmony_ci [R_MIPS_64] = apply_r_mips_64, 3008c2ecf20Sopenharmony_ci [R_MIPS_HIGHER] = apply_r_mips_higher, 3018c2ecf20Sopenharmony_ci [R_MIPS_HIGHEST] = apply_r_mips_highest, 3028c2ecf20Sopenharmony_ci [R_MIPS_PC21_S2] = apply_r_mips_pc21, 3038c2ecf20Sopenharmony_ci [R_MIPS_PC26_S2] = apply_r_mips_pc26, 3048c2ecf20Sopenharmony_ci}; 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cistatic int __apply_relocate(Elf_Shdr *sechdrs, const char *strtab, 3078c2ecf20Sopenharmony_ci unsigned int symindex, unsigned int relsec, 3088c2ecf20Sopenharmony_ci struct module *me, bool rela) 3098c2ecf20Sopenharmony_ci{ 3108c2ecf20Sopenharmony_ci union { 3118c2ecf20Sopenharmony_ci Elf_Mips_Rel *rel; 3128c2ecf20Sopenharmony_ci Elf_Mips_Rela *rela; 3138c2ecf20Sopenharmony_ci } r; 3148c2ecf20Sopenharmony_ci reloc_handler handler; 3158c2ecf20Sopenharmony_ci Elf_Sym *sym; 3168c2ecf20Sopenharmony_ci u32 *location, base; 3178c2ecf20Sopenharmony_ci unsigned int i, type; 3188c2ecf20Sopenharmony_ci Elf_Addr v; 3198c2ecf20Sopenharmony_ci int err = 0; 3208c2ecf20Sopenharmony_ci size_t reloc_sz; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci pr_debug("Applying relocate section %u to %u\n", relsec, 3238c2ecf20Sopenharmony_ci sechdrs[relsec].sh_info); 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci r.rel = (void *)sechdrs[relsec].sh_addr; 3268c2ecf20Sopenharmony_ci reloc_sz = rela ? sizeof(*r.rela) : sizeof(*r.rel); 3278c2ecf20Sopenharmony_ci me->arch.r_mips_hi16_list = NULL; 3288c2ecf20Sopenharmony_ci for (i = 0; i < sechdrs[relsec].sh_size / reloc_sz; i++) { 3298c2ecf20Sopenharmony_ci /* This is where to make the change */ 3308c2ecf20Sopenharmony_ci location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 3318c2ecf20Sopenharmony_ci + r.rel->r_offset; 3328c2ecf20Sopenharmony_ci /* This is the symbol it is referring to */ 3338c2ecf20Sopenharmony_ci sym = (Elf_Sym *)sechdrs[symindex].sh_addr 3348c2ecf20Sopenharmony_ci + ELF_MIPS_R_SYM(*r.rel); 3358c2ecf20Sopenharmony_ci if (sym->st_value >= -MAX_ERRNO) { 3368c2ecf20Sopenharmony_ci /* Ignore unresolved weak symbol */ 3378c2ecf20Sopenharmony_ci if (ELF_ST_BIND(sym->st_info) == STB_WEAK) 3388c2ecf20Sopenharmony_ci continue; 3398c2ecf20Sopenharmony_ci pr_warn("%s: Unknown symbol %s\n", 3408c2ecf20Sopenharmony_ci me->name, strtab + sym->st_name); 3418c2ecf20Sopenharmony_ci err = -ENOENT; 3428c2ecf20Sopenharmony_ci goto out; 3438c2ecf20Sopenharmony_ci } 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci type = ELF_MIPS_R_TYPE(*r.rel); 3468c2ecf20Sopenharmony_ci if (type < ARRAY_SIZE(reloc_handlers)) 3478c2ecf20Sopenharmony_ci handler = reloc_handlers[type]; 3488c2ecf20Sopenharmony_ci else 3498c2ecf20Sopenharmony_ci handler = NULL; 3508c2ecf20Sopenharmony_ci 3518c2ecf20Sopenharmony_ci if (!handler) { 3528c2ecf20Sopenharmony_ci pr_err("%s: Unknown relocation type %u\n", 3538c2ecf20Sopenharmony_ci me->name, type); 3548c2ecf20Sopenharmony_ci err = -EINVAL; 3558c2ecf20Sopenharmony_ci goto out; 3568c2ecf20Sopenharmony_ci } 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci if (rela) { 3598c2ecf20Sopenharmony_ci v = sym->st_value + r.rela->r_addend; 3608c2ecf20Sopenharmony_ci base = 0; 3618c2ecf20Sopenharmony_ci r.rela = &r.rela[1]; 3628c2ecf20Sopenharmony_ci } else { 3638c2ecf20Sopenharmony_ci v = sym->st_value; 3648c2ecf20Sopenharmony_ci base = *location; 3658c2ecf20Sopenharmony_ci r.rel = &r.rel[1]; 3668c2ecf20Sopenharmony_ci } 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ci err = handler(me, location, base, v, rela); 3698c2ecf20Sopenharmony_ci if (err) 3708c2ecf20Sopenharmony_ci goto out; 3718c2ecf20Sopenharmony_ci } 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ciout: 3748c2ecf20Sopenharmony_ci /* 3758c2ecf20Sopenharmony_ci * Normally the hi16 list should be deallocated at this point. A 3768c2ecf20Sopenharmony_ci * malformed binary however could contain a series of R_MIPS_HI16 3778c2ecf20Sopenharmony_ci * relocations not followed by a R_MIPS_LO16 relocation, or if we hit 3788c2ecf20Sopenharmony_ci * an error processing a reloc we might have gotten here before 3798c2ecf20Sopenharmony_ci * reaching the R_MIPS_LO16. In either case, free up the list and 3808c2ecf20Sopenharmony_ci * return an error. 3818c2ecf20Sopenharmony_ci */ 3828c2ecf20Sopenharmony_ci if (me->arch.r_mips_hi16_list) { 3838c2ecf20Sopenharmony_ci free_relocation_chain(me->arch.r_mips_hi16_list); 3848c2ecf20Sopenharmony_ci me->arch.r_mips_hi16_list = NULL; 3858c2ecf20Sopenharmony_ci err = err ?: -ENOEXEC; 3868c2ecf20Sopenharmony_ci } 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci return err; 3898c2ecf20Sopenharmony_ci} 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ciint apply_relocate(Elf_Shdr *sechdrs, const char *strtab, 3928c2ecf20Sopenharmony_ci unsigned int symindex, unsigned int relsec, 3938c2ecf20Sopenharmony_ci struct module *me) 3948c2ecf20Sopenharmony_ci{ 3958c2ecf20Sopenharmony_ci return __apply_relocate(sechdrs, strtab, symindex, relsec, me, false); 3968c2ecf20Sopenharmony_ci} 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci#ifdef CONFIG_MODULES_USE_ELF_RELA 3998c2ecf20Sopenharmony_ciint apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 4008c2ecf20Sopenharmony_ci unsigned int symindex, unsigned int relsec, 4018c2ecf20Sopenharmony_ci struct module *me) 4028c2ecf20Sopenharmony_ci{ 4038c2ecf20Sopenharmony_ci return __apply_relocate(sechdrs, strtab, symindex, relsec, me, true); 4048c2ecf20Sopenharmony_ci} 4058c2ecf20Sopenharmony_ci#endif /* CONFIG_MODULES_USE_ELF_RELA */ 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci/* Given an address, look for it in the module exception tables. */ 4088c2ecf20Sopenharmony_ciconst struct exception_table_entry *search_module_dbetables(unsigned long addr) 4098c2ecf20Sopenharmony_ci{ 4108c2ecf20Sopenharmony_ci unsigned long flags; 4118c2ecf20Sopenharmony_ci const struct exception_table_entry *e = NULL; 4128c2ecf20Sopenharmony_ci struct mod_arch_specific *dbe; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci spin_lock_irqsave(&dbe_lock, flags); 4158c2ecf20Sopenharmony_ci list_for_each_entry(dbe, &dbe_list, dbe_list) { 4168c2ecf20Sopenharmony_ci e = search_extable(dbe->dbe_start, 4178c2ecf20Sopenharmony_ci dbe->dbe_end - dbe->dbe_start, addr); 4188c2ecf20Sopenharmony_ci if (e) 4198c2ecf20Sopenharmony_ci break; 4208c2ecf20Sopenharmony_ci } 4218c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&dbe_lock, flags); 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_ci /* Now, if we found one, we are running inside it now, hence 4248c2ecf20Sopenharmony_ci we cannot unload the module, hence no refcnt needed. */ 4258c2ecf20Sopenharmony_ci return e; 4268c2ecf20Sopenharmony_ci} 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci/* Put in dbe list if necessary. */ 4298c2ecf20Sopenharmony_ciint module_finalize(const Elf_Ehdr *hdr, 4308c2ecf20Sopenharmony_ci const Elf_Shdr *sechdrs, 4318c2ecf20Sopenharmony_ci struct module *me) 4328c2ecf20Sopenharmony_ci{ 4338c2ecf20Sopenharmony_ci const Elf_Shdr *s; 4348c2ecf20Sopenharmony_ci char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci /* Make jump label nops. */ 4378c2ecf20Sopenharmony_ci jump_label_apply_nops(me); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&me->arch.dbe_list); 4408c2ecf20Sopenharmony_ci for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 4418c2ecf20Sopenharmony_ci if (strcmp("__dbe_table", secstrings + s->sh_name) != 0) 4428c2ecf20Sopenharmony_ci continue; 4438c2ecf20Sopenharmony_ci me->arch.dbe_start = (void *)s->sh_addr; 4448c2ecf20Sopenharmony_ci me->arch.dbe_end = (void *)s->sh_addr + s->sh_size; 4458c2ecf20Sopenharmony_ci spin_lock_irq(&dbe_lock); 4468c2ecf20Sopenharmony_ci list_add(&me->arch.dbe_list, &dbe_list); 4478c2ecf20Sopenharmony_ci spin_unlock_irq(&dbe_lock); 4488c2ecf20Sopenharmony_ci } 4498c2ecf20Sopenharmony_ci return 0; 4508c2ecf20Sopenharmony_ci} 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_civoid module_arch_cleanup(struct module *mod) 4538c2ecf20Sopenharmony_ci{ 4548c2ecf20Sopenharmony_ci spin_lock_irq(&dbe_lock); 4558c2ecf20Sopenharmony_ci list_del(&mod->arch.dbe_list); 4568c2ecf20Sopenharmony_ci spin_unlock_irq(&dbe_lock); 4578c2ecf20Sopenharmony_ci} 458