1 /* Kernel module help for PPC64.
2 Copyright (C) 2001, 2003 Rusty Russell IBM Corporation.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/module.h>
22 #include <linux/elf.h>
23 #include <linux/moduleloader.h>
24 #include <linux/err.h>
25 #include <linux/vmalloc.h>
26 #include <linux/ftrace.h>
27 #include <linux/bug.h>
28 #include <linux/uaccess.h>
29 #include <asm/module.h>
30 #include <asm/firmware.h>
31 #include <asm/code-patching.h>
32 #include <linux/sort.h>
33 #include <asm/setup.h>
34
35 /* FIXME: We don't do .init separately. To do this, we'd need to have
36 a separate r2 value in the init and core section, and stub between
37 them, too.
38
39 Using a magic allocator which places modules within 32MB solves
40 this, and makes other things simpler. Anton?
41 --RR. */
42
43 #if defined(_CALL_ELF) && _CALL_ELF == 2
44 #define R2_STACK_OFFSET 24
45
46 /* An address is simply the address of the function. */
47 typedef unsigned long func_desc_t;
48
func_desc(unsigned long addr)49 static func_desc_t func_desc(unsigned long addr)
50 {
51 return addr;
52 }
func_addr(unsigned long addr)53 static unsigned long func_addr(unsigned long addr)
54 {
55 return addr;
56 }
stub_func_addr(func_desc_t func)57 static unsigned long stub_func_addr(func_desc_t func)
58 {
59 return func;
60 }
61
62 /* PowerPC64 specific values for the Elf64_Sym st_other field. */
63 #define STO_PPC64_LOCAL_BIT 5
64 #define STO_PPC64_LOCAL_MASK (7 << STO_PPC64_LOCAL_BIT)
65 #define PPC64_LOCAL_ENTRY_OFFSET(other) \
66 (((1 << (((other) & STO_PPC64_LOCAL_MASK) >> STO_PPC64_LOCAL_BIT)) >> 2) << 2)
67
local_entry_offset(const Elf64_Sym * sym)68 static unsigned int local_entry_offset(const Elf64_Sym *sym)
69 {
70 /* sym->st_other indicates offset to local entry point
71 * (otherwise it will assume r12 is the address of the start
72 * of function and try to derive r2 from it). */
73 return PPC64_LOCAL_ENTRY_OFFSET(sym->st_other);
74 }
75 #else
76 #define R2_STACK_OFFSET 40
77
78 /* An address is address of the OPD entry, which contains address of fn. */
79 typedef struct ppc64_opd_entry func_desc_t;
80
func_desc(unsigned long addr)81 static func_desc_t func_desc(unsigned long addr)
82 {
83 return *(struct ppc64_opd_entry *)addr;
84 }
func_addr(unsigned long addr)85 static unsigned long func_addr(unsigned long addr)
86 {
87 return func_desc(addr).funcaddr;
88 }
stub_func_addr(func_desc_t func)89 static unsigned long stub_func_addr(func_desc_t func)
90 {
91 return func.funcaddr;
92 }
local_entry_offset(const Elf64_Sym * sym)93 static unsigned int local_entry_offset(const Elf64_Sym *sym)
94 {
95 return 0;
96 }
97 #endif
98
99 /* Like PPC32, we need little trampolines to do > 24-bit jumps (into
100 the kernel itself). But on PPC64, these need to be used for every
101 jump, actually, to reset r2 (TOC+0x8000). */
102 struct ppc64_stub_entry
103 {
104 /* 28 byte jump instruction sequence (7 instructions). We only
105 * need 6 instructions on ABIv2 but we always allocate 7 so
106 * so we don't have to modify the trampoline load instruction. */
107 u32 jump[7];
108 u32 unused;
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
127 static u32 ppc64_stub_insns[] = {
128 0x3d620000, /* addis r11,r2, <high> */
129 0x396b0000, /* addi r11,r11, <low> */
130 /* Save current r2 value in magic place on the stack. */
131 0xf8410000|R2_STACK_OFFSET, /* std r2,R2_STACK_OFFSET(r1) */
132 0xe98b0020, /* ld r12,32(r11) */
133 #if !defined(_CALL_ELF) || _CALL_ELF != 2
134 /* Set up new r2 from function descriptor */
135 0xe84b0028, /* ld r2,40(r11) */
136 #endif
137 0x7d8903a6, /* mtctr r12 */
138 0x4e800420 /* bctr */
139 };
140
141 #ifdef CONFIG_DYNAMIC_FTRACE
142
143 static u32 ppc64_stub_mask[] = {
144 0xffff0000,
145 0xffff0000,
146 0xffffffff,
147 0xffffffff,
148 #if !defined(_CALL_ELF) || _CALL_ELF != 2
149 0xffffffff,
150 #endif
151 0xffffffff,
152 0xffffffff
153 };
154
is_module_trampoline(u32 * p)155 bool is_module_trampoline(u32 *p)
156 {
157 unsigned int i;
158 u32 insns[ARRAY_SIZE(ppc64_stub_insns)];
159
160 BUILD_BUG_ON(sizeof(ppc64_stub_insns) != sizeof(ppc64_stub_mask));
161
162 if (probe_kernel_read(insns, p, sizeof(insns)))
163 return -EFAULT;
164
165 for (i = 0; i < ARRAY_SIZE(ppc64_stub_insns); i++) {
166 u32 insna = insns[i];
167 u32 insnb = ppc64_stub_insns[i];
168 u32 mask = ppc64_stub_mask[i];
169
170 if ((insna & mask) != (insnb & mask))
171 return false;
172 }
173
174 return true;
175 }
176
module_trampoline_target(struct module * mod,u32 * trampoline,unsigned long * target)177 int module_trampoline_target(struct module *mod, u32 *trampoline,
178 unsigned long *target)
179 {
180 u32 buf[2];
181 u16 upper, lower;
182 long offset;
183 void *toc_entry;
184
185 if (probe_kernel_read(buf, trampoline, sizeof(buf)))
186 return -EFAULT;
187
188 upper = buf[0] & 0xffff;
189 lower = buf[1] & 0xffff;
190
191 /* perform the addis/addi, both signed */
192 offset = ((short)upper << 16) + (short)lower;
193
194 /*
195 * Now get the address this trampoline jumps to. This
196 * is always 32 bytes into our trampoline stub.
197 */
198 toc_entry = (void *)mod->arch.toc + offset + 32;
199
200 if (probe_kernel_read(target, toc_entry, sizeof(*target)))
201 return -EFAULT;
202
203 return 0;
204 }
205
206 #endif
207
208 /* Count how many different 24-bit relocations (different symbol,
209 different addend) */
count_relocs(const Elf64_Rela * rela,unsigned int num)210 static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
211 {
212 unsigned int i, r_info, r_addend, _count_relocs;
213
214 /* FIXME: Only count external ones --RR */
215 _count_relocs = 0;
216 r_info = 0;
217 r_addend = 0;
218 for (i = 0; i < num; i++)
219 /* Only count 24-bit relocs, others don't need stubs */
220 if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
221 (r_info != ELF64_R_SYM(rela[i].r_info) ||
222 r_addend != rela[i].r_addend)) {
223 _count_relocs++;
224 r_info = ELF64_R_SYM(rela[i].r_info);
225 r_addend = rela[i].r_addend;
226 }
227
228 return _count_relocs;
229 }
230
relacmp(const void * _x,const void * _y)231 static int relacmp(const void *_x, const void *_y)
232 {
233 const Elf64_Rela *x, *y;
234
235 y = (Elf64_Rela *)_x;
236 x = (Elf64_Rela *)_y;
237
238 /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
239 * make the comparison cheaper/faster. It won't affect the sorting or
240 * the counting algorithms' performance
241 */
242 if (x->r_info < y->r_info)
243 return -1;
244 else if (x->r_info > y->r_info)
245 return 1;
246 else if (x->r_addend < y->r_addend)
247 return -1;
248 else if (x->r_addend > y->r_addend)
249 return 1;
250 else
251 return 0;
252 }
253
relaswap(void * _x,void * _y,int size)254 static void relaswap(void *_x, void *_y, int size)
255 {
256 uint64_t *x, *y, tmp;
257 int i;
258
259 y = (uint64_t *)_x;
260 x = (uint64_t *)_y;
261
262 for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) {
263 tmp = x[i];
264 x[i] = y[i];
265 y[i] = tmp;
266 }
267 }
268
269 /* Get size of potential trampolines required. */
get_stubs_size(const Elf64_Ehdr * hdr,const Elf64_Shdr * sechdrs)270 static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
271 const Elf64_Shdr *sechdrs)
272 {
273 /* One extra reloc so it's always 0-funcaddr terminated */
274 unsigned long relocs = 1;
275 unsigned i;
276
277 /* Every relocated section... */
278 for (i = 1; i < hdr->e_shnum; i++) {
279 if (sechdrs[i].sh_type == SHT_RELA) {
280 pr_debug("Found relocations in section %u\n", i);
281 pr_debug("Ptr: %p. Number: %Lu\n",
282 (void *)sechdrs[i].sh_addr,
283 sechdrs[i].sh_size / sizeof(Elf64_Rela));
284
285 /* Sort the relocation information based on a symbol and
286 * addend key. This is a stable O(n*log n) complexity
287 * alogrithm but it will reduce the complexity of
288 * count_relocs() to linear complexity O(n)
289 */
290 sort((void *)sechdrs[i].sh_addr,
291 sechdrs[i].sh_size / sizeof(Elf64_Rela),
292 sizeof(Elf64_Rela), relacmp, relaswap);
293
294 relocs += count_relocs((void *)sechdrs[i].sh_addr,
295 sechdrs[i].sh_size
296 / sizeof(Elf64_Rela));
297 }
298 }
299
300 #ifdef CONFIG_DYNAMIC_FTRACE
301 /* make the trampoline to the ftrace_caller */
302 relocs++;
303 #endif
304
305 pr_debug("Looks like a total of %lu stubs, max\n", relocs);
306 return relocs * sizeof(struct ppc64_stub_entry);
307 }
308
309 /* Still needed for ELFv2, for .TOC. */
dedotify_versions(struct modversion_info * vers,unsigned long size)310 static void dedotify_versions(struct modversion_info *vers,
311 unsigned long size)
312 {
313 struct modversion_info *end;
314
315 for (end = (void *)vers + size; vers < end; vers++)
316 if (vers->name[0] == '.') {
317 memmove(vers->name, vers->name+1, strlen(vers->name));
318 #ifdef ARCH_RELOCATES_KCRCTAB
319 /* The TOC symbol has no CRC computed. To avoid CRC
320 * check failing, we must force it to the expected
321 * value (see CRC check in module.c).
322 */
323 if (!strcmp(vers->name, "TOC."))
324 vers->crc = -(unsigned long)reloc_start;
325 #endif
326 }
327 }
328
329 /*
330 * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC.
331 * seem to be defined (value set later).
332 */
dedotify(Elf64_Sym * syms,unsigned int numsyms,char * strtab)333 static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
334 {
335 unsigned int i;
336
337 for (i = 1; i < numsyms; i++) {
338 if (syms[i].st_shndx == SHN_UNDEF) {
339 char *name = strtab + syms[i].st_name;
340 if (name[0] == '.') {
341 if (strcmp(name+1, "TOC.") == 0)
342 syms[i].st_shndx = SHN_ABS;
343 syms[i].st_name++;
344 }
345 }
346 }
347 }
348
find_dot_toc(Elf64_Shdr * sechdrs,const char * strtab,unsigned int symindex)349 static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
350 const char *strtab,
351 unsigned int symindex)
352 {
353 unsigned int i, numsyms;
354 Elf64_Sym *syms;
355
356 syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
357 numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
358
359 for (i = 1; i < numsyms; i++) {
360 if (syms[i].st_shndx == SHN_ABS
361 && strcmp(strtab + syms[i].st_name, "TOC.") == 0)
362 return &syms[i];
363 }
364 return NULL;
365 }
366
module_frob_arch_sections(Elf64_Ehdr * hdr,Elf64_Shdr * sechdrs,char * secstrings,struct module * me)367 int module_frob_arch_sections(Elf64_Ehdr *hdr,
368 Elf64_Shdr *sechdrs,
369 char *secstrings,
370 struct module *me)
371 {
372 unsigned int i;
373
374 /* Find .toc and .stubs sections, symtab and strtab */
375 for (i = 1; i < hdr->e_shnum; i++) {
376 char *p;
377 if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
378 me->arch.stubs_section = i;
379 else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0)
380 me->arch.toc_section = i;
381 else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
382 dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
383 sechdrs[i].sh_size);
384
385 /* We don't handle .init for the moment: rename to _init */
386 while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
387 p[0] = '_';
388
389 if (sechdrs[i].sh_type == SHT_SYMTAB)
390 dedotify((void *)hdr + sechdrs[i].sh_offset,
391 sechdrs[i].sh_size / sizeof(Elf64_Sym),
392 (void *)hdr
393 + sechdrs[sechdrs[i].sh_link].sh_offset);
394 }
395
396 if (!me->arch.stubs_section) {
397 pr_err("%s: doesn't contain .stubs.\n", me->name);
398 return -ENOEXEC;
399 }
400
401 /* If we don't have a .toc, just use .stubs. We need to set r2
402 to some reasonable value in case the module calls out to
403 other functions via a stub, or if a function pointer escapes
404 the module by some means. */
405 if (!me->arch.toc_section)
406 me->arch.toc_section = me->arch.stubs_section;
407
408 /* Override the stubs size */
409 sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
410 return 0;
411 }
412
413 /* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
414 gives the value maximum span in an instruction which uses a signed
415 offset) */
my_r2(Elf64_Shdr * sechdrs,struct module * me)416 static inline unsigned long my_r2(Elf64_Shdr *sechdrs, struct module *me)
417 {
418 return sechdrs[me->arch.toc_section].sh_addr + 0x8000;
419 }
420
421 /* Both low and high 16 bits are added as SIGNED additions, so if low
422 16 bits has high bit set, high 16 bits must be adjusted. These
423 macros do that (stolen from binutils). */
424 #define PPC_LO(v) ((v) & 0xffff)
425 #define PPC_HI(v) (((v) >> 16) & 0xffff)
426 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
427
428 /* Patch stub to reference function and correct r2 value. */
create_stub(Elf64_Shdr * sechdrs,struct ppc64_stub_entry * entry,unsigned long addr,struct module * me)429 static inline int create_stub(Elf64_Shdr *sechdrs,
430 struct ppc64_stub_entry *entry,
431 unsigned long addr,
432 struct module *me)
433 {
434 long reladdr;
435
436 memcpy(entry->jump, ppc64_stub_insns, sizeof(ppc64_stub_insns));
437
438 /* Stub uses address relative to r2. */
439 reladdr = (unsigned long)entry - my_r2(sechdrs, me);
440 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
441 pr_err("%s: Address %p of stub out of range of %p.\n",
442 me->name, (void *)reladdr, (void *)my_r2);
443 return 0;
444 }
445 pr_debug("Stub %p get data from reladdr %li\n", entry, reladdr);
446
447 entry->jump[0] |= PPC_HA(reladdr);
448 entry->jump[1] |= PPC_LO(reladdr);
449 entry->funcdata = func_desc(addr);
450 return 1;
451 }
452
453 /* Create stub to jump to function described in this OPD/ptr: we need the
454 stub to set up the TOC ptr (r2) for the function. */
stub_for_addr(Elf64_Shdr * sechdrs,unsigned long addr,struct module * me)455 static unsigned long stub_for_addr(Elf64_Shdr *sechdrs,
456 unsigned long addr,
457 struct module *me)
458 {
459 struct ppc64_stub_entry *stubs;
460 unsigned int i, num_stubs;
461
462 num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
463
464 /* Find this stub, or if that fails, the next avail. entry */
465 stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
466 for (i = 0; stub_func_addr(stubs[i].funcdata); i++) {
467 BUG_ON(i >= num_stubs);
468
469 if (stub_func_addr(stubs[i].funcdata) == func_addr(addr))
470 return (unsigned long)&stubs[i];
471 }
472
473 if (!create_stub(sechdrs, &stubs[i], addr, me))
474 return 0;
475
476 return (unsigned long)&stubs[i];
477 }
478
479 /* We expect a noop next: if it is, replace it with instruction to
480 restore r2. */
restore_r2(u32 * instruction,struct module * me)481 static int restore_r2(u32 *instruction, struct module *me)
482 {
483 if (*instruction != PPC_INST_NOP) {
484 pr_err("%s: Expect noop after relocate, got %08x\n",
485 me->name, *instruction);
486 return 0;
487 }
488 /* ld r2,R2_STACK_OFFSET(r1) */
489 *instruction = 0xe8410000 | R2_STACK_OFFSET;
490 return 1;
491 }
492
apply_relocate_add(Elf64_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)493 int apply_relocate_add(Elf64_Shdr *sechdrs,
494 const char *strtab,
495 unsigned int symindex,
496 unsigned int relsec,
497 struct module *me)
498 {
499 unsigned int i;
500 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
501 Elf64_Sym *sym;
502 unsigned long *location;
503 unsigned long value;
504
505 pr_debug("Applying ADD relocate section %u to %u\n", relsec,
506 sechdrs[relsec].sh_info);
507
508 /* First time we're called, we can fix up .TOC. */
509 if (!me->arch.toc_fixed) {
510 sym = find_dot_toc(sechdrs, strtab, symindex);
511 /* It's theoretically possible that a module doesn't want a
512 * .TOC. so don't fail it just for that. */
513 if (sym)
514 sym->st_value = my_r2(sechdrs, me);
515 me->arch.toc_fixed = true;
516 }
517
518 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
519 /* This is where to make the change */
520 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
521 + rela[i].r_offset;
522 /* This is the symbol it is referring to */
523 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
524 + ELF64_R_SYM(rela[i].r_info);
525
526 pr_debug("RELOC at %p: %li-type as %s (0x%lx) + %li\n",
527 location, (long)ELF64_R_TYPE(rela[i].r_info),
528 strtab + sym->st_name, (unsigned long)sym->st_value,
529 (long)rela[i].r_addend);
530
531 /* `Everything is relative'. */
532 value = sym->st_value + rela[i].r_addend;
533
534 switch (ELF64_R_TYPE(rela[i].r_info)) {
535 case R_PPC64_ADDR32:
536 /* Simply set it */
537 *(u32 *)location = value;
538 break;
539
540 case R_PPC64_ADDR64:
541 /* Simply set it */
542 *(unsigned long *)location = value;
543 break;
544
545 case R_PPC64_TOC:
546 *(unsigned long *)location = my_r2(sechdrs, me);
547 break;
548
549 case R_PPC64_TOC16:
550 /* Subtract TOC pointer */
551 value -= my_r2(sechdrs, me);
552 if (value + 0x8000 > 0xffff) {
553 pr_err("%s: bad TOC16 relocation (0x%lx)\n",
554 me->name, value);
555 return -ENOEXEC;
556 }
557 *((uint16_t *) location)
558 = (*((uint16_t *) location) & ~0xffff)
559 | (value & 0xffff);
560 break;
561
562 case R_PPC64_TOC16_LO:
563 /* Subtract TOC pointer */
564 value -= my_r2(sechdrs, me);
565 *((uint16_t *) location)
566 = (*((uint16_t *) location) & ~0xffff)
567 | (value & 0xffff);
568 break;
569
570 case R_PPC64_TOC16_DS:
571 /* Subtract TOC pointer */
572 value -= my_r2(sechdrs, me);
573 if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
574 pr_err("%s: bad TOC16_DS relocation (0x%lx)\n",
575 me->name, value);
576 return -ENOEXEC;
577 }
578 *((uint16_t *) location)
579 = (*((uint16_t *) location) & ~0xfffc)
580 | (value & 0xfffc);
581 break;
582
583 case R_PPC64_TOC16_LO_DS:
584 /* Subtract TOC pointer */
585 value -= my_r2(sechdrs, me);
586 if ((value & 3) != 0) {
587 pr_err("%s: bad TOC16_LO_DS relocation (0x%lx)\n",
588 me->name, value);
589 return -ENOEXEC;
590 }
591 *((uint16_t *) location)
592 = (*((uint16_t *) location) & ~0xfffc)
593 | (value & 0xfffc);
594 break;
595
596 case R_PPC64_TOC16_HA:
597 /* Subtract TOC pointer */
598 value -= my_r2(sechdrs, me);
599 value = ((value + 0x8000) >> 16);
600 *((uint16_t *) location)
601 = (*((uint16_t *) location) & ~0xffff)
602 | (value & 0xffff);
603 break;
604
605 case R_PPC_REL24:
606 /* FIXME: Handle weak symbols here --RR */
607 if (sym->st_shndx == SHN_UNDEF) {
608 /* External: go via stub */
609 value = stub_for_addr(sechdrs, value, me);
610 if (!value)
611 return -ENOENT;
612 if (!restore_r2((u32 *)location + 1, me))
613 return -ENOEXEC;
614 } else
615 value += local_entry_offset(sym);
616
617 /* Convert value to relative */
618 value -= (unsigned long)location;
619 if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
620 pr_err("%s: REL24 %li out of range!\n",
621 me->name, (long int)value);
622 return -ENOEXEC;
623 }
624
625 /* Only replace bits 2 through 26 */
626 *(uint32_t *)location
627 = (*(uint32_t *)location & ~0x03fffffc)
628 | (value & 0x03fffffc);
629 break;
630
631 case R_PPC64_REL64:
632 /* 64 bits relative (used by features fixups) */
633 *location = value - (unsigned long)location;
634 break;
635
636 case R_PPC64_TOCSAVE:
637 /*
638 * Marker reloc indicates we don't have to save r2.
639 * That would only save us one instruction, so ignore
640 * it.
641 */
642 break;
643
644 case R_PPC64_ENTRY:
645 /*
646 * Optimize ELFv2 large code model entry point if
647 * the TOC is within 2GB range of current location.
648 */
649 value = my_r2(sechdrs, me) - (unsigned long)location;
650 if (value + 0x80008000 > 0xffffffff)
651 break;
652 /*
653 * Check for the large code model prolog sequence:
654 * ld r2, ...(r12)
655 * add r2, r2, r12
656 */
657 if ((((uint32_t *)location)[0] & ~0xfffc)
658 != 0xe84c0000)
659 break;
660 if (((uint32_t *)location)[1] != 0x7c426214)
661 break;
662 /*
663 * If found, replace it with:
664 * addis r2, r12, (.TOC.-func)@ha
665 * addi r2, r2, (.TOC.-func)@l
666 */
667 ((uint32_t *)location)[0] = 0x3c4c0000 + PPC_HA(value);
668 ((uint32_t *)location)[1] = 0x38420000 + PPC_LO(value);
669 break;
670
671 case R_PPC64_REL16_HA:
672 /* Subtract location pointer */
673 value -= (unsigned long)location;
674 value = ((value + 0x8000) >> 16);
675 *((uint16_t *) location)
676 = (*((uint16_t *) location) & ~0xffff)
677 | (value & 0xffff);
678 break;
679
680 case R_PPC64_REL16_LO:
681 /* Subtract location pointer */
682 value -= (unsigned long)location;
683 *((uint16_t *) location)
684 = (*((uint16_t *) location) & ~0xffff)
685 | (value & 0xffff);
686 break;
687
688 default:
689 pr_err("%s: Unknown ADD relocation: %lu\n",
690 me->name,
691 (unsigned long)ELF64_R_TYPE(rela[i].r_info));
692 return -ENOEXEC;
693 }
694 }
695
696 #ifdef CONFIG_DYNAMIC_FTRACE
697 me->arch.toc = my_r2(sechdrs, me);
698 me->arch.tramp = stub_for_addr(sechdrs,
699 (unsigned long)ftrace_caller,
700 me);
701 #endif
702
703 return 0;
704 }
705