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> 347568cb4eSPaul Mackerras 357568cb4eSPaul Mackerras /* FIXME: We don't do .init separately. To do this, we'd need to have 367568cb4eSPaul Mackerras a separate r2 value in the init and core section, and stub between 377568cb4eSPaul Mackerras them, too. 387568cb4eSPaul Mackerras 397568cb4eSPaul Mackerras Using a magic allocator which places modules within 32MB solves 407568cb4eSPaul Mackerras this, and makes other things simpler. Anton? 417568cb4eSPaul Mackerras --RR. */ 427568cb4eSPaul Mackerras 43d2fae548SRusty Russell #if defined(_CALL_ELF) && _CALL_ELF == 2 44d2fae548SRusty Russell #define R2_STACK_OFFSET 24 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 76d2fae548SRusty Russell #define R2_STACK_OFFSET 40 77008d7a91SRusty Russell 78008d7a91SRusty Russell /* An address is address of the OPD entry, which contains address of fn. */ 79008d7a91SRusty Russell typedef struct ppc64_opd_entry func_desc_t; 80008d7a91SRusty Russell 81008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr) 82008d7a91SRusty Russell { 83008d7a91SRusty Russell return *(struct ppc64_opd_entry *)addr; 84008d7a91SRusty Russell } 85008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr) 86008d7a91SRusty Russell { 87008d7a91SRusty Russell return func_desc(addr).funcaddr; 88008d7a91SRusty Russell } 89008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func) 90008d7a91SRusty Russell { 91008d7a91SRusty Russell return func.funcaddr; 92008d7a91SRusty Russell } 93008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym) 94008d7a91SRusty Russell { 95008d7a91SRusty Russell return 0; 96008d7a91SRusty Russell } 97d2fae548SRusty Russell #endif 98d2fae548SRusty Russell 99*f17c4e01SMichael Ellerman #define STUB_MAGIC 0x73747562 /* stub */ 100*f17c4e01SMichael Ellerman 1017568cb4eSPaul Mackerras /* Like PPC32, we need little trampolines to do > 24-bit jumps (into 1027568cb4eSPaul Mackerras the kernel itself). But on PPC64, these need to be used for every 1037568cb4eSPaul Mackerras jump, actually, to reset r2 (TOC+0x8000). */ 1047568cb4eSPaul Mackerras struct ppc64_stub_entry 1057568cb4eSPaul Mackerras { 10683775b85SAnton Blanchard /* 28 byte jump instruction sequence (7 instructions). We only 10783775b85SAnton Blanchard * need 6 instructions on ABIv2 but we always allocate 7 so 10883775b85SAnton Blanchard * so we don't have to modify the trampoline load instruction. */ 1090e60e46eSRusty Russell u32 jump[7]; 110*f17c4e01SMichael Ellerman /* Used by ftrace to identify stubs */ 111*f17c4e01SMichael Ellerman u32 magic; 1127568cb4eSPaul Mackerras /* Data for the above code */ 113008d7a91SRusty Russell func_desc_t funcdata; 1147568cb4eSPaul Mackerras }; 1157568cb4eSPaul Mackerras 1165c729a11SRusty Russell /* 1175c729a11SRusty Russell * PPC64 uses 24 bit jumps, but we need to jump into other modules or 1185c729a11SRusty Russell * the kernel which may be further. So we jump to a stub. 1195c729a11SRusty Russell * 1205c729a11SRusty Russell * For ELFv1 we need to use this to set up the new r2 value (aka TOC 1215c729a11SRusty Russell * pointer). For ELFv2 it's the callee's responsibility to set up the 1225c729a11SRusty Russell * new r2, but for both we need to save the old r2. 1235c729a11SRusty Russell * 1245c729a11SRusty Russell * We could simply patch the new r2 value and function pointer into 1255c729a11SRusty Russell * the stub, but it's significantly shorter to put these values at the 1265c729a11SRusty Russell * end of the stub code, and patch the stub address (32-bits relative 1275c729a11SRusty Russell * to the TOC ptr, r2) into the stub. 1285c729a11SRusty Russell */ 12983775b85SAnton Blanchard 13083775b85SAnton Blanchard static u32 ppc64_stub_insns[] = { 131b1ce369eSRusty Russell 0x3d620000, /* addis r11,r2, <high> */ 132b1ce369eSRusty Russell 0x396b0000, /* addi r11,r11, <low> */ 133fed8393eSEugene Surovegin /* Save current r2 value in magic place on the stack. */ 134d2fae548SRusty Russell 0xf8410000|R2_STACK_OFFSET, /* std r2,R2_STACK_OFFSET(r1) */ 135b1ce369eSRusty Russell 0xe98b0020, /* ld r12,32(r11) */ 1365c729a11SRusty Russell #if !defined(_CALL_ELF) || _CALL_ELF != 2 1375c729a11SRusty Russell /* Set up new r2 from function descriptor */ 138872aa779SRusty Russell 0xe84b0028, /* ld r2,40(r11) */ 1395c729a11SRusty Russell #endif 140b1ce369eSRusty Russell 0x7d8903a6, /* mtctr r12 */ 1410e60e46eSRusty Russell 0x4e800420 /* bctr */ 14283775b85SAnton Blanchard }; 14383775b85SAnton Blanchard 14483775b85SAnton Blanchard #ifdef CONFIG_DYNAMIC_FTRACE 145*f17c4e01SMichael Ellerman int module_trampoline_target(struct module *mod, unsigned long addr, 146dd9fa162SAnton Blanchard unsigned long *target) 147dd9fa162SAnton Blanchard { 148*f17c4e01SMichael Ellerman struct ppc64_stub_entry *stub; 149*f17c4e01SMichael Ellerman func_desc_t funcdata; 150*f17c4e01SMichael Ellerman u32 magic; 151dd9fa162SAnton Blanchard 152*f17c4e01SMichael Ellerman if (!within_module_core(addr, mod)) { 153*f17c4e01SMichael Ellerman pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name); 154dd9fa162SAnton Blanchard return -EFAULT; 155*f17c4e01SMichael Ellerman } 156dd9fa162SAnton Blanchard 157*f17c4e01SMichael Ellerman stub = (struct ppc64_stub_entry *)addr; 158dd9fa162SAnton Blanchard 159*f17c4e01SMichael Ellerman if (probe_kernel_read(&magic, &stub->magic, sizeof(magic))) { 160*f17c4e01SMichael Ellerman pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name); 161dd9fa162SAnton Blanchard return -EFAULT; 162*f17c4e01SMichael Ellerman } 163*f17c4e01SMichael Ellerman 164*f17c4e01SMichael Ellerman if (magic != STUB_MAGIC) { 165*f17c4e01SMichael Ellerman pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name); 166*f17c4e01SMichael Ellerman return -EFAULT; 167*f17c4e01SMichael Ellerman } 168*f17c4e01SMichael Ellerman 169*f17c4e01SMichael Ellerman if (probe_kernel_read(&funcdata, &stub->funcdata, sizeof(funcdata))) { 170*f17c4e01SMichael Ellerman pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name); 171*f17c4e01SMichael Ellerman return -EFAULT; 172*f17c4e01SMichael Ellerman } 173*f17c4e01SMichael Ellerman 174*f17c4e01SMichael Ellerman *target = stub_func_addr(funcdata); 175dd9fa162SAnton Blanchard 176dd9fa162SAnton Blanchard return 0; 177dd9fa162SAnton Blanchard } 17883775b85SAnton Blanchard #endif 1797568cb4eSPaul Mackerras 1807568cb4eSPaul Mackerras /* Count how many different 24-bit relocations (different symbol, 1817568cb4eSPaul Mackerras different addend) */ 1827568cb4eSPaul Mackerras static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num) 1837568cb4eSPaul Mackerras { 184eda09fbdSEmil Medve unsigned int i, r_info, r_addend, _count_relocs; 1857568cb4eSPaul Mackerras 1867568cb4eSPaul Mackerras /* FIXME: Only count external ones --RR */ 187eda09fbdSEmil Medve _count_relocs = 0; 188eda09fbdSEmil Medve r_info = 0; 189eda09fbdSEmil Medve r_addend = 0; 190eda09fbdSEmil Medve for (i = 0; i < num; i++) 1917568cb4eSPaul Mackerras /* Only count 24-bit relocs, others don't need stubs */ 192eda09fbdSEmil Medve if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 && 193eda09fbdSEmil Medve (r_info != ELF64_R_SYM(rela[i].r_info) || 194eda09fbdSEmil Medve r_addend != rela[i].r_addend)) { 195eda09fbdSEmil Medve _count_relocs++; 196eda09fbdSEmil Medve r_info = ELF64_R_SYM(rela[i].r_info); 197eda09fbdSEmil Medve r_addend = rela[i].r_addend; 1987568cb4eSPaul Mackerras } 199eda09fbdSEmil Medve 200eda09fbdSEmil Medve return _count_relocs; 2017568cb4eSPaul Mackerras } 2027568cb4eSPaul Mackerras 203eda09fbdSEmil Medve static int relacmp(const void *_x, const void *_y) 204eda09fbdSEmil Medve { 205eda09fbdSEmil Medve const Elf64_Rela *x, *y; 206eda09fbdSEmil Medve 207eda09fbdSEmil Medve y = (Elf64_Rela *)_x; 208eda09fbdSEmil Medve x = (Elf64_Rela *)_y; 209eda09fbdSEmil Medve 210eda09fbdSEmil Medve /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to 211eda09fbdSEmil Medve * make the comparison cheaper/faster. It won't affect the sorting or 212eda09fbdSEmil Medve * the counting algorithms' performance 213eda09fbdSEmil Medve */ 214eda09fbdSEmil Medve if (x->r_info < y->r_info) 215eda09fbdSEmil Medve return -1; 216eda09fbdSEmil Medve else if (x->r_info > y->r_info) 217eda09fbdSEmil Medve return 1; 218eda09fbdSEmil Medve else if (x->r_addend < y->r_addend) 219eda09fbdSEmil Medve return -1; 220eda09fbdSEmil Medve else if (x->r_addend > y->r_addend) 221eda09fbdSEmil Medve return 1; 222eda09fbdSEmil Medve else 223eda09fbdSEmil Medve return 0; 224eda09fbdSEmil Medve } 225eda09fbdSEmil Medve 226eda09fbdSEmil Medve static void relaswap(void *_x, void *_y, int size) 227eda09fbdSEmil Medve { 228eda09fbdSEmil Medve uint64_t *x, *y, tmp; 229eda09fbdSEmil Medve int i; 230eda09fbdSEmil Medve 231eda09fbdSEmil Medve y = (uint64_t *)_x; 232eda09fbdSEmil Medve x = (uint64_t *)_y; 233eda09fbdSEmil Medve 234eda09fbdSEmil Medve for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) { 235eda09fbdSEmil Medve tmp = x[i]; 236eda09fbdSEmil Medve x[i] = y[i]; 237eda09fbdSEmil Medve y[i] = tmp; 238eda09fbdSEmil Medve } 239eda09fbdSEmil Medve } 240eda09fbdSEmil Medve 2417568cb4eSPaul Mackerras /* Get size of potential trampolines required. */ 2427568cb4eSPaul Mackerras static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, 2437568cb4eSPaul Mackerras const Elf64_Shdr *sechdrs) 2447568cb4eSPaul Mackerras { 2457568cb4eSPaul Mackerras /* One extra reloc so it's always 0-funcaddr terminated */ 2467568cb4eSPaul Mackerras unsigned long relocs = 1; 2477568cb4eSPaul Mackerras unsigned i; 2487568cb4eSPaul Mackerras 2497568cb4eSPaul Mackerras /* Every relocated section... */ 2507568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 2517568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_RELA) { 252c7d1f6afSAnton Blanchard pr_debug("Found relocations in section %u\n", i); 253c7d1f6afSAnton Blanchard pr_debug("Ptr: %p. Number: %Lu\n", 2547568cb4eSPaul Mackerras (void *)sechdrs[i].sh_addr, 2557568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Rela)); 256eda09fbdSEmil Medve 257eda09fbdSEmil Medve /* Sort the relocation information based on a symbol and 258eda09fbdSEmil Medve * addend key. This is a stable O(n*log n) complexity 259eda09fbdSEmil Medve * alogrithm but it will reduce the complexity of 260eda09fbdSEmil Medve * count_relocs() to linear complexity O(n) 261eda09fbdSEmil Medve */ 262eda09fbdSEmil Medve sort((void *)sechdrs[i].sh_addr, 263eda09fbdSEmil Medve sechdrs[i].sh_size / sizeof(Elf64_Rela), 264eda09fbdSEmil Medve sizeof(Elf64_Rela), relacmp, relaswap); 265eda09fbdSEmil Medve 2667568cb4eSPaul Mackerras relocs += count_relocs((void *)sechdrs[i].sh_addr, 2677568cb4eSPaul Mackerras sechdrs[i].sh_size 2687568cb4eSPaul Mackerras / sizeof(Elf64_Rela)); 2697568cb4eSPaul Mackerras } 2707568cb4eSPaul Mackerras } 2717568cb4eSPaul Mackerras 272f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 273f48cb8b4SSteven Rostedt /* make the trampoline to the ftrace_caller */ 274f48cb8b4SSteven Rostedt relocs++; 275f48cb8b4SSteven Rostedt #endif 276f48cb8b4SSteven Rostedt 277c7d1f6afSAnton Blanchard pr_debug("Looks like a total of %lu stubs, max\n", relocs); 2787568cb4eSPaul Mackerras return relocs * sizeof(struct ppc64_stub_entry); 2797568cb4eSPaul Mackerras } 2807568cb4eSPaul Mackerras 2815b12c5c6SRusty Russell /* Still needed for ELFv2, for .TOC. */ 2827568cb4eSPaul Mackerras static void dedotify_versions(struct modversion_info *vers, 2837568cb4eSPaul Mackerras unsigned long size) 2847568cb4eSPaul Mackerras { 2857568cb4eSPaul Mackerras struct modversion_info *end; 2867568cb4eSPaul Mackerras 2877568cb4eSPaul Mackerras for (end = (void *)vers + size; vers < end; vers++) 288c2cbcf53SLaurent Dufour if (vers->name[0] == '.') { 2897568cb4eSPaul Mackerras memmove(vers->name, vers->name+1, strlen(vers->name)); 290c2cbcf53SLaurent Dufour #ifdef ARCH_RELOCATES_KCRCTAB 291c2cbcf53SLaurent Dufour /* The TOC symbol has no CRC computed. To avoid CRC 292c2cbcf53SLaurent Dufour * check failing, we must force it to the expected 293c2cbcf53SLaurent Dufour * value (see CRC check in module.c). 294c2cbcf53SLaurent Dufour */ 295c2cbcf53SLaurent Dufour if (!strcmp(vers->name, "TOC.")) 296c2cbcf53SLaurent Dufour vers->crc = -(unsigned long)reloc_start; 297c2cbcf53SLaurent Dufour #endif 298c2cbcf53SLaurent Dufour } 2997568cb4eSPaul Mackerras } 3007568cb4eSPaul Mackerras 301c153693dSAlan Modra /* 302c153693dSAlan Modra * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. 303c153693dSAlan Modra * seem to be defined (value set later). 304c153693dSAlan Modra */ 3057568cb4eSPaul Mackerras static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) 3067568cb4eSPaul Mackerras { 3077568cb4eSPaul Mackerras unsigned int i; 3087568cb4eSPaul Mackerras 3097568cb4eSPaul Mackerras for (i = 1; i < numsyms; i++) { 3107568cb4eSPaul Mackerras if (syms[i].st_shndx == SHN_UNDEF) { 3117568cb4eSPaul Mackerras char *name = strtab + syms[i].st_name; 312c153693dSAlan Modra if (name[0] == '.') { 313c153693dSAlan Modra if (strcmp(name+1, "TOC.") == 0) 314c153693dSAlan Modra syms[i].st_shndx = SHN_ABS; 3157568cb4eSPaul Mackerras memmove(name, name+1, strlen(name)); 3167568cb4eSPaul Mackerras } 3177568cb4eSPaul Mackerras } 3187568cb4eSPaul Mackerras } 319c153693dSAlan Modra } 3207568cb4eSPaul Mackerras 3214edebbeaSRusty Russell static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs, 3224edebbeaSRusty Russell const char *strtab, 3234edebbeaSRusty Russell unsigned int symindex) 3244edebbeaSRusty Russell { 3254edebbeaSRusty Russell unsigned int i, numsyms; 3264edebbeaSRusty Russell Elf64_Sym *syms; 3274edebbeaSRusty Russell 3284edebbeaSRusty Russell syms = (Elf64_Sym *)sechdrs[symindex].sh_addr; 3294edebbeaSRusty Russell numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym); 3304edebbeaSRusty Russell 3314edebbeaSRusty Russell for (i = 1; i < numsyms; i++) { 332c153693dSAlan Modra if (syms[i].st_shndx == SHN_ABS 333008d7a91SRusty Russell && strcmp(strtab + syms[i].st_name, "TOC.") == 0) 3344edebbeaSRusty Russell return &syms[i]; 3354edebbeaSRusty Russell } 3364edebbeaSRusty Russell return NULL; 3374edebbeaSRusty Russell } 3384edebbeaSRusty Russell 3397568cb4eSPaul Mackerras int module_frob_arch_sections(Elf64_Ehdr *hdr, 3407568cb4eSPaul Mackerras Elf64_Shdr *sechdrs, 3417568cb4eSPaul Mackerras char *secstrings, 3427568cb4eSPaul Mackerras struct module *me) 3437568cb4eSPaul Mackerras { 3447568cb4eSPaul Mackerras unsigned int i; 3457568cb4eSPaul Mackerras 3467568cb4eSPaul Mackerras /* Find .toc and .stubs sections, symtab and strtab */ 3477568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 3487568cb4eSPaul Mackerras char *p; 3497568cb4eSPaul Mackerras if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) 3507568cb4eSPaul Mackerras me->arch.stubs_section = i; 3517568cb4eSPaul Mackerras else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) 3527568cb4eSPaul Mackerras me->arch.toc_section = i; 3537568cb4eSPaul Mackerras else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) 3547568cb4eSPaul Mackerras dedotify_versions((void *)hdr + sechdrs[i].sh_offset, 3557568cb4eSPaul Mackerras sechdrs[i].sh_size); 3567568cb4eSPaul Mackerras 3577568cb4eSPaul Mackerras /* We don't handle .init for the moment: rename to _init */ 3587568cb4eSPaul Mackerras while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init"))) 3597568cb4eSPaul Mackerras p[0] = '_'; 3607568cb4eSPaul Mackerras 3617568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_SYMTAB) 3627568cb4eSPaul Mackerras dedotify((void *)hdr + sechdrs[i].sh_offset, 3637568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Sym), 3647568cb4eSPaul Mackerras (void *)hdr 3657568cb4eSPaul Mackerras + sechdrs[sechdrs[i].sh_link].sh_offset); 3667568cb4eSPaul Mackerras } 367f749edaeSAlan Modra 368f749edaeSAlan Modra if (!me->arch.stubs_section) { 369c7d1f6afSAnton Blanchard pr_err("%s: doesn't contain .stubs.\n", me->name); 3707568cb4eSPaul Mackerras return -ENOEXEC; 3717568cb4eSPaul Mackerras } 3727568cb4eSPaul Mackerras 373f749edaeSAlan Modra /* If we don't have a .toc, just use .stubs. We need to set r2 374f749edaeSAlan Modra to some reasonable value in case the module calls out to 375f749edaeSAlan Modra other functions via a stub, or if a function pointer escapes 376f749edaeSAlan Modra the module by some means. */ 377f749edaeSAlan Modra if (!me->arch.toc_section) 378f749edaeSAlan Modra me->arch.toc_section = me->arch.stubs_section; 379f749edaeSAlan Modra 3807568cb4eSPaul Mackerras /* Override the stubs size */ 3817568cb4eSPaul Mackerras sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs); 3827568cb4eSPaul Mackerras return 0; 3837568cb4eSPaul Mackerras } 3847568cb4eSPaul Mackerras 3857568cb4eSPaul Mackerras /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this 3867568cb4eSPaul Mackerras gives the value maximum span in an instruction which uses a signed 3877568cb4eSPaul Mackerras offset) */ 388136cd345SMichael Ellerman static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me) 3897568cb4eSPaul Mackerras { 3907568cb4eSPaul Mackerras return sechdrs[me->arch.toc_section].sh_addr + 0x8000; 3917568cb4eSPaul Mackerras } 3927568cb4eSPaul Mackerras 3937568cb4eSPaul Mackerras /* Both low and high 16 bits are added as SIGNED additions, so if low 3947568cb4eSPaul Mackerras 16 bits has high bit set, high 16 bits must be adjusted. These 3957568cb4eSPaul Mackerras macros do that (stolen from binutils). */ 3967568cb4eSPaul Mackerras #define PPC_LO(v) ((v) & 0xffff) 3977568cb4eSPaul Mackerras #define PPC_HI(v) (((v) >> 16) & 0xffff) 3987568cb4eSPaul Mackerras #define PPC_HA(v) PPC_HI ((v) + 0x8000) 3997568cb4eSPaul Mackerras 4007568cb4eSPaul Mackerras /* Patch stub to reference function and correct r2 value. */ 401136cd345SMichael Ellerman static inline int create_stub(const Elf64_Shdr *sechdrs, 4027568cb4eSPaul Mackerras struct ppc64_stub_entry *entry, 403008d7a91SRusty Russell unsigned long addr, 4047568cb4eSPaul Mackerras struct module *me) 4057568cb4eSPaul Mackerras { 4067568cb4eSPaul Mackerras long reladdr; 4077568cb4eSPaul Mackerras 40883775b85SAnton Blanchard memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns)); 4097568cb4eSPaul Mackerras 4107568cb4eSPaul Mackerras /* Stub uses address relative to r2. */ 4117568cb4eSPaul Mackerras reladdr = (unsigned long)entry - my_r2(sechdrs, me); 4127568cb4eSPaul Mackerras if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 413c7d1f6afSAnton Blanchard pr_err("%s: Address %p of stub out of range of %p.\n", 4147568cb4eSPaul Mackerras me->name, (void *)reladdr, (void *)my_r2); 4157568cb4eSPaul Mackerras return 0; 4167568cb4eSPaul Mackerras } 417c7d1f6afSAnton Blanchard pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr); 4187568cb4eSPaul Mackerras 4190e60e46eSRusty Russell entry->jump[0] |= PPC_HA(reladdr); 4200e60e46eSRusty Russell entry->jump[1] |= PPC_LO(reladdr); 421008d7a91SRusty Russell entry->funcdata = func_desc(addr); 422*f17c4e01SMichael Ellerman entry->magic = STUB_MAGIC; 423*f17c4e01SMichael Ellerman 4247568cb4eSPaul Mackerras return 1; 4257568cb4eSPaul Mackerras } 4267568cb4eSPaul Mackerras 427008d7a91SRusty Russell /* Create stub to jump to function described in this OPD/ptr: we need the 4287568cb4eSPaul Mackerras stub to set up the TOC ptr (r2) for the function. */ 429136cd345SMichael Ellerman static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs, 430008d7a91SRusty Russell unsigned long addr, 4317568cb4eSPaul Mackerras struct module *me) 4327568cb4eSPaul Mackerras { 4337568cb4eSPaul Mackerras struct ppc64_stub_entry *stubs; 4347568cb4eSPaul Mackerras unsigned int i, num_stubs; 4357568cb4eSPaul Mackerras 4367568cb4eSPaul Mackerras num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); 4377568cb4eSPaul Mackerras 4387568cb4eSPaul Mackerras /* Find this stub, or if that fails, the next avail. entry */ 4397568cb4eSPaul Mackerras stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; 440008d7a91SRusty Russell for (i = 0; stub_func_addr(stubs[i].funcdata); i++) { 4417568cb4eSPaul Mackerras BUG_ON(i >= num_stubs); 4427568cb4eSPaul Mackerras 443008d7a91SRusty Russell if (stub_func_addr(stubs[i].funcdata) == func_addr(addr)) 4447568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4457568cb4eSPaul Mackerras } 4467568cb4eSPaul Mackerras 447008d7a91SRusty Russell if (!create_stub(sechdrs, &stubs[i], addr, me)) 4487568cb4eSPaul Mackerras return 0; 4497568cb4eSPaul Mackerras 4507568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4517568cb4eSPaul Mackerras } 4527568cb4eSPaul Mackerras 4537568cb4eSPaul Mackerras /* We expect a noop next: if it is, replace it with instruction to 4547568cb4eSPaul Mackerras restore r2. */ 4557568cb4eSPaul Mackerras static int restore_r2(u32 *instruction, struct module *me) 4567568cb4eSPaul Mackerras { 45716c57b36SKumar Gala if (*instruction != PPC_INST_NOP) { 458c7d1f6afSAnton Blanchard pr_err("%s: Expect noop after relocate, got %08x\n", 4597568cb4eSPaul Mackerras me->name, *instruction); 4607568cb4eSPaul Mackerras return 0; 4617568cb4eSPaul Mackerras } 462d2fae548SRusty Russell /* ld r2,R2_STACK_OFFSET(r1) */ 463d2fae548SRusty Russell *instruction = 0xe8410000 | R2_STACK_OFFSET; 4647568cb4eSPaul Mackerras return 1; 4657568cb4eSPaul Mackerras } 4667568cb4eSPaul Mackerras 4677568cb4eSPaul Mackerras int apply_relocate_add(Elf64_Shdr *sechdrs, 4687568cb4eSPaul Mackerras const char *strtab, 4697568cb4eSPaul Mackerras unsigned int symindex, 4707568cb4eSPaul Mackerras unsigned int relsec, 4717568cb4eSPaul Mackerras struct module *me) 4727568cb4eSPaul Mackerras { 4737568cb4eSPaul Mackerras unsigned int i; 4747568cb4eSPaul Mackerras Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 4757568cb4eSPaul Mackerras Elf64_Sym *sym; 4767568cb4eSPaul Mackerras unsigned long *location; 4777568cb4eSPaul Mackerras unsigned long value; 4787568cb4eSPaul Mackerras 479c7d1f6afSAnton Blanchard pr_debug("Applying ADD relocate section %u to %u\n", relsec, 4807568cb4eSPaul Mackerras sechdrs[relsec].sh_info); 4814edebbeaSRusty Russell 4824edebbeaSRusty Russell /* First time we're called, we can fix up .TOC. */ 4834edebbeaSRusty Russell if (!me->arch.toc_fixed) { 4844edebbeaSRusty Russell sym = find_dot_toc(sechdrs, strtab, symindex); 4854edebbeaSRusty Russell /* It's theoretically possible that a module doesn't want a 4864edebbeaSRusty Russell * .TOC. so don't fail it just for that. */ 4874edebbeaSRusty Russell if (sym) 4884edebbeaSRusty Russell sym->st_value = my_r2(sechdrs, me); 4894edebbeaSRusty Russell me->arch.toc_fixed = true; 4904edebbeaSRusty Russell } 4914edebbeaSRusty Russell 4927568cb4eSPaul Mackerras for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { 4937568cb4eSPaul Mackerras /* This is where to make the change */ 4947568cb4eSPaul Mackerras location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 4957568cb4eSPaul Mackerras + rela[i].r_offset; 4967568cb4eSPaul Mackerras /* This is the symbol it is referring to */ 4977568cb4eSPaul Mackerras sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 4987568cb4eSPaul Mackerras + ELF64_R_SYM(rela[i].r_info); 4997568cb4eSPaul Mackerras 500c7d1f6afSAnton Blanchard pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n", 5017568cb4eSPaul Mackerras location, (long)ELF64_R_TYPE(rela[i].r_info), 5027568cb4eSPaul Mackerras strtab + sym->st_name, (unsigned long)sym->st_value, 5037568cb4eSPaul Mackerras (long)rela[i].r_addend); 5047568cb4eSPaul Mackerras 5057568cb4eSPaul Mackerras /* `Everything is relative'. */ 5067568cb4eSPaul Mackerras value = sym->st_value + rela[i].r_addend; 5077568cb4eSPaul Mackerras 5087568cb4eSPaul Mackerras switch (ELF64_R_TYPE(rela[i].r_info)) { 5097568cb4eSPaul Mackerras case R_PPC64_ADDR32: 5107568cb4eSPaul Mackerras /* Simply set it */ 5117568cb4eSPaul Mackerras *(u32 *)location = value; 5127568cb4eSPaul Mackerras break; 5137568cb4eSPaul Mackerras 5147568cb4eSPaul Mackerras case R_PPC64_ADDR64: 5157568cb4eSPaul Mackerras /* Simply set it */ 5167568cb4eSPaul Mackerras *(unsigned long *)location = value; 5177568cb4eSPaul Mackerras break; 5187568cb4eSPaul Mackerras 5197568cb4eSPaul Mackerras case R_PPC64_TOC: 5207568cb4eSPaul Mackerras *(unsigned long *)location = my_r2(sechdrs, me); 5217568cb4eSPaul Mackerras break; 5227568cb4eSPaul Mackerras 5237568cb4eSPaul Mackerras case R_PPC64_TOC16: 524f749edaeSAlan Modra /* Subtract TOC pointer */ 5257568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 5267568cb4eSPaul Mackerras if (value + 0x8000 > 0xffff) { 527c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16 relocation (0x%lx)\n", 5287568cb4eSPaul Mackerras me->name, value); 5297568cb4eSPaul Mackerras return -ENOEXEC; 5307568cb4eSPaul Mackerras } 5317568cb4eSPaul Mackerras *((uint16_t *) location) 5327568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xffff) 5337568cb4eSPaul Mackerras | (value & 0xffff); 5347568cb4eSPaul Mackerras break; 5357568cb4eSPaul Mackerras 5361fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO: 5371fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5381fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 5391fbe9cf2SAnton Blanchard *((uint16_t *) location) 5401fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 5411fbe9cf2SAnton Blanchard | (value & 0xffff); 5421fbe9cf2SAnton Blanchard break; 5431fbe9cf2SAnton Blanchard 5447568cb4eSPaul Mackerras case R_PPC64_TOC16_DS: 545f749edaeSAlan Modra /* Subtract TOC pointer */ 5467568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 5477568cb4eSPaul Mackerras if ((value & 3) != 0 || value + 0x8000 > 0xffff) { 548c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_DS relocation (0x%lx)\n", 5497568cb4eSPaul Mackerras me->name, value); 5507568cb4eSPaul Mackerras return -ENOEXEC; 5517568cb4eSPaul Mackerras } 5527568cb4eSPaul Mackerras *((uint16_t *) location) 5537568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xfffc) 5547568cb4eSPaul Mackerras | (value & 0xfffc); 5557568cb4eSPaul Mackerras break; 5567568cb4eSPaul Mackerras 5571fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO_DS: 5581fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5591fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 5601fbe9cf2SAnton Blanchard if ((value & 3) != 0) { 561c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n", 5621fbe9cf2SAnton Blanchard me->name, value); 5631fbe9cf2SAnton Blanchard return -ENOEXEC; 5641fbe9cf2SAnton Blanchard } 5651fbe9cf2SAnton Blanchard *((uint16_t *) location) 5661fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xfffc) 5671fbe9cf2SAnton Blanchard | (value & 0xfffc); 5681fbe9cf2SAnton Blanchard break; 5691fbe9cf2SAnton Blanchard 5701fbe9cf2SAnton Blanchard case R_PPC64_TOC16_HA: 5711fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5721fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 5731fbe9cf2SAnton Blanchard value = ((value + 0x8000) >> 16); 5741fbe9cf2SAnton Blanchard *((uint16_t *) location) 5751fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 5761fbe9cf2SAnton Blanchard | (value & 0xffff); 5771fbe9cf2SAnton Blanchard break; 5781fbe9cf2SAnton Blanchard 5797568cb4eSPaul Mackerras case R_PPC_REL24: 5807568cb4eSPaul Mackerras /* FIXME: Handle weak symbols here --RR */ 5817568cb4eSPaul Mackerras if (sym->st_shndx == SHN_UNDEF) { 5827568cb4eSPaul Mackerras /* External: go via stub */ 5837568cb4eSPaul Mackerras value = stub_for_addr(sechdrs, value, me); 5847568cb4eSPaul Mackerras if (!value) 5857568cb4eSPaul Mackerras return -ENOENT; 5867568cb4eSPaul Mackerras if (!restore_r2((u32 *)location + 1, me)) 5877568cb4eSPaul Mackerras return -ENOEXEC; 588008d7a91SRusty Russell } else 589008d7a91SRusty Russell value += local_entry_offset(sym); 5907568cb4eSPaul Mackerras 5917568cb4eSPaul Mackerras /* Convert value to relative */ 5927568cb4eSPaul Mackerras value -= (unsigned long)location; 5937568cb4eSPaul Mackerras if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ 594c7d1f6afSAnton Blanchard pr_err("%s: REL24 %li out of range!\n", 5957568cb4eSPaul Mackerras me->name, (long int)value); 5967568cb4eSPaul Mackerras return -ENOEXEC; 5977568cb4eSPaul Mackerras } 5987568cb4eSPaul Mackerras 5997568cb4eSPaul Mackerras /* Only replace bits 2 through 26 */ 6007568cb4eSPaul Mackerras *(uint32_t *)location 6017568cb4eSPaul Mackerras = (*(uint32_t *)location & ~0x03fffffc) 6027568cb4eSPaul Mackerras | (value & 0x03fffffc); 6037568cb4eSPaul Mackerras break; 6047568cb4eSPaul Mackerras 60521c4ff80SBenjamin Herrenschmidt case R_PPC64_REL64: 60621c4ff80SBenjamin Herrenschmidt /* 64 bits relative (used by features fixups) */ 60721c4ff80SBenjamin Herrenschmidt *location = value - (unsigned long)location; 60821c4ff80SBenjamin Herrenschmidt break; 60921c4ff80SBenjamin Herrenschmidt 610d247da0aSRusty Russell case R_PPC64_TOCSAVE: 611d247da0aSRusty Russell /* 612d247da0aSRusty Russell * Marker reloc indicates we don't have to save r2. 613d247da0aSRusty Russell * That would only save us one instruction, so ignore 614d247da0aSRusty Russell * it. 615d247da0aSRusty Russell */ 616d247da0aSRusty Russell break; 617d247da0aSRusty Russell 618a61674bdSUlrich Weigand case R_PPC64_ENTRY: 619a61674bdSUlrich Weigand /* 620a61674bdSUlrich Weigand * Optimize ELFv2 large code model entry point if 621a61674bdSUlrich Weigand * the TOC is within 2GB range of current location. 622a61674bdSUlrich Weigand */ 623a61674bdSUlrich Weigand value = my_r2(sechdrs, me) - (unsigned long)location; 624a61674bdSUlrich Weigand if (value + 0x80008000 > 0xffffffff) 625a61674bdSUlrich Weigand break; 626a61674bdSUlrich Weigand /* 627a61674bdSUlrich Weigand * Check for the large code model prolog sequence: 628a61674bdSUlrich Weigand * ld r2, ...(r12) 629a61674bdSUlrich Weigand * add r2, r2, r12 630a61674bdSUlrich Weigand */ 631a61674bdSUlrich Weigand if ((((uint32_t *)location)[0] & ~0xfffc) 632a61674bdSUlrich Weigand != 0xe84c0000) 633a61674bdSUlrich Weigand break; 634a61674bdSUlrich Weigand if (((uint32_t *)location)[1] != 0x7c426214) 635a61674bdSUlrich Weigand break; 636a61674bdSUlrich Weigand /* 637a61674bdSUlrich Weigand * If found, replace it with: 638a61674bdSUlrich Weigand * addis r2, r12, (.TOC.-func)@ha 639a61674bdSUlrich Weigand * addi r2, r12, (.TOC.-func)@l 640a61674bdSUlrich Weigand */ 641a61674bdSUlrich Weigand ((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value); 642a61674bdSUlrich Weigand ((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value); 643a61674bdSUlrich Weigand break; 644a61674bdSUlrich Weigand 6450906584aSRusty Russell case R_PPC64_REL16_HA: 6460906584aSRusty Russell /* Subtract location pointer */ 6470906584aSRusty Russell value -= (unsigned long)location; 6480906584aSRusty Russell value = ((value + 0x8000) >> 16); 6490906584aSRusty Russell *((uint16_t *) location) 6500906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 6510906584aSRusty Russell | (value & 0xffff); 6520906584aSRusty Russell break; 6530906584aSRusty Russell 6540906584aSRusty Russell case R_PPC64_REL16_LO: 6550906584aSRusty Russell /* Subtract location pointer */ 6560906584aSRusty Russell value -= (unsigned long)location; 6570906584aSRusty Russell *((uint16_t *) location) 6580906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 6590906584aSRusty Russell | (value & 0xffff); 6600906584aSRusty Russell break; 6610906584aSRusty Russell 6627568cb4eSPaul Mackerras default: 663c7d1f6afSAnton Blanchard pr_err("%s: Unknown ADD relocation: %lu\n", 6647568cb4eSPaul Mackerras me->name, 6657568cb4eSPaul Mackerras (unsigned long)ELF64_R_TYPE(rela[i].r_info)); 6667568cb4eSPaul Mackerras return -ENOEXEC; 6677568cb4eSPaul Mackerras } 6687568cb4eSPaul Mackerras } 6697568cb4eSPaul Mackerras 670136cd345SMichael Ellerman return 0; 671136cd345SMichael Ellerman } 672136cd345SMichael Ellerman 673f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 674136cd345SMichael Ellerman int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs) 675136cd345SMichael Ellerman { 676136cd345SMichael Ellerman mod->arch.toc = my_r2(sechdrs, mod); 677136cd345SMichael Ellerman mod->arch.tramp = stub_for_addr(sechdrs, (unsigned long)ftrace_caller, mod); 678136cd345SMichael Ellerman 679136cd345SMichael Ellerman if (!mod->arch.tramp) 680136cd345SMichael Ellerman return -ENOENT; 681f48cb8b4SSteven Rostedt 6827568cb4eSPaul Mackerras return 0; 6837568cb4eSPaul Mackerras } 684136cd345SMichael Ellerman #endif 685