OMAP3: PM: move context-loss counting into OMAP PM
[linux-ginger.git] / arch / parisc / kernel / module.c
blob212074653df7fc5a109da9222137a13226cc7bff
1 /* Kernel dynamically loadable module help for PARISC.
3 * The best reference for this stuff is probably the Processor-
4 * Specific ELF Supplement for PA-RISC:
5 * http://ftp.parisc-linux.org/docs/arch/elf-pa-hp.pdf
7 * Linux/PA-RISC Project (http://www.parisc-linux.org/)
8 * Copyright (C) 2003 Randolph Chung <tausq at debian . org>
9 * Copyright (C) 2008 Helge Deller <deller@gmx.de>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Notes:
28 * - PLT stub handling
29 * On 32bit (and sometimes 64bit) and with big kernel modules like xfs or
30 * ipv6 the relocation types R_PARISC_PCREL17F and R_PARISC_PCREL22F may
31 * fail to reach their PLT stub if we only create one big stub array for
32 * all sections at the beginning of the core or init section.
33 * Instead we now insert individual PLT stub entries directly in front of
34 * of the code sections where the stubs are actually called.
35 * This reduces the distance between the PCREL location and the stub entry
36 * so that the relocations can be fulfilled.
37 * While calculating the final layout of the kernel module in memory, the
38 * kernel module loader calls arch_mod_section_prepend() to request the
39 * to be reserved amount of memory in front of each individual section.
41 * - SEGREL32 handling
42 * We are not doing SEGREL32 handling correctly. According to the ABI, we
43 * should do a value offset, like this:
44 * if (in_init(me, (void *)val))
45 * val -= (uint32_t)me->module_init;
46 * else
47 * val -= (uint32_t)me->module_core;
48 * However, SEGREL32 is used only for PARISC unwind entries, and we want
49 * those entries to have an absolute address, and not just an offset.
51 * The unwind table mechanism has the ability to specify an offset for
52 * the unwind table; however, because we split off the init functions into
53 * a different piece of memory, it is not possible to do this using a
54 * single offset. Instead, we use the above hack for now.
57 #include <linux/moduleloader.h>
58 #include <linux/elf.h>
59 #include <linux/vmalloc.h>
60 #include <linux/fs.h>
61 #include <linux/string.h>
62 #include <linux/kernel.h>
63 #include <linux/bug.h>
65 #include <asm/unwind.h>
67 #if 0
68 #define DEBUGP printk
69 #else
70 #define DEBUGP(fmt...)
71 #endif
73 #define RELOC_REACHABLE(val, bits) \
74 (( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \
75 ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \
76 0 : 1)
78 #define CHECK_RELOC(val, bits) \
79 if (!RELOC_REACHABLE(val, bits)) { \
80 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
81 me->name, strtab + sym->st_name, (unsigned long)val, bits); \
82 return -ENOEXEC; \
85 /* Maximum number of GOT entries. We use a long displacement ldd from
86 * the bottom of the table, which has a maximum signed displacement of
87 * 0x3fff; however, since we're only going forward, this becomes
88 * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
89 * at most 1023 entries.
90 * To overcome this 14bit displacement with some kernel modules, we'll
91 * use instead the unusal 16bit displacement method (see reassemble_16a)
92 * which gives us a maximum positive displacement of 0x7fff, and as such
93 * allows us to allocate up to 4095 GOT entries. */
94 #define MAX_GOTS 4095
96 /* three functions to determine where in the module core
97 * or init pieces the location is */
98 static inline int in_init(struct module *me, void *loc)
100 return (loc >= me->module_init &&
101 loc <= (me->module_init + me->init_size));
104 static inline int in_core(struct module *me, void *loc)
106 return (loc >= me->module_core &&
107 loc <= (me->module_core + me->core_size));
110 static inline int in_local(struct module *me, void *loc)
112 return in_init(me, loc) || in_core(me, loc);
115 #ifndef CONFIG_64BIT
116 struct got_entry {
117 Elf32_Addr addr;
120 struct stub_entry {
121 Elf32_Word insns[2]; /* each stub entry has two insns */
123 #else
124 struct got_entry {
125 Elf64_Addr addr;
128 struct stub_entry {
129 Elf64_Word insns[4]; /* each stub entry has four insns */
131 #endif
133 /* Field selection types defined by hppa */
134 #define rnd(x) (((x)+0x1000)&~0x1fff)
135 /* fsel: full 32 bits */
136 #define fsel(v,a) ((v)+(a))
137 /* lsel: select left 21 bits */
138 #define lsel(v,a) (((v)+(a))>>11)
139 /* rsel: select right 11 bits */
140 #define rsel(v,a) (((v)+(a))&0x7ff)
141 /* lrsel with rounding of addend to nearest 8k */
142 #define lrsel(v,a) (((v)+rnd(a))>>11)
143 /* rrsel with rounding of addend to nearest 8k */
144 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
146 #define mask(x,sz) ((x) & ~((1<<(sz))-1))
149 /* The reassemble_* functions prepare an immediate value for
150 insertion into an opcode. pa-risc uses all sorts of weird bitfields
151 in the instruction to hold the value. */
152 static inline int sign_unext(int x, int len)
154 int len_ones;
156 len_ones = (1 << len) - 1;
157 return x & len_ones;
160 static inline int low_sign_unext(int x, int len)
162 int sign, temp;
164 sign = (x >> (len-1)) & 1;
165 temp = sign_unext(x, len-1);
166 return (temp << 1) | sign;
169 static inline int reassemble_14(int as14)
171 return (((as14 & 0x1fff) << 1) |
172 ((as14 & 0x2000) >> 13));
175 static inline int reassemble_16a(int as16)
177 int s, t;
179 /* Unusual 16-bit encoding, for wide mode only. */
180 t = (as16 << 1) & 0xffff;
181 s = (as16 & 0x8000);
182 return (t ^ s ^ (s >> 1)) | (s >> 15);
186 static inline int reassemble_17(int as17)
188 return (((as17 & 0x10000) >> 16) |
189 ((as17 & 0x0f800) << 5) |
190 ((as17 & 0x00400) >> 8) |
191 ((as17 & 0x003ff) << 3));
194 static inline int reassemble_21(int as21)
196 return (((as21 & 0x100000) >> 20) |
197 ((as21 & 0x0ffe00) >> 8) |
198 ((as21 & 0x000180) << 7) |
199 ((as21 & 0x00007c) << 14) |
200 ((as21 & 0x000003) << 12));
203 static inline int reassemble_22(int as22)
205 return (((as22 & 0x200000) >> 21) |
206 ((as22 & 0x1f0000) << 5) |
207 ((as22 & 0x00f800) << 5) |
208 ((as22 & 0x000400) >> 8) |
209 ((as22 & 0x0003ff) << 3));
212 void *module_alloc(unsigned long size)
214 if (size == 0)
215 return NULL;
216 return vmalloc(size);
219 #ifndef CONFIG_64BIT
220 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
222 return 0;
225 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
227 return 0;
230 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
232 unsigned long cnt = 0;
234 for (; n > 0; n--, rela++)
236 switch (ELF32_R_TYPE(rela->r_info)) {
237 case R_PARISC_PCREL17F:
238 case R_PARISC_PCREL22F:
239 cnt++;
243 return cnt;
245 #else
246 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
248 unsigned long cnt = 0;
250 for (; n > 0; n--, rela++)
252 switch (ELF64_R_TYPE(rela->r_info)) {
253 case R_PARISC_LTOFF21L:
254 case R_PARISC_LTOFF14R:
255 case R_PARISC_PCREL22F:
256 cnt++;
260 return cnt;
263 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
265 unsigned long cnt = 0;
267 for (; n > 0; n--, rela++)
269 switch (ELF64_R_TYPE(rela->r_info)) {
270 case R_PARISC_FPTR64:
271 cnt++;
275 return cnt;
278 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
280 unsigned long cnt = 0;
282 for (; n > 0; n--, rela++)
284 switch (ELF64_R_TYPE(rela->r_info)) {
285 case R_PARISC_PCREL22F:
286 cnt++;
290 return cnt;
292 #endif
295 /* Free memory returned from module_alloc */
296 void module_free(struct module *mod, void *module_region)
298 kfree(mod->arch.section);
299 mod->arch.section = NULL;
301 vfree(module_region);
304 /* Additional bytes needed in front of individual sections */
305 unsigned int arch_mod_section_prepend(struct module *mod,
306 unsigned int section)
308 /* size needed for all stubs of this section (including
309 * one additional for correct alignment of the stubs) */
310 return (mod->arch.section[section].stub_entries + 1)
311 * sizeof(struct stub_entry);
314 #define CONST
315 int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
316 CONST Elf_Shdr *sechdrs,
317 CONST char *secstrings,
318 struct module *me)
320 unsigned long gots = 0, fdescs = 0, len;
321 unsigned int i;
323 len = hdr->e_shnum * sizeof(me->arch.section[0]);
324 me->arch.section = kzalloc(len, GFP_KERNEL);
325 if (!me->arch.section)
326 return -ENOMEM;
328 for (i = 1; i < hdr->e_shnum; i++) {
329 const Elf_Rela *rels = (void *)sechdrs[i].sh_addr;
330 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
331 unsigned int count, s;
333 if (strncmp(secstrings + sechdrs[i].sh_name,
334 ".PARISC.unwind", 14) == 0)
335 me->arch.unwind_section = i;
337 if (sechdrs[i].sh_type != SHT_RELA)
338 continue;
340 /* some of these are not relevant for 32-bit/64-bit
341 * we leave them here to make the code common. the
342 * compiler will do its thing and optimize out the
343 * stuff we don't need
345 gots += count_gots(rels, nrels);
346 fdescs += count_fdescs(rels, nrels);
348 /* XXX: By sorting the relocs and finding duplicate entries
349 * we could reduce the number of necessary stubs and save
350 * some memory. */
351 count = count_stubs(rels, nrels);
352 if (!count)
353 continue;
355 /* so we need relocation stubs. reserve necessary memory. */
356 /* sh_info gives the section for which we need to add stubs. */
357 s = sechdrs[i].sh_info;
359 /* each code section should only have one relocation section */
360 WARN_ON(me->arch.section[s].stub_entries);
362 /* store number of stubs we need for this section */
363 me->arch.section[s].stub_entries += count;
366 /* align things a bit */
367 me->core_size = ALIGN(me->core_size, 16);
368 me->arch.got_offset = me->core_size;
369 me->core_size += gots * sizeof(struct got_entry);
371 me->core_size = ALIGN(me->core_size, 16);
372 me->arch.fdesc_offset = me->core_size;
373 me->core_size += fdescs * sizeof(Elf_Fdesc);
375 me->arch.got_max = gots;
376 me->arch.fdesc_max = fdescs;
378 return 0;
381 #ifdef CONFIG_64BIT
382 static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
384 unsigned int i;
385 struct got_entry *got;
387 value += addend;
389 BUG_ON(value == 0);
391 got = me->module_core + me->arch.got_offset;
392 for (i = 0; got[i].addr; i++)
393 if (got[i].addr == value)
394 goto out;
396 BUG_ON(++me->arch.got_count > me->arch.got_max);
398 got[i].addr = value;
399 out:
400 DEBUGP("GOT ENTRY %d[%x] val %lx\n", i, i*sizeof(struct got_entry),
401 value);
402 return i * sizeof(struct got_entry);
404 #endif /* CONFIG_64BIT */
406 #ifdef CONFIG_64BIT
407 static Elf_Addr get_fdesc(struct module *me, unsigned long value)
409 Elf_Fdesc *fdesc = me->module_core + me->arch.fdesc_offset;
411 if (!value) {
412 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
413 return 0;
416 /* Look for existing fdesc entry. */
417 while (fdesc->addr) {
418 if (fdesc->addr == value)
419 return (Elf_Addr)fdesc;
420 fdesc++;
423 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
425 /* Create new one */
426 fdesc->addr = value;
427 fdesc->gp = (Elf_Addr)me->module_core + me->arch.got_offset;
428 return (Elf_Addr)fdesc;
430 #endif /* CONFIG_64BIT */
432 enum elf_stub_type {
433 ELF_STUB_GOT,
434 ELF_STUB_MILLI,
435 ELF_STUB_DIRECT,
438 static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
439 enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec)
441 struct stub_entry *stub;
442 int __maybe_unused d;
444 /* initialize stub_offset to point in front of the section */
445 if (!me->arch.section[targetsec].stub_offset) {
446 loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
447 sizeof(struct stub_entry);
448 /* get correct alignment for the stubs */
449 loc0 = ALIGN(loc0, sizeof(struct stub_entry));
450 me->arch.section[targetsec].stub_offset = loc0;
453 /* get address of stub entry */
454 stub = (void *) me->arch.section[targetsec].stub_offset;
455 me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
457 /* do not write outside available stub area */
458 BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
461 #ifndef CONFIG_64BIT
462 /* for 32-bit the stub looks like this:
463 * ldil L'XXX,%r1
464 * be,n R'XXX(%sr4,%r1)
466 //value = *(unsigned long *)((value + addend) & ~3); /* why? */
468 stub->insns[0] = 0x20200000; /* ldil L'XXX,%r1 */
469 stub->insns[1] = 0xe0202002; /* be,n R'XXX(%sr4,%r1) */
471 stub->insns[0] |= reassemble_21(lrsel(value, addend));
472 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
474 #else
475 /* for 64-bit we have three kinds of stubs:
476 * for normal function calls:
477 * ldd 0(%dp),%dp
478 * ldd 10(%dp), %r1
479 * bve (%r1)
480 * ldd 18(%dp), %dp
482 * for millicode:
483 * ldil 0, %r1
484 * ldo 0(%r1), %r1
485 * ldd 10(%r1), %r1
486 * bve,n (%r1)
488 * for direct branches (jumps between different section of the
489 * same module):
490 * ldil 0, %r1
491 * ldo 0(%r1), %r1
492 * bve,n (%r1)
494 switch (stub_type) {
495 case ELF_STUB_GOT:
496 d = get_got(me, value, addend);
497 if (d <= 15) {
498 /* Format 5 */
499 stub->insns[0] = 0x0f6010db; /* ldd 0(%dp),%dp */
500 stub->insns[0] |= low_sign_unext(d, 5) << 16;
501 } else {
502 /* Format 3 */
503 stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */
504 stub->insns[0] |= reassemble_16a(d);
506 stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */
507 stub->insns[2] = 0xe820d000; /* bve (%r1) */
508 stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */
509 break;
510 case ELF_STUB_MILLI:
511 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
512 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
513 stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */
514 stub->insns[3] = 0xe820d002; /* bve,n (%r1) */
516 stub->insns[0] |= reassemble_21(lrsel(value, addend));
517 stub->insns[1] |= reassemble_14(rrsel(value, addend));
518 break;
519 case ELF_STUB_DIRECT:
520 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
521 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
522 stub->insns[2] = 0xe820d002; /* bve,n (%r1) */
524 stub->insns[0] |= reassemble_21(lrsel(value, addend));
525 stub->insns[1] |= reassemble_14(rrsel(value, addend));
526 break;
529 #endif
531 return (Elf_Addr)stub;
534 int apply_relocate(Elf_Shdr *sechdrs,
535 const char *strtab,
536 unsigned int symindex,
537 unsigned int relsec,
538 struct module *me)
540 /* parisc should not need this ... */
541 printk(KERN_ERR "module %s: RELOCATION unsupported\n",
542 me->name);
543 return -ENOEXEC;
546 #ifndef CONFIG_64BIT
547 int apply_relocate_add(Elf_Shdr *sechdrs,
548 const char *strtab,
549 unsigned int symindex,
550 unsigned int relsec,
551 struct module *me)
553 int i;
554 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
555 Elf32_Sym *sym;
556 Elf32_Word *loc;
557 Elf32_Addr val;
558 Elf32_Sword addend;
559 Elf32_Addr dot;
560 Elf_Addr loc0;
561 unsigned int targetsec = sechdrs[relsec].sh_info;
562 //unsigned long dp = (unsigned long)$global$;
563 register unsigned long dp asm ("r27");
565 DEBUGP("Applying relocate section %u to %u\n", relsec,
566 targetsec);
567 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
568 /* This is where to make the change */
569 loc = (void *)sechdrs[targetsec].sh_addr
570 + rel[i].r_offset;
571 /* This is the start of the target section */
572 loc0 = sechdrs[targetsec].sh_addr;
573 /* This is the symbol it is referring to */
574 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
575 + ELF32_R_SYM(rel[i].r_info);
576 if (!sym->st_value) {
577 printk(KERN_WARNING "%s: Unknown symbol %s\n",
578 me->name, strtab + sym->st_name);
579 return -ENOENT;
581 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
582 dot = (Elf32_Addr)loc & ~0x03;
584 val = sym->st_value;
585 addend = rel[i].r_addend;
587 #if 0
588 #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
589 DEBUGP("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
590 strtab + sym->st_name,
591 (uint32_t)loc, val, addend,
592 r(R_PARISC_PLABEL32)
593 r(R_PARISC_DIR32)
594 r(R_PARISC_DIR21L)
595 r(R_PARISC_DIR14R)
596 r(R_PARISC_SEGREL32)
597 r(R_PARISC_DPREL21L)
598 r(R_PARISC_DPREL14R)
599 r(R_PARISC_PCREL17F)
600 r(R_PARISC_PCREL22F)
601 "UNKNOWN");
602 #undef r
603 #endif
605 switch (ELF32_R_TYPE(rel[i].r_info)) {
606 case R_PARISC_PLABEL32:
607 /* 32-bit function address */
608 /* no function descriptors... */
609 *loc = fsel(val, addend);
610 break;
611 case R_PARISC_DIR32:
612 /* direct 32-bit ref */
613 *loc = fsel(val, addend);
614 break;
615 case R_PARISC_DIR21L:
616 /* left 21 bits of effective address */
617 val = lrsel(val, addend);
618 *loc = mask(*loc, 21) | reassemble_21(val);
619 break;
620 case R_PARISC_DIR14R:
621 /* right 14 bits of effective address */
622 val = rrsel(val, addend);
623 *loc = mask(*loc, 14) | reassemble_14(val);
624 break;
625 case R_PARISC_SEGREL32:
626 /* 32-bit segment relative address */
627 /* See note about special handling of SEGREL32 at
628 * the beginning of this file.
630 *loc = fsel(val, addend);
631 break;
632 case R_PARISC_DPREL21L:
633 /* left 21 bit of relative address */
634 val = lrsel(val - dp, addend);
635 *loc = mask(*loc, 21) | reassemble_21(val);
636 break;
637 case R_PARISC_DPREL14R:
638 /* right 14 bit of relative address */
639 val = rrsel(val - dp, addend);
640 *loc = mask(*loc, 14) | reassemble_14(val);
641 break;
642 case R_PARISC_PCREL17F:
643 /* 17-bit PC relative address */
644 /* calculate direct call offset */
645 val += addend;
646 val = (val - dot - 8)/4;
647 if (!RELOC_REACHABLE(val, 17)) {
648 /* direct distance too far, create
649 * stub entry instead */
650 val = get_stub(me, sym->st_value, addend,
651 ELF_STUB_DIRECT, loc0, targetsec);
652 val = (val - dot - 8)/4;
653 CHECK_RELOC(val, 17);
655 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
656 break;
657 case R_PARISC_PCREL22F:
658 /* 22-bit PC relative address; only defined for pa20 */
659 /* calculate direct call offset */
660 val += addend;
661 val = (val - dot - 8)/4;
662 if (!RELOC_REACHABLE(val, 22)) {
663 /* direct distance too far, create
664 * stub entry instead */
665 val = get_stub(me, sym->st_value, addend,
666 ELF_STUB_DIRECT, loc0, targetsec);
667 val = (val - dot - 8)/4;
668 CHECK_RELOC(val, 22);
670 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
671 break;
673 default:
674 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
675 me->name, ELF32_R_TYPE(rel[i].r_info));
676 return -ENOEXEC;
680 return 0;
683 #else
684 int apply_relocate_add(Elf_Shdr *sechdrs,
685 const char *strtab,
686 unsigned int symindex,
687 unsigned int relsec,
688 struct module *me)
690 int i;
691 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
692 Elf64_Sym *sym;
693 Elf64_Word *loc;
694 Elf64_Xword *loc64;
695 Elf64_Addr val;
696 Elf64_Sxword addend;
697 Elf64_Addr dot;
698 Elf_Addr loc0;
699 unsigned int targetsec = sechdrs[relsec].sh_info;
701 DEBUGP("Applying relocate section %u to %u\n", relsec,
702 targetsec);
703 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
704 /* This is where to make the change */
705 loc = (void *)sechdrs[targetsec].sh_addr
706 + rel[i].r_offset;
707 /* This is the start of the target section */
708 loc0 = sechdrs[targetsec].sh_addr;
709 /* This is the symbol it is referring to */
710 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
711 + ELF64_R_SYM(rel[i].r_info);
712 if (!sym->st_value) {
713 printk(KERN_WARNING "%s: Unknown symbol %s\n",
714 me->name, strtab + sym->st_name);
715 return -ENOENT;
717 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
718 dot = (Elf64_Addr)loc & ~0x03;
719 loc64 = (Elf64_Xword *)loc;
721 val = sym->st_value;
722 addend = rel[i].r_addend;
724 #if 0
725 #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
726 printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
727 strtab + sym->st_name,
728 loc, val, addend,
729 r(R_PARISC_LTOFF14R)
730 r(R_PARISC_LTOFF21L)
731 r(R_PARISC_PCREL22F)
732 r(R_PARISC_DIR64)
733 r(R_PARISC_SEGREL32)
734 r(R_PARISC_FPTR64)
735 "UNKNOWN");
736 #undef r
737 #endif
739 switch (ELF64_R_TYPE(rel[i].r_info)) {
740 case R_PARISC_LTOFF21L:
741 /* LT-relative; left 21 bits */
742 val = get_got(me, val, addend);
743 DEBUGP("LTOFF21L Symbol %s loc %p val %lx\n",
744 strtab + sym->st_name,
745 loc, val);
746 val = lrsel(val, 0);
747 *loc = mask(*loc, 21) | reassemble_21(val);
748 break;
749 case R_PARISC_LTOFF14R:
750 /* L(ltoff(val+addend)) */
751 /* LT-relative; right 14 bits */
752 val = get_got(me, val, addend);
753 val = rrsel(val, 0);
754 DEBUGP("LTOFF14R Symbol %s loc %p val %lx\n",
755 strtab + sym->st_name,
756 loc, val);
757 *loc = mask(*loc, 14) | reassemble_14(val);
758 break;
759 case R_PARISC_PCREL22F:
760 /* PC-relative; 22 bits */
761 DEBUGP("PCREL22F Symbol %s loc %p val %lx\n",
762 strtab + sym->st_name,
763 loc, val);
764 val += addend;
765 /* can we reach it locally? */
766 if (in_local(me, (void *)val)) {
767 /* this is the case where the symbol is local
768 * to the module, but in a different section,
769 * so stub the jump in case it's more than 22
770 * bits away */
771 val = (val - dot - 8)/4;
772 if (!RELOC_REACHABLE(val, 22)) {
773 /* direct distance too far, create
774 * stub entry instead */
775 val = get_stub(me, sym->st_value,
776 addend, ELF_STUB_DIRECT,
777 loc0, targetsec);
778 } else {
779 /* Ok, we can reach it directly. */
780 val = sym->st_value;
781 val += addend;
783 } else {
784 val = sym->st_value;
785 if (strncmp(strtab + sym->st_name, "$$", 2)
786 == 0)
787 val = get_stub(me, val, addend, ELF_STUB_MILLI,
788 loc0, targetsec);
789 else
790 val = get_stub(me, val, addend, ELF_STUB_GOT,
791 loc0, targetsec);
793 DEBUGP("STUB FOR %s loc %lx, val %lx+%lx at %lx\n",
794 strtab + sym->st_name, loc, sym->st_value,
795 addend, val);
796 val = (val - dot - 8)/4;
797 CHECK_RELOC(val, 22);
798 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
799 break;
800 case R_PARISC_DIR64:
801 /* 64-bit effective address */
802 *loc64 = val + addend;
803 break;
804 case R_PARISC_SEGREL32:
805 /* 32-bit segment relative address */
806 /* See note about special handling of SEGREL32 at
807 * the beginning of this file.
809 *loc = fsel(val, addend);
810 break;
811 case R_PARISC_FPTR64:
812 /* 64-bit function address */
813 if(in_local(me, (void *)(val + addend))) {
814 *loc64 = get_fdesc(me, val+addend);
815 DEBUGP("FDESC for %s at %p points to %lx\n",
816 strtab + sym->st_name, *loc64,
817 ((Elf_Fdesc *)*loc64)->addr);
818 } else {
819 /* if the symbol is not local to this
820 * module then val+addend is a pointer
821 * to the function descriptor */
822 DEBUGP("Non local FPTR64 Symbol %s loc %p val %lx\n",
823 strtab + sym->st_name,
824 loc, val);
825 *loc64 = val + addend;
827 break;
829 default:
830 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
831 me->name, ELF64_R_TYPE(rel[i].r_info));
832 return -ENOEXEC;
835 return 0;
837 #endif
839 static void
840 register_unwind_table(struct module *me,
841 const Elf_Shdr *sechdrs)
843 unsigned char *table, *end;
844 unsigned long gp;
846 if (!me->arch.unwind_section)
847 return;
849 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
850 end = table + sechdrs[me->arch.unwind_section].sh_size;
851 gp = (Elf_Addr)me->module_core + me->arch.got_offset;
853 DEBUGP("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
854 me->arch.unwind_section, table, end, gp);
855 me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
858 static void
859 deregister_unwind_table(struct module *me)
861 if (me->arch.unwind)
862 unwind_table_remove(me->arch.unwind);
865 int module_finalize(const Elf_Ehdr *hdr,
866 const Elf_Shdr *sechdrs,
867 struct module *me)
869 int i;
870 unsigned long nsyms;
871 const char *strtab = NULL;
872 Elf_Sym *newptr, *oldptr;
873 Elf_Shdr *symhdr = NULL;
874 #ifdef DEBUG
875 Elf_Fdesc *entry;
876 u32 *addr;
878 entry = (Elf_Fdesc *)me->init;
879 printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
880 entry->gp, entry->addr);
881 addr = (u32 *)entry->addr;
882 printk("INSNS: %x %x %x %x\n",
883 addr[0], addr[1], addr[2], addr[3]);
884 printk("got entries used %ld, gots max %ld\n"
885 "fdescs used %ld, fdescs max %ld\n",
886 me->arch.got_count, me->arch.got_max,
887 me->arch.fdesc_count, me->arch.fdesc_max);
888 #endif
890 register_unwind_table(me, sechdrs);
892 /* haven't filled in me->symtab yet, so have to find it
893 * ourselves */
894 for (i = 1; i < hdr->e_shnum; i++) {
895 if(sechdrs[i].sh_type == SHT_SYMTAB
896 && (sechdrs[i].sh_flags & SHF_ALLOC)) {
897 int strindex = sechdrs[i].sh_link;
898 /* FIXME: AWFUL HACK
899 * The cast is to drop the const from
900 * the sechdrs pointer */
901 symhdr = (Elf_Shdr *)&sechdrs[i];
902 strtab = (char *)sechdrs[strindex].sh_addr;
903 break;
907 DEBUGP("module %s: strtab %p, symhdr %p\n",
908 me->name, strtab, symhdr);
910 if(me->arch.got_count > MAX_GOTS) {
911 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
912 me->name, me->arch.got_count, MAX_GOTS);
913 return -EINVAL;
916 kfree(me->arch.section);
917 me->arch.section = NULL;
919 /* no symbol table */
920 if(symhdr == NULL)
921 return 0;
923 oldptr = (void *)symhdr->sh_addr;
924 newptr = oldptr + 1; /* we start counting at 1 */
925 nsyms = symhdr->sh_size / sizeof(Elf_Sym);
926 DEBUGP("OLD num_symtab %lu\n", nsyms);
928 for (i = 1; i < nsyms; i++) {
929 oldptr++; /* note, count starts at 1 so preincrement */
930 if(strncmp(strtab + oldptr->st_name,
931 ".L", 2) == 0)
932 continue;
934 if(newptr != oldptr)
935 *newptr++ = *oldptr;
936 else
937 newptr++;
940 nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
941 DEBUGP("NEW num_symtab %lu\n", nsyms);
942 symhdr->sh_size = nsyms * sizeof(Elf_Sym);
943 return module_bug_finalize(hdr, sechdrs, me);
946 void module_arch_cleanup(struct module *mod)
948 deregister_unwind_table(mod);
949 module_bug_cleanup(mod);