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