889 lines · 18.5 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 <fcntl.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <errno.h>#include "builtin.h" #include "elf.h"#include "warn.h" #define MAX_NAME_LEN 128 static inline u32 str_hash(const char *str){	return jhash(str, strlen(str), 0);} static inline int elf_hash_bits(void){	return vmlinux ? ELF_HASH_BITS : 16;} #define elf_hash_add(hashtable, node, key) \	hlist_add_head(node, &hashtable[hash_min(key, elf_hash_bits())]) static void elf_hash_init(struct hlist_head *table){	__hash_init(table, 1U << elf_hash_bits());} #define elf_hash_for_each_possible(name, obj, member, key)			\	hlist_for_each_entry(obj, &name[hash_min(key, elf_hash_bits())], member) static void rb_add(struct rb_root *tree, struct rb_node *node,		   int (*cmp)(struct rb_node *, const struct rb_node *)){	struct rb_node **link = &tree->rb_node;	struct rb_node *parent = NULL; 	while (*link) {		parent = *link;		if (cmp(node, parent) < 0)			link = &parent->rb_left;		else			link = &parent->rb_right;	} 	rb_link_node(node, parent, link);	rb_insert_color(node, tree);} static struct rb_node *rb_find_first(const struct rb_root *tree, const void *key,			       int (*cmp)(const void *key, const struct rb_node *)){	struct rb_node *node = tree->rb_node;	struct rb_node *match = NULL; 	while (node) {		int c = cmp(key, node);		if (c <= 0) {			if (!c)				match = node;			node = node->rb_left;		} else if (c > 0) {			node = node->rb_right;		}	} 	return match;} static struct rb_node *rb_next_match(struct rb_node *node, const void *key,				    int (*cmp)(const void *key, const struct rb_node *)){	node = rb_next(node);	if (node && cmp(key, node))		node = NULL;	return node;} #define rb_for_each(tree, node, key, cmp) \	for ((node) = rb_find_first((tree), (key), (cmp)); \	     (node); (node) = rb_next_match((node), (key), (cmp))) static int symbol_to_offset(struct rb_node *a, const struct rb_node *b){	struct symbol *sa = rb_entry(a, struct symbol, node);	struct symbol *sb = rb_entry(b, struct symbol, node); 	if (sa->offset < sb->offset)		return -1;	if (sa->offset > sb->offset)		return 1; 	if (sa->len < sb->len)		return -1;	if (sa->len > sb->len)		return 1; 	sa->alias = sb; 	return 0;} static int symbol_by_offset(const void *key, const struct rb_node *node){	const struct symbol *s = rb_entry(node, struct symbol, node);	const unsigned long *o = key; 	if (*o < s->offset)		return -1;	if (*o >= s->offset + s->len)		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(elf->section_name_hash, 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(elf->section_hash, 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(elf->symbol_hash, 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_node *node; 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {		struct symbol *s = rb_entry(node, struct symbol, node); 		if (s->offset == offset && s->type != STT_SECTION)			return s;	} 	return NULL;} struct symbol *find_func_by_offset(struct section *sec, unsigned long offset){	struct rb_node *node; 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {		struct symbol *s = rb_entry(node, struct symbol, node); 		if (s->offset == offset && s->type == STT_FUNC)			return s;	} 	return NULL;} struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset){	struct rb_node *node; 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {		struct symbol *s = rb_entry(node, struct symbol, node); 		if (s->type != STT_SECTION)			return s;	} 	return NULL;} struct symbol *find_func_containing(struct section *sec, unsigned long offset){	struct rb_node *node; 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {		struct symbol *s = rb_entry(node, struct symbol, node); 		if (s->type == STT_FUNC)			return s;	} 	return NULL;} struct symbol *find_symbol_by_name(const struct elf *elf, const char *name){	struct symbol *sym; 	elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))		if (!strcmp(sym->name, name))			return sym; 	return NULL;} struct rela *find_rela_by_dest_range(const struct elf *elf, struct section *sec,				     unsigned long offset, unsigned int len){	struct rela *rela, *r = NULL;	unsigned long o; 	if (!sec->rela)		return NULL; 	sec = sec->rela; 	for_offset_range(o, offset, offset + len) {		elf_hash_for_each_possible(elf->rela_hash, rela, hash,				       sec_offset_hash(sec, o)) {			if (rela->sec != sec)				continue; 			if (rela->offset >= offset && rela->offset < offset + len) {				if (!r || rela->offset < r->offset)					r = rela;			}		}		if (r)			return r;	} 	return NULL;} struct rela *find_rela_by_dest(const struct elf *elf, struct section *sec, unsigned long offset){	return find_rela_by_dest_range(elf, sec, offset, 1);} 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;	} 	for (i = 0; i < sections_nr; i++) {		sec = malloc(sizeof(*sec));		if (!sec) {			perror("malloc");			return -1;		}		memset(sec, 0, sizeof(*sec)); 		INIT_LIST_HEAD(&sec->symbol_list);		INIT_LIST_HEAD(&sec->rela_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) {			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;			}		}		sec->len = sec->sh.sh_size; 		list_add_tail(&sec->list, &elf->sections);		elf_hash_add(elf->section_hash, &sec->hash, sec->idx);		elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));	} 	if (stats)		printf("nr_sections: %lu\n", (unsigned long)sections_nr); 	/* 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 int read_symbols(struct elf *elf){	struct section *symtab, *symtab_shndx, *sec;	struct symbol *sym, *pfunc;	struct list_head *entry;	struct rb_node *pnode;	int symbols_nr, i;	char *coldstr;	Elf_Data *shndx_data = NULL;	Elf32_Word shndx; 	symtab = find_section_by_name(elf, ".symtab");	if (!symtab) {		WARN("missing symbol table");		return -1;	} 	symtab_shndx = find_section_by_name(elf, ".symtab_shndx");	if (symtab_shndx)		shndx_data = symtab_shndx->data; 	symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize; 	for (i = 0; i < symbols_nr; i++) {		sym = malloc(sizeof(*sym));		if (!sym) {			perror("malloc");			return -1;		}		memset(sym, 0, sizeof(*sym));		sym->alias = sym; 		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;		} 		sym->type = GELF_ST_TYPE(sym->sym.st_info);		sym->bind = GELF_ST_BIND(sym->sym.st_info); 		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 (sym->type == STT_SECTION) {				sym->name = sym->sec->name;				sym->sec->sym = sym;			}		} else			sym->sec = find_section_by_index(elf, 0); 		sym->offset = sym->sym.st_value;		sym->len = sym->sym.st_size; 		rb_add(&sym->sec->symbol_tree, &sym->node, symbol_to_offset);		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(elf->symbol_hash, &sym->hash, sym->idx);		elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));	} 	if (stats)		printf("nr_symbols: %lu\n", (unsigned long)symbols_nr); 	/* Create parent/child links for any cold subfunctions */	list_for_each_entry(sec, &elf->sections, list) {		list_for_each_entry(sym, &sec->symbol_list, list) {			char pname[MAX_NAME_LEN + 1];			size_t pnamelen;			if (sym->type != STT_FUNC)				continue;			sym->pfunc = sym->cfunc = sym;			coldstr = strstr(sym->name, ".cold");			if (!coldstr)				continue; 			pnamelen = coldstr - sym->name;			if (pnamelen > MAX_NAME_LEN) {				WARN("%s(): parent function name exceeds maximum length of %d characters",				     sym->name, MAX_NAME_LEN);				return -1;			} 			strncpy(pname, sym->name, pnamelen);			pname[pnamelen] = '\0';			pfunc = find_symbol_by_name(elf, pname); 			if (!pfunc) {				WARN("%s(): can't find parent function",				     sym->name);				return -1;			} 			sym->pfunc = pfunc;			pfunc->cfunc = sym; 			/*			 * Unfortunately, -fnoreorder-functions puts the child			 * inside the parent.  Remove the overlap so we can			 * have sane assumptions.			 *			 * Note that pfunc->len now no longer matches			 * pfunc->sym.st_size.			 */			if (sym->sec == pfunc->sec &&			    sym->offset >= pfunc->offset &&			    sym->offset + sym->len == pfunc->offset + pfunc->len) {				pfunc->len -= sym->len;			}		}	} 	return 0; err:	free(sym);	return -1;} void elf_add_rela(struct elf *elf, struct rela *rela){	struct section *sec = rela->sec; 	list_add_tail(&rela->list, &sec->rela_list);	elf_hash_add(elf->rela_hash, &rela->hash, rela_hash(rela));} static int read_relas(struct elf *elf){	struct section *sec;	struct rela *rela;	int i;	unsigned int symndx;	unsigned long nr_rela, max_rela = 0, tot_rela = 0; 

Lines 1–500 of 889.