orc_gen.c 4.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
 */

#include <stdlib.h>
#include <string.h>

#include "check.h"
#include "warn.h"

int create_orc(struct objtool_file *file)
{
	struct instruction *insn;

	for_each_insn(file, insn) {
		struct orc_entry *orc = &insn->orc;
18 19
		struct cfi_reg *cfa = &insn->cfi.cfa;
		struct cfi_reg *bp = &insn->cfi.regs[CFI_BP];
20

21 22 23
		if (!insn->sec->text)
			continue;

24
		orc->end = insn->cfi.end;
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
		if (cfa->base == CFI_UNDEFINED) {
			orc->sp_reg = ORC_REG_UNDEFINED;
			continue;
		}

		switch (cfa->base) {
		case CFI_SP:
			orc->sp_reg = ORC_REG_SP;
			break;
		case CFI_SP_INDIRECT:
			orc->sp_reg = ORC_REG_SP_INDIRECT;
			break;
		case CFI_BP:
			orc->sp_reg = ORC_REG_BP;
			break;
		case CFI_BP_INDIRECT:
			orc->sp_reg = ORC_REG_BP_INDIRECT;
			break;
		case CFI_R10:
			orc->sp_reg = ORC_REG_R10;
			break;
		case CFI_R13:
			orc->sp_reg = ORC_REG_R13;
			break;
		case CFI_DI:
			orc->sp_reg = ORC_REG_DI;
			break;
		case CFI_DX:
			orc->sp_reg = ORC_REG_DX;
			break;
		default:
			WARN_FUNC("unknown CFA base reg %d",
				  insn->sec, insn->offset, cfa->base);
			return -1;
		}

		switch(bp->base) {
		case CFI_UNDEFINED:
			orc->bp_reg = ORC_REG_UNDEFINED;
			break;
		case CFI_CFA:
			orc->bp_reg = ORC_REG_PREV_SP;
			break;
		case CFI_BP:
			orc->bp_reg = ORC_REG_BP;
			break;
		default:
			WARN_FUNC("unknown BP base reg %d",
				  insn->sec, insn->offset, bp->base);
			return -1;
		}

		orc->sp_offset = cfa->offset;
		orc->bp_offset = bp->offset;
80
		orc->type = insn->cfi.type;
81 82 83 84 85
	}

	return 0;
}

M
Matt Helsley 已提交
86
static int create_orc_entry(struct elf *elf, struct section *u_sec, struct section *ip_relocsec,
87 88 89 90
				unsigned int idx, struct section *insn_sec,
				unsigned long insn_off, struct orc_entry *o)
{
	struct orc_entry *orc;
M
Matt Helsley 已提交
91
	struct reloc *reloc;
92 93 94 95 96

	/* populate ORC data */
	orc = (struct orc_entry *)u_sec->data->d_buf + idx;
	memcpy(orc, o, sizeof(*orc));

M
Matt Helsley 已提交
97 98 99
	/* populate reloc for ip */
	reloc = malloc(sizeof(*reloc));
	if (!reloc) {
100 101 102
		perror("malloc");
		return -1;
	}
M
Matt Helsley 已提交
103
	memset(reloc, 0, sizeof(*reloc));
104

105
	if (insn_sec->sym) {
M
Matt Helsley 已提交
106 107
		reloc->sym = insn_sec->sym;
		reloc->addend = insn_off;
108 109 110 111 112
	} else {
		/*
		 * The Clang assembler doesn't produce section symbols, so we
		 * have to reference the function symbol instead:
		 */
M
Matt Helsley 已提交
113 114
		reloc->sym = find_symbol_containing(insn_sec, insn_off);
		if (!reloc->sym) {
115 116 117 118
			/*
			 * Hack alert.  This happens when we need to reference
			 * the NOP pad insn immediately after the function.
			 */
M
Matt Helsley 已提交
119
			reloc->sym = find_symbol_containing(insn_sec,
120 121
							   insn_off - 1);
		}
M
Matt Helsley 已提交
122
		if (!reloc->sym) {
123 124 125 126 127
			WARN("missing symbol for insn at offset 0x%lx\n",
			     insn_off);
			return -1;
		}

M
Matt Helsley 已提交
128
		reloc->addend = insn_off - reloc->sym->offset;
129 130
	}

M
Matt Helsley 已提交
131 132 133
	reloc->type = R_X86_64_PC32;
	reloc->offset = idx * sizeof(int);
	reloc->sec = ip_relocsec;
134

M
Matt Helsley 已提交
135
	elf_add_reloc(elf, reloc);
136 137 138 139 140 141 142

	return 0;
}

int create_orc_sections(struct objtool_file *file)
{
	struct instruction *insn, *prev_insn;
M
Matt Helsley 已提交
143
	struct section *sec, *u_sec, *ip_relocsec;
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	unsigned int idx;

	struct orc_entry empty = {
		.sp_reg = ORC_REG_UNDEFINED,
		.bp_reg  = ORC_REG_UNDEFINED,
		.type    = ORC_TYPE_CALL,
	};

	sec = find_section_by_name(file->elf, ".orc_unwind");
	if (sec) {
		WARN("file already has .orc_unwind section, skipping");
		return -1;
	}

	/* count the number of needed orcs */
	idx = 0;
	for_each_sec(file, sec) {
		if (!sec->text)
			continue;

		prev_insn = NULL;
		sec_for_each_insn(file, sec, insn) {
			if (!prev_insn ||
			    memcmp(&insn->orc, &prev_insn->orc,
				   sizeof(struct orc_entry))) {
				idx++;
			}
			prev_insn = insn;
		}

		/* section terminator */
		if (prev_insn)
			idx++;
	}
	if (!idx)
		return -1;


	/* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
183
	sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), idx);
184 185
	if (!sec)
		return -1;
186

187
	ip_relocsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
M
Matt Helsley 已提交
188
	if (!ip_relocsec)
189 190 191
		return -1;

	/* create .orc_unwind section */
192
	u_sec = elf_create_section(file->elf, ".orc_unwind", 0,
193 194 195 196 197 198 199 200 201 202 203 204 205
				   sizeof(struct orc_entry), idx);

	/* populate sections */
	idx = 0;
	for_each_sec(file, sec) {
		if (!sec->text)
			continue;

		prev_insn = NULL;
		sec_for_each_insn(file, sec, insn) {
			if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
						 sizeof(struct orc_entry))) {

M
Matt Helsley 已提交
206
				if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
207 208 209 210 211 212 213 214 215 216 217
						     insn->sec, insn->offset,
						     &insn->orc))
					return -1;

				idx++;
			}
			prev_insn = insn;
		}

		/* section terminator */
		if (prev_insn) {
M
Matt Helsley 已提交
218
			if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
219 220 221 222 223 224 225 226 227
					     prev_insn->sec,
					     prev_insn->offset + prev_insn->len,
					     &empty))
				return -1;

			idx++;
		}
	}

228
	if (elf_rebuild_reloc_section(file->elf, ip_relocsec))
229 230 231 232
		return -1;

	return 0;
}