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