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 997568cb4eSPaul Mackerras /* Like PPC32, we need little trampolines to do > 24-bit jumps (into 1007568cb4eSPaul Mackerras the kernel itself). But on PPC64, these need to be used for every 1017568cb4eSPaul Mackerras jump, actually, to reset r2 (TOC+0x8000). */ 1027568cb4eSPaul Mackerras struct ppc64_stub_entry 1037568cb4eSPaul Mackerras { 10483775b85SAnton Blanchard /* 28 byte jump instruction sequence (7 instructions). We only 10583775b85SAnton Blanchard * need 6 instructions on ABIv2 but we always allocate 7 so 10683775b85SAnton Blanchard * so we don't have to modify the trampoline load instruction. */ 1070e60e46eSRusty Russell u32 jump[7]; 1080e60e46eSRusty Russell u32 unused; 1097568cb4eSPaul Mackerras /* Data for the above code */ 110008d7a91SRusty Russell func_desc_t funcdata; 1117568cb4eSPaul Mackerras }; 1127568cb4eSPaul Mackerras 1135c729a11SRusty Russell /* 1145c729a11SRusty Russell * PPC64 uses 24 bit jumps, but we need to jump into other modules or 1155c729a11SRusty Russell * the kernel which may be further. So we jump to a stub. 1165c729a11SRusty Russell * 1175c729a11SRusty Russell * For ELFv1 we need to use this to set up the new r2 value (aka TOC 1185c729a11SRusty Russell * pointer). For ELFv2 it's the callee's responsibility to set up the 1195c729a11SRusty Russell * new r2, but for both we need to save the old r2. 1205c729a11SRusty Russell * 1215c729a11SRusty Russell * We could simply patch the new r2 value and function pointer into 1225c729a11SRusty Russell * the stub, but it's significantly shorter to put these values at the 1235c729a11SRusty Russell * end of the stub code, and patch the stub address (32-bits relative 1245c729a11SRusty Russell * to the TOC ptr, r2) into the stub. 1255c729a11SRusty Russell */ 12683775b85SAnton Blanchard 12783775b85SAnton Blanchard static u32 ppc64_stub_insns[] = { 128b1ce369eSRusty Russell 0x3d620000, /* addis r11,r2, <high> */ 129b1ce369eSRusty Russell 0x396b0000, /* addi r11,r11, <low> */ 130fed8393eSEugene Surovegin /* Save current r2 value in magic place on the stack. */ 131d2fae548SRusty Russell 0xf8410000|R2_STACK_OFFSET, /* std r2,R2_STACK_OFFSET(r1) */ 132b1ce369eSRusty Russell 0xe98b0020, /* ld r12,32(r11) */ 1335c729a11SRusty Russell #if !defined(_CALL_ELF) || _CALL_ELF != 2 1345c729a11SRusty Russell /* Set up new r2 from function descriptor */ 135872aa779SRusty Russell 0xe84b0028, /* ld r2,40(r11) */ 1365c729a11SRusty Russell #endif 137b1ce369eSRusty Russell 0x7d8903a6, /* mtctr r12 */ 1380e60e46eSRusty Russell 0x4e800420 /* bctr */ 13983775b85SAnton Blanchard }; 14083775b85SAnton Blanchard 14183775b85SAnton Blanchard #ifdef CONFIG_DYNAMIC_FTRACE 14283775b85SAnton Blanchard 14383775b85SAnton Blanchard static u32 ppc64_stub_mask[] = { 14483775b85SAnton Blanchard 0xffff0000, 14583775b85SAnton Blanchard 0xffff0000, 14683775b85SAnton Blanchard 0xffffffff, 14783775b85SAnton Blanchard 0xffffffff, 14883775b85SAnton Blanchard #if !defined(_CALL_ELF) || _CALL_ELF != 2 14983775b85SAnton Blanchard 0xffffffff, 15083775b85SAnton Blanchard #endif 15183775b85SAnton Blanchard 0xffffffff, 15283775b85SAnton Blanchard 0xffffffff 15383775b85SAnton Blanchard }; 15483775b85SAnton Blanchard 15583775b85SAnton Blanchard bool is_module_trampoline(u32 *p) 15683775b85SAnton Blanchard { 15783775b85SAnton Blanchard unsigned int i; 15883775b85SAnton Blanchard u32 insns[ARRAY_SIZE(ppc64_stub_insns)]; 15983775b85SAnton Blanchard 16083775b85SAnton Blanchard BUILD_BUG_ON(sizeof(ppc64_stub_insns) != sizeof(ppc64_stub_mask)); 16183775b85SAnton Blanchard 16283775b85SAnton Blanchard if (probe_kernel_read(insns, p, sizeof(insns))) 16383775b85SAnton Blanchard return -EFAULT; 16483775b85SAnton Blanchard 16583775b85SAnton Blanchard for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) { 16683775b85SAnton Blanchard u32 insna = insns[i]; 16783775b85SAnton Blanchard u32 insnb = ppc64_stub_insns[i]; 16883775b85SAnton Blanchard u32 mask = ppc64_stub_mask[i]; 16983775b85SAnton Blanchard 17083775b85SAnton Blanchard if ((insna & mask) != (insnb & mask)) 17183775b85SAnton Blanchard return false; 17283775b85SAnton Blanchard } 17383775b85SAnton Blanchard 17483775b85SAnton Blanchard return true; 17583775b85SAnton Blanchard } 17683775b85SAnton Blanchard 177dd9fa162SAnton Blanchard int module_trampoline_target(struct module *mod, u32 *trampoline, 178dd9fa162SAnton Blanchard unsigned long *target) 179dd9fa162SAnton Blanchard { 180dd9fa162SAnton Blanchard u32 buf[2]; 181dd9fa162SAnton Blanchard u16 upper, lower; 182dd9fa162SAnton Blanchard long offset; 183dd9fa162SAnton Blanchard void *toc_entry; 184dd9fa162SAnton Blanchard 185dd9fa162SAnton Blanchard if (probe_kernel_read(buf, trampoline, sizeof(buf))) 186dd9fa162SAnton Blanchard return -EFAULT; 187dd9fa162SAnton Blanchard 188dd9fa162SAnton Blanchard upper = buf[0] & 0xffff; 189dd9fa162SAnton Blanchard lower = buf[1] & 0xffff; 190dd9fa162SAnton Blanchard 191dd9fa162SAnton Blanchard /* perform the addis/addi, both signed */ 192dd9fa162SAnton Blanchard offset = ((short)upper << 16) + (short)lower; 193dd9fa162SAnton Blanchard 194dd9fa162SAnton Blanchard /* 195dd9fa162SAnton Blanchard * Now get the address this trampoline jumps to. This 196dd9fa162SAnton Blanchard * is always 32 bytes into our trampoline stub. 197dd9fa162SAnton Blanchard */ 198dd9fa162SAnton Blanchard toc_entry = (void *)mod->arch.toc + offset + 32; 199dd9fa162SAnton Blanchard 200dd9fa162SAnton Blanchard if (probe_kernel_read(target, toc_entry, sizeof(*target))) 201dd9fa162SAnton Blanchard return -EFAULT; 202dd9fa162SAnton Blanchard 203dd9fa162SAnton Blanchard return 0; 204dd9fa162SAnton Blanchard } 205dd9fa162SAnton Blanchard 20683775b85SAnton Blanchard #endif 2077568cb4eSPaul Mackerras 2087568cb4eSPaul Mackerras /* Count how many different 24-bit relocations (different symbol, 2097568cb4eSPaul Mackerras different addend) */ 2107568cb4eSPaul Mackerras static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num) 2117568cb4eSPaul Mackerras { 212eda09fbdSEmil Medve unsigned int i, r_info, r_addend, _count_relocs; 2137568cb4eSPaul Mackerras 2147568cb4eSPaul Mackerras /* FIXME: Only count external ones --RR */ 215eda09fbdSEmil Medve _count_relocs = 0; 216eda09fbdSEmil Medve r_info = 0; 217eda09fbdSEmil Medve r_addend = 0; 218eda09fbdSEmil Medve for (i = 0; i < num; i++) 2197568cb4eSPaul Mackerras /* Only count 24-bit relocs, others don't need stubs */ 220eda09fbdSEmil Medve if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 && 221eda09fbdSEmil Medve (r_info != ELF64_R_SYM(rela[i].r_info) || 222eda09fbdSEmil Medve r_addend != rela[i].r_addend)) { 223eda09fbdSEmil Medve _count_relocs++; 224eda09fbdSEmil Medve r_info = ELF64_R_SYM(rela[i].r_info); 225eda09fbdSEmil Medve r_addend = rela[i].r_addend; 2267568cb4eSPaul Mackerras } 227eda09fbdSEmil Medve 228eda09fbdSEmil Medve return _count_relocs; 2297568cb4eSPaul Mackerras } 2307568cb4eSPaul Mackerras 231eda09fbdSEmil Medve static int relacmp(const void *_x, const void *_y) 232eda09fbdSEmil Medve { 233eda09fbdSEmil Medve const Elf64_Rela *x, *y; 234eda09fbdSEmil Medve 235eda09fbdSEmil Medve y = (Elf64_Rela *)_x; 236eda09fbdSEmil Medve x = (Elf64_Rela *)_y; 237eda09fbdSEmil Medve 238eda09fbdSEmil Medve /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to 239eda09fbdSEmil Medve * make the comparison cheaper/faster. It won't affect the sorting or 240eda09fbdSEmil Medve * the counting algorithms' performance 241eda09fbdSEmil Medve */ 242eda09fbdSEmil Medve if (x->r_info < y->r_info) 243eda09fbdSEmil Medve return -1; 244eda09fbdSEmil Medve else if (x->r_info > y->r_info) 245eda09fbdSEmil Medve return 1; 246eda09fbdSEmil Medve else if (x->r_addend < y->r_addend) 247eda09fbdSEmil Medve return -1; 248eda09fbdSEmil Medve else if (x->r_addend > y->r_addend) 249eda09fbdSEmil Medve return 1; 250eda09fbdSEmil Medve else 251eda09fbdSEmil Medve return 0; 252eda09fbdSEmil Medve } 253eda09fbdSEmil Medve 254eda09fbdSEmil Medve static void relaswap(void *_x, void *_y, int size) 255eda09fbdSEmil Medve { 256eda09fbdSEmil Medve uint64_t *x, *y, tmp; 257eda09fbdSEmil Medve int i; 258eda09fbdSEmil Medve 259eda09fbdSEmil Medve y = (uint64_t *)_x; 260eda09fbdSEmil Medve x = (uint64_t *)_y; 261eda09fbdSEmil Medve 262eda09fbdSEmil Medve for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) { 263eda09fbdSEmil Medve tmp = x[i]; 264eda09fbdSEmil Medve x[i] = y[i]; 265eda09fbdSEmil Medve y[i] = tmp; 266eda09fbdSEmil Medve } 267eda09fbdSEmil Medve } 268eda09fbdSEmil Medve 2697568cb4eSPaul Mackerras /* Get size of potential trampolines required. */ 2707568cb4eSPaul Mackerras static unsigned long get_stubs_size(const Elf64_Ehdr *hdr, 2717568cb4eSPaul Mackerras const Elf64_Shdr *sechdrs) 2727568cb4eSPaul Mackerras { 2737568cb4eSPaul Mackerras /* One extra reloc so it's always 0-funcaddr terminated */ 2747568cb4eSPaul Mackerras unsigned long relocs = 1; 2757568cb4eSPaul Mackerras unsigned i; 2767568cb4eSPaul Mackerras 2777568cb4eSPaul Mackerras /* Every relocated section... */ 2787568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 2797568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_RELA) { 280c7d1f6afSAnton Blanchard pr_debug("Found relocations in section %u\n", i); 281c7d1f6afSAnton Blanchard pr_debug("Ptr: %p. Number: %Lu\n", 2827568cb4eSPaul Mackerras (void *)sechdrs[i].sh_addr, 2837568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Rela)); 284eda09fbdSEmil Medve 285eda09fbdSEmil Medve /* Sort the relocation information based on a symbol and 286eda09fbdSEmil Medve * addend key. This is a stable O(n*log n) complexity 287eda09fbdSEmil Medve * alogrithm but it will reduce the complexity of 288eda09fbdSEmil Medve * count_relocs() to linear complexity O(n) 289eda09fbdSEmil Medve */ 290eda09fbdSEmil Medve sort((void *)sechdrs[i].sh_addr, 291eda09fbdSEmil Medve sechdrs[i].sh_size / sizeof(Elf64_Rela), 292eda09fbdSEmil Medve sizeof(Elf64_Rela), relacmp, relaswap); 293eda09fbdSEmil Medve 2947568cb4eSPaul Mackerras relocs += count_relocs((void *)sechdrs[i].sh_addr, 2957568cb4eSPaul Mackerras sechdrs[i].sh_size 2967568cb4eSPaul Mackerras / sizeof(Elf64_Rela)); 2977568cb4eSPaul Mackerras } 2987568cb4eSPaul Mackerras } 2997568cb4eSPaul Mackerras 300f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 301f48cb8b4SSteven Rostedt /* make the trampoline to the ftrace_caller */ 302f48cb8b4SSteven Rostedt relocs++; 303f48cb8b4SSteven Rostedt #endif 304f48cb8b4SSteven Rostedt 305c7d1f6afSAnton Blanchard pr_debug("Looks like a total of %lu stubs, max\n", relocs); 3067568cb4eSPaul Mackerras return relocs * sizeof(struct ppc64_stub_entry); 3077568cb4eSPaul Mackerras } 3087568cb4eSPaul Mackerras 3095b12c5c6SRusty Russell /* Still needed for ELFv2, for .TOC. */ 3107568cb4eSPaul Mackerras static void dedotify_versions(struct modversion_info *vers, 3117568cb4eSPaul Mackerras unsigned long size) 3127568cb4eSPaul Mackerras { 3137568cb4eSPaul Mackerras struct modversion_info *end; 3147568cb4eSPaul Mackerras 3157568cb4eSPaul Mackerras for (end = (void *)vers + size; vers < end; vers++) 316c2cbcf53SLaurent Dufour if (vers->name[0] == '.') { 3177568cb4eSPaul Mackerras memmove(vers->name, vers->name+1, strlen(vers->name)); 318c2cbcf53SLaurent Dufour #ifdef ARCH_RELOCATES_KCRCTAB 319c2cbcf53SLaurent Dufour /* The TOC symbol has no CRC computed. To avoid CRC 320c2cbcf53SLaurent Dufour * check failing, we must force it to the expected 321c2cbcf53SLaurent Dufour * value (see CRC check in module.c). 322c2cbcf53SLaurent Dufour */ 323c2cbcf53SLaurent Dufour if (!strcmp(vers->name, "TOC.")) 324c2cbcf53SLaurent Dufour vers->crc = -(unsigned long)reloc_start; 325c2cbcf53SLaurent Dufour #endif 326c2cbcf53SLaurent Dufour } 3277568cb4eSPaul Mackerras } 3287568cb4eSPaul Mackerras 329c153693dSAlan Modra /* 330c153693dSAlan Modra * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. 331c153693dSAlan Modra * seem to be defined (value set later). 332c153693dSAlan Modra */ 3337568cb4eSPaul Mackerras static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab) 3347568cb4eSPaul Mackerras { 3357568cb4eSPaul Mackerras unsigned int i; 3367568cb4eSPaul Mackerras 3377568cb4eSPaul Mackerras for (i = 1; i < numsyms; i++) { 3387568cb4eSPaul Mackerras if (syms[i].st_shndx == SHN_UNDEF) { 3397568cb4eSPaul Mackerras char *name = strtab + syms[i].st_name; 340c153693dSAlan Modra if (name[0] == '.') { 341c153693dSAlan Modra if (strcmp(name+1, "TOC.") == 0) 342c153693dSAlan Modra syms[i].st_shndx = SHN_ABS; 343*f15838e9SAndreas Schwab syms[i].st_name++; 3447568cb4eSPaul Mackerras } 3457568cb4eSPaul Mackerras } 3467568cb4eSPaul Mackerras } 347c153693dSAlan Modra } 3487568cb4eSPaul Mackerras 3494edebbeaSRusty Russell static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs, 3504edebbeaSRusty Russell const char *strtab, 3514edebbeaSRusty Russell unsigned int symindex) 3524edebbeaSRusty Russell { 3534edebbeaSRusty Russell unsigned int i, numsyms; 3544edebbeaSRusty Russell Elf64_Sym *syms; 3554edebbeaSRusty Russell 3564edebbeaSRusty Russell syms = (Elf64_Sym *)sechdrs[symindex].sh_addr; 3574edebbeaSRusty Russell numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym); 3584edebbeaSRusty Russell 3594edebbeaSRusty Russell for (i = 1; i < numsyms; i++) { 360c153693dSAlan Modra if (syms[i].st_shndx == SHN_ABS 361008d7a91SRusty Russell && strcmp(strtab + syms[i].st_name, "TOC.") == 0) 3624edebbeaSRusty Russell return &syms[i]; 3634edebbeaSRusty Russell } 3644edebbeaSRusty Russell return NULL; 3654edebbeaSRusty Russell } 3664edebbeaSRusty Russell 3677568cb4eSPaul Mackerras int module_frob_arch_sections(Elf64_Ehdr *hdr, 3687568cb4eSPaul Mackerras Elf64_Shdr *sechdrs, 3697568cb4eSPaul Mackerras char *secstrings, 3707568cb4eSPaul Mackerras struct module *me) 3717568cb4eSPaul Mackerras { 3727568cb4eSPaul Mackerras unsigned int i; 3737568cb4eSPaul Mackerras 3747568cb4eSPaul Mackerras /* Find .toc and .stubs sections, symtab and strtab */ 3757568cb4eSPaul Mackerras for (i = 1; i < hdr->e_shnum; i++) { 3767568cb4eSPaul Mackerras char *p; 3777568cb4eSPaul Mackerras if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0) 3787568cb4eSPaul Mackerras me->arch.stubs_section = i; 3797568cb4eSPaul Mackerras else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) 3807568cb4eSPaul Mackerras me->arch.toc_section = i; 3817568cb4eSPaul Mackerras else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0) 3827568cb4eSPaul Mackerras dedotify_versions((void *)hdr + sechdrs[i].sh_offset, 3837568cb4eSPaul Mackerras sechdrs[i].sh_size); 3847568cb4eSPaul Mackerras 3857568cb4eSPaul Mackerras /* We don't handle .init for the moment: rename to _init */ 3867568cb4eSPaul Mackerras while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init"))) 3877568cb4eSPaul Mackerras p[0] = '_'; 3887568cb4eSPaul Mackerras 3897568cb4eSPaul Mackerras if (sechdrs[i].sh_type == SHT_SYMTAB) 3907568cb4eSPaul Mackerras dedotify((void *)hdr + sechdrs[i].sh_offset, 3917568cb4eSPaul Mackerras sechdrs[i].sh_size / sizeof(Elf64_Sym), 3927568cb4eSPaul Mackerras (void *)hdr 3937568cb4eSPaul Mackerras + sechdrs[sechdrs[i].sh_link].sh_offset); 3947568cb4eSPaul Mackerras } 395f749edaeSAlan Modra 396f749edaeSAlan Modra if (!me->arch.stubs_section) { 397c7d1f6afSAnton Blanchard pr_err("%s: doesn't contain .stubs.\n", me->name); 3987568cb4eSPaul Mackerras return -ENOEXEC; 3997568cb4eSPaul Mackerras } 4007568cb4eSPaul Mackerras 401f749edaeSAlan Modra /* If we don't have a .toc, just use .stubs. We need to set r2 402f749edaeSAlan Modra to some reasonable value in case the module calls out to 403f749edaeSAlan Modra other functions via a stub, or if a function pointer escapes 404f749edaeSAlan Modra the module by some means. */ 405f749edaeSAlan Modra if (!me->arch.toc_section) 406f749edaeSAlan Modra me->arch.toc_section = me->arch.stubs_section; 407f749edaeSAlan Modra 4087568cb4eSPaul Mackerras /* Override the stubs size */ 4097568cb4eSPaul Mackerras sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs); 4107568cb4eSPaul Mackerras return 0; 4117568cb4eSPaul Mackerras } 4127568cb4eSPaul Mackerras 4137568cb4eSPaul Mackerras /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this 4147568cb4eSPaul Mackerras gives the value maximum span in an instruction which uses a signed 4157568cb4eSPaul Mackerras offset) */ 4167568cb4eSPaul Mackerras static inline unsigned long my_r2(Elf64_Shdr *sechdrs, struct module *me) 4177568cb4eSPaul Mackerras { 4187568cb4eSPaul Mackerras return sechdrs[me->arch.toc_section].sh_addr + 0x8000; 4197568cb4eSPaul Mackerras } 4207568cb4eSPaul Mackerras 4217568cb4eSPaul Mackerras /* Both low and high 16 bits are added as SIGNED additions, so if low 4227568cb4eSPaul Mackerras 16 bits has high bit set, high 16 bits must be adjusted. These 4237568cb4eSPaul Mackerras macros do that (stolen from binutils). */ 4247568cb4eSPaul Mackerras #define PPC_LO(v) ((v) & 0xffff) 4257568cb4eSPaul Mackerras #define PPC_HI(v) (((v) >> 16) & 0xffff) 4267568cb4eSPaul Mackerras #define PPC_HA(v) PPC_HI ((v) + 0x8000) 4277568cb4eSPaul Mackerras 4287568cb4eSPaul Mackerras /* Patch stub to reference function and correct r2 value. */ 4297568cb4eSPaul Mackerras static inline int create_stub(Elf64_Shdr *sechdrs, 4307568cb4eSPaul Mackerras struct ppc64_stub_entry *entry, 431008d7a91SRusty Russell unsigned long addr, 4327568cb4eSPaul Mackerras struct module *me) 4337568cb4eSPaul Mackerras { 4347568cb4eSPaul Mackerras long reladdr; 4357568cb4eSPaul Mackerras 43683775b85SAnton Blanchard memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns)); 4377568cb4eSPaul Mackerras 4387568cb4eSPaul Mackerras /* Stub uses address relative to r2. */ 4397568cb4eSPaul Mackerras reladdr = (unsigned long)entry - my_r2(sechdrs, me); 4407568cb4eSPaul Mackerras if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) { 441c7d1f6afSAnton Blanchard pr_err("%s: Address %p of stub out of range of %p.\n", 4427568cb4eSPaul Mackerras me->name, (void *)reladdr, (void *)my_r2); 4437568cb4eSPaul Mackerras return 0; 4447568cb4eSPaul Mackerras } 445c7d1f6afSAnton Blanchard pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr); 4467568cb4eSPaul Mackerras 4470e60e46eSRusty Russell entry->jump[0] |= PPC_HA(reladdr); 4480e60e46eSRusty Russell entry->jump[1] |= PPC_LO(reladdr); 449008d7a91SRusty Russell entry->funcdata = func_desc(addr); 4507568cb4eSPaul Mackerras return 1; 4517568cb4eSPaul Mackerras } 4527568cb4eSPaul Mackerras 453008d7a91SRusty Russell /* Create stub to jump to function described in this OPD/ptr: we need the 4547568cb4eSPaul Mackerras stub to set up the TOC ptr (r2) for the function. */ 4557568cb4eSPaul Mackerras static unsigned long stub_for_addr(Elf64_Shdr *sechdrs, 456008d7a91SRusty Russell unsigned long addr, 4577568cb4eSPaul Mackerras struct module *me) 4587568cb4eSPaul Mackerras { 4597568cb4eSPaul Mackerras struct ppc64_stub_entry *stubs; 4607568cb4eSPaul Mackerras unsigned int i, num_stubs; 4617568cb4eSPaul Mackerras 4627568cb4eSPaul Mackerras num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs); 4637568cb4eSPaul Mackerras 4647568cb4eSPaul Mackerras /* Find this stub, or if that fails, the next avail. entry */ 4657568cb4eSPaul Mackerras stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr; 466008d7a91SRusty Russell for (i = 0; stub_func_addr(stubs[i].funcdata); i++) { 4677568cb4eSPaul Mackerras BUG_ON(i >= num_stubs); 4687568cb4eSPaul Mackerras 469008d7a91SRusty Russell if (stub_func_addr(stubs[i].funcdata) == func_addr(addr)) 4707568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4717568cb4eSPaul Mackerras } 4727568cb4eSPaul Mackerras 473008d7a91SRusty Russell if (!create_stub(sechdrs, &stubs[i], addr, me)) 4747568cb4eSPaul Mackerras return 0; 4757568cb4eSPaul Mackerras 4767568cb4eSPaul Mackerras return (unsigned long)&stubs[i]; 4777568cb4eSPaul Mackerras } 4787568cb4eSPaul Mackerras 4797568cb4eSPaul Mackerras /* We expect a noop next: if it is, replace it with instruction to 4807568cb4eSPaul Mackerras restore r2. */ 4817568cb4eSPaul Mackerras static int restore_r2(u32 *instruction, struct module *me) 4827568cb4eSPaul Mackerras { 48316c57b36SKumar Gala if (*instruction != PPC_INST_NOP) { 484c7d1f6afSAnton Blanchard pr_err("%s: Expect noop after relocate, got %08x\n", 4857568cb4eSPaul Mackerras me->name, *instruction); 4867568cb4eSPaul Mackerras return 0; 4877568cb4eSPaul Mackerras } 488d2fae548SRusty Russell /* ld r2,R2_STACK_OFFSET(r1) */ 489d2fae548SRusty Russell *instruction = 0xe8410000 | R2_STACK_OFFSET; 4907568cb4eSPaul Mackerras return 1; 4917568cb4eSPaul Mackerras } 4927568cb4eSPaul Mackerras 4937568cb4eSPaul Mackerras int apply_relocate_add(Elf64_Shdr *sechdrs, 4947568cb4eSPaul Mackerras const char *strtab, 4957568cb4eSPaul Mackerras unsigned int symindex, 4967568cb4eSPaul Mackerras unsigned int relsec, 4977568cb4eSPaul Mackerras struct module *me) 4987568cb4eSPaul Mackerras { 4997568cb4eSPaul Mackerras unsigned int i; 5007568cb4eSPaul Mackerras Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr; 5017568cb4eSPaul Mackerras Elf64_Sym *sym; 5027568cb4eSPaul Mackerras unsigned long *location; 5037568cb4eSPaul Mackerras unsigned long value; 5047568cb4eSPaul Mackerras 505c7d1f6afSAnton Blanchard pr_debug("Applying ADD relocate section %u to %u\n", relsec, 5067568cb4eSPaul Mackerras sechdrs[relsec].sh_info); 5074edebbeaSRusty Russell 5084edebbeaSRusty Russell /* First time we're called, we can fix up .TOC. */ 5094edebbeaSRusty Russell if (!me->arch.toc_fixed) { 5104edebbeaSRusty Russell sym = find_dot_toc(sechdrs, strtab, symindex); 5114edebbeaSRusty Russell /* It's theoretically possible that a module doesn't want a 5124edebbeaSRusty Russell * .TOC. so don't fail it just for that. */ 5134edebbeaSRusty Russell if (sym) 5144edebbeaSRusty Russell sym->st_value = my_r2(sechdrs, me); 5154edebbeaSRusty Russell me->arch.toc_fixed = true; 5164edebbeaSRusty Russell } 5174edebbeaSRusty Russell 5187568cb4eSPaul Mackerras for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) { 5197568cb4eSPaul Mackerras /* This is where to make the change */ 5207568cb4eSPaul Mackerras location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr 5217568cb4eSPaul Mackerras + rela[i].r_offset; 5227568cb4eSPaul Mackerras /* This is the symbol it is referring to */ 5237568cb4eSPaul Mackerras sym = (Elf64_Sym *)sechdrs[symindex].sh_addr 5247568cb4eSPaul Mackerras + ELF64_R_SYM(rela[i].r_info); 5257568cb4eSPaul Mackerras 526c7d1f6afSAnton Blanchard pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n", 5277568cb4eSPaul Mackerras location, (long)ELF64_R_TYPE(rela[i].r_info), 5287568cb4eSPaul Mackerras strtab + sym->st_name, (unsigned long)sym->st_value, 5297568cb4eSPaul Mackerras (long)rela[i].r_addend); 5307568cb4eSPaul Mackerras 5317568cb4eSPaul Mackerras /* `Everything is relative'. */ 5327568cb4eSPaul Mackerras value = sym->st_value + rela[i].r_addend; 5337568cb4eSPaul Mackerras 5347568cb4eSPaul Mackerras switch (ELF64_R_TYPE(rela[i].r_info)) { 5357568cb4eSPaul Mackerras case R_PPC64_ADDR32: 5367568cb4eSPaul Mackerras /* Simply set it */ 5377568cb4eSPaul Mackerras *(u32 *)location = value; 5387568cb4eSPaul Mackerras break; 5397568cb4eSPaul Mackerras 5407568cb4eSPaul Mackerras case R_PPC64_ADDR64: 5417568cb4eSPaul Mackerras /* Simply set it */ 5427568cb4eSPaul Mackerras *(unsigned long *)location = value; 5437568cb4eSPaul Mackerras break; 5447568cb4eSPaul Mackerras 5457568cb4eSPaul Mackerras case R_PPC64_TOC: 5467568cb4eSPaul Mackerras *(unsigned long *)location = my_r2(sechdrs, me); 5477568cb4eSPaul Mackerras break; 5487568cb4eSPaul Mackerras 5497568cb4eSPaul Mackerras case R_PPC64_TOC16: 550f749edaeSAlan Modra /* Subtract TOC pointer */ 5517568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 5527568cb4eSPaul Mackerras if (value + 0x8000 > 0xffff) { 553c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16 relocation (0x%lx)\n", 5547568cb4eSPaul Mackerras me->name, value); 5557568cb4eSPaul Mackerras return -ENOEXEC; 5567568cb4eSPaul Mackerras } 5577568cb4eSPaul Mackerras *((uint16_t *) location) 5587568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xffff) 5597568cb4eSPaul Mackerras | (value & 0xffff); 5607568cb4eSPaul Mackerras break; 5617568cb4eSPaul Mackerras 5621fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO: 5631fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5641fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 5651fbe9cf2SAnton Blanchard *((uint16_t *) location) 5661fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 5671fbe9cf2SAnton Blanchard | (value & 0xffff); 5681fbe9cf2SAnton Blanchard break; 5691fbe9cf2SAnton Blanchard 5707568cb4eSPaul Mackerras case R_PPC64_TOC16_DS: 571f749edaeSAlan Modra /* Subtract TOC pointer */ 5727568cb4eSPaul Mackerras value -= my_r2(sechdrs, me); 5737568cb4eSPaul Mackerras if ((value & 3) != 0 || value + 0x8000 > 0xffff) { 574c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_DS relocation (0x%lx)\n", 5757568cb4eSPaul Mackerras me->name, value); 5767568cb4eSPaul Mackerras return -ENOEXEC; 5777568cb4eSPaul Mackerras } 5787568cb4eSPaul Mackerras *((uint16_t *) location) 5797568cb4eSPaul Mackerras = (*((uint16_t *) location) & ~0xfffc) 5807568cb4eSPaul Mackerras | (value & 0xfffc); 5817568cb4eSPaul Mackerras break; 5827568cb4eSPaul Mackerras 5831fbe9cf2SAnton Blanchard case R_PPC64_TOC16_LO_DS: 5841fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5851fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 5861fbe9cf2SAnton Blanchard if ((value & 3) != 0) { 587c7d1f6afSAnton Blanchard pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n", 5881fbe9cf2SAnton Blanchard me->name, value); 5891fbe9cf2SAnton Blanchard return -ENOEXEC; 5901fbe9cf2SAnton Blanchard } 5911fbe9cf2SAnton Blanchard *((uint16_t *) location) 5921fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xfffc) 5931fbe9cf2SAnton Blanchard | (value & 0xfffc); 5941fbe9cf2SAnton Blanchard break; 5951fbe9cf2SAnton Blanchard 5961fbe9cf2SAnton Blanchard case R_PPC64_TOC16_HA: 5971fbe9cf2SAnton Blanchard /* Subtract TOC pointer */ 5981fbe9cf2SAnton Blanchard value -= my_r2(sechdrs, me); 5991fbe9cf2SAnton Blanchard value = ((value + 0x8000) >> 16); 6001fbe9cf2SAnton Blanchard *((uint16_t *) location) 6011fbe9cf2SAnton Blanchard = (*((uint16_t *) location) & ~0xffff) 6021fbe9cf2SAnton Blanchard | (value & 0xffff); 6031fbe9cf2SAnton Blanchard break; 6041fbe9cf2SAnton Blanchard 6057568cb4eSPaul Mackerras case R_PPC_REL24: 6067568cb4eSPaul Mackerras /* FIXME: Handle weak symbols here --RR */ 6077568cb4eSPaul Mackerras if (sym->st_shndx == SHN_UNDEF) { 6087568cb4eSPaul Mackerras /* External: go via stub */ 6097568cb4eSPaul Mackerras value = stub_for_addr(sechdrs, value, me); 6107568cb4eSPaul Mackerras if (!value) 6117568cb4eSPaul Mackerras return -ENOENT; 6127568cb4eSPaul Mackerras if (!restore_r2((u32 *)location + 1, me)) 6137568cb4eSPaul Mackerras return -ENOEXEC; 614008d7a91SRusty Russell } else 615008d7a91SRusty Russell value += local_entry_offset(sym); 6167568cb4eSPaul Mackerras 6177568cb4eSPaul Mackerras /* Convert value to relative */ 6187568cb4eSPaul Mackerras value -= (unsigned long)location; 6197568cb4eSPaul Mackerras if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){ 620c7d1f6afSAnton Blanchard pr_err("%s: REL24 %li out of range!\n", 6217568cb4eSPaul Mackerras me->name, (long int)value); 6227568cb4eSPaul Mackerras return -ENOEXEC; 6237568cb4eSPaul Mackerras } 6247568cb4eSPaul Mackerras 6257568cb4eSPaul Mackerras /* Only replace bits 2 through 26 */ 6267568cb4eSPaul Mackerras *(uint32_t *)location 6277568cb4eSPaul Mackerras = (*(uint32_t *)location & ~0x03fffffc) 6287568cb4eSPaul Mackerras | (value & 0x03fffffc); 6297568cb4eSPaul Mackerras break; 6307568cb4eSPaul Mackerras 63121c4ff80SBenjamin Herrenschmidt case R_PPC64_REL64: 63221c4ff80SBenjamin Herrenschmidt /* 64 bits relative (used by features fixups) */ 63321c4ff80SBenjamin Herrenschmidt *location = value - (unsigned long)location; 63421c4ff80SBenjamin Herrenschmidt break; 63521c4ff80SBenjamin Herrenschmidt 636d247da0aSRusty Russell case R_PPC64_TOCSAVE: 637d247da0aSRusty Russell /* 638d247da0aSRusty Russell * Marker reloc indicates we don't have to save r2. 639d247da0aSRusty Russell * That would only save us one instruction, so ignore 640d247da0aSRusty Russell * it. 641d247da0aSRusty Russell */ 642d247da0aSRusty Russell break; 643d247da0aSRusty Russell 644a61674bdSUlrich Weigand case R_PPC64_ENTRY: 645a61674bdSUlrich Weigand /* 646a61674bdSUlrich Weigand * Optimize ELFv2 large code model entry point if 647a61674bdSUlrich Weigand * the TOC is within 2GB range of current location. 648a61674bdSUlrich Weigand */ 649a61674bdSUlrich Weigand value = my_r2(sechdrs, me) - (unsigned long)location; 650a61674bdSUlrich Weigand if (value + 0x80008000 > 0xffffffff) 651a61674bdSUlrich Weigand break; 652a61674bdSUlrich Weigand /* 653a61674bdSUlrich Weigand * Check for the large code model prolog sequence: 654a61674bdSUlrich Weigand * ld r2, ...(r12) 655a61674bdSUlrich Weigand * add r2, r2, r12 656a61674bdSUlrich Weigand */ 657a61674bdSUlrich Weigand if ((((uint32_t *)location)[0] & ~0xfffc) 658a61674bdSUlrich Weigand != 0xe84c0000) 659a61674bdSUlrich Weigand break; 660a61674bdSUlrich Weigand if (((uint32_t *)location)[1] != 0x7c426214) 661a61674bdSUlrich Weigand break; 662a61674bdSUlrich Weigand /* 663a61674bdSUlrich Weigand * If found, replace it with: 664a61674bdSUlrich Weigand * addis r2, r12, (.TOC.-func)@ha 665a61674bdSUlrich Weigand * addi r2, r12, (.TOC.-func)@l 666a61674bdSUlrich Weigand */ 667a61674bdSUlrich Weigand ((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value); 668a61674bdSUlrich Weigand ((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value); 669a61674bdSUlrich Weigand break; 670a61674bdSUlrich Weigand 6710906584aSRusty Russell case R_PPC64_REL16_HA: 6720906584aSRusty Russell /* Subtract location pointer */ 6730906584aSRusty Russell value -= (unsigned long)location; 6740906584aSRusty Russell value = ((value + 0x8000) >> 16); 6750906584aSRusty Russell *((uint16_t *) location) 6760906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 6770906584aSRusty Russell | (value & 0xffff); 6780906584aSRusty Russell break; 6790906584aSRusty Russell 6800906584aSRusty Russell case R_PPC64_REL16_LO: 6810906584aSRusty Russell /* Subtract location pointer */ 6820906584aSRusty Russell value -= (unsigned long)location; 6830906584aSRusty Russell *((uint16_t *) location) 6840906584aSRusty Russell = (*((uint16_t *) location) & ~0xffff) 6850906584aSRusty Russell | (value & 0xffff); 6860906584aSRusty Russell break; 6870906584aSRusty Russell 6887568cb4eSPaul Mackerras default: 689c7d1f6afSAnton Blanchard pr_err("%s: Unknown ADD relocation: %lu\n", 6907568cb4eSPaul Mackerras me->name, 6917568cb4eSPaul Mackerras (unsigned long)ELF64_R_TYPE(rela[i].r_info)); 6927568cb4eSPaul Mackerras return -ENOEXEC; 6937568cb4eSPaul Mackerras } 6947568cb4eSPaul Mackerras } 6957568cb4eSPaul Mackerras 696f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE 697f48cb8b4SSteven Rostedt me->arch.toc = my_r2(sechdrs, me); 698f48cb8b4SSteven Rostedt me->arch.tramp = stub_for_addr(sechdrs, 699f48cb8b4SSteven Rostedt (unsigned long)ftrace_caller, 700f48cb8b4SSteven Rostedt me); 701f48cb8b4SSteven Rostedt #endif 702f48cb8b4SSteven Rostedt 7037568cb4eSPaul Mackerras return 0; 7047568cb4eSPaul Mackerras } 705