Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * AArch64 loadable module support. |
| 3 | * |
| 4 | * Copyright (C) 2012 ARM Limited |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * Author: Will Deacon <will.deacon@arm.com> |
| 19 | */ |
| 20 | |
| 21 | #include <linux/bitops.h> |
| 22 | #include <linux/elf.h> |
| 23 | #include <linux/gfp.h> |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 24 | #include <linux/kasan.h> |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/mm.h> |
| 27 | #include <linux/moduleloader.h> |
| 28 | #include <linux/vmalloc.h> |
Paul Walmsley | 2c2b282 | 2015-01-05 17:38:41 -0700 | [diff] [blame] | 29 | #include <asm/alternative.h> |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 30 | #include <asm/insn.h> |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 31 | #include <asm/sections.h> |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 32 | |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 33 | void *module_alloc(unsigned long size) |
| 34 | { |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 35 | void *p; |
| 36 | |
Ard Biesheuvel | f80fb3a | 2016-01-26 14:12:01 +0100 | [diff] [blame^] | 37 | p = __vmalloc_node_range(size, MODULE_ALIGN, module_alloc_base, |
| 38 | module_alloc_base + MODULES_VSIZE, |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 39 | GFP_KERNEL, PAGE_KERNEL_EXEC, 0, |
| 40 | NUMA_NO_NODE, __builtin_return_address(0)); |
| 41 | |
Ard Biesheuvel | fd045f6 | 2015-11-24 12:37:35 +0100 | [diff] [blame] | 42 | if (!p && IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) && |
| 43 | !IS_ENABLED(CONFIG_KASAN)) |
| 44 | /* |
| 45 | * KASAN can only deal with module allocations being served |
| 46 | * from the reserved module region, since the remainder of |
| 47 | * the vmalloc region is already backed by zero shadow pages, |
| 48 | * and punching holes into it is non-trivial. Since the module |
| 49 | * region is not randomized when KASAN is enabled, it is even |
| 50 | * less likely that the module region gets exhausted, so we |
| 51 | * can simply omit this fallback in that case. |
| 52 | */ |
| 53 | p = __vmalloc_node_range(size, MODULE_ALIGN, VMALLOC_START, |
| 54 | VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_EXEC, 0, |
| 55 | NUMA_NO_NODE, __builtin_return_address(0)); |
| 56 | |
Andrey Ryabinin | 39d114d | 2015-10-12 18:52:58 +0300 | [diff] [blame] | 57 | if (p && (kasan_module_alloc(p, size) < 0)) { |
| 58 | vfree(p); |
| 59 | return NULL; |
| 60 | } |
| 61 | |
| 62 | return p; |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | enum aarch64_reloc_op { |
| 66 | RELOC_OP_NONE, |
| 67 | RELOC_OP_ABS, |
| 68 | RELOC_OP_PREL, |
| 69 | RELOC_OP_PAGE, |
| 70 | }; |
| 71 | |
| 72 | static u64 do_reloc(enum aarch64_reloc_op reloc_op, void *place, u64 val) |
| 73 | { |
| 74 | switch (reloc_op) { |
| 75 | case RELOC_OP_ABS: |
| 76 | return val; |
| 77 | case RELOC_OP_PREL: |
| 78 | return val - (u64)place; |
| 79 | case RELOC_OP_PAGE: |
| 80 | return (val & ~0xfff) - ((u64)place & ~0xfff); |
| 81 | case RELOC_OP_NONE: |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | pr_err("do_reloc: unknown relocation operation %d\n", reloc_op); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int reloc_data(enum aarch64_reloc_op op, void *place, u64 val, int len) |
| 90 | { |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 91 | s64 sval = do_reloc(op, place, val); |
| 92 | |
| 93 | switch (len) { |
| 94 | case 16: |
| 95 | *(s16 *)place = sval; |
Ard Biesheuvel | f930896 | 2016-01-05 10:18:52 +0100 | [diff] [blame] | 96 | if (sval < S16_MIN || sval > U16_MAX) |
| 97 | return -ERANGE; |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 98 | break; |
| 99 | case 32: |
| 100 | *(s32 *)place = sval; |
Ard Biesheuvel | f930896 | 2016-01-05 10:18:52 +0100 | [diff] [blame] | 101 | if (sval < S32_MIN || sval > U32_MAX) |
| 102 | return -ERANGE; |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 103 | break; |
| 104 | case 64: |
| 105 | *(s64 *)place = sval; |
| 106 | break; |
| 107 | default: |
| 108 | pr_err("Invalid length (%d) for data relocation\n", len); |
| 109 | return 0; |
| 110 | } |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 114 | enum aarch64_insn_movw_imm_type { |
| 115 | AARCH64_INSN_IMM_MOVNZ, |
| 116 | AARCH64_INSN_IMM_MOVKZ, |
| 117 | }; |
| 118 | |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 119 | static int reloc_insn_movw(enum aarch64_reloc_op op, void *place, u64 val, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 120 | int lsb, enum aarch64_insn_movw_imm_type imm_type) |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 121 | { |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 122 | u64 imm; |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 123 | s64 sval; |
| 124 | u32 insn = le32_to_cpu(*(u32 *)place); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 125 | |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 126 | sval = do_reloc(op, place, val); |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 127 | imm = sval >> lsb; |
Will Deacon | 122e2fa | 2013-11-05 10:16:52 +0000 | [diff] [blame] | 128 | |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 129 | if (imm_type == AARCH64_INSN_IMM_MOVNZ) { |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 130 | /* |
| 131 | * For signed MOVW relocations, we have to manipulate the |
| 132 | * instruction encoding depending on whether or not the |
| 133 | * immediate is less than zero. |
| 134 | */ |
| 135 | insn &= ~(3 << 29); |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 136 | if (sval >= 0) { |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 137 | /* >=0: Set the instruction to MOVZ (opcode 10b). */ |
| 138 | insn |= 2 << 29; |
| 139 | } else { |
| 140 | /* |
| 141 | * <0: Set the instruction to MOVN (opcode 00b). |
| 142 | * Since we've masked the opcode already, we |
| 143 | * don't need to do anything other than |
| 144 | * inverting the new immediate field. |
| 145 | */ |
| 146 | imm = ~imm; |
| 147 | } |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 150 | /* Update the instruction with the new encoding. */ |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 151 | insn = aarch64_insn_encode_immediate(AARCH64_INSN_IMM_16, insn, imm); |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 152 | *(u32 *)place = cpu_to_le32(insn); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 153 | |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 154 | if (imm > U16_MAX) |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 155 | return -ERANGE; |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int reloc_insn_imm(enum aarch64_reloc_op op, void *place, u64 val, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 161 | int lsb, int len, enum aarch64_insn_imm_type imm_type) |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 162 | { |
| 163 | u64 imm, imm_mask; |
| 164 | s64 sval; |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 165 | u32 insn = le32_to_cpu(*(u32 *)place); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 166 | |
| 167 | /* Calculate the relocation value. */ |
| 168 | sval = do_reloc(op, place, val); |
| 169 | sval >>= lsb; |
| 170 | |
| 171 | /* Extract the value bits and shift them to bit 0. */ |
| 172 | imm_mask = (BIT(lsb + len) - 1) >> lsb; |
| 173 | imm = sval & imm_mask; |
| 174 | |
| 175 | /* Update the instruction's immediate field. */ |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 176 | insn = aarch64_insn_encode_immediate(imm_type, insn, imm); |
| 177 | *(u32 *)place = cpu_to_le32(insn); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 178 | |
| 179 | /* |
| 180 | * Extract the upper value bits (including the sign bit) and |
| 181 | * shift them to bit 0. |
| 182 | */ |
| 183 | sval = (s64)(sval & ~(imm_mask >> 1)) >> (len - 1); |
| 184 | |
| 185 | /* |
| 186 | * Overflow has occurred if the upper bits are not all equal to |
| 187 | * the sign bit of the value. |
| 188 | */ |
| 189 | if ((u64)(sval + 1) >= 2) |
| 190 | return -ERANGE; |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int apply_relocate_add(Elf64_Shdr *sechdrs, |
| 196 | const char *strtab, |
| 197 | unsigned int symindex, |
| 198 | unsigned int relsec, |
| 199 | struct module *me) |
| 200 | { |
| 201 | unsigned int i; |
| 202 | int ovf; |
| 203 | bool overflow_check; |
| 204 | Elf64_Sym *sym; |
| 205 | void *loc; |
| 206 | u64 val; |
| 207 | Elf64_Rela *rel = (void *)sechdrs[relsec].sh_addr; |
| 208 | |
| 209 | for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) { |
| 210 | /* loc corresponds to P in the AArch64 ELF document. */ |
| 211 | loc = (void *)sechdrs[sechdrs[relsec].sh_info].sh_addr |
| 212 | + rel[i].r_offset; |
| 213 | |
| 214 | /* sym is the ELF symbol we're referring to. */ |
| 215 | sym = (Elf64_Sym *)sechdrs[symindex].sh_addr |
| 216 | + ELF64_R_SYM(rel[i].r_info); |
| 217 | |
| 218 | /* val corresponds to (S + A) in the AArch64 ELF document. */ |
| 219 | val = sym->st_value + rel[i].r_addend; |
| 220 | |
| 221 | /* Check for overflow by default. */ |
| 222 | overflow_check = true; |
| 223 | |
| 224 | /* Perform the static relocation. */ |
| 225 | switch (ELF64_R_TYPE(rel[i].r_info)) { |
| 226 | /* Null relocations. */ |
| 227 | case R_ARM_NONE: |
| 228 | case R_AARCH64_NONE: |
| 229 | ovf = 0; |
| 230 | break; |
| 231 | |
| 232 | /* Data relocations. */ |
| 233 | case R_AARCH64_ABS64: |
| 234 | overflow_check = false; |
| 235 | ovf = reloc_data(RELOC_OP_ABS, loc, val, 64); |
| 236 | break; |
| 237 | case R_AARCH64_ABS32: |
| 238 | ovf = reloc_data(RELOC_OP_ABS, loc, val, 32); |
| 239 | break; |
| 240 | case R_AARCH64_ABS16: |
| 241 | ovf = reloc_data(RELOC_OP_ABS, loc, val, 16); |
| 242 | break; |
| 243 | case R_AARCH64_PREL64: |
| 244 | overflow_check = false; |
| 245 | ovf = reloc_data(RELOC_OP_PREL, loc, val, 64); |
| 246 | break; |
| 247 | case R_AARCH64_PREL32: |
| 248 | ovf = reloc_data(RELOC_OP_PREL, loc, val, 32); |
| 249 | break; |
| 250 | case R_AARCH64_PREL16: |
| 251 | ovf = reloc_data(RELOC_OP_PREL, loc, val, 16); |
| 252 | break; |
| 253 | |
| 254 | /* MOVW instruction relocations. */ |
| 255 | case R_AARCH64_MOVW_UABS_G0_NC: |
| 256 | overflow_check = false; |
| 257 | case R_AARCH64_MOVW_UABS_G0: |
| 258 | ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 259 | AARCH64_INSN_IMM_MOVKZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 260 | break; |
| 261 | case R_AARCH64_MOVW_UABS_G1_NC: |
| 262 | overflow_check = false; |
| 263 | case R_AARCH64_MOVW_UABS_G1: |
| 264 | ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 265 | AARCH64_INSN_IMM_MOVKZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 266 | break; |
| 267 | case R_AARCH64_MOVW_UABS_G2_NC: |
| 268 | overflow_check = false; |
| 269 | case R_AARCH64_MOVW_UABS_G2: |
| 270 | ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 271 | AARCH64_INSN_IMM_MOVKZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 272 | break; |
| 273 | case R_AARCH64_MOVW_UABS_G3: |
| 274 | /* We're using the top bits so we can't overflow. */ |
| 275 | overflow_check = false; |
| 276 | ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 48, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 277 | AARCH64_INSN_IMM_MOVKZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 278 | break; |
| 279 | case R_AARCH64_MOVW_SABS_G0: |
| 280 | ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 0, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 281 | AARCH64_INSN_IMM_MOVNZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 282 | break; |
| 283 | case R_AARCH64_MOVW_SABS_G1: |
| 284 | ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 16, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 285 | AARCH64_INSN_IMM_MOVNZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 286 | break; |
| 287 | case R_AARCH64_MOVW_SABS_G2: |
| 288 | ovf = reloc_insn_movw(RELOC_OP_ABS, loc, val, 32, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 289 | AARCH64_INSN_IMM_MOVNZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 290 | break; |
| 291 | case R_AARCH64_MOVW_PREL_G0_NC: |
| 292 | overflow_check = false; |
| 293 | ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 294 | AARCH64_INSN_IMM_MOVKZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 295 | break; |
| 296 | case R_AARCH64_MOVW_PREL_G0: |
| 297 | ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 0, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 298 | AARCH64_INSN_IMM_MOVNZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 299 | break; |
| 300 | case R_AARCH64_MOVW_PREL_G1_NC: |
| 301 | overflow_check = false; |
| 302 | ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 303 | AARCH64_INSN_IMM_MOVKZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 304 | break; |
| 305 | case R_AARCH64_MOVW_PREL_G1: |
| 306 | ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 16, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 307 | AARCH64_INSN_IMM_MOVNZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 308 | break; |
| 309 | case R_AARCH64_MOVW_PREL_G2_NC: |
| 310 | overflow_check = false; |
| 311 | ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32, |
Ard Biesheuvel | b24a557 | 2016-01-05 10:18:51 +0100 | [diff] [blame] | 312 | AARCH64_INSN_IMM_MOVKZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 313 | break; |
| 314 | case R_AARCH64_MOVW_PREL_G2: |
| 315 | ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 32, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 316 | AARCH64_INSN_IMM_MOVNZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 317 | break; |
| 318 | case R_AARCH64_MOVW_PREL_G3: |
| 319 | /* We're using the top bits so we can't overflow. */ |
| 320 | overflow_check = false; |
| 321 | ovf = reloc_insn_movw(RELOC_OP_PREL, loc, val, 48, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 322 | AARCH64_INSN_IMM_MOVNZ); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 323 | break; |
| 324 | |
| 325 | /* Immediate instruction relocations. */ |
| 326 | case R_AARCH64_LD_PREL_LO19: |
| 327 | ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 328 | AARCH64_INSN_IMM_19); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 329 | break; |
| 330 | case R_AARCH64_ADR_PREL_LO21: |
| 331 | ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 0, 21, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 332 | AARCH64_INSN_IMM_ADR); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 333 | break; |
Will Deacon | df057cc | 2015-03-17 12:15:02 +0000 | [diff] [blame] | 334 | #ifndef CONFIG_ARM64_ERRATUM_843419 |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 335 | case R_AARCH64_ADR_PREL_PG_HI21_NC: |
| 336 | overflow_check = false; |
| 337 | case R_AARCH64_ADR_PREL_PG_HI21: |
| 338 | ovf = reloc_insn_imm(RELOC_OP_PAGE, loc, val, 12, 21, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 339 | AARCH64_INSN_IMM_ADR); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 340 | break; |
Will Deacon | df057cc | 2015-03-17 12:15:02 +0000 | [diff] [blame] | 341 | #endif |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 342 | case R_AARCH64_ADD_ABS_LO12_NC: |
| 343 | case R_AARCH64_LDST8_ABS_LO12_NC: |
| 344 | overflow_check = false; |
| 345 | ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 0, 12, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 346 | AARCH64_INSN_IMM_12); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 347 | break; |
| 348 | case R_AARCH64_LDST16_ABS_LO12_NC: |
| 349 | overflow_check = false; |
| 350 | ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 1, 11, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 351 | AARCH64_INSN_IMM_12); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 352 | break; |
| 353 | case R_AARCH64_LDST32_ABS_LO12_NC: |
| 354 | overflow_check = false; |
| 355 | ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 2, 10, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 356 | AARCH64_INSN_IMM_12); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 357 | break; |
| 358 | case R_AARCH64_LDST64_ABS_LO12_NC: |
| 359 | overflow_check = false; |
| 360 | ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 3, 9, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 361 | AARCH64_INSN_IMM_12); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 362 | break; |
| 363 | case R_AARCH64_LDST128_ABS_LO12_NC: |
| 364 | overflow_check = false; |
| 365 | ovf = reloc_insn_imm(RELOC_OP_ABS, loc, val, 4, 8, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 366 | AARCH64_INSN_IMM_12); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 367 | break; |
| 368 | case R_AARCH64_TSTBR14: |
| 369 | ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 14, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 370 | AARCH64_INSN_IMM_14); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 371 | break; |
| 372 | case R_AARCH64_CONDBR19: |
| 373 | ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 19, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 374 | AARCH64_INSN_IMM_19); |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 375 | break; |
| 376 | case R_AARCH64_JUMP26: |
| 377 | case R_AARCH64_CALL26: |
| 378 | ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, 26, |
Jiang Liu | c84fced | 2014-01-07 22:17:10 +0800 | [diff] [blame] | 379 | AARCH64_INSN_IMM_26); |
Ard Biesheuvel | fd045f6 | 2015-11-24 12:37:35 +0100 | [diff] [blame] | 380 | |
| 381 | if (IS_ENABLED(CONFIG_ARM64_MODULE_PLTS) && |
| 382 | ovf == -ERANGE) { |
| 383 | val = module_emit_plt_entry(me, &rel[i], sym); |
| 384 | ovf = reloc_insn_imm(RELOC_OP_PREL, loc, val, 2, |
| 385 | 26, AARCH64_INSN_IMM_26); |
| 386 | } |
Will Deacon | 257cb25 | 2012-03-05 11:49:33 +0000 | [diff] [blame] | 387 | break; |
| 388 | |
| 389 | default: |
| 390 | pr_err("module %s: unsupported RELA relocation: %llu\n", |
| 391 | me->name, ELF64_R_TYPE(rel[i].r_info)); |
| 392 | return -ENOEXEC; |
| 393 | } |
| 394 | |
| 395 | if (overflow_check && ovf == -ERANGE) |
| 396 | goto overflow; |
| 397 | |
| 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | |
| 402 | overflow: |
| 403 | pr_err("module %s: overflow in relocation type %d val %Lx\n", |
| 404 | me->name, (int)ELF64_R_TYPE(rel[i].r_info), val); |
| 405 | return -ENOEXEC; |
| 406 | } |
Andre Przywara | 932ded4 | 2014-11-28 13:40:45 +0000 | [diff] [blame] | 407 | |
| 408 | int module_finalize(const Elf_Ehdr *hdr, |
| 409 | const Elf_Shdr *sechdrs, |
| 410 | struct module *me) |
| 411 | { |
| 412 | const Elf_Shdr *s, *se; |
| 413 | const char *secstrs = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset; |
| 414 | |
| 415 | for (s = sechdrs, se = sechdrs + hdr->e_shnum; s < se; s++) { |
| 416 | if (strcmp(".altinstructions", secstrs + s->sh_name) == 0) { |
| 417 | apply_alternatives((void *)s->sh_addr, s->sh_size); |
| 418 | return 0; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |