2 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
25 int create_orc(struct objtool_file *file)
27 struct instruction *insn;
29 for_each_insn(file, insn) {
30 struct orc_entry *orc = &insn->orc;
31 struct cfi_reg *cfa = &insn->state.cfa;
32 struct cfi_reg *bp = &insn->state.regs[CFI_BP];
34 orc->end = insn->state.end;
36 if (cfa->base == CFI_UNDEFINED) {
37 orc->sp_reg = ORC_REG_UNDEFINED;
43 orc->sp_reg = ORC_REG_SP;
46 orc->sp_reg = ORC_REG_SP_INDIRECT;
49 orc->sp_reg = ORC_REG_BP;
52 orc->sp_reg = ORC_REG_BP_INDIRECT;
55 orc->sp_reg = ORC_REG_R10;
58 orc->sp_reg = ORC_REG_R13;
61 orc->sp_reg = ORC_REG_DI;
64 orc->sp_reg = ORC_REG_DX;
67 WARN_FUNC("unknown CFA base reg %d",
68 insn->sec, insn->offset, cfa->base);
74 orc->bp_reg = ORC_REG_UNDEFINED;
77 orc->bp_reg = ORC_REG_PREV_SP;
80 orc->bp_reg = ORC_REG_BP;
83 WARN_FUNC("unknown BP base reg %d",
84 insn->sec, insn->offset, bp->base);
88 orc->sp_offset = cfa->offset;
89 orc->bp_offset = bp->offset;
90 orc->type = insn->state.type;
96 static int create_orc_entry(struct section *u_sec, struct section *ip_relasec,
97 unsigned int idx, struct section *insn_sec,
98 unsigned long insn_off, struct orc_entry *o)
100 struct orc_entry *orc;
103 /* populate ORC data */
104 orc = (struct orc_entry *)u_sec->data->d_buf + idx;
105 memcpy(orc, o, sizeof(*orc));
107 /* populate rela for ip */
108 rela = malloc(sizeof(*rela));
113 memset(rela, 0, sizeof(*rela));
116 rela->sym = insn_sec->sym;
117 rela->addend = insn_off;
120 * The Clang assembler doesn't produce section symbols, so we
121 * have to reference the function symbol instead:
123 rela->sym = find_symbol_containing(insn_sec, insn_off);
126 * Hack alert. This happens when we need to reference
127 * the NOP pad insn immediately after the function.
129 rela->sym = find_symbol_containing(insn_sec,
133 WARN("missing symbol for insn at offset 0x%lx\n",
138 rela->addend = insn_off - rela->sym->offset;
141 rela->type = R_X86_64_PC32;
142 rela->offset = idx * sizeof(int);
144 list_add_tail(&rela->list, &ip_relasec->rela_list);
145 hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
150 int create_orc_sections(struct objtool_file *file)
152 struct instruction *insn, *prev_insn;
153 struct section *sec, *u_sec, *ip_relasec;
156 struct orc_entry empty = {
157 .sp_reg = ORC_REG_UNDEFINED,
158 .bp_reg = ORC_REG_UNDEFINED,
159 .type = ORC_TYPE_CALL,
162 sec = find_section_by_name(file->elf, ".orc_unwind");
164 WARN("file already has .orc_unwind section, skipping");
168 /* count the number of needed orcs */
170 for_each_sec(file, sec) {
175 sec_for_each_insn(file, sec, insn) {
177 memcmp(&insn->orc, &prev_insn->orc,
178 sizeof(struct orc_entry))) {
184 /* section terminator */
192 /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
193 sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), idx);
197 ip_relasec = elf_create_rela_section(file->elf, sec);
201 /* create .orc_unwind section */
202 u_sec = elf_create_section(file->elf, ".orc_unwind",
203 sizeof(struct orc_entry), idx);
205 /* populate sections */
207 for_each_sec(file, sec) {
212 sec_for_each_insn(file, sec, insn) {
213 if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
214 sizeof(struct orc_entry))) {
216 if (create_orc_entry(u_sec, ip_relasec, idx,
217 insn->sec, insn->offset,
226 /* section terminator */
228 if (create_orc_entry(u_sec, ip_relasec, idx,
230 prev_insn->offset + prev_insn->len,
238 if (elf_rebuild_rela_section(ip_relasec))