1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4  */
5 
6 #ifndef _OBJTOOL_ELF_H
7 #define _OBJTOOL_ELF_H
8 
9 #include <stdio.h>
10 #include <gelf.h>
11 #include <linux/list.h>
12 #include <linux/hashtable.h>
13 #include <linux/rbtree.h>
14 #include <linux/jhash.h>
15 
16 #ifdef LIBELF_USE_DEPRECATED
17 # define elf_getshdrnum    elf_getshnum
18 # define elf_getshdrstrndx elf_getshstrndx
19 #endif
20 
21 /*
22  * Fallback for systems without this "read, mmaping if possible" cmd.
23  */
24 #ifndef ELF_C_READ_MMAP
25 #define ELF_C_READ_MMAP ELF_C_READ
26 #endif
27 
28 struct section {
29 	struct list_head list;
30 	struct hlist_node hash;
31 	struct hlist_node name_hash;
32 	GElf_Shdr sh;
33 	struct rb_root symbol_tree;
34 	struct list_head symbol_list;
35 	struct list_head reloc_list;
36 	struct section *base, *reloc;
37 	struct symbol *sym;
38 	Elf_Data *data;
39 	char *name;
40 	int idx;
41 	bool changed, text, rodata, noinstr;
42 };
43 
44 struct symbol {
45 	struct list_head list;
46 	struct rb_node node;
47 	struct hlist_node hash;
48 	struct hlist_node name_hash;
49 	GElf_Sym sym;
50 	struct section *sec;
51 	char *name;
52 	unsigned int idx;
53 	unsigned char bind, type;
54 	unsigned long offset;
55 	unsigned int len;
56 	struct symbol *pfunc, *cfunc, *alias;
57 	bool uaccess_safe;
58 	bool static_call_tramp;
59 };
60 
61 struct reloc {
62 	struct list_head list;
63 	struct hlist_node hash;
64 	union {
65 		GElf_Rela rela;
66 		GElf_Rel  rel;
67 	};
68 	struct section *sec;
69 	struct symbol *sym;
70 	unsigned long offset;
71 	unsigned int type;
72 	int addend;
73 	int idx;
74 	bool jump_table_start;
75 };
76 
77 #define ELF_HASH_BITS	20
78 
79 struct elf {
80 	Elf *elf;
81 	GElf_Ehdr ehdr;
82 	int fd;
83 	bool changed;
84 	char *name;
85 	unsigned int text_size;
86 	struct list_head sections;
87 
88 	int symbol_bits;
89 	int symbol_name_bits;
90 	int section_bits;
91 	int section_name_bits;
92 	int reloc_bits;
93 
94 	struct hlist_head *symbol_hash;
95 	struct hlist_head *symbol_name_hash;
96 	struct hlist_head *section_hash;
97 	struct hlist_head *section_name_hash;
98 	struct hlist_head *reloc_hash;
99 };
100 
101 #define OFFSET_STRIDE_BITS	4
102 #define OFFSET_STRIDE		(1UL << OFFSET_STRIDE_BITS)
103 #define OFFSET_STRIDE_MASK	(~(OFFSET_STRIDE - 1))
104 
105 #define for_offset_range(_offset, _start, _end)			\
106 	for (_offset = ((_start) & OFFSET_STRIDE_MASK);		\
107 	     _offset >= ((_start) & OFFSET_STRIDE_MASK) &&	\
108 	     _offset <= ((_end) & OFFSET_STRIDE_MASK);		\
109 	     _offset += OFFSET_STRIDE)
110 
sec_offset_hash(struct section * sec,unsigned long offset)111 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
112 {
113 	u32 ol, oh, idx = sec->idx;
114 
115 	offset &= OFFSET_STRIDE_MASK;
116 
117 	ol = offset;
118 	oh = (offset >> 16) >> 16;
119 
120 	__jhash_mix(ol, oh, idx);
121 
122 	return ol;
123 }
124 
reloc_hash(struct reloc * reloc)125 static inline u32 reloc_hash(struct reloc *reloc)
126 {
127 	return sec_offset_hash(reloc->sec, reloc->offset);
128 }
129 
130 struct elf *elf_open_read(const char *name, int flags);
131 struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr);
132 
133 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
134 		  unsigned int type, struct symbol *sym, int addend);
135 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
136 			  unsigned long offset, unsigned int type,
137 			  struct section *insn_sec, unsigned long insn_off);
138 
139 int elf_write_insn(struct elf *elf, struct section *sec,
140 		   unsigned long offset, unsigned int len,
141 		   const char *insn);
142 int elf_write_reloc(struct elf *elf, struct reloc *reloc);
143 struct symbol *elf_create_undef_symbol(struct elf *elf, const char *name);
144 int elf_write(struct elf *elf);
145 void elf_close(struct elf *elf);
146 
147 struct section *find_section_by_name(const struct elf *elf, const char *name);
148 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
149 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
150 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
151 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
152 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
153 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
154 				     unsigned long offset, unsigned int len);
155 struct symbol *find_func_containing(struct section *sec, unsigned long offset);
156 
157 #define for_each_sec(file, sec)						\
158 	list_for_each_entry(sec, &file->elf->sections, list)
159 
160 #endif /* _OBJTOOL_ELF_H */
161