xref: /linux/tools/objtool/elf.c (revision 1698872b5c772aebc5c43ca445cc0a79f12b9fcc)
1 /*
2  * elf.c - ELF access library
3  *
4  * Adapted from kpatch (https://github.com/dynup/kpatch):
5  * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
6  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 #include "elf.h"
31 #include "warn.h"
32 
33 struct section *find_section_by_name(struct elf *elf, const char *name)
34 {
35 	struct section *sec;
36 
37 	list_for_each_entry(sec, &elf->sections, list)
38 		if (!strcmp(sec->name, name))
39 			return sec;
40 
41 	return NULL;
42 }
43 
44 static struct section *find_section_by_index(struct elf *elf,
45 					     unsigned int idx)
46 {
47 	struct section *sec;
48 
49 	list_for_each_entry(sec, &elf->sections, list)
50 		if (sec->idx == idx)
51 			return sec;
52 
53 	return NULL;
54 }
55 
56 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
57 {
58 	struct section *sec;
59 	struct symbol *sym;
60 
61 	list_for_each_entry(sec, &elf->sections, list)
62 		list_for_each_entry(sym, &sec->symbol_list, list)
63 			if (sym->idx == idx)
64 				return sym;
65 
66 	return NULL;
67 }
68 
69 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
70 {
71 	struct symbol *sym;
72 
73 	list_for_each_entry(sym, &sec->symbol_list, list)
74 		if (sym->type != STT_SECTION &&
75 		    sym->offset == offset)
76 			return sym;
77 
78 	return NULL;
79 }
80 
81 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
82 				     unsigned int len)
83 {
84 	struct rela *rela;
85 
86 	if (!sec->rela)
87 		return NULL;
88 
89 	list_for_each_entry(rela, &sec->rela->rela_list, list)
90 		if (rela->offset >= offset && rela->offset < offset + len)
91 			return rela;
92 
93 	return NULL;
94 }
95 
96 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
97 {
98 	return find_rela_by_dest_range(sec, offset, 1);
99 }
100 
101 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
102 {
103 	struct symbol *func;
104 
105 	list_for_each_entry(func, &sec->symbol_list, list)
106 		if (func->type == STT_FUNC && offset >= func->offset &&
107 		    offset < func->offset + func->len)
108 			return func;
109 
110 	return NULL;
111 }
112 
113 static int read_sections(struct elf *elf)
114 {
115 	Elf_Scn *s = NULL;
116 	struct section *sec;
117 	size_t shstrndx, sections_nr;
118 	int i;
119 
120 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
121 		perror("elf_getshdrnum");
122 		return -1;
123 	}
124 
125 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
126 		perror("elf_getshdrstrndx");
127 		return -1;
128 	}
129 
130 	for (i = 0; i < sections_nr; i++) {
131 		sec = malloc(sizeof(*sec));
132 		if (!sec) {
133 			perror("malloc");
134 			return -1;
135 		}
136 		memset(sec, 0, sizeof(*sec));
137 
138 		INIT_LIST_HEAD(&sec->symbol_list);
139 		INIT_LIST_HEAD(&sec->rela_list);
140 
141 		list_add_tail(&sec->list, &elf->sections);
142 
143 		s = elf_getscn(elf->elf, i);
144 		if (!s) {
145 			perror("elf_getscn");
146 			return -1;
147 		}
148 
149 		sec->idx = elf_ndxscn(s);
150 
151 		if (!gelf_getshdr(s, &sec->sh)) {
152 			perror("gelf_getshdr");
153 			return -1;
154 		}
155 
156 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
157 		if (!sec->name) {
158 			perror("elf_strptr");
159 			return -1;
160 		}
161 
162 		sec->elf_data = elf_getdata(s, NULL);
163 		if (!sec->elf_data) {
164 			perror("elf_getdata");
165 			return -1;
166 		}
167 
168 		if (sec->elf_data->d_off != 0 ||
169 		    sec->elf_data->d_size != sec->sh.sh_size) {
170 			WARN("unexpected data attributes for %s", sec->name);
171 			return -1;
172 		}
173 
174 		sec->data = (unsigned long)sec->elf_data->d_buf;
175 		sec->len = sec->elf_data->d_size;
176 	}
177 
178 	/* sanity check, one more call to elf_nextscn() should return NULL */
179 	if (elf_nextscn(elf->elf, s)) {
180 		WARN("section entry mismatch");
181 		return -1;
182 	}
183 
184 	return 0;
185 }
186 
187 static int read_symbols(struct elf *elf)
188 {
189 	struct section *symtab;
190 	struct symbol *sym;
191 	struct list_head *entry, *tmp;
192 	int symbols_nr, i;
193 
194 	symtab = find_section_by_name(elf, ".symtab");
195 	if (!symtab) {
196 		WARN("missing symbol table");
197 		return -1;
198 	}
199 
200 	symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
201 
202 	for (i = 0; i < symbols_nr; i++) {
203 		sym = malloc(sizeof(*sym));
204 		if (!sym) {
205 			perror("malloc");
206 			return -1;
207 		}
208 		memset(sym, 0, sizeof(*sym));
209 
210 		sym->idx = i;
211 
212 		if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
213 			perror("gelf_getsym");
214 			goto err;
215 		}
216 
217 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
218 				       sym->sym.st_name);
219 		if (!sym->name) {
220 			perror("elf_strptr");
221 			goto err;
222 		}
223 
224 		sym->type = GELF_ST_TYPE(sym->sym.st_info);
225 		sym->bind = GELF_ST_BIND(sym->sym.st_info);
226 
227 		if (sym->sym.st_shndx > SHN_UNDEF &&
228 		    sym->sym.st_shndx < SHN_LORESERVE) {
229 			sym->sec = find_section_by_index(elf,
230 							 sym->sym.st_shndx);
231 			if (!sym->sec) {
232 				WARN("couldn't find section for symbol %s",
233 				     sym->name);
234 				goto err;
235 			}
236 			if (sym->type == STT_SECTION) {
237 				sym->name = sym->sec->name;
238 				sym->sec->sym = sym;
239 			}
240 		} else
241 			sym->sec = find_section_by_index(elf, 0);
242 
243 		sym->offset = sym->sym.st_value;
244 		sym->len = sym->sym.st_size;
245 
246 		/* sorted insert into a per-section list */
247 		entry = &sym->sec->symbol_list;
248 		list_for_each_prev(tmp, &sym->sec->symbol_list) {
249 			struct symbol *s;
250 
251 			s = list_entry(tmp, struct symbol, list);
252 
253 			if (sym->offset > s->offset) {
254 				entry = tmp;
255 				break;
256 			}
257 
258 			if (sym->offset == s->offset && sym->len >= s->len) {
259 				entry = tmp;
260 				break;
261 			}
262 		}
263 		list_add(&sym->list, entry);
264 	}
265 
266 	return 0;
267 
268 err:
269 	free(sym);
270 	return -1;
271 }
272 
273 static int read_relas(struct elf *elf)
274 {
275 	struct section *sec;
276 	struct rela *rela;
277 	int i;
278 	unsigned int symndx;
279 
280 	list_for_each_entry(sec, &elf->sections, list) {
281 		if (sec->sh.sh_type != SHT_RELA)
282 			continue;
283 
284 		sec->base = find_section_by_name(elf, sec->name + 5);
285 		if (!sec->base) {
286 			WARN("can't find base section for rela section %s",
287 			     sec->name);
288 			return -1;
289 		}
290 
291 		sec->base->rela = sec;
292 
293 		for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
294 			rela = malloc(sizeof(*rela));
295 			if (!rela) {
296 				perror("malloc");
297 				return -1;
298 			}
299 			memset(rela, 0, sizeof(*rela));
300 
301 			list_add_tail(&rela->list, &sec->rela_list);
302 
303 			if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
304 				perror("gelf_getrela");
305 				return -1;
306 			}
307 
308 			rela->type = GELF_R_TYPE(rela->rela.r_info);
309 			rela->addend = rela->rela.r_addend;
310 			rela->offset = rela->rela.r_offset;
311 			symndx = GELF_R_SYM(rela->rela.r_info);
312 			rela->sym = find_symbol_by_index(elf, symndx);
313 			if (!rela->sym) {
314 				WARN("can't find rela entry symbol %d for %s",
315 				     symndx, sec->name);
316 				return -1;
317 			}
318 		}
319 	}
320 
321 	return 0;
322 }
323 
324 struct elf *elf_open(const char *name)
325 {
326 	struct elf *elf;
327 
328 	elf_version(EV_CURRENT);
329 
330 	elf = malloc(sizeof(*elf));
331 	if (!elf) {
332 		perror("malloc");
333 		return NULL;
334 	}
335 	memset(elf, 0, sizeof(*elf));
336 
337 	INIT_LIST_HEAD(&elf->sections);
338 
339 	elf->name = strdup(name);
340 	if (!elf->name) {
341 		perror("strdup");
342 		goto err;
343 	}
344 
345 	elf->fd = open(name, O_RDONLY);
346 	if (elf->fd == -1) {
347 		perror("open");
348 		goto err;
349 	}
350 
351 	elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
352 	if (!elf->elf) {
353 		perror("elf_begin");
354 		goto err;
355 	}
356 
357 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
358 		perror("gelf_getehdr");
359 		goto err;
360 	}
361 
362 	if (read_sections(elf))
363 		goto err;
364 
365 	if (read_symbols(elf))
366 		goto err;
367 
368 	if (read_relas(elf))
369 		goto err;
370 
371 	return elf;
372 
373 err:
374 	elf_close(elf);
375 	return NULL;
376 }
377 
378 void elf_close(struct elf *elf)
379 {
380 	struct section *sec, *tmpsec;
381 	struct symbol *sym, *tmpsym;
382 	struct rela *rela, *tmprela;
383 
384 	list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
385 		list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
386 			list_del(&sym->list);
387 			free(sym);
388 		}
389 		list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
390 			list_del(&rela->list);
391 			free(rela);
392 		}
393 		list_del(&sec->list);
394 		free(sec);
395 	}
396 	if (elf->name)
397 		free(elf->name);
398 	if (elf->fd > 0)
399 		close(elf->fd);
400 	if (elf->elf)
401 		elf_end(elf->elf);
402 	free(elf);
403 }
404