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