xref: /openbmc/linux/tools/objtool/elf.c (revision c0ecca6604b80e438b032578634c6e133c7028f6)
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 static int read_sections(struct elf *elf)
215 {
216 	Elf_Scn *s = NULL;
217 	struct section *sec;
218 	size_t shstrndx, sections_nr;
219 	int i;
220 
221 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
222 		WARN_ELF("elf_getshdrnum");
223 		return -1;
224 	}
225 
226 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
227 		WARN_ELF("elf_getshdrstrndx");
228 		return -1;
229 	}
230 
231 	for (i = 0; i < sections_nr; i++) {
232 		sec = malloc(sizeof(*sec));
233 		if (!sec) {
234 			perror("malloc");
235 			return -1;
236 		}
237 		memset(sec, 0, sizeof(*sec));
238 
239 		INIT_LIST_HEAD(&sec->symbol_list);
240 		INIT_LIST_HEAD(&sec->reloc_list);
241 
242 		s = elf_getscn(elf->elf, i);
243 		if (!s) {
244 			WARN_ELF("elf_getscn");
245 			return -1;
246 		}
247 
248 		sec->idx = elf_ndxscn(s);
249 
250 		if (!gelf_getshdr(s, &sec->sh)) {
251 			WARN_ELF("gelf_getshdr");
252 			return -1;
253 		}
254 
255 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
256 		if (!sec->name) {
257 			WARN_ELF("elf_strptr");
258 			return -1;
259 		}
260 
261 		if (sec->sh.sh_size != 0) {
262 			sec->data = elf_getdata(s, NULL);
263 			if (!sec->data) {
264 				WARN_ELF("elf_getdata");
265 				return -1;
266 			}
267 			if (sec->data->d_off != 0 ||
268 			    sec->data->d_size != sec->sh.sh_size) {
269 				WARN("unexpected data attributes for %s",
270 				     sec->name);
271 				return -1;
272 			}
273 		}
274 		sec->len = sec->sh.sh_size;
275 
276 		list_add_tail(&sec->list, &elf->sections);
277 		elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
278 		elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
279 	}
280 
281 	if (stats)
282 		printf("nr_sections: %lu\n", (unsigned long)sections_nr);
283 
284 	/* sanity check, one more call to elf_nextscn() should return NULL */
285 	if (elf_nextscn(elf->elf, s)) {
286 		WARN("section entry mismatch");
287 		return -1;
288 	}
289 
290 	return 0;
291 }
292 
293 static void elf_add_symbol(struct elf *elf, struct symbol *sym)
294 {
295 	struct list_head *entry;
296 	struct rb_node *pnode;
297 
298 	sym->type = GELF_ST_TYPE(sym->sym.st_info);
299 	sym->bind = GELF_ST_BIND(sym->sym.st_info);
300 
301 	sym->offset = sym->sym.st_value;
302 	sym->len = sym->sym.st_size;
303 
304 	rb_add(&sym->node, &sym->sec->symbol_tree, symbol_to_offset);
305 	pnode = rb_prev(&sym->node);
306 	if (pnode)
307 		entry = &rb_entry(pnode, struct symbol, node)->list;
308 	else
309 		entry = &sym->sec->symbol_list;
310 	list_add(&sym->list, entry);
311 	elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
312 	elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
313 
314 	/*
315 	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
316 	 * can exist within a function, confusing the sorting.
317 	 */
318 	if (!sym->len)
319 		rb_erase(&sym->node, &sym->sec->symbol_tree);
320 }
321 
322 static int read_symbols(struct elf *elf)
323 {
324 	struct section *symtab, *symtab_shndx, *sec;
325 	struct symbol *sym, *pfunc;
326 	int symbols_nr, i;
327 	char *coldstr;
328 	Elf_Data *shndx_data = NULL;
329 	Elf32_Word shndx;
330 
331 	symtab = find_section_by_name(elf, ".symtab");
332 	if (!symtab) {
333 		/*
334 		 * A missing symbol table is actually possible if it's an empty
335 		 * .o file.  This can happen for thunk_64.o.
336 		 */
337 		return 0;
338 	}
339 
340 	symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
341 	if (symtab_shndx)
342 		shndx_data = symtab_shndx->data;
343 
344 	symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
345 
346 	for (i = 0; i < symbols_nr; i++) {
347 		sym = malloc(sizeof(*sym));
348 		if (!sym) {
349 			perror("malloc");
350 			return -1;
351 		}
352 		memset(sym, 0, sizeof(*sym));
353 		sym->alias = sym;
354 
355 		sym->idx = i;
356 
357 		if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
358 				      &shndx)) {
359 			WARN_ELF("gelf_getsymshndx");
360 			goto err;
361 		}
362 
363 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
364 				       sym->sym.st_name);
365 		if (!sym->name) {
366 			WARN_ELF("elf_strptr");
367 			goto err;
368 		}
369 
370 		if ((sym->sym.st_shndx > SHN_UNDEF &&
371 		     sym->sym.st_shndx < SHN_LORESERVE) ||
372 		    (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
373 			if (sym->sym.st_shndx != SHN_XINDEX)
374 				shndx = sym->sym.st_shndx;
375 
376 			sym->sec = find_section_by_index(elf, shndx);
377 			if (!sym->sec) {
378 				WARN("couldn't find section for symbol %s",
379 				     sym->name);
380 				goto err;
381 			}
382 			if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
383 				sym->name = sym->sec->name;
384 				sym->sec->sym = sym;
385 			}
386 		} else
387 			sym->sec = find_section_by_index(elf, 0);
388 
389 		elf_add_symbol(elf, sym);
390 	}
391 
392 	if (stats)
393 		printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
394 
395 	/* Create parent/child links for any cold subfunctions */
396 	list_for_each_entry(sec, &elf->sections, list) {
397 		list_for_each_entry(sym, &sec->symbol_list, list) {
398 			char pname[MAX_NAME_LEN + 1];
399 			size_t pnamelen;
400 			if (sym->type != STT_FUNC)
401 				continue;
402 
403 			if (sym->pfunc == NULL)
404 				sym->pfunc = sym;
405 
406 			if (sym->cfunc == NULL)
407 				sym->cfunc = sym;
408 
409 			coldstr = strstr(sym->name, ".cold");
410 			if (!coldstr)
411 				continue;
412 
413 			pnamelen = coldstr - sym->name;
414 			if (pnamelen > MAX_NAME_LEN) {
415 				WARN("%s(): parent function name exceeds maximum length of %d characters",
416 				     sym->name, MAX_NAME_LEN);
417 				return -1;
418 			}
419 
420 			strncpy(pname, sym->name, pnamelen);
421 			pname[pnamelen] = '\0';
422 			pfunc = find_symbol_by_name(elf, pname);
423 
424 			if (!pfunc) {
425 				WARN("%s(): can't find parent function",
426 				     sym->name);
427 				return -1;
428 			}
429 
430 			sym->pfunc = pfunc;
431 			pfunc->cfunc = sym;
432 
433 			/*
434 			 * Unfortunately, -fnoreorder-functions puts the child
435 			 * inside the parent.  Remove the overlap so we can
436 			 * have sane assumptions.
437 			 *
438 			 * Note that pfunc->len now no longer matches
439 			 * pfunc->sym.st_size.
440 			 */
441 			if (sym->sec == pfunc->sec &&
442 			    sym->offset >= pfunc->offset &&
443 			    sym->offset + sym->len == pfunc->offset + pfunc->len) {
444 				pfunc->len -= sym->len;
445 			}
446 		}
447 	}
448 
449 	return 0;
450 
451 err:
452 	free(sym);
453 	return -1;
454 }
455 
456 static struct section *elf_create_reloc_section(struct elf *elf,
457 						struct section *base,
458 						int reltype);
459 
460 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
461 		  unsigned int type, struct symbol *sym, int addend)
462 {
463 	struct reloc *reloc;
464 
465 	if (!sec->reloc && !elf_create_reloc_section(elf, sec, SHT_RELA))
466 		return -1;
467 
468 	reloc = malloc(sizeof(*reloc));
469 	if (!reloc) {
470 		perror("malloc");
471 		return -1;
472 	}
473 	memset(reloc, 0, sizeof(*reloc));
474 
475 	reloc->sec = sec->reloc;
476 	reloc->offset = offset;
477 	reloc->type = type;
478 	reloc->sym = sym;
479 	reloc->addend = addend;
480 
481 	list_add_tail(&reloc->list, &sec->reloc->reloc_list);
482 	elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
483 
484 	sec->reloc->changed = true;
485 
486 	return 0;
487 }
488 
489 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
490 			  unsigned long offset, unsigned int type,
491 			  struct section *insn_sec, unsigned long insn_off)
492 {
493 	struct symbol *sym;
494 	int addend;
495 
496 	if (insn_sec->sym) {
497 		sym = insn_sec->sym;
498 		addend = insn_off;
499 
500 	} else {
501 		/*
502 		 * The Clang assembler strips section symbols, so we have to
503 		 * reference the function symbol instead:
504 		 */
505 		sym = find_symbol_containing(insn_sec, insn_off);
506 		if (!sym) {
507 			/*
508 			 * Hack alert.  This happens when we need to reference
509 			 * the NOP pad insn immediately after the function.
510 			 */
511 			sym = find_symbol_containing(insn_sec, insn_off - 1);
512 		}
513 
514 		if (!sym) {
515 			WARN("can't find symbol containing %s+0x%lx", insn_sec->name, insn_off);
516 			return -1;
517 		}
518 
519 		addend = insn_off - sym->offset;
520 	}
521 
522 	return elf_add_reloc(elf, sec, offset, type, sym, addend);
523 }
524 
525 static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
526 {
527 	if (!gelf_getrel(sec->data, i, &reloc->rel)) {
528 		WARN_ELF("gelf_getrel");
529 		return -1;
530 	}
531 	reloc->type = GELF_R_TYPE(reloc->rel.r_info);
532 	reloc->addend = 0;
533 	reloc->offset = reloc->rel.r_offset;
534 	*symndx = GELF_R_SYM(reloc->rel.r_info);
535 	return 0;
536 }
537 
538 static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
539 {
540 	if (!gelf_getrela(sec->data, i, &reloc->rela)) {
541 		WARN_ELF("gelf_getrela");
542 		return -1;
543 	}
544 	reloc->type = GELF_R_TYPE(reloc->rela.r_info);
545 	reloc->addend = reloc->rela.r_addend;
546 	reloc->offset = reloc->rela.r_offset;
547 	*symndx = GELF_R_SYM(reloc->rela.r_info);
548 	return 0;
549 }
550 
551 static int read_relocs(struct elf *elf)
552 {
553 	struct section *sec;
554 	struct reloc *reloc;
555 	int i;
556 	unsigned int symndx;
557 	unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
558 
559 	list_for_each_entry(sec, &elf->sections, list) {
560 		if ((sec->sh.sh_type != SHT_RELA) &&
561 		    (sec->sh.sh_type != SHT_REL))
562 			continue;
563 
564 		sec->base = find_section_by_index(elf, sec->sh.sh_info);
565 		if (!sec->base) {
566 			WARN("can't find base section for reloc section %s",
567 			     sec->name);
568 			return -1;
569 		}
570 
571 		sec->base->reloc = sec;
572 
573 		nr_reloc = 0;
574 		for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
575 			reloc = malloc(sizeof(*reloc));
576 			if (!reloc) {
577 				perror("malloc");
578 				return -1;
579 			}
580 			memset(reloc, 0, sizeof(*reloc));
581 			switch (sec->sh.sh_type) {
582 			case SHT_REL:
583 				if (read_rel_reloc(sec, i, reloc, &symndx))
584 					return -1;
585 				break;
586 			case SHT_RELA:
587 				if (read_rela_reloc(sec, i, reloc, &symndx))
588 					return -1;
589 				break;
590 			default: return -1;
591 			}
592 
593 			reloc->sec = sec;
594 			reloc->idx = i;
595 			reloc->sym = find_symbol_by_index(elf, symndx);
596 			if (!reloc->sym) {
597 				WARN("can't find reloc entry symbol %d for %s",
598 				     symndx, sec->name);
599 				return -1;
600 			}
601 
602 			list_add_tail(&reloc->list, &sec->reloc_list);
603 			elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
604 
605 			nr_reloc++;
606 		}
607 		max_reloc = max(max_reloc, nr_reloc);
608 		tot_reloc += nr_reloc;
609 	}
610 
611 	if (stats) {
612 		printf("max_reloc: %lu\n", max_reloc);
613 		printf("tot_reloc: %lu\n", tot_reloc);
614 	}
615 
616 	return 0;
617 }
618 
619 struct elf *elf_open_read(const char *name, int flags)
620 {
621 	struct elf *elf;
622 	Elf_Cmd cmd;
623 
624 	elf_version(EV_CURRENT);
625 
626 	elf = malloc(sizeof(*elf));
627 	if (!elf) {
628 		perror("malloc");
629 		return NULL;
630 	}
631 	memset(elf, 0, offsetof(struct elf, sections));
632 
633 	INIT_LIST_HEAD(&elf->sections);
634 
635 	elf_hash_init(elf->symbol_hash);
636 	elf_hash_init(elf->symbol_name_hash);
637 	elf_hash_init(elf->section_hash);
638 	elf_hash_init(elf->section_name_hash);
639 	elf_hash_init(elf->reloc_hash);
640 
641 	elf->fd = open(name, flags);
642 	if (elf->fd == -1) {
643 		fprintf(stderr, "objtool: Can't open '%s': %s\n",
644 			name, strerror(errno));
645 		goto err;
646 	}
647 
648 	if ((flags & O_ACCMODE) == O_RDONLY)
649 		cmd = ELF_C_READ_MMAP;
650 	else if ((flags & O_ACCMODE) == O_RDWR)
651 		cmd = ELF_C_RDWR;
652 	else /* O_WRONLY */
653 		cmd = ELF_C_WRITE;
654 
655 	elf->elf = elf_begin(elf->fd, cmd, NULL);
656 	if (!elf->elf) {
657 		WARN_ELF("elf_begin");
658 		goto err;
659 	}
660 
661 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
662 		WARN_ELF("gelf_getehdr");
663 		goto err;
664 	}
665 
666 	if (read_sections(elf))
667 		goto err;
668 
669 	if (read_symbols(elf))
670 		goto err;
671 
672 	if (read_relocs(elf))
673 		goto err;
674 
675 	return elf;
676 
677 err:
678 	elf_close(elf);
679 	return NULL;
680 }
681 
682 static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
683 {
684 	Elf_Data *data;
685 	Elf_Scn *s;
686 	int len;
687 
688 	if (!strtab)
689 		strtab = find_section_by_name(elf, ".strtab");
690 	if (!strtab) {
691 		WARN("can't find .strtab section");
692 		return -1;
693 	}
694 
695 	s = elf_getscn(elf->elf, strtab->idx);
696 	if (!s) {
697 		WARN_ELF("elf_getscn");
698 		return -1;
699 	}
700 
701 	data = elf_newdata(s);
702 	if (!data) {
703 		WARN_ELF("elf_newdata");
704 		return -1;
705 	}
706 
707 	data->d_buf = str;
708 	data->d_size = strlen(str) + 1;
709 	data->d_align = 1;
710 
711 	len = strtab->len;
712 	strtab->len += data->d_size;
713 	strtab->changed = true;
714 
715 	return len;
716 }
717 
718 struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name)
719 {
720 	struct section *symtab, *symtab_shndx;
721 	struct symbol *sym;
722 	Elf_Data *data;
723 	Elf_Scn *s;
724 
725 	sym = malloc(sizeof(*sym));
726 	if (!sym) {
727 		perror("malloc");
728 		return NULL;
729 	}
730 	memset(sym, 0, sizeof(*sym));
731 
732 	sym->name = strdup(name);
733 
734 	sym->sym.st_name = elf_add_string(elf, NULL, sym->name);
735 	if (sym->sym.st_name == -1)
736 		return NULL;
737 
738 	sym->sym.st_info = GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
739 	// st_other 0
740 	// st_shndx 0
741 	// st_value 0
742 	// st_size 0
743 
744 	symtab = find_section_by_name(elf, ".symtab");
745 	if (!symtab) {
746 		WARN("can't find .symtab");
747 		return NULL;
748 	}
749 
750 	s = elf_getscn(elf->elf, symtab->idx);
751 	if (!s) {
752 		WARN_ELF("elf_getscn");
753 		return NULL;
754 	}
755 
756 	data = elf_newdata(s);
757 	if (!data) {
758 		WARN_ELF("elf_newdata");
759 		return NULL;
760 	}
761 
762 	data->d_buf = &sym->sym;
763 	data->d_size = sizeof(sym->sym);
764 	data->d_align = 1;
765 	data->d_type = ELF_T_SYM;
766 
767 	sym->idx = symtab->len / sizeof(sym->sym);
768 
769 	symtab->len += data->d_size;
770 	symtab->changed = true;
771 
772 	symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
773 	if (symtab_shndx) {
774 		s = elf_getscn(elf->elf, symtab_shndx->idx);
775 		if (!s) {
776 			WARN_ELF("elf_getscn");
777 			return NULL;
778 		}
779 
780 		data = elf_newdata(s);
781 		if (!data) {
782 			WARN_ELF("elf_newdata");
783 			return NULL;
784 		}
785 
786 		data->d_buf = &sym->sym.st_size; /* conveniently 0 */
787 		data->d_size = sizeof(Elf32_Word);
788 		data->d_align = 4;
789 		data->d_type = ELF_T_WORD;
790 
791 		symtab_shndx->len += 4;
792 		symtab_shndx->changed = true;
793 	}
794 
795 	sym->sec = find_section_by_index(elf, 0);
796 
797 	elf_add_symbol(elf, sym);
798 
799 	return sym;
800 }
801 
802 struct section *elf_create_section(struct elf *elf, const char *name,
803 				   unsigned int sh_flags, size_t entsize, int nr)
804 {
805 	struct section *sec, *shstrtab;
806 	size_t size = entsize * nr;
807 	Elf_Scn *s;
808 
809 	sec = malloc(sizeof(*sec));
810 	if (!sec) {
811 		perror("malloc");
812 		return NULL;
813 	}
814 	memset(sec, 0, sizeof(*sec));
815 
816 	INIT_LIST_HEAD(&sec->symbol_list);
817 	INIT_LIST_HEAD(&sec->reloc_list);
818 
819 	s = elf_newscn(elf->elf);
820 	if (!s) {
821 		WARN_ELF("elf_newscn");
822 		return NULL;
823 	}
824 
825 	sec->name = strdup(name);
826 	if (!sec->name) {
827 		perror("strdup");
828 		return NULL;
829 	}
830 
831 	sec->idx = elf_ndxscn(s);
832 	sec->len = size;
833 	sec->changed = true;
834 
835 	sec->data = elf_newdata(s);
836 	if (!sec->data) {
837 		WARN_ELF("elf_newdata");
838 		return NULL;
839 	}
840 
841 	sec->data->d_size = size;
842 	sec->data->d_align = 1;
843 
844 	if (size) {
845 		sec->data->d_buf = malloc(size);
846 		if (!sec->data->d_buf) {
847 			perror("malloc");
848 			return NULL;
849 		}
850 		memset(sec->data->d_buf, 0, size);
851 	}
852 
853 	if (!gelf_getshdr(s, &sec->sh)) {
854 		WARN_ELF("gelf_getshdr");
855 		return NULL;
856 	}
857 
858 	sec->sh.sh_size = size;
859 	sec->sh.sh_entsize = entsize;
860 	sec->sh.sh_type = SHT_PROGBITS;
861 	sec->sh.sh_addralign = 1;
862 	sec->sh.sh_flags = SHF_ALLOC | sh_flags;
863 
864 	/* Add section name to .shstrtab (or .strtab for Clang) */
865 	shstrtab = find_section_by_name(elf, ".shstrtab");
866 	if (!shstrtab)
867 		shstrtab = find_section_by_name(elf, ".strtab");
868 	if (!shstrtab) {
869 		WARN("can't find .shstrtab or .strtab section");
870 		return NULL;
871 	}
872 	sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
873 	if (sec->sh.sh_name == -1)
874 		return NULL;
875 
876 	list_add_tail(&sec->list, &elf->sections);
877 	elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
878 	elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
879 
880 	elf->changed = true;
881 
882 	return sec;
883 }
884 
885 static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
886 {
887 	char *relocname;
888 	struct section *sec;
889 
890 	relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
891 	if (!relocname) {
892 		perror("malloc");
893 		return NULL;
894 	}
895 	strcpy(relocname, ".rel");
896 	strcat(relocname, base->name);
897 
898 	sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0);
899 	free(relocname);
900 	if (!sec)
901 		return NULL;
902 
903 	base->reloc = sec;
904 	sec->base = base;
905 
906 	sec->sh.sh_type = SHT_REL;
907 	sec->sh.sh_addralign = 8;
908 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
909 	sec->sh.sh_info = base->idx;
910 	sec->sh.sh_flags = SHF_INFO_LINK;
911 
912 	return sec;
913 }
914 
915 static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
916 {
917 	char *relocname;
918 	struct section *sec;
919 
920 	relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
921 	if (!relocname) {
922 		perror("malloc");
923 		return NULL;
924 	}
925 	strcpy(relocname, ".rela");
926 	strcat(relocname, base->name);
927 
928 	sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
929 	free(relocname);
930 	if (!sec)
931 		return NULL;
932 
933 	base->reloc = sec;
934 	sec->base = base;
935 
936 	sec->sh.sh_type = SHT_RELA;
937 	sec->sh.sh_addralign = 8;
938 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
939 	sec->sh.sh_info = base->idx;
940 	sec->sh.sh_flags = SHF_INFO_LINK;
941 
942 	return sec;
943 }
944 
945 static struct section *elf_create_reloc_section(struct elf *elf,
946 					 struct section *base,
947 					 int reltype)
948 {
949 	switch (reltype) {
950 	case SHT_REL:  return elf_create_rel_reloc_section(elf, base);
951 	case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
952 	default:       return NULL;
953 	}
954 }
955 
956 static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
957 {
958 	struct reloc *reloc;
959 	int idx = 0, size;
960 	void *buf;
961 
962 	/* Allocate a buffer for relocations */
963 	size = nr * sizeof(GElf_Rel);
964 	buf = malloc(size);
965 	if (!buf) {
966 		perror("malloc");
967 		return -1;
968 	}
969 
970 	sec->data->d_buf = buf;
971 	sec->data->d_size = size;
972 	sec->data->d_type = ELF_T_REL;
973 
974 	sec->sh.sh_size = size;
975 
976 	idx = 0;
977 	list_for_each_entry(reloc, &sec->reloc_list, list) {
978 		reloc->rel.r_offset = reloc->offset;
979 		reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
980 		gelf_update_rel(sec->data, idx, &reloc->rel);
981 		idx++;
982 	}
983 
984 	return 0;
985 }
986 
987 static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
988 {
989 	struct reloc *reloc;
990 	int idx = 0, size;
991 	void *buf;
992 
993 	/* Allocate a buffer for relocations with addends */
994 	size = nr * sizeof(GElf_Rela);
995 	buf = malloc(size);
996 	if (!buf) {
997 		perror("malloc");
998 		return -1;
999 	}
1000 
1001 	sec->data->d_buf = buf;
1002 	sec->data->d_size = size;
1003 	sec->data->d_type = ELF_T_RELA;
1004 
1005 	sec->sh.sh_size = size;
1006 
1007 	idx = 0;
1008 	list_for_each_entry(reloc, &sec->reloc_list, list) {
1009 		reloc->rela.r_offset = reloc->offset;
1010 		reloc->rela.r_addend = reloc->addend;
1011 		reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1012 		gelf_update_rela(sec->data, idx, &reloc->rela);
1013 		idx++;
1014 	}
1015 
1016 	return 0;
1017 }
1018 
1019 static int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
1020 {
1021 	struct reloc *reloc;
1022 	int nr;
1023 
1024 	nr = 0;
1025 	list_for_each_entry(reloc, &sec->reloc_list, list)
1026 		nr++;
1027 
1028 	switch (sec->sh.sh_type) {
1029 	case SHT_REL:  return elf_rebuild_rel_reloc_section(sec, nr);
1030 	case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr);
1031 	default:       return -1;
1032 	}
1033 }
1034 
1035 int elf_write_insn(struct elf *elf, struct section *sec,
1036 		   unsigned long offset, unsigned int len,
1037 		   const char *insn)
1038 {
1039 	Elf_Data *data = sec->data;
1040 
1041 	if (data->d_type != ELF_T_BYTE || data->d_off) {
1042 		WARN("write to unexpected data for section: %s", sec->name);
1043 		return -1;
1044 	}
1045 
1046 	memcpy(data->d_buf + offset, insn, len);
1047 	elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
1048 
1049 	elf->changed = true;
1050 
1051 	return 0;
1052 }
1053 
1054 int elf_write_reloc(struct elf *elf, struct reloc *reloc)
1055 {
1056 	struct section *sec = reloc->sec;
1057 
1058 	if (sec->sh.sh_type == SHT_REL) {
1059 		reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1060 		reloc->rel.r_offset = reloc->offset;
1061 
1062 		if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
1063 			WARN_ELF("gelf_update_rel");
1064 			return -1;
1065 		}
1066 	} else {
1067 		reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
1068 		reloc->rela.r_addend = reloc->addend;
1069 		reloc->rela.r_offset = reloc->offset;
1070 
1071 		if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
1072 			WARN_ELF("gelf_update_rela");
1073 			return -1;
1074 		}
1075 	}
1076 
1077 	elf->changed = true;
1078 
1079 	return 0;
1080 }
1081 
1082 int elf_write(struct elf *elf)
1083 {
1084 	struct section *sec;
1085 	Elf_Scn *s;
1086 
1087 	/* Update changed relocation sections and section headers: */
1088 	list_for_each_entry(sec, &elf->sections, list) {
1089 		if (sec->changed) {
1090 			if (sec->base &&
1091 			    elf_rebuild_reloc_section(elf, sec)) {
1092 				WARN("elf_rebuild_reloc_section");
1093 				return -1;
1094 			}
1095 
1096 			s = elf_getscn(elf->elf, sec->idx);
1097 			if (!s) {
1098 				WARN_ELF("elf_getscn");
1099 				return -1;
1100 			}
1101 			if (!gelf_update_shdr(s, &sec->sh)) {
1102 				WARN_ELF("gelf_update_shdr");
1103 				return -1;
1104 			}
1105 
1106 			sec->changed = false;
1107 			elf->changed = true;
1108 		}
1109 	}
1110 
1111 	/* Make sure the new section header entries get updated properly. */
1112 	elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1113 
1114 	/* Write all changes to the file. */
1115 	if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1116 		WARN_ELF("elf_update");
1117 		return -1;
1118 	}
1119 
1120 	elf->changed = false;
1121 
1122 	return 0;
1123 }
1124 
1125 void elf_close(struct elf *elf)
1126 {
1127 	struct section *sec, *tmpsec;
1128 	struct symbol *sym, *tmpsym;
1129 	struct reloc *reloc, *tmpreloc;
1130 
1131 	if (elf->elf)
1132 		elf_end(elf->elf);
1133 
1134 	if (elf->fd > 0)
1135 		close(elf->fd);
1136 
1137 	list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
1138 		list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
1139 			list_del(&sym->list);
1140 			hash_del(&sym->hash);
1141 			free(sym);
1142 		}
1143 		list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
1144 			list_del(&reloc->list);
1145 			hash_del(&reloc->hash);
1146 			free(reloc);
1147 		}
1148 		list_del(&sec->list);
1149 		free(sec);
1150 	}
1151 
1152 	free(elf);
1153 }
1154