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