xref: /openbmc/linux/tools/objtool/elf.c (revision 78700c0a)
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 		hash_for_each_possible(sec->symbol_hash, sym, hash, idx)
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 	unsigned long o;
86 
87 	if (!sec->rela)
88 		return NULL;
89 
90 	for (o = offset; o < offset + len; o++)
91 		hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
92 			if (rela->offset == o)
93 				return rela;
94 
95 	return NULL;
96 }
97 
98 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
99 {
100 	return find_rela_by_dest_range(sec, offset, 1);
101 }
102 
103 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
104 {
105 	struct symbol *func;
106 
107 	list_for_each_entry(func, &sec->symbol_list, list)
108 		if (func->type == STT_FUNC && offset >= func->offset &&
109 		    offset < func->offset + func->len)
110 			return func;
111 
112 	return NULL;
113 }
114 
115 static int read_sections(struct elf *elf)
116 {
117 	Elf_Scn *s = NULL;
118 	struct section *sec;
119 	size_t shstrndx, sections_nr;
120 	int i;
121 
122 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
123 		perror("elf_getshdrnum");
124 		return -1;
125 	}
126 
127 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
128 		perror("elf_getshdrstrndx");
129 		return -1;
130 	}
131 
132 	for (i = 0; i < sections_nr; i++) {
133 		sec = malloc(sizeof(*sec));
134 		if (!sec) {
135 			perror("malloc");
136 			return -1;
137 		}
138 		memset(sec, 0, sizeof(*sec));
139 
140 		INIT_LIST_HEAD(&sec->symbol_list);
141 		INIT_LIST_HEAD(&sec->rela_list);
142 		hash_init(sec->rela_hash);
143 		hash_init(sec->symbol_hash);
144 
145 		list_add_tail(&sec->list, &elf->sections);
146 
147 		s = elf_getscn(elf->elf, i);
148 		if (!s) {
149 			perror("elf_getscn");
150 			return -1;
151 		}
152 
153 		sec->idx = elf_ndxscn(s);
154 
155 		if (!gelf_getshdr(s, &sec->sh)) {
156 			perror("gelf_getshdr");
157 			return -1;
158 		}
159 
160 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
161 		if (!sec->name) {
162 			perror("elf_strptr");
163 			return -1;
164 		}
165 
166 		sec->elf_data = elf_getdata(s, NULL);
167 		if (!sec->elf_data) {
168 			perror("elf_getdata");
169 			return -1;
170 		}
171 
172 		if (sec->elf_data->d_off != 0 ||
173 		    sec->elf_data->d_size != sec->sh.sh_size) {
174 			WARN("unexpected data attributes for %s", sec->name);
175 			return -1;
176 		}
177 
178 		sec->data = (unsigned long)sec->elf_data->d_buf;
179 		sec->len = sec->elf_data->d_size;
180 	}
181 
182 	/* sanity check, one more call to elf_nextscn() should return NULL */
183 	if (elf_nextscn(elf->elf, s)) {
184 		WARN("section entry mismatch");
185 		return -1;
186 	}
187 
188 	return 0;
189 }
190 
191 static int read_symbols(struct elf *elf)
192 {
193 	struct section *symtab;
194 	struct symbol *sym;
195 	struct list_head *entry, *tmp;
196 	int symbols_nr, i;
197 
198 	symtab = find_section_by_name(elf, ".symtab");
199 	if (!symtab) {
200 		WARN("missing symbol table");
201 		return -1;
202 	}
203 
204 	symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
205 
206 	for (i = 0; i < symbols_nr; i++) {
207 		sym = malloc(sizeof(*sym));
208 		if (!sym) {
209 			perror("malloc");
210 			return -1;
211 		}
212 		memset(sym, 0, sizeof(*sym));
213 
214 		sym->idx = i;
215 
216 		if (!gelf_getsym(symtab->elf_data, i, &sym->sym)) {
217 			perror("gelf_getsym");
218 			goto err;
219 		}
220 
221 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
222 				       sym->sym.st_name);
223 		if (!sym->name) {
224 			perror("elf_strptr");
225 			goto err;
226 		}
227 
228 		sym->type = GELF_ST_TYPE(sym->sym.st_info);
229 		sym->bind = GELF_ST_BIND(sym->sym.st_info);
230 
231 		if (sym->sym.st_shndx > SHN_UNDEF &&
232 		    sym->sym.st_shndx < SHN_LORESERVE) {
233 			sym->sec = find_section_by_index(elf,
234 							 sym->sym.st_shndx);
235 			if (!sym->sec) {
236 				WARN("couldn't find section for symbol %s",
237 				     sym->name);
238 				goto err;
239 			}
240 			if (sym->type == STT_SECTION) {
241 				sym->name = sym->sec->name;
242 				sym->sec->sym = sym;
243 			}
244 		} else
245 			sym->sec = find_section_by_index(elf, 0);
246 
247 		sym->offset = sym->sym.st_value;
248 		sym->len = sym->sym.st_size;
249 
250 		/* sorted insert into a per-section list */
251 		entry = &sym->sec->symbol_list;
252 		list_for_each_prev(tmp, &sym->sec->symbol_list) {
253 			struct symbol *s;
254 
255 			s = list_entry(tmp, struct symbol, list);
256 
257 			if (sym->offset > s->offset) {
258 				entry = tmp;
259 				break;
260 			}
261 
262 			if (sym->offset == s->offset && sym->len >= s->len) {
263 				entry = tmp;
264 				break;
265 			}
266 		}
267 		list_add(&sym->list, entry);
268 		hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
269 	}
270 
271 	return 0;
272 
273 err:
274 	free(sym);
275 	return -1;
276 }
277 
278 static int read_relas(struct elf *elf)
279 {
280 	struct section *sec;
281 	struct rela *rela;
282 	int i;
283 	unsigned int symndx;
284 
285 	list_for_each_entry(sec, &elf->sections, list) {
286 		if (sec->sh.sh_type != SHT_RELA)
287 			continue;
288 
289 		sec->base = find_section_by_name(elf, sec->name + 5);
290 		if (!sec->base) {
291 			WARN("can't find base section for rela section %s",
292 			     sec->name);
293 			return -1;
294 		}
295 
296 		sec->base->rela = sec;
297 
298 		for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
299 			rela = malloc(sizeof(*rela));
300 			if (!rela) {
301 				perror("malloc");
302 				return -1;
303 			}
304 			memset(rela, 0, sizeof(*rela));
305 
306 			if (!gelf_getrela(sec->elf_data, i, &rela->rela)) {
307 				perror("gelf_getrela");
308 				return -1;
309 			}
310 
311 			rela->type = GELF_R_TYPE(rela->rela.r_info);
312 			rela->addend = rela->rela.r_addend;
313 			rela->offset = rela->rela.r_offset;
314 			symndx = GELF_R_SYM(rela->rela.r_info);
315 			rela->sym = find_symbol_by_index(elf, symndx);
316 			if (!rela->sym) {
317 				WARN("can't find rela entry symbol %d for %s",
318 				     symndx, sec->name);
319 				return -1;
320 			}
321 
322 			list_add_tail(&rela->list, &sec->rela_list);
323 			hash_add(sec->rela_hash, &rela->hash, rela->offset);
324 
325 		}
326 	}
327 
328 	return 0;
329 }
330 
331 struct elf *elf_open(const char *name)
332 {
333 	struct elf *elf;
334 
335 	elf_version(EV_CURRENT);
336 
337 	elf = malloc(sizeof(*elf));
338 	if (!elf) {
339 		perror("malloc");
340 		return NULL;
341 	}
342 	memset(elf, 0, sizeof(*elf));
343 
344 	INIT_LIST_HEAD(&elf->sections);
345 
346 	elf->name = strdup(name);
347 	if (!elf->name) {
348 		perror("strdup");
349 		goto err;
350 	}
351 
352 	elf->fd = open(name, O_RDONLY);
353 	if (elf->fd == -1) {
354 		perror("open");
355 		goto err;
356 	}
357 
358 	elf->elf = elf_begin(elf->fd, ELF_C_READ_MMAP, NULL);
359 	if (!elf->elf) {
360 		perror("elf_begin");
361 		goto err;
362 	}
363 
364 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
365 		perror("gelf_getehdr");
366 		goto err;
367 	}
368 
369 	if (read_sections(elf))
370 		goto err;
371 
372 	if (read_symbols(elf))
373 		goto err;
374 
375 	if (read_relas(elf))
376 		goto err;
377 
378 	return elf;
379 
380 err:
381 	elf_close(elf);
382 	return NULL;
383 }
384 
385 void elf_close(struct elf *elf)
386 {
387 	struct section *sec, *tmpsec;
388 	struct symbol *sym, *tmpsym;
389 	struct rela *rela, *tmprela;
390 
391 	list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
392 		list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
393 			list_del(&sym->list);
394 			hash_del(&sym->hash);
395 			free(sym);
396 		}
397 		list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
398 			list_del(&rela->list);
399 			hash_del(&rela->hash);
400 			free(rela);
401 		}
402 		list_del(&sec->list);
403 		free(sec);
404 	}
405 	if (elf->name)
406 		free(elf->name);
407 	if (elf->fd > 0)
408 		close(elf->fd);
409 	if (elf->elf)
410 		elf_end(elf->elf);
411 	free(elf);
412 }
413