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