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