xref: /linux-6.15/arch/powerpc/kernel/module_64.c (revision 5633e85b)
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>
34336a7b5dSMichael Ellerman #include <asm/sections.h>
357568cb4eSPaul Mackerras 
367568cb4eSPaul Mackerras /* FIXME: We don't do .init separately.  To do this, we'd need to have
377568cb4eSPaul Mackerras    a separate r2 value in the init and core section, and stub between
387568cb4eSPaul Mackerras    them, too.
397568cb4eSPaul Mackerras 
407568cb4eSPaul Mackerras    Using a magic allocator which places modules within 32MB solves
417568cb4eSPaul Mackerras    this, and makes other things simpler.  Anton?
427568cb4eSPaul Mackerras    --RR.  */
437568cb4eSPaul Mackerras 
44f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v2
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
76008d7a91SRusty Russell 
77008d7a91SRusty Russell /* An address is address of the OPD entry, which contains address of fn. */
78008d7a91SRusty Russell typedef struct ppc64_opd_entry func_desc_t;
79008d7a91SRusty Russell 
80008d7a91SRusty Russell static func_desc_t func_desc(unsigned long addr)
81008d7a91SRusty Russell {
82008d7a91SRusty Russell 	return *(struct ppc64_opd_entry *)addr;
83008d7a91SRusty Russell }
84008d7a91SRusty Russell static unsigned long func_addr(unsigned long addr)
85008d7a91SRusty Russell {
86008d7a91SRusty Russell 	return func_desc(addr).funcaddr;
87008d7a91SRusty Russell }
88008d7a91SRusty Russell static unsigned long stub_func_addr(func_desc_t func)
89008d7a91SRusty Russell {
90008d7a91SRusty Russell 	return func.funcaddr;
91008d7a91SRusty Russell }
92008d7a91SRusty Russell static unsigned int local_entry_offset(const Elf64_Sym *sym)
93008d7a91SRusty Russell {
94008d7a91SRusty Russell 	return 0;
95008d7a91SRusty Russell }
96*5633e85bSSergey Senozhatsky 
97*5633e85bSSergey Senozhatsky void *dereference_module_function_descriptor(struct module *mod, void *ptr)
98*5633e85bSSergey Senozhatsky {
99*5633e85bSSergey Senozhatsky 	if (ptr < (void *)mod->arch.start_opd ||
100*5633e85bSSergey Senozhatsky 			ptr >= (void *)mod->arch.end_opd)
101*5633e85bSSergey Senozhatsky 		return ptr;
102*5633e85bSSergey Senozhatsky 
103*5633e85bSSergey Senozhatsky 	return dereference_function_descriptor(ptr);
104*5633e85bSSergey Senozhatsky }
105d2fae548SRusty Russell #endif
106d2fae548SRusty Russell 
107f17c4e01SMichael Ellerman #define STUB_MAGIC 0x73747562 /* stub */
108f17c4e01SMichael Ellerman 
1097568cb4eSPaul Mackerras /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
1107568cb4eSPaul Mackerras    the kernel itself).  But on PPC64, these need to be used for every
1117568cb4eSPaul Mackerras    jump, actually, to reset r2 (TOC+0x8000). */
1127568cb4eSPaul Mackerras struct ppc64_stub_entry
1137568cb4eSPaul Mackerras {
11483775b85SAnton Blanchard 	/* 28 byte jump instruction sequence (7 instructions). We only
11583775b85SAnton Blanchard 	 * need 6 instructions on ABIv2 but we always allocate 7 so
11683775b85SAnton Blanchard 	 * so we don't have to modify the trampoline load instruction. */
1170e60e46eSRusty Russell 	u32 jump[7];
118f17c4e01SMichael Ellerman 	/* Used by ftrace to identify stubs */
119f17c4e01SMichael Ellerman 	u32 magic;
1207568cb4eSPaul Mackerras 	/* Data for the above code */
121008d7a91SRusty Russell 	func_desc_t funcdata;
1227568cb4eSPaul Mackerras };
1237568cb4eSPaul Mackerras 
1245c729a11SRusty Russell /*
1255c729a11SRusty Russell  * PPC64 uses 24 bit jumps, but we need to jump into other modules or
1265c729a11SRusty Russell  * the kernel which may be further.  So we jump to a stub.
1275c729a11SRusty Russell  *
1285c729a11SRusty Russell  * For ELFv1 we need to use this to set up the new r2 value (aka TOC
1295c729a11SRusty Russell  * pointer).  For ELFv2 it's the callee's responsibility to set up the
1305c729a11SRusty Russell  * new r2, but for both we need to save the old r2.
1315c729a11SRusty Russell  *
1325c729a11SRusty Russell  * We could simply patch the new r2 value and function pointer into
1335c729a11SRusty Russell  * the stub, but it's significantly shorter to put these values at the
1345c729a11SRusty Russell  * end of the stub code, and patch the stub address (32-bits relative
1355c729a11SRusty Russell  * to the TOC ptr, r2) into the stub.
1365c729a11SRusty Russell  */
13783775b85SAnton Blanchard 
13883775b85SAnton Blanchard static u32 ppc64_stub_insns[] = {
139b1ce369eSRusty Russell 	0x3d620000,			/* addis   r11,r2, <high> */
140b1ce369eSRusty Russell 	0x396b0000,			/* addi    r11,r11, <low> */
141fed8393eSEugene Surovegin 	/* Save current r2 value in magic place on the stack. */
142d2fae548SRusty Russell 	0xf8410000|R2_STACK_OFFSET,	/* std     r2,R2_STACK_OFFSET(r1) */
143b1ce369eSRusty Russell 	0xe98b0020,			/* ld      r12,32(r11) */
144f55d9665SMichael Ellerman #ifdef PPC64_ELF_ABI_v1
1455c729a11SRusty Russell 	/* Set up new r2 from function descriptor */
146872aa779SRusty Russell 	0xe84b0028,			/* ld      r2,40(r11) */
1475c729a11SRusty Russell #endif
148b1ce369eSRusty Russell 	0x7d8903a6,			/* mtctr   r12 */
1490e60e46eSRusty Russell 	0x4e800420			/* bctr */
15083775b85SAnton Blanchard };
15183775b85SAnton Blanchard 
15283775b85SAnton Blanchard #ifdef CONFIG_DYNAMIC_FTRACE
153f17c4e01SMichael Ellerman int module_trampoline_target(struct module *mod, unsigned long addr,
154dd9fa162SAnton Blanchard 			     unsigned long *target)
155dd9fa162SAnton Blanchard {
156f17c4e01SMichael Ellerman 	struct ppc64_stub_entry *stub;
157f17c4e01SMichael Ellerman 	func_desc_t funcdata;
158f17c4e01SMichael Ellerman 	u32 magic;
159dd9fa162SAnton Blanchard 
160f17c4e01SMichael Ellerman 	if (!within_module_core(addr, mod)) {
161f17c4e01SMichael Ellerman 		pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
162dd9fa162SAnton Blanchard 		return -EFAULT;
163f17c4e01SMichael Ellerman 	}
164dd9fa162SAnton Blanchard 
165f17c4e01SMichael Ellerman 	stub = (struct ppc64_stub_entry *)addr;
166dd9fa162SAnton Blanchard 
167f17c4e01SMichael Ellerman 	if (probe_kernel_read(&magic, &stub->magic, sizeof(magic))) {
168f17c4e01SMichael Ellerman 		pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
169dd9fa162SAnton Blanchard 		return -EFAULT;
170f17c4e01SMichael Ellerman 	}
171f17c4e01SMichael Ellerman 
172f17c4e01SMichael Ellerman 	if (magic != STUB_MAGIC) {
173f17c4e01SMichael Ellerman 		pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
174f17c4e01SMichael Ellerman 		return -EFAULT;
175f17c4e01SMichael Ellerman 	}
176f17c4e01SMichael Ellerman 
177f17c4e01SMichael Ellerman 	if (probe_kernel_read(&funcdata, &stub->funcdata, sizeof(funcdata))) {
178f17c4e01SMichael Ellerman 		pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
179f17c4e01SMichael Ellerman                 return -EFAULT;
180f17c4e01SMichael Ellerman 	}
181f17c4e01SMichael Ellerman 
182f17c4e01SMichael Ellerman 	*target = stub_func_addr(funcdata);
183dd9fa162SAnton Blanchard 
184dd9fa162SAnton Blanchard 	return 0;
185dd9fa162SAnton Blanchard }
18683775b85SAnton Blanchard #endif
1877568cb4eSPaul Mackerras 
1887568cb4eSPaul Mackerras /* Count how many different 24-bit relocations (different symbol,
1897568cb4eSPaul Mackerras    different addend) */
1907568cb4eSPaul Mackerras static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
1917568cb4eSPaul Mackerras {
192eda09fbdSEmil Medve 	unsigned int i, r_info, r_addend, _count_relocs;
1937568cb4eSPaul Mackerras 
1947568cb4eSPaul Mackerras 	/* FIXME: Only count external ones --RR */
195eda09fbdSEmil Medve 	_count_relocs = 0;
196eda09fbdSEmil Medve 	r_info = 0;
197eda09fbdSEmil Medve 	r_addend = 0;
198eda09fbdSEmil Medve 	for (i = 0; i < num; i++)
1997568cb4eSPaul Mackerras 		/* Only count 24-bit relocs, others don't need stubs */
200eda09fbdSEmil Medve 		if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
201eda09fbdSEmil Medve 		    (r_info != ELF64_R_SYM(rela[i].r_info) ||
202eda09fbdSEmil Medve 		     r_addend != rela[i].r_addend)) {
203eda09fbdSEmil Medve 			_count_relocs++;
204eda09fbdSEmil Medve 			r_info = ELF64_R_SYM(rela[i].r_info);
205eda09fbdSEmil Medve 			r_addend = rela[i].r_addend;
2067568cb4eSPaul Mackerras 		}
207eda09fbdSEmil Medve 
208eda09fbdSEmil Medve 	return _count_relocs;
2097568cb4eSPaul Mackerras }
2107568cb4eSPaul Mackerras 
211eda09fbdSEmil Medve static int relacmp(const void *_x, const void *_y)
212eda09fbdSEmil Medve {
213eda09fbdSEmil Medve 	const Elf64_Rela *x, *y;
214eda09fbdSEmil Medve 
215eda09fbdSEmil Medve 	y = (Elf64_Rela *)_x;
216eda09fbdSEmil Medve 	x = (Elf64_Rela *)_y;
217eda09fbdSEmil Medve 
218eda09fbdSEmil Medve 	/* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
219eda09fbdSEmil Medve 	 * make the comparison cheaper/faster. It won't affect the sorting or
220eda09fbdSEmil Medve 	 * the counting algorithms' performance
221eda09fbdSEmil Medve 	 */
222eda09fbdSEmil Medve 	if (x->r_info < y->r_info)
223eda09fbdSEmil Medve 		return -1;
224eda09fbdSEmil Medve 	else if (x->r_info > y->r_info)
225eda09fbdSEmil Medve 		return 1;
226eda09fbdSEmil Medve 	else if (x->r_addend < y->r_addend)
227eda09fbdSEmil Medve 		return -1;
228eda09fbdSEmil Medve 	else if (x->r_addend > y->r_addend)
229eda09fbdSEmil Medve 		return 1;
230eda09fbdSEmil Medve 	else
231eda09fbdSEmil Medve 		return 0;
232eda09fbdSEmil Medve }
233eda09fbdSEmil Medve 
234eda09fbdSEmil Medve static void relaswap(void *_x, void *_y, int size)
235eda09fbdSEmil Medve {
236eda09fbdSEmil Medve 	uint64_t *x, *y, tmp;
237eda09fbdSEmil Medve 	int i;
238eda09fbdSEmil Medve 
239eda09fbdSEmil Medve 	y = (uint64_t *)_x;
240eda09fbdSEmil Medve 	x = (uint64_t *)_y;
241eda09fbdSEmil Medve 
242eda09fbdSEmil Medve 	for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) {
243eda09fbdSEmil Medve 		tmp = x[i];
244eda09fbdSEmil Medve 		x[i] = y[i];
245eda09fbdSEmil Medve 		y[i] = tmp;
246eda09fbdSEmil Medve 	}
247eda09fbdSEmil Medve }
248eda09fbdSEmil Medve 
2497568cb4eSPaul Mackerras /* Get size of potential trampolines required. */
2507568cb4eSPaul Mackerras static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
2517568cb4eSPaul Mackerras 				    const Elf64_Shdr *sechdrs)
2527568cb4eSPaul Mackerras {
2537568cb4eSPaul Mackerras 	/* One extra reloc so it's always 0-funcaddr terminated */
2547568cb4eSPaul Mackerras 	unsigned long relocs = 1;
2557568cb4eSPaul Mackerras 	unsigned i;
2567568cb4eSPaul Mackerras 
2577568cb4eSPaul Mackerras 	/* Every relocated section... */
2587568cb4eSPaul Mackerras 	for (i = 1; i < hdr->e_shnum; i++) {
2597568cb4eSPaul Mackerras 		if (sechdrs[i].sh_type == SHT_RELA) {
260c7d1f6afSAnton Blanchard 			pr_debug("Found relocations in section %u\n", i);
261c7d1f6afSAnton Blanchard 			pr_debug("Ptr: %p.  Number: %Lu\n",
2627568cb4eSPaul Mackerras 			       (void *)sechdrs[i].sh_addr,
2637568cb4eSPaul Mackerras 			       sechdrs[i].sh_size / sizeof(Elf64_Rela));
264eda09fbdSEmil Medve 
265eda09fbdSEmil Medve 			/* Sort the relocation information based on a symbol and
266eda09fbdSEmil Medve 			 * addend key. This is a stable O(n*log n) complexity
267eda09fbdSEmil Medve 			 * alogrithm but it will reduce the complexity of
268eda09fbdSEmil Medve 			 * count_relocs() to linear complexity O(n)
269eda09fbdSEmil Medve 			 */
270eda09fbdSEmil Medve 			sort((void *)sechdrs[i].sh_addr,
271eda09fbdSEmil Medve 			     sechdrs[i].sh_size / sizeof(Elf64_Rela),
272eda09fbdSEmil Medve 			     sizeof(Elf64_Rela), relacmp, relaswap);
273eda09fbdSEmil Medve 
2747568cb4eSPaul Mackerras 			relocs += count_relocs((void *)sechdrs[i].sh_addr,
2757568cb4eSPaul Mackerras 					       sechdrs[i].sh_size
2767568cb4eSPaul Mackerras 					       / sizeof(Elf64_Rela));
2777568cb4eSPaul Mackerras 		}
2787568cb4eSPaul Mackerras 	}
2797568cb4eSPaul Mackerras 
280f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
281f48cb8b4SSteven Rostedt 	/* make the trampoline to the ftrace_caller */
282f48cb8b4SSteven Rostedt 	relocs++;
283f48cb8b4SSteven Rostedt #endif
284f48cb8b4SSteven Rostedt 
285c7d1f6afSAnton Blanchard 	pr_debug("Looks like a total of %lu stubs, max\n", relocs);
2867568cb4eSPaul Mackerras 	return relocs * sizeof(struct ppc64_stub_entry);
2877568cb4eSPaul Mackerras }
2887568cb4eSPaul Mackerras 
2895b12c5c6SRusty Russell /* Still needed for ELFv2, for .TOC. */
2907568cb4eSPaul Mackerras static void dedotify_versions(struct modversion_info *vers,
2917568cb4eSPaul Mackerras 			      unsigned long size)
2927568cb4eSPaul Mackerras {
2937568cb4eSPaul Mackerras 	struct modversion_info *end;
2947568cb4eSPaul Mackerras 
2957568cb4eSPaul Mackerras 	for (end = (void *)vers + size; vers < end; vers++)
296c2cbcf53SLaurent Dufour 		if (vers->name[0] == '.') {
2977568cb4eSPaul Mackerras 			memmove(vers->name, vers->name+1, strlen(vers->name));
298c2cbcf53SLaurent Dufour 		}
2997568cb4eSPaul Mackerras }
3007568cb4eSPaul Mackerras 
301c153693dSAlan Modra /*
302c153693dSAlan Modra  * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
303c153693dSAlan Modra  * seem to be defined (value set later).
304c153693dSAlan Modra  */
3057568cb4eSPaul Mackerras static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
3067568cb4eSPaul Mackerras {
3077568cb4eSPaul Mackerras 	unsigned int i;
3087568cb4eSPaul Mackerras 
3097568cb4eSPaul Mackerras 	for (i = 1; i < numsyms; i++) {
3107568cb4eSPaul Mackerras 		if (syms[i].st_shndx == SHN_UNDEF) {
3117568cb4eSPaul Mackerras 			char *name = strtab + syms[i].st_name;
312c153693dSAlan Modra 			if (name[0] == '.') {
313c153693dSAlan Modra 				if (strcmp(name+1, "TOC.") == 0)
314c153693dSAlan Modra 					syms[i].st_shndx = SHN_ABS;
315f15838e9SAndreas Schwab 				syms[i].st_name++;
3167568cb4eSPaul Mackerras 			}
3177568cb4eSPaul Mackerras 		}
3187568cb4eSPaul Mackerras 	}
319c153693dSAlan Modra }
3207568cb4eSPaul Mackerras 
3214edebbeaSRusty Russell static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
3224edebbeaSRusty Russell 			       const char *strtab,
3234edebbeaSRusty Russell 			       unsigned int symindex)
3244edebbeaSRusty Russell {
3254edebbeaSRusty Russell 	unsigned int i, numsyms;
3264edebbeaSRusty Russell 	Elf64_Sym *syms;
3274edebbeaSRusty Russell 
3284edebbeaSRusty Russell 	syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
3294edebbeaSRusty Russell 	numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
3304edebbeaSRusty Russell 
3314edebbeaSRusty Russell 	for (i = 1; i < numsyms; i++) {
332c153693dSAlan Modra 		if (syms[i].st_shndx == SHN_ABS
333008d7a91SRusty Russell 		    && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
3344edebbeaSRusty Russell 			return &syms[i];
3354edebbeaSRusty Russell 	}
3364edebbeaSRusty Russell 	return NULL;
3374edebbeaSRusty Russell }
3384edebbeaSRusty Russell 
3397568cb4eSPaul Mackerras int module_frob_arch_sections(Elf64_Ehdr *hdr,
3407568cb4eSPaul Mackerras 			      Elf64_Shdr *sechdrs,
3417568cb4eSPaul Mackerras 			      char *secstrings,
3427568cb4eSPaul Mackerras 			      struct module *me)
3437568cb4eSPaul Mackerras {
3447568cb4eSPaul Mackerras 	unsigned int i;
3457568cb4eSPaul Mackerras 
3467568cb4eSPaul Mackerras 	/* Find .toc and .stubs sections, symtab and strtab */
3477568cb4eSPaul Mackerras 	for (i = 1; i < hdr->e_shnum; i++) {
3487568cb4eSPaul Mackerras 		char *p;
3497568cb4eSPaul Mackerras 		if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
3507568cb4eSPaul Mackerras 			me->arch.stubs_section = i;
3517568cb4eSPaul Mackerras 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0)
3527568cb4eSPaul Mackerras 			me->arch.toc_section = i;
3537568cb4eSPaul Mackerras 		else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
3547568cb4eSPaul Mackerras 			dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
3557568cb4eSPaul Mackerras 					  sechdrs[i].sh_size);
356*5633e85bSSergey Senozhatsky 		else if (!strcmp(secstrings + sechdrs[i].sh_name, ".opd")) {
357*5633e85bSSergey Senozhatsky 			me->arch.start_opd = sechdrs[i].sh_addr;
358*5633e85bSSergey Senozhatsky 			me->arch.end_opd = sechdrs[i].sh_addr +
359*5633e85bSSergey Senozhatsky 					   sechdrs[i].sh_size;
360*5633e85bSSergey Senozhatsky 		}
3617568cb4eSPaul Mackerras 
3627568cb4eSPaul Mackerras 		/* We don't handle .init for the moment: rename to _init */
3637568cb4eSPaul Mackerras 		while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
3647568cb4eSPaul Mackerras 			p[0] = '_';
3657568cb4eSPaul Mackerras 
3667568cb4eSPaul Mackerras 		if (sechdrs[i].sh_type == SHT_SYMTAB)
3677568cb4eSPaul Mackerras 			dedotify((void *)hdr + sechdrs[i].sh_offset,
3687568cb4eSPaul Mackerras 				 sechdrs[i].sh_size / sizeof(Elf64_Sym),
3697568cb4eSPaul Mackerras 				 (void *)hdr
3707568cb4eSPaul Mackerras 				 + sechdrs[sechdrs[i].sh_link].sh_offset);
3717568cb4eSPaul Mackerras 	}
372f749edaeSAlan Modra 
373f749edaeSAlan Modra 	if (!me->arch.stubs_section) {
374c7d1f6afSAnton Blanchard 		pr_err("%s: doesn't contain .stubs.\n", me->name);
3757568cb4eSPaul Mackerras 		return -ENOEXEC;
3767568cb4eSPaul Mackerras 	}
3777568cb4eSPaul Mackerras 
378f749edaeSAlan Modra 	/* If we don't have a .toc, just use .stubs.  We need to set r2
379f749edaeSAlan Modra 	   to some reasonable value in case the module calls out to
380f749edaeSAlan Modra 	   other functions via a stub, or if a function pointer escapes
381f749edaeSAlan Modra 	   the module by some means.  */
382f749edaeSAlan Modra 	if (!me->arch.toc_section)
383f749edaeSAlan Modra 		me->arch.toc_section = me->arch.stubs_section;
384f749edaeSAlan Modra 
3857568cb4eSPaul Mackerras 	/* Override the stubs size */
3867568cb4eSPaul Mackerras 	sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
3877568cb4eSPaul Mackerras 	return 0;
3887568cb4eSPaul Mackerras }
3897568cb4eSPaul Mackerras 
3907568cb4eSPaul Mackerras /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
3917568cb4eSPaul Mackerras    gives the value maximum span in an instruction which uses a signed
3927568cb4eSPaul Mackerras    offset) */
393136cd345SMichael Ellerman static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me)
3947568cb4eSPaul Mackerras {
3957568cb4eSPaul Mackerras 	return sechdrs[me->arch.toc_section].sh_addr + 0x8000;
3967568cb4eSPaul Mackerras }
3977568cb4eSPaul Mackerras 
3987568cb4eSPaul Mackerras /* Both low and high 16 bits are added as SIGNED additions, so if low
3997568cb4eSPaul Mackerras    16 bits has high bit set, high 16 bits must be adjusted.  These
4007568cb4eSPaul Mackerras    macros do that (stolen from binutils). */
4017568cb4eSPaul Mackerras #define PPC_LO(v) ((v) & 0xffff)
4027568cb4eSPaul Mackerras #define PPC_HI(v) (((v) >> 16) & 0xffff)
4037568cb4eSPaul Mackerras #define PPC_HA(v) PPC_HI ((v) + 0x8000)
4047568cb4eSPaul Mackerras 
4057568cb4eSPaul Mackerras /* Patch stub to reference function and correct r2 value. */
406136cd345SMichael Ellerman static inline int create_stub(const Elf64_Shdr *sechdrs,
4077568cb4eSPaul Mackerras 			      struct ppc64_stub_entry *entry,
408008d7a91SRusty Russell 			      unsigned long addr,
4097568cb4eSPaul Mackerras 			      struct module *me)
4107568cb4eSPaul Mackerras {
4117568cb4eSPaul Mackerras 	long reladdr;
4127568cb4eSPaul Mackerras 
41383775b85SAnton Blanchard 	memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns));
4147568cb4eSPaul Mackerras 
4157568cb4eSPaul Mackerras 	/* Stub uses address relative to r2. */
4167568cb4eSPaul Mackerras 	reladdr = (unsigned long)entry - my_r2(sechdrs, me);
4177568cb4eSPaul Mackerras 	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
418c7d1f6afSAnton Blanchard 		pr_err("%s: Address %p of stub out of range of %p.\n",
4197568cb4eSPaul Mackerras 		       me->name, (void *)reladdr, (void *)my_r2);
4207568cb4eSPaul Mackerras 		return 0;
4217568cb4eSPaul Mackerras 	}
422c7d1f6afSAnton Blanchard 	pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
4237568cb4eSPaul Mackerras 
4240e60e46eSRusty Russell 	entry->jump[0] |= PPC_HA(reladdr);
4250e60e46eSRusty Russell 	entry->jump[1] |= PPC_LO(reladdr);
426008d7a91SRusty Russell 	entry->funcdata = func_desc(addr);
427f17c4e01SMichael Ellerman 	entry->magic = STUB_MAGIC;
428f17c4e01SMichael Ellerman 
4297568cb4eSPaul Mackerras 	return 1;
4307568cb4eSPaul Mackerras }
4317568cb4eSPaul Mackerras 
432008d7a91SRusty Russell /* Create stub to jump to function described in this OPD/ptr: we need the
4337568cb4eSPaul Mackerras    stub to set up the TOC ptr (r2) for the function. */
434136cd345SMichael Ellerman static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
435008d7a91SRusty Russell 				   unsigned long addr,
4367568cb4eSPaul Mackerras 				   struct module *me)
4377568cb4eSPaul Mackerras {
4387568cb4eSPaul Mackerras 	struct ppc64_stub_entry *stubs;
4397568cb4eSPaul Mackerras 	unsigned int i, num_stubs;
4407568cb4eSPaul Mackerras 
4417568cb4eSPaul Mackerras 	num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
4427568cb4eSPaul Mackerras 
4437568cb4eSPaul Mackerras 	/* Find this stub, or if that fails, the next avail. entry */
4447568cb4eSPaul Mackerras 	stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
445008d7a91SRusty Russell 	for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
4461c0437afSKamalesh Babulal 		if (WARN_ON(i >= num_stubs))
4471c0437afSKamalesh Babulal 			return 0;
4487568cb4eSPaul Mackerras 
449008d7a91SRusty Russell 		if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
4507568cb4eSPaul Mackerras 			return (unsigned long)&stubs[i];
4517568cb4eSPaul Mackerras 	}
4527568cb4eSPaul Mackerras 
453008d7a91SRusty Russell 	if (!create_stub(sechdrs, &stubs[i], addr, me))
4547568cb4eSPaul Mackerras 		return 0;
4557568cb4eSPaul Mackerras 
4567568cb4eSPaul Mackerras 	return (unsigned long)&stubs[i];
4577568cb4eSPaul Mackerras }
4587568cb4eSPaul Mackerras 
45915308664STorsten Duwe #ifdef CC_USING_MPROFILE_KERNEL
46015308664STorsten Duwe static bool is_early_mcount_callsite(u32 *instruction)
46115308664STorsten Duwe {
46215308664STorsten Duwe 	/*
46315308664STorsten Duwe 	 * Check if this is one of the -mprofile-kernel sequences.
46415308664STorsten Duwe 	 */
46515308664STorsten Duwe 	if (instruction[-1] == PPC_INST_STD_LR &&
46615308664STorsten Duwe 	    instruction[-2] == PPC_INST_MFLR)
46715308664STorsten Duwe 		return true;
46815308664STorsten Duwe 
46915308664STorsten Duwe 	if (instruction[-1] == PPC_INST_MFLR)
47015308664STorsten Duwe 		return true;
47115308664STorsten Duwe 
47215308664STorsten Duwe 	return false;
47315308664STorsten Duwe }
47415308664STorsten Duwe 
47515308664STorsten Duwe /*
47615308664STorsten Duwe  * In case of _mcount calls, do not save the current callee's TOC (in r2) into
47715308664STorsten Duwe  * the original caller's stack frame. If we did we would clobber the saved TOC
47815308664STorsten Duwe  * value of the original caller.
47915308664STorsten Duwe  */
48015308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr)
48115308664STorsten Duwe {
48215308664STorsten Duwe 	struct ppc64_stub_entry *stub = (struct ppc64_stub_entry *)addr;
48315308664STorsten Duwe 
48415308664STorsten Duwe 	/* Only for calls to _mcount */
48515308664STorsten Duwe 	if (strcmp("_mcount", name) != 0)
48615308664STorsten Duwe 		return;
48715308664STorsten Duwe 
48815308664STorsten Duwe 	stub->jump[2] = PPC_INST_NOP;
48915308664STorsten Duwe }
49015308664STorsten Duwe #else
49115308664STorsten Duwe static void squash_toc_save_inst(const char *name, unsigned long addr) { }
49215308664STorsten Duwe 
49315308664STorsten Duwe /* without -mprofile-kernel, mcount calls are never early */
49415308664STorsten Duwe static bool is_early_mcount_callsite(u32 *instruction)
49515308664STorsten Duwe {
49615308664STorsten Duwe 	return false;
49715308664STorsten Duwe }
49815308664STorsten Duwe #endif
49915308664STorsten Duwe 
5007568cb4eSPaul Mackerras /* We expect a noop next: if it is, replace it with instruction to
5017568cb4eSPaul Mackerras    restore r2. */
5027568cb4eSPaul Mackerras static int restore_r2(u32 *instruction, struct module *me)
5037568cb4eSPaul Mackerras {
50415308664STorsten Duwe 	if (is_early_mcount_callsite(instruction - 1))
50515308664STorsten Duwe 		return 1;
50631278b17SMichael Ellerman 
50731278b17SMichael Ellerman 	if (*instruction != PPC_INST_NOP) {
508c7d1f6afSAnton Blanchard 		pr_err("%s: Expect noop after relocate, got %08x\n",
5097568cb4eSPaul Mackerras 		       me->name, *instruction);
5107568cb4eSPaul Mackerras 		return 0;
5117568cb4eSPaul Mackerras 	}
512d2fae548SRusty Russell 	/* ld r2,R2_STACK_OFFSET(r1) */
51315308664STorsten Duwe 	*instruction = PPC_INST_LD_TOC;
5147568cb4eSPaul Mackerras 	return 1;
5157568cb4eSPaul Mackerras }
5167568cb4eSPaul Mackerras 
5177568cb4eSPaul Mackerras int apply_relocate_add(Elf64_Shdr *sechdrs,
5187568cb4eSPaul Mackerras 		       const char *strtab,
5197568cb4eSPaul Mackerras 		       unsigned int symindex,
5207568cb4eSPaul Mackerras 		       unsigned int relsec,
5217568cb4eSPaul Mackerras 		       struct module *me)
5227568cb4eSPaul Mackerras {
5237568cb4eSPaul Mackerras 	unsigned int i;
5247568cb4eSPaul Mackerras 	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
5257568cb4eSPaul Mackerras 	Elf64_Sym *sym;
5267568cb4eSPaul Mackerras 	unsigned long *location;
5277568cb4eSPaul Mackerras 	unsigned long value;
5287568cb4eSPaul Mackerras 
529c7d1f6afSAnton Blanchard 	pr_debug("Applying ADD relocate section %u to %u\n", relsec,
5307568cb4eSPaul Mackerras 	       sechdrs[relsec].sh_info);
5314edebbeaSRusty Russell 
5324edebbeaSRusty Russell 	/* First time we're called, we can fix up .TOC. */
5334edebbeaSRusty Russell 	if (!me->arch.toc_fixed) {
5344edebbeaSRusty Russell 		sym = find_dot_toc(sechdrs, strtab, symindex);
5354edebbeaSRusty Russell 		/* It's theoretically possible that a module doesn't want a
5364edebbeaSRusty Russell 		 * .TOC. so don't fail it just for that. */
5374edebbeaSRusty Russell 		if (sym)
5384edebbeaSRusty Russell 			sym->st_value = my_r2(sechdrs, me);
5394edebbeaSRusty Russell 		me->arch.toc_fixed = true;
5404edebbeaSRusty Russell 	}
5414edebbeaSRusty Russell 
5427568cb4eSPaul Mackerras 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
5437568cb4eSPaul Mackerras 		/* This is where to make the change */
5447568cb4eSPaul Mackerras 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
5457568cb4eSPaul Mackerras 			+ rela[i].r_offset;
5467568cb4eSPaul Mackerras 		/* This is the symbol it is referring to */
5477568cb4eSPaul Mackerras 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
5487568cb4eSPaul Mackerras 			+ ELF64_R_SYM(rela[i].r_info);
5497568cb4eSPaul Mackerras 
550c7d1f6afSAnton Blanchard 		pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
5517568cb4eSPaul Mackerras 		       location, (long)ELF64_R_TYPE(rela[i].r_info),
5527568cb4eSPaul Mackerras 		       strtab + sym->st_name, (unsigned long)sym->st_value,
5537568cb4eSPaul Mackerras 		       (long)rela[i].r_addend);
5547568cb4eSPaul Mackerras 
5557568cb4eSPaul Mackerras 		/* `Everything is relative'. */
5567568cb4eSPaul Mackerras 		value = sym->st_value + rela[i].r_addend;
5577568cb4eSPaul Mackerras 
5587568cb4eSPaul Mackerras 		switch (ELF64_R_TYPE(rela[i].r_info)) {
5597568cb4eSPaul Mackerras 		case R_PPC64_ADDR32:
5607568cb4eSPaul Mackerras 			/* Simply set it */
5617568cb4eSPaul Mackerras 			*(u32 *)location = value;
5627568cb4eSPaul Mackerras 			break;
5637568cb4eSPaul Mackerras 
5647568cb4eSPaul Mackerras 		case R_PPC64_ADDR64:
5657568cb4eSPaul Mackerras 			/* Simply set it */
5667568cb4eSPaul Mackerras 			*(unsigned long *)location = value;
5677568cb4eSPaul Mackerras 			break;
5687568cb4eSPaul Mackerras 
5697568cb4eSPaul Mackerras 		case R_PPC64_TOC:
5707568cb4eSPaul Mackerras 			*(unsigned long *)location = my_r2(sechdrs, me);
5717568cb4eSPaul Mackerras 			break;
5727568cb4eSPaul Mackerras 
5737568cb4eSPaul Mackerras 		case R_PPC64_TOC16:
574f749edaeSAlan Modra 			/* Subtract TOC pointer */
5757568cb4eSPaul Mackerras 			value -= my_r2(sechdrs, me);
5767568cb4eSPaul Mackerras 			if (value + 0x8000 > 0xffff) {
577c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16 relocation (0x%lx)\n",
5787568cb4eSPaul Mackerras 				       me->name, value);
5797568cb4eSPaul Mackerras 				return -ENOEXEC;
5807568cb4eSPaul Mackerras 			}
5817568cb4eSPaul Mackerras 			*((uint16_t *) location)
5827568cb4eSPaul Mackerras 				= (*((uint16_t *) location) & ~0xffff)
5837568cb4eSPaul Mackerras 				| (value & 0xffff);
5847568cb4eSPaul Mackerras 			break;
5857568cb4eSPaul Mackerras 
5861fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_LO:
5871fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
5881fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
5891fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
5901fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xffff)
5911fbe9cf2SAnton Blanchard 				| (value & 0xffff);
5921fbe9cf2SAnton Blanchard 			break;
5931fbe9cf2SAnton Blanchard 
5947568cb4eSPaul Mackerras 		case R_PPC64_TOC16_DS:
595f749edaeSAlan Modra 			/* Subtract TOC pointer */
5967568cb4eSPaul Mackerras 			value -= my_r2(sechdrs, me);
5977568cb4eSPaul Mackerras 			if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
598c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
5997568cb4eSPaul Mackerras 				       me->name, value);
6007568cb4eSPaul Mackerras 				return -ENOEXEC;
6017568cb4eSPaul Mackerras 			}
6027568cb4eSPaul Mackerras 			*((uint16_t *) location)
6037568cb4eSPaul Mackerras 				= (*((uint16_t *) location) & ~0xfffc)
6047568cb4eSPaul Mackerras 				| (value & 0xfffc);
6057568cb4eSPaul Mackerras 			break;
6067568cb4eSPaul Mackerras 
6071fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_LO_DS:
6081fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
6091fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
6101fbe9cf2SAnton Blanchard 			if ((value & 3) != 0) {
611c7d1f6afSAnton Blanchard 				pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
6121fbe9cf2SAnton Blanchard 				       me->name, value);
6131fbe9cf2SAnton Blanchard 				return -ENOEXEC;
6141fbe9cf2SAnton Blanchard 			}
6151fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
6161fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xfffc)
6171fbe9cf2SAnton Blanchard 				| (value & 0xfffc);
6181fbe9cf2SAnton Blanchard 			break;
6191fbe9cf2SAnton Blanchard 
6201fbe9cf2SAnton Blanchard 		case R_PPC64_TOC16_HA:
6211fbe9cf2SAnton Blanchard 			/* Subtract TOC pointer */
6221fbe9cf2SAnton Blanchard 			value -= my_r2(sechdrs, me);
6231fbe9cf2SAnton Blanchard 			value = ((value + 0x8000) >> 16);
6241fbe9cf2SAnton Blanchard 			*((uint16_t *) location)
6251fbe9cf2SAnton Blanchard 				= (*((uint16_t *) location) & ~0xffff)
6261fbe9cf2SAnton Blanchard 				| (value & 0xffff);
6271fbe9cf2SAnton Blanchard 			break;
6281fbe9cf2SAnton Blanchard 
6297568cb4eSPaul Mackerras 		case R_PPC_REL24:
6307568cb4eSPaul Mackerras 			/* FIXME: Handle weak symbols here --RR */
6317568cb4eSPaul Mackerras 			if (sym->st_shndx == SHN_UNDEF) {
6327568cb4eSPaul Mackerras 				/* External: go via stub */
6337568cb4eSPaul Mackerras 				value = stub_for_addr(sechdrs, value, me);
6347568cb4eSPaul Mackerras 				if (!value)
6357568cb4eSPaul Mackerras 					return -ENOENT;
6367568cb4eSPaul Mackerras 				if (!restore_r2((u32 *)location + 1, me))
6377568cb4eSPaul Mackerras 					return -ENOEXEC;
63815308664STorsten Duwe 
63915308664STorsten Duwe 				squash_toc_save_inst(strtab + sym->st_name, value);
640008d7a91SRusty Russell 			} else
641008d7a91SRusty Russell 				value += local_entry_offset(sym);
6427568cb4eSPaul Mackerras 
6437568cb4eSPaul Mackerras 			/* Convert value to relative */
6447568cb4eSPaul Mackerras 			value -= (unsigned long)location;
6457568cb4eSPaul Mackerras 			if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
646c7d1f6afSAnton Blanchard 				pr_err("%s: REL24 %li out of range!\n",
6477568cb4eSPaul Mackerras 				       me->name, (long int)value);
6487568cb4eSPaul Mackerras 				return -ENOEXEC;
6497568cb4eSPaul Mackerras 			}
6507568cb4eSPaul Mackerras 
6517568cb4eSPaul Mackerras 			/* Only replace bits 2 through 26 */
6527568cb4eSPaul Mackerras 			*(uint32_t *)location
6537568cb4eSPaul Mackerras 				= (*(uint32_t *)location & ~0x03fffffc)
6547568cb4eSPaul Mackerras 				| (value & 0x03fffffc);
6557568cb4eSPaul Mackerras 			break;
6567568cb4eSPaul Mackerras 
65721c4ff80SBenjamin Herrenschmidt 		case R_PPC64_REL64:
65821c4ff80SBenjamin Herrenschmidt 			/* 64 bits relative (used by features fixups) */
65921c4ff80SBenjamin Herrenschmidt 			*location = value - (unsigned long)location;
66021c4ff80SBenjamin Herrenschmidt 			break;
66121c4ff80SBenjamin Herrenschmidt 
6629f751b82SMichael Ellerman 		case R_PPC64_REL32:
6639f751b82SMichael Ellerman 			/* 32 bits relative (used by relative exception tables) */
6649f751b82SMichael Ellerman 			*(u32 *)location = value - (unsigned long)location;
6659f751b82SMichael Ellerman 			break;
6669f751b82SMichael Ellerman 
667d247da0aSRusty Russell 		case R_PPC64_TOCSAVE:
668d247da0aSRusty Russell 			/*
669d247da0aSRusty Russell 			 * Marker reloc indicates we don't have to save r2.
670d247da0aSRusty Russell 			 * That would only save us one instruction, so ignore
671d247da0aSRusty Russell 			 * it.
672d247da0aSRusty Russell 			 */
673d247da0aSRusty Russell 			break;
674d247da0aSRusty Russell 
675a61674bdSUlrich Weigand 		case R_PPC64_ENTRY:
676a61674bdSUlrich Weigand 			/*
677a61674bdSUlrich Weigand 			 * Optimize ELFv2 large code model entry point if
678a61674bdSUlrich Weigand 			 * the TOC is within 2GB range of current location.
679a61674bdSUlrich Weigand 			 */
680a61674bdSUlrich Weigand 			value = my_r2(sechdrs, me) - (unsigned long)location;
681a61674bdSUlrich Weigand 			if (value + 0x80008000 > 0xffffffff)
682a61674bdSUlrich Weigand 				break;
683a61674bdSUlrich Weigand 			/*
684a61674bdSUlrich Weigand 			 * Check for the large code model prolog sequence:
685a61674bdSUlrich Weigand 		         *	ld r2, ...(r12)
686a61674bdSUlrich Weigand 			 *	add r2, r2, r12
687a61674bdSUlrich Weigand 			 */
688a61674bdSUlrich Weigand 			if ((((uint32_t *)location)[0] & ~0xfffc)
689a61674bdSUlrich Weigand 			    != 0xe84c0000)
690a61674bdSUlrich Weigand 				break;
691a61674bdSUlrich Weigand 			if (((uint32_t *)location)[1] != 0x7c426214)
692a61674bdSUlrich Weigand 				break;
693a61674bdSUlrich Weigand 			/*
694a61674bdSUlrich Weigand 			 * If found, replace it with:
695a61674bdSUlrich Weigand 			 *	addis r2, r12, (.TOC.-func)@ha
696a61674bdSUlrich Weigand 			 *	addi r2, r12, (.TOC.-func)@l
697a61674bdSUlrich Weigand 			 */
698a61674bdSUlrich Weigand 			((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value);
699a61674bdSUlrich Weigand 			((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value);
700a61674bdSUlrich Weigand 			break;
701a61674bdSUlrich Weigand 
7020906584aSRusty Russell 		case R_PPC64_REL16_HA:
7030906584aSRusty Russell 			/* Subtract location pointer */
7040906584aSRusty Russell 			value -= (unsigned long)location;
7050906584aSRusty Russell 			value = ((value + 0x8000) >> 16);
7060906584aSRusty Russell 			*((uint16_t *) location)
7070906584aSRusty Russell 				= (*((uint16_t *) location) & ~0xffff)
7080906584aSRusty Russell 				| (value & 0xffff);
7090906584aSRusty Russell 			break;
7100906584aSRusty Russell 
7110906584aSRusty Russell 		case R_PPC64_REL16_LO:
7120906584aSRusty Russell 			/* Subtract location pointer */
7130906584aSRusty Russell 			value -= (unsigned long)location;
7140906584aSRusty Russell 			*((uint16_t *) location)
7150906584aSRusty Russell 				= (*((uint16_t *) location) & ~0xffff)
7160906584aSRusty Russell 				| (value & 0xffff);
7170906584aSRusty Russell 			break;
7180906584aSRusty Russell 
7197568cb4eSPaul Mackerras 		default:
720c7d1f6afSAnton Blanchard 			pr_err("%s: Unknown ADD relocation: %lu\n",
7217568cb4eSPaul Mackerras 			       me->name,
7227568cb4eSPaul Mackerras 			       (unsigned long)ELF64_R_TYPE(rela[i].r_info));
7237568cb4eSPaul Mackerras 			return -ENOEXEC;
7247568cb4eSPaul Mackerras 		}
7257568cb4eSPaul Mackerras 	}
7267568cb4eSPaul Mackerras 
727136cd345SMichael Ellerman 	return 0;
728136cd345SMichael Ellerman }
729136cd345SMichael Ellerman 
730f48cb8b4SSteven Rostedt #ifdef CONFIG_DYNAMIC_FTRACE
731336a7b5dSMichael Ellerman 
732336a7b5dSMichael Ellerman #ifdef CC_USING_MPROFILE_KERNEL
733336a7b5dSMichael Ellerman 
734336a7b5dSMichael Ellerman #define PACATOC offsetof(struct paca_struct, kernel_toc)
735336a7b5dSMichael Ellerman 
736336a7b5dSMichael Ellerman /*
737336a7b5dSMichael Ellerman  * For mprofile-kernel we use a special stub for ftrace_caller() because we
738336a7b5dSMichael Ellerman  * can't rely on r2 containing this module's TOC when we enter the stub.
739336a7b5dSMichael Ellerman  *
740336a7b5dSMichael Ellerman  * That can happen if the function calling us didn't need to use the toc. In
741336a7b5dSMichael Ellerman  * that case it won't have setup r2, and the r2 value will be either the
742336a7b5dSMichael Ellerman  * kernel's toc, or possibly another modules toc.
743336a7b5dSMichael Ellerman  *
744336a7b5dSMichael Ellerman  * To deal with that this stub uses the kernel toc, which is always accessible
745336a7b5dSMichael Ellerman  * via the paca (in r13). The target (ftrace_caller()) is responsible for
746336a7b5dSMichael Ellerman  * saving and restoring the toc before returning.
747336a7b5dSMichael Ellerman  */
748336a7b5dSMichael Ellerman static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, struct module *me)
749336a7b5dSMichael Ellerman {
750336a7b5dSMichael Ellerman 	struct ppc64_stub_entry *entry;
751336a7b5dSMichael Ellerman 	unsigned int i, num_stubs;
752336a7b5dSMichael Ellerman 	static u32 stub_insns[] = {
753336a7b5dSMichael Ellerman 		0xe98d0000 | PACATOC, 	/* ld      r12,PACATOC(r13)	*/
754336a7b5dSMichael Ellerman 		0x3d8c0000,		/* addis   r12,r12,<high>	*/
755336a7b5dSMichael Ellerman 		0x398c0000, 		/* addi    r12,r12,<low>	*/
756336a7b5dSMichael Ellerman 		0x7d8903a6, 		/* mtctr   r12			*/
757336a7b5dSMichael Ellerman 		0x4e800420, 		/* bctr				*/
758336a7b5dSMichael Ellerman 	};
759336a7b5dSMichael Ellerman 	long reladdr;
760336a7b5dSMichael Ellerman 
761336a7b5dSMichael Ellerman 	num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*entry);
762336a7b5dSMichael Ellerman 
763336a7b5dSMichael Ellerman 	/* Find the next available stub entry */
764336a7b5dSMichael Ellerman 	entry = (void *)sechdrs[me->arch.stubs_section].sh_addr;
765336a7b5dSMichael Ellerman 	for (i = 0; i < num_stubs && stub_func_addr(entry->funcdata); i++, entry++);
766336a7b5dSMichael Ellerman 
767336a7b5dSMichael Ellerman 	if (i >= num_stubs) {
768336a7b5dSMichael Ellerman 		pr_err("%s: Unable to find a free slot for ftrace stub.\n", me->name);
769336a7b5dSMichael Ellerman 		return 0;
770336a7b5dSMichael Ellerman 	}
771336a7b5dSMichael Ellerman 
772336a7b5dSMichael Ellerman 	memcpy(entry->jump, stub_insns, sizeof(stub_insns));
773336a7b5dSMichael Ellerman 
774336a7b5dSMichael Ellerman 	/* Stub uses address relative to kernel toc (from the paca) */
775336a7b5dSMichael Ellerman 	reladdr = (unsigned long)ftrace_caller - kernel_toc_addr();
776336a7b5dSMichael Ellerman 	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
777336a7b5dSMichael Ellerman 		pr_err("%s: Address of ftrace_caller out of range of kernel_toc.\n", me->name);
778336a7b5dSMichael Ellerman 		return 0;
779336a7b5dSMichael Ellerman 	}
780336a7b5dSMichael Ellerman 
781336a7b5dSMichael Ellerman 	entry->jump[1] |= PPC_HA(reladdr);
782336a7b5dSMichael Ellerman 	entry->jump[2] |= PPC_LO(reladdr);
783336a7b5dSMichael Ellerman 
784336a7b5dSMichael Ellerman 	/* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */
785336a7b5dSMichael Ellerman 	entry->funcdata = func_desc((unsigned long)ftrace_caller);
786336a7b5dSMichael Ellerman 	entry->magic = STUB_MAGIC;
787336a7b5dSMichael Ellerman 
788336a7b5dSMichael Ellerman 	return (unsigned long)entry;
789336a7b5dSMichael Ellerman }
790336a7b5dSMichael Ellerman #else
791336a7b5dSMichael Ellerman static unsigned long create_ftrace_stub(const Elf64_Shdr *sechdrs, struct module *me)
792336a7b5dSMichael Ellerman {
793336a7b5dSMichael Ellerman 	return stub_for_addr(sechdrs, (unsigned long)ftrace_caller, me);
794336a7b5dSMichael Ellerman }
795f48cb8b4SSteven Rostedt #endif
796f48cb8b4SSteven Rostedt 
797136cd345SMichael Ellerman int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
798136cd345SMichael Ellerman {
799136cd345SMichael Ellerman 	mod->arch.toc = my_r2(sechdrs, mod);
800336a7b5dSMichael Ellerman 	mod->arch.tramp = create_ftrace_stub(sechdrs, mod);
801136cd345SMichael Ellerman 
802136cd345SMichael Ellerman 	if (!mod->arch.tramp)
803136cd345SMichael Ellerman 		return -ENOENT;
8047568cb4eSPaul Mackerras 
8057568cb4eSPaul Mackerras 	return 0;
8067568cb4eSPaul Mackerras }
807136cd345SMichael Ellerman #endif
808