xref: /linux/tools/objtool/elf.c (revision 74ce1896c6c65b2f8cccbf59162d542988835835)
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 symbol *find_symbol_containing(struct section *sec, unsigned long offset)
82 {
83 	struct symbol *sym;
84 
85 	list_for_each_entry(sym, &sec->symbol_list, list)
86 		if (sym->type != STT_SECTION &&
87 		    offset >= sym->offset && offset < sym->offset + sym->len)
88 			return sym;
89 
90 	return NULL;
91 }
92 
93 struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset,
94 				     unsigned int len)
95 {
96 	struct rela *rela;
97 	unsigned long o;
98 
99 	if (!sec->rela)
100 		return NULL;
101 
102 	for (o = offset; o < offset + len; o++)
103 		hash_for_each_possible(sec->rela->rela_hash, rela, hash, o)
104 			if (rela->offset == o)
105 				return rela;
106 
107 	return NULL;
108 }
109 
110 struct rela *find_rela_by_dest(struct section *sec, unsigned long offset)
111 {
112 	return find_rela_by_dest_range(sec, offset, 1);
113 }
114 
115 struct symbol *find_containing_func(struct section *sec, unsigned long offset)
116 {
117 	struct symbol *func;
118 
119 	list_for_each_entry(func, &sec->symbol_list, list)
120 		if (func->type == STT_FUNC && offset >= func->offset &&
121 		    offset < func->offset + func->len)
122 			return func;
123 
124 	return NULL;
125 }
126 
127 static int read_sections(struct elf *elf)
128 {
129 	Elf_Scn *s = NULL;
130 	struct section *sec;
131 	size_t shstrndx, sections_nr;
132 	int i;
133 
134 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
135 		WARN_ELF("elf_getshdrnum");
136 		return -1;
137 	}
138 
139 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
140 		WARN_ELF("elf_getshdrstrndx");
141 		return -1;
142 	}
143 
144 	for (i = 0; i < sections_nr; i++) {
145 		sec = malloc(sizeof(*sec));
146 		if (!sec) {
147 			perror("malloc");
148 			return -1;
149 		}
150 		memset(sec, 0, sizeof(*sec));
151 
152 		INIT_LIST_HEAD(&sec->symbol_list);
153 		INIT_LIST_HEAD(&sec->rela_list);
154 		hash_init(sec->rela_hash);
155 		hash_init(sec->symbol_hash);
156 
157 		list_add_tail(&sec->list, &elf->sections);
158 
159 		s = elf_getscn(elf->elf, i);
160 		if (!s) {
161 			WARN_ELF("elf_getscn");
162 			return -1;
163 		}
164 
165 		sec->idx = elf_ndxscn(s);
166 
167 		if (!gelf_getshdr(s, &sec->sh)) {
168 			WARN_ELF("gelf_getshdr");
169 			return -1;
170 		}
171 
172 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
173 		if (!sec->name) {
174 			WARN_ELF("elf_strptr");
175 			return -1;
176 		}
177 
178 		sec->data = elf_getdata(s, NULL);
179 		if (!sec->data) {
180 			WARN_ELF("elf_getdata");
181 			return -1;
182 		}
183 
184 		if (sec->data->d_off != 0 ||
185 		    sec->data->d_size != sec->sh.sh_size) {
186 			WARN("unexpected data attributes for %s", sec->name);
187 			return -1;
188 		}
189 
190 		sec->len = sec->data->d_size;
191 	}
192 
193 	/* sanity check, one more call to elf_nextscn() should return NULL */
194 	if (elf_nextscn(elf->elf, s)) {
195 		WARN("section entry mismatch");
196 		return -1;
197 	}
198 
199 	return 0;
200 }
201 
202 static int read_symbols(struct elf *elf)
203 {
204 	struct section *symtab;
205 	struct symbol *sym;
206 	struct list_head *entry, *tmp;
207 	int symbols_nr, i;
208 
209 	symtab = find_section_by_name(elf, ".symtab");
210 	if (!symtab) {
211 		WARN("missing symbol table");
212 		return -1;
213 	}
214 
215 	symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
216 
217 	for (i = 0; i < symbols_nr; i++) {
218 		sym = malloc(sizeof(*sym));
219 		if (!sym) {
220 			perror("malloc");
221 			return -1;
222 		}
223 		memset(sym, 0, sizeof(*sym));
224 
225 		sym->idx = i;
226 
227 		if (!gelf_getsym(symtab->data, i, &sym->sym)) {
228 			WARN_ELF("gelf_getsym");
229 			goto err;
230 		}
231 
232 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
233 				       sym->sym.st_name);
234 		if (!sym->name) {
235 			WARN_ELF("elf_strptr");
236 			goto err;
237 		}
238 
239 		sym->type = GELF_ST_TYPE(sym->sym.st_info);
240 		sym->bind = GELF_ST_BIND(sym->sym.st_info);
241 
242 		if (sym->sym.st_shndx > SHN_UNDEF &&
243 		    sym->sym.st_shndx < SHN_LORESERVE) {
244 			sym->sec = find_section_by_index(elf,
245 							 sym->sym.st_shndx);
246 			if (!sym->sec) {
247 				WARN("couldn't find section for symbol %s",
248 				     sym->name);
249 				goto err;
250 			}
251 			if (sym->type == STT_SECTION) {
252 				sym->name = sym->sec->name;
253 				sym->sec->sym = sym;
254 			}
255 		} else
256 			sym->sec = find_section_by_index(elf, 0);
257 
258 		sym->offset = sym->sym.st_value;
259 		sym->len = sym->sym.st_size;
260 
261 		/* sorted insert into a per-section list */
262 		entry = &sym->sec->symbol_list;
263 		list_for_each_prev(tmp, &sym->sec->symbol_list) {
264 			struct symbol *s;
265 
266 			s = list_entry(tmp, struct symbol, list);
267 
268 			if (sym->offset > s->offset) {
269 				entry = tmp;
270 				break;
271 			}
272 
273 			if (sym->offset == s->offset && sym->len >= s->len) {
274 				entry = tmp;
275 				break;
276 			}
277 		}
278 		list_add(&sym->list, entry);
279 		hash_add(sym->sec->symbol_hash, &sym->hash, sym->idx);
280 	}
281 
282 	return 0;
283 
284 err:
285 	free(sym);
286 	return -1;
287 }
288 
289 static int read_relas(struct elf *elf)
290 {
291 	struct section *sec;
292 	struct rela *rela;
293 	int i;
294 	unsigned int symndx;
295 
296 	list_for_each_entry(sec, &elf->sections, list) {
297 		if (sec->sh.sh_type != SHT_RELA)
298 			continue;
299 
300 		sec->base = find_section_by_name(elf, sec->name + 5);
301 		if (!sec->base) {
302 			WARN("can't find base section for rela section %s",
303 			     sec->name);
304 			return -1;
305 		}
306 
307 		sec->base->rela = sec;
308 
309 		for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
310 			rela = malloc(sizeof(*rela));
311 			if (!rela) {
312 				perror("malloc");
313 				return -1;
314 			}
315 			memset(rela, 0, sizeof(*rela));
316 
317 			if (!gelf_getrela(sec->data, i, &rela->rela)) {
318 				WARN_ELF("gelf_getrela");
319 				return -1;
320 			}
321 
322 			rela->type = GELF_R_TYPE(rela->rela.r_info);
323 			rela->addend = rela->rela.r_addend;
324 			rela->offset = rela->rela.r_offset;
325 			symndx = GELF_R_SYM(rela->rela.r_info);
326 			rela->sym = find_symbol_by_index(elf, symndx);
327 			if (!rela->sym) {
328 				WARN("can't find rela entry symbol %d for %s",
329 				     symndx, sec->name);
330 				return -1;
331 			}
332 
333 			list_add_tail(&rela->list, &sec->rela_list);
334 			hash_add(sec->rela_hash, &rela->hash, rela->offset);
335 
336 		}
337 	}
338 
339 	return 0;
340 }
341 
342 struct elf *elf_open(const char *name, int flags)
343 {
344 	struct elf *elf;
345 	Elf_Cmd cmd;
346 
347 	elf_version(EV_CURRENT);
348 
349 	elf = malloc(sizeof(*elf));
350 	if (!elf) {
351 		perror("malloc");
352 		return NULL;
353 	}
354 	memset(elf, 0, sizeof(*elf));
355 
356 	INIT_LIST_HEAD(&elf->sections);
357 
358 	elf->fd = open(name, flags);
359 	if (elf->fd == -1) {
360 		perror("open");
361 		goto err;
362 	}
363 
364 	if ((flags & O_ACCMODE) == O_RDONLY)
365 		cmd = ELF_C_READ_MMAP;
366 	else if ((flags & O_ACCMODE) == O_RDWR)
367 		cmd = ELF_C_RDWR;
368 	else /* O_WRONLY */
369 		cmd = ELF_C_WRITE;
370 
371 	elf->elf = elf_begin(elf->fd, cmd, NULL);
372 	if (!elf->elf) {
373 		WARN_ELF("elf_begin");
374 		goto err;
375 	}
376 
377 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
378 		WARN_ELF("gelf_getehdr");
379 		goto err;
380 	}
381 
382 	if (read_sections(elf))
383 		goto err;
384 
385 	if (read_symbols(elf))
386 		goto err;
387 
388 	if (read_relas(elf))
389 		goto err;
390 
391 	return elf;
392 
393 err:
394 	elf_close(elf);
395 	return NULL;
396 }
397 
398 struct section *elf_create_section(struct elf *elf, const char *name,
399 				   size_t entsize, int nr)
400 {
401 	struct section *sec, *shstrtab;
402 	size_t size = entsize * nr;
403 	struct Elf_Scn *s;
404 	Elf_Data *data;
405 
406 	sec = malloc(sizeof(*sec));
407 	if (!sec) {
408 		perror("malloc");
409 		return NULL;
410 	}
411 	memset(sec, 0, sizeof(*sec));
412 
413 	INIT_LIST_HEAD(&sec->symbol_list);
414 	INIT_LIST_HEAD(&sec->rela_list);
415 	hash_init(sec->rela_hash);
416 	hash_init(sec->symbol_hash);
417 
418 	list_add_tail(&sec->list, &elf->sections);
419 
420 	s = elf_newscn(elf->elf);
421 	if (!s) {
422 		WARN_ELF("elf_newscn");
423 		return NULL;
424 	}
425 
426 	sec->name = strdup(name);
427 	if (!sec->name) {
428 		perror("strdup");
429 		return NULL;
430 	}
431 
432 	sec->idx = elf_ndxscn(s);
433 	sec->len = size;
434 	sec->changed = true;
435 
436 	sec->data = elf_newdata(s);
437 	if (!sec->data) {
438 		WARN_ELF("elf_newdata");
439 		return NULL;
440 	}
441 
442 	sec->data->d_size = size;
443 	sec->data->d_align = 1;
444 
445 	if (size) {
446 		sec->data->d_buf = malloc(size);
447 		if (!sec->data->d_buf) {
448 			perror("malloc");
449 			return NULL;
450 		}
451 		memset(sec->data->d_buf, 0, size);
452 	}
453 
454 	if (!gelf_getshdr(s, &sec->sh)) {
455 		WARN_ELF("gelf_getshdr");
456 		return NULL;
457 	}
458 
459 	sec->sh.sh_size = size;
460 	sec->sh.sh_entsize = entsize;
461 	sec->sh.sh_type = SHT_PROGBITS;
462 	sec->sh.sh_addralign = 1;
463 	sec->sh.sh_flags = SHF_ALLOC;
464 
465 
466 	/* Add section name to .shstrtab */
467 	shstrtab = find_section_by_name(elf, ".shstrtab");
468 	if (!shstrtab) {
469 		WARN("can't find .shstrtab section");
470 		return NULL;
471 	}
472 
473 	s = elf_getscn(elf->elf, shstrtab->idx);
474 	if (!s) {
475 		WARN_ELF("elf_getscn");
476 		return NULL;
477 	}
478 
479 	data = elf_newdata(s);
480 	if (!data) {
481 		WARN_ELF("elf_newdata");
482 		return NULL;
483 	}
484 
485 	data->d_buf = sec->name;
486 	data->d_size = strlen(name) + 1;
487 	data->d_align = 1;
488 
489 	sec->sh.sh_name = shstrtab->len;
490 
491 	shstrtab->len += strlen(name) + 1;
492 	shstrtab->changed = true;
493 
494 	return sec;
495 }
496 
497 struct section *elf_create_rela_section(struct elf *elf, struct section *base)
498 {
499 	char *relaname;
500 	struct section *sec;
501 
502 	relaname = malloc(strlen(base->name) + strlen(".rela") + 1);
503 	if (!relaname) {
504 		perror("malloc");
505 		return NULL;
506 	}
507 	strcpy(relaname, ".rela");
508 	strcat(relaname, base->name);
509 
510 	sec = elf_create_section(elf, relaname, sizeof(GElf_Rela), 0);
511 	if (!sec)
512 		return NULL;
513 
514 	base->rela = sec;
515 	sec->base = base;
516 
517 	sec->sh.sh_type = SHT_RELA;
518 	sec->sh.sh_addralign = 8;
519 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
520 	sec->sh.sh_info = base->idx;
521 	sec->sh.sh_flags = SHF_INFO_LINK;
522 
523 	return sec;
524 }
525 
526 int elf_rebuild_rela_section(struct section *sec)
527 {
528 	struct rela *rela;
529 	int nr, idx = 0, size;
530 	GElf_Rela *relas;
531 
532 	nr = 0;
533 	list_for_each_entry(rela, &sec->rela_list, list)
534 		nr++;
535 
536 	size = nr * sizeof(*relas);
537 	relas = malloc(size);
538 	if (!relas) {
539 		perror("malloc");
540 		return -1;
541 	}
542 
543 	sec->data->d_buf = relas;
544 	sec->data->d_size = size;
545 
546 	sec->sh.sh_size = size;
547 
548 	idx = 0;
549 	list_for_each_entry(rela, &sec->rela_list, list) {
550 		relas[idx].r_offset = rela->offset;
551 		relas[idx].r_addend = rela->addend;
552 		relas[idx].r_info = GELF_R_INFO(rela->sym->idx, rela->type);
553 		idx++;
554 	}
555 
556 	return 0;
557 }
558 
559 int elf_write(struct elf *elf)
560 {
561 	struct section *sec;
562 	Elf_Scn *s;
563 
564 	list_for_each_entry(sec, &elf->sections, list) {
565 		if (sec->changed) {
566 			s = elf_getscn(elf->elf, sec->idx);
567 			if (!s) {
568 				WARN_ELF("elf_getscn");
569 				return -1;
570 			}
571 			if (!gelf_update_shdr (s, &sec->sh)) {
572 				WARN_ELF("gelf_update_shdr");
573 				return -1;
574 			}
575 		}
576 	}
577 
578 	if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
579 		WARN_ELF("elf_update");
580 		return -1;
581 	}
582 
583 	return 0;
584 }
585 
586 void elf_close(struct elf *elf)
587 {
588 	struct section *sec, *tmpsec;
589 	struct symbol *sym, *tmpsym;
590 	struct rela *rela, *tmprela;
591 
592 	if (elf->elf)
593 		elf_end(elf->elf);
594 
595 	if (elf->fd > 0)
596 		close(elf->fd);
597 
598 	list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
599 		list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
600 			list_del(&sym->list);
601 			hash_del(&sym->hash);
602 			free(sym);
603 		}
604 		list_for_each_entry_safe(rela, tmprela, &sec->rela_list, list) {
605 			list_del(&rela->list);
606 			hash_del(&rela->hash);
607 			free(rela);
608 		}
609 		list_del(&sec->list);
610 		free(sec);
611 	}
612 
613 	free(elf);
614 }
615