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_symbol_table_section(Elf* elf_handle);
54 
55 bool
56 find_symbol_table_section_index(Elf* elf_handle, size_t& symtab_index);
57 
58 enum hash_table_kind
59 {
60   NO_HASH_TABLE_KIND = 0,
61   SYSV_HASH_TABLE_KIND,
62   GNU_HASH_TABLE_KIND
63 };
64 
65 hash_table_kind
66 find_hash_table_section_index(Elf*	elf_handle,
67 			      size_t&	ht_section_index,
68 			      size_t&	symtab_section_index);
69 
70 Elf_Scn*
71 find_text_section(Elf* elf_handle);
72 
73 Elf_Scn*
74 find_bss_section(Elf* elf_handle);
75 
76 Elf_Scn*
77 find_rodata_section(Elf* elf_handle);
78 
79 Elf_Scn*
80 find_data_section(Elf* elf_handle);
81 
82 Elf_Scn*
83 find_data1_section(Elf* elf_handle);
84 
85 Elf_Scn*
86 find_opd_section(Elf* elf_handle);
87 
88 bool
89 get_symbol_versionning_sections(Elf*		elf_handle,
90 				Elf_Scn*&	versym_section,
91 				Elf_Scn*&	verdef_section,
92 				Elf_Scn*&	verneed_section);
93 
94 Elf_Scn*
95 find_ksymtab_section(Elf* elf_handle);
96 
97 Elf_Scn*
98 find_ksymtab_gpl_section(Elf* elf_handle);
99 
100 Elf_Scn*
101 find_ksymtab_strings_section(Elf *elf_handle);
102 
103 Elf_Scn*
104 find_relocation_section(Elf* elf_handle, Elf_Scn* target_section);
105 
106 //
107 // Helpers for symbol versioning
108 //
109 
110 bool
111 get_version_definition_for_versym(Elf*			 elf_handle,
112 				  GElf_Versym*		 versym,
113 				  Elf_Scn*		 verdef_section,
114 				  elf_symbol::version&	 version);
115 
116 bool
117 get_version_needed_for_versym(Elf*			elf_handle,
118 			      GElf_Versym*		versym,
119 			      Elf_Scn*			verneed_section,
120 			      elf_symbol::version&	version);
121 
122 bool
123 get_version_for_symbol(Elf*			elf_handle,
124 		       size_t			symbol_index,
125 		       bool			get_def_version,
126 		       elf_symbol::version&	version);
127 
128 //
129 // Architecture specific helpers
130 //
131 bool
132 architecture_is_ppc64(Elf* elf_handle);
133 
134 bool
135 architecture_is_arm32(Elf* elf_handle);
136 
137 bool
138 architecture_is_big_endian(Elf* elf_handle);
139 
140 GElf_Addr
141 lookup_ppc64_elf_fn_entry_point_address(Elf*	  elf_handle,
142 					GElf_Addr fn_desc_address);
143 
144 //
145 // Helpers for Linux Kernel Binaries
146 //
147 
148 bool
149 is_linux_kernel_module(Elf *elf_handle);
150 
151 bool
152 is_linux_kernel(Elf *elf_handle);
153 
154 //
155 // Misc Helpers
156 //
157 
158 bool
159 get_binary_load_address(Elf* elf_handle, GElf_Addr& load_address);
160 
161 unsigned char
162 get_architecture_word_size(Elf* elf_handle);
163 
164 bool
165 is_executable(Elf* elf_handle);
166 
167 bool
168 is_dso(Elf* elf_handle);
169 
170 GElf_Addr
171 maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym);
172 
173 bool
174 address_is_in_opd_section(Elf* elf_handle, Dwarf_Addr addr);
175 
176 } // end namespace elf_helpers
177 } // end namespace abigail
178 
179 #endif // __ABG_ELF_HELPERS_H__
180