Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[pandora-kernel.git] / arch / parisc / kernel / module.c
1 /*    Kernel dynamically loadable module help for PARISC.
2  *
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
6  *
7  *    Linux/PA-RISC Project (http://www.parisc-linux.org/)
8  *    Copyright (C) 2003 Randolph Chung <tausq at debian . org>
9  *
10  *
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.
15  *
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.
20  *
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
24  *
25  *
26  *    Notes:
27  *    - SEGREL32 handling
28  *      We are not doing SEGREL32 handling correctly. According to the ABI, we
29  *      should do a value offset, like this:
30  *                      if (in_init(me, (void *)val))
31  *                              val -= (uint32_t)me->module_init;
32  *                      else
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.
36  *
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.
41  */
42
43 #include <linux/moduleloader.h>
44 #include <linux/elf.h>
45 #include <linux/vmalloc.h>
46 #include <linux/fs.h>
47 #include <linux/string.h>
48 #include <linux/kernel.h>
49 #include <linux/bug.h>
50 #include <linux/uaccess.h>
51
52 #include <asm/sections.h>
53 #include <asm/unwind.h>
54
55 #if 0
56 #define DEBUGP printk
57 #else
58 #define DEBUGP(fmt...)
59 #endif
60
61 #define CHECK_RELOC(val, bits) \
62         if ( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 )  || \
63              ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) { \
64                 printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
65                 me->name, strtab + sym->st_name, (unsigned long)val, bits); \
66                 return -ENOEXEC;                        \
67         }
68
69 /* Maximum number of GOT entries. We use a long displacement ldd from
70  * the bottom of the table, which has a maximum signed displacement of
71  * 0x3fff; however, since we're only going forward, this becomes
72  * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
73  * at most 1023 entries */
74 #define MAX_GOTS        1023
75
76 /* three functions to determine where in the module core
77  * or init pieces the location is */
78 static inline int in_init(struct module *me, void *loc)
79 {
80         return (loc >= me->module_init &&
81                 loc <= (me->module_init + me->init_size));
82 }
83
84 static inline int in_core(struct module *me, void *loc)
85 {
86         return (loc >= me->module_core &&
87                 loc <= (me->module_core + me->core_size));
88 }
89
90 static inline int in_local(struct module *me, void *loc)
91 {
92         return in_init(me, loc) || in_core(me, loc);
93 }
94
95 static inline int in_local_section(struct module *me, void *loc, void *dot)
96 {
97         return (in_init(me, loc) && in_init(me, dot)) ||
98                 (in_core(me, loc) && in_core(me, dot));
99 }
100
101
102 #ifndef CONFIG_64BIT
103 struct got_entry {
104         Elf32_Addr addr;
105 };
106
107 #define Elf_Fdesc       Elf32_Fdesc
108
109 struct stub_entry {
110         Elf32_Word insns[2]; /* each stub entry has two insns */
111 };
112 #else
113 struct got_entry {
114         Elf64_Addr addr;
115 };
116
117 #define Elf_Fdesc       Elf64_Fdesc
118
119 struct stub_entry {
120         Elf64_Word insns[4]; /* each stub entry has four insns */
121 };
122 #endif
123
124 /* Field selection types defined by hppa */
125 #define rnd(x)                  (((x)+0x1000)&~0x1fff)
126 /* fsel: full 32 bits */
127 #define fsel(v,a)               ((v)+(a))
128 /* lsel: select left 21 bits */
129 #define lsel(v,a)               (((v)+(a))>>11)
130 /* rsel: select right 11 bits */
131 #define rsel(v,a)               (((v)+(a))&0x7ff)
132 /* lrsel with rounding of addend to nearest 8k */
133 #define lrsel(v,a)              (((v)+rnd(a))>>11)
134 /* rrsel with rounding of addend to nearest 8k */
135 #define rrsel(v,a)              ((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
136
137 #define mask(x,sz)              ((x) & ~((1<<(sz))-1))
138
139
140 /* The reassemble_* functions prepare an immediate value for
141    insertion into an opcode. pa-risc uses all sorts of weird bitfields
142    in the instruction to hold the value.  */
143 static inline int reassemble_14(int as14)
144 {
145         return (((as14 & 0x1fff) << 1) |
146                 ((as14 & 0x2000) >> 13));
147 }
148
149 static inline int reassemble_17(int as17)
150 {
151         return (((as17 & 0x10000) >> 16) |
152                 ((as17 & 0x0f800) << 5) |
153                 ((as17 & 0x00400) >> 8) |
154                 ((as17 & 0x003ff) << 3));
155 }
156
157 static inline int reassemble_21(int as21)
158 {
159         return (((as21 & 0x100000) >> 20) |
160                 ((as21 & 0x0ffe00) >> 8) |
161                 ((as21 & 0x000180) << 7) |
162                 ((as21 & 0x00007c) << 14) |
163                 ((as21 & 0x000003) << 12));
164 }
165
166 static inline int reassemble_22(int as22)
167 {
168         return (((as22 & 0x200000) >> 21) |
169                 ((as22 & 0x1f0000) << 5) |
170                 ((as22 & 0x00f800) << 5) |
171                 ((as22 & 0x000400) >> 8) |
172                 ((as22 & 0x0003ff) << 3));
173 }
174
175 void *module_alloc(unsigned long size)
176 {
177         if (size == 0)
178                 return NULL;
179         return vmalloc(size);
180 }
181
182 #ifndef CONFIG_64BIT
183 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
184 {
185         return 0;
186 }
187
188 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
189 {
190         return 0;
191 }
192
193 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
194 {
195         unsigned long cnt = 0;
196
197         for (; n > 0; n--, rela++)
198         {
199                 switch (ELF32_R_TYPE(rela->r_info)) {
200                         case R_PARISC_PCREL17F:
201                         case R_PARISC_PCREL22F:
202                                 cnt++;
203                 }
204         }
205
206         return cnt;
207 }
208 #else
209 static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
210 {
211         unsigned long cnt = 0;
212
213         for (; n > 0; n--, rela++)
214         {
215                 switch (ELF64_R_TYPE(rela->r_info)) {
216                         case R_PARISC_LTOFF21L:
217                         case R_PARISC_LTOFF14R:
218                         case R_PARISC_PCREL22F:
219                                 cnt++;
220                 }
221         }
222
223         return cnt;
224 }
225
226 static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
227 {
228         unsigned long cnt = 0;
229
230         for (; n > 0; n--, rela++)
231         {
232                 switch (ELF64_R_TYPE(rela->r_info)) {
233                         case R_PARISC_FPTR64:
234                                 cnt++;
235                 }
236         }
237
238         return cnt;
239 }
240
241 static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
242 {
243         unsigned long cnt = 0;
244
245         for (; n > 0; n--, rela++)
246         {
247                 switch (ELF64_R_TYPE(rela->r_info)) {
248                         case R_PARISC_PCREL22F:
249                                 cnt++;
250                 }
251         }
252
253         return cnt;
254 }
255 #endif
256
257
258 /* Free memory returned from module_alloc */
259 void module_free(struct module *mod, void *module_region)
260 {
261         vfree(module_region);
262         /* FIXME: If module_region == mod->init_region, trim exception
263            table entries. */
264 }
265
266 #define CONST 
267 int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
268                               CONST Elf_Shdr *sechdrs,
269                               CONST char *secstrings,
270                               struct module *me)
271 {
272         unsigned long gots = 0, fdescs = 0, stubs = 0, init_stubs = 0;
273         unsigned int i;
274
275         for (i = 1; i < hdr->e_shnum; i++) {
276                 const Elf_Rela *rels = (void *)hdr + sechdrs[i].sh_offset;
277                 unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
278
279                 if (strncmp(secstrings + sechdrs[i].sh_name,
280                             ".PARISC.unwind", 14) == 0)
281                         me->arch.unwind_section = i;
282
283                 if (sechdrs[i].sh_type != SHT_RELA)
284                         continue;
285
286                 /* some of these are not relevant for 32-bit/64-bit
287                  * we leave them here to make the code common. the
288                  * compiler will do its thing and optimize out the
289                  * stuff we don't need
290                  */
291                 gots += count_gots(rels, nrels);
292                 fdescs += count_fdescs(rels, nrels);
293                 if(strncmp(secstrings + sechdrs[i].sh_name,
294                            ".rela.init", 10) == 0)
295                         init_stubs += count_stubs(rels, nrels);
296                 else
297                         stubs += count_stubs(rels, nrels);
298         }
299
300         /* align things a bit */
301         me->core_size = ALIGN(me->core_size, 16);
302         me->arch.got_offset = me->core_size;
303         me->core_size += gots * sizeof(struct got_entry);
304
305         me->core_size = ALIGN(me->core_size, 16);
306         me->arch.fdesc_offset = me->core_size;
307         me->core_size += fdescs * sizeof(Elf_Fdesc);
308
309         me->core_size = ALIGN(me->core_size, 16);
310         me->arch.stub_offset = me->core_size;
311         me->core_size += stubs * sizeof(struct stub_entry);
312
313         me->init_size = ALIGN(me->init_size, 16);
314         me->arch.init_stub_offset = me->init_size;
315         me->init_size += init_stubs * sizeof(struct stub_entry);
316
317         me->arch.got_max = gots;
318         me->arch.fdesc_max = fdescs;
319         me->arch.stub_max = stubs;
320         me->arch.init_stub_max = init_stubs;
321
322         return 0;
323 }
324
325 #ifdef CONFIG_64BIT
326 static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
327 {
328         unsigned int i;
329         struct got_entry *got;
330
331         value += addend;
332
333         BUG_ON(value == 0);
334
335         got = me->module_core + me->arch.got_offset;
336         for (i = 0; got[i].addr; i++)
337                 if (got[i].addr == value)
338                         goto out;
339
340         BUG_ON(++me->arch.got_count > me->arch.got_max);
341
342         got[i].addr = value;
343  out:
344         DEBUGP("GOT ENTRY %d[%x] val %lx\n", i, i*sizeof(struct got_entry),
345                value);
346         return i * sizeof(struct got_entry);
347 }
348 #endif /* CONFIG_64BIT */
349
350 #ifdef CONFIG_64BIT
351 static Elf_Addr get_fdesc(struct module *me, unsigned long value)
352 {
353         Elf_Fdesc *fdesc = me->module_core + me->arch.fdesc_offset;
354
355         if (!value) {
356                 printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
357                 return 0;
358         }
359
360         /* Look for existing fdesc entry. */
361         while (fdesc->addr) {
362                 if (fdesc->addr == value)
363                         return (Elf_Addr)fdesc;
364                 fdesc++;
365         }
366
367         BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
368
369         /* Create new one */
370         fdesc->addr = value;
371         fdesc->gp = (Elf_Addr)me->module_core + me->arch.got_offset;
372         return (Elf_Addr)fdesc;
373 }
374 #endif /* CONFIG_64BIT */
375
376 enum elf_stub_type {
377         ELF_STUB_GOT,
378         ELF_STUB_MILLI,
379         ELF_STUB_DIRECT,
380 };
381
382 static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
383         enum elf_stub_type stub_type, int init_section)
384 {
385         unsigned long i;
386         struct stub_entry *stub;
387
388         if(init_section) {
389                 i = me->arch.init_stub_count++;
390                 BUG_ON(me->arch.init_stub_count > me->arch.init_stub_max);
391                 stub = me->module_init + me->arch.init_stub_offset + 
392                         i * sizeof(struct stub_entry);
393         } else {
394                 i = me->arch.stub_count++;
395                 BUG_ON(me->arch.stub_count > me->arch.stub_max);
396                 stub = me->module_core + me->arch.stub_offset + 
397                         i * sizeof(struct stub_entry);
398         }
399
400 #ifndef CONFIG_64BIT
401 /* for 32-bit the stub looks like this:
402  *      ldil L'XXX,%r1
403  *      be,n R'XXX(%sr4,%r1)
404  */
405         //value = *(unsigned long *)((value + addend) & ~3); /* why? */
406
407         stub->insns[0] = 0x20200000;    /* ldil L'XXX,%r1       */
408         stub->insns[1] = 0xe0202002;    /* be,n R'XXX(%sr4,%r1) */
409
410         stub->insns[0] |= reassemble_21(lrsel(value, addend));
411         stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
412
413 #else
414 /* for 64-bit we have three kinds of stubs:
415  * for normal function calls:
416  *      ldd 0(%dp),%dp
417  *      ldd 10(%dp), %r1
418  *      bve (%r1)
419  *      ldd 18(%dp), %dp
420  *
421  * for millicode:
422  *      ldil 0, %r1
423  *      ldo 0(%r1), %r1
424  *      ldd 10(%r1), %r1
425  *      bve,n (%r1)
426  *
427  * for direct branches (jumps between different section of the
428  * same module):
429  *      ldil 0, %r1
430  *      ldo 0(%r1), %r1
431  *      bve,n (%r1)
432  */
433         switch (stub_type) {
434         case ELF_STUB_GOT:
435                 stub->insns[0] = 0x537b0000;    /* ldd 0(%dp),%dp       */
436                 stub->insns[1] = 0x53610020;    /* ldd 10(%dp),%r1      */
437                 stub->insns[2] = 0xe820d000;    /* bve (%r1)            */
438                 stub->insns[3] = 0x537b0030;    /* ldd 18(%dp),%dp      */
439
440                 stub->insns[0] |= reassemble_14(get_got(me, value, addend) & 0x3fff);
441                 break;
442         case ELF_STUB_MILLI:
443                 stub->insns[0] = 0x20200000;    /* ldil 0,%r1           */
444                 stub->insns[1] = 0x34210000;    /* ldo 0(%r1), %r1      */
445                 stub->insns[2] = 0x50210020;    /* ldd 10(%r1),%r1      */
446                 stub->insns[3] = 0xe820d002;    /* bve,n (%r1)          */
447
448                 stub->insns[0] |= reassemble_21(lrsel(value, addend));
449                 stub->insns[1] |= reassemble_14(rrsel(value, addend));
450                 break;
451         case ELF_STUB_DIRECT:
452                 stub->insns[0] = 0x20200000;    /* ldil 0,%r1           */
453                 stub->insns[1] = 0x34210000;    /* ldo 0(%r1), %r1      */
454                 stub->insns[2] = 0xe820d002;    /* bve,n (%r1)          */
455
456                 stub->insns[0] |= reassemble_21(lrsel(value, addend));
457                 stub->insns[1] |= reassemble_14(rrsel(value, addend));
458                 break;
459         }
460
461 #endif
462
463         return (Elf_Addr)stub;
464 }
465
466 int apply_relocate(Elf_Shdr *sechdrs,
467                    const char *strtab,
468                    unsigned int symindex,
469                    unsigned int relsec,
470                    struct module *me)
471 {
472         /* parisc should not need this ... */
473         printk(KERN_ERR "module %s: RELOCATION unsupported\n",
474                me->name);
475         return -ENOEXEC;
476 }
477
478 #ifndef CONFIG_64BIT
479 int apply_relocate_add(Elf_Shdr *sechdrs,
480                        const char *strtab,
481                        unsigned int symindex,
482                        unsigned int relsec,
483                        struct module *me)
484 {
485         int i;
486         Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
487         Elf32_Sym *sym;
488         Elf32_Word *loc;
489         Elf32_Addr val;
490         Elf32_Sword addend;
491         Elf32_Addr dot;
492         //unsigned long dp = (unsigned long)$global$;
493         register unsigned long dp asm ("r27");
494
495         DEBUGP("Applying relocate section %u to %u\n", relsec,
496                sechdrs[relsec].sh_info);
497         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
498                 /* This is where to make the change */
499                 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
500                       + rel[i].r_offset;
501                 /* This is the symbol it is referring to */
502                 sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
503                         + ELF32_R_SYM(rel[i].r_info);
504                 if (!sym->st_value) {
505                         printk(KERN_WARNING "%s: Unknown symbol %s\n",
506                                me->name, strtab + sym->st_name);
507                         return -ENOENT;
508                 }
509                 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
510                 dot =  (Elf32_Addr)loc & ~0x03;
511
512                 val = sym->st_value;
513                 addend = rel[i].r_addend;
514
515 #if 0
516 #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
517                 DEBUGP("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
518                         strtab + sym->st_name,
519                         (uint32_t)loc, val, addend,
520                         r(R_PARISC_PLABEL32)
521                         r(R_PARISC_DIR32)
522                         r(R_PARISC_DIR21L)
523                         r(R_PARISC_DIR14R)
524                         r(R_PARISC_SEGREL32)
525                         r(R_PARISC_DPREL21L)
526                         r(R_PARISC_DPREL14R)
527                         r(R_PARISC_PCREL17F)
528                         r(R_PARISC_PCREL22F)
529                         "UNKNOWN");
530 #undef r
531 #endif
532
533                 switch (ELF32_R_TYPE(rel[i].r_info)) {
534                 case R_PARISC_PLABEL32:
535                         /* 32-bit function address */
536                         /* no function descriptors... */
537                         *loc = fsel(val, addend);
538                         break;
539                 case R_PARISC_DIR32:
540                         /* direct 32-bit ref */
541                         *loc = fsel(val, addend);
542                         break;
543                 case R_PARISC_DIR21L:
544                         /* left 21 bits of effective address */
545                         val = lrsel(val, addend);
546                         *loc = mask(*loc, 21) | reassemble_21(val);
547                         break;
548                 case R_PARISC_DIR14R:
549                         /* right 14 bits of effective address */
550                         val = rrsel(val, addend);
551                         *loc = mask(*loc, 14) | reassemble_14(val);
552                         break;
553                 case R_PARISC_SEGREL32:
554                         /* 32-bit segment relative address */
555                         /* See note about special handling of SEGREL32 at
556                          * the beginning of this file.
557                          */
558                         *loc = fsel(val, addend); 
559                         break;
560                 case R_PARISC_DPREL21L:
561                         /* left 21 bit of relative address */
562                         val = lrsel(val - dp, addend);
563                         *loc = mask(*loc, 21) | reassemble_21(val);
564                         break;
565                 case R_PARISC_DPREL14R:
566                         /* right 14 bit of relative address */
567                         val = rrsel(val - dp, addend);
568                         *loc = mask(*loc, 14) | reassemble_14(val);
569                         break;
570                 case R_PARISC_PCREL17F:
571                         /* 17-bit PC relative address */
572                         val = get_stub(me, val, addend, ELF_STUB_GOT, in_init(me, loc));
573                         val = (val - dot - 8)/4;
574                         CHECK_RELOC(val, 17)
575                         *loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
576                         break;
577                 case R_PARISC_PCREL22F:
578                         /* 22-bit PC relative address; only defined for pa20 */
579                         val = get_stub(me, val, addend, ELF_STUB_GOT, in_init(me, loc));
580                         DEBUGP("STUB FOR %s loc %lx+%lx at %lx\n", 
581                                strtab + sym->st_name, (unsigned long)loc, addend, 
582                                val)
583                         val = (val - dot - 8)/4;
584                         CHECK_RELOC(val, 22);
585                         *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
586                         break;
587
588                 default:
589                         printk(KERN_ERR "module %s: Unknown relocation: %u\n",
590                                me->name, ELF32_R_TYPE(rel[i].r_info));
591                         return -ENOEXEC;
592                 }
593         }
594
595         return 0;
596 }
597
598 #else
599 int apply_relocate_add(Elf_Shdr *sechdrs,
600                        const char *strtab,
601                        unsigned int symindex,
602                        unsigned int relsec,
603                        struct module *me)
604 {
605         int i;
606         Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
607         Elf64_Sym *sym;
608         Elf64_Word *loc;
609         Elf64_Xword *loc64;
610         Elf64_Addr val;
611         Elf64_Sxword addend;
612         Elf64_Addr dot;
613
614         DEBUGP("Applying relocate section %u to %u\n", relsec,
615                sechdrs[relsec].sh_info);
616         for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
617                 /* This is where to make the change */
618                 loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
619                       + rel[i].r_offset;
620                 /* This is the symbol it is referring to */
621                 sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
622                         + ELF64_R_SYM(rel[i].r_info);
623                 if (!sym->st_value) {
624                         printk(KERN_WARNING "%s: Unknown symbol %s\n",
625                                me->name, strtab + sym->st_name);
626                         return -ENOENT;
627                 }
628                 //dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
629                 dot = (Elf64_Addr)loc & ~0x03;
630                 loc64 = (Elf64_Xword *)loc;
631
632                 val = sym->st_value;
633                 addend = rel[i].r_addend;
634
635 #if 0
636 #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
637                 printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
638                         strtab + sym->st_name,
639                         loc, val, addend,
640                         r(R_PARISC_LTOFF14R)
641                         r(R_PARISC_LTOFF21L)
642                         r(R_PARISC_PCREL22F)
643                         r(R_PARISC_DIR64)
644                         r(R_PARISC_SEGREL32)
645                         r(R_PARISC_FPTR64)
646                         "UNKNOWN");
647 #undef r
648 #endif
649
650                 switch (ELF64_R_TYPE(rel[i].r_info)) {
651                 case R_PARISC_LTOFF21L:
652                         /* LT-relative; left 21 bits */
653                         val = get_got(me, val, addend);
654                         DEBUGP("LTOFF21L Symbol %s loc %p val %lx\n",
655                                strtab + sym->st_name,
656                                loc, val);
657                         val = lrsel(val, 0);
658                         *loc = mask(*loc, 21) | reassemble_21(val);
659                         break;
660                 case R_PARISC_LTOFF14R:
661                         /* L(ltoff(val+addend)) */
662                         /* LT-relative; right 14 bits */
663                         val = get_got(me, val, addend);
664                         val = rrsel(val, 0);
665                         DEBUGP("LTOFF14R Symbol %s loc %p val %lx\n",
666                                strtab + sym->st_name,
667                                loc, val);
668                         *loc = mask(*loc, 14) | reassemble_14(val);
669                         break;
670                 case R_PARISC_PCREL22F:
671                         /* PC-relative; 22 bits */
672                         DEBUGP("PCREL22F Symbol %s loc %p val %lx\n",
673                                strtab + sym->st_name,
674                                loc, val);
675                         /* can we reach it locally? */
676                         if(!in_local_section(me, (void *)val, (void *)dot)) {
677
678                                 if (in_local(me, (void *)val))
679                                         /* this is the case where the
680                                          * symbol is local to the
681                                          * module, but in a different
682                                          * section, so stub the jump
683                                          * in case it's more than 22
684                                          * bits away */
685                                         val = get_stub(me, val, addend, ELF_STUB_DIRECT,
686                                                        in_init(me, loc));
687                                 else if (strncmp(strtab + sym->st_name, "$$", 2)
688                                     == 0)
689                                         val = get_stub(me, val, addend, ELF_STUB_MILLI,
690                                                        in_init(me, loc));
691                                 else
692                                         val = get_stub(me, val, addend, ELF_STUB_GOT,
693                                                        in_init(me, loc));
694                         }
695                         DEBUGP("STUB FOR %s loc %lx, val %lx+%lx at %lx\n", 
696                                strtab + sym->st_name, loc, sym->st_value,
697                                addend, val);
698                         /* FIXME: local symbols work as long as the
699                          * core and init pieces aren't separated too
700                          * far.  If this is ever broken, you will trip
701                          * the check below.  The way to fix it would
702                          * be to generate local stubs to go between init
703                          * and core */
704                         if((Elf64_Sxword)(val - dot - 8) > 0x800000 -1 ||
705                            (Elf64_Sxword)(val - dot - 8) < -0x800000) {
706                                 printk(KERN_ERR "Module %s, symbol %s is out of range for PCREL22F relocation\n",
707                                        me->name, strtab + sym->st_name);
708                                 return -ENOEXEC;
709                         }
710                         val = (val - dot - 8)/4;
711                         *loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
712                         break;
713                 case R_PARISC_DIR64:
714                         /* 64-bit effective address */
715                         *loc64 = val + addend;
716                         break;
717                 case R_PARISC_SEGREL32:
718                         /* 32-bit segment relative address */
719                         /* See note about special handling of SEGREL32 at
720                          * the beginning of this file.
721                          */
722                         *loc = fsel(val, addend); 
723                         break;
724                 case R_PARISC_FPTR64:
725                         /* 64-bit function address */
726                         if(in_local(me, (void *)(val + addend))) {
727                                 *loc64 = get_fdesc(me, val+addend);
728                                 DEBUGP("FDESC for %s at %p points to %lx\n",
729                                        strtab + sym->st_name, *loc64,
730                                        ((Elf_Fdesc *)*loc64)->addr);
731                         } else {
732                                 /* if the symbol is not local to this
733                                  * module then val+addend is a pointer
734                                  * to the function descriptor */
735                                 DEBUGP("Non local FPTR64 Symbol %s loc %p val %lx\n",
736                                        strtab + sym->st_name,
737                                        loc, val);
738                                 *loc64 = val + addend;
739                         }
740                         break;
741
742                 default:
743                         printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
744                                me->name, ELF64_R_TYPE(rel[i].r_info));
745                         return -ENOEXEC;
746                 }
747         }
748         return 0;
749 }
750 #endif
751
752 static void
753 register_unwind_table(struct module *me,
754                       const Elf_Shdr *sechdrs)
755 {
756         unsigned char *table, *end;
757         unsigned long gp;
758
759         if (!me->arch.unwind_section)
760                 return;
761
762         table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
763         end = table + sechdrs[me->arch.unwind_section].sh_size;
764         gp = (Elf_Addr)me->module_core + me->arch.got_offset;
765
766         DEBUGP("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
767                me->arch.unwind_section, table, end, gp);
768         me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
769 }
770
771 static void
772 deregister_unwind_table(struct module *me)
773 {
774         if (me->arch.unwind)
775                 unwind_table_remove(me->arch.unwind);
776 }
777
778 int module_finalize(const Elf_Ehdr *hdr,
779                     const Elf_Shdr *sechdrs,
780                     struct module *me)
781 {
782         int i;
783         unsigned long nsyms;
784         const char *strtab = NULL;
785         Elf_Sym *newptr, *oldptr;
786         Elf_Shdr *symhdr = NULL;
787 #ifdef DEBUG
788         Elf_Fdesc *entry;
789         u32 *addr;
790
791         entry = (Elf_Fdesc *)me->init;
792         printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
793                entry->gp, entry->addr);
794         addr = (u32 *)entry->addr;
795         printk("INSNS: %x %x %x %x\n",
796                addr[0], addr[1], addr[2], addr[3]);
797         printk("stubs used %ld, stubs max %ld\n"
798                "init_stubs used %ld, init stubs max %ld\n"
799                "got entries used %ld, gots max %ld\n"
800                "fdescs used %ld, fdescs max %ld\n",
801                me->arch.stub_count, me->arch.stub_max,
802                me->arch.init_stub_count, me->arch.init_stub_max,
803                me->arch.got_count, me->arch.got_max,
804                me->arch.fdesc_count, me->arch.fdesc_max);
805 #endif
806
807         register_unwind_table(me, sechdrs);
808
809         /* haven't filled in me->symtab yet, so have to find it
810          * ourselves */
811         for (i = 1; i < hdr->e_shnum; i++) {
812                 if(sechdrs[i].sh_type == SHT_SYMTAB
813                    && (sechdrs[i].sh_type & SHF_ALLOC)) {
814                         int strindex = sechdrs[i].sh_link;
815                         /* FIXME: AWFUL HACK
816                          * The cast is to drop the const from
817                          * the sechdrs pointer */
818                         symhdr = (Elf_Shdr *)&sechdrs[i];
819                         strtab = (char *)sechdrs[strindex].sh_addr;
820                         break;
821                 }
822         }
823
824         DEBUGP("module %s: strtab %p, symhdr %p\n",
825                me->name, strtab, symhdr);
826
827         if(me->arch.got_count > MAX_GOTS) {
828                 printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
829                                 me->name, me->arch.got_count, MAX_GOTS);
830                 return -EINVAL;
831         }
832         
833         /* no symbol table */
834         if(symhdr == NULL)
835                 return 0;
836
837         oldptr = (void *)symhdr->sh_addr;
838         newptr = oldptr + 1;    /* we start counting at 1 */
839         nsyms = symhdr->sh_size / sizeof(Elf_Sym);
840         DEBUGP("OLD num_symtab %lu\n", nsyms);
841
842         for (i = 1; i < nsyms; i++) {
843                 oldptr++;       /* note, count starts at 1 so preincrement */
844                 if(strncmp(strtab + oldptr->st_name,
845                               ".L", 2) == 0)
846                         continue;
847
848                 if(newptr != oldptr)
849                         *newptr++ = *oldptr;
850                 else
851                         newptr++;
852
853         }
854         nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
855         DEBUGP("NEW num_symtab %lu\n", nsyms);
856         symhdr->sh_size = nsyms * sizeof(Elf_Sym);
857         return module_bug_finalize(hdr, sechdrs, me);
858 }
859
860 void module_arch_cleanup(struct module *mod)
861 {
862         deregister_unwind_table(mod);
863         module_bug_cleanup(mod);
864 }
865
866 #ifdef CONFIG_64BIT
867 void *dereference_function_descriptor(void *ptr)
868 {
869         Elf64_Fdesc *desc = ptr;
870         void *p;
871
872         if (!probe_kernel_address(&desc->addr, p))
873                 ptr = p;
874         return ptr;
875 }
876 #endif