]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - tools/objtool/include/objtool/elf.h
x86,objtool: Create .return_sites
[mirror_ubuntu-jammy-kernel.git] / tools / objtool / include / 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 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 u8 uaccess_safe : 1;
58 u8 static_call_tramp : 1;
59 u8 retpoline_thunk : 1;
60 u8 return_thunk : 1;
61 u8 fentry : 1;
62 u8 kcov : 1;
63 };
64
65 struct reloc {
66 struct list_head list;
67 struct hlist_node hash;
68 union {
69 GElf_Rela rela;
70 GElf_Rel rel;
71 };
72 struct section *sec;
73 struct symbol *sym;
74 unsigned long offset;
75 unsigned int type;
76 s64 addend;
77 int idx;
78 bool jump_table_start;
79 };
80
81 #define ELF_HASH_BITS 20
82
83 struct elf {
84 Elf *elf;
85 GElf_Ehdr ehdr;
86 int fd;
87 bool changed;
88 char *name;
89 unsigned int text_size;
90 struct list_head sections;
91
92 int symbol_bits;
93 int symbol_name_bits;
94 int section_bits;
95 int section_name_bits;
96 int reloc_bits;
97
98 struct hlist_head *symbol_hash;
99 struct hlist_head *symbol_name_hash;
100 struct hlist_head *section_hash;
101 struct hlist_head *section_name_hash;
102 struct hlist_head *reloc_hash;
103 };
104
105 #define OFFSET_STRIDE_BITS 4
106 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
107 #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
108
109 #define for_offset_range(_offset, _start, _end) \
110 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
111 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \
112 _offset <= ((_end) & OFFSET_STRIDE_MASK); \
113 _offset += OFFSET_STRIDE)
114
115 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
116 {
117 u32 ol, oh, idx = sec->idx;
118
119 offset &= OFFSET_STRIDE_MASK;
120
121 ol = offset;
122 oh = (offset >> 16) >> 16;
123
124 __jhash_mix(ol, oh, idx);
125
126 return ol;
127 }
128
129 static inline u32 reloc_hash(struct reloc *reloc)
130 {
131 return sec_offset_hash(reloc->sec, reloc->offset);
132 }
133
134 struct elf *elf_open_read(const char *name, int flags);
135 struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr);
136
137 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
138 unsigned int type, struct symbol *sym, s64 addend);
139 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
140 unsigned long offset, unsigned int type,
141 struct section *insn_sec, unsigned long insn_off);
142
143 int elf_write_insn(struct elf *elf, struct section *sec,
144 unsigned long offset, unsigned int len,
145 const char *insn);
146 int elf_write_reloc(struct elf *elf, struct reloc *reloc);
147 int elf_write(struct elf *elf);
148 void elf_close(struct elf *elf);
149
150 struct section *find_section_by_name(const struct elf *elf, const char *name);
151 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
152 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
153 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
154 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
155 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
156 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
157 unsigned long offset, unsigned int len);
158 struct symbol *find_func_containing(struct section *sec, unsigned long offset);
159
160 #define for_each_sec(file, sec) \
161 list_for_each_entry(sec, &file->elf->sections, list)
162
163 #endif /* _OBJTOOL_ELF_H */