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 if (!insn_sec->sym) {
104 WARN("missing symbol for section %s", insn_sec->name);
108 /* populate ORC data */
109 orc = (struct orc_entry *)u_sec->data->d_buf + idx;
110 memcpy(orc, o, sizeof(*orc));
112 /* populate rela for ip */
113 rela = malloc(sizeof(*rela));
118 memset(rela, 0, sizeof(*rela));
120 rela->sym = insn_sec->sym;
121 rela->addend = insn_off;
122 rela->type = R_X86_64_PC32;
123 rela->offset = idx * sizeof(int);
125 list_add_tail(&rela->list, &ip_relasec->rela_list);
126 hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
131 int create_orc_sections(struct objtool_file *file)
133 struct instruction *insn, *prev_insn;
134 struct section *sec, *u_sec, *ip_relasec;
137 struct orc_entry empty = {
138 .sp_reg = ORC_REG_UNDEFINED,
139 .bp_reg = ORC_REG_UNDEFINED,
140 .type = ORC_TYPE_CALL,
143 sec = find_section_by_name(file->elf, ".orc_unwind");
145 WARN("file already has .orc_unwind section, skipping");
149 /* count the number of needed orcs */
151 for_each_sec(file, sec) {
156 sec_for_each_insn(file, sec, insn) {
158 memcmp(&insn->orc, &prev_insn->orc,
159 sizeof(struct orc_entry))) {
165 /* section terminator */
173 /* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
174 sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), idx);
178 ip_relasec = elf_create_rela_section(file->elf, sec);
182 /* create .orc_unwind section */
183 u_sec = elf_create_section(file->elf, ".orc_unwind",
184 sizeof(struct orc_entry), idx);
186 /* populate sections */
188 for_each_sec(file, sec) {
193 sec_for_each_insn(file, sec, insn) {
194 if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
195 sizeof(struct orc_entry))) {
197 if (create_orc_entry(u_sec, ip_relasec, idx,
198 insn->sec, insn->offset,
207 /* section terminator */
209 if (create_orc_entry(u_sec, ip_relasec, idx,
211 prev_insn->offset + prev_insn->len,
219 if (elf_rebuild_rela_section(ip_relasec))