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