1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
9 #include <linux/objtool_types.h>
10 #include <asm/orc_types.h>
12 #include <objtool/check.h>
13 #include <objtool/warn.h>
14 #include <objtool/endianness.h>
16 static int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi,
17 struct instruction *insn)
19 struct cfi_reg *bp = &cfi->regs[CFI_BP];
21 memset(orc, 0, sizeof(*orc));
25 * This is usually either unreachable nops/traps (which don't
26 * trigger unreachable instruction warnings), or
27 * STACK_FRAME_NON_STANDARD functions.
29 orc->type = ORC_TYPE_UNDEFINED;
34 case UNWIND_HINT_TYPE_UNDEFINED:
35 orc->type = ORC_TYPE_UNDEFINED;
37 case UNWIND_HINT_TYPE_END_OF_STACK:
38 orc->type = ORC_TYPE_END_OF_STACK;
40 case UNWIND_HINT_TYPE_CALL:
41 orc->type = ORC_TYPE_CALL;
43 case UNWIND_HINT_TYPE_REGS:
44 orc->type = ORC_TYPE_REGS;
46 case UNWIND_HINT_TYPE_REGS_PARTIAL:
47 orc->type = ORC_TYPE_REGS_PARTIAL;
50 WARN_INSN(insn, "unknown unwind hint type %d", cfi->type);
54 orc->signal = cfi->signal;
56 switch (cfi->cfa.base) {
58 orc->sp_reg = ORC_REG_SP;
61 orc->sp_reg = ORC_REG_SP_INDIRECT;
64 orc->sp_reg = ORC_REG_BP;
67 orc->sp_reg = ORC_REG_BP_INDIRECT;
70 orc->sp_reg = ORC_REG_R10;
73 orc->sp_reg = ORC_REG_R13;
76 orc->sp_reg = ORC_REG_DI;
79 orc->sp_reg = ORC_REG_DX;
82 WARN_INSN(insn, "unknown CFA base reg %d", cfi->cfa.base);
88 orc->bp_reg = ORC_REG_UNDEFINED;
91 orc->bp_reg = ORC_REG_PREV_SP;
94 orc->bp_reg = ORC_REG_BP;
97 WARN_INSN(insn, "unknown BP base reg %d", bp->base);
101 orc->sp_offset = cfi->cfa.offset;
102 orc->bp_offset = bp->offset;
107 static int write_orc_entry(struct elf *elf, struct section *orc_sec,
108 struct section *ip_sec, unsigned int idx,
109 struct section *insn_sec, unsigned long insn_off,
112 struct orc_entry *orc;
114 /* populate ORC data */
115 orc = (struct orc_entry *)orc_sec->data->d_buf + idx;
116 memcpy(orc, o, sizeof(*orc));
117 orc->sp_offset = bswap_if_needed(elf, orc->sp_offset);
118 orc->bp_offset = bswap_if_needed(elf, orc->bp_offset);
120 /* populate reloc for ip */
121 if (!elf_init_reloc_text_sym(elf, ip_sec, idx * sizeof(int), idx,
128 struct orc_list_entry {
129 struct list_head list;
130 struct orc_entry orc;
131 struct section *insn_sec;
132 unsigned long insn_off;
135 static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
136 struct section *sec, unsigned long offset)
138 struct orc_list_entry *entry = malloc(sizeof(*entry));
141 WARN("malloc failed");
146 entry->insn_sec = sec;
147 entry->insn_off = offset;
149 list_add_tail(&entry->list, orc_list);
153 static unsigned long alt_group_len(struct alt_group *alt_group)
155 return alt_group->last_insn->offset +
156 alt_group->last_insn->len -
157 alt_group->first_insn->offset;
160 int orc_create(struct objtool_file *file)
162 struct section *sec, *orc_sec;
163 unsigned int nr = 0, idx = 0;
164 struct orc_list_entry *entry;
165 struct list_head orc_list;
167 struct orc_entry null = { .type = ORC_TYPE_UNDEFINED };
169 /* Build a deduplicated list of ORC entries: */
170 INIT_LIST_HEAD(&orc_list);
171 for_each_sec(file, sec) {
172 struct orc_entry orc, prev_orc = {0};
173 struct instruction *insn;
179 sec_for_each_insn(file, sec, insn) {
180 struct alt_group *alt_group = insn->alt_group;
184 if (init_orc_entry(&orc, insn->cfi, insn))
186 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
188 if (orc_list_add(&orc_list, &orc, sec,
198 * Alternatives can have different stack layout
199 * possibilities (but they shouldn't conflict).
200 * Instead of traversing the instructions, use the
201 * alt_group's flattened byte-offset-addressed CFI
204 for (i = 0; i < alt_group_len(alt_group); i++) {
205 struct cfi_state *cfi = alt_group->cfi[i];
208 /* errors are reported on the original insn */
209 if (init_orc_entry(&orc, cfi, insn))
211 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
213 if (orc_list_add(&orc_list, &orc, insn->sec,
221 /* Skip to the end of the alt_group */
222 insn = alt_group->last_insn;
225 /* Add a section terminator */
227 orc_list_add(&orc_list, &null, sec, sec->sh.sh_size);
234 /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
235 sec = find_section_by_name(file->elf, ".orc_unwind");
237 WARN("file already has .orc_unwind section, skipping");
240 orc_sec = elf_create_section(file->elf, ".orc_unwind",
241 sizeof(struct orc_entry), nr);
245 sec = elf_create_section_pair(file->elf, ".orc_unwind_ip", sizeof(int), nr, nr);
249 /* Write ORC entries to sections: */
250 list_for_each_entry(entry, &orc_list, list) {
251 if (write_orc_entry(file->elf, orc_sec, sec, idx++,
252 entry->insn_sec, entry->insn_off,