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