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