17568cb4eSPaul Mackerras /* Kernel module help for PPC64. 27568cb4eSPaul Mackerras Copyright (C) 2001, 2003 Rusty Russell IBM Corporation. 37568cb4eSPaul Mackerras 47568cb4eSPaul Mackerras This program is free software; you can redistribute it and/or modify 57568cb4eSPaul Mackerras it under the terms of the GNU General Public License as published by 67568cb4eSPaul Mackerras the Free Software Foundation; either version 2 of the License, or 77568cb4eSPaul Mackerras (at your option) any later version. 87568cb4eSPaul Mackerras 97568cb4eSPaul Mackerras This program is distributed in the hope that it will be useful, 107568cb4eSPaul Mackerras but WITHOUT ANY WARRANTY; without even the implied warranty of 117568cb4eSPaul Mackerras MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 127568cb4eSPaul Mackerras GNU General Public License for more details. 137568cb4eSPaul Mackerras 147568cb4eSPaul Mackerras You should have received a copy of the GNU General Public License 157568cb4eSPaul Mackerras along with this program; if not, write to the Free Software 167568cb4eSPaul Mackerras Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 177568cb4eSPaul Mackerras */ 18c7d1f6afSAnton Blanchard 19c7d1f6afSAnton Blanchard #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 20c7d1f6afSAnton Blanchard 217568cb4eSPaul Mackerras #include <linux/module.h> 227568cb4eSPaul Mackerras #include <linux/elf.h> 237568cb4eSPaul Mackerras #include <linux/moduleloader.h> 247568cb4eSPaul Mackerras #include <linux/err.h> 257568cb4eSPaul Mackerras #include <linux/vmalloc.h> 26f48cb8b4SSteven Rostedt #include <linux/ftrace.h> 2773c9ceabSJeremy Fitzhardinge #include <linux/bug.h> 2883775b85SAnton Blanchard #include <linux/uaccess.h> 297568cb4eSPaul Mackerras #include <asm/module.h> 3021c4ff80SBenjamin Herrenschmidt #include <asm/firmware.h> 31b7bcda63SMichael Ellerman #include <asm/code-patching.h> 32eda09fbdSEmil Medve #include <linux/sort.h> 33b88c4767SRobert Jennings #include <asm/setup.h> 34336a7b5dSMichael Ellerman #include <asm/sections.h> 357568cb4eSPaul Mackerras 367568cb4eSPaul Mackerras /* FIXME: We don't do .init separately. To do this, we'd need to have 377568cb4eSPaul Mackerras a separate r2 value in the init and core section, and stub between 387568cb4eSPaul Mackerras them, too. 397568cb4eSPaul Mackerras 407568cb4eSPaul Mackerras Using a magic allocator which places modules within 32MB solves 417568cb4eSPaul Mackerras this, and makes other things simpler. Anton? 427568cb4eSPaul Mackerras --RR. */ 437568cb4eSPaul Mackerras 44f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v2 45008d7a91SRusty Russell 46008d7a91SRusty Russell /* An address is simply the address of the function. */ 47008d7a91SRusty Russell typedef unsigned long func_desc_t; 48008d7a91SRusty Russell 49008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr) 50008d7a91SRusty Russell { 51008d7a91SRusty Russell return addr; 52008d7a91SRusty Russell } 53008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr) 54008d7a91SRusty Russell { 55008d7a91SRusty Russell return addr; 56008d7a91SRusty Russell } 57008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func) 58008d7a91SRusty Russell { 59008d7a91SRusty Russell return func; 60008d7a91SRusty Russell } 61008d7a91SRusty Russell 62008d7a91SRusty Russell /* PowerPC64 specific values for the Elf64_Sym st_other field. */ 63008d7a91SRusty Russell #define STO_PPC64_LOCAL_BIT 5 64008d7a91SRusty Russell #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT) 65008d7a91SRusty Russell #define PPC64_LOCAL_ENTRY_OFFSET(other) \ 66008d7a91SRusty Russell (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2) 67008d7a91SRusty Russell 68008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym) 69008d7a91SRusty Russell { 70008d7a91SRusty Russell /* sym->st_other indicates offset to local entry point 71008d7a91SRusty Russell * (otherwise it will assume r12 is the address of the start 72008d7a91SRusty Russell * of function and try to derive r2 from it). */ 73008d7a91SRusty Russell return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other); 74008d7a91SRusty Russell } 75d2fae548SRusty Russell #else 76008d7a91SRusty Russell 77008d7a91SRusty Russell /* An address is address of the OPD entry, which contains address of fn. */ 78008d7a91SRusty Russell typedef struct ppc64_opd_entry func_desc_t; 79008d7a91SRusty Russell 80008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr) 81008d7a91SRusty Russell { 82008d7a91SRusty Russell return *(struct ppc64_opd_entry *)addr; 83008d7a91SRusty Russell } 84008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr) 85008d7a91SRusty Russell { 86008d7a91SRusty Russell return func_desc(addr).funcaddr; 87008d7a91SRusty Russell } 88008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func) 89008d7a91SRusty Russell { 90008d7a91SRusty Russell return func.funcaddr; 91008d7a91SRusty Russell } 92008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym) 93008d7a91SRusty Russell { 94008d7a91SRusty Russell return 0; 95008d7a91SRusty Russell } 965633e85bSSergey Senozhatsky 975633e85bSSergey Senozhatsky void *dereference_module_function_descriptor(struct module *mod, void *ptr) 985633e85bSSergey Senozhatsky { 995633e85bSSergey Senozhatsky if (ptr < (void *)mod->arch.start_opd || 1005633e85bSSergey Senozhatsky ptr >= (void *)mod->arch.end_opd) 1015633e85bSSergey Senozhatsky return ptr; 1025633e85bSSergey Senozhatsky 1035633e85bSSergey Senozhatsky return dereference_function_descriptor(ptr); 1045633e85bSSergey Senozhatsky } 105d2fae548SRusty Russell #endif 106d2fae548SRusty Russell 107f17c4e01SMichael Ellerman #define STUB_MAGIC 0x73747562 /* stub */ 108f17c4e01SMichael Ellerman 1097568cb4eSPaul Mackerras /* Like PPC32, we need little trampolines to do > 24-bit jumps (into 1107568cb4eSPaul Mackerras the kernel itself). But on PPC64, these need to be used for every 1117568cb4eSPaul Mackerras jump, actually, to reset r2 (TOC+0x8000). */ 1127568cb4eSPaul Mackerras struct ppc64_stub_entry 1137568cb4eSPaul Mackerras { 11483775b85SAnton Blanchard /* 28 byte jump instruction sequence (7 instructions). We only 11583775b85SAnton Blanchard * need 6 instructions on ABIv2 but we always allocate 7 so 11683775b85SAnton Blanchard * so we don't have to modify the trampoline load instruction. */ 1170e60e46eSRusty Russell u32 jump[7]; 118f17c4e01SMichael Ellerman /* Used by ftrace to identify stubs */ 119f17c4e01SMichael Ellerman u32 magic; 1207568cb4eSPaul Mackerras /* Data for the above code */ 121008d7a91SRusty Russell func_desc_t funcdata; 1227568cb4eSPaul Mackerras }; 1237568cb4eSPaul Mackerras 1245c729a11SRusty Russell /* 1255c729a11SRusty Russell * PPC64 uses 24 bit jumps, but we need to jump into other modules or 1265c729a11SRusty Russell * the kernel which may be further. So we jump to a stub. 1275c729a11SRusty Russell * 1285c729a11SRusty Russell * For ELFv1 we need to use this to set up the new r2 value (aka TOC 1295c729a11SRusty Russell * pointer). For ELFv2 it's the callee's responsibility to set up the 1305c729a11SRusty Russell * new r2, but for both we need to save the old r2. 1315c729a11SRusty Russell * 1325c729a11SRusty Russell * We could simply patch the new r2 value and function pointer into 1335c729a11SRusty Russell * the stub, but it's significantly shorter to put these values at the 1345c729a11SRusty Russell * end of the stub code, and patch the stub address (32-bits relative 1355c729a11SRusty Russell * to the TOC ptr, r2) into the stub. 136*a2b6f26cSChristophe Leroy * 137*a2b6f26cSChristophe Leroy * addis r11,r2, <high> 138*a2b6f26cSChristophe Leroy * addi r11,r11, <low> 139*a2b6f26cSChristophe Leroy * std r2,R2_STACK_OFFSET(r1) 140*a2b6f26cSChristophe Leroy * ld r12,32(r11) 141*a2b6f26cSChristophe Leroy * ld r2,40(r11) 142*a2b6f26cSChristophe Leroy * mtctr r12 143*a2b6f26cSChristophe Leroy * bctr 1445c729a11SRusty Russell */ 14583775b85SAnton Blanchard static u32 ppc64_stub_insns[] = { 146*a2b6f26cSChristophe Leroy PPC_INST_ADDIS | __PPC_RT(R11) | __PPC_RA(R2), 147*a2b6f26cSChristophe Leroy PPC_INST_ADDI | __PPC_RT(R11) | __PPC_RA(R11), 148fed8393eSEugene Surovegin /* Save current r2 value in magic place on the stack. */ 149*a2b6f26cSChristophe Leroy PPC_INST_STD | __PPC_RS(R2) | __PPC_RA(R1) | R2_STACK_OFFSET, 150*a2b6f26cSChristophe Leroy PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R11) | 32, 151f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v1 1525c729a11SRusty Russell /* Set up new r2 from function descriptor */ 153*a2b6f26cSChristophe Leroy PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R11) | 40, 1545c729a11SRusty Russell #endif 155*a2b6f26cSChristophe Leroy PPC_INST_MTCTR | __PPC_RS(R12), 156*a2b6f26cSChristophe Leroy PPC_INST_BCTR, 15783775b85SAnton Blanchard }; 15883775b85SAnton Blanchard 15983775b85SAnton Blanchard #ifdef CONFIG_DYNAMIC_FTRACE 160f17c4e01SMichael Ellerman int module_trampoline_target(struct module *mod, unsigned long addr, 161dd9fa162SAnton Blanchard unsigned long *target) 162dd9fa162SAnton Blanchard { 163f17c4e01SMichael Ellerman struct ppc64_stub_entry *stub; 164f17c4e01SMichael Ellerman func_desc_t funcdata; 165f17c4e01SMichael Ellerman u32 magic; 166dd9fa162SAnton Blanchard 167f17c4e01SMichael Ellerman if (!within_module_core(addr, mod)) { 168f17c4e01SMichael Ellerman pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name); 169dd9fa162SAnton Blanchard return -EFAULT; 170f17c4e01SMichael Ellerman } 171dd9fa162SAnton Blanchard 172f17c4e01SMichael Ellerman stub = (struct ppc64_stub_entry *)addr; 173dd9fa162SAnton Blanchard 174f17c4e01SMichael Ellerman if (probe_kernel_read(&magic, &stub->magic, sizeof(magic))) { 175f17c4e01SMichael Ellerman pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name); 176dd9fa162SAnton Blanchard return -EFAULT; 177f17c4e01SMichael Ellerman } 178f17c4e01SMichael Ellerman 179f17c4e01SMichael Ellerman if (magic != STUB_MAGIC) { 180f17c4e01SMichael Ellerman pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name); 181f17c4e01SMichael Ellerman return -EFAULT; 182f17c4e01SMichael Ellerman } 183f17c4e01SMichael Ellerman 184f17c4e01SMichael Ellerman if (probe_kernel_read(&funcdata, &stub->funcdata, sizeof(funcdata))) { 185f17c4e01SMichael Ellerman pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name); 186f17c4e01SMichael Ellerman return -EFAULT; 187f17c4e01SMichael Ellerman } 188f17c4e01SMichael Ellerman 189f17c4e01SMichael Ellerman *target = stub_func_addr(funcdata); 190dd9fa162SAnton Blanchard 191dd9fa162SAnton Blanchard return 0; 192dd9fa162SAnton Blanchard } 19383775b85SAnton Blanchard #endif 1947568cb4eSPaul Mackerras 1957568cb4eSPaul Mackerras /* Count how many different 24-bit relocations (different symbol, 1967568cb4eSPaul Mackerras different addend) */ 1977568cb4eSPaul Mackerras static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num) 1987568cb4eSPaul Mackerras { 199eda09fbdSEmil Medve unsigned int i, r_info, r_addend, _count_relocs; 2007568cb4eSPaul Mackerras 2017568cb4eSPaul Mackerras /* FIXME: Only count external ones --RR */ 202eda09fbdSEmil Medve _count_relocs = 0; 203eda09fbdSEmil Medve r_info = 0; 204eda09fbdSEmil Medve r_addend = 0; 205eda09fbdSEmil Medve for (i = 0; i < num; i++) 2067568cb4eSPaul Mackerras /* Only count 24-bit relocs, others don't need stubs */ 207eda09fbdSEmil Medve if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 && 208eda09fbdSEmil Medve (r_info != ELF64_R_SYM(rela[i].r_info) || 209eda09fbdSEmil Medve r_addend != rela[i].r_addend)) { 210eda09fbdSEmil Medve _count_relocs++; 211eda09fbdSEmil Medve r_info = ELF64_R_SYM(rela[i].r_info); 212eda09fbdSEmil Medve r_addend = rela[i].r_addend; 2137568cb4eSPaul Mackerras } 214eda09fbdSEmil Medve 215eda09fbdSEmil Medve return _count_relocs; 2167568cb4eSPaul Mackerras } 2177568cb4eSPaul Mackerras 218eda09fbdSEmil Medve static int relacmp(const void *_x, const void *_y) 219eda09fbdSEmil Medve { 220eda09fbdSEmil Medve const Elf64_Rela *x, *y; 221eda09fbdSEmil Medve 222eda09fbdSEmil Medve y = (Elf64_Rela *)_x; 223eda09fbdSEmil Medve x = (Elf64_Rela *)_y; 224eda09fbdSEmil Medve 225eda09fbdSEmil Medve /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to 226eda09fbdSEmil Medve * make the comparison cheaper/faster. It won't affect the sorting or 227eda09fbdSEmil Medve * the counting algorithms' performance 228eda09fbdSEmil Medve */ 229eda09fbdSEmil Medve if (x->r_info < y->r_info) 230eda09fbdSEmil Medve return -1; 231eda09fbdSEmil Medve else if (x->r_info > y->r_info) 232eda09fbdSEmil Medve return 1; 233eda09fbdSEmil Medve else if (x->r_addend < y->r_addend) 234eda09fbdSEmil Medve return -1; 235eda09fbdSEmil Medve else if (x->r_addend > y->r_addend) 236eda09fbdSEmil Medve return 1; 237eda09fbdSEmil Medve else 238eda09fbdSEmil Medve return 0; 239eda09fbdSEmil Medve } 240eda09fbdSEmil Medve 241eda09fbdSEmil Medve static void relaswap(void *_x, void *_y, int size) 242eda09fbdSEmil Medve { 243eda09fbdSEmil Medve uint64_t *x, *y, tmp; 244eda09fbdSEmil Medve int i; 245eda09fbdSEmil Medve 246eda09fbdSEmil Medve y = (uint64_t *)_x; 247eda09fbdSEmil Medve x = (uint64_t *)_y; 248eda09fbdSEmil Medve 249eda09fbdSEmil Medve for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) { 250eda09fbdSEmil Medve tmp = x[i]; 251eda09fbdSEmil Medve x[i] = y[i]; 252eda09fbdSEmil Medve y[i] = tmp; 253eda09fbdSEmil Medve } 254eda09fbdSEmil Medve } 255eda09fbdSEmil Medve 2567568cb4eSPaul Mackerras /* Get size of potential trampolines required. */ 2577568cb4eSPaul Mackerras static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, 2587568cb4eSPaul Mackerras const Elf64_Shdr *sechdrs) 2597568cb4eSPaul Mackerras { 2607568cb4eSPaul Mackerras /* One extra reloc so it's always 0-funcaddr terminated */ 2617568cb4eSPaul Mackerras unsigned long relocs = 1; 2627568cb4eSPaul Mackerras unsigned i; 2637568cb4eSPaul Mackerras 2647568cb4eSPaul Mackerras /* Every relocated section... */ 2657568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 2667568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_RELA) { 267c7d1f6afSAnton Blanchard pr_debug("Found relocations in section %u\n", i); 268c7d1f6afSAnton Blanchard pr_debug("Ptr: %p. Number: %Lu\n", 2697568cb4eSPaul Mackerras (void *)sechdrs[i].sh_addr, 2707568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Rela)); 271eda09fbdSEmil Medve 272eda09fbdSEmil Medve /* Sort the relocation information based on a symbol and 273eda09fbdSEmil Medve * addend key. This is a stable O(n*log n) complexity 274eda09fbdSEmil Medve * alogrithm but it will reduce the complexity of 275eda09fbdSEmil Medve * count_relocs() to linear complexity O(n) 276eda09fbdSEmil Medve */ 277eda09fbdSEmil Medve sort((void *)sechdrs[i].sh_addr, 278eda09fbdSEmil Medve sechdrs[i].sh_size / sizeof(Elf64_Rela), 279eda09fbdSEmil Medve sizeof(Elf64_Rela), relacmp, relaswap); 280eda09fbdSEmil Medve 2817568cb4eSPaul Mackerras relocs += count_relocs((void *)sechdrs[i].sh_addr, 2827568cb4eSPaul Mackerras sechdrs[i].sh_size 2837568cb4eSPaul Mackerras / sizeof(Elf64_Rela)); 2847568cb4eSPaul Mackerras } 2857568cb4eSPaul Mackerras } 2867568cb4eSPaul Mackerras 287f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 288f48cb8b4SSteven Rostedt /* make the trampoline to the ftrace_caller */ 289f48cb8b4SSteven Rostedt relocs++; 290ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 291ae30cc05SNaveen N. Rao /* an additional one for ftrace_regs_caller */ 292ae30cc05SNaveen N. Rao relocs++; 293ae30cc05SNaveen N. Rao #endif 294f48cb8b4SSteven Rostedt #endif 295f48cb8b4SSteven Rostedt 296c7d1f6afSAnton Blanchard pr_debug("Looks like a total of %lu stubs, max\n", relocs); 2977568cb4eSPaul Mackerras return relocs * sizeof(struct ppc64_stub_entry); 2987568cb4eSPaul Mackerras } 2997568cb4eSPaul Mackerras 3005b12c5c6SRusty Russell /* Still needed for ELFv2, for .TOC. */ 3017568cb4eSPaul Mackerras static void dedotify_versions(struct modversion_info *vers, 3027568cb4eSPaul Mackerras unsigned long size) 3037568cb4eSPaul Mackerras { 3047568cb4eSPaul Mackerras struct modversion_info *end; 3057568cb4eSPaul Mackerras 3067568cb4eSPaul Mackerras for (end = (void *)vers + size; vers < end; vers++) 307c2cbcf53SLaurent Dufour if (vers->name[0] == '.') { 3087568cb4eSPaul Mackerras memmove(vers->name, vers->name+1, strlen(vers->name)); 309c2cbcf53SLaurent Dufour } 3107568cb4eSPaul Mackerras } 3117568cb4eSPaul Mackerras 312c153693dSAlan Modra /* 313c153693dSAlan Modra * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. 314c153693dSAlan Modra * seem to be defined (value set later). 315c153693dSAlan Modra */ 3167568cb4eSPaul Mackerras static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) 3177568cb4eSPaul Mackerras { 3187568cb4eSPaul Mackerras unsigned int i; 3197568cb4eSPaul Mackerras 3207568cb4eSPaul Mackerras for (i = 1; i < numsyms; i++) { 3217568cb4eSPaul Mackerras if (syms[i].st_shndx == SHN_UNDEF) { 3227568cb4eSPaul Mackerras char *name = strtab + syms[i].st_name; 323c153693dSAlan Modra if (name[0] == '.') { 324c153693dSAlan Modra if (strcmp(name+1, "TOC.") == 0) 325c153693dSAlan Modra syms[i].st_shndx = SHN_ABS; 326f15838e9SAndreas Schwab syms[i].st_name++; 3277568cb4eSPaul Mackerras } 3287568cb4eSPaul Mackerras } 3297568cb4eSPaul Mackerras } 330c153693dSAlan Modra } 3317568cb4eSPaul Mackerras 3324edebbeaSRusty Russell static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs, 3334edebbeaSRusty Russell const char *strtab, 3344edebbeaSRusty Russell unsigned int symindex) 3354edebbeaSRusty Russell { 3364edebbeaSRusty Russell unsigned int i, numsyms; 3374edebbeaSRusty Russell Elf64_Sym *syms; 3384edebbeaSRusty Russell 3394edebbeaSRusty Russell syms = (Elf64_Sym *)sechdrs[symindex].sh_addr; 3404edebbeaSRusty Russell numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym); 3414edebbeaSRusty Russell 3424edebbeaSRusty Russell for (i = 1; i < numsyms; i++) { 343c153693dSAlan Modra if (syms[i].st_shndx == SHN_ABS 344008d7a91SRusty Russell && strcmp(strtab + syms[i].st_name, "TOC.") == 0) 3454edebbeaSRusty Russell return &syms[i]; 3464edebbeaSRusty Russell } 3474edebbeaSRusty Russell return NULL; 3484edebbeaSRusty Russell } 3494edebbeaSRusty Russell 3507568cb4eSPaul Mackerras int module_frob_arch_sections(Elf64_Ehdr *hdr, 3517568cb4eSPaul Mackerras Elf64_Shdr *sechdrs, 3527568cb4eSPaul Mackerras char *secstrings, 3537568cb4eSPaul Mackerras struct module *me) 3547568cb4eSPaul Mackerras { 3557568cb4eSPaul Mackerras unsigned int i; 3567568cb4eSPaul Mackerras 3577568cb4eSPaul Mackerras /* Find .toc and .stubs sections, symtab and strtab */ 3587568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 3597568cb4eSPaul Mackerras char *p; 3607568cb4eSPaul Mackerras if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) 3617568cb4eSPaul Mackerras me->arch.stubs_section = i; 3625c45b528SAlan Modra else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) { 3637568cb4eSPaul Mackerras me->arch.toc_section = i; 3645c45b528SAlan Modra if (sechdrs[i].sh_addralign < 8) 3655c45b528SAlan Modra sechdrs[i].sh_addralign = 8; 3665c45b528SAlan Modra } 3677568cb4eSPaul Mackerras else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) 3687568cb4eSPaul Mackerras dedotify_versions((void *)hdr + sechdrs[i].sh_offset, 3697568cb4eSPaul Mackerras sechdrs[i].sh_size); 3707568cb4eSPaul Mackerras 3717568cb4eSPaul Mackerras /* We don't handle .init for the moment: rename to _init */ 3727568cb4eSPaul Mackerras while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init"))) 3737568cb4eSPaul Mackerras p[0] = '_'; 3747568cb4eSPaul Mackerras 3757568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_SYMTAB) 3767568cb4eSPaul Mackerras dedotify((void *)hdr + sechdrs[i].sh_offset, 3777568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Sym), 3787568cb4eSPaul Mackerras (void *)hdr 3797568cb4eSPaul Mackerras + sechdrs[sechdrs[i].sh_link].sh_offset); 3807568cb4eSPaul Mackerras } 381f749edaeSAlan Modra 382f749edaeSAlan Modra if (!me->arch.stubs_section) { 383c7d1f6afSAnton Blanchard pr_err("%s: doesn't contain .stubs.\n", me->name); 3847568cb4eSPaul Mackerras return -ENOEXEC; 3857568cb4eSPaul Mackerras } 3867568cb4eSPaul Mackerras 387f749edaeSAlan Modra /* If we don't have a .toc, just use .stubs. We need to set r2 388f749edaeSAlan Modra to some reasonable value in case the module calls out to 389f749edaeSAlan Modra other functions via a stub, or if a function pointer escapes 390f749edaeSAlan Modra the module by some means. */ 391f749edaeSAlan Modra if (!me->arch.toc_section) 392f749edaeSAlan Modra me->arch.toc_section = me->arch.stubs_section; 393f749edaeSAlan Modra 3947568cb4eSPaul Mackerras /* Override the stubs size */ 3957568cb4eSPaul Mackerras sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs); 3967568cb4eSPaul Mackerras return 0; 3977568cb4eSPaul Mackerras } 3987568cb4eSPaul Mackerras 3995c45b528SAlan Modra /* 4005c45b528SAlan Modra * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the 4015c45b528SAlan Modra * value maximum span in an instruction which uses a signed offset). Round down 4025c45b528SAlan Modra * to a 256 byte boundary for the odd case where we are setting up r2 without a 4035c45b528SAlan Modra * .toc section. 4045c45b528SAlan Modra */ 405136cd345SMichael Ellerman static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me) 4067568cb4eSPaul Mackerras { 4075c45b528SAlan Modra return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000; 4087568cb4eSPaul Mackerras } 4097568cb4eSPaul Mackerras 4107568cb4eSPaul Mackerras /* Patch stub to reference function and correct r2 value. */ 411136cd345SMichael Ellerman static inline int create_stub(const Elf64_Shdr *sechdrs, 4127568cb4eSPaul Mackerras struct ppc64_stub_entry *entry, 413008d7a91SRusty Russell unsigned long addr, 4147568cb4eSPaul Mackerras struct module *me) 4157568cb4eSPaul Mackerras { 4167568cb4eSPaul Mackerras long reladdr; 4177568cb4eSPaul Mackerras 41883775b85SAnton Blanchard memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns)); 4197568cb4eSPaul Mackerras 4207568cb4eSPaul Mackerras /* Stub uses address relative to r2. */ 4217568cb4eSPaul Mackerras reladdr = (unsigned long)entry - my_r2(sechdrs, me); 4227568cb4eSPaul Mackerras if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 423c7d1f6afSAnton Blanchard pr_err("%s: Address %p of stub out of range of %p.\n", 4247568cb4eSPaul Mackerras me->name, (void *)reladdr, (void *)my_r2); 4257568cb4eSPaul Mackerras return 0; 4267568cb4eSPaul Mackerras } 427c7d1f6afSAnton Blanchard pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr); 4287568cb4eSPaul Mackerras 4290e60e46eSRusty Russell entry->jump[0] |= PPC_HA(reladdr); 4300e60e46eSRusty Russell entry->jump[1] |= PPC_LO(reladdr); 431008d7a91SRusty Russell entry->funcdata = func_desc(addr); 432f17c4e01SMichael Ellerman entry->magic = STUB_MAGIC; 433f17c4e01SMichael Ellerman 4347568cb4eSPaul Mackerras return 1; 4357568cb4eSPaul Mackerras } 4367568cb4eSPaul Mackerras 437008d7a91SRusty Russell /* Create stub to jump to function described in this OPD/ptr: we need the 4387568cb4eSPaul Mackerras stub to set up the TOC ptr (r2) for the function. */ 439136cd345SMichael Ellerman static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs, 440008d7a91SRusty Russell unsigned long addr, 4417568cb4eSPaul Mackerras struct module *me) 4427568cb4eSPaul Mackerras { 4437568cb4eSPaul Mackerras struct ppc64_stub_entry *stubs; 4447568cb4eSPaul Mackerras unsigned int i, num_stubs; 4457568cb4eSPaul Mackerras 4467568cb4eSPaul Mackerras num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); 4477568cb4eSPaul Mackerras 4487568cb4eSPaul Mackerras /* Find this stub, or if that fails, the next avail. entry */ 4497568cb4eSPaul Mackerras stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; 450008d7a91SRusty Russell for (i = 0; stub_func_addr(stubs[i].funcdata); i++) { 4511c0437afSKamalesh Babulal if (WARN_ON(i >= num_stubs)) 4521c0437afSKamalesh Babulal return 0; 4537568cb4eSPaul Mackerras 454008d7a91SRusty Russell if (stub_func_addr(stubs[i].funcdata) == func_addr(addr)) 4557568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4567568cb4eSPaul Mackerras } 4577568cb4eSPaul Mackerras 458008d7a91SRusty Russell if (!create_stub(sechdrs, &stubs[i], addr, me)) 4597568cb4eSPaul Mackerras return 0; 4607568cb4eSPaul Mackerras 4617568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4627568cb4eSPaul Mackerras } 4637568cb4eSPaul Mackerras 464abba7597SNicholas Piggin #ifdef CONFIG_MPROFILE_KERNEL 465250122baSNaveen N. Rao static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction) 46615308664STorsten Duwe { 467250122baSNaveen N. Rao if (strcmp("_mcount", name)) 468250122baSNaveen N. Rao return false; 469250122baSNaveen N. Rao 47015308664STorsten Duwe /* 47115308664STorsten Duwe * Check if this is one of the -mprofile-kernel sequences. 47215308664STorsten Duwe */ 47315308664STorsten Duwe if (instruction[-1] == PPC_INST_STD_LR && 47415308664STorsten Duwe instruction[-2] == PPC_INST_MFLR) 47515308664STorsten Duwe return true; 47615308664STorsten Duwe 47715308664STorsten Duwe if (instruction[-1] == PPC_INST_MFLR) 47815308664STorsten Duwe return true; 47915308664STorsten Duwe 48015308664STorsten Duwe return false; 48115308664STorsten Duwe } 48215308664STorsten Duwe 48315308664STorsten Duwe /* 48415308664STorsten Duwe * In case of _mcount calls, do not save the current callee's TOC (in r2) into 48515308664STorsten Duwe * the original caller's stack frame. If we did we would clobber the saved TOC 48615308664STorsten Duwe * value of the original caller. 48715308664STorsten Duwe */ 48815308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr) 48915308664STorsten Duwe { 49015308664STorsten Duwe struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr; 49115308664STorsten Duwe 49215308664STorsten Duwe /* Only for calls to _mcount */ 49315308664STorsten Duwe if (strcmp("_mcount", name) != 0) 49415308664STorsten Duwe return; 49515308664STorsten Duwe 49615308664STorsten Duwe stub->jump[2] = PPC_INST_NOP; 49715308664STorsten Duwe } 49815308664STorsten Duwe #else 49915308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr) { } 50015308664STorsten Duwe 501250122baSNaveen N. Rao static bool is_mprofile_mcount_callsite(const char *name, u32 *instruction) 50215308664STorsten Duwe { 50315308664STorsten Duwe return false; 50415308664STorsten Duwe } 50515308664STorsten Duwe #endif 50615308664STorsten Duwe 5077568cb4eSPaul Mackerras /* We expect a noop next: if it is, replace it with instruction to 5087568cb4eSPaul Mackerras restore r2. */ 509250122baSNaveen N. Rao static int restore_r2(const char *name, u32 *instruction, struct module *me) 5107568cb4eSPaul Mackerras { 511b9eab08dSJosh Poimboeuf u32 *prev_insn = instruction - 1; 512b9eab08dSJosh Poimboeuf 513250122baSNaveen N. Rao if (is_mprofile_mcount_callsite(name, prev_insn)) 514b9eab08dSJosh Poimboeuf return 1; 515b9eab08dSJosh Poimboeuf 516b9eab08dSJosh Poimboeuf /* 517b9eab08dSJosh Poimboeuf * Make sure the branch isn't a sibling call. Sibling calls aren't 518b9eab08dSJosh Poimboeuf * "link" branches and they don't return, so they don't need the r2 519b9eab08dSJosh Poimboeuf * restore afterwards. 520b9eab08dSJosh Poimboeuf */ 521b9eab08dSJosh Poimboeuf if (!instr_is_relative_link_branch(*prev_insn)) 52215308664STorsten Duwe return 1; 52331278b17SMichael Ellerman 52431278b17SMichael Ellerman if (*instruction != PPC_INST_NOP) { 5251ea61ea2SJosh Poimboeuf pr_err("%s: Expected nop after call, got %08x at %pS\n", 5261ea61ea2SJosh Poimboeuf me->name, *instruction, instruction); 5277568cb4eSPaul Mackerras return 0; 5287568cb4eSPaul Mackerras } 529d2fae548SRusty Russell /* ld r2,R2_STACK_OFFSET(r1) */ 53015308664STorsten Duwe *instruction = PPC_INST_LD_TOC; 5317568cb4eSPaul Mackerras return 1; 5327568cb4eSPaul Mackerras } 5337568cb4eSPaul Mackerras 5347568cb4eSPaul Mackerras int apply_relocate_add(Elf64_Shdr *sechdrs, 5357568cb4eSPaul Mackerras const char *strtab, 5367568cb4eSPaul Mackerras unsigned int symindex, 5377568cb4eSPaul Mackerras unsigned int relsec, 5387568cb4eSPaul Mackerras struct module *me) 5397568cb4eSPaul Mackerras { 5407568cb4eSPaul Mackerras unsigned int i; 5417568cb4eSPaul Mackerras Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 5427568cb4eSPaul Mackerras Elf64_Sym *sym; 5437568cb4eSPaul Mackerras unsigned long *location; 5447568cb4eSPaul Mackerras unsigned long value; 5457568cb4eSPaul Mackerras 546c7d1f6afSAnton Blanchard pr_debug("Applying ADD relocate section %u to %u\n", relsec, 5477568cb4eSPaul Mackerras sechdrs[relsec].sh_info); 5484edebbeaSRusty Russell 5494edebbeaSRusty Russell /* First time we're called, we can fix up .TOC. */ 5504edebbeaSRusty Russell if (!me->arch.toc_fixed) { 5514edebbeaSRusty Russell sym = find_dot_toc(sechdrs, strtab, symindex); 5524edebbeaSRusty Russell /* It's theoretically possible that a module doesn't want a 5534edebbeaSRusty Russell * .TOC. so don't fail it just for that. */ 5544edebbeaSRusty Russell if (sym) 5554edebbeaSRusty Russell sym->st_value = my_r2(sechdrs, me); 5564edebbeaSRusty Russell me->arch.toc_fixed = true; 5574edebbeaSRusty Russell } 5584edebbeaSRusty Russell 5597568cb4eSPaul Mackerras for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { 5607568cb4eSPaul Mackerras /* This is where to make the change */ 5617568cb4eSPaul Mackerras location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 5627568cb4eSPaul Mackerras + rela[i].r_offset; 5637568cb4eSPaul Mackerras /* This is the symbol it is referring to */ 5647568cb4eSPaul Mackerras sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 5657568cb4eSPaul Mackerras + ELF64_R_SYM(rela[i].r_info); 5667568cb4eSPaul Mackerras 567c7d1f6afSAnton Blanchard pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n", 5687568cb4eSPaul Mackerras location, (long)ELF64_R_TYPE(rela[i].r_info), 5697568cb4eSPaul Mackerras strtab + sym->st_name, (unsigned long)sym->st_value, 5707568cb4eSPaul Mackerras (long)rela[i].r_addend); 5717568cb4eSPaul Mackerras 5727568cb4eSPaul Mackerras /* `Everything is relative'. */ 5737568cb4eSPaul Mackerras value = sym->st_value + rela[i].r_addend; 5747568cb4eSPaul Mackerras 5757568cb4eSPaul Mackerras switch (ELF64_R_TYPE(rela[i].r_info)) { 5767568cb4eSPaul Mackerras case R_PPC64_ADDR32: 5777568cb4eSPaul Mackerras /* Simply set it */ 5787568cb4eSPaul Mackerras *(u32 *)location = value; 5797568cb4eSPaul Mackerras break; 5807568cb4eSPaul Mackerras 5817568cb4eSPaul Mackerras case R_PPC64_ADDR64: 5827568cb4eSPaul Mackerras /* Simply set it */ 5837568cb4eSPaul Mackerras *(unsigned long *)location = value; 5847568cb4eSPaul Mackerras break; 5857568cb4eSPaul Mackerras 5867568cb4eSPaul Mackerras case R_PPC64_TOC: 5877568cb4eSPaul Mackerras *(unsigned long *)location = my_r2(sechdrs, me); 5887568cb4eSPaul Mackerras break; 5897568cb4eSPaul Mackerras 5907568cb4eSPaul Mackerras case R_PPC64_TOC16: 591f749edaeSAlan Modra /* Subtract TOC pointer */ 5927568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 5937568cb4eSPaul Mackerras if (value + 0x8000 > 0xffff) { 594c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16 relocation (0x%lx)\n", 5957568cb4eSPaul Mackerras me->name, value); 5967568cb4eSPaul Mackerras return -ENOEXEC; 5977568cb4eSPaul Mackerras } 5987568cb4eSPaul Mackerras *((uint16_t *) location) 5997568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xffff) 6007568cb4eSPaul Mackerras | (value & 0xffff); 6017568cb4eSPaul Mackerras break; 6027568cb4eSPaul Mackerras 6031fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO: 6041fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 6051fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 6061fbe9cf2SAnton Blanchard *((uint16_t *) location) 6071fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 6081fbe9cf2SAnton Blanchard | (value & 0xffff); 6091fbe9cf2SAnton Blanchard break; 6101fbe9cf2SAnton Blanchard 6117568cb4eSPaul Mackerras case R_PPC64_TOC16_DS: 612f749edaeSAlan Modra /* Subtract TOC pointer */ 6137568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 6147568cb4eSPaul Mackerras if ((value & 3) != 0 || value + 0x8000 > 0xffff) { 615c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_DS relocation (0x%lx)\n", 6167568cb4eSPaul Mackerras me->name, value); 6177568cb4eSPaul Mackerras return -ENOEXEC; 6187568cb4eSPaul Mackerras } 6197568cb4eSPaul Mackerras *((uint16_t *) location) 6207568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xfffc) 6217568cb4eSPaul Mackerras | (value & 0xfffc); 6227568cb4eSPaul Mackerras break; 6237568cb4eSPaul Mackerras 6241fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO_DS: 6251fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 6261fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 6271fbe9cf2SAnton Blanchard if ((value & 3) != 0) { 628c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n", 6291fbe9cf2SAnton Blanchard me->name, value); 6301fbe9cf2SAnton Blanchard return -ENOEXEC; 6311fbe9cf2SAnton Blanchard } 6321fbe9cf2SAnton Blanchard *((uint16_t *) location) 6331fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xfffc) 6341fbe9cf2SAnton Blanchard | (value & 0xfffc); 6351fbe9cf2SAnton Blanchard break; 6361fbe9cf2SAnton Blanchard 6371fbe9cf2SAnton Blanchard case R_PPC64_TOC16_HA: 6381fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 6391fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 6401fbe9cf2SAnton Blanchard value = ((value + 0x8000) >> 16); 6411fbe9cf2SAnton Blanchard *((uint16_t *) location) 6421fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 6431fbe9cf2SAnton Blanchard | (value & 0xffff); 6441fbe9cf2SAnton Blanchard break; 6451fbe9cf2SAnton Blanchard 6467568cb4eSPaul Mackerras case R_PPC_REL24: 6477568cb4eSPaul Mackerras /* FIXME: Handle weak symbols here --RR */ 648a443bf6eSKamalesh Babulal if (sym->st_shndx == SHN_UNDEF || 649a443bf6eSKamalesh Babulal sym->st_shndx == SHN_LIVEPATCH) { 6507568cb4eSPaul Mackerras /* External: go via stub */ 6517568cb4eSPaul Mackerras value = stub_for_addr(sechdrs, value, me); 6527568cb4eSPaul Mackerras if (!value) 6537568cb4eSPaul Mackerras return -ENOENT; 654250122baSNaveen N. Rao if (!restore_r2(strtab + sym->st_name, 655250122baSNaveen N. Rao (u32 *)location + 1, me)) 6567568cb4eSPaul Mackerras return -ENOEXEC; 65715308664STorsten Duwe 65815308664STorsten Duwe squash_toc_save_inst(strtab + sym->st_name, value); 659008d7a91SRusty Russell } else 660008d7a91SRusty Russell value += local_entry_offset(sym); 6617568cb4eSPaul Mackerras 6627568cb4eSPaul Mackerras /* Convert value to relative */ 6637568cb4eSPaul Mackerras value -= (unsigned long)location; 6647568cb4eSPaul Mackerras if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ 665c7d1f6afSAnton Blanchard pr_err("%s: REL24 %li out of range!\n", 6667568cb4eSPaul Mackerras me->name, (long int)value); 6677568cb4eSPaul Mackerras return -ENOEXEC; 6687568cb4eSPaul Mackerras } 6697568cb4eSPaul Mackerras 6707568cb4eSPaul Mackerras /* Only replace bits 2 through 26 */ 6717568cb4eSPaul Mackerras *(uint32_t *)location 6727568cb4eSPaul Mackerras = (*(uint32_t *)location & ~0x03fffffc) 6737568cb4eSPaul Mackerras | (value & 0x03fffffc); 6747568cb4eSPaul Mackerras break; 6757568cb4eSPaul Mackerras 67621c4ff80SBenjamin Herrenschmidt case R_PPC64_REL64: 67721c4ff80SBenjamin Herrenschmidt /* 64 bits relative (used by features fixups) */ 67821c4ff80SBenjamin Herrenschmidt *location = value - (unsigned long)location; 67921c4ff80SBenjamin Herrenschmidt break; 68021c4ff80SBenjamin Herrenschmidt 6819f751b82SMichael Ellerman case R_PPC64_REL32: 6829f751b82SMichael Ellerman /* 32 bits relative (used by relative exception tables) */ 683b851ba02SNicholas Piggin /* Convert value to relative */ 684b851ba02SNicholas Piggin value -= (unsigned long)location; 685b851ba02SNicholas Piggin if (value + 0x80000000 > 0xffffffff) { 686b851ba02SNicholas Piggin pr_err("%s: REL32 %li out of range!\n", 687b851ba02SNicholas Piggin me->name, (long int)value); 688b851ba02SNicholas Piggin return -ENOEXEC; 689b851ba02SNicholas Piggin } 690b851ba02SNicholas Piggin *(u32 *)location = value; 6919f751b82SMichael Ellerman break; 6929f751b82SMichael Ellerman 693d247da0aSRusty Russell case R_PPC64_TOCSAVE: 694d247da0aSRusty Russell /* 695d247da0aSRusty Russell * Marker reloc indicates we don't have to save r2. 696d247da0aSRusty Russell * That would only save us one instruction, so ignore 697d247da0aSRusty Russell * it. 698d247da0aSRusty Russell */ 699d247da0aSRusty Russell break; 700d247da0aSRusty Russell 701a61674bdSUlrich Weigand case R_PPC64_ENTRY: 702a61674bdSUlrich Weigand /* 703a61674bdSUlrich Weigand * Optimize ELFv2 large code model entry point if 704a61674bdSUlrich Weigand * the TOC is within 2GB range of current location. 705a61674bdSUlrich Weigand */ 706a61674bdSUlrich Weigand value = my_r2(sechdrs, me) - (unsigned long)location; 707a61674bdSUlrich Weigand if (value + 0x80008000 > 0xffffffff) 708a61674bdSUlrich Weigand break; 709a61674bdSUlrich Weigand /* 710a61674bdSUlrich Weigand * Check for the large code model prolog sequence: 711a61674bdSUlrich Weigand * ld r2, ...(r12) 712a61674bdSUlrich Weigand * add r2, r2, r12 713a61674bdSUlrich Weigand */ 714*a2b6f26cSChristophe Leroy if ((((uint32_t *)location)[0] & ~0xfffc) != 715*a2b6f26cSChristophe Leroy (PPC_INST_LD | __PPC_RT(R2) | __PPC_RA(R12))) 716a61674bdSUlrich Weigand break; 717*a2b6f26cSChristophe Leroy if (((uint32_t *)location)[1] != 718*a2b6f26cSChristophe Leroy (PPC_INST_ADD | __PPC_RT(R2) | __PPC_RA(R2) | __PPC_RB(R12))) 719a61674bdSUlrich Weigand break; 720a61674bdSUlrich Weigand /* 721a61674bdSUlrich Weigand * If found, replace it with: 722a61674bdSUlrich Weigand * addis r2, r12, (.TOC.-func)@ha 7232fb0a2c9SMichael Ellerman * addi r2, r2, (.TOC.-func)@l 724a61674bdSUlrich Weigand */ 725*a2b6f26cSChristophe Leroy ((uint32_t *)location)[0] = PPC_INST_ADDIS | __PPC_RT(R2) | 726*a2b6f26cSChristophe Leroy __PPC_RA(R12) | PPC_HA(value); 727*a2b6f26cSChristophe Leroy ((uint32_t *)location)[1] = PPC_INST_ADDI | __PPC_RT(R2) | 728*a2b6f26cSChristophe Leroy __PPC_RA(R2) | PPC_LO(value); 729a61674bdSUlrich Weigand break; 730a61674bdSUlrich Weigand 7310906584aSRusty Russell case R_PPC64_REL16_HA: 7320906584aSRusty Russell /* Subtract location pointer */ 7330906584aSRusty Russell value -= (unsigned long)location; 7340906584aSRusty Russell value = ((value + 0x8000) >> 16); 7350906584aSRusty Russell *((uint16_t *) location) 7360906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 7370906584aSRusty Russell | (value & 0xffff); 7380906584aSRusty Russell break; 7390906584aSRusty Russell 7400906584aSRusty Russell case R_PPC64_REL16_LO: 7410906584aSRusty Russell /* Subtract location pointer */ 7420906584aSRusty Russell value -= (unsigned long)location; 7430906584aSRusty Russell *((uint16_t *) location) 7440906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 7450906584aSRusty Russell | (value & 0xffff); 7460906584aSRusty Russell break; 7470906584aSRusty Russell 7487568cb4eSPaul Mackerras default: 749c7d1f6afSAnton Blanchard pr_err("%s: Unknown ADD relocation: %lu\n", 7507568cb4eSPaul Mackerras me->name, 7517568cb4eSPaul Mackerras (unsigned long)ELF64_R_TYPE(rela[i].r_info)); 7527568cb4eSPaul Mackerras return -ENOEXEC; 7537568cb4eSPaul Mackerras } 7547568cb4eSPaul Mackerras } 7557568cb4eSPaul Mackerras 756136cd345SMichael Ellerman return 0; 757136cd345SMichael Ellerman } 758136cd345SMichael Ellerman 759f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 760336a7b5dSMichael Ellerman 761abba7597SNicholas Piggin #ifdef CONFIG_MPROFILE_KERNEL 762336a7b5dSMichael Ellerman 763336a7b5dSMichael Ellerman #define PACATOC offsetof(struct paca_struct, kernel_toc) 764336a7b5dSMichael Ellerman 765336a7b5dSMichael Ellerman /* 766336a7b5dSMichael Ellerman * For mprofile-kernel we use a special stub for ftrace_caller() because we 767336a7b5dSMichael Ellerman * can't rely on r2 containing this module's TOC when we enter the stub. 768336a7b5dSMichael Ellerman * 769336a7b5dSMichael Ellerman * That can happen if the function calling us didn't need to use the toc. In 770336a7b5dSMichael Ellerman * that case it won't have setup r2, and the r2 value will be either the 771336a7b5dSMichael Ellerman * kernel's toc, or possibly another modules toc. 772336a7b5dSMichael Ellerman * 773336a7b5dSMichael Ellerman * To deal with that this stub uses the kernel toc, which is always accessible 774336a7b5dSMichael Ellerman * via the paca (in r13). The target (ftrace_caller()) is responsible for 775336a7b5dSMichael Ellerman * saving and restoring the toc before returning. 776336a7b5dSMichael Ellerman */ 777ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, 778ae30cc05SNaveen N. Rao struct module *me, unsigned long addr) 779336a7b5dSMichael Ellerman { 780336a7b5dSMichael Ellerman struct ppc64_stub_entry *entry; 781336a7b5dSMichael Ellerman unsigned int i, num_stubs; 782*a2b6f26cSChristophe Leroy /* 783*a2b6f26cSChristophe Leroy * ld r12,PACATOC(r13) 784*a2b6f26cSChristophe Leroy * addis r12,r12,<high> 785*a2b6f26cSChristophe Leroy * addi r12,r12,<low> 786*a2b6f26cSChristophe Leroy * mtctr r12 787*a2b6f26cSChristophe Leroy * bctr 788*a2b6f26cSChristophe Leroy */ 789336a7b5dSMichael Ellerman static u32 stub_insns[] = { 790*a2b6f26cSChristophe Leroy PPC_INST_LD | __PPC_RT(R12) | __PPC_RA(R13) | PACATOC, 791*a2b6f26cSChristophe Leroy PPC_INST_ADDIS | __PPC_RT(R12) | __PPC_RA(R12), 792*a2b6f26cSChristophe Leroy PPC_INST_ADDI | __PPC_RT(R12) | __PPC_RA(R12), 793*a2b6f26cSChristophe Leroy PPC_INST_MTCTR | __PPC_RS(R12), 794*a2b6f26cSChristophe Leroy PPC_INST_BCTR, 795336a7b5dSMichael Ellerman }; 796336a7b5dSMichael Ellerman long reladdr; 797336a7b5dSMichael Ellerman 798336a7b5dSMichael Ellerman num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*entry); 799336a7b5dSMichael Ellerman 800336a7b5dSMichael Ellerman /* Find the next available stub entry */ 801336a7b5dSMichael Ellerman entry = (void *)sechdrs[me->arch.stubs_section].sh_addr; 802336a7b5dSMichael Ellerman for (i = 0; i < num_stubs && stub_func_addr(entry->funcdata); i++, entry++); 803336a7b5dSMichael Ellerman 804336a7b5dSMichael Ellerman if (i >= num_stubs) { 805336a7b5dSMichael Ellerman pr_err("%s: Unable to find a free slot for ftrace stub.\n", me->name); 806336a7b5dSMichael Ellerman return 0; 807336a7b5dSMichael Ellerman } 808336a7b5dSMichael Ellerman 809336a7b5dSMichael Ellerman memcpy(entry->jump, stub_insns, sizeof(stub_insns)); 810336a7b5dSMichael Ellerman 811336a7b5dSMichael Ellerman /* Stub uses address relative to kernel toc (from the paca) */ 812ae30cc05SNaveen N. Rao reladdr = addr - kernel_toc_addr(); 813336a7b5dSMichael Ellerman if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 814ae30cc05SNaveen N. Rao pr_err("%s: Address of %ps out of range of kernel_toc.\n", 815ae30cc05SNaveen N. Rao me->name, (void *)addr); 816336a7b5dSMichael Ellerman return 0; 817336a7b5dSMichael Ellerman } 818336a7b5dSMichael Ellerman 819336a7b5dSMichael Ellerman entry->jump[1] |= PPC_HA(reladdr); 820336a7b5dSMichael Ellerman entry->jump[2] |= PPC_LO(reladdr); 821336a7b5dSMichael Ellerman 822336a7b5dSMichael Ellerman /* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */ 823ae30cc05SNaveen N. Rao entry->funcdata = func_desc(addr); 824336a7b5dSMichael Ellerman entry->magic = STUB_MAGIC; 825336a7b5dSMichael Ellerman 826336a7b5dSMichael Ellerman return (unsigned long)entry; 827336a7b5dSMichael Ellerman } 828336a7b5dSMichael Ellerman #else 829ae30cc05SNaveen N. Rao static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, 830ae30cc05SNaveen N. Rao struct module *me, unsigned long addr) 831336a7b5dSMichael Ellerman { 832ae30cc05SNaveen N. Rao return stub_for_addr(sechdrs, addr, me); 833336a7b5dSMichael Ellerman } 834f48cb8b4SSteven Rostedt #endif 835f48cb8b4SSteven Rostedt 836136cd345SMichael Ellerman int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs) 837136cd345SMichael Ellerman { 838ae30cc05SNaveen N. Rao mod->arch.tramp = create_ftrace_stub(sechdrs, mod, 839ae30cc05SNaveen N. Rao (unsigned long)ftrace_caller); 840ae30cc05SNaveen N. Rao #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS 841ae30cc05SNaveen N. Rao mod->arch.tramp_regs = create_ftrace_stub(sechdrs, mod, 842ae30cc05SNaveen N. Rao (unsigned long)ftrace_regs_caller); 843ae30cc05SNaveen N. Rao if (!mod->arch.tramp_regs) 844ae30cc05SNaveen N. Rao return -ENOENT; 845ae30cc05SNaveen N. Rao #endif 846136cd345SMichael Ellerman 847136cd345SMichael Ellerman if (!mod->arch.tramp) 848136cd345SMichael Ellerman return -ENOENT; 8497568cb4eSPaul Mackerras 8507568cb4eSPaul Mackerras return 0; 8517568cb4eSPaul Mackerras } 852136cd345SMichael Ellerman #endif 853