1a17ae4c3SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 21da177e4SLinus Torvalds /* 3a53c8fabSHeiko Carstens * Kernel module help for s390. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * S390 version 6a53c8fabSHeiko Carstens * Copyright IBM Corp. 2002, 2003 71da177e4SLinus Torvalds * Author(s): Arnd Bergmann (arndb@de.ibm.com) 81da177e4SLinus Torvalds * Martin Schwidefsky (schwidefsky@de.ibm.com) 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * based on i386 version 111da177e4SLinus Torvalds * Copyright (C) 2001 Rusty Russell. 121da177e4SLinus Torvalds */ 131da177e4SLinus Torvalds #include <linux/module.h> 141da177e4SLinus Torvalds #include <linux/elf.h> 151da177e4SLinus Torvalds #include <linux/vmalloc.h> 161da177e4SLinus Torvalds #include <linux/fs.h> 171da177e4SLinus Torvalds #include <linux/string.h> 181da177e4SLinus Torvalds #include <linux/kernel.h> 19793213a8SVasily Gorbik #include <linux/kasan.h> 202b67fc46SHeiko Carstens #include <linux/moduleloader.h> 21c0007f1aSHeiko Carstens #include <linux/bug.h> 22*be242261SPeter Zijlstra #include <linux/memory.h> 23686140a1SVasily Gorbik #include <asm/alternative.h> 24f19fbd5eSMartin Schwidefsky #include <asm/nospec-branch.h> 25f19fbd5eSMartin Schwidefsky #include <asm/facility.h> 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds #if 0 281da177e4SLinus Torvalds #define DEBUGP printk 291da177e4SLinus Torvalds #else 301da177e4SLinus Torvalds #define DEBUGP(fmt , ...) 311da177e4SLinus Torvalds #endif 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #define PLT_ENTRY_SIZE 20 341da177e4SLinus Torvalds 35c972cc60SHeiko Carstens void *module_alloc(unsigned long size) 36c972cc60SHeiko Carstens { 37793213a8SVasily Gorbik void *p; 38793213a8SVasily Gorbik 39c972cc60SHeiko Carstens if (PAGE_ALIGN(size) > MODULES_LEN) 40c972cc60SHeiko Carstens return NULL; 41793213a8SVasily Gorbik p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END, 42793213a8SVasily Gorbik GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE, 43c972cc60SHeiko Carstens __builtin_return_address(0)); 44793213a8SVasily Gorbik if (p && (kasan_module_alloc(p, size) < 0)) { 45793213a8SVasily Gorbik vfree(p); 46793213a8SVasily Gorbik return NULL; 47793213a8SVasily Gorbik } 48793213a8SVasily Gorbik return p; 49c972cc60SHeiko Carstens } 50c972cc60SHeiko Carstens 51d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod) 521da177e4SLinus Torvalds { 53f31e0960SJessica Yu if (is_livepatch_module(mod) && 54f31e0960SJessica Yu mod->state == MODULE_STATE_LIVE) 55f31e0960SJessica Yu return; 56f31e0960SJessica Yu 576a03f5f0SChristian Borntraeger vfree(mod->arch.syminfo); 586a03f5f0SChristian Borntraeger mod->arch.syminfo = NULL; 593164a3cbSHendrik Brueckner } 601da177e4SLinus Torvalds 61083e14c0SMartin Schwidefsky static void check_rela(Elf_Rela *rela, struct module *me) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds struct mod_arch_syminfo *info; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds info = me->arch.syminfo + ELF_R_SYM (rela->r_info); 661da177e4SLinus Torvalds switch (ELF_R_TYPE (rela->r_info)) { 671da177e4SLinus Torvalds case R_390_GOT12: /* 12 bit GOT offset. */ 681da177e4SLinus Torvalds case R_390_GOT16: /* 16 bit GOT offset. */ 691da177e4SLinus Torvalds case R_390_GOT20: /* 20 bit GOT offset. */ 701da177e4SLinus Torvalds case R_390_GOT32: /* 32 bit GOT offset. */ 711da177e4SLinus Torvalds case R_390_GOT64: /* 64 bit GOT offset. */ 721da177e4SLinus Torvalds case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */ 731da177e4SLinus Torvalds case R_390_GOTPLT12: /* 12 bit offset to jump slot. */ 741da177e4SLinus Torvalds case R_390_GOTPLT16: /* 16 bit offset to jump slot. */ 751da177e4SLinus Torvalds case R_390_GOTPLT20: /* 20 bit offset to jump slot. */ 761da177e4SLinus Torvalds case R_390_GOTPLT32: /* 32 bit offset to jump slot. */ 771da177e4SLinus Torvalds case R_390_GOTPLT64: /* 64 bit offset to jump slot. */ 781da177e4SLinus Torvalds case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */ 791da177e4SLinus Torvalds if (info->got_offset == -1UL) { 801da177e4SLinus Torvalds info->got_offset = me->arch.got_size; 811da177e4SLinus Torvalds me->arch.got_size += sizeof(void*); 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds break; 841da177e4SLinus Torvalds case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */ 851da177e4SLinus Torvalds case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */ 861da177e4SLinus Torvalds case R_390_PLT32: /* 32 bit PC relative PLT address. */ 871da177e4SLinus Torvalds case R_390_PLT64: /* 64 bit PC relative PLT address. */ 881da177e4SLinus Torvalds case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */ 891da177e4SLinus Torvalds case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */ 901da177e4SLinus Torvalds case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */ 911da177e4SLinus Torvalds if (info->plt_offset == -1UL) { 921da177e4SLinus Torvalds info->plt_offset = me->arch.plt_size; 931da177e4SLinus Torvalds me->arch.plt_size += PLT_ENTRY_SIZE; 941da177e4SLinus Torvalds } 951da177e4SLinus Torvalds break; 961da177e4SLinus Torvalds case R_390_COPY: 971da177e4SLinus Torvalds case R_390_GLOB_DAT: 981da177e4SLinus Torvalds case R_390_JMP_SLOT: 991da177e4SLinus Torvalds case R_390_RELATIVE: 1001da177e4SLinus Torvalds /* Only needed if we want to support loading of 1011da177e4SLinus Torvalds modules linked with -shared. */ 1021da177e4SLinus Torvalds break; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * Account for GOT and PLT relocations. We can't add sections for 1081da177e4SLinus Torvalds * got and plt but we can increase the core module size. 1091da177e4SLinus Torvalds */ 110083e14c0SMartin Schwidefsky int module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 1111da177e4SLinus Torvalds char *secstrings, struct module *me) 1121da177e4SLinus Torvalds { 1131da177e4SLinus Torvalds Elf_Shdr *symtab; 1141da177e4SLinus Torvalds Elf_Sym *symbols; 1151da177e4SLinus Torvalds Elf_Rela *rela; 1161da177e4SLinus Torvalds char *strings; 1171da177e4SLinus Torvalds int nrela, i, j; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds /* Find symbol table and string table. */ 120d2c993d8SHeiko Carstens symtab = NULL; 1211da177e4SLinus Torvalds for (i = 0; i < hdr->e_shnum; i++) 1221da177e4SLinus Torvalds switch (sechdrs[i].sh_type) { 1231da177e4SLinus Torvalds case SHT_SYMTAB: 1241da177e4SLinus Torvalds symtab = sechdrs + i; 1251da177e4SLinus Torvalds break; 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds if (!symtab) { 1281da177e4SLinus Torvalds printk(KERN_ERR "module %s: no symbol table\n", me->name); 1291da177e4SLinus Torvalds return -ENOEXEC; 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds /* Allocate one syminfo structure per symbol. */ 1331da177e4SLinus Torvalds me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym); 13442bc47b3SKees Cook me->arch.syminfo = vmalloc(array_size(sizeof(struct mod_arch_syminfo), 13542bc47b3SKees Cook me->arch.nsyms)); 1361da177e4SLinus Torvalds if (!me->arch.syminfo) 1371da177e4SLinus Torvalds return -ENOMEM; 1381da177e4SLinus Torvalds symbols = (void *) hdr + symtab->sh_offset; 1391da177e4SLinus Torvalds strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset; 1401da177e4SLinus Torvalds for (i = 0; i < me->arch.nsyms; i++) { 1411da177e4SLinus Torvalds if (symbols[i].st_shndx == SHN_UNDEF && 1421da177e4SLinus Torvalds strcmp(strings + symbols[i].st_name, 1431da177e4SLinus Torvalds "_GLOBAL_OFFSET_TABLE_") == 0) 1441da177e4SLinus Torvalds /* "Define" it as absolute. */ 1451da177e4SLinus Torvalds symbols[i].st_shndx = SHN_ABS; 1461da177e4SLinus Torvalds me->arch.syminfo[i].got_offset = -1UL; 1471da177e4SLinus Torvalds me->arch.syminfo[i].plt_offset = -1UL; 1481da177e4SLinus Torvalds me->arch.syminfo[i].got_initialized = 0; 1491da177e4SLinus Torvalds me->arch.syminfo[i].plt_initialized = 0; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* Search for got/plt relocations. */ 1531da177e4SLinus Torvalds me->arch.got_size = me->arch.plt_size = 0; 1541da177e4SLinus Torvalds for (i = 0; i < hdr->e_shnum; i++) { 1551da177e4SLinus Torvalds if (sechdrs[i].sh_type != SHT_RELA) 1561da177e4SLinus Torvalds continue; 1571da177e4SLinus Torvalds nrela = sechdrs[i].sh_size / sizeof(Elf_Rela); 1581da177e4SLinus Torvalds rela = (void *) hdr + sechdrs[i].sh_offset; 1591da177e4SLinus Torvalds for (j = 0; j < nrela; j++) 1601da177e4SLinus Torvalds check_rela(rela + j, me); 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* Increase core size by size of got & plt and set start 1641da177e4SLinus Torvalds offsets for got and plt. */ 1657523e4dcSRusty Russell me->core_layout.size = ALIGN(me->core_layout.size, 4); 1667523e4dcSRusty Russell me->arch.got_offset = me->core_layout.size; 1677523e4dcSRusty Russell me->core_layout.size += me->arch.got_size; 1687523e4dcSRusty Russell me->arch.plt_offset = me->core_layout.size; 169f19fbd5eSMartin Schwidefsky if (me->arch.plt_size) { 1706e179d64SMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) 171f19fbd5eSMartin Schwidefsky me->arch.plt_size += PLT_ENTRY_SIZE; 1727523e4dcSRusty Russell me->core_layout.size += me->arch.plt_size; 173f19fbd5eSMartin Schwidefsky } 1741da177e4SLinus Torvalds return 0; 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 177083e14c0SMartin Schwidefsky static int apply_rela_bits(Elf_Addr loc, Elf_Addr val, 178*be242261SPeter Zijlstra int sign, int bits, int shift, 179*be242261SPeter Zijlstra void *(*write)(void *dest, const void *src, size_t len)) 180083e14c0SMartin Schwidefsky { 181083e14c0SMartin Schwidefsky unsigned long umax; 182083e14c0SMartin Schwidefsky long min, max; 183*be242261SPeter Zijlstra void *dest = (void *)loc; 184083e14c0SMartin Schwidefsky 185083e14c0SMartin Schwidefsky if (val & ((1UL << shift) - 1)) 186083e14c0SMartin Schwidefsky return -ENOEXEC; 187083e14c0SMartin Schwidefsky if (sign) { 188083e14c0SMartin Schwidefsky val = (Elf_Addr)(((long) val) >> shift); 189083e14c0SMartin Schwidefsky min = -(1L << (bits - 1)); 190083e14c0SMartin Schwidefsky max = (1L << (bits - 1)) - 1; 191083e14c0SMartin Schwidefsky if ((long) val < min || (long) val > max) 192083e14c0SMartin Schwidefsky return -ENOEXEC; 193083e14c0SMartin Schwidefsky } else { 194083e14c0SMartin Schwidefsky val >>= shift; 195083e14c0SMartin Schwidefsky umax = ((1UL << (bits - 1)) << 1) - 1; 196083e14c0SMartin Schwidefsky if ((unsigned long) val > umax) 197083e14c0SMartin Schwidefsky return -ENOEXEC; 198083e14c0SMartin Schwidefsky } 199083e14c0SMartin Schwidefsky 200*be242261SPeter Zijlstra if (bits == 8) { 201*be242261SPeter Zijlstra unsigned char tmp = val; 202*be242261SPeter Zijlstra write(dest, &tmp, 1); 203*be242261SPeter Zijlstra } else if (bits == 12) { 204*be242261SPeter Zijlstra unsigned short tmp = (val & 0xfff) | 205083e14c0SMartin Schwidefsky (*(unsigned short *) loc & 0xf000); 206*be242261SPeter Zijlstra write(dest, &tmp, 2); 207*be242261SPeter Zijlstra } else if (bits == 16) { 208*be242261SPeter Zijlstra unsigned short tmp = val; 209*be242261SPeter Zijlstra write(dest, &tmp, 2); 210*be242261SPeter Zijlstra } else if (bits == 20) { 211*be242261SPeter Zijlstra unsigned int tmp = (val & 0xfff) << 16 | 212*be242261SPeter Zijlstra (val & 0xff000) >> 4 | (*(unsigned int *) loc & 0xf00000ff); 213*be242261SPeter Zijlstra write(dest, &tmp, 4); 214*be242261SPeter Zijlstra } else if (bits == 32) { 215*be242261SPeter Zijlstra unsigned int tmp = val; 216*be242261SPeter Zijlstra write(dest, &tmp, 4); 217*be242261SPeter Zijlstra } else if (bits == 64) { 218*be242261SPeter Zijlstra unsigned long tmp = val; 219*be242261SPeter Zijlstra write(dest, &tmp, 8); 220*be242261SPeter Zijlstra } 221083e14c0SMartin Schwidefsky return 0; 222083e14c0SMartin Schwidefsky } 223083e14c0SMartin Schwidefsky 224083e14c0SMartin Schwidefsky static int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab, 225*be242261SPeter Zijlstra const char *strtab, struct module *me, 226*be242261SPeter Zijlstra void *(*write)(void *dest, const void *src, size_t len)) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds struct mod_arch_syminfo *info; 2291da177e4SLinus Torvalds Elf_Addr loc, val; 2301da177e4SLinus Torvalds int r_type, r_sym; 23172a6b43eSHeiko Carstens int rc = -ENOEXEC; 2321da177e4SLinus Torvalds 2331da177e4SLinus Torvalds /* This is where to make the change */ 2341da177e4SLinus Torvalds loc = base + rela->r_offset; 2351da177e4SLinus Torvalds /* This is the symbol it is referring to. Note that all 2361da177e4SLinus Torvalds undefined symbols have been resolved. */ 2371da177e4SLinus Torvalds r_sym = ELF_R_SYM(rela->r_info); 2381da177e4SLinus Torvalds r_type = ELF_R_TYPE(rela->r_info); 2391da177e4SLinus Torvalds info = me->arch.syminfo + r_sym; 2401da177e4SLinus Torvalds val = symtab[r_sym].st_value; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds switch (r_type) { 243e80cfc31SHendrik Brueckner case R_390_NONE: /* No relocation. */ 244e80cfc31SHendrik Brueckner rc = 0; 245e80cfc31SHendrik Brueckner break; 2461da177e4SLinus Torvalds case R_390_8: /* Direct 8 bit. */ 2471da177e4SLinus Torvalds case R_390_12: /* Direct 12 bit. */ 2481da177e4SLinus Torvalds case R_390_16: /* Direct 16 bit. */ 2491da177e4SLinus Torvalds case R_390_20: /* Direct 20 bit. */ 2501da177e4SLinus Torvalds case R_390_32: /* Direct 32 bit. */ 2511da177e4SLinus Torvalds case R_390_64: /* Direct 64 bit. */ 2521da177e4SLinus Torvalds val += rela->r_addend; 2531da177e4SLinus Torvalds if (r_type == R_390_8) 254*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 8, 0, write); 2551da177e4SLinus Torvalds else if (r_type == R_390_12) 256*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 12, 0, write); 2571da177e4SLinus Torvalds else if (r_type == R_390_16) 258*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 2591da177e4SLinus Torvalds else if (r_type == R_390_20) 260*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 20, 0, write); 2611da177e4SLinus Torvalds else if (r_type == R_390_32) 262*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 2631da177e4SLinus Torvalds else if (r_type == R_390_64) 264*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 2651da177e4SLinus Torvalds break; 2661da177e4SLinus Torvalds case R_390_PC16: /* PC relative 16 bit. */ 2671da177e4SLinus Torvalds case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */ 2681da177e4SLinus Torvalds case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */ 2691da177e4SLinus Torvalds case R_390_PC32: /* PC relative 32 bit. */ 2701da177e4SLinus Torvalds case R_390_PC64: /* PC relative 64 bit. */ 2711da177e4SLinus Torvalds val += rela->r_addend - loc; 2721da177e4SLinus Torvalds if (r_type == R_390_PC16) 273*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 16, 0, write); 2741da177e4SLinus Torvalds else if (r_type == R_390_PC16DBL) 275*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 16, 1, write); 2761da177e4SLinus Torvalds else if (r_type == R_390_PC32DBL) 277*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 2781da177e4SLinus Torvalds else if (r_type == R_390_PC32) 279*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 0, write); 2801da177e4SLinus Torvalds else if (r_type == R_390_PC64) 281*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 64, 0, write); 2821da177e4SLinus Torvalds break; 2831da177e4SLinus Torvalds case R_390_GOT12: /* 12 bit GOT offset. */ 2841da177e4SLinus Torvalds case R_390_GOT16: /* 16 bit GOT offset. */ 2851da177e4SLinus Torvalds case R_390_GOT20: /* 20 bit GOT offset. */ 2861da177e4SLinus Torvalds case R_390_GOT32: /* 32 bit GOT offset. */ 2871da177e4SLinus Torvalds case R_390_GOT64: /* 64 bit GOT offset. */ 2881da177e4SLinus Torvalds case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */ 2891da177e4SLinus Torvalds case R_390_GOTPLT12: /* 12 bit offset to jump slot. */ 2901da177e4SLinus Torvalds case R_390_GOTPLT20: /* 20 bit offset to jump slot. */ 2911da177e4SLinus Torvalds case R_390_GOTPLT16: /* 16 bit offset to jump slot. */ 2921da177e4SLinus Torvalds case R_390_GOTPLT32: /* 32 bit offset to jump slot. */ 2931da177e4SLinus Torvalds case R_390_GOTPLT64: /* 64 bit offset to jump slot. */ 2941da177e4SLinus Torvalds case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */ 2951da177e4SLinus Torvalds if (info->got_initialized == 0) { 296*be242261SPeter Zijlstra Elf_Addr *gotent = me->core_layout.base + 297*be242261SPeter Zijlstra me->arch.got_offset + 2981da177e4SLinus Torvalds info->got_offset; 299*be242261SPeter Zijlstra 300*be242261SPeter Zijlstra write(gotent, &val, sizeof(*gotent)); 3011da177e4SLinus Torvalds info->got_initialized = 1; 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds val = info->got_offset + rela->r_addend; 3041da177e4SLinus Torvalds if (r_type == R_390_GOT12 || 3051da177e4SLinus Torvalds r_type == R_390_GOTPLT12) 306*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 12, 0, write); 3071da177e4SLinus Torvalds else if (r_type == R_390_GOT16 || 3081da177e4SLinus Torvalds r_type == R_390_GOTPLT16) 309*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 3101da177e4SLinus Torvalds else if (r_type == R_390_GOT20 || 3111da177e4SLinus Torvalds r_type == R_390_GOTPLT20) 312*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 20, 0, write); 3131da177e4SLinus Torvalds else if (r_type == R_390_GOT32 || 3141da177e4SLinus Torvalds r_type == R_390_GOTPLT32) 315*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 3161da177e4SLinus Torvalds else if (r_type == R_390_GOT64 || 3171da177e4SLinus Torvalds r_type == R_390_GOTPLT64) 318*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 319083e14c0SMartin Schwidefsky else if (r_type == R_390_GOTENT || 320083e14c0SMartin Schwidefsky r_type == R_390_GOTPLTENT) { 3217523e4dcSRusty Russell val += (Elf_Addr) me->core_layout.base - loc; 322*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 323083e14c0SMartin Schwidefsky } 3241da177e4SLinus Torvalds break; 3251da177e4SLinus Torvalds case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */ 3261da177e4SLinus Torvalds case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */ 3271da177e4SLinus Torvalds case R_390_PLT32: /* 32 bit PC relative PLT address. */ 3281da177e4SLinus Torvalds case R_390_PLT64: /* 64 bit PC relative PLT address. */ 3291da177e4SLinus Torvalds case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */ 3301da177e4SLinus Torvalds case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */ 3311da177e4SLinus Torvalds case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */ 3321da177e4SLinus Torvalds if (info->plt_initialized == 0) { 333*be242261SPeter Zijlstra unsigned int insn[5]; 334*be242261SPeter Zijlstra unsigned int *ip = me->core_layout.base + 335*be242261SPeter Zijlstra me->arch.plt_offset + 3361da177e4SLinus Torvalds info->plt_offset; 337*be242261SPeter Zijlstra 338*be242261SPeter Zijlstra insn[0] = 0x0d10e310; /* basr 1,0 */ 339*be242261SPeter Zijlstra insn[1] = 0x100a0004; /* lg 1,10(1) */ 3406e179d64SMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && !nospec_disable) { 341f19fbd5eSMartin Schwidefsky unsigned int *ij; 342f19fbd5eSMartin Schwidefsky ij = me->core_layout.base + 343f19fbd5eSMartin Schwidefsky me->arch.plt_offset + 344f19fbd5eSMartin Schwidefsky me->arch.plt_size - PLT_ENTRY_SIZE; 345*be242261SPeter Zijlstra insn[2] = 0xa7f40000 + /* j __jump_r1 */ 346f19fbd5eSMartin Schwidefsky (unsigned int)(u16) 347f19fbd5eSMartin Schwidefsky (((unsigned long) ij - 8 - 348f19fbd5eSMartin Schwidefsky (unsigned long) ip) / 2); 349f19fbd5eSMartin Schwidefsky } else { 350*be242261SPeter Zijlstra insn[2] = 0x07f10000; /* br %r1 */ 351f19fbd5eSMartin Schwidefsky } 352*be242261SPeter Zijlstra insn[3] = (unsigned int) (val >> 32); 353*be242261SPeter Zijlstra insn[4] = (unsigned int) val; 354*be242261SPeter Zijlstra 355*be242261SPeter Zijlstra write(ip, insn, sizeof(insn)); 3561da177e4SLinus Torvalds info->plt_initialized = 1; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds if (r_type == R_390_PLTOFF16 || 359504665a9SMartin Schwidefsky r_type == R_390_PLTOFF32 || 360504665a9SMartin Schwidefsky r_type == R_390_PLTOFF64) 3611da177e4SLinus Torvalds val = me->arch.plt_offset - me->arch.got_offset + 3621da177e4SLinus Torvalds info->plt_offset + rela->r_addend; 363504665a9SMartin Schwidefsky else { 364504665a9SMartin Schwidefsky if (!((r_type == R_390_PLT16DBL && 365504665a9SMartin Schwidefsky val - loc + 0xffffUL < 0x1ffffeUL) || 366504665a9SMartin Schwidefsky (r_type == R_390_PLT32DBL && 367504665a9SMartin Schwidefsky val - loc + 0xffffffffULL < 0x1fffffffeULL))) 3687523e4dcSRusty Russell val = (Elf_Addr) me->core_layout.base + 369504665a9SMartin Schwidefsky me->arch.plt_offset + 370504665a9SMartin Schwidefsky info->plt_offset; 371504665a9SMartin Schwidefsky val += rela->r_addend - loc; 372504665a9SMartin Schwidefsky } 3731da177e4SLinus Torvalds if (r_type == R_390_PLT16DBL) 374*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 16, 1, write); 3751da177e4SLinus Torvalds else if (r_type == R_390_PLTOFF16) 376*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 3771da177e4SLinus Torvalds else if (r_type == R_390_PLT32DBL) 378*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 3791da177e4SLinus Torvalds else if (r_type == R_390_PLT32 || 3801da177e4SLinus Torvalds r_type == R_390_PLTOFF32) 381*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 3821da177e4SLinus Torvalds else if (r_type == R_390_PLT64 || 3831da177e4SLinus Torvalds r_type == R_390_PLTOFF64) 384*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 3851da177e4SLinus Torvalds break; 3861da177e4SLinus Torvalds case R_390_GOTOFF16: /* 16 bit offset to GOT. */ 3871da177e4SLinus Torvalds case R_390_GOTOFF32: /* 32 bit offset to GOT. */ 3881da177e4SLinus Torvalds case R_390_GOTOFF64: /* 64 bit offset to GOT. */ 3891da177e4SLinus Torvalds val = val + rela->r_addend - 3907523e4dcSRusty Russell ((Elf_Addr) me->core_layout.base + me->arch.got_offset); 3911da177e4SLinus Torvalds if (r_type == R_390_GOTOFF16) 392*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 16, 0, write); 3931da177e4SLinus Torvalds else if (r_type == R_390_GOTOFF32) 394*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 32, 0, write); 3951da177e4SLinus Torvalds else if (r_type == R_390_GOTOFF64) 396*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 0, 64, 0, write); 3971da177e4SLinus Torvalds break; 3981da177e4SLinus Torvalds case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */ 3991da177e4SLinus Torvalds case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */ 4007523e4dcSRusty Russell val = (Elf_Addr) me->core_layout.base + me->arch.got_offset + 4011da177e4SLinus Torvalds rela->r_addend - loc; 4021da177e4SLinus Torvalds if (r_type == R_390_GOTPC) 403*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 0, write); 4041da177e4SLinus Torvalds else if (r_type == R_390_GOTPCDBL) 405*be242261SPeter Zijlstra rc = apply_rela_bits(loc, val, 1, 32, 1, write); 4061da177e4SLinus Torvalds break; 4071da177e4SLinus Torvalds case R_390_COPY: 4081da177e4SLinus Torvalds case R_390_GLOB_DAT: /* Create GOT entry. */ 4091da177e4SLinus Torvalds case R_390_JMP_SLOT: /* Create PLT entry. */ 4101da177e4SLinus Torvalds case R_390_RELATIVE: /* Adjust by program base. */ 4111da177e4SLinus Torvalds /* Only needed if we want to support loading of 4121da177e4SLinus Torvalds modules linked with -shared. */ 413083e14c0SMartin Schwidefsky return -ENOEXEC; 4141da177e4SLinus Torvalds default: 415083e14c0SMartin Schwidefsky printk(KERN_ERR "module %s: unknown relocation: %u\n", 4161da177e4SLinus Torvalds me->name, r_type); 4171da177e4SLinus Torvalds return -ENOEXEC; 4181da177e4SLinus Torvalds } 419083e14c0SMartin Schwidefsky if (rc) { 420083e14c0SMartin Schwidefsky printk(KERN_ERR "module %s: relocation error for symbol %s " 421083e14c0SMartin Schwidefsky "(r_type %i, value 0x%lx)\n", 422083e14c0SMartin Schwidefsky me->name, strtab + symtab[r_sym].st_name, 423083e14c0SMartin Schwidefsky r_type, (unsigned long) val); 424083e14c0SMartin Schwidefsky return rc; 425083e14c0SMartin Schwidefsky } 4261da177e4SLinus Torvalds return 0; 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 429*be242261SPeter Zijlstra static int __apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 4301da177e4SLinus Torvalds unsigned int symindex, unsigned int relsec, 431*be242261SPeter Zijlstra struct module *me, 432*be242261SPeter Zijlstra void *(*write)(void *dest, const void *src, size_t len)) 4331da177e4SLinus Torvalds { 4341da177e4SLinus Torvalds Elf_Addr base; 4351da177e4SLinus Torvalds Elf_Sym *symtab; 4361da177e4SLinus Torvalds Elf_Rela *rela; 4371da177e4SLinus Torvalds unsigned long i, n; 4381da177e4SLinus Torvalds int rc; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds DEBUGP("Applying relocate section %u to %u\n", 4411da177e4SLinus Torvalds relsec, sechdrs[relsec].sh_info); 4421da177e4SLinus Torvalds base = sechdrs[sechdrs[relsec].sh_info].sh_addr; 4431da177e4SLinus Torvalds symtab = (Elf_Sym *) sechdrs[symindex].sh_addr; 4441da177e4SLinus Torvalds rela = (Elf_Rela *) sechdrs[relsec].sh_addr; 4451da177e4SLinus Torvalds n = sechdrs[relsec].sh_size / sizeof(Elf_Rela); 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds for (i = 0; i < n; i++, rela++) { 448*be242261SPeter Zijlstra rc = apply_rela(rela, base, symtab, strtab, me, write); 4491da177e4SLinus Torvalds if (rc) 4501da177e4SLinus Torvalds return rc; 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds return 0; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 455*be242261SPeter Zijlstra int apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 456*be242261SPeter Zijlstra unsigned int symindex, unsigned int relsec, 457*be242261SPeter Zijlstra struct module *me) 458*be242261SPeter Zijlstra { 459*be242261SPeter Zijlstra bool early = me->state == MODULE_STATE_UNFORMED; 460*be242261SPeter Zijlstra void *(*write)(void *, const void *, size_t) = memcpy; 461*be242261SPeter Zijlstra 462*be242261SPeter Zijlstra if (!early) 463*be242261SPeter Zijlstra write = s390_kernel_write; 464*be242261SPeter Zijlstra 465*be242261SPeter Zijlstra return __apply_relocate_add(sechdrs, strtab, symindex, relsec, me, 466*be242261SPeter Zijlstra write); 467*be242261SPeter Zijlstra } 468*be242261SPeter Zijlstra 4691da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr, 4701da177e4SLinus Torvalds const Elf_Shdr *sechdrs, 4711da177e4SLinus Torvalds struct module *me) 4721da177e4SLinus Torvalds { 473686140a1SVasily Gorbik const Elf_Shdr *s; 474f19fbd5eSMartin Schwidefsky char *secstrings, *secname; 475f19fbd5eSMartin Schwidefsky void *aseg; 476f19fbd5eSMartin Schwidefsky 477f19fbd5eSMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && 4786e179d64SMartin Schwidefsky !nospec_disable && me->arch.plt_size) { 479f19fbd5eSMartin Schwidefsky unsigned int *ij; 480f19fbd5eSMartin Schwidefsky 481f19fbd5eSMartin Schwidefsky ij = me->core_layout.base + me->arch.plt_offset + 482f19fbd5eSMartin Schwidefsky me->arch.plt_size - PLT_ENTRY_SIZE; 483f19fbd5eSMartin Schwidefsky if (test_facility(35)) { 484f19fbd5eSMartin Schwidefsky ij[0] = 0xc6000000; /* exrl %r0,.+10 */ 485f19fbd5eSMartin Schwidefsky ij[1] = 0x0005a7f4; /* j . */ 486f19fbd5eSMartin Schwidefsky ij[2] = 0x000007f1; /* br %r1 */ 487f19fbd5eSMartin Schwidefsky } else { 488f19fbd5eSMartin Schwidefsky ij[0] = 0x44000000 | (unsigned int) 489f19fbd5eSMartin Schwidefsky offsetof(struct lowcore, br_r1_trampoline); 490f19fbd5eSMartin Schwidefsky ij[1] = 0xa7f40000; /* j . */ 491f19fbd5eSMartin Schwidefsky } 492f19fbd5eSMartin Schwidefsky } 493686140a1SVasily Gorbik 494686140a1SVasily Gorbik secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; 495686140a1SVasily Gorbik for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) { 496f19fbd5eSMartin Schwidefsky aseg = (void *) s->sh_addr; 497f19fbd5eSMartin Schwidefsky secname = secstrings + s->sh_name; 498686140a1SVasily Gorbik 499f19fbd5eSMartin Schwidefsky if (!strcmp(".altinstructions", secname)) 500f19fbd5eSMartin Schwidefsky /* patch .altinstructions */ 501686140a1SVasily Gorbik apply_alternatives(aseg, aseg + s->sh_size); 502f19fbd5eSMartin Schwidefsky 503f19fbd5eSMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && 504b29cd7c4SVasily Gorbik (str_has_prefix(secname, ".s390_indirect"))) 5056e179d64SMartin Schwidefsky nospec_revert(aseg, aseg + s->sh_size); 506f19fbd5eSMartin Schwidefsky 507f19fbd5eSMartin Schwidefsky if (IS_ENABLED(CONFIG_EXPOLINE) && 508b29cd7c4SVasily Gorbik (str_has_prefix(secname, ".s390_return"))) 5096e179d64SMartin Schwidefsky nospec_revert(aseg, aseg + s->sh_size); 510686140a1SVasily Gorbik } 511686140a1SVasily Gorbik 5126f367769SHeiko Carstens jump_label_apply_nops(me); 5135336377dSLinus Torvalds return 0; 5141da177e4SLinus Torvalds } 515