1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* Kernel module help for PPC64. 3 Copyright (C) 2001, 2003 Rusty Russell IBM Corporation. 4 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/module.h> 10 #include <linux/elf.h> 11 #include <linux/moduleloader.h> 12 #include <linux/err.h> 13 #include <linux/vmalloc.h> 14 #include <linux/ftrace.h> 15 #include <linux/bug.h> 16 #include <linux/uaccess.h> 17 #include <asm/module.h> 18 #include <asm/firmware.h> 19 #include <asm/code-patching.h> 20 #include <linux/sort.h> 21 #include <asm/setup.h> 22 #include <asm/sections.h> 23 #include <asm/inst.h> 24 25 /* FIXME: We don't do .init separately. To do this, we'd need to have 26 a separate r2 value in the init and core section, and stub between 27 them, too. 28 29 Using a magic allocator which places modules within 32MB solves 30 this, and makes other things simpler. Anton? 31 --RR. */ 32 33 #ifdef PPC64_ELF_ABI_v2 34 35 static func_desc_t func_desc(unsigned long addr) 36 { 37 func_desc_t desc = { 38 .addr = addr, 39 }; 40 41 return desc; 42 } 43 44 /* PowerPC64 specific values for the Elf64_Sym st_other field. */ 45 #define STO_PPC64_LOCAL_BIT 5 46 #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT) 47 #define PPC64_LOCAL_ENTRY_OFFSET(other) \ 48 (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2) 49 50 static unsigned int local_entry_offset(const Elf64_Sym *sym) 51 { 52 /* sym->st_other indicates offset to local entry point 53 * (otherwise it will assume r12 is the address of the start 54 * of function and try to derive r2 from it). */ 55 return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other); 56 } 57 #else 58 59 static func_desc_t func_desc(unsigned long addr) 60 { 61 return *(struct func_desc *)addr; 62 } 63 static unsigned int local_entry_offset(const Elf64_Sym *sym) 64 { 65 return 0; 66 } 67 68 void *dereference_module_function_descriptor(struct module *mod, void *ptr) 69 { 70 if (ptr < (void *)mod->arch.start_opd || 71 ptr >= (void *)mod->arch.end_opd) 72 return ptr; 73 74 return dereference_function_descriptor(ptr); 75 } 76 #endif 77 78 static unsigned long func_addr(unsigned long addr) 79 { 80 return func_desc(addr).addr; 81 } 82 83 static unsigned long stub_func_addr(func_desc_t func) 84 { 85 return func.addr; 86 } 87 88 #define STUB_MAGIC 0x73747562 /* stub */ 89 90 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into 91 the kernel itself). But on PPC64, these need to be used for every 92 jump, actually, to reset r2 (TOC+0x8000). */ 93 struct ppc64_stub_entry 94 { 95 /* 28 byte jump instruction sequence (7 instructions). We only 96 * need 6 instructions on ABIv2 but we always allocate 7 so 97 * so we don't have to modify the trampoline load instruction. */ 98 u32 jump[7]; 99 /* Used by ftrace to identify stubs */ 100 u32 magic; 101 /* Data for the above code */ 102 func_desc_t funcdata; 103 }; 104 105 /* 106 * PPC64 uses 24 bit jumps, but we need to jump into other modules or 107 * the kernel which may be further. So we jump to a stub. 108 * 109 * For ELFv1 we need to use this to set up the new r2 value (aka TOC 110 * pointer). For ELFv2 it's the callee's responsibility to set up the 111 * new r2, but for both we need to save the old r2. 112 * 113 * We could simply patch the new r2 value and function pointer into 114 * the stub, but it's significantly shorter to put these values at the 115 * end of the stub code, and patch the stub address (32-bits relative 116 * to the TOC ptr, r2) into the stub. 117 */ 118 static u32 ppc64_stub_insns[] = { 119 PPC_RAW_ADDIS(_R11, _R2, 0), 120 PPC_RAW_ADDI(_R11, _R11, 0), 121 /* Save current r2 value in magic place on the stack. */ 122 PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET), 123 PPC_RAW_LD(_R12, _R11, 32), 124 #ifdef PPC64_ELF_ABI_v1 125 /* Set up new r2 from function descriptor */ 126 PPC_RAW_LD(_R2, _R11, 40), 127 #endif 128 PPC_RAW_MTCTR(_R12), 129 PPC_RAW_BCTR(), 130 }; 131 132 /* Count how many different 24-bit relocations (different symbol, 133 different addend) */ 134 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num) 135 { 136 unsigned int i, r_info, r_addend, _count_relocs; 137 138 /* FIXME: Only count external ones --RR */ 139 _count_relocs = 0; 140 r_info = 0; 141 r_addend = 0; 142 for (i = 0; i < num; i++) 143 /* Only count 24-bit relocs, others don't need stubs */ 144 if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 && 145 (r_info != ELF64_R_SYM(rela[i].r_info) || 146 r_addend != rela[i].r_addend)) { 147 _count_relocs++; 148 r_info = ELF64_R_SYM(rela[i].r_info); 149 r_addend = rela[i].r_addend; 150 } 151 152 return _count_relocs; 153 } 154 155 static int relacmp(const void *_x, const void *_y) 156 { 157 const Elf64_Rela *x, *y; 158 159 y = (Elf64_Rela *)_x; 160 x = (Elf64_Rela *)_y; 161 162 /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to 163 * make the comparison cheaper/faster. It won't affect the sorting or 164 * the counting algorithms' performance 165 */ 166 if (x->r_info < y->r_info) 167 return -1; 168 else if (x->r_info > y->r_info) 169 return 1; 170 else if (x->r_addend < y->r_addend) 171 return -1; 172 else if (x->r_addend > y->r_addend) 173 return 1; 174 else 175 return 0; 176 } 177 178 /* Get size of potential trampolines required. */ 179 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, 180 const Elf64_Shdr *sechdrs) 181 { 182 /* One extra reloc so it's always 0-addr terminated */ 183 unsigned long relocs = 1; 184 unsigned i; 185 186 /* Every relocated section... */ 187 for (i = 1; i < hdr->e_shnum; i++) { 188 if (sechdrs[i].sh_type == SHT_RELA) { 189 pr_debug("Found relocations in section %u\n", i); 190 pr_debug("Ptr: %p. Number: %Lu\n", 191 (void *)sechdrs[i].sh_addr, 192 sechdrs[i].sh_size / sizeof(Elf64_Rela)); 193 194 /* Sort the relocation information based on a symbol and 195 * addend key. This is a stable O(n*log n) complexity 196 * alogrithm but it will reduce the complexity of 197 * count_relocs() to linear complexity O(n) 198 */ 199 sort((void *)sechdrs[i].sh_addr, 200 sechdrs[i].sh_size / sizeof(Elf64_Rela), 201 sizeof(Elf64_Rela), relacmp, NULL); 202 203 relocs += count_relocs((void *)sechdrs[i].sh_addr, 204 sechdrs[i].sh_size 205 / sizeof(Elf64_Rela)); 206 } 207 } 208 209 #ifdef CONFIG_DYNAMIC_FTRACE 210 /* make the trampoline to the ftrace_caller */ 211 relocs++; 212 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 213 /* an additional one for ftrace_regs_caller */ 214 relocs++; 215 #endif 216 #endif 217 218 pr_debug("Looks like a total of %lu stubs, max\n", relocs); 219 return relocs * sizeof(struct ppc64_stub_entry); 220 } 221 222 /* Still needed for ELFv2, for .TOC. */ 223 static void dedotify_versions(struct modversion_info *vers, 224 unsigned long size) 225 { 226 struct modversion_info *end; 227 228 for (end = (void *)vers + size; vers < end; vers++) 229 if (vers->name[0] == '.') { 230 memmove(vers->name, vers->name+1, strlen(vers->name)); 231 } 232 } 233 234 /* 235 * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. 236 * seem to be defined (value set later). 237 */ 238 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) 239 { 240 unsigned int i; 241 242 for (i = 1; i < numsyms; i++) { 243 if (syms[i].st_shndx == SHN_UNDEF) { 244 char *name = strtab + syms[i].st_name; 245 if (name[0] == '.') { 246 if (strcmp(name+1, "TOC.") == 0) 247 syms[i].st_shndx = SHN_ABS; 248 syms[i].st_name++; 249 } 250 } 251 } 252 } 253 254 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs, 255 const char *strtab, 256 unsigned int symindex) 257 { 258 unsigned int i, numsyms; 259 Elf64_Sym *syms; 260 261 syms = (Elf64_Sym *)sechdrs[symindex].sh_addr; 262 numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym); 263 264 for (i = 1; i < numsyms; i++) { 265 if (syms[i].st_shndx == SHN_ABS 266 && strcmp(strtab + syms[i].st_name, "TOC.") == 0) 267 return &syms[i]; 268 } 269 return NULL; 270 } 271 272 int module_frob_arch_sections(Elf64_Ehdr *hdr, 273 Elf64_Shdr *sechdrs, 274 char *secstrings, 275 struct module *me) 276 { 277 unsigned int i; 278 279 /* Find .toc and .stubs sections, symtab and strtab */ 280 for (i = 1; i < hdr->e_shnum; i++) { 281 char *p; 282 if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) 283 me->arch.stubs_section = i; 284 else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) { 285 me->arch.toc_section = i; 286 if (sechdrs[i].sh_addralign < 8) 287 sechdrs[i].sh_addralign = 8; 288 } 289 else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) 290 dedotify_versions((void *)hdr + sechdrs[i].sh_offset, 291 sechdrs[i].sh_size); 292 293 /* We don't handle .init for the moment: rename to _init */ 294 while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init"))) 295 p[0] = '_'; 296 297 if (sechdrs[i].sh_type == SHT_SYMTAB) 298 dedotify((void *)hdr + sechdrs[i].sh_offset, 299 sechdrs[i].sh_size / sizeof(Elf64_Sym), 300 (void *)hdr 301 + sechdrs[sechdrs[i].sh_link].sh_offset); 302 } 303 304 if (!me->arch.stubs_section) { 305 pr_err("%s: doesn't contain .stubs.\n", me->name); 306 return -ENOEXEC; 307 } 308 309 /* If we don't have a .toc, just use .stubs. We need to set r2 310 to some reasonable value in case the module calls out to 311 other functions via a stub, or if a function pointer escapes 312 the module by some means. */ 313 if (!me->arch.toc_section) 314 me->arch.toc_section = me->arch.stubs_section; 315 316 /* Override the stubs size */ 317 sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs); 318 return 0; 319 } 320 321 #ifdef CONFIG_MPROFILE_KERNEL 322 323 static u32 stub_insns[] = { 324 PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)), 325 PPC_RAW_ADDIS(_R12, _R12, 0), 326 PPC_RAW_ADDI(_R12, _R12, 0), 327 PPC_RAW_MTCTR(_R12), 328 PPC_RAW_BCTR(), 329 }; 330 331 /* 332 * For mprofile-kernel we use a special stub for ftrace_caller() because we 333 * can't rely on r2 containing this module's TOC when we enter the stub. 334 * 335 * That can happen if the function calling us didn't need to use the toc. In 336 * that case it won't have setup r2, and the r2 value will be either the 337 * kernel's toc, or possibly another modules toc. 338 * 339 * To deal with that this stub uses the kernel toc, which is always accessible 340 * via the paca (in r13). The target (ftrace_caller()) is responsible for 341 * saving and restoring the toc before returning. 342 */ 343 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry, 344 unsigned long addr, 345 struct module *me) 346 { 347 long reladdr; 348 349 memcpy(entry->jump, stub_insns, sizeof(stub_insns)); 350 351 /* Stub uses address relative to kernel toc (from the paca) */ 352 reladdr = addr - kernel_toc_addr(); 353 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 354 pr_err("%s: Address of %ps out of range of kernel_toc.\n", 355 me->name, (void *)addr); 356 return 0; 357 } 358 359 entry->jump[1] |= PPC_HA(reladdr); 360 entry->jump[2] |= PPC_LO(reladdr); 361 362 /* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */ 363 entry->funcdata = func_desc(addr); 364 entry->magic = STUB_MAGIC; 365 366 return 1; 367 } 368 369 static bool is_mprofile_ftrace_call(const char *name) 370 { 371 if (!strcmp("_mcount", name)) 372 return true; 373 #ifdef CONFIG_DYNAMIC_FTRACE 374 if (!strcmp("ftrace_caller", name)) 375 return true; 376 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 377 if (!strcmp("ftrace_regs_caller", name)) 378 return true; 379 #endif 380 #endif 381 382 return false; 383 } 384 #else 385 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry, 386 unsigned long addr, 387 struct module *me) 388 { 389 return 0; 390 } 391 392 static bool is_mprofile_ftrace_call(const char *name) 393 { 394 return false; 395 } 396 #endif 397 398 /* 399 * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the 400 * value maximum span in an instruction which uses a signed offset). Round down 401 * to a 256 byte boundary for the odd case where we are setting up r2 without a 402 * .toc section. 403 */ 404 static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me) 405 { 406 return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000; 407 } 408 409 /* Patch stub to reference function and correct r2 value. */ 410 static inline int create_stub(const Elf64_Shdr *sechdrs, 411 struct ppc64_stub_entry *entry, 412 unsigned long addr, 413 struct module *me, 414 const char *name) 415 { 416 long reladdr; 417 func_desc_t desc; 418 int i; 419 420 if (is_mprofile_ftrace_call(name)) 421 return create_ftrace_stub(entry, addr, me); 422 423 for (i = 0; i < sizeof(ppc64_stub_insns) / sizeof(u32); i++) { 424 if (patch_instruction(&entry->jump[i], 425 ppc_inst(ppc64_stub_insns[i]))) 426 return 0; 427 } 428 429 /* Stub uses address relative to r2. */ 430 reladdr = (unsigned long)entry - my_r2(sechdrs, me); 431 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 432 pr_err("%s: Address %p of stub out of range of %p.\n", 433 me->name, (void *)reladdr, (void *)my_r2); 434 return 0; 435 } 436 pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr); 437 438 if (patch_instruction(&entry->jump[0], 439 ppc_inst(entry->jump[0] | PPC_HA(reladdr)))) 440 return 0; 441 442 if (patch_instruction(&entry->jump[1], 443 ppc_inst(entry->jump[1] | PPC_LO(reladdr)))) 444 return 0; 445 446 // func_desc_t is 8 bytes if ABIv2, else 16 bytes 447 desc = func_desc(addr); 448 for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) { 449 if (patch_instruction(((u32 *)&entry->funcdata) + i, 450 ppc_inst(((u32 *)(&desc))[i]))) 451 return 0; 452 } 453 454 if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC))) 455 return 0; 456 457 return 1; 458 } 459 460 /* Create stub to jump to function described in this OPD/ptr: we need the 461 stub to set up the TOC ptr (r2) for the function. */ 462 static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs, 463 unsigned long addr, 464 struct module *me, 465 const char *name) 466 { 467 struct ppc64_stub_entry *stubs; 468 unsigned int i, num_stubs; 469 470 num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); 471 472 /* Find this stub, or if that fails, the next avail. entry */ 473 stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; 474 for (i = 0; stub_func_addr(stubs[i].funcdata); i++) { 475 if (WARN_ON(i >= num_stubs)) 476 return 0; 477 478 if (stub_func_addr(stubs[i].funcdata) == func_addr(addr)) 479 return (unsigned long)&stubs[i]; 480 } 481 482 if (!create_stub(sechdrs, &stubs[i], addr, me, name)) 483 return 0; 484 485 return (unsigned long)&stubs[i]; 486 } 487 488 /* We expect a noop next: if it is, replace it with instruction to 489 restore r2. */ 490 static int restore_r2(const char *name, u32 *instruction, struct module *me) 491 { 492 u32 *prev_insn = instruction - 1; 493 494 if (is_mprofile_ftrace_call(name)) 495 return 1; 496 497 /* 498 * Make sure the branch isn't a sibling call. Sibling calls aren't 499 * "link" branches and they don't return, so they don't need the r2 500 * restore afterwards. 501 */ 502 if (!instr_is_relative_link_branch(ppc_inst(*prev_insn))) 503 return 1; 504 505 if (*instruction != PPC_RAW_NOP()) { 506 pr_err("%s: Expected nop after call, got %08x at %pS\n", 507 me->name, *instruction, instruction); 508 return 0; 509 } 510 511 /* ld r2,R2_STACK_OFFSET(r1) */ 512 if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC))) 513 return 0; 514 515 return 1; 516 } 517 518 int apply_relocate_add(Elf64_Shdr *sechdrs, 519 const char *strtab, 520 unsigned int symindex, 521 unsigned int relsec, 522 struct module *me) 523 { 524 unsigned int i; 525 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 526 Elf64_Sym *sym; 527 unsigned long *location; 528 unsigned long value; 529 530 pr_debug("Applying ADD relocate section %u to %u\n", relsec, 531 sechdrs[relsec].sh_info); 532 533 /* First time we're called, we can fix up .TOC. */ 534 if (!me->arch.toc_fixed) { 535 sym = find_dot_toc(sechdrs, strtab, symindex); 536 /* It's theoretically possible that a module doesn't want a 537 * .TOC. so don't fail it just for that. */ 538 if (sym) 539 sym->st_value = my_r2(sechdrs, me); 540 me->arch.toc_fixed = true; 541 } 542 543 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { 544 /* This is where to make the change */ 545 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 546 + rela[i].r_offset; 547 /* This is the symbol it is referring to */ 548 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 549 + ELF64_R_SYM(rela[i].r_info); 550 551 pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n", 552 location, (long)ELF64_R_TYPE(rela[i].r_info), 553 strtab + sym->st_name, (unsigned long)sym->st_value, 554 (long)rela[i].r_addend); 555 556 /* `Everything is relative'. */ 557 value = sym->st_value + rela[i].r_addend; 558 559 switch (ELF64_R_TYPE(rela[i].r_info)) { 560 case R_PPC64_ADDR32: 561 /* Simply set it */ 562 *(u32 *)location = value; 563 break; 564 565 case R_PPC64_ADDR64: 566 /* Simply set it */ 567 *(unsigned long *)location = value; 568 break; 569 570 case R_PPC64_TOC: 571 *(unsigned long *)location = my_r2(sechdrs, me); 572 break; 573 574 case R_PPC64_TOC16: 575 /* Subtract TOC pointer */ 576 value -= my_r2(sechdrs, me); 577 if (value + 0x8000 > 0xffff) { 578 pr_err("%s: bad TOC16 relocation (0x%lx)\n", 579 me->name, value); 580 return -ENOEXEC; 581 } 582 *((uint16_t *) location) 583 = (*((uint16_t *) location) & ~0xffff) 584 | (value & 0xffff); 585 break; 586 587 case R_PPC64_TOC16_LO: 588 /* Subtract TOC pointer */ 589 value -= my_r2(sechdrs, me); 590 *((uint16_t *) location) 591 = (*((uint16_t *) location) & ~0xffff) 592 | (value & 0xffff); 593 break; 594 595 case R_PPC64_TOC16_DS: 596 /* Subtract TOC pointer */ 597 value -= my_r2(sechdrs, me); 598 if ((value & 3) != 0 || value + 0x8000 > 0xffff) { 599 pr_err("%s: bad TOC16_DS relocation (0x%lx)\n", 600 me->name, value); 601 return -ENOEXEC; 602 } 603 *((uint16_t *) location) 604 = (*((uint16_t *) location) & ~0xfffc) 605 | (value & 0xfffc); 606 break; 607 608 case R_PPC64_TOC16_LO_DS: 609 /* Subtract TOC pointer */ 610 value -= my_r2(sechdrs, me); 611 if ((value & 3) != 0) { 612 pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n", 613 me->name, value); 614 return -ENOEXEC; 615 } 616 *((uint16_t *) location) 617 = (*((uint16_t *) location) & ~0xfffc) 618 | (value & 0xfffc); 619 break; 620 621 case R_PPC64_TOC16_HA: 622 /* Subtract TOC pointer */ 623 value -= my_r2(sechdrs, me); 624 value = ((value + 0x8000) >> 16); 625 *((uint16_t *) location) 626 = (*((uint16_t *) location) & ~0xffff) 627 | (value & 0xffff); 628 break; 629 630 case R_PPC_REL24: 631 /* FIXME: Handle weak symbols here --RR */ 632 if (sym->st_shndx == SHN_UNDEF || 633 sym->st_shndx == SHN_LIVEPATCH) { 634 /* External: go via stub */ 635 value = stub_for_addr(sechdrs, value, me, 636 strtab + sym->st_name); 637 if (!value) 638 return -ENOENT; 639 if (!restore_r2(strtab + sym->st_name, 640 (u32 *)location + 1, me)) 641 return -ENOEXEC; 642 } else 643 value += local_entry_offset(sym); 644 645 /* Convert value to relative */ 646 value -= (unsigned long)location; 647 if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ 648 pr_err("%s: REL24 %li out of range!\n", 649 me->name, (long int)value); 650 return -ENOEXEC; 651 } 652 653 /* Only replace bits 2 through 26 */ 654 value = (*(uint32_t *)location & ~0x03fffffc) 655 | (value & 0x03fffffc); 656 657 if (patch_instruction((u32 *)location, ppc_inst(value))) 658 return -EFAULT; 659 660 break; 661 662 case R_PPC64_REL64: 663 /* 64 bits relative (used by features fixups) */ 664 *location = value - (unsigned long)location; 665 break; 666 667 case R_PPC64_REL32: 668 /* 32 bits relative (used by relative exception tables) */ 669 /* Convert value to relative */ 670 value -= (unsigned long)location; 671 if (value + 0x80000000 > 0xffffffff) { 672 pr_err("%s: REL32 %li out of range!\n", 673 me->name, (long int)value); 674 return -ENOEXEC; 675 } 676 *(u32 *)location = value; 677 break; 678 679 case R_PPC64_TOCSAVE: 680 /* 681 * Marker reloc indicates we don't have to save r2. 682 * That would only save us one instruction, so ignore 683 * it. 684 */ 685 break; 686 687 case R_PPC64_ENTRY: 688 /* 689 * Optimize ELFv2 large code model entry point if 690 * the TOC is within 2GB range of current location. 691 */ 692 value = my_r2(sechdrs, me) - (unsigned long)location; 693 if (value + 0x80008000 > 0xffffffff) 694 break; 695 /* 696 * Check for the large code model prolog sequence: 697 * ld r2, ...(r12) 698 * add r2, r2, r12 699 */ 700 if ((((uint32_t *)location)[0] & ~0xfffc) != PPC_RAW_LD(_R2, _R12, 0)) 701 break; 702 if (((uint32_t *)location)[1] != PPC_RAW_ADD(_R2, _R2, _R12)) 703 break; 704 /* 705 * If found, replace it with: 706 * addis r2, r12, (.TOC.-func)@ha 707 * addi r2, r2, (.TOC.-func)@l 708 */ 709 ((uint32_t *)location)[0] = PPC_RAW_ADDIS(_R2, _R12, PPC_HA(value)); 710 ((uint32_t *)location)[1] = PPC_RAW_ADDI(_R2, _R2, PPC_LO(value)); 711 break; 712 713 case R_PPC64_REL16_HA: 714 /* Subtract location pointer */ 715 value -= (unsigned long)location; 716 value = ((value + 0x8000) >> 16); 717 *((uint16_t *) location) 718 = (*((uint16_t *) location) & ~0xffff) 719 | (value & 0xffff); 720 break; 721 722 case R_PPC64_REL16_LO: 723 /* Subtract location pointer */ 724 value -= (unsigned long)location; 725 *((uint16_t *) location) 726 = (*((uint16_t *) location) & ~0xffff) 727 | (value & 0xffff); 728 break; 729 730 default: 731 pr_err("%s: Unknown ADD relocation: %lu\n", 732 me->name, 733 (unsigned long)ELF64_R_TYPE(rela[i].r_info)); 734 return -ENOEXEC; 735 } 736 } 737 738 return 0; 739 } 740 741 #ifdef CONFIG_DYNAMIC_FTRACE 742 int module_trampoline_target(struct module *mod, unsigned long addr, 743 unsigned long *target) 744 { 745 struct ppc64_stub_entry *stub; 746 func_desc_t funcdata; 747 u32 magic; 748 749 if (!within_module_core(addr, mod)) { 750 pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name); 751 return -EFAULT; 752 } 753 754 stub = (struct ppc64_stub_entry *)addr; 755 756 if (copy_from_kernel_nofault(&magic, &stub->magic, 757 sizeof(magic))) { 758 pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name); 759 return -EFAULT; 760 } 761 762 if (magic != STUB_MAGIC) { 763 pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name); 764 return -EFAULT; 765 } 766 767 if (copy_from_kernel_nofault(&funcdata, &stub->funcdata, 768 sizeof(funcdata))) { 769 pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name); 770 return -EFAULT; 771 } 772 773 *target = stub_func_addr(funcdata); 774 775 return 0; 776 } 777 778 int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs) 779 { 780 mod->arch.tramp = stub_for_addr(sechdrs, 781 (unsigned long)ftrace_caller, 782 mod, 783 "ftrace_caller"); 784 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 785 mod->arch.tramp_regs = stub_for_addr(sechdrs, 786 (unsigned long)ftrace_regs_caller, 787 mod, 788 "ftrace_regs_caller"); 789 if (!mod->arch.tramp_regs) 790 return -ENOENT; 791 #endif 792 793 if (!mod->arch.tramp) 794 return -ENOENT; 795 796 return 0; 797 } 798 #endif 799