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