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