GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / parisc / kernel / module.c
blobc619b38109cad4b09a7621d1b05aa3195fffe9ef
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>
64 #include <linux/slab.h>
66 #include <asm/unwind.h>
68 #define DEBUGP(fmt...)
70 #define RELOC_REACHABLE(val, bits) \
71 (( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \
72 ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \
73 0 : 1)
75 #define CHECK_RELOC(val, bits) \
76 if (!RELOC_REACHABLE(val, bits)) { \
77 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
78 me->name, strtab + sym->st_name, (unsigned long)val, bits); \
79 return -ENOEXEC; \
82 /* Maximum number of GOT entries. We use a long displacement ldd from
83 * the bottom of the table, which has a maximum signed displacement of
84 * 0x3fff; however, since we're only going forward, this becomes
85 * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
86 * at most 1023 entries.
87 * To overcome this 14bit displacement with some kernel modules, we'll
88 * use instead the unusal 16bit displacement method (see reassemble_16a)
89 * which gives us a maximum positive displacement of 0x7fff, and as such
90 * allows us to allocate up to 4095 GOT entries. */
91 #define MAX_GOTS 4095
93 /* three functions to determine where in the module core
94 * or init pieces the location is */
95 static inline int in_init(struct module *me, void *loc)
97 return (loc >= me->module_init &&
98 loc <= (me->module_init + me->init_size));
101 static inline int in_core(struct module *me, void *loc)
103 return (loc >= me->module_core &&
104 loc <= (me->module_core + me->core_size));
107 static inline int in_local(struct module *me, void *loc)
109 return in_init(me, loc) || in_core(me, loc);
112 #ifndef CONFIG_64BIT
113 struct got_entry {
114 Elf32_Addr addr;
117 struct stub_entry {
118 Elf32_Word insns[2]; /* each stub entry has two insns */
120 #else
121 struct got_entry {
122 Elf64_Addr addr;
125 struct stub_entry {
126 Elf64_Word insns[4]; /* each stub entry has four insns */
128 #endif
130 /* Field selection types defined by hppa */
131 #define rnd(x) (((x)+0x1000)&~0x1fff)
132 /* fsel: full 32 bits */
133 #define fsel(v,a) ((v)+(a))
134 /* lsel: select left 21 bits */
135 #define lsel(v,a) (((v)+(a))>>11)
136 /* rsel: select right 11 bits */
137 #define rsel(v,a) (((v)+(a))&0x7ff)
138 /* lrsel with rounding of addend to nearest 8k */
139 #define lrsel(v,a) (((v)+rnd(a))>>11)
140 /* rrsel with rounding of addend to nearest 8k */
141 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
143 #define mask(x,sz) ((x) & ~((1<<(sz))-1))
146 /* The reassemble_* functions prepare an immediate value for
147 insertion into an opcode. pa-risc uses all sorts of weird bitfields
148 in the instruction to hold the value. */
149 static inline int sign_unext(int x, int len)
151 int len_ones;
153 len_ones = (1 << len) - 1;
154 return x & len_ones;
157 static inline int low_sign_unext(int x, int len)
159 int sign, temp;
161 sign = (x >> (len-1)) & 1;
162 temp = sign_unext(x, len-1);
163 return (temp << 1) | sign;
166 static inline int reassemble_14(int as14)
168 return (((as14 & 0x1fff) << 1) |
169 ((as14 & 0x2000) >> 13));
172 static inline int reassemble_16a(int as16)
174 int s, t;
176 /* Unusual 16-bit encoding, for wide mode only. */
177 t = (as16 << 1) & 0xffff;
178 s = (as16 & 0x8000);
179 return (t ^ s ^ (s >> 1)) | (s >> 15);
183 static inline int reassemble_17(int as17)
185 return (((as17 & 0x10000) >> 16) |
186 ((as17 & 0x0f800) << 5) |
187 ((as17 & 0x00400) >> 8) |
188 ((as17 & 0x003ff) << 3));
191 static inline int reassemble_21(int as21)
193 return (((as21 & 0x100000) >> 20) |
194 ((as21 & 0x0ffe00) >> 8) |
195 ((as21 & 0x000180) << 7) |
196 ((as21 & 0x00007c) << 14) |
197 ((as21 & 0x000003) << 12));
200 static inline int reassemble_22(int as22)
202 return (((as22 & 0x200000) >> 21) |
203 ((as22 & 0x1f0000) << 5) |
204 ((as22 & 0x00f800) << 5) |
205 ((as22 & 0x000400) >> 8) |
206 ((as22 & 0x0003ff) << 3));
209 void *module_alloc(unsigned long size)
211 if (size == 0)
212 return NULL;
213 return vmalloc(size);
216 #ifndef CONFIG_64BIT
217 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
219 return 0;
222 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
224 return 0;
227 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
229 unsigned long cnt = 0;
231 for (; n > 0; n--, rela++)
233 switch (ELF32_R_TYPE(rela->r_info)) {
234 case R_PARISC_PCREL17F:
235 case R_PARISC_PCREL22F:
236 cnt++;
240 return cnt;
242 #else
243 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
245 unsigned long cnt = 0;
247 for (; n > 0; n--, rela++)
249 switch (ELF64_R_TYPE(rela->r_info)) {
250 case R_PARISC_LTOFF21L:
251 case R_PARISC_LTOFF14R:
252 case R_PARISC_PCREL22F:
253 cnt++;
257 return cnt;
260 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
262 unsigned long cnt = 0;
264 for (; n > 0; n--, rela++)
266 switch (ELF64_R_TYPE(rela->r_info)) {
267 case R_PARISC_FPTR64:
268 cnt++;
272 return cnt;
275 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
277 unsigned long cnt = 0;
279 for (; n > 0; n--, rela++)
281 switch (ELF64_R_TYPE(rela->r_info)) {
282 case R_PARISC_PCREL22F:
283 cnt++;
287 return cnt;
289 #endif
292 /* Free memory returned from module_alloc */
293 void module_free(struct module *mod, void *module_region)
295 kfree(mod->arch.section);
296 mod->arch.section = NULL;
298 vfree(module_region);
301 /* Additional bytes needed in front of individual sections */
302 unsigned int arch_mod_section_prepend(struct module *mod,
303 unsigned int section)
305 /* size needed for all stubs of this section (including
306 * one additional for correct alignment of the stubs) */
307 return (mod->arch.section[section].stub_entries + 1)
308 * sizeof(struct stub_entry);
311 #define CONST
312 int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
313 CONST Elf_Shdr *sechdrs,
314 CONST char *secstrings,
315 struct module *me)
317 unsigned long gots = 0, fdescs = 0, len;
318 unsigned int i;
320 len = hdr->e_shnum * sizeof(me->arch.section[0]);
321 me->arch.section = kzalloc(len, GFP_KERNEL);
322 if (!me->arch.section)
323 return -ENOMEM;
325 for (i = 1; i < hdr->e_shnum; i++) {
326 const Elf_Rela *rels = (void *)sechdrs[i].sh_addr;
327 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
328 unsigned int count, s;
330 if (strncmp(secstrings + sechdrs[i].sh_name,
331 ".PARISC.unwind", 14) == 0)
332 me->arch.unwind_section = i;
334 if (sechdrs[i].sh_type != SHT_RELA)
335 continue;
337 /* some of these are not relevant for 32-bit/64-bit
338 * we leave them here to make the code common. the
339 * compiler will do its thing and optimize out the
340 * stuff we don't need
342 gots += count_gots(rels, nrels);
343 fdescs += count_fdescs(rels, nrels);
345 count = count_stubs(rels, nrels);
346 if (!count)
347 continue;
349 /* so we need relocation stubs. reserve necessary memory. */
350 /* sh_info gives the section for which we need to add stubs. */
351 s = sechdrs[i].sh_info;
353 /* each code section should only have one relocation section */
354 WARN_ON(me->arch.section[s].stub_entries);
356 /* store number of stubs we need for this section */
357 me->arch.section[s].stub_entries += count;
360 /* align things a bit */
361 me->core_size = ALIGN(me->core_size, 16);
362 me->arch.got_offset = me->core_size;
363 me->core_size += gots * sizeof(struct got_entry);
365 me->core_size = ALIGN(me->core_size, 16);
366 me->arch.fdesc_offset = me->core_size;
367 me->core_size += fdescs * sizeof(Elf_Fdesc);
369 me->arch.got_max = gots;
370 me->arch.fdesc_max = fdescs;
372 return 0;
375 #ifdef CONFIG_64BIT
376 static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
378 unsigned int i;
379 struct got_entry *got;
381 value += addend;
383 BUG_ON(value == 0);
385 got = me->module_core + me->arch.got_offset;
386 for (i = 0; got[i].addr; i++)
387 if (got[i].addr == value)
388 goto out;
390 BUG_ON(++me->arch.got_count > me->arch.got_max);
392 got[i].addr = value;
393 out:
394 DEBUGP("GOT ENTRY %d[%x] val %lx\n", i, i*sizeof(struct got_entry),
395 value);
396 return i * sizeof(struct got_entry);
398 #endif /* CONFIG_64BIT */
400 #ifdef CONFIG_64BIT
401 static Elf_Addr get_fdesc(struct module *me, unsigned long value)
403 Elf_Fdesc *fdesc = me->module_core + me->arch.fdesc_offset;
405 if (!value) {
406 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
407 return 0;
410 /* Look for existing fdesc entry. */
411 while (fdesc->addr) {
412 if (fdesc->addr == value)
413 return (Elf_Addr)fdesc;
414 fdesc++;
417 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
419 /* Create new one */
420 fdesc->addr = value;
421 fdesc->gp = (Elf_Addr)me->module_core + me->arch.got_offset;
422 return (Elf_Addr)fdesc;
424 #endif /* CONFIG_64BIT */
426 enum elf_stub_type {
427 ELF_STUB_GOT,
428 ELF_STUB_MILLI,
429 ELF_STUB_DIRECT,
432 static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
433 enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec)
435 struct stub_entry *stub;
436 int __maybe_unused d;
438 /* initialize stub_offset to point in front of the section */
439 if (!me->arch.section[targetsec].stub_offset) {
440 loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
441 sizeof(struct stub_entry);
442 /* get correct alignment for the stubs */
443 loc0 = ALIGN(loc0, sizeof(struct stub_entry));
444 me->arch.section[targetsec].stub_offset = loc0;
447 /* get address of stub entry */
448 stub = (void *) me->arch.section[targetsec].stub_offset;
449 me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
451 /* do not write outside available stub area */
452 BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
455 #ifndef CONFIG_64BIT
456 //value = *(unsigned long *)((value + addend) & ~3); /* why? */
458 stub->insns[0] = 0x20200000;
459 stub->insns[1] = 0xe0202002;
461 stub->insns[0] |= reassemble_21(lrsel(value, addend));
462 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
464 #else
465 /* for 64-bit we have three kinds of stubs:
466 * for normal function calls:
467 * ldd 0(%dp),%dp
468 * ldd 10(%dp), %r1
469 * bve (%r1)
470 * ldd 18(%dp), %dp
472 * for millicode:
473 * ldil 0, %r1
474 * ldo 0(%r1), %r1
475 * ldd 10(%r1), %r1
476 * bve,n (%r1)
478 * for direct branches (jumps between different section of the
479 * same module):
480 * ldil 0, %r1
481 * ldo 0(%r1), %r1
482 * bve,n (%r1)
484 switch (stub_type) {
485 case ELF_STUB_GOT:
486 d = get_got(me, value, addend);
487 if (d <= 15) {
488 /* Format 5 */
489 stub->insns[0] = 0x0f6010db; /* ldd 0(%dp),%dp */
490 stub->insns[0] |= low_sign_unext(d, 5) << 16;
491 } else {
492 /* Format 3 */
493 stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */
494 stub->insns[0] |= reassemble_16a(d);
496 stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */
497 stub->insns[2] = 0xe820d000; /* bve (%r1) */
498 stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */
499 break;
500 case ELF_STUB_MILLI:
501 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
502 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
503 stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */
504 stub->insns[3] = 0xe820d002; /* bve,n (%r1) */
506 stub->insns[0] |= reassemble_21(lrsel(value, addend));
507 stub->insns[1] |= reassemble_14(rrsel(value, addend));
508 break;
509 case ELF_STUB_DIRECT:
510 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
511 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
512 stub->insns[2] = 0xe820d002; /* bve,n (%r1) */
514 stub->insns[0] |= reassemble_21(lrsel(value, addend));
515 stub->insns[1] |= reassemble_14(rrsel(value, addend));
516 break;
519 #endif
521 return (Elf_Addr)stub;
524 int apply_relocate(Elf_Shdr *sechdrs,
525 const char *strtab,
526 unsigned int symindex,
527 unsigned int relsec,
528 struct module *me)
530 /* parisc should not need this ... */
531 printk(KERN_ERR "module %s: RELOCATION unsupported\n",
532 me->name);
533 return -ENOEXEC;
536 #ifndef CONFIG_64BIT
537 int apply_relocate_add(Elf_Shdr *sechdrs,
538 const char *strtab,
539 unsigned int symindex,
540 unsigned int relsec,
541 struct module *me)
543 int i;
544 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
545 Elf32_Sym *sym;
546 Elf32_Word *loc;
547 Elf32_Addr val;
548 Elf32_Sword addend;
549 Elf32_Addr dot;
550 Elf_Addr loc0;
551 unsigned int targetsec = sechdrs[relsec].sh_info;
552 //unsigned long dp = (unsigned long)$global$;
553 register unsigned long dp asm ("r27");
555 DEBUGP("Applying relocate section %u to %u\n", relsec,
556 targetsec);
557 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
558 /* This is where to make the change */
559 loc = (void *)sechdrs[targetsec].sh_addr
560 + rel[i].r_offset;
561 /* This is the start of the target section */
562 loc0 = sechdrs[targetsec].sh_addr;
563 /* This is the symbol it is referring to */
564 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
565 + ELF32_R_SYM(rel[i].r_info);
566 if (!sym->st_value) {
567 printk(KERN_WARNING "%s: Unknown symbol %s\n",
568 me->name, strtab + sym->st_name);
569 return -ENOENT;
571 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
572 dot = (Elf32_Addr)loc & ~0x03;
574 val = sym->st_value;
575 addend = rel[i].r_addend;
578 switch (ELF32_R_TYPE(rel[i].r_info)) {
579 case R_PARISC_PLABEL32:
580 /* 32-bit function address */
581 /* no function descriptors... */
582 *loc = fsel(val, addend);
583 break;
584 case R_PARISC_DIR32:
585 /* direct 32-bit ref */
586 *loc = fsel(val, addend);
587 break;
588 case R_PARISC_DIR21L:
589 /* left 21 bits of effective address */
590 val = lrsel(val, addend);
591 *loc = mask(*loc, 21) | reassemble_21(val);
592 break;
593 case R_PARISC_DIR14R:
594 /* right 14 bits of effective address */
595 val = rrsel(val, addend);
596 *loc = mask(*loc, 14) | reassemble_14(val);
597 break;
598 case R_PARISC_SEGREL32:
599 /* 32-bit segment relative address */
600 /* See note about special handling of SEGREL32 at
601 * the beginning of this file.
603 *loc = fsel(val, addend);
604 break;
605 case R_PARISC_DPREL21L:
606 /* left 21 bit of relative address */
607 val = lrsel(val - dp, addend);
608 *loc = mask(*loc, 21) | reassemble_21(val);
609 break;
610 case R_PARISC_DPREL14R:
611 /* right 14 bit of relative address */
612 val = rrsel(val - dp, addend);
613 *loc = mask(*loc, 14) | reassemble_14(val);
614 break;
615 case R_PARISC_PCREL17F:
616 /* 17-bit PC relative address */
617 /* calculate direct call offset */
618 val += addend;
619 val = (val - dot - 8)/4;
620 if (!RELOC_REACHABLE(val, 17)) {
621 /* direct distance too far, create
622 * stub entry instead */
623 val = get_stub(me, sym->st_value, addend,
624 ELF_STUB_DIRECT, loc0, targetsec);
625 val = (val - dot - 8)/4;
626 CHECK_RELOC(val, 17);
628 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
629 break;
630 case R_PARISC_PCREL22F:
631 /* 22-bit PC relative address; only defined for pa20 */
632 /* calculate direct call offset */
633 val += addend;
634 val = (val - dot - 8)/4;
635 if (!RELOC_REACHABLE(val, 22)) {
636 /* direct distance too far, create
637 * stub entry instead */
638 val = get_stub(me, sym->st_value, addend,
639 ELF_STUB_DIRECT, loc0, targetsec);
640 val = (val - dot - 8)/4;
641 CHECK_RELOC(val, 22);
643 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
644 break;
646 default:
647 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
648 me->name, ELF32_R_TYPE(rel[i].r_info));
649 return -ENOEXEC;
653 return 0;
656 #else
657 int apply_relocate_add(Elf_Shdr *sechdrs,
658 const char *strtab,
659 unsigned int symindex,
660 unsigned int relsec,
661 struct module *me)
663 int i;
664 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
665 Elf64_Sym *sym;
666 Elf64_Word *loc;
667 Elf64_Xword *loc64;
668 Elf64_Addr val;
669 Elf64_Sxword addend;
670 Elf64_Addr dot;
671 Elf_Addr loc0;
672 unsigned int targetsec = sechdrs[relsec].sh_info;
674 DEBUGP("Applying relocate section %u to %u\n", relsec,
675 targetsec);
676 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
677 /* This is where to make the change */
678 loc = (void *)sechdrs[targetsec].sh_addr
679 + rel[i].r_offset;
680 /* This is the start of the target section */
681 loc0 = sechdrs[targetsec].sh_addr;
682 /* This is the symbol it is referring to */
683 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
684 + ELF64_R_SYM(rel[i].r_info);
685 if (!sym->st_value) {
686 printk(KERN_WARNING "%s: Unknown symbol %s\n",
687 me->name, strtab + sym->st_name);
688 return -ENOENT;
690 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
691 dot = (Elf64_Addr)loc & ~0x03;
692 loc64 = (Elf64_Xword *)loc;
694 val = sym->st_value;
695 addend = rel[i].r_addend;
698 switch (ELF64_R_TYPE(rel[i].r_info)) {
699 case R_PARISC_LTOFF21L:
700 /* LT-relative; left 21 bits */
701 val = get_got(me, val, addend);
702 DEBUGP("LTOFF21L Symbol %s loc %p val %lx\n",
703 strtab + sym->st_name,
704 loc, val);
705 val = lrsel(val, 0);
706 *loc = mask(*loc, 21) | reassemble_21(val);
707 break;
708 case R_PARISC_LTOFF14R:
709 /* L(ltoff(val+addend)) */
710 /* LT-relative; right 14 bits */
711 val = get_got(me, val, addend);
712 val = rrsel(val, 0);
713 DEBUGP("LTOFF14R Symbol %s loc %p val %lx\n",
714 strtab + sym->st_name,
715 loc, val);
716 *loc = mask(*loc, 14) | reassemble_14(val);
717 break;
718 case R_PARISC_PCREL22F:
719 /* PC-relative; 22 bits */
720 DEBUGP("PCREL22F Symbol %s loc %p val %lx\n",
721 strtab + sym->st_name,
722 loc, val);
723 val += addend;
724 /* can we reach it locally? */
725 if (in_local(me, (void *)val)) {
726 /* this is the case where the symbol is local
727 * to the module, but in a different section,
728 * so stub the jump in case it's more than 22
729 * bits away */
730 val = (val - dot - 8)/4;
731 if (!RELOC_REACHABLE(val, 22)) {
732 /* direct distance too far, create
733 * stub entry instead */
734 val = get_stub(me, sym->st_value,
735 addend, ELF_STUB_DIRECT,
736 loc0, targetsec);
737 } else {
738 /* Ok, we can reach it directly. */
739 val = sym->st_value;
740 val += addend;
742 } else {
743 val = sym->st_value;
744 if (strncmp(strtab + sym->st_name, "$$", 2)
745 == 0)
746 val = get_stub(me, val, addend, ELF_STUB_MILLI,
747 loc0, targetsec);
748 else
749 val = get_stub(me, val, addend, ELF_STUB_GOT,
750 loc0, targetsec);
752 DEBUGP("STUB FOR %s loc %lx, val %lx+%lx at %lx\n",
753 strtab + sym->st_name, loc, sym->st_value,
754 addend, val);
755 val = (val - dot - 8)/4;
756 CHECK_RELOC(val, 22);
757 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
758 break;
759 case R_PARISC_DIR64:
760 /* 64-bit effective address */
761 *loc64 = val + addend;
762 break;
763 case R_PARISC_SEGREL32:
764 /* 32-bit segment relative address */
765 /* See note about special handling of SEGREL32 at
766 * the beginning of this file.
768 *loc = fsel(val, addend);
769 break;
770 case R_PARISC_FPTR64:
771 /* 64-bit function address */
772 if(in_local(me, (void *)(val + addend))) {
773 *loc64 = get_fdesc(me, val+addend);
774 DEBUGP("FDESC for %s at %p points to %lx\n",
775 strtab + sym->st_name, *loc64,
776 ((Elf_Fdesc *)*loc64)->addr);
777 } else {
778 /* if the symbol is not local to this
779 * module then val+addend is a pointer
780 * to the function descriptor */
781 DEBUGP("Non local FPTR64 Symbol %s loc %p val %lx\n",
782 strtab + sym->st_name,
783 loc, val);
784 *loc64 = val + addend;
786 break;
788 default:
789 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
790 me->name, ELF64_R_TYPE(rel[i].r_info));
791 return -ENOEXEC;
794 return 0;
796 #endif
798 static void
799 register_unwind_table(struct module *me,
800 const Elf_Shdr *sechdrs)
802 unsigned char *table, *end;
803 unsigned long gp;
805 if (!me->arch.unwind_section)
806 return;
808 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
809 end = table + sechdrs[me->arch.unwind_section].sh_size;
810 gp = (Elf_Addr)me->module_core + me->arch.got_offset;
812 DEBUGP("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
813 me->arch.unwind_section, table, end, gp);
814 me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
817 static void
818 deregister_unwind_table(struct module *me)
820 if (me->arch.unwind)
821 unwind_table_remove(me->arch.unwind);
824 int module_finalize(const Elf_Ehdr *hdr,
825 const Elf_Shdr *sechdrs,
826 struct module *me)
828 int i;
829 unsigned long nsyms;
830 const char *strtab = NULL;
831 Elf_Sym *newptr, *oldptr;
832 Elf_Shdr *symhdr = NULL;
833 #ifdef DEBUG
834 Elf_Fdesc *entry;
835 u32 *addr;
837 entry = (Elf_Fdesc *)me->init;
838 printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
839 entry->gp, entry->addr);
840 addr = (u32 *)entry->addr;
841 printk("INSNS: %x %x %x %x\n",
842 addr[0], addr[1], addr[2], addr[3]);
843 printk("got entries used %ld, gots max %ld\n"
844 "fdescs used %ld, fdescs max %ld\n",
845 me->arch.got_count, me->arch.got_max,
846 me->arch.fdesc_count, me->arch.fdesc_max);
847 #endif
849 register_unwind_table(me, sechdrs);
851 /* haven't filled in me->symtab yet, so have to find it
852 * ourselves */
853 for (i = 1; i < hdr->e_shnum; i++) {
854 if(sechdrs[i].sh_type == SHT_SYMTAB
855 && (sechdrs[i].sh_flags & SHF_ALLOC)) {
856 int strindex = sechdrs[i].sh_link;
857 symhdr = (Elf_Shdr *)&sechdrs[i];
858 strtab = (char *)sechdrs[strindex].sh_addr;
859 break;
863 DEBUGP("module %s: strtab %p, symhdr %p\n",
864 me->name, strtab, symhdr);
866 if(me->arch.got_count > MAX_GOTS) {
867 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
868 me->name, me->arch.got_count, MAX_GOTS);
869 return -EINVAL;
872 kfree(me->arch.section);
873 me->arch.section = NULL;
875 /* no symbol table */
876 if(symhdr == NULL)
877 return 0;
879 oldptr = (void *)symhdr->sh_addr;
880 newptr = oldptr + 1; /* we start counting at 1 */
881 nsyms = symhdr->sh_size / sizeof(Elf_Sym);
882 DEBUGP("OLD num_symtab %lu\n", nsyms);
884 for (i = 1; i < nsyms; i++) {
885 oldptr++; /* note, count starts at 1 so preincrement */
886 if(strncmp(strtab + oldptr->st_name,
887 ".L", 2) == 0)
888 continue;
890 if(newptr != oldptr)
891 *newptr++ = *oldptr;
892 else
893 newptr++;
896 nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
897 DEBUGP("NEW num_symtab %lu\n", nsyms);
898 symhdr->sh_size = nsyms * sizeof(Elf_Sym);
899 return 0;
902 void module_arch_cleanup(struct module *mod)
904 deregister_unwind_table(mod);