blob: aff0cee7bac1746bfc820a99b0a5f321486e9e7c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
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
David Brazdil0f672f62019-12-10 10:32:29 +000014#include "builtin.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000015#include "special.h"
16#include "warn.h"
Olivier Deprez157378f2022-04-04 15:47:50 +020017#include "arch_special.h"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018
19struct special_entry {
20 const char *sec;
21 bool group, jump_or_nop;
22 unsigned char size, orig, new;
23 unsigned char orig_len, new_len; /* group only */
24 unsigned char feature; /* ALTERNATIVE macro CPU feature */
25};
26
27struct special_entry entries[] = {
28 {
29 .sec = ".altinstructions",
30 .group = true,
31 .size = ALT_ENTRY_SIZE,
32 .orig = ALT_ORIG_OFFSET,
33 .orig_len = ALT_ORIG_LEN_OFFSET,
34 .new = ALT_NEW_OFFSET,
35 .new_len = ALT_NEW_LEN_OFFSET,
36 .feature = ALT_FEATURE_OFFSET,
37 },
38 {
39 .sec = "__jump_table",
40 .jump_or_nop = true,
41 .size = JUMP_ENTRY_SIZE,
42 .orig = JUMP_ORIG_OFFSET,
43 .new = JUMP_NEW_OFFSET,
44 },
45 {
46 .sec = "__ex_table",
47 .size = EX_ENTRY_SIZE,
48 .orig = EX_ORIG_OFFSET,
49 .new = EX_NEW_OFFSET,
50 },
51 {},
52};
53
Olivier Deprez157378f2022-04-04 15:47:50 +020054void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
55{
56}
57
Olivier Deprez92d4c212022-12-06 15:05:30 +010058static void reloc_to_sec_off(struct reloc *reloc, struct section **sec,
59 unsigned long *off)
60{
61 *sec = reloc->sym->sec;
62 *off = reloc->sym->offset + reloc->addend;
63}
64
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000065static int get_alt_entry(struct elf *elf, struct special_entry *entry,
66 struct section *sec, int idx,
67 struct special_alt *alt)
68{
Olivier Deprez157378f2022-04-04 15:47:50 +020069 struct reloc *orig_reloc, *new_reloc;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 unsigned long offset;
71
72 offset = idx * entry->size;
73
74 alt->group = entry->group;
75 alt->jump_or_nop = entry->jump_or_nop;
76
77 if (alt->group) {
78 alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
79 entry->orig_len);
80 alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
81 entry->new_len);
82 }
83
84 if (entry->feature) {
85 unsigned short feature;
86
87 feature = *(unsigned short *)(sec->data->d_buf + offset +
88 entry->feature);
Olivier Deprez157378f2022-04-04 15:47:50 +020089 arch_handle_alternative(feature, alt);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090 }
91
Olivier Deprez157378f2022-04-04 15:47:50 +020092 orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
93 if (!orig_reloc) {
94 WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000095 return -1;
96 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000097
Olivier Deprez92d4c212022-12-06 15:05:30 +010098 reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000099
100 if (!entry->group || alt->new_len) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200101 new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
102 if (!new_reloc) {
103 WARN_FUNC("can't find new reloc",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000104 sec, offset + entry->new);
105 return -1;
106 }
107
Olivier Deprez92d4c212022-12-06 15:05:30 +0100108 reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
110 /* _ASM_EXTABLE_EX hack */
111 if (alt->new_off >= 0x7ffffff0)
112 alt->new_off -= 0x7ffffff0;
113 }
114
115 return 0;
116}
117
118/*
119 * Read all the special sections and create a list of special_alt structs which
120 * describe all the alternate instructions which can be patched in or
121 * redirected to at runtime.
122 */
123int special_get_alts(struct elf *elf, struct list_head *alts)
124{
125 struct special_entry *entry;
126 struct section *sec;
127 unsigned int nr_entries;
128 struct special_alt *alt;
129 int idx, ret;
130
131 INIT_LIST_HEAD(alts);
132
133 for (entry = entries; entry->sec; entry++) {
134 sec = find_section_by_name(elf, entry->sec);
135 if (!sec)
136 continue;
137
138 if (sec->len % entry->size != 0) {
139 WARN("%s size not a multiple of %d",
140 sec->name, entry->size);
141 return -1;
142 }
143
144 nr_entries = sec->len / entry->size;
145
146 for (idx = 0; idx < nr_entries; idx++) {
147 alt = malloc(sizeof(*alt));
148 if (!alt) {
149 WARN("malloc failed");
150 return -1;
151 }
152 memset(alt, 0, sizeof(*alt));
153
154 ret = get_alt_entry(elf, entry, sec, idx, alt);
Olivier Deprez92d4c212022-12-06 15:05:30 +0100155 if (ret > 0)
156 continue;
157 if (ret < 0)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158 return ret;
159
160 list_add_tail(&alt->list, alts);
161 }
162 }
163
164 return 0;
165}