Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * This file reads all the special sections which have alternate instructions |
| 8 | * which can be patched in or redirected to at runtime. |
| 9 | */ |
| 10 | |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | |
Vasily Gorbik | 7786032 | 2020-11-13 00:03:32 +0100 | [diff] [blame] | 14 | #include <arch/special.h> |
| 15 | #include <objtool/builtin.h> |
| 16 | #include <objtool/special.h> |
| 17 | #include <objtool/warn.h> |
| 18 | #include <objtool/endianness.h> |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 19 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 20 | struct special_entry { |
| 21 | const char *sec; |
| 22 | bool group, jump_or_nop; |
| 23 | unsigned char size, orig, new; |
| 24 | unsigned char orig_len, new_len; /* group only */ |
| 25 | unsigned char feature; /* ALTERNATIVE macro CPU feature */ |
Peter Zijlstra | cbf82a3 | 2021-05-06 21:34:02 +0200 | [diff] [blame] | 26 | unsigned char key; /* jump_label key */ |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | struct special_entry entries[] = { |
| 30 | { |
| 31 | .sec = ".altinstructions", |
| 32 | .group = true, |
| 33 | .size = ALT_ENTRY_SIZE, |
| 34 | .orig = ALT_ORIG_OFFSET, |
| 35 | .orig_len = ALT_ORIG_LEN_OFFSET, |
| 36 | .new = ALT_NEW_OFFSET, |
| 37 | .new_len = ALT_NEW_LEN_OFFSET, |
| 38 | .feature = ALT_FEATURE_OFFSET, |
| 39 | }, |
| 40 | { |
| 41 | .sec = "__jump_table", |
| 42 | .jump_or_nop = true, |
| 43 | .size = JUMP_ENTRY_SIZE, |
| 44 | .orig = JUMP_ORIG_OFFSET, |
| 45 | .new = JUMP_NEW_OFFSET, |
Peter Zijlstra | cbf82a3 | 2021-05-06 21:34:02 +0200 | [diff] [blame] | 46 | .key = JUMP_KEY_OFFSET, |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 47 | }, |
| 48 | { |
| 49 | .sec = "__ex_table", |
| 50 | .size = EX_ENTRY_SIZE, |
| 51 | .orig = EX_ORIG_OFFSET, |
| 52 | .new = EX_NEW_OFFSET, |
| 53 | }, |
| 54 | {}, |
| 55 | }; |
| 56 | |
Julien Thierry | eda3dc9 | 2020-09-04 16:30:22 +0100 | [diff] [blame] | 57 | void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt) |
| 58 | { |
| 59 | } |
| 60 | |
Josh Poimboeuf | 4d8b359 | 2021-10-04 10:07:50 -0700 | [diff] [blame] | 61 | static void reloc_to_sec_off(struct reloc *reloc, struct section **sec, |
| 62 | unsigned long *off) |
Peter Zijlstra | 24ff652 | 2021-09-30 12:43:10 +0200 | [diff] [blame] | 63 | { |
Josh Poimboeuf | 4d8b359 | 2021-10-04 10:07:50 -0700 | [diff] [blame] | 64 | *sec = reloc->sym->sec; |
| 65 | *off = reloc->sym->offset + reloc->addend; |
Peter Zijlstra | 24ff652 | 2021-09-30 12:43:10 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 68 | static int get_alt_entry(struct elf *elf, struct special_entry *entry, |
| 69 | struct section *sec, int idx, |
| 70 | struct special_alt *alt) |
| 71 | { |
Matt Helsley | f197422 | 2020-05-29 14:01:13 -0700 | [diff] [blame] | 72 | struct reloc *orig_reloc, *new_reloc; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 73 | unsigned long offset; |
| 74 | |
| 75 | offset = idx * entry->size; |
| 76 | |
| 77 | alt->group = entry->group; |
| 78 | alt->jump_or_nop = entry->jump_or_nop; |
| 79 | |
| 80 | if (alt->group) { |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 81 | alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset + |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 82 | entry->orig_len); |
Josh Poimboeuf | baa4146 | 2017-06-28 10:11:07 -0500 | [diff] [blame] | 83 | alt->new_len = *(unsigned char *)(sec->data->d_buf + offset + |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 84 | entry->new_len); |
| 85 | } |
| 86 | |
| 87 | if (entry->feature) { |
| 88 | unsigned short feature; |
| 89 | |
Vasily Gorbik | 8bfe273 | 2020-11-13 00:03:29 +0100 | [diff] [blame] | 90 | feature = bswap_if_needed(*(unsigned short *)(sec->data->d_buf + |
| 91 | offset + |
| 92 | entry->feature)); |
Julien Thierry | eda3dc9 | 2020-09-04 16:30:22 +0100 | [diff] [blame] | 93 | arch_handle_alternative(feature, alt); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 94 | } |
| 95 | |
Matt Helsley | f197422 | 2020-05-29 14:01:13 -0700 | [diff] [blame] | 96 | orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig); |
| 97 | if (!orig_reloc) { |
| 98 | WARN_FUNC("can't find orig reloc", sec, offset + entry->orig); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 99 | return -1; |
| 100 | } |
Josh Poimboeuf | 4d8b359 | 2021-10-04 10:07:50 -0700 | [diff] [blame] | 101 | |
| 102 | reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 103 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 104 | if (!entry->group || alt->new_len) { |
Matt Helsley | f197422 | 2020-05-29 14:01:13 -0700 | [diff] [blame] | 105 | new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new); |
| 106 | if (!new_reloc) { |
| 107 | WARN_FUNC("can't find new reloc", |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 108 | sec, offset + entry->new); |
| 109 | return -1; |
| 110 | } |
| 111 | |
Josh Poimboeuf | 4d8b359 | 2021-10-04 10:07:50 -0700 | [diff] [blame] | 112 | reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 113 | |
| 114 | /* _ASM_EXTABLE_EX hack */ |
| 115 | if (alt->new_off >= 0x7ffffff0) |
| 116 | alt->new_off -= 0x7ffffff0; |
| 117 | } |
| 118 | |
Peter Zijlstra | cbf82a3 | 2021-05-06 21:34:02 +0200 | [diff] [blame] | 119 | if (entry->key) { |
| 120 | struct reloc *key_reloc; |
| 121 | |
| 122 | key_reloc = find_reloc_by_dest(elf, sec, offset + entry->key); |
| 123 | if (!key_reloc) { |
| 124 | WARN_FUNC("can't find key reloc", |
| 125 | sec, offset + entry->key); |
| 126 | return -1; |
| 127 | } |
| 128 | alt->key_addend = key_reloc->addend; |
| 129 | } |
| 130 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * Read all the special sections and create a list of special_alt structs which |
| 136 | * describe all the alternate instructions which can be patched in or |
| 137 | * redirected to at runtime. |
| 138 | */ |
| 139 | int special_get_alts(struct elf *elf, struct list_head *alts) |
| 140 | { |
| 141 | struct special_entry *entry; |
| 142 | struct section *sec; |
| 143 | unsigned int nr_entries; |
| 144 | struct special_alt *alt; |
| 145 | int idx, ret; |
| 146 | |
| 147 | INIT_LIST_HEAD(alts); |
| 148 | |
| 149 | for (entry = entries; entry->sec; entry++) { |
| 150 | sec = find_section_by_name(elf, entry->sec); |
| 151 | if (!sec) |
| 152 | continue; |
| 153 | |
Joe Lawrence | fe255fe | 2021-08-22 18:50:37 -0400 | [diff] [blame] | 154 | if (sec->sh.sh_size % entry->size != 0) { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 155 | WARN("%s size not a multiple of %d", |
| 156 | sec->name, entry->size); |
| 157 | return -1; |
| 158 | } |
| 159 | |
Joe Lawrence | fe255fe | 2021-08-22 18:50:37 -0400 | [diff] [blame] | 160 | nr_entries = sec->sh.sh_size / entry->size; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 161 | |
| 162 | for (idx = 0; idx < nr_entries; idx++) { |
| 163 | alt = malloc(sizeof(*alt)); |
| 164 | if (!alt) { |
| 165 | WARN("malloc failed"); |
| 166 | return -1; |
| 167 | } |
| 168 | memset(alt, 0, sizeof(*alt)); |
| 169 | |
| 170 | ret = get_alt_entry(elf, entry, sec, idx, alt); |
Peter Zijlstra | 50e7b4a | 2021-03-26 16:12:14 +0100 | [diff] [blame] | 171 | if (ret > 0) |
| 172 | continue; |
| 173 | if (ret < 0) |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 174 | return ret; |
| 175 | |
| 176 | list_add_tail(&alt->list, alts); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |