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 rela_list
;
36 struct section
*base
, *rela
;
42 bool changed
, text
, rodata
;
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
;
70 bool jump_table_start
;
78 struct list_head sections
;
79 DECLARE_HASHTABLE(symbol_hash
, 20);
80 DECLARE_HASHTABLE(symbol_name_hash
, 20);
81 DECLARE_HASHTABLE(section_hash
, 16);
82 DECLARE_HASHTABLE(section_name_hash
, 16);
83 DECLARE_HASHTABLE(rela_hash
, 20);
86 #define OFFSET_STRIDE_BITS 4
87 #define OFFSET_STRIDE (1UL << OFFSET_STRIDE_BITS)
88 #define OFFSET_STRIDE_MASK (~(OFFSET_STRIDE - 1))
90 #define for_offset_range(_offset, _start, _end) \
91 for (_offset = ((_start) & OFFSET_STRIDE_MASK); \
92 _offset >= ((_start) & OFFSET_STRIDE_MASK) && \
93 _offset <= ((_end) & OFFSET_STRIDE_MASK); \
94 _offset += OFFSET_STRIDE)
96 static inline u32
sec_offset_hash(struct section
*sec
, unsigned long offset
)
98 u32 ol
, oh
, idx
= sec
->idx
;
100 offset
&= OFFSET_STRIDE_MASK
;
103 oh
= (offset
>> 16) >> 16;
105 __jhash_mix(ol
, oh
, idx
);
110 static inline u32
rela_hash(struct rela
*rela
)
112 return sec_offset_hash(rela
->sec
, rela
->offset
);
115 struct elf
*elf_read(const char *name
, int flags
);
116 struct section
*find_section_by_name(struct elf
*elf
, const char *name
);
117 struct symbol
*find_func_by_offset(struct section
*sec
, unsigned long offset
);
118 struct symbol
*find_symbol_by_offset(struct section
*sec
, unsigned long offset
);
119 struct symbol
*find_symbol_by_name(struct elf
*elf
, const char *name
);
120 struct symbol
*find_symbol_containing(struct section
*sec
, unsigned long offset
);
121 struct rela
*find_rela_by_dest(struct elf
*elf
, struct section
*sec
, unsigned long offset
);
122 struct rela
*find_rela_by_dest_range(struct elf
*elf
, struct section
*sec
,
123 unsigned long offset
, unsigned int len
);
124 struct symbol
*find_func_containing(struct section
*sec
, unsigned long offset
);
125 struct section
*elf_create_section(struct elf
*elf
, const char *name
, size_t
127 struct section
*elf_create_rela_section(struct elf
*elf
, struct section
*base
);
128 int elf_rebuild_rela_section(struct section
*sec
);
129 int elf_write(struct elf
*elf
);
130 void elf_close(struct elf
*elf
);
132 #define for_each_sec(file, sec) \
133 list_for_each_entry(sec, &file->elf->sections, list)
135 #endif /* _OBJTOOL_ELF_H */