Linux Audio

Check our new training course

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