xref: /linux-6.15/tools/objtool/elf.c (revision 3e7be635)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2442f04c3SJosh Poimboeuf /*
3442f04c3SJosh Poimboeuf  * elf.c - ELF access library
4442f04c3SJosh Poimboeuf  *
5442f04c3SJosh Poimboeuf  * Adapted from kpatch (https://github.com/dynup/kpatch):
6442f04c3SJosh Poimboeuf  * Copyright (C) 2013-2015 Josh Poimboeuf <[email protected]>
7442f04c3SJosh Poimboeuf  * Copyright (C) 2014 Seth Jennings <[email protected]>
8442f04c3SJosh Poimboeuf  */
9442f04c3SJosh Poimboeuf 
10442f04c3SJosh Poimboeuf #include <sys/types.h>
11442f04c3SJosh Poimboeuf #include <sys/stat.h>
1225cf0d8aSPeter Zijlstra #include <sys/mman.h>
13442f04c3SJosh Poimboeuf #include <fcntl.h>
14442f04c3SJosh Poimboeuf #include <stdio.h>
15442f04c3SJosh Poimboeuf #include <stdlib.h>
16442f04c3SJosh Poimboeuf #include <string.h>
17442f04c3SJosh Poimboeuf #include <unistd.h>
18385d11b1SJosh Poimboeuf #include <errno.h>
195da6aea3SPeter Zijlstra #include <linux/interval_tree_generic.h>
207786032eSVasily Gorbik #include <objtool/builtin.h>
21442f04c3SJosh Poimboeuf 
227786032eSVasily Gorbik #include <objtool/elf.h>
237786032eSVasily Gorbik #include <objtool/warn.h>
24442f04c3SJosh Poimboeuf 
str_hash(const char * str)25ae358196SPeter Zijlstra static inline u32 str_hash(const char *str)
26ae358196SPeter Zijlstra {
27ae358196SPeter Zijlstra 	return jhash(str, strlen(str), 0);
28ae358196SPeter Zijlstra }
29ae358196SPeter Zijlstra 
3025cf0d8aSPeter Zijlstra #define __elf_table(name)	(elf->name##_hash)
3125cf0d8aSPeter Zijlstra #define __elf_bits(name)	(elf->name##_bits)
3234f7c96dSPeter Zijlstra 
3302b54001SJosh Poimboeuf #define __elf_table_entry(name, key) \
3402b54001SJosh Poimboeuf 	__elf_table(name)[hash_min(key, __elf_bits(name))]
3502b54001SJosh Poimboeuf 
3625cf0d8aSPeter Zijlstra #define elf_hash_add(name, node, key)					\
3702b54001SJosh Poimboeuf ({									\
3802b54001SJosh Poimboeuf 	struct elf_hash_node *__node = node;				\
3902b54001SJosh Poimboeuf 	__node->next = __elf_table_entry(name, key);			\
4002b54001SJosh Poimboeuf 	__elf_table_entry(name, key) = __node;				\
4102b54001SJosh Poimboeuf })
4202b54001SJosh Poimboeuf 
__elf_hash_del(struct elf_hash_node * node,struct elf_hash_node ** head)4302b54001SJosh Poimboeuf static inline void __elf_hash_del(struct elf_hash_node *node,
4402b54001SJosh Poimboeuf 				  struct elf_hash_node **head)
4502b54001SJosh Poimboeuf {
4602b54001SJosh Poimboeuf 	struct elf_hash_node *cur, *prev;
4702b54001SJosh Poimboeuf 
4802b54001SJosh Poimboeuf 	if (node == *head) {
4902b54001SJosh Poimboeuf 		*head = node->next;
5002b54001SJosh Poimboeuf 		return;
5102b54001SJosh Poimboeuf 	}
5202b54001SJosh Poimboeuf 
5302b54001SJosh Poimboeuf 	for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
5402b54001SJosh Poimboeuf 		if (cur == node) {
5502b54001SJosh Poimboeuf 			prev->next = cur->next;
5602b54001SJosh Poimboeuf 			break;
5702b54001SJosh Poimboeuf 		}
5802b54001SJosh Poimboeuf 	}
5902b54001SJosh Poimboeuf }
6002b54001SJosh Poimboeuf 
6102b54001SJosh Poimboeuf #define elf_hash_del(name, node, key) \
6202b54001SJosh Poimboeuf 	__elf_hash_del(node, &__elf_table_entry(name, key))
6302b54001SJosh Poimboeuf 
6402b54001SJosh Poimboeuf #define elf_list_entry(ptr, type, member)				\
6502b54001SJosh Poimboeuf ({									\
6602b54001SJosh Poimboeuf 	typeof(ptr) __ptr = (ptr);					\
6702b54001SJosh Poimboeuf 	__ptr ? container_of(__ptr, type, member) : NULL;		\
6802b54001SJosh Poimboeuf })
6934f7c96dSPeter Zijlstra 
7034f7c96dSPeter Zijlstra #define elf_hash_for_each_possible(name, obj, member, key)		\
7102b54001SJosh Poimboeuf 	for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
7202b54001SJosh Poimboeuf 	     obj;							\
7302b54001SJosh Poimboeuf 	     obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
7425cf0d8aSPeter Zijlstra 
7525cf0d8aSPeter Zijlstra #define elf_alloc_hash(name, size)					\
7625cf0d8aSPeter Zijlstra ({									\
7725cf0d8aSPeter Zijlstra 	__elf_bits(name) = max(10, ilog2(size));			\
7802b54001SJosh Poimboeuf 	__elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
7925cf0d8aSPeter Zijlstra 				 PROT_READ|PROT_WRITE,			\
8025cf0d8aSPeter Zijlstra 				 MAP_PRIVATE|MAP_ANON, -1, 0);		\
8125cf0d8aSPeter Zijlstra 	if (__elf_table(name) == (void *)-1L) {				\
82*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("mmap fail " #name);			\
8325cf0d8aSPeter Zijlstra 		__elf_table(name) = NULL;				\
8425cf0d8aSPeter Zijlstra 	}								\
8525cf0d8aSPeter Zijlstra 	__elf_table(name);						\
8625cf0d8aSPeter Zijlstra })
8734f7c96dSPeter Zijlstra 
__sym_start(struct symbol * s)885da6aea3SPeter Zijlstra static inline unsigned long __sym_start(struct symbol *s)
892a362eccSPeter Zijlstra {
905da6aea3SPeter Zijlstra 	return s->offset;
912a362eccSPeter Zijlstra }
922a362eccSPeter Zijlstra 
__sym_last(struct symbol * s)935da6aea3SPeter Zijlstra static inline unsigned long __sym_last(struct symbol *s)
942a362eccSPeter Zijlstra {
955da6aea3SPeter Zijlstra 	return s->offset + s->len - 1;
962a362eccSPeter Zijlstra }
972a362eccSPeter Zijlstra 
985da6aea3SPeter Zijlstra INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
995da6aea3SPeter Zijlstra 		     __sym_start, __sym_last, static, __sym)
1005da6aea3SPeter Zijlstra 
1015da6aea3SPeter Zijlstra #define __sym_for_each(_iter, _tree, _start, _end)			\
1025da6aea3SPeter Zijlstra 	for (_iter = __sym_iter_first((_tree), (_start), (_end));	\
1035da6aea3SPeter Zijlstra 	     _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
1045da6aea3SPeter Zijlstra 
1054adb2368SPeter Zijlstra struct symbol_hole {
1064adb2368SPeter Zijlstra 	unsigned long key;
1074adb2368SPeter Zijlstra 	const struct symbol *sym;
1084adb2368SPeter Zijlstra };
1094adb2368SPeter Zijlstra 
1104adb2368SPeter Zijlstra /*
1114adb2368SPeter Zijlstra  * Find !section symbol where @offset is after it.
1124adb2368SPeter Zijlstra  */
symbol_hole_by_offset(const void * key,const struct rb_node * node)1134adb2368SPeter Zijlstra static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
1144adb2368SPeter Zijlstra {
1154adb2368SPeter Zijlstra 	const struct symbol *s = rb_entry(node, struct symbol, node);
1164adb2368SPeter Zijlstra 	struct symbol_hole *sh = (void *)key;
1174adb2368SPeter Zijlstra 
1184adb2368SPeter Zijlstra 	if (sh->key < s->offset)
1194adb2368SPeter Zijlstra 		return -1;
1204adb2368SPeter Zijlstra 
1214adb2368SPeter Zijlstra 	if (sh->key >= s->offset + s->len) {
1224adb2368SPeter Zijlstra 		if (s->type != STT_SECTION)
1234adb2368SPeter Zijlstra 			sh->sym = s;
1244adb2368SPeter Zijlstra 		return 1;
1254adb2368SPeter Zijlstra 	}
1264adb2368SPeter Zijlstra 
1274adb2368SPeter Zijlstra 	return 0;
1284adb2368SPeter Zijlstra }
1294adb2368SPeter Zijlstra 
find_section_by_name(const struct elf * elf,const char * name)130894e48caSIngo Molnar struct section *find_section_by_name(const struct elf *elf, const char *name)
131442f04c3SJosh Poimboeuf {
132442f04c3SJosh Poimboeuf 	struct section *sec;
133442f04c3SJosh Poimboeuf 
13425cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
135442f04c3SJosh Poimboeuf 		if (!strcmp(sec->name, name))
136442f04c3SJosh Poimboeuf 			return sec;
13725cf0d8aSPeter Zijlstra 	}
138442f04c3SJosh Poimboeuf 
139442f04c3SJosh Poimboeuf 	return NULL;
140442f04c3SJosh Poimboeuf }
141442f04c3SJosh Poimboeuf 
find_section_by_index(struct elf * elf,unsigned int idx)142442f04c3SJosh Poimboeuf static struct section *find_section_by_index(struct elf *elf,
143442f04c3SJosh Poimboeuf 					     unsigned int idx)
144442f04c3SJosh Poimboeuf {
145442f04c3SJosh Poimboeuf 	struct section *sec;
146442f04c3SJosh Poimboeuf 
14725cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(section, sec, hash, idx) {
148442f04c3SJosh Poimboeuf 		if (sec->idx == idx)
149442f04c3SJosh Poimboeuf 			return sec;
15025cf0d8aSPeter Zijlstra 	}
151442f04c3SJosh Poimboeuf 
152442f04c3SJosh Poimboeuf 	return NULL;
153442f04c3SJosh Poimboeuf }
154442f04c3SJosh Poimboeuf 
find_symbol_by_index(struct elf * elf,unsigned int idx)155442f04c3SJosh Poimboeuf static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
156442f04c3SJosh Poimboeuf {
157442f04c3SJosh Poimboeuf 	struct symbol *sym;
158442f04c3SJosh Poimboeuf 
15925cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(symbol, sym, hash, idx) {
160442f04c3SJosh Poimboeuf 		if (sym->idx == idx)
161442f04c3SJosh Poimboeuf 			return sym;
16225cf0d8aSPeter Zijlstra 	}
163442f04c3SJosh Poimboeuf 
164442f04c3SJosh Poimboeuf 	return NULL;
165442f04c3SJosh Poimboeuf }
166442f04c3SJosh Poimboeuf 
find_symbol_by_offset(struct section * sec,unsigned long offset)167442f04c3SJosh Poimboeuf struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
168442f04c3SJosh Poimboeuf {
1695da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
1705da6aea3SPeter Zijlstra 	struct symbol *iter;
171442f04c3SJosh Poimboeuf 
1725da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
1735da6aea3SPeter Zijlstra 		if (iter->offset == offset && iter->type != STT_SECTION)
1745da6aea3SPeter Zijlstra 			return iter;
1752a362eccSPeter Zijlstra 	}
1767acfe531SJosh Poimboeuf 
1777acfe531SJosh Poimboeuf 	return NULL;
1787acfe531SJosh Poimboeuf }
1797acfe531SJosh Poimboeuf 
find_func_by_offset(struct section * sec,unsigned long offset)1807acfe531SJosh Poimboeuf struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
1817acfe531SJosh Poimboeuf {
1825da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
1835da6aea3SPeter Zijlstra 	struct symbol *iter;
1847acfe531SJosh Poimboeuf 
1855da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
1865da6aea3SPeter Zijlstra 		if (iter->offset == offset && iter->type == STT_FUNC)
1875da6aea3SPeter Zijlstra 			return iter;
1882a362eccSPeter Zijlstra 	}
1892a362eccSPeter Zijlstra 
1902a362eccSPeter Zijlstra 	return NULL;
1912a362eccSPeter Zijlstra }
1922a362eccSPeter Zijlstra 
find_symbol_containing(const struct section * sec,unsigned long offset)193b490f453SMiroslav Benes struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
1942a362eccSPeter Zijlstra {
1955da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
1965da6aea3SPeter Zijlstra 	struct symbol *iter;
1972a362eccSPeter Zijlstra 
1985da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
1995da6aea3SPeter Zijlstra 		if (iter->type != STT_SECTION)
2005da6aea3SPeter Zijlstra 			return iter;
2012a362eccSPeter Zijlstra 	}
2022a362eccSPeter Zijlstra 
2032a362eccSPeter Zijlstra 	return NULL;
2042a362eccSPeter Zijlstra }
2052a362eccSPeter Zijlstra 
2064adb2368SPeter Zijlstra /*
2074adb2368SPeter Zijlstra  * Returns size of hole starting at @offset.
2084adb2368SPeter Zijlstra  */
find_symbol_hole_containing(const struct section * sec,unsigned long offset)2094adb2368SPeter Zijlstra int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
2104adb2368SPeter Zijlstra {
2114adb2368SPeter Zijlstra 	struct symbol_hole hole = {
2124adb2368SPeter Zijlstra 		.key = offset,
2134adb2368SPeter Zijlstra 		.sym = NULL,
2144adb2368SPeter Zijlstra 	};
2154adb2368SPeter Zijlstra 	struct rb_node *n;
2164adb2368SPeter Zijlstra 	struct symbol *s;
2174adb2368SPeter Zijlstra 
2184adb2368SPeter Zijlstra 	/*
2194adb2368SPeter Zijlstra 	 * Find the rightmost symbol for which @offset is after it.
2204adb2368SPeter Zijlstra 	 */
2215da6aea3SPeter Zijlstra 	n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
2224adb2368SPeter Zijlstra 
2234adb2368SPeter Zijlstra 	/* found a symbol that contains @offset */
2244adb2368SPeter Zijlstra 	if (n)
2254adb2368SPeter Zijlstra 		return 0; /* not a hole */
2264adb2368SPeter Zijlstra 
22718e88509SRong Xu 	/*
22818e88509SRong Xu 	 * @offset >= sym->offset + sym->len, find symbol after it.
22918e88509SRong Xu 	 * When hole.sym is empty, use the first node to compute the hole.
23018e88509SRong Xu 	 * If there is no symbol in the section, the first node will be NULL,
23118e88509SRong Xu 	 * in which case, -1 is returned to skip the whole section.
23218e88509SRong Xu 	 */
23318e88509SRong Xu 	if (hole.sym)
2344adb2368SPeter Zijlstra 		n = rb_next(&hole.sym->node);
23518e88509SRong Xu 	else
23618e88509SRong Xu 		n = rb_first_cached(&sec->symbol_tree);
23718e88509SRong Xu 
2384adb2368SPeter Zijlstra 	if (!n)
2394adb2368SPeter Zijlstra 		return -1; /* until end of address space */
2404adb2368SPeter Zijlstra 
2414adb2368SPeter Zijlstra 	/* hole until start of next symbol */
2424adb2368SPeter Zijlstra 	s = rb_entry(n, struct symbol, node);
2434adb2368SPeter Zijlstra 	return s->offset - offset;
2444adb2368SPeter Zijlstra }
2454adb2368SPeter Zijlstra 
find_func_containing(struct section * sec,unsigned long offset)24653d20720SPeter Zijlstra struct symbol *find_func_containing(struct section *sec, unsigned long offset)
2472a362eccSPeter Zijlstra {
2485da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
2495da6aea3SPeter Zijlstra 	struct symbol *iter;
2502a362eccSPeter Zijlstra 
2515da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
2525da6aea3SPeter Zijlstra 		if (iter->type == STT_FUNC)
2535da6aea3SPeter Zijlstra 			return iter;
2542a362eccSPeter Zijlstra 	}
255442f04c3SJosh Poimboeuf 
256442f04c3SJosh Poimboeuf 	return NULL;
257442f04c3SJosh Poimboeuf }
258442f04c3SJosh Poimboeuf 
find_symbol_by_name(const struct elf * elf,const char * name)259894e48caSIngo Molnar struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
26013810435SJosh Poimboeuf {
26113810435SJosh Poimboeuf 	struct symbol *sym;
26213810435SJosh Poimboeuf 
26325cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
26413810435SJosh Poimboeuf 		if (!strcmp(sym->name, name))
26513810435SJosh Poimboeuf 			return sym;
26625cf0d8aSPeter Zijlstra 	}
26713810435SJosh Poimboeuf 
26813810435SJosh Poimboeuf 	return NULL;
26913810435SJosh Poimboeuf }
27013810435SJosh Poimboeuf 
find_reloc_by_dest_range(const struct elf * elf,struct section * sec,unsigned long offset,unsigned int len)271f1974222SMatt Helsley struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
2728b5fa6bcSPeter Zijlstra 				     unsigned long offset, unsigned int len)
273442f04c3SJosh Poimboeuf {
274f1974222SMatt Helsley 	struct reloc *reloc, *r = NULL;
275a5bd6236SJosh Poimboeuf 	struct section *rsec;
276042ba73fSJosh Poimboeuf 	unsigned long o;
277442f04c3SJosh Poimboeuf 
278a5bd6236SJosh Poimboeuf 	rsec = sec->rsec;
279a5bd6236SJosh Poimboeuf 	if (!rsec)
280442f04c3SJosh Poimboeuf 		return NULL;
281442f04c3SJosh Poimboeuf 
28274b873e4SPeter Zijlstra 	for_offset_range(o, offset, offset + len) {
28325cf0d8aSPeter Zijlstra 		elf_hash_for_each_possible(reloc, reloc, hash,
284a5bd6236SJosh Poimboeuf 					   sec_offset_hash(rsec, o)) {
285a5bd6236SJosh Poimboeuf 			if (reloc->sec != rsec)
28674b873e4SPeter Zijlstra 				continue;
28774b873e4SPeter Zijlstra 
288e4cbb9b8SJosh Poimboeuf 			if (reloc_offset(reloc) >= offset &&
289e4cbb9b8SJosh Poimboeuf 			    reloc_offset(reloc) < offset + len) {
290e4cbb9b8SJosh Poimboeuf 				if (!r || reloc_offset(reloc) < reloc_offset(r))
291f1974222SMatt Helsley 					r = reloc;
2928b5fa6bcSPeter Zijlstra 			}
2938b5fa6bcSPeter Zijlstra 		}
29474b873e4SPeter Zijlstra 		if (r)
29574b873e4SPeter Zijlstra 			return r;
29674b873e4SPeter Zijlstra 	}
297442f04c3SJosh Poimboeuf 
298442f04c3SJosh Poimboeuf 	return NULL;
299442f04c3SJosh Poimboeuf }
300442f04c3SJosh Poimboeuf 
find_reloc_by_dest(const struct elf * elf,struct section * sec,unsigned long offset)301f1974222SMatt Helsley struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
302442f04c3SJosh Poimboeuf {
303f1974222SMatt Helsley 	return find_reloc_by_dest_range(elf, sec, offset, 1);
304442f04c3SJosh Poimboeuf }
305442f04c3SJosh Poimboeuf 
is_dwarf_section(struct section * sec)306b4c96ef0SJosh Poimboeuf static bool is_dwarf_section(struct section *sec)
307b4c96ef0SJosh Poimboeuf {
308b4c96ef0SJosh Poimboeuf 	return !strncmp(sec->name, ".debug_", 7);
309b4c96ef0SJosh Poimboeuf }
310b4c96ef0SJosh Poimboeuf 
read_sections(struct elf * elf)311442f04c3SJosh Poimboeuf static int read_sections(struct elf *elf)
312442f04c3SJosh Poimboeuf {
313442f04c3SJosh Poimboeuf 	Elf_Scn *s = NULL;
314442f04c3SJosh Poimboeuf 	struct section *sec;
315442f04c3SJosh Poimboeuf 	size_t shstrndx, sections_nr;
316442f04c3SJosh Poimboeuf 	int i;
317442f04c3SJosh Poimboeuf 
318442f04c3SJosh Poimboeuf 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
319*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_getshdrnum");
320442f04c3SJosh Poimboeuf 		return -1;
321442f04c3SJosh Poimboeuf 	}
322442f04c3SJosh Poimboeuf 
323442f04c3SJosh Poimboeuf 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
324*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_getshdrstrndx");
325442f04c3SJosh Poimboeuf 		return -1;
326442f04c3SJosh Poimboeuf 	}
327442f04c3SJosh Poimboeuf 
32825cf0d8aSPeter Zijlstra 	if (!elf_alloc_hash(section, sections_nr) ||
32925cf0d8aSPeter Zijlstra 	    !elf_alloc_hash(section_name, sections_nr))
33025cf0d8aSPeter Zijlstra 		return -1;
33125cf0d8aSPeter Zijlstra 
3328045b8f0SThomas Weißschuh 	elf->section_data = calloc(sections_nr, sizeof(*sec));
3338045b8f0SThomas Weißschuh 	if (!elf->section_data) {
334*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("calloc");
335442f04c3SJosh Poimboeuf 		return -1;
336442f04c3SJosh Poimboeuf 	}
3378045b8f0SThomas Weißschuh 	for (i = 0; i < sections_nr; i++) {
3388045b8f0SThomas Weißschuh 		sec = &elf->section_data[i];
339442f04c3SJosh Poimboeuf 
340a196e171SJosh Poimboeuf 		INIT_LIST_HEAD(&sec->symbol_list);
341442f04c3SJosh Poimboeuf 
342442f04c3SJosh Poimboeuf 		s = elf_getscn(elf->elf, i);
343442f04c3SJosh Poimboeuf 		if (!s) {
344*3e7be635SJosh Poimboeuf 			ERROR_ELF("elf_getscn");
345442f04c3SJosh Poimboeuf 			return -1;
346442f04c3SJosh Poimboeuf 		}
347442f04c3SJosh Poimboeuf 
348442f04c3SJosh Poimboeuf 		sec->idx = elf_ndxscn(s);
349442f04c3SJosh Poimboeuf 
350442f04c3SJosh Poimboeuf 		if (!gelf_getshdr(s, &sec->sh)) {
351*3e7be635SJosh Poimboeuf 			ERROR_ELF("gelf_getshdr");
352442f04c3SJosh Poimboeuf 			return -1;
353442f04c3SJosh Poimboeuf 		}
354442f04c3SJosh Poimboeuf 
355442f04c3SJosh Poimboeuf 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
356442f04c3SJosh Poimboeuf 		if (!sec->name) {
357*3e7be635SJosh Poimboeuf 			ERROR_ELF("elf_strptr");
358442f04c3SJosh Poimboeuf 			return -1;
359442f04c3SJosh Poimboeuf 		}
360442f04c3SJosh Poimboeuf 
361b4c96ef0SJosh Poimboeuf 		if (sec->sh.sh_size != 0 && !is_dwarf_section(sec)) {
362baa41469SJosh Poimboeuf 			sec->data = elf_getdata(s, NULL);
363baa41469SJosh Poimboeuf 			if (!sec->data) {
364*3e7be635SJosh Poimboeuf 				ERROR_ELF("elf_getdata");
365442f04c3SJosh Poimboeuf 				return -1;
366442f04c3SJosh Poimboeuf 			}
367baa41469SJosh Poimboeuf 			if (sec->data->d_off != 0 ||
368baa41469SJosh Poimboeuf 			    sec->data->d_size != sec->sh.sh_size) {
369*3e7be635SJosh Poimboeuf 				ERROR("unexpected data attributes for %s", sec->name);
370442f04c3SJosh Poimboeuf 				return -1;
371442f04c3SJosh Poimboeuf 			}
372df968c93SPetr Vandrovec 		}
37353038996SPeter Zijlstra 
37453038996SPeter Zijlstra 		list_add_tail(&sec->list, &elf->sections);
37525cf0d8aSPeter Zijlstra 		elf_hash_add(section, &sec->hash, sec->idx);
37625cf0d8aSPeter Zijlstra 		elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
377eb0481bbSJosh Poimboeuf 
378eb0481bbSJosh Poimboeuf 		if (is_reloc_sec(sec))
379ebcef730SJosh Poimboeuf 			elf->num_relocs += sec_num_entries(sec);
380442f04c3SJosh Poimboeuf 	}
381442f04c3SJosh Poimboeuf 
3822daf7fabSJosh Poimboeuf 	if (opts.stats) {
3831e11f3fdSPeter Zijlstra 		printf("nr_sections: %lu\n", (unsigned long)sections_nr);
38425cf0d8aSPeter Zijlstra 		printf("section_bits: %d\n", elf->section_bits);
38525cf0d8aSPeter Zijlstra 	}
3861e11f3fdSPeter Zijlstra 
387442f04c3SJosh Poimboeuf 	/* sanity check, one more call to elf_nextscn() should return NULL */
388442f04c3SJosh Poimboeuf 	if (elf_nextscn(elf->elf, s)) {
389*3e7be635SJosh Poimboeuf 		ERROR("section entry mismatch");
390442f04c3SJosh Poimboeuf 		return -1;
391442f04c3SJosh Poimboeuf 	}
392442f04c3SJosh Poimboeuf 
393442f04c3SJosh Poimboeuf 	return 0;
394442f04c3SJosh Poimboeuf }
395442f04c3SJosh Poimboeuf 
elf_add_symbol(struct elf * elf,struct symbol * sym)3969a7827b7SPeter Zijlstra static void elf_add_symbol(struct elf *elf, struct symbol *sym)
3979a7827b7SPeter Zijlstra {
3989a7827b7SPeter Zijlstra 	struct list_head *entry;
3999a7827b7SPeter Zijlstra 	struct rb_node *pnode;
4005da6aea3SPeter Zijlstra 	struct symbol *iter;
4019a7827b7SPeter Zijlstra 
402ead165faSPeter Zijlstra 	INIT_LIST_HEAD(&sym->pv_target);
403ead165faSPeter Zijlstra 	sym->alias = sym;
404ead165faSPeter Zijlstra 
4059a7827b7SPeter Zijlstra 	sym->type = GELF_ST_TYPE(sym->sym.st_info);
4069a7827b7SPeter Zijlstra 	sym->bind = GELF_ST_BIND(sym->sym.st_info);
4079a7827b7SPeter Zijlstra 
408753da417SJosh Poimboeuf 	if (sym->type == STT_FILE)
409753da417SJosh Poimboeuf 		elf->num_files++;
410753da417SJosh Poimboeuf 
4119a7827b7SPeter Zijlstra 	sym->offset = sym->sym.st_value;
4129a7827b7SPeter Zijlstra 	sym->len = sym->sym.st_size;
4139a7827b7SPeter Zijlstra 
4145da6aea3SPeter Zijlstra 	__sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
4155da6aea3SPeter Zijlstra 		if (iter->offset == sym->offset && iter->type == sym->type)
4165da6aea3SPeter Zijlstra 			iter->alias = sym;
4175da6aea3SPeter Zijlstra 	}
4185da6aea3SPeter Zijlstra 
4195da6aea3SPeter Zijlstra 	__sym_insert(sym, &sym->sec->symbol_tree);
4209a7827b7SPeter Zijlstra 	pnode = rb_prev(&sym->node);
4219a7827b7SPeter Zijlstra 	if (pnode)
4229a7827b7SPeter Zijlstra 		entry = &rb_entry(pnode, struct symbol, node)->list;
4239a7827b7SPeter Zijlstra 	else
4249a7827b7SPeter Zijlstra 		entry = &sym->sec->symbol_list;
4259a7827b7SPeter Zijlstra 	list_add(&sym->list, entry);
42625cf0d8aSPeter Zijlstra 	elf_hash_add(symbol, &sym->hash, sym->idx);
42725cf0d8aSPeter Zijlstra 	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
4289a7827b7SPeter Zijlstra 
4299a7827b7SPeter Zijlstra 	/*
4309a7827b7SPeter Zijlstra 	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
4319a7827b7SPeter Zijlstra 	 * can exist within a function, confusing the sorting.
4329a7827b7SPeter Zijlstra 	 */
4339a7827b7SPeter Zijlstra 	if (!sym->len)
4345da6aea3SPeter Zijlstra 		__sym_remove(sym, &sym->sec->symbol_tree);
4359a7827b7SPeter Zijlstra }
4369a7827b7SPeter Zijlstra 
read_symbols(struct elf * elf)437442f04c3SJosh Poimboeuf static int read_symbols(struct elf *elf)
438442f04c3SJosh Poimboeuf {
43928fe1d7bSSami Tolvanen 	struct section *symtab, *symtab_shndx, *sec;
4402a362eccSPeter Zijlstra 	struct symbol *sym, *pfunc;
441442f04c3SJosh Poimboeuf 	int symbols_nr, i;
44213810435SJosh Poimboeuf 	char *coldstr;
44328fe1d7bSSami Tolvanen 	Elf_Data *shndx_data = NULL;
44428fe1d7bSSami Tolvanen 	Elf32_Word shndx;
445442f04c3SJosh Poimboeuf 
446442f04c3SJosh Poimboeuf 	symtab = find_section_by_name(elf, ".symtab");
44725cf0d8aSPeter Zijlstra 	if (symtab) {
44828fe1d7bSSami Tolvanen 		symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
44928fe1d7bSSami Tolvanen 		if (symtab_shndx)
45028fe1d7bSSami Tolvanen 			shndx_data = symtab_shndx->data;
45128fe1d7bSSami Tolvanen 
452ebcef730SJosh Poimboeuf 		symbols_nr = sec_num_entries(symtab);
45325cf0d8aSPeter Zijlstra 	} else {
45425cf0d8aSPeter Zijlstra 		/*
45525cf0d8aSPeter Zijlstra 		 * A missing symbol table is actually possible if it's an empty
45625cf0d8aSPeter Zijlstra 		 * .o file. This can happen for thunk_64.o. Make sure to at
45725cf0d8aSPeter Zijlstra 		 * least allocate the symbol hash tables so we can do symbol
45825cf0d8aSPeter Zijlstra 		 * lookups without crashing.
45925cf0d8aSPeter Zijlstra 		 */
46025cf0d8aSPeter Zijlstra 		symbols_nr = 0;
46125cf0d8aSPeter Zijlstra 	}
46225cf0d8aSPeter Zijlstra 
46325cf0d8aSPeter Zijlstra 	if (!elf_alloc_hash(symbol, symbols_nr) ||
46425cf0d8aSPeter Zijlstra 	    !elf_alloc_hash(symbol_name, symbols_nr))
46525cf0d8aSPeter Zijlstra 		return -1;
466442f04c3SJosh Poimboeuf 
4678045b8f0SThomas Weißschuh 	elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
4688045b8f0SThomas Weißschuh 	if (!elf->symbol_data) {
469*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("calloc");
470442f04c3SJosh Poimboeuf 		return -1;
471442f04c3SJosh Poimboeuf 	}
4728045b8f0SThomas Weißschuh 	for (i = 0; i < symbols_nr; i++) {
4738045b8f0SThomas Weißschuh 		sym = &elf->symbol_data[i];
474442f04c3SJosh Poimboeuf 
475442f04c3SJosh Poimboeuf 		sym->idx = i;
476442f04c3SJosh Poimboeuf 
47728fe1d7bSSami Tolvanen 		if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
47828fe1d7bSSami Tolvanen 				      &shndx)) {
479*3e7be635SJosh Poimboeuf 			ERROR_ELF("gelf_getsymshndx");
480442f04c3SJosh Poimboeuf 			goto err;
481442f04c3SJosh Poimboeuf 		}
482442f04c3SJosh Poimboeuf 
483442f04c3SJosh Poimboeuf 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
484442f04c3SJosh Poimboeuf 				       sym->sym.st_name);
485442f04c3SJosh Poimboeuf 		if (!sym->name) {
486*3e7be635SJosh Poimboeuf 			ERROR_ELF("elf_strptr");
487442f04c3SJosh Poimboeuf 			goto err;
488442f04c3SJosh Poimboeuf 		}
489442f04c3SJosh Poimboeuf 
49028fe1d7bSSami Tolvanen 		if ((sym->sym.st_shndx > SHN_UNDEF &&
49128fe1d7bSSami Tolvanen 		     sym->sym.st_shndx < SHN_LORESERVE) ||
49228fe1d7bSSami Tolvanen 		    (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
49328fe1d7bSSami Tolvanen 			if (sym->sym.st_shndx != SHN_XINDEX)
49428fe1d7bSSami Tolvanen 				shndx = sym->sym.st_shndx;
49528fe1d7bSSami Tolvanen 
49628fe1d7bSSami Tolvanen 			sym->sec = find_section_by_index(elf, shndx);
497442f04c3SJosh Poimboeuf 			if (!sym->sec) {
498*3e7be635SJosh Poimboeuf 				ERROR("couldn't find section for symbol %s", sym->name);
499442f04c3SJosh Poimboeuf 				goto err;
500442f04c3SJosh Poimboeuf 			}
5019a7827b7SPeter Zijlstra 			if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
502442f04c3SJosh Poimboeuf 				sym->name = sym->sec->name;
503442f04c3SJosh Poimboeuf 				sym->sec->sym = sym;
504442f04c3SJosh Poimboeuf 			}
505442f04c3SJosh Poimboeuf 		} else
506442f04c3SJosh Poimboeuf 			sym->sec = find_section_by_index(elf, 0);
507442f04c3SJosh Poimboeuf 
5089a7827b7SPeter Zijlstra 		elf_add_symbol(elf, sym);
509442f04c3SJosh Poimboeuf 	}
510442f04c3SJosh Poimboeuf 
5112daf7fabSJosh Poimboeuf 	if (opts.stats) {
5121e11f3fdSPeter Zijlstra 		printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
51325cf0d8aSPeter Zijlstra 		printf("symbol_bits: %d\n", elf->symbol_bits);
51425cf0d8aSPeter Zijlstra 	}
5151e11f3fdSPeter Zijlstra 
51613810435SJosh Poimboeuf 	/* Create parent/child links for any cold subfunctions */
51713810435SJosh Poimboeuf 	list_for_each_entry(sec, &elf->sections, list) {
5189290e772SJosh Poimboeuf 		sec_for_each_sym(sec, sym) {
519f404a58dSAaron Plattner 			char *pname;
52022566c16SArtem Savkov 			size_t pnamelen;
52113810435SJosh Poimboeuf 			if (sym->type != STT_FUNC)
52213810435SJosh Poimboeuf 				continue;
523e000acc1SKristen Carlson Accardi 
524e000acc1SKristen Carlson Accardi 			if (sym->pfunc == NULL)
525e000acc1SKristen Carlson Accardi 				sym->pfunc = sym;
526e000acc1SKristen Carlson Accardi 
527e000acc1SKristen Carlson Accardi 			if (sym->cfunc == NULL)
528e000acc1SKristen Carlson Accardi 				sym->cfunc = sym;
529e000acc1SKristen Carlson Accardi 
530bcb6fb5dSJosh Poimboeuf 			coldstr = strstr(sym->name, ".cold");
53108b393d0SJosh Poimboeuf 			if (!coldstr)
53208b393d0SJosh Poimboeuf 				continue;
53308b393d0SJosh Poimboeuf 
53422566c16SArtem Savkov 			pnamelen = coldstr - sym->name;
535f404a58dSAaron Plattner 			pname = strndup(sym->name, pnamelen);
536f404a58dSAaron Plattner 			if (!pname) {
537*3e7be635SJosh Poimboeuf 				ERROR("%s(): failed to allocate memory", sym->name);
53822566c16SArtem Savkov 				return -1;
53922566c16SArtem Savkov 			}
54022566c16SArtem Savkov 
54122566c16SArtem Savkov 			pfunc = find_symbol_by_name(elf, pname);
542f404a58dSAaron Plattner 			free(pname);
54313810435SJosh Poimboeuf 
54413810435SJosh Poimboeuf 			if (!pfunc) {
545*3e7be635SJosh Poimboeuf 				ERROR("%s(): can't find parent function", sym->name);
5460b9301fbSArtem Savkov 				return -1;
54713810435SJosh Poimboeuf 			}
54813810435SJosh Poimboeuf 
54913810435SJosh Poimboeuf 			sym->pfunc = pfunc;
55013810435SJosh Poimboeuf 			pfunc->cfunc = sym;
55108b393d0SJosh Poimboeuf 
55208b393d0SJosh Poimboeuf 			/*
55308b393d0SJosh Poimboeuf 			 * Unfortunately, -fnoreorder-functions puts the child
55408b393d0SJosh Poimboeuf 			 * inside the parent.  Remove the overlap so we can
55508b393d0SJosh Poimboeuf 			 * have sane assumptions.
55608b393d0SJosh Poimboeuf 			 *
55708b393d0SJosh Poimboeuf 			 * Note that pfunc->len now no longer matches
55808b393d0SJosh Poimboeuf 			 * pfunc->sym.st_size.
55908b393d0SJosh Poimboeuf 			 */
56008b393d0SJosh Poimboeuf 			if (sym->sec == pfunc->sec &&
56108b393d0SJosh Poimboeuf 			    sym->offset >= pfunc->offset &&
56208b393d0SJosh Poimboeuf 			    sym->offset + sym->len == pfunc->offset + pfunc->len) {
56308b393d0SJosh Poimboeuf 				pfunc->len -= sym->len;
56413810435SJosh Poimboeuf 			}
56513810435SJosh Poimboeuf 		}
56613810435SJosh Poimboeuf 	}
56713810435SJosh Poimboeuf 
568442f04c3SJosh Poimboeuf 	return 0;
569442f04c3SJosh Poimboeuf 
570442f04c3SJosh Poimboeuf err:
571442f04c3SJosh Poimboeuf 	free(sym);
572442f04c3SJosh Poimboeuf 	return -1;
573442f04c3SJosh Poimboeuf }
574442f04c3SJosh Poimboeuf 
5754abff6d4SPeter Zijlstra /*
576fcf93355SJosh Poimboeuf  * @sym's idx has changed.  Update the relocs which reference it.
5774abff6d4SPeter Zijlstra  */
elf_update_sym_relocs(struct elf * elf,struct symbol * sym)578fcf93355SJosh Poimboeuf static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
5794abff6d4SPeter Zijlstra {
5804abff6d4SPeter Zijlstra 	struct reloc *reloc;
5814abff6d4SPeter Zijlstra 
582ef753d66SJosh Poimboeuf 	for (reloc = sym->relocs; reloc; reloc = sym_next_reloc(reloc))
583ec24b927SJosh Poimboeuf 		set_reloc_sym(elf, reloc, reloc->sym->idx);
584fcf93355SJosh Poimboeuf 
585fcf93355SJosh Poimboeuf 	return 0;
5864abff6d4SPeter Zijlstra }
5874abff6d4SPeter Zijlstra 
5884abff6d4SPeter Zijlstra /*
589ead165faSPeter Zijlstra  * The libelf API is terrible; gelf_update_sym*() takes a data block relative
590ead165faSPeter Zijlstra  * index value, *NOT* the symbol index. As such, iterate the data blocks and
591ead165faSPeter Zijlstra  * adjust index until it fits.
592ead165faSPeter Zijlstra  *
593ead165faSPeter Zijlstra  * If no data block is found, allow adding a new data block provided the index
594ead165faSPeter Zijlstra  * is only one past the end.
5954abff6d4SPeter Zijlstra  */
elf_update_symbol(struct elf * elf,struct section * symtab,struct section * symtab_shndx,struct symbol * sym)596ead165faSPeter Zijlstra static int elf_update_symbol(struct elf *elf, struct section *symtab,
597ead165faSPeter Zijlstra 			     struct section *symtab_shndx, struct symbol *sym)
5984abff6d4SPeter Zijlstra {
599ead165faSPeter Zijlstra 	Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
600ead165faSPeter Zijlstra 	Elf_Data *symtab_data = NULL, *shndx_data = NULL;
601ead165faSPeter Zijlstra 	Elf64_Xword entsize = symtab->sh.sh_entsize;
602ead165faSPeter Zijlstra 	int max_idx, idx = sym->idx;
603ead165faSPeter Zijlstra 	Elf_Scn *s, *t = NULL;
6045141d3a0SSami Tolvanen 	bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
6055141d3a0SSami Tolvanen 				sym->sym.st_shndx != SHN_XINDEX;
6065141d3a0SSami Tolvanen 
6075141d3a0SSami Tolvanen 	if (is_special_shndx)
6085141d3a0SSami Tolvanen 		shndx = sym->sym.st_shndx;
6094abff6d4SPeter Zijlstra 
6104abff6d4SPeter Zijlstra 	s = elf_getscn(elf->elf, symtab->idx);
6114abff6d4SPeter Zijlstra 	if (!s) {
612*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_getscn");
6134abff6d4SPeter Zijlstra 		return -1;
6144abff6d4SPeter Zijlstra 	}
6154abff6d4SPeter Zijlstra 
6164abff6d4SPeter Zijlstra 	if (symtab_shndx) {
617ead165faSPeter Zijlstra 		t = elf_getscn(elf->elf, symtab_shndx->idx);
618ead165faSPeter Zijlstra 		if (!t) {
619*3e7be635SJosh Poimboeuf 			ERROR_ELF("elf_getscn");
6204abff6d4SPeter Zijlstra 			return -1;
6214abff6d4SPeter Zijlstra 		}
622ead165faSPeter Zijlstra 	}
6234abff6d4SPeter Zijlstra 
624ead165faSPeter Zijlstra 	for (;;) {
625ead165faSPeter Zijlstra 		/* get next data descriptor for the relevant sections */
626ead165faSPeter Zijlstra 		symtab_data = elf_getdata(s, symtab_data);
627ead165faSPeter Zijlstra 		if (t)
628ead165faSPeter Zijlstra 			shndx_data = elf_getdata(t, shndx_data);
629ead165faSPeter Zijlstra 
630ead165faSPeter Zijlstra 		/* end-of-list */
631ead165faSPeter Zijlstra 		if (!symtab_data) {
63213f60e80SPeter Zijlstra 			/*
63313f60e80SPeter Zijlstra 			 * Over-allocate to avoid O(n^2) symbol creation
63413f60e80SPeter Zijlstra 			 * behaviour.  The down side is that libelf doesn't
63513f60e80SPeter Zijlstra 			 * like this; see elf_truncate_section() for the fixup.
63613f60e80SPeter Zijlstra 			 */
63713f60e80SPeter Zijlstra 			int num = max(1U, sym->idx/3);
638ead165faSPeter Zijlstra 			void *buf;
639ead165faSPeter Zijlstra 
640ead165faSPeter Zijlstra 			if (idx) {
641ead165faSPeter Zijlstra 				/* we don't do holes in symbol tables */
642*3e7be635SJosh Poimboeuf 				ERROR("index out of range");
6434abff6d4SPeter Zijlstra 				return -1;
6444abff6d4SPeter Zijlstra 			}
6454abff6d4SPeter Zijlstra 
646ead165faSPeter Zijlstra 			/* if @idx == 0, it's the next contiguous entry, create it */
647ead165faSPeter Zijlstra 			symtab_data = elf_newdata(s);
648ead165faSPeter Zijlstra 			if (t)
649ead165faSPeter Zijlstra 				shndx_data = elf_newdata(t);
650ead165faSPeter Zijlstra 
65113f60e80SPeter Zijlstra 			buf = calloc(num, entsize);
652ead165faSPeter Zijlstra 			if (!buf) {
653*3e7be635SJosh Poimboeuf 				ERROR_GLIBC("calloc");
654ead165faSPeter Zijlstra 				return -1;
655ead165faSPeter Zijlstra 			}
656ead165faSPeter Zijlstra 
657ead165faSPeter Zijlstra 			symtab_data->d_buf = buf;
65813f60e80SPeter Zijlstra 			symtab_data->d_size = num * entsize;
659ead165faSPeter Zijlstra 			symtab_data->d_align = 1;
660ead165faSPeter Zijlstra 			symtab_data->d_type = ELF_T_SYM;
661ead165faSPeter Zijlstra 
662ff408273SJosh Poimboeuf 			mark_sec_changed(elf, symtab, true);
66313f60e80SPeter Zijlstra 			symtab->truncate = true;
664ead165faSPeter Zijlstra 
665ead165faSPeter Zijlstra 			if (t) {
66613f60e80SPeter Zijlstra 				buf = calloc(num, sizeof(Elf32_Word));
66713f60e80SPeter Zijlstra 				if (!buf) {
668*3e7be635SJosh Poimboeuf 					ERROR_GLIBC("calloc");
66913f60e80SPeter Zijlstra 					return -1;
67013f60e80SPeter Zijlstra 				}
67113f60e80SPeter Zijlstra 
67213f60e80SPeter Zijlstra 				shndx_data->d_buf = buf;
67313f60e80SPeter Zijlstra 				shndx_data->d_size = num * sizeof(Elf32_Word);
674ead165faSPeter Zijlstra 				shndx_data->d_align = sizeof(Elf32_Word);
6754abff6d4SPeter Zijlstra 				shndx_data->d_type = ELF_T_WORD;
6764abff6d4SPeter Zijlstra 
677ff408273SJosh Poimboeuf 				mark_sec_changed(elf, symtab_shndx, true);
67813f60e80SPeter Zijlstra 				symtab_shndx->truncate = true;
6794abff6d4SPeter Zijlstra 			}
6804abff6d4SPeter Zijlstra 
681ead165faSPeter Zijlstra 			break;
682ead165faSPeter Zijlstra 		}
683ead165faSPeter Zijlstra 
684ead165faSPeter Zijlstra 		/* empty blocks should not happen */
685ead165faSPeter Zijlstra 		if (!symtab_data->d_size) {
686*3e7be635SJosh Poimboeuf 			ERROR("zero size data");
687ead165faSPeter Zijlstra 			return -1;
688ead165faSPeter Zijlstra 		}
689ead165faSPeter Zijlstra 
690ead165faSPeter Zijlstra 		/* is this the right block? */
691ead165faSPeter Zijlstra 		max_idx = symtab_data->d_size / entsize;
692ead165faSPeter Zijlstra 		if (idx < max_idx)
693ead165faSPeter Zijlstra 			break;
694ead165faSPeter Zijlstra 
695ead165faSPeter Zijlstra 		/* adjust index and try again */
696ead165faSPeter Zijlstra 		idx -= max_idx;
697ead165faSPeter Zijlstra 	}
698ead165faSPeter Zijlstra 
699ead165faSPeter Zijlstra 	/* something went side-ways */
700ead165faSPeter Zijlstra 	if (idx < 0) {
701*3e7be635SJosh Poimboeuf 		ERROR("negative index");
702ead165faSPeter Zijlstra 		return -1;
703ead165faSPeter Zijlstra 	}
704ead165faSPeter Zijlstra 
705ead165faSPeter Zijlstra 	/* setup extended section index magic and write the symbol */
7065141d3a0SSami Tolvanen 	if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) {
707ead165faSPeter Zijlstra 		sym->sym.st_shndx = shndx;
708ead165faSPeter Zijlstra 		if (!shndx_data)
709ead165faSPeter Zijlstra 			shndx = 0;
710ead165faSPeter Zijlstra 	} else {
711ead165faSPeter Zijlstra 		sym->sym.st_shndx = SHN_XINDEX;
712ead165faSPeter Zijlstra 		if (!shndx_data) {
713*3e7be635SJosh Poimboeuf 			ERROR("no .symtab_shndx");
714ead165faSPeter Zijlstra 			return -1;
715ead165faSPeter Zijlstra 		}
716ead165faSPeter Zijlstra 	}
717ead165faSPeter Zijlstra 
718ead165faSPeter Zijlstra 	if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
719*3e7be635SJosh Poimboeuf 		ERROR_ELF("gelf_update_symshndx");
720ead165faSPeter Zijlstra 		return -1;
721ead165faSPeter Zijlstra 	}
722ead165faSPeter Zijlstra 
723ead165faSPeter Zijlstra 	return 0;
7244abff6d4SPeter Zijlstra }
7254abff6d4SPeter Zijlstra 
7264abff6d4SPeter Zijlstra static struct symbol *
__elf_create_symbol(struct elf * elf,struct symbol * sym)7274c91be8eSPeter Zijlstra __elf_create_symbol(struct elf *elf, struct symbol *sym)
7284abff6d4SPeter Zijlstra {
7294abff6d4SPeter Zijlstra 	struct section *symtab, *symtab_shndx;
730ead165faSPeter Zijlstra 	Elf32_Word first_non_local, new_idx;
7314c91be8eSPeter Zijlstra 	struct symbol *old;
7324abff6d4SPeter Zijlstra 
7334abff6d4SPeter Zijlstra 	symtab = find_section_by_name(elf, ".symtab");
7344abff6d4SPeter Zijlstra 	if (symtab) {
7354abff6d4SPeter Zijlstra 		symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
7364abff6d4SPeter Zijlstra 	} else {
737*3e7be635SJosh Poimboeuf 		ERROR("no .symtab");
7384abff6d4SPeter Zijlstra 		return NULL;
7394abff6d4SPeter Zijlstra 	}
7404abff6d4SPeter Zijlstra 
741ebcef730SJosh Poimboeuf 	new_idx = sec_num_entries(symtab);
7424abff6d4SPeter Zijlstra 
7434c91be8eSPeter Zijlstra 	if (GELF_ST_BIND(sym->sym.st_info) != STB_LOCAL)
7444c91be8eSPeter Zijlstra 		goto non_local;
745ead165faSPeter Zijlstra 
746ead165faSPeter Zijlstra 	/*
747ead165faSPeter Zijlstra 	 * Move the first global symbol, as per sh_info, into a new, higher
748ead165faSPeter Zijlstra 	 * symbol index. This fees up a spot for a new local symbol.
749ead165faSPeter Zijlstra 	 */
750ead165faSPeter Zijlstra 	first_non_local = symtab->sh.sh_info;
751ead165faSPeter Zijlstra 	old = find_symbol_by_index(elf, first_non_local);
752ead165faSPeter Zijlstra 	if (old) {
753ead165faSPeter Zijlstra 
75402b54001SJosh Poimboeuf 		elf_hash_del(symbol, &old->hash, old->idx);
75502b54001SJosh Poimboeuf 		elf_hash_add(symbol, &old->hash, new_idx);
75602b54001SJosh Poimboeuf 		old->idx = new_idx;
757ead165faSPeter Zijlstra 
758ead165faSPeter Zijlstra 		if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
759*3e7be635SJosh Poimboeuf 			ERROR("elf_update_symbol move");
7604abff6d4SPeter Zijlstra 			return NULL;
7614abff6d4SPeter Zijlstra 		}
7624abff6d4SPeter Zijlstra 
763fcf93355SJosh Poimboeuf 		if (elf_update_sym_relocs(elf, old))
764fcf93355SJosh Poimboeuf 			return NULL;
765fcf93355SJosh Poimboeuf 
766ead165faSPeter Zijlstra 		new_idx = first_non_local;
767ead165faSPeter Zijlstra 	}
768ead165faSPeter Zijlstra 
7694c91be8eSPeter Zijlstra 	/*
7704c91be8eSPeter Zijlstra 	 * Either way, we will add a LOCAL symbol.
7714c91be8eSPeter Zijlstra 	 */
7724c91be8eSPeter Zijlstra 	symtab->sh.sh_info += 1;
7734c91be8eSPeter Zijlstra 
7744c91be8eSPeter Zijlstra non_local:
775ead165faSPeter Zijlstra 	sym->idx = new_idx;
776ead165faSPeter Zijlstra 	if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
777*3e7be635SJosh Poimboeuf 		ERROR("elf_update_symbol");
7784abff6d4SPeter Zijlstra 		return NULL;
7794abff6d4SPeter Zijlstra 	}
7804abff6d4SPeter Zijlstra 
78113f60e80SPeter Zijlstra 	symtab->sh.sh_size += symtab->sh.sh_entsize;
782ff408273SJosh Poimboeuf 	mark_sec_changed(elf, symtab, true);
78313f60e80SPeter Zijlstra 
78413f60e80SPeter Zijlstra 	if (symtab_shndx) {
78513f60e80SPeter Zijlstra 		symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
786ff408273SJosh Poimboeuf 		mark_sec_changed(elf, symtab_shndx, true);
78713f60e80SPeter Zijlstra 	}
78813f60e80SPeter Zijlstra 
7894c91be8eSPeter Zijlstra 	return sym;
7904c91be8eSPeter Zijlstra }
791ead165faSPeter Zijlstra 
7924c91be8eSPeter Zijlstra static struct symbol *
elf_create_section_symbol(struct elf * elf,struct section * sec)7934c91be8eSPeter Zijlstra elf_create_section_symbol(struct elf *elf, struct section *sec)
7944c91be8eSPeter Zijlstra {
7954c91be8eSPeter Zijlstra 	struct symbol *sym = calloc(1, sizeof(*sym));
7964c91be8eSPeter Zijlstra 
7974c91be8eSPeter Zijlstra 	if (!sym) {
798*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("malloc");
7994c91be8eSPeter Zijlstra 		return NULL;
8004c91be8eSPeter Zijlstra 	}
8014c91be8eSPeter Zijlstra 
8024c91be8eSPeter Zijlstra 	sym->name = sec->name;
8034c91be8eSPeter Zijlstra 	sym->sec = sec;
8044c91be8eSPeter Zijlstra 
8054c91be8eSPeter Zijlstra 	// st_name 0
8064c91be8eSPeter Zijlstra 	sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
8074c91be8eSPeter Zijlstra 	// st_other 0
8084c91be8eSPeter Zijlstra 	// st_value 0
8094c91be8eSPeter Zijlstra 	// st_size 0
8104c91be8eSPeter Zijlstra 
8114c91be8eSPeter Zijlstra 	sym = __elf_create_symbol(elf, sym);
8124c91be8eSPeter Zijlstra 	if (sym)
8134abff6d4SPeter Zijlstra 		elf_add_symbol(elf, sym);
8144abff6d4SPeter Zijlstra 
8154abff6d4SPeter Zijlstra 	return sym;
8164abff6d4SPeter Zijlstra }
8174abff6d4SPeter Zijlstra 
8189f2899feSPeter Zijlstra static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
8199f2899feSPeter Zijlstra 
8209f2899feSPeter Zijlstra struct symbol *
elf_create_prefix_symbol(struct elf * elf,struct symbol * orig,long size)8219f2899feSPeter Zijlstra elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
8229f2899feSPeter Zijlstra {
8239f2899feSPeter Zijlstra 	struct symbol *sym = calloc(1, sizeof(*sym));
8249f2899feSPeter Zijlstra 	size_t namelen = strlen(orig->name) + sizeof("__pfx_");
8259f2899feSPeter Zijlstra 	char *name = malloc(namelen);
8269f2899feSPeter Zijlstra 
8279f2899feSPeter Zijlstra 	if (!sym || !name) {
828*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("malloc");
8299f2899feSPeter Zijlstra 		return NULL;
8309f2899feSPeter Zijlstra 	}
8319f2899feSPeter Zijlstra 
8329f2899feSPeter Zijlstra 	snprintf(name, namelen, "__pfx_%s", orig->name);
8339f2899feSPeter Zijlstra 
8349f2899feSPeter Zijlstra 	sym->name = name;
8359f2899feSPeter Zijlstra 	sym->sec = orig->sec;
8369f2899feSPeter Zijlstra 
8379f2899feSPeter Zijlstra 	sym->sym.st_name = elf_add_string(elf, NULL, name);
8389f2899feSPeter Zijlstra 	sym->sym.st_info = orig->sym.st_info;
8399f2899feSPeter Zijlstra 	sym->sym.st_value = orig->sym.st_value - size;
8409f2899feSPeter Zijlstra 	sym->sym.st_size = size;
8419f2899feSPeter Zijlstra 
8429f2899feSPeter Zijlstra 	sym = __elf_create_symbol(elf, sym);
8439f2899feSPeter Zijlstra 	if (sym)
8449f2899feSPeter Zijlstra 		elf_add_symbol(elf, sym);
8459f2899feSPeter Zijlstra 
8469f2899feSPeter Zijlstra 	return sym;
8479f2899feSPeter Zijlstra }
8489f2899feSPeter Zijlstra 
elf_init_reloc(struct elf * elf,struct section * rsec,unsigned int reloc_idx,unsigned long offset,struct symbol * sym,s64 addend,unsigned int type)8496342a20eSJosh Poimboeuf static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
8506342a20eSJosh Poimboeuf 				    unsigned int reloc_idx,
8516342a20eSJosh Poimboeuf 				    unsigned long offset, struct symbol *sym,
8526342a20eSJosh Poimboeuf 				    s64 addend, unsigned int type)
8536342a20eSJosh Poimboeuf {
854e0a9349bSJosh Poimboeuf 	struct reloc *reloc, empty = { 0 };
8556342a20eSJosh Poimboeuf 
856ebcef730SJosh Poimboeuf 	if (reloc_idx >= sec_num_entries(rsec)) {
857*3e7be635SJosh Poimboeuf 		ERROR("%s: bad reloc_idx %u for %s with %d relocs",
858ebcef730SJosh Poimboeuf 		      __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
8596342a20eSJosh Poimboeuf 		return NULL;
8606342a20eSJosh Poimboeuf 	}
8616342a20eSJosh Poimboeuf 
862ebcef730SJosh Poimboeuf 	reloc = &rsec->relocs[reloc_idx];
863e0a9349bSJosh Poimboeuf 
864e0a9349bSJosh Poimboeuf 	if (memcmp(reloc, &empty, sizeof(empty))) {
865*3e7be635SJosh Poimboeuf 		ERROR("%s: %s: reloc %d already initialized!",
866e0a9349bSJosh Poimboeuf 		      __func__, rsec->name, reloc_idx);
8676342a20eSJosh Poimboeuf 		return NULL;
8686342a20eSJosh Poimboeuf 	}
8696342a20eSJosh Poimboeuf 
8706342a20eSJosh Poimboeuf 	reloc->sec = rsec;
8716342a20eSJosh Poimboeuf 	reloc->sym = sym;
8726342a20eSJosh Poimboeuf 
873ec24b927SJosh Poimboeuf 	set_reloc_offset(elf, reloc, offset);
874ec24b927SJosh Poimboeuf 	set_reloc_sym(elf, reloc, sym->idx);
875ec24b927SJosh Poimboeuf 	set_reloc_type(elf, reloc, type);
876ec24b927SJosh Poimboeuf 	set_reloc_addend(elf, reloc, addend);
877fcf93355SJosh Poimboeuf 
8786342a20eSJosh Poimboeuf 	elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
879ef753d66SJosh Poimboeuf 	set_sym_next_reloc(reloc, sym->relocs);
880890f10a4SJosh Poimboeuf 	sym->relocs = reloc;
8816342a20eSJosh Poimboeuf 
8826342a20eSJosh Poimboeuf 	return reloc;
8836342a20eSJosh Poimboeuf }
8846342a20eSJosh Poimboeuf 
elf_init_reloc_text_sym(struct elf * elf,struct section * sec,unsigned long offset,unsigned int reloc_idx,struct section * insn_sec,unsigned long insn_off)8856342a20eSJosh Poimboeuf struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
8866342a20eSJosh Poimboeuf 				      unsigned long offset,
8876342a20eSJosh Poimboeuf 				      unsigned int reloc_idx,
8886342a20eSJosh Poimboeuf 				      struct section *insn_sec,
8896342a20eSJosh Poimboeuf 				      unsigned long insn_off)
890ef47cc01SPeter Zijlstra {
8914abff6d4SPeter Zijlstra 	struct symbol *sym = insn_sec->sym;
8924abff6d4SPeter Zijlstra 	int addend = insn_off;
893ef47cc01SPeter Zijlstra 
8946342a20eSJosh Poimboeuf 	if (!(insn_sec->sh.sh_flags & SHF_EXECINSTR)) {
895*3e7be635SJosh Poimboeuf 		ERROR("bad call to %s() for data symbol %s", __func__, sym->name);
8966342a20eSJosh Poimboeuf 		return NULL;
8976342a20eSJosh Poimboeuf 	}
8986342a20eSJosh Poimboeuf 
899ef47cc01SPeter Zijlstra 	if (!sym) {
9004abff6d4SPeter Zijlstra 		/*
9014abff6d4SPeter Zijlstra 		 * Due to how weak functions work, we must use section based
9024abff6d4SPeter Zijlstra 		 * relocations. Symbol based relocations would result in the
9034abff6d4SPeter Zijlstra 		 * weak and non-weak function annotations being overlaid on the
9044abff6d4SPeter Zijlstra 		 * non-weak function after linking.
9054abff6d4SPeter Zijlstra 		 */
9064abff6d4SPeter Zijlstra 		sym = elf_create_section_symbol(elf, insn_sec);
9074abff6d4SPeter Zijlstra 		if (!sym)
9086342a20eSJosh Poimboeuf 			return NULL;
909ef47cc01SPeter Zijlstra 
9104abff6d4SPeter Zijlstra 		insn_sec->sym = sym;
911ef47cc01SPeter Zijlstra 	}
912ef47cc01SPeter Zijlstra 
9136342a20eSJosh Poimboeuf 	return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
9146342a20eSJosh Poimboeuf 			      elf_text_rela_type(elf));
9156342a20eSJosh Poimboeuf }
9166342a20eSJosh Poimboeuf 
elf_init_reloc_data_sym(struct elf * elf,struct section * sec,unsigned long offset,unsigned int reloc_idx,struct symbol * sym,s64 addend)9176342a20eSJosh Poimboeuf struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
9186342a20eSJosh Poimboeuf 				      unsigned long offset,
9196342a20eSJosh Poimboeuf 				      unsigned int reloc_idx,
9206342a20eSJosh Poimboeuf 				      struct symbol *sym,
9216342a20eSJosh Poimboeuf 				      s64 addend)
9226342a20eSJosh Poimboeuf {
9236342a20eSJosh Poimboeuf 	if (sym->sec && (sec->sh.sh_flags & SHF_EXECINSTR)) {
924*3e7be635SJosh Poimboeuf 		ERROR("bad call to %s() for text symbol %s", __func__, sym->name);
9256342a20eSJosh Poimboeuf 		return NULL;
9266342a20eSJosh Poimboeuf 	}
9276342a20eSJosh Poimboeuf 
9286342a20eSJosh Poimboeuf 	return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
9296342a20eSJosh Poimboeuf 			      elf_data_rela_type(elf));
93034f7c96dSPeter Zijlstra }
93134f7c96dSPeter Zijlstra 
read_relocs(struct elf * elf)932f1974222SMatt Helsley static int read_relocs(struct elf *elf)
933442f04c3SJosh Poimboeuf {
934eb0481bbSJosh Poimboeuf 	unsigned long nr_reloc, max_reloc = 0;
935a5bd6236SJosh Poimboeuf 	struct section *rsec;
936f1974222SMatt Helsley 	struct reloc *reloc;
937442f04c3SJosh Poimboeuf 	unsigned int symndx;
93819526717SPeter Zijlstra 	struct symbol *sym;
93919526717SPeter Zijlstra 	int i;
940442f04c3SJosh Poimboeuf 
941eb0481bbSJosh Poimboeuf 	if (!elf_alloc_hash(reloc, elf->num_relocs))
94225cf0d8aSPeter Zijlstra 		return -1;
94325cf0d8aSPeter Zijlstra 
944a5bd6236SJosh Poimboeuf 	list_for_each_entry(rsec, &elf->sections, list) {
945eb0481bbSJosh Poimboeuf 		if (!is_reloc_sec(rsec))
946442f04c3SJosh Poimboeuf 			continue;
947442f04c3SJosh Poimboeuf 
948a5bd6236SJosh Poimboeuf 		rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
949a5bd6236SJosh Poimboeuf 		if (!rsec->base) {
950*3e7be635SJosh Poimboeuf 			ERROR("can't find base section for reloc section %s", rsec->name);
951442f04c3SJosh Poimboeuf 			return -1;
952442f04c3SJosh Poimboeuf 		}
953442f04c3SJosh Poimboeuf 
954a5bd6236SJosh Poimboeuf 		rsec->base->rsec = rsec;
955442f04c3SJosh Poimboeuf 
956f1974222SMatt Helsley 		nr_reloc = 0;
957ebcef730SJosh Poimboeuf 		rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
958ebcef730SJosh Poimboeuf 		if (!rsec->relocs) {
959*3e7be635SJosh Poimboeuf 			ERROR_GLIBC("calloc");
960442f04c3SJosh Poimboeuf 			return -1;
961442f04c3SJosh Poimboeuf 		}
962ebcef730SJosh Poimboeuf 		for (i = 0; i < sec_num_entries(rsec); i++) {
963ebcef730SJosh Poimboeuf 			reloc = &rsec->relocs[i];
96453257a97SJosh Poimboeuf 
965a5bd6236SJosh Poimboeuf 			reloc->sec = rsec;
966ec24b927SJosh Poimboeuf 			symndx = reloc_sym(reloc);
96719526717SPeter Zijlstra 			reloc->sym = sym = find_symbol_by_index(elf, symndx);
968f1974222SMatt Helsley 			if (!reloc->sym) {
969*3e7be635SJosh Poimboeuf 				ERROR("can't find reloc entry symbol %d for %s", symndx, rsec->name);
970442f04c3SJosh Poimboeuf 				return -1;
971442f04c3SJosh Poimboeuf 			}
972042ba73fSJosh Poimboeuf 
97325cf0d8aSPeter Zijlstra 			elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
974ef753d66SJosh Poimboeuf 			set_sym_next_reloc(reloc, sym->relocs);
975890f10a4SJosh Poimboeuf 			sym->relocs = reloc;
9763a647607SPeter Zijlstra 
977f1974222SMatt Helsley 			nr_reloc++;
978442f04c3SJosh Poimboeuf 		}
979f1974222SMatt Helsley 		max_reloc = max(max_reloc, nr_reloc);
9801e11f3fdSPeter Zijlstra 	}
9811e11f3fdSPeter Zijlstra 
9822daf7fabSJosh Poimboeuf 	if (opts.stats) {
983f1974222SMatt Helsley 		printf("max_reloc: %lu\n", max_reloc);
984eb0481bbSJosh Poimboeuf 		printf("num_relocs: %lu\n", elf->num_relocs);
98525cf0d8aSPeter Zijlstra 		printf("reloc_bits: %d\n", elf->reloc_bits);
986442f04c3SJosh Poimboeuf 	}
987442f04c3SJosh Poimboeuf 
988442f04c3SJosh Poimboeuf 	return 0;
989442f04c3SJosh Poimboeuf }
990442f04c3SJosh Poimboeuf 
elf_open_read(const char * name,int flags)991bc359ff2SIngo Molnar struct elf *elf_open_read(const char *name, int flags)
992442f04c3SJosh Poimboeuf {
993442f04c3SJosh Poimboeuf 	struct elf *elf;
994627fce14SJosh Poimboeuf 	Elf_Cmd cmd;
995442f04c3SJosh Poimboeuf 
996442f04c3SJosh Poimboeuf 	elf_version(EV_CURRENT);
997442f04c3SJosh Poimboeuf 
998442f04c3SJosh Poimboeuf 	elf = malloc(sizeof(*elf));
999442f04c3SJosh Poimboeuf 	if (!elf) {
1000*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("malloc");
1001442f04c3SJosh Poimboeuf 		return NULL;
1002442f04c3SJosh Poimboeuf 	}
10039f71fbcdSMichal Kubecek 	memset(elf, 0, sizeof(*elf));
1004442f04c3SJosh Poimboeuf 
1005442f04c3SJosh Poimboeuf 	INIT_LIST_HEAD(&elf->sections);
1006442f04c3SJosh Poimboeuf 
1007627fce14SJosh Poimboeuf 	elf->fd = open(name, flags);
1008442f04c3SJosh Poimboeuf 	if (elf->fd == -1) {
1009385d11b1SJosh Poimboeuf 		fprintf(stderr, "objtool: Can't open '%s': %s\n",
1010385d11b1SJosh Poimboeuf 			name, strerror(errno));
1011442f04c3SJosh Poimboeuf 		goto err;
1012442f04c3SJosh Poimboeuf 	}
1013442f04c3SJosh Poimboeuf 
1014627fce14SJosh Poimboeuf 	if ((flags & O_ACCMODE) == O_RDONLY)
1015627fce14SJosh Poimboeuf 		cmd = ELF_C_READ_MMAP;
1016627fce14SJosh Poimboeuf 	else if ((flags & O_ACCMODE) == O_RDWR)
1017627fce14SJosh Poimboeuf 		cmd = ELF_C_RDWR;
1018627fce14SJosh Poimboeuf 	else /* O_WRONLY */
1019627fce14SJosh Poimboeuf 		cmd = ELF_C_WRITE;
1020627fce14SJosh Poimboeuf 
1021627fce14SJosh Poimboeuf 	elf->elf = elf_begin(elf->fd, cmd, NULL);
1022442f04c3SJosh Poimboeuf 	if (!elf->elf) {
1023*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_begin");
1024442f04c3SJosh Poimboeuf 		goto err;
1025442f04c3SJosh Poimboeuf 	}
1026442f04c3SJosh Poimboeuf 
1027442f04c3SJosh Poimboeuf 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1028*3e7be635SJosh Poimboeuf 		ERROR_ELF("gelf_getehdr");
1029442f04c3SJosh Poimboeuf 		goto err;
1030442f04c3SJosh Poimboeuf 	}
1031442f04c3SJosh Poimboeuf 
1032442f04c3SJosh Poimboeuf 	if (read_sections(elf))
1033442f04c3SJosh Poimboeuf 		goto err;
1034442f04c3SJosh Poimboeuf 
1035442f04c3SJosh Poimboeuf 	if (read_symbols(elf))
1036442f04c3SJosh Poimboeuf 		goto err;
1037442f04c3SJosh Poimboeuf 
1038f1974222SMatt Helsley 	if (read_relocs(elf))
1039442f04c3SJosh Poimboeuf 		goto err;
1040442f04c3SJosh Poimboeuf 
1041442f04c3SJosh Poimboeuf 	return elf;
1042442f04c3SJosh Poimboeuf 
1043442f04c3SJosh Poimboeuf err:
1044442f04c3SJosh Poimboeuf 	elf_close(elf);
1045442f04c3SJosh Poimboeuf 	return NULL;
1046442f04c3SJosh Poimboeuf }
1047442f04c3SJosh Poimboeuf 
elf_add_string(struct elf * elf,struct section * strtab,char * str)1048417a4dc9SPeter Zijlstra static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
1049417a4dc9SPeter Zijlstra {
1050417a4dc9SPeter Zijlstra 	Elf_Data *data;
1051417a4dc9SPeter Zijlstra 	Elf_Scn *s;
1052417a4dc9SPeter Zijlstra 	int len;
1053417a4dc9SPeter Zijlstra 
1054417a4dc9SPeter Zijlstra 	if (!strtab)
1055417a4dc9SPeter Zijlstra 		strtab = find_section_by_name(elf, ".strtab");
1056417a4dc9SPeter Zijlstra 	if (!strtab) {
1057*3e7be635SJosh Poimboeuf 		ERROR("can't find .strtab section");
1058417a4dc9SPeter Zijlstra 		return -1;
1059417a4dc9SPeter Zijlstra 	}
1060417a4dc9SPeter Zijlstra 
1061417a4dc9SPeter Zijlstra 	s = elf_getscn(elf->elf, strtab->idx);
1062417a4dc9SPeter Zijlstra 	if (!s) {
1063*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_getscn");
1064417a4dc9SPeter Zijlstra 		return -1;
1065417a4dc9SPeter Zijlstra 	}
1066417a4dc9SPeter Zijlstra 
1067417a4dc9SPeter Zijlstra 	data = elf_newdata(s);
1068417a4dc9SPeter Zijlstra 	if (!data) {
1069*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_newdata");
1070417a4dc9SPeter Zijlstra 		return -1;
1071417a4dc9SPeter Zijlstra 	}
1072417a4dc9SPeter Zijlstra 
1073417a4dc9SPeter Zijlstra 	data->d_buf = str;
1074417a4dc9SPeter Zijlstra 	data->d_size = strlen(str) + 1;
1075417a4dc9SPeter Zijlstra 	data->d_align = 1;
1076417a4dc9SPeter Zijlstra 
1077fe255fe6SJoe Lawrence 	len = strtab->sh.sh_size;
1078fe255fe6SJoe Lawrence 	strtab->sh.sh_size += data->d_size;
1079ff408273SJosh Poimboeuf 
1080ff408273SJosh Poimboeuf 	mark_sec_changed(elf, strtab, true);
1081417a4dc9SPeter Zijlstra 
1082417a4dc9SPeter Zijlstra 	return len;
1083417a4dc9SPeter Zijlstra }
1084417a4dc9SPeter Zijlstra 
elf_create_section(struct elf * elf,const char * name,size_t entsize,unsigned int nr)1085627fce14SJosh Poimboeuf struct section *elf_create_section(struct elf *elf, const char *name,
10866342a20eSJosh Poimboeuf 				   size_t entsize, unsigned int nr)
1087627fce14SJosh Poimboeuf {
1088627fce14SJosh Poimboeuf 	struct section *sec, *shstrtab;
1089627fce14SJosh Poimboeuf 	size_t size = entsize * nr;
10903c3ea503SMichael Forney 	Elf_Scn *s;
1091627fce14SJosh Poimboeuf 
1092627fce14SJosh Poimboeuf 	sec = malloc(sizeof(*sec));
1093627fce14SJosh Poimboeuf 	if (!sec) {
1094*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("malloc");
1095627fce14SJosh Poimboeuf 		return NULL;
1096627fce14SJosh Poimboeuf 	}
1097627fce14SJosh Poimboeuf 	memset(sec, 0, sizeof(*sec));
1098627fce14SJosh Poimboeuf 
1099627fce14SJosh Poimboeuf 	INIT_LIST_HEAD(&sec->symbol_list);
1100627fce14SJosh Poimboeuf 
1101627fce14SJosh Poimboeuf 	s = elf_newscn(elf->elf);
1102627fce14SJosh Poimboeuf 	if (!s) {
1103*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_newscn");
1104627fce14SJosh Poimboeuf 		return NULL;
1105627fce14SJosh Poimboeuf 	}
1106627fce14SJosh Poimboeuf 
1107627fce14SJosh Poimboeuf 	sec->name = strdup(name);
1108627fce14SJosh Poimboeuf 	if (!sec->name) {
1109*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("strdup");
1110627fce14SJosh Poimboeuf 		return NULL;
1111627fce14SJosh Poimboeuf 	}
1112627fce14SJosh Poimboeuf 
1113627fce14SJosh Poimboeuf 	sec->idx = elf_ndxscn(s);
1114627fce14SJosh Poimboeuf 
1115627fce14SJosh Poimboeuf 	sec->data = elf_newdata(s);
1116627fce14SJosh Poimboeuf 	if (!sec->data) {
1117*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_newdata");
1118627fce14SJosh Poimboeuf 		return NULL;
1119627fce14SJosh Poimboeuf 	}
1120627fce14SJosh Poimboeuf 
1121627fce14SJosh Poimboeuf 	sec->data->d_size = size;
1122627fce14SJosh Poimboeuf 	sec->data->d_align = 1;
1123627fce14SJosh Poimboeuf 
1124627fce14SJosh Poimboeuf 	if (size) {
1125627fce14SJosh Poimboeuf 		sec->data->d_buf = malloc(size);
1126627fce14SJosh Poimboeuf 		if (!sec->data->d_buf) {
1127*3e7be635SJosh Poimboeuf 			ERROR_GLIBC("malloc");
1128627fce14SJosh Poimboeuf 			return NULL;
1129627fce14SJosh Poimboeuf 		}
1130627fce14SJosh Poimboeuf 		memset(sec->data->d_buf, 0, size);
1131627fce14SJosh Poimboeuf 	}
1132627fce14SJosh Poimboeuf 
1133627fce14SJosh Poimboeuf 	if (!gelf_getshdr(s, &sec->sh)) {
1134*3e7be635SJosh Poimboeuf 		ERROR_ELF("gelf_getshdr");
1135627fce14SJosh Poimboeuf 		return NULL;
1136627fce14SJosh Poimboeuf 	}
1137627fce14SJosh Poimboeuf 
1138627fce14SJosh Poimboeuf 	sec->sh.sh_size = size;
1139627fce14SJosh Poimboeuf 	sec->sh.sh_entsize = entsize;
1140627fce14SJosh Poimboeuf 	sec->sh.sh_type = SHT_PROGBITS;
1141627fce14SJosh Poimboeuf 	sec->sh.sh_addralign = 1;
11422707579dSJosh Poimboeuf 	sec->sh.sh_flags = SHF_ALLOC;
1143627fce14SJosh Poimboeuf 
11446d77d3b4SSimon Ser 	/* Add section name to .shstrtab (or .strtab for Clang) */
1145627fce14SJosh Poimboeuf 	shstrtab = find_section_by_name(elf, ".shstrtab");
11466d77d3b4SSimon Ser 	if (!shstrtab)
11476d77d3b4SSimon Ser 		shstrtab = find_section_by_name(elf, ".strtab");
1148627fce14SJosh Poimboeuf 	if (!shstrtab) {
1149*3e7be635SJosh Poimboeuf 		ERROR("can't find .shstrtab or .strtab section");
1150627fce14SJosh Poimboeuf 		return NULL;
1151627fce14SJosh Poimboeuf 	}
1152417a4dc9SPeter Zijlstra 	sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1153417a4dc9SPeter Zijlstra 	if (sec->sh.sh_name == -1)
1154627fce14SJosh Poimboeuf 		return NULL;
1155627fce14SJosh Poimboeuf 
115653038996SPeter Zijlstra 	list_add_tail(&sec->list, &elf->sections);
115725cf0d8aSPeter Zijlstra 	elf_hash_add(section, &sec->hash, sec->idx);
115825cf0d8aSPeter Zijlstra 	elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
115953038996SPeter Zijlstra 
1160ff408273SJosh Poimboeuf 	mark_sec_changed(elf, sec, true);
11612b10be23SPeter Zijlstra 
1162627fce14SJosh Poimboeuf 	return sec;
1163627fce14SJosh Poimboeuf }
1164627fce14SJosh Poimboeuf 
elf_create_rela_section(struct elf * elf,struct section * sec,unsigned int reloc_nr)116553257a97SJosh Poimboeuf static struct section *elf_create_rela_section(struct elf *elf,
11666342a20eSJosh Poimboeuf 					       struct section *sec,
11676342a20eSJosh Poimboeuf 					       unsigned int reloc_nr)
1168fb414783SMatt Helsley {
1169a5bd6236SJosh Poimboeuf 	struct section *rsec;
117053257a97SJosh Poimboeuf 	char *rsec_name;
1171fb414783SMatt Helsley 
117253257a97SJosh Poimboeuf 	rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
117353257a97SJosh Poimboeuf 	if (!rsec_name) {
1174*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("malloc");
1175fb414783SMatt Helsley 		return NULL;
1176fb414783SMatt Helsley 	}
117753257a97SJosh Poimboeuf 	strcpy(rsec_name, ".rela");
117853257a97SJosh Poimboeuf 	strcat(rsec_name, sec->name);
1179fb414783SMatt Helsley 
11806342a20eSJosh Poimboeuf 	rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr);
118153257a97SJosh Poimboeuf 	free(rsec_name);
1182a5bd6236SJosh Poimboeuf 	if (!rsec)
1183fb414783SMatt Helsley 		return NULL;
1184fb414783SMatt Helsley 
11856342a20eSJosh Poimboeuf 	rsec->data->d_type = ELF_T_RELA;
118653257a97SJosh Poimboeuf 	rsec->sh.sh_type = SHT_RELA;
118753257a97SJosh Poimboeuf 	rsec->sh.sh_addralign = elf_addr_size(elf);
1188a5bd6236SJosh Poimboeuf 	rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1189a5bd6236SJosh Poimboeuf 	rsec->sh.sh_info = sec->idx;
1190a5bd6236SJosh Poimboeuf 	rsec->sh.sh_flags = SHF_INFO_LINK;
1191fb414783SMatt Helsley 
1192ebcef730SJosh Poimboeuf 	rsec->relocs = calloc(sec_num_entries(rsec), sizeof(struct reloc));
1193ebcef730SJosh Poimboeuf 	if (!rsec->relocs) {
1194*3e7be635SJosh Poimboeuf 		ERROR_GLIBC("calloc");
1195e0a9349bSJosh Poimboeuf 		return NULL;
1196e0a9349bSJosh Poimboeuf 	}
1197e0a9349bSJosh Poimboeuf 
11986342a20eSJosh Poimboeuf 	sec->rsec = rsec;
11996342a20eSJosh Poimboeuf 	rsec->base = sec;
12006342a20eSJosh Poimboeuf 
1201a5bd6236SJosh Poimboeuf 	return rsec;
1202fb414783SMatt Helsley }
1203fb414783SMatt Helsley 
elf_create_section_pair(struct elf * elf,const char * name,size_t entsize,unsigned int nr,unsigned int reloc_nr)12046342a20eSJosh Poimboeuf struct section *elf_create_section_pair(struct elf *elf, const char *name,
12056342a20eSJosh Poimboeuf 					size_t entsize, unsigned int nr,
12066342a20eSJosh Poimboeuf 					unsigned int reloc_nr)
12076342a20eSJosh Poimboeuf {
12086342a20eSJosh Poimboeuf 	struct section *sec;
12096342a20eSJosh Poimboeuf 
12106342a20eSJosh Poimboeuf 	sec = elf_create_section(elf, name, entsize, nr);
12116342a20eSJosh Poimboeuf 	if (!sec)
12126342a20eSJosh Poimboeuf 		return NULL;
12136342a20eSJosh Poimboeuf 
12146342a20eSJosh Poimboeuf 	if (!elf_create_rela_section(elf, sec, reloc_nr))
12156342a20eSJosh Poimboeuf 		return NULL;
12166342a20eSJosh Poimboeuf 
12176342a20eSJosh Poimboeuf 	return sec;
12186342a20eSJosh Poimboeuf }
12196342a20eSJosh Poimboeuf 
elf_write_insn(struct elf * elf,struct section * sec,unsigned long offset,unsigned int len,const char * insn)1220fdabdd0bSPeter Zijlstra int elf_write_insn(struct elf *elf, struct section *sec,
1221fdabdd0bSPeter Zijlstra 		   unsigned long offset, unsigned int len,
1222fdabdd0bSPeter Zijlstra 		   const char *insn)
1223fdabdd0bSPeter Zijlstra {
1224fdabdd0bSPeter Zijlstra 	Elf_Data *data = sec->data;
1225fdabdd0bSPeter Zijlstra 
1226fdabdd0bSPeter Zijlstra 	if (data->d_type != ELF_T_BYTE || data->d_off) {
1227*3e7be635SJosh Poimboeuf 		ERROR("write to unexpected data for section: %s", sec->name);
1228fdabdd0bSPeter Zijlstra 		return -1;
1229fdabdd0bSPeter Zijlstra 	}
1230fdabdd0bSPeter Zijlstra 
1231fdabdd0bSPeter Zijlstra 	memcpy(data->d_buf + offset, insn, len);
1232fdabdd0bSPeter Zijlstra 
1233ff408273SJosh Poimboeuf 	mark_sec_changed(elf, sec, true);
1234fdabdd0bSPeter Zijlstra 
1235fdabdd0bSPeter Zijlstra 	return 0;
1236fdabdd0bSPeter Zijlstra }
1237fdabdd0bSPeter Zijlstra 
123813f60e80SPeter Zijlstra /*
123913f60e80SPeter Zijlstra  * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
124013f60e80SPeter Zijlstra  * do you:
124113f60e80SPeter Zijlstra  *
124213f60e80SPeter Zijlstra  *   A) adhere to the section header and truncate the data, or
124313f60e80SPeter Zijlstra  *   B) ignore the section header and write out all the data you've got?
124413f60e80SPeter Zijlstra  *
124513f60e80SPeter Zijlstra  * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
124613f60e80SPeter Zijlstra  */
elf_truncate_section(struct elf * elf,struct section * sec)124713f60e80SPeter Zijlstra static int elf_truncate_section(struct elf *elf, struct section *sec)
124813f60e80SPeter Zijlstra {
124913f60e80SPeter Zijlstra 	u64 size = sec->sh.sh_size;
125013f60e80SPeter Zijlstra 	bool truncated = false;
125113f60e80SPeter Zijlstra 	Elf_Data *data = NULL;
125213f60e80SPeter Zijlstra 	Elf_Scn *s;
125313f60e80SPeter Zijlstra 
125413f60e80SPeter Zijlstra 	s = elf_getscn(elf->elf, sec->idx);
125513f60e80SPeter Zijlstra 	if (!s) {
1256*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_getscn");
125713f60e80SPeter Zijlstra 		return -1;
125813f60e80SPeter Zijlstra 	}
125913f60e80SPeter Zijlstra 
126013f60e80SPeter Zijlstra 	for (;;) {
126113f60e80SPeter Zijlstra 		/* get next data descriptor for the relevant section */
126213f60e80SPeter Zijlstra 		data = elf_getdata(s, data);
126313f60e80SPeter Zijlstra 
126413f60e80SPeter Zijlstra 		if (!data) {
126513f60e80SPeter Zijlstra 			if (size) {
1266*3e7be635SJosh Poimboeuf 				ERROR("end of section data but non-zero size left\n");
126713f60e80SPeter Zijlstra 				return -1;
126813f60e80SPeter Zijlstra 			}
126913f60e80SPeter Zijlstra 			return 0;
127013f60e80SPeter Zijlstra 		}
127113f60e80SPeter Zijlstra 
127213f60e80SPeter Zijlstra 		if (truncated) {
127313f60e80SPeter Zijlstra 			/* when we remove symbols */
1274*3e7be635SJosh Poimboeuf 			ERROR("truncated; but more data\n");
127513f60e80SPeter Zijlstra 			return -1;
127613f60e80SPeter Zijlstra 		}
127713f60e80SPeter Zijlstra 
127813f60e80SPeter Zijlstra 		if (!data->d_size) {
1279*3e7be635SJosh Poimboeuf 			ERROR("zero size data");
128013f60e80SPeter Zijlstra 			return -1;
128113f60e80SPeter Zijlstra 		}
128213f60e80SPeter Zijlstra 
128313f60e80SPeter Zijlstra 		if (data->d_size > size) {
128413f60e80SPeter Zijlstra 			truncated = true;
128513f60e80SPeter Zijlstra 			data->d_size = size;
128613f60e80SPeter Zijlstra 		}
128713f60e80SPeter Zijlstra 
128813f60e80SPeter Zijlstra 		size -= data->d_size;
128913f60e80SPeter Zijlstra 	}
129013f60e80SPeter Zijlstra }
129113f60e80SPeter Zijlstra 
elf_write(struct elf * elf)12922b10be23SPeter Zijlstra int elf_write(struct elf *elf)
1293627fce14SJosh Poimboeuf {
1294627fce14SJosh Poimboeuf 	struct section *sec;
1295627fce14SJosh Poimboeuf 	Elf_Scn *s;
1296627fce14SJosh Poimboeuf 
12973a647607SPeter Zijlstra 	/* Update changed relocation sections and section headers: */
1298627fce14SJosh Poimboeuf 	list_for_each_entry(sec, &elf->sections, list) {
129913f60e80SPeter Zijlstra 		if (sec->truncate)
130013f60e80SPeter Zijlstra 			elf_truncate_section(elf, sec);
130113f60e80SPeter Zijlstra 
1302ff408273SJosh Poimboeuf 		if (sec_changed(sec)) {
1303627fce14SJosh Poimboeuf 			s = elf_getscn(elf->elf, sec->idx);
1304627fce14SJosh Poimboeuf 			if (!s) {
1305*3e7be635SJosh Poimboeuf 				ERROR_ELF("elf_getscn");
1306627fce14SJosh Poimboeuf 				return -1;
1307627fce14SJosh Poimboeuf 			}
1308ff408273SJosh Poimboeuf 
1309ff408273SJosh Poimboeuf 			/* Note this also flags the section dirty */
1310627fce14SJosh Poimboeuf 			if (!gelf_update_shdr(s, &sec->sh)) {
1311*3e7be635SJosh Poimboeuf 				ERROR_ELF("gelf_update_shdr");
1312627fce14SJosh Poimboeuf 				return -1;
1313627fce14SJosh Poimboeuf 			}
13142b10be23SPeter Zijlstra 
1315ff408273SJosh Poimboeuf 			mark_sec_changed(elf, sec, false);
1316627fce14SJosh Poimboeuf 		}
1317627fce14SJosh Poimboeuf 	}
1318627fce14SJosh Poimboeuf 
131997dab2aeSJosh Poimboeuf 	/* Make sure the new section header entries get updated properly. */
132097dab2aeSJosh Poimboeuf 	elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
132197dab2aeSJosh Poimboeuf 
132297dab2aeSJosh Poimboeuf 	/* Write all changes to the file. */
1323627fce14SJosh Poimboeuf 	if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1324*3e7be635SJosh Poimboeuf 		ERROR_ELF("elf_update");
1325627fce14SJosh Poimboeuf 		return -1;
1326627fce14SJosh Poimboeuf 	}
1327627fce14SJosh Poimboeuf 
13282b10be23SPeter Zijlstra 	elf->changed = false;
13292b10be23SPeter Zijlstra 
1330627fce14SJosh Poimboeuf 	return 0;
1331627fce14SJosh Poimboeuf }
1332627fce14SJosh Poimboeuf 
elf_close(struct elf * elf)1333442f04c3SJosh Poimboeuf void elf_close(struct elf *elf)
1334442f04c3SJosh Poimboeuf {
1335baa41469SJosh Poimboeuf 	if (elf->elf)
1336baa41469SJosh Poimboeuf 		elf_end(elf->elf);
1337baa41469SJosh Poimboeuf 
1338baa41469SJosh Poimboeuf 	if (elf->fd > 0)
1339baa41469SJosh Poimboeuf 		close(elf->fd);
1340baa41469SJosh Poimboeuf 
13415201a9bcSJosh Poimboeuf 	/*
13425201a9bcSJosh Poimboeuf 	 * NOTE: All remaining allocations are leaked on purpose.  Objtool is
13435201a9bcSJosh Poimboeuf 	 * about to exit anyway.
13445201a9bcSJosh Poimboeuf 	 */
1345442f04c3SJosh Poimboeuf }
1346