0001
0002
0003
0004
0005
0006
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008
0009 #include <linux/module.h>
0010 #include <linux/moduleloader.h>
0011 #include <linux/elf.h>
0012 #include <linux/vmalloc.h>
0013 #include <linux/fs.h>
0014 #include <linux/string.h>
0015 #include <linux/kernel.h>
0016 #include <linux/ftrace.h>
0017 #include <linux/cache.h>
0018 #include <linux/bug.h>
0019 #include <linux/sort.h>
0020 #include <asm/setup.h>
0021 #include <asm/code-patching.h>
0022
0023
0024
0025 static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
0026 {
0027 unsigned int i, r_info, r_addend, _count_relocs;
0028
0029 _count_relocs = 0;
0030 r_info = 0;
0031 r_addend = 0;
0032 for (i = 0; i < num; i++)
0033
0034 if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
0035 (r_info != ELF32_R_SYM(rela[i].r_info) ||
0036 r_addend != rela[i].r_addend)) {
0037 _count_relocs++;
0038 r_info = ELF32_R_SYM(rela[i].r_info);
0039 r_addend = rela[i].r_addend;
0040 }
0041
0042 #ifdef CONFIG_DYNAMIC_FTRACE
0043 _count_relocs++;
0044 #endif
0045 return _count_relocs;
0046 }
0047
0048 static int relacmp(const void *_x, const void *_y)
0049 {
0050 const Elf32_Rela *x, *y;
0051
0052 y = (Elf32_Rela *)_x;
0053 x = (Elf32_Rela *)_y;
0054
0055
0056
0057
0058
0059 if (x->r_info < y->r_info)
0060 return -1;
0061 else if (x->r_info > y->r_info)
0062 return 1;
0063 else if (x->r_addend < y->r_addend)
0064 return -1;
0065 else if (x->r_addend > y->r_addend)
0066 return 1;
0067 else
0068 return 0;
0069 }
0070
0071
0072
0073 static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
0074 const Elf32_Shdr *sechdrs,
0075 const char *secstrings,
0076 int is_init)
0077 {
0078 unsigned long ret = 0;
0079 unsigned i;
0080
0081
0082
0083 for (i = 1; i < hdr->e_shnum; i++) {
0084
0085
0086 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != NULL)
0087 != is_init)
0088 continue;
0089
0090
0091 if (strstr(secstrings + sechdrs[i].sh_name, ".debug"))
0092 continue;
0093
0094 if (sechdrs[i].sh_type == SHT_RELA) {
0095 pr_debug("Found relocations in section %u\n", i);
0096 pr_debug("Ptr: %p. Number: %u\n",
0097 (void *)hdr + sechdrs[i].sh_offset,
0098 sechdrs[i].sh_size / sizeof(Elf32_Rela));
0099
0100
0101
0102
0103
0104
0105 sort((void *)hdr + sechdrs[i].sh_offset,
0106 sechdrs[i].sh_size / sizeof(Elf32_Rela),
0107 sizeof(Elf32_Rela), relacmp, NULL);
0108
0109 ret += count_relocs((void *)hdr
0110 + sechdrs[i].sh_offset,
0111 sechdrs[i].sh_size
0112 / sizeof(Elf32_Rela))
0113 * sizeof(struct ppc_plt_entry);
0114 }
0115 }
0116
0117 return ret;
0118 }
0119
0120 int module_frob_arch_sections(Elf32_Ehdr *hdr,
0121 Elf32_Shdr *sechdrs,
0122 char *secstrings,
0123 struct module *me)
0124 {
0125 unsigned int i;
0126
0127
0128 for (i = 0; i < hdr->e_shnum; i++) {
0129 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
0130 me->arch.init_plt_section = i;
0131 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
0132 me->arch.core_plt_section = i;
0133 }
0134 if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
0135 pr_err("Module doesn't contain .plt or .init.plt sections.\n");
0136 return -ENOEXEC;
0137 }
0138
0139
0140 sechdrs[me->arch.core_plt_section].sh_size
0141 = get_plt_size(hdr, sechdrs, secstrings, 0);
0142 sechdrs[me->arch.init_plt_section].sh_size
0143 = get_plt_size(hdr, sechdrs, secstrings, 1);
0144 return 0;
0145 }
0146
0147 static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
0148 {
0149 if (entry->jump[0] != PPC_RAW_LIS(_R12, PPC_HA(val)))
0150 return 0;
0151 if (entry->jump[1] != PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))
0152 return 0;
0153 return 1;
0154 }
0155
0156
0157 static uint32_t do_plt_call(void *location,
0158 Elf32_Addr val,
0159 const Elf32_Shdr *sechdrs,
0160 struct module *mod)
0161 {
0162 struct ppc_plt_entry *entry;
0163
0164 pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
0165
0166 if (location >= mod->core_layout.base
0167 && location < mod->core_layout.base + mod->core_layout.size)
0168 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
0169 else
0170 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
0171
0172
0173 while (entry->jump[0]) {
0174 if (entry_matches(entry, val)) return (uint32_t)entry;
0175 entry++;
0176 }
0177
0178 if (patch_instruction(&entry->jump[0], ppc_inst(PPC_RAW_LIS(_R12, PPC_HA(val)))))
0179 return 0;
0180 if (patch_instruction(&entry->jump[1], ppc_inst(PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))))
0181 return 0;
0182 if (patch_instruction(&entry->jump[2], ppc_inst(PPC_RAW_MTCTR(_R12))))
0183 return 0;
0184 if (patch_instruction(&entry->jump[3], ppc_inst(PPC_RAW_BCTR())))
0185 return 0;
0186
0187 pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
0188 return (uint32_t)entry;
0189 }
0190
0191 static int patch_location_16(uint32_t *loc, u16 value)
0192 {
0193 loc = PTR_ALIGN_DOWN(loc, sizeof(u32));
0194 return patch_instruction(loc, ppc_inst((*loc & 0xffff0000) | value));
0195 }
0196
0197 int apply_relocate_add(Elf32_Shdr *sechdrs,
0198 const char *strtab,
0199 unsigned int symindex,
0200 unsigned int relsec,
0201 struct module *module)
0202 {
0203 unsigned int i;
0204 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
0205 Elf32_Sym *sym;
0206 uint32_t *location;
0207 uint32_t value;
0208
0209 pr_debug("Applying ADD relocate section %u to %u\n", relsec,
0210 sechdrs[relsec].sh_info);
0211 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
0212
0213 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
0214 + rela[i].r_offset;
0215
0216
0217 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
0218 + ELF32_R_SYM(rela[i].r_info);
0219
0220 value = sym->st_value + rela[i].r_addend;
0221
0222 switch (ELF32_R_TYPE(rela[i].r_info)) {
0223 case R_PPC_ADDR32:
0224
0225 *(uint32_t *)location = value;
0226 break;
0227
0228 case R_PPC_ADDR16_LO:
0229
0230 if (patch_location_16(location, PPC_LO(value)))
0231 return -EFAULT;
0232 break;
0233
0234 case R_PPC_ADDR16_HI:
0235
0236 if (patch_location_16(location, PPC_HI(value)))
0237 return -EFAULT;
0238 break;
0239
0240 case R_PPC_ADDR16_HA:
0241 if (patch_location_16(location, PPC_HA(value)))
0242 return -EFAULT;
0243 break;
0244
0245 case R_PPC_REL24:
0246 if ((int)(value - (uint32_t)location) < -0x02000000
0247 || (int)(value - (uint32_t)location) >= 0x02000000) {
0248 value = do_plt_call(location, value,
0249 sechdrs, module);
0250 if (!value)
0251 return -EFAULT;
0252 }
0253
0254
0255 pr_debug("REL24 value = %08X. location = %08X\n",
0256 value, (uint32_t)location);
0257 pr_debug("Location before: %08X.\n",
0258 *(uint32_t *)location);
0259 value = (*(uint32_t *)location & ~PPC_LI_MASK) |
0260 PPC_LI(value - (uint32_t)location);
0261
0262 if (patch_instruction(location, ppc_inst(value)))
0263 return -EFAULT;
0264
0265 pr_debug("Location after: %08X.\n",
0266 *(uint32_t *)location);
0267 pr_debug("ie. jump to %08X+%08X = %08X\n",
0268 *(uint32_t *)PPC_LI((uint32_t)location), (uint32_t)location,
0269 (*(uint32_t *)PPC_LI((uint32_t)location)) + (uint32_t)location);
0270 break;
0271
0272 case R_PPC_REL32:
0273
0274 *(uint32_t *)location = value - (uint32_t)location;
0275 break;
0276
0277 default:
0278 pr_err("%s: unknown ADD relocation: %u\n",
0279 module->name,
0280 ELF32_R_TYPE(rela[i].r_info));
0281 return -ENOEXEC;
0282 }
0283 }
0284
0285 return 0;
0286 }
0287
0288 #ifdef CONFIG_DYNAMIC_FTRACE
0289 notrace int module_trampoline_target(struct module *mod, unsigned long addr,
0290 unsigned long *target)
0291 {
0292 ppc_inst_t jmp[4];
0293
0294
0295 if (copy_inst_from_kernel_nofault(jmp, (void *)addr))
0296 return -EFAULT;
0297 if (__copy_inst_from_kernel_nofault(jmp + 1, (void *)addr + 4))
0298 return -EFAULT;
0299 if (__copy_inst_from_kernel_nofault(jmp + 2, (void *)addr + 8))
0300 return -EFAULT;
0301 if (__copy_inst_from_kernel_nofault(jmp + 3, (void *)addr + 12))
0302 return -EFAULT;
0303
0304
0305 if ((ppc_inst_val(jmp[0]) & 0xffff0000) != PPC_RAW_LIS(_R12, 0))
0306 return -EINVAL;
0307 if ((ppc_inst_val(jmp[1]) & 0xffff0000) != PPC_RAW_ADDI(_R12, _R12, 0))
0308 return -EINVAL;
0309 if (ppc_inst_val(jmp[2]) != PPC_RAW_MTCTR(_R12))
0310 return -EINVAL;
0311 if (ppc_inst_val(jmp[3]) != PPC_RAW_BCTR())
0312 return -EINVAL;
0313
0314 addr = (ppc_inst_val(jmp[1]) & 0xffff) | ((ppc_inst_val(jmp[0]) & 0xffff) << 16);
0315 if (addr & 0x8000)
0316 addr -= 0x10000;
0317
0318 *target = addr;
0319
0320 return 0;
0321 }
0322
0323 int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
0324 {
0325 module->arch.tramp = do_plt_call(module->core_layout.base,
0326 (unsigned long)ftrace_caller,
0327 sechdrs, module);
0328 if (!module->arch.tramp)
0329 return -ENOENT;
0330
0331 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
0332 module->arch.tramp_regs = do_plt_call(module->core_layout.base,
0333 (unsigned long)ftrace_regs_caller,
0334 sechdrs, module);
0335 if (!module->arch.tramp_regs)
0336 return -ENOENT;
0337 #endif
0338
0339 return 0;
0340 }
0341 #endif