11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
2a53c8fab3f87c995c30ac226a03af95361243144Heiko Carstens *  Kernel module help for s390.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
41da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  S390 version
5a53c8fab3f87c995c30ac226a03af95361243144Heiko Carstens *    Copyright IBM Corp. 2002, 2003
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *		 Martin Schwidefsky (schwidefsky@de.ibm.com)
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  based on i386 version
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *    Copyright (C) 2001 Rusty Russell.
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is free software; you can redistribute it and/or modify
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  it under the terms of the GNU General Public License as published by
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  the Free Software Foundation; either version 2 of the License, or
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  (at your option) any later version.
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  This program is distributed in the hope that it will be useful,
181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  but WITHOUT ANY WARRANTY; without even the implied warranty of
191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  GNU General Public License for more details.
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  You should have received a copy of the GNU General Public License
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  along with this program; if not, write to the Free Software
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/elf.h>
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/vmalloc.h>
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/fs.h>
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/string.h>
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
322b67fc46061b2171fb8fbb55d1ac717abd533569Heiko Carstens#include <linux/moduleloader.h>
33c0007f1a65762eaf55633d403b380130ec60adadHeiko Carstens#include <linux/bug.h>
341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#if 0
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DEBUGP printk
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#else
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define DEBUGP(fmt , ...)
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#endif
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
41347a8dc3b815f0c0fa62a1df075184ffe4cbdcf1Martin Schwidefsky#ifndef CONFIG_64BIT
421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define PLT_ENTRY_SIZE 12
43347a8dc3b815f0c0fa62a1df075184ffe4cbdcf1Martin Schwidefsky#else /* CONFIG_64BIT */
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define PLT_ENTRY_SIZE 20
45347a8dc3b815f0c0fa62a1df075184ffe4cbdcf1Martin Schwidefsky#endif /* CONFIG_64BIT */
461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
47c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens#ifdef CONFIG_64BIT
48c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstensvoid *module_alloc(unsigned long size)
49c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens{
50c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens	if (PAGE_ALIGN(size) > MODULES_LEN)
51c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens		return NULL;
52c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens	return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
5340c3baa7c66f1352521378ee83509fb8f4c465deJianguo Wu				    GFP_KERNEL, PAGE_KERNEL, NUMA_NO_NODE,
54c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens				    __builtin_return_address(0));
55c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens}
56c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens#endif
57c972cc60c23f5a6309292bfcc91a441743ba027eHeiko Carstens
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* Free memory returned from module_alloc */
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsvoid module_free(struct module *mod, void *module_region)
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
613164a3cbf8a6bdf85fe3816fe670b391f31c004aHendrik Brueckner	if (mod) {
623164a3cbf8a6bdf85fe3816fe670b391f31c004aHendrik Brueckner		vfree(mod->arch.syminfo);
633164a3cbf8a6bdf85fe3816fe670b391f31c004aHendrik Brueckner		mod->arch.syminfo = NULL;
643164a3cbf8a6bdf85fe3816fe670b391f31c004aHendrik Brueckner	}
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	vfree(module_region);
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
68083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefskystatic void check_rela(Elf_Rela *rela, struct module *me)
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mod_arch_syminfo *info;
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info = me->arch.syminfo + ELF_R_SYM (rela->r_info);
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (ELF_R_TYPE (rela->r_info)) {
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT12:	/* 12 bit GOT offset.  */
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT16:	/* 16 bit GOT offset.  */
761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT20:	/* 20 bit GOT offset.  */
771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT32:	/* 32 bit GOT offset.  */
781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT64:	/* 64 bit GOT offset.  */
791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTENT:	/* 32 bit PC rel. to GOT entry shifted by 1. */
801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT12:	/* 12 bit offset to jump slot.	*/
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT16:	/* 16 bit offset to jump slot.  */
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT20:	/* 20 bit offset to jump slot.  */
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT32:	/* 32 bit offset to jump slot.  */
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT64:	/* 64 bit offset to jump slot.	*/
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLTENT:	/* 32 bit rel. offset to jump slot >> 1. */
861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (info->got_offset == -1UL) {
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info->got_offset = me->arch.got_size;
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			me->arch.got_size += sizeof(void*);
891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT16DBL:	/* 16 bit PC rel. PLT shifted by 1.  */
921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT32DBL:	/* 32 bit PC rel. PLT shifted by 1.  */
931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT32:	/* 32 bit PC relative PLT address.  */
941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT64:	/* 64 bit PC relative PLT address.  */
951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLTOFF16:	/* 16 bit offset from GOT to PLT. */
961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLTOFF32:	/* 32 bit offset from GOT to PLT. */
971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLTOFF64:	/* 16 bit offset from GOT to PLT. */
981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (info->plt_offset == -1UL) {
991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info->plt_offset = me->arch.plt_size;
1001da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			me->arch.plt_size += PLT_ENTRY_SIZE;
1011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
1031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_COPY:
1041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GLOB_DAT:
1051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_JMP_SLOT:
1061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_RELATIVE:
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Only needed if we want to support loading of
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		   modules linked with -shared. */
1091da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
1141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Account for GOT and PLT relocations. We can't add sections for
1151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * got and plt but we can increase the core module size.
1161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
117083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefskyint module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
118083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			      char *secstrings, struct module *me)
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
1201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Elf_Shdr *symtab;
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Elf_Sym *symbols;
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Elf_Rela *rela;
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	char *strings;
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int nrela, i, j;
1251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Find symbol table and string table. */
127d2c993d845781d160a7ef759a3e65c6892c4a270Heiko Carstens	symtab = NULL;
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < hdr->e_shnum; i++)
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		switch (sechdrs[i].sh_type) {
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		case SHT_SYMTAB:
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			symtab = sechdrs + i;
1321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			break;
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!symtab) {
1351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		printk(KERN_ERR "module %s: no symbol table\n", me->name);
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOEXEC;
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Allocate one syminfo structure per symbol. */
1401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym);
1411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->arch.syminfo = vmalloc(me->arch.nsyms *
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				   sizeof(struct mod_arch_syminfo));
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!me->arch.syminfo)
1441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	symbols = (void *) hdr + symtab->sh_offset;
1461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset;
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < me->arch.nsyms; i++) {
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (symbols[i].st_shndx == SHN_UNDEF &&
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    strcmp(strings + symbols[i].st_name,
1501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			   "_GLOBAL_OFFSET_TABLE_") == 0)
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			/* "Define" it as absolute. */
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			symbols[i].st_shndx = SHN_ABS;
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		me->arch.syminfo[i].got_offset = -1UL;
1541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		me->arch.syminfo[i].plt_offset = -1UL;
1551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		me->arch.syminfo[i].got_initialized = 0;
1561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		me->arch.syminfo[i].plt_initialized = 0;
1571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Search for got/plt relocations. */
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->arch.got_size = me->arch.plt_size = 0;
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < hdr->e_shnum; i++) {
1621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (sechdrs[i].sh_type != SHT_RELA)
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			continue;
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		nrela = sechdrs[i].sh_size / sizeof(Elf_Rela);
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		rela = (void *) hdr + sechdrs[i].sh_offset;
1661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		for (j = 0; j < nrela; j++)
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			check_rela(rela + j, me);
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Increase core size by size of got & plt and set start
1711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   offsets for got and plt. */
1721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->core_size = ALIGN(me->core_size, 4);
1731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->arch.got_offset = me->core_size;
1741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->core_size += me->arch.got_size;
1751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->arch.plt_offset = me->core_size;
1761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	me->core_size += me->arch.plt_size;
1771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
180083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefskystatic int apply_rela_bits(Elf_Addr loc, Elf_Addr val,
181083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			   int sign, int bits, int shift)
182083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky{
183083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	unsigned long umax;
184083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	long min, max;
185083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky
186083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	if (val & ((1UL << shift) - 1))
187083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		return -ENOEXEC;
188083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	if (sign) {
189083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		val = (Elf_Addr)(((long) val) >> shift);
190083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		min = -(1L << (bits - 1));
191083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		max = (1L << (bits - 1)) - 1;
192083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		if ((long) val < min || (long) val > max)
193083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			return -ENOEXEC;
194083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	} else {
195083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		val >>= shift;
196083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		umax = ((1UL << (bits - 1)) << 1) - 1;
197083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		if ((unsigned long) val > umax)
198083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			return -ENOEXEC;
199083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	}
200083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky
201083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	if (bits == 8)
202083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		*(unsigned char *) loc = val;
203083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	else if (bits == 12)
204083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		*(unsigned short *) loc = (val & 0xfff) |
205083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			(*(unsigned short *) loc & 0xf000);
206083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	else if (bits == 16)
207083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		*(unsigned short *) loc = val;
208083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	else if (bits == 20)
209083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		*(unsigned int *) loc = (val & 0xfff) << 16 |
210083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			(val & 0xff000) >> 4 |
211083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			(*(unsigned int *) loc & 0xf00000ff);
212083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	else if (bits == 32)
213083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		*(unsigned int *) loc = val;
214083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	else if (bits == 64)
215083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		*(unsigned long *) loc = val;
216083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	return 0;
217083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky}
218083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky
219083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefskystatic int apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab,
220083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		      const char *strtab, struct module *me)
2211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct mod_arch_syminfo *info;
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Elf_Addr loc, val;
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int r_type, r_sym;
22572a6b43e4bbb818ac1875a3a572cb2aa8c9af820Heiko Carstens	int rc = -ENOEXEC;
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* This is where to make the change */
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	loc = base + rela->r_offset;
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* This is the symbol it is referring to.  Note that all
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	   undefined symbols have been resolved.  */
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	r_sym = ELF_R_SYM(rela->r_info);
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	r_type = ELF_R_TYPE(rela->r_info);
2331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	info = me->arch.syminfo + r_sym;
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	val = symtab[r_sym].st_value;
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	switch (r_type) {
237e80cfc31d872b6b85b8966bce6ba80bee401a7ddHendrik Brueckner	case R_390_NONE:	/* No relocation.  */
238e80cfc31d872b6b85b8966bce6ba80bee401a7ddHendrik Brueckner		rc = 0;
239e80cfc31d872b6b85b8966bce6ba80bee401a7ddHendrik Brueckner		break;
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_8:		/* Direct 8 bit.   */
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_12:		/* Direct 12 bit.  */
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_16:		/* Direct 16 bit.  */
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_20:		/* Direct 20 bit.  */
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_32:		/* Direct 32 bit.  */
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_64:		/* Direct 64 bit.  */
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		val += rela->r_addend;
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (r_type == R_390_8)
248083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 8, 0);
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_12)
250083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 12, 0);
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_16)
252083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 16, 0);
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_20)
254083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 20, 0);
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_32)
256083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 32, 0);
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_64)
258083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 64, 0);
2591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PC16:	/* PC relative 16 bit.  */
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PC16DBL:	/* PC relative 16 bit shifted by 1.  */
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PC32DBL:	/* PC relative 32 bit shifted by 1.  */
2631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PC32:	/* PC relative 32 bit.  */
2641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PC64:	/* PC relative 64 bit.	*/
2651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		val += rela->r_addend - loc;
2661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (r_type == R_390_PC16)
267083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 16, 0);
2681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PC16DBL)
269083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 16, 1);
2701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PC32DBL)
271083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 32, 1);
2721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PC32)
273083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 32, 0);
2741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PC64)
275083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 64, 0);
2761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
2771da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT12:	/* 12 bit GOT offset.  */
2781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT16:	/* 16 bit GOT offset.  */
2791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT20:	/* 20 bit GOT offset.  */
2801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT32:	/* 32 bit GOT offset.  */
2811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOT64:	/* 64 bit GOT offset.  */
2821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTENT:	/* 32 bit PC rel. to GOT entry shifted by 1. */
2831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT12:	/* 12 bit offset to jump slot.	*/
2841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT20:	/* 20 bit offset to jump slot.  */
2851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT16:	/* 16 bit offset to jump slot.  */
2861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT32:	/* 32 bit offset to jump slot.  */
2871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLT64:	/* 64 bit offset to jump slot.	*/
2881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPLTENT:	/* 32 bit rel. offset to jump slot >> 1. */
2891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (info->got_initialized == 0) {
2901da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			Elf_Addr *gotent;
2911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			gotent = me->module_core + me->arch.got_offset +
2931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				info->got_offset;
2941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			*gotent = val;
2951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info->got_initialized = 1;
2961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
2971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		val = info->got_offset + rela->r_addend;
2981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (r_type == R_390_GOT12 ||
2991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    r_type == R_390_GOTPLT12)
300083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 12, 0);
3011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_GOT16 ||
3021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 r_type == R_390_GOTPLT16)
303083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 16, 0);
3041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_GOT20 ||
3051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 r_type == R_390_GOTPLT20)
306083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 20, 0);
3071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_GOT32 ||
3081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 r_type == R_390_GOTPLT32)
309083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 32, 0);
3101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_GOT64 ||
3111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 r_type == R_390_GOTPLT64)
312083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 64, 0);
313083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		else if (r_type == R_390_GOTENT ||
314083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			 r_type == R_390_GOTPLTENT) {
315083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			val += (Elf_Addr) me->module_core - loc;
316083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 32, 1);
317083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		}
3181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT16DBL:	/* 16 bit PC rel. PLT shifted by 1.  */
3201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT32DBL:	/* 32 bit PC rel. PLT shifted by 1.  */
3211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT32:	/* 32 bit PC relative PLT address.  */
3221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLT64:	/* 64 bit PC relative PLT address.  */
3231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLTOFF16:	/* 16 bit offset from GOT to PLT. */
3241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLTOFF32:	/* 32 bit offset from GOT to PLT. */
3251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_PLTOFF64:	/* 16 bit offset from GOT to PLT. */
3261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (info->plt_initialized == 0) {
3271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			unsigned int *ip;
3281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip = me->module_core + me->arch.plt_offset +
3291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				info->plt_offset;
330347a8dc3b815f0c0fa62a1df075184ffe4cbdcf1Martin Schwidefsky#ifndef CONFIG_64BIT
3311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */
3321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[1] = 0x100607f1;
3331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[2] = val;
334347a8dc3b815f0c0fa62a1df075184ffe4cbdcf1Martin Schwidefsky#else /* CONFIG_64BIT */
3351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */
3361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[1] = 0x100a0004;
3371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[2] = 0x07f10000;
3381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[3] = (unsigned int) (val >> 32);
3391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			ip[4] = (unsigned int) val;
340347a8dc3b815f0c0fa62a1df075184ffe4cbdcf1Martin Schwidefsky#endif /* CONFIG_64BIT */
3411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			info->plt_initialized = 1;
3421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
3431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (r_type == R_390_PLTOFF16 ||
344504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky		    r_type == R_390_PLTOFF32 ||
345504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky		    r_type == R_390_PLTOFF64)
3461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			val = me->arch.plt_offset - me->arch.got_offset +
3471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				info->plt_offset + rela->r_addend;
348504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky		else {
349504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky			if (!((r_type == R_390_PLT16DBL &&
350504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky			       val - loc + 0xffffUL < 0x1ffffeUL) ||
351504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky			      (r_type == R_390_PLT32DBL &&
352504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky			       val - loc + 0xffffffffULL < 0x1fffffffeULL)))
353504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky				val = (Elf_Addr) me->module_core +
354504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky					me->arch.plt_offset +
355504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky					info->plt_offset;
356504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky			val += rela->r_addend - loc;
357504665a91498f43d220b7d0942281067283a35f7Martin Schwidefsky		}
3581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (r_type == R_390_PLT16DBL)
359083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 16, 1);
3601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PLTOFF16)
361083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 16, 0);
3621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PLT32DBL)
363083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 32, 1);
3641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PLT32 ||
3651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 r_type == R_390_PLTOFF32)
366083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 32, 0);
3671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_PLT64 ||
3681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			 r_type == R_390_PLTOFF64)
369083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 64, 0);
3701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTOFF16:	/* 16 bit offset to GOT.  */
3721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTOFF32:	/* 32 bit offset to GOT.  */
3731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTOFF64:	/* 64 bit offset to GOT. */
3741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		val = val + rela->r_addend -
3751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			((Elf_Addr) me->module_core + me->arch.got_offset);
3761da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (r_type == R_390_GOTOFF16)
377083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 16, 0);
3781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_GOTOFF32)
379083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 32, 0);
3801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_GOTOFF64)
381083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 0, 64, 0);
3821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPC:	/* 32 bit PC relative offset to GOT. */
3841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GOTPCDBL:	/* 32 bit PC rel. off. to GOT shifted by 1. */
3851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		val = (Elf_Addr) me->module_core + me->arch.got_offset +
3861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			rela->r_addend - loc;
3871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (r_type == R_390_GOTPC)
388083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 32, 0);
3891da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		else if (r_type == R_390_GOTPCDBL)
390083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky			rc = apply_rela_bits(loc, val, 1, 32, 1);
3911da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		break;
3921da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_COPY:
3931da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_GLOB_DAT:	/* Create GOT entry.  */
3941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_JMP_SLOT:	/* Create PLT entry.  */
3951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	case R_390_RELATIVE:	/* Adjust by program base.  */
3961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* Only needed if we want to support loading of
3971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		   modules linked with -shared. */
398083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		return -ENOEXEC;
3991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	default:
400083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		printk(KERN_ERR "module %s: unknown relocation: %u\n",
4011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		       me->name, r_type);
4021da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOEXEC;
4031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
404083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	if (rc) {
405083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		printk(KERN_ERR "module %s: relocation error for symbol %s "
406083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		       "(r_type %i, value 0x%lx)\n",
407083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		       me->name, strtab + symtab[r_sym].st_name,
408083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		       r_type, (unsigned long) val);
409083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		return rc;
410083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky	}
4111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
414083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefskyint apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab,
415083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		       unsigned int symindex, unsigned int relsec,
416083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		       struct module *me)
4171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Elf_Addr base;
4191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Elf_Sym *symtab;
4201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	Elf_Rela *rela;
4211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	unsigned long i, n;
4221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int rc;
4231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	DEBUGP("Applying relocate section %u to %u\n",
4251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	       relsec, sechdrs[relsec].sh_info);
4261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	base = sechdrs[sechdrs[relsec].sh_info].sh_addr;
4271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	symtab = (Elf_Sym *) sechdrs[symindex].sh_addr;
4281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	rela = (Elf_Rela *) sechdrs[relsec].sh_addr;
4291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	n = sechdrs[relsec].sh_size / sizeof(Elf_Rela);
4301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	for (i = 0; i < n; i++, rela++) {
432083e14c09b7ae0247b9944a386fdc32cd0719da1Martin Schwidefsky		rc = apply_rela(rela, base, symtab, strtab, me);
4331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		if (rc)
4341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			return rc;
4351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
4361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return 0;
4371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
4381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
4391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsint module_finalize(const Elf_Ehdr *hdr,
4401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    const Elf_Shdr *sechdrs,
4411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		    struct module *me)
4421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
4431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	vfree(me->arch.syminfo);
4446a03f5f0a0a660895eac03749165c1f9313ef2b8Christian Borntraeger	me->arch.syminfo = NULL;
4455336377d6225959624146629ce3fc88ee8ecda3dLinus Torvalds	return 0;
4461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
447