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