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