Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / objtool / special.c
blob1a2420febd08a4f48e954490f92676d99466a54d
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
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 */
11 #include <stdlib.h>
12 #include <string.h>
14 #include "builtin.h"
15 #include "special.h"
16 #include "warn.h"
17 #include "arch_special.h"
19 struct 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 */
27 struct special_entry entries[] = {
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,
39 .sec = "__jump_table",
40 .jump_or_nop = true,
41 .size = JUMP_ENTRY_SIZE,
42 .orig = JUMP_ORIG_OFFSET,
43 .new = JUMP_NEW_OFFSET,
46 .sec = "__ex_table",
47 .size = EX_ENTRY_SIZE,
48 .orig = EX_ORIG_OFFSET,
49 .new = EX_NEW_OFFSET,
51 {},
54 void __weak arch_handle_alternative(unsigned short feature, struct special_alt *alt)
58 static int get_alt_entry(struct elf *elf, struct special_entry *entry,
59 struct section *sec, int idx,
60 struct special_alt *alt)
62 struct reloc *orig_reloc, *new_reloc;
63 unsigned long offset;
65 offset = idx * entry->size;
67 alt->group = entry->group;
68 alt->jump_or_nop = entry->jump_or_nop;
70 if (alt->group) {
71 alt->orig_len = *(unsigned char *)(sec->data->d_buf + offset +
72 entry->orig_len);
73 alt->new_len = *(unsigned char *)(sec->data->d_buf + offset +
74 entry->new_len);
77 if (entry->feature) {
78 unsigned short feature;
80 feature = *(unsigned short *)(sec->data->d_buf + offset +
81 entry->feature);
82 arch_handle_alternative(feature, alt);
85 orig_reloc = find_reloc_by_dest(elf, sec, offset + entry->orig);
86 if (!orig_reloc) {
87 WARN_FUNC("can't find orig reloc", sec, offset + entry->orig);
88 return -1;
90 if (orig_reloc->sym->type != STT_SECTION) {
91 WARN_FUNC("don't know how to handle non-section reloc symbol %s",
92 sec, offset + entry->orig, orig_reloc->sym->name);
93 return -1;
96 alt->orig_sec = orig_reloc->sym->sec;
97 alt->orig_off = orig_reloc->addend;
99 if (!entry->group || alt->new_len) {
100 new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
101 if (!new_reloc) {
102 WARN_FUNC("can't find new reloc",
103 sec, offset + entry->new);
104 return -1;
107 alt->new_sec = new_reloc->sym->sec;
108 alt->new_off = (unsigned int)new_reloc->addend;
110 /* _ASM_EXTABLE_EX hack */
111 if (alt->new_off >= 0x7ffffff0)
112 alt->new_off -= 0x7ffffff0;
115 return 0;
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.
123 int special_get_alts(struct elf *elf, struct list_head *alts)
125 struct special_entry *entry;
126 struct section *sec;
127 unsigned int nr_entries;
128 struct special_alt *alt;
129 int idx, ret;
131 INIT_LIST_HEAD(alts);
133 for (entry = entries; entry->sec; entry++) {
134 sec = find_section_by_name(elf, entry->sec);
135 if (!sec)
136 continue;
138 if (sec->len % entry->size != 0) {
139 WARN("%s size not a multiple of %d",
140 sec->name, entry->size);
141 return -1;
144 nr_entries = sec->len / entry->size;
146 for (idx = 0; idx < nr_entries; idx++) {
147 alt = malloc(sizeof(*alt));
148 if (!alt) {
149 WARN("malloc failed");
150 return -1;
152 memset(alt, 0, sizeof(*alt));
154 ret = get_alt_entry(elf, entry, sec, idx, alt);
155 if (ret)
156 return ret;
158 list_add_tail(&alt->list, alts);
162 return 0;