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>
15 static int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi,
16 struct instruction *insn)
18 struct cfi_reg *bp = &cfi->regs[CFI_BP];
20 memset(orc, 0, sizeof(*orc));
24 orc->sp_reg = ORC_REG_UNDEFINED;
30 if (cfi->cfa.base == CFI_UNDEFINED) {
31 orc->sp_reg = ORC_REG_UNDEFINED;
35 switch (cfi->cfa.base) {
37 orc->sp_reg = ORC_REG_SP;
40 orc->sp_reg = ORC_REG_SP_INDIRECT;
43 orc->sp_reg = ORC_REG_BP;
46 orc->sp_reg = ORC_REG_BP_INDIRECT;
49 orc->sp_reg = ORC_REG_R10;
52 orc->sp_reg = ORC_REG_R13;
55 orc->sp_reg = ORC_REG_DI;
58 orc->sp_reg = ORC_REG_DX;
61 WARN_FUNC("unknown CFA base reg %d",
62 insn->sec, insn->offset, cfi->cfa.base);
68 orc->bp_reg = ORC_REG_UNDEFINED;
71 orc->bp_reg = ORC_REG_PREV_SP;
74 orc->bp_reg = ORC_REG_BP;
77 WARN_FUNC("unknown BP base reg %d",
78 insn->sec, insn->offset, bp->base);
82 orc->sp_offset = cfi->cfa.offset;
83 orc->bp_offset = bp->offset;
84 orc->type = cfi->type;
89 static int write_orc_entry(struct elf *elf, struct section *orc_sec,
90 struct section *ip_sec, unsigned int idx,
91 struct section *insn_sec, unsigned long insn_off,
94 struct orc_entry *orc;
96 /* populate ORC data */
97 orc = (struct orc_entry *)orc_sec->data->d_buf + idx;
98 memcpy(orc, o, sizeof(*orc));
100 /* populate reloc for ip */
101 if (elf_add_reloc_to_insn(elf, ip_sec, idx * sizeof(int), R_X86_64_PC32,
108 struct orc_list_entry {
109 struct list_head list;
110 struct orc_entry orc;
111 struct section *insn_sec;
112 unsigned long insn_off;
115 static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
116 struct section *sec, unsigned long offset)
118 struct orc_list_entry *entry = malloc(sizeof(*entry));
121 WARN("malloc failed");
126 entry->insn_sec = sec;
127 entry->insn_off = offset;
129 list_add_tail(&entry->list, orc_list);
133 static unsigned long alt_group_len(struct alt_group *alt_group)
135 return alt_group->last_insn->offset +
136 alt_group->last_insn->len -
137 alt_group->first_insn->offset;
140 int orc_create(struct objtool_file *file)
142 struct section *sec, *orc_sec;
143 unsigned int nr = 0, idx = 0;
144 struct orc_list_entry *entry;
145 struct list_head orc_list;
147 struct orc_entry null = {
148 .sp_reg = ORC_REG_UNDEFINED,
149 .bp_reg = ORC_REG_UNDEFINED,
150 .type = UNWIND_HINT_TYPE_CALL,
153 /* Build a deduplicated list of ORC entries: */
154 INIT_LIST_HEAD(&orc_list);
155 for_each_sec(file, sec) {
156 struct orc_entry orc, prev_orc = {0};
157 struct instruction *insn;
163 sec_for_each_insn(file, sec, insn) {
164 struct alt_group *alt_group = insn->alt_group;
168 if (init_orc_entry(&orc, insn->cfi, insn))
170 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
172 if (orc_list_add(&orc_list, &orc, sec,
182 * Alternatives can have different stack layout
183 * possibilities (but they shouldn't conflict).
184 * Instead of traversing the instructions, use the
185 * alt_group's flattened byte-offset-addressed CFI
188 for (i = 0; i < alt_group_len(alt_group); i++) {
189 struct cfi_state *cfi = alt_group->cfi[i];
192 /* errors are reported on the original insn */
193 if (init_orc_entry(&orc, cfi, insn))
195 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
197 if (orc_list_add(&orc_list, &orc, insn->sec,
205 /* Skip to the end of the alt_group */
206 insn = alt_group->last_insn;
209 /* Add a section terminator */
211 orc_list_add(&orc_list, &null, sec, sec->len);
218 /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
219 sec = find_section_by_name(file->elf, ".orc_unwind");
221 WARN("file already has .orc_unwind section, skipping");
224 orc_sec = elf_create_section(file->elf, ".orc_unwind", 0,
225 sizeof(struct orc_entry), nr);
229 sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), nr);
233 /* Write ORC entries to sections: */
234 list_for_each_entry(entry, &orc_list, list) {
235 if (write_orc_entry(file->elf, orc_sec, sec, idx++,
236 entry->insn_sec, entry->insn_off,