Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*  Kernel module help for PPC64.
0003     Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
0004 
0005 */
0006 
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008 
0009 #include <linux/module.h>
0010 #include <linux/elf.h>
0011 #include <linux/moduleloader.h>
0012 #include <linux/err.h>
0013 #include <linux/vmalloc.h>
0014 #include <linux/ftrace.h>
0015 #include <linux/bug.h>
0016 #include <linux/uaccess.h>
0017 #include <linux/kernel.h>
0018 #include <asm/module.h>
0019 #include <asm/firmware.h>
0020 #include <asm/code-patching.h>
0021 #include <linux/sort.h>
0022 #include <asm/setup.h>
0023 #include <asm/sections.h>
0024 #include <asm/inst.h>
0025 
0026 /* FIXME: We don't do .init separately.  To do this, we'd need to have
0027    a separate r2 value in the init and core section, and stub between
0028    them, too.
0029 
0030    Using a magic allocator which places modules within 32MB solves
0031    this, and makes other things simpler.  Anton?
0032    --RR.  */
0033 
0034 #ifdef CONFIG_PPC64_ELF_ABI_V2
0035 
0036 static func_desc_t func_desc(unsigned long addr)
0037 {
0038     func_desc_t desc = {
0039         .addr = addr,
0040     };
0041 
0042     return desc;
0043 }
0044 
0045 /* PowerPC64 specific values for the Elf64_Sym st_other field.  */
0046 #define STO_PPC64_LOCAL_BIT 5
0047 #define STO_PPC64_LOCAL_MASK    (7 << STO_PPC64_LOCAL_BIT)
0048 #define PPC64_LOCAL_ENTRY_OFFSET(other)                 \
0049  (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
0050 
0051 static unsigned int local_entry_offset(const Elf64_Sym *sym)
0052 {
0053     /* sym->st_other indicates offset to local entry point
0054      * (otherwise it will assume r12 is the address of the start
0055      * of function and try to derive r2 from it). */
0056     return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
0057 }
0058 #else
0059 
0060 static func_desc_t func_desc(unsigned long addr)
0061 {
0062     return *(struct func_desc *)addr;
0063 }
0064 static unsigned int local_entry_offset(const Elf64_Sym *sym)
0065 {
0066     return 0;
0067 }
0068 
0069 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
0070 {
0071     if (ptr < (void *)mod->arch.start_opd ||
0072             ptr >= (void *)mod->arch.end_opd)
0073         return ptr;
0074 
0075     return dereference_function_descriptor(ptr);
0076 }
0077 #endif
0078 
0079 static unsigned long func_addr(unsigned long addr)
0080 {
0081     return func_desc(addr).addr;
0082 }
0083 
0084 static unsigned long stub_func_addr(func_desc_t func)
0085 {
0086     return func.addr;
0087 }
0088 
0089 #define STUB_MAGIC 0x73747562 /* stub */
0090 
0091 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
0092    the kernel itself).  But on PPC64, these need to be used for every
0093    jump, actually, to reset r2 (TOC+0x8000). */
0094 struct ppc64_stub_entry
0095 {
0096     /* 28 byte jump instruction sequence (7 instructions). We only
0097      * need 6 instructions on ABIv2 but we always allocate 7 so
0098      * so we don't have to modify the trampoline load instruction. */
0099     u32 jump[7];
0100     /* Used by ftrace to identify stubs */
0101     u32 magic;
0102     /* Data for the above code */
0103     func_desc_t funcdata;
0104 };
0105 
0106 /*
0107  * PPC64 uses 24 bit jumps, but we need to jump into other modules or
0108  * the kernel which may be further.  So we jump to a stub.
0109  *
0110  * For ELFv1 we need to use this to set up the new r2 value (aka TOC
0111  * pointer).  For ELFv2 it's the callee's responsibility to set up the
0112  * new r2, but for both we need to save the old r2.
0113  *
0114  * We could simply patch the new r2 value and function pointer into
0115  * the stub, but it's significantly shorter to put these values at the
0116  * end of the stub code, and patch the stub address (32-bits relative
0117  * to the TOC ptr, r2) into the stub.
0118  */
0119 static u32 ppc64_stub_insns[] = {
0120     PPC_RAW_ADDIS(_R11, _R2, 0),
0121     PPC_RAW_ADDI(_R11, _R11, 0),
0122     /* Save current r2 value in magic place on the stack. */
0123     PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET),
0124     PPC_RAW_LD(_R12, _R11, 32),
0125 #ifdef CONFIG_PPC64_ELF_ABI_V1
0126     /* Set up new r2 from function descriptor */
0127     PPC_RAW_LD(_R2, _R11, 40),
0128 #endif
0129     PPC_RAW_MTCTR(_R12),
0130     PPC_RAW_BCTR(),
0131 };
0132 
0133 /* Count how many different 24-bit relocations (different symbol,
0134    different addend) */
0135 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
0136 {
0137     unsigned int i, r_info, r_addend, _count_relocs;
0138 
0139     /* FIXME: Only count external ones --RR */
0140     _count_relocs = 0;
0141     r_info = 0;
0142     r_addend = 0;
0143     for (i = 0; i < num; i++)
0144         /* Only count 24-bit relocs, others don't need stubs */
0145         if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
0146             (r_info != ELF64_R_SYM(rela[i].r_info) ||
0147              r_addend != rela[i].r_addend)) {
0148             _count_relocs++;
0149             r_info = ELF64_R_SYM(rela[i].r_info);
0150             r_addend = rela[i].r_addend;
0151         }
0152 
0153     return _count_relocs;
0154 }
0155 
0156 static int relacmp(const void *_x, const void *_y)
0157 {
0158     const Elf64_Rela *x, *y;
0159 
0160     y = (Elf64_Rela *)_x;
0161     x = (Elf64_Rela *)_y;
0162 
0163     /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
0164      * make the comparison cheaper/faster. It won't affect the sorting or
0165      * the counting algorithms' performance
0166      */
0167     if (x->r_info < y->r_info)
0168         return -1;
0169     else if (x->r_info > y->r_info)
0170         return 1;
0171     else if (x->r_addend < y->r_addend)
0172         return -1;
0173     else if (x->r_addend > y->r_addend)
0174         return 1;
0175     else
0176         return 0;
0177 }
0178 
0179 /* Get size of potential trampolines required. */
0180 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
0181                     const Elf64_Shdr *sechdrs)
0182 {
0183     /* One extra reloc so it's always 0-addr terminated */
0184     unsigned long relocs = 1;
0185     unsigned i;
0186 
0187     /* Every relocated section... */
0188     for (i = 1; i < hdr->e_shnum; i++) {
0189         if (sechdrs[i].sh_type == SHT_RELA) {
0190             pr_debug("Found relocations in section %u\n", i);
0191             pr_debug("Ptr: %p.  Number: %Lu\n",
0192                    (void *)sechdrs[i].sh_addr,
0193                    sechdrs[i].sh_size / sizeof(Elf64_Rela));
0194 
0195             /* Sort the relocation information based on a symbol and
0196              * addend key. This is a stable O(n*log n) complexity
0197              * algorithm but it will reduce the complexity of
0198              * count_relocs() to linear complexity O(n)
0199              */
0200             sort((void *)sechdrs[i].sh_addr,
0201                  sechdrs[i].sh_size / sizeof(Elf64_Rela),
0202                  sizeof(Elf64_Rela), relacmp, NULL);
0203 
0204             relocs += count_relocs((void *)sechdrs[i].sh_addr,
0205                            sechdrs[i].sh_size
0206                            / sizeof(Elf64_Rela));
0207         }
0208     }
0209 
0210 #ifdef CONFIG_DYNAMIC_FTRACE
0211     /* make the trampoline to the ftrace_caller */
0212     relocs++;
0213 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
0214     /* an additional one for ftrace_regs_caller */
0215     relocs++;
0216 #endif
0217 #endif
0218 
0219     pr_debug("Looks like a total of %lu stubs, max\n", relocs);
0220     return relocs * sizeof(struct ppc64_stub_entry);
0221 }
0222 
0223 /* Still needed for ELFv2, for .TOC. */
0224 static void dedotify_versions(struct modversion_info *vers,
0225                   unsigned long size)
0226 {
0227     struct modversion_info *end;
0228 
0229     for (end = (void *)vers + size; vers < end; vers++)
0230         if (vers->name[0] == '.') {
0231             memmove(vers->name, vers->name+1, strlen(vers->name));
0232         }
0233 }
0234 
0235 /*
0236  * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
0237  * seem to be defined (value set later).
0238  */
0239 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
0240 {
0241     unsigned int i;
0242 
0243     for (i = 1; i < numsyms; i++) {
0244         if (syms[i].st_shndx == SHN_UNDEF) {
0245             char *name = strtab + syms[i].st_name;
0246             if (name[0] == '.') {
0247                 if (strcmp(name+1, "TOC.") == 0)
0248                     syms[i].st_shndx = SHN_ABS;
0249                 syms[i].st_name++;
0250             }
0251         }
0252     }
0253 }
0254 
0255 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
0256                    const char *strtab,
0257                    unsigned int symindex)
0258 {
0259     unsigned int i, numsyms;
0260     Elf64_Sym *syms;
0261 
0262     syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
0263     numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
0264 
0265     for (i = 1; i < numsyms; i++) {
0266         if (syms[i].st_shndx == SHN_ABS
0267             && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
0268             return &syms[i];
0269     }
0270     return NULL;
0271 }
0272 
0273 bool module_init_section(const char *name)
0274 {
0275     /* We don't handle .init for the moment: always return false. */
0276     return false;
0277 }
0278 
0279 int module_frob_arch_sections(Elf64_Ehdr *hdr,
0280                   Elf64_Shdr *sechdrs,
0281                   char *secstrings,
0282                   struct module *me)
0283 {
0284     unsigned int i;
0285 
0286     /* Find .toc and .stubs sections, symtab and strtab */
0287     for (i = 1; i < hdr->e_shnum; i++) {
0288         if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
0289             me->arch.stubs_section = i;
0290         else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) {
0291             me->arch.toc_section = i;
0292             if (sechdrs[i].sh_addralign < 8)
0293                 sechdrs[i].sh_addralign = 8;
0294         }
0295         else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
0296             dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
0297                       sechdrs[i].sh_size);
0298 
0299         if (sechdrs[i].sh_type == SHT_SYMTAB)
0300             dedotify((void *)hdr + sechdrs[i].sh_offset,
0301                  sechdrs[i].sh_size / sizeof(Elf64_Sym),
0302                  (void *)hdr
0303                  + sechdrs[sechdrs[i].sh_link].sh_offset);
0304     }
0305 
0306     if (!me->arch.stubs_section) {
0307         pr_err("%s: doesn't contain .stubs.\n", me->name);
0308         return -ENOEXEC;
0309     }
0310 
0311     /* If we don't have a .toc, just use .stubs.  We need to set r2
0312        to some reasonable value in case the module calls out to
0313        other functions via a stub, or if a function pointer escapes
0314        the module by some means.  */
0315     if (!me->arch.toc_section)
0316         me->arch.toc_section = me->arch.stubs_section;
0317 
0318     /* Override the stubs size */
0319     sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
0320     return 0;
0321 }
0322 
0323 #ifdef CONFIG_MPROFILE_KERNEL
0324 
0325 static u32 stub_insns[] = {
0326     PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)),
0327     PPC_RAW_ADDIS(_R12, _R12, 0),
0328     PPC_RAW_ADDI(_R12, _R12, 0),
0329     PPC_RAW_MTCTR(_R12),
0330     PPC_RAW_BCTR(),
0331 };
0332 
0333 /*
0334  * For mprofile-kernel we use a special stub for ftrace_caller() because we
0335  * can't rely on r2 containing this module's TOC when we enter the stub.
0336  *
0337  * That can happen if the function calling us didn't need to use the toc. In
0338  * that case it won't have setup r2, and the r2 value will be either the
0339  * kernel's toc, or possibly another modules toc.
0340  *
0341  * To deal with that this stub uses the kernel toc, which is always accessible
0342  * via the paca (in r13). The target (ftrace_caller()) is responsible for
0343  * saving and restoring the toc before returning.
0344  */
0345 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
0346                     unsigned long addr,
0347                     struct module *me)
0348 {
0349     long reladdr;
0350 
0351     memcpy(entry->jump, stub_insns, sizeof(stub_insns));
0352 
0353     /* Stub uses address relative to kernel toc (from the paca) */
0354     reladdr = addr - kernel_toc_addr();
0355     if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
0356         pr_err("%s: Address of %ps out of range of kernel_toc.\n",
0357                             me->name, (void *)addr);
0358         return 0;
0359     }
0360 
0361     entry->jump[1] |= PPC_HA(reladdr);
0362     entry->jump[2] |= PPC_LO(reladdr);
0363 
0364     /* Even though we don't use funcdata in the stub, it's needed elsewhere. */
0365     entry->funcdata = func_desc(addr);
0366     entry->magic = STUB_MAGIC;
0367 
0368     return 1;
0369 }
0370 
0371 static bool is_mprofile_ftrace_call(const char *name)
0372 {
0373     if (!strcmp("_mcount", name))
0374         return true;
0375 #ifdef CONFIG_DYNAMIC_FTRACE
0376     if (!strcmp("ftrace_caller", name))
0377         return true;
0378 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
0379     if (!strcmp("ftrace_regs_caller", name))
0380         return true;
0381 #endif
0382 #endif
0383 
0384     return false;
0385 }
0386 #else
0387 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
0388                     unsigned long addr,
0389                     struct module *me)
0390 {
0391     return 0;
0392 }
0393 
0394 static bool is_mprofile_ftrace_call(const char *name)
0395 {
0396     return false;
0397 }
0398 #endif
0399 
0400 /*
0401  * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the
0402  * value maximum span in an instruction which uses a signed offset). Round down
0403  * to a 256 byte boundary for the odd case where we are setting up r2 without a
0404  * .toc section.
0405  */
0406 static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me)
0407 {
0408     return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000;
0409 }
0410 
0411 /* Patch stub to reference function and correct r2 value. */
0412 static inline int create_stub(const Elf64_Shdr *sechdrs,
0413                   struct ppc64_stub_entry *entry,
0414                   unsigned long addr,
0415                   struct module *me,
0416                   const char *name)
0417 {
0418     long reladdr;
0419     func_desc_t desc;
0420     int i;
0421 
0422     if (is_mprofile_ftrace_call(name))
0423         return create_ftrace_stub(entry, addr, me);
0424 
0425     for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) {
0426         if (patch_instruction(&entry->jump[i],
0427                       ppc_inst(ppc64_stub_insns[i])))
0428             return 0;
0429     }
0430 
0431     /* Stub uses address relative to r2. */
0432     reladdr = (unsigned long)entry - my_r2(sechdrs, me);
0433     if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
0434         pr_err("%s: Address %p of stub out of range of %p.\n",
0435                me->name, (void *)reladdr, (void *)my_r2);
0436         return 0;
0437     }
0438     pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
0439 
0440     if (patch_instruction(&entry->jump[0],
0441                   ppc_inst(entry->jump[0] | PPC_HA(reladdr))))
0442         return 0;
0443 
0444     if (patch_instruction(&entry->jump[1],
0445               ppc_inst(entry->jump[1] | PPC_LO(reladdr))))
0446         return 0;
0447 
0448     // func_desc_t is 8 bytes if ABIv2, else 16 bytes
0449     desc = func_desc(addr);
0450     for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
0451         if (patch_instruction(((u32 *)&entry->funcdata) + i,
0452                       ppc_inst(((u32 *)(&desc))[i])))
0453             return 0;
0454     }
0455 
0456     if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC)))
0457         return 0;
0458 
0459     return 1;
0460 }
0461 
0462 /* Create stub to jump to function described in this OPD/ptr: we need the
0463    stub to set up the TOC ptr (r2) for the function. */
0464 static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
0465                    unsigned long addr,
0466                    struct module *me,
0467                    const char *name)
0468 {
0469     struct ppc64_stub_entry *stubs;
0470     unsigned int i, num_stubs;
0471 
0472     num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
0473 
0474     /* Find this stub, or if that fails, the next avail. entry */
0475     stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
0476     for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
0477         if (WARN_ON(i >= num_stubs))
0478             return 0;
0479 
0480         if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
0481             return (unsigned long)&stubs[i];
0482     }
0483 
0484     if (!create_stub(sechdrs, &stubs[i], addr, me, name))
0485         return 0;
0486 
0487     return (unsigned long)&stubs[i];
0488 }
0489 
0490 /* We expect a noop next: if it is, replace it with instruction to
0491    restore r2. */
0492 static int restore_r2(const char *name, u32 *instruction, struct module *me)
0493 {
0494     u32 *prev_insn = instruction - 1;
0495 
0496     if (is_mprofile_ftrace_call(name))
0497         return 1;
0498 
0499     /*
0500      * Make sure the branch isn't a sibling call.  Sibling calls aren't
0501      * "link" branches and they don't return, so they don't need the r2
0502      * restore afterwards.
0503      */
0504     if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
0505         return 1;
0506 
0507     if (*instruction != PPC_RAW_NOP()) {
0508         pr_err("%s: Expected nop after call, got %08x at %pS\n",
0509             me->name, *instruction, instruction);
0510         return 0;
0511     }
0512 
0513     /* ld r2,R2_STACK_OFFSET(r1) */
0514     if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)))
0515         return 0;
0516 
0517     return 1;
0518 }
0519 
0520 int apply_relocate_add(Elf64_Shdr *sechdrs,
0521                const char *strtab,
0522                unsigned int symindex,
0523                unsigned int relsec,
0524                struct module *me)
0525 {
0526     unsigned int i;
0527     Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
0528     Elf64_Sym *sym;
0529     unsigned long *location;
0530     unsigned long value;
0531 
0532     pr_debug("Applying ADD relocate section %u to %u\n", relsec,
0533            sechdrs[relsec].sh_info);
0534 
0535     /* First time we're called, we can fix up .TOC. */
0536     if (!me->arch.toc_fixed) {
0537         sym = find_dot_toc(sechdrs, strtab, symindex);
0538         /* It's theoretically possible that a module doesn't want a
0539          * .TOC. so don't fail it just for that. */
0540         if (sym)
0541             sym->st_value = my_r2(sechdrs, me);
0542         me->arch.toc_fixed = true;
0543     }
0544 
0545     for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
0546         /* This is where to make the change */
0547         location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
0548             + rela[i].r_offset;
0549         /* This is the symbol it is referring to */
0550         sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
0551             + ELF64_R_SYM(rela[i].r_info);
0552 
0553         pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
0554                location, (long)ELF64_R_TYPE(rela[i].r_info),
0555                strtab + sym->st_name, (unsigned long)sym->st_value,
0556                (long)rela[i].r_addend);
0557 
0558         /* `Everything is relative'. */
0559         value = sym->st_value + rela[i].r_addend;
0560 
0561         switch (ELF64_R_TYPE(rela[i].r_info)) {
0562         case R_PPC64_ADDR32:
0563             /* Simply set it */
0564             *(u32 *)location = value;
0565             break;
0566 
0567         case R_PPC64_ADDR64:
0568             /* Simply set it */
0569             *(unsigned long *)location = value;
0570             break;
0571 
0572         case R_PPC64_TOC:
0573             *(unsigned long *)location = my_r2(sechdrs, me);
0574             break;
0575 
0576         case R_PPC64_TOC16:
0577             /* Subtract TOC pointer */
0578             value -= my_r2(sechdrs, me);
0579             if (value + 0x8000 > 0xffff) {
0580                 pr_err("%s: bad TOC16 relocation (0x%lx)\n",
0581                        me->name, value);
0582                 return -ENOEXEC;
0583             }
0584             *((uint16_t *) location)
0585                 = (*((uint16_t *) location) & ~0xffff)
0586                 | (value & 0xffff);
0587             break;
0588 
0589         case R_PPC64_TOC16_LO:
0590             /* Subtract TOC pointer */
0591             value -= my_r2(sechdrs, me);
0592             *((uint16_t *) location)
0593                 = (*((uint16_t *) location) & ~0xffff)
0594                 | (value & 0xffff);
0595             break;
0596 
0597         case R_PPC64_TOC16_DS:
0598             /* Subtract TOC pointer */
0599             value -= my_r2(sechdrs, me);
0600             if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
0601                 pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
0602                        me->name, value);
0603                 return -ENOEXEC;
0604             }
0605             *((uint16_t *) location)
0606                 = (*((uint16_t *) location) & ~0xfffc)
0607                 | (value & 0xfffc);
0608             break;
0609 
0610         case R_PPC64_TOC16_LO_DS:
0611             /* Subtract TOC pointer */
0612             value -= my_r2(sechdrs, me);
0613             if ((value & 3) != 0) {
0614                 pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
0615                        me->name, value);
0616                 return -ENOEXEC;
0617             }
0618             *((uint16_t *) location)
0619                 = (*((uint16_t *) location) & ~0xfffc)
0620                 | (value & 0xfffc);
0621             break;
0622 
0623         case R_PPC64_TOC16_HA:
0624             /* Subtract TOC pointer */
0625             value -= my_r2(sechdrs, me);
0626             value = ((value + 0x8000) >> 16);
0627             *((uint16_t *) location)
0628                 = (*((uint16_t *) location) & ~0xffff)
0629                 | (value & 0xffff);
0630             break;
0631 
0632         case R_PPC_REL24:
0633             /* FIXME: Handle weak symbols here --RR */
0634             if (sym->st_shndx == SHN_UNDEF ||
0635                 sym->st_shndx == SHN_LIVEPATCH) {
0636                 /* External: go via stub */
0637                 value = stub_for_addr(sechdrs, value, me,
0638                         strtab + sym->st_name);
0639                 if (!value)
0640                     return -ENOENT;
0641                 if (!restore_r2(strtab + sym->st_name,
0642                             (u32 *)location + 1, me))
0643                     return -ENOEXEC;
0644             } else
0645                 value += local_entry_offset(sym);
0646 
0647             /* Convert value to relative */
0648             value -= (unsigned long)location;
0649             if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
0650                 pr_err("%s: REL24 %li out of range!\n",
0651                        me->name, (long int)value);
0652                 return -ENOEXEC;
0653             }
0654 
0655             /* Only replace bits 2 through 26 */
0656             value = (*(uint32_t *)location & ~PPC_LI_MASK) | PPC_LI(value);
0657 
0658             if (patch_instruction((u32 *)location, ppc_inst(value)))
0659                 return -EFAULT;
0660 
0661             break;
0662 
0663         case R_PPC64_REL64:
0664             /* 64 bits relative (used by features fixups) */
0665             *location = value - (unsigned long)location;
0666             break;
0667 
0668         case R_PPC64_REL32:
0669             /* 32 bits relative (used by relative exception tables) */
0670             /* Convert value to relative */
0671             value -= (unsigned long)location;
0672             if (value + 0x80000000 > 0xffffffff) {
0673                 pr_err("%s: REL32 %li out of range!\n",
0674                        me->name, (long int)value);
0675                 return -ENOEXEC;
0676             }
0677             *(u32 *)location = value;
0678             break;
0679 
0680         case R_PPC64_TOCSAVE:
0681             /*
0682              * Marker reloc indicates we don't have to save r2.
0683              * That would only save us one instruction, so ignore
0684              * it.
0685              */
0686             break;
0687 
0688         case R_PPC64_ENTRY:
0689             /*
0690              * Optimize ELFv2 large code model entry point if
0691              * the TOC is within 2GB range of current location.
0692              */
0693             value = my_r2(sechdrs, me) - (unsigned long)location;
0694             if (value + 0x80008000 > 0xffffffff)
0695                 break;
0696             /*
0697              * Check for the large code model prolog sequence:
0698                  *  ld r2, ...(r12)
0699              *  add r2, r2, r12
0700              */
0701             if ((((uint32_t *)location)[0] & ~0xfffc) != PPC_RAW_LD(_R2, _R12, 0))
0702                 break;
0703             if (((uint32_t *)location)[1] != PPC_RAW_ADD(_R2, _R2, _R12))
0704                 break;
0705             /*
0706              * If found, replace it with:
0707              *  addis r2, r12, (.TOC.-func)@ha
0708              *  addi  r2,  r2, (.TOC.-func)@l
0709              */
0710             ((uint32_t *)location)[0] = PPC_RAW_ADDIS(_R2, _R12, PPC_HA(value));
0711             ((uint32_t *)location)[1] = PPC_RAW_ADDI(_R2, _R2, PPC_LO(value));
0712             break;
0713 
0714         case R_PPC64_REL16_HA:
0715             /* Subtract location pointer */
0716             value -= (unsigned long)location;
0717             value = ((value + 0x8000) >> 16);
0718             *((uint16_t *) location)
0719                 = (*((uint16_t *) location) & ~0xffff)
0720                 | (value & 0xffff);
0721             break;
0722 
0723         case R_PPC64_REL16_LO:
0724             /* Subtract location pointer */
0725             value -= (unsigned long)location;
0726             *((uint16_t *) location)
0727                 = (*((uint16_t *) location) & ~0xffff)
0728                 | (value & 0xffff);
0729             break;
0730 
0731         default:
0732             pr_err("%s: Unknown ADD relocation: %lu\n",
0733                    me->name,
0734                    (unsigned long)ELF64_R_TYPE(rela[i].r_info));
0735             return -ENOEXEC;
0736         }
0737     }
0738 
0739     return 0;
0740 }
0741 
0742 #ifdef CONFIG_DYNAMIC_FTRACE
0743 int module_trampoline_target(struct module *mod, unsigned long addr,
0744                  unsigned long *target)
0745 {
0746     struct ppc64_stub_entry *stub;
0747     func_desc_t funcdata;
0748     u32 magic;
0749 
0750     if (!within_module_core(addr, mod)) {
0751         pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
0752         return -EFAULT;
0753     }
0754 
0755     stub = (struct ppc64_stub_entry *)addr;
0756 
0757     if (copy_from_kernel_nofault(&magic, &stub->magic,
0758             sizeof(magic))) {
0759         pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
0760         return -EFAULT;
0761     }
0762 
0763     if (magic != STUB_MAGIC) {
0764         pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
0765         return -EFAULT;
0766     }
0767 
0768     if (copy_from_kernel_nofault(&funcdata, &stub->funcdata,
0769             sizeof(funcdata))) {
0770         pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
0771                 return -EFAULT;
0772     }
0773 
0774     *target = stub_func_addr(funcdata);
0775 
0776     return 0;
0777 }
0778 
0779 int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
0780 {
0781     mod->arch.tramp = stub_for_addr(sechdrs,
0782                     (unsigned long)ftrace_caller,
0783                     mod,
0784                     "ftrace_caller");
0785 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
0786     mod->arch.tramp_regs = stub_for_addr(sechdrs,
0787                     (unsigned long)ftrace_regs_caller,
0788                     mod,
0789                     "ftrace_regs_caller");
0790     if (!mod->arch.tramp_regs)
0791         return -ENOENT;
0792 #endif
0793 
0794     if (!mod->arch.tramp)
0795         return -ENOENT;
0796 
0797     return 0;
0798 }
0799 #endif