1 /* Kernel dynamically loadable module help for PARISC. 2 * 3 * The best reference for this stuff is probably the Processor- 4 * Specific ELF Supplement for PA-RISC: 5 * http://ftp.parisc-linux.org/docs/arch/elf-pa-hp.pdf 6 * 7 * Linux/PA-RISC Project (http://www.parisc-linux.org/) 8 * Copyright (C) 2003 Randolph Chung <tausq at debian . org> 9 * Copyright (C) 2008 Helge Deller <deller@gmx.de> 10 * 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 * 27 * Notes: 28 * - PLT stub handling 29 * On 32bit (and sometimes 64bit) and with big kernel modules like xfs or 30 * ipv6 the relocation types R_PARISC_PCREL17F and R_PARISC_PCREL22F may 31 * fail to reach their PLT stub if we only create one big stub array for 32 * all sections at the beginning of the core or init section. 33 * Instead we now insert individual PLT stub entries directly in front of 34 * of the code sections where the stubs are actually called. 35 * This reduces the distance between the PCREL location and the stub entry 36 * so that the relocations can be fulfilled. 37 * While calculating the final layout of the kernel module in memory, the 38 * kernel module loader calls arch_mod_section_prepend() to request the 39 * to be reserved amount of memory in front of each individual section. 40 * 41 * - SEGREL32 handling 42 * We are not doing SEGREL32 handling correctly. According to the ABI, we 43 * should do a value offset, like this: 44 * if (in_init(me, (void *)val)) 45 * val -= (uint32_t)me->module_init; 46 * else 47 * val -= (uint32_t)me->module_core; 48 * However, SEGREL32 is used only for PARISC unwind entries, and we want 49 * those entries to have an absolute address, and not just an offset. 50 * 51 * The unwind table mechanism has the ability to specify an offset for 52 * the unwind table; however, because we split off the init functions into 53 * a different piece of memory, it is not possible to do this using a 54 * single offset. Instead, we use the above hack for now. 55 */ 56 57 #include <linux/moduleloader.h> 58 #include <linux/elf.h> 59 #include <linux/vmalloc.h> 60 #include <linux/fs.h> 61 #include <linux/string.h> 62 #include <linux/kernel.h> 63 #include <linux/bug.h> 64 65 #include <asm/unwind.h> 66 67 #if 0 68 #define DEBUGP printk 69 #else 70 #define DEBUGP(fmt...) 71 #endif 72 73 #define RELOC_REACHABLE(val, bits) \ 74 (( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \ 75 ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \ 76 0 : 1) 77 78 #define CHECK_RELOC(val, bits) \ 79 if (!RELOC_REACHABLE(val, bits)) { \ 80 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \ 81 me->name, strtab + sym->st_name, (unsigned long)val, bits); \ 82 return -ENOEXEC; \ 83 } 84 85 /* Maximum number of GOT entries. We use a long displacement ldd from 86 * the bottom of the table, which has a maximum signed displacement of 87 * 0x3fff; however, since we're only going forward, this becomes 88 * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have 89 * at most 1023 entries */ 90 #define MAX_GOTS 1023 91 92 /* three functions to determine where in the module core 93 * or init pieces the location is */ 94 static inline int in_init(struct module *me, void *loc) 95 { 96 return (loc >= me->module_init && 97 loc <= (me->module_init + me->init_size)); 98 } 99 100 static inline int in_core(struct module *me, void *loc) 101 { 102 return (loc >= me->module_core && 103 loc <= (me->module_core + me->core_size)); 104 } 105 106 static inline int in_local(struct module *me, void *loc) 107 { 108 return in_init(me, loc) || in_core(me, loc); 109 } 110 111 #ifndef CONFIG_64BIT 112 struct got_entry { 113 Elf32_Addr addr; 114 }; 115 116 struct stub_entry { 117 Elf32_Word insns[2]; /* each stub entry has two insns */ 118 }; 119 #else 120 struct got_entry { 121 Elf64_Addr addr; 122 }; 123 124 struct stub_entry { 125 Elf64_Word insns[4]; /* each stub entry has four insns */ 126 }; 127 #endif 128 129 /* Field selection types defined by hppa */ 130 #define rnd(x) (((x)+0x1000)&~0x1fff) 131 /* fsel: full 32 bits */ 132 #define fsel(v,a) ((v)+(a)) 133 /* lsel: select left 21 bits */ 134 #define lsel(v,a) (((v)+(a))>>11) 135 /* rsel: select right 11 bits */ 136 #define rsel(v,a) (((v)+(a))&0x7ff) 137 /* lrsel with rounding of addend to nearest 8k */ 138 #define lrsel(v,a) (((v)+rnd(a))>>11) 139 /* rrsel with rounding of addend to nearest 8k */ 140 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a))) 141 142 #define mask(x,sz) ((x) & ~((1<<(sz))-1)) 143 144 145 /* The reassemble_* functions prepare an immediate value for 146 insertion into an opcode. pa-risc uses all sorts of weird bitfields 147 in the instruction to hold the value. */ 148 static inline int reassemble_14(int as14) 149 { 150 return (((as14 & 0x1fff) << 1) | 151 ((as14 & 0x2000) >> 13)); 152 } 153 154 static inline int reassemble_17(int as17) 155 { 156 return (((as17 & 0x10000) >> 16) | 157 ((as17 & 0x0f800) << 5) | 158 ((as17 & 0x00400) >> 8) | 159 ((as17 & 0x003ff) << 3)); 160 } 161 162 static inline int reassemble_21(int as21) 163 { 164 return (((as21 & 0x100000) >> 20) | 165 ((as21 & 0x0ffe00) >> 8) | 166 ((as21 & 0x000180) << 7) | 167 ((as21 & 0x00007c) << 14) | 168 ((as21 & 0x000003) << 12)); 169 } 170 171 static inline int reassemble_22(int as22) 172 { 173 return (((as22 & 0x200000) >> 21) | 174 ((as22 & 0x1f0000) << 5) | 175 ((as22 & 0x00f800) << 5) | 176 ((as22 & 0x000400) >> 8) | 177 ((as22 & 0x0003ff) << 3)); 178 } 179 180 void *module_alloc(unsigned long size) 181 { 182 if (size == 0) 183 return NULL; 184 return vmalloc(size); 185 } 186 187 #ifndef CONFIG_64BIT 188 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n) 189 { 190 return 0; 191 } 192 193 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n) 194 { 195 return 0; 196 } 197 198 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n) 199 { 200 unsigned long cnt = 0; 201 202 for (; n > 0; n--, rela++) 203 { 204 switch (ELF32_R_TYPE(rela->r_info)) { 205 case R_PARISC_PCREL17F: 206 case R_PARISC_PCREL22F: 207 cnt++; 208 } 209 } 210 211 return cnt; 212 } 213 #else 214 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n) 215 { 216 unsigned long cnt = 0; 217 218 for (; n > 0; n--, rela++) 219 { 220 switch (ELF64_R_TYPE(rela->r_info)) { 221 case R_PARISC_LTOFF21L: 222 case R_PARISC_LTOFF14R: 223 case R_PARISC_PCREL22F: 224 cnt++; 225 } 226 } 227 228 return cnt; 229 } 230 231 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n) 232 { 233 unsigned long cnt = 0; 234 235 for (; n > 0; n--, rela++) 236 { 237 switch (ELF64_R_TYPE(rela->r_info)) { 238 case R_PARISC_FPTR64: 239 cnt++; 240 } 241 } 242 243 return cnt; 244 } 245 246 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n) 247 { 248 unsigned long cnt = 0; 249 250 for (; n > 0; n--, rela++) 251 { 252 switch (ELF64_R_TYPE(rela->r_info)) { 253 case R_PARISC_PCREL22F: 254 cnt++; 255 } 256 } 257 258 return cnt; 259 } 260 #endif 261 262 263 /* Free memory returned from module_alloc */ 264 void module_free(struct module *mod, void *module_region) 265 { 266 kfree(mod->arch.section); 267 mod->arch.section = NULL; 268 269 vfree(module_region); 270 } 271 272 /* Additional bytes needed in front of individual sections */ 273 unsigned int arch_mod_section_prepend(struct module *mod, 274 unsigned int section) 275 { 276 /* size needed for all stubs of this section (including 277 * one additional for correct alignment of the stubs) */ 278 return (mod->arch.section[section].stub_entries + 1) 279 * sizeof(struct stub_entry); 280 } 281 282 #define CONST 283 int module_frob_arch_sections(CONST Elf_Ehdr *hdr, 284 CONST Elf_Shdr *sechdrs, 285 CONST char *secstrings, 286 struct module *me) 287 { 288 unsigned long gots = 0, fdescs = 0, len; 289 unsigned int i; 290 291 len = hdr->e_shnum * sizeof(me->arch.section[0]); 292 me->arch.section = kzalloc(len, GFP_KERNEL); 293 if (!me->arch.section) 294 return -ENOMEM; 295 296 for (i = 1; i < hdr->e_shnum; i++) { 297 const Elf_Rela *rels = (void *)sechdrs[i].sh_addr; 298 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels); 299 unsigned int count, s; 300 301 if (strncmp(secstrings + sechdrs[i].sh_name, 302 ".PARISC.unwind", 14) == 0) 303 me->arch.unwind_section = i; 304 305 if (sechdrs[i].sh_type != SHT_RELA) 306 continue; 307 308 /* some of these are not relevant for 32-bit/64-bit 309 * we leave them here to make the code common. the 310 * compiler will do its thing and optimize out the 311 * stuff we don't need 312 */ 313 gots += count_gots(rels, nrels); 314 fdescs += count_fdescs(rels, nrels); 315 316 /* XXX: By sorting the relocs and finding duplicate entries 317 * we could reduce the number of necessary stubs and save 318 * some memory. */ 319 count = count_stubs(rels, nrels); 320 if (!count) 321 continue; 322 323 /* so we need relocation stubs. reserve necessary memory. */ 324 /* sh_info gives the section for which we need to add stubs. */ 325 s = sechdrs[i].sh_info; 326 327 /* each code section should only have one relocation section */ 328 WARN_ON(me->arch.section[s].stub_entries); 329 330 /* store number of stubs we need for this section */ 331 me->arch.section[s].stub_entries += count; 332 } 333 334 /* align things a bit */ 335 me->core_size = ALIGN(me->core_size, 16); 336 me->arch.got_offset = me->core_size; 337 me->core_size += gots * sizeof(struct got_entry); 338 339 me->core_size = ALIGN(me->core_size, 16); 340 me->arch.fdesc_offset = me->core_size; 341 me->core_size += fdescs * sizeof(Elf_Fdesc); 342 343 me->arch.got_max = gots; 344 me->arch.fdesc_max = fdescs; 345 346 return 0; 347 } 348 349 #ifdef CONFIG_64BIT 350 static Elf64_Word get_got(struct module *me, unsigned long value, long addend) 351 { 352 unsigned int i; 353 struct got_entry *got; 354 355 value += addend; 356 357 BUG_ON(value == 0); 358 359 got = me->module_core + me->arch.got_offset; 360 for (i = 0; got[i].addr; i++) 361 if (got[i].addr == value) 362 goto out; 363 364 BUG_ON(++me->arch.got_count > me->arch.got_max); 365 366 got[i].addr = value; 367 out: 368 DEBUGP("GOT ENTRY %d[%x] val %lx\n", i, i*sizeof(struct got_entry), 369 value); 370 return i * sizeof(struct got_entry); 371 } 372 #endif /* CONFIG_64BIT */ 373 374 #ifdef CONFIG_64BIT 375 static Elf_Addr get_fdesc(struct module *me, unsigned long value) 376 { 377 Elf_Fdesc *fdesc = me->module_core + me->arch.fdesc_offset; 378 379 if (!value) { 380 printk(KERN_ERR "%s: zero OPD requested!\n", me->name); 381 return 0; 382 } 383 384 /* Look for existing fdesc entry. */ 385 while (fdesc->addr) { 386 if (fdesc->addr == value) 387 return (Elf_Addr)fdesc; 388 fdesc++; 389 } 390 391 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max); 392 393 /* Create new one */ 394 fdesc->addr = value; 395 fdesc->gp = (Elf_Addr)me->module_core + me->arch.got_offset; 396 return (Elf_Addr)fdesc; 397 } 398 #endif /* CONFIG_64BIT */ 399 400 enum elf_stub_type { 401 ELF_STUB_GOT, 402 ELF_STUB_MILLI, 403 ELF_STUB_DIRECT, 404 }; 405 406 static Elf_Addr get_stub(struct module *me, unsigned long value, long addend, 407 enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec) 408 { 409 struct stub_entry *stub; 410 411 /* initialize stub_offset to point in front of the section */ 412 if (!me->arch.section[targetsec].stub_offset) { 413 loc0 -= (me->arch.section[targetsec].stub_entries + 1) * 414 sizeof(struct stub_entry); 415 /* get correct alignment for the stubs */ 416 loc0 = ALIGN(loc0, sizeof(struct stub_entry)); 417 me->arch.section[targetsec].stub_offset = loc0; 418 } 419 420 /* get address of stub entry */ 421 stub = (void *) me->arch.section[targetsec].stub_offset; 422 me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry); 423 424 /* do not write outside available stub area */ 425 BUG_ON(0 == me->arch.section[targetsec].stub_entries--); 426 427 428 #ifndef CONFIG_64BIT 429 /* for 32-bit the stub looks like this: 430 * ldil L'XXX,%r1 431 * be,n R'XXX(%sr4,%r1) 432 */ 433 //value = *(unsigned long *)((value + addend) & ~3); /* why? */ 434 435 stub->insns[0] = 0x20200000; /* ldil L'XXX,%r1 */ 436 stub->insns[1] = 0xe0202002; /* be,n R'XXX(%sr4,%r1) */ 437 438 stub->insns[0] |= reassemble_21(lrsel(value, addend)); 439 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4); 440 441 #else 442 /* for 64-bit we have three kinds of stubs: 443 * for normal function calls: 444 * ldd 0(%dp),%dp 445 * ldd 10(%dp), %r1 446 * bve (%r1) 447 * ldd 18(%dp), %dp 448 * 449 * for millicode: 450 * ldil 0, %r1 451 * ldo 0(%r1), %r1 452 * ldd 10(%r1), %r1 453 * bve,n (%r1) 454 * 455 * for direct branches (jumps between different section of the 456 * same module): 457 * ldil 0, %r1 458 * ldo 0(%r1), %r1 459 * bve,n (%r1) 460 */ 461 switch (stub_type) { 462 case ELF_STUB_GOT: 463 stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */ 464 stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */ 465 stub->insns[2] = 0xe820d000; /* bve (%r1) */ 466 stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */ 467 468 stub->insns[0] |= reassemble_14(get_got(me, value, addend) & 0x3fff); 469 break; 470 case ELF_STUB_MILLI: 471 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */ 472 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */ 473 stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */ 474 stub->insns[3] = 0xe820d002; /* bve,n (%r1) */ 475 476 stub->insns[0] |= reassemble_21(lrsel(value, addend)); 477 stub->insns[1] |= reassemble_14(rrsel(value, addend)); 478 break; 479 case ELF_STUB_DIRECT: 480 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */ 481 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */ 482 stub->insns[2] = 0xe820d002; /* bve,n (%r1) */ 483 484 stub->insns[0] |= reassemble_21(lrsel(value, addend)); 485 stub->insns[1] |= reassemble_14(rrsel(value, addend)); 486 break; 487 } 488 489 #endif 490 491 return (Elf_Addr)stub; 492 } 493 494 int apply_relocate(Elf_Shdr *sechdrs, 495 const char *strtab, 496 unsigned int symindex, 497 unsigned int relsec, 498 struct module *me) 499 { 500 /* parisc should not need this ... */ 501 printk(KERN_ERR "module %s: RELOCATION unsupported\n", 502 me->name); 503 return -ENOEXEC; 504 } 505 506 #ifndef CONFIG_64BIT 507 int apply_relocate_add(Elf_Shdr *sechdrs, 508 const char *strtab, 509 unsigned int symindex, 510 unsigned int relsec, 511 struct module *me) 512 { 513 int i; 514 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; 515 Elf32_Sym *sym; 516 Elf32_Word *loc; 517 Elf32_Addr val; 518 Elf32_Sword addend; 519 Elf32_Addr dot; 520 Elf_Addr loc0; 521 unsigned int targetsec = sechdrs[relsec].sh_info; 522 //unsigned long dp = (unsigned long)$global$; 523 register unsigned long dp asm ("r27"); 524 525 DEBUGP("Applying relocate section %u to %u\n", relsec, 526 targetsec); 527 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 528 /* This is where to make the change */ 529 loc = (void *)sechdrs[targetsec].sh_addr 530 + rel[i].r_offset; 531 /* This is the start of the target section */ 532 loc0 = sechdrs[targetsec].sh_addr; 533 /* This is the symbol it is referring to */ 534 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr 535 + ELF32_R_SYM(rel[i].r_info); 536 if (!sym->st_value) { 537 printk(KERN_WARNING "%s: Unknown symbol %s\n", 538 me->name, strtab + sym->st_name); 539 return -ENOENT; 540 } 541 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03; 542 dot = (Elf32_Addr)loc & ~0x03; 543 544 val = sym->st_value; 545 addend = rel[i].r_addend; 546 547 #if 0 548 #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t : 549 DEBUGP("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n", 550 strtab + sym->st_name, 551 (uint32_t)loc, val, addend, 552 r(R_PARISC_PLABEL32) 553 r(R_PARISC_DIR32) 554 r(R_PARISC_DIR21L) 555 r(R_PARISC_DIR14R) 556 r(R_PARISC_SEGREL32) 557 r(R_PARISC_DPREL21L) 558 r(R_PARISC_DPREL14R) 559 r(R_PARISC_PCREL17F) 560 r(R_PARISC_PCREL22F) 561 "UNKNOWN"); 562 #undef r 563 #endif 564 565 switch (ELF32_R_TYPE(rel[i].r_info)) { 566 case R_PARISC_PLABEL32: 567 /* 32-bit function address */ 568 /* no function descriptors... */ 569 *loc = fsel(val, addend); 570 break; 571 case R_PARISC_DIR32: 572 /* direct 32-bit ref */ 573 *loc = fsel(val, addend); 574 break; 575 case R_PARISC_DIR21L: 576 /* left 21 bits of effective address */ 577 val = lrsel(val, addend); 578 *loc = mask(*loc, 21) | reassemble_21(val); 579 break; 580 case R_PARISC_DIR14R: 581 /* right 14 bits of effective address */ 582 val = rrsel(val, addend); 583 *loc = mask(*loc, 14) | reassemble_14(val); 584 break; 585 case R_PARISC_SEGREL32: 586 /* 32-bit segment relative address */ 587 /* See note about special handling of SEGREL32 at 588 * the beginning of this file. 589 */ 590 *loc = fsel(val, addend); 591 break; 592 case R_PARISC_DPREL21L: 593 /* left 21 bit of relative address */ 594 val = lrsel(val - dp, addend); 595 *loc = mask(*loc, 21) | reassemble_21(val); 596 break; 597 case R_PARISC_DPREL14R: 598 /* right 14 bit of relative address */ 599 val = rrsel(val - dp, addend); 600 *loc = mask(*loc, 14) | reassemble_14(val); 601 break; 602 case R_PARISC_PCREL17F: 603 /* 17-bit PC relative address */ 604 /* calculate direct call offset */ 605 val += addend; 606 val = (val - dot - 8)/4; 607 if (!RELOC_REACHABLE(val, 17)) { 608 /* direct distance too far, create 609 * stub entry instead */ 610 val = get_stub(me, sym->st_value, addend, 611 ELF_STUB_DIRECT, loc0, targetsec); 612 val = (val - dot - 8)/4; 613 CHECK_RELOC(val, 17); 614 } 615 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val); 616 break; 617 case R_PARISC_PCREL22F: 618 /* 22-bit PC relative address; only defined for pa20 */ 619 /* calculate direct call offset */ 620 val += addend; 621 val = (val - dot - 8)/4; 622 if (!RELOC_REACHABLE(val, 22)) { 623 /* direct distance too far, create 624 * stub entry instead */ 625 val = get_stub(me, sym->st_value, addend, 626 ELF_STUB_DIRECT, loc0, targetsec); 627 val = (val - dot - 8)/4; 628 CHECK_RELOC(val, 22); 629 } 630 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val); 631 break; 632 633 default: 634 printk(KERN_ERR "module %s: Unknown relocation: %u\n", 635 me->name, ELF32_R_TYPE(rel[i].r_info)); 636 return -ENOEXEC; 637 } 638 } 639 640 return 0; 641 } 642 643 #else 644 int apply_relocate_add(Elf_Shdr *sechdrs, 645 const char *strtab, 646 unsigned int symindex, 647 unsigned int relsec, 648 struct module *me) 649 { 650 int i; 651 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr; 652 Elf64_Sym *sym; 653 Elf64_Word *loc; 654 Elf64_Xword *loc64; 655 Elf64_Addr val; 656 Elf64_Sxword addend; 657 Elf64_Addr dot; 658 Elf_Addr loc0; 659 unsigned int targetsec = sechdrs[relsec].sh_info; 660 661 DEBUGP("Applying relocate section %u to %u\n", relsec, 662 targetsec); 663 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { 664 /* This is where to make the change */ 665 loc = (void *)sechdrs[targetsec].sh_addr 666 + rel[i].r_offset; 667 /* This is the start of the target section */ 668 loc0 = sechdrs[targetsec].sh_addr; 669 /* This is the symbol it is referring to */ 670 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 671 + ELF64_R_SYM(rel[i].r_info); 672 if (!sym->st_value) { 673 printk(KERN_WARNING "%s: Unknown symbol %s\n", 674 me->name, strtab + sym->st_name); 675 return -ENOENT; 676 } 677 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03; 678 dot = (Elf64_Addr)loc & ~0x03; 679 loc64 = (Elf64_Xword *)loc; 680 681 val = sym->st_value; 682 addend = rel[i].r_addend; 683 684 #if 0 685 #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t : 686 printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n", 687 strtab + sym->st_name, 688 loc, val, addend, 689 r(R_PARISC_LTOFF14R) 690 r(R_PARISC_LTOFF21L) 691 r(R_PARISC_PCREL22F) 692 r(R_PARISC_DIR64) 693 r(R_PARISC_SEGREL32) 694 r(R_PARISC_FPTR64) 695 "UNKNOWN"); 696 #undef r 697 #endif 698 699 switch (ELF64_R_TYPE(rel[i].r_info)) { 700 case R_PARISC_LTOFF21L: 701 /* LT-relative; left 21 bits */ 702 val = get_got(me, val, addend); 703 DEBUGP("LTOFF21L Symbol %s loc %p val %lx\n", 704 strtab + sym->st_name, 705 loc, val); 706 val = lrsel(val, 0); 707 *loc = mask(*loc, 21) | reassemble_21(val); 708 break; 709 case R_PARISC_LTOFF14R: 710 /* L(ltoff(val+addend)) */ 711 /* LT-relative; right 14 bits */ 712 val = get_got(me, val, addend); 713 val = rrsel(val, 0); 714 DEBUGP("LTOFF14R Symbol %s loc %p val %lx\n", 715 strtab + sym->st_name, 716 loc, val); 717 *loc = mask(*loc, 14) | reassemble_14(val); 718 break; 719 case R_PARISC_PCREL22F: 720 /* PC-relative; 22 bits */ 721 DEBUGP("PCREL22F Symbol %s loc %p val %lx\n", 722 strtab + sym->st_name, 723 loc, val); 724 val += addend; 725 /* can we reach it locally? */ 726 if (in_local(me, (void *)val)) { 727 /* this is the case where the symbol is local 728 * to the module, but in a different section, 729 * so stub the jump in case it's more than 22 730 * bits away */ 731 val = (val - dot - 8)/4; 732 if (!RELOC_REACHABLE(val, 22)) { 733 /* direct distance too far, create 734 * stub entry instead */ 735 val = get_stub(me, sym->st_value, 736 addend, ELF_STUB_DIRECT, 737 loc0, targetsec); 738 } else { 739 /* Ok, we can reach it directly. */ 740 val = sym->st_value; 741 val += addend; 742 } 743 } else { 744 val = sym->st_value; 745 if (strncmp(strtab + sym->st_name, "$$", 2) 746 == 0) 747 val = get_stub(me, val, addend, ELF_STUB_MILLI, 748 loc0, targetsec); 749 else 750 val = get_stub(me, val, addend, ELF_STUB_GOT, 751 loc0, targetsec); 752 } 753 DEBUGP("STUB FOR %s loc %lx, val %lx+%lx at %lx\n", 754 strtab + sym->st_name, loc, sym->st_value, 755 addend, val); 756 val = (val - dot - 8)/4; 757 CHECK_RELOC(val, 22); 758 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val); 759 break; 760 case R_PARISC_DIR64: 761 /* 64-bit effective address */ 762 *loc64 = val + addend; 763 break; 764 case R_PARISC_SEGREL32: 765 /* 32-bit segment relative address */ 766 /* See note about special handling of SEGREL32 at 767 * the beginning of this file. 768 */ 769 *loc = fsel(val, addend); 770 break; 771 case R_PARISC_FPTR64: 772 /* 64-bit function address */ 773 if(in_local(me, (void *)(val + addend))) { 774 *loc64 = get_fdesc(me, val+addend); 775 DEBUGP("FDESC for %s at %p points to %lx\n", 776 strtab + sym->st_name, *loc64, 777 ((Elf_Fdesc *)*loc64)->addr); 778 } else { 779 /* if the symbol is not local to this 780 * module then val+addend is a pointer 781 * to the function descriptor */ 782 DEBUGP("Non local FPTR64 Symbol %s loc %p val %lx\n", 783 strtab + sym->st_name, 784 loc, val); 785 *loc64 = val + addend; 786 } 787 break; 788 789 default: 790 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n", 791 me->name, ELF64_R_TYPE(rel[i].r_info)); 792 return -ENOEXEC; 793 } 794 } 795 return 0; 796 } 797 #endif 798 799 static void 800 register_unwind_table(struct module *me, 801 const Elf_Shdr *sechdrs) 802 { 803 unsigned char *table, *end; 804 unsigned long gp; 805 806 if (!me->arch.unwind_section) 807 return; 808 809 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr; 810 end = table + sechdrs[me->arch.unwind_section].sh_size; 811 gp = (Elf_Addr)me->module_core + me->arch.got_offset; 812 813 DEBUGP("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n", 814 me->arch.unwind_section, table, end, gp); 815 me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end); 816 } 817 818 static void 819 deregister_unwind_table(struct module *me) 820 { 821 if (me->arch.unwind) 822 unwind_table_remove(me->arch.unwind); 823 } 824 825 int module_finalize(const Elf_Ehdr *hdr, 826 const Elf_Shdr *sechdrs, 827 struct module *me) 828 { 829 int i; 830 unsigned long nsyms; 831 const char *strtab = NULL; 832 Elf_Sym *newptr, *oldptr; 833 Elf_Shdr *symhdr = NULL; 834 #ifdef DEBUG 835 Elf_Fdesc *entry; 836 u32 *addr; 837 838 entry = (Elf_Fdesc *)me->init; 839 printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry, 840 entry->gp, entry->addr); 841 addr = (u32 *)entry->addr; 842 printk("INSNS: %x %x %x %x\n", 843 addr[0], addr[1], addr[2], addr[3]); 844 printk("got entries used %ld, gots max %ld\n" 845 "fdescs used %ld, fdescs max %ld\n", 846 me->arch.got_count, me->arch.got_max, 847 me->arch.fdesc_count, me->arch.fdesc_max); 848 #endif 849 850 register_unwind_table(me, sechdrs); 851 852 /* haven't filled in me->symtab yet, so have to find it 853 * ourselves */ 854 for (i = 1; i < hdr->e_shnum; i++) { 855 if(sechdrs[i].sh_type == SHT_SYMTAB 856 && (sechdrs[i].sh_type & SHF_ALLOC)) { 857 int strindex = sechdrs[i].sh_link; 858 /* FIXME: AWFUL HACK 859 * The cast is to drop the const from 860 * the sechdrs pointer */ 861 symhdr = (Elf_Shdr *)&sechdrs[i]; 862 strtab = (char *)sechdrs[strindex].sh_addr; 863 break; 864 } 865 } 866 867 DEBUGP("module %s: strtab %p, symhdr %p\n", 868 me->name, strtab, symhdr); 869 870 if(me->arch.got_count > MAX_GOTS) { 871 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n", 872 me->name, me->arch.got_count, MAX_GOTS); 873 return -EINVAL; 874 } 875 876 kfree(me->arch.section); 877 me->arch.section = NULL; 878 879 /* no symbol table */ 880 if(symhdr == NULL) 881 return 0; 882 883 oldptr = (void *)symhdr->sh_addr; 884 newptr = oldptr + 1; /* we start counting at 1 */ 885 nsyms = symhdr->sh_size / sizeof(Elf_Sym); 886 DEBUGP("OLD num_symtab %lu\n", nsyms); 887 888 for (i = 1; i < nsyms; i++) { 889 oldptr++; /* note, count starts at 1 so preincrement */ 890 if(strncmp(strtab + oldptr->st_name, 891 ".L", 2) == 0) 892 continue; 893 894 if(newptr != oldptr) 895 *newptr++ = *oldptr; 896 else 897 newptr++; 898 899 } 900 nsyms = newptr - (Elf_Sym *)symhdr->sh_addr; 901 DEBUGP("NEW num_symtab %lu\n", nsyms); 902 symhdr->sh_size = nsyms * sizeof(Elf_Sym); 903 return module_bug_finalize(hdr, sechdrs, me); 904 } 905 906 void module_arch_cleanup(struct module *mod) 907 { 908 deregister_unwind_table(mod); 909 module_bug_cleanup(mod); 910 } 911