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