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 orc->sp_reg = ORC_REG_UNDEFINED;
30 case UNWIND_HINT_TYPE_CALL:
31 orc->type = ORC_TYPE_CALL;
33 case UNWIND_HINT_TYPE_REGS:
34 orc->type = ORC_TYPE_REGS;
36 case UNWIND_HINT_TYPE_REGS_PARTIAL:
37 orc->type = ORC_TYPE_REGS_PARTIAL;
40 WARN_FUNC("unknown unwind hint type %d",
41 insn->sec, insn->offset, cfi->type);
46 orc->signal = cfi->signal;
48 if (cfi->cfa.base == CFI_UNDEFINED) {
49 orc->sp_reg = ORC_REG_UNDEFINED;
53 switch (cfi->cfa.base) {
55 orc->sp_reg = ORC_REG_SP;
58 orc->sp_reg = ORC_REG_SP_INDIRECT;
61 orc->sp_reg = ORC_REG_BP;
64 orc->sp_reg = ORC_REG_BP_INDIRECT;
67 orc->sp_reg = ORC_REG_R10;
70 orc->sp_reg = ORC_REG_R13;
73 orc->sp_reg = ORC_REG_DI;
76 orc->sp_reg = ORC_REG_DX;
79 WARN_FUNC("unknown CFA base reg %d",
80 insn->sec, insn->offset, cfi->cfa.base);
86 orc->bp_reg = ORC_REG_UNDEFINED;
89 orc->bp_reg = ORC_REG_PREV_SP;
92 orc->bp_reg = ORC_REG_BP;
95 WARN_FUNC("unknown BP base reg %d",
96 insn->sec, insn->offset, bp->base);
100 orc->sp_offset = cfi->cfa.offset;
101 orc->bp_offset = bp->offset;
106 static int write_orc_entry(struct elf *elf, struct section *orc_sec,
107 struct section *ip_sec, unsigned int idx,
108 struct section *insn_sec, unsigned long insn_off,
111 struct orc_entry *orc;
113 /* populate ORC data */
114 orc = (struct orc_entry *)orc_sec->data->d_buf + idx;
115 memcpy(orc, o, sizeof(*orc));
116 orc->sp_offset = bswap_if_needed(elf, orc->sp_offset);
117 orc->bp_offset = bswap_if_needed(elf, orc->bp_offset);
119 /* populate reloc for ip */
120 if (elf_add_reloc_to_insn(elf, ip_sec, idx * sizeof(int), R_X86_64_PC32,
127 struct orc_list_entry {
128 struct list_head list;
129 struct orc_entry orc;
130 struct section *insn_sec;
131 unsigned long insn_off;
134 static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
135 struct section *sec, unsigned long offset)
137 struct orc_list_entry *entry = malloc(sizeof(*entry));
140 WARN("malloc failed");
145 entry->insn_sec = sec;
146 entry->insn_off = offset;
148 list_add_tail(&entry->list, orc_list);
152 static unsigned long alt_group_len(struct alt_group *alt_group)
154 return alt_group->last_insn->offset +
155 alt_group->last_insn->len -
156 alt_group->first_insn->offset;
159 int orc_create(struct objtool_file *file)
161 struct section *sec, *orc_sec;
162 unsigned int nr = 0, idx = 0;
163 struct orc_list_entry *entry;
164 struct list_head orc_list;
166 struct orc_entry null = {
167 .sp_reg = ORC_REG_UNDEFINED,
168 .bp_reg = ORC_REG_UNDEFINED,
169 .type = ORC_TYPE_CALL,
172 /* Build a deduplicated list of ORC entries: */
173 INIT_LIST_HEAD(&orc_list);
174 for_each_sec(file, sec) {
175 struct orc_entry orc, prev_orc = {0};
176 struct instruction *insn;
182 sec_for_each_insn(file, sec, insn) {
183 struct alt_group *alt_group = insn->alt_group;
187 if (init_orc_entry(&orc, insn->cfi, insn))
189 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
191 if (orc_list_add(&orc_list, &orc, sec,
201 * Alternatives can have different stack layout
202 * possibilities (but they shouldn't conflict).
203 * Instead of traversing the instructions, use the
204 * alt_group's flattened byte-offset-addressed CFI
207 for (i = 0; i < alt_group_len(alt_group); i++) {
208 struct cfi_state *cfi = alt_group->cfi[i];
211 /* errors are reported on the original insn */
212 if (init_orc_entry(&orc, cfi, insn))
214 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
216 if (orc_list_add(&orc_list, &orc, insn->sec,
224 /* Skip to the end of the alt_group */
225 insn = alt_group->last_insn;
228 /* Add a section terminator */
230 orc_list_add(&orc_list, &null, sec, sec->sh.sh_size);
237 /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
238 sec = find_section_by_name(file->elf, ".orc_unwind");
240 WARN("file already has .orc_unwind section, skipping");
243 orc_sec = elf_create_section(file->elf, ".orc_unwind", 0,
244 sizeof(struct orc_entry), nr);
248 sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), nr);
252 /* Write ORC entries to sections: */
253 list_for_each_entry(entry, &orc_list, list) {
254 if (write_orc_entry(file->elf, orc_sec, sec, idx++,
255 entry->insn_sec, entry->insn_off,