xref: /openbmc/linux/tools/objtool/elf.c (revision 9f71fbcd)
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 <jpoimboe@redhat.com>
7442f04c3SJosh Poimboeuf  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
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 
2522566c16SArtem Savkov #define MAX_NAME_LEN 128
2622566c16SArtem Savkov 
str_hash(const char * str)27ae358196SPeter Zijlstra static inline u32 str_hash(const char *str)
28ae358196SPeter Zijlstra {
29ae358196SPeter Zijlstra 	return jhash(str, strlen(str), 0);
30ae358196SPeter Zijlstra }
31ae358196SPeter Zijlstra 
3225cf0d8aSPeter Zijlstra #define __elf_table(name)	(elf->name##_hash)
3325cf0d8aSPeter Zijlstra #define __elf_bits(name)	(elf->name##_bits)
3434f7c96dSPeter Zijlstra 
3502b54001SJosh Poimboeuf #define __elf_table_entry(name, key) \
3602b54001SJosh Poimboeuf 	__elf_table(name)[hash_min(key, __elf_bits(name))]
3702b54001SJosh Poimboeuf 
3825cf0d8aSPeter Zijlstra #define elf_hash_add(name, node, key)					\
3902b54001SJosh Poimboeuf ({									\
4002b54001SJosh Poimboeuf 	struct elf_hash_node *__node = node;				\
4102b54001SJosh Poimboeuf 	__node->next = __elf_table_entry(name, key);			\
4202b54001SJosh Poimboeuf 	__elf_table_entry(name, key) = __node;				\
4302b54001SJosh Poimboeuf })
4402b54001SJosh Poimboeuf 
__elf_hash_del(struct elf_hash_node * node,struct elf_hash_node ** head)4502b54001SJosh Poimboeuf static inline void __elf_hash_del(struct elf_hash_node *node,
4602b54001SJosh Poimboeuf 				  struct elf_hash_node **head)
4702b54001SJosh Poimboeuf {
4802b54001SJosh Poimboeuf 	struct elf_hash_node *cur, *prev;
4902b54001SJosh Poimboeuf 
5002b54001SJosh Poimboeuf 	if (node == *head) {
5102b54001SJosh Poimboeuf 		*head = node->next;
5202b54001SJosh Poimboeuf 		return;
5302b54001SJosh Poimboeuf 	}
5402b54001SJosh Poimboeuf 
5502b54001SJosh Poimboeuf 	for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {
5602b54001SJosh Poimboeuf 		if (cur == node) {
5702b54001SJosh Poimboeuf 			prev->next = cur->next;
5802b54001SJosh Poimboeuf 			break;
5902b54001SJosh Poimboeuf 		}
6002b54001SJosh Poimboeuf 	}
6102b54001SJosh Poimboeuf }
6202b54001SJosh Poimboeuf 
6302b54001SJosh Poimboeuf #define elf_hash_del(name, node, key) \
6402b54001SJosh Poimboeuf 	__elf_hash_del(node, &__elf_table_entry(name, key))
6502b54001SJosh Poimboeuf 
6602b54001SJosh Poimboeuf #define elf_list_entry(ptr, type, member)				\
6702b54001SJosh Poimboeuf ({									\
6802b54001SJosh Poimboeuf 	typeof(ptr) __ptr = (ptr);					\
6902b54001SJosh Poimboeuf 	__ptr ? container_of(__ptr, type, member) : NULL;		\
7002b54001SJosh Poimboeuf })
7134f7c96dSPeter Zijlstra 
7234f7c96dSPeter Zijlstra #define elf_hash_for_each_possible(name, obj, member, key)		\
7302b54001SJosh Poimboeuf 	for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \
7402b54001SJosh Poimboeuf 	     obj;							\
7502b54001SJosh Poimboeuf 	     obj = elf_list_entry(obj->member.next, typeof(*(obj)), member))
7625cf0d8aSPeter Zijlstra 
7725cf0d8aSPeter Zijlstra #define elf_alloc_hash(name, size) \
7825cf0d8aSPeter Zijlstra ({ \
7925cf0d8aSPeter Zijlstra 	__elf_bits(name) = max(10, ilog2(size)); \
8002b54001SJosh Poimboeuf 	__elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \
8125cf0d8aSPeter Zijlstra 				 PROT_READ|PROT_WRITE, \
8225cf0d8aSPeter Zijlstra 				 MAP_PRIVATE|MAP_ANON, -1, 0); \
8325cf0d8aSPeter Zijlstra 	if (__elf_table(name) == (void *)-1L) { \
8425cf0d8aSPeter Zijlstra 		WARN("mmap fail " #name); \
8525cf0d8aSPeter Zijlstra 		__elf_table(name) = NULL; \
8625cf0d8aSPeter Zijlstra 	} \
8725cf0d8aSPeter Zijlstra 	__elf_table(name); \
8825cf0d8aSPeter Zijlstra })
8934f7c96dSPeter Zijlstra 
__sym_start(struct symbol * s)905da6aea3SPeter Zijlstra static inline unsigned long __sym_start(struct symbol *s)
912a362eccSPeter Zijlstra {
925da6aea3SPeter Zijlstra 	return s->offset;
932a362eccSPeter Zijlstra }
942a362eccSPeter Zijlstra 
__sym_last(struct symbol * s)955da6aea3SPeter Zijlstra static inline unsigned long __sym_last(struct symbol *s)
962a362eccSPeter Zijlstra {
975da6aea3SPeter Zijlstra 	return s->offset + s->len - 1;
982a362eccSPeter Zijlstra }
992a362eccSPeter Zijlstra 
1005da6aea3SPeter Zijlstra INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,
1015da6aea3SPeter Zijlstra 		     __sym_start, __sym_last, static, __sym)
1025da6aea3SPeter Zijlstra 
1035da6aea3SPeter Zijlstra #define __sym_for_each(_iter, _tree, _start, _end)			\
1045da6aea3SPeter Zijlstra 	for (_iter = __sym_iter_first((_tree), (_start), (_end));	\
1055da6aea3SPeter Zijlstra 	     _iter; _iter = __sym_iter_next(_iter, (_start), (_end)))
1065da6aea3SPeter Zijlstra 
1074adb2368SPeter Zijlstra struct symbol_hole {
1084adb2368SPeter Zijlstra 	unsigned long key;
1094adb2368SPeter Zijlstra 	const struct symbol *sym;
1104adb2368SPeter Zijlstra };
1114adb2368SPeter Zijlstra 
1124adb2368SPeter Zijlstra /*
1134adb2368SPeter Zijlstra  * Find !section symbol where @offset is after it.
1144adb2368SPeter Zijlstra  */
symbol_hole_by_offset(const void * key,const struct rb_node * node)1154adb2368SPeter Zijlstra static int symbol_hole_by_offset(const void *key, const struct rb_node *node)
1164adb2368SPeter Zijlstra {
1174adb2368SPeter Zijlstra 	const struct symbol *s = rb_entry(node, struct symbol, node);
1184adb2368SPeter Zijlstra 	struct symbol_hole *sh = (void *)key;
1194adb2368SPeter Zijlstra 
1204adb2368SPeter Zijlstra 	if (sh->key < s->offset)
1214adb2368SPeter Zijlstra 		return -1;
1224adb2368SPeter Zijlstra 
1234adb2368SPeter Zijlstra 	if (sh->key >= s->offset + s->len) {
1244adb2368SPeter Zijlstra 		if (s->type != STT_SECTION)
1254adb2368SPeter Zijlstra 			sh->sym = s;
1264adb2368SPeter Zijlstra 		return 1;
1274adb2368SPeter Zijlstra 	}
1284adb2368SPeter Zijlstra 
1294adb2368SPeter Zijlstra 	return 0;
1304adb2368SPeter Zijlstra }
1314adb2368SPeter Zijlstra 
find_section_by_name(const struct elf * elf,const char * name)132894e48caSIngo Molnar struct section *find_section_by_name(const struct elf *elf, const char *name)
133442f04c3SJosh Poimboeuf {
134442f04c3SJosh Poimboeuf 	struct section *sec;
135442f04c3SJosh Poimboeuf 
13625cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {
137442f04c3SJosh Poimboeuf 		if (!strcmp(sec->name, name))
138442f04c3SJosh Poimboeuf 			return sec;
13925cf0d8aSPeter Zijlstra 	}
140442f04c3SJosh Poimboeuf 
141442f04c3SJosh Poimboeuf 	return NULL;
142442f04c3SJosh Poimboeuf }
143442f04c3SJosh Poimboeuf 
find_section_by_index(struct elf * elf,unsigned int idx)144442f04c3SJosh Poimboeuf static struct section *find_section_by_index(struct elf *elf,
145442f04c3SJosh Poimboeuf 					     unsigned int idx)
146442f04c3SJosh Poimboeuf {
147442f04c3SJosh Poimboeuf 	struct section *sec;
148442f04c3SJosh Poimboeuf 
14925cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(section, sec, hash, idx) {
150442f04c3SJosh Poimboeuf 		if (sec->idx == idx)
151442f04c3SJosh Poimboeuf 			return sec;
15225cf0d8aSPeter Zijlstra 	}
153442f04c3SJosh Poimboeuf 
154442f04c3SJosh Poimboeuf 	return NULL;
155442f04c3SJosh Poimboeuf }
156442f04c3SJosh Poimboeuf 
find_symbol_by_index(struct elf * elf,unsigned int idx)157442f04c3SJosh Poimboeuf static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
158442f04c3SJosh Poimboeuf {
159442f04c3SJosh Poimboeuf 	struct symbol *sym;
160442f04c3SJosh Poimboeuf 
16125cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(symbol, sym, hash, idx) {
162442f04c3SJosh Poimboeuf 		if (sym->idx == idx)
163442f04c3SJosh Poimboeuf 			return sym;
16425cf0d8aSPeter Zijlstra 	}
165442f04c3SJosh Poimboeuf 
166442f04c3SJosh Poimboeuf 	return NULL;
167442f04c3SJosh Poimboeuf }
168442f04c3SJosh Poimboeuf 
find_symbol_by_offset(struct section * sec,unsigned long offset)169442f04c3SJosh Poimboeuf struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
170442f04c3SJosh Poimboeuf {
1715da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
1725da6aea3SPeter Zijlstra 	struct symbol *iter;
173442f04c3SJosh Poimboeuf 
1745da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
1755da6aea3SPeter Zijlstra 		if (iter->offset == offset && iter->type != STT_SECTION)
1765da6aea3SPeter Zijlstra 			return iter;
1772a362eccSPeter Zijlstra 	}
1787acfe531SJosh Poimboeuf 
1797acfe531SJosh Poimboeuf 	return NULL;
1807acfe531SJosh Poimboeuf }
1817acfe531SJosh Poimboeuf 
find_func_by_offset(struct section * sec,unsigned long offset)1827acfe531SJosh Poimboeuf struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
1837acfe531SJosh Poimboeuf {
1845da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
1855da6aea3SPeter Zijlstra 	struct symbol *iter;
1867acfe531SJosh Poimboeuf 
1875da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
1885da6aea3SPeter Zijlstra 		if (iter->offset == offset && iter->type == STT_FUNC)
1895da6aea3SPeter Zijlstra 			return iter;
1902a362eccSPeter Zijlstra 	}
1912a362eccSPeter Zijlstra 
1922a362eccSPeter Zijlstra 	return NULL;
1932a362eccSPeter Zijlstra }
1942a362eccSPeter Zijlstra 
find_symbol_containing(const struct section * sec,unsigned long offset)195b490f453SMiroslav Benes struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
1962a362eccSPeter Zijlstra {
1975da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
1985da6aea3SPeter Zijlstra 	struct symbol *iter;
1992a362eccSPeter Zijlstra 
2005da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
2015da6aea3SPeter Zijlstra 		if (iter->type != STT_SECTION)
2025da6aea3SPeter Zijlstra 			return iter;
2032a362eccSPeter Zijlstra 	}
2042a362eccSPeter Zijlstra 
2052a362eccSPeter Zijlstra 	return NULL;
2062a362eccSPeter Zijlstra }
2072a362eccSPeter Zijlstra 
2084adb2368SPeter Zijlstra /*
2094adb2368SPeter Zijlstra  * Returns size of hole starting at @offset.
2104adb2368SPeter Zijlstra  */
find_symbol_hole_containing(const struct section * sec,unsigned long offset)2114adb2368SPeter Zijlstra int find_symbol_hole_containing(const struct section *sec, unsigned long offset)
2124adb2368SPeter Zijlstra {
2134adb2368SPeter Zijlstra 	struct symbol_hole hole = {
2144adb2368SPeter Zijlstra 		.key = offset,
2154adb2368SPeter Zijlstra 		.sym = NULL,
2164adb2368SPeter Zijlstra 	};
2174adb2368SPeter Zijlstra 	struct rb_node *n;
2184adb2368SPeter Zijlstra 	struct symbol *s;
2194adb2368SPeter Zijlstra 
2204adb2368SPeter Zijlstra 	/*
2214adb2368SPeter Zijlstra 	 * Find the rightmost symbol for which @offset is after it.
2224adb2368SPeter Zijlstra 	 */
2235da6aea3SPeter Zijlstra 	n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset);
2244adb2368SPeter Zijlstra 
2254adb2368SPeter Zijlstra 	/* found a symbol that contains @offset */
2264adb2368SPeter Zijlstra 	if (n)
2274adb2368SPeter Zijlstra 		return 0; /* not a hole */
2284adb2368SPeter Zijlstra 
2294adb2368SPeter Zijlstra 	/* didn't find a symbol for which @offset is after it */
2304adb2368SPeter Zijlstra 	if (!hole.sym)
2314adb2368SPeter Zijlstra 		return 0; /* not a hole */
2324adb2368SPeter Zijlstra 
2334adb2368SPeter Zijlstra 	/* @offset >= sym->offset + sym->len, find symbol after it */
2344adb2368SPeter Zijlstra 	n = rb_next(&hole.sym->node);
2354adb2368SPeter Zijlstra 	if (!n)
2364adb2368SPeter Zijlstra 		return -1; /* until end of address space */
2374adb2368SPeter Zijlstra 
2384adb2368SPeter Zijlstra 	/* hole until start of next symbol */
2394adb2368SPeter Zijlstra 	s = rb_entry(n, struct symbol, node);
2404adb2368SPeter Zijlstra 	return s->offset - offset;
2414adb2368SPeter Zijlstra }
2424adb2368SPeter Zijlstra 
find_func_containing(struct section * sec,unsigned long offset)24353d20720SPeter Zijlstra struct symbol *find_func_containing(struct section *sec, unsigned long offset)
2442a362eccSPeter Zijlstra {
2455da6aea3SPeter Zijlstra 	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;
2465da6aea3SPeter Zijlstra 	struct symbol *iter;
2472a362eccSPeter Zijlstra 
2485da6aea3SPeter Zijlstra 	__sym_for_each(iter, tree, offset, offset) {
2495da6aea3SPeter Zijlstra 		if (iter->type == STT_FUNC)
2505da6aea3SPeter Zijlstra 			return iter;
2512a362eccSPeter Zijlstra 	}
252442f04c3SJosh Poimboeuf 
253442f04c3SJosh Poimboeuf 	return NULL;
254442f04c3SJosh Poimboeuf }
255442f04c3SJosh Poimboeuf 
find_symbol_by_name(const struct elf * elf,const char * name)256894e48caSIngo Molnar struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
25713810435SJosh Poimboeuf {
25813810435SJosh Poimboeuf 	struct symbol *sym;
25913810435SJosh Poimboeuf 
26025cf0d8aSPeter Zijlstra 	elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {
26113810435SJosh Poimboeuf 		if (!strcmp(sym->name, name))
26213810435SJosh Poimboeuf 			return sym;
26325cf0d8aSPeter Zijlstra 	}
26413810435SJosh Poimboeuf 
26513810435SJosh Poimboeuf 	return NULL;
26613810435SJosh Poimboeuf }
26713810435SJosh Poimboeuf 
find_reloc_by_dest_range(const struct elf * elf,struct section * sec,unsigned long offset,unsigned int len)268f1974222SMatt Helsley struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
2698b5fa6bcSPeter Zijlstra 				     unsigned long offset, unsigned int len)
270442f04c3SJosh Poimboeuf {
271f1974222SMatt Helsley 	struct reloc *reloc, *r = NULL;
272a5bd6236SJosh Poimboeuf 	struct section *rsec;
273042ba73fSJosh Poimboeuf 	unsigned long o;
274442f04c3SJosh Poimboeuf 
275a5bd6236SJosh Poimboeuf 	rsec = sec->rsec;
276a5bd6236SJosh Poimboeuf 	if (!rsec)
277442f04c3SJosh Poimboeuf 		return NULL;
278442f04c3SJosh Poimboeuf 
27974b873e4SPeter Zijlstra 	for_offset_range(o, offset, offset + len) {
28025cf0d8aSPeter Zijlstra 		elf_hash_for_each_possible(reloc, reloc, hash,
281a5bd6236SJosh Poimboeuf 					   sec_offset_hash(rsec, o)) {
282a5bd6236SJosh Poimboeuf 			if (reloc->sec != rsec)
28374b873e4SPeter Zijlstra 				continue;
28474b873e4SPeter Zijlstra 
285e4cbb9b8SJosh Poimboeuf 			if (reloc_offset(reloc) >= offset &&
286e4cbb9b8SJosh Poimboeuf 			    reloc_offset(reloc) < offset + len) {
287e4cbb9b8SJosh Poimboeuf 				if (!r || reloc_offset(reloc) < reloc_offset(r))
288f1974222SMatt Helsley 					r = reloc;
2898b5fa6bcSPeter Zijlstra 			}
2908b5fa6bcSPeter Zijlstra 		}
29174b873e4SPeter Zijlstra 		if (r)
29274b873e4SPeter Zijlstra 			return r;
29374b873e4SPeter Zijlstra 	}
294442f04c3SJosh Poimboeuf 
295442f04c3SJosh Poimboeuf 	return NULL;
296442f04c3SJosh Poimboeuf }
297442f04c3SJosh Poimboeuf 
find_reloc_by_dest(const struct elf * elf,struct section * sec,unsigned long offset)298f1974222SMatt Helsley struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
299442f04c3SJosh Poimboeuf {
300f1974222SMatt Helsley 	return find_reloc_by_dest_range(elf, sec, offset, 1);
301442f04c3SJosh Poimboeuf }
302442f04c3SJosh Poimboeuf 
is_dwarf_section(struct section * sec)303b4c96ef0SJosh Poimboeuf static bool is_dwarf_section(struct section *sec)
304b4c96ef0SJosh Poimboeuf {
305b4c96ef0SJosh Poimboeuf 	return !strncmp(sec->name, ".debug_", 7);
306b4c96ef0SJosh Poimboeuf }
307b4c96ef0SJosh Poimboeuf 
read_sections(struct elf * elf)308442f04c3SJosh Poimboeuf static int read_sections(struct elf *elf)
309442f04c3SJosh Poimboeuf {
310442f04c3SJosh Poimboeuf 	Elf_Scn *s = NULL;
311442f04c3SJosh Poimboeuf 	struct section *sec;
312442f04c3SJosh Poimboeuf 	size_t shstrndx, sections_nr;
313442f04c3SJosh Poimboeuf 	int i;
314442f04c3SJosh Poimboeuf 
315442f04c3SJosh Poimboeuf 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
316baa41469SJosh Poimboeuf 		WARN_ELF("elf_getshdrnum");
317442f04c3SJosh Poimboeuf 		return -1;
318442f04c3SJosh Poimboeuf 	}
319442f04c3SJosh Poimboeuf 
320442f04c3SJosh Poimboeuf 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
321baa41469SJosh Poimboeuf 		WARN_ELF("elf_getshdrstrndx");
322442f04c3SJosh Poimboeuf 		return -1;
323442f04c3SJosh Poimboeuf 	}
324442f04c3SJosh Poimboeuf 
32525cf0d8aSPeter Zijlstra 	if (!elf_alloc_hash(section, sections_nr) ||
32625cf0d8aSPeter Zijlstra 	    !elf_alloc_hash(section_name, sections_nr))
32725cf0d8aSPeter Zijlstra 		return -1;
32825cf0d8aSPeter Zijlstra 
3298045b8f0SThomas Weißschuh 	elf->section_data = calloc(sections_nr, sizeof(*sec));
3308045b8f0SThomas Weißschuh 	if (!elf->section_data) {
3318045b8f0SThomas Weißschuh 		perror("calloc");
332442f04c3SJosh Poimboeuf 		return -1;
333442f04c3SJosh Poimboeuf 	}
3348045b8f0SThomas Weißschuh 	for (i = 0; i < sections_nr; i++) {
3358045b8f0SThomas Weißschuh 		sec = &elf->section_data[i];
336442f04c3SJosh Poimboeuf 
337a196e171SJosh Poimboeuf 		INIT_LIST_HEAD(&sec->symbol_list);
338442f04c3SJosh Poimboeuf 
339442f04c3SJosh Poimboeuf 		s = elf_getscn(elf->elf, i);
340442f04c3SJosh Poimboeuf 		if (!s) {
341baa41469SJosh Poimboeuf 			WARN_ELF("elf_getscn");
342442f04c3SJosh Poimboeuf 			return -1;
343442f04c3SJosh Poimboeuf 		}
344442f04c3SJosh Poimboeuf 
345442f04c3SJosh Poimboeuf 		sec->idx = elf_ndxscn(s);
346442f04c3SJosh Poimboeuf 
347442f04c3SJosh Poimboeuf 		if (!gelf_getshdr(s, &sec->sh)) {
348baa41469SJosh Poimboeuf 			WARN_ELF("gelf_getshdr");
349442f04c3SJosh Poimboeuf 			return -1;
350442f04c3SJosh Poimboeuf 		}
351442f04c3SJosh Poimboeuf 
352442f04c3SJosh Poimboeuf 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
353442f04c3SJosh Poimboeuf 		if (!sec->name) {
354baa41469SJosh Poimboeuf 			WARN_ELF("elf_strptr");
355442f04c3SJosh Poimboeuf 			return -1;
356442f04c3SJosh Poimboeuf 		}
357442f04c3SJosh Poimboeuf 
358b4c96ef0SJosh Poimboeuf 		if (sec->sh.sh_size != 0 && !is_dwarf_section(sec)) {
359baa41469SJosh Poimboeuf 			sec->data = elf_getdata(s, NULL);
360baa41469SJosh Poimboeuf 			if (!sec->data) {
361baa41469SJosh Poimboeuf 				WARN_ELF("elf_getdata");
362442f04c3SJosh Poimboeuf 				return -1;
363442f04c3SJosh Poimboeuf 			}
364baa41469SJosh Poimboeuf 			if (sec->data->d_off != 0 ||
365baa41469SJosh Poimboeuf 			    sec->data->d_size != sec->sh.sh_size) {
366df968c93SPetr Vandrovec 				WARN("unexpected data attributes for %s",
367df968c93SPetr Vandrovec 				     sec->name);
368442f04c3SJosh Poimboeuf 				return -1;
369442f04c3SJosh Poimboeuf 			}
370df968c93SPetr Vandrovec 		}
37153038996SPeter Zijlstra 
37253038996SPeter Zijlstra 		list_add_tail(&sec->list, &elf->sections);
37325cf0d8aSPeter Zijlstra 		elf_hash_add(section, &sec->hash, sec->idx);
37425cf0d8aSPeter Zijlstra 		elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
375eb0481bbSJosh Poimboeuf 
376eb0481bbSJosh Poimboeuf 		if (is_reloc_sec(sec))
377ebcef730SJosh Poimboeuf 			elf->num_relocs += sec_num_entries(sec);
378442f04c3SJosh Poimboeuf 	}
379442f04c3SJosh Poimboeuf 
3802daf7fabSJosh Poimboeuf 	if (opts.stats) {
3811e11f3fdSPeter Zijlstra 		printf("nr_sections: %lu\n", (unsigned long)sections_nr);
38225cf0d8aSPeter Zijlstra 		printf("section_bits: %d\n", elf->section_bits);
38325cf0d8aSPeter Zijlstra 	}
3841e11f3fdSPeter Zijlstra 
385442f04c3SJosh Poimboeuf 	/* sanity check, one more call to elf_nextscn() should return NULL */
386442f04c3SJosh Poimboeuf 	if (elf_nextscn(elf->elf, s)) {
387442f04c3SJosh Poimboeuf 		WARN("section entry mismatch");
388442f04c3SJosh Poimboeuf 		return -1;
389442f04c3SJosh Poimboeuf 	}
390442f04c3SJosh Poimboeuf 
391442f04c3SJosh Poimboeuf 	return 0;
392442f04c3SJosh Poimboeuf }
393442f04c3SJosh Poimboeuf 
elf_add_symbol(struct elf * elf,struct symbol * sym)3949a7827b7SPeter Zijlstra static void elf_add_symbol(struct elf *elf, struct symbol *sym)
3959a7827b7SPeter Zijlstra {
3969a7827b7SPeter Zijlstra 	struct list_head *entry;
3979a7827b7SPeter Zijlstra 	struct rb_node *pnode;
3985da6aea3SPeter Zijlstra 	struct symbol *iter;
3999a7827b7SPeter Zijlstra 
400ead165faSPeter Zijlstra 	INIT_LIST_HEAD(&sym->pv_target);
401ead165faSPeter Zijlstra 	sym->alias = sym;
402ead165faSPeter Zijlstra 
4039a7827b7SPeter Zijlstra 	sym->type = GELF_ST_TYPE(sym->sym.st_info);
4049a7827b7SPeter Zijlstra 	sym->bind = GELF_ST_BIND(sym->sym.st_info);
4059a7827b7SPeter Zijlstra 
406753da417SJosh Poimboeuf 	if (sym->type == STT_FILE)
407753da417SJosh Poimboeuf 		elf->num_files++;
408753da417SJosh Poimboeuf 
4099a7827b7SPeter Zijlstra 	sym->offset = sym->sym.st_value;
4109a7827b7SPeter Zijlstra 	sym->len = sym->sym.st_size;
4119a7827b7SPeter Zijlstra 
4125da6aea3SPeter Zijlstra 	__sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {
4135da6aea3SPeter Zijlstra 		if (iter->offset == sym->offset && iter->type == sym->type)
4145da6aea3SPeter Zijlstra 			iter->alias = sym;
4155da6aea3SPeter Zijlstra 	}
4165da6aea3SPeter Zijlstra 
4175da6aea3SPeter Zijlstra 	__sym_insert(sym, &sym->sec->symbol_tree);
4189a7827b7SPeter Zijlstra 	pnode = rb_prev(&sym->node);
4199a7827b7SPeter Zijlstra 	if (pnode)
4209a7827b7SPeter Zijlstra 		entry = &rb_entry(pnode, struct symbol, node)->list;
4219a7827b7SPeter Zijlstra 	else
4229a7827b7SPeter Zijlstra 		entry = &sym->sec->symbol_list;
4239a7827b7SPeter Zijlstra 	list_add(&sym->list, entry);
42425cf0d8aSPeter Zijlstra 	elf_hash_add(symbol, &sym->hash, sym->idx);
42525cf0d8aSPeter Zijlstra 	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name));
4269a7827b7SPeter Zijlstra 
4279a7827b7SPeter Zijlstra 	/*
4289a7827b7SPeter Zijlstra 	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
4299a7827b7SPeter Zijlstra 	 * can exist within a function, confusing the sorting.
4309a7827b7SPeter Zijlstra 	 */
4319a7827b7SPeter Zijlstra 	if (!sym->len)
4325da6aea3SPeter Zijlstra 		__sym_remove(sym, &sym->sec->symbol_tree);
4339a7827b7SPeter Zijlstra }
4349a7827b7SPeter Zijlstra 
read_symbols(struct elf * elf)435442f04c3SJosh Poimboeuf static int read_symbols(struct elf *elf)
436442f04c3SJosh Poimboeuf {
43728fe1d7bSSami Tolvanen 	struct section *symtab, *symtab_shndx, *sec;
4382a362eccSPeter Zijlstra 	struct symbol *sym, *pfunc;
439442f04c3SJosh Poimboeuf 	int symbols_nr, i;
44013810435SJosh Poimboeuf 	char *coldstr;
44128fe1d7bSSami Tolvanen 	Elf_Data *shndx_data = NULL;
44228fe1d7bSSami Tolvanen 	Elf32_Word shndx;
443442f04c3SJosh Poimboeuf 
444442f04c3SJosh Poimboeuf 	symtab = find_section_by_name(elf, ".symtab");
44525cf0d8aSPeter Zijlstra 	if (symtab) {
44628fe1d7bSSami Tolvanen 		symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
44728fe1d7bSSami Tolvanen 		if (symtab_shndx)
44828fe1d7bSSami Tolvanen 			shndx_data = symtab_shndx->data;
44928fe1d7bSSami Tolvanen 
450ebcef730SJosh Poimboeuf 		symbols_nr = sec_num_entries(symtab);
45125cf0d8aSPeter Zijlstra 	} else {
45225cf0d8aSPeter Zijlstra 		/*
45325cf0d8aSPeter Zijlstra 		 * A missing symbol table is actually possible if it's an empty
45425cf0d8aSPeter Zijlstra 		 * .o file. This can happen for thunk_64.o. Make sure to at
45525cf0d8aSPeter Zijlstra 		 * least allocate the symbol hash tables so we can do symbol
45625cf0d8aSPeter Zijlstra 		 * lookups without crashing.
45725cf0d8aSPeter Zijlstra 		 */
45825cf0d8aSPeter Zijlstra 		symbols_nr = 0;
45925cf0d8aSPeter Zijlstra 	}
46025cf0d8aSPeter Zijlstra 
46125cf0d8aSPeter Zijlstra 	if (!elf_alloc_hash(symbol, symbols_nr) ||
46225cf0d8aSPeter Zijlstra 	    !elf_alloc_hash(symbol_name, symbols_nr))
46325cf0d8aSPeter Zijlstra 		return -1;
464442f04c3SJosh Poimboeuf 
4658045b8f0SThomas Weißschuh 	elf->symbol_data = calloc(symbols_nr, sizeof(*sym));
4668045b8f0SThomas Weißschuh 	if (!elf->symbol_data) {
4678045b8f0SThomas Weißschuh 		perror("calloc");
468442f04c3SJosh Poimboeuf 		return -1;
469442f04c3SJosh Poimboeuf 	}
4708045b8f0SThomas Weißschuh 	for (i = 0; i < symbols_nr; i++) {
4718045b8f0SThomas Weißschuh 		sym = &elf->symbol_data[i];
472442f04c3SJosh Poimboeuf 
473442f04c3SJosh Poimboeuf 		sym->idx = i;
474442f04c3SJosh Poimboeuf 
47528fe1d7bSSami Tolvanen 		if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
47628fe1d7bSSami Tolvanen 				      &shndx)) {
47728fe1d7bSSami Tolvanen 			WARN_ELF("gelf_getsymshndx");
478442f04c3SJosh Poimboeuf 			goto err;
479442f04c3SJosh Poimboeuf 		}
480442f04c3SJosh Poimboeuf 
481442f04c3SJosh Poimboeuf 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
482442f04c3SJosh Poimboeuf 				       sym->sym.st_name);
483442f04c3SJosh Poimboeuf 		if (!sym->name) {
484baa41469SJosh Poimboeuf 			WARN_ELF("elf_strptr");
485442f04c3SJosh Poimboeuf 			goto err;
486442f04c3SJosh Poimboeuf 		}
487442f04c3SJosh Poimboeuf 
48828fe1d7bSSami Tolvanen 		if ((sym->sym.st_shndx > SHN_UNDEF &&
48928fe1d7bSSami Tolvanen 		     sym->sym.st_shndx < SHN_LORESERVE) ||
49028fe1d7bSSami Tolvanen 		    (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
49128fe1d7bSSami Tolvanen 			if (sym->sym.st_shndx != SHN_XINDEX)
49228fe1d7bSSami Tolvanen 				shndx = sym->sym.st_shndx;
49328fe1d7bSSami Tolvanen 
49428fe1d7bSSami Tolvanen 			sym->sec = find_section_by_index(elf, shndx);
495442f04c3SJosh Poimboeuf 			if (!sym->sec) {
496442f04c3SJosh Poimboeuf 				WARN("couldn't find section for symbol %s",
497442f04c3SJosh Poimboeuf 				     sym->name);
498442f04c3SJosh Poimboeuf 				goto err;
499442f04c3SJosh Poimboeuf 			}
5009a7827b7SPeter Zijlstra 			if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {
501442f04c3SJosh Poimboeuf 				sym->name = sym->sec->name;
502442f04c3SJosh Poimboeuf 				sym->sec->sym = sym;
503442f04c3SJosh Poimboeuf 			}
504442f04c3SJosh Poimboeuf 		} else
505442f04c3SJosh Poimboeuf 			sym->sec = find_section_by_index(elf, 0);
506442f04c3SJosh Poimboeuf 
5079a7827b7SPeter Zijlstra 		elf_add_symbol(elf, sym);
508442f04c3SJosh Poimboeuf 	}
509442f04c3SJosh Poimboeuf 
5102daf7fabSJosh Poimboeuf 	if (opts.stats) {
5111e11f3fdSPeter Zijlstra 		printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
51225cf0d8aSPeter Zijlstra 		printf("symbol_bits: %d\n", elf->symbol_bits);
51325cf0d8aSPeter Zijlstra 	}
5141e11f3fdSPeter Zijlstra 
51513810435SJosh Poimboeuf 	/* Create parent/child links for any cold subfunctions */
51613810435SJosh Poimboeuf 	list_for_each_entry(sec, &elf->sections, list) {
5179290e772SJosh Poimboeuf 		sec_for_each_sym(sec, sym) {
51822566c16SArtem Savkov 			char pname[MAX_NAME_LEN + 1];
51922566c16SArtem Savkov 			size_t pnamelen;
52013810435SJosh Poimboeuf 			if (sym->type != STT_FUNC)
52113810435SJosh Poimboeuf 				continue;
522e000acc1SKristen Carlson Accardi 
523e000acc1SKristen Carlson Accardi 			if (sym->pfunc == NULL)
524e000acc1SKristen Carlson Accardi 				sym->pfunc = sym;
525e000acc1SKristen Carlson Accardi 
526e000acc1SKristen Carlson Accardi 			if (sym->cfunc == NULL)
527e000acc1SKristen Carlson Accardi 				sym->cfunc = sym;
528e000acc1SKristen Carlson Accardi 
529bcb6fb5dSJosh Poimboeuf 			coldstr = strstr(sym->name, ".cold");
53008b393d0SJosh Poimboeuf 			if (!coldstr)
53108b393d0SJosh Poimboeuf 				continue;
53208b393d0SJosh Poimboeuf 
53322566c16SArtem Savkov 			pnamelen = coldstr - sym->name;
53422566c16SArtem Savkov 			if (pnamelen > MAX_NAME_LEN) {
53522566c16SArtem Savkov 				WARN("%s(): parent function name exceeds maximum length of %d characters",
53622566c16SArtem Savkov 				     sym->name, MAX_NAME_LEN);
53722566c16SArtem Savkov 				return -1;
53822566c16SArtem Savkov 			}
53922566c16SArtem Savkov 
54022566c16SArtem Savkov 			strncpy(pname, sym->name, pnamelen);
54122566c16SArtem Savkov 			pname[pnamelen] = '\0';
54222566c16SArtem Savkov 			pfunc = find_symbol_by_name(elf, pname);
54313810435SJosh Poimboeuf 
54413810435SJosh Poimboeuf 			if (!pfunc) {
54513810435SJosh Poimboeuf 				WARN("%s(): can't find parent function",
54613810435SJosh Poimboeuf 				     sym->name);
5470b9301fbSArtem Savkov 				return -1;
54813810435SJosh Poimboeuf 			}
54913810435SJosh Poimboeuf 
55013810435SJosh Poimboeuf 			sym->pfunc = pfunc;
55113810435SJosh Poimboeuf 			pfunc->cfunc = sym;
55208b393d0SJosh Poimboeuf 
55308b393d0SJosh Poimboeuf 			/*
55408b393d0SJosh Poimboeuf 			 * Unfortunately, -fnoreorder-functions puts the child
55508b393d0SJosh Poimboeuf 			 * inside the parent.  Remove the overlap so we can
55608b393d0SJosh Poimboeuf 			 * have sane assumptions.
55708b393d0SJosh Poimboeuf 			 *
55808b393d0SJosh Poimboeuf 			 * Note that pfunc->len now no longer matches
55908b393d0SJosh Poimboeuf 			 * pfunc->sym.st_size.
56008b393d0SJosh Poimboeuf 			 */
56108b393d0SJosh Poimboeuf 			if (sym->sec == pfunc->sec &&
56208b393d0SJosh Poimboeuf 			    sym->offset >= pfunc->offset &&
56308b393d0SJosh Poimboeuf 			    sym->offset + sym->len == pfunc->offset + pfunc->len) {
56408b393d0SJosh Poimboeuf 				pfunc->len -= sym->len;
56513810435SJosh Poimboeuf 			}
56613810435SJosh Poimboeuf 		}
56713810435SJosh Poimboeuf 	}
56813810435SJosh Poimboeuf 
569442f04c3SJosh Poimboeuf 	return 0;
570442f04c3SJosh Poimboeuf 
571442f04c3SJosh Poimboeuf err:
572442f04c3SJosh Poimboeuf 	free(sym);
573442f04c3SJosh Poimboeuf 	return -1;
574442f04c3SJosh Poimboeuf }
575442f04c3SJosh Poimboeuf 
5764abff6d4SPeter Zijlstra /*
577fcf93355SJosh Poimboeuf  * @sym's idx has changed.  Update the relocs which reference it.
5784abff6d4SPeter Zijlstra  */
elf_update_sym_relocs(struct elf * elf,struct symbol * sym)579fcf93355SJosh Poimboeuf static int elf_update_sym_relocs(struct elf *elf, struct symbol *sym)
5804abff6d4SPeter Zijlstra {
5814abff6d4SPeter Zijlstra 	struct reloc *reloc;
5824abff6d4SPeter Zijlstra 
583ec24b927SJosh Poimboeuf 	for (reloc = sym->relocs; reloc; reloc = reloc->sym_next_reloc)
584ec24b927SJosh Poimboeuf 		set_reloc_sym(elf, reloc, reloc->sym->idx);
585fcf93355SJosh Poimboeuf 
586fcf93355SJosh Poimboeuf 	return 0;
5874abff6d4SPeter Zijlstra }
5884abff6d4SPeter Zijlstra 
5894abff6d4SPeter Zijlstra /*
590ead165faSPeter Zijlstra  * The libelf API is terrible; gelf_update_sym*() takes a data block relative
591ead165faSPeter Zijlstra  * index value, *NOT* the symbol index. As such, iterate the data blocks and
592ead165faSPeter Zijlstra  * adjust index until it fits.
593ead165faSPeter Zijlstra  *
594ead165faSPeter Zijlstra  * If no data block is found, allow adding a new data block provided the index
595ead165faSPeter Zijlstra  * is only one past the end.
5964abff6d4SPeter Zijlstra  */
elf_update_symbol(struct elf * elf,struct section * symtab,struct section * symtab_shndx,struct symbol * sym)597ead165faSPeter Zijlstra static int elf_update_symbol(struct elf *elf, struct section *symtab,
598ead165faSPeter Zijlstra 			     struct section *symtab_shndx, struct symbol *sym)
5994abff6d4SPeter Zijlstra {
600ead165faSPeter Zijlstra 	Elf32_Word shndx = sym->sec ? sym->sec->idx : SHN_UNDEF;
601ead165faSPeter Zijlstra 	Elf_Data *symtab_data = NULL, *shndx_data = NULL;
602ead165faSPeter Zijlstra 	Elf64_Xword entsize = symtab->sh.sh_entsize;
603ead165faSPeter Zijlstra 	int max_idx, idx = sym->idx;
604ead165faSPeter Zijlstra 	Elf_Scn *s, *t = NULL;
6055141d3a0SSami Tolvanen 	bool is_special_shndx = sym->sym.st_shndx >= SHN_LORESERVE &&
6065141d3a0SSami Tolvanen 				sym->sym.st_shndx != SHN_XINDEX;
6075141d3a0SSami Tolvanen 
6085141d3a0SSami Tolvanen 	if (is_special_shndx)
6095141d3a0SSami Tolvanen 		shndx = sym->sym.st_shndx;
6104abff6d4SPeter Zijlstra 
6114abff6d4SPeter Zijlstra 	s = elf_getscn(elf->elf, symtab->idx);
6124abff6d4SPeter Zijlstra 	if (!s) {
6134abff6d4SPeter Zijlstra 		WARN_ELF("elf_getscn");
6144abff6d4SPeter Zijlstra 		return -1;
6154abff6d4SPeter Zijlstra 	}
6164abff6d4SPeter Zijlstra 
6174abff6d4SPeter Zijlstra 	if (symtab_shndx) {
618ead165faSPeter Zijlstra 		t = elf_getscn(elf->elf, symtab_shndx->idx);
619ead165faSPeter Zijlstra 		if (!t) {
6204abff6d4SPeter Zijlstra 			WARN_ELF("elf_getscn");
6214abff6d4SPeter Zijlstra 			return -1;
6224abff6d4SPeter Zijlstra 		}
623ead165faSPeter Zijlstra 	}
6244abff6d4SPeter Zijlstra 
625ead165faSPeter Zijlstra 	for (;;) {
626ead165faSPeter Zijlstra 		/* get next data descriptor for the relevant sections */
627ead165faSPeter Zijlstra 		symtab_data = elf_getdata(s, symtab_data);
628ead165faSPeter Zijlstra 		if (t)
629ead165faSPeter Zijlstra 			shndx_data = elf_getdata(t, shndx_data);
630ead165faSPeter Zijlstra 
631ead165faSPeter Zijlstra 		/* end-of-list */
632ead165faSPeter Zijlstra 		if (!symtab_data) {
63313f60e80SPeter Zijlstra 			/*
63413f60e80SPeter Zijlstra 			 * Over-allocate to avoid O(n^2) symbol creation
63513f60e80SPeter Zijlstra 			 * behaviour.  The down side is that libelf doesn't
63613f60e80SPeter Zijlstra 			 * like this; see elf_truncate_section() for the fixup.
63713f60e80SPeter Zijlstra 			 */
63813f60e80SPeter Zijlstra 			int num = max(1U, sym->idx/3);
639ead165faSPeter Zijlstra 			void *buf;
640ead165faSPeter Zijlstra 
641ead165faSPeter Zijlstra 			if (idx) {
642ead165faSPeter Zijlstra 				/* we don't do holes in symbol tables */
643ead165faSPeter Zijlstra 				WARN("index out of range");
6444abff6d4SPeter Zijlstra 				return -1;
6454abff6d4SPeter Zijlstra 			}
6464abff6d4SPeter Zijlstra 
647ead165faSPeter Zijlstra 			/* if @idx == 0, it's the next contiguous entry, create it */
648ead165faSPeter Zijlstra 			symtab_data = elf_newdata(s);
649ead165faSPeter Zijlstra 			if (t)
650ead165faSPeter Zijlstra 				shndx_data = elf_newdata(t);
651ead165faSPeter Zijlstra 
65213f60e80SPeter Zijlstra 			buf = calloc(num, entsize);
653ead165faSPeter Zijlstra 			if (!buf) {
654ead165faSPeter Zijlstra 				WARN("malloc");
655ead165faSPeter Zijlstra 				return -1;
656ead165faSPeter Zijlstra 			}
657ead165faSPeter Zijlstra 
658ead165faSPeter Zijlstra 			symtab_data->d_buf = buf;
65913f60e80SPeter Zijlstra 			symtab_data->d_size = num * entsize;
660ead165faSPeter Zijlstra 			symtab_data->d_align = 1;
661ead165faSPeter Zijlstra 			symtab_data->d_type = ELF_T_SYM;
662ead165faSPeter Zijlstra 
663ff408273SJosh Poimboeuf 			mark_sec_changed(elf, symtab, true);
66413f60e80SPeter Zijlstra 			symtab->truncate = true;
665ead165faSPeter Zijlstra 
666ead165faSPeter Zijlstra 			if (t) {
66713f60e80SPeter Zijlstra 				buf = calloc(num, sizeof(Elf32_Word));
66813f60e80SPeter Zijlstra 				if (!buf) {
66913f60e80SPeter Zijlstra 					WARN("malloc");
67013f60e80SPeter Zijlstra 					return -1;
67113f60e80SPeter Zijlstra 				}
67213f60e80SPeter Zijlstra 
67313f60e80SPeter Zijlstra 				shndx_data->d_buf = buf;
67413f60e80SPeter Zijlstra 				shndx_data->d_size = num * sizeof(Elf32_Word);
675ead165faSPeter Zijlstra 				shndx_data->d_align = sizeof(Elf32_Word);
6764abff6d4SPeter Zijlstra 				shndx_data->d_type = ELF_T_WORD;
6774abff6d4SPeter Zijlstra 
678ff408273SJosh Poimboeuf 				mark_sec_changed(elf, symtab_shndx, true);
67913f60e80SPeter Zijlstra 				symtab_shndx->truncate = true;
6804abff6d4SPeter Zijlstra 			}
6814abff6d4SPeter Zijlstra 
682ead165faSPeter Zijlstra 			break;
683ead165faSPeter Zijlstra 		}
684ead165faSPeter Zijlstra 
685ead165faSPeter Zijlstra 		/* empty blocks should not happen */
686ead165faSPeter Zijlstra 		if (!symtab_data->d_size) {
687ead165faSPeter Zijlstra 			WARN("zero size data");
688ead165faSPeter Zijlstra 			return -1;
689ead165faSPeter Zijlstra 		}
690ead165faSPeter Zijlstra 
691ead165faSPeter Zijlstra 		/* is this the right block? */
692ead165faSPeter Zijlstra 		max_idx = symtab_data->d_size / entsize;
693ead165faSPeter Zijlstra 		if (idx < max_idx)
694ead165faSPeter Zijlstra 			break;
695ead165faSPeter Zijlstra 
696ead165faSPeter Zijlstra 		/* adjust index and try again */
697ead165faSPeter Zijlstra 		idx -= max_idx;
698ead165faSPeter Zijlstra 	}
699ead165faSPeter Zijlstra 
700ead165faSPeter Zijlstra 	/* something went side-ways */
701ead165faSPeter Zijlstra 	if (idx < 0) {
702ead165faSPeter Zijlstra 		WARN("negative index");
703ead165faSPeter Zijlstra 		return -1;
704ead165faSPeter Zijlstra 	}
705ead165faSPeter Zijlstra 
706ead165faSPeter Zijlstra 	/* setup extended section index magic and write the symbol */
7075141d3a0SSami Tolvanen 	if ((shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) || is_special_shndx) {
708ead165faSPeter Zijlstra 		sym->sym.st_shndx = shndx;
709ead165faSPeter Zijlstra 		if (!shndx_data)
710ead165faSPeter Zijlstra 			shndx = 0;
711ead165faSPeter Zijlstra 	} else {
712ead165faSPeter Zijlstra 		sym->sym.st_shndx = SHN_XINDEX;
713ead165faSPeter Zijlstra 		if (!shndx_data) {
714ead165faSPeter Zijlstra 			WARN("no .symtab_shndx");
715ead165faSPeter Zijlstra 			return -1;
716ead165faSPeter Zijlstra 		}
717ead165faSPeter Zijlstra 	}
718ead165faSPeter Zijlstra 
719ead165faSPeter Zijlstra 	if (!gelf_update_symshndx(symtab_data, shndx_data, idx, &sym->sym, shndx)) {
720ead165faSPeter Zijlstra 		WARN_ELF("gelf_update_symshndx");
721ead165faSPeter Zijlstra 		return -1;
722ead165faSPeter Zijlstra 	}
723ead165faSPeter Zijlstra 
724ead165faSPeter Zijlstra 	return 0;
7254abff6d4SPeter Zijlstra }
7264abff6d4SPeter Zijlstra 
7274abff6d4SPeter Zijlstra static struct symbol *
__elf_create_symbol(struct elf * elf,struct symbol * sym)7284c91be8eSPeter Zijlstra __elf_create_symbol(struct elf *elf, struct symbol *sym)
7294abff6d4SPeter Zijlstra {
7304abff6d4SPeter Zijlstra 	struct section *symtab, *symtab_shndx;
731ead165faSPeter Zijlstra 	Elf32_Word first_non_local, new_idx;
7324c91be8eSPeter Zijlstra 	struct symbol *old;
7334abff6d4SPeter Zijlstra 
7344abff6d4SPeter Zijlstra 	symtab = find_section_by_name(elf, ".symtab");
7354abff6d4SPeter Zijlstra 	if (symtab) {
7364abff6d4SPeter Zijlstra 		symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
7374abff6d4SPeter Zijlstra 	} else {
7384abff6d4SPeter Zijlstra 		WARN("no .symtab");
7394abff6d4SPeter Zijlstra 		return NULL;
7404abff6d4SPeter Zijlstra 	}
7414abff6d4SPeter Zijlstra 
742ebcef730SJosh Poimboeuf 	new_idx = sec_num_entries(symtab);
7434abff6d4SPeter Zijlstra 
7444c91be8eSPeter Zijlstra 	if (GELF_ST_BIND(sym->sym.st_info) != STB_LOCAL)
7454c91be8eSPeter Zijlstra 		goto non_local;
746ead165faSPeter Zijlstra 
747ead165faSPeter Zijlstra 	/*
748ead165faSPeter Zijlstra 	 * Move the first global symbol, as per sh_info, into a new, higher
749ead165faSPeter Zijlstra 	 * symbol index. This fees up a spot for a new local symbol.
750ead165faSPeter Zijlstra 	 */
751ead165faSPeter Zijlstra 	first_non_local = symtab->sh.sh_info;
752ead165faSPeter Zijlstra 	old = find_symbol_by_index(elf, first_non_local);
753ead165faSPeter Zijlstra 	if (old) {
754ead165faSPeter Zijlstra 
75502b54001SJosh Poimboeuf 		elf_hash_del(symbol, &old->hash, old->idx);
75602b54001SJosh Poimboeuf 		elf_hash_add(symbol, &old->hash, new_idx);
75702b54001SJosh Poimboeuf 		old->idx = new_idx;
758ead165faSPeter Zijlstra 
759ead165faSPeter Zijlstra 		if (elf_update_symbol(elf, symtab, symtab_shndx, old)) {
760ead165faSPeter Zijlstra 			WARN("elf_update_symbol move");
7614abff6d4SPeter Zijlstra 			return NULL;
7624abff6d4SPeter Zijlstra 		}
7634abff6d4SPeter Zijlstra 
764fcf93355SJosh Poimboeuf 		if (elf_update_sym_relocs(elf, old))
765fcf93355SJosh Poimboeuf 			return NULL;
766fcf93355SJosh Poimboeuf 
767ead165faSPeter Zijlstra 		new_idx = first_non_local;
768ead165faSPeter Zijlstra 	}
769ead165faSPeter Zijlstra 
7704c91be8eSPeter Zijlstra 	/*
7714c91be8eSPeter Zijlstra 	 * Either way, we will add a LOCAL symbol.
7724c91be8eSPeter Zijlstra 	 */
7734c91be8eSPeter Zijlstra 	symtab->sh.sh_info += 1;
7744c91be8eSPeter Zijlstra 
7754c91be8eSPeter Zijlstra non_local:
776ead165faSPeter Zijlstra 	sym->idx = new_idx;
777ead165faSPeter Zijlstra 	if (elf_update_symbol(elf, symtab, symtab_shndx, sym)) {
778ead165faSPeter Zijlstra 		WARN("elf_update_symbol");
7794abff6d4SPeter Zijlstra 		return NULL;
7804abff6d4SPeter Zijlstra 	}
7814abff6d4SPeter Zijlstra 
78213f60e80SPeter Zijlstra 	symtab->sh.sh_size += symtab->sh.sh_entsize;
783ff408273SJosh Poimboeuf 	mark_sec_changed(elf, symtab, true);
78413f60e80SPeter Zijlstra 
78513f60e80SPeter Zijlstra 	if (symtab_shndx) {
78613f60e80SPeter Zijlstra 		symtab_shndx->sh.sh_size += sizeof(Elf32_Word);
787ff408273SJosh Poimboeuf 		mark_sec_changed(elf, symtab_shndx, true);
78813f60e80SPeter Zijlstra 	}
78913f60e80SPeter Zijlstra 
7904c91be8eSPeter Zijlstra 	return sym;
7914c91be8eSPeter Zijlstra }
792ead165faSPeter Zijlstra 
7934c91be8eSPeter Zijlstra static struct symbol *
elf_create_section_symbol(struct elf * elf,struct section * sec)7944c91be8eSPeter Zijlstra elf_create_section_symbol(struct elf *elf, struct section *sec)
7954c91be8eSPeter Zijlstra {
7964c91be8eSPeter Zijlstra 	struct symbol *sym = calloc(1, sizeof(*sym));
7974c91be8eSPeter Zijlstra 
7984c91be8eSPeter Zijlstra 	if (!sym) {
7994c91be8eSPeter Zijlstra 		perror("malloc");
8004c91be8eSPeter Zijlstra 		return NULL;
8014c91be8eSPeter Zijlstra 	}
8024c91be8eSPeter Zijlstra 
8034c91be8eSPeter Zijlstra 	sym->name = sec->name;
8044c91be8eSPeter Zijlstra 	sym->sec = sec;
8054c91be8eSPeter Zijlstra 
8064c91be8eSPeter Zijlstra 	// st_name 0
8074c91be8eSPeter Zijlstra 	sym->sym.st_info = GELF_ST_INFO(STB_LOCAL, STT_SECTION);
8084c91be8eSPeter Zijlstra 	// st_other 0
8094c91be8eSPeter Zijlstra 	// st_value 0
8104c91be8eSPeter Zijlstra 	// st_size 0
8114c91be8eSPeter Zijlstra 
8124c91be8eSPeter Zijlstra 	sym = __elf_create_symbol(elf, sym);
8134c91be8eSPeter Zijlstra 	if (sym)
8144abff6d4SPeter Zijlstra 		elf_add_symbol(elf, sym);
8154abff6d4SPeter Zijlstra 
8164abff6d4SPeter Zijlstra 	return sym;
8174abff6d4SPeter Zijlstra }
8184abff6d4SPeter Zijlstra 
8199f2899feSPeter Zijlstra static int elf_add_string(struct elf *elf, struct section *strtab, char *str);
8209f2899feSPeter Zijlstra 
8219f2899feSPeter Zijlstra struct symbol *
elf_create_prefix_symbol(struct elf * elf,struct symbol * orig,long size)8229f2899feSPeter Zijlstra elf_create_prefix_symbol(struct elf *elf, struct symbol *orig, long size)
8239f2899feSPeter Zijlstra {
8249f2899feSPeter Zijlstra 	struct symbol *sym = calloc(1, sizeof(*sym));
8259f2899feSPeter Zijlstra 	size_t namelen = strlen(orig->name) + sizeof("__pfx_");
8269f2899feSPeter Zijlstra 	char *name = malloc(namelen);
8279f2899feSPeter Zijlstra 
8289f2899feSPeter Zijlstra 	if (!sym || !name) {
8299f2899feSPeter Zijlstra 		perror("malloc");
8309f2899feSPeter Zijlstra 		return NULL;
8319f2899feSPeter Zijlstra 	}
8329f2899feSPeter Zijlstra 
8339f2899feSPeter Zijlstra 	snprintf(name, namelen, "__pfx_%s", orig->name);
8349f2899feSPeter Zijlstra 
8359f2899feSPeter Zijlstra 	sym->name = name;
8369f2899feSPeter Zijlstra 	sym->sec = orig->sec;
8379f2899feSPeter Zijlstra 
8389f2899feSPeter Zijlstra 	sym->sym.st_name = elf_add_string(elf, NULL, name);
8399f2899feSPeter Zijlstra 	sym->sym.st_info = orig->sym.st_info;
8409f2899feSPeter Zijlstra 	sym->sym.st_value = orig->sym.st_value - size;
8419f2899feSPeter Zijlstra 	sym->sym.st_size = size;
8429f2899feSPeter Zijlstra 
8439f2899feSPeter Zijlstra 	sym = __elf_create_symbol(elf, sym);
8449f2899feSPeter Zijlstra 	if (sym)
8459f2899feSPeter Zijlstra 		elf_add_symbol(elf, sym);
8469f2899feSPeter Zijlstra 
8479f2899feSPeter Zijlstra 	return sym;
8489f2899feSPeter Zijlstra }
8499f2899feSPeter 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)8506342a20eSJosh Poimboeuf static struct reloc *elf_init_reloc(struct elf *elf, struct section *rsec,
8516342a20eSJosh Poimboeuf 				    unsigned int reloc_idx,
8526342a20eSJosh Poimboeuf 				    unsigned long offset, struct symbol *sym,
8536342a20eSJosh Poimboeuf 				    s64 addend, unsigned int type)
8546342a20eSJosh Poimboeuf {
855e0a9349bSJosh Poimboeuf 	struct reloc *reloc, empty = { 0 };
8566342a20eSJosh Poimboeuf 
857ebcef730SJosh Poimboeuf 	if (reloc_idx >= sec_num_entries(rsec)) {
858ebcef730SJosh Poimboeuf 		WARN("%s: bad reloc_idx %u for %s with %d relocs",
859ebcef730SJosh Poimboeuf 		     __func__, reloc_idx, rsec->name, sec_num_entries(rsec));
8606342a20eSJosh Poimboeuf 		return NULL;
8616342a20eSJosh Poimboeuf 	}
8626342a20eSJosh Poimboeuf 
863ebcef730SJosh Poimboeuf 	reloc = &rsec->relocs[reloc_idx];
864e0a9349bSJosh Poimboeuf 
865e0a9349bSJosh Poimboeuf 	if (memcmp(reloc, &empty, sizeof(empty))) {
866e0a9349bSJosh Poimboeuf 		WARN("%s: %s: reloc %d already initialized!",
867e0a9349bSJosh Poimboeuf 		     __func__, rsec->name, reloc_idx);
8686342a20eSJosh Poimboeuf 		return NULL;
8696342a20eSJosh Poimboeuf 	}
8706342a20eSJosh Poimboeuf 
8716342a20eSJosh Poimboeuf 	reloc->sec = rsec;
8726342a20eSJosh Poimboeuf 	reloc->sym = sym;
8736342a20eSJosh Poimboeuf 
874ec24b927SJosh Poimboeuf 	set_reloc_offset(elf, reloc, offset);
875ec24b927SJosh Poimboeuf 	set_reloc_sym(elf, reloc, sym->idx);
876ec24b927SJosh Poimboeuf 	set_reloc_type(elf, reloc, type);
877ec24b927SJosh Poimboeuf 	set_reloc_addend(elf, reloc, addend);
878fcf93355SJosh Poimboeuf 
8796342a20eSJosh Poimboeuf 	elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
880890f10a4SJosh Poimboeuf 	reloc->sym_next_reloc = sym->relocs;
881890f10a4SJosh Poimboeuf 	sym->relocs = reloc;
8826342a20eSJosh Poimboeuf 
8836342a20eSJosh Poimboeuf 	return reloc;
8846342a20eSJosh Poimboeuf }
8856342a20eSJosh 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)8866342a20eSJosh Poimboeuf struct reloc *elf_init_reloc_text_sym(struct elf *elf, struct section *sec,
8876342a20eSJosh Poimboeuf 				      unsigned long offset,
8886342a20eSJosh Poimboeuf 				      unsigned int reloc_idx,
8896342a20eSJosh Poimboeuf 				      struct section *insn_sec,
8906342a20eSJosh Poimboeuf 				      unsigned long insn_off)
891ef47cc01SPeter Zijlstra {
8924abff6d4SPeter Zijlstra 	struct symbol *sym = insn_sec->sym;
8934abff6d4SPeter Zijlstra 	int addend = insn_off;
894ef47cc01SPeter Zijlstra 
8956342a20eSJosh Poimboeuf 	if (!(insn_sec->sh.sh_flags & SHF_EXECINSTR)) {
8966342a20eSJosh Poimboeuf 		WARN("bad call to %s() for data symbol %s",
8976342a20eSJosh Poimboeuf 		     __func__, sym->name);
8986342a20eSJosh Poimboeuf 		return NULL;
8996342a20eSJosh Poimboeuf 	}
9006342a20eSJosh Poimboeuf 
901ef47cc01SPeter Zijlstra 	if (!sym) {
9024abff6d4SPeter Zijlstra 		/*
9034abff6d4SPeter Zijlstra 		 * Due to how weak functions work, we must use section based
9044abff6d4SPeter Zijlstra 		 * relocations. Symbol based relocations would result in the
9054abff6d4SPeter Zijlstra 		 * weak and non-weak function annotations being overlaid on the
9064abff6d4SPeter Zijlstra 		 * non-weak function after linking.
9074abff6d4SPeter Zijlstra 		 */
9084abff6d4SPeter Zijlstra 		sym = elf_create_section_symbol(elf, insn_sec);
9094abff6d4SPeter Zijlstra 		if (!sym)
9106342a20eSJosh Poimboeuf 			return NULL;
911ef47cc01SPeter Zijlstra 
9124abff6d4SPeter Zijlstra 		insn_sec->sym = sym;
913ef47cc01SPeter Zijlstra 	}
914ef47cc01SPeter Zijlstra 
9156342a20eSJosh Poimboeuf 	return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
9166342a20eSJosh Poimboeuf 			      elf_text_rela_type(elf));
9176342a20eSJosh Poimboeuf }
9186342a20eSJosh Poimboeuf 
elf_init_reloc_data_sym(struct elf * elf,struct section * sec,unsigned long offset,unsigned int reloc_idx,struct symbol * sym,s64 addend)9196342a20eSJosh Poimboeuf struct reloc *elf_init_reloc_data_sym(struct elf *elf, struct section *sec,
9206342a20eSJosh Poimboeuf 				      unsigned long offset,
9216342a20eSJosh Poimboeuf 				      unsigned int reloc_idx,
9226342a20eSJosh Poimboeuf 				      struct symbol *sym,
9236342a20eSJosh Poimboeuf 				      s64 addend)
9246342a20eSJosh Poimboeuf {
9256342a20eSJosh Poimboeuf 	if (sym->sec && (sec->sh.sh_flags & SHF_EXECINSTR)) {
9266342a20eSJosh Poimboeuf 		WARN("bad call to %s() for text symbol %s",
9276342a20eSJosh Poimboeuf 		     __func__, sym->name);
9286342a20eSJosh Poimboeuf 		return NULL;
9296342a20eSJosh Poimboeuf 	}
9306342a20eSJosh Poimboeuf 
9316342a20eSJosh Poimboeuf 	return elf_init_reloc(elf, sec->rsec, reloc_idx, offset, sym, addend,
9326342a20eSJosh Poimboeuf 			      elf_data_rela_type(elf));
93334f7c96dSPeter Zijlstra }
93434f7c96dSPeter Zijlstra 
read_relocs(struct elf * elf)935f1974222SMatt Helsley static int read_relocs(struct elf *elf)
936442f04c3SJosh Poimboeuf {
937eb0481bbSJosh Poimboeuf 	unsigned long nr_reloc, max_reloc = 0;
938a5bd6236SJosh Poimboeuf 	struct section *rsec;
939f1974222SMatt Helsley 	struct reloc *reloc;
940442f04c3SJosh Poimboeuf 	unsigned int symndx;
94119526717SPeter Zijlstra 	struct symbol *sym;
94219526717SPeter Zijlstra 	int i;
943442f04c3SJosh Poimboeuf 
944eb0481bbSJosh Poimboeuf 	if (!elf_alloc_hash(reloc, elf->num_relocs))
94525cf0d8aSPeter Zijlstra 		return -1;
94625cf0d8aSPeter Zijlstra 
947a5bd6236SJosh Poimboeuf 	list_for_each_entry(rsec, &elf->sections, list) {
948eb0481bbSJosh Poimboeuf 		if (!is_reloc_sec(rsec))
949442f04c3SJosh Poimboeuf 			continue;
950442f04c3SJosh Poimboeuf 
951a5bd6236SJosh Poimboeuf 		rsec->base = find_section_by_index(elf, rsec->sh.sh_info);
952a5bd6236SJosh Poimboeuf 		if (!rsec->base) {
953f1974222SMatt Helsley 			WARN("can't find base section for reloc section %s",
954a5bd6236SJosh Poimboeuf 			     rsec->name);
955442f04c3SJosh Poimboeuf 			return -1;
956442f04c3SJosh Poimboeuf 		}
957442f04c3SJosh Poimboeuf 
958a5bd6236SJosh Poimboeuf 		rsec->base->rsec = rsec;
959442f04c3SJosh Poimboeuf 
960f1974222SMatt Helsley 		nr_reloc = 0;
961ebcef730SJosh Poimboeuf 		rsec->relocs = calloc(sec_num_entries(rsec), sizeof(*reloc));
962ebcef730SJosh Poimboeuf 		if (!rsec->relocs) {
9638045b8f0SThomas Weißschuh 			perror("calloc");
964442f04c3SJosh Poimboeuf 			return -1;
965442f04c3SJosh Poimboeuf 		}
966ebcef730SJosh Poimboeuf 		for (i = 0; i < sec_num_entries(rsec); i++) {
967ebcef730SJosh Poimboeuf 			reloc = &rsec->relocs[i];
96853257a97SJosh Poimboeuf 
969a5bd6236SJosh Poimboeuf 			reloc->sec = rsec;
970ec24b927SJosh Poimboeuf 			symndx = reloc_sym(reloc);
97119526717SPeter Zijlstra 			reloc->sym = sym = find_symbol_by_index(elf, symndx);
972f1974222SMatt Helsley 			if (!reloc->sym) {
973f1974222SMatt Helsley 				WARN("can't find reloc entry symbol %d for %s",
974a5bd6236SJosh Poimboeuf 				     symndx, rsec->name);
975442f04c3SJosh Poimboeuf 				return -1;
976442f04c3SJosh Poimboeuf 			}
977042ba73fSJosh Poimboeuf 
97825cf0d8aSPeter Zijlstra 			elf_hash_add(reloc, &reloc->hash, reloc_hash(reloc));
979890f10a4SJosh Poimboeuf 			reloc->sym_next_reloc = sym->relocs;
980890f10a4SJosh Poimboeuf 			sym->relocs = reloc;
9813a647607SPeter Zijlstra 
982f1974222SMatt Helsley 			nr_reloc++;
983442f04c3SJosh Poimboeuf 		}
984f1974222SMatt Helsley 		max_reloc = max(max_reloc, nr_reloc);
9851e11f3fdSPeter Zijlstra 	}
9861e11f3fdSPeter Zijlstra 
9872daf7fabSJosh Poimboeuf 	if (opts.stats) {
988f1974222SMatt Helsley 		printf("max_reloc: %lu\n", max_reloc);
989eb0481bbSJosh Poimboeuf 		printf("num_relocs: %lu\n", elf->num_relocs);
99025cf0d8aSPeter Zijlstra 		printf("reloc_bits: %d\n", elf->reloc_bits);
991442f04c3SJosh Poimboeuf 	}
992442f04c3SJosh Poimboeuf 
993442f04c3SJosh Poimboeuf 	return 0;
994442f04c3SJosh Poimboeuf }
995442f04c3SJosh Poimboeuf 
elf_open_read(const char * name,int flags)996bc359ff2SIngo Molnar struct elf *elf_open_read(const char *name, int flags)
997442f04c3SJosh Poimboeuf {
998442f04c3SJosh Poimboeuf 	struct elf *elf;
999627fce14SJosh Poimboeuf 	Elf_Cmd cmd;
1000442f04c3SJosh Poimboeuf 
1001442f04c3SJosh Poimboeuf 	elf_version(EV_CURRENT);
1002442f04c3SJosh Poimboeuf 
1003442f04c3SJosh Poimboeuf 	elf = malloc(sizeof(*elf));
1004442f04c3SJosh Poimboeuf 	if (!elf) {
1005442f04c3SJosh Poimboeuf 		perror("malloc");
1006442f04c3SJosh Poimboeuf 		return NULL;
1007442f04c3SJosh Poimboeuf 	}
1008*9f71fbcdSMichal Kubecek 	memset(elf, 0, sizeof(*elf));
1009442f04c3SJosh Poimboeuf 
1010442f04c3SJosh Poimboeuf 	INIT_LIST_HEAD(&elf->sections);
1011442f04c3SJosh Poimboeuf 
1012627fce14SJosh Poimboeuf 	elf->fd = open(name, flags);
1013442f04c3SJosh Poimboeuf 	if (elf->fd == -1) {
1014385d11b1SJosh Poimboeuf 		fprintf(stderr, "objtool: Can't open '%s': %s\n",
1015385d11b1SJosh Poimboeuf 			name, strerror(errno));
1016442f04c3SJosh Poimboeuf 		goto err;
1017442f04c3SJosh Poimboeuf 	}
1018442f04c3SJosh Poimboeuf 
1019627fce14SJosh Poimboeuf 	if ((flags & O_ACCMODE) == O_RDONLY)
1020627fce14SJosh Poimboeuf 		cmd = ELF_C_READ_MMAP;
1021627fce14SJosh Poimboeuf 	else if ((flags & O_ACCMODE) == O_RDWR)
1022627fce14SJosh Poimboeuf 		cmd = ELF_C_RDWR;
1023627fce14SJosh Poimboeuf 	else /* O_WRONLY */
1024627fce14SJosh Poimboeuf 		cmd = ELF_C_WRITE;
1025627fce14SJosh Poimboeuf 
1026627fce14SJosh Poimboeuf 	elf->elf = elf_begin(elf->fd, cmd, NULL);
1027442f04c3SJosh Poimboeuf 	if (!elf->elf) {
1028baa41469SJosh Poimboeuf 		WARN_ELF("elf_begin");
1029442f04c3SJosh Poimboeuf 		goto err;
1030442f04c3SJosh Poimboeuf 	}
1031442f04c3SJosh Poimboeuf 
1032442f04c3SJosh Poimboeuf 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
1033baa41469SJosh Poimboeuf 		WARN_ELF("gelf_getehdr");
1034442f04c3SJosh Poimboeuf 		goto err;
1035442f04c3SJosh Poimboeuf 	}
1036442f04c3SJosh Poimboeuf 
1037442f04c3SJosh Poimboeuf 	if (read_sections(elf))
1038442f04c3SJosh Poimboeuf 		goto err;
1039442f04c3SJosh Poimboeuf 
1040442f04c3SJosh Poimboeuf 	if (read_symbols(elf))
1041442f04c3SJosh Poimboeuf 		goto err;
1042442f04c3SJosh Poimboeuf 
1043f1974222SMatt Helsley 	if (read_relocs(elf))
1044442f04c3SJosh Poimboeuf 		goto err;
1045442f04c3SJosh Poimboeuf 
1046442f04c3SJosh Poimboeuf 	return elf;
1047442f04c3SJosh Poimboeuf 
1048442f04c3SJosh Poimboeuf err:
1049442f04c3SJosh Poimboeuf 	elf_close(elf);
1050442f04c3SJosh Poimboeuf 	return NULL;
1051442f04c3SJosh Poimboeuf }
1052442f04c3SJosh Poimboeuf 
elf_add_string(struct elf * elf,struct section * strtab,char * str)1053417a4dc9SPeter Zijlstra static int elf_add_string(struct elf *elf, struct section *strtab, char *str)
1054417a4dc9SPeter Zijlstra {
1055417a4dc9SPeter Zijlstra 	Elf_Data *data;
1056417a4dc9SPeter Zijlstra 	Elf_Scn *s;
1057417a4dc9SPeter Zijlstra 	int len;
1058417a4dc9SPeter Zijlstra 
1059417a4dc9SPeter Zijlstra 	if (!strtab)
1060417a4dc9SPeter Zijlstra 		strtab = find_section_by_name(elf, ".strtab");
1061417a4dc9SPeter Zijlstra 	if (!strtab) {
1062417a4dc9SPeter Zijlstra 		WARN("can't find .strtab section");
1063417a4dc9SPeter Zijlstra 		return -1;
1064417a4dc9SPeter Zijlstra 	}
1065417a4dc9SPeter Zijlstra 
1066417a4dc9SPeter Zijlstra 	s = elf_getscn(elf->elf, strtab->idx);
1067417a4dc9SPeter Zijlstra 	if (!s) {
1068417a4dc9SPeter Zijlstra 		WARN_ELF("elf_getscn");
1069417a4dc9SPeter Zijlstra 		return -1;
1070417a4dc9SPeter Zijlstra 	}
1071417a4dc9SPeter Zijlstra 
1072417a4dc9SPeter Zijlstra 	data = elf_newdata(s);
1073417a4dc9SPeter Zijlstra 	if (!data) {
1074417a4dc9SPeter Zijlstra 		WARN_ELF("elf_newdata");
1075417a4dc9SPeter Zijlstra 		return -1;
1076417a4dc9SPeter Zijlstra 	}
1077417a4dc9SPeter Zijlstra 
1078417a4dc9SPeter Zijlstra 	data->d_buf = str;
1079417a4dc9SPeter Zijlstra 	data->d_size = strlen(str) + 1;
1080417a4dc9SPeter Zijlstra 	data->d_align = 1;
1081417a4dc9SPeter Zijlstra 
1082fe255fe6SJoe Lawrence 	len = strtab->sh.sh_size;
1083fe255fe6SJoe Lawrence 	strtab->sh.sh_size += data->d_size;
1084ff408273SJosh Poimboeuf 
1085ff408273SJosh Poimboeuf 	mark_sec_changed(elf, strtab, true);
1086417a4dc9SPeter Zijlstra 
1087417a4dc9SPeter Zijlstra 	return len;
1088417a4dc9SPeter Zijlstra }
1089417a4dc9SPeter Zijlstra 
elf_create_section(struct elf * elf,const char * name,size_t entsize,unsigned int nr)1090627fce14SJosh Poimboeuf struct section *elf_create_section(struct elf *elf, const char *name,
10916342a20eSJosh Poimboeuf 				   size_t entsize, unsigned int nr)
1092627fce14SJosh Poimboeuf {
1093627fce14SJosh Poimboeuf 	struct section *sec, *shstrtab;
1094627fce14SJosh Poimboeuf 	size_t size = entsize * nr;
10953c3ea503SMichael Forney 	Elf_Scn *s;
1096627fce14SJosh Poimboeuf 
1097627fce14SJosh Poimboeuf 	sec = malloc(sizeof(*sec));
1098627fce14SJosh Poimboeuf 	if (!sec) {
1099627fce14SJosh Poimboeuf 		perror("malloc");
1100627fce14SJosh Poimboeuf 		return NULL;
1101627fce14SJosh Poimboeuf 	}
1102627fce14SJosh Poimboeuf 	memset(sec, 0, sizeof(*sec));
1103627fce14SJosh Poimboeuf 
1104627fce14SJosh Poimboeuf 	INIT_LIST_HEAD(&sec->symbol_list);
1105627fce14SJosh Poimboeuf 
1106627fce14SJosh Poimboeuf 	s = elf_newscn(elf->elf);
1107627fce14SJosh Poimboeuf 	if (!s) {
1108627fce14SJosh Poimboeuf 		WARN_ELF("elf_newscn");
1109627fce14SJosh Poimboeuf 		return NULL;
1110627fce14SJosh Poimboeuf 	}
1111627fce14SJosh Poimboeuf 
1112627fce14SJosh Poimboeuf 	sec->name = strdup(name);
1113627fce14SJosh Poimboeuf 	if (!sec->name) {
1114627fce14SJosh Poimboeuf 		perror("strdup");
1115627fce14SJosh Poimboeuf 		return NULL;
1116627fce14SJosh Poimboeuf 	}
1117627fce14SJosh Poimboeuf 
1118627fce14SJosh Poimboeuf 	sec->idx = elf_ndxscn(s);
1119627fce14SJosh Poimboeuf 
1120627fce14SJosh Poimboeuf 	sec->data = elf_newdata(s);
1121627fce14SJosh Poimboeuf 	if (!sec->data) {
1122627fce14SJosh Poimboeuf 		WARN_ELF("elf_newdata");
1123627fce14SJosh Poimboeuf 		return NULL;
1124627fce14SJosh Poimboeuf 	}
1125627fce14SJosh Poimboeuf 
1126627fce14SJosh Poimboeuf 	sec->data->d_size = size;
1127627fce14SJosh Poimboeuf 	sec->data->d_align = 1;
1128627fce14SJosh Poimboeuf 
1129627fce14SJosh Poimboeuf 	if (size) {
1130627fce14SJosh Poimboeuf 		sec->data->d_buf = malloc(size);
1131627fce14SJosh Poimboeuf 		if (!sec->data->d_buf) {
1132627fce14SJosh Poimboeuf 			perror("malloc");
1133627fce14SJosh Poimboeuf 			return NULL;
1134627fce14SJosh Poimboeuf 		}
1135627fce14SJosh Poimboeuf 		memset(sec->data->d_buf, 0, size);
1136627fce14SJosh Poimboeuf 	}
1137627fce14SJosh Poimboeuf 
1138627fce14SJosh Poimboeuf 	if (!gelf_getshdr(s, &sec->sh)) {
1139627fce14SJosh Poimboeuf 		WARN_ELF("gelf_getshdr");
1140627fce14SJosh Poimboeuf 		return NULL;
1141627fce14SJosh Poimboeuf 	}
1142627fce14SJosh Poimboeuf 
1143627fce14SJosh Poimboeuf 	sec->sh.sh_size = size;
1144627fce14SJosh Poimboeuf 	sec->sh.sh_entsize = entsize;
1145627fce14SJosh Poimboeuf 	sec->sh.sh_type = SHT_PROGBITS;
1146627fce14SJosh Poimboeuf 	sec->sh.sh_addralign = 1;
11472707579dSJosh Poimboeuf 	sec->sh.sh_flags = SHF_ALLOC;
1148627fce14SJosh Poimboeuf 
11496d77d3b4SSimon Ser 	/* Add section name to .shstrtab (or .strtab for Clang) */
1150627fce14SJosh Poimboeuf 	shstrtab = find_section_by_name(elf, ".shstrtab");
11516d77d3b4SSimon Ser 	if (!shstrtab)
11526d77d3b4SSimon Ser 		shstrtab = find_section_by_name(elf, ".strtab");
1153627fce14SJosh Poimboeuf 	if (!shstrtab) {
11546d77d3b4SSimon Ser 		WARN("can't find .shstrtab or .strtab section");
1155627fce14SJosh Poimboeuf 		return NULL;
1156627fce14SJosh Poimboeuf 	}
1157417a4dc9SPeter Zijlstra 	sec->sh.sh_name = elf_add_string(elf, shstrtab, sec->name);
1158417a4dc9SPeter Zijlstra 	if (sec->sh.sh_name == -1)
1159627fce14SJosh Poimboeuf 		return NULL;
1160627fce14SJosh Poimboeuf 
116153038996SPeter Zijlstra 	list_add_tail(&sec->list, &elf->sections);
116225cf0d8aSPeter Zijlstra 	elf_hash_add(section, &sec->hash, sec->idx);
116325cf0d8aSPeter Zijlstra 	elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name));
116453038996SPeter Zijlstra 
1165ff408273SJosh Poimboeuf 	mark_sec_changed(elf, sec, true);
11662b10be23SPeter Zijlstra 
1167627fce14SJosh Poimboeuf 	return sec;
1168627fce14SJosh Poimboeuf }
1169627fce14SJosh Poimboeuf 
elf_create_rela_section(struct elf * elf,struct section * sec,unsigned int reloc_nr)117053257a97SJosh Poimboeuf static struct section *elf_create_rela_section(struct elf *elf,
11716342a20eSJosh Poimboeuf 					       struct section *sec,
11726342a20eSJosh Poimboeuf 					       unsigned int reloc_nr)
1173fb414783SMatt Helsley {
1174a5bd6236SJosh Poimboeuf 	struct section *rsec;
117553257a97SJosh Poimboeuf 	char *rsec_name;
1176fb414783SMatt Helsley 
117753257a97SJosh Poimboeuf 	rsec_name = malloc(strlen(sec->name) + strlen(".rela") + 1);
117853257a97SJosh Poimboeuf 	if (!rsec_name) {
1179fb414783SMatt Helsley 		perror("malloc");
1180fb414783SMatt Helsley 		return NULL;
1181fb414783SMatt Helsley 	}
118253257a97SJosh Poimboeuf 	strcpy(rsec_name, ".rela");
118353257a97SJosh Poimboeuf 	strcat(rsec_name, sec->name);
1184fb414783SMatt Helsley 
11856342a20eSJosh Poimboeuf 	rsec = elf_create_section(elf, rsec_name, elf_rela_size(elf), reloc_nr);
118653257a97SJosh Poimboeuf 	free(rsec_name);
1187a5bd6236SJosh Poimboeuf 	if (!rsec)
1188fb414783SMatt Helsley 		return NULL;
1189fb414783SMatt Helsley 
11906342a20eSJosh Poimboeuf 	rsec->data->d_type = ELF_T_RELA;
119153257a97SJosh Poimboeuf 	rsec->sh.sh_type = SHT_RELA;
119253257a97SJosh Poimboeuf 	rsec->sh.sh_addralign = elf_addr_size(elf);
1193a5bd6236SJosh Poimboeuf 	rsec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
1194a5bd6236SJosh Poimboeuf 	rsec->sh.sh_info = sec->idx;
1195a5bd6236SJosh Poimboeuf 	rsec->sh.sh_flags = SHF_INFO_LINK;
1196fb414783SMatt Helsley 
1197ebcef730SJosh Poimboeuf 	rsec->relocs = calloc(sec_num_entries(rsec), sizeof(struct reloc));
1198ebcef730SJosh Poimboeuf 	if (!rsec->relocs) {
1199e0a9349bSJosh Poimboeuf 		perror("calloc");
1200e0a9349bSJosh Poimboeuf 		return NULL;
1201e0a9349bSJosh Poimboeuf 	}
1202e0a9349bSJosh Poimboeuf 
12036342a20eSJosh Poimboeuf 	sec->rsec = rsec;
12046342a20eSJosh Poimboeuf 	rsec->base = sec;
12056342a20eSJosh Poimboeuf 
1206a5bd6236SJosh Poimboeuf 	return rsec;
1207fb414783SMatt Helsley }
1208fb414783SMatt Helsley 
elf_create_section_pair(struct elf * elf,const char * name,size_t entsize,unsigned int nr,unsigned int reloc_nr)12096342a20eSJosh Poimboeuf struct section *elf_create_section_pair(struct elf *elf, const char *name,
12106342a20eSJosh Poimboeuf 					size_t entsize, unsigned int nr,
12116342a20eSJosh Poimboeuf 					unsigned int reloc_nr)
12126342a20eSJosh Poimboeuf {
12136342a20eSJosh Poimboeuf 	struct section *sec;
12146342a20eSJosh Poimboeuf 
12156342a20eSJosh Poimboeuf 	sec = elf_create_section(elf, name, entsize, nr);
12166342a20eSJosh Poimboeuf 	if (!sec)
12176342a20eSJosh Poimboeuf 		return NULL;
12186342a20eSJosh Poimboeuf 
12196342a20eSJosh Poimboeuf 	if (!elf_create_rela_section(elf, sec, reloc_nr))
12206342a20eSJosh Poimboeuf 		return NULL;
12216342a20eSJosh Poimboeuf 
12226342a20eSJosh Poimboeuf 	return sec;
12236342a20eSJosh Poimboeuf }
12246342a20eSJosh Poimboeuf 
elf_write_insn(struct elf * elf,struct section * sec,unsigned long offset,unsigned int len,const char * insn)1225fdabdd0bSPeter Zijlstra int elf_write_insn(struct elf *elf, struct section *sec,
1226fdabdd0bSPeter Zijlstra 		   unsigned long offset, unsigned int len,
1227fdabdd0bSPeter Zijlstra 		   const char *insn)
1228fdabdd0bSPeter Zijlstra {
1229fdabdd0bSPeter Zijlstra 	Elf_Data *data = sec->data;
1230fdabdd0bSPeter Zijlstra 
1231fdabdd0bSPeter Zijlstra 	if (data->d_type != ELF_T_BYTE || data->d_off) {
1232fdabdd0bSPeter Zijlstra 		WARN("write to unexpected data for section: %s", sec->name);
1233fdabdd0bSPeter Zijlstra 		return -1;
1234fdabdd0bSPeter Zijlstra 	}
1235fdabdd0bSPeter Zijlstra 
1236fdabdd0bSPeter Zijlstra 	memcpy(data->d_buf + offset, insn, len);
1237fdabdd0bSPeter Zijlstra 
1238ff408273SJosh Poimboeuf 	mark_sec_changed(elf, sec, true);
1239fdabdd0bSPeter Zijlstra 
1240fdabdd0bSPeter Zijlstra 	return 0;
1241fdabdd0bSPeter Zijlstra }
1242fdabdd0bSPeter Zijlstra 
124313f60e80SPeter Zijlstra /*
124413f60e80SPeter Zijlstra  * When Elf_Scn::sh_size is smaller than the combined Elf_Data::d_size
124513f60e80SPeter Zijlstra  * do you:
124613f60e80SPeter Zijlstra  *
124713f60e80SPeter Zijlstra  *   A) adhere to the section header and truncate the data, or
124813f60e80SPeter Zijlstra  *   B) ignore the section header and write out all the data you've got?
124913f60e80SPeter Zijlstra  *
125013f60e80SPeter Zijlstra  * Yes, libelf sucks and we need to manually truncate if we over-allocate data.
125113f60e80SPeter Zijlstra  */
elf_truncate_section(struct elf * elf,struct section * sec)125213f60e80SPeter Zijlstra static int elf_truncate_section(struct elf *elf, struct section *sec)
125313f60e80SPeter Zijlstra {
125413f60e80SPeter Zijlstra 	u64 size = sec->sh.sh_size;
125513f60e80SPeter Zijlstra 	bool truncated = false;
125613f60e80SPeter Zijlstra 	Elf_Data *data = NULL;
125713f60e80SPeter Zijlstra 	Elf_Scn *s;
125813f60e80SPeter Zijlstra 
125913f60e80SPeter Zijlstra 	s = elf_getscn(elf->elf, sec->idx);
126013f60e80SPeter Zijlstra 	if (!s) {
126113f60e80SPeter Zijlstra 		WARN_ELF("elf_getscn");
126213f60e80SPeter Zijlstra 		return -1;
126313f60e80SPeter Zijlstra 	}
126413f60e80SPeter Zijlstra 
126513f60e80SPeter Zijlstra 	for (;;) {
126613f60e80SPeter Zijlstra 		/* get next data descriptor for the relevant section */
126713f60e80SPeter Zijlstra 		data = elf_getdata(s, data);
126813f60e80SPeter Zijlstra 
126913f60e80SPeter Zijlstra 		if (!data) {
127013f60e80SPeter Zijlstra 			if (size) {
127113f60e80SPeter Zijlstra 				WARN("end of section data but non-zero size left\n");
127213f60e80SPeter Zijlstra 				return -1;
127313f60e80SPeter Zijlstra 			}
127413f60e80SPeter Zijlstra 			return 0;
127513f60e80SPeter Zijlstra 		}
127613f60e80SPeter Zijlstra 
127713f60e80SPeter Zijlstra 		if (truncated) {
127813f60e80SPeter Zijlstra 			/* when we remove symbols */
127913f60e80SPeter Zijlstra 			WARN("truncated; but more data\n");
128013f60e80SPeter Zijlstra 			return -1;
128113f60e80SPeter Zijlstra 		}
128213f60e80SPeter Zijlstra 
128313f60e80SPeter Zijlstra 		if (!data->d_size) {
128413f60e80SPeter Zijlstra 			WARN("zero size data");
128513f60e80SPeter Zijlstra 			return -1;
128613f60e80SPeter Zijlstra 		}
128713f60e80SPeter Zijlstra 
128813f60e80SPeter Zijlstra 		if (data->d_size > size) {
128913f60e80SPeter Zijlstra 			truncated = true;
129013f60e80SPeter Zijlstra 			data->d_size = size;
129113f60e80SPeter Zijlstra 		}
129213f60e80SPeter Zijlstra 
129313f60e80SPeter Zijlstra 		size -= data->d_size;
129413f60e80SPeter Zijlstra 	}
129513f60e80SPeter Zijlstra }
129613f60e80SPeter Zijlstra 
elf_write(struct elf * elf)12972b10be23SPeter Zijlstra int elf_write(struct elf *elf)
1298627fce14SJosh Poimboeuf {
1299627fce14SJosh Poimboeuf 	struct section *sec;
1300627fce14SJosh Poimboeuf 	Elf_Scn *s;
1301627fce14SJosh Poimboeuf 
13022daf7fabSJosh Poimboeuf 	if (opts.dryrun)
1303f2d3a250SPeter Zijlstra 		return 0;
1304f2d3a250SPeter Zijlstra 
13053a647607SPeter Zijlstra 	/* Update changed relocation sections and section headers: */
1306627fce14SJosh Poimboeuf 	list_for_each_entry(sec, &elf->sections, list) {
130713f60e80SPeter Zijlstra 		if (sec->truncate)
130813f60e80SPeter Zijlstra 			elf_truncate_section(elf, sec);
130913f60e80SPeter Zijlstra 
1310ff408273SJosh Poimboeuf 		if (sec_changed(sec)) {
1311627fce14SJosh Poimboeuf 			s = elf_getscn(elf->elf, sec->idx);
1312627fce14SJosh Poimboeuf 			if (!s) {
1313627fce14SJosh Poimboeuf 				WARN_ELF("elf_getscn");
1314627fce14SJosh Poimboeuf 				return -1;
1315627fce14SJosh Poimboeuf 			}
1316ff408273SJosh Poimboeuf 
1317ff408273SJosh Poimboeuf 			/* Note this also flags the section dirty */
1318627fce14SJosh Poimboeuf 			if (!gelf_update_shdr(s, &sec->sh)) {
1319627fce14SJosh Poimboeuf 				WARN_ELF("gelf_update_shdr");
1320627fce14SJosh Poimboeuf 				return -1;
1321627fce14SJosh Poimboeuf 			}
13222b10be23SPeter Zijlstra 
1323ff408273SJosh Poimboeuf 			mark_sec_changed(elf, sec, false);
1324627fce14SJosh Poimboeuf 		}
1325627fce14SJosh Poimboeuf 	}
1326627fce14SJosh Poimboeuf 
132797dab2aeSJosh Poimboeuf 	/* Make sure the new section header entries get updated properly. */
132897dab2aeSJosh Poimboeuf 	elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
132997dab2aeSJosh Poimboeuf 
133097dab2aeSJosh Poimboeuf 	/* Write all changes to the file. */
1331627fce14SJosh Poimboeuf 	if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
1332627fce14SJosh Poimboeuf 		WARN_ELF("elf_update");
1333627fce14SJosh Poimboeuf 		return -1;
1334627fce14SJosh Poimboeuf 	}
1335627fce14SJosh Poimboeuf 
13362b10be23SPeter Zijlstra 	elf->changed = false;
13372b10be23SPeter Zijlstra 
1338627fce14SJosh Poimboeuf 	return 0;
1339627fce14SJosh Poimboeuf }
1340627fce14SJosh Poimboeuf 
elf_close(struct elf * elf)1341442f04c3SJosh Poimboeuf void elf_close(struct elf *elf)
1342442f04c3SJosh Poimboeuf {
1343baa41469SJosh Poimboeuf 	if (elf->elf)
1344baa41469SJosh Poimboeuf 		elf_end(elf->elf);
1345baa41469SJosh Poimboeuf 
1346baa41469SJosh Poimboeuf 	if (elf->fd > 0)
1347baa41469SJosh Poimboeuf 		close(elf->fd);
1348baa41469SJosh Poimboeuf 
13495201a9bcSJosh Poimboeuf 	/*
13505201a9bcSJosh Poimboeuf 	 * NOTE: All remaining allocations are leaked on purpose.  Objtool is
13515201a9bcSJosh Poimboeuf 	 * about to exit anyway.
13525201a9bcSJosh Poimboeuf 	 */
1353442f04c3SJosh Poimboeuf }
1354