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;
41 bool changed, text, rodata, noinstr;
45 struct list_head list;
47 struct hlist_node hash;
48 struct hlist_node name_hash;
53 unsigned char bind, type;
56 struct symbol *pfunc, *cfunc, *alias;
58 u8 static_call_tramp : 1;
59 u8 retpoline_thunk : 1;
62 u8 profiling_func : 1;
64 struct list_head pv_target;
68 struct list_head list;
69 struct hlist_node hash;
80 bool jump_table_start;
83 #define ELF_HASH_BITS 20
91 unsigned int text_size, num_files;
92 struct list_head sections;
97 int section_name_bits;
100 struct hlist_head *symbol_hash;
101 struct hlist_head *symbol_name_hash;
102 struct hlist_head *section_hash;
103 struct hlist_head *section_name_hash;
104 struct hlist_head *reloc_hash;
107 #define OFFSET_STRIDE_BITS 4
108 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
109 #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
111 #define for_offset_range(_offset, _start, _end) \
112 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
113 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \
114 _offset <= ((_end) & OFFSET_STRIDE_MASK); \
115 _offset += OFFSET_STRIDE)
117 static inline u32 sec_offset_hash(struct section *sec, unsigned long offset)
119 u32 ol, oh, idx = sec->idx;
121 offset &= OFFSET_STRIDE_MASK;
124 oh = (offset >> 16) >> 16;
126 __jhash_mix(ol, oh, idx);
131 static inline u32 reloc_hash(struct reloc *reloc)
133 return sec_offset_hash(reloc->sec, reloc->offset);
137 * Try to see if it's a whole archive (vmlinux.o or module).
139 * Note this will miss the case where a module only has one source file.
141 static inline bool has_multiple_files(struct elf *elf)
143 return elf->num_files > 1;
146 struct elf *elf_open_read(const char *name, int flags);
147 struct section *elf_create_section(struct elf *elf, const char *name, unsigned int sh_flags, size_t entsize, int nr);
149 int elf_add_reloc(struct elf *elf, struct section *sec, unsigned long offset,
150 unsigned int type, struct symbol *sym, s64 addend);
151 int elf_add_reloc_to_insn(struct elf *elf, struct section *sec,
152 unsigned long offset, unsigned int type,
153 struct section *insn_sec, unsigned long insn_off);
155 int elf_write_insn(struct elf *elf, struct section *sec,
156 unsigned long offset, unsigned int len,
158 int elf_write_reloc(struct elf *elf, struct reloc *reloc);
159 int elf_write(struct elf *elf);
160 void elf_close(struct elf *elf);
162 struct section *find_section_by_name(const struct elf *elf, const char *name);
163 struct symbol *find_func_by_offset(struct section *sec, unsigned long offset);
164 struct symbol *find_symbol_by_offset(struct section *sec, unsigned long offset);
165 struct symbol *find_symbol_by_name(const struct elf *elf, const char *name);
166 struct symbol *find_symbol_containing(const struct section *sec, unsigned long offset);
167 int find_symbol_hole_containing(const struct section *sec, unsigned long offset);
168 struct reloc *find_reloc_by_dest(const struct elf *elf, struct section *sec, unsigned long offset);
169 struct reloc *find_reloc_by_dest_range(const struct elf *elf, struct section *sec,
170 unsigned long offset, unsigned int len);
171 struct symbol *find_func_containing(struct section *sec, unsigned long offset);
173 #define for_each_sec(file, sec) \
174 list_for_each_entry(sec, &file->elf->sections, list)
176 #endif /* _OBJTOOL_ELF_H */