1 /* 2 * Kernel module help for s390. 3 * 4 * S390 version 5 * Copyright IBM Corp. 2002, 2003 6 * Author(s): Arnd Bergmann (arndb@de.ibm.com) 7 * Martin Schwidefsky (schwidefsky@de.ibm.com) 8 * 9 * based on i386 version 10 * Copyright (C) 2001 Rusty Russell. 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License as published by 14 * the Free Software Foundation; either version 2 of the License, or 15 * (at your option) any later version. 16 * 17 * This program is distributed in the hope that it will be useful, 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 * GNU General Public License for more details. 21 * 22 * You should have received a copy of the GNU General Public License 23 * along with this program; if not, write to the Free Software 24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 */ 26 #include <linux/module.h> 27 #include <linux/elf.h> 28 #include <linux/vmalloc.h> 29 #include <linux/fs.h> 30 #include <linux/string.h> 31 #include <linux/kernel.h> 32 #include <linux/moduleloader.h> 33 #include <linux/bug.h> 34 35 #if 0 36 #define DEBUGP printk 37 #else 38 #define DEBUGP(fmt , ...) 39 #endif 40 41 #ifndef CONFIG_64BIT 42 #define PLT_ENTRY_SIZE 12 43 #else /* CONFIG_64BIT */ 44 #define PLT_ENTRY_SIZE 20 45 #endif /* CONFIG_64BIT */ 46 47 #ifdef CONFIG_64BIT 48 void *module_alloc(unsigned long size) 49 { 50 if (PAGE_ALIGN(size) > MODULES_LEN) 51 return NULL; 52 return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END, 53 GFP_KERNEL, PAGE_KERNEL, -1, 54 __builtin_return_address(0)); 55 } 56 #endif 57 58 /* Free memory returned from module_alloc */ 59 void module_free(struct module *mod, void *module_region) 60 { 61 if (mod) { 62 vfree(mod->arch.syminfo); 63 mod->arch.syminfo = NULL; 64 } 65 vfree(module_region); 66 } 67 68 static void 69 check_rela(Elf_Rela *rela, struct module *me) 70 { 71 struct mod_arch_syminfo *info; 72 73 info = me->arch.syminfo + ELF_R_SYM (rela->r_info); 74 switch (ELF_R_TYPE (rela->r_info)) { 75 case R_390_GOT12: /* 12 bit GOT offset. */ 76 case R_390_GOT16: /* 16 bit GOT offset. */ 77 case R_390_GOT20: /* 20 bit GOT offset. */ 78 case R_390_GOT32: /* 32 bit GOT offset. */ 79 case R_390_GOT64: /* 64 bit GOT offset. */ 80 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */ 81 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */ 82 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */ 83 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */ 84 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */ 85 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */ 86 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */ 87 if (info->got_offset == -1UL) { 88 info->got_offset = me->arch.got_size; 89 me->arch.got_size += sizeof(void*); 90 } 91 break; 92 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */ 93 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */ 94 case R_390_PLT32: /* 32 bit PC relative PLT address. */ 95 case R_390_PLT64: /* 64 bit PC relative PLT address. */ 96 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */ 97 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */ 98 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */ 99 if (info->plt_offset == -1UL) { 100 info->plt_offset = me->arch.plt_size; 101 me->arch.plt_size += PLT_ENTRY_SIZE; 102 } 103 break; 104 case R_390_COPY: 105 case R_390_GLOB_DAT: 106 case R_390_JMP_SLOT: 107 case R_390_RELATIVE: 108 /* Only needed if we want to support loading of 109 modules linked with -shared. */ 110 break; 111 } 112 } 113 114 /* 115 * Account for GOT and PLT relocations. We can't add sections for 116 * got and plt but we can increase the core module size. 117 */ 118 int 119 module_frob_arch_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, 120 char *secstrings, struct module *me) 121 { 122 Elf_Shdr *symtab; 123 Elf_Sym *symbols; 124 Elf_Rela *rela; 125 char *strings; 126 int nrela, i, j; 127 128 /* Find symbol table and string table. */ 129 symtab = NULL; 130 for (i = 0; i < hdr->e_shnum; i++) 131 switch (sechdrs[i].sh_type) { 132 case SHT_SYMTAB: 133 symtab = sechdrs + i; 134 break; 135 } 136 if (!symtab) { 137 printk(KERN_ERR "module %s: no symbol table\n", me->name); 138 return -ENOEXEC; 139 } 140 141 /* Allocate one syminfo structure per symbol. */ 142 me->arch.nsyms = symtab->sh_size / sizeof(Elf_Sym); 143 me->arch.syminfo = vmalloc(me->arch.nsyms * 144 sizeof(struct mod_arch_syminfo)); 145 if (!me->arch.syminfo) 146 return -ENOMEM; 147 symbols = (void *) hdr + symtab->sh_offset; 148 strings = (void *) hdr + sechdrs[symtab->sh_link].sh_offset; 149 for (i = 0; i < me->arch.nsyms; i++) { 150 if (symbols[i].st_shndx == SHN_UNDEF && 151 strcmp(strings + symbols[i].st_name, 152 "_GLOBAL_OFFSET_TABLE_") == 0) 153 /* "Define" it as absolute. */ 154 symbols[i].st_shndx = SHN_ABS; 155 me->arch.syminfo[i].got_offset = -1UL; 156 me->arch.syminfo[i].plt_offset = -1UL; 157 me->arch.syminfo[i].got_initialized = 0; 158 me->arch.syminfo[i].plt_initialized = 0; 159 } 160 161 /* Search for got/plt relocations. */ 162 me->arch.got_size = me->arch.plt_size = 0; 163 for (i = 0; i < hdr->e_shnum; i++) { 164 if (sechdrs[i].sh_type != SHT_RELA) 165 continue; 166 nrela = sechdrs[i].sh_size / sizeof(Elf_Rela); 167 rela = (void *) hdr + sechdrs[i].sh_offset; 168 for (j = 0; j < nrela; j++) 169 check_rela(rela + j, me); 170 } 171 172 /* Increase core size by size of got & plt and set start 173 offsets for got and plt. */ 174 me->core_size = ALIGN(me->core_size, 4); 175 me->arch.got_offset = me->core_size; 176 me->core_size += me->arch.got_size; 177 me->arch.plt_offset = me->core_size; 178 me->core_size += me->arch.plt_size; 179 return 0; 180 } 181 182 static int 183 apply_rela(Elf_Rela *rela, Elf_Addr base, Elf_Sym *symtab, 184 struct module *me) 185 { 186 struct mod_arch_syminfo *info; 187 Elf_Addr loc, val; 188 int r_type, r_sym; 189 190 /* This is where to make the change */ 191 loc = base + rela->r_offset; 192 /* This is the symbol it is referring to. Note that all 193 undefined symbols have been resolved. */ 194 r_sym = ELF_R_SYM(rela->r_info); 195 r_type = ELF_R_TYPE(rela->r_info); 196 info = me->arch.syminfo + r_sym; 197 val = symtab[r_sym].st_value; 198 199 switch (r_type) { 200 case R_390_8: /* Direct 8 bit. */ 201 case R_390_12: /* Direct 12 bit. */ 202 case R_390_16: /* Direct 16 bit. */ 203 case R_390_20: /* Direct 20 bit. */ 204 case R_390_32: /* Direct 32 bit. */ 205 case R_390_64: /* Direct 64 bit. */ 206 val += rela->r_addend; 207 if (r_type == R_390_8) 208 *(unsigned char *) loc = val; 209 else if (r_type == R_390_12) 210 *(unsigned short *) loc = (val & 0xfff) | 211 (*(unsigned short *) loc & 0xf000); 212 else if (r_type == R_390_16) 213 *(unsigned short *) loc = val; 214 else if (r_type == R_390_20) 215 *(unsigned int *) loc = 216 (*(unsigned int *) loc & 0xf00000ff) | 217 (val & 0xfff) << 16 | (val & 0xff000) >> 4; 218 else if (r_type == R_390_32) 219 *(unsigned int *) loc = val; 220 else if (r_type == R_390_64) 221 *(unsigned long *) loc = val; 222 break; 223 case R_390_PC16: /* PC relative 16 bit. */ 224 case R_390_PC16DBL: /* PC relative 16 bit shifted by 1. */ 225 case R_390_PC32DBL: /* PC relative 32 bit shifted by 1. */ 226 case R_390_PC32: /* PC relative 32 bit. */ 227 case R_390_PC64: /* PC relative 64 bit. */ 228 val += rela->r_addend - loc; 229 if (r_type == R_390_PC16) 230 *(unsigned short *) loc = val; 231 else if (r_type == R_390_PC16DBL) 232 *(unsigned short *) loc = val >> 1; 233 else if (r_type == R_390_PC32DBL) 234 *(unsigned int *) loc = val >> 1; 235 else if (r_type == R_390_PC32) 236 *(unsigned int *) loc = val; 237 else if (r_type == R_390_PC64) 238 *(unsigned long *) loc = val; 239 break; 240 case R_390_GOT12: /* 12 bit GOT offset. */ 241 case R_390_GOT16: /* 16 bit GOT offset. */ 242 case R_390_GOT20: /* 20 bit GOT offset. */ 243 case R_390_GOT32: /* 32 bit GOT offset. */ 244 case R_390_GOT64: /* 64 bit GOT offset. */ 245 case R_390_GOTENT: /* 32 bit PC rel. to GOT entry shifted by 1. */ 246 case R_390_GOTPLT12: /* 12 bit offset to jump slot. */ 247 case R_390_GOTPLT20: /* 20 bit offset to jump slot. */ 248 case R_390_GOTPLT16: /* 16 bit offset to jump slot. */ 249 case R_390_GOTPLT32: /* 32 bit offset to jump slot. */ 250 case R_390_GOTPLT64: /* 64 bit offset to jump slot. */ 251 case R_390_GOTPLTENT: /* 32 bit rel. offset to jump slot >> 1. */ 252 if (info->got_initialized == 0) { 253 Elf_Addr *gotent; 254 255 gotent = me->module_core + me->arch.got_offset + 256 info->got_offset; 257 *gotent = val; 258 info->got_initialized = 1; 259 } 260 val = info->got_offset + rela->r_addend; 261 if (r_type == R_390_GOT12 || 262 r_type == R_390_GOTPLT12) 263 *(unsigned short *) loc = (val & 0xfff) | 264 (*(unsigned short *) loc & 0xf000); 265 else if (r_type == R_390_GOT16 || 266 r_type == R_390_GOTPLT16) 267 *(unsigned short *) loc = val; 268 else if (r_type == R_390_GOT20 || 269 r_type == R_390_GOTPLT20) 270 *(unsigned int *) loc = 271 (*(unsigned int *) loc & 0xf00000ff) | 272 (val & 0xfff) << 16 | (val & 0xff000) >> 4; 273 else if (r_type == R_390_GOT32 || 274 r_type == R_390_GOTPLT32) 275 *(unsigned int *) loc = val; 276 else if (r_type == R_390_GOTENT || 277 r_type == R_390_GOTPLTENT) 278 *(unsigned int *) loc = 279 (val + (Elf_Addr) me->module_core - loc) >> 1; 280 else if (r_type == R_390_GOT64 || 281 r_type == R_390_GOTPLT64) 282 *(unsigned long *) loc = val; 283 break; 284 case R_390_PLT16DBL: /* 16 bit PC rel. PLT shifted by 1. */ 285 case R_390_PLT32DBL: /* 32 bit PC rel. PLT shifted by 1. */ 286 case R_390_PLT32: /* 32 bit PC relative PLT address. */ 287 case R_390_PLT64: /* 64 bit PC relative PLT address. */ 288 case R_390_PLTOFF16: /* 16 bit offset from GOT to PLT. */ 289 case R_390_PLTOFF32: /* 32 bit offset from GOT to PLT. */ 290 case R_390_PLTOFF64: /* 16 bit offset from GOT to PLT. */ 291 if (info->plt_initialized == 0) { 292 unsigned int *ip; 293 ip = me->module_core + me->arch.plt_offset + 294 info->plt_offset; 295 #ifndef CONFIG_64BIT 296 ip[0] = 0x0d105810; /* basr 1,0; l 1,6(1); br 1 */ 297 ip[1] = 0x100607f1; 298 ip[2] = val; 299 #else /* CONFIG_64BIT */ 300 ip[0] = 0x0d10e310; /* basr 1,0; lg 1,10(1); br 1 */ 301 ip[1] = 0x100a0004; 302 ip[2] = 0x07f10000; 303 ip[3] = (unsigned int) (val >> 32); 304 ip[4] = (unsigned int) val; 305 #endif /* CONFIG_64BIT */ 306 info->plt_initialized = 1; 307 } 308 if (r_type == R_390_PLTOFF16 || 309 r_type == R_390_PLTOFF32 || 310 r_type == R_390_PLTOFF64) 311 val = me->arch.plt_offset - me->arch.got_offset + 312 info->plt_offset + rela->r_addend; 313 else { 314 if (!((r_type == R_390_PLT16DBL && 315 val - loc + 0xffffUL < 0x1ffffeUL) || 316 (r_type == R_390_PLT32DBL && 317 val - loc + 0xffffffffULL < 0x1fffffffeULL))) 318 val = (Elf_Addr) me->module_core + 319 me->arch.plt_offset + 320 info->plt_offset; 321 val += rela->r_addend - loc; 322 } 323 if (r_type == R_390_PLT16DBL) 324 *(unsigned short *) loc = val >> 1; 325 else if (r_type == R_390_PLTOFF16) 326 *(unsigned short *) loc = val; 327 else if (r_type == R_390_PLT32DBL) 328 *(unsigned int *) loc = val >> 1; 329 else if (r_type == R_390_PLT32 || 330 r_type == R_390_PLTOFF32) 331 *(unsigned int *) loc = val; 332 else if (r_type == R_390_PLT64 || 333 r_type == R_390_PLTOFF64) 334 *(unsigned long *) loc = val; 335 break; 336 case R_390_GOTOFF16: /* 16 bit offset to GOT. */ 337 case R_390_GOTOFF32: /* 32 bit offset to GOT. */ 338 case R_390_GOTOFF64: /* 64 bit offset to GOT. */ 339 val = val + rela->r_addend - 340 ((Elf_Addr) me->module_core + me->arch.got_offset); 341 if (r_type == R_390_GOTOFF16) 342 *(unsigned short *) loc = val; 343 else if (r_type == R_390_GOTOFF32) 344 *(unsigned int *) loc = val; 345 else if (r_type == R_390_GOTOFF64) 346 *(unsigned long *) loc = val; 347 break; 348 case R_390_GOTPC: /* 32 bit PC relative offset to GOT. */ 349 case R_390_GOTPCDBL: /* 32 bit PC rel. off. to GOT shifted by 1. */ 350 val = (Elf_Addr) me->module_core + me->arch.got_offset + 351 rela->r_addend - loc; 352 if (r_type == R_390_GOTPC) 353 *(unsigned int *) loc = val; 354 else if (r_type == R_390_GOTPCDBL) 355 *(unsigned int *) loc = val >> 1; 356 break; 357 case R_390_COPY: 358 case R_390_GLOB_DAT: /* Create GOT entry. */ 359 case R_390_JMP_SLOT: /* Create PLT entry. */ 360 case R_390_RELATIVE: /* Adjust by program base. */ 361 /* Only needed if we want to support loading of 362 modules linked with -shared. */ 363 break; 364 default: 365 printk(KERN_ERR "module %s: Unknown relocation: %u\n", 366 me->name, r_type); 367 return -ENOEXEC; 368 } 369 return 0; 370 } 371 372 int 373 apply_relocate_add(Elf_Shdr *sechdrs, const char *strtab, 374 unsigned int symindex, unsigned int relsec, 375 struct module *me) 376 { 377 Elf_Addr base; 378 Elf_Sym *symtab; 379 Elf_Rela *rela; 380 unsigned long i, n; 381 int rc; 382 383 DEBUGP("Applying relocate section %u to %u\n", 384 relsec, sechdrs[relsec].sh_info); 385 base = sechdrs[sechdrs[relsec].sh_info].sh_addr; 386 symtab = (Elf_Sym *) sechdrs[symindex].sh_addr; 387 rela = (Elf_Rela *) sechdrs[relsec].sh_addr; 388 n = sechdrs[relsec].sh_size / sizeof(Elf_Rela); 389 390 for (i = 0; i < n; i++, rela++) { 391 rc = apply_rela(rela, base, symtab, me); 392 if (rc) 393 return rc; 394 } 395 return 0; 396 } 397 398 int module_finalize(const Elf_Ehdr *hdr, 399 const Elf_Shdr *sechdrs, 400 struct module *me) 401 { 402 vfree(me->arch.syminfo); 403 me->arch.syminfo = NULL; 404 return 0; 405 } 406