1353 lines · 29.4 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0-or-later/* * elf.c - ELF access library * * Adapted from kpatch (https://github.com/dynup/kpatch): * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com> * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com> */ #include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h>#include <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <errno.h>#include <linux/interval_tree_generic.h>#include <objtool/builtin.h> #include <objtool/elf.h>#include <objtool/warn.h> #define MAX_NAME_LEN 128 static inline u32 str_hash(const char *str){	return jhash(str, strlen(str), 0);} #define __elf_table(name)	(elf->name##_hash)#define __elf_bits(name)	(elf->name##_bits) #define __elf_table_entry(name, key) \	__elf_table(name)[hash_min(key, __elf_bits(name))] #define elf_hash_add(name, node, key)					\({									\	struct elf_hash_node *__node = node;				\	__node->next = __elf_table_entry(name, key);			\	__elf_table_entry(name, key) = __node;				\}) static inline void __elf_hash_del(struct elf_hash_node *node,				  struct elf_hash_node **head){	struct elf_hash_node *cur, *prev; 	if (node == *head) {		*head = node->next;		return;	} 	for (prev = NULL, cur = *head; cur; prev = cur, cur = cur->next) {		if (cur == node) {			prev->next = cur->next;			break;		}	}} #define elf_hash_del(name, node, key) \	__elf_hash_del(node, &__elf_table_entry(name, key)) #define elf_list_entry(ptr, type, member)				\({									\	typeof(ptr) __ptr = (ptr);					\	__ptr ? container_of(__ptr, type, member) : NULL;		\}) #define elf_hash_for_each_possible(name, obj, member, key)		\	for (obj = elf_list_entry(__elf_table_entry(name, key), typeof(*obj), member); \	     obj;							\	     obj = elf_list_entry(obj->member.next, typeof(*(obj)), member)) #define elf_alloc_hash(name, size) \({ \	__elf_bits(name) = max(10, ilog2(size)); \	__elf_table(name) = mmap(NULL, sizeof(struct elf_hash_node *) << __elf_bits(name), \				 PROT_READ|PROT_WRITE, \				 MAP_PRIVATE|MAP_ANON, -1, 0); \	if (__elf_table(name) == (void *)-1L) { \		WARN("mmap fail " #name); \		__elf_table(name) = NULL; \	} \	__elf_table(name); \}) static inline unsigned long __sym_start(struct symbol *s){	return s->offset;} static inline unsigned long __sym_last(struct symbol *s){	return s->offset + s->len - 1;} INTERVAL_TREE_DEFINE(struct symbol, node, unsigned long, __subtree_last,		     __sym_start, __sym_last, static, __sym) #define __sym_for_each(_iter, _tree, _start, _end)			\	for (_iter = __sym_iter_first((_tree), (_start), (_end));	\	     _iter; _iter = __sym_iter_next(_iter, (_start), (_end))) struct symbol_hole {	unsigned long key;	const struct symbol *sym;}; /* * Find !section symbol where @offset is after it. */static int symbol_hole_by_offset(const void *key, const struct rb_node *node){	const struct symbol *s = rb_entry(node, struct symbol, node);	struct symbol_hole *sh = (void *)key; 	if (sh->key < s->offset)		return -1; 	if (sh->key >= s->offset + s->len) {		if (s->type != STT_SECTION)			sh->sym = s;		return 1;	} 	return 0;} struct section *find_section_by_name(const struct elf *elf, const char *name){	struct section *sec; 	elf_hash_for_each_possible(section_name, sec, name_hash, str_hash(name)) {		if (!strcmp(sec->name, name))			return sec;	} 	return NULL;} static struct section *find_section_by_index(struct elf *elf,					     unsigned int idx){	struct section *sec; 	elf_hash_for_each_possible(section, sec, hash, idx) {		if (sec->idx == idx)			return sec;	} 	return NULL;} static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx){	struct symbol *sym; 	elf_hash_for_each_possible(symbol, sym, hash, idx) {		if (sym->idx == idx)			return sym;	} 	return NULL;} struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset){	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;	struct symbol *iter; 	__sym_for_each(iter, tree, offset, offset) {		if (iter->offset == offset && iter->type != STT_SECTION)			return iter;	} 	return NULL;} struct symbol *find_func_by_offset(struct section *sec, unsigned long offset){	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;	struct symbol *iter; 	__sym_for_each(iter, tree, offset, offset) {		if (iter->offset == offset && iter->type == STT_FUNC)			return iter;	} 	return NULL;} struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset){	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;	struct symbol *iter; 	__sym_for_each(iter, tree, offset, offset) {		if (iter->type != STT_SECTION)			return iter;	} 	return NULL;} /* * Returns size of hole starting at @offset. */int find_symbol_hole_containing(const struct section *sec, unsigned long offset){	struct symbol_hole hole = {		.key = offset,		.sym = NULL,	};	struct rb_node *n;	struct symbol *s; 	/*	 * Find the rightmost symbol for which @offset is after it.	 */	n = rb_find(&hole, &sec->symbol_tree.rb_root, symbol_hole_by_offset); 	/* found a symbol that contains @offset */	if (n)		return 0; /* not a hole */ 	/* didn't find a symbol for which @offset is after it */	if (!hole.sym)		return 0; /* not a hole */ 	/* @offset >= sym->offset + sym->len, find symbol after it */	n = rb_next(&hole.sym->node);	if (!n)		return -1; /* until end of address space */ 	/* hole until start of next symbol */	s = rb_entry(n, struct symbol, node);	return s->offset - offset;} struct symbol *find_func_containing(struct section *sec, unsigned long offset){	struct rb_root_cached *tree = (struct rb_root_cached *)&sec->symbol_tree;	struct symbol *iter; 	__sym_for_each(iter, tree, offset, offset) {		if (iter->type == STT_FUNC)			return iter;	} 	return NULL;} struct symbol *find_symbol_by_name(const struct elf *elf, const char *name){	struct symbol *sym; 	elf_hash_for_each_possible(symbol_name, sym, name_hash, str_hash(name)) {		if (!strcmp(sym->name, name))			return sym;	} 	return NULL;} struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,				     unsigned long offset, unsigned int len){	struct reloc *reloc, *r = NULL;	struct section *rsec;	unsigned long o; 	rsec = sec->rsec;	if (!rsec)		return NULL; 	for_offset_range(o, offset, offset + len) {		elf_hash_for_each_possible(reloc, reloc, hash,					   sec_offset_hash(rsec, o)) {			if (reloc->sec != rsec)				continue; 			if (reloc_offset(reloc) >= offset &&			    reloc_offset(reloc) < offset + len) {				if (!r || reloc_offset(reloc) < reloc_offset(r))					r = reloc;			}		}		if (r)			return r;	} 	return NULL;} struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset){	return find_reloc_by_dest_range(elf, sec, offset, 1);} static bool is_dwarf_section(struct section *sec){	return !strncmp(sec->name, ".debug_", 7);} static int read_sections(struct elf *elf){	Elf_Scn *s = NULL;	struct section *sec;	size_t shstrndx, sections_nr;	int i; 	if (elf_getshdrnum(elf->elf, &sections_nr)) {		WARN_ELF("elf_getshdrnum");		return -1;	} 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {		WARN_ELF("elf_getshdrstrndx");		return -1;	} 	if (!elf_alloc_hash(section, sections_nr) ||	    !elf_alloc_hash(section_name, sections_nr))		return -1; 	elf->section_data = calloc(sections_nr, sizeof(*sec));	if (!elf->section_data) {		perror("calloc");		return -1;	}	for (i = 0; i < sections_nr; i++) {		sec = &elf->section_data[i]; 		INIT_LIST_HEAD(&sec->symbol_list); 		s = elf_getscn(elf->elf, i);		if (!s) {			WARN_ELF("elf_getscn");			return -1;		} 		sec->idx = elf_ndxscn(s); 		if (!gelf_getshdr(s, &sec->sh)) {			WARN_ELF("gelf_getshdr");			return -1;		} 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);		if (!sec->name) {			WARN_ELF("elf_strptr");			return -1;		} 		if (sec->sh.sh_size != 0 && !is_dwarf_section(sec)) {			sec->data = elf_getdata(s, NULL);			if (!sec->data) {				WARN_ELF("elf_getdata");				return -1;			}			if (sec->data->d_off != 0 ||			    sec->data->d_size != sec->sh.sh_size) {				WARN("unexpected data attributes for %s",				     sec->name);				return -1;			}		} 		list_add_tail(&sec->list, &elf->sections);		elf_hash_add(section, &sec->hash, sec->idx);		elf_hash_add(section_name, &sec->name_hash, str_hash(sec->name)); 		if (is_reloc_sec(sec))			elf->num_relocs += sec_num_entries(sec);	} 	if (opts.stats) {		printf("nr_sections: %lu\n", (unsigned long)sections_nr);		printf("section_bits: %d\n", elf->section_bits);	} 	/* sanity check, one more call to elf_nextscn() should return NULL */	if (elf_nextscn(elf->elf, s)) {		WARN("section entry mismatch");		return -1;	} 	return 0;} static void elf_add_symbol(struct elf *elf, struct symbol *sym){	struct list_head *entry;	struct rb_node *pnode;	struct symbol *iter; 	INIT_LIST_HEAD(&sym->pv_target);	sym->alias = sym; 	sym->type = GELF_ST_TYPE(sym->sym.st_info);	sym->bind = GELF_ST_BIND(sym->sym.st_info); 	if (sym->type == STT_FILE)		elf->num_files++; 	sym->offset = sym->sym.st_value;	sym->len = sym->sym.st_size; 	__sym_for_each(iter, &sym->sec->symbol_tree, sym->offset, sym->offset) {		if (iter->offset == sym->offset && iter->type == sym->type)			iter->alias = sym;	} 	__sym_insert(sym, &sym->sec->symbol_tree);	pnode = rb_prev(&sym->node);	if (pnode)		entry = &rb_entry(pnode, struct symbol, node)->list;	else		entry = &sym->sec->symbol_list;	list_add(&sym->list, entry);	elf_hash_add(symbol, &sym->hash, sym->idx);	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->name)); 	/*	 * Don't store empty STT_NOTYPE symbols in the rbtree.  They	 * can exist within a function, confusing the sorting.	 */	if (!sym->len)		__sym_remove(sym, &sym->sec->symbol_tree);} static int read_symbols(struct elf *elf){	struct section *symtab, *symtab_shndx, *sec;	struct symbol *sym, *pfunc;	int symbols_nr, i;	char *coldstr;	Elf_Data *shndx_data = NULL;	Elf32_Word shndx; 	symtab = find_section_by_name(elf, ".symtab");	if (symtab) {		symtab_shndx = find_section_by_name(elf, ".symtab_shndx");		if (symtab_shndx)			shndx_data = symtab_shndx->data; 		symbols_nr = sec_num_entries(symtab);	} else {		/*		 * A missing symbol table is actually possible if it's an empty		 * .o file. This can happen for thunk_64.o. Make sure to at		 * least allocate the symbol hash tables so we can do symbol		 * lookups without crashing.		 */		symbols_nr = 0;	} 	if (!elf_alloc_hash(symbol, symbols_nr) ||	    !elf_alloc_hash(symbol_name, symbols_nr))		return -1; 	elf->symbol_data = calloc(symbols_nr, sizeof(*sym));	if (!elf->symbol_data) {		perror("calloc");		return -1;	}	for (i = 0; i < symbols_nr; i++) {		sym = &elf->symbol_data[i]; 		sym->idx = i; 		if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,				      &shndx)) {			WARN_ELF("gelf_getsymshndx");			goto err;		} 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,				       sym->sym.st_name);		if (!sym->name) {			WARN_ELF("elf_strptr");			goto err;		} 		if ((sym->sym.st_shndx > SHN_UNDEF &&		     sym->sym.st_shndx < SHN_LORESERVE) ||		    (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {			if (sym->sym.st_shndx != SHN_XINDEX)				shndx = sym->sym.st_shndx; 			sym->sec = find_section_by_index(elf, shndx);			if (!sym->sec) {				WARN("couldn't find section for symbol %s",				     sym->name);				goto err;			}			if (GELF_ST_TYPE(sym->sym.st_info) == STT_SECTION) {

Lines 1–500 of 1353.