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>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * We are not doing SEGREL32 handling correctly. According to the ABI, we
29 * should do a value offset, like this:
30 * if (is_init(me, (void *)val))
31 * val -= (uint32_t)me->module_init;
33 * val -= (uint32_t)me->module_core;
34 * However, SEGREL32 is used only for PARISC unwind entries, and we want
35 * those entries to have an absolute address, and not just an offset.
37 * The unwind table mechanism has the ability to specify an offset for
38 * the unwind table; however, because we split off the init functions into
39 * a different piece of memory, it is not possible to do this using a
40 * single offset. Instead, we use the above hack for now.
43 #include <linux/moduleloader.h>
44 #include <linux/elf.h>
45 #include <linux/vmalloc.h>
47 #include <linux/string.h>
48 #include <linux/kernel.h>
50 #include <asm/unwind.h>
55 #define DEBUGP(fmt...)
58 #define CHECK_RELOC(val, bits) \
59 if ( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \
60 ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) { \
61 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
62 me->name, strtab + sym->st_name, (unsigned long)val, bits); \
66 /* Maximum number of GOT entries. We use a long displacement ldd from
67 * the bottom of the table, which has a maximum signed displacement of
68 * 0x3fff; however, since we're only going forward, this becomes
69 * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
70 * at most 1023 entries */
73 /* three functions to determine where in the module core
74 * or init pieces the location is */
75 static inline int is_init(struct module *me, void *loc)
77 return (loc >= me->module_init &&
78 loc <= (me->module_init + me->init_size));
81 static inline int is_core(struct module *me, void *loc)
83 return (loc >= me->module_core &&
84 loc <= (me->module_core + me->core_size));
87 static inline int is_local(struct module *me, void *loc)
89 return is_init(me, loc) || is_core(me, loc);
98 #define Elf_Fdesc Elf32_Fdesc
101 Elf32_Word insns[2]; /* each stub entry has two insns */
108 #define Elf_Fdesc Elf64_Fdesc
111 Elf64_Word insns[4]; /* each stub entry has four insns */
115 /* Field selection types defined by hppa */
116 #define rnd(x) (((x)+0x1000)&~0x1fff)
117 /* fsel: full 32 bits */
118 #define fsel(v,a) ((v)+(a))
119 /* lsel: select left 21 bits */
120 #define lsel(v,a) (((v)+(a))>>11)
121 /* rsel: select right 11 bits */
122 #define rsel(v,a) (((v)+(a))&0x7ff)
123 /* lrsel with rounding of addend to nearest 8k */
124 #define lrsel(v,a) (((v)+rnd(a))>>11)
125 /* rrsel with rounding of addend to nearest 8k */
126 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
128 #define mask(x,sz) ((x) & ~((1<<(sz))-1))
131 /* The reassemble_* functions prepare an immediate value for
132 insertion into an opcode. pa-risc uses all sorts of weird bitfields
133 in the instruction to hold the value. */
134 static inline int reassemble_14(int as14)
136 return (((as14 & 0x1fff) << 1) |
137 ((as14 & 0x2000) >> 13));
140 static inline int reassemble_17(int as17)
142 return (((as17 & 0x10000) >> 16) |
143 ((as17 & 0x0f800) << 5) |
144 ((as17 & 0x00400) >> 8) |
145 ((as17 & 0x003ff) << 3));
148 static inline int reassemble_21(int as21)
150 return (((as21 & 0x100000) >> 20) |
151 ((as21 & 0x0ffe00) >> 8) |
152 ((as21 & 0x000180) << 7) |
153 ((as21 & 0x00007c) << 14) |
154 ((as21 & 0x000003) << 12));
157 static inline int reassemble_22(int as22)
159 return (((as22 & 0x200000) >> 21) |
160 ((as22 & 0x1f0000) << 5) |
161 ((as22 & 0x00f800) << 5) |
162 ((as22 & 0x000400) >> 8) |
163 ((as22 & 0x0003ff) << 3));
166 void *module_alloc(unsigned long size)
170 return vmalloc(size);
174 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
179 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
184 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
186 unsigned long cnt = 0;
188 for (; n > 0; n--, rela++)
190 switch (ELF32_R_TYPE(rela->r_info)) {
191 case R_PARISC_PCREL17F:
192 case R_PARISC_PCREL22F:
200 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
202 unsigned long cnt = 0;
204 for (; n > 0; n--, rela++)
206 switch (ELF64_R_TYPE(rela->r_info)) {
207 case R_PARISC_LTOFF21L:
208 case R_PARISC_LTOFF14R:
209 case R_PARISC_PCREL22F:
217 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
219 unsigned long cnt = 0;
221 for (; n > 0; n--, rela++)
223 switch (ELF64_R_TYPE(rela->r_info)) {
224 case R_PARISC_FPTR64:
232 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
234 unsigned long cnt = 0;
236 for (; n > 0; n--, rela++)
238 switch (ELF64_R_TYPE(rela->r_info)) {
239 case R_PARISC_PCREL22F:
249 /* Free memory returned from module_alloc */
250 void module_free(struct module *mod, void *module_region)
252 vfree(module_region);
253 /* FIXME: If module_region == mod->init_region, trim exception
258 int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
259 CONST Elf_Shdr *sechdrs,
260 CONST char *secstrings,
263 unsigned long gots = 0, fdescs = 0, stubs = 0, init_stubs = 0;
266 for (i = 1; i < hdr->e_shnum; i++) {
267 const Elf_Rela *rels = (void *)hdr + sechdrs[i].sh_offset;
268 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
270 if (strncmp(secstrings + sechdrs[i].sh_name,
271 ".PARISC.unwind", 14) == 0)
272 me->arch.unwind_section = i;
274 if (sechdrs[i].sh_type != SHT_RELA)
277 /* some of these are not relevant for 32-bit/64-bit
278 * we leave them here to make the code common. the
279 * compiler will do its thing and optimize out the
280 * stuff we don't need
282 gots += count_gots(rels, nrels);
283 fdescs += count_fdescs(rels, nrels);
284 if(strncmp(secstrings + sechdrs[i].sh_name,
285 ".rela.init", 10) == 0)
286 init_stubs += count_stubs(rels, nrels);
288 stubs += count_stubs(rels, nrels);
291 /* align things a bit */
292 me->core_size = ALIGN(me->core_size, 16);
293 me->arch.got_offset = me->core_size;
294 me->core_size += gots * sizeof(struct got_entry);
296 me->core_size = ALIGN(me->core_size, 16);
297 me->arch.fdesc_offset = me->core_size;
298 me->core_size += fdescs * sizeof(Elf_Fdesc);
300 me->core_size = ALIGN(me->core_size, 16);
301 me->arch.stub_offset = me->core_size;
302 me->core_size += stubs * sizeof(struct stub_entry);
304 me->init_size = ALIGN(me->init_size, 16);
305 me->arch.init_stub_offset = me->init_size;
306 me->init_size += init_stubs * sizeof(struct stub_entry);
308 me->arch.got_max = gots;
309 me->arch.fdesc_max = fdescs;
310 me->arch.stub_max = stubs;
311 me->arch.init_stub_max = init_stubs;
317 static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
320 struct got_entry *got;
326 got = me->module_core + me->arch.got_offset;
327 for (i = 0; got[i].addr; i++)
328 if (got[i].addr == value)
331 BUG_ON(++me->arch.got_count > me->arch.got_max);
335 DEBUGP("GOT ENTRY %d[%x] val %lx\n", i, i*sizeof(struct got_entry),
337 return i * sizeof(struct got_entry);
339 #endif /* __LP64__ */
342 static Elf_Addr get_fdesc(struct module *me, unsigned long value)
344 Elf_Fdesc *fdesc = me->module_core + me->arch.fdesc_offset;
347 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
351 /* Look for existing fdesc entry. */
352 while (fdesc->addr) {
353 if (fdesc->addr == value)
354 return (Elf_Addr)fdesc;
358 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
362 fdesc->gp = (Elf_Addr)me->module_core + me->arch.got_offset;
363 return (Elf_Addr)fdesc;
365 #endif /* __LP64__ */
367 static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
368 int millicode, int init_section)
371 struct stub_entry *stub;
374 i = me->arch.init_stub_count++;
375 BUG_ON(me->arch.init_stub_count > me->arch.init_stub_max);
376 stub = me->module_init + me->arch.init_stub_offset +
377 i * sizeof(struct stub_entry);
379 i = me->arch.stub_count++;
380 BUG_ON(me->arch.stub_count > me->arch.stub_max);
381 stub = me->module_core + me->arch.stub_offset +
382 i * sizeof(struct stub_entry);
386 /* for 32-bit the stub looks like this:
388 * be,n R'XXX(%sr4,%r1)
390 //value = *(unsigned long *)((value + addend) & ~3); /* why? */
392 stub->insns[0] = 0x20200000; /* ldil L'XXX,%r1 */
393 stub->insns[1] = 0xe0202002; /* be,n R'XXX(%sr4,%r1) */
395 stub->insns[0] |= reassemble_21(lrsel(value, addend));
396 stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
399 /* for 64-bit we have two kinds of stubs:
400 * for normal function calls:
414 stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp */
415 stub->insns[1] = 0x53610020; /* ldd 10(%dp),%r1 */
416 stub->insns[2] = 0xe820d000; /* bve (%r1) */
417 stub->insns[3] = 0x537b0030; /* ldd 18(%dp),%dp */
419 stub->insns[0] |= reassemble_14(get_got(me, value, addend) & 0x3fff);
423 stub->insns[0] = 0x20200000; /* ldil 0,%r1 */
424 stub->insns[1] = 0x34210000; /* ldo 0(%r1), %r1 */
425 stub->insns[2] = 0x50210020; /* ldd 10(%r1),%r1 */
426 stub->insns[3] = 0xe820d002; /* bve,n (%r1) */
428 stub->insns[0] |= reassemble_21(lrsel(value, addend));
429 stub->insns[1] |= reassemble_14(rrsel(value, addend));
433 return (Elf_Addr)stub;
436 int apply_relocate(Elf_Shdr *sechdrs,
438 unsigned int symindex,
442 /* parisc should not need this ... */
443 printk(KERN_ERR "module %s: RELOCATION unsupported\n",
449 int apply_relocate_add(Elf_Shdr *sechdrs,
451 unsigned int symindex,
456 Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
462 //unsigned long dp = (unsigned long)$global$;
463 register unsigned long dp asm ("r27");
465 DEBUGP("Applying relocate section %u to %u\n", relsec,
466 sechdrs[relsec].sh_info);
467 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
468 /* This is where to make the change */
469 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
471 /* This is the symbol it is referring to */
472 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
473 + ELF32_R_SYM(rel[i].r_info);
474 if (!sym->st_value) {
475 printk(KERN_WARNING "%s: Unknown symbol %s\n",
476 me->name, strtab + sym->st_name);
479 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
480 dot = (Elf32_Addr)loc & ~0x03;
483 addend = rel[i].r_addend;
486 #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
487 DEBUGP("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
488 strtab + sym->st_name,
489 (uint32_t)loc, val, addend,
503 switch (ELF32_R_TYPE(rel[i].r_info)) {
504 case R_PARISC_PLABEL32:
505 /* 32-bit function address */
506 /* no function descriptors... */
507 *loc = fsel(val, addend);
510 /* direct 32-bit ref */
511 *loc = fsel(val, addend);
513 case R_PARISC_DIR21L:
514 /* left 21 bits of effective address */
515 val = lrsel(val, addend);
516 *loc = mask(*loc, 21) | reassemble_21(val);
518 case R_PARISC_DIR14R:
519 /* right 14 bits of effective address */
520 val = rrsel(val, addend);
521 *loc = mask(*loc, 14) | reassemble_14(val);
523 case R_PARISC_SEGREL32:
524 /* 32-bit segment relative address */
525 /* See note about special handling of SEGREL32 at
526 * the beginning of this file.
528 *loc = fsel(val, addend);
530 case R_PARISC_DPREL21L:
531 /* left 21 bit of relative address */
532 val = lrsel(val - dp, addend);
533 *loc = mask(*loc, 21) | reassemble_21(val);
535 case R_PARISC_DPREL14R:
536 /* right 14 bit of relative address */
537 val = rrsel(val - dp, addend);
538 *loc = mask(*loc, 14) | reassemble_14(val);
540 case R_PARISC_PCREL17F:
541 /* 17-bit PC relative address */
542 val = get_stub(me, val, addend, 0, is_init(me, loc));
543 val = (val - dot - 8)/4;
545 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
547 case R_PARISC_PCREL22F:
548 /* 22-bit PC relative address; only defined for pa20 */
549 val = get_stub(me, val, addend, 0, is_init(me, loc));
550 DEBUGP("STUB FOR %s loc %lx+%lx at %lx\n",
551 strtab + sym->st_name, (unsigned long)loc, addend,
553 val = (val - dot - 8)/4;
554 CHECK_RELOC(val, 22);
555 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
559 printk(KERN_ERR "module %s: Unknown relocation: %u\n",
560 me->name, ELF32_R_TYPE(rel[i].r_info));
569 int apply_relocate_add(Elf_Shdr *sechdrs,
571 unsigned int symindex,
576 Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
584 DEBUGP("Applying relocate section %u to %u\n", relsec,
585 sechdrs[relsec].sh_info);
586 for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
587 /* This is where to make the change */
588 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
590 /* This is the symbol it is referring to */
591 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
592 + ELF64_R_SYM(rel[i].r_info);
593 if (!sym->st_value) {
594 printk(KERN_WARNING "%s: Unknown symbol %s\n",
595 me->name, strtab + sym->st_name);
598 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
599 dot = (Elf64_Addr)loc & ~0x03;
600 loc64 = (Elf64_Xword *)loc;
603 addend = rel[i].r_addend;
606 #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
607 printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
608 strtab + sym->st_name,
620 switch (ELF64_R_TYPE(rel[i].r_info)) {
621 case R_PARISC_LTOFF21L:
622 /* LT-relative; left 21 bits */
623 val = get_got(me, val, addend);
624 DEBUGP("LTOFF21L Symbol %s loc %p val %lx\n",
625 strtab + sym->st_name,
628 *loc = mask(*loc, 21) | reassemble_21(val);
630 case R_PARISC_LTOFF14R:
631 /* L(ltoff(val+addend)) */
632 /* LT-relative; right 14 bits */
633 val = get_got(me, val, addend);
635 DEBUGP("LTOFF14R Symbol %s loc %p val %lx\n",
636 strtab + sym->st_name,
638 *loc = mask(*loc, 14) | reassemble_14(val);
640 case R_PARISC_PCREL22F:
641 /* PC-relative; 22 bits */
642 DEBUGP("PCREL22F Symbol %s loc %p val %lx\n",
643 strtab + sym->st_name,
645 /* can we reach it locally? */
646 if(!is_local(me, (void *)val)) {
647 if (strncmp(strtab + sym->st_name, "$$", 2)
649 val = get_stub(me, val, addend, 1,
652 val = get_stub(me, val, addend, 0,
655 DEBUGP("STUB FOR %s loc %lx, val %lx+%lx at %lx\n",
656 strtab + sym->st_name, loc, sym->st_value,
658 /* FIXME: local symbols work as long as the
659 * core and init pieces aren't separated too
660 * far. If this is ever broken, you will trip
661 * the check below. The way to fix it would
662 * be to generate local stubs to go between init
664 if((Elf64_Sxword)(val - dot - 8) > 0x800000 -1 ||
665 (Elf64_Sxword)(val - dot - 8) < -0x800000) {
666 printk(KERN_ERR "Module %s, symbol %s is out of range for PCREL22F relocation\n",
667 me->name, strtab + sym->st_name);
670 val = (val - dot - 8)/4;
671 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
674 /* 64-bit effective address */
675 *loc64 = val + addend;
677 case R_PARISC_SEGREL32:
678 /* 32-bit segment relative address */
679 /* See note about special handling of SEGREL32 at
680 * the beginning of this file.
682 *loc = fsel(val, addend);
684 case R_PARISC_FPTR64:
685 /* 64-bit function address */
686 if(is_local(me, (void *)(val + addend))) {
687 *loc64 = get_fdesc(me, val+addend);
688 DEBUGP("FDESC for %s at %p points to %lx\n",
689 strtab + sym->st_name, *loc64,
690 ((Elf_Fdesc *)*loc64)->addr);
692 /* if the symbol is not local to this
693 * module then val+addend is a pointer
694 * to the function descriptor */
695 DEBUGP("Non local FPTR64 Symbol %s loc %p val %lx\n",
696 strtab + sym->st_name,
698 *loc64 = val + addend;
703 printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
704 me->name, ELF64_R_TYPE(rel[i].r_info));
713 register_unwind_table(struct module *me,
714 const Elf_Shdr *sechdrs)
716 unsigned char *table, *end;
719 if (!me->arch.unwind_section)
722 table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
723 end = table + sechdrs[me->arch.unwind_section].sh_size;
724 gp = (Elf_Addr)me->module_core + me->arch.got_offset;
726 DEBUGP("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
727 me->arch.unwind_section, table, end, gp);
728 me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
732 deregister_unwind_table(struct module *me)
735 unwind_table_remove(me->arch.unwind);
738 int module_finalize(const Elf_Ehdr *hdr,
739 const Elf_Shdr *sechdrs,
744 const char *strtab = NULL;
745 Elf_Sym *newptr, *oldptr;
746 Elf_Shdr *symhdr = NULL;
751 entry = (Elf_Fdesc *)me->init;
752 printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
753 entry->gp, entry->addr);
754 addr = (u32 *)entry->addr;
755 printk("INSNS: %x %x %x %x\n",
756 addr[0], addr[1], addr[2], addr[3]);
757 printk("stubs used %ld, stubs max %ld\n"
758 "init_stubs used %ld, init stubs max %ld\n"
759 "got entries used %ld, gots max %ld\n"
760 "fdescs used %ld, fdescs max %ld\n",
761 me->arch.stub_count, me->arch.stub_max,
762 me->arch.init_stub_count, me->arch.init_stub_max,
763 me->arch.got_count, me->arch.got_max,
764 me->arch.fdesc_count, me->arch.fdesc_max);
767 register_unwind_table(me, sechdrs);
769 /* haven't filled in me->symtab yet, so have to find it
771 for (i = 1; i < hdr->e_shnum; i++) {
772 if(sechdrs[i].sh_type == SHT_SYMTAB
773 && (sechdrs[i].sh_type & SHF_ALLOC)) {
774 int strindex = sechdrs[i].sh_link;
776 * The cast is to drop the const from
777 * the sechdrs pointer */
778 symhdr = (Elf_Shdr *)&sechdrs[i];
779 strtab = (char *)sechdrs[strindex].sh_addr;
784 DEBUGP("module %s: strtab %p, symhdr %p\n",
785 me->name, strtab, symhdr);
787 if(me->arch.got_count > MAX_GOTS) {
788 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d\n", me->name, me->arch.got_count, MAX_GOTS);
792 /* no symbol table */
796 oldptr = (void *)symhdr->sh_addr;
797 newptr = oldptr + 1; /* we start counting at 1 */
798 nsyms = symhdr->sh_size / sizeof(Elf_Sym);
799 DEBUGP("OLD num_symtab %lu\n", nsyms);
801 for (i = 1; i < nsyms; i++) {
802 oldptr++; /* note, count starts at 1 so preincrement */
803 if(strncmp(strtab + oldptr->st_name,
813 nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
814 DEBUGP("NEW num_symtab %lu\n", nsyms);
815 symhdr->sh_size = nsyms * sizeof(Elf_Sym);
819 void module_arch_cleanup(struct module *mod)
821 deregister_unwind_table(mod);