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