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)
18 struct instruction *insn = container_of(cfi, struct instruction, cfi);
19 struct cfi_reg *bp = &cfi->regs[CFI_BP];
21 memset(orc, 0, sizeof(*orc));
25 if (cfi->cfa.base == CFI_UNDEFINED) {
26 orc->sp_reg = ORC_REG_UNDEFINED;
30 switch (cfi->cfa.base) {
32 orc->sp_reg = ORC_REG_SP;
35 orc->sp_reg = ORC_REG_SP_INDIRECT;
38 orc->sp_reg = ORC_REG_BP;
41 orc->sp_reg = ORC_REG_BP_INDIRECT;
44 orc->sp_reg = ORC_REG_R10;
47 orc->sp_reg = ORC_REG_R13;
50 orc->sp_reg = ORC_REG_DI;
53 orc->sp_reg = ORC_REG_DX;
56 WARN_FUNC("unknown CFA base reg %d",
57 insn->sec, insn->offset, cfi->cfa.base);
63 orc->bp_reg = ORC_REG_UNDEFINED;
66 orc->bp_reg = ORC_REG_PREV_SP;
69 orc->bp_reg = ORC_REG_BP;
72 WARN_FUNC("unknown BP base reg %d",
73 insn->sec, insn->offset, bp->base);
77 orc->sp_offset = cfi->cfa.offset;
78 orc->bp_offset = bp->offset;
79 orc->type = cfi->type;
84 static int write_orc_entry(struct elf *elf, struct section *orc_sec,
85 struct section *ip_sec, unsigned int idx,
86 struct section *insn_sec, unsigned long insn_off,
89 struct orc_entry *orc;
91 /* populate ORC data */
92 orc = (struct orc_entry *)orc_sec->data->d_buf + idx;
93 memcpy(orc, o, sizeof(*orc));
94 orc->sp_offset = bswap_if_needed(orc->sp_offset);
95 orc->bp_offset = bswap_if_needed(orc->bp_offset);
97 /* populate reloc for ip */
98 if (elf_add_reloc_to_insn(elf, ip_sec, idx * sizeof(int), R_X86_64_PC32,
105 struct orc_list_entry {
106 struct list_head list;
107 struct orc_entry orc;
108 struct section *insn_sec;
109 unsigned long insn_off;
112 static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
113 struct section *sec, unsigned long offset)
115 struct orc_list_entry *entry = malloc(sizeof(*entry));
118 WARN("malloc failed");
123 entry->insn_sec = sec;
124 entry->insn_off = offset;
126 list_add_tail(&entry->list, orc_list);
130 static unsigned long alt_group_len(struct alt_group *alt_group)
132 return alt_group->last_insn->offset +
133 alt_group->last_insn->len -
134 alt_group->first_insn->offset;
137 int orc_create(struct objtool_file *file)
139 struct section *sec, *orc_sec;
140 unsigned int nr = 0, idx = 0;
141 struct orc_list_entry *entry;
142 struct list_head orc_list;
144 struct orc_entry null = {
145 .sp_reg = ORC_REG_UNDEFINED,
146 .bp_reg = ORC_REG_UNDEFINED,
147 .type = UNWIND_HINT_TYPE_CALL,
150 /* Build a deduplicated list of ORC entries: */
151 INIT_LIST_HEAD(&orc_list);
152 for_each_sec(file, sec) {
153 struct orc_entry orc, prev_orc = {0};
154 struct instruction *insn;
160 sec_for_each_insn(file, sec, insn) {
161 struct alt_group *alt_group = insn->alt_group;
165 if (init_orc_entry(&orc, &insn->cfi))
167 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
169 if (orc_list_add(&orc_list, &orc, sec,
179 * Alternatives can have different stack layout
180 * possibilities (but they shouldn't conflict).
181 * Instead of traversing the instructions, use the
182 * alt_group's flattened byte-offset-addressed CFI
185 for (i = 0; i < alt_group_len(alt_group); i++) {
186 struct cfi_state *cfi = alt_group->cfi[i];
189 if (init_orc_entry(&orc, cfi))
191 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
193 if (orc_list_add(&orc_list, &orc, insn->sec,
201 /* Skip to the end of the alt_group */
202 insn = alt_group->last_insn;
205 /* Add a section terminator */
207 orc_list_add(&orc_list, &null, sec, sec->sh.sh_size);
214 /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
215 sec = find_section_by_name(file->elf, ".orc_unwind");
217 WARN("file already has .orc_unwind section, skipping");
220 orc_sec = elf_create_section(file->elf, ".orc_unwind", 0,
221 sizeof(struct orc_entry), nr);
225 sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), nr);
229 /* Write ORC entries to sections: */
230 list_for_each_entry(entry, &orc_list, list) {
231 if (write_orc_entry(file->elf, orc_sec, sec, idx++,
232 entry->insn_sec, entry->insn_off,