1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
11 #include <linux/list.h>
12 #include <linux/hashtable.h>
13 #include <linux/rbtree.h>
14 #include <linux/jhash.h>
16 #ifdef LIBELF_USE_DEPRECATED
17 # define elf_getshdrnum elf_getshnum
18 # define elf_getshdrstrndx elf_getshstrndx
22 * Fallback for systems without this "read, mmaping if possible" cmd.
24 #ifndef ELF_C_READ_MMAP
25 #define ELF_C_READ_MMAP ELF_C_READ
29 struct list_head list;
30 struct hlist_node hash;
31 struct hlist_node name_hash;
33 struct rb_root symbol_tree;
34 struct list_head symbol_list;
35 struct list_head reloc_list;
36 struct section *base, *reloc;
42 bool changed, text, rodata, noinstr;
46 struct list_head list;
48 struct hlist_node hash;
49 struct hlist_node name_hash;
54 unsigned char bind, type;
57 struct symbol *pfunc, *cfunc, *alias;
62 struct list_head list;
63 struct hlist_node hash;
74 bool jump_table_start;
77 #define ELF_HASH_BITS 20
85 struct list_head sections;
86 DECLARE_HASHTABLE(symbol_hash, ELF_HASH_BITS);
87 DECLARE_HASHTABLE(symbol_name_hash, ELF_HASH_BITS);
88 DECLARE_HASHTABLE(section_hash, ELF_HASH_BITS);
89 DECLARE_HASHTABLE(section_name_hash, ELF_HASH_BITS);
90 DECLARE_HASHTABLE(reloc_hash, ELF_HASH_BITS);
93 #define OFFSET_STRIDE_BITS 4
94 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
95 #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
97 #define for_offset_range(_offset, _start, _end) \
98 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
99 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \
100 _offset <= ((_end) & OFFSET_STRIDE_MASK); \
101 _offset += OFFSET_STRIDE)
103 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
105 u32 ol, oh, idx = sec->idx;
107 offset &= OFFSET_STRIDE_MASK;
110 oh = (offset >> 16) >> 16;
112 __jhash_mix(ol, oh, idx);
117 static inline u32 reloc_hash(struct reloc *reloc)
119 return sec_offset_hash(reloc->sec, reloc->offset);
122 struct elf *elf_open_read(const char *name, int flags);
123 struct section *elf_create_section(struct elf *elf, const char *name, size_t entsize, int nr);
124 struct section *elf_create_reloc_section(struct elf *elf, struct section *base, int reltype);
125 void elf_add_reloc(struct elf *elf, struct reloc *reloc);
126 int elf_write_insn(struct elf *elf, struct section *sec,
127 unsigned long offset, unsigned int len,
129 int elf_write_reloc(struct elf *elf, struct reloc *reloc);
130 int elf_write(struct elf *elf);
131 void elf_close(struct elf *elf);
133 struct section *find_section_by_name(const struct elf *elf, const char *name);
134 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
135 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
136 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
137 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
138 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
139 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
140 unsigned long offset, unsigned int len);
141 struct symbol *find_func_containing(struct section *sec, unsigned long offset);
142 int elf_rebuild_reloc_section(struct elf *elf, struct section *sec);
144 #define for_each_sec(file, sec) \
145 list_for_each_entry(sec, &file->elf->sections, list)
147 #endif /* _OBJTOOL_ELF_H */