58 #include <linux/elf.h>
61 #include <linux/string.h>
62 #include <linux/kernel.h>
65 #include <linux/slab.h>
67 #include <asm/pgtable.h>
68 #include <asm/unwind.h>
73 #define DEBUGP(fmt...)
76 #define RELOC_REACHABLE(val, bits) \
77 (( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 ) || \
78 ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \
81 #define CHECK_RELOC(val, bits) \
82 if (!RELOC_REACHABLE(val, bits)) { \
83 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
84 me->name, strtab + sym->st_name, (unsigned long)val, bits); \
101 static inline int in_init(
struct module *me,
void *
loc)
103 return (loc >= me->module_init &&
104 loc <= (me->module_init + me->init_size));
107 static inline int in_core(
struct module *me,
void *
loc)
109 return (loc >= me->module_core &&
110 loc <= (me->module_core + me->core_size));
113 static inline int in_local(
struct module *me,
void *loc)
115 return in_init(me, loc) || in_core(me, loc);
137 #define rnd(x) (((x)+0x1000)&~0x1fff)
139 #define fsel(v,a) ((v)+(a))
141 #define lsel(v,a) (((v)+(a))>>11)
143 #define rsel(v,a) (((v)+(a))&0x7ff)
145 #define lrsel(v,a) (((v)+rnd(a))>>11)
147 #define rrsel(v,a) ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
149 #define mask(x,sz) ((x) & ~((1<<(sz))-1))
155 static inline int sign_unext(
int x,
int len)
159 len_ones = (1 << len) - 1;
163 static inline int low_sign_unext(
int x,
int len)
167 sign = (x >> (len-1)) & 1;
168 temp = sign_unext(x, len-1);
169 return (temp << 1) |
sign;
172 static inline int reassemble_14(
int as14)
174 return (((as14 & 0x1fff) << 1) |
175 ((as14 & 0x2000) >> 13));
178 static inline int reassemble_16a(
int as16)
183 t = (as16 << 1) & 0xffff;
185 return (t ^ s ^ (s >> 1)) | (s >> 15);
189 static inline int reassemble_17(
int as17)
191 return (((as17 & 0x10000) >> 16) |
192 ((as17 & 0x0f800) << 5) |
193 ((as17 & 0x00400) >> 8) |
194 ((as17 & 0x003ff) << 3));
197 static inline int reassemble_21(
int as21)
199 return (((as21 & 0x100000) >> 20) |
200 ((as21 & 0x0ffe00) >> 8) |
201 ((as21 & 0x000180) << 7) |
202 ((as21 & 0x00007c) << 14) |
203 ((as21 & 0x000003) << 12));
206 static inline int reassemble_22(
int as22)
208 return (((as22 & 0x200000) >> 21) |
209 ((as22 & 0x1f0000) << 5) |
210 ((as22 & 0x00f800) << 5) |
211 ((as22 & 0x000400) >> 8) |
212 ((as22 & 0x0003ff) << 3));
225 __builtin_return_address(0));
229 static inline unsigned long count_gots(
const Elf_Rela *rela,
unsigned long n)
234 static inline unsigned long count_fdescs(
const Elf_Rela *rela,
unsigned long n)
239 static inline unsigned long count_stubs(
const Elf_Rela *rela,
unsigned long n)
241 unsigned long cnt = 0;
243 for (; n > 0; n--, rela++)
255 static inline unsigned long count_gots(
const Elf_Rela *rela,
unsigned long n)
257 unsigned long cnt = 0;
259 for (; n > 0; n--, rela++)
272 static inline unsigned long count_fdescs(
const Elf_Rela *rela,
unsigned long n)
274 unsigned long cnt = 0;
276 for (; n > 0; n--, rela++)
287 static inline unsigned long count_stubs(
const Elf_Rela *rela,
unsigned long n)
289 unsigned long cnt = 0;
291 for (; n > 0; n--, rela++)
307 kfree(mod->arch.section);
308 mod->arch.section =
NULL;
310 vfree(module_region);
319 return (mod->arch.section[section].stub_entries + 1)
326 CONST char *secstrings,
329 unsigned long gots = 0, fdescs = 0, len;
332 len = hdr->e_shnum *
sizeof(me->arch.section[0]);
334 if (!me->arch.section)
337 for (i = 1; i < hdr->e_shnum; i++) {
338 const Elf_Rela *rels = (
void *)sechdrs[i].sh_addr;
339 unsigned long nrels = sechdrs[
i].sh_size /
sizeof(*rels);
342 if (
strncmp(secstrings + sechdrs[i].sh_name,
343 ".PARISC.unwind", 14) == 0)
344 me->arch.unwind_section = i;
354 gots += count_gots(rels, nrels);
355 fdescs += count_fdescs(rels, nrels);
360 count = count_stubs(rels, nrels);
366 s = sechdrs[
i].sh_info;
369 WARN_ON(me->arch.section[s].stub_entries);
372 me->arch.section[
s].stub_entries +=
count;
376 me->core_size =
ALIGN(me->core_size, 16);
377 me->arch.got_offset = me->core_size;
378 me->core_size += gots *
sizeof(
struct got_entry);
380 me->core_size =
ALIGN(me->core_size, 16);
381 me->arch.fdesc_offset = me->core_size;
382 me->core_size += fdescs *
sizeof(Elf_Fdesc);
384 me->arch.got_max = gots;
385 me->arch.fdesc_max = fdescs;
400 got = me->module_core + me->arch.got_offset;
401 for (i = 0; got[
i].
addr; i++)
402 if (got[i].
addr == value)
405 BUG_ON(++me->arch.got_count > me->arch.got_max);
418 Elf_Fdesc *
fdesc = me->module_core + me->arch.fdesc_offset;
426 while (fdesc->addr) {
427 if (fdesc->addr == value)
432 BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
436 fdesc->gp = (
Elf_Addr)me->module_core + me->arch.got_offset;
447 static Elf_Addr get_stub(
struct module *me,
unsigned long value,
long addend,
454 if (!me->arch.section[targetsec].stub_offset) {
455 loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
459 me->arch.section[targetsec].stub_offset = loc0;
463 stub = (
void *) me->arch.section[targetsec].stub_offset;
464 me->arch.section[targetsec].stub_offset +=
sizeof(
struct stub_entry);
467 BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
477 stub->
insns[0] = 0x20200000;
478 stub->
insns[1] = 0xe0202002;
480 stub->
insns[0] |= reassemble_21(
lrsel(value, addend));
481 stub->
insns[1] |= reassemble_17(
rrsel(value, addend) / 4);
505 d = get_got(me, value, addend);
508 stub->
insns[0] = 0x0f6010db;
509 stub->
insns[0] |= low_sign_unext(d, 5) << 16;
512 stub->
insns[0] = 0x537b0000;
513 stub->
insns[0] |= reassemble_16a(d);
515 stub->
insns[1] = 0x53610020;
516 stub->
insns[2] = 0xe820d000;
517 stub->
insns[3] = 0x537b0030;
520 stub->
insns[0] = 0x20200000;
521 stub->
insns[1] = 0x34210000;
522 stub->
insns[2] = 0x50210020;
523 stub->
insns[3] = 0xe820d002;
525 stub->
insns[0] |= reassemble_21(
lrsel(value, addend));
526 stub->
insns[1] |= reassemble_14(
rrsel(value, addend));
529 stub->
insns[0] = 0x20200000;
530 stub->
insns[1] = 0x34210000;
531 stub->
insns[2] = 0xe820d002;
533 stub->
insns[0] |= reassemble_21(
lrsel(value, addend));
534 stub->
insns[1] |= reassemble_14(
rrsel(value, addend));
546 unsigned int symindex,
551 Elf32_Rela *rel = (
void *)sechdrs[relsec].sh_addr;
558 unsigned int targetsec = sechdrs[relsec].sh_info;
560 register unsigned long dp asm (
"r27");
562 DEBUGP(
"Applying relocate section %u to %u\n", relsec,
564 for (i = 0; i < sechdrs[relsec].sh_size /
sizeof(*rel); i++) {
566 loc = (
void *)sechdrs[targetsec].sh_addr
569 loc0 = sechdrs[targetsec].sh_addr;
571 sym = (
Elf32_Sym *)sechdrs[symindex].sh_addr
585 #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
586 DEBUGP(
"Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
606 *loc =
fsel(val, addend);
610 *loc =
fsel(val, addend);
614 val =
lrsel(val, addend);
615 *loc =
mask(*loc, 21) | reassemble_21(val);
619 val =
rrsel(val, addend);
620 *loc =
mask(*loc, 14) | reassemble_14(val);
627 *loc =
fsel(val, addend);
632 *loc =
mask(*loc, 21) | reassemble_21(val);
637 *loc =
mask(*loc, 14) | reassemble_14(val);
643 val = (val - dot - 8)/4;
647 val = get_stub(me, sym->
st_value, addend,
649 val = (val - dot - 8)/4;
652 *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
658 val = (val - dot - 8)/4;
662 val = get_stub(me, sym->
st_value, addend,
664 val = (val - dot - 8)/4;
667 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
683 unsigned int symindex,
688 Elf64_Rela *rel = (
void *)sechdrs[relsec].sh_addr;
696 unsigned int targetsec = sechdrs[relsec].sh_info;
698 DEBUGP(
"Applying relocate section %u to %u\n", relsec,
700 for (i = 0; i < sechdrs[relsec].sh_size /
sizeof(*rel); i++) {
702 loc = (
void *)sechdrs[targetsec].sh_addr
705 loc0 = sechdrs[targetsec].sh_addr;
707 sym = (
Elf64_Sym *)sechdrs[symindex].sh_addr
722 #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
723 printk(
"Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
739 val = get_got(me, val, addend);
740 DEBUGP(
"LTOFF21L Symbol %s loc %p val %lx\n",
744 *loc =
mask(*loc, 21) | reassemble_21(val);
749 val = get_got(me, val, addend);
751 DEBUGP(
"LTOFF14R Symbol %s loc %p val %lx\n",
754 *loc =
mask(*loc, 14) | reassemble_14(val);
758 DEBUGP(
"PCREL22F Symbol %s loc %p val %lx\n",
763 if (in_local(me, (
void *)val)) {
768 val = (val - dot - 8)/4;
790 DEBUGP(
"STUB FOR %s loc %lx, val %lx+%lx at %lx\n",
793 val = (val - dot - 8)/4;
795 *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
799 *loc64 = val + addend;
806 *loc =
fsel(val, addend);
810 if(in_local(me, (
void *)(val + addend))) {
811 *loc64 = get_fdesc(me, val+addend);
812 DEBUGP(
"FDESC for %s at %p points to %lx\n",
814 ((Elf_Fdesc *)*loc64)->addr);
819 DEBUGP(
"Non local FPTR64 Symbol %s loc %p val %lx\n",
822 *loc64 = val + addend;
837 register_unwind_table(
struct module *me,
843 if (!me->arch.unwind_section)
846 table = (
unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
847 end = table + sechdrs[me->arch.unwind_section].sh_size;
848 gp = (
Elf_Addr)me->module_core + me->arch.got_offset;
850 DEBUGP(
"register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
851 me->arch.unwind_section, table, end, gp);
856 deregister_unwind_table(
struct module *me)
868 const char *strtab =
NULL;
875 entry = (Elf_Fdesc *)me->init;
876 printk(
"FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
877 entry->gp, entry->addr);
878 addr = (
u32 *)entry->addr;
879 printk(
"INSNS: %x %x %x %x\n",
880 addr[0], addr[1], addr[2], addr[3]);
881 printk(
"got entries used %ld, gots max %ld\n"
882 "fdescs used %ld, fdescs max %ld\n",
883 me->arch.got_count, me->arch.got_max,
884 me->arch.fdesc_count, me->arch.fdesc_max);
887 register_unwind_table(me, sechdrs);
891 for (i = 1; i < hdr->e_shnum; i++) {
894 int strindex = sechdrs[
i].sh_link;
899 strtab = (
char *)sechdrs[strindex].sh_addr;
904 DEBUGP(
"module %s: strtab %p, symhdr %p\n",
905 me->
name, strtab, symhdr);
908 printk(
KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
913 kfree(me->arch.section);
914 me->arch.section =
NULL;
920 oldptr = (
void *)symhdr->sh_addr;
922 nsyms = symhdr->sh_size /
sizeof(
Elf_Sym);
923 DEBUGP(
"OLD num_symtab %lu\n", nsyms);
925 for (i = 1; i < nsyms; i++) {
927 if(
strncmp(strtab + oldptr->st_name,
937 nsyms = newptr - (
Elf_Sym *)symhdr->sh_addr;
938 DEBUGP(
"NEW num_symtab %lu\n", nsyms);
939 symhdr->sh_size = nsyms *
sizeof(
Elf_Sym);
945 deregister_unwind_table(mod);