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