1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
12 int create_orc(struct objtool_file *file)
14 struct instruction *insn;
16 for_each_insn(file, insn) {
17 struct orc_entry *orc = &insn->orc;
18 struct cfi_reg *cfa = &insn->cfi.cfa;
19 struct cfi_reg *bp = &insn->cfi.regs[CFI_BP];
21 orc->end = insn->cfi.end;
23 if (cfa->base == CFI_UNDEFINED) {
24 orc->sp_reg = ORC_REG_UNDEFINED;
30 orc->sp_reg = ORC_REG_SP;
33 orc->sp_reg = ORC_REG_SP_INDIRECT;
36 orc->sp_reg = ORC_REG_BP;
39 orc->sp_reg = ORC_REG_BP_INDIRECT;
42 orc->sp_reg = ORC_REG_R10;
45 orc->sp_reg = ORC_REG_R13;
48 orc->sp_reg = ORC_REG_DI;
51 orc->sp_reg = ORC_REG_DX;
54 WARN_FUNC("unknown CFA base reg %d",
55 insn->sec, insn->offset, cfa->base);
61 orc->bp_reg = ORC_REG_UNDEFINED;
64 orc->bp_reg = ORC_REG_PREV_SP;
67 orc->bp_reg = ORC_REG_BP;
70 WARN_FUNC("unknown BP base reg %d",
71 insn->sec, insn->offset, bp->base);
75 orc->sp_offset = cfa->offset;
76 orc->bp_offset = bp->offset;
77 orc->type = insn->cfi.type;
83 static int create_orc_entry(struct elf *elf, struct section *u_sec, struct section *ip_relocsec,
84 unsigned int idx, struct section *insn_sec,
85 unsigned long insn_off, struct orc_entry *o)
87 struct orc_entry *orc;
90 /* populate ORC data */
91 orc = (struct orc_entry *)u_sec->data->d_buf + idx;
92 memcpy(orc, o, sizeof(*orc));
94 /* populate reloc for ip */
95 reloc = malloc(sizeof(*reloc));
100 memset(reloc, 0, sizeof(*reloc));
103 reloc->sym = insn_sec->sym;
104 reloc->addend = insn_off;
107 * The Clang assembler doesn't produce section symbols, so we
108 * have to reference the function symbol instead:
110 reloc->sym = find_symbol_containing(insn_sec, insn_off);
113 * Hack alert. This happens when we need to reference
114 * the NOP pad insn immediately after the function.
116 reloc->sym = find_symbol_containing(insn_sec,
120 WARN("missing symbol for insn at offset 0x%lx\n",
125 reloc->addend = insn_off - reloc->sym->offset;
128 reloc->type = R_X86_64_PC32;
129 reloc->offset = idx * sizeof(int);
130 reloc->sec = ip_relocsec;
132 elf_add_reloc(elf, reloc);
137 int create_orc_sections(struct objtool_file *file)
139 struct instruction *insn, *prev_insn;
140 struct section *sec, *u_sec, *ip_relocsec;
143 struct orc_entry empty = {
144 .sp_reg = ORC_REG_UNDEFINED,
145 .bp_reg = ORC_REG_UNDEFINED,
146 .type = ORC_TYPE_CALL,
149 sec = find_section_by_name(file->elf, ".orc_unwind");
151 WARN("file already has .orc_unwind section, skipping");
155 /* count the number of needed orcs */
157 for_each_sec(file, sec) {
162 sec_for_each_insn(file, sec, insn) {
164 memcmp(&insn->orc, &prev_insn->orc,
165 sizeof(struct orc_entry))) {
171 /* section terminator */
179 /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
180 sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), idx);
184 ip_relocsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
188 /* create .orc_unwind section */
189 u_sec = elf_create_section(file->elf, ".orc_unwind", 0,
190 sizeof(struct orc_entry), idx);
192 /* populate sections */
194 for_each_sec(file, sec) {
199 sec_for_each_insn(file, sec, insn) {
200 if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
201 sizeof(struct orc_entry))) {
203 if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
204 insn->sec, insn->offset,
213 /* section terminator */
215 if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
217 prev_insn->offset + prev_insn->len,
225 if (elf_rebuild_reloc_section(file->elf, ip_relocsec))