xref: /openbmc/linux/arch/powerpc/kernel/module_32.c (revision 7dfbfb87)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ed981856SPaul Mackerras /*  Kernel module help for PPC.
3ed981856SPaul Mackerras     Copyright (C) 2001 Rusty Russell.
4ed981856SPaul Mackerras 
5ed981856SPaul Mackerras */
6c7d1f6afSAnton Blanchard 
7c7d1f6afSAnton Blanchard #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8c7d1f6afSAnton Blanchard 
9ed981856SPaul Mackerras #include <linux/module.h>
10ed981856SPaul Mackerras #include <linux/moduleloader.h>
11ed981856SPaul Mackerras #include <linux/elf.h>
12ed981856SPaul Mackerras #include <linux/vmalloc.h>
13ed981856SPaul Mackerras #include <linux/fs.h>
14ed981856SPaul Mackerras #include <linux/string.h>
15ed981856SPaul Mackerras #include <linux/kernel.h>
167cc45e64SSteven Rostedt #include <linux/ftrace.h>
17ed981856SPaul Mackerras #include <linux/cache.h>
1873c9ceabSJeremy Fitzhardinge #include <linux/bug.h>
19eda09fbdSEmil Medve #include <linux/sort.h>
20b88c4767SRobert Jennings #include <asm/setup.h>
2121c4ff80SBenjamin Herrenschmidt 
22ed981856SPaul Mackerras /* Count how many different relocations (different symbol, different
23ed981856SPaul Mackerras    addend) */
24ed981856SPaul Mackerras static unsigned int count_relocs(const Elf32_Rela *rela, unsigned int num)
25ed981856SPaul Mackerras {
26eda09fbdSEmil Medve 	unsigned int i, r_info, r_addend, _count_relocs;
27ed981856SPaul Mackerras 
28eda09fbdSEmil Medve 	_count_relocs = 0;
29eda09fbdSEmil Medve 	r_info = 0;
30eda09fbdSEmil Medve 	r_addend = 0;
31eda09fbdSEmil Medve 	for (i = 0; i < num; i++)
32eda09fbdSEmil Medve 		/* Only count 24-bit relocs, others don't need stubs */
33eda09fbdSEmil Medve 		if (ELF32_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
34eda09fbdSEmil Medve 		    (r_info != ELF32_R_SYM(rela[i].r_info) ||
35eda09fbdSEmil Medve 		     r_addend != rela[i].r_addend)) {
36eda09fbdSEmil Medve 			_count_relocs++;
37eda09fbdSEmil Medve 			r_info = ELF32_R_SYM(rela[i].r_info);
38eda09fbdSEmil Medve 			r_addend = rela[i].r_addend;
39ed981856SPaul Mackerras 		}
40eda09fbdSEmil Medve 
417cc45e64SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
427cc45e64SSteven Rostedt 	_count_relocs++;	/* add one for ftrace_caller */
437cc45e64SSteven Rostedt #endif
44eda09fbdSEmil Medve 	return _count_relocs;
45ed981856SPaul Mackerras }
46eda09fbdSEmil Medve 
47eda09fbdSEmil Medve static int relacmp(const void *_x, const void *_y)
48eda09fbdSEmil Medve {
49eda09fbdSEmil Medve 	const Elf32_Rela *x, *y;
50eda09fbdSEmil Medve 
51eda09fbdSEmil Medve 	y = (Elf32_Rela *)_x;
52eda09fbdSEmil Medve 	x = (Elf32_Rela *)_y;
53eda09fbdSEmil Medve 
54eda09fbdSEmil Medve 	/* Compare the entire r_info (as opposed to ELF32_R_SYM(r_info) only) to
55eda09fbdSEmil Medve 	 * make the comparison cheaper/faster. It won't affect the sorting or
56eda09fbdSEmil Medve 	 * the counting algorithms' performance
57eda09fbdSEmil Medve 	 */
58eda09fbdSEmil Medve 	if (x->r_info < y->r_info)
59eda09fbdSEmil Medve 		return -1;
60eda09fbdSEmil Medve 	else if (x->r_info > y->r_info)
61eda09fbdSEmil Medve 		return 1;
62eda09fbdSEmil Medve 	else if (x->r_addend < y->r_addend)
63eda09fbdSEmil Medve 		return -1;
64eda09fbdSEmil Medve 	else if (x->r_addend > y->r_addend)
65eda09fbdSEmil Medve 		return 1;
66eda09fbdSEmil Medve 	else
67eda09fbdSEmil Medve 		return 0;
68eda09fbdSEmil Medve }
69eda09fbdSEmil Medve 
70ed981856SPaul Mackerras /* Get the potential trampolines size required of the init and
71ed981856SPaul Mackerras    non-init sections */
72ed981856SPaul Mackerras static unsigned long get_plt_size(const Elf32_Ehdr *hdr,
73ed981856SPaul Mackerras 				  const Elf32_Shdr *sechdrs,
74ed981856SPaul Mackerras 				  const char *secstrings,
75ed981856SPaul Mackerras 				  int is_init)
76ed981856SPaul Mackerras {
77ed981856SPaul Mackerras 	unsigned long ret = 0;
78ed981856SPaul Mackerras 	unsigned i;
79ed981856SPaul Mackerras 
80ed981856SPaul Mackerras 	/* Everything marked ALLOC (this includes the exported
81ed981856SPaul Mackerras            symbols) */
82ed981856SPaul Mackerras 	for (i = 1; i < hdr->e_shnum; i++) {
83ed981856SPaul Mackerras 		/* If it's called *.init*, and we're not init, we're
84ed981856SPaul Mackerras                    not interested */
85d8731527SMathieu Malaterre 		if ((strstr(secstrings + sechdrs[i].sh_name, ".init") != NULL)
86ed981856SPaul Mackerras 		    != is_init)
87ed981856SPaul Mackerras 			continue;
88ed981856SPaul Mackerras 
89ed981856SPaul Mackerras 		/* We don't want to look at debug sections. */
90d8731527SMathieu Malaterre 		if (strstr(secstrings + sechdrs[i].sh_name, ".debug"))
91ed981856SPaul Mackerras 			continue;
92ed981856SPaul Mackerras 
93ed981856SPaul Mackerras 		if (sechdrs[i].sh_type == SHT_RELA) {
94c7d1f6afSAnton Blanchard 			pr_debug("Found relocations in section %u\n", i);
95c7d1f6afSAnton Blanchard 			pr_debug("Ptr: %p.  Number: %u\n",
96ed981856SPaul Mackerras 			       (void *)hdr + sechdrs[i].sh_offset,
97ed981856SPaul Mackerras 			       sechdrs[i].sh_size / sizeof(Elf32_Rela));
98eda09fbdSEmil Medve 
99eda09fbdSEmil Medve 			/* Sort the relocation information based on a symbol and
100eda09fbdSEmil Medve 			 * addend key. This is a stable O(n*log n) complexity
101eda09fbdSEmil Medve 			 * alogrithm but it will reduce the complexity of
102eda09fbdSEmil Medve 			 * count_relocs() to linear complexity O(n)
103eda09fbdSEmil Medve 			 */
104eda09fbdSEmil Medve 			sort((void *)hdr + sechdrs[i].sh_offset,
105eda09fbdSEmil Medve 			     sechdrs[i].sh_size / sizeof(Elf32_Rela),
106bac7ca7bSAndrey Abramov 			     sizeof(Elf32_Rela), relacmp, NULL);
107eda09fbdSEmil Medve 
108ed981856SPaul Mackerras 			ret += count_relocs((void *)hdr
109ed981856SPaul Mackerras 					     + sechdrs[i].sh_offset,
110ed981856SPaul Mackerras 					     sechdrs[i].sh_size
111ed981856SPaul Mackerras 					     / sizeof(Elf32_Rela))
112ed981856SPaul Mackerras 				* sizeof(struct ppc_plt_entry);
113ed981856SPaul Mackerras 		}
114ed981856SPaul Mackerras 	}
115ed981856SPaul Mackerras 
116ed981856SPaul Mackerras 	return ret;
117ed981856SPaul Mackerras }
118ed981856SPaul Mackerras 
119ed981856SPaul Mackerras int module_frob_arch_sections(Elf32_Ehdr *hdr,
120ed981856SPaul Mackerras 			      Elf32_Shdr *sechdrs,
121ed981856SPaul Mackerras 			      char *secstrings,
122ed981856SPaul Mackerras 			      struct module *me)
123ed981856SPaul Mackerras {
124ed981856SPaul Mackerras 	unsigned int i;
125ed981856SPaul Mackerras 
126ed981856SPaul Mackerras 	/* Find .plt and .init.plt sections */
127ed981856SPaul Mackerras 	for (i = 0; i < hdr->e_shnum; i++) {
128ed981856SPaul Mackerras 		if (strcmp(secstrings + sechdrs[i].sh_name, ".init.plt") == 0)
129ed981856SPaul Mackerras 			me->arch.init_plt_section = i;
130ed981856SPaul Mackerras 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".plt") == 0)
131ed981856SPaul Mackerras 			me->arch.core_plt_section = i;
132ed981856SPaul Mackerras 	}
133ed981856SPaul Mackerras 	if (!me->arch.core_plt_section || !me->arch.init_plt_section) {
134c7d1f6afSAnton Blanchard 		pr_err("Module doesn't contain .plt or .init.plt sections.\n");
135ed981856SPaul Mackerras 		return -ENOEXEC;
136ed981856SPaul Mackerras 	}
137ed981856SPaul Mackerras 
138ed981856SPaul Mackerras 	/* Override their sizes */
139ed981856SPaul Mackerras 	sechdrs[me->arch.core_plt_section].sh_size
140ed981856SPaul Mackerras 		= get_plt_size(hdr, sechdrs, secstrings, 0);
141ed981856SPaul Mackerras 	sechdrs[me->arch.init_plt_section].sh_size
142ed981856SPaul Mackerras 		= get_plt_size(hdr, sechdrs, secstrings, 1);
143ed981856SPaul Mackerras 	return 0;
144ed981856SPaul Mackerras }
145ed981856SPaul Mackerras 
146ed981856SPaul Mackerras static inline int entry_matches(struct ppc_plt_entry *entry, Elf32_Addr val)
147ed981856SPaul Mackerras {
14847b04699SChristophe Leroy 	if (entry->jump[0] != PPC_RAW_LIS(_R12, PPC_HA(val)))
149ed981856SPaul Mackerras 		return 0;
15047b04699SChristophe Leroy 	if (entry->jump[1] != PPC_RAW_ADDI(_R12, _R12, PPC_LO(val)))
1514eb4516eSChristophe Leroy 		return 0;
1524eb4516eSChristophe Leroy 	return 1;
153ed981856SPaul Mackerras }
154ed981856SPaul Mackerras 
155ed981856SPaul Mackerras /* Set up a trampoline in the PLT to bounce us to the distant function */
156ed981856SPaul Mackerras static uint32_t do_plt_call(void *location,
157ed981856SPaul Mackerras 			    Elf32_Addr val,
158136cd345SMichael Ellerman 			    const Elf32_Shdr *sechdrs,
159ed981856SPaul Mackerras 			    struct module *mod)
160ed981856SPaul Mackerras {
161ed981856SPaul Mackerras 	struct ppc_plt_entry *entry;
162ed981856SPaul Mackerras 
163c7d1f6afSAnton Blanchard 	pr_debug("Doing plt for call to 0x%x at 0x%x\n", val, (unsigned int)location);
164ed981856SPaul Mackerras 	/* Init, or core PLT? */
1657523e4dcSRusty Russell 	if (location >= mod->core_layout.base
1667523e4dcSRusty Russell 	    && location < mod->core_layout.base + mod->core_layout.size)
167ed981856SPaul Mackerras 		entry = (void *)sechdrs[mod->arch.core_plt_section].sh_addr;
168ed981856SPaul Mackerras 	else
169ed981856SPaul Mackerras 		entry = (void *)sechdrs[mod->arch.init_plt_section].sh_addr;
170ed981856SPaul Mackerras 
171ed981856SPaul Mackerras 	/* Find this entry, or if that fails, the next avail. entry */
172ed981856SPaul Mackerras 	while (entry->jump[0]) {
173ed981856SPaul Mackerras 		if (entry_matches(entry, val)) return (uint32_t)entry;
174ed981856SPaul Mackerras 		entry++;
175ed981856SPaul Mackerras 	}
176ed981856SPaul Mackerras 
17747b04699SChristophe Leroy 	entry->jump[0] = PPC_RAW_LIS(_R12, PPC_HA(val));
17847b04699SChristophe Leroy 	entry->jump[1] = PPC_RAW_ADDI(_R12, _R12, PPC_LO(val));
17947b04699SChristophe Leroy 	entry->jump[2] = PPC_RAW_MTCTR(_R12);
18047b04699SChristophe Leroy 	entry->jump[3] = PPC_RAW_BCTR();
181ed981856SPaul Mackerras 
182c7d1f6afSAnton Blanchard 	pr_debug("Initialized plt for 0x%x at %p\n", val, entry);
183ed981856SPaul Mackerras 	return (uint32_t)entry;
184ed981856SPaul Mackerras }
185ed981856SPaul Mackerras 
186ed981856SPaul Mackerras int apply_relocate_add(Elf32_Shdr *sechdrs,
187ed981856SPaul Mackerras 		       const char *strtab,
188ed981856SPaul Mackerras 		       unsigned int symindex,
189ed981856SPaul Mackerras 		       unsigned int relsec,
190ed981856SPaul Mackerras 		       struct module *module)
191ed981856SPaul Mackerras {
192ed981856SPaul Mackerras 	unsigned int i;
193ed981856SPaul Mackerras 	Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
194ed981856SPaul Mackerras 	Elf32_Sym *sym;
195ed981856SPaul Mackerras 	uint32_t *location;
196ed981856SPaul Mackerras 	uint32_t value;
197ed981856SPaul Mackerras 
198c7d1f6afSAnton Blanchard 	pr_debug("Applying ADD relocate section %u to %u\n", relsec,
199ed981856SPaul Mackerras 	       sechdrs[relsec].sh_info);
200ed981856SPaul Mackerras 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
201ed981856SPaul Mackerras 		/* This is where to make the change */
202ed981856SPaul Mackerras 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
203ed981856SPaul Mackerras 			+ rela[i].r_offset;
204ed981856SPaul Mackerras 		/* This is the symbol it is referring to.  Note that all
205ed981856SPaul Mackerras 		   undefined symbols have been resolved.  */
206ed981856SPaul Mackerras 		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
207ed981856SPaul Mackerras 			+ ELF32_R_SYM(rela[i].r_info);
208ed981856SPaul Mackerras 		/* `Everything is relative'. */
209ed981856SPaul Mackerras 		value = sym->st_value + rela[i].r_addend;
210ed981856SPaul Mackerras 
211ed981856SPaul Mackerras 		switch (ELF32_R_TYPE(rela[i].r_info)) {
212ed981856SPaul Mackerras 		case R_PPC_ADDR32:
213ed981856SPaul Mackerras 			/* Simply set it */
214ed981856SPaul Mackerras 			*(uint32_t *)location = value;
215ed981856SPaul Mackerras 			break;
216ed981856SPaul Mackerras 
217ed981856SPaul Mackerras 		case R_PPC_ADDR16_LO:
218ed981856SPaul Mackerras 			/* Low half of the symbol */
219ed981856SPaul Mackerras 			*(uint16_t *)location = value;
220ed981856SPaul Mackerras 			break;
221ed981856SPaul Mackerras 
2229a3d6458SSimon Vallet 		case R_PPC_ADDR16_HI:
2239a3d6458SSimon Vallet 			/* Higher half of the symbol */
2249a3d6458SSimon Vallet 			*(uint16_t *)location = (value >> 16);
2259a3d6458SSimon Vallet 			break;
2269a3d6458SSimon Vallet 
227ed981856SPaul Mackerras 		case R_PPC_ADDR16_HA:
228ed981856SPaul Mackerras 			/* Sign-adjusted lower 16 bits: PPC ELF ABI says:
229ed981856SPaul Mackerras 			   (((x >> 16) + ((x & 0x8000) ? 1 : 0))) & 0xFFFF.
230ed981856SPaul Mackerras 			   This is the same, only sane.
231ed981856SPaul Mackerras 			 */
232ed981856SPaul Mackerras 			*(uint16_t *)location = (value + 0x8000) >> 16;
233ed981856SPaul Mackerras 			break;
234ed981856SPaul Mackerras 
235ed981856SPaul Mackerras 		case R_PPC_REL24:
236ed981856SPaul Mackerras 			if ((int)(value - (uint32_t)location) < -0x02000000
237ed981856SPaul Mackerras 			    || (int)(value - (uint32_t)location) >= 0x02000000)
238ed981856SPaul Mackerras 				value = do_plt_call(location, value,
239ed981856SPaul Mackerras 						    sechdrs, module);
240ed981856SPaul Mackerras 
241ed981856SPaul Mackerras 			/* Only replace bits 2 through 26 */
242c7d1f6afSAnton Blanchard 			pr_debug("REL24 value = %08X. location = %08X\n",
243ed981856SPaul Mackerras 			       value, (uint32_t)location);
244c7d1f6afSAnton Blanchard 			pr_debug("Location before: %08X.\n",
245ed981856SPaul Mackerras 			       *(uint32_t *)location);
246ed981856SPaul Mackerras 			*(uint32_t *)location
247ed981856SPaul Mackerras 				= (*(uint32_t *)location & ~0x03fffffc)
248ed981856SPaul Mackerras 				| ((value - (uint32_t)location)
249ed981856SPaul Mackerras 				   & 0x03fffffc);
250c7d1f6afSAnton Blanchard 			pr_debug("Location after: %08X.\n",
251ed981856SPaul Mackerras 			       *(uint32_t *)location);
252c7d1f6afSAnton Blanchard 			pr_debug("ie. jump to %08X+%08X = %08X\n",
253ed981856SPaul Mackerras 			       *(uint32_t *)location & 0x03fffffc,
254ed981856SPaul Mackerras 			       (uint32_t)location,
255ed981856SPaul Mackerras 			       (*(uint32_t *)location & 0x03fffffc)
256ed981856SPaul Mackerras 			       + (uint32_t)location);
257ed981856SPaul Mackerras 			break;
258ed981856SPaul Mackerras 
259ed981856SPaul Mackerras 		case R_PPC_REL32:
260ed981856SPaul Mackerras 			/* 32-bit relative jump. */
261ed981856SPaul Mackerras 			*(uint32_t *)location = value - (uint32_t)location;
262ed981856SPaul Mackerras 			break;
263ed981856SPaul Mackerras 
264ed981856SPaul Mackerras 		default:
265c7d1f6afSAnton Blanchard 			pr_err("%s: unknown ADD relocation: %u\n",
266ed981856SPaul Mackerras 			       module->name,
267ed981856SPaul Mackerras 			       ELF32_R_TYPE(rela[i].r_info));
268ed981856SPaul Mackerras 			return -ENOEXEC;
269ed981856SPaul Mackerras 		}
270ed981856SPaul Mackerras 	}
271136cd345SMichael Ellerman 
272ed981856SPaul Mackerras 	return 0;
273ed981856SPaul Mackerras }
274136cd345SMichael Ellerman 
275136cd345SMichael Ellerman #ifdef CONFIG_DYNAMIC_FTRACE
276c93d4f6eSChristophe Leroy int module_trampoline_target(struct module *mod, unsigned long addr,
277c93d4f6eSChristophe Leroy 			     unsigned long *target)
278c93d4f6eSChristophe Leroy {
279c93d4f6eSChristophe Leroy 	unsigned int jmp[4];
280c93d4f6eSChristophe Leroy 
281c93d4f6eSChristophe Leroy 	/* Find where the trampoline jumps to */
282c93d4f6eSChristophe Leroy 	if (copy_from_kernel_nofault(jmp, (void *)addr, sizeof(jmp)))
283c93d4f6eSChristophe Leroy 		return -EFAULT;
284c93d4f6eSChristophe Leroy 
285c93d4f6eSChristophe Leroy 	/* verify that this is what we expect it to be */
286c93d4f6eSChristophe Leroy 	if ((jmp[0] & 0xffff0000) != PPC_RAW_LIS(_R12, 0) ||
287c93d4f6eSChristophe Leroy 	    (jmp[1] & 0xffff0000) != PPC_RAW_ADDI(_R12, _R12, 0) ||
288c93d4f6eSChristophe Leroy 	    jmp[2] != PPC_RAW_MTCTR(_R12) ||
289c93d4f6eSChristophe Leroy 	    jmp[3] != PPC_RAW_BCTR())
290c93d4f6eSChristophe Leroy 		return -EINVAL;
291c93d4f6eSChristophe Leroy 
292c93d4f6eSChristophe Leroy 	addr = (jmp[1] & 0xffff) | ((jmp[0] & 0xffff) << 16);
293c93d4f6eSChristophe Leroy 	if (addr & 0x8000)
294c93d4f6eSChristophe Leroy 		addr -= 0x10000;
295c93d4f6eSChristophe Leroy 
296c93d4f6eSChristophe Leroy 	*target = addr;
297c93d4f6eSChristophe Leroy 
298c93d4f6eSChristophe Leroy 	return 0;
299c93d4f6eSChristophe Leroy }
300c93d4f6eSChristophe Leroy 
301136cd345SMichael Ellerman int module_finalize_ftrace(struct module *module, const Elf_Shdr *sechdrs)
302136cd345SMichael Ellerman {
303136cd345SMichael Ellerman 	module->arch.tramp = do_plt_call(module->core_layout.base,
304136cd345SMichael Ellerman 					 (unsigned long)ftrace_caller,
305136cd345SMichael Ellerman 					 sechdrs, module);
306136cd345SMichael Ellerman 	if (!module->arch.tramp)
307136cd345SMichael Ellerman 		return -ENOENT;
308136cd345SMichael Ellerman 
309*7dfbfb87SChristophe Leroy #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
310*7dfbfb87SChristophe Leroy 	module->arch.tramp_regs = do_plt_call(module->core_layout.base,
311*7dfbfb87SChristophe Leroy 					      (unsigned long)ftrace_regs_caller,
312*7dfbfb87SChristophe Leroy 					      sechdrs, module);
313*7dfbfb87SChristophe Leroy 	if (!module->arch.tramp_regs)
314*7dfbfb87SChristophe Leroy 		return -ENOENT;
315*7dfbfb87SChristophe Leroy #endif
316*7dfbfb87SChristophe Leroy 
317136cd345SMichael Ellerman 	return 0;
318136cd345SMichael Ellerman }
319136cd345SMichael Ellerman #endif
320