libabigail
abg-elf-helpers.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2020-2025 Google, Inc.
5 
6 /// @file
7 ///
8 /// This contains a set of ELF utilities used by the dwarf reader.
9 
10 #ifndef __ABG_ELF_HELPERS_H__
11 #define __ABG_ELF_HELPERS_H__
12 
13 #include "config.h"
14 
15 #include <elfutils/libdwfl.h>
16 #include <elfutils/libdwelf.h>
17 #include <gelf.h>
18 #include <string>
19 
20 #include "abg-ir.h"
21 
22 namespace abigail
23 {
24 
25 namespace elf_helpers
26 {
27 
28 /// A functor used by @ref dwfl_sptr.
30 {
31  void
32  operator()(Dwfl* dwfl)
33  {dwfl_end(dwfl);}
34 };//end struct dwfl_deleter
35 
36 /// A convenience typedef for a shared pointer to a Dwfl.
37 typedef shared_ptr<Dwfl> dwfl_sptr;
38 
39 /// Convenience typedef for a map which key is an elf address and
40 /// which value is an elf_symbol_sptr.
41 typedef unordered_map<GElf_Addr, elf_symbol_sptr> addr_elf_symbol_sptr_map_type;
42 
43 /// Convenience typedef for a set of ELF addresses.
44 typedef unordered_set<GElf_Addr> address_set_type;
45 
46 /// Convenience typedef for a shared pointer to an @ref address_set_type.
47 typedef shared_ptr<address_set_type> address_set_sptr;
48 
49 //
50 // ELF Value Converters
51 //
52 
54 stt_to_elf_symbol_type(unsigned char stt);
55 
57 stb_to_elf_symbol_binding(unsigned char stb);
58 
60 stv_to_elf_symbol_visibility(unsigned char stv);
61 
62 std::string
63 e_machine_to_string(GElf_Half e_machine);
64 
65 //
66 // ELF section helpers
67 //
68 
69 Elf_Scn*
70 find_section(Elf* elf_handle,
71  const std::string& name,
72  Elf64_Word section_type);
73 
74 Elf_Scn*
75 find_section_by_name(Elf* elf_handle, const std::string& name);
76 
77 Elf_Scn*
78 find_section(Elf* elf_handle, Elf64_Word section_type);
79 
80 Elf_Scn*
81 find_symtab_section(Elf* elf_handle);
82 
83 Elf_Scn*
84 find_dynsym_section(Elf* elf_handle);
85 
86 Elf_Scn*
87 find_symbol_table_section(Elf* elf_handle);
88 
89 bool
90 find_symbol_table_section_index(Elf* elf_handle, size_t& symtab_index);
91 
92 enum hash_table_kind
93 {
94  NO_HASH_TABLE_KIND = 0,
95  SYSV_HASH_TABLE_KIND,
96  GNU_HASH_TABLE_KIND
97 };
98 
99 hash_table_kind
100 find_hash_table_section_index(Elf* elf_handle,
101  size_t& ht_section_index,
102  size_t& symtab_section_index);
103 
104 Elf_Scn*
105 find_text_section(Elf* elf_handle);
106 
107 Elf_Scn*
108 find_bss_section(Elf* elf_handle);
109 
110 Elf_Scn*
111 find_rodata_section(Elf* elf_handle);
112 
113 Elf_Scn*
114 find_data_section(Elf* elf_handle);
115 
116 Elf_Scn*
117 find_data1_section(Elf* elf_handle);
118 
119 Elf_Scn*
120 find_opd_section(Elf* elf_handle);
121 
122 bool
123 get_symbol_versionning_sections(Elf* elf_handle,
124  Elf_Scn*& versym_section,
125  Elf_Scn*& verdef_section,
126  Elf_Scn*& verneed_section);
127 
128 Elf_Scn*
129 find_ksymtab_section(Elf* elf_handle);
130 
131 Elf_Scn*
132 find_ksymtab_gpl_section(Elf* elf_handle);
133 
134 Elf_Scn*
135 find_ksymtab_strings_section(Elf *elf_handle);
136 
137 Elf_Scn*
138 find_relocation_section(Elf* elf_handle, Elf_Scn* target_section);
139 
140 Elf_Scn*
141 find_strtab_for_symtab_section(Elf* elf_handle,
142  Elf_Scn* symtab_section);
143 
144 //
145 // Helpers for symbol versioning
146 //
147 
148 bool
149 get_version_definition_for_versym(Elf* elf_handle,
150  GElf_Versym* versym,
151  Elf_Scn* verdef_section,
152  elf_symbol::version& version);
153 
154 bool
155 get_version_needed_for_versym(Elf* elf_handle,
156  GElf_Versym* versym,
157  Elf_Scn* verneed_section,
158  elf_symbol::version& version);
159 
160 bool
161 get_version_for_symbol(Elf* elf_handle,
162  size_t symbol_index,
163  bool get_def_version,
164  elf_symbol::version& version);
165 
166 bool
167 get_crc_for_symbol(Elf* elf_handle, GElf_Sym* crc_symbol, uint32_t& crc_value);
168 
169 //
170 // Architecture specific helpers
171 //
172 bool
173 architecture_is_ppc64(Elf* elf_handle);
174 
175 bool
176 architecture_is_ppc32(Elf* elf_handle);
177 
178 bool
179 architecture_is_arm32(Elf* elf_handle);
180 
181 bool
182 architecture_is_arm64(Elf* elf_handle);
183 
184 bool
185 architecture_is_big_endian(Elf* elf_handle);
186 
187 GElf_Addr
188 lookup_ppc64_elf_fn_entry_point_address(Elf* elf_handle,
189  GElf_Addr fn_desc_address);
190 
191 //
192 // Helpers for Linux Kernel Binaries
193 //
194 
195 bool
196 is_linux_kernel_module(Elf *elf_handle);
197 
198 bool
199 is_linux_kernel(Elf *elf_handle);
200 
201 //
202 // elfutils helpers
203 //
204 
205 const Dwfl_Callbacks&
206 initialize_dwfl_callbacks(Dwfl_Callbacks&, char**);
207 
208 dwfl_sptr
209 create_new_dwfl_handle(Dwfl_Callbacks&);
210 
211 //
212 // Misc Helpers
213 //
214 
215 bool
216 get_binary_load_address(Elf* elf_handle, GElf_Addr& load_address);
217 
218 unsigned char
219 get_architecture_word_size(Elf* elf_handle);
220 
221 bool
222 is_executable(Elf* elf_handle);
223 
224 bool
225 is_dso(Elf* elf_handle);
226 
227 GElf_Addr
228 maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym);
229 
230 bool
231 address_is_in_opd_section(Elf* elf_handle, Dwarf_Addr addr);
232 
233 bool
234 lookup_data_tag_from_dynamic_segment(Elf* elf,
235  Elf64_Sxword data_tag,
236  vector<string>& dt_tag_data);
237 
238 bool
239 get_soname_of_elf_file(const string& path, string &soname);
240 } // end namespace elf_helpers
241 } // end namespace abigail
242 
243 #endif // __ABG_ELF_HELPERS_H__
visibility
The visibility of the symbol.
Definition: abg-ir.h:986
Toplevel namespace for libabigail.
Types of the main internal representation of libabigail.
A functor used by dwfl_sptr.
type
The type of a symbol.
Definition: abg-ir.h:964
binding
The binding of a symbol.
Definition: abg-ir.h:977
The abstraction of the version of an ELF symbol.
Definition: abg-ir.h:1231