xref: /openbmc/linux/arch/powerpc/kernel/module_64.c (revision 6427c165)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*  Kernel module help for PPC64.
3     Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
4 
5 */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/module.h>
10 #include <linux/elf.h>
11 #include <linux/moduleloader.h>
12 #include <linux/err.h>
13 #include <linux/vmalloc.h>
14 #include <linux/ftrace.h>
15 #include <linux/bug.h>
16 #include <linux/uaccess.h>
17 #include <asm/module.h>
18 #include <asm/firmware.h>
19 #include <asm/code-patching.h>
20 #include <linux/sort.h>
21 #include <asm/setup.h>
22 #include <asm/sections.h>
23 #include <asm/inst.h>
24 
25 /* FIXME: We don't do .init separately.  To do this, we'd need to have
26    a separate r2 value in the init and core section, and stub between
27    them, too.
28 
29    Using a magic allocator which places modules within 32MB solves
30    this, and makes other things simpler.  Anton?
31    --RR.  */
32 
33 #ifdef PPC64_ELF_ABI_v2
34 
35 /* An address is simply the address of the function. */
36 typedef unsigned long func_desc_t;
37 
38 static func_desc_t func_desc(unsigned long addr)
39 {
40 	return addr;
41 }
42 static unsigned long func_addr(unsigned long addr)
43 {
44 	return addr;
45 }
46 static unsigned long stub_func_addr(func_desc_t func)
47 {
48 	return func;
49 }
50 
51 /* PowerPC64 specific values for the Elf64_Sym st_other field.  */
52 #define STO_PPC64_LOCAL_BIT	5
53 #define STO_PPC64_LOCAL_MASK	(7 << STO_PPC64_LOCAL_BIT)
54 #define PPC64_LOCAL_ENTRY_OFFSET(other)					\
55  (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
56 
57 static unsigned int local_entry_offset(const Elf64_Sym *sym)
58 {
59 	/* sym->st_other indicates offset to local entry point
60 	 * (otherwise it will assume r12 is the address of the start
61 	 * of function and try to derive r2 from it). */
62 	return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
63 }
64 #else
65 
66 /* An address is address of the OPD entry, which contains address of fn. */
67 typedef struct ppc64_opd_entry func_desc_t;
68 
69 static func_desc_t func_desc(unsigned long addr)
70 {
71 	return *(struct ppc64_opd_entry *)addr;
72 }
73 static unsigned long func_addr(unsigned long addr)
74 {
75 	return func_desc(addr).funcaddr;
76 }
77 static unsigned long stub_func_addr(func_desc_t func)
78 {
79 	return func.funcaddr;
80 }
81 static unsigned int local_entry_offset(const Elf64_Sym *sym)
82 {
83 	return 0;
84 }
85 
86 void *dereference_module_function_descriptor(struct module *mod, void *ptr)
87 {
88 	if (ptr < (void *)mod->arch.start_opd ||
89 			ptr >= (void *)mod->arch.end_opd)
90 		return ptr;
91 
92 	return dereference_function_descriptor(ptr);
93 }
94 #endif
95 
96 #define STUB_MAGIC 0x73747562 /* stub */
97 
98 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
99    the kernel itself).  But on PPC64, these need to be used for every
100    jump, actually, to reset r2 (TOC+0x8000). */
101 struct ppc64_stub_entry
102 {
103 	/* 28 byte jump instruction sequence (7 instructions). We only
104 	 * need 6 instructions on ABIv2 but we always allocate 7 so
105 	 * so we don't have to modify the trampoline load instruction. */
106 	u32 jump[7];
107 	/* Used by ftrace to identify stubs */
108 	u32 magic;
109 	/* Data for the above code */
110 	func_desc_t funcdata;
111 };
112 
113 /*
114  * PPC64 uses 24 bit jumps, but we need to jump into other modules or
115  * the kernel which may be further.  So we jump to a stub.
116  *
117  * For ELFv1 we need to use this to set up the new r2 value (aka TOC
118  * pointer).  For ELFv2 it's the callee's responsibility to set up the
119  * new r2, but for both we need to save the old r2.
120  *
121  * We could simply patch the new r2 value and function pointer into
122  * the stub, but it's significantly shorter to put these values at the
123  * end of the stub code, and patch the stub address (32-bits relative
124  * to the TOC ptr, r2) into the stub.
125  */
126 static u32 ppc64_stub_insns[] = {
127 	PPC_RAW_ADDIS(_R11, _R2, 0),
128 	PPC_RAW_ADDI(_R11, _R11, 0),
129 	/* Save current r2 value in magic place on the stack. */
130 	PPC_RAW_STD(_R2, _R1, R2_STACK_OFFSET),
131 	PPC_RAW_LD(_R12, _R11, 32),
132 #ifdef PPC64_ELF_ABI_v1
133 	/* Set up new r2 from function descriptor */
134 	PPC_RAW_LD(_R2, _R11, 40),
135 #endif
136 	PPC_RAW_MTCTR(_R12),
137 	PPC_RAW_BCTR(),
138 };
139 
140 /* Count how many different 24-bit relocations (different symbol,
141    different addend) */
142 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
143 {
144 	unsigned int i, r_info, r_addend, _count_relocs;
145 
146 	/* FIXME: Only count external ones --RR */
147 	_count_relocs = 0;
148 	r_info = 0;
149 	r_addend = 0;
150 	for (i = 0; i < num; i++)
151 		/* Only count 24-bit relocs, others don't need stubs */
152 		if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
153 		    (r_info != ELF64_R_SYM(rela[i].r_info) ||
154 		     r_addend != rela[i].r_addend)) {
155 			_count_relocs++;
156 			r_info = ELF64_R_SYM(rela[i].r_info);
157 			r_addend = rela[i].r_addend;
158 		}
159 
160 	return _count_relocs;
161 }
162 
163 static int relacmp(const void *_x, const void *_y)
164 {
165 	const Elf64_Rela *x, *y;
166 
167 	y = (Elf64_Rela *)_x;
168 	x = (Elf64_Rela *)_y;
169 
170 	/* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
171 	 * make the comparison cheaper/faster. It won't affect the sorting or
172 	 * the counting algorithms' performance
173 	 */
174 	if (x->r_info < y->r_info)
175 		return -1;
176 	else if (x->r_info > y->r_info)
177 		return 1;
178 	else if (x->r_addend < y->r_addend)
179 		return -1;
180 	else if (x->r_addend > y->r_addend)
181 		return 1;
182 	else
183 		return 0;
184 }
185 
186 /* Get size of potential trampolines required. */
187 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
188 				    const Elf64_Shdr *sechdrs)
189 {
190 	/* One extra reloc so it's always 0-funcaddr terminated */
191 	unsigned long relocs = 1;
192 	unsigned i;
193 
194 	/* Every relocated section... */
195 	for (i = 1; i < hdr->e_shnum; i++) {
196 		if (sechdrs[i].sh_type == SHT_RELA) {
197 			pr_debug("Found relocations in section %u\n", i);
198 			pr_debug("Ptr: %p.  Number: %Lu\n",
199 			       (void *)sechdrs[i].sh_addr,
200 			       sechdrs[i].sh_size / sizeof(Elf64_Rela));
201 
202 			/* Sort the relocation information based on a symbol and
203 			 * addend key. This is a stable O(n*log n) complexity
204 			 * alogrithm but it will reduce the complexity of
205 			 * count_relocs() to linear complexity O(n)
206 			 */
207 			sort((void *)sechdrs[i].sh_addr,
208 			     sechdrs[i].sh_size / sizeof(Elf64_Rela),
209 			     sizeof(Elf64_Rela), relacmp, NULL);
210 
211 			relocs += count_relocs((void *)sechdrs[i].sh_addr,
212 					       sechdrs[i].sh_size
213 					       / sizeof(Elf64_Rela));
214 		}
215 	}
216 
217 #ifdef CONFIG_DYNAMIC_FTRACE
218 	/* make the trampoline to the ftrace_caller */
219 	relocs++;
220 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
221 	/* an additional one for ftrace_regs_caller */
222 	relocs++;
223 #endif
224 #endif
225 
226 	pr_debug("Looks like a total of %lu stubs, max\n", relocs);
227 	return relocs * sizeof(struct ppc64_stub_entry);
228 }
229 
230 /* Still needed for ELFv2, for .TOC. */
231 static void dedotify_versions(struct modversion_info *vers,
232 			      unsigned long size)
233 {
234 	struct modversion_info *end;
235 
236 	for (end = (void *)vers + size; vers < end; vers++)
237 		if (vers->name[0] == '.') {
238 			memmove(vers->name, vers->name+1, strlen(vers->name));
239 		}
240 }
241 
242 /*
243  * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
244  * seem to be defined (value set later).
245  */
246 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
247 {
248 	unsigned int i;
249 
250 	for (i = 1; i < numsyms; i++) {
251 		if (syms[i].st_shndx == SHN_UNDEF) {
252 			char *name = strtab + syms[i].st_name;
253 			if (name[0] == '.') {
254 				if (strcmp(name+1, "TOC.") == 0)
255 					syms[i].st_shndx = SHN_ABS;
256 				syms[i].st_name++;
257 			}
258 		}
259 	}
260 }
261 
262 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
263 			       const char *strtab,
264 			       unsigned int symindex)
265 {
266 	unsigned int i, numsyms;
267 	Elf64_Sym *syms;
268 
269 	syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
270 	numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
271 
272 	for (i = 1; i < numsyms; i++) {
273 		if (syms[i].st_shndx == SHN_ABS
274 		    && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
275 			return &syms[i];
276 	}
277 	return NULL;
278 }
279 
280 int module_frob_arch_sections(Elf64_Ehdr *hdr,
281 			      Elf64_Shdr *sechdrs,
282 			      char *secstrings,
283 			      struct module *me)
284 {
285 	unsigned int i;
286 
287 	/* Find .toc and .stubs sections, symtab and strtab */
288 	for (i = 1; i < hdr->e_shnum; i++) {
289 		char *p;
290 		if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
291 			me->arch.stubs_section = i;
292 		else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0) {
293 			me->arch.toc_section = i;
294 			if (sechdrs[i].sh_addralign < 8)
295 				sechdrs[i].sh_addralign = 8;
296 		}
297 		else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
298 			dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
299 					  sechdrs[i].sh_size);
300 
301 		/* We don't handle .init for the moment: rename to _init */
302 		while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
303 			p[0] = '_';
304 
305 		if (sechdrs[i].sh_type == SHT_SYMTAB)
306 			dedotify((void *)hdr + sechdrs[i].sh_offset,
307 				 sechdrs[i].sh_size / sizeof(Elf64_Sym),
308 				 (void *)hdr
309 				 + sechdrs[sechdrs[i].sh_link].sh_offset);
310 	}
311 
312 	if (!me->arch.stubs_section) {
313 		pr_err("%s: doesn't contain .stubs.\n", me->name);
314 		return -ENOEXEC;
315 	}
316 
317 	/* If we don't have a .toc, just use .stubs.  We need to set r2
318 	   to some reasonable value in case the module calls out to
319 	   other functions via a stub, or if a function pointer escapes
320 	   the module by some means.  */
321 	if (!me->arch.toc_section)
322 		me->arch.toc_section = me->arch.stubs_section;
323 
324 	/* Override the stubs size */
325 	sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
326 	return 0;
327 }
328 
329 #ifdef CONFIG_MPROFILE_KERNEL
330 
331 static u32 stub_insns[] = {
332 	PPC_RAW_LD(_R12, _R13, offsetof(struct paca_struct, kernel_toc)),
333 	PPC_RAW_ADDIS(_R12, _R12, 0),
334 	PPC_RAW_ADDI(_R12, _R12, 0),
335 	PPC_RAW_MTCTR(_R12),
336 	PPC_RAW_BCTR(),
337 };
338 
339 /*
340  * For mprofile-kernel we use a special stub for ftrace_caller() because we
341  * can't rely on r2 containing this module's TOC when we enter the stub.
342  *
343  * That can happen if the function calling us didn't need to use the toc. In
344  * that case it won't have setup r2, and the r2 value will be either the
345  * kernel's toc, or possibly another modules toc.
346  *
347  * To deal with that this stub uses the kernel toc, which is always accessible
348  * via the paca (in r13). The target (ftrace_caller()) is responsible for
349  * saving and restoring the toc before returning.
350  */
351 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
352 					unsigned long addr,
353 					struct module *me)
354 {
355 	long reladdr;
356 
357 	memcpy(entry->jump, stub_insns, sizeof(stub_insns));
358 
359 	/* Stub uses address relative to kernel toc (from the paca) */
360 	reladdr = addr - kernel_toc_addr();
361 	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
362 		pr_err("%s: Address of %ps out of range of kernel_toc.\n",
363 							me->name, (void *)addr);
364 		return 0;
365 	}
366 
367 	entry->jump[1] |= PPC_HA(reladdr);
368 	entry->jump[2] |= PPC_LO(reladdr);
369 
370 	/* Eventhough we don't use funcdata in the stub, it's needed elsewhere. */
371 	entry->funcdata = func_desc(addr);
372 	entry->magic = STUB_MAGIC;
373 
374 	return 1;
375 }
376 
377 static bool is_mprofile_ftrace_call(const char *name)
378 {
379 	if (!strcmp("_mcount", name))
380 		return true;
381 #ifdef CONFIG_DYNAMIC_FTRACE
382 	if (!strcmp("ftrace_caller", name))
383 		return true;
384 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
385 	if (!strcmp("ftrace_regs_caller", name))
386 		return true;
387 #endif
388 #endif
389 
390 	return false;
391 }
392 #else
393 static inline int create_ftrace_stub(struct ppc64_stub_entry *entry,
394 					unsigned long addr,
395 					struct module *me)
396 {
397 	return 0;
398 }
399 
400 static bool is_mprofile_ftrace_call(const char *name)
401 {
402 	return false;
403 }
404 #endif
405 
406 /*
407  * r2 is the TOC pointer: it actually points 0x8000 into the TOC (this gives the
408  * value maximum span in an instruction which uses a signed offset). Round down
409  * to a 256 byte boundary for the odd case where we are setting up r2 without a
410  * .toc section.
411  */
412 static inline unsigned long my_r2(const Elf64_Shdr *sechdrs, struct module *me)
413 {
414 	return (sechdrs[me->arch.toc_section].sh_addr & ~0xfful) + 0x8000;
415 }
416 
417 /* Patch stub to reference function and correct r2 value. */
418 static inline int create_stub(const Elf64_Shdr *sechdrs,
419 			      struct ppc64_stub_entry *entry,
420 			      unsigned long addr,
421 			      struct module *me,
422 			      const char *name)
423 {
424 	long reladdr;
425 	func_desc_t desc;
426 	int i;
427 
428 	if (is_mprofile_ftrace_call(name))
429 		return create_ftrace_stub(entry, addr, me);
430 
431 	for (i = 0; i < sizeof(ppc64_stub_insns) / sizeof(u32); i++) {
432 		if (patch_instruction(&entry->jump[i],
433 				      ppc_inst(ppc64_stub_insns[i])))
434 			return 0;
435 	}
436 
437 	/* Stub uses address relative to r2. */
438 	reladdr = (unsigned long)entry - my_r2(sechdrs, me);
439 	if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
440 		pr_err("%s: Address %p of stub out of range of %p.\n",
441 		       me->name, (void *)reladdr, (void *)my_r2);
442 		return 0;
443 	}
444 	pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
445 
446 	if (patch_instruction(&entry->jump[0],
447 			      ppc_inst(entry->jump[0] | PPC_HA(reladdr))))
448 		return 0;
449 
450 	if (patch_instruction(&entry->jump[1],
451 			  ppc_inst(entry->jump[1] | PPC_LO(reladdr))))
452 		return 0;
453 
454 	// func_desc_t is 8 bytes if ABIv2, else 16 bytes
455 	desc = func_desc(addr);
456 	for (i = 0; i < sizeof(func_desc_t) / sizeof(u32); i++) {
457 		if (patch_instruction(((u32 *)&entry->funcdata) + i,
458 				      ppc_inst(((u32 *)(&desc))[i])))
459 			return 0;
460 	}
461 
462 	if (patch_instruction(&entry->magic, ppc_inst(STUB_MAGIC)))
463 		return 0;
464 
465 	return 1;
466 }
467 
468 /* Create stub to jump to function described in this OPD/ptr: we need the
469    stub to set up the TOC ptr (r2) for the function. */
470 static unsigned long stub_for_addr(const Elf64_Shdr *sechdrs,
471 				   unsigned long addr,
472 				   struct module *me,
473 				   const char *name)
474 {
475 	struct ppc64_stub_entry *stubs;
476 	unsigned int i, num_stubs;
477 
478 	num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
479 
480 	/* Find this stub, or if that fails, the next avail. entry */
481 	stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
482 	for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
483 		if (WARN_ON(i >= num_stubs))
484 			return 0;
485 
486 		if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
487 			return (unsigned long)&stubs[i];
488 	}
489 
490 	if (!create_stub(sechdrs, &stubs[i], addr, me, name))
491 		return 0;
492 
493 	return (unsigned long)&stubs[i];
494 }
495 
496 /* We expect a noop next: if it is, replace it with instruction to
497    restore r2. */
498 static int restore_r2(const char *name, u32 *instruction, struct module *me)
499 {
500 	u32 *prev_insn = instruction - 1;
501 
502 	if (is_mprofile_ftrace_call(name))
503 		return 1;
504 
505 	/*
506 	 * Make sure the branch isn't a sibling call.  Sibling calls aren't
507 	 * "link" branches and they don't return, so they don't need the r2
508 	 * restore afterwards.
509 	 */
510 	if (!instr_is_relative_link_branch(ppc_inst(*prev_insn)))
511 		return 1;
512 
513 	if (*instruction != PPC_RAW_NOP()) {
514 		pr_err("%s: Expected nop after call, got %08x at %pS\n",
515 			me->name, *instruction, instruction);
516 		return 0;
517 	}
518 
519 	/* ld r2,R2_STACK_OFFSET(r1) */
520 	if (patch_instruction(instruction, ppc_inst(PPC_INST_LD_TOC)))
521 		return 0;
522 
523 	return 1;
524 }
525 
526 int apply_relocate_add(Elf64_Shdr *sechdrs,
527 		       const char *strtab,
528 		       unsigned int symindex,
529 		       unsigned int relsec,
530 		       struct module *me)
531 {
532 	unsigned int i;
533 	Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
534 	Elf64_Sym *sym;
535 	unsigned long *location;
536 	unsigned long value;
537 
538 	pr_debug("Applying ADD relocate section %u to %u\n", relsec,
539 	       sechdrs[relsec].sh_info);
540 
541 	/* First time we're called, we can fix up .TOC. */
542 	if (!me->arch.toc_fixed) {
543 		sym = find_dot_toc(sechdrs, strtab, symindex);
544 		/* It's theoretically possible that a module doesn't want a
545 		 * .TOC. so don't fail it just for that. */
546 		if (sym)
547 			sym->st_value = my_r2(sechdrs, me);
548 		me->arch.toc_fixed = true;
549 	}
550 
551 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
552 		/* This is where to make the change */
553 		location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
554 			+ rela[i].r_offset;
555 		/* This is the symbol it is referring to */
556 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
557 			+ ELF64_R_SYM(rela[i].r_info);
558 
559 		pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
560 		       location, (long)ELF64_R_TYPE(rela[i].r_info),
561 		       strtab + sym->st_name, (unsigned long)sym->st_value,
562 		       (long)rela[i].r_addend);
563 
564 		/* `Everything is relative'. */
565 		value = sym->st_value + rela[i].r_addend;
566 
567 		switch (ELF64_R_TYPE(rela[i].r_info)) {
568 		case R_PPC64_ADDR32:
569 			/* Simply set it */
570 			*(u32 *)location = value;
571 			break;
572 
573 		case R_PPC64_ADDR64:
574 			/* Simply set it */
575 			*(unsigned long *)location = value;
576 			break;
577 
578 		case R_PPC64_TOC:
579 			*(unsigned long *)location = my_r2(sechdrs, me);
580 			break;
581 
582 		case R_PPC64_TOC16:
583 			/* Subtract TOC pointer */
584 			value -= my_r2(sechdrs, me);
585 			if (value + 0x8000 > 0xffff) {
586 				pr_err("%s: bad TOC16 relocation (0x%lx)\n",
587 				       me->name, value);
588 				return -ENOEXEC;
589 			}
590 			*((uint16_t *) location)
591 				= (*((uint16_t *) location) & ~0xffff)
592 				| (value & 0xffff);
593 			break;
594 
595 		case R_PPC64_TOC16_LO:
596 			/* Subtract TOC pointer */
597 			value -= my_r2(sechdrs, me);
598 			*((uint16_t *) location)
599 				= (*((uint16_t *) location) & ~0xffff)
600 				| (value & 0xffff);
601 			break;
602 
603 		case R_PPC64_TOC16_DS:
604 			/* Subtract TOC pointer */
605 			value -= my_r2(sechdrs, me);
606 			if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
607 				pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
608 				       me->name, value);
609 				return -ENOEXEC;
610 			}
611 			*((uint16_t *) location)
612 				= (*((uint16_t *) location) & ~0xfffc)
613 				| (value & 0xfffc);
614 			break;
615 
616 		case R_PPC64_TOC16_LO_DS:
617 			/* Subtract TOC pointer */
618 			value -= my_r2(sechdrs, me);
619 			if ((value & 3) != 0) {
620 				pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
621 				       me->name, value);
622 				return -ENOEXEC;
623 			}
624 			*((uint16_t *) location)
625 				= (*((uint16_t *) location) & ~0xfffc)
626 				| (value & 0xfffc);
627 			break;
628 
629 		case R_PPC64_TOC16_HA:
630 			/* Subtract TOC pointer */
631 			value -= my_r2(sechdrs, me);
632 			value = ((value + 0x8000) >> 16);
633 			*((uint16_t *) location)
634 				= (*((uint16_t *) location) & ~0xffff)
635 				| (value & 0xffff);
636 			break;
637 
638 		case R_PPC_REL24:
639 			/* FIXME: Handle weak symbols here --RR */
640 			if (sym->st_shndx == SHN_UNDEF ||
641 			    sym->st_shndx == SHN_LIVEPATCH) {
642 				/* External: go via stub */
643 				value = stub_for_addr(sechdrs, value, me,
644 						strtab + sym->st_name);
645 				if (!value)
646 					return -ENOENT;
647 				if (!restore_r2(strtab + sym->st_name,
648 							(u32 *)location + 1, me))
649 					return -ENOEXEC;
650 			} else
651 				value += local_entry_offset(sym);
652 
653 			/* Convert value to relative */
654 			value -= (unsigned long)location;
655 			if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
656 				pr_err("%s: REL24 %li out of range!\n",
657 				       me->name, (long int)value);
658 				return -ENOEXEC;
659 			}
660 
661 			/* Only replace bits 2 through 26 */
662 			value = (*(uint32_t *)location & ~0x03fffffc)
663 				| (value & 0x03fffffc);
664 
665 			if (patch_instruction((u32 *)location, ppc_inst(value)))
666 				return -EFAULT;
667 
668 			break;
669 
670 		case R_PPC64_REL64:
671 			/* 64 bits relative (used by features fixups) */
672 			*location = value - (unsigned long)location;
673 			break;
674 
675 		case R_PPC64_REL32:
676 			/* 32 bits relative (used by relative exception tables) */
677 			/* Convert value to relative */
678 			value -= (unsigned long)location;
679 			if (value + 0x80000000 > 0xffffffff) {
680 				pr_err("%s: REL32 %li out of range!\n",
681 				       me->name, (long int)value);
682 				return -ENOEXEC;
683 			}
684 			*(u32 *)location = value;
685 			break;
686 
687 		case R_PPC64_TOCSAVE:
688 			/*
689 			 * Marker reloc indicates we don't have to save r2.
690 			 * That would only save us one instruction, so ignore
691 			 * it.
692 			 */
693 			break;
694 
695 		case R_PPC64_ENTRY:
696 			/*
697 			 * Optimize ELFv2 large code model entry point if
698 			 * the TOC is within 2GB range of current location.
699 			 */
700 			value = my_r2(sechdrs, me) - (unsigned long)location;
701 			if (value + 0x80008000 > 0xffffffff)
702 				break;
703 			/*
704 			 * Check for the large code model prolog sequence:
705 		         *	ld r2, ...(r12)
706 			 *	add r2, r2, r12
707 			 */
708 			if ((((uint32_t *)location)[0] & ~0xfffc) != PPC_RAW_LD(_R2, _R12, 0))
709 				break;
710 			if (((uint32_t *)location)[1] != PPC_RAW_ADD(_R2, _R2, _R12))
711 				break;
712 			/*
713 			 * If found, replace it with:
714 			 *	addis r2, r12, (.TOC.-func)@ha
715 			 *	addi  r2,  r2, (.TOC.-func)@l
716 			 */
717 			((uint32_t *)location)[0] = PPC_RAW_ADDIS(_R2, _R12, PPC_HA(value));
718 			((uint32_t *)location)[1] = PPC_RAW_ADDI(_R2, _R2, PPC_LO(value));
719 			break;
720 
721 		case R_PPC64_REL16_HA:
722 			/* Subtract location pointer */
723 			value -= (unsigned long)location;
724 			value = ((value + 0x8000) >> 16);
725 			*((uint16_t *) location)
726 				= (*((uint16_t *) location) & ~0xffff)
727 				| (value & 0xffff);
728 			break;
729 
730 		case R_PPC64_REL16_LO:
731 			/* Subtract location pointer */
732 			value -= (unsigned long)location;
733 			*((uint16_t *) location)
734 				= (*((uint16_t *) location) & ~0xffff)
735 				| (value & 0xffff);
736 			break;
737 
738 		default:
739 			pr_err("%s: Unknown ADD relocation: %lu\n",
740 			       me->name,
741 			       (unsigned long)ELF64_R_TYPE(rela[i].r_info));
742 			return -ENOEXEC;
743 		}
744 	}
745 
746 	return 0;
747 }
748 
749 #ifdef CONFIG_DYNAMIC_FTRACE
750 int module_trampoline_target(struct module *mod, unsigned long addr,
751 			     unsigned long *target)
752 {
753 	struct ppc64_stub_entry *stub;
754 	func_desc_t funcdata;
755 	u32 magic;
756 
757 	if (!within_module_core(addr, mod)) {
758 		pr_err("%s: stub %lx not in module %s\n", __func__, addr, mod->name);
759 		return -EFAULT;
760 	}
761 
762 	stub = (struct ppc64_stub_entry *)addr;
763 
764 	if (copy_from_kernel_nofault(&magic, &stub->magic,
765 			sizeof(magic))) {
766 		pr_err("%s: fault reading magic for stub %lx for %s\n", __func__, addr, mod->name);
767 		return -EFAULT;
768 	}
769 
770 	if (magic != STUB_MAGIC) {
771 		pr_err("%s: bad magic for stub %lx for %s\n", __func__, addr, mod->name);
772 		return -EFAULT;
773 	}
774 
775 	if (copy_from_kernel_nofault(&funcdata, &stub->funcdata,
776 			sizeof(funcdata))) {
777 		pr_err("%s: fault reading funcdata for stub %lx for %s\n", __func__, addr, mod->name);
778                 return -EFAULT;
779 	}
780 
781 	*target = stub_func_addr(funcdata);
782 
783 	return 0;
784 }
785 
786 int module_finalize_ftrace(struct module *mod, const Elf_Shdr *sechdrs)
787 {
788 	mod->arch.tramp = stub_for_addr(sechdrs,
789 					(unsigned long)ftrace_caller,
790 					mod,
791 					"ftrace_caller");
792 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
793 	mod->arch.tramp_regs = stub_for_addr(sechdrs,
794 					(unsigned long)ftrace_regs_caller,
795 					mod,
796 					"ftrace_regs_caller");
797 	if (!mod->arch.tramp_regs)
798 		return -ENOENT;
799 #endif
800 
801 	if (!mod->arch.tramp)
802 		return -ENOENT;
803 
804 	return 0;
805 }
806 #endif
807