• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #include "orc.h"
22 #include "check.h"
23 #include "warn.h"
24 
create_orc(struct objtool_file * file)25 int create_orc(struct objtool_file *file)
26 {
27 	struct instruction *insn;
28 
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];
33 
34 		orc->end = insn->state.end;
35 
36 		if (cfa->base == CFI_UNDEFINED) {
37 			orc->sp_reg = ORC_REG_UNDEFINED;
38 			continue;
39 		}
40 
41 		switch (cfa->base) {
42 		case CFI_SP:
43 			orc->sp_reg = ORC_REG_SP;
44 			break;
45 		case CFI_SP_INDIRECT:
46 			orc->sp_reg = ORC_REG_SP_INDIRECT;
47 			break;
48 		case CFI_BP:
49 			orc->sp_reg = ORC_REG_BP;
50 			break;
51 		case CFI_BP_INDIRECT:
52 			orc->sp_reg = ORC_REG_BP_INDIRECT;
53 			break;
54 		case CFI_R10:
55 			orc->sp_reg = ORC_REG_R10;
56 			break;
57 		case CFI_R13:
58 			orc->sp_reg = ORC_REG_R13;
59 			break;
60 		case CFI_DI:
61 			orc->sp_reg = ORC_REG_DI;
62 			break;
63 		case CFI_DX:
64 			orc->sp_reg = ORC_REG_DX;
65 			break;
66 		default:
67 			WARN_FUNC("unknown CFA base reg %d",
68 				  insn->sec, insn->offset, cfa->base);
69 			return -1;
70 		}
71 
72 		switch(bp->base) {
73 		case CFI_UNDEFINED:
74 			orc->bp_reg = ORC_REG_UNDEFINED;
75 			break;
76 		case CFI_CFA:
77 			orc->bp_reg = ORC_REG_PREV_SP;
78 			break;
79 		case CFI_BP:
80 			orc->bp_reg = ORC_REG_BP;
81 			break;
82 		default:
83 			WARN_FUNC("unknown BP base reg %d",
84 				  insn->sec, insn->offset, bp->base);
85 			return -1;
86 		}
87 
88 		orc->sp_offset = cfa->offset;
89 		orc->bp_offset = bp->offset;
90 		orc->type = insn->state.type;
91 	}
92 
93 	return 0;
94 }
95 
create_orc_entry(struct section * u_sec,struct section * ip_relasec,unsigned int idx,struct section * insn_sec,unsigned long insn_off,struct orc_entry * o)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)
99 {
100 	struct orc_entry *orc;
101 	struct rela *rela;
102 
103 	/* populate ORC data */
104 	orc = (struct orc_entry *)u_sec->data->d_buf + idx;
105 	memcpy(orc, o, sizeof(*orc));
106 
107 	/* populate rela for ip */
108 	rela = malloc(sizeof(*rela));
109 	if (!rela) {
110 		perror("malloc");
111 		return -1;
112 	}
113 	memset(rela, 0, sizeof(*rela));
114 
115 	if (insn_sec->sym) {
116 		rela->sym = insn_sec->sym;
117 		rela->addend = insn_off;
118 	} else {
119 		/*
120 		 * The Clang assembler doesn't produce section symbols, so we
121 		 * have to reference the function symbol instead:
122 		 */
123 		rela->sym = find_symbol_containing(insn_sec, insn_off);
124 		if (!rela->sym) {
125 			/*
126 			 * Hack alert.  This happens when we need to reference
127 			 * the NOP pad insn immediately after the function.
128 			 */
129 			rela->sym = find_symbol_containing(insn_sec,
130 							   insn_off - 1);
131 		}
132 		if (!rela->sym) {
133 			WARN("missing symbol for insn at offset 0x%lx\n",
134 			     insn_off);
135 			return -1;
136 		}
137 
138 		rela->addend = insn_off - rela->sym->offset;
139 	}
140 
141 	rela->type = R_X86_64_PC32;
142 	rela->offset = idx * sizeof(int);
143 
144 	list_add_tail(&rela->list, &ip_relasec->rela_list);
145 	hash_add(ip_relasec->rela_hash, &rela->hash, rela->offset);
146 
147 	return 0;
148 }
149 
create_orc_sections(struct objtool_file * file)150 int create_orc_sections(struct objtool_file *file)
151 {
152 	struct instruction *insn, *prev_insn;
153 	struct section *sec, *u_sec, *ip_relasec;
154 	unsigned int idx;
155 
156 	struct orc_entry empty = {
157 		.sp_reg = ORC_REG_UNDEFINED,
158 		.bp_reg  = ORC_REG_UNDEFINED,
159 		.type    = ORC_TYPE_CALL,
160 	};
161 
162 	sec = find_section_by_name(file->elf, ".orc_unwind");
163 	if (sec) {
164 		WARN("file already has .orc_unwind section, skipping");
165 		return -1;
166 	}
167 
168 	/* count the number of needed orcs */
169 	idx = 0;
170 	for_each_sec(file, sec) {
171 		if (!sec->text)
172 			continue;
173 
174 		prev_insn = NULL;
175 		sec_for_each_insn(file, sec, insn) {
176 			if (!prev_insn ||
177 			    memcmp(&insn->orc, &prev_insn->orc,
178 				   sizeof(struct orc_entry))) {
179 				idx++;
180 			}
181 			prev_insn = insn;
182 		}
183 
184 		/* section terminator */
185 		if (prev_insn)
186 			idx++;
187 	}
188 	if (!idx)
189 		return -1;
190 
191 
192 	/* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
193 	sec = elf_create_section(file->elf, ".orc_unwind_ip", sizeof(int), idx);
194 	if (!sec)
195 		return -1;
196 
197 	ip_relasec = elf_create_rela_section(file->elf, sec);
198 	if (!ip_relasec)
199 		return -1;
200 
201 	/* create .orc_unwind section */
202 	u_sec = elf_create_section(file->elf, ".orc_unwind",
203 				   sizeof(struct orc_entry), idx);
204 
205 	/* populate sections */
206 	idx = 0;
207 	for_each_sec(file, sec) {
208 		if (!sec->text)
209 			continue;
210 
211 		prev_insn = NULL;
212 		sec_for_each_insn(file, sec, insn) {
213 			if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
214 						 sizeof(struct orc_entry))) {
215 
216 				if (create_orc_entry(u_sec, ip_relasec, idx,
217 						     insn->sec, insn->offset,
218 						     &insn->orc))
219 					return -1;
220 
221 				idx++;
222 			}
223 			prev_insn = insn;
224 		}
225 
226 		/* section terminator */
227 		if (prev_insn) {
228 			if (create_orc_entry(u_sec, ip_relasec, idx,
229 					     prev_insn->sec,
230 					     prev_insn->offset + prev_insn->len,
231 					     &empty))
232 				return -1;
233 
234 			idx++;
235 		}
236 	}
237 
238 	if (elf_rebuild_rela_section(ip_relasec))
239 		return -1;
240 
241 	return 0;
242 }
243