| 1 |
/* Copyright (c) 2013-2023, David Anderson All rights reserved. |
| 2 |
|
| 3 |
Redistribution and use in source and binary forms, with |
| 4 |
or without modification, are permitted provided that the |
| 5 |
following conditions are met: |
| 6 |
|
| 7 |
Redistributions of source code must retain the above |
| 8 |
copyright notice, this list of conditions and the following |
| 9 |
disclaimer. |
| 10 |
|
| 11 |
Redistributions in binary form must reproduce the above |
| 12 |
copyright notice, this list of conditions and the following |
| 13 |
disclaimer in the documentation and/or other materials |
| 14 |
provided with the distribution. |
| 15 |
|
| 16 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 17 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 18 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 21 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 28 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
*/ |
| 31 |
#ifndef READELFOBJ_H |
| 32 |
#define READELFOBJ_H |
| 33 |
|
| 34 |
#ifdef __cplusplus |
| 35 |
extern "C" { |
| 36 |
#endif /* __cplusplus */ |
| 37 |
|
| 38 |
/* Use this for .rel. too. */ |
| 39 |
struct generic_rela { |
| 40 |
Dwarf_Unsigned gr_offset; |
| 41 |
Dwarf_Unsigned gr_info; |
| 42 |
Dwarf_Unsigned gr_sym; /* From info */ |
| 43 |
Dwarf_Unsigned gr_type; /* From info */ |
| 44 |
Dwarf_Signed gr_addend; |
| 45 |
unsigned char gr_type2; /*MIPS64*/ |
| 46 |
unsigned char gr_type3; /*MIPS64*/ |
| 47 |
/* The following TRUE if .rela. and FALSE if .rel. |
| 48 |
if FALSE, gr_addend will be zero. */ |
| 49 |
int gr_is_rela; |
| 50 |
}; |
| 51 |
|
| 52 |
/* The following are generic to simplify handling |
| 53 |
Elf32 and Elf64. Some fields added where |
| 54 |
the two sizes have different extraction code. */ |
| 55 |
struct generic_ehdr { |
| 56 |
unsigned char ge_ident[EI_NIDENT]; |
| 57 |
Dwarf_Unsigned ge_type; |
| 58 |
Dwarf_Unsigned ge_machine; |
| 59 |
Dwarf_Unsigned ge_version; |
| 60 |
Dwarf_Unsigned ge_entry; |
| 61 |
Dwarf_Unsigned ge_phoff; |
| 62 |
Dwarf_Unsigned ge_shoff; |
| 63 |
Dwarf_Unsigned ge_flags; |
| 64 |
Dwarf_Unsigned ge_ehsize; |
| 65 |
Dwarf_Unsigned ge_phentsize; |
| 66 |
Dwarf_Unsigned ge_phnum; |
| 67 |
Dwarf_Unsigned ge_shentsize; |
| 68 |
Dwarf_Unsigned ge_shnum; |
| 69 |
/* if ge_shnum >= 0xff00 SHN_LORESERVE |
| 70 |
Once section zero is read we put the sh_size |
| 71 |
member as the true count and set ge_shnum_in_shnum TRUE. |
| 72 |
ge_shnum_extended is TRUE if the object used the extension |
| 73 |
mechanism */ |
| 74 |
Dwarf_Bool ge_shnum_in_shnum; |
| 75 |
Dwarf_Bool ge_shnum_extended; |
| 76 |
|
| 77 |
/* if section num of sec strings >= 0xff SHN_LORESERVE |
| 78 |
this member holds SHN_XINDEX (0xffff) and the real |
| 79 |
section string index is the sh_link value of section |
| 80 |
0. ge_sstrndx_extended is TRUE if the object used |
| 81 |
the extension mechanism */ |
| 82 |
Dwarf_Unsigned ge_shstrndx; |
| 83 |
Dwarf_Bool ge_strndx_in_strndx; |
| 84 |
Dwarf_Bool ge_strndx_extended; |
| 85 |
}; |
| 86 |
struct generic_phdr { |
| 87 |
Dwarf_Unsigned gp_type; |
| 88 |
Dwarf_Unsigned gp_flags; |
| 89 |
Dwarf_Unsigned gp_offset; |
| 90 |
Dwarf_Unsigned gp_vaddr; |
| 91 |
Dwarf_Unsigned gp_paddr; |
| 92 |
Dwarf_Unsigned gp_filesz; |
| 93 |
Dwarf_Unsigned gp_memsz; |
| 94 |
Dwarf_Unsigned gp_align; |
| 95 |
}; |
| 96 |
struct generic_shdr { |
| 97 |
Dwarf_Unsigned gh_secnum; |
| 98 |
Dwarf_Unsigned gh_name; |
| 99 |
const char * gh_namestring; |
| 100 |
Dwarf_Unsigned gh_type; |
| 101 |
Dwarf_Unsigned gh_flags; |
| 102 |
Dwarf_Unsigned gh_addr; |
| 103 |
Dwarf_Unsigned gh_offset; |
| 104 |
Dwarf_Unsigned gh_size; |
| 105 |
Dwarf_Unsigned gh_link; |
| 106 |
/* Section index (in an SHT_REL or SHT_RELA section) |
| 107 |
of the target section from gh_link. Otherwise 0. */ |
| 108 |
Dwarf_Unsigned gh_reloc_target_secnum; |
| 109 |
Dwarf_Unsigned gh_info; |
| 110 |
Dwarf_Unsigned gh_addralign; |
| 111 |
Dwarf_Unsigned gh_entsize; |
| 112 |
|
| 113 |
/* Zero unless content read in. Malloc space |
| 114 |
of size gh_size, in bytes. For dwarf |
| 115 |
and strings mainly. free() this if not null*/ |
| 116 |
char * gh_content; |
| 117 |
|
| 118 |
/* If a .rel or .rela section this will point |
| 119 |
to generic relocation records if such |
| 120 |
have been loaded. |
| 121 |
free() this if not null. */ |
| 122 |
Dwarf_Unsigned gh_relcount; |
| 123 |
struct generic_rela * gh_rels; |
| 124 |
|
| 125 |
/* For SHT_GROUP based grouping, which |
| 126 |
group is this section in. 0 unknown, |
| 127 |
1 DW_GROUP_NUMBER_BASE base DWARF, |
| 128 |
2 DW_GROUPNUMBER_DWO dwo sections, 3+ |
| 129 |
are in an SHT_GROUP. GNU uses this. |
| 130 |
set with group number (3+) from SHT_GROUP |
| 131 |
and the flags should have SHF_GROUP set |
| 132 |
if in SHT_GROUP. Must only be in one group? */ |
| 133 |
Dwarf_Unsigned gh_section_group_number; |
| 134 |
|
| 135 |
/* Content of an SHT_GROUP section as an array |
| 136 |
of integers. [0] is the version, which |
| 137 |
can only be one(1) . */ |
| 138 |
Dwarf_Unsigned * gh_sht_group_array; |
| 139 |
/* Number of elements in the gh_sht_group_array. */ |
| 140 |
Dwarf_Unsigned gh_sht_group_array_count; |
| 141 |
|
| 142 |
/* TRUE if .debug_info .eh_frame etc. */ |
| 143 |
char gh_is_dwarf; |
| 144 |
}; |
| 145 |
|
| 146 |
struct generic_dynentry { |
| 147 |
Dwarf_Unsigned gd_tag; |
| 148 |
/* gd_val stands in for d_ptr and d_val union, |
| 149 |
the union adds nothing in practice since |
| 150 |
we expect ptrsize <= ulongest. */ |
| 151 |
Dwarf_Unsigned gd_val; |
| 152 |
Dwarf_Unsigned gd_dyn_file_offset; |
| 153 |
}; |
| 154 |
|
| 155 |
struct generic_symentry { |
| 156 |
Dwarf_Unsigned gs_name; |
| 157 |
Dwarf_Unsigned gs_value; |
| 158 |
Dwarf_Unsigned gs_size; |
| 159 |
Dwarf_Unsigned gs_info; |
| 160 |
Dwarf_Unsigned gs_other; |
| 161 |
Dwarf_Unsigned gs_shndx; |
| 162 |
/* derived */ |
| 163 |
Dwarf_Unsigned gs_bind; |
| 164 |
Dwarf_Unsigned gs_type; |
| 165 |
}; |
| 166 |
|
| 167 |
struct location { |
| 168 |
const char *g_name; |
| 169 |
Dwarf_Unsigned g_offset; |
| 170 |
Dwarf_Unsigned g_count; |
| 171 |
Dwarf_Unsigned g_entrysize; |
| 172 |
Dwarf_Unsigned g_totalsize; |
| 173 |
}; |
| 174 |
|
| 175 |
typedef struct elf_filedata_s { |
| 176 |
/* f_ident[0] == 'E' means it is elf and |
| 177 |
elf_filedata_s is the struct involved. |
| 178 |
Other means error/corruption of some kind. |
| 179 |
f_ident[1] is a version number. |
| 180 |
Only version 1 is defined. */ |
| 181 |
char f_ident[8]; |
| 182 |
char * f_path; /* non-null if known. Must be freed */ |
| 183 |
int f_fd; |
| 184 |
unsigned f_machine; /* EM_* */ |
| 185 |
int f_destruct_close_fd; |
| 186 |
int f_is_64bit; |
| 187 |
unsigned f_endian; |
| 188 |
Dwarf_Unsigned f_filesize; |
| 189 |
Dwarf_Unsigned f_flags; |
| 190 |
/* Elf size, not DWARF. 32 or 64 */ |
| 191 |
Dwarf_Small f_offsetsize; |
| 192 |
Dwarf_Small f_pointersize; |
| 193 |
int f_ftype; |
| 194 |
int f_path_source; |
| 195 |
|
| 196 |
Dwarf_Unsigned f_max_secdata_offset; |
| 197 |
Dwarf_Unsigned f_max_progdata_offset; |
| 198 |
|
| 199 |
void (*f_copy_word) (void *, const void *, unsigned long); |
| 200 |
|
| 201 |
struct location f_loc_ehdr; |
| 202 |
struct generic_ehdr* f_ehdr; |
| 203 |
|
| 204 |
struct location f_loc_shdr; |
| 205 |
struct generic_shdr* f_shdr; |
| 206 |
|
| 207 |
struct location f_loc_phdr; |
| 208 |
struct generic_phdr* f_phdr; |
| 209 |
|
| 210 |
char *f_elf_shstrings_data; /* section name strings */ |
| 211 |
/* length of currentsection. Might be zero..*/ |
| 212 |
Dwarf_Unsigned f_elf_shstrings_length; |
| 213 |
/* size of malloc-d space */ |
| 214 |
Dwarf_Unsigned f_elf_shstrings_max; |
| 215 |
|
| 216 |
/* This is the .dynamic section */ |
| 217 |
struct location f_loc_dynamic; |
| 218 |
struct generic_dynentry * f_dynamic; |
| 219 |
Dwarf_Unsigned f_dynamic_sect_index; |
| 220 |
|
| 221 |
/* .dynsym, .dynstr */ |
| 222 |
struct location f_loc_dynsym; |
| 223 |
struct generic_symentry* f_dynsym; |
| 224 |
char *f_dynsym_sect_strings; |
| 225 |
Dwarf_Unsigned f_dynsym_sect_strings_max; |
| 226 |
Dwarf_Unsigned f_dynsym_sect_strings_sect_index; |
| 227 |
Dwarf_Unsigned f_dynsym_sect_index; |
| 228 |
|
| 229 |
/* .symtab .strtab */ |
| 230 |
struct location f_loc_symtab; |
| 231 |
struct generic_symentry* f_symtab; |
| 232 |
char * f_symtab_sect_strings; |
| 233 |
Dwarf_Unsigned f_symtab_sect_strings_max; |
| 234 |
Dwarf_Unsigned f_symtab_sect_strings_sect_index; |
| 235 |
Dwarf_Unsigned f_symtab_sect_index; |
| 236 |
|
| 237 |
/* Starts at 3. 0,1,2 used specially. */ |
| 238 |
Dwarf_Unsigned f_sg_next_group_number; |
| 239 |
/* Both the following will be zero unless there |
| 240 |
are explicit Elf groups. */ |
| 241 |
Dwarf_Unsigned f_sht_group_type_section_count; |
| 242 |
Dwarf_Unsigned f_shf_group_flag_section_count; |
| 243 |
Dwarf_Unsigned f_dwo_group_section_count; |
| 244 |
} dwarf_elf_object_access_internals_t; |
| 245 |
|
| 246 |
int dwarf_construct_elf_access(int fd, |
| 247 |
const char *path, |
| 248 |
dwarf_elf_object_access_internals_t **ep,int *errcode); |
| 249 |
int dwarf_destruct_elf_access( |
| 250 |
dwarf_elf_object_access_internals_t *ep,int *errcode); |
| 251 |
int _dwarf_load_elf_header( |
| 252 |
dwarf_elf_object_access_internals_t *ep,int *errcode); |
| 253 |
int _dwarf_load_elf_sectheaders( |
| 254 |
dwarf_elf_object_access_internals_t* ep,int *errcode); |
| 255 |
int _dwarf_load_elf_symtab_symbols( |
| 256 |
dwarf_elf_object_access_internals_t *ep,int *errcode); |
| 257 |
int _dwarf_load_elf_symstr( |
| 258 |
dwarf_elf_object_access_internals_t *ep, int *errcode); |
| 259 |
|
| 260 |
/* These two enums used for type safety in passing |
| 261 |
values. */ |
| 262 |
enum RelocRela { |
| 263 |
RelocIsRela = 1, |
| 264 |
RelocIsRel = 2 |
| 265 |
}; |
| 266 |
enum RelocOffsetSize { |
| 267 |
RelocOffset32 = 4, |
| 268 |
RelocOffset64 = 8 |
| 269 |
}; |
| 270 |
|
| 271 |
int _dwarf_load_elf_relx(dwarf_elf_object_access_internals_t *ep, |
| 272 |
Dwarf_Unsigned secnum,enum RelocRela,int *errcode); |
| 273 |
|
| 274 |
#ifndef EI_NIDENT |
| 275 |
#define EI_NIDENT 16 |
| 276 |
#endif /* EI_NIDENT */ |
| 277 |
|
| 278 |
#ifndef SHN_XINDEX |
| 279 |
#define SHN_XINDEX 0xffff |
| 280 |
#endif /* SHN_XINDEX */ |
| 281 |
|
| 282 |
#ifndef SHN_lORESERVE |
| 283 |
#define SHN_LORESERVE 0xff00 |
| 284 |
#endif /* SHN_lORESERVE */ |
| 285 |
|
| 286 |
#ifdef __cplusplus |
| 287 |
} |
| 288 |
#endif /* __cplusplus */ |
| 289 |
|
| 290 |
#endif /* READELFOBJ_H */ |