xref: /linux/tools/objtool/elf.c (revision cdea5cadb0ca403b1929f8d29929c0eda0f715d6)
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 <sys/mman.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <errno.h>
19 #include <libgen.h>
20 #include <ctype.h>
21 #include <linux/align.h>
22 #include <linux/kernel.h>
23 #include <linux/interval_tree_generic.h>
24 #include <linux/log2.h>
25 #include <objtool/builtin.h>
26 #include <objtool/elf.h>
27 #include <objtool/warn.h>
28 
29 static ssize_t demangled_name_len(const char *name);
30 
31 static inline u32 str_hash(const char *str)
32 {
33 	return jhash(str, strlen(str), 0);
34 }
35 
36 static inline u32 str_hash_demangled(const char *str)
37 {
38 	return jhash(str, demangled_name_len(str), 0);
39 }
40 
41 #define __elf_table(name)	(elf->name##_hash)
42 #define __elf_bits(name)	(elf->name##_bits)
43 
44 #define __elf_table_entry(name, key) \
45 	__elf_table(name)[hash_min(key, __elf_bits(name))]
46 
47 #define elf_hash_add(name, node, key)					\
48 ({									\
49 	struct elf_hash_node *__node = node;				\
50 	__node->next = __elf_table_entry(name, key);			\
51 	__elf_table_entry(name, key) = __node;				\
52 })
53 
54 static inline void __elf_hash_del(struct elf_hash_node *node,
55 				  struct elf_hash_node **head)
56 {
57 	struct elf_hash_node *cur, *prev;
58 
59 	if (node == *head) {
60 		*head = node->next;
61 		return;
62 	}
63 
64 	for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
65 		if (cur == node) {
66 			prev->next = cur->next;
67 			break;
68 		}
69 	}
70 }
71 
72 #define elf_hash_del(name, node, key) \
73 	__elf_hash_del(node, &__elf_table_entry(name, key))
74 
75 #define elf_list_entry(ptr, type, member)				\
76 ({									\
77 	typeof(ptr) __ptr = (ptr);					\
78 	__ptr ? container_of(__ptr, type, member) : NULL;		\
79 })
80 
81 #define elf_hash_for_each_possible(name, obj, member, key)		\
82 	for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
83 	     obj;							\
84 	     obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
85 
86 #define elf_alloc_hash(name, size)					\
87 ({									\
88 	__elf_bits(name) = max(10, ilog2(size));			\
89 	__elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
90 				 PROT_READ|PROT_WRITE,			\
91 				 MAP_PRIVATE|MAP_ANON, -1, 0);		\
92 	if (__elf_table(name) == (void *)-1L) {				\
93 		ERROR_GLIBC("mmap fail " #name);			\
94 		__elf_table(name) = NULL;				\
95 	}								\
96 	__elf_table(name);						\
97 })
98 
99 static inline unsigned long __sym_start(struct symbol *s)
100 {
101 	return s->offset;
102 }
103 
104 static inline unsigned long __sym_last(struct symbol *s)
105 {
106 	return s->offset + (s->len ? s->len - 1 : 0);
107 }
108 
109 INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
110 		     __sym_start, __sym_last, static inline __maybe_unused,
111 		     __sym)
112 
113 #define __sym_for_each(_iter, _tree, _start, _end)			\
114 	for (_iter = __sym_iter_first((_tree), (_start), (_end));	\
115 	     _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
116 
117 struct symbol_hole {
118 	unsigned long key;
119 	const struct symbol *sym;
120 };
121 
122 /*
123  * Find the last symbol before @offset.
124  */
125 static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
126 {
127 	const struct symbol *s = rb_entry(node, struct symbol, node);
128 	struct symbol_hole *sh = (void *)key;
129 
130 	if (sh->key < s->offset)
131 		return -1;
132 
133 	if (sh->key >= s->offset + s->len) {
134 		sh->sym = s;
135 		return 1;
136 	}
137 
138 	return 0;
139 }
140 
141 struct section *find_section_by_name(const struct elf *elf, const char *name)
142 {
143 	struct section *sec;
144 
145 	elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
146 		if (!strcmp(sec->name, name))
147 			return sec;
148 	}
149 
150 	return NULL;
151 }
152 
153 static struct section *find_section_by_index(struct elf *elf,
154 					     unsigned int idx)
155 {
156 	struct section *sec;
157 
158 	elf_hash_for_each_possible(section, sec, hash, idx) {
159 		if (sec->idx == idx)
160 			return sec;
161 	}
162 
163 	return NULL;
164 }
165 
166 static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
167 {
168 	struct symbol *sym;
169 
170 	elf_hash_for_each_possible(symbol, sym, hash, idx) {
171 		if (sym->idx == idx)
172 			return sym;
173 	}
174 
175 	return NULL;
176 }
177 
178 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
179 {
180 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
181 	struct symbol *sym;
182 
183 	__sym_for_each(sym, tree, offset, offset) {
184 		if (sym->offset == offset && !is_sec_sym(sym))
185 			return sym->alias;
186 	}
187 
188 	return NULL;
189 }
190 
191 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
192 {
193 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
194 	struct symbol *func;
195 
196 	__sym_for_each(func, tree, offset, offset) {
197 		if (func->offset == offset && is_func_sym(func))
198 			return func->alias;
199 	}
200 
201 	return NULL;
202 }
203 
204 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
205 {
206 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
207 	struct symbol *sym = NULL, *tmp;
208 
209 	__sym_for_each(tmp, tree, offset, offset) {
210 		if (tmp->len) {
211 			if (!sym) {
212 				sym = tmp;
213 				continue;
214 			}
215 
216 			if (sym->offset != tmp->offset || sym->len != tmp->len) {
217 				/*
218 				 * In the rare case of overlapping symbols,
219 				 * pick the smaller one.
220 				 *
221 				 * TODO: outlaw overlapping symbols
222 				 */
223 				if (tmp->len < sym->len)
224 					sym = tmp;
225 			}
226 		}
227 	}
228 
229 	return sym ? sym->alias : NULL;
230 }
231 
232 /*
233  * Returns size of hole starting at @offset.
234  */
235 int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
236 {
237 	struct symbol_hole hole = {
238 		.key = offset,
239 		.sym = NULL,
240 	};
241 	struct rb_node *n;
242 	struct symbol *s;
243 
244 	/*
245 	 * Find the rightmost symbol for which @offset is after it.
246 	 */
247 	n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
248 
249 	/* found a symbol that contains @offset */
250 	if (n)
251 		return 0; /* not a hole */
252 
253 	/*
254 	 * @offset >= sym->offset + sym->len, find symbol after it.
255 	 * When hole.sym is empty, use the first node to compute the hole.
256 	 * If there is no symbol in the section, the first node will be NULL,
257 	 * in which case, -1 is returned to skip the whole section.
258 	 */
259 	if (hole.sym)
260 		n = rb_next(&hole.sym->node);
261 	else
262 		n = rb_first_cached(&sec->symbol_tree);
263 
264 	if (!n)
265 		return -1; /* until end of address space */
266 
267 	/* hole until start of next symbol */
268 	s = rb_entry(n, struct symbol, node);
269 	return s->offset - offset;
270 }
271 
272 struct symbol *find_func_containing(struct section *sec, unsigned long offset)
273 {
274 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
275 	struct symbol *func;
276 
277 	__sym_for_each(func, tree, offset, offset) {
278 		if (is_func_sym(func))
279 			return func->alias;
280 	}
281 
282 	return NULL;
283 }
284 
285 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
286 {
287 	struct symbol *sym;
288 
289 	elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
290 		if (!strcmp(sym->name, name))
291 			return sym;
292 	}
293 
294 	return NULL;
295 }
296 
297 /* Find local symbol with matching STT_FILE */
298 static struct symbol *find_local_symbol_by_file_and_name(const struct elf *elf,
299 							 struct symbol *file,
300 							 const char *name)
301 {
302 	struct symbol *sym;
303 
304 	elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash_demangled(name)) {
305 		if (sym->bind == STB_LOCAL && sym->file == file &&
306 		    !strcmp(sym->name, name)) {
307 			return sym;
308 		}
309 	}
310 
311 	return NULL;
312 }
313 
314 struct symbol *find_global_symbol_by_name(const struct elf *elf, const char *name)
315 {
316 	struct symbol *sym;
317 
318 	elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash_demangled(name)) {
319 		if (!strcmp(sym->name, name) && !is_local_sym(sym))
320 			return sym;
321 	}
322 
323 	return NULL;
324 }
325 
326 void iterate_global_symbol_by_demangled_name(const struct elf *elf,
327 					     const char *demangled_name,
328 					     void (*process)(struct symbol *sym, void *data),
329 					     void *data)
330 {
331 	struct symbol *sym;
332 
333 	elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(demangled_name)) {
334 		if (!strcmp(sym->demangled_name, demangled_name) && !is_local_sym(sym))
335 			process(sym, data);
336 	}
337 }
338 
339 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
340 				     unsigned long offset, unsigned int len)
341 {
342 	struct reloc *reloc, *r = NULL;
343 	struct section *rsec;
344 	unsigned long o;
345 
346 	rsec = sec->rsec;
347 	if (!rsec)
348 		return NULL;
349 
350 	for_offset_range(o, offset, offset + len) {
351 		elf_hash_for_each_possible(reloc, reloc, hash,
352 					   sec_offset_hash(rsec, o)) {
353 			if (reloc->sec != rsec)
354 				continue;
355 
356 			if (reloc_offset(reloc) >= offset &&
357 			    reloc_offset(reloc) < offset + len) {
358 				if (!r || reloc_offset(reloc) < reloc_offset(r))
359 					r = reloc;
360 			}
361 		}
362 		if (r)
363 			return r;
364 	}
365 
366 	return NULL;
367 }
368 
369 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
370 {
371 	return find_reloc_by_dest_range(elf, sec, offset, 1);
372 }
373 
374 static bool is_dwarf_section(struct section *sec)
375 {
376 	return !strncmp(sec->name, ".debug_", 7);
377 }
378 
379 static int read_sections(struct elf *elf)
380 {
381 	Elf_Scn *s = NULL;
382 	struct section *sec;
383 	size_t shstrndx, sections_nr;
384 	int i;
385 
386 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
387 		ERROR_ELF("elf_getshdrnum");
388 		return -1;
389 	}
390 
391 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
392 		ERROR_ELF("elf_getshdrstrndx");
393 		return -1;
394 	}
395 
396 	if (!elf_alloc_hash(section, sections_nr) ||
397 	    !elf_alloc_hash(section_name, sections_nr))
398 		return -1;
399 
400 	elf->section_data = calloc(sections_nr, sizeof(*sec));
401 	if (!elf->section_data) {
402 		ERROR_GLIBC("calloc");
403 		return -1;
404 	}
405 	for (i = 0; i < sections_nr; i++) {
406 		sec = &elf->section_data[i];
407 
408 		INIT_LIST_HEAD(&sec->symbol_list);
409 
410 		s = elf_getscn(elf->elf, i);
411 		if (!s) {
412 			ERROR_ELF("elf_getscn");
413 			return -1;
414 		}
415 
416 		sec->idx = elf_ndxscn(s);
417 
418 		if (!gelf_getshdr(s, &sec->sh)) {
419 			ERROR_ELF("gelf_getshdr");
420 			return -1;
421 		}
422 
423 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
424 		if (!sec->name) {
425 			ERROR_ELF("elf_strptr");
426 			return -1;
427 		}
428 
429 		if (sec_size(sec) != 0 && !is_dwarf_section(sec)) {
430 			sec->data = elf_getdata(s, NULL);
431 			if (!sec->data) {
432 				ERROR_ELF("elf_getdata");
433 				return -1;
434 			}
435 			if (sec->data->d_off != 0 ||
436 			    sec->data->d_size != sec_size(sec)) {
437 				ERROR("unexpected data attributes for %s", sec->name);
438 				return -1;
439 			}
440 		}
441 
442 		list_add_tail(&sec->list, &elf->sections);
443 		elf_hash_add(section, &sec->hash, sec->idx);
444 		elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
445 
446 		if (is_reloc_sec(sec))
447 			elf->num_relocs += sec_num_entries(sec);
448 	}
449 
450 	if (opts.stats) {
451 		printf("nr_sections: %lu\n", (unsigned long)sections_nr);
452 		printf("section_bits: %d\n", elf->section_bits);
453 	}
454 
455 	/* sanity check, one more call to elf_nextscn() should return NULL */
456 	if (elf_nextscn(elf->elf, s)) {
457 		ERROR("section entry mismatch");
458 		return -1;
459 	}
460 
461 	return 0;
462 }
463 
464 /*
465  * Returns desired length of the demangled name.
466  * If name doesn't need demangling, return strlen(name).
467  */
468 static ssize_t demangled_name_len(const char *name)
469 {
470 	ssize_t idx;
471 	const char *p;
472 
473 	p = strstr(name, ".llvm.");
474 	if (p)
475 		return p - name;
476 
477 	if (!strstarts(name, "__UNIQUE_ID_") && !strchr(name, '.'))
478 		return strlen(name);
479 
480 	for (idx = strlen(name) - 1; idx >= 0; idx--) {
481 		char c = name[idx];
482 
483 		if (!isdigit(c) && c != '.' && c != '_')
484 			break;
485 	}
486 	if (idx <= 0)
487 		return strlen(name);
488 	return idx + 1;
489 }
490 
491 /*
492  * Remove number suffix of a symbol.
493  *
494  * Specifically, remove trailing numbers for "__UNIQUE_ID_" symbols and
495  * symbols with '.'.
496  *
497  * With CONFIG_LTO_CLANG_THIN, it is possible to have nested __UNIQUE_ID_,
498  * such as
499  *
500  *   __UNIQUE_ID_addressable___UNIQUE_ID_pci_invalid_bar_694_695
501  *
502  * to remove both trailing numbers, also remove trailing '_'.
503  *
504  * For symbols with llvm suffix, i.e., foo.llvm.<hash>, remove the
505  * .llvm.<hash> part.
506  */
507 static const char *demangle_name(struct symbol *sym)
508 {
509 	char *str;
510 	ssize_t len;
511 
512 	if (!is_func_sym(sym) && !is_object_sym(sym))
513 		return sym->name;
514 
515 	len = demangled_name_len(sym->name);
516 	if (len == strlen(sym->name))
517 		return sym->name;
518 
519 	str = strndup(sym->name, len);
520 	if (!str) {
521 		ERROR_GLIBC("strdup");
522 		return NULL;
523 	}
524 
525 	return str;
526 }
527 
528 static int elf_add_symbol(struct elf *elf, struct symbol *sym)
529 {
530 	struct list_head *entry;
531 	struct rb_node *pnode;
532 	struct symbol *iter;
533 
534 	INIT_LIST_HEAD(&sym->pv_target);
535 	sym->alias = sym;
536 
537 	sym->type = GELF_ST_TYPE(sym->sym.st_info);
538 	sym->bind = GELF_ST_BIND(sym->sym.st_info);
539 
540 	if (is_file_sym(sym))
541 		elf->num_files++;
542 
543 	sym->offset = sym->sym.st_value;
544 	sym->len = sym->sym.st_size;
545 
546 	__sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
547 		if (!is_undef_sym(iter) && iter->offset == sym->offset &&
548 		    iter->type == sym->type && iter->len == sym->len)
549 			iter->alias = sym;
550 	}
551 
552 	__sym_insert(sym, &sym->sec->symbol_tree);
553 	pnode = rb_prev(&sym->node);
554 	if (pnode)
555 		entry = &rb_entry(pnode, struct symbol, node)->list;
556 	else
557 		entry = &sym->sec->symbol_list;
558 	list_add(&sym->list, entry);
559 
560 	sym->demangled_name = demangle_name(sym);
561 	if (!sym->demangled_name)
562 		return -1;
563 
564 	list_add_tail(&sym->global_list, &elf->symbols);
565 	elf_hash_add(symbol, &sym->hash, sym->idx);
566 	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->demangled_name));
567 
568 	if (is_func_sym(sym) &&
569 	    (strstarts(sym->name, "__pfx_") ||
570 	     strstarts(sym->name, "__cfi_") ||
571 	     strstarts(sym->name, "__pi___pfx_") ||
572 	     strstarts(sym->name, "__pi___cfi_")))
573 		sym->prefix = 1;
574 
575 	if (strstarts(sym->name, ".klp.sym"))
576 		sym->klp = 1;
577 
578 	if (!sym->klp && !is_sec_sym(sym) && strstr(sym->name, ".cold")) {
579 		sym->cold = 1;
580 
581 		/*
582 		 * Clang doesn't mark cold subfunctions as STT_FUNC, which
583 		 * breaks several objtool assumptions.  Fake it.
584 		 */
585 		sym->type = STT_FUNC;
586 	}
587 
588 	sym->pfunc = sym->cfunc = sym;
589 
590 	return 0;
591 }
592 
593 static int read_symbols(struct elf *elf)
594 {
595 	struct section *symtab, *symtab_shndx, *sec;
596 	struct symbol *sym, *pfunc, *file = NULL;
597 	int symbols_nr, i;
598 	char *coldstr;
599 	Elf_Data *shndx_data = NULL;
600 	Elf32_Word shndx;
601 
602 	symtab = find_section_by_name(elf, ".symtab");
603 	if (symtab) {
604 		symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
605 		if (symtab_shndx)
606 			shndx_data = symtab_shndx->data;
607 
608 		symbols_nr = sec_num_entries(symtab);
609 	} else {
610 		/*
611 		 * A missing symbol table is actually possible if it's an empty
612 		 * .o file. This can happen for thunk_64.o. Make sure to at
613 		 * least allocate the symbol hash tables so we can do symbol
614 		 * lookups without crashing.
615 		 */
616 		symbols_nr = 0;
617 	}
618 
619 	if (!elf_alloc_hash(symbol, symbols_nr) ||
620 	    !elf_alloc_hash(symbol_name, symbols_nr))
621 		return -1;
622 
623 	elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
624 	if (!elf->symbol_data) {
625 		ERROR_GLIBC("calloc");
626 		return -1;
627 	}
628 
629 	INIT_LIST_HEAD(&elf->symbols);
630 
631 	for (i = 0; i < symbols_nr; i++) {
632 		sym = &elf->symbol_data[i];
633 
634 		sym->idx = i;
635 
636 		if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
637 				      &shndx)) {
638 			ERROR_ELF("gelf_getsymshndx");
639 			return -1;
640 		}
641 
642 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
643 				       sym->sym.st_name);
644 		if (!sym->name) {
645 			ERROR_ELF("elf_strptr");
646 			return -1;
647 		}
648 
649 		if ((sym->sym.st_shndx > SHN_UNDEF &&
650 		     sym->sym.st_shndx < SHN_LORESERVE) ||
651 		    (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
652 			if (sym->sym.st_shndx != SHN_XINDEX)
653 				shndx = sym->sym.st_shndx;
654 
655 			sym->sec = find_section_by_index(elf, shndx);
656 			if (!sym->sec) {
657 				ERROR("couldn't find section for symbol %s", sym->name);
658 				return -1;
659 			}
660 			if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
661 				sym->name = sym->sec->name;
662 				sym->sec->sym = sym;
663 			}
664 		} else
665 			sym->sec = find_section_by_index(elf, 0);
666 
667 		if (elf_add_symbol(elf, sym))
668 			return -1;
669 
670 		if (is_file_sym(sym))
671 			file = sym;
672 		else if (sym->bind == STB_LOCAL)
673 			sym->file = file;
674 	}
675 
676 	if (opts.stats) {
677 		printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
678 		printf("symbol_bits: %d\n", elf->symbol_bits);
679 	}
680 
681 	/* Create parent/child links for any cold subfunctions */
682 	list_for_each_entry(sec, &elf->sections, list) {
683 		sec_for_each_sym(sec, sym) {
684 			char *pname;
685 			size_t pnamelen;
686 
687 			if (!sym->cold)
688 				continue;
689 
690 			coldstr = strstr(sym->name, ".cold");
691 			if (!coldstr) {
692 				ERROR("%s(): cold subfunction without \".cold\"?", sym->name);
693 				return -1;
694 			}
695 
696 			pnamelen = coldstr - sym->name;
697 			pname = strndup(sym->name, pnamelen);
698 			if (!pname) {
699 				ERROR("%s(): failed to allocate memory", sym->name);
700 				return -1;
701 			}
702 
703 			pfunc = find_local_symbol_by_file_and_name(elf, sym->file, pname);
704 			if (!pfunc)
705 				pfunc = find_global_symbol_by_name(elf, pname);
706 			free(pname);
707 
708 			if (!pfunc) {
709 				ERROR("%s(): can't find parent function", sym->name);
710 				return -1;
711 			}
712 
713 			sym->pfunc = pfunc->alias;
714 			pfunc->cfunc = sym;
715 			pfunc->alias->cfunc = sym;
716 
717 			/*
718 			 * Unfortunately, -fnoreorder-functions puts the child
719 			 * inside the parent.  Remove the overlap so we can
720 			 * have sane assumptions.
721 			 *
722 			 * Note that pfunc->len now no longer matches
723 			 * pfunc->sym.st_size.
724 			 */
725 			if (sym->sec == pfunc->sec &&
726 			    sym->offset >= pfunc->offset &&
727 			    sym->offset + sym->len == pfunc->offset + pfunc->len) {
728 				pfunc->len -= sym->len;
729 			}
730 		}
731 	}
732 
733 	return 0;
734 }
735 
736 static int mark_group_syms(struct elf *elf)
737 {
738 	struct section *symtab, *sec;
739 	struct symbol *sym;
740 
741 	symtab = find_section_by_name(elf, ".symtab");
742 	if (!symtab) {
743 		ERROR("no .symtab");
744 		return -1;
745 	}
746 
747 	for_each_sec(elf, sec) {
748 		if (sec->sh.sh_type == SHT_GROUP &&
749 		    sec->sh.sh_link == symtab->idx) {
750 			sym = find_symbol_by_index(elf, sec->sh.sh_info);
751 			if (!sym) {
752 				ERROR("%s: can't find SHT_GROUP signature symbol",
753 				      sec->name);
754 				return -1;
755 			}
756 
757 			sym->group_sec = sec;
758 		}
759 	}
760 
761 	return 0;
762 }
763 
764 /*
765  * @sym's idx has changed.  Update the relocs which reference it.
766  */
767 static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
768 {
769 	struct reloc *reloc;
770 
771 	for (reloc = sym->relocs; reloc; reloc = sym_next_reloc(reloc))
772 		set_reloc_sym(elf, reloc, reloc->sym->idx);
773 
774 	return 0;
775 }
776 
777 /*
778  * The libelf API is terrible; gelf_update_sym*() takes a data block relative
779  * index value, *NOT* the symbol index. As such, iterate the data blocks and
780  * adjust index until it fits.
781  *
782  * If no data block is found, allow adding a new data block provided the index
783  * is only one past the end.
784  */
785 static int elf_update_symbol(struct elf *elf, struct section *symtab,
786 			     struct section *symtab_shndx, struct symbol *sym)
787 {
788 	Elf32_Word shndx;
789 	Elf_Data *symtab_data = NULL, *shndx_data = NULL;
790 	Elf64_Xword entsize = symtab->sh.sh_entsize;
791 	int max_idx, idx = sym->idx;
792 	Elf_Scn *s, *t = NULL;
793 	bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
794 				sym->sym.st_shndx != SHN_XINDEX;
795 
796 	shndx = is_special_shndx ? sym->sym.st_shndx : sym->sec->idx;
797 
798 	s = elf_getscn(elf->elf, symtab->idx);
799 	if (!s) {
800 		ERROR_ELF("elf_getscn");
801 		return -1;
802 	}
803 
804 	if (symtab_shndx) {
805 		t = elf_getscn(elf->elf, symtab_shndx->idx);
806 		if (!t) {
807 			ERROR_ELF("elf_getscn");
808 			return -1;
809 		}
810 	}
811 
812 	for (;;) {
813 		/* get next data descriptor for the relevant sections */
814 		symtab_data = elf_getdata(s, symtab_data);
815 		if (t)
816 			shndx_data = elf_getdata(t, shndx_data);
817 
818 		/* end-of-list */
819 		if (!symtab_data) {
820 			/*
821 			 * Over-allocate to avoid O(n^2) symbol creation
822 			 * behaviour.  The down side is that libelf doesn't
823 			 * like this; see elf_truncate_section() for the fixup.
824 			 */
825 			int num = max(1U, sym->idx/3);
826 			void *buf;
827 
828 			if (idx) {
829 				/* we don't do holes in symbol tables */
830 				ERROR("index out of range");
831 				return -1;
832 			}
833 
834 			/* if @idx == 0, it's the next contiguous entry, create it */
835 			symtab_data = elf_newdata(s);
836 			if (t)
837 				shndx_data = elf_newdata(t);
838 
839 			buf = calloc(num, entsize);
840 			if (!buf) {
841 				ERROR_GLIBC("calloc");
842 				return -1;
843 			}
844 
845 			symtab_data->d_buf = buf;
846 			symtab_data->d_size = num * entsize;
847 			symtab_data->d_align = 1;
848 			symtab_data->d_type = ELF_T_SYM;
849 
850 			mark_sec_changed(elf, symtab, true);
851 			symtab->truncate = true;
852 
853 			if (t) {
854 				buf = calloc(num, sizeof(Elf32_Word));
855 				if (!buf) {
856 					ERROR_GLIBC("calloc");
857 					return -1;
858 				}
859 
860 				shndx_data->d_buf = buf;
861 				shndx_data->d_size = num * sizeof(Elf32_Word);
862 				shndx_data->d_align = sizeof(Elf32_Word);
863 				shndx_data->d_type = ELF_T_WORD;
864 
865 				mark_sec_changed(elf, symtab_shndx, true);
866 				symtab_shndx->truncate = true;
867 			}
868 
869 			break;
870 		}
871 
872 		/* empty blocks should not happen */
873 		if (!symtab_data->d_size) {
874 			ERROR("zero size data");
875 			return -1;
876 		}
877 
878 		/* is this the right block? */
879 		max_idx = symtab_data->d_size / entsize;
880 		if (idx < max_idx)
881 			break;
882 
883 		/* adjust index and try again */
884 		idx -= max_idx;
885 	}
886 
887 	/* something went side-ways */
888 	if (idx < 0) {
889 		ERROR("negative index");
890 		return -1;
891 	}
892 
893 	/* setup extended section index magic and write the symbol */
894 	if (shndx < SHN_LORESERVE || is_special_shndx) {
895 		sym->sym.st_shndx = shndx;
896 		if (!shndx_data)
897 			shndx = 0;
898 	} else {
899 		sym->sym.st_shndx = SHN_XINDEX;
900 		if (!shndx_data) {
901 			ERROR("no .symtab_shndx");
902 			return -1;
903 		}
904 	}
905 
906 	if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
907 		ERROR_ELF("gelf_update_symshndx");
908 		return -1;
909 	}
910 
911 	return 0;
912 }
913 
914 struct symbol *elf_create_symbol(struct elf *elf, const char *name,
915 				 struct section *sec, unsigned int bind,
916 				 unsigned int type, unsigned long offset,
917 				 size_t size)
918 {
919 	struct section *symtab, *symtab_shndx;
920 	Elf32_Word first_non_local, new_idx;
921 	struct symbol *old, *sym;
922 
923 	sym = calloc(1, sizeof(*sym));
924 	if (!sym) {
925 		ERROR_GLIBC("calloc");
926 		return NULL;
927 	}
928 
929 	sym->name = strdup(name);
930 	if (!sym->name) {
931 		ERROR_GLIBC("strdup");
932 		return NULL;
933 	}
934 
935 	if (type != STT_SECTION) {
936 		sym->sym.st_name = elf_add_string(elf, NULL, sym->name);
937 		if (sym->sym.st_name == -1)
938 			return NULL;
939 	}
940 
941 	if (sec) {
942 		sym->sec = sec;
943 	} else {
944 		sym->sec = find_section_by_index(elf, 0);
945 		if (!sym->sec) {
946 			ERROR("no NULL section");
947 			return NULL;
948 		}
949 	}
950 
951 	sym->sym.st_info  = GELF_ST_INFO(bind, type);
952 	sym->sym.st_value = offset;
953 	sym->sym.st_size  = size;
954 
955 	symtab = find_section_by_name(elf, ".symtab");
956 	if (!symtab) {
957 		ERROR("no .symtab");
958 		return NULL;
959 	}
960 
961 	symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
962 
963 	new_idx = sec_num_entries(symtab);
964 
965 	if (bind != STB_LOCAL)
966 		goto non_local;
967 
968 	/*
969 	 * Move the first global symbol, as per sh_info, into a new, higher
970 	 * symbol index. This frees up a spot for a new local symbol.
971 	 */
972 	first_non_local = symtab->sh.sh_info;
973 	old = find_symbol_by_index(elf, first_non_local);
974 	if (old) {
975 
976 		elf_hash_del(symbol, &old->hash, old->idx);
977 		elf_hash_add(symbol, &old->hash, new_idx);
978 		old->idx = new_idx;
979 
980 		if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
981 			ERROR("elf_update_symbol move");
982 			return NULL;
983 		}
984 
985 		if (elf_update_sym_relocs(elf, old))
986 			return NULL;
987 
988 		if (old->group_sec) {
989 			old->group_sec->sh.sh_info = new_idx;
990 			mark_sec_changed(elf, old->group_sec, true);
991 		}
992 
993 		new_idx = first_non_local;
994 	}
995 
996 	/*
997 	 * Either way, we will add a LOCAL symbol.
998 	 */
999 	symtab->sh.sh_info += 1;
1000 
1001 non_local:
1002 	sym->idx = new_idx;
1003 	if (sym->idx && elf_update_symbol(elf, symtab, symtab_shndx, sym))
1004 		return NULL;
1005 
1006 	symtab->sh.sh_size += symtab->sh.sh_entsize;
1007 	mark_sec_changed(elf, symtab, true);
1008 
1009 	if (symtab_shndx) {
1010 		symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
1011 		mark_sec_changed(elf, symtab_shndx, true);
1012 	}
1013 
1014 	if (elf_add_symbol(elf, sym))
1015 		return NULL;
1016 
1017 	return sym;
1018 }
1019 
1020 struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec)
1021 {
1022 	struct symbol *sym = calloc(1, sizeof(*sym));
1023 
1024 	sym = elf_create_symbol(elf, sec->name, sec, STB_LOCAL, STT_SECTION, 0, 0);
1025 	if (!sym)
1026 		return NULL;
1027 
1028 	sec->sym = sym;
1029 
1030 	return sym;
1031 }
1032 
1033 struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
1034 			     unsigned int reloc_idx, unsigned long offset,
1035 			     struct symbol *sym, s64 addend, unsigned int type)
1036 {
1037 	struct reloc *reloc, empty = { 0 };
1038 
1039 	if (reloc_idx >= sec_num_entries(rsec)) {
1040 		ERROR("%s: bad reloc_idx %u for %s with %d relocs",
1041 		      __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
1042 		return NULL;
1043 	}
1044 
1045 	reloc = &rsec->relocs[reloc_idx];
1046 
1047 	if (memcmp(reloc, &empty, sizeof(empty))) {
1048 		ERROR("%s: %s: reloc %d already initialized!",
1049 		      __func__, rsec->name, reloc_idx);
1050 		return NULL;
1051 	}
1052 
1053 	reloc->sec = rsec;
1054 	reloc->sym = sym;
1055 
1056 	set_reloc_offset(elf, reloc, offset);
1057 	set_reloc_sym(elf, reloc, sym->idx);
1058 	set_reloc_type(elf, reloc, type);
1059 	set_reloc_addend(elf, reloc, addend);
1060 
1061 	elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
1062 	set_sym_next_reloc(reloc, sym->relocs);
1063 	sym->relocs = reloc;
1064 
1065 	return reloc;
1066 }
1067 
1068 struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
1069 				      unsigned long offset,
1070 				      unsigned int reloc_idx,
1071 				      struct section *insn_sec,
1072 				      unsigned long insn_off)
1073 {
1074 	struct symbol *sym = insn_sec->sym;
1075 	s64 addend = insn_off;
1076 
1077 	if (!is_text_sec(insn_sec)) {
1078 		ERROR("bad call to %s() for data symbol %s", __func__, sym->name);
1079 		return NULL;
1080 	}
1081 
1082 	if (!sym) {
1083 		/*
1084 		 * Due to how weak functions work, we must use section based
1085 		 * relocations. Symbol based relocations would result in the
1086 		 * weak and non-weak function annotations being overlaid on the
1087 		 * non-weak function after linking.
1088 		 */
1089 		sym = elf_create_section_symbol(elf, insn_sec);
1090 		if (!sym)
1091 			return NULL;
1092 	}
1093 
1094 	return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
1095 			      elf_text_rela_type(elf));
1096 }
1097 
1098 struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
1099 				      unsigned long offset,
1100 				      unsigned int reloc_idx,
1101 				      struct symbol *sym,
1102 				      s64 addend)
1103 {
1104 	if (is_text_sec(sec)) {
1105 		ERROR("bad call to %s() for text symbol %s", __func__, sym->name);
1106 		return NULL;
1107 	}
1108 
1109 	return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
1110 			      elf_data_rela_type(elf));
1111 }
1112 
1113 static int read_relocs(struct elf *elf)
1114 {
1115 	unsigned long nr_reloc, max_reloc = 0;
1116 	struct section *rsec;
1117 	struct reloc *reloc;
1118 	unsigned int symndx;
1119 	struct symbol *sym;
1120 	int i;
1121 
1122 	if (!elf_alloc_hash(reloc, elf->num_relocs))
1123 		return -1;
1124 
1125 	list_for_each_entry(rsec, &elf->sections, list) {
1126 		if (!is_reloc_sec(rsec))
1127 			continue;
1128 
1129 		rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
1130 		if (!rsec->base) {
1131 			ERROR("can't find base section for reloc section %s", rsec->name);
1132 			return -1;
1133 		}
1134 
1135 		rsec->base->rsec = rsec;
1136 
1137 		/* nr_alloc_relocs=0: libelf owns d_buf */
1138 		rsec->nr_alloc_relocs = 0;
1139 
1140 		rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
1141 		if (!rsec->relocs) {
1142 			ERROR_GLIBC("calloc");
1143 			return -1;
1144 		}
1145 
1146 		nr_reloc = 0;
1147 		for (i = 0; i < sec_num_entries(rsec); i++) {
1148 			reloc = &rsec->relocs[i];
1149 
1150 			reloc->sec = rsec;
1151 			symndx = reloc_sym(reloc);
1152 			reloc->sym = sym = find_symbol_by_index(elf, symndx);
1153 			if (!reloc->sym) {
1154 				ERROR("can't find reloc entry symbol %d for %s", symndx, rsec->name);
1155 				return -1;
1156 			}
1157 
1158 			elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
1159 			set_sym_next_reloc(reloc, sym->relocs);
1160 			sym->relocs = reloc;
1161 
1162 			nr_reloc++;
1163 		}
1164 		max_reloc = max(max_reloc, nr_reloc);
1165 	}
1166 
1167 	if (opts.stats) {
1168 		printf("max_reloc: %lu\n", max_reloc);
1169 		printf("num_relocs: %lu\n", elf->num_relocs);
1170 		printf("reloc_bits: %d\n", elf->reloc_bits);
1171 	}
1172 
1173 	return 0;
1174 }
1175 
1176 struct elf *elf_open_read(const char *name, int flags)
1177 {
1178 	struct elf *elf;
1179 	Elf_Cmd cmd;
1180 
1181 	elf_version(EV_CURRENT);
1182 
1183 	elf = malloc(sizeof(*elf));
1184 	if (!elf) {
1185 		ERROR_GLIBC("malloc");
1186 		return NULL;
1187 	}
1188 	memset(elf, 0, sizeof(*elf));
1189 
1190 	INIT_LIST_HEAD(&elf->sections);
1191 
1192 	elf->fd = open(name, flags);
1193 	if (elf->fd == -1) {
1194 		fprintf(stderr, "objtool: Can't open '%s': %s\n",
1195 			name, strerror(errno));
1196 		goto err;
1197 	}
1198 
1199 	elf->name = strdup(name);
1200 	if (!elf->name) {
1201 		ERROR_GLIBC("strdup");
1202 		return NULL;
1203 	}
1204 
1205 	if ((flags & O_ACCMODE) == O_RDONLY)
1206 		cmd = ELF_C_READ_MMAP;
1207 	else if ((flags & O_ACCMODE) == O_RDWR)
1208 		cmd = ELF_C_RDWR;
1209 	else /* O_WRONLY */
1210 		cmd = ELF_C_WRITE;
1211 
1212 	elf->elf = elf_begin(elf->fd, cmd, NULL);
1213 	if (!elf->elf) {
1214 		ERROR_ELF("elf_begin");
1215 		goto err;
1216 	}
1217 
1218 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1219 		ERROR_ELF("gelf_getehdr");
1220 		goto err;
1221 	}
1222 
1223 	if (read_sections(elf))
1224 		goto err;
1225 
1226 	if (read_symbols(elf))
1227 		goto err;
1228 
1229 	if (mark_group_syms(elf))
1230 		goto err;
1231 
1232 	if (read_relocs(elf))
1233 		goto err;
1234 
1235 	return elf;
1236 
1237 err:
1238 	elf_close(elf);
1239 	return NULL;
1240 }
1241 
1242 struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
1243 {
1244 	struct section *null, *symtab, *strtab, *shstrtab;
1245 	char *dir, *base, *tmp_name;
1246 	struct symbol *sym;
1247 	struct elf *elf;
1248 
1249 	elf_version(EV_CURRENT);
1250 
1251 	elf = calloc(1, sizeof(*elf));
1252 	if (!elf) {
1253 		ERROR_GLIBC("calloc");
1254 		return NULL;
1255 	}
1256 
1257 	INIT_LIST_HEAD(&elf->sections);
1258 
1259 	dir = strdup(name);
1260 	if (!dir) {
1261 		ERROR_GLIBC("strdup");
1262 		return NULL;
1263 	}
1264 
1265 	dir = dirname(dir);
1266 
1267 	base = strdup(name);
1268 	if (!base) {
1269 		ERROR_GLIBC("strdup");
1270 		return NULL;
1271 	}
1272 
1273 	base = basename(base);
1274 
1275 	tmp_name = malloc(256);
1276 	if (!tmp_name) {
1277 		ERROR_GLIBC("malloc");
1278 		return NULL;
1279 	}
1280 
1281 	snprintf(tmp_name, 256, "%s/%s.XXXXXX", dir, base);
1282 
1283 	elf->fd = mkstemp(tmp_name);
1284 	if (elf->fd == -1) {
1285 		ERROR_GLIBC("can't create tmp file");
1286 		exit(1);
1287 	}
1288 
1289 	elf->tmp_name = tmp_name;
1290 
1291 	elf->name = strdup(name);
1292 	if (!elf->name) {
1293 		ERROR_GLIBC("strdup");
1294 		return NULL;
1295 	}
1296 
1297 	elf->elf = elf_begin(elf->fd, ELF_C_WRITE, NULL);
1298 	if (!elf->elf) {
1299 		ERROR_ELF("elf_begin");
1300 		return NULL;
1301 	}
1302 
1303 	if (!gelf_newehdr(elf->elf, ELFCLASS64)) {
1304 		ERROR_ELF("gelf_newehdr");
1305 		return NULL;
1306 	}
1307 
1308 	memcpy(&elf->ehdr, ehdr, sizeof(elf->ehdr));
1309 
1310 	if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) {
1311 		ERROR_ELF("gelf_update_ehdr");
1312 		return NULL;
1313 	}
1314 
1315 	INIT_LIST_HEAD(&elf->symbols);
1316 
1317 	if (!elf_alloc_hash(section,		1000) ||
1318 	    !elf_alloc_hash(section_name,	1000) ||
1319 	    !elf_alloc_hash(symbol,		10000) ||
1320 	    !elf_alloc_hash(symbol_name,	10000) ||
1321 	    !elf_alloc_hash(reloc,		100000))
1322 		return NULL;
1323 
1324 	null		= elf_create_section(elf, NULL, 0, 0, SHT_NULL, 0, 0);
1325 	shstrtab	= elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0);
1326 	strtab		= elf_create_section(elf, NULL, 0, 0, SHT_STRTAB, 1, 0);
1327 
1328 	if (!null || !shstrtab || !strtab)
1329 		return NULL;
1330 
1331 	null->name	= "";
1332 	shstrtab->name	= ".shstrtab";
1333 	strtab->name	= ".strtab";
1334 
1335 	null->sh.sh_name	= elf_add_string(elf, shstrtab, null->name);
1336 	shstrtab->sh.sh_name	= elf_add_string(elf, shstrtab, shstrtab->name);
1337 	strtab->sh.sh_name	= elf_add_string(elf, shstrtab, strtab->name);
1338 
1339 	if (null->sh.sh_name == -1 || shstrtab->sh.sh_name == -1 || strtab->sh.sh_name == -1)
1340 		return NULL;
1341 
1342 	elf_hash_add(section_name, &null->name_hash,		str_hash(null->name));
1343 	elf_hash_add(section_name, &strtab->name_hash,		str_hash(strtab->name));
1344 	elf_hash_add(section_name, &shstrtab->name_hash,	str_hash(shstrtab->name));
1345 
1346 	if (elf_add_string(elf, strtab, "") == -1)
1347 		return NULL;
1348 
1349 	symtab = elf_create_section(elf, ".symtab", 0x18, 0x18, SHT_SYMTAB, 0x8, 0);
1350 	if (!symtab)
1351 		return NULL;
1352 
1353 	symtab->sh.sh_link = strtab->idx;
1354 	symtab->sh.sh_info = 1;
1355 
1356 	elf->ehdr.e_shstrndx = shstrtab->idx;
1357 	if (!gelf_update_ehdr(elf->elf, &elf->ehdr)) {
1358 		ERROR_ELF("gelf_update_ehdr");
1359 		return NULL;
1360 	}
1361 
1362 	sym = calloc(1, sizeof(*sym));
1363 	if (!sym) {
1364 		ERROR_GLIBC("calloc");
1365 		return NULL;
1366 	}
1367 
1368 	sym->name = "";
1369 	sym->sec = null;
1370 	elf_add_symbol(elf, sym);
1371 
1372 	return elf;
1373 }
1374 
1375 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
1376 {
1377 	unsigned int offset;
1378 
1379 	if (!strtab)
1380 		strtab = find_section_by_name(elf, ".strtab");
1381 	if (!strtab) {
1382 		ERROR("can't find .strtab section");
1383 		return -1;
1384 	}
1385 
1386 	if (!strtab->sh.sh_addralign) {
1387 		ERROR("'%s': invalid sh_addralign", strtab->name);
1388 		return -1;
1389 	}
1390 
1391 	offset = ALIGN(sec_size(strtab), strtab->sh.sh_addralign);
1392 
1393 	if (!elf_add_data(elf, strtab, str, strlen(str) + 1))
1394 		return -1;
1395 
1396 	return offset;
1397 }
1398 
1399 void *elf_add_data(struct elf *elf, struct section *sec, const void *data, size_t size)
1400 {
1401 	unsigned long offset;
1402 	Elf_Scn *s;
1403 
1404 	if (!sec->sh.sh_addralign) {
1405 		ERROR("'%s': invalid sh_addralign", sec->name);
1406 		return NULL;
1407 	}
1408 
1409 	s = elf_getscn(elf->elf, sec->idx);
1410 	if (!s) {
1411 		ERROR_ELF("elf_getscn");
1412 		return NULL;
1413 	}
1414 
1415 	sec->data = elf_newdata(s);
1416 	if (!sec->data) {
1417 		ERROR_ELF("elf_newdata");
1418 		return NULL;
1419 	}
1420 
1421 	sec->data->d_buf = calloc(1, size);
1422 	if (!sec->data->d_buf) {
1423 		ERROR_GLIBC("calloc");
1424 		return NULL;
1425 	}
1426 
1427 	if (data)
1428 		memcpy(sec->data->d_buf, data, size);
1429 
1430 	sec->data->d_size = size;
1431 	sec->data->d_align = 1;
1432 
1433 	offset = ALIGN(sec_size(sec), sec->sh.sh_addralign);
1434 	sec->sh.sh_size = offset + size;
1435 
1436 	mark_sec_changed(elf, sec, true);
1437 
1438 	return sec->data->d_buf;
1439 }
1440 
1441 struct section *elf_create_section(struct elf *elf, const char *name,
1442 				   size_t size, size_t entsize,
1443 				   unsigned int type, unsigned int align,
1444 				   unsigned int flags)
1445 {
1446 	struct section *sec, *shstrtab;
1447 	Elf_Scn *s;
1448 
1449 	if (name && find_section_by_name(elf, name)) {
1450 		ERROR("section '%s' already exists", name);
1451 		return NULL;
1452 	}
1453 
1454 	sec = calloc(1, sizeof(*sec));
1455 	if (!sec) {
1456 		ERROR_GLIBC("calloc");
1457 		return NULL;
1458 	}
1459 
1460 	INIT_LIST_HEAD(&sec->symbol_list);
1461 
1462 	/* don't actually create the section, just the data structures */
1463 	if (type == SHT_NULL)
1464 		goto add;
1465 
1466 	s = elf_newscn(elf->elf);
1467 	if (!s) {
1468 		ERROR_ELF("elf_newscn");
1469 		return NULL;
1470 	}
1471 
1472 	sec->idx = elf_ndxscn(s);
1473 
1474 	if (size) {
1475 		sec->data = elf_newdata(s);
1476 		if (!sec->data) {
1477 			ERROR_ELF("elf_newdata");
1478 			return NULL;
1479 		}
1480 
1481 		sec->data->d_size = size;
1482 		sec->data->d_align = 1;
1483 
1484 		sec->data->d_buf = calloc(1, size);
1485 		if (!sec->data->d_buf) {
1486 			ERROR_GLIBC("calloc");
1487 			return NULL;
1488 		}
1489 	}
1490 
1491 	if (!gelf_getshdr(s, &sec->sh)) {
1492 		ERROR_ELF("gelf_getshdr");
1493 		return NULL;
1494 	}
1495 
1496 	sec->sh.sh_size = size;
1497 	sec->sh.sh_entsize = entsize;
1498 	sec->sh.sh_type = type;
1499 	sec->sh.sh_addralign = align;
1500 	sec->sh.sh_flags = flags;
1501 
1502 	if (name) {
1503 		sec->name = strdup(name);
1504 		if (!sec->name) {
1505 			ERROR("strdup");
1506 			return NULL;
1507 		}
1508 
1509 		/* Add section name to .shstrtab (or .strtab for Clang) */
1510 		shstrtab = find_section_by_name(elf, ".shstrtab");
1511 		if (!shstrtab) {
1512 			shstrtab = find_section_by_name(elf, ".strtab");
1513 			if (!shstrtab) {
1514 				ERROR("can't find .shstrtab or .strtab");
1515 				return NULL;
1516 			}
1517 		}
1518 		sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1519 		if (sec->sh.sh_name == -1)
1520 			return NULL;
1521 
1522 		elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
1523 	}
1524 
1525 add:
1526 	list_add_tail(&sec->list, &elf->sections);
1527 	elf_hash_add(section, &sec->hash, sec->idx);
1528 
1529 	mark_sec_changed(elf, sec, true);
1530 
1531 	return sec;
1532 }
1533 
1534 static int elf_alloc_reloc(struct elf *elf, struct section *rsec)
1535 {
1536 	struct reloc *old_relocs, *old_relocs_end, *new_relocs;
1537 	unsigned int nr_relocs_old = sec_num_entries(rsec);
1538 	unsigned int nr_relocs_new = nr_relocs_old + 1;
1539 	unsigned long nr_alloc;
1540 	struct symbol *sym;
1541 
1542 	if (!rsec->data) {
1543 		rsec->data = elf_newdata(elf_getscn(elf->elf, rsec->idx));
1544 		if (!rsec->data) {
1545 			ERROR_ELF("elf_newdata");
1546 			return -1;
1547 		}
1548 
1549 		rsec->data->d_align = 1;
1550 		rsec->data->d_type = ELF_T_RELA;
1551 		rsec->data->d_buf = NULL;
1552 	}
1553 
1554 	rsec->data->d_size = nr_relocs_new * elf_rela_size(elf);
1555 	rsec->sh.sh_size   = rsec->data->d_size;
1556 
1557 	nr_alloc = max(64UL, roundup_pow_of_two(nr_relocs_new));
1558 	if (nr_alloc <= rsec->nr_alloc_relocs)
1559 		return 0;
1560 
1561 	if (rsec->data->d_buf && !rsec->nr_alloc_relocs) {
1562 		void *orig_buf = rsec->data->d_buf;
1563 
1564 		/*
1565 		 * The original d_buf is owned by libelf so it can't be
1566 		 * realloced.
1567 		 */
1568 		rsec->data->d_buf = malloc(nr_alloc * elf_rela_size(elf));
1569 		if (!rsec->data->d_buf) {
1570 			ERROR_GLIBC("malloc");
1571 			return -1;
1572 		}
1573 		memcpy(rsec->data->d_buf, orig_buf,
1574 		       nr_relocs_old * elf_rela_size(elf));
1575 	} else {
1576 		rsec->data->d_buf = realloc(rsec->data->d_buf,
1577 					    nr_alloc * elf_rela_size(elf));
1578 		if (!rsec->data->d_buf) {
1579 			ERROR_GLIBC("realloc");
1580 			return -1;
1581 		}
1582 	}
1583 
1584 	rsec->nr_alloc_relocs = nr_alloc;
1585 
1586 	old_relocs = rsec->relocs;
1587 	new_relocs = calloc(nr_alloc, sizeof(struct reloc));
1588 	if (!new_relocs) {
1589 		ERROR_GLIBC("calloc");
1590 		return -1;
1591 	}
1592 
1593 	if (!old_relocs)
1594 		goto done;
1595 
1596 	/*
1597 	 * The struct reloc's address has changed.  Update all the symbols and
1598 	 * relocs which reference it.
1599 	 */
1600 
1601 	old_relocs_end = &old_relocs[nr_relocs_old];
1602 	for_each_sym(elf, sym) {
1603 		struct reloc *reloc;
1604 
1605 		reloc = sym->relocs;
1606 		if (!reloc)
1607 			continue;
1608 
1609 		if (reloc >= old_relocs && reloc < old_relocs_end)
1610 			sym->relocs = &new_relocs[reloc - old_relocs];
1611 
1612 		while (1) {
1613 			struct reloc *next_reloc = sym_next_reloc(reloc);
1614 
1615 			if (!next_reloc)
1616 				break;
1617 
1618 			if (next_reloc >= old_relocs && next_reloc < old_relocs_end)
1619 				set_sym_next_reloc(reloc, &new_relocs[next_reloc - old_relocs]);
1620 
1621 			reloc = next_reloc;
1622 		}
1623 	}
1624 
1625 	memcpy(new_relocs, old_relocs, nr_relocs_old * sizeof(struct reloc));
1626 
1627 	for (int i = 0; i < nr_relocs_old; i++) {
1628 		struct reloc *old = &old_relocs[i];
1629 		struct reloc *new = &new_relocs[i];
1630 		u32 key = reloc_hash(old);
1631 
1632 		elf_hash_del(reloc, &old->hash, key);
1633 		elf_hash_add(reloc, &new->hash, key);
1634 	}
1635 
1636 	free(old_relocs);
1637 done:
1638 	rsec->relocs = new_relocs;
1639 	return 0;
1640 }
1641 
1642 struct section *elf_create_rela_section(struct elf *elf, struct section *sec,
1643 					unsigned int nr_relocs)
1644 {
1645 	struct section *rsec;
1646 	char *rsec_name;
1647 
1648 	rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
1649 	if (!rsec_name) {
1650 		ERROR_GLIBC("malloc");
1651 		return NULL;
1652 	}
1653 	strcpy(rsec_name, ".rela");
1654 	strcat(rsec_name, sec->name);
1655 
1656 	rsec = elf_create_section(elf, rsec_name, nr_relocs * elf_rela_size(elf),
1657 				  elf_rela_size(elf), SHT_RELA, elf_addr_size(elf),
1658 				  SHF_INFO_LINK);
1659 	free(rsec_name);
1660 	if (!rsec)
1661 		return NULL;
1662 
1663 	if (nr_relocs) {
1664 		rsec->data->d_type = ELF_T_RELA;
1665 
1666 		rsec->nr_alloc_relocs = nr_relocs;
1667 		rsec->relocs = calloc(nr_relocs, sizeof(struct reloc));
1668 		if (!rsec->relocs) {
1669 			ERROR_GLIBC("calloc");
1670 			return NULL;
1671 		}
1672 	}
1673 
1674 	rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1675 	rsec->sh.sh_info = sec->idx;
1676 
1677 	sec->rsec = rsec;
1678 	rsec->base = sec;
1679 
1680 	return rsec;
1681 }
1682 
1683 struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
1684 			       unsigned long offset,
1685 			       struct symbol *sym, s64 addend,
1686 			       unsigned int type)
1687 {
1688 	struct section *rsec = sec->rsec;
1689 
1690 	if (!rsec) {
1691 		rsec = elf_create_rela_section(elf, sec, 0);
1692 		if (!rsec)
1693 			return NULL;
1694 	}
1695 
1696 	if (find_reloc_by_dest(elf, sec, offset)) {
1697 		ERROR_FUNC(sec, offset, "duplicate reloc");
1698 		return NULL;
1699 	}
1700 
1701 	if (elf_alloc_reloc(elf, rsec))
1702 		return NULL;
1703 
1704 	mark_sec_changed(elf, rsec, true);
1705 
1706 	return elf_init_reloc(elf, rsec, sec_num_entries(rsec) - 1, offset, sym,
1707 			      addend, type);
1708 }
1709 
1710 struct section *elf_create_section_pair(struct elf *elf, const char *name,
1711 					size_t entsize, unsigned int nr,
1712 					unsigned int nr_relocs)
1713 {
1714 	struct section *sec;
1715 
1716 	sec = elf_create_section(elf, name, nr * entsize, entsize,
1717 				 SHT_PROGBITS, 1, SHF_ALLOC);
1718 	if (!sec)
1719 		return NULL;
1720 
1721 	if (!elf_create_rela_section(elf, sec, nr_relocs))
1722 		return NULL;
1723 
1724 	return sec;
1725 }
1726 
1727 int elf_write_insn(struct elf *elf, struct section *sec,
1728 		   unsigned long offset, unsigned int len,
1729 		   const char *insn)
1730 {
1731 	Elf_Data *data = sec->data;
1732 
1733 	if (data->d_type != ELF_T_BYTE || data->d_off) {
1734 		ERROR("write to unexpected data for section: %s", sec->name);
1735 		return -1;
1736 	}
1737 
1738 	memcpy(data->d_buf + offset, insn, len);
1739 
1740 	mark_sec_changed(elf, sec, true);
1741 
1742 	return 0;
1743 }
1744 
1745 /*
1746  * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
1747  * do you:
1748  *
1749  *   A) adhere to the section header and truncate the data, or
1750  *   B) ignore the section header and write out all the data you've got?
1751  *
1752  * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
1753  */
1754 static int elf_truncate_section(struct elf *elf, struct section *sec)
1755 {
1756 	u64 size = sec_size(sec);
1757 	bool truncated = false;
1758 	Elf_Data *data = NULL;
1759 	Elf_Scn *s;
1760 
1761 	s = elf_getscn(elf->elf, sec->idx);
1762 	if (!s) {
1763 		ERROR_ELF("elf_getscn");
1764 		return -1;
1765 	}
1766 
1767 	for (;;) {
1768 		/* get next data descriptor for the relevant section */
1769 		data = elf_getdata(s, data);
1770 		if (!data) {
1771 			if (size) {
1772 				ERROR("end of section data but non-zero size left\n");
1773 				return -1;
1774 			}
1775 			return 0;
1776 		}
1777 
1778 		if (truncated) {
1779 			/* when we remove symbols */
1780 			ERROR("truncated; but more data\n");
1781 			return -1;
1782 		}
1783 
1784 		if (!data->d_size) {
1785 			ERROR("zero size data");
1786 			return -1;
1787 		}
1788 
1789 		if (data->d_size > size) {
1790 			truncated = true;
1791 			data->d_size = size;
1792 		}
1793 
1794 		size -= data->d_size;
1795 	}
1796 }
1797 
1798 int elf_write(struct elf *elf)
1799 {
1800 	struct section *sec;
1801 	Elf_Scn *s;
1802 
1803 	/* Update changed relocation sections and section headers: */
1804 	list_for_each_entry(sec, &elf->sections, list) {
1805 		if (sec->truncate && elf_truncate_section(elf, sec))
1806 			return -1;
1807 
1808 		if (sec_changed(sec)) {
1809 			s = elf_getscn(elf->elf, sec->idx);
1810 			if (!s) {
1811 				ERROR_ELF("elf_getscn");
1812 				return -1;
1813 			}
1814 
1815 			/* Note this also flags the section dirty */
1816 			if (!gelf_update_shdr(s, &sec->sh)) {
1817 				ERROR_ELF("gelf_update_shdr");
1818 				return -1;
1819 			}
1820 
1821 			mark_sec_changed(elf, sec, false);
1822 		}
1823 	}
1824 
1825 	/* Make sure the new section header entries get updated properly. */
1826 	elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
1827 
1828 	/* Write all changes to the file. */
1829 	if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1830 		ERROR_ELF("elf_update");
1831 		return -1;
1832 	}
1833 
1834 	elf->changed = false;
1835 
1836 	return 0;
1837 }
1838 
1839 int elf_close(struct elf *elf)
1840 {
1841 	if (elf->elf)
1842 		elf_end(elf->elf);
1843 
1844 	if (elf->fd > 0)
1845 		close(elf->fd);
1846 
1847 	if (elf->tmp_name && rename(elf->tmp_name, elf->name))
1848 		return -1;
1849 
1850 	/*
1851 	 * NOTE: All remaining allocations are leaked on purpose.  Objtool is
1852 	 * about to exit anyway.
1853 	 */
1854 	return 0;
1855 }
1856