Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * elf.c - ELF access library
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Adapted from kpatch (https://github.com/dynup/kpatch):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2013-2015 Josh Poimboeuf <jpoimboe@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include "elf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include "warn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define MAX_NAME_LEN 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) static inline u32 str_hash(const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 	return jhash(str, strlen(str), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) static inline int elf_hash_bits(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	return vmlinux ? ELF_HASH_BITS : 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define elf_hash_add(hashtable, node, key) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	hlist_add_head(node, &hashtable[hash_min(key, elf_hash_bits())])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) static void elf_hash_init(struct hlist_head *table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 	__hash_init(table, 1U << elf_hash_bits());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define elf_hash_for_each_possible(name, obj, member, key)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 	hlist_for_each_entry(obj, &name[hash_min(key, elf_hash_bits())], member)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static void rb_add(struct rb_root *tree, struct rb_node *node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 		   int (*cmp)(struct rb_node *, const struct rb_node *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	struct rb_node **link = &tree->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	struct rb_node *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	while (*link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 		parent = *link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 		if (cmp(node, parent) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 			link = &parent->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 			link = &parent->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	rb_link_node(node, parent, link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	rb_insert_color(node, tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static struct rb_node *rb_find_first(const struct rb_root *tree, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 			       int (*cmp)(const void *key, const struct rb_node *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct rb_node *node = tree->rb_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct rb_node *match = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	while (node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		int c = cmp(key, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 		if (c <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 			if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 				match = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 			node = node->rb_left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 		} else if (c > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 			node = node->rb_right;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 	return match;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static struct rb_node *rb_next_match(struct rb_node *node, const void *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 				    int (*cmp)(const void *key, const struct rb_node *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	node = rb_next(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 	if (node && cmp(key, node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 		node = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	return node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define rb_for_each(tree, node, key, cmp) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	for ((node) = rb_find_first((tree), (key), (cmp)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	     (node); (node) = rb_next_match((node), (key), (cmp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) static int symbol_to_offset(struct rb_node *a, const struct rb_node *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	struct symbol *sa = rb_entry(a, struct symbol, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	struct symbol *sb = rb_entry(b, struct symbol, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	if (sa->offset < sb->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	if (sa->offset > sb->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 	if (sa->len < sb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	if (sa->len > sb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	sa->alias = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) static int symbol_by_offset(const void *key, const struct rb_node *node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 	const struct symbol *s = rb_entry(node, struct symbol, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	const unsigned long *o = key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	if (*o < s->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 	if (*o >= s->offset + s->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) struct section *find_section_by_name(const struct elf *elf, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	struct section *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	elf_hash_for_each_possible(elf->section_name_hash, sec, name_hash, str_hash(name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		if (!strcmp(sec->name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 			return sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) static struct section *find_section_by_index(struct elf *elf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 					     unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	struct section *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	elf_hash_for_each_possible(elf->section_hash, sec, hash, idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		if (sec->idx == idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 			return sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) static struct symbol *find_symbol_by_index(struct elf *elf, unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	struct symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	elf_hash_for_each_possible(elf->symbol_hash, sym, hash, idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 		if (sym->idx == idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 			return sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 		struct symbol *s = rb_entry(node, struct symbol, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		if (s->offset == offset && s->type != STT_SECTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 			return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) struct symbol *find_func_by_offset(struct section *sec, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 		struct symbol *s = rb_entry(node, struct symbol, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		if (s->offset == offset && s->type == STT_FUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 			return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		struct symbol *s = rb_entry(node, struct symbol, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		if (s->type != STT_SECTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 			return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) struct symbol *find_func_containing(struct section *sec, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct rb_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	rb_for_each(&sec->symbol_tree, node, &offset, symbol_by_offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		struct symbol *s = rb_entry(node, struct symbol, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 		if (s->type == STT_FUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 			return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) struct symbol *find_symbol_by_name(const struct elf *elf, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct symbol *sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	elf_hash_for_each_possible(elf->symbol_name_hash, sym, name_hash, str_hash(name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		if (!strcmp(sym->name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 			return sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 				     unsigned long offset, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	struct reloc *reloc, *r = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	unsigned long o;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	if (!sec->reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	sec = sec->reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	for_offset_range(o, offset, offset + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		elf_hash_for_each_possible(elf->reloc_hash, reloc, hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 				       sec_offset_hash(sec, o)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			if (reloc->sec != sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 			if (reloc->offset >= offset && reloc->offset < offset + len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 				if (!r || reloc->offset < r->offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 					r = reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	return find_reloc_by_dest_range(elf, sec, offset, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) void insn_to_reloc_sym_addend(struct section *sec, unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 			      struct reloc *reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	if (sec->sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		reloc->sym = sec->sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		reloc->addend = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	 * The Clang assembler strips section symbols, so we have to reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	 * the function symbol instead:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	reloc->sym = find_symbol_containing(sec, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	if (!reloc->sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		 * Hack alert.  This happens when we need to reference the NOP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		 * pad insn immediately after the function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		reloc->sym = find_symbol_containing(sec, offset - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	if (reloc->sym)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		reloc->addend = offset - reloc->sym->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) static int read_sections(struct elf *elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	Elf_Scn *s = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct section *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	size_t shstrndx, sections_nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	if (elf_getshdrnum(elf->elf, &sections_nr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		WARN_ELF("elf_getshdrnum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	if (elf_getshdrstrndx(elf->elf, &shstrndx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 		WARN_ELF("elf_getshdrstrndx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	for (i = 0; i < sections_nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		sec = malloc(sizeof(*sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		if (!sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 			perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		memset(sec, 0, sizeof(*sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		INIT_LIST_HEAD(&sec->symbol_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		INIT_LIST_HEAD(&sec->reloc_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		s = elf_getscn(elf->elf, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			WARN_ELF("elf_getscn");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		sec->idx = elf_ndxscn(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		if (!gelf_getshdr(s, &sec->sh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			WARN_ELF("gelf_getshdr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		sec->name = elf_strptr(elf->elf, shstrndx, sec->sh.sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		if (!sec->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 			WARN_ELF("elf_strptr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		if (sec->sh.sh_size != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 			sec->data = elf_getdata(s, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 			if (!sec->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 				WARN_ELF("elf_getdata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 			if (sec->data->d_off != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			    sec->data->d_size != sec->sh.sh_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 				WARN("unexpected data attributes for %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 				     sec->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		sec->len = sec->sh.sh_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		list_add_tail(&sec->list, &elf->sections);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		printf("nr_sections: %lu\n", (unsigned long)sections_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	/* sanity check, one more call to elf_nextscn() should return NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	if (elf_nextscn(elf->elf, s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		WARN("section entry mismatch");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) static int read_symbols(struct elf *elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct section *symtab, *symtab_shndx, *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	struct symbol *sym, *pfunc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	struct list_head *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	struct rb_node *pnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	int symbols_nr, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	char *coldstr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	Elf_Data *shndx_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	Elf32_Word shndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	symtab = find_section_by_name(elf, ".symtab");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	if (!symtab) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		 * A missing symbol table is actually possible if it's an empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		 * .o file.  This can happen for thunk_64.o.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	symtab_shndx = find_section_by_name(elf, ".symtab_shndx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	if (symtab_shndx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		shndx_data = symtab_shndx->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	symbols_nr = symtab->sh.sh_size / symtab->sh.sh_entsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	for (i = 0; i < symbols_nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		sym = malloc(sizeof(*sym));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		if (!sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		memset(sym, 0, sizeof(*sym));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 		sym->alias = sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 		sym->idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		if (!gelf_getsymshndx(symtab->data, shndx_data, i, &sym->sym,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 				      &shndx)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			WARN_ELF("gelf_getsymshndx");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		sym->name = elf_strptr(elf->elf, symtab->sh.sh_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 				       sym->sym.st_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 		if (!sym->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 			WARN_ELF("elf_strptr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		sym->type = GELF_ST_TYPE(sym->sym.st_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		sym->bind = GELF_ST_BIND(sym->sym.st_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		if ((sym->sym.st_shndx > SHN_UNDEF &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		     sym->sym.st_shndx < SHN_LORESERVE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 		    (shndx_data && sym->sym.st_shndx == SHN_XINDEX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			if (sym->sym.st_shndx != SHN_XINDEX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 				shndx = sym->sym.st_shndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 			sym->sec = find_section_by_index(elf, shndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 			if (!sym->sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 				WARN("couldn't find section for symbol %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 				     sym->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 				goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			if (sym->type == STT_SECTION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 				sym->name = sym->sec->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 				sym->sec->sym = sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			sym->sec = find_section_by_index(elf, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 		sym->offset = sym->sym.st_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		sym->len = sym->sym.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		rb_add(&sym->sec->symbol_tree, &sym->node, symbol_to_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		pnode = rb_prev(&sym->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		if (pnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 			entry = &rb_entry(pnode, struct symbol, node)->list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 			entry = &sym->sec->symbol_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		list_add(&sym->list, entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		elf_hash_add(elf->symbol_hash, &sym->hash, sym->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		elf_hash_add(elf->symbol_name_hash, &sym->name_hash, str_hash(sym->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 		 * Don't store empty STT_NOTYPE symbols in the rbtree.  They
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		 * can exist within a function, confusing the sorting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		if (!sym->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			rb_erase(&sym->node, &sym->sec->symbol_tree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	if (stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		printf("nr_symbols: %lu\n", (unsigned long)symbols_nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	/* Create parent/child links for any cold subfunctions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	list_for_each_entry(sec, &elf->sections, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 		list_for_each_entry(sym, &sec->symbol_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 			char pname[MAX_NAME_LEN + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 			size_t pnamelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 			if (sym->type != STT_FUNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 			if (sym->pfunc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 				sym->pfunc = sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 			if (sym->cfunc == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 				sym->cfunc = sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 			coldstr = strstr(sym->name, ".cold");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 			if (!coldstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 			pnamelen = coldstr - sym->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 			if (pnamelen > MAX_NAME_LEN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 				WARN("%s(): parent function name exceeds maximum length of %d characters",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 				     sym->name, MAX_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 			strncpy(pname, sym->name, pnamelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 			pname[pnamelen] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			pfunc = find_symbol_by_name(elf, pname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			if (!pfunc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 				WARN("%s(): can't find parent function",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 				     sym->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			sym->pfunc = pfunc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 			pfunc->cfunc = sym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 			 * Unfortunately, -fnoreorder-functions puts the child
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			 * inside the parent.  Remove the overlap so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			 * have sane assumptions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			 * Note that pfunc->len now no longer matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 			 * pfunc->sym.st_size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 			if (sym->sec == pfunc->sec &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			    sym->offset >= pfunc->offset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 			    sym->offset + sym->len == pfunc->offset + pfunc->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 				pfunc->len -= sym->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	free(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) void elf_add_reloc(struct elf *elf, struct reloc *reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct section *sec = reloc->sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	list_add_tail(&reloc->list, &sec->reloc_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	elf_hash_add(elf->reloc_hash, &reloc->hash, reloc_hash(reloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) static int read_rel_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (!gelf_getrel(sec->data, i, &reloc->rel)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		WARN_ELF("gelf_getrel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	reloc->type = GELF_R_TYPE(reloc->rel.r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	reloc->addend = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	reloc->offset = reloc->rel.r_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	*symndx = GELF_R_SYM(reloc->rel.r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) static int read_rela_reloc(struct section *sec, int i, struct reloc *reloc, unsigned int *symndx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	if (!gelf_getrela(sec->data, i, &reloc->rela)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		WARN_ELF("gelf_getrela");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	reloc->type = GELF_R_TYPE(reloc->rela.r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	reloc->addend = reloc->rela.r_addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	reloc->offset = reloc->rela.r_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	*symndx = GELF_R_SYM(reloc->rela.r_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) static int read_relocs(struct elf *elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	struct section *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	struct reloc *reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	unsigned int symndx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	unsigned long nr_reloc, max_reloc = 0, tot_reloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	list_for_each_entry(sec, &elf->sections, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		if ((sec->sh.sh_type != SHT_RELA) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		    (sec->sh.sh_type != SHT_REL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		sec->base = find_section_by_index(elf, sec->sh.sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		if (!sec->base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 			WARN("can't find base section for reloc section %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 			     sec->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		sec->base->reloc = sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		nr_reloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		for (i = 0; i < sec->sh.sh_size / sec->sh.sh_entsize; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			reloc = malloc(sizeof(*reloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 			if (!reloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 				perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			memset(reloc, 0, sizeof(*reloc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 			switch (sec->sh.sh_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 			case SHT_REL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 				if (read_rel_reloc(sec, i, reloc, &symndx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			case SHT_RELA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 				if (read_rela_reloc(sec, i, reloc, &symndx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 					return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 			default: return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			reloc->sec = sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 			reloc->idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 			reloc->sym = find_symbol_by_index(elf, symndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			if (!reloc->sym) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 				WARN("can't find reloc entry symbol %d for %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 				     symndx, sec->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 			elf_add_reloc(elf, reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			nr_reloc++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		max_reloc = max(max_reloc, nr_reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		tot_reloc += nr_reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	if (stats) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		printf("max_reloc: %lu\n", max_reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		printf("tot_reloc: %lu\n", tot_reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) struct elf *elf_open_read(const char *name, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	struct elf *elf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	Elf_Cmd cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	elf_version(EV_CURRENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	elf = malloc(sizeof(*elf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	if (!elf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	memset(elf, 0, offsetof(struct elf, sections));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	INIT_LIST_HEAD(&elf->sections);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	elf_hash_init(elf->symbol_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	elf_hash_init(elf->symbol_name_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	elf_hash_init(elf->section_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	elf_hash_init(elf->section_name_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	elf_hash_init(elf->reloc_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	elf->fd = open(name, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	if (elf->fd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		fprintf(stderr, "objtool: Can't open '%s': %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 			name, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if ((flags & O_ACCMODE) == O_RDONLY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		cmd = ELF_C_READ_MMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	else if ((flags & O_ACCMODE) == O_RDWR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		cmd = ELF_C_RDWR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	else /* O_WRONLY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		cmd = ELF_C_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	elf->elf = elf_begin(elf->fd, cmd, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	if (!elf->elf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		WARN_ELF("elf_begin");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		WARN_ELF("gelf_getehdr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	if (read_sections(elf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (read_symbols(elf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	if (read_relocs(elf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	return elf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	elf_close(elf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) struct section *elf_create_section(struct elf *elf, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 				   unsigned int sh_flags, size_t entsize, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct section *sec, *shstrtab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	size_t size = entsize * nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	Elf_Scn *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	Elf_Data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	sec = malloc(sizeof(*sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (!sec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	memset(sec, 0, sizeof(*sec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	INIT_LIST_HEAD(&sec->symbol_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	INIT_LIST_HEAD(&sec->reloc_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	s = elf_newscn(elf->elf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 		WARN_ELF("elf_newscn");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	sec->name = strdup(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	if (!sec->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		perror("strdup");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	sec->idx = elf_ndxscn(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	sec->len = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	sec->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	sec->data = elf_newdata(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	if (!sec->data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		WARN_ELF("elf_newdata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	sec->data->d_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	sec->data->d_align = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		sec->data->d_buf = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		if (!sec->data->d_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		memset(sec->data->d_buf, 0, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	if (!gelf_getshdr(s, &sec->sh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		WARN_ELF("gelf_getshdr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	sec->sh.sh_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	sec->sh.sh_entsize = entsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	sec->sh.sh_type = SHT_PROGBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	sec->sh.sh_addralign = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	sec->sh.sh_flags = SHF_ALLOC | sh_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	/* Add section name to .shstrtab (or .strtab for Clang) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	shstrtab = find_section_by_name(elf, ".shstrtab");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	if (!shstrtab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		shstrtab = find_section_by_name(elf, ".strtab");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	if (!shstrtab) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		WARN("can't find .shstrtab or .strtab section");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	s = elf_getscn(elf->elf, shstrtab->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		WARN_ELF("elf_getscn");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	data = elf_newdata(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		WARN_ELF("elf_newdata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	data->d_buf = sec->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	data->d_size = strlen(name) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	data->d_align = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	sec->sh.sh_name = shstrtab->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	shstrtab->len += strlen(name) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	shstrtab->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	list_add_tail(&sec->list, &elf->sections);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	elf_hash_add(elf->section_hash, &sec->hash, sec->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	elf_hash_add(elf->section_name_hash, &sec->name_hash, str_hash(sec->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	elf->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	return sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) static struct section *elf_create_rel_reloc_section(struct elf *elf, struct section *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	char *relocname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	struct section *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	relocname = malloc(strlen(base->name) + strlen(".rel") + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	if (!relocname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	strcpy(relocname, ".rel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	strcat(relocname, base->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rel), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	free(relocname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 	if (!sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	base->reloc = sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	sec->base = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	sec->sh.sh_type = SHT_REL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	sec->sh.sh_addralign = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	sec->sh.sh_info = base->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	sec->sh.sh_flags = SHF_INFO_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	return sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) static struct section *elf_create_rela_reloc_section(struct elf *elf, struct section *base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	char *relocname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	struct section *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	relocname = malloc(strlen(base->name) + strlen(".rela") + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	if (!relocname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	strcpy(relocname, ".rela");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	strcat(relocname, base->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	sec = elf_create_section(elf, relocname, 0, sizeof(GElf_Rela), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	free(relocname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	if (!sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	base->reloc = sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	sec->base = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	sec->sh.sh_type = SHT_RELA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	sec->sh.sh_addralign = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	sec->sh.sh_link = find_section_by_name(elf, ".symtab")->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	sec->sh.sh_info = base->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	sec->sh.sh_flags = SHF_INFO_LINK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	return sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) struct section *elf_create_reloc_section(struct elf *elf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 					 struct section *base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 					 int reltype)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	switch (reltype) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	case SHT_REL:  return elf_create_rel_reloc_section(elf, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	case SHT_RELA: return elf_create_rela_reloc_section(elf, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	default:       return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	struct reloc *reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	int idx = 0, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	GElf_Rel *relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	/* Allocate a buffer for relocations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	size = nr * sizeof(*relocs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	relocs = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (!relocs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	sec->data->d_buf = relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	sec->data->d_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	sec->sh.sh_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	list_for_each_entry(reloc, &sec->reloc_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		relocs[idx].r_offset = reloc->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	struct reloc *reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	int idx = 0, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	GElf_Rela *relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/* Allocate a buffer for relocations with addends */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	size = nr * sizeof(*relocs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	relocs = malloc(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (!relocs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	sec->data->d_buf = relocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	sec->data->d_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	sec->sh.sh_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	list_for_each_entry(reloc, &sec->reloc_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		relocs[idx].r_offset = reloc->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		relocs[idx].r_addend = reloc->addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) int elf_rebuild_reloc_section(struct elf *elf, struct section *sec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct reloc *reloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	int nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	sec->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	elf->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	nr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	list_for_each_entry(reloc, &sec->reloc_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		nr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	switch (sec->sh.sh_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	case SHT_REL:  return elf_rebuild_rel_reloc_section(sec, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	case SHT_RELA: return elf_rebuild_rela_reloc_section(sec, nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 	default:       return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) int elf_write_insn(struct elf *elf, struct section *sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		   unsigned long offset, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		   const char *insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	Elf_Data *data = sec->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	if (data->d_type != ELF_T_BYTE || data->d_off) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		WARN("write to unexpected data for section: %s", sec->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	memcpy(data->d_buf + offset, insn, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	elf_flagdata(data, ELF_C_SET, ELF_F_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	elf->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) int elf_write_reloc(struct elf *elf, struct reloc *reloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	struct section *sec = reloc->sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (sec->sh.sh_type == SHT_REL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		reloc->rel.r_offset = reloc->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		if (!gelf_update_rel(sec->data, reloc->idx, &reloc->rel)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			WARN_ELF("gelf_update_rel");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		reloc->rela.r_addend = reloc->addend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		reloc->rela.r_offset = reloc->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		if (!gelf_update_rela(sec->data, reloc->idx, &reloc->rela)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			WARN_ELF("gelf_update_rela");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 	elf->changed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) int elf_write(struct elf *elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 	struct section *sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	Elf_Scn *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	/* Update section headers for changed sections: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	list_for_each_entry(sec, &elf->sections, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		if (sec->changed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			s = elf_getscn(elf->elf, sec->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 				WARN_ELF("elf_getscn");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			if (!gelf_update_shdr(s, &sec->sh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 				WARN_ELF("gelf_update_shdr");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 				return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 			sec->changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	/* Make sure the new section header entries get updated properly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	elf_flagelf(elf->elf, ELF_C_SET, ELF_F_DIRTY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	/* Write all changes to the file. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	if (elf_update(elf->elf, ELF_C_WRITE) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		WARN_ELF("elf_update");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	elf->changed = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) void elf_close(struct elf *elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	struct section *sec, *tmpsec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	struct symbol *sym, *tmpsym;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	struct reloc *reloc, *tmpreloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	if (elf->elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 		elf_end(elf->elf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	if (elf->fd > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 		close(elf->fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	list_for_each_entry_safe(sec, tmpsec, &elf->sections, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 		list_for_each_entry_safe(sym, tmpsym, &sec->symbol_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 			list_del(&sym->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			hash_del(&sym->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			free(sym);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		list_for_each_entry_safe(reloc, tmpreloc, &sec->reloc_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 			list_del(&reloc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			hash_del(&reloc->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 			free(reloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 		list_del(&sec->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		free(sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	free(elf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }