xref: /linux-6.15/arch/parisc/kernel/module.c (revision 0cc2dc49)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*    Kernel dynamically loadable module help for PARISC.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *    The best reference for this stuff is probably the Processor-
51da177e4SLinus Torvalds  *    Specific ELF Supplement for PA-RISC:
6486a77c9SHelge Deller  *        https://parisc.wiki.kernel.org/index.php/File:Elf-pa-hp.pdf
71da177e4SLinus Torvalds  *
8486a77c9SHelge Deller  *    Linux/PA-RISC Project
91da177e4SLinus Torvalds  *    Copyright (C) 2003 Randolph Chung <tausq at debian . org>
10c298be74SHelge Deller  *    Copyright (C) 2008 Helge Deller <[email protected]>
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  *    Notes:
13c298be74SHelge Deller  *    - PLT stub handling
14c298be74SHelge Deller  *      On 32bit (and sometimes 64bit) and with big kernel modules like xfs or
15c298be74SHelge Deller  *      ipv6 the relocation types R_PARISC_PCREL17F and R_PARISC_PCREL22F may
16c298be74SHelge Deller  *      fail to reach their PLT stub if we only create one big stub array for
17c298be74SHelge Deller  *      all sections at the beginning of the core or init section.
18c298be74SHelge Deller  *      Instead we now insert individual PLT stub entries directly in front of
19c298be74SHelge Deller  *      of the code sections where the stubs are actually called.
20c298be74SHelge Deller  *      This reduces the distance between the PCREL location and the stub entry
21c298be74SHelge Deller  *      so that the relocations can be fulfilled.
22c298be74SHelge Deller  *      While calculating the final layout of the kernel module in memory, the
23c298be74SHelge Deller  *      kernel module loader calls arch_mod_section_prepend() to request the
24c298be74SHelge Deller  *      to be reserved amount of memory in front of each individual section.
25c298be74SHelge Deller  *
261da177e4SLinus Torvalds  *    - SEGREL32 handling
271da177e4SLinus Torvalds  *      We are not doing SEGREL32 handling correctly. According to the ABI, we
281da177e4SLinus Torvalds  *      should do a value offset, like this:
29959ed340SEric Biederman  *			if (in_init(me, (void *)val))
30ac3b4328SSong Liu  *				val -= (uint32_t)me->mem[MOD_INIT_TEXT].base;
311da177e4SLinus Torvalds  *			else
32ac3b4328SSong Liu  *				val -= (uint32_t)me->mem[MOD_TEXT].base;
331da177e4SLinus Torvalds  *	However, SEGREL32 is used only for PARISC unwind entries, and we want
341da177e4SLinus Torvalds  *	those entries to have an absolute address, and not just an offset.
351da177e4SLinus Torvalds  *
361da177e4SLinus Torvalds  *	The unwind table mechanism has the ability to specify an offset for
371da177e4SLinus Torvalds  *	the unwind table; however, because we split off the init functions into
381da177e4SLinus Torvalds  *	a different piece of memory, it is not possible to do this using a
391da177e4SLinus Torvalds  *	single offset. Instead, we use the above hack for now.
401da177e4SLinus Torvalds  */
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #include <linux/moduleloader.h>
431da177e4SLinus Torvalds #include <linux/elf.h>
441da177e4SLinus Torvalds #include <linux/fs.h>
45a1326b17SMark Rutland #include <linux/ftrace.h>
461da177e4SLinus Torvalds #include <linux/string.h>
471da177e4SLinus Torvalds #include <linux/kernel.h>
486891f8a1SHelge Deller #include <linux/bug.h>
49d7dd2ff1SJames Bottomley #include <linux/mm.h>
505a0e3ad6STejun Heo #include <linux/slab.h>
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #include <asm/unwind.h>
531705bd6aSSergey Senozhatsky #include <asm/sections.h>
541da177e4SLinus Torvalds 
55c298be74SHelge Deller #define RELOC_REACHABLE(val, bits) \
56c298be74SHelge Deller 	(( ( !((val) & (1<<((bits)-1))) && ((val)>>(bits)) != 0 )  ||	\
57c298be74SHelge Deller 	     ( ((val) & (1<<((bits)-1))) && ((val)>>(bits)) != (((__typeof__(val))(~0))>>((bits)+2)))) ? \
58c298be74SHelge Deller 	0 : 1)
59c298be74SHelge Deller 
601da177e4SLinus Torvalds #define CHECK_RELOC(val, bits) \
61c298be74SHelge Deller 	if (!RELOC_REACHABLE(val, bits)) { \
621da177e4SLinus Torvalds 		printk(KERN_ERR "module %s relocation of symbol %s is out of range (0x%lx in %d bits)\n", \
631da177e4SLinus Torvalds 		me->name, strtab + sym->st_name, (unsigned long)val, bits); \
641da177e4SLinus Torvalds 		return -ENOEXEC;			\
651da177e4SLinus Torvalds 	}
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /* Maximum number of GOT entries. We use a long displacement ldd from
681da177e4SLinus Torvalds  * the bottom of the table, which has a maximum signed displacement of
691da177e4SLinus Torvalds  * 0x3fff; however, since we're only going forward, this becomes
701da177e4SLinus Torvalds  * 0x1fff, and thus, since each GOT entry is 8 bytes long we can have
71b4f2e2adSJohn David Anglin  * at most 1023 entries.
72b4f2e2adSJohn David Anglin  * To overcome this 14bit displacement with some kernel modules, we'll
73b4f2e2adSJohn David Anglin  * use instead the unusal 16bit displacement method (see reassemble_16a)
74b4f2e2adSJohn David Anglin  * which gives us a maximum positive displacement of 0x7fff, and as such
75b4f2e2adSJohn David Anglin  * allows us to allocate up to 4095 GOT entries. */
76b4f2e2adSJohn David Anglin #define MAX_GOTS	4095
771da177e4SLinus Torvalds 
78a8f44e38SHelge Deller #ifndef CONFIG_64BIT
791da177e4SLinus Torvalds struct got_entry {
801da177e4SLinus Torvalds 	Elf32_Addr addr;
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds struct stub_entry {
841da177e4SLinus Torvalds 	Elf32_Word insns[2]; /* each stub entry has two insns */
851da177e4SLinus Torvalds };
861da177e4SLinus Torvalds #else
871da177e4SLinus Torvalds struct got_entry {
881da177e4SLinus Torvalds 	Elf64_Addr addr;
891da177e4SLinus Torvalds };
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds struct stub_entry {
921da177e4SLinus Torvalds 	Elf64_Word insns[4]; /* each stub entry has four insns */
931da177e4SLinus Torvalds };
941da177e4SLinus Torvalds #endif
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /* Field selection types defined by hppa */
971da177e4SLinus Torvalds #define rnd(x)			(((x)+0x1000)&~0x1fff)
981da177e4SLinus Torvalds /* fsel: full 32 bits */
991da177e4SLinus Torvalds #define fsel(v,a)		((v)+(a))
1001da177e4SLinus Torvalds /* lsel: select left 21 bits */
1011da177e4SLinus Torvalds #define lsel(v,a)		(((v)+(a))>>11)
1021da177e4SLinus Torvalds /* rsel: select right 11 bits */
1031da177e4SLinus Torvalds #define rsel(v,a)		(((v)+(a))&0x7ff)
1041da177e4SLinus Torvalds /* lrsel with rounding of addend to nearest 8k */
1051da177e4SLinus Torvalds #define lrsel(v,a)		(((v)+rnd(a))>>11)
1061da177e4SLinus Torvalds /* rrsel with rounding of addend to nearest 8k */
1071da177e4SLinus Torvalds #define rrsel(v,a)		((((v)+rnd(a))&0x7ff)+((a)-rnd(a)))
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds #define mask(x,sz)		((x) & ~((1<<(sz))-1))
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* The reassemble_* functions prepare an immediate value for
1131da177e4SLinus Torvalds    insertion into an opcode. pa-risc uses all sorts of weird bitfields
1141da177e4SLinus Torvalds    in the instruction to hold the value.  */
sign_unext(int x,int len)115b4f2e2adSJohn David Anglin static inline int sign_unext(int x, int len)
116b4f2e2adSJohn David Anglin {
117b4f2e2adSJohn David Anglin 	int len_ones;
118b4f2e2adSJohn David Anglin 
119b4f2e2adSJohn David Anglin 	len_ones = (1 << len) - 1;
120b4f2e2adSJohn David Anglin 	return x & len_ones;
121b4f2e2adSJohn David Anglin }
122b4f2e2adSJohn David Anglin 
low_sign_unext(int x,int len)123b4f2e2adSJohn David Anglin static inline int low_sign_unext(int x, int len)
124b4f2e2adSJohn David Anglin {
125b4f2e2adSJohn David Anglin 	int sign, temp;
126b4f2e2adSJohn David Anglin 
127b4f2e2adSJohn David Anglin 	sign = (x >> (len-1)) & 1;
128b4f2e2adSJohn David Anglin 	temp = sign_unext(x, len-1);
129b4f2e2adSJohn David Anglin 	return (temp << 1) | sign;
130b4f2e2adSJohn David Anglin }
131b4f2e2adSJohn David Anglin 
reassemble_14(int as14)1321da177e4SLinus Torvalds static inline int reassemble_14(int as14)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	return (((as14 & 0x1fff) << 1) |
1351da177e4SLinus Torvalds 		((as14 & 0x2000) >> 13));
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
reassemble_16a(int as16)138b4f2e2adSJohn David Anglin static inline int reassemble_16a(int as16)
139b4f2e2adSJohn David Anglin {
140b4f2e2adSJohn David Anglin 	int s, t;
141b4f2e2adSJohn David Anglin 
142b4f2e2adSJohn David Anglin 	/* Unusual 16-bit encoding, for wide mode only.  */
143b4f2e2adSJohn David Anglin 	t = (as16 << 1) & 0xffff;
144b4f2e2adSJohn David Anglin 	s = (as16 & 0x8000);
145b4f2e2adSJohn David Anglin 	return (t ^ s ^ (s >> 1)) | (s >> 15);
146b4f2e2adSJohn David Anglin }
147b4f2e2adSJohn David Anglin 
148b4f2e2adSJohn David Anglin 
reassemble_17(int as17)1491da177e4SLinus Torvalds static inline int reassemble_17(int as17)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	return (((as17 & 0x10000) >> 16) |
1521da177e4SLinus Torvalds 		((as17 & 0x0f800) << 5) |
1531da177e4SLinus Torvalds 		((as17 & 0x00400) >> 8) |
1541da177e4SLinus Torvalds 		((as17 & 0x003ff) << 3));
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
reassemble_21(int as21)1571da177e4SLinus Torvalds static inline int reassemble_21(int as21)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds 	return (((as21 & 0x100000) >> 20) |
1601da177e4SLinus Torvalds 		((as21 & 0x0ffe00) >> 8) |
1611da177e4SLinus Torvalds 		((as21 & 0x000180) << 7) |
1621da177e4SLinus Torvalds 		((as21 & 0x00007c) << 14) |
1631da177e4SLinus Torvalds 		((as21 & 0x000003) << 12));
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
reassemble_22(int as22)1661da177e4SLinus Torvalds static inline int reassemble_22(int as22)
1671da177e4SLinus Torvalds {
1681da177e4SLinus Torvalds 	return (((as22 & 0x200000) >> 21) |
1691da177e4SLinus Torvalds 		((as22 & 0x1f0000) << 5) |
1701da177e4SLinus Torvalds 		((as22 & 0x00f800) << 5) |
1711da177e4SLinus Torvalds 		((as22 & 0x000400) >> 8) |
1721da177e4SLinus Torvalds 		((as22 & 0x0003ff) << 3));
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
175a8f44e38SHelge Deller #ifndef CONFIG_64BIT
count_gots(const Elf_Rela * rela,unsigned long n)1761da177e4SLinus Torvalds static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
1771da177e4SLinus Torvalds {
1781da177e4SLinus Torvalds 	return 0;
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
count_fdescs(const Elf_Rela * rela,unsigned long n)1811da177e4SLinus Torvalds static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
1821da177e4SLinus Torvalds {
1831da177e4SLinus Torvalds 	return 0;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
count_stubs(const Elf_Rela * rela,unsigned long n)1861da177e4SLinus Torvalds static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
1871da177e4SLinus Torvalds {
1881da177e4SLinus Torvalds 	unsigned long cnt = 0;
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	for (; n > 0; n--, rela++)
1911da177e4SLinus Torvalds 	{
1921da177e4SLinus Torvalds 		switch (ELF32_R_TYPE(rela->r_info)) {
1931da177e4SLinus Torvalds 			case R_PARISC_PCREL17F:
1941da177e4SLinus Torvalds 			case R_PARISC_PCREL22F:
1951da177e4SLinus Torvalds 				cnt++;
1961da177e4SLinus Torvalds 		}
1971da177e4SLinus Torvalds 	}
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	return cnt;
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds #else
count_gots(const Elf_Rela * rela,unsigned long n)2021da177e4SLinus Torvalds static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds 	unsigned long cnt = 0;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	for (; n > 0; n--, rela++)
2071da177e4SLinus Torvalds 	{
2081da177e4SLinus Torvalds 		switch (ELF64_R_TYPE(rela->r_info)) {
2091da177e4SLinus Torvalds 			case R_PARISC_LTOFF21L:
2101da177e4SLinus Torvalds 			case R_PARISC_LTOFF14R:
2111da177e4SLinus Torvalds 			case R_PARISC_PCREL22F:
2121da177e4SLinus Torvalds 				cnt++;
2131da177e4SLinus Torvalds 		}
2141da177e4SLinus Torvalds 	}
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	return cnt;
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
count_fdescs(const Elf_Rela * rela,unsigned long n)2191da177e4SLinus Torvalds static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	unsigned long cnt = 0;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	for (; n > 0; n--, rela++)
2241da177e4SLinus Torvalds 	{
2251da177e4SLinus Torvalds 		switch (ELF64_R_TYPE(rela->r_info)) {
2261da177e4SLinus Torvalds 			case R_PARISC_FPTR64:
2271da177e4SLinus Torvalds 				cnt++;
2281da177e4SLinus Torvalds 		}
2291da177e4SLinus Torvalds 	}
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	return cnt;
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
count_stubs(const Elf_Rela * rela,unsigned long n)2341da177e4SLinus Torvalds static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n)
2351da177e4SLinus Torvalds {
2361da177e4SLinus Torvalds 	unsigned long cnt = 0;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	for (; n > 0; n--, rela++)
2391da177e4SLinus Torvalds 	{
2401da177e4SLinus Torvalds 		switch (ELF64_R_TYPE(rela->r_info)) {
2411da177e4SLinus Torvalds 			case R_PARISC_PCREL22F:
2421da177e4SLinus Torvalds 				cnt++;
2431da177e4SLinus Torvalds 		}
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	return cnt;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds #endif
2491da177e4SLinus Torvalds 
module_arch_freeing_init(struct module * mod)250d453cdedSRusty Russell void module_arch_freeing_init(struct module *mod)
2511da177e4SLinus Torvalds {
252c298be74SHelge Deller 	kfree(mod->arch.section);
253c298be74SHelge Deller 	mod->arch.section = NULL;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
256c298be74SHelge Deller /* Additional bytes needed in front of individual sections */
arch_mod_section_prepend(struct module * mod,unsigned int section)257c298be74SHelge Deller unsigned int arch_mod_section_prepend(struct module *mod,
258c298be74SHelge Deller 				      unsigned int section)
259c298be74SHelge Deller {
260c298be74SHelge Deller 	/* size needed for all stubs of this section (including
261c298be74SHelge Deller 	 * one additional for correct alignment of the stubs) */
262c298be74SHelge Deller 	return (mod->arch.section[section].stub_entries + 1)
263c298be74SHelge Deller 		* sizeof(struct stub_entry);
264c298be74SHelge Deller }
265c298be74SHelge Deller 
2661da177e4SLinus Torvalds #define CONST
module_frob_arch_sections(CONST Elf_Ehdr * hdr,CONST Elf_Shdr * sechdrs,CONST char * secstrings,struct module * me)2671da177e4SLinus Torvalds int module_frob_arch_sections(CONST Elf_Ehdr *hdr,
2681da177e4SLinus Torvalds 			      CONST Elf_Shdr *sechdrs,
2691da177e4SLinus Torvalds 			      CONST char *secstrings,
2701da177e4SLinus Torvalds 			      struct module *me)
2711da177e4SLinus Torvalds {
272c298be74SHelge Deller 	unsigned long gots = 0, fdescs = 0, len;
2731da177e4SLinus Torvalds 	unsigned int i;
274ac3b4328SSong Liu 	struct module_memory *mod_mem;
2751da177e4SLinus Torvalds 
276c298be74SHelge Deller 	len = hdr->e_shnum * sizeof(me->arch.section[0]);
277c298be74SHelge Deller 	me->arch.section = kzalloc(len, GFP_KERNEL);
278c298be74SHelge Deller 	if (!me->arch.section)
279c298be74SHelge Deller 		return -ENOMEM;
280c298be74SHelge Deller 
2811da177e4SLinus Torvalds 	for (i = 1; i < hdr->e_shnum; i++) {
282c298be74SHelge Deller 		const Elf_Rela *rels = (void *)sechdrs[i].sh_addr;
2831da177e4SLinus Torvalds 		unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels);
284c298be74SHelge Deller 		unsigned int count, s;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 		if (strncmp(secstrings + sechdrs[i].sh_name,
2871da177e4SLinus Torvalds 			    ".PARISC.unwind", 14) == 0)
2881da177e4SLinus Torvalds 			me->arch.unwind_section = i;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 		if (sechdrs[i].sh_type != SHT_RELA)
2911da177e4SLinus Torvalds 			continue;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 		/* some of these are not relevant for 32-bit/64-bit
2941da177e4SLinus Torvalds 		 * we leave them here to make the code common. the
2951da177e4SLinus Torvalds 		 * compiler will do its thing and optimize out the
2961da177e4SLinus Torvalds 		 * stuff we don't need
2971da177e4SLinus Torvalds 		 */
2981da177e4SLinus Torvalds 		gots += count_gots(rels, nrels);
2991da177e4SLinus Torvalds 		fdescs += count_fdescs(rels, nrels);
300c298be74SHelge Deller 
301c298be74SHelge Deller 		/* XXX: By sorting the relocs and finding duplicate entries
302c298be74SHelge Deller 		 *  we could reduce the number of necessary stubs and save
303c298be74SHelge Deller 		 *  some memory. */
304c298be74SHelge Deller 		count = count_stubs(rels, nrels);
305c298be74SHelge Deller 		if (!count)
306c298be74SHelge Deller 			continue;
307c298be74SHelge Deller 
308c298be74SHelge Deller 		/* so we need relocation stubs. reserve necessary memory. */
309c298be74SHelge Deller 		/* sh_info gives the section for which we need to add stubs. */
310c298be74SHelge Deller 		s = sechdrs[i].sh_info;
311c298be74SHelge Deller 
312c298be74SHelge Deller 		/* each code section should only have one relocation section */
313c298be74SHelge Deller 		WARN_ON(me->arch.section[s].stub_entries);
314c298be74SHelge Deller 
315c298be74SHelge Deller 		/* store number of stubs we need for this section */
316c298be74SHelge Deller 		me->arch.section[s].stub_entries += count;
3171da177e4SLinus Torvalds 	}
3181da177e4SLinus Torvalds 
319ac3b4328SSong Liu 	mod_mem = &me->mem[MOD_TEXT];
3201da177e4SLinus Torvalds 	/* align things a bit */
321ac3b4328SSong Liu 	mod_mem->size = ALIGN(mod_mem->size, 16);
322ac3b4328SSong Liu 	me->arch.got_offset = mod_mem->size;
323ac3b4328SSong Liu 	mod_mem->size += gots * sizeof(struct got_entry);
3241da177e4SLinus Torvalds 
325ac3b4328SSong Liu 	mod_mem->size = ALIGN(mod_mem->size, 16);
326ac3b4328SSong Liu 	me->arch.fdesc_offset = mod_mem->size;
327ac3b4328SSong Liu 	mod_mem->size += fdescs * sizeof(Elf_Fdesc);
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	me->arch.got_max = gots;
3301da177e4SLinus Torvalds 	me->arch.fdesc_max = fdescs;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	return 0;
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds 
335a8f44e38SHelge Deller #ifdef CONFIG_64BIT
get_got(struct module * me,unsigned long value,long addend)3361da177e4SLinus Torvalds static Elf64_Word get_got(struct module *me, unsigned long value, long addend)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	unsigned int i;
3391da177e4SLinus Torvalds 	struct got_entry *got;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	value += addend;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	BUG_ON(value == 0);
3441da177e4SLinus Torvalds 
345ac3b4328SSong Liu 	got = me->mem[MOD_TEXT].base + me->arch.got_offset;
3461da177e4SLinus Torvalds 	for (i = 0; got[i].addr; i++)
3471da177e4SLinus Torvalds 		if (got[i].addr == value)
3481da177e4SLinus Torvalds 			goto out;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	BUG_ON(++me->arch.got_count > me->arch.got_max);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	got[i].addr = value;
3531da177e4SLinus Torvalds  out:
3546183d68bSSven Schnelle 	pr_debug("GOT ENTRY %d[%lx] val %lx\n", i, i*sizeof(struct got_entry),
3551da177e4SLinus Torvalds 	       value);
3561da177e4SLinus Torvalds 	return i * sizeof(struct got_entry);
3571da177e4SLinus Torvalds }
358a8f44e38SHelge Deller #endif /* CONFIG_64BIT */
3591da177e4SLinus Torvalds 
360a8f44e38SHelge Deller #ifdef CONFIG_64BIT
get_fdesc(struct module * me,unsigned long value)3611da177e4SLinus Torvalds static Elf_Addr get_fdesc(struct module *me, unsigned long value)
3621da177e4SLinus Torvalds {
363ac3b4328SSong Liu 	Elf_Fdesc *fdesc = me->mem[MOD_TEXT].base + me->arch.fdesc_offset;
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	if (!value) {
3661da177e4SLinus Torvalds 		printk(KERN_ERR "%s: zero OPD requested!\n", me->name);
3671da177e4SLinus Torvalds 		return 0;
3681da177e4SLinus Torvalds 	}
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	/* Look for existing fdesc entry. */
3711da177e4SLinus Torvalds 	while (fdesc->addr) {
3721da177e4SLinus Torvalds 		if (fdesc->addr == value)
3731da177e4SLinus Torvalds 			return (Elf_Addr)fdesc;
3741da177e4SLinus Torvalds 		fdesc++;
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds 	BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max);
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	/* Create new one */
3801da177e4SLinus Torvalds 	fdesc->addr = value;
381ac3b4328SSong Liu 	fdesc->gp = (Elf_Addr)me->mem[MOD_TEXT].base + me->arch.got_offset;
3821da177e4SLinus Torvalds 	return (Elf_Addr)fdesc;
3831da177e4SLinus Torvalds }
384a8f44e38SHelge Deller #endif /* CONFIG_64BIT */
3851da177e4SLinus Torvalds 
3866e1b9585SJames Bottomley enum elf_stub_type {
3876e1b9585SJames Bottomley 	ELF_STUB_GOT,
3886e1b9585SJames Bottomley 	ELF_STUB_MILLI,
3896e1b9585SJames Bottomley 	ELF_STUB_DIRECT,
3906e1b9585SJames Bottomley };
3916e1b9585SJames Bottomley 
get_stub(struct module * me,unsigned long value,long addend,enum elf_stub_type stub_type,Elf_Addr loc0,unsigned int targetsec)3921da177e4SLinus Torvalds static Elf_Addr get_stub(struct module *me, unsigned long value, long addend,
393c298be74SHelge Deller 	enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	struct stub_entry *stub;
396b4f2e2adSJohn David Anglin 	int __maybe_unused d;
3971da177e4SLinus Torvalds 
398c298be74SHelge Deller 	/* initialize stub_offset to point in front of the section */
399c298be74SHelge Deller 	if (!me->arch.section[targetsec].stub_offset) {
400c298be74SHelge Deller 		loc0 -= (me->arch.section[targetsec].stub_entries + 1) *
401c298be74SHelge Deller 				sizeof(struct stub_entry);
402c298be74SHelge Deller 		/* get correct alignment for the stubs */
403c298be74SHelge Deller 		loc0 = ALIGN(loc0, sizeof(struct stub_entry));
404c298be74SHelge Deller 		me->arch.section[targetsec].stub_offset = loc0;
4051da177e4SLinus Torvalds 	}
4061da177e4SLinus Torvalds 
407c298be74SHelge Deller 	/* get address of stub entry */
408c298be74SHelge Deller 	stub = (void *) me->arch.section[targetsec].stub_offset;
409c298be74SHelge Deller 	me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry);
410c298be74SHelge Deller 
411c298be74SHelge Deller 	/* do not write outside available stub area */
412c298be74SHelge Deller 	BUG_ON(0 == me->arch.section[targetsec].stub_entries--);
413c298be74SHelge Deller 
414c298be74SHelge Deller 
415a8f44e38SHelge Deller #ifndef CONFIG_64BIT
4161da177e4SLinus Torvalds /* for 32-bit the stub looks like this:
4171da177e4SLinus Torvalds  * 	ldil L'XXX,%r1
4181da177e4SLinus Torvalds  * 	be,n R'XXX(%sr4,%r1)
4191da177e4SLinus Torvalds  */
4201da177e4SLinus Torvalds 	//value = *(unsigned long *)((value + addend) & ~3); /* why? */
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	stub->insns[0] = 0x20200000;	/* ldil L'XXX,%r1	*/
4231da177e4SLinus Torvalds 	stub->insns[1] = 0xe0202002;	/* be,n R'XXX(%sr4,%r1)	*/
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	stub->insns[0] |= reassemble_21(lrsel(value, addend));
4261da177e4SLinus Torvalds 	stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds #else
4296e1b9585SJames Bottomley /* for 64-bit we have three kinds of stubs:
4301da177e4SLinus Torvalds  * for normal function calls:
4311da177e4SLinus Torvalds  * 	ldd 0(%dp),%dp
4321da177e4SLinus Torvalds  * 	ldd 10(%dp), %r1
4331da177e4SLinus Torvalds  * 	bve (%r1)
4341da177e4SLinus Torvalds  * 	ldd 18(%dp), %dp
4351da177e4SLinus Torvalds  *
4361da177e4SLinus Torvalds  * for millicode:
4371da177e4SLinus Torvalds  * 	ldil 0, %r1
4381da177e4SLinus Torvalds  * 	ldo 0(%r1), %r1
4391da177e4SLinus Torvalds  * 	ldd 10(%r1), %r1
4401da177e4SLinus Torvalds  * 	bve,n (%r1)
4416e1b9585SJames Bottomley  *
4426e1b9585SJames Bottomley  * for direct branches (jumps between different section of the
4436e1b9585SJames Bottomley  * same module):
4446e1b9585SJames Bottomley  *	ldil 0, %r1
4456e1b9585SJames Bottomley  *	ldo 0(%r1), %r1
4466e1b9585SJames Bottomley  *	bve,n (%r1)
4471da177e4SLinus Torvalds  */
4486e1b9585SJames Bottomley 	switch (stub_type) {
4496e1b9585SJames Bottomley 	case ELF_STUB_GOT:
450b4f2e2adSJohn David Anglin 		d = get_got(me, value, addend);
451b4f2e2adSJohn David Anglin 		if (d <= 15) {
452b4f2e2adSJohn David Anglin 			/* Format 5 */
453b4f2e2adSJohn David Anglin 			stub->insns[0] = 0x0f6010db; /* ldd 0(%dp),%dp	*/
454b4f2e2adSJohn David Anglin 			stub->insns[0] |= low_sign_unext(d, 5) << 16;
455b4f2e2adSJohn David Anglin 		} else {
456b4f2e2adSJohn David Anglin 			/* Format 3 */
4571da177e4SLinus Torvalds 			stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp	*/
458b4f2e2adSJohn David Anglin 			stub->insns[0] |= reassemble_16a(d);
459b4f2e2adSJohn David Anglin 		}
4601da177e4SLinus Torvalds 		stub->insns[1] = 0x53610020;	/* ldd 10(%dp),%r1	*/
4611da177e4SLinus Torvalds 		stub->insns[2] = 0xe820d000;	/* bve (%r1)		*/
4621da177e4SLinus Torvalds 		stub->insns[3] = 0x537b0030;	/* ldd 18(%dp),%dp	*/
4636e1b9585SJames Bottomley 		break;
4646e1b9585SJames Bottomley 	case ELF_STUB_MILLI:
4651da177e4SLinus Torvalds 		stub->insns[0] = 0x20200000;	/* ldil 0,%r1		*/
4661da177e4SLinus Torvalds 		stub->insns[1] = 0x34210000;	/* ldo 0(%r1), %r1	*/
4671da177e4SLinus Torvalds 		stub->insns[2] = 0x50210020;	/* ldd 10(%r1),%r1	*/
4681da177e4SLinus Torvalds 		stub->insns[3] = 0xe820d002;	/* bve,n (%r1)		*/
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 		stub->insns[0] |= reassemble_21(lrsel(value, addend));
4711da177e4SLinus Torvalds 		stub->insns[1] |= reassemble_14(rrsel(value, addend));
4726e1b9585SJames Bottomley 		break;
4736e1b9585SJames Bottomley 	case ELF_STUB_DIRECT:
4746e1b9585SJames Bottomley 		stub->insns[0] = 0x20200000;    /* ldil 0,%r1           */
4756e1b9585SJames Bottomley 		stub->insns[1] = 0x34210000;    /* ldo 0(%r1), %r1      */
4766e1b9585SJames Bottomley 		stub->insns[2] = 0xe820d002;    /* bve,n (%r1)          */
4776e1b9585SJames Bottomley 
4786e1b9585SJames Bottomley 		stub->insns[0] |= reassemble_21(lrsel(value, addend));
4796e1b9585SJames Bottomley 		stub->insns[1] |= reassemble_14(rrsel(value, addend));
4806e1b9585SJames Bottomley 		break;
4811da177e4SLinus Torvalds 	}
4826e1b9585SJames Bottomley 
4831da177e4SLinus Torvalds #endif
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	return (Elf_Addr)stub;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
488a8f44e38SHelge Deller #ifndef CONFIG_64BIT
apply_relocate_add(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)4891da177e4SLinus Torvalds int apply_relocate_add(Elf_Shdr *sechdrs,
4901da177e4SLinus Torvalds 		       const char *strtab,
4911da177e4SLinus Torvalds 		       unsigned int symindex,
4921da177e4SLinus Torvalds 		       unsigned int relsec,
4931da177e4SLinus Torvalds 		       struct module *me)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	int i;
4961da177e4SLinus Torvalds 	Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
4971da177e4SLinus Torvalds 	Elf32_Sym *sym;
4981da177e4SLinus Torvalds 	Elf32_Word *loc;
4991da177e4SLinus Torvalds 	Elf32_Addr val;
5001da177e4SLinus Torvalds 	Elf32_Sword addend;
5011da177e4SLinus Torvalds 	Elf32_Addr dot;
502c298be74SHelge Deller 	Elf_Addr loc0;
503c298be74SHelge Deller 	unsigned int targetsec = sechdrs[relsec].sh_info;
5041da177e4SLinus Torvalds 	//unsigned long dp = (unsigned long)$global$;
5051da177e4SLinus Torvalds 	register unsigned long dp asm ("r27");
5061da177e4SLinus Torvalds 
5076183d68bSSven Schnelle 	pr_debug("Applying relocate section %u to %u\n", relsec,
508c298be74SHelge Deller 	       targetsec);
5091da177e4SLinus Torvalds 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
5101da177e4SLinus Torvalds 		/* This is where to make the change */
511c298be74SHelge Deller 		loc = (void *)sechdrs[targetsec].sh_addr
5121da177e4SLinus Torvalds 		      + rel[i].r_offset;
513c298be74SHelge Deller 		/* This is the start of the target section */
514c298be74SHelge Deller 		loc0 = sechdrs[targetsec].sh_addr;
5151da177e4SLinus Torvalds 		/* This is the symbol it is referring to */
5161da177e4SLinus Torvalds 		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
5171da177e4SLinus Torvalds 			+ ELF32_R_SYM(rel[i].r_info);
5181da177e4SLinus Torvalds 		if (!sym->st_value) {
5191da177e4SLinus Torvalds 			printk(KERN_WARNING "%s: Unknown symbol %s\n",
5201da177e4SLinus Torvalds 			       me->name, strtab + sym->st_name);
5211da177e4SLinus Torvalds 			return -ENOENT;
5221da177e4SLinus Torvalds 		}
5231da177e4SLinus Torvalds 		//dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
5241da177e4SLinus Torvalds 		dot =  (Elf32_Addr)loc & ~0x03;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 		val = sym->st_value;
5271da177e4SLinus Torvalds 		addend = rel[i].r_addend;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds #if 0
5301da177e4SLinus Torvalds #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t :
5316183d68bSSven Schnelle 		pr_debug("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n",
5321da177e4SLinus Torvalds 			strtab + sym->st_name,
5331da177e4SLinus Torvalds 			(uint32_t)loc, val, addend,
5341da177e4SLinus Torvalds 			r(R_PARISC_PLABEL32)
5351da177e4SLinus Torvalds 			r(R_PARISC_DIR32)
5361da177e4SLinus Torvalds 			r(R_PARISC_DIR21L)
5371da177e4SLinus Torvalds 			r(R_PARISC_DIR14R)
5381da177e4SLinus Torvalds 			r(R_PARISC_SEGREL32)
5391da177e4SLinus Torvalds 			r(R_PARISC_DPREL21L)
5401da177e4SLinus Torvalds 			r(R_PARISC_DPREL14R)
5411da177e4SLinus Torvalds 			r(R_PARISC_PCREL17F)
5421da177e4SLinus Torvalds 			r(R_PARISC_PCREL22F)
5431da177e4SLinus Torvalds 			"UNKNOWN");
5441da177e4SLinus Torvalds #undef r
5451da177e4SLinus Torvalds #endif
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 		switch (ELF32_R_TYPE(rel[i].r_info)) {
5481da177e4SLinus Torvalds 		case R_PARISC_PLABEL32:
5491da177e4SLinus Torvalds 			/* 32-bit function address */
5501da177e4SLinus Torvalds 			/* no function descriptors... */
5511da177e4SLinus Torvalds 			*loc = fsel(val, addend);
5521da177e4SLinus Torvalds 			break;
5531da177e4SLinus Torvalds 		case R_PARISC_DIR32:
5541da177e4SLinus Torvalds 			/* direct 32-bit ref */
5551da177e4SLinus Torvalds 			*loc = fsel(val, addend);
5561da177e4SLinus Torvalds 			break;
5571da177e4SLinus Torvalds 		case R_PARISC_DIR21L:
5581da177e4SLinus Torvalds 			/* left 21 bits of effective address */
5591da177e4SLinus Torvalds 			val = lrsel(val, addend);
5601da177e4SLinus Torvalds 			*loc = mask(*loc, 21) | reassemble_21(val);
5611da177e4SLinus Torvalds 			break;
5621da177e4SLinus Torvalds 		case R_PARISC_DIR14R:
5631da177e4SLinus Torvalds 			/* right 14 bits of effective address */
5641da177e4SLinus Torvalds 			val = rrsel(val, addend);
5651da177e4SLinus Torvalds 			*loc = mask(*loc, 14) | reassemble_14(val);
5661da177e4SLinus Torvalds 			break;
5671da177e4SLinus Torvalds 		case R_PARISC_SEGREL32:
5681da177e4SLinus Torvalds 			/* 32-bit segment relative address */
5691da177e4SLinus Torvalds 			/* See note about special handling of SEGREL32 at
5701da177e4SLinus Torvalds 			 * the beginning of this file.
5711da177e4SLinus Torvalds 			 */
5721da177e4SLinus Torvalds 			*loc = fsel(val, addend);
5731da177e4SLinus Torvalds 			break;
5745f655322SMikulas Patocka 		case R_PARISC_SECREL32:
5755f655322SMikulas Patocka 			/* 32-bit section relative address. */
5765f655322SMikulas Patocka 			*loc = fsel(val, addend);
5775f655322SMikulas Patocka 			break;
5781da177e4SLinus Torvalds 		case R_PARISC_DPREL21L:
5791da177e4SLinus Torvalds 			/* left 21 bit of relative address */
5801da177e4SLinus Torvalds 			val = lrsel(val - dp, addend);
5811da177e4SLinus Torvalds 			*loc = mask(*loc, 21) | reassemble_21(val);
5821da177e4SLinus Torvalds 			break;
5831da177e4SLinus Torvalds 		case R_PARISC_DPREL14R:
5841da177e4SLinus Torvalds 			/* right 14 bit of relative address */
5851da177e4SLinus Torvalds 			val = rrsel(val - dp, addend);
5861da177e4SLinus Torvalds 			*loc = mask(*loc, 14) | reassemble_14(val);
5871da177e4SLinus Torvalds 			break;
5881da177e4SLinus Torvalds 		case R_PARISC_PCREL17F:
5891da177e4SLinus Torvalds 			/* 17-bit PC relative address */
590c298be74SHelge Deller 			/* calculate direct call offset */
591c298be74SHelge Deller 			val += addend;
5921da177e4SLinus Torvalds 			val = (val - dot - 8)/4;
593c298be74SHelge Deller 			if (!RELOC_REACHABLE(val, 17)) {
594c298be74SHelge Deller 				/* direct distance too far, create
595c298be74SHelge Deller 				 * stub entry instead */
596c298be74SHelge Deller 				val = get_stub(me, sym->st_value, addend,
597c298be74SHelge Deller 					ELF_STUB_DIRECT, loc0, targetsec);
598c298be74SHelge Deller 				val = (val - dot - 8)/4;
599c298be74SHelge Deller 				CHECK_RELOC(val, 17);
600c298be74SHelge Deller 			}
6011da177e4SLinus Torvalds 			*loc = (*loc & ~0x1f1ffd) | reassemble_17(val);
6021da177e4SLinus Torvalds 			break;
6031da177e4SLinus Torvalds 		case R_PARISC_PCREL22F:
6041da177e4SLinus Torvalds 			/* 22-bit PC relative address; only defined for pa20 */
605c298be74SHelge Deller 			/* calculate direct call offset */
606c298be74SHelge Deller 			val += addend;
607c298be74SHelge Deller 			val = (val - dot - 8)/4;
608c298be74SHelge Deller 			if (!RELOC_REACHABLE(val, 22)) {
609c298be74SHelge Deller 				/* direct distance too far, create
610c298be74SHelge Deller 				 * stub entry instead */
611c298be74SHelge Deller 				val = get_stub(me, sym->st_value, addend,
612c298be74SHelge Deller 					ELF_STUB_DIRECT, loc0, targetsec);
6131da177e4SLinus Torvalds 				val = (val - dot - 8)/4;
6141da177e4SLinus Torvalds 				CHECK_RELOC(val, 22);
615c298be74SHelge Deller 			}
6161da177e4SLinus Torvalds 			*loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
6171da177e4SLinus Torvalds 			break;
618592570c9SHelge Deller 		case R_PARISC_PCREL32:
619592570c9SHelge Deller 			/* 32-bit PC relative address */
620592570c9SHelge Deller 			*loc = val - dot - 8 + addend;
621592570c9SHelge Deller 			break;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 		default:
6241da177e4SLinus Torvalds 			printk(KERN_ERR "module %s: Unknown relocation: %u\n",
6251da177e4SLinus Torvalds 			       me->name, ELF32_R_TYPE(rel[i].r_info));
6261da177e4SLinus Torvalds 			return -ENOEXEC;
6271da177e4SLinus Torvalds 		}
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 	return 0;
6311da177e4SLinus Torvalds }
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds #else
apply_relocate_add(Elf_Shdr * sechdrs,const char * strtab,unsigned int symindex,unsigned int relsec,struct module * me)6341da177e4SLinus Torvalds int apply_relocate_add(Elf_Shdr *sechdrs,
6351da177e4SLinus Torvalds 		       const char *strtab,
6361da177e4SLinus Torvalds 		       unsigned int symindex,
6371da177e4SLinus Torvalds 		       unsigned int relsec,
6381da177e4SLinus Torvalds 		       struct module *me)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	int i;
6411da177e4SLinus Torvalds 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr;
6421da177e4SLinus Torvalds 	Elf64_Sym *sym;
6431da177e4SLinus Torvalds 	Elf64_Word *loc;
6441da177e4SLinus Torvalds 	Elf64_Xword *loc64;
6451da177e4SLinus Torvalds 	Elf64_Addr val;
6461da177e4SLinus Torvalds 	Elf64_Sxword addend;
6471da177e4SLinus Torvalds 	Elf64_Addr dot;
648c298be74SHelge Deller 	Elf_Addr loc0;
649c298be74SHelge Deller 	unsigned int targetsec = sechdrs[relsec].sh_info;
6501da177e4SLinus Torvalds 
6516183d68bSSven Schnelle 	pr_debug("Applying relocate section %u to %u\n", relsec,
652c298be74SHelge Deller 	       targetsec);
6531da177e4SLinus Torvalds 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
6541da177e4SLinus Torvalds 		/* This is where to make the change */
655c298be74SHelge Deller 		loc = (void *)sechdrs[targetsec].sh_addr
6561da177e4SLinus Torvalds 		      + rel[i].r_offset;
657c298be74SHelge Deller 		/* This is the start of the target section */
658c298be74SHelge Deller 		loc0 = sechdrs[targetsec].sh_addr;
6591da177e4SLinus Torvalds 		/* This is the symbol it is referring to */
6601da177e4SLinus Torvalds 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr
6611da177e4SLinus Torvalds 			+ ELF64_R_SYM(rel[i].r_info);
6621da177e4SLinus Torvalds 		if (!sym->st_value) {
6631da177e4SLinus Torvalds 			printk(KERN_WARNING "%s: Unknown symbol %s\n",
6641da177e4SLinus Torvalds 			       me->name, strtab + sym->st_name);
6651da177e4SLinus Torvalds 			return -ENOENT;
6661da177e4SLinus Torvalds 		}
6671da177e4SLinus Torvalds 		//dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03;
6681da177e4SLinus Torvalds 		dot = (Elf64_Addr)loc & ~0x03;
6691da177e4SLinus Torvalds 		loc64 = (Elf64_Xword *)loc;
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds 		val = sym->st_value;
6721da177e4SLinus Torvalds 		addend = rel[i].r_addend;
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds #if 0
6751da177e4SLinus Torvalds #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t :
6761da177e4SLinus Torvalds 		printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n",
6771da177e4SLinus Torvalds 			strtab + sym->st_name,
6781da177e4SLinus Torvalds 			loc, val, addend,
6791da177e4SLinus Torvalds 			r(R_PARISC_LTOFF14R)
6801da177e4SLinus Torvalds 			r(R_PARISC_LTOFF21L)
6811da177e4SLinus Torvalds 			r(R_PARISC_PCREL22F)
6821da177e4SLinus Torvalds 			r(R_PARISC_DIR64)
6831da177e4SLinus Torvalds 			r(R_PARISC_SEGREL32)
6841da177e4SLinus Torvalds 			r(R_PARISC_FPTR64)
6851da177e4SLinus Torvalds 			"UNKNOWN");
6861da177e4SLinus Torvalds #undef r
6871da177e4SLinus Torvalds #endif
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds 		switch (ELF64_R_TYPE(rel[i].r_info)) {
6901da177e4SLinus Torvalds 		case R_PARISC_LTOFF21L:
6911da177e4SLinus Torvalds 			/* LT-relative; left 21 bits */
6921da177e4SLinus Torvalds 			val = get_got(me, val, addend);
6936183d68bSSven Schnelle 			pr_debug("LTOFF21L Symbol %s loc %p val %llx\n",
6941da177e4SLinus Torvalds 			       strtab + sym->st_name,
6951da177e4SLinus Torvalds 			       loc, val);
6961da177e4SLinus Torvalds 			val = lrsel(val, 0);
6971da177e4SLinus Torvalds 			*loc = mask(*loc, 21) | reassemble_21(val);
6981da177e4SLinus Torvalds 			break;
6991da177e4SLinus Torvalds 		case R_PARISC_LTOFF14R:
7001da177e4SLinus Torvalds 			/* L(ltoff(val+addend)) */
7011da177e4SLinus Torvalds 			/* LT-relative; right 14 bits */
7021da177e4SLinus Torvalds 			val = get_got(me, val, addend);
7031da177e4SLinus Torvalds 			val = rrsel(val, 0);
7046183d68bSSven Schnelle 			pr_debug("LTOFF14R Symbol %s loc %p val %llx\n",
7051da177e4SLinus Torvalds 			       strtab + sym->st_name,
7061da177e4SLinus Torvalds 			       loc, val);
7071da177e4SLinus Torvalds 			*loc = mask(*loc, 14) | reassemble_14(val);
7081da177e4SLinus Torvalds 			break;
7091da177e4SLinus Torvalds 		case R_PARISC_PCREL22F:
7101da177e4SLinus Torvalds 			/* PC-relative; 22 bits */
7116183d68bSSven Schnelle 			pr_debug("PCREL22F Symbol %s loc %p val %llx\n",
7121da177e4SLinus Torvalds 			       strtab + sym->st_name,
7131da177e4SLinus Torvalds 			       loc, val);
714c298be74SHelge Deller 			val += addend;
7151da177e4SLinus Torvalds 			/* can we reach it locally? */
716ac3b4328SSong Liu 			if (within_module(val, me)) {
717c298be74SHelge Deller 				/* this is the case where the symbol is local
718c298be74SHelge Deller 				 * to the module, but in a different section,
719c298be74SHelge Deller 				 * so stub the jump in case it's more than 22
7206e1b9585SJames Bottomley 				 * bits away */
721c298be74SHelge Deller 				val = (val - dot - 8)/4;
722c298be74SHelge Deller 				if (!RELOC_REACHABLE(val, 22)) {
723c298be74SHelge Deller 					/* direct distance too far, create
724c298be74SHelge Deller 					 * stub entry instead */
725c298be74SHelge Deller 					val = get_stub(me, sym->st_value,
726c298be74SHelge Deller 						addend, ELF_STUB_DIRECT,
727c298be74SHelge Deller 						loc0, targetsec);
728c298be74SHelge Deller 				} else {
729c298be74SHelge Deller 					/* Ok, we can reach it directly. */
730c298be74SHelge Deller 					val = sym->st_value;
731c298be74SHelge Deller 					val += addend;
732c298be74SHelge Deller 				}
733c298be74SHelge Deller 			} else {
734c298be74SHelge Deller 				val = sym->st_value;
735c298be74SHelge Deller 				if (strncmp(strtab + sym->st_name, "$$", 2)
7361da177e4SLinus Torvalds 				    == 0)
7376e1b9585SJames Bottomley 					val = get_stub(me, val, addend, ELF_STUB_MILLI,
738c298be74SHelge Deller 						       loc0, targetsec);
7391da177e4SLinus Torvalds 				else
7406e1b9585SJames Bottomley 					val = get_stub(me, val, addend, ELF_STUB_GOT,
741c298be74SHelge Deller 						       loc0, targetsec);
7421da177e4SLinus Torvalds 			}
7436183d68bSSven Schnelle 			pr_debug("STUB FOR %s loc %px, val %llx+%llx at %llx\n",
7441da177e4SLinus Torvalds 			       strtab + sym->st_name, loc, sym->st_value,
7451da177e4SLinus Torvalds 			       addend, val);
7461da177e4SLinus Torvalds 			val = (val - dot - 8)/4;
747c298be74SHelge Deller 			CHECK_RELOC(val, 22);
7481da177e4SLinus Torvalds 			*loc = (*loc & ~0x3ff1ffd) | reassemble_22(val);
7491da177e4SLinus Torvalds 			break;
750592570c9SHelge Deller 		case R_PARISC_PCREL32:
751592570c9SHelge Deller 			/* 32-bit PC relative address */
752592570c9SHelge Deller 			*loc = val - dot - 8 + addend;
753592570c9SHelge Deller 			break;
754d2ba3b17SHelge Deller 		case R_PARISC_PCREL64:
755d2ba3b17SHelge Deller 			/* 64-bit PC relative address */
756d2ba3b17SHelge Deller 			*loc64 = val - dot - 8 + addend;
757d2ba3b17SHelge Deller 			break;
7581da177e4SLinus Torvalds 		case R_PARISC_DIR64:
7591da177e4SLinus Torvalds 			/* 64-bit effective address */
7601da177e4SLinus Torvalds 			*loc64 = val + addend;
7611da177e4SLinus Torvalds 			break;
7621da177e4SLinus Torvalds 		case R_PARISC_SEGREL32:
7631da177e4SLinus Torvalds 			/* 32-bit segment relative address */
7641da177e4SLinus Torvalds 			/* See note about special handling of SEGREL32 at
7651da177e4SLinus Torvalds 			 * the beginning of this file.
7661da177e4SLinus Torvalds 			 */
7671da177e4SLinus Torvalds 			*loc = fsel(val, addend);
7681da177e4SLinus Torvalds 			break;
7695f655322SMikulas Patocka 		case R_PARISC_SECREL32:
7705f655322SMikulas Patocka 			/* 32-bit section relative address. */
7715f655322SMikulas Patocka 			*loc = fsel(val, addend);
7725f655322SMikulas Patocka 			break;
7731da177e4SLinus Torvalds 		case R_PARISC_FPTR64:
7741da177e4SLinus Torvalds 			/* 64-bit function address */
775ac3b4328SSong Liu 			if (within_module(val + addend, me)) {
7761da177e4SLinus Torvalds 				*loc64 = get_fdesc(me, val+addend);
7776183d68bSSven Schnelle 				pr_debug("FDESC for %s at %llx points to %llx\n",
7781da177e4SLinus Torvalds 				       strtab + sym->st_name, *loc64,
7791da177e4SLinus Torvalds 				       ((Elf_Fdesc *)*loc64)->addr);
7801da177e4SLinus Torvalds 			} else {
7811da177e4SLinus Torvalds 				/* if the symbol is not local to this
7821da177e4SLinus Torvalds 				 * module then val+addend is a pointer
7831da177e4SLinus Torvalds 				 * to the function descriptor */
7846183d68bSSven Schnelle 				pr_debug("Non local FPTR64 Symbol %s loc %p val %llx\n",
7851da177e4SLinus Torvalds 				       strtab + sym->st_name,
7861da177e4SLinus Torvalds 				       loc, val);
7871da177e4SLinus Torvalds 				*loc64 = val + addend;
7881da177e4SLinus Torvalds 			}
7891da177e4SLinus Torvalds 			break;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 		default:
7921da177e4SLinus Torvalds 			printk(KERN_ERR "module %s: Unknown relocation: %Lu\n",
7931da177e4SLinus Torvalds 			       me->name, ELF64_R_TYPE(rel[i].r_info));
7941da177e4SLinus Torvalds 			return -ENOEXEC;
7951da177e4SLinus Torvalds 		}
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 	return 0;
7981da177e4SLinus Torvalds }
7991da177e4SLinus Torvalds #endif
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds static void
register_unwind_table(struct module * me,const Elf_Shdr * sechdrs)8021da177e4SLinus Torvalds register_unwind_table(struct module *me,
8031da177e4SLinus Torvalds 		      const Elf_Shdr *sechdrs)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	unsigned char *table, *end;
8061da177e4SLinus Torvalds 	unsigned long gp;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (!me->arch.unwind_section)
8091da177e4SLinus Torvalds 		return;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr;
8121da177e4SLinus Torvalds 	end = table + sechdrs[me->arch.unwind_section].sh_size;
813ac3b4328SSong Liu 	gp = (Elf_Addr)me->mem[MOD_TEXT].base + me->arch.got_offset;
8141da177e4SLinus Torvalds 
8156183d68bSSven Schnelle 	pr_debug("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n",
8161da177e4SLinus Torvalds 	       me->arch.unwind_section, table, end, gp);
8171da177e4SLinus Torvalds 	me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end);
8181da177e4SLinus Torvalds }
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds static void
deregister_unwind_table(struct module * me)8211da177e4SLinus Torvalds deregister_unwind_table(struct module *me)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	if (me->arch.unwind)
8241da177e4SLinus Torvalds 		unwind_table_remove(me->arch.unwind);
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
module_finalize(const Elf_Ehdr * hdr,const Elf_Shdr * sechdrs,struct module * me)8271da177e4SLinus Torvalds int module_finalize(const Elf_Ehdr *hdr,
8281da177e4SLinus Torvalds 		    const Elf_Shdr *sechdrs,
8291da177e4SLinus Torvalds 		    struct module *me)
8301da177e4SLinus Torvalds {
8311da177e4SLinus Torvalds 	int i;
8321da177e4SLinus Torvalds 	unsigned long nsyms;
8331da177e4SLinus Torvalds 	const char *strtab = NULL;
8348cc28269SHelge Deller 	const Elf_Shdr *s;
8358cc28269SHelge Deller 	char *secstrings;
836*0e466703SHelge Deller 	int symindex __maybe_unused = -1;
8371da177e4SLinus Torvalds 	Elf_Sym *newptr, *oldptr;
8381da177e4SLinus Torvalds 	Elf_Shdr *symhdr = NULL;
8391da177e4SLinus Torvalds #ifdef DEBUG
8401da177e4SLinus Torvalds 	Elf_Fdesc *entry;
8411da177e4SLinus Torvalds 	u32 *addr;
8421da177e4SLinus Torvalds 
8431da177e4SLinus Torvalds 	entry = (Elf_Fdesc *)me->init;
8441da177e4SLinus Torvalds 	printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry,
8451da177e4SLinus Torvalds 	       entry->gp, entry->addr);
8461da177e4SLinus Torvalds 	addr = (u32 *)entry->addr;
8471da177e4SLinus Torvalds 	printk("INSNS: %x %x %x %x\n",
8481da177e4SLinus Torvalds 	       addr[0], addr[1], addr[2], addr[3]);
849c298be74SHelge Deller 	printk("got entries used %ld, gots max %ld\n"
8501da177e4SLinus Torvalds 	       "fdescs used %ld, fdescs max %ld\n",
8511da177e4SLinus Torvalds 	       me->arch.got_count, me->arch.got_max,
8521da177e4SLinus Torvalds 	       me->arch.fdesc_count, me->arch.fdesc_max);
8531da177e4SLinus Torvalds #endif
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	register_unwind_table(me, sechdrs);
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	/* haven't filled in me->symtab yet, so have to find it
8581da177e4SLinus Torvalds 	 * ourselves */
8591da177e4SLinus Torvalds 	for (i = 1; i < hdr->e_shnum; i++) {
8601da177e4SLinus Torvalds 		if(sechdrs[i].sh_type == SHT_SYMTAB
861fe579c69SJulia Lawall 		   && (sechdrs[i].sh_flags & SHF_ALLOC)) {
8621da177e4SLinus Torvalds 			int strindex = sechdrs[i].sh_link;
8636ca63662SSven Schnelle 			symindex = i;
8641da177e4SLinus Torvalds 			/* FIXME: AWFUL HACK
8651da177e4SLinus Torvalds 			 * The cast is to drop the const from
8661da177e4SLinus Torvalds 			 * the sechdrs pointer */
8671da177e4SLinus Torvalds 			symhdr = (Elf_Shdr *)&sechdrs[i];
8681da177e4SLinus Torvalds 			strtab = (char *)sechdrs[strindex].sh_addr;
8691da177e4SLinus Torvalds 			break;
8701da177e4SLinus Torvalds 		}
8711da177e4SLinus Torvalds 	}
8721da177e4SLinus Torvalds 
8736183d68bSSven Schnelle 	pr_debug("module %s: strtab %p, symhdr %p\n",
8741da177e4SLinus Torvalds 	       me->name, strtab, symhdr);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	if(me->arch.got_count > MAX_GOTS) {
877f8fc18a1SHelge Deller 		printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n",
878f8fc18a1SHelge Deller 				me->name, me->arch.got_count, MAX_GOTS);
8791da177e4SLinus Torvalds 		return -EINVAL;
8801da177e4SLinus Torvalds 	}
8811da177e4SLinus Torvalds 
882c298be74SHelge Deller 	kfree(me->arch.section);
883c298be74SHelge Deller 	me->arch.section = NULL;
884c298be74SHelge Deller 
8851da177e4SLinus Torvalds 	/* no symbol table */
8861da177e4SLinus Torvalds 	if(symhdr == NULL)
8871da177e4SLinus Torvalds 		return 0;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	oldptr = (void *)symhdr->sh_addr;
8901da177e4SLinus Torvalds 	newptr = oldptr + 1;	/* we start counting at 1 */
8911da177e4SLinus Torvalds 	nsyms = symhdr->sh_size / sizeof(Elf_Sym);
8926183d68bSSven Schnelle 	pr_debug("OLD num_symtab %lu\n", nsyms);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	for (i = 1; i < nsyms; i++) {
8951da177e4SLinus Torvalds 		oldptr++;	/* note, count starts at 1 so preincrement */
8961da177e4SLinus Torvalds 		if(strncmp(strtab + oldptr->st_name,
8971da177e4SLinus Torvalds 			      ".L", 2) == 0)
8981da177e4SLinus Torvalds 			continue;
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 		if(newptr != oldptr)
9011da177e4SLinus Torvalds 			*newptr++ = *oldptr;
9021da177e4SLinus Torvalds 		else
9031da177e4SLinus Torvalds 			newptr++;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	}
9061da177e4SLinus Torvalds 	nsyms = newptr - (Elf_Sym *)symhdr->sh_addr;
9076183d68bSSven Schnelle 	pr_debug("NEW num_symtab %lu\n", nsyms);
9081da177e4SLinus Torvalds 	symhdr->sh_size = nsyms * sizeof(Elf_Sym);
9098cc28269SHelge Deller 
9108cc28269SHelge Deller 	/* find .altinstructions section */
9118cc28269SHelge Deller 	secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
9128cc28269SHelge Deller 	for (s = sechdrs; s < sechdrs + hdr->e_shnum; s++) {
9138cc28269SHelge Deller 		void *aseg = (void *) s->sh_addr;
9148cc28269SHelge Deller 		char *secname = secstrings + s->sh_name;
9158cc28269SHelge Deller 
9168cc28269SHelge Deller 		if (!strcmp(".altinstructions", secname))
9178cc28269SHelge Deller 			/* patch .altinstructions */
9188cc28269SHelge Deller 			apply_alternatives(aseg, aseg + s->sh_size, me->name);
9198cc28269SHelge Deller 
920a1326b17SMark Rutland #ifdef CONFIG_DYNAMIC_FTRACE
9216ca63662SSven Schnelle 		/* For 32 bit kernels we're compiling modules with
9226ca63662SSven Schnelle 		 * -ffunction-sections so we must relocate the addresses in the
923a1326b17SMark Rutland 		 *  ftrace callsite section.
9246ca63662SSven Schnelle 		 */
925a1326b17SMark Rutland 		if (symindex != -1 && !strcmp(secname, FTRACE_CALLSITE_SECTION)) {
926a1326b17SMark Rutland 			int err;
9276ca63662SSven Schnelle 			if (s->sh_type == SHT_REL)
9286ca63662SSven Schnelle 				err = apply_relocate((Elf_Shdr *)sechdrs,
9296ca63662SSven Schnelle 							strtab, symindex,
9306ca63662SSven Schnelle 							s - sechdrs, me);
9316ca63662SSven Schnelle 			else if (s->sh_type == SHT_RELA)
9326ca63662SSven Schnelle 				err = apply_relocate_add((Elf_Shdr *)sechdrs,
9336ca63662SSven Schnelle 							strtab, symindex,
9346ca63662SSven Schnelle 							s - sechdrs, me);
9356ca63662SSven Schnelle 			if (err)
9366ca63662SSven Schnelle 				return err;
9376ca63662SSven Schnelle 		}
938a1326b17SMark Rutland #endif
9396ca63662SSven Schnelle 	}
9405336377dSLinus Torvalds 	return 0;
9411da177e4SLinus Torvalds }
9421da177e4SLinus Torvalds 
module_arch_cleanup(struct module * mod)9431da177e4SLinus Torvalds void module_arch_cleanup(struct module *mod)
9441da177e4SLinus Torvalds {
9451da177e4SLinus Torvalds 	deregister_unwind_table(mod);
9461da177e4SLinus Torvalds }
9471705bd6aSSergey Senozhatsky 
9481705bd6aSSergey Senozhatsky #ifdef CONFIG_64BIT
dereference_module_function_descriptor(struct module * mod,void * ptr)9491705bd6aSSergey Senozhatsky void *dereference_module_function_descriptor(struct module *mod, void *ptr)
9501705bd6aSSergey Senozhatsky {
951ac3b4328SSong Liu 	unsigned long start_opd = (Elf64_Addr)mod->mem[MOD_TEXT].base +
9521705bd6aSSergey Senozhatsky 				   mod->arch.fdesc_offset;
9531705bd6aSSergey Senozhatsky 	unsigned long end_opd = start_opd +
9541705bd6aSSergey Senozhatsky 				mod->arch.fdesc_count * sizeof(Elf64_Fdesc);
9551705bd6aSSergey Senozhatsky 
9561705bd6aSSergey Senozhatsky 	if (ptr < (void *)start_opd || ptr >= (void *)end_opd)
9571705bd6aSSergey Senozhatsky 		return ptr;
9581705bd6aSSergey Senozhatsky 
9591705bd6aSSergey Senozhatsky 	return dereference_function_descriptor(ptr);
9601705bd6aSSergey Senozhatsky }
9611705bd6aSSergey Senozhatsky #endif
962