^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Kernel module help for s390.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * S390 version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright IBM Corp. 2002, 2003
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author(s): Arnd Bergmann (arndb@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Martin Schwidefsky (schwidefsky@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * based on i386 version
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (C) 2001 Rusty Russell.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/moduleloader.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/alternative.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/nospec-branch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/facility.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define DEBUGP printk
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define DEBUGP(fmt , ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define PLT_ENTRY_SIZE 22
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) void *module_alloc(unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) void *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (PAGE_ALIGN(size) > MODULES_LEN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) __builtin_return_address(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (p && (kasan_module_alloc(p, size) < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) vfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) void module_arch_freeing_init(struct module *mod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (is_livepatch_module(mod) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) mod->state == MODULE_STATE_LIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) vfree(mod->arch.syminfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) mod->arch.syminfo = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) static void check_rela(Elf_Rela *rela, struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct mod_arch_syminfo *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) switch (ELF_R_TYPE (rela->r_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) case R_390_GOT12: /* 12 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) case R_390_GOT16: /* 16 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) case R_390_GOT20: /* 20 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) case R_390_GOT32: /* 32 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case R_390_GOT64: /* 64 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (info->got_offset == -1UL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) info->got_offset = me->arch.got_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) me->arch.got_size += sizeof(void*);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case R_390_PLT32: /* 32 bit PC relative PLT address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) case R_390_PLT64: /* 64 bit PC relative PLT address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (info->plt_offset == -1UL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) info->plt_offset = me->arch.plt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) me->arch.plt_size += PLT_ENTRY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) case R_390_COPY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case R_390_GLOB_DAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) case R_390_JMP_SLOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case R_390_RELATIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* Only needed if we want to support loading of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) modules linked with -shared. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * Account for GOT and PLT relocations. We can't add sections for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * got and plt but we can increase the core module size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) char *secstrings, struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) Elf_Shdr *symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) Elf_Sym *symbols;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) Elf_Rela *rela;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) char *strings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int nrela, i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* Find symbol table and string table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) symtab = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) for (i = 0; i < hdr->e_shnum; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) switch (sechdrs[i].sh_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case SHT_SYMTAB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) symtab = sechdrs + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!symtab) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) printk(KERN_ERR "module %s: no symbol table\n", me->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Allocate one syminfo structure per symbol. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) me->arch.syminfo = vmalloc(array_size(sizeof(struct mod_arch_syminfo),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) me->arch.nsyms));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!me->arch.syminfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) symbols = (void *) hdr + symtab->sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) for (i = 0; i < me->arch.nsyms; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (symbols[i].st_shndx == SHN_UNDEF &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) strcmp(strings + symbols[i].st_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) "_GLOBAL_OFFSET_TABLE_") == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* "Define" it as absolute. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) symbols[i].st_shndx = SHN_ABS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) me->arch.syminfo[i].got_offset = -1UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) me->arch.syminfo[i].plt_offset = -1UL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) me->arch.syminfo[i].got_initialized = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) me->arch.syminfo[i].plt_initialized = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* Search for got/plt relocations. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) me->arch.got_size = me->arch.plt_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) for (i = 0; i < hdr->e_shnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (sechdrs[i].sh_type != SHT_RELA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) rela = (void *) hdr + sechdrs[i].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) for (j = 0; j < nrela; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) check_rela(rela + j, me);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /* Increase core size by size of got & plt and set start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) offsets for got and plt. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) me->core_layout.size = ALIGN(me->core_layout.size, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) me->arch.got_offset = me->core_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) me->core_layout.size += me->arch.got_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) me->arch.plt_offset = me->core_layout.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (me->arch.plt_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) me->arch.plt_size += PLT_ENTRY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) me->core_layout.size += me->arch.plt_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int sign, int bits, int shift,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) void *(*write)(void *dest, const void *src, size_t len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) unsigned long umax;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) long min, max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) void *dest = (void *)loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (val & ((1UL << shift) - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (sign) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) val = (Elf_Addr)(((long) val) >> shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) min = -(1L << (bits - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) max = (1L << (bits - 1)) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if ((long) val < min || (long) val > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) val >>= shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) umax = ((1UL << (bits - 1)) << 1) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if ((unsigned long) val > umax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (bits == 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) unsigned char tmp = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) write(dest, &tmp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) } else if (bits == 12) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) unsigned short tmp = (val & 0xfff) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) (*(unsigned short *) loc & 0xf000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) write(dest, &tmp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) } else if (bits == 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) unsigned short tmp = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) write(dest, &tmp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) } else if (bits == 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) unsigned int tmp = (val & 0xfff) << 16 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) (val & 0xff000) >> 4 | (*(unsigned int *) loc & 0xf00000ff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) write(dest, &tmp, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) } else if (bits == 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) unsigned int tmp = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) write(dest, &tmp, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) } else if (bits == 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) unsigned long tmp = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) write(dest, &tmp, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) const char *strtab, struct module *me,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) void *(*write)(void *dest, const void *src, size_t len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) struct mod_arch_syminfo *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) Elf_Addr loc, val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int r_type, r_sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) int rc = -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* This is where to make the change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) loc = base + rela->r_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* This is the symbol it is referring to. Note that all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) undefined symbols have been resolved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) r_sym = ELF_R_SYM(rela->r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) r_type = ELF_R_TYPE(rela->r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) info = me->arch.syminfo + r_sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) val = symtab[r_sym].st_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) switch (r_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) case R_390_NONE: /* No relocation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) case R_390_8: /* Direct 8 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) case R_390_12: /* Direct 12 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) case R_390_16: /* Direct 16 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) case R_390_20: /* Direct 20 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) case R_390_32: /* Direct 32 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) case R_390_64: /* Direct 64 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) val += rela->r_addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (r_type == R_390_8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) rc = apply_rela_bits(loc, val, 0, 8, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) else if (r_type == R_390_12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) rc = apply_rela_bits(loc, val, 0, 12, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) else if (r_type == R_390_16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) rc = apply_rela_bits(loc, val, 0, 16, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) else if (r_type == R_390_20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) rc = apply_rela_bits(loc, val, 1, 20, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) else if (r_type == R_390_32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) rc = apply_rela_bits(loc, val, 0, 32, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) else if (r_type == R_390_64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) rc = apply_rela_bits(loc, val, 0, 64, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) case R_390_PC16: /* PC relative 16 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case R_390_PC32: /* PC relative 32 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) case R_390_PC64: /* PC relative 64 bit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) val += rela->r_addend - loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (r_type == R_390_PC16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) rc = apply_rela_bits(loc, val, 1, 16, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) else if (r_type == R_390_PC16DBL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) rc = apply_rela_bits(loc, val, 1, 16, 1, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) else if (r_type == R_390_PC32DBL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) rc = apply_rela_bits(loc, val, 1, 32, 1, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) else if (r_type == R_390_PC32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) rc = apply_rela_bits(loc, val, 1, 32, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) else if (r_type == R_390_PC64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) rc = apply_rela_bits(loc, val, 1, 64, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) case R_390_GOT12: /* 12 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) case R_390_GOT16: /* 16 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) case R_390_GOT20: /* 20 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) case R_390_GOT32: /* 32 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) case R_390_GOT64: /* 64 bit GOT offset. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) case R_390_GOTPLT12: /* 12 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) case R_390_GOTPLT20: /* 20 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) case R_390_GOTPLT16: /* 16 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) case R_390_GOTPLT32: /* 32 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) case R_390_GOTPLT64: /* 64 bit offset to jump slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (info->got_initialized == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) Elf_Addr *gotent = me->core_layout.base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) me->arch.got_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) info->got_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) write(gotent, &val, sizeof(*gotent));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) info->got_initialized = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) val = info->got_offset + rela->r_addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (r_type == R_390_GOT12 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) r_type == R_390_GOTPLT12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) rc = apply_rela_bits(loc, val, 0, 12, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) else if (r_type == R_390_GOT16 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) r_type == R_390_GOTPLT16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) rc = apply_rela_bits(loc, val, 0, 16, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) else if (r_type == R_390_GOT20 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) r_type == R_390_GOTPLT20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) rc = apply_rela_bits(loc, val, 1, 20, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) else if (r_type == R_390_GOT32 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) r_type == R_390_GOTPLT32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) rc = apply_rela_bits(loc, val, 0, 32, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) else if (r_type == R_390_GOT64 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) r_type == R_390_GOTPLT64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) rc = apply_rela_bits(loc, val, 0, 64, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) else if (r_type == R_390_GOTENT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) r_type == R_390_GOTPLTENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) val += (Elf_Addr) me->core_layout.base - loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) rc = apply_rela_bits(loc, val, 1, 32, 1, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) case R_390_PLT32: /* 32 bit PC relative PLT address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) case R_390_PLT64: /* 64 bit PC relative PLT address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (info->plt_initialized == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) unsigned char insn[PLT_ENTRY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) char *plt_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) char *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) plt_base = me->core_layout.base + me->arch.plt_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ip = plt_base + info->plt_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) *(int *)insn = 0x0d10e310; /* basr 1,0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) *(int *)&insn[4] = 0x100c0004; /* lg 1,12(1) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) char *jump_r1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) jump_r1 = plt_base + me->arch.plt_size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) PLT_ENTRY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* brcl 0xf,__jump_r1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) *(short *)&insn[8] = 0xc0f4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) *(int *)&insn[10] = (jump_r1 - (ip + 8)) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) *(int *)&insn[8] = 0x07f10000; /* br %r1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) *(long *)&insn[14] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) write(ip, insn, sizeof(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) info->plt_initialized = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (r_type == R_390_PLTOFF16 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) r_type == R_390_PLTOFF32 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) r_type == R_390_PLTOFF64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) val = me->arch.plt_offset - me->arch.got_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) info->plt_offset + rela->r_addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!((r_type == R_390_PLT16DBL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) val - loc + 0xffffUL < 0x1ffffeUL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) (r_type == R_390_PLT32DBL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) val - loc + 0xffffffffULL < 0x1fffffffeULL)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) val = (Elf_Addr) me->core_layout.base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) me->arch.plt_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) info->plt_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) val += rela->r_addend - loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (r_type == R_390_PLT16DBL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) rc = apply_rela_bits(loc, val, 1, 16, 1, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) else if (r_type == R_390_PLTOFF16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) rc = apply_rela_bits(loc, val, 0, 16, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) else if (r_type == R_390_PLT32DBL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) rc = apply_rela_bits(loc, val, 1, 32, 1, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) else if (r_type == R_390_PLT32 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) r_type == R_390_PLTOFF32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) rc = apply_rela_bits(loc, val, 0, 32, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) else if (r_type == R_390_PLT64 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) r_type == R_390_PLTOFF64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) rc = apply_rela_bits(loc, val, 0, 64, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) case R_390_GOTOFF16: /* 16 bit offset to GOT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) case R_390_GOTOFF32: /* 32 bit offset to GOT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) case R_390_GOTOFF64: /* 64 bit offset to GOT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) val = val + rela->r_addend -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) ((Elf_Addr) me->core_layout.base + me->arch.got_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (r_type == R_390_GOTOFF16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) rc = apply_rela_bits(loc, val, 0, 16, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) else if (r_type == R_390_GOTOFF32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) rc = apply_rela_bits(loc, val, 0, 32, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) else if (r_type == R_390_GOTOFF64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) rc = apply_rela_bits(loc, val, 0, 64, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) val = (Elf_Addr) me->core_layout.base + me->arch.got_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) rela->r_addend - loc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (r_type == R_390_GOTPC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) rc = apply_rela_bits(loc, val, 1, 32, 0, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) else if (r_type == R_390_GOTPCDBL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) rc = apply_rela_bits(loc, val, 1, 32, 1, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) case R_390_COPY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) case R_390_GLOB_DAT: /* Create GOT entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) case R_390_JMP_SLOT: /* Create PLT entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) case R_390_RELATIVE: /* Adjust by program base. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) /* Only needed if we want to support loading of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) modules linked with -shared. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) printk(KERN_ERR "module %s: unknown relocation: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) me->name, r_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return -ENOEXEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) printk(KERN_ERR "module %s: relocation error for symbol %s "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) "(r_type %i, value 0x%lx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) me->name, strtab + symtab[r_sym].st_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) r_type, (unsigned long) val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) static int __apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) unsigned int symindex, unsigned int relsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) struct module *me,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) void *(*write)(void *dest, const void *src, size_t len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) Elf_Addr base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) Elf_Sym *symtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) Elf_Rela *rela;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) unsigned long i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) DEBUGP("Applying relocate section %u to %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) relsec, sechdrs[relsec].sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) for (i = 0; i < n; i++, rela++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) rc = apply_rela(rela, base, symtab, strtab, me, write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) unsigned int symindex, unsigned int relsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) bool early = me->state == MODULE_STATE_UNFORMED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) void *(*write)(void *, const void *, size_t) = memcpy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (!early)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) write = s390_kernel_write;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) return __apply_relocate_add(sechdrs, strtab, symindex, relsec, me,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) int module_finalize(const Elf_Ehdr *hdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) const Elf_Shdr *sechdrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct module *me)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) const Elf_Shdr *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) char *secstrings, *secname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) void *aseg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (IS_ENABLED(CONFIG_EXPOLINE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) !nospec_disable && me->arch.plt_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) unsigned int *ij;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) ij = me->core_layout.base + me->arch.plt_offset +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) me->arch.plt_size - PLT_ENTRY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (test_facility(35)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) ij[0] = 0xc6000000; /* exrl %r0,.+10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) ij[1] = 0x0005a7f4; /* j . */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) ij[2] = 0x000007f1; /* br %r1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) ij[0] = 0x44000000 | (unsigned int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) offsetof(struct lowcore, br_r1_trampoline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) ij[1] = 0xa7f40000; /* j . */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) aseg = (void *) s->sh_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) secname = secstrings + s->sh_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (!strcmp(".altinstructions", secname))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) /* patch .altinstructions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) apply_alternatives(aseg, aseg + s->sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (IS_ENABLED(CONFIG_EXPOLINE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) (str_has_prefix(secname, ".s390_indirect")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) nospec_revert(aseg, aseg + s->sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (IS_ENABLED(CONFIG_EXPOLINE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) (str_has_prefix(secname, ".s390_return")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) nospec_revert(aseg, aseg + s->sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) jump_label_apply_nops(me);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) }