Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *
0004  *  Copyright (C) 2001 Rusty Russell.
0005  *  Copyright (C) 2003, 2004 Ralf Baechle (ralf@linux-mips.org)
0006  *  Copyright (C) 2005 Thiemo Seufer
0007  */
0008 
0009 #undef DEBUG
0010 
0011 #include <linux/extable.h>
0012 #include <linux/moduleloader.h>
0013 #include <linux/elf.h>
0014 #include <linux/mm.h>
0015 #include <linux/numa.h>
0016 #include <linux/vmalloc.h>
0017 #include <linux/slab.h>
0018 #include <linux/fs.h>
0019 #include <linux/string.h>
0020 #include <linux/kernel.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/jump_label.h>
0023 
0024 extern void jump_label_apply_nops(struct module *mod);
0025 
0026 struct mips_hi16 {
0027     struct mips_hi16 *next;
0028     Elf_Addr *addr;
0029     Elf_Addr value;
0030 };
0031 
0032 static LIST_HEAD(dbe_list);
0033 static DEFINE_SPINLOCK(dbe_lock);
0034 
0035 #ifdef MODULE_START
0036 void *module_alloc(unsigned long size)
0037 {
0038     return __vmalloc_node_range(size, 1, MODULE_START, MODULE_END,
0039                 GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE,
0040                 __builtin_return_address(0));
0041 }
0042 #endif
0043 
0044 static void apply_r_mips_32(u32 *location, u32 base, Elf_Addr v)
0045 {
0046     *location = base + v;
0047 }
0048 
0049 static int apply_r_mips_26(struct module *me, u32 *location, u32 base,
0050                Elf_Addr v)
0051 {
0052     if (v % 4) {
0053         pr_err("module %s: dangerous R_MIPS_26 relocation\n",
0054                me->name);
0055         return -ENOEXEC;
0056     }
0057 
0058     if ((v & 0xf0000000) != (((unsigned long)location + 4) & 0xf0000000)) {
0059         pr_err("module %s: relocation overflow\n",
0060                me->name);
0061         return -ENOEXEC;
0062     }
0063 
0064     *location = (*location & ~0x03ffffff) |
0065             ((base + (v >> 2)) & 0x03ffffff);
0066 
0067     return 0;
0068 }
0069 
0070 static int apply_r_mips_hi16(struct module *me, u32 *location, Elf_Addr v,
0071                  bool rela)
0072 {
0073     struct mips_hi16 *n;
0074 
0075     if (rela) {
0076         *location = (*location & 0xffff0000) |
0077                 ((((long long) v + 0x8000LL) >> 16) & 0xffff);
0078         return 0;
0079     }
0080 
0081     /*
0082      * We cannot relocate this one now because we don't know the value of
0083      * the carry we need to add.  Save the information, and let LO16 do the
0084      * actual relocation.
0085      */
0086     n = kmalloc(sizeof *n, GFP_KERNEL);
0087     if (!n)
0088         return -ENOMEM;
0089 
0090     n->addr = (Elf_Addr *)location;
0091     n->value = v;
0092     n->next = me->arch.r_mips_hi16_list;
0093     me->arch.r_mips_hi16_list = n;
0094 
0095     return 0;
0096 }
0097 
0098 static void free_relocation_chain(struct mips_hi16 *l)
0099 {
0100     struct mips_hi16 *next;
0101 
0102     while (l) {
0103         next = l->next;
0104         kfree(l);
0105         l = next;
0106     }
0107 }
0108 
0109 static int apply_r_mips_lo16(struct module *me, u32 *location,
0110                  u32 base, Elf_Addr v, bool rela)
0111 {
0112     unsigned long insnlo = base;
0113     struct mips_hi16 *l;
0114     Elf_Addr val, vallo;
0115 
0116     if (rela) {
0117         *location = (*location & 0xffff0000) | (v & 0xffff);
0118         return 0;
0119     }
0120 
0121     /* Sign extend the addend we extract from the lo insn.  */
0122     vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
0123 
0124     if (me->arch.r_mips_hi16_list != NULL) {
0125         l = me->arch.r_mips_hi16_list;
0126         while (l != NULL) {
0127             struct mips_hi16 *next;
0128             unsigned long insn;
0129 
0130             /*
0131              * The value for the HI16 had best be the same.
0132              */
0133             if (v != l->value)
0134                 goto out_danger;
0135 
0136             /*
0137              * Do the HI16 relocation.  Note that we actually don't
0138              * need to know anything about the LO16 itself, except
0139              * where to find the low 16 bits of the addend needed
0140              * by the LO16.
0141              */
0142             insn = *l->addr;
0143             val = ((insn & 0xffff) << 16) + vallo;
0144             val += v;
0145 
0146             /*
0147              * Account for the sign extension that will happen in
0148              * the low bits.
0149              */
0150             val = ((val >> 16) + ((val & 0x8000) != 0)) & 0xffff;
0151 
0152             insn = (insn & ~0xffff) | val;
0153             *l->addr = insn;
0154 
0155             next = l->next;
0156             kfree(l);
0157             l = next;
0158         }
0159 
0160         me->arch.r_mips_hi16_list = NULL;
0161     }
0162 
0163     /*
0164      * Ok, we're done with the HI16 relocs.  Now deal with the LO16.
0165      */
0166     val = v + vallo;
0167     insnlo = (insnlo & ~0xffff) | (val & 0xffff);
0168     *location = insnlo;
0169 
0170     return 0;
0171 
0172 out_danger:
0173     free_relocation_chain(l);
0174     me->arch.r_mips_hi16_list = NULL;
0175 
0176     pr_err("module %s: dangerous R_MIPS_LO16 relocation\n", me->name);
0177 
0178     return -ENOEXEC;
0179 }
0180 
0181 static int apply_r_mips_pc(struct module *me, u32 *location, u32 base,
0182                Elf_Addr v, unsigned int bits)
0183 {
0184     unsigned long mask = GENMASK(bits - 1, 0);
0185     unsigned long se_bits;
0186     long offset;
0187 
0188     if (v % 4) {
0189         pr_err("module %s: dangerous R_MIPS_PC%u relocation\n",
0190                me->name, bits);
0191         return -ENOEXEC;
0192     }
0193 
0194     /* retrieve & sign extend implicit addend if any */
0195     offset = base & mask;
0196     offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
0197 
0198     offset += ((long)v - (long)location) >> 2;
0199 
0200     /* check the sign bit onwards are identical - ie. we didn't overflow */
0201     se_bits = (offset & BIT(bits - 1)) ? ~0ul : 0;
0202     if ((offset & ~mask) != (se_bits & ~mask)) {
0203         pr_err("module %s: relocation overflow\n", me->name);
0204         return -ENOEXEC;
0205     }
0206 
0207     *location = (*location & ~mask) | (offset & mask);
0208 
0209     return 0;
0210 }
0211 
0212 static int apply_r_mips_pc16(struct module *me, u32 *location, u32 base,
0213                  Elf_Addr v)
0214 {
0215     return apply_r_mips_pc(me, location, base, v, 16);
0216 }
0217 
0218 static int apply_r_mips_pc21(struct module *me, u32 *location, u32 base,
0219                  Elf_Addr v)
0220 {
0221     return apply_r_mips_pc(me, location, base, v, 21);
0222 }
0223 
0224 static int apply_r_mips_pc26(struct module *me, u32 *location, u32 base,
0225                  Elf_Addr v)
0226 {
0227     return apply_r_mips_pc(me, location, base, v, 26);
0228 }
0229 
0230 static int apply_r_mips_64(u32 *location, Elf_Addr v, bool rela)
0231 {
0232     if (WARN_ON(!rela))
0233         return -EINVAL;
0234 
0235     *(Elf_Addr *)location = v;
0236 
0237     return 0;
0238 }
0239 
0240 static int apply_r_mips_higher(u32 *location, Elf_Addr v, bool rela)
0241 {
0242     if (WARN_ON(!rela))
0243         return -EINVAL;
0244 
0245     *location = (*location & 0xffff0000) |
0246             ((((long long)v + 0x80008000LL) >> 32) & 0xffff);
0247 
0248     return 0;
0249 }
0250 
0251 static int apply_r_mips_highest(u32 *location, Elf_Addr v, bool rela)
0252 {
0253     if (WARN_ON(!rela))
0254         return -EINVAL;
0255 
0256     *location = (*location & 0xffff0000) |
0257             ((((long long)v + 0x800080008000LL) >> 48) & 0xffff);
0258 
0259     return 0;
0260 }
0261 
0262 /**
0263  * reloc_handler() - Apply a particular relocation to a module
0264  * @type: type of the relocation to apply
0265  * @me: the module to apply the reloc to
0266  * @location: the address at which the reloc is to be applied
0267  * @base: the existing value at location for REL-style; 0 for RELA-style
0268  * @v: the value of the reloc, with addend for RELA-style
0269  * @rela: indication of is this a RELA (true) or REL (false) relocation
0270  *
0271  * Each implemented relocation function applies a particular type of
0272  * relocation to the module @me. Relocs that may be found in either REL or RELA
0273  * variants can be handled by making use of the @base & @v parameters which are
0274  * set to values which abstract the difference away from the particular reloc
0275  * implementations.
0276  *
0277  * Return: 0 upon success, else -ERRNO
0278  */
0279 static int reloc_handler(u32 type, struct module *me, u32 *location, u32 base,
0280              Elf_Addr v, bool rela)
0281 {
0282     switch (type) {
0283     case R_MIPS_NONE:
0284         break;
0285     case R_MIPS_32:
0286         apply_r_mips_32(location, base, v);
0287         break;
0288     case R_MIPS_26:
0289         return apply_r_mips_26(me, location, base, v);
0290     case R_MIPS_HI16:
0291         return apply_r_mips_hi16(me, location, v, rela);
0292     case R_MIPS_LO16:
0293         return apply_r_mips_lo16(me, location, base, v, rela);
0294     case R_MIPS_PC16:
0295         return apply_r_mips_pc16(me, location, base, v);
0296     case R_MIPS_PC21_S2:
0297         return apply_r_mips_pc21(me, location, base, v);
0298     case R_MIPS_PC26_S2:
0299         return apply_r_mips_pc26(me, location, base, v);
0300     case R_MIPS_64:
0301         return apply_r_mips_64(location, v, rela);
0302     case R_MIPS_HIGHER:
0303         return apply_r_mips_higher(location, v, rela);
0304     case R_MIPS_HIGHEST:
0305         return apply_r_mips_highest(location, v, rela);
0306     default:
0307         pr_err("%s: Unknown relocation type %u\n", me->name, type);
0308         return -EINVAL;
0309     }
0310 
0311     return 0;
0312 }
0313 
0314 static int __apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
0315                 unsigned int symindex, unsigned int relsec,
0316                 struct module *me, bool rela)
0317 {
0318     union {
0319         Elf_Mips_Rel *rel;
0320         Elf_Mips_Rela *rela;
0321     } r;
0322     Elf_Sym *sym;
0323     u32 *location, base;
0324     unsigned int i, type;
0325     Elf_Addr v;
0326     int err = 0;
0327     size_t reloc_sz;
0328 
0329     pr_debug("Applying relocate section %u to %u\n", relsec,
0330            sechdrs[relsec].sh_info);
0331 
0332     r.rel = (void *)sechdrs[relsec].sh_addr;
0333     reloc_sz = rela ? sizeof(*r.rela) : sizeof(*r.rel);
0334     me->arch.r_mips_hi16_list = NULL;
0335     for (i = 0; i < sechdrs[relsec].sh_size / reloc_sz; i++) {
0336         /* This is where to make the change */
0337         location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
0338             + r.rel->r_offset;
0339         /* This is the symbol it is referring to */
0340         sym = (Elf_Sym *)sechdrs[symindex].sh_addr
0341             + ELF_MIPS_R_SYM(*r.rel);
0342         if (sym->st_value >= -MAX_ERRNO) {
0343             /* Ignore unresolved weak symbol */
0344             if (ELF_ST_BIND(sym->st_info) == STB_WEAK)
0345                 continue;
0346             pr_warn("%s: Unknown symbol %s\n",
0347                 me->name, strtab + sym->st_name);
0348             err = -ENOENT;
0349             goto out;
0350         }
0351 
0352         type = ELF_MIPS_R_TYPE(*r.rel);
0353 
0354         if (rela) {
0355             v = sym->st_value + r.rela->r_addend;
0356             base = 0;
0357             r.rela = &r.rela[1];
0358         } else {
0359             v = sym->st_value;
0360             base = *location;
0361             r.rel = &r.rel[1];
0362         }
0363 
0364         err = reloc_handler(type, me, location, base, v, rela);
0365         if (err)
0366             goto out;
0367     }
0368 
0369 out:
0370     /*
0371      * Normally the hi16 list should be deallocated at this point. A
0372      * malformed binary however could contain a series of R_MIPS_HI16
0373      * relocations not followed by a R_MIPS_LO16 relocation, or if we hit
0374      * an error processing a reloc we might have gotten here before
0375      * reaching the R_MIPS_LO16. In either case, free up the list and
0376      * return an error.
0377      */
0378     if (me->arch.r_mips_hi16_list) {
0379         free_relocation_chain(me->arch.r_mips_hi16_list);
0380         me->arch.r_mips_hi16_list = NULL;
0381         err = err ?: -ENOEXEC;
0382     }
0383 
0384     return err;
0385 }
0386 
0387 int apply_relocate(Elf_Shdr *sechdrs, const char *strtab,
0388            unsigned int symindex, unsigned int relsec,
0389            struct module *me)
0390 {
0391     return __apply_relocate(sechdrs, strtab, symindex, relsec, me, false);
0392 }
0393 
0394 #ifdef CONFIG_MODULES_USE_ELF_RELA
0395 int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
0396                unsigned int symindex, unsigned int relsec,
0397                struct module *me)
0398 {
0399     return __apply_relocate(sechdrs, strtab, symindex, relsec, me, true);
0400 }
0401 #endif /* CONFIG_MODULES_USE_ELF_RELA */
0402 
0403 /* Given an address, look for it in the module exception tables. */
0404 const struct exception_table_entry *search_module_dbetables(unsigned long addr)
0405 {
0406     unsigned long flags;
0407     const struct exception_table_entry *e = NULL;
0408     struct mod_arch_specific *dbe;
0409 
0410     spin_lock_irqsave(&dbe_lock, flags);
0411     list_for_each_entry(dbe, &dbe_list, dbe_list) {
0412         e = search_extable(dbe->dbe_start,
0413                    dbe->dbe_end - dbe->dbe_start, addr);
0414         if (e)
0415             break;
0416     }
0417     spin_unlock_irqrestore(&dbe_lock, flags);
0418 
0419     /* Now, if we found one, we are running inside it now, hence
0420        we cannot unload the module, hence no refcnt needed. */
0421     return e;
0422 }
0423 
0424 /* Put in dbe list if necessary. */
0425 int module_finalize(const Elf_Ehdr *hdr,
0426             const Elf_Shdr *sechdrs,
0427             struct module *me)
0428 {
0429     const Elf_Shdr *s;
0430     char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
0431 
0432     if (IS_ENABLED(CONFIG_JUMP_LABEL))
0433         jump_label_apply_nops(me);
0434 
0435     INIT_LIST_HEAD(&me->arch.dbe_list);
0436     for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
0437         if (strcmp("__dbe_table", secstrings + s->sh_name) != 0)
0438             continue;
0439         me->arch.dbe_start = (void *)s->sh_addr;
0440         me->arch.dbe_end = (void *)s->sh_addr + s->sh_size;
0441         spin_lock_irq(&dbe_lock);
0442         list_add(&me->arch.dbe_list, &dbe_list);
0443         spin_unlock_irq(&dbe_lock);
0444     }
0445     return 0;
0446 }
0447 
0448 void module_arch_cleanup(struct module *mod)
0449 {
0450     spin_lock_irq(&dbe_lock);
0451     list_del(&mod->arch.dbe_list);
0452     spin_unlock_irq(&dbe_lock);
0453 }