• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * elf.c - ELF access library
4  *
5  * Adapted from kpatch (https://github.com/dynup/kpatch):
6  * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
7  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
8  */
9 
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <errno.h>
18 #include "builtin.h"
19 
20 #include "elf.h"
21 #include "warn.h"
22 
23 #define MAX_NAME_LEN 128
24 
str_hash(const char * str)25 static inline u32 str_hash(const char *str)
26 {
27 	return jhash(str, strlen(str), 0);
28 }
29 
elf_hash_bits(void)30 static inline int elf_hash_bits(void)
31 {
32 	return vmlinux ? ELF_HASH_BITS : 16;
33 }
34 
35 #define elf_hash_add(hashtable, node, key) \
36 	hlist_add_head(node, &hashtable[hash_min(key, elf_hash_bits())])
37 
elf_hash_init(struct hlist_head * table)38 static void elf_hash_init(struct hlist_head *table)
39 {
40 	__hash_init(table, 1U << elf_hash_bits());
41 }
42 
43 #define elf_hash_for_each_possible(name, obj, member, key)			\
44 	hlist_for_each_entry(obj, &name[hash_min(key, elf_hash_bits())], member)
45 
rb_add(struct rb_root * tree,struct rb_node * node,int (* cmp)(struct rb_node *,const struct rb_node *))46 static void rb_add(struct rb_root *tree, struct rb_node *node,
47 		   int (*cmp)(struct rb_node *, const struct rb_node *))
48 {
49 	struct rb_node **link = &tree->rb_node;
50 	struct rb_node *parent = NULL;
51 
52 	while (*link) {
53 		parent = *link;
54 		if (cmp(node, parent) < 0)
55 			link = &parent->rb_left;
56 		else
57 			link = &parent->rb_right;
58 	}
59 
60 	rb_link_node(node, parent, link);
61 	rb_insert_color(node, tree);
62 }
63 
rb_find_first(const struct rb_root * tree,const void * key,int (* cmp)(const void * key,const struct rb_node *))64 static struct rb_node *rb_find_first(const struct rb_root *tree, const void *key,
65 			       int (*cmp)(const void *key, const struct rb_node *))
66 {
67 	struct rb_node *node = tree->rb_node;
68 	struct rb_node *match = NULL;
69 
70 	while (node) {
71 		int c = cmp(key, node);
72 		if (c <= 0) {
73 			if (!c)
74 				match = node;
75 			node = node->rb_left;
76 		} else if (c > 0) {
77 			node = node->rb_right;
78 		}
79 	}
80 
81 	return match;
82 }
83 
rb_next_match(struct rb_node * node,const void * key,int (* cmp)(const void * key,const struct rb_node *))84 static struct rb_node *rb_next_match(struct rb_node *node, const void *key,
85 				    int (*cmp)(const void *key, const struct rb_node *))
86 {
87 	node = rb_next(node);
88 	if (node && cmp(key, node))
89 		node = NULL;
90 	return node;
91 }
92 
93 #define rb_for_each(tree, node, key, cmp) \
94 	for ((node) = rb_find_first((tree), (key), (cmp)); \
95 	     (node); (node) = rb_next_match((node), (key), (cmp)))
96 
symbol_to_offset(struct rb_node * a,const struct rb_node * b)97 static int symbol_to_offset(struct rb_node *a, const struct rb_node *b)
98 {
99 	struct symbol *sa = rb_entry(a, struct symbol, node);
100 	struct symbol *sb = rb_entry(b, struct symbol, node);
101 
102 	if (sa->offset < sb->offset)
103 		return -1;
104 	if (sa->offset > sb->offset)
105 		return 1;
106 
107 	if (sa->len < sb->len)
108 		return -1;
109 	if (sa->len > sb->len)
110 		return 1;
111 
112 	sa->alias = sb;
113 
114 	return 0;
115 }
116 
symbol_by_offset(const void * key,const struct rb_node * node)117 static int symbol_by_offset(const void *key, const struct rb_node *node)
118 {
119 	const struct symbol *s = rb_entry(node, struct symbol, node);
120 	const unsigned long *o = key;
121 
122 	if (*o < s->offset)
123 		return -1;
124 	if (*o >= s->offset + s->len)
125 		return 1;
126 
127 	return 0;
128 }
129 
find_section_by_name(const struct elf * elf,const char * name)130 struct section *find_section_by_name(const struct elf *elf, const char *name)
131 {
132 	struct section *sec;
133 
134 	elf_hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
135 		if (!strcmp(sec->name, name))
136 			return sec;
137 
138 	return NULL;
139 }
140 
find_section_by_index(struct elf * elf,unsigned int idx)141 static struct section *find_section_by_index(struct elf *elf,
142 					     unsigned int idx)
143 {
144 	struct section *sec;
145 
146 	elf_hash_for_each_possible(elf->section_hash, sec, hash, idx)
147 		if (sec->idx == idx)
148 			return sec;
149 
150 	return NULL;
151 }
152 
find_symbol_by_index(struct elf * elf,unsigned int idx)153 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
154 {
155 	struct symbol *sym;
156 
157 	elf_hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
158 		if (sym->idx == idx)
159 			return sym;
160 
161 	return NULL;
162 }
163 
find_symbol_by_offset(struct section * sec,unsigned long offset)164 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
165 {
166 	struct rb_node *node;
167 
168 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
169 		struct symbol *s = rb_entry(node, struct symbol, node);
170 
171 		if (s->offset == offset && s->type != STT_SECTION)
172 			return s;
173 	}
174 
175 	return NULL;
176 }
177 
find_func_by_offset(struct section * sec,unsigned long offset)178 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
179 {
180 	struct rb_node *node;
181 
182 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
183 		struct symbol *s = rb_entry(node, struct symbol, node);
184 
185 		if (s->offset == offset && s->type == STT_FUNC)
186 			return s;
187 	}
188 
189 	return NULL;
190 }
191 
find_symbol_containing(const struct section * sec,unsigned long offset)192 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
193 {
194 	struct rb_node *node;
195 
196 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
197 		struct symbol *s = rb_entry(node, struct symbol, node);
198 
199 		if (s->type != STT_SECTION)
200 			return s;
201 	}
202 
203 	return NULL;
204 }
205 
find_func_containing(struct section * sec,unsigned long offset)206 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
207 {
208 	struct rb_node *node;
209 
210 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
211 		struct symbol *s = rb_entry(node, struct symbol, node);
212 
213 		if (s->type == STT_FUNC)
214 			return s;
215 	}
216 
217 	return NULL;
218 }
219 
find_symbol_by_name(const struct elf * elf,const char * name)220 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
221 {
222 	struct symbol *sym;
223 
224 	elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
225 		if (!strcmp(sym->name, name))
226 			return sym;
227 
228 	return NULL;
229 }
230 
find_reloc_by_dest_range(const struct elf * elf,struct section * sec,unsigned long offset,unsigned int len)231 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
232 				     unsigned long offset, unsigned int len)
233 {
234 	struct reloc *reloc, *r = NULL;
235 	unsigned long o;
236 
237 	if (!sec->reloc)
238 		return NULL;
239 
240 	sec = sec->reloc;
241 
242 	for_offset_range(o, offset, offset + len) {
243 		elf_hash_for_each_possible(elf->reloc_hash, reloc, hash,
244 				       sec_offset_hash(sec, o)) {
245 			if (reloc->sec != sec)
246 				continue;
247 
248 			if (reloc->offset >= offset && reloc->offset < offset + len) {
249 				if (!r || reloc->offset < r->offset)
250 					r = reloc;
251 			}
252 		}
253 		if (r)
254 			return r;
255 	}
256 
257 	return NULL;
258 }
259 
find_reloc_by_dest(const struct elf * elf,struct section * sec,unsigned long offset)260 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
261 {
262 	return find_reloc_by_dest_range(elf, sec, offset, 1);
263 }
264 
read_sections(struct elf * elf)265 static int read_sections(struct elf *elf)
266 {
267 	Elf_Scn *s = NULL;
268 	struct section *sec;
269 	size_t shstrndx, sections_nr;
270 	int i;
271 
272 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
273 		WARN_ELF("elf_getshdrnum");
274 		return -1;
275 	}
276 
277 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
278 		WARN_ELF("elf_getshdrstrndx");
279 		return -1;
280 	}
281 
282 	for (i = 0; i < sections_nr; i++) {
283 		sec = malloc(sizeof(*sec));
284 		if (!sec) {
285 			perror("malloc");
286 			return -1;
287 		}
288 		memset(sec, 0, sizeof(*sec));
289 
290 		INIT_LIST_HEAD(&sec->symbol_list);
291 		INIT_LIST_HEAD(&sec->reloc_list);
292 
293 		s = elf_getscn(elf->elf, i);
294 		if (!s) {
295 			WARN_ELF("elf_getscn");
296 			return -1;
297 		}
298 
299 		sec->idx = elf_ndxscn(s);
300 
301 		if (!gelf_getshdr(s, &sec->sh)) {
302 			WARN_ELF("gelf_getshdr");
303 			return -1;
304 		}
305 
306 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
307 		if (!sec->name) {
308 			WARN_ELF("elf_strptr");
309 			return -1;
310 		}
311 
312 		if (sec->sh.sh_size != 0) {
313 			sec->data = elf_getdata(s, NULL);
314 			if (!sec->data) {
315 				WARN_ELF("elf_getdata");
316 				return -1;
317 			}
318 			if (sec->data->d_off != 0 ||
319 			    sec->data->d_size != sec->sh.sh_size) {
320 				WARN("unexpected data attributes for %s",
321 				     sec->name);
322 				return -1;
323 			}
324 		}
325 		sec->len = sec->sh.sh_size;
326 
327 		list_add_tail(&sec->list, &elf->sections);
328 		elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
329 		elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
330 	}
331 
332 	if (stats)
333 		printf("nr_sections: %lu\n", (unsigned long)sections_nr);
334 
335 	/* sanity check, one more call to elf_nextscn() should return NULL */
336 	if (elf_nextscn(elf->elf, s)) {
337 		WARN("section entry mismatch");
338 		return -1;
339 	}
340 
341 	return 0;
342 }
343 
elf_add_symbol(struct elf * elf,struct symbol * sym)344 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
345 {
346 	struct list_head *entry;
347 	struct rb_node *pnode;
348 
349 	sym->type = GELF_ST_TYPE(sym->sym.st_info);
350 	sym->bind = GELF_ST_BIND(sym->sym.st_info);
351 
352 	sym->offset = sym->sym.st_value;
353 	sym->len = sym->sym.st_size;
354 
355 	rb_add(&sym->sec->symbol_tree, &sym->node, symbol_to_offset);
356 	pnode = rb_prev(&sym->node);
357 	if (pnode)
358 		entry = &rb_entry(pnode, struct symbol, node)->list;
359 	else
360 		entry = &sym->sec->symbol_list;
361 	list_add(&sym->list, entry);
362 	elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
363 	elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
364 
365 	/*
366 	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
367 	 * can exist within a function, confusing the sorting.
368 	 */
369 	if (!sym->len)
370 		rb_erase(&sym->node, &sym->sec->symbol_tree);
371 }
372 
read_symbols(struct elf * elf)373 static int read_symbols(struct elf *elf)
374 {
375 	struct section *symtab, *symtab_shndx, *sec;
376 	struct symbol *sym, *pfunc;
377 	int symbols_nr, i;
378 	char *coldstr;
379 	Elf_Data *shndx_data = NULL;
380 	Elf32_Word shndx;
381 
382 	symtab = find_section_by_name(elf, ".symtab");
383 	if (!symtab) {
384 		/*
385 		 * A missing symbol table is actually possible if it's an empty
386 		 * .o file.  This can happen for thunk_64.o.
387 		 */
388 		return 0;
389 	}
390 
391 	symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
392 	if (symtab_shndx)
393 		shndx_data = symtab_shndx->data;
394 
395 	symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
396 
397 	for (i = 0; i < symbols_nr; i++) {
398 		sym = malloc(sizeof(*sym));
399 		if (!sym) {
400 			perror("malloc");
401 			return -1;
402 		}
403 		memset(sym, 0, sizeof(*sym));
404 		sym->alias = sym;
405 
406 		sym->idx = i;
407 
408 		if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
409 				      &shndx)) {
410 			WARN_ELF("gelf_getsymshndx");
411 			goto err;
412 		}
413 
414 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
415 				       sym->sym.st_name);
416 		if (!sym->name) {
417 			WARN_ELF("elf_strptr");
418 			goto err;
419 		}
420 
421 		if ((sym->sym.st_shndx > SHN_UNDEF &&
422 		     sym->sym.st_shndx < SHN_LORESERVE) ||
423 		    (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
424 			if (sym->sym.st_shndx != SHN_XINDEX)
425 				shndx = sym->sym.st_shndx;
426 
427 			sym->sec = find_section_by_index(elf, shndx);
428 			if (!sym->sec) {
429 				WARN("couldn't find section for symbol %s",
430 				     sym->name);
431 				goto err;
432 			}
433 			if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
434 				sym->name = sym->sec->name;
435 				sym->sec->sym = sym;
436 			}
437 		} else
438 			sym->sec = find_section_by_index(elf, 0);
439 
440 		elf_add_symbol(elf, sym);
441 	}
442 
443 	if (stats)
444 		printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
445 
446 	/* Create parent/child links for any cold subfunctions */
447 	list_for_each_entry(sec, &elf->sections, list) {
448 		list_for_each_entry(sym, &sec->symbol_list, list) {
449 			char pname[MAX_NAME_LEN + 1];
450 			size_t pnamelen;
451 			if (sym->type != STT_FUNC)
452 				continue;
453 
454 			if (sym->pfunc == NULL)
455 				sym->pfunc = sym;
456 
457 			if (sym->cfunc == NULL)
458 				sym->cfunc = sym;
459 
460 			coldstr = strstr(sym->name, ".cold");
461 			if (!coldstr)
462 				continue;
463 
464 			pnamelen = coldstr - sym->name;
465 			if (pnamelen > MAX_NAME_LEN) {
466 				WARN("%s(): parent function name exceeds maximum length of %d characters",
467 				     sym->name, MAX_NAME_LEN);
468 				return -1;
469 			}
470 
471 			strncpy(pname, sym->name, pnamelen);
472 			pname[pnamelen] = '\0';
473 			pfunc = find_symbol_by_name(elf, pname);
474 
475 			if (!pfunc) {
476 				WARN("%s(): can't find parent function",
477 				     sym->name);
478 				return -1;
479 			}
480 
481 			sym->pfunc = pfunc;
482 			pfunc->cfunc = sym;
483 
484 			/*
485 			 * Unfortunately, -fnoreorder-functions puts the child
486 			 * inside the parent.  Remove the overlap so we can
487 			 * have sane assumptions.
488 			 *
489 			 * Note that pfunc->len now no longer matches
490 			 * pfunc->sym.st_size.
491 			 */
492 			if (sym->sec == pfunc->sec &&
493 			    sym->offset >= pfunc->offset &&
494 			    sym->offset + sym->len == pfunc->offset + pfunc->len) {
495 				pfunc->len -= sym->len;
496 			}
497 		}
498 	}
499 
500 	return 0;
501 
502 err:
503 	free(sym);
504 	return -1;
505 }
506 
507 static struct section *elf_create_reloc_section(struct elf *elf,
508 						struct section *base,
509 						int reltype);
510 
elf_add_reloc(struct elf * elf,struct section * sec,unsigned long offset,unsigned int type,struct symbol * sym,int addend)511 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
512 		  unsigned int type, struct symbol *sym, int addend)
513 {
514 	struct reloc *reloc;
515 
516 	if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA))
517 		return -1;
518 
519 	reloc = malloc(sizeof(*reloc));
520 	if (!reloc) {
521 		perror("malloc");
522 		return -1;
523 	}
524 	memset(reloc, 0, sizeof(*reloc));
525 
526 	reloc->sec = sec->reloc;
527 	reloc->offset = offset;
528 	reloc->type = type;
529 	reloc->sym = sym;
530 	reloc->addend = addend;
531 
532 	list_add_tail(&reloc->list, &sec->reloc->reloc_list);
533 	elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
534 
535 	sec->reloc->changed = true;
536 
537 	return 0;
538 }
539 
elf_add_reloc_to_insn(struct elf * elf,struct section * sec,unsigned long offset,unsigned int type,struct section * insn_sec,unsigned long insn_off)540 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
541 			  unsigned long offset, unsigned int type,
542 			  struct section *insn_sec, unsigned long insn_off)
543 {
544 	struct symbol *sym;
545 	int addend;
546 
547 	if (insn_sec->sym) {
548 		sym = insn_sec->sym;
549 		addend = insn_off;
550 
551 	} else {
552 		/*
553 		 * The Clang assembler strips section symbols, so we have to
554 		 * reference the function symbol instead:
555 		 */
556 		sym = find_symbol_containing(insn_sec, insn_off);
557 		if (!sym) {
558 			/*
559 			 * Hack alert.  This happens when we need to reference
560 			 * the NOP pad insn immediately after the function.
561 			 */
562 			sym = find_symbol_containing(insn_sec, insn_off - 1);
563 		}
564 
565 		if (!sym) {
566 			WARN("can't find symbol containing %s+0x%lx", insn_sec->name, insn_off);
567 			return -1;
568 		}
569 
570 		addend = insn_off - sym->offset;
571 	}
572 
573 	return elf_add_reloc(elf, sec, offset, type, sym, addend);
574 }
575 
read_rel_reloc(struct section * sec,int i,struct reloc * reloc,unsigned int * symndx)576 static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
577 {
578 	if (!gelf_getrel(sec->data, i, &reloc->rel)) {
579 		WARN_ELF("gelf_getrel");
580 		return -1;
581 	}
582 	reloc->type = GELF_R_TYPE(reloc->rel.r_info);
583 	reloc->addend = 0;
584 	reloc->offset = reloc->rel.r_offset;
585 	*symndx = GELF_R_SYM(reloc->rel.r_info);
586 	return 0;
587 }
588 
read_rela_reloc(struct section * sec,int i,struct reloc * reloc,unsigned int * symndx)589 static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
590 {
591 	if (!gelf_getrela(sec->data, i, &reloc->rela)) {
592 		WARN_ELF("gelf_getrela");
593 		return -1;
594 	}
595 	reloc->type = GELF_R_TYPE(reloc->rela.r_info);
596 	reloc->addend = reloc->rela.r_addend;
597 	reloc->offset = reloc->rela.r_offset;
598 	*symndx = GELF_R_SYM(reloc->rela.r_info);
599 	return 0;
600 }
601 
read_relocs(struct elf * elf)602 static int read_relocs(struct elf *elf)
603 {
604 	struct section *sec;
605 	struct reloc *reloc;
606 	int i;
607 	unsigned int symndx;
608 	unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
609 
610 	list_for_each_entry(sec, &elf->sections, list) {
611 		if ((sec->sh.sh_type != SHT_RELA) &&
612 		    (sec->sh.sh_type != SHT_REL))
613 			continue;
614 
615 		sec->base = find_section_by_index(elf, sec->sh.sh_info);
616 		if (!sec->base) {
617 			WARN("can't find base section for reloc section %s",
618 			     sec->name);
619 			return -1;
620 		}
621 
622 		sec->base->reloc = sec;
623 
624 		nr_reloc = 0;
625 		for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
626 			reloc = malloc(sizeof(*reloc));
627 			if (!reloc) {
628 				perror("malloc");
629 				return -1;
630 			}
631 			memset(reloc, 0, sizeof(*reloc));
632 			switch (sec->sh.sh_type) {
633 			case SHT_REL:
634 				if (read_rel_reloc(sec, i, reloc, &symndx))
635 					return -1;
636 				break;
637 			case SHT_RELA:
638 				if (read_rela_reloc(sec, i, reloc, &symndx))
639 					return -1;
640 				break;
641 			default: return -1;
642 			}
643 
644 			reloc->sec = sec;
645 			reloc->idx = i;
646 			reloc->sym = find_symbol_by_index(elf, symndx);
647 			if (!reloc->sym) {
648 				WARN("can't find reloc entry symbol %d for %s",
649 				     symndx, sec->name);
650 				return -1;
651 			}
652 
653 			list_add_tail(&reloc->list, &sec->reloc_list);
654 			elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
655 
656 			nr_reloc++;
657 		}
658 		max_reloc = max(max_reloc, nr_reloc);
659 		tot_reloc += nr_reloc;
660 	}
661 
662 	if (stats) {
663 		printf("max_reloc: %lu\n", max_reloc);
664 		printf("tot_reloc: %lu\n", tot_reloc);
665 	}
666 
667 	return 0;
668 }
669 
elf_open_read(const char * name,int flags)670 struct elf *elf_open_read(const char *name, int flags)
671 {
672 	struct elf *elf;
673 	Elf_Cmd cmd;
674 
675 	elf_version(EV_CURRENT);
676 
677 	elf = malloc(sizeof(*elf));
678 	if (!elf) {
679 		perror("malloc");
680 		return NULL;
681 	}
682 	memset(elf, 0, offsetof(struct elf, sections));
683 
684 	INIT_LIST_HEAD(&elf->sections);
685 
686 	elf_hash_init(elf->symbol_hash);
687 	elf_hash_init(elf->symbol_name_hash);
688 	elf_hash_init(elf->section_hash);
689 	elf_hash_init(elf->section_name_hash);
690 	elf_hash_init(elf->reloc_hash);
691 
692 	elf->fd = open(name, flags);
693 	if (elf->fd == -1) {
694 		fprintf(stderr, "objtool: Can't open '%s': %s\n",
695 			name, strerror(errno));
696 		goto err;
697 	}
698 
699 	if ((flags & O_ACCMODE) == O_RDONLY)
700 		cmd = ELF_C_READ_MMAP;
701 	else if ((flags & O_ACCMODE) == O_RDWR)
702 		cmd = ELF_C_RDWR;
703 	else /* O_WRONLY */
704 		cmd = ELF_C_WRITE;
705 
706 	elf->elf = elf_begin(elf->fd, cmd, NULL);
707 	if (!elf->elf) {
708 		WARN_ELF("elf_begin");
709 		goto err;
710 	}
711 
712 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
713 		WARN_ELF("gelf_getehdr");
714 		goto err;
715 	}
716 
717 	if (read_sections(elf))
718 		goto err;
719 
720 	if (read_symbols(elf))
721 		goto err;
722 
723 	if (read_relocs(elf))
724 		goto err;
725 
726 	return elf;
727 
728 err:
729 	elf_close(elf);
730 	return NULL;
731 }
732 
elf_add_string(struct elf * elf,struct section * strtab,char * str)733 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
734 {
735 	Elf_Data *data;
736 	Elf_Scn *s;
737 	int len;
738 
739 	if (!strtab)
740 		strtab = find_section_by_name(elf, ".strtab");
741 	if (!strtab) {
742 		WARN("can't find .strtab section");
743 		return -1;
744 	}
745 
746 	s = elf_getscn(elf->elf, strtab->idx);
747 	if (!s) {
748 		WARN_ELF("elf_getscn");
749 		return -1;
750 	}
751 
752 	data = elf_newdata(s);
753 	if (!data) {
754 		WARN_ELF("elf_newdata");
755 		return -1;
756 	}
757 
758 	data->d_buf = str;
759 	data->d_size = strlen(str) + 1;
760 	data->d_align = 1;
761 	data->d_type = ELF_T_SYM;
762 
763 	len = strtab->len;
764 	strtab->len += data->d_size;
765 	strtab->changed = true;
766 
767 	return len;
768 }
769 
elf_create_section(struct elf * elf,const char * name,unsigned int sh_flags,size_t entsize,int nr)770 struct section *elf_create_section(struct elf *elf, const char *name,
771 				   unsigned int sh_flags, size_t entsize, int nr)
772 {
773 	struct section *sec, *shstrtab;
774 	size_t size = entsize * nr;
775 	Elf_Scn *s;
776 
777 	sec = malloc(sizeof(*sec));
778 	if (!sec) {
779 		perror("malloc");
780 		return NULL;
781 	}
782 	memset(sec, 0, sizeof(*sec));
783 
784 	INIT_LIST_HEAD(&sec->symbol_list);
785 	INIT_LIST_HEAD(&sec->reloc_list);
786 
787 	s = elf_newscn(elf->elf);
788 	if (!s) {
789 		WARN_ELF("elf_newscn");
790 		return NULL;
791 	}
792 
793 	sec->name = strdup(name);
794 	if (!sec->name) {
795 		perror("strdup");
796 		return NULL;
797 	}
798 
799 	sec->idx = elf_ndxscn(s);
800 	sec->len = size;
801 	sec->changed = true;
802 
803 	sec->data = elf_newdata(s);
804 	if (!sec->data) {
805 		WARN_ELF("elf_newdata");
806 		return NULL;
807 	}
808 
809 	sec->data->d_size = size;
810 	sec->data->d_align = 1;
811 
812 	if (size) {
813 		sec->data->d_buf = malloc(size);
814 		if (!sec->data->d_buf) {
815 			perror("malloc");
816 			return NULL;
817 		}
818 		memset(sec->data->d_buf, 0, size);
819 	}
820 
821 	if (!gelf_getshdr(s, &sec->sh)) {
822 		WARN_ELF("gelf_getshdr");
823 		return NULL;
824 	}
825 
826 	sec->sh.sh_size = size;
827 	sec->sh.sh_entsize = entsize;
828 	sec->sh.sh_type = SHT_PROGBITS;
829 	sec->sh.sh_addralign = 1;
830 	sec->sh.sh_flags = SHF_ALLOC | sh_flags;
831 
832 	/* Add section name to .shstrtab (or .strtab for Clang) */
833 	shstrtab = find_section_by_name(elf, ".shstrtab");
834 	if (!shstrtab)
835 		shstrtab = find_section_by_name(elf, ".strtab");
836 	if (!shstrtab) {
837 		WARN("can't find .shstrtab or .strtab section");
838 		return NULL;
839 	}
840 	sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
841 	if (sec->sh.sh_name == -1)
842 		return NULL;
843 
844 	list_add_tail(&sec->list, &elf->sections);
845 	elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
846 	elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
847 
848 	elf->changed = true;
849 
850 	return sec;
851 }
852 
elf_create_rel_reloc_section(struct elf * elf,struct section * base)853 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
854 {
855 	char *relocname;
856 	struct section *sec;
857 
858 	relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
859 	if (!relocname) {
860 		perror("malloc");
861 		return NULL;
862 	}
863 	strcpy(relocname, ".rel");
864 	strcat(relocname, base->name);
865 
866 	sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0);
867 	free(relocname);
868 	if (!sec)
869 		return NULL;
870 
871 	base->reloc = sec;
872 	sec->base = base;
873 
874 	sec->sh.sh_type = SHT_REL;
875 	sec->sh.sh_addralign = 8;
876 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
877 	sec->sh.sh_info = base->idx;
878 	sec->sh.sh_flags = SHF_INFO_LINK;
879 
880 	return sec;
881 }
882 
elf_create_rela_reloc_section(struct elf * elf,struct section * base)883 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
884 {
885 	char *relocname;
886 	struct section *sec;
887 
888 	relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
889 	if (!relocname) {
890 		perror("malloc");
891 		return NULL;
892 	}
893 	strcpy(relocname, ".rela");
894 	strcat(relocname, base->name);
895 
896 	sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
897 	free(relocname);
898 	if (!sec)
899 		return NULL;
900 
901 	base->reloc = sec;
902 	sec->base = base;
903 
904 	sec->sh.sh_type = SHT_RELA;
905 	sec->sh.sh_addralign = 8;
906 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
907 	sec->sh.sh_info = base->idx;
908 	sec->sh.sh_flags = SHF_INFO_LINK;
909 
910 	return sec;
911 }
912 
elf_create_reloc_section(struct elf * elf,struct section * base,int reltype)913 static struct section *elf_create_reloc_section(struct elf *elf,
914 					 struct section *base,
915 					 int reltype)
916 {
917 	switch (reltype) {
918 	case SHT_REL:  return elf_create_rel_reloc_section(elf, base);
919 	case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
920 	default:       return NULL;
921 	}
922 }
923 
elf_rebuild_rel_reloc_section(struct section * sec,int nr)924 static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
925 {
926 	struct reloc *reloc;
927 	int idx = 0, size;
928 	GElf_Rel *relocs;
929 
930 	/* Allocate a buffer for relocations */
931 	size = nr * sizeof(*relocs);
932 	relocs = malloc(size);
933 	if (!relocs) {
934 		perror("malloc");
935 		return -1;
936 	}
937 
938 	sec->data->d_buf = relocs;
939 	sec->data->d_size = size;
940 
941 	sec->sh.sh_size = size;
942 
943 	idx = 0;
944 	list_for_each_entry(reloc, &sec->reloc_list, list) {
945 		relocs[idx].r_offset = reloc->offset;
946 		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
947 		idx++;
948 	}
949 
950 	return 0;
951 }
952 
elf_rebuild_rela_reloc_section(struct section * sec,int nr)953 static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
954 {
955 	struct reloc *reloc;
956 	int idx = 0, size;
957 	GElf_Rela *relocs;
958 
959 	/* Allocate a buffer for relocations with addends */
960 	size = nr * sizeof(*relocs);
961 	relocs = malloc(size);
962 	if (!relocs) {
963 		perror("malloc");
964 		return -1;
965 	}
966 
967 	sec->data->d_buf = relocs;
968 	sec->data->d_size = size;
969 
970 	sec->sh.sh_size = size;
971 
972 	idx = 0;
973 	list_for_each_entry(reloc, &sec->reloc_list, list) {
974 		relocs[idx].r_offset = reloc->offset;
975 		relocs[idx].r_addend = reloc->addend;
976 		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
977 		idx++;
978 	}
979 
980 	return 0;
981 }
982 
elf_rebuild_reloc_section(struct elf * elf,struct section * sec)983 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
984 {
985 	struct reloc *reloc;
986 	int nr;
987 
988 	nr = 0;
989 	list_for_each_entry(reloc, &sec->reloc_list, list)
990 		nr++;
991 
992 	switch (sec->sh.sh_type) {
993 	case SHT_REL:  return elf_rebuild_rel_reloc_section(sec, nr);
994 	case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr);
995 	default:       return -1;
996 	}
997 }
998 
elf_write_insn(struct elf * elf,struct section * sec,unsigned long offset,unsigned int len,const char * insn)999 int elf_write_insn(struct elf *elf, struct section *sec,
1000 		   unsigned long offset, unsigned int len,
1001 		   const char *insn)
1002 {
1003 	Elf_Data *data = sec->data;
1004 
1005 	if (data->d_type != ELF_T_BYTE || data->d_off) {
1006 		WARN("write to unexpected data for section: %s", sec->name);
1007 		return -1;
1008 	}
1009 
1010 	memcpy(data->d_buf + offset, insn, len);
1011 	elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
1012 
1013 	elf->changed = true;
1014 
1015 	return 0;
1016 }
1017 
elf_write_reloc(struct elf * elf,struct reloc * reloc)1018 int elf_write_reloc(struct elf *elf, struct reloc *reloc)
1019 {
1020 	struct section *sec = reloc->sec;
1021 
1022 	if (sec->sh.sh_type == SHT_REL) {
1023 		reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1024 		reloc->rel.r_offset = reloc->offset;
1025 
1026 		if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
1027 			WARN_ELF("gelf_update_rel");
1028 			return -1;
1029 		}
1030 	} else {
1031 		reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1032 		reloc->rela.r_addend = reloc->addend;
1033 		reloc->rela.r_offset = reloc->offset;
1034 
1035 		if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
1036 			WARN_ELF("gelf_update_rela");
1037 			return -1;
1038 		}
1039 	}
1040 
1041 	elf->changed = true;
1042 
1043 	return 0;
1044 }
1045 
elf_write(struct elf * elf)1046 int elf_write(struct elf *elf)
1047 {
1048 	struct section *sec;
1049 	Elf_Scn *s;
1050 
1051 	/* Update changed relocation sections and section headers: */
1052 	list_for_each_entry(sec, &elf->sections, list) {
1053 		if (sec->changed) {
1054 			if (sec->base &&
1055 			    elf_rebuild_reloc_section(elf, sec)) {
1056 				WARN("elf_rebuild_reloc_section");
1057 				return -1;
1058 			}
1059 
1060 			s = elf_getscn(elf->elf, sec->idx);
1061 			if (!s) {
1062 				WARN_ELF("elf_getscn");
1063 				return -1;
1064 			}
1065 			if (!gelf_update_shdr(s, &sec->sh)) {
1066 				WARN_ELF("gelf_update_shdr");
1067 				return -1;
1068 			}
1069 
1070 			sec->changed = false;
1071 			elf->changed = true;
1072 		}
1073 	}
1074 
1075 	/* Make sure the new section header entries get updated properly. */
1076 	elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1077 
1078 	/* Write all changes to the file. */
1079 	if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1080 		WARN_ELF("elf_update");
1081 		return -1;
1082 	}
1083 
1084 	elf->changed = false;
1085 
1086 	return 0;
1087 }
1088 
elf_close(struct elf * elf)1089 void elf_close(struct elf *elf)
1090 {
1091 	struct section *sec, *tmpsec;
1092 	struct symbol *sym, *tmpsym;
1093 	struct reloc *reloc, *tmpreloc;
1094 
1095 	if (elf->elf)
1096 		elf_end(elf->elf);
1097 
1098 	if (elf->fd > 0)
1099 		close(elf->fd);
1100 
1101 	list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
1102 		list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
1103 			list_del(&sym->list);
1104 			hash_del(&sym->hash);
1105 			free(sym);
1106 		}
1107 		list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
1108 			list_del(&reloc->list);
1109 			hash_del(&reloc->hash);
1110 			free(reloc);
1111 		}
1112 		list_del(&sec->list);
1113 		free(sec);
1114 	}
1115 
1116 	free(elf);
1117 }
1118