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