David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 13 | #include <linux/rbtree.h> |
| 14 | #include <linux/jhash.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 30 | struct hlist_node hash; |
| 31 | struct hlist_node name_hash; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 32 | GElf_Shdr sh; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 33 | struct rb_root symbol_tree; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 34 | struct list_head symbol_list; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 35 | struct list_head reloc_list; |
| 36 | struct section *base, *reloc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 37 | struct symbol *sym; |
| 38 | Elf_Data *data; |
| 39 | char *name; |
| 40 | int idx; |
| 41 | unsigned int len; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 42 | bool changed, text, rodata, noinstr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct symbol { |
| 46 | struct list_head list; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 47 | struct rb_node node; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 48 | struct hlist_node hash; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 49 | struct hlist_node name_hash; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 57 | struct symbol *pfunc, *cfunc, *alias; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 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; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 66 | struct reloc { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | struct list_head list; |
| 68 | struct hlist_node hash; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 69 | union { |
| 70 | GElf_Rela rela; |
| 71 | GElf_Rel rel; |
| 72 | }; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 73 | struct section *sec; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 74 | struct symbol *sym; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | unsigned long offset; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 76 | unsigned int type; |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 77 | s64 addend; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 78 | int idx; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 79 | bool jump_table_start; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 82 | #define ELF_HASH_BITS 20 |
| 83 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 84 | struct elf { |
| 85 | Elf *elf; |
| 86 | GElf_Ehdr ehdr; |
| 87 | int fd; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 88 | bool changed; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 89 | char *name; |
| 90 | struct list_head sections; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 91 | DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS); |
| 92 | DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS); |
| 93 | DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS); |
| 94 | DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS); |
| 95 | DECLARE_HASHTABLE(reloc_hash, ELF_HASH_BITS); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 98 | #define OFFSET_STRIDE_BITS 4 |
| 99 | #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS) |
| 100 | #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 101 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 102 | #define for_offset_range(_offset, _start, _end) \ |
| 103 | for (_offset = ((_start) & OFFSET_STRIDE_MASK); \ |
| 104 | _offset >= ((_start) & OFFSET_STRIDE_MASK) && \ |
| 105 | _offset <= ((_end) & OFFSET_STRIDE_MASK); \ |
| 106 | _offset += OFFSET_STRIDE) |
| 107 | |
| 108 | static inline u32 sec_offset_hash(struct section *sec, unsigned long offset) |
| 109 | { |
| 110 | u32 ol, oh, idx = sec->idx; |
| 111 | |
| 112 | offset &= OFFSET_STRIDE_MASK; |
| 113 | |
| 114 | ol = offset; |
| 115 | oh = (offset >> 16) >> 16; |
| 116 | |
| 117 | __jhash_mix(ol, oh, idx); |
| 118 | |
| 119 | return ol; |
| 120 | } |
| 121 | |
| 122 | static inline u32 reloc_hash(struct reloc *reloc) |
| 123 | { |
| 124 | return sec_offset_hash(reloc->sec, reloc->offset); |
| 125 | } |
| 126 | |
| 127 | struct elf *elf_open_read(const char *name, int flags); |
| 128 | struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr); |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 129 | |
| 130 | int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset, |
| 131 | unsigned int type, struct symbol *sym, s64 addend); |
| 132 | int elf_add_reloc_to_insn(struct elf *elf, struct section *sec, |
| 133 | unsigned long offset, unsigned int type, |
| 134 | struct section *insn_sec, unsigned long insn_off); |
| 135 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 136 | int elf_write_insn(struct elf *elf, struct section *sec, |
| 137 | unsigned long offset, unsigned int len, |
| 138 | const char *insn); |
| 139 | int elf_write_reloc(struct elf *elf, struct reloc *reloc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 140 | int elf_write(struct elf *elf); |
| 141 | void elf_close(struct elf *elf); |
| 142 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 143 | struct section *find_section_by_name(const struct elf *elf, const char *name); |
| 144 | struct symbol *find_func_by_offset(struct section *sec, unsigned long offset); |
| 145 | struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset); |
| 146 | struct symbol *find_symbol_by_name(const struct elf *elf, const char *name); |
| 147 | struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset); |
| 148 | struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset); |
| 149 | struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec, |
| 150 | unsigned long offset, unsigned int len); |
| 151 | struct symbol *find_func_containing(struct section *sec, unsigned long offset); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 152 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 153 | #define for_each_sec(file, sec) \ |
| 154 | list_for_each_entry(sec, &file->elf->sections, list) |
| 155 | |
| 156 | #endif /* _OBJTOOL_ELF_H */ |