.. | .. |
---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-or-later */ |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> |
---|
3 | | - * |
---|
4 | | - * This program is free software; you can redistribute it and/or |
---|
5 | | - * modify it under the terms of the GNU General Public License |
---|
6 | | - * as published by the Free Software Foundation; either version 2 |
---|
7 | | - * of the License, or (at your option) any later version. |
---|
8 | | - * |
---|
9 | | - * This program is distributed in the hope that it will be useful, |
---|
10 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | | - * GNU General Public License for more details. |
---|
13 | | - * |
---|
14 | | - * You should have received a copy of the GNU General Public License |
---|
15 | | - * along with this program; if not, see <http://www.gnu.org/licenses/>. |
---|
16 | 4 | */ |
---|
17 | 5 | |
---|
18 | 6 | #ifndef _OBJTOOL_ELF_H |
---|
.. | .. |
---|
22 | 10 | #include <gelf.h> |
---|
23 | 11 | #include <linux/list.h> |
---|
24 | 12 | #include <linux/hashtable.h> |
---|
| 13 | +#include <linux/rbtree.h> |
---|
| 14 | +#include <linux/jhash.h> |
---|
25 | 15 | |
---|
26 | 16 | #ifdef LIBELF_USE_DEPRECATED |
---|
27 | 17 | # define elf_getshdrnum elf_getshnum |
---|
.. | .. |
---|
37 | 27 | |
---|
38 | 28 | struct section { |
---|
39 | 29 | struct list_head list; |
---|
| 30 | + struct hlist_node hash; |
---|
| 31 | + struct hlist_node name_hash; |
---|
40 | 32 | GElf_Shdr sh; |
---|
| 33 | + struct rb_root symbol_tree; |
---|
41 | 34 | struct list_head symbol_list; |
---|
42 | | - DECLARE_HASHTABLE(symbol_hash, 8); |
---|
43 | | - struct list_head rela_list; |
---|
44 | | - DECLARE_HASHTABLE(rela_hash, 16); |
---|
45 | | - struct section *base, *rela; |
---|
| 35 | + struct list_head reloc_list; |
---|
| 36 | + struct section *base, *reloc; |
---|
46 | 37 | struct symbol *sym; |
---|
47 | 38 | Elf_Data *data; |
---|
48 | 39 | char *name; |
---|
49 | 40 | int idx; |
---|
50 | 41 | unsigned int len; |
---|
51 | | - bool changed, text, rodata; |
---|
| 42 | + bool changed, text, rodata, noinstr; |
---|
52 | 43 | }; |
---|
53 | 44 | |
---|
54 | 45 | struct symbol { |
---|
55 | 46 | struct list_head list; |
---|
| 47 | + struct rb_node node; |
---|
56 | 48 | struct hlist_node hash; |
---|
| 49 | + struct hlist_node name_hash; |
---|
57 | 50 | GElf_Sym sym; |
---|
58 | 51 | struct section *sec; |
---|
59 | 52 | char *name; |
---|
.. | .. |
---|
61 | 54 | unsigned char bind, type; |
---|
62 | 55 | unsigned long offset; |
---|
63 | 56 | unsigned int len; |
---|
64 | | - struct symbol *pfunc, *cfunc; |
---|
| 57 | + struct symbol *pfunc, *cfunc, *alias; |
---|
| 58 | + u8 uaccess_safe : 1; |
---|
| 59 | + u8 static_call_tramp : 1; |
---|
| 60 | + u8 retpoline_thunk : 1; |
---|
| 61 | + u8 return_thunk : 1; |
---|
| 62 | + u8 fentry : 1; |
---|
| 63 | + u8 kcov : 1; |
---|
| 64 | + u8 embedded_insn : 1; |
---|
65 | 65 | }; |
---|
66 | 66 | |
---|
67 | | -struct rela { |
---|
| 67 | +struct reloc { |
---|
68 | 68 | struct list_head list; |
---|
69 | 69 | struct hlist_node hash; |
---|
70 | | - GElf_Rela rela; |
---|
71 | | - struct section *rela_sec; |
---|
| 70 | + union { |
---|
| 71 | + GElf_Rela rela; |
---|
| 72 | + GElf_Rel rel; |
---|
| 73 | + }; |
---|
| 74 | + struct section *sec; |
---|
72 | 75 | struct symbol *sym; |
---|
73 | | - unsigned int type; |
---|
74 | 76 | unsigned long offset; |
---|
75 | | - int addend; |
---|
| 77 | + unsigned int type; |
---|
| 78 | + s64 addend; |
---|
| 79 | + int idx; |
---|
| 80 | + bool jump_table_start; |
---|
76 | 81 | }; |
---|
| 82 | + |
---|
| 83 | +#define ELF_HASH_BITS 20 |
---|
77 | 84 | |
---|
78 | 85 | struct elf { |
---|
79 | 86 | Elf *elf; |
---|
80 | 87 | GElf_Ehdr ehdr; |
---|
81 | 88 | int fd; |
---|
| 89 | + bool changed; |
---|
82 | 90 | char *name; |
---|
83 | 91 | struct list_head sections; |
---|
84 | | - DECLARE_HASHTABLE(rela_hash, 16); |
---|
| 92 | + DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS); |
---|
| 93 | + DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS); |
---|
| 94 | + DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS); |
---|
| 95 | + DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS); |
---|
| 96 | + DECLARE_HASHTABLE(reloc_hash, ELF_HASH_BITS); |
---|
85 | 97 | }; |
---|
86 | 98 | |
---|
| 99 | +#define OFFSET_STRIDE_BITS 4 |
---|
| 100 | +#define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) |
---|
| 101 | +#define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1)) |
---|
87 | 102 | |
---|
88 | | -struct elf *elf_open(const char *name, int flags); |
---|
89 | | -struct section *find_section_by_name(struct elf *elf, const char *name); |
---|
90 | | -struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset); |
---|
91 | | -struct symbol *find_symbol_by_name(struct elf *elf, const char *name); |
---|
92 | | -struct symbol *find_symbol_containing(struct section *sec, unsigned long offset); |
---|
93 | | -struct rela *find_rela_by_dest(struct section *sec, unsigned long offset); |
---|
94 | | -struct rela *find_rela_by_dest_range(struct section *sec, unsigned long offset, |
---|
95 | | - unsigned int len); |
---|
96 | | -struct symbol *find_containing_func(struct section *sec, unsigned long offset); |
---|
97 | | -struct section *elf_create_section(struct elf *elf, const char *name, size_t |
---|
98 | | - entsize, int nr); |
---|
99 | | -struct section *elf_create_rela_section(struct elf *elf, struct section *base); |
---|
100 | | -int elf_rebuild_rela_section(struct section *sec); |
---|
| 103 | +#define for_offset_range(_offset, _start, _end) \ |
---|
| 104 | + for (_offset = ((_start) & OFFSET_STRIDE_MASK); \ |
---|
| 105 | + _offset >= ((_start) & OFFSET_STRIDE_MASK) && \ |
---|
| 106 | + _offset <= ((_end) & OFFSET_STRIDE_MASK); \ |
---|
| 107 | + _offset += OFFSET_STRIDE) |
---|
| 108 | + |
---|
| 109 | +static inline u32 sec_offset_hash(struct section *sec, unsigned long offset) |
---|
| 110 | +{ |
---|
| 111 | + u32 ol, oh, idx = sec->idx; |
---|
| 112 | + |
---|
| 113 | + offset &= OFFSET_STRIDE_MASK; |
---|
| 114 | + |
---|
| 115 | + ol = offset; |
---|
| 116 | + oh = (offset >> 16) >> 16; |
---|
| 117 | + |
---|
| 118 | + __jhash_mix(ol, oh, idx); |
---|
| 119 | + |
---|
| 120 | + return ol; |
---|
| 121 | +} |
---|
| 122 | + |
---|
| 123 | +static inline u32 reloc_hash(struct reloc *reloc) |
---|
| 124 | +{ |
---|
| 125 | + return sec_offset_hash(reloc->sec, reloc->offset); |
---|
| 126 | +} |
---|
| 127 | + |
---|
| 128 | +struct elf *elf_open_read(const char *name, int flags); |
---|
| 129 | +struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr); |
---|
| 130 | + |
---|
| 131 | +int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, |
---|
| 132 | + unsigned int type, struct symbol *sym, s64 addend); |
---|
| 133 | +int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, |
---|
| 134 | + unsigned long offset, unsigned int type, |
---|
| 135 | + struct section *insn_sec, unsigned long insn_off); |
---|
| 136 | + |
---|
| 137 | +int elf_write_insn(struct elf *elf, struct section *sec, |
---|
| 138 | + unsigned long offset, unsigned int len, |
---|
| 139 | + const char *insn); |
---|
| 140 | +int elf_write_reloc(struct elf *elf, struct reloc *reloc); |
---|
101 | 141 | int elf_write(struct elf *elf); |
---|
102 | 142 | void elf_close(struct elf *elf); |
---|
103 | 143 | |
---|
| 144 | +struct section *find_section_by_name(const struct elf *elf, const char *name); |
---|
| 145 | +struct symbol *find_func_by_offset(struct section *sec, unsigned long offset); |
---|
| 146 | +struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset); |
---|
| 147 | +struct symbol *find_symbol_by_name(const struct elf *elf, const char *name); |
---|
| 148 | +struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset); |
---|
| 149 | +struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset); |
---|
| 150 | +struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, |
---|
| 151 | + unsigned long offset, unsigned int len); |
---|
| 152 | +struct symbol *find_func_containing(struct section *sec, unsigned long offset); |
---|
| 153 | + |
---|
104 | 154 | #define for_each_sec(file, sec) \ |
---|
105 | 155 | list_for_each_entry(sec, &file->elf->sections, list) |
---|
106 | 156 | |
---|