| 1 |
/* ELF support for BFD. |
| 2 |
Copyright (C) 1991-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
Written by Fred Fish @ Cygnus Support, from information published |
| 5 |
in "UNIX System V Release 4, Programmers Guide: ANSI C and |
| 6 |
Programming Support Tools". |
| 7 |
|
| 8 |
This file is part of BFD, the Binary File Descriptor library. |
| 9 |
|
| 10 |
This program is free software; you can redistribute it and/or modify |
| 11 |
it under the terms of the GNU General Public License as published by |
| 12 |
the Free Software Foundation; either version 3 of the License, or |
| 13 |
(at your option) any later version. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
| 23 |
MA 02110-1301, USA. */ |
| 24 |
|
| 25 |
/* This file is part of ELF support for BFD, and contains the portions |
| 26 |
that describe how ELF is represented internally in the BFD library. |
| 27 |
I.E. it describes the in-memory representation of ELF. It requires |
| 28 |
the elf-common.h file which contains the portions that are common to |
| 29 |
both the internal and external representations. */ |
| 30 |
|
| 31 |
/* NOTE that these structures are not kept in the same order as they appear |
| 32 |
in the object file. In some cases they've been reordered for more optimal |
| 33 |
packing under various circumstances. */ |
| 34 |
|
| 35 |
#ifndef _ELF_INTERNAL_H |
| 36 |
#define _ELF_INTERNAL_H |
| 37 |
|
| 38 |
/* Special section indices, which may show up in st_shndx fields, among |
| 39 |
other places. */ |
| 40 |
|
| 41 |
#undef SHN_UNDEF |
| 42 |
#undef SHN_LORESERVE |
| 43 |
#undef SHN_LOPROC |
| 44 |
#undef SHN_HIPROC |
| 45 |
#undef SHN_LOOS |
| 46 |
#undef SHN_HIOS |
| 47 |
#undef SHN_ABS |
| 48 |
#undef SHN_COMMON |
| 49 |
#undef SHN_XINDEX |
| 50 |
#undef SHN_HIRESERVE |
| 51 |
#define SHN_UNDEF 0 /* Undefined section reference */ |
| 52 |
#define SHN_LORESERVE (-0x100u) /* Begin range of reserved indices */ |
| 53 |
#define SHN_LOPROC (-0x100u) /* Begin range of appl-specific */ |
| 54 |
#define SHN_HIPROC (-0xE1u) /* End range of appl-specific */ |
| 55 |
#define SHN_LOOS (-0xE0u) /* OS specific semantics, lo */ |
| 56 |
#define SHN_HIOS (-0xC1u) /* OS specific semantics, hi */ |
| 57 |
#define SHN_ABS (-0xFu) /* Associated symbol is absolute */ |
| 58 |
#define SHN_COMMON (-0xEu) /* Associated symbol is in common */ |
| 59 |
#define SHN_XINDEX (-0x1u) /* Section index is held elsewhere */ |
| 60 |
#define SHN_HIRESERVE (-0x1u) /* End range of reserved indices */ |
| 61 |
#define SHN_BAD (-0x101u) /* Used internally by bfd */ |
| 62 |
|
| 63 |
/* ELF Header */ |
| 64 |
|
| 65 |
#define EI_NIDENT 16 /* Size of e_ident[] */ |
| 66 |
|
| 67 |
typedef struct elf_internal_ehdr { |
| 68 |
unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ |
| 69 |
bfd_vma e_entry; /* Entry point virtual address */ |
| 70 |
bfd_size_type e_phoff; /* Program header table file offset */ |
| 71 |
bfd_size_type e_shoff; /* Section header table file offset */ |
| 72 |
unsigned long e_version; /* Identifies object file version */ |
| 73 |
unsigned long e_flags; /* Processor-specific flags */ |
| 74 |
unsigned short e_type; /* Identifies object file type */ |
| 75 |
unsigned short e_machine; /* Specifies required architecture */ |
| 76 |
unsigned int e_ehsize; /* ELF header size in bytes */ |
| 77 |
unsigned int e_phentsize; /* Program header table entry size */ |
| 78 |
unsigned int e_phnum; /* Program header table entry count */ |
| 79 |
unsigned int e_shentsize; /* Section header table entry size */ |
| 80 |
unsigned int e_shnum; /* Section header table entry count */ |
| 81 |
unsigned int e_shstrndx; /* Section header string table index */ |
| 82 |
} Elf_Internal_Ehdr; |
| 83 |
|
| 84 |
/* Program header */ |
| 85 |
|
| 86 |
struct elf_internal_phdr { |
| 87 |
unsigned long p_type; /* Identifies program segment type. */ |
| 88 |
unsigned long p_flags; /* Segment flags. */ |
| 89 |
bfd_vma p_offset; /* Segment file offset in octets. */ |
| 90 |
bfd_vma p_vaddr; /* Segment virtual address in octets. */ |
| 91 |
bfd_vma p_paddr; /* Segment physical address in octets. */ |
| 92 |
bfd_vma p_filesz; /* Segment size in file in octets. */ |
| 93 |
bfd_vma p_memsz; /* Segment size in memory in octets. */ |
| 94 |
bfd_vma p_align; /* Segment alignment in bytes, file |
| 95 |
& memory */ |
| 96 |
}; |
| 97 |
|
| 98 |
typedef struct elf_internal_phdr Elf_Internal_Phdr; |
| 99 |
|
| 100 |
/* Section header */ |
| 101 |
|
| 102 |
typedef struct elf_internal_shdr { |
| 103 |
unsigned int sh_name; /* Section name, index in string tbl */ |
| 104 |
unsigned int sh_type; /* Type of section */ |
| 105 |
bfd_vma sh_flags; /* Miscellaneous section attributes */ |
| 106 |
bfd_vma sh_addr; /* Section virtual addr at execution in |
| 107 |
octets. */ |
| 108 |
file_ptr sh_offset; /* Section file offset in octets. */ |
| 109 |
bfd_size_type sh_size; /* Size of section in octets. */ |
| 110 |
unsigned int sh_link; /* Index of another section */ |
| 111 |
unsigned int sh_info; /* Additional section information */ |
| 112 |
bfd_vma sh_addralign; /* Section alignment */ |
| 113 |
bfd_size_type sh_entsize; /* Entry size if section holds table */ |
| 114 |
|
| 115 |
/* The internal rep also has some cached info associated with it. */ |
| 116 |
asection * bfd_section; /* Associated BFD section. */ |
| 117 |
unsigned char *contents; /* Section contents. */ |
| 118 |
} Elf_Internal_Shdr; |
| 119 |
|
| 120 |
/* Compression header */ |
| 121 |
|
| 122 |
typedef struct elf_internal_chdr { |
| 123 |
unsigned int ch_type; /* Type of compression */ |
| 124 |
bfd_size_type ch_size; /* Size of uncompressed data in bytes */ |
| 125 |
bfd_vma ch_addralign; /* Alignment of uncompressed data */ |
| 126 |
} Elf_Internal_Chdr; |
| 127 |
|
| 128 |
/* Symbol table entry */ |
| 129 |
|
| 130 |
struct elf_internal_sym { |
| 131 |
bfd_vma st_value; /* Value of the symbol */ |
| 132 |
bfd_vma st_size; /* Associated symbol size */ |
| 133 |
unsigned long st_name; /* Symbol name, index in string tbl */ |
| 134 |
unsigned char st_info; /* Type and binding attributes */ |
| 135 |
unsigned char st_other; /* Visibilty, and target specific */ |
| 136 |
unsigned char st_target_internal; /* Internal-only information */ |
| 137 |
unsigned int st_shndx; /* Associated section index */ |
| 138 |
}; |
| 139 |
|
| 140 |
typedef struct elf_internal_sym Elf_Internal_Sym; |
| 141 |
|
| 142 |
/* Note segments */ |
| 143 |
|
| 144 |
typedef struct elf_internal_note { |
| 145 |
unsigned long namesz; /* Size of entry's owner string */ |
| 146 |
unsigned long descsz; /* Size of the note descriptor */ |
| 147 |
unsigned long type; /* Interpretation of the descriptor */ |
| 148 |
char * namedata; /* Start of the name+desc data */ |
| 149 |
char * descdata; /* Start of the desc data */ |
| 150 |
bfd_vma descpos; /* File offset of the descdata */ |
| 151 |
} Elf_Internal_Note; |
| 152 |
|
| 153 |
/* Relocation Entries */ |
| 154 |
|
| 155 |
typedef struct elf_internal_rela { |
| 156 |
bfd_vma r_offset; /* Location at which to apply the action */ |
| 157 |
bfd_vma r_info; /* Index and Type of relocation */ |
| 158 |
bfd_vma r_addend; /* Constant addend used to compute value */ |
| 159 |
} Elf_Internal_Rela; |
| 160 |
|
| 161 |
/* dynamic section structure */ |
| 162 |
|
| 163 |
typedef struct elf_internal_dyn { |
| 164 |
/* This needs to support 64-bit values in elf64. */ |
| 165 |
bfd_vma d_tag; /* entry tag value */ |
| 166 |
union { |
| 167 |
/* This needs to support 64-bit values in elf64. */ |
| 168 |
bfd_vma d_val; |
| 169 |
bfd_vma d_ptr; |
| 170 |
} d_un; |
| 171 |
} Elf_Internal_Dyn; |
| 172 |
|
| 173 |
/* This structure appears in a SHT_GNU_verdef section. */ |
| 174 |
|
| 175 |
typedef struct elf_internal_verdef { |
| 176 |
unsigned short vd_version; /* Version number of structure. */ |
| 177 |
unsigned short vd_flags; /* Flags (VER_FLG_*). */ |
| 178 |
unsigned short vd_ndx; /* Version index. */ |
| 179 |
unsigned short vd_cnt; /* Number of verdaux entries. */ |
| 180 |
unsigned long vd_hash; /* Hash of name. */ |
| 181 |
unsigned long vd_aux; /* Offset to verdaux entries. */ |
| 182 |
unsigned long vd_next; /* Offset to next verdef. */ |
| 183 |
|
| 184 |
/* These fields are set up when BFD reads in the structure. FIXME: |
| 185 |
It would be cleaner to store these in a different structure. */ |
| 186 |
bfd *vd_bfd; /* BFD. */ |
| 187 |
const char *vd_nodename; /* Version name. */ |
| 188 |
struct elf_internal_verdef *vd_nextdef; /* vd_next as pointer. */ |
| 189 |
struct elf_internal_verdaux *vd_auxptr; /* vd_aux as pointer. */ |
| 190 |
unsigned int vd_exp_refno; /* Used by the linker. */ |
| 191 |
} Elf_Internal_Verdef; |
| 192 |
|
| 193 |
/* This structure appears in a SHT_GNU_verdef section. */ |
| 194 |
|
| 195 |
typedef struct elf_internal_verdaux { |
| 196 |
unsigned long vda_name; /* String table offset of name. */ |
| 197 |
unsigned long vda_next; /* Offset to next verdaux. */ |
| 198 |
|
| 199 |
/* These fields are set up when BFD reads in the structure. FIXME: |
| 200 |
It would be cleaner to store these in a different structure. */ |
| 201 |
const char *vda_nodename; /* vda_name as pointer. */ |
| 202 |
struct elf_internal_verdaux *vda_nextptr; /* vda_next as pointer. */ |
| 203 |
} Elf_Internal_Verdaux; |
| 204 |
|
| 205 |
/* This structure appears in a SHT_GNU_verneed section. */ |
| 206 |
|
| 207 |
typedef struct elf_internal_verneed { |
| 208 |
unsigned short vn_version; /* Version number of structure. */ |
| 209 |
unsigned short vn_cnt; /* Number of vernaux entries. */ |
| 210 |
unsigned long vn_file; /* String table offset of library name. */ |
| 211 |
unsigned long vn_aux; /* Offset to vernaux entries. */ |
| 212 |
unsigned long vn_next; /* Offset to next verneed. */ |
| 213 |
|
| 214 |
/* These fields are set up when BFD reads in the structure. FIXME: |
| 215 |
It would be cleaner to store these in a different structure. */ |
| 216 |
bfd *vn_bfd; /* BFD. */ |
| 217 |
const char *vn_filename; /* vn_file as pointer. */ |
| 218 |
struct elf_internal_vernaux *vn_auxptr; /* vn_aux as pointer. */ |
| 219 |
struct elf_internal_verneed *vn_nextref; /* vn_nextref as pointer. */ |
| 220 |
} Elf_Internal_Verneed; |
| 221 |
|
| 222 |
/* This structure appears in a SHT_GNU_verneed section. */ |
| 223 |
|
| 224 |
typedef struct elf_internal_vernaux { |
| 225 |
unsigned long vna_hash; /* Hash of dependency name. */ |
| 226 |
unsigned short vna_flags; /* Flags (VER_FLG_*). */ |
| 227 |
unsigned short vna_other; /* Unused. */ |
| 228 |
unsigned long vna_name; /* String table offset to version name. */ |
| 229 |
unsigned long vna_next; /* Offset to next vernaux. */ |
| 230 |
|
| 231 |
/* These fields are set up when BFD reads in the structure. FIXME: |
| 232 |
It would be cleaner to store these in a different structure. */ |
| 233 |
const char *vna_nodename; /* vna_name as pointer. */ |
| 234 |
struct elf_internal_vernaux *vna_nextptr; /* vna_next as pointer. */ |
| 235 |
} Elf_Internal_Vernaux; |
| 236 |
|
| 237 |
/* This structure appears in a SHT_GNU_versym section. This is not a |
| 238 |
standard ELF structure; ELF just uses Elf32_Half. */ |
| 239 |
|
| 240 |
typedef struct elf_internal_versym { |
| 241 |
unsigned short vs_vers; |
| 242 |
} Elf_Internal_Versym; |
| 243 |
|
| 244 |
/* Structure for syminfo section. */ |
| 245 |
typedef struct |
| 246 |
{ |
| 247 |
unsigned short int si_boundto; |
| 248 |
unsigned short int si_flags; |
| 249 |
} Elf_Internal_Syminfo; |
| 250 |
|
| 251 |
/* This structure appears on the stack and in NT_AUXV core file notes. */ |
| 252 |
typedef struct |
| 253 |
{ |
| 254 |
bfd_vma a_type; |
| 255 |
bfd_vma a_val; |
| 256 |
} Elf_Internal_Auxv; |
| 257 |
|
| 258 |
|
| 259 |
/* This structure is used to describe how sections should be assigned |
| 260 |
to program segments. */ |
| 261 |
|
| 262 |
struct elf_segment_map |
| 263 |
{ |
| 264 |
/* Next program segment. */ |
| 265 |
struct elf_segment_map *next; |
| 266 |
/* Program segment type. */ |
| 267 |
unsigned long p_type; |
| 268 |
/* Program segment flags. */ |
| 269 |
unsigned long p_flags; |
| 270 |
/* Program segment physical address in octets. */ |
| 271 |
bfd_vma p_paddr; |
| 272 |
/* Program segment virtual address offset from section vma in bytes. */ |
| 273 |
bfd_vma p_vaddr_offset; |
| 274 |
/* Program segment alignment. */ |
| 275 |
bfd_vma p_align; |
| 276 |
/* Segment size in file and memory in octets. */ |
| 277 |
bfd_vma p_size; |
| 278 |
/* Whether the p_flags field is valid; if not, the flags are based |
| 279 |
on the section flags. */ |
| 280 |
unsigned int p_flags_valid : 1; |
| 281 |
/* Whether the p_paddr field is valid; if not, the physical address |
| 282 |
is based on the section lma values. */ |
| 283 |
unsigned int p_paddr_valid : 1; |
| 284 |
/* Whether the p_align field is valid; if not, PT_LOAD segment |
| 285 |
alignment is based on the default maximum page size. */ |
| 286 |
unsigned int p_align_valid : 1; |
| 287 |
/* Whether the p_size field is valid; if not, the size are based |
| 288 |
on the section sizes. */ |
| 289 |
unsigned int p_size_valid : 1; |
| 290 |
/* Whether this segment includes the file header. */ |
| 291 |
unsigned int includes_filehdr : 1; |
| 292 |
/* Whether this segment includes the program headers. */ |
| 293 |
unsigned int includes_phdrs : 1; |
| 294 |
/* Assume this PT_LOAD header has an lma of zero when sorting |
| 295 |
headers before assigning file offsets. PT_LOAD headers with this |
| 296 |
flag set are placed after one with includes_filehdr set, and |
| 297 |
before PT_LOAD headers without this flag set. */ |
| 298 |
unsigned int no_sort_lma : 1; |
| 299 |
/* Index holding original order before sorting segments. */ |
| 300 |
unsigned int idx; |
| 301 |
/* Number of sections (may be 0). */ |
| 302 |
unsigned int count; |
| 303 |
/* Sections. Actual number of elements is in count field. */ |
| 304 |
asection *sections[1]; |
| 305 |
}; |
| 306 |
|
| 307 |
/* .tbss is special. It doesn't contribute memory space to normal |
| 308 |
segments and it doesn't take file space in normal segments. */ |
| 309 |
#define ELF_TBSS_SPECIAL(sec_hdr, segment) \ |
| 310 |
(((sec_hdr)->sh_flags & SHF_TLS) != 0 \ |
| 311 |
&& (sec_hdr)->sh_type == SHT_NOBITS \ |
| 312 |
&& (segment)->p_type != PT_TLS) |
| 313 |
|
| 314 |
#define ELF_SECTION_SIZE(sec_hdr, segment) \ |
| 315 |
(ELF_TBSS_SPECIAL(sec_hdr, segment) ? 0 : (sec_hdr)->sh_size) |
| 316 |
|
| 317 |
/* Decide if the section SEC_HDR is in SEGMENT. If CHECK_VMA, then |
| 318 |
VMAs are checked for alloc sections. If STRICT, then a zero size |
| 319 |
section won't match at the end of a segment, unless the segment |
| 320 |
is also zero size. Regardless of STRICT and CHECK_VMA, zero size |
| 321 |
sections won't match at the start or end of PT_DYNAMIC nor PT_NOTE, |
| 322 |
unless PT_DYNAMIC and PT_NOTE are themselves zero sized. */ |
| 323 |
#define ELF_SECTION_IN_SEGMENT_1(sec_hdr, segment, check_vma, strict) \ |
| 324 |
((/* Only PT_LOAD, PT_GNU_RELRO and PT_TLS segments can contain \ |
| 325 |
SHF_TLS sections. */ \ |
| 326 |
((((sec_hdr)->sh_flags & SHF_TLS) != 0) \ |
| 327 |
&& ((segment)->p_type == PT_TLS \ |
| 328 |
|| (segment)->p_type == PT_GNU_RELRO \ |
| 329 |
|| (segment)->p_type == PT_LOAD)) \ |
| 330 |
/* PT_TLS segment contains only SHF_TLS sections, PT_PHDR no \ |
| 331 |
sections at all. */ \ |
| 332 |
|| (((sec_hdr)->sh_flags & SHF_TLS) == 0 \ |
| 333 |
&& (segment)->p_type != PT_TLS \ |
| 334 |
&& (segment)->p_type != PT_PHDR)) \ |
| 335 |
/* PT_LOAD and similar segments only have SHF_ALLOC sections. */ \ |
| 336 |
&& !(((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \ |
| 337 |
&& ((segment)->p_type == PT_LOAD \ |
| 338 |
|| (segment)->p_type == PT_DYNAMIC \ |
| 339 |
|| (segment)->p_type == PT_GNU_EH_FRAME \ |
| 340 |
|| (segment)->p_type == PT_GNU_STACK \ |
| 341 |
|| (segment)->p_type == PT_GNU_RELRO \ |
| 342 |
|| (segment)->p_type == PT_GNU_SFRAME \ |
| 343 |
|| ((segment)->p_type >= PT_GNU_MBIND_LO \ |
| 344 |
&& (segment)->p_type <= PT_GNU_MBIND_HI))) \ |
| 345 |
/* Any section besides one of type SHT_NOBITS must have file \ |
| 346 |
offsets within the segment. */ \ |
| 347 |
&& ((sec_hdr)->sh_type == SHT_NOBITS \ |
| 348 |
|| ((bfd_vma) (sec_hdr)->sh_offset >= (segment)->p_offset \ |
| 349 |
&& (!(strict) \ |
| 350 |
|| ((sec_hdr)->sh_offset - (segment)->p_offset \ |
| 351 |
<= (segment)->p_filesz - 1)) \ |
| 352 |
&& (((sec_hdr)->sh_offset - (segment)->p_offset \ |
| 353 |
+ ELF_SECTION_SIZE(sec_hdr, segment)) \ |
| 354 |
<= (segment)->p_filesz))) \ |
| 355 |
/* SHF_ALLOC sections must have VMAs within the segment. */ \ |
| 356 |
&& (!(check_vma) \ |
| 357 |
|| ((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \ |
| 358 |
|| ((sec_hdr)->sh_addr >= (segment)->p_vaddr \ |
| 359 |
&& (!(strict) \ |
| 360 |
|| ((sec_hdr)->sh_addr - (segment)->p_vaddr \ |
| 361 |
<= (segment)->p_memsz - 1)) \ |
| 362 |
&& (((sec_hdr)->sh_addr - (segment)->p_vaddr \ |
| 363 |
+ ELF_SECTION_SIZE(sec_hdr, segment)) \ |
| 364 |
<= (segment)->p_memsz))) \ |
| 365 |
/* No zero size sections at start or end of PT_DYNAMIC nor \ |
| 366 |
PT_NOTE. */ \ |
| 367 |
&& (((segment)->p_type != PT_DYNAMIC \ |
| 368 |
&& (segment)->p_type != PT_NOTE) \ |
| 369 |
|| (sec_hdr)->sh_size != 0 \ |
| 370 |
|| (segment)->p_memsz == 0 \ |
| 371 |
|| (((sec_hdr)->sh_type == SHT_NOBITS \ |
| 372 |
|| ((bfd_vma) (sec_hdr)->sh_offset > (segment)->p_offset \ |
| 373 |
&& ((sec_hdr)->sh_offset - (segment)->p_offset \ |
| 374 |
< (segment)->p_filesz))) \ |
| 375 |
&& (((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \ |
| 376 |
|| ((sec_hdr)->sh_addr > (segment)->p_vaddr \ |
| 377 |
&& ((sec_hdr)->sh_addr - (segment)->p_vaddr \ |
| 378 |
< (segment)->p_memsz)))))) |
| 379 |
|
| 380 |
#define ELF_SECTION_IN_SEGMENT(sec_hdr, segment) \ |
| 381 |
(ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 0)) |
| 382 |
|
| 383 |
#define ELF_SECTION_IN_SEGMENT_STRICT(sec_hdr, segment) \ |
| 384 |
(ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 1)) |
| 385 |
|
| 386 |
#endif /* _ELF_INTERNAL_H */ |