1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Kernel module help for PPC.
3 Copyright (C) 2001 Rusty Russell.
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/moduleloader.h>
11 #include <linux/elf.h>
12 #include <linux/vmalloc.h>
14 #include <linux/string.h>
15 #include <linux/kernel.h>
16 #include <linux/ftrace.h>
17 #include <linux/cache.h>
18 #include <linux/bug.h>
19 #include <linux/sort.h>
20 #include <asm/setup.h>
21 #include <asm/code-patching.h>
23 /* Count how many different relocations (different symbol, different
25 static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
27 unsigned int i, r_info, r_addend, _count_relocs;
32 for (i = 0; i < num; i++)
33 /* Only count 24-bit relocs, others don't need stubs */
34 if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
35 (r_info != ELF32_R_SYM(rela[i].r_info) ||
36 r_addend != rela[i].r_addend)) {
38 r_info = ELF32_R_SYM(rela[i].r_info);
39 r_addend = rela[i].r_addend;
42 #ifdef CONFIG_DYNAMIC_FTRACE
43 _count_relocs++; /* add one for ftrace_caller */
48 static int relacmp(const void *_x, const void *_y)
50 const Elf32_Rela *x, *y;
55 /* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
56 * make the comparison cheaper/faster. It won't affect the sorting or
57 * the counting algorithms' performance
59 if (x->r_info < y->r_info)
61 else if (x->r_info > y->r_info)
63 else if (x->r_addend < y->r_addend)
65 else if (x->r_addend > y->r_addend)
71 /* Get the potential trampolines size required of the init and
73 static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
74 const Elf32_Shdr *sechdrs,
75 const char *secstrings,
78 unsigned long ret = 0;
81 /* Everything marked ALLOC (this includes the exported
83 for (i = 1; i < hdr->e_shnum; i++) {
84 /* If it's called *.init*, and we're not init, we're
86 if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != NULL)
90 /* We don't want to look at debug sections. */
91 if (strstr(secstrings + sechdrs[i].sh_name, ".debug"))
94 if (sechdrs[i].sh_type == SHT_RELA) {
95 pr_debug("Found relocations in section %u\n", i);
96 pr_debug("Ptr: %p. Number: %u\n",
97 (void *)hdr + sechdrs[i].sh_offset,
98 sechdrs[i].sh_size / sizeof(Elf32_Rela));
100 /* Sort the relocation information based on a symbol and
101 * addend key. This is a stable O(n*log n) complexity
102 * algorithm but it will reduce the complexity of
103 * count_relocs() to linear complexity O(n)
105 sort((void *)hdr + sechdrs[i].sh_offset,
106 sechdrs[i].sh_size / sizeof(Elf32_Rela),
107 sizeof(Elf32_Rela), relacmp, NULL);
109 ret += count_relocs((void *)hdr
110 + sechdrs[i].sh_offset,
112 / sizeof(Elf32_Rela))
113 * sizeof(struct ppc_plt_entry);
120 int module_frob_arch_sections(Elf32_Ehdr *hdr,
127 /* Find .plt and .init.plt sections */
128 for (i = 0; i < hdr->e_shnum; i++) {
129 if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
130 me->arch.init_plt_section = i;
131 else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
132 me->arch.core_plt_section = i;
134 if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
135 pr_err("Module doesn't contain .plt or .init.plt sections.\n");
139 /* Override their sizes */
140 sechdrs[me->arch.core_plt_section].sh_size
141 = get_plt_size(hdr, sechdrs, secstrings, 0);
142 sechdrs[me->arch.init_plt_section].sh_size
143 = get_plt_size(hdr, sechdrs, secstrings, 1);
147 static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
149 if (entry->jump[0] != PPC_RAW_LIS(_R12, PPC_HA(val)))
151 if (entry->jump[1] != PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))
156 /* Set up a trampoline in the PLT to bounce us to the distant function */
157 static uint32_t do_plt_call(void *location,
159 const Elf32_Shdr *sechdrs,
162 struct ppc_plt_entry *entry;
164 pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
165 /* Init, or core PLT? */
166 if (within_module_core((unsigned long)location, mod))
167 entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
169 entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
171 /* Find this entry, or if that fails, the next avail. entry */
172 while (entry->jump[0]) {
173 if (entry_matches(entry, val)) return (uint32_t)entry;
177 if (patch_instruction(&entry->jump[0], ppc_inst(PPC_RAW_LIS(_R12, PPC_HA(val)))))
179 if (patch_instruction(&entry->jump[1], ppc_inst(PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))))
181 if (patch_instruction(&entry->jump[2], ppc_inst(PPC_RAW_MTCTR(_R12))))
183 if (patch_instruction(&entry->jump[3], ppc_inst(PPC_RAW_BCTR())))
186 pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
187 return (uint32_t)entry;
190 static int patch_location_16(uint32_t *loc, u16 value)
192 loc = PTR_ALIGN_DOWN(loc, sizeof(u32));
193 return patch_instruction(loc, ppc_inst((*loc & 0xffff0000) | value));
196 int apply_relocate_add(Elf32_Shdr *sechdrs,
198 unsigned int symindex,
200 struct module *module)
203 Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
208 pr_debug("Applying ADD relocate section %u to %u\n", relsec,
209 sechdrs[relsec].sh_info);
210 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
211 /* This is where to make the change */
212 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
214 /* This is the symbol it is referring to. Note that all
215 undefined symbols have been resolved. */
216 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
217 + ELF32_R_SYM(rela[i].r_info);
218 /* `Everything is relative'. */
219 value = sym->st_value + rela[i].r_addend;
221 switch (ELF32_R_TYPE(rela[i].r_info)) {
224 *(uint32_t *)location = value;
227 case R_PPC_ADDR16_LO:
228 /* Low half of the symbol */
229 if (patch_location_16(location, PPC_LO(value)))
233 case R_PPC_ADDR16_HI:
234 /* Higher half of the symbol */
235 if (patch_location_16(location, PPC_HI(value)))
239 case R_PPC_ADDR16_HA:
240 if (patch_location_16(location, PPC_HA(value)))
245 if ((int)(value - (uint32_t)location) < -0x02000000
246 || (int)(value - (uint32_t)location) >= 0x02000000) {
247 value = do_plt_call(location, value,
253 /* Only replace bits 2 through 26 */
254 pr_debug("REL24 value = %08X. location = %08X\n",
255 value, (uint32_t)location);
256 pr_debug("Location before: %08X.\n",
257 *(uint32_t *)location);
258 value = (*(uint32_t *)location & ~PPC_LI_MASK) |
259 PPC_LI(value - (uint32_t)location);
261 if (patch_instruction(location, ppc_inst(value)))
264 pr_debug("Location after: %08X.\n",
265 *(uint32_t *)location);
266 pr_debug("ie. jump to %08X+%08X = %08X\n",
267 *(uint32_t *)PPC_LI((uint32_t)location), (uint32_t)location,
268 (*(uint32_t *)PPC_LI((uint32_t)location)) + (uint32_t)location);
272 /* 32-bit relative jump. */
273 *(uint32_t *)location = value - (uint32_t)location;
277 pr_err("%s: unknown ADD relocation: %u\n",
279 ELF32_R_TYPE(rela[i].r_info));
287 #ifdef CONFIG_DYNAMIC_FTRACE
288 notrace int module_trampoline_target(struct module *mod, unsigned long addr,
289 unsigned long *target)
293 /* Find where the trampoline jumps to */
294 if (copy_inst_from_kernel_nofault(jmp, (void *)addr))
296 if (__copy_inst_from_kernel_nofault(jmp + 1, (void *)addr + 4))
298 if (__copy_inst_from_kernel_nofault(jmp + 2, (void *)addr + 8))
300 if (__copy_inst_from_kernel_nofault(jmp + 3, (void *)addr + 12))
303 /* verify that this is what we expect it to be */
304 if ((ppc_inst_val(jmp[0]) & 0xffff0000) != PPC_RAW_LIS(_R12, 0))
306 if ((ppc_inst_val(jmp[1]) & 0xffff0000) != PPC_RAW_ADDI(_R12, _R12, 0))
308 if (ppc_inst_val(jmp[2]) != PPC_RAW_MTCTR(_R12))
310 if (ppc_inst_val(jmp[3]) != PPC_RAW_BCTR())
313 addr = (ppc_inst_val(jmp[1]) & 0xffff) | ((ppc_inst_val(jmp[0]) & 0xffff) << 16);
322 int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
324 module->arch.tramp = do_plt_call(module->mem[MOD_TEXT].base,
325 (unsigned long)ftrace_caller,
327 if (!module->arch.tramp)
330 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
331 module->arch.tramp_regs = do_plt_call(module->mem[MOD_TEXT].base,
332 (unsigned long)ftrace_regs_caller,
334 if (!module->arch.tramp_regs)