| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 9 |  *    Copyright (C) 2008 Helge Deller <deller@gmx.de> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 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 |  * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  *    - SEGREL32 handling | 
 | 42 |  *      We are not doing SEGREL32 handling correctly. According to the ABI, we | 
 | 43 |  *      should do a value offset, like this: | 
| Eric Biederman | 959ed34 | 2006-09-29 02:00:06 -0700 | [diff] [blame] | 44 |  *			if (in_init(me, (void *)val)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Helge Deller | 6891f8a | 2006-12-16 16:16:50 +0100 | [diff] [blame] | 63 | #include <linux/bug.h> | 
| James Bottomley | d7dd2ff | 2011-04-14 18:25:21 -0500 | [diff] [blame] | 64 | #include <linux/mm.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 65 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 |  | 
| James Bottomley | d7dd2ff | 2011-04-14 18:25:21 -0500 | [diff] [blame] | 67 | #include <asm/pgtable.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <asm/unwind.h> | 
 | 69 |  | 
 | 70 | #if 0 | 
 | 71 | #define DEBUGP printk | 
 | 72 | #else | 
 | 73 | #define DEBUGP(fmt...) | 
 | 74 | #endif | 
 | 75 |  | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | #define CHECK_RELOC(val, bits) \ | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 82 | 	if (!RELOC_REACHABLE(val, bits)) { \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| John David Anglin | b4f2e2a | 2009-08-02 12:34:08 +0200 | [diff] [blame] | 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 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
 | 99 | /* three functions to determine where in the module core | 
 | 100 |  * or init pieces the location is */ | 
| Eric Biederman | 959ed34 | 2006-09-29 02:00:06 -0700 | [diff] [blame] | 101 | static inline int in_init(struct module *me, void *loc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | { | 
 | 103 | 	return (loc >= me->module_init && | 
 | 104 | 		loc <= (me->module_init + me->init_size)); | 
 | 105 | } | 
 | 106 |  | 
| Eric Biederman | 959ed34 | 2006-09-29 02:00:06 -0700 | [diff] [blame] | 107 | static inline int in_core(struct module *me, void *loc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { | 
 | 109 | 	return (loc >= me->module_core && | 
 | 110 | 		loc <= (me->module_core + me->core_size)); | 
 | 111 | } | 
 | 112 |  | 
| Eric Biederman | 959ed34 | 2006-09-29 02:00:06 -0700 | [diff] [blame] | 113 | static inline int in_local(struct module *me, void *loc) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { | 
| Eric Biederman | 959ed34 | 2006-09-29 02:00:06 -0700 | [diff] [blame] | 115 | 	return in_init(me, loc) || in_core(me, loc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } | 
 | 117 |  | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 118 | #ifndef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | struct got_entry { | 
 | 120 | 	Elf32_Addr addr; | 
 | 121 | }; | 
 | 122 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | struct stub_entry { | 
 | 124 | 	Elf32_Word insns[2]; /* each stub entry has two insns */ | 
 | 125 | }; | 
 | 126 | #else | 
 | 127 | struct got_entry { | 
 | 128 | 	Elf64_Addr addr; | 
 | 129 | }; | 
 | 130 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | struct 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.  */ | 
| John David Anglin | b4f2e2a | 2009-08-02 12:34:08 +0200 | [diff] [blame] | 155 | static 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 |  | 
 | 163 | static 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | static inline int reassemble_14(int as14) | 
 | 173 | { | 
 | 174 | 	return (((as14 & 0x1fff) << 1) | | 
 | 175 | 		((as14 & 0x2000) >> 13)); | 
 | 176 | } | 
 | 177 |  | 
| John David Anglin | b4f2e2a | 2009-08-02 12:34:08 +0200 | [diff] [blame] | 178 | static 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 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | static 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 |  | 
 | 197 | static 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 |  | 
 | 206 | static 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 |  | 
 | 215 | void *module_alloc(unsigned long size) | 
 | 216 | { | 
 | 217 | 	if (size == 0) | 
 | 218 | 		return NULL; | 
| James Bottomley | d7dd2ff | 2011-04-14 18:25:21 -0500 | [diff] [blame] | 219 | 	/* using RWX means less protection for modules, but it's | 
 | 220 | 	 * easier than trying to map the text, data, init_text and | 
 | 221 | 	 * init_data correctly */ | 
 | 222 | 	return __vmalloc_node_range(size, 1, VMALLOC_START, VMALLOC_END, | 
 | 223 | 				    GFP_KERNEL | __GFP_HIGHMEM, | 
 | 224 | 				    PAGE_KERNEL_RWX, -1, | 
 | 225 | 				    __builtin_return_address(0)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } | 
 | 227 |  | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 228 | #ifndef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n) | 
 | 230 | { | 
 | 231 | 	return 0; | 
 | 232 | } | 
 | 233 |  | 
 | 234 | static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n) | 
 | 235 | { | 
 | 236 | 	return 0; | 
 | 237 | } | 
 | 238 |  | 
 | 239 | static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n) | 
 | 240 | { | 
 | 241 | 	unsigned long cnt = 0; | 
 | 242 |  | 
 | 243 | 	for (; n > 0; n--, rela++) | 
 | 244 | 	{ | 
 | 245 | 		switch (ELF32_R_TYPE(rela->r_info)) { | 
 | 246 | 			case R_PARISC_PCREL17F: | 
 | 247 | 			case R_PARISC_PCREL22F: | 
 | 248 | 				cnt++; | 
 | 249 | 		} | 
 | 250 | 	} | 
 | 251 |  | 
 | 252 | 	return cnt; | 
 | 253 | } | 
 | 254 | #else | 
 | 255 | static inline unsigned long count_gots(const Elf_Rela *rela, unsigned long n) | 
 | 256 | { | 
 | 257 | 	unsigned long cnt = 0; | 
 | 258 |  | 
 | 259 | 	for (; n > 0; n--, rela++) | 
 | 260 | 	{ | 
 | 261 | 		switch (ELF64_R_TYPE(rela->r_info)) { | 
 | 262 | 			case R_PARISC_LTOFF21L: | 
 | 263 | 			case R_PARISC_LTOFF14R: | 
 | 264 | 			case R_PARISC_PCREL22F: | 
 | 265 | 				cnt++; | 
 | 266 | 		} | 
 | 267 | 	} | 
 | 268 |  | 
 | 269 | 	return cnt; | 
 | 270 | } | 
 | 271 |  | 
 | 272 | static inline unsigned long count_fdescs(const Elf_Rela *rela, unsigned long n) | 
 | 273 | { | 
 | 274 | 	unsigned long cnt = 0; | 
 | 275 |  | 
 | 276 | 	for (; n > 0; n--, rela++) | 
 | 277 | 	{ | 
 | 278 | 		switch (ELF64_R_TYPE(rela->r_info)) { | 
 | 279 | 			case R_PARISC_FPTR64: | 
 | 280 | 				cnt++; | 
 | 281 | 		} | 
 | 282 | 	} | 
 | 283 |  | 
 | 284 | 	return cnt; | 
 | 285 | } | 
 | 286 |  | 
 | 287 | static inline unsigned long count_stubs(const Elf_Rela *rela, unsigned long n) | 
 | 288 | { | 
 | 289 | 	unsigned long cnt = 0; | 
 | 290 |  | 
 | 291 | 	for (; n > 0; n--, rela++) | 
 | 292 | 	{ | 
 | 293 | 		switch (ELF64_R_TYPE(rela->r_info)) { | 
 | 294 | 			case R_PARISC_PCREL22F: | 
 | 295 | 				cnt++; | 
 | 296 | 		} | 
 | 297 | 	} | 
 | 298 |  | 
 | 299 | 	return cnt; | 
 | 300 | } | 
 | 301 | #endif | 
 | 302 |  | 
 | 303 |  | 
 | 304 | /* Free memory returned from module_alloc */ | 
 | 305 | void module_free(struct module *mod, void *module_region) | 
 | 306 | { | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 307 | 	kfree(mod->arch.section); | 
 | 308 | 	mod->arch.section = NULL; | 
 | 309 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | 	vfree(module_region); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } | 
 | 312 |  | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 313 | /* Additional bytes needed in front of individual sections */ | 
 | 314 | unsigned int arch_mod_section_prepend(struct module *mod, | 
 | 315 | 				      unsigned int section) | 
 | 316 | { | 
 | 317 | 	/* size needed for all stubs of this section (including | 
 | 318 | 	 * one additional for correct alignment of the stubs) */ | 
 | 319 | 	return (mod->arch.section[section].stub_entries + 1) | 
 | 320 | 		* sizeof(struct stub_entry); | 
 | 321 | } | 
 | 322 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | #define CONST  | 
 | 324 | int module_frob_arch_sections(CONST Elf_Ehdr *hdr, | 
 | 325 | 			      CONST Elf_Shdr *sechdrs, | 
 | 326 | 			      CONST char *secstrings, | 
 | 327 | 			      struct module *me) | 
 | 328 | { | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 329 | 	unsigned long gots = 0, fdescs = 0, len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 	unsigned int i; | 
 | 331 |  | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 332 | 	len = hdr->e_shnum * sizeof(me->arch.section[0]); | 
 | 333 | 	me->arch.section = kzalloc(len, GFP_KERNEL); | 
 | 334 | 	if (!me->arch.section) | 
 | 335 | 		return -ENOMEM; | 
 | 336 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | 	for (i = 1; i < hdr->e_shnum; i++) { | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 338 | 		const Elf_Rela *rels = (void *)sechdrs[i].sh_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | 		unsigned long nrels = sechdrs[i].sh_size / sizeof(*rels); | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 340 | 		unsigned int count, s; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 |  | 
 | 342 | 		if (strncmp(secstrings + sechdrs[i].sh_name, | 
 | 343 | 			    ".PARISC.unwind", 14) == 0) | 
 | 344 | 			me->arch.unwind_section = i; | 
 | 345 |  | 
 | 346 | 		if (sechdrs[i].sh_type != SHT_RELA) | 
 | 347 | 			continue; | 
 | 348 |  | 
 | 349 | 		/* some of these are not relevant for 32-bit/64-bit | 
 | 350 | 		 * we leave them here to make the code common. the | 
 | 351 | 		 * compiler will do its thing and optimize out the | 
 | 352 | 		 * stuff we don't need | 
 | 353 | 		 */ | 
 | 354 | 		gots += count_gots(rels, nrels); | 
 | 355 | 		fdescs += count_fdescs(rels, nrels); | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 356 |  | 
 | 357 | 		/* XXX: By sorting the relocs and finding duplicate entries | 
 | 358 | 		 *  we could reduce the number of necessary stubs and save | 
 | 359 | 		 *  some memory. */ | 
 | 360 | 		count = count_stubs(rels, nrels); | 
 | 361 | 		if (!count) | 
 | 362 | 			continue; | 
 | 363 |  | 
 | 364 | 		/* so we need relocation stubs. reserve necessary memory. */ | 
 | 365 | 		/* sh_info gives the section for which we need to add stubs. */ | 
 | 366 | 		s = sechdrs[i].sh_info; | 
 | 367 |  | 
 | 368 | 		/* each code section should only have one relocation section */ | 
 | 369 | 		WARN_ON(me->arch.section[s].stub_entries); | 
 | 370 |  | 
 | 371 | 		/* store number of stubs we need for this section */ | 
 | 372 | 		me->arch.section[s].stub_entries += count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | 	} | 
 | 374 |  | 
 | 375 | 	/* align things a bit */ | 
 | 376 | 	me->core_size = ALIGN(me->core_size, 16); | 
 | 377 | 	me->arch.got_offset = me->core_size; | 
 | 378 | 	me->core_size += gots * sizeof(struct got_entry); | 
 | 379 |  | 
 | 380 | 	me->core_size = ALIGN(me->core_size, 16); | 
 | 381 | 	me->arch.fdesc_offset = me->core_size; | 
 | 382 | 	me->core_size += fdescs * sizeof(Elf_Fdesc); | 
 | 383 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | 	me->arch.got_max = gots; | 
 | 385 | 	me->arch.fdesc_max = fdescs; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 |  | 
 | 387 | 	return 0; | 
 | 388 | } | 
 | 389 |  | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 390 | #ifdef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | static Elf64_Word get_got(struct module *me, unsigned long value, long addend) | 
 | 392 | { | 
 | 393 | 	unsigned int i; | 
 | 394 | 	struct got_entry *got; | 
 | 395 |  | 
 | 396 | 	value += addend; | 
 | 397 |  | 
 | 398 | 	BUG_ON(value == 0); | 
 | 399 |  | 
 | 400 | 	got = me->module_core + me->arch.got_offset; | 
 | 401 | 	for (i = 0; got[i].addr; i++) | 
 | 402 | 		if (got[i].addr == value) | 
 | 403 | 			goto out; | 
 | 404 |  | 
 | 405 | 	BUG_ON(++me->arch.got_count > me->arch.got_max); | 
 | 406 |  | 
 | 407 | 	got[i].addr = value; | 
 | 408 |  out: | 
 | 409 | 	DEBUGP("GOT ENTRY %d[%x] val %lx\n", i, i*sizeof(struct got_entry), | 
 | 410 | 	       value); | 
 | 411 | 	return i * sizeof(struct got_entry); | 
 | 412 | } | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 413 | #endif /* CONFIG_64BIT */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 |  | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 415 | #ifdef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | static Elf_Addr get_fdesc(struct module *me, unsigned long value) | 
 | 417 | { | 
 | 418 | 	Elf_Fdesc *fdesc = me->module_core + me->arch.fdesc_offset; | 
 | 419 |  | 
 | 420 | 	if (!value) { | 
 | 421 | 		printk(KERN_ERR "%s: zero OPD requested!\n", me->name); | 
 | 422 | 		return 0; | 
 | 423 | 	} | 
 | 424 |  | 
 | 425 | 	/* Look for existing fdesc entry. */ | 
 | 426 | 	while (fdesc->addr) { | 
 | 427 | 		if (fdesc->addr == value) | 
 | 428 | 			return (Elf_Addr)fdesc; | 
 | 429 | 		fdesc++; | 
 | 430 | 	} | 
 | 431 |  | 
 | 432 | 	BUG_ON(++me->arch.fdesc_count > me->arch.fdesc_max); | 
 | 433 |  | 
 | 434 | 	/* Create new one */ | 
 | 435 | 	fdesc->addr = value; | 
 | 436 | 	fdesc->gp = (Elf_Addr)me->module_core + me->arch.got_offset; | 
 | 437 | 	return (Elf_Addr)fdesc; | 
 | 438 | } | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 439 | #endif /* CONFIG_64BIT */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 |  | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 441 | enum elf_stub_type { | 
 | 442 | 	ELF_STUB_GOT, | 
 | 443 | 	ELF_STUB_MILLI, | 
 | 444 | 	ELF_STUB_DIRECT, | 
 | 445 | }; | 
 | 446 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | static Elf_Addr get_stub(struct module *me, unsigned long value, long addend, | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 448 | 	enum elf_stub_type stub_type, Elf_Addr loc0, unsigned int targetsec) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | 	struct stub_entry *stub; | 
| John David Anglin | b4f2e2a | 2009-08-02 12:34:08 +0200 | [diff] [blame] | 451 | 	int __maybe_unused d; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 |  | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 453 | 	/* initialize stub_offset to point in front of the section */ | 
 | 454 | 	if (!me->arch.section[targetsec].stub_offset) { | 
 | 455 | 		loc0 -= (me->arch.section[targetsec].stub_entries + 1) * | 
 | 456 | 				sizeof(struct stub_entry); | 
 | 457 | 		/* get correct alignment for the stubs */ | 
 | 458 | 		loc0 = ALIGN(loc0, sizeof(struct stub_entry)); | 
 | 459 | 		me->arch.section[targetsec].stub_offset = loc0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | 	} | 
 | 461 |  | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 462 | 	/* get address of stub entry */ | 
 | 463 | 	stub = (void *) me->arch.section[targetsec].stub_offset; | 
 | 464 | 	me->arch.section[targetsec].stub_offset += sizeof(struct stub_entry); | 
 | 465 |  | 
 | 466 | 	/* do not write outside available stub area */ | 
 | 467 | 	BUG_ON(0 == me->arch.section[targetsec].stub_entries--); | 
 | 468 |  | 
 | 469 |  | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 470 | #ifndef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | /* for 32-bit the stub looks like this: | 
 | 472 |  * 	ldil L'XXX,%r1 | 
 | 473 |  * 	be,n R'XXX(%sr4,%r1) | 
 | 474 |  */ | 
 | 475 | 	//value = *(unsigned long *)((value + addend) & ~3); /* why? */ | 
 | 476 |  | 
 | 477 | 	stub->insns[0] = 0x20200000;	/* ldil L'XXX,%r1	*/ | 
 | 478 | 	stub->insns[1] = 0xe0202002;	/* be,n R'XXX(%sr4,%r1)	*/ | 
 | 479 |  | 
 | 480 | 	stub->insns[0] |= reassemble_21(lrsel(value, addend)); | 
 | 481 | 	stub->insns[1] |= reassemble_17(rrsel(value, addend) / 4); | 
 | 482 |  | 
 | 483 | #else | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 484 | /* for 64-bit we have three kinds of stubs: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 |  * for normal function calls: | 
 | 486 |  * 	ldd 0(%dp),%dp | 
 | 487 |  * 	ldd 10(%dp), %r1 | 
 | 488 |  * 	bve (%r1) | 
 | 489 |  * 	ldd 18(%dp), %dp | 
 | 490 |  * | 
 | 491 |  * for millicode: | 
 | 492 |  * 	ldil 0, %r1 | 
 | 493 |  * 	ldo 0(%r1), %r1 | 
 | 494 |  * 	ldd 10(%r1), %r1 | 
 | 495 |  * 	bve,n (%r1) | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 496 |  * | 
 | 497 |  * for direct branches (jumps between different section of the | 
 | 498 |  * same module): | 
 | 499 |  *	ldil 0, %r1 | 
 | 500 |  *	ldo 0(%r1), %r1 | 
 | 501 |  *	bve,n (%r1) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 |  */ | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 503 | 	switch (stub_type) { | 
 | 504 | 	case ELF_STUB_GOT: | 
| John David Anglin | b4f2e2a | 2009-08-02 12:34:08 +0200 | [diff] [blame] | 505 | 		d = get_got(me, value, addend); | 
 | 506 | 		if (d <= 15) { | 
 | 507 | 			/* Format 5 */ | 
 | 508 | 			stub->insns[0] = 0x0f6010db; /* ldd 0(%dp),%dp	*/ | 
 | 509 | 			stub->insns[0] |= low_sign_unext(d, 5) << 16; | 
 | 510 | 		} else { | 
 | 511 | 			/* Format 3 */ | 
 | 512 | 			stub->insns[0] = 0x537b0000; /* ldd 0(%dp),%dp	*/ | 
 | 513 | 			stub->insns[0] |= reassemble_16a(d); | 
 | 514 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | 		stub->insns[1] = 0x53610020;	/* ldd 10(%dp),%r1	*/ | 
 | 516 | 		stub->insns[2] = 0xe820d000;	/* bve (%r1)		*/ | 
 | 517 | 		stub->insns[3] = 0x537b0030;	/* ldd 18(%dp),%dp	*/ | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 518 | 		break; | 
 | 519 | 	case ELF_STUB_MILLI: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | 		stub->insns[0] = 0x20200000;	/* ldil 0,%r1		*/ | 
 | 521 | 		stub->insns[1] = 0x34210000;	/* ldo 0(%r1), %r1	*/ | 
 | 522 | 		stub->insns[2] = 0x50210020;	/* ldd 10(%r1),%r1	*/ | 
 | 523 | 		stub->insns[3] = 0xe820d002;	/* bve,n (%r1)		*/ | 
 | 524 |  | 
 | 525 | 		stub->insns[0] |= reassemble_21(lrsel(value, addend)); | 
 | 526 | 		stub->insns[1] |= reassemble_14(rrsel(value, addend)); | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 527 | 		break; | 
 | 528 | 	case ELF_STUB_DIRECT: | 
 | 529 | 		stub->insns[0] = 0x20200000;    /* ldil 0,%r1           */ | 
 | 530 | 		stub->insns[1] = 0x34210000;    /* ldo 0(%r1), %r1      */ | 
 | 531 | 		stub->insns[2] = 0xe820d002;    /* bve,n (%r1)          */ | 
 | 532 |  | 
 | 533 | 		stub->insns[0] |= reassemble_21(lrsel(value, addend)); | 
 | 534 | 		stub->insns[1] |= reassemble_14(rrsel(value, addend)); | 
 | 535 | 		break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | 	} | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 537 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | #endif | 
 | 539 |  | 
 | 540 | 	return (Elf_Addr)stub; | 
 | 541 | } | 
 | 542 |  | 
| Helge Deller | a8f44e3 | 2007-01-28 14:58:52 +0100 | [diff] [blame] | 543 | #ifndef CONFIG_64BIT | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | int apply_relocate_add(Elf_Shdr *sechdrs, | 
 | 545 | 		       const char *strtab, | 
 | 546 | 		       unsigned int symindex, | 
 | 547 | 		       unsigned int relsec, | 
 | 548 | 		       struct module *me) | 
 | 549 | { | 
 | 550 | 	int i; | 
 | 551 | 	Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr; | 
 | 552 | 	Elf32_Sym *sym; | 
 | 553 | 	Elf32_Word *loc; | 
 | 554 | 	Elf32_Addr val; | 
 | 555 | 	Elf32_Sword addend; | 
 | 556 | 	Elf32_Addr dot; | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 557 | 	Elf_Addr loc0; | 
 | 558 | 	unsigned int targetsec = sechdrs[relsec].sh_info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | 	//unsigned long dp = (unsigned long)$global$; | 
 | 560 | 	register unsigned long dp asm ("r27"); | 
 | 561 |  | 
 | 562 | 	DEBUGP("Applying relocate section %u to %u\n", relsec, | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 563 | 	       targetsec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { | 
 | 565 | 		/* This is where to make the change */ | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 566 | 		loc = (void *)sechdrs[targetsec].sh_addr | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | 		      + rel[i].r_offset; | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 568 | 		/* This is the start of the target section */ | 
 | 569 | 		loc0 = sechdrs[targetsec].sh_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | 		/* This is the symbol it is referring to */ | 
 | 571 | 		sym = (Elf32_Sym *)sechdrs[symindex].sh_addr | 
 | 572 | 			+ ELF32_R_SYM(rel[i].r_info); | 
 | 573 | 		if (!sym->st_value) { | 
 | 574 | 			printk(KERN_WARNING "%s: Unknown symbol %s\n", | 
 | 575 | 			       me->name, strtab + sym->st_name); | 
 | 576 | 			return -ENOENT; | 
 | 577 | 		} | 
 | 578 | 		//dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03; | 
 | 579 | 		dot =  (Elf32_Addr)loc & ~0x03; | 
 | 580 |  | 
 | 581 | 		val = sym->st_value; | 
 | 582 | 		addend = rel[i].r_addend; | 
 | 583 |  | 
 | 584 | #if 0 | 
 | 585 | #define r(t) ELF32_R_TYPE(rel[i].r_info)==t ? #t : | 
 | 586 | 		DEBUGP("Symbol %s loc 0x%x val 0x%x addend 0x%x: %s\n", | 
 | 587 | 			strtab + sym->st_name, | 
 | 588 | 			(uint32_t)loc, val, addend, | 
 | 589 | 			r(R_PARISC_PLABEL32) | 
 | 590 | 			r(R_PARISC_DIR32) | 
 | 591 | 			r(R_PARISC_DIR21L) | 
 | 592 | 			r(R_PARISC_DIR14R) | 
 | 593 | 			r(R_PARISC_SEGREL32) | 
 | 594 | 			r(R_PARISC_DPREL21L) | 
 | 595 | 			r(R_PARISC_DPREL14R) | 
 | 596 | 			r(R_PARISC_PCREL17F) | 
 | 597 | 			r(R_PARISC_PCREL22F) | 
 | 598 | 			"UNKNOWN"); | 
 | 599 | #undef r | 
 | 600 | #endif | 
 | 601 |  | 
 | 602 | 		switch (ELF32_R_TYPE(rel[i].r_info)) { | 
 | 603 | 		case R_PARISC_PLABEL32: | 
 | 604 | 			/* 32-bit function address */ | 
 | 605 | 			/* no function descriptors... */ | 
 | 606 | 			*loc = fsel(val, addend); | 
 | 607 | 			break; | 
 | 608 | 		case R_PARISC_DIR32: | 
 | 609 | 			/* direct 32-bit ref */ | 
 | 610 | 			*loc = fsel(val, addend); | 
 | 611 | 			break; | 
 | 612 | 		case R_PARISC_DIR21L: | 
 | 613 | 			/* left 21 bits of effective address */ | 
 | 614 | 			val = lrsel(val, addend); | 
 | 615 | 			*loc = mask(*loc, 21) | reassemble_21(val); | 
 | 616 | 			break; | 
 | 617 | 		case R_PARISC_DIR14R: | 
 | 618 | 			/* right 14 bits of effective address */ | 
 | 619 | 			val = rrsel(val, addend); | 
 | 620 | 			*loc = mask(*loc, 14) | reassemble_14(val); | 
 | 621 | 			break; | 
 | 622 | 		case R_PARISC_SEGREL32: | 
 | 623 | 			/* 32-bit segment relative address */ | 
 | 624 | 			/* See note about special handling of SEGREL32 at | 
 | 625 | 			 * the beginning of this file. | 
 | 626 | 			 */ | 
 | 627 | 			*loc = fsel(val, addend);  | 
 | 628 | 			break; | 
 | 629 | 		case R_PARISC_DPREL21L: | 
 | 630 | 			/* left 21 bit of relative address */ | 
 | 631 | 			val = lrsel(val - dp, addend); | 
 | 632 | 			*loc = mask(*loc, 21) | reassemble_21(val); | 
 | 633 | 			break; | 
 | 634 | 		case R_PARISC_DPREL14R: | 
 | 635 | 			/* right 14 bit of relative address */ | 
 | 636 | 			val = rrsel(val - dp, addend); | 
 | 637 | 			*loc = mask(*loc, 14) | reassemble_14(val); | 
 | 638 | 			break; | 
 | 639 | 		case R_PARISC_PCREL17F: | 
 | 640 | 			/* 17-bit PC relative address */ | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 641 | 			/* calculate direct call offset */ | 
 | 642 | 			val += addend; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | 			val = (val - dot - 8)/4; | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 644 | 			if (!RELOC_REACHABLE(val, 17)) { | 
 | 645 | 				/* direct distance too far, create | 
 | 646 | 				 * stub entry instead */ | 
 | 647 | 				val = get_stub(me, sym->st_value, addend, | 
 | 648 | 					ELF_STUB_DIRECT, loc0, targetsec); | 
 | 649 | 				val = (val - dot - 8)/4; | 
 | 650 | 				CHECK_RELOC(val, 17); | 
 | 651 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | 			*loc = (*loc & ~0x1f1ffd) | reassemble_17(val); | 
 | 653 | 			break; | 
 | 654 | 		case R_PARISC_PCREL22F: | 
 | 655 | 			/* 22-bit PC relative address; only defined for pa20 */ | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 656 | 			/* calculate direct call offset */ | 
 | 657 | 			val += addend; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | 			val = (val - dot - 8)/4; | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 659 | 			if (!RELOC_REACHABLE(val, 22)) { | 
 | 660 | 				/* direct distance too far, create | 
 | 661 | 				 * stub entry instead */ | 
 | 662 | 				val = get_stub(me, sym->st_value, addend, | 
 | 663 | 					ELF_STUB_DIRECT, loc0, targetsec); | 
 | 664 | 				val = (val - dot - 8)/4; | 
 | 665 | 				CHECK_RELOC(val, 22); | 
 | 666 | 			} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | 			*loc = (*loc & ~0x3ff1ffd) | reassemble_22(val); | 
 | 668 | 			break; | 
 | 669 |  | 
 | 670 | 		default: | 
 | 671 | 			printk(KERN_ERR "module %s: Unknown relocation: %u\n", | 
 | 672 | 			       me->name, ELF32_R_TYPE(rel[i].r_info)); | 
 | 673 | 			return -ENOEXEC; | 
 | 674 | 		} | 
 | 675 | 	} | 
 | 676 |  | 
 | 677 | 	return 0; | 
 | 678 | } | 
 | 679 |  | 
 | 680 | #else | 
 | 681 | int apply_relocate_add(Elf_Shdr *sechdrs, | 
 | 682 | 		       const char *strtab, | 
 | 683 | 		       unsigned int symindex, | 
 | 684 | 		       unsigned int relsec, | 
 | 685 | 		       struct module *me) | 
 | 686 | { | 
 | 687 | 	int i; | 
 | 688 | 	Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr; | 
 | 689 | 	Elf64_Sym *sym; | 
 | 690 | 	Elf64_Word *loc; | 
 | 691 | 	Elf64_Xword *loc64; | 
 | 692 | 	Elf64_Addr val; | 
 | 693 | 	Elf64_Sxword addend; | 
 | 694 | 	Elf64_Addr dot; | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 695 | 	Elf_Addr loc0; | 
 | 696 | 	unsigned int targetsec = sechdrs[relsec].sh_info; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 |  | 
 | 698 | 	DEBUGP("Applying relocate section %u to %u\n", relsec, | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 699 | 	       targetsec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | 	for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { | 
 | 701 | 		/* This is where to make the change */ | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 702 | 		loc = (void *)sechdrs[targetsec].sh_addr | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | 		      + rel[i].r_offset; | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 704 | 		/* This is the start of the target section */ | 
 | 705 | 		loc0 = sechdrs[targetsec].sh_addr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | 		/* This is the symbol it is referring to */ | 
 | 707 | 		sym = (Elf64_Sym *)sechdrs[symindex].sh_addr | 
 | 708 | 			+ ELF64_R_SYM(rel[i].r_info); | 
 | 709 | 		if (!sym->st_value) { | 
 | 710 | 			printk(KERN_WARNING "%s: Unknown symbol %s\n", | 
 | 711 | 			       me->name, strtab + sym->st_name); | 
 | 712 | 			return -ENOENT; | 
 | 713 | 		} | 
 | 714 | 		//dot = (sechdrs[relsec].sh_addr + rel->r_offset) & ~0x03; | 
 | 715 | 		dot = (Elf64_Addr)loc & ~0x03; | 
 | 716 | 		loc64 = (Elf64_Xword *)loc; | 
 | 717 |  | 
 | 718 | 		val = sym->st_value; | 
 | 719 | 		addend = rel[i].r_addend; | 
 | 720 |  | 
 | 721 | #if 0 | 
 | 722 | #define r(t) ELF64_R_TYPE(rel[i].r_info)==t ? #t : | 
 | 723 | 		printk("Symbol %s loc %p val 0x%Lx addend 0x%Lx: %s\n", | 
 | 724 | 			strtab + sym->st_name, | 
 | 725 | 			loc, val, addend, | 
 | 726 | 			r(R_PARISC_LTOFF14R) | 
 | 727 | 			r(R_PARISC_LTOFF21L) | 
 | 728 | 			r(R_PARISC_PCREL22F) | 
 | 729 | 			r(R_PARISC_DIR64) | 
 | 730 | 			r(R_PARISC_SEGREL32) | 
 | 731 | 			r(R_PARISC_FPTR64) | 
 | 732 | 			"UNKNOWN"); | 
 | 733 | #undef r | 
 | 734 | #endif | 
 | 735 |  | 
 | 736 | 		switch (ELF64_R_TYPE(rel[i].r_info)) { | 
 | 737 | 		case R_PARISC_LTOFF21L: | 
 | 738 | 			/* LT-relative; left 21 bits */ | 
 | 739 | 			val = get_got(me, val, addend); | 
 | 740 | 			DEBUGP("LTOFF21L Symbol %s loc %p val %lx\n", | 
 | 741 | 			       strtab + sym->st_name, | 
 | 742 | 			       loc, val); | 
 | 743 | 			val = lrsel(val, 0); | 
 | 744 | 			*loc = mask(*loc, 21) | reassemble_21(val); | 
 | 745 | 			break; | 
 | 746 | 		case R_PARISC_LTOFF14R: | 
 | 747 | 			/* L(ltoff(val+addend)) */ | 
 | 748 | 			/* LT-relative; right 14 bits */ | 
 | 749 | 			val = get_got(me, val, addend); | 
 | 750 | 			val = rrsel(val, 0); | 
 | 751 | 			DEBUGP("LTOFF14R Symbol %s loc %p val %lx\n", | 
 | 752 | 			       strtab + sym->st_name, | 
 | 753 | 			       loc, val); | 
 | 754 | 			*loc = mask(*loc, 14) | reassemble_14(val); | 
 | 755 | 			break; | 
 | 756 | 		case R_PARISC_PCREL22F: | 
 | 757 | 			/* PC-relative; 22 bits */ | 
 | 758 | 			DEBUGP("PCREL22F Symbol %s loc %p val %lx\n", | 
 | 759 | 			       strtab + sym->st_name, | 
 | 760 | 			       loc, val); | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 761 | 			val += addend; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | 			/* can we reach it locally? */ | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 763 | 			if (in_local(me, (void *)val)) { | 
 | 764 | 				/* this is the case where the symbol is local | 
 | 765 | 				 * to the module, but in a different section, | 
 | 766 | 				 * so stub the jump in case it's more than 22 | 
 | 767 | 				 * bits away */ | 
 | 768 | 				val = (val - dot - 8)/4; | 
 | 769 | 				if (!RELOC_REACHABLE(val, 22)) { | 
 | 770 | 					/* direct distance too far, create | 
 | 771 | 					 * stub entry instead */ | 
 | 772 | 					val = get_stub(me, sym->st_value, | 
 | 773 | 						addend, ELF_STUB_DIRECT, | 
 | 774 | 						loc0, targetsec); | 
 | 775 | 				} else { | 
 | 776 | 					/* Ok, we can reach it directly. */ | 
 | 777 | 					val = sym->st_value; | 
 | 778 | 					val += addend; | 
 | 779 | 				} | 
 | 780 | 			} else { | 
 | 781 | 				val = sym->st_value; | 
 | 782 | 				if (strncmp(strtab + sym->st_name, "$$", 2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | 				    == 0) | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 784 | 					val = get_stub(me, val, addend, ELF_STUB_MILLI, | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 785 | 						       loc0, targetsec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | 				else | 
| James Bottomley | 6e1b958 | 2006-06-23 14:15:20 -0600 | [diff] [blame] | 787 | 					val = get_stub(me, val, addend, ELF_STUB_GOT, | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 788 | 						       loc0, targetsec); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | 			} | 
 | 790 | 			DEBUGP("STUB FOR %s loc %lx, val %lx+%lx at %lx\n",  | 
 | 791 | 			       strtab + sym->st_name, loc, sym->st_value, | 
 | 792 | 			       addend, val); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | 			val = (val - dot - 8)/4; | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 794 | 			CHECK_RELOC(val, 22); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | 			*loc = (*loc & ~0x3ff1ffd) | reassemble_22(val); | 
 | 796 | 			break; | 
 | 797 | 		case R_PARISC_DIR64: | 
 | 798 | 			/* 64-bit effective address */ | 
 | 799 | 			*loc64 = val + addend; | 
 | 800 | 			break; | 
 | 801 | 		case R_PARISC_SEGREL32: | 
 | 802 | 			/* 32-bit segment relative address */ | 
 | 803 | 			/* See note about special handling of SEGREL32 at | 
 | 804 | 			 * the beginning of this file. | 
 | 805 | 			 */ | 
 | 806 | 			*loc = fsel(val, addend);  | 
 | 807 | 			break; | 
 | 808 | 		case R_PARISC_FPTR64: | 
 | 809 | 			/* 64-bit function address */ | 
| Eric Biederman | 959ed34 | 2006-09-29 02:00:06 -0700 | [diff] [blame] | 810 | 			if(in_local(me, (void *)(val + addend))) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | 				*loc64 = get_fdesc(me, val+addend); | 
 | 812 | 				DEBUGP("FDESC for %s at %p points to %lx\n", | 
 | 813 | 				       strtab + sym->st_name, *loc64, | 
 | 814 | 				       ((Elf_Fdesc *)*loc64)->addr); | 
 | 815 | 			} else { | 
 | 816 | 				/* if the symbol is not local to this | 
 | 817 | 				 * module then val+addend is a pointer | 
 | 818 | 				 * to the function descriptor */ | 
 | 819 | 				DEBUGP("Non local FPTR64 Symbol %s loc %p val %lx\n", | 
 | 820 | 				       strtab + sym->st_name, | 
 | 821 | 				       loc, val); | 
 | 822 | 				*loc64 = val + addend; | 
 | 823 | 			} | 
 | 824 | 			break; | 
 | 825 |  | 
 | 826 | 		default: | 
 | 827 | 			printk(KERN_ERR "module %s: Unknown relocation: %Lu\n", | 
 | 828 | 			       me->name, ELF64_R_TYPE(rel[i].r_info)); | 
 | 829 | 			return -ENOEXEC; | 
 | 830 | 		} | 
 | 831 | 	} | 
 | 832 | 	return 0; | 
 | 833 | } | 
 | 834 | #endif | 
 | 835 |  | 
 | 836 | static void | 
 | 837 | register_unwind_table(struct module *me, | 
 | 838 | 		      const Elf_Shdr *sechdrs) | 
 | 839 | { | 
 | 840 | 	unsigned char *table, *end; | 
 | 841 | 	unsigned long gp; | 
 | 842 |  | 
 | 843 | 	if (!me->arch.unwind_section) | 
 | 844 | 		return; | 
 | 845 |  | 
 | 846 | 	table = (unsigned char *)sechdrs[me->arch.unwind_section].sh_addr; | 
 | 847 | 	end = table + sechdrs[me->arch.unwind_section].sh_size; | 
 | 848 | 	gp = (Elf_Addr)me->module_core + me->arch.got_offset; | 
 | 849 |  | 
 | 850 | 	DEBUGP("register_unwind_table(), sect = %d at 0x%p - 0x%p (gp=0x%lx)\n", | 
 | 851 | 	       me->arch.unwind_section, table, end, gp); | 
 | 852 | 	me->arch.unwind = unwind_table_add(me->name, 0, gp, table, end); | 
 | 853 | } | 
 | 854 |  | 
 | 855 | static void | 
 | 856 | deregister_unwind_table(struct module *me) | 
 | 857 | { | 
 | 858 | 	if (me->arch.unwind) | 
 | 859 | 		unwind_table_remove(me->arch.unwind); | 
 | 860 | } | 
 | 861 |  | 
 | 862 | int module_finalize(const Elf_Ehdr *hdr, | 
 | 863 | 		    const Elf_Shdr *sechdrs, | 
 | 864 | 		    struct module *me) | 
 | 865 | { | 
 | 866 | 	int i; | 
 | 867 | 	unsigned long nsyms; | 
 | 868 | 	const char *strtab = NULL; | 
 | 869 | 	Elf_Sym *newptr, *oldptr; | 
 | 870 | 	Elf_Shdr *symhdr = NULL; | 
 | 871 | #ifdef DEBUG | 
 | 872 | 	Elf_Fdesc *entry; | 
 | 873 | 	u32 *addr; | 
 | 874 |  | 
 | 875 | 	entry = (Elf_Fdesc *)me->init; | 
 | 876 | 	printk("FINALIZE, ->init FPTR is %p, GP %lx ADDR %lx\n", entry, | 
 | 877 | 	       entry->gp, entry->addr); | 
 | 878 | 	addr = (u32 *)entry->addr; | 
 | 879 | 	printk("INSNS: %x %x %x %x\n", | 
 | 880 | 	       addr[0], addr[1], addr[2], addr[3]); | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 881 | 	printk("got entries used %ld, gots max %ld\n" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | 	       "fdescs used %ld, fdescs max %ld\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | 	       me->arch.got_count, me->arch.got_max, | 
 | 884 | 	       me->arch.fdesc_count, me->arch.fdesc_max); | 
 | 885 | #endif | 
 | 886 |  | 
 | 887 | 	register_unwind_table(me, sechdrs); | 
 | 888 |  | 
 | 889 | 	/* haven't filled in me->symtab yet, so have to find it | 
 | 890 | 	 * ourselves */ | 
 | 891 | 	for (i = 1; i < hdr->e_shnum; i++) { | 
 | 892 | 		if(sechdrs[i].sh_type == SHT_SYMTAB | 
| Julia Lawall | fe579c69 | 2009-08-04 22:27:07 +0200 | [diff] [blame] | 893 | 		   && (sechdrs[i].sh_flags & SHF_ALLOC)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | 			int strindex = sechdrs[i].sh_link; | 
 | 895 | 			/* FIXME: AWFUL HACK | 
 | 896 | 			 * The cast is to drop the const from | 
 | 897 | 			 * the sechdrs pointer */ | 
 | 898 | 			symhdr = (Elf_Shdr *)&sechdrs[i]; | 
 | 899 | 			strtab = (char *)sechdrs[strindex].sh_addr; | 
 | 900 | 			break; | 
 | 901 | 		} | 
 | 902 | 	} | 
 | 903 |  | 
 | 904 | 	DEBUGP("module %s: strtab %p, symhdr %p\n", | 
 | 905 | 	       me->name, strtab, symhdr); | 
 | 906 |  | 
 | 907 | 	if(me->arch.got_count > MAX_GOTS) { | 
| Helge Deller | f8fc18a | 2006-10-18 21:44:30 +0200 | [diff] [blame] | 908 | 		printk(KERN_ERR "%s: Global Offset Table overflow (used %ld, allowed %d)\n", | 
 | 909 | 				me->name, me->arch.got_count, MAX_GOTS); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | 		return -EINVAL; | 
 | 911 | 	} | 
| Helge Deller | c298be7 | 2009-01-01 22:25:30 +0100 | [diff] [blame] | 912 |  | 
 | 913 | 	kfree(me->arch.section); | 
 | 914 | 	me->arch.section = NULL; | 
 | 915 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | 	/* no symbol table */ | 
 | 917 | 	if(symhdr == NULL) | 
 | 918 | 		return 0; | 
 | 919 |  | 
 | 920 | 	oldptr = (void *)symhdr->sh_addr; | 
 | 921 | 	newptr = oldptr + 1;	/* we start counting at 1 */ | 
 | 922 | 	nsyms = symhdr->sh_size / sizeof(Elf_Sym); | 
 | 923 | 	DEBUGP("OLD num_symtab %lu\n", nsyms); | 
 | 924 |  | 
 | 925 | 	for (i = 1; i < nsyms; i++) { | 
 | 926 | 		oldptr++;	/* note, count starts at 1 so preincrement */ | 
 | 927 | 		if(strncmp(strtab + oldptr->st_name, | 
 | 928 | 			      ".L", 2) == 0) | 
 | 929 | 			continue; | 
 | 930 |  | 
 | 931 | 		if(newptr != oldptr) | 
 | 932 | 			*newptr++ = *oldptr; | 
 | 933 | 		else | 
 | 934 | 			newptr++; | 
 | 935 |  | 
 | 936 | 	} | 
 | 937 | 	nsyms = newptr - (Elf_Sym *)symhdr->sh_addr; | 
 | 938 | 	DEBUGP("NEW num_symtab %lu\n", nsyms); | 
 | 939 | 	symhdr->sh_size = nsyms * sizeof(Elf_Sym); | 
| Linus Torvalds | 5336377 | 2010-10-05 11:29:27 -0700 | [diff] [blame] | 940 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } | 
 | 942 |  | 
 | 943 | void module_arch_cleanup(struct module *mod) | 
 | 944 | { | 
 | 945 | 	deregister_unwind_table(mod); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |