Merge tag 'rcu_urgent_for_5.8_rc3' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / tools / objtool / elf.h
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 rela_list;
36         struct section *base, *rela;
37         struct symbol *sym;
38         Elf_Data *data;
39         char *name;
40         int idx;
41         unsigned int len;
42         bool changed, text, rodata, noinstr;
43 };
44
45 struct symbol {
46         struct list_head list;
47         struct rb_node node;
48         struct hlist_node hash;
49         struct hlist_node name_hash;
50         GElf_Sym sym;
51         struct section *sec;
52         char *name;
53         unsigned int idx;
54         unsigned char bind, type;
55         unsigned long offset;
56         unsigned int len;
57         struct symbol *pfunc, *cfunc, *alias;
58         bool uaccess_safe;
59 };
60
61 struct rela {
62         struct list_head list;
63         struct hlist_node hash;
64         GElf_Rela rela;
65         struct section *sec;
66         struct symbol *sym;
67         unsigned long offset;
68         unsigned int type;
69         int addend;
70         int idx;
71         bool jump_table_start;
72 };
73
74 #define ELF_HASH_BITS   20
75
76 struct elf {
77         Elf *elf;
78         GElf_Ehdr ehdr;
79         int fd;
80         bool changed;
81         char *name;
82         struct list_head sections;
83         DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
84         DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS);
85         DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS);
86         DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS);
87         DECLARE_HASHTABLE(rela_hash, ELF_HASH_BITS);
88 };
89
90 #define OFFSET_STRIDE_BITS      4
91 #define OFFSET_STRIDE           (1UL << OFFSET_STRIDE_BITS)
92 #define OFFSET_STRIDE_MASK      (~(OFFSET_STRIDE - 1))
93
94 #define for_offset_range(_offset, _start, _end)                 \
95         for (_offset = ((_start) & OFFSET_STRIDE_MASK);         \
96              _offset >= ((_start) & OFFSET_STRIDE_MASK) &&      \
97              _offset <= ((_end) & OFFSET_STRIDE_MASK);          \
98              _offset += OFFSET_STRIDE)
99
100 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
101 {
102         u32 ol, oh, idx = sec->idx;
103
104         offset &= OFFSET_STRIDE_MASK;
105
106         ol = offset;
107         oh = (offset >> 16) >> 16;
108
109         __jhash_mix(ol, oh, idx);
110
111         return ol;
112 }
113
114 static inline u32 rela_hash(struct rela *rela)
115 {
116         return sec_offset_hash(rela->sec, rela->offset);
117 }
118
119 struct elf *elf_open_read(const char *name, int flags);
120 struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
121 struct section *elf_create_rela_section(struct elf *elf, struct section *base);
122 void elf_add_rela(struct elf *elf, struct rela *rela);
123 int elf_write_insn(struct elf *elf, struct section *sec,
124                    unsigned long offset, unsigned int len,
125                    const char *insn);
126 int elf_write_rela(struct elf *elf, struct rela *rela);
127 int elf_write(struct elf *elf);
128 void elf_close(struct elf *elf);
129
130 struct section *find_section_by_name(const struct elf *elf, const char *name);
131 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
132 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
133 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
134 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
135 struct rela *find_rela_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
136 struct rela *find_rela_by_dest_range(const struct elf *elf, struct section *sec,
137                                      unsigned long offset, unsigned int len);
138 struct symbol *find_func_containing(struct section *sec, unsigned long offset);
139 int elf_rebuild_rela_section(struct elf *elf, struct section *sec);
140
141 #define for_each_sec(file, sec)                                         \
142         list_for_each_entry(sec, &file->elf->sections, list)
143
144 #endif /* _OBJTOOL_ELF_H */