]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - arch/powerpc/kernel/module_64.c
powerpc: modules: skip r2 setup for ELFv2
[mirror_ubuntu-zesty-kernel.git] / arch / powerpc / kernel / module_64.c
CommitLineData
1da177e4
LT
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#include <linux/module.h>
19#include <linux/elf.h>
20#include <linux/moduleloader.h>
21#include <linux/err.h>
22#include <linux/vmalloc.h>
f48cb8b4 23#include <linux/ftrace.h>
73c9ceab 24#include <linux/bug.h>
1da177e4 25#include <asm/module.h>
21c4ff80 26#include <asm/firmware.h>
b7bcda63 27#include <asm/code-patching.h>
eda09fbd 28#include <linux/sort.h>
b88c4767 29#include <asm/setup.h>
1da177e4
LT
30
31/* FIXME: We don't do .init separately. To do this, we'd need to have
32 a separate r2 value in the init and core section, and stub between
33 them, too.
34
35 Using a magic allocator which places modules within 32MB solves
36 this, and makes other things simpler. Anton?
37 --RR. */
38#if 0
39#define DEBUGP printk
40#else
41#define DEBUGP(fmt , ...)
42#endif
43
d2fae548
RR
44#if defined(_CALL_ELF) && _CALL_ELF == 2
45#define R2_STACK_OFFSET 24
46#else
47#define R2_STACK_OFFSET 40
48#endif
49
1da177e4
LT
50/* Like PPC32, we need little trampolines to do > 24-bit jumps (into
51 the kernel itself). But on PPC64, these need to be used for every
52 jump, actually, to reset r2 (TOC+0x8000). */
53struct ppc64_stub_entry
54{
55 /* 28 byte jump instruction sequence (7 instructions) */
0e60e46e
RR
56 u32 jump[7];
57 u32 unused;
1da177e4
LT
58 /* Data for the above code */
59 struct ppc64_opd_entry opd;
60};
61
5c729a11
RR
62/*
63 * PPC64 uses 24 bit jumps, but we need to jump into other modules or
64 * the kernel which may be further. So we jump to a stub.
65 *
66 * For ELFv1 we need to use this to set up the new r2 value (aka TOC
67 * pointer). For ELFv2 it's the callee's responsibility to set up the
68 * new r2, but for both we need to save the old r2.
69 *
70 * We could simply patch the new r2 value and function pointer into
71 * the stub, but it's significantly shorter to put these values at the
72 * end of the stub code, and patch the stub address (32-bits relative
73 * to the TOC ptr, r2) into the stub.
74 */
1da177e4
LT
75static struct ppc64_stub_entry ppc64_stub =
76{ .jump = {
b1ce369e
RR
77 0x3d620000, /* addis r11,r2, <high> */
78 0x396b0000, /* addi r11,r11, <low> */
fed8393e 79 /* Save current r2 value in magic place on the stack. */
d2fae548 80 0xf8410000|R2_STACK_OFFSET, /* std r2,R2_STACK_OFFSET(r1) */
b1ce369e 81 0xe98b0020, /* ld r12,32(r11) */
5c729a11
RR
82#if !defined(_CALL_ELF) || _CALL_ELF != 2
83 /* Set up new r2 from function descriptor */
b1ce369e 84 0xe84b0026, /* ld r2,40(r11) */
5c729a11 85#endif
b1ce369e 86 0x7d8903a6, /* mtctr r12 */
d2fae548 87 0x4e800420 /* bctr */
1da177e4
LT
88} };
89
90/* Count how many different 24-bit relocations (different symbol,
91 different addend) */
92static unsigned int count_relocs(const Elf64_Rela *rela, unsigned int num)
93{
eda09fbd 94 unsigned int i, r_info, r_addend, _count_relocs;
1da177e4
LT
95
96 /* FIXME: Only count external ones --RR */
eda09fbd
EM
97 _count_relocs = 0;
98 r_info = 0;
99 r_addend = 0;
100 for (i = 0; i < num; i++)
1da177e4 101 /* Only count 24-bit relocs, others don't need stubs */
eda09fbd
EM
102 if (ELF64_R_TYPE(rela[i].r_info) == R_PPC_REL24 &&
103 (r_info != ELF64_R_SYM(rela[i].r_info) ||
104 r_addend != rela[i].r_addend)) {
105 _count_relocs++;
106 r_info = ELF64_R_SYM(rela[i].r_info);
107 r_addend = rela[i].r_addend;
1da177e4 108 }
eda09fbd
EM
109
110 return _count_relocs;
1da177e4
LT
111}
112
eda09fbd
EM
113static int relacmp(const void *_x, const void *_y)
114{
115 const Elf64_Rela *x, *y;
116
117 y = (Elf64_Rela *)_x;
118 x = (Elf64_Rela *)_y;
119
120 /* Compare the entire r_info (as opposed to ELF64_R_SYM(r_info) only) to
121 * make the comparison cheaper/faster. It won't affect the sorting or
122 * the counting algorithms' performance
123 */
124 if (x->r_info < y->r_info)
125 return -1;
126 else if (x->r_info > y->r_info)
127 return 1;
128 else if (x->r_addend < y->r_addend)
129 return -1;
130 else if (x->r_addend > y->r_addend)
131 return 1;
132 else
133 return 0;
134}
135
136static void relaswap(void *_x, void *_y, int size)
137{
138 uint64_t *x, *y, tmp;
139 int i;
140
141 y = (uint64_t *)_x;
142 x = (uint64_t *)_y;
143
144 for (i = 0; i < sizeof(Elf64_Rela) / sizeof(uint64_t); i++) {
145 tmp = x[i];
146 x[i] = y[i];
147 y[i] = tmp;
148 }
149}
150
1da177e4
LT
151/* Get size of potential trampolines required. */
152static unsigned long get_stubs_size(const Elf64_Ehdr *hdr,
153 const Elf64_Shdr *sechdrs)
154{
155 /* One extra reloc so it's always 0-funcaddr terminated */
156 unsigned long relocs = 1;
157 unsigned i;
158
159 /* Every relocated section... */
160 for (i = 1; i < hdr->e_shnum; i++) {
161 if (sechdrs[i].sh_type == SHT_RELA) {
162 DEBUGP("Found relocations in section %u\n", i);
163 DEBUGP("Ptr: %p. Number: %lu\n",
164 (void *)sechdrs[i].sh_addr,
165 sechdrs[i].sh_size / sizeof(Elf64_Rela));
eda09fbd
EM
166
167 /* Sort the relocation information based on a symbol and
168 * addend key. This is a stable O(n*log n) complexity
169 * alogrithm but it will reduce the complexity of
170 * count_relocs() to linear complexity O(n)
171 */
172 sort((void *)sechdrs[i].sh_addr,
173 sechdrs[i].sh_size / sizeof(Elf64_Rela),
174 sizeof(Elf64_Rela), relacmp, relaswap);
175
1da177e4
LT
176 relocs += count_relocs((void *)sechdrs[i].sh_addr,
177 sechdrs[i].sh_size
178 / sizeof(Elf64_Rela));
179 }
180 }
181
f48cb8b4
SR
182#ifdef CONFIG_DYNAMIC_FTRACE
183 /* make the trampoline to the ftrace_caller */
184 relocs++;
185#endif
186
1da177e4
LT
187 DEBUGP("Looks like a total of %lu stubs, max\n", relocs);
188 return relocs * sizeof(struct ppc64_stub_entry);
189}
190
5b12c5c6 191/* Still needed for ELFv2, for .TOC. */
1da177e4
LT
192static void dedotify_versions(struct modversion_info *vers,
193 unsigned long size)
194{
195 struct modversion_info *end;
196
197 for (end = (void *)vers + size; vers < end; vers++)
198 if (vers->name[0] == '.')
199 memmove(vers->name, vers->name+1, strlen(vers->name));
200}
201
5b12c5c6 202/* Undefined symbols which refer to .funcname, hack to funcname (or .TOC.) */
1da177e4
LT
203static void dedotify(Elf64_Sym *syms, unsigned int numsyms, char *strtab)
204{
205 unsigned int i;
206
207 for (i = 1; i < numsyms; i++) {
208 if (syms[i].st_shndx == SHN_UNDEF) {
209 char *name = strtab + syms[i].st_name;
210 if (name[0] == '.')
211 memmove(name, name+1, strlen(name));
212 }
213 }
214}
215
4edebbea
RR
216static Elf64_Sym *find_dot_toc(Elf64_Shdr *sechdrs,
217 const char *strtab,
218 unsigned int symindex)
219{
220 unsigned int i, numsyms;
221 Elf64_Sym *syms;
222
223 syms = (Elf64_Sym *)sechdrs[symindex].sh_addr;
224 numsyms = sechdrs[symindex].sh_size / sizeof(Elf64_Sym);
225
226 for (i = 1; i < numsyms; i++) {
227 if (syms[i].st_shndx == SHN_UNDEF
228 && strcmp(strtab + syms[i].st_name, ".TOC.") == 0)
229 return &syms[i];
230 }
231 return NULL;
232}
233
1da177e4
LT
234int module_frob_arch_sections(Elf64_Ehdr *hdr,
235 Elf64_Shdr *sechdrs,
236 char *secstrings,
237 struct module *me)
238{
239 unsigned int i;
240
241 /* Find .toc and .stubs sections, symtab and strtab */
242 for (i = 1; i < hdr->e_shnum; i++) {
243 char *p;
244 if (strcmp(secstrings + sechdrs[i].sh_name, ".stubs") == 0)
245 me->arch.stubs_section = i;
246 else if (strcmp(secstrings + sechdrs[i].sh_name, ".toc") == 0)
247 me->arch.toc_section = i;
248 else if (strcmp(secstrings+sechdrs[i].sh_name,"__versions")==0)
249 dedotify_versions((void *)hdr + sechdrs[i].sh_offset,
250 sechdrs[i].sh_size);
251
252 /* We don't handle .init for the moment: rename to _init */
253 while ((p = strstr(secstrings + sechdrs[i].sh_name, ".init")))
254 p[0] = '_';
255
256 if (sechdrs[i].sh_type == SHT_SYMTAB)
257 dedotify((void *)hdr + sechdrs[i].sh_offset,
258 sechdrs[i].sh_size / sizeof(Elf64_Sym),
259 (void *)hdr
260 + sechdrs[sechdrs[i].sh_link].sh_offset);
261 }
f749edae
AM
262
263 if (!me->arch.stubs_section) {
264 printk("%s: doesn't contain .stubs.\n", me->name);
1da177e4
LT
265 return -ENOEXEC;
266 }
267
f749edae
AM
268 /* If we don't have a .toc, just use .stubs. We need to set r2
269 to some reasonable value in case the module calls out to
270 other functions via a stub, or if a function pointer escapes
271 the module by some means. */
272 if (!me->arch.toc_section)
273 me->arch.toc_section = me->arch.stubs_section;
274
1da177e4
LT
275 /* Override the stubs size */
276 sechdrs[me->arch.stubs_section].sh_size = get_stubs_size(hdr, sechdrs);
277 return 0;
278}
279
1da177e4
LT
280/* r2 is the TOC pointer: it actually points 0x8000 into the TOC (this
281 gives the value maximum span in an instruction which uses a signed
282 offset) */
283static inline unsigned long my_r2(Elf64_Shdr *sechdrs, struct module *me)
284{
285 return sechdrs[me->arch.toc_section].sh_addr + 0x8000;
286}
287
288/* Both low and high 16 bits are added as SIGNED additions, so if low
289 16 bits has high bit set, high 16 bits must be adjusted. These
290 macros do that (stolen from binutils). */
291#define PPC_LO(v) ((v) & 0xffff)
292#define PPC_HI(v) (((v) >> 16) & 0xffff)
293#define PPC_HA(v) PPC_HI ((v) + 0x8000)
294
295/* Patch stub to reference function and correct r2 value. */
296static inline int create_stub(Elf64_Shdr *sechdrs,
297 struct ppc64_stub_entry *entry,
298 struct ppc64_opd_entry *opd,
299 struct module *me)
300{
1da177e4
LT
301 long reladdr;
302
303 *entry = ppc64_stub;
304
1da177e4
LT
305 /* Stub uses address relative to r2. */
306 reladdr = (unsigned long)entry - my_r2(sechdrs, me);
307 if (reladdr > 0x7FFFFFFF || reladdr < -(0x80000000L)) {
308 printk("%s: Address %p of stub out of range of %p.\n",
309 me->name, (void *)reladdr, (void *)my_r2);
310 return 0;
311 }
312 DEBUGP("Stub %p get data from reladdr %li\n", entry, reladdr);
313
0e60e46e
RR
314 entry->jump[0] |= PPC_HA(reladdr);
315 entry->jump[1] |= PPC_LO(reladdr);
1da177e4
LT
316 entry->opd.funcaddr = opd->funcaddr;
317 entry->opd.r2 = opd->r2;
318 return 1;
319}
320
321/* Create stub to jump to function described in this OPD: we need the
322 stub to set up the TOC ptr (r2) for the function. */
323static unsigned long stub_for_addr(Elf64_Shdr *sechdrs,
324 unsigned long opdaddr,
325 struct module *me)
326{
327 struct ppc64_stub_entry *stubs;
328 struct ppc64_opd_entry *opd = (void *)opdaddr;
329 unsigned int i, num_stubs;
330
331 num_stubs = sechdrs[me->arch.stubs_section].sh_size / sizeof(*stubs);
332
333 /* Find this stub, or if that fails, the next avail. entry */
334 stubs = (void *)sechdrs[me->arch.stubs_section].sh_addr;
335 for (i = 0; stubs[i].opd.funcaddr; i++) {
336 BUG_ON(i >= num_stubs);
337
338 if (stubs[i].opd.funcaddr == opd->funcaddr)
339 return (unsigned long)&stubs[i];
340 }
341
342 if (!create_stub(sechdrs, &stubs[i], opd, me))
343 return 0;
344
345 return (unsigned long)&stubs[i];
346}
347
348/* We expect a noop next: if it is, replace it with instruction to
349 restore r2. */
350static int restore_r2(u32 *instruction, struct module *me)
351{
16c57b36 352 if (*instruction != PPC_INST_NOP) {
1da177e4
LT
353 printk("%s: Expect noop after relocate, got %08x\n",
354 me->name, *instruction);
355 return 0;
356 }
d2fae548
RR
357 /* ld r2,R2_STACK_OFFSET(r1) */
358 *instruction = 0xe8410000 | R2_STACK_OFFSET;
1da177e4
LT
359 return 1;
360}
361
362int apply_relocate_add(Elf64_Shdr *sechdrs,
363 const char *strtab,
364 unsigned int symindex,
365 unsigned int relsec,
366 struct module *me)
367{
368 unsigned int i;
369 Elf64_Rela *rela = (void *)sechdrs[relsec].sh_addr;
370 Elf64_Sym *sym;
371 unsigned long *location;
372 unsigned long value;
373
374 DEBUGP("Applying ADD relocate section %u to %u\n", relsec,
375 sechdrs[relsec].sh_info);
4edebbea
RR
376
377 /* First time we're called, we can fix up .TOC. */
378 if (!me->arch.toc_fixed) {
379 sym = find_dot_toc(sechdrs, strtab, symindex);
380 /* It's theoretically possible that a module doesn't want a
381 * .TOC. so don't fail it just for that. */
382 if (sym)
383 sym->st_value = my_r2(sechdrs, me);
384 me->arch.toc_fixed = true;
385 }
386
1da177e4
LT
387 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rela); i++) {
388 /* This is where to make the change */
389 location = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
390 + rela[i].r_offset;
391 /* This is the symbol it is referring to */
392 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
393 + ELF64_R_SYM(rela[i].r_info);
394
395 DEBUGP("RELOC at %p: %li-type as %s (%lu) + %li\n",
396 location, (long)ELF64_R_TYPE(rela[i].r_info),
397 strtab + sym->st_name, (unsigned long)sym->st_value,
398 (long)rela[i].r_addend);
399
400 /* `Everything is relative'. */
401 value = sym->st_value + rela[i].r_addend;
402
403 switch (ELF64_R_TYPE(rela[i].r_info)) {
404 case R_PPC64_ADDR32:
405 /* Simply set it */
406 *(u32 *)location = value;
407 break;
eda09fbd 408
1da177e4
LT
409 case R_PPC64_ADDR64:
410 /* Simply set it */
411 *(unsigned long *)location = value;
412 break;
413
414 case R_PPC64_TOC:
415 *(unsigned long *)location = my_r2(sechdrs, me);
416 break;
417
9149ccfa 418 case R_PPC64_TOC16:
f749edae 419 /* Subtract TOC pointer */
9149ccfa
PB
420 value -= my_r2(sechdrs, me);
421 if (value + 0x8000 > 0xffff) {
422 printk("%s: bad TOC16 relocation (%lu)\n",
423 me->name, value);
424 return -ENOEXEC;
425 }
426 *((uint16_t *) location)
427 = (*((uint16_t *) location) & ~0xffff)
428 | (value & 0xffff);
429 break;
430
1fbe9cf2
AB
431 case R_PPC64_TOC16_LO:
432 /* Subtract TOC pointer */
433 value -= my_r2(sechdrs, me);
434 *((uint16_t *) location)
435 = (*((uint16_t *) location) & ~0xffff)
436 | (value & 0xffff);
437 break;
438
1da177e4 439 case R_PPC64_TOC16_DS:
f749edae 440 /* Subtract TOC pointer */
1da177e4
LT
441 value -= my_r2(sechdrs, me);
442 if ((value & 3) != 0 || value + 0x8000 > 0xffff) {
443 printk("%s: bad TOC16_DS relocation (%lu)\n",
444 me->name, value);
445 return -ENOEXEC;
446 }
447 *((uint16_t *) location)
448 = (*((uint16_t *) location) & ~0xfffc)
449 | (value & 0xfffc);
450 break;
451
1fbe9cf2
AB
452 case R_PPC64_TOC16_LO_DS:
453 /* Subtract TOC pointer */
454 value -= my_r2(sechdrs, me);
455 if ((value & 3) != 0) {
456 printk("%s: bad TOC16_LO_DS relocation (%lu)\n",
457 me->name, value);
458 return -ENOEXEC;
459 }
460 *((uint16_t *) location)
461 = (*((uint16_t *) location) & ~0xfffc)
462 | (value & 0xfffc);
463 break;
464
465 case R_PPC64_TOC16_HA:
466 /* Subtract TOC pointer */
467 value -= my_r2(sechdrs, me);
468 value = ((value + 0x8000) >> 16);
469 *((uint16_t *) location)
470 = (*((uint16_t *) location) & ~0xffff)
471 | (value & 0xffff);
472 break;
473
1da177e4
LT
474 case R_PPC_REL24:
475 /* FIXME: Handle weak symbols here --RR */
476 if (sym->st_shndx == SHN_UNDEF) {
477 /* External: go via stub */
478 value = stub_for_addr(sechdrs, value, me);
479 if (!value)
480 return -ENOENT;
481 if (!restore_r2((u32 *)location + 1, me))
482 return -ENOEXEC;
483 }
484
485 /* Convert value to relative */
486 value -= (unsigned long)location;
487 if (value + 0x2000000 > 0x3ffffff || (value & 3) != 0){
488 printk("%s: REL24 %li out of range!\n",
489 me->name, (long int)value);
490 return -ENOEXEC;
491 }
492
493 /* Only replace bits 2 through 26 */
eda09fbd 494 *(uint32_t *)location
1da177e4
LT
495 = (*(uint32_t *)location & ~0x03fffffc)
496 | (value & 0x03fffffc);
497 break;
498
21c4ff80
BH
499 case R_PPC64_REL64:
500 /* 64 bits relative (used by features fixups) */
501 *location = value - (unsigned long)location;
502 break;
503
d247da0a
RR
504 case R_PPC64_TOCSAVE:
505 /*
506 * Marker reloc indicates we don't have to save r2.
507 * That would only save us one instruction, so ignore
508 * it.
509 */
510 break;
511
0906584a
RR
512 case R_PPC64_REL16_HA:
513 /* Subtract location pointer */
514 value -= (unsigned long)location;
515 value = ((value + 0x8000) >> 16);
516 *((uint16_t *) location)
517 = (*((uint16_t *) location) & ~0xffff)
518 | (value & 0xffff);
519 break;
520
521 case R_PPC64_REL16_LO:
522 /* Subtract location pointer */
523 value -= (unsigned long)location;
524 *((uint16_t *) location)
525 = (*((uint16_t *) location) & ~0xffff)
526 | (value & 0xffff);
527 break;
528
1da177e4
LT
529 default:
530 printk("%s: Unknown ADD relocation: %lu\n",
531 me->name,
532 (unsigned long)ELF64_R_TYPE(rela[i].r_info));
533 return -ENOEXEC;
534 }
535 }
536
f48cb8b4
SR
537#ifdef CONFIG_DYNAMIC_FTRACE
538 me->arch.toc = my_r2(sechdrs, me);
539 me->arch.tramp = stub_for_addr(sechdrs,
540 (unsigned long)ftrace_caller,
541 me);
542#endif
543
1da177e4
LT
544 return 0;
545}