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_rsec, unsigned int idx,
86 struct section *insn_sec, unsigned long insn_off,
89 struct orc_entry *orc;
92 /* populate ORC data */
93 orc = (struct orc_entry *)orc_sec->data->d_buf + idx;
94 memcpy(orc, o, sizeof(*orc));
95 orc->sp_offset = bswap_if_needed(orc->sp_offset);
96 orc->bp_offset = bswap_if_needed(orc->bp_offset);
98 /* populate reloc for ip */
99 reloc = malloc(sizeof(*reloc));
104 memset(reloc, 0, sizeof(*reloc));
106 insn_to_reloc_sym_addend(insn_sec, insn_off, reloc);
108 WARN("missing symbol for insn at offset 0x%lx",
113 reloc->type = R_X86_64_PC32;
114 reloc->offset = idx * sizeof(int);
115 reloc->sec = ip_rsec;
117 elf_add_reloc(elf, reloc);
122 struct orc_list_entry {
123 struct list_head list;
124 struct orc_entry orc;
125 struct section *insn_sec;
126 unsigned long insn_off;
129 static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
130 struct section *sec, unsigned long offset)
132 struct orc_list_entry *entry = malloc(sizeof(*entry));
135 WARN("malloc failed");
140 entry->insn_sec = sec;
141 entry->insn_off = offset;
143 list_add_tail(&entry->list, orc_list);
147 static unsigned long alt_group_len(struct alt_group *alt_group)
149 return alt_group->last_insn->offset +
150 alt_group->last_insn->len -
151 alt_group->first_insn->offset;
154 int orc_create(struct objtool_file *file)
156 struct section *sec, *ip_rsec, *orc_sec;
157 unsigned int nr = 0, idx = 0;
158 struct orc_list_entry *entry;
159 struct list_head orc_list;
161 struct orc_entry null = {
162 .sp_reg = ORC_REG_UNDEFINED,
163 .bp_reg = ORC_REG_UNDEFINED,
164 .type = UNWIND_HINT_TYPE_CALL,
167 /* Build a deduplicated list of ORC entries: */
168 INIT_LIST_HEAD(&orc_list);
169 for_each_sec(file, sec) {
170 struct orc_entry orc, prev_orc = {0};
171 struct instruction *insn;
177 sec_for_each_insn(file, sec, insn) {
178 struct alt_group *alt_group = insn->alt_group;
182 if (init_orc_entry(&orc, &insn->cfi))
184 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
186 if (orc_list_add(&orc_list, &orc, sec,
196 * Alternatives can have different stack layout
197 * possibilities (but they shouldn't conflict).
198 * Instead of traversing the instructions, use the
199 * alt_group's flattened byte-offset-addressed CFI
202 for (i = 0; i < alt_group_len(alt_group); i++) {
203 struct cfi_state *cfi = alt_group->cfi[i];
206 if (init_orc_entry(&orc, cfi))
208 if (!memcmp(&prev_orc, &orc, sizeof(orc)))
210 if (orc_list_add(&orc_list, &orc, insn->sec,
218 /* Skip to the end of the alt_group */
219 insn = alt_group->last_insn;
222 /* Add a section terminator */
224 orc_list_add(&orc_list, &null, sec, sec->len);
231 /* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
232 sec = find_section_by_name(file->elf, ".orc_unwind");
234 WARN("file already has .orc_unwind section, skipping");
237 orc_sec = elf_create_section(file->elf, ".orc_unwind", 0,
238 sizeof(struct orc_entry), nr);
242 sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), nr);
245 ip_rsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
249 /* Write ORC entries to sections: */
250 list_for_each_entry(entry, &orc_list, list) {
251 if (write_orc_entry(file->elf, orc_sec, ip_rsec, idx++,
252 entry->insn_sec, entry->insn_off,
257 if (elf_rebuild_reloc_section(file->elf, ip_rsec))