• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2020 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 <gelf.h>
17 #include <string>
18 
19 #include "abg-ir.h"
20 
21 namespace abigail
22 {
23 
24 namespace elf_helpers
25 {
26 
27 //
28 // ELF Value Converters
29 //
30 
31 elf_symbol::type
32 stt_to_elf_symbol_type(unsigned char stt);
33 
34 elf_symbol::binding
35 stb_to_elf_symbol_binding(unsigned char stb);
36 
37 elf_symbol::visibility
38 stv_to_elf_symbol_visibility(unsigned char stv);
39 
40 std::string
41 e_machine_to_string(GElf_Half e_machine);
42 
43 //
44 // ELF section helpers
45 //
46 
47 Elf_Scn*
48 find_section(Elf*		elf_handle,
49 	     const std::string& name,
50 	     Elf64_Word		section_type);
51 
52 Elf_Scn*
53 find_section_by_name(Elf* elf_handle, const std::string& name);
54 
55 Elf_Scn*
56 find_section(Elf* elf_handle, Elf64_Word section_type);
57 
58 Elf_Scn*
59 find_symtab_section(Elf* elf_handle);
60 
61 Elf_Scn*
62 find_dynsym_section(Elf* elf_handle);
63 
64 Elf_Scn*
65 find_symbol_table_section(Elf* elf_handle);
66 
67 bool
68 find_symbol_table_section_index(Elf* elf_handle, size_t& symtab_index);
69 
70 enum hash_table_kind
71 {
72   NO_HASH_TABLE_KIND = 0,
73   SYSV_HASH_TABLE_KIND,
74   GNU_HASH_TABLE_KIND
75 };
76 
77 hash_table_kind
78 find_hash_table_section_index(Elf*	elf_handle,
79 			      size_t&	ht_section_index,
80 			      size_t&	symtab_section_index);
81 
82 Elf_Scn*
83 find_text_section(Elf* elf_handle);
84 
85 Elf_Scn*
86 find_bss_section(Elf* elf_handle);
87 
88 Elf_Scn*
89 find_rodata_section(Elf* elf_handle);
90 
91 Elf_Scn*
92 find_data_section(Elf* elf_handle);
93 
94 Elf_Scn*
95 find_data1_section(Elf* elf_handle);
96 
97 Elf_Scn*
98 find_opd_section(Elf* elf_handle);
99 
100 bool
101 get_symbol_versionning_sections(Elf*		elf_handle,
102 				Elf_Scn*&	versym_section,
103 				Elf_Scn*&	verdef_section,
104 				Elf_Scn*&	verneed_section);
105 
106 Elf_Scn*
107 find_ksymtab_section(Elf* elf_handle);
108 
109 Elf_Scn*
110 find_ksymtab_gpl_section(Elf* elf_handle);
111 
112 Elf_Scn*
113 find_ksymtab_strings_section(Elf *elf_handle);
114 
115 Elf_Scn*
116 find_relocation_section(Elf* elf_handle, Elf_Scn* target_section);
117 
118 Elf_Scn*
119 find_strtab_for_symtab_section(Elf*	elf_handle,
120                                Elf_Scn*	symtab_section);
121 
122 //
123 // Helpers for symbol versioning
124 //
125 
126 bool
127 get_version_definition_for_versym(Elf*			 elf_handle,
128 				  GElf_Versym*		 versym,
129 				  Elf_Scn*		 verdef_section,
130 				  elf_symbol::version&	 version);
131 
132 bool
133 get_version_needed_for_versym(Elf*			elf_handle,
134 			      GElf_Versym*		versym,
135 			      Elf_Scn*			verneed_section,
136 			      elf_symbol::version&	version);
137 
138 bool
139 get_version_for_symbol(Elf*			elf_handle,
140 		       size_t			symbol_index,
141 		       bool			get_def_version,
142 		       elf_symbol::version&	version);
143 
144 bool
145 get_crc_for_symbol(Elf* elf_handle, GElf_Sym* crc_symbol, uint32_t& crc_value);
146 
147 //
148 // Architecture specific helpers
149 //
150 bool
151 architecture_is_ppc64(Elf* elf_handle);
152 
153 bool
154 architecture_is_ppc32(Elf* elf_handle);
155 
156 bool
157 architecture_is_arm32(Elf* elf_handle);
158 
159 bool
160 architecture_is_arm64(Elf* elf_handle);
161 
162 bool
163 architecture_is_big_endian(Elf* elf_handle);
164 
165 GElf_Addr
166 lookup_ppc64_elf_fn_entry_point_address(Elf*	  elf_handle,
167 					GElf_Addr fn_desc_address);
168 
169 //
170 // Helpers for Linux Kernel Binaries
171 //
172 
173 bool
174 is_linux_kernel_module(Elf *elf_handle);
175 
176 bool
177 is_linux_kernel(Elf *elf_handle);
178 
179 //
180 // Misc Helpers
181 //
182 
183 bool
184 get_binary_load_address(Elf* elf_handle, GElf_Addr& load_address);
185 
186 unsigned char
187 get_architecture_word_size(Elf* elf_handle);
188 
189 bool
190 is_executable(Elf* elf_handle);
191 
192 bool
193 is_dso(Elf* elf_handle);
194 
195 GElf_Addr
196 maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym);
197 
198 bool
199 address_is_in_opd_section(Elf* elf_handle, Dwarf_Addr addr);
200 
201 } // end namespace elf_helpers
202 } // end namespace abigail
203 
204 #endif // __ABG_ELF_HELPERS_H__
205