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