1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
9 #include <linux/objtool.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;
31 if (cfi->cfa.base == CFI_UNDEFINED) {
32 orc->sp_reg = ORC_REG_UNDEFINED;
36 switch (cfi->cfa.base) {
38 orc->sp_reg = ORC_REG_SP;
41 orc->sp_reg = ORC_REG_SP_INDIRECT;
44 orc->sp_reg = ORC_REG_BP;
47 orc->sp_reg = ORC_REG_BP_INDIRECT;
50 orc->sp_reg = ORC_REG_R10;
53 orc->sp_reg = ORC_REG_R13;
56 orc->sp_reg = ORC_REG_DI;
59 orc->sp_reg = ORC_REG_DX;
62 WARN_FUNC("unknown CFA base reg %d",
63 insn->sec, insn->offset, cfi->cfa.base);
69 orc->bp_reg = ORC_REG_UNDEFINED;
72 orc->bp_reg = ORC_REG_PREV_SP;
75 orc->bp_reg = ORC_REG_BP;
78 WARN_FUNC("unknown BP base reg %d",
79 insn->sec, insn->offset, bp->base);
83 orc->sp_offset = cfi->cfa.offset;
84 orc->bp_offset = bp->offset;
85 orc->type = cfi->type;
90 static int write_orc_entry(struct elf *elf, struct section *orc_sec,
91 struct section *ip_sec, unsigned int idx,
92 struct section *insn_sec, unsigned long insn_off,
95 struct orc_entry *orc;
97 /* populate ORC data */
98 orc = (struct orc_entry *)orc_sec->data->d_buf + idx;
99 memcpy(orc, o, sizeof(*orc));
100 orc->sp_offset = bswap_if_needed(orc->sp_offset);
101 orc->bp_offset = bswap_if_needed(orc->bp_offset);
103 /* populate reloc for ip */
104 if (elf_add_reloc_to_insn(elf, ip_sec, idx * sizeof(int), R_X86_64_PC32,
111 struct orc_list_entry {
112 struct list_head list;
113 struct orc_entry orc;
114 struct section *insn_sec;
115 unsigned long insn_off;
118 static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
119 struct section *sec, unsigned long offset)
121 struct orc_list_entry *entry = malloc(sizeof(*entry));
124 WARN("malloc failed");
129 entry->insn_sec = sec;
130 entry->insn_off = offset;
132 list_add_tail(&entry->list, orc_list);
136 static unsigned long alt_group_len(struct alt_group *alt_group)
138 return alt_group->last_insn->offset +
139 alt_group->last_insn->len -
140 alt_group->first_insn->offset;
143 int orc_create(struct objtool_file *file)
145 struct section *sec, *orc_sec;
146 unsigned int nr = 0, idx = 0;
147 struct orc_list_entry *entry;
148 struct list_head orc_list;
150 struct orc_entry null = {
151 .sp_reg = ORC_REG_UNDEFINED,
152 .bp_reg = ORC_REG_UNDEFINED,
153 .type = UNWIND_HINT_TYPE_CALL,
156 /* Build a deduplicated list of ORC entries: */
157 INIT_LIST_HEAD(&orc_list);
158 for_each_sec(file, sec) {
159 struct orc_entry orc, prev_orc = {0};
160 struct instruction *insn;
166 sec_for_each_insn(file, sec, insn) {
167 struct alt_group *alt_group = insn->alt_group;
171 if (init_orc_entry(&orc, insn->cfi, insn))
173 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
175 if (orc_list_add(&orc_list, &orc, sec,
185 * Alternatives can have different stack layout
186 * possibilities (but they shouldn't conflict).
187 * Instead of traversing the instructions, use the
188 * alt_group's flattened byte-offset-addressed CFI
191 for (i = 0; i < alt_group_len(alt_group); i++) {
192 struct cfi_state *cfi = alt_group->cfi[i];
195 /* errors are reported on the original insn */
196 if (init_orc_entry(&orc, cfi, insn))
198 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
200 if (orc_list_add(&orc_list, &orc, insn->sec,
208 /* Skip to the end of the alt_group */
209 insn = alt_group->last_insn;
212 /* Add a section terminator */
214 orc_list_add(&orc_list, &null, sec, sec->sh.sh_size);
221 /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
222 sec = find_section_by_name(file->elf, ".orc_unwind");
224 WARN("file already has .orc_unwind section, skipping");
227 orc_sec = elf_create_section(file->elf, ".orc_unwind", 0,
228 sizeof(struct orc_entry), nr);
232 sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), nr);
236 /* Write ORC entries to sections: */
237 list_for_each_entry(entry, &orc_list, list) {
238 if (write_orc_entry(file->elf, orc_sec, sec, idx++,
239 entry->insn_sec, entry->insn_off,