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