0001
0002
0003
0004
0005
0006
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
0083
0084
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
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
0132
0133 if (v != l->value)
0134 goto out_danger;
0135
0136
0137
0138
0139
0140
0141
0142 insn = *l->addr;
0143 val = ((insn & 0xffff) << 16) + vallo;
0144 val += v;
0145
0146
0147
0148
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
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
0195 offset = base & mask;
0196 offset |= (offset & BIT(bits - 1)) ? ~mask : 0;
0197
0198 offset += ((long)v - (long)location) >> 2;
0199
0200
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
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
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
0337 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
0338 + r.rel->r_offset;
0339
0340 sym = (Elf_Sym *)sechdrs[symindex].sh_addr
0341 + ELF_MIPS_R_SYM(*r.rel);
0342 if (sym->st_value >= -MAX_ERRNO) {
0343
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
0372
0373
0374
0375
0376
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
0402
0403
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
0420
0421 return e;
0422 }
0423
0424
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 }