Rizin
unix-like reverse engineering framework and cli tools
internal.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002, 2003, 2006, 2007 Free Software Foundation, Inc.
2 // SPDX-License-Identifier: GPL-2.0-or-later
3 
4 /* ELF support for BFD.
5  Copyright 1991, 1992, 1993, 1994, 1995, 1997, 1998, 2000, 2001, 2002,
6  2003, 2006, 2007 Free Software Foundation, Inc.
7 
8  Written by Fred Fish @ Cygnus Support, from information published
9  in "UNIX System V Release 4, Programmers Guide: ANSI C and
10  Programming Support Tools".
11 
12 This file is part of BFD, the Binary File Descriptor library.
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
27 
28 /* This file is part of ELF support for BFD, and contains the portions
29  that describe how ELF is represented internally in the BFD library.
30  I.E. it describes the in-memory representation of ELF. It requires
31  the elf-common.h file which contains the portions that are common to
32  both the internal and external representations. */
33 
34 /* NOTE that these structures are not kept in the same order as they appear
35  in the object file. In some cases they've been reordered for more optimal
36  packing under various circumstances. */
37 
38 #ifndef _ELF_INTERNAL_H
39 #define _ELF_INTERNAL_H
40 
41 /* ELF Header */
42 
43 #define EI_NIDENT 16 /* Size of e_ident[] */
44 
45 typedef struct elf_internal_ehdr {
46  unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */
47  bfd_vma e_entry; /* Entry point virtual address */
48  bfd_size_type e_phoff; /* Program header table file offset */
49  bfd_size_type e_shoff; /* Section header table file offset */
50  unsigned long e_version; /* Identifies object file version */
51  unsigned long e_flags; /* Processor-specific flags */
52  unsigned short e_type; /* Identifies object file type */
53  unsigned short e_machine; /* Specifies required architecture */
54  unsigned int e_ehsize; /* ELF header size in bytes */
55  unsigned int e_phentsize; /* Program header table entry size */
56  unsigned int e_phnum; /* Program header table entry count */
57  unsigned int e_shentsize; /* Section header table entry size */
58  unsigned int e_shnum; /* Section header table entry count */
59  unsigned int e_shstrndx; /* Section header string table index */
61 
62 /* Program header */
63 
65  unsigned long p_type; /* Identifies program segment type */
66  unsigned long p_flags; /* Segment flags */
67  bfd_vma p_offset; /* Segment file offset */
68  bfd_vma p_vaddr; /* Segment virtual address */
69  bfd_vma p_paddr; /* Segment physical address */
70  bfd_vma p_filesz; /* Segment size in file */
71  bfd_vma p_memsz; /* Segment size in memory */
72  bfd_vma p_align; /* Segment alignment, file & memory */
73 };
74 
76 
77 /* Section header */
78 
79 typedef struct elf_internal_shdr {
80  unsigned int sh_name; /* Section name, index in string tbl */
81  unsigned int sh_type; /* Type of section */
82  bfd_vma sh_flags; /* Miscellaneous section attributes */
83  bfd_vma sh_addr; /* Section virtual addr at execution */
84  bfd_size_type sh_size; /* Size of section in bytes */
85  bfd_size_type sh_entsize; /* Entry size if section holds table */
86  unsigned long sh_link; /* Index of another section */
87  unsigned long sh_info; /* Additional section information */
88  file_ptr sh_offset; /* Section file offset */
89  unsigned int sh_addralign; /* Section alignment */
90 
91  /* The internal rep also has some cached info associated with it. */
92  asection *bfd_section; /* Associated BFD section. */
93  unsigned char *contents; /* Section contents. */
95 
96 /* Symbol table entry */
97 
99  bfd_vma st_value; /* Value of the symbol */
100  bfd_vma st_size; /* Associated symbol size */
101  unsigned long st_name; /* Symbol name, index in string tbl */
102  unsigned char st_info; /* Type and binding attributes */
103  unsigned char st_other; /* Visibilty, and target specific */
104  unsigned int st_shndx; /* Associated section index */
105 };
106 
107 typedef struct elf_internal_sym Elf_Internal_Sym;
108 
109 /* Note segments */
110 
111 typedef struct elf_internal_note {
112  unsigned long namesz; /* Size of entry's owner string */
113  unsigned long descsz; /* Size of the note descriptor */
114  unsigned long type; /* Interpretation of the descriptor */
115  char *namedata; /* Start of the name+desc data */
116  char *descdata; /* Start of the desc data */
117  bfd_vma descpos; /* File offset of the descdata */
119 
120 /* Relocation Entries */
121 
122 typedef struct elf_internal_rela {
123  bfd_vma rz_offset; /* Location at which to apply the action */
124  bfd_vma rz_info; /* Index and Type of relocation */
125  bfd_vma rz_addend; /* Constant addend used to compute value */
127 
128 /* dynamic section structure */
129 
130 typedef struct elf_internal_dyn {
131  /* This needs to support 64-bit values in elf64. */
132  bfd_vma d_tag; /* entry tag value */
133  union {
134  /* This needs to support 64-bit values in elf64. */
137  } d_un;
139 
140 /* This structure appears in a SHT_GNU_verdef section. */
141 
142 typedef struct elf_internal_verdef {
143  unsigned short vd_version; /* Version number of structure. */
144  unsigned short vd_flags; /* Flags (VER_FLG_*). */
145  unsigned short vd_ndx; /* Version index. */
146  unsigned short vd_cnt; /* Number of verdaux entries. */
147  unsigned long vd_hash; /* Hash of name. */
148  unsigned long vd_aux; /* Offset to verdaux entries. */
149  unsigned long vd_next; /* Offset to next verdef. */
150 
151  /* These fields are set up when BFD reads in the structure. FIXME:
152  It would be cleaner to store these in a different structure. */
153  bfd *vd_bfd; /* BFD. */
154  const char *vd_nodename; /* Version name. */
155  struct elf_internal_verdef *vd_nextdef; /* vd_next as pointer. */
156  struct elf_internal_verdaux *vd_auxptr; /* vd_aux as pointer. */
157  unsigned int vd_exp_refno; /* Used by the linker. */
159 
160 /* This structure appears in a SHT_GNU_verdef section. */
161 
162 typedef struct elf_internal_verdaux {
163  unsigned long vda_name; /* String table offset of name. */
164  unsigned long vda_next; /* Offset to next verdaux. */
165 
166  /* These fields are set up when BFD reads in the structure. FIXME:
167  It would be cleaner to store these in a different structure. */
168  const char *vda_nodename; /* vda_name as pointer. */
169  struct elf_internal_verdaux *vda_nextptr; /* vda_next as pointer. */
171 
172 /* This structure appears in a SHT_GNU_verneed section. */
173 
174 typedef struct elf_internal_verneed {
175  unsigned short vn_version; /* Version number of structure. */
176  unsigned short vn_cnt; /* Number of vernaux entries. */
177  unsigned long vn_file; /* String table offset of library name. */
178  unsigned long vn_aux; /* Offset to vernaux entries. */
179  unsigned long vn_next; /* Offset to next verneed. */
180 
181  /* These fields are set up when BFD reads in the structure. FIXME:
182  It would be cleaner to store these in a different structure. */
183  bfd *vn_bfd; /* BFD. */
184  const char *vn_filename; /* vn_file as pointer. */
185  struct elf_internal_vernaux *vn_auxptr; /* vn_aux as pointer. */
186  struct elf_internal_verneed *vn_nextref; /* vn_nextref as pointer. */
188 
189 /* This structure appears in a SHT_GNU_verneed section. */
190 
191 typedef struct elf_internal_vernaux {
192  unsigned long vna_hash; /* Hash of dependency name. */
193  unsigned short vna_flags; /* Flags (VER_FLG_*). */
194  unsigned short vna_other; /* Unused. */
195  unsigned long vna_name; /* String table offset to version name. */
196  unsigned long vna_next; /* Offset to next vernaux. */
197 
198  /* These fields are set up when BFD reads in the structure. FIXME:
199  It would be cleaner to store these in a different structure. */
200  const char *vna_nodename; /* vna_name as pointer. */
201  struct elf_internal_vernaux *vna_nextptr; /* vna_next as pointer. */
203 
204 /* This structure appears in a SHT_GNU_versym section. This is not a
205  standard ELF structure; ELF just uses Elf32_Half. */
206 
207 typedef struct elf_internal_versym {
208  unsigned short vs_vers;
210 
211 /* Structure for syminfo section. */
212 typedef struct
213 {
214  unsigned short int si_boundto;
215  unsigned short int si_flags;
217 
218 /* This structure appears on the stack and in NT_AUXV core file notes. */
219 typedef struct
220 {
224 
225 /* This structure is used to describe how sections should be assigned
226  to program segments. */
227 
229  /* Next program segment. */
231  /* Program segment type. */
232  unsigned long p_type;
233  /* Program segment flags. */
234  unsigned long p_flags;
235  /* Program segment physical address. */
237  /* Program segment virtual address offset from section vma. */
239  /* Program segment alignment. */
241  /* Whether the p_flags field is valid; if not, the flags are based
242  on the section flags. */
243  unsigned int p_flags_valid : 1;
244  /* Whether the p_paddr field is valid; if not, the physical address
245  is based on the section lma values. */
246  unsigned int p_paddr_valid : 1;
247  /* Whether the p_align field is valid; if not, PT_LOAD segment
248  alignment is based on the default maximum page size. */
249  unsigned int p_align_valid : 1;
250  /* Whether this segment includes the file header. */
251  unsigned int includes_filehdr : 1;
252  /* Whether this segment includes the program headers. */
253  unsigned int includes_phdrs : 1;
254  /* Number of sections (may be 0). */
255  unsigned int count;
256  /* Sections. Actual number of elements is in count field. */
258 };
259 
260 /* .tbss is special. It doesn't contribute memory space to normal
261  segments and it doesn't take file space in normal segments. */
262 #define ELF_SECTION_SIZE(sec_hdr, segment) \
263  (((sec_hdr->sh_flags & SHF_TLS) == 0 || sec_hdr->sh_type != SHT_NOBITS || segment->p_type == PT_TLS) ? sec_hdr->sh_size : 0)
264 
265 /* Decide if the given sec_hdr is in the given segment. PT_TLS segment
266  contains only SHF_TLS sections. Only PT_LOAD and PT_TLS segments
267  can contain SHF_TLS sections. */
268 #define ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment) \
269  (((((sec_hdr->sh_flags & SHF_TLS) != 0) && (segment->p_type == PT_TLS || segment->p_type == PT_LOAD)) || ((sec_hdr->sh_flags & SHF_TLS) == 0 && segment->p_type != PT_TLS)) /* Any section besides one of type SHT_NOBITS must have a file \
270  offset within the segment. */ \
271  && (sec_hdr->sh_type == SHT_NOBITS || ((bfd_vma)sec_hdr->sh_offset >= segment->p_offset && (sec_hdr->sh_offset + ELF_SECTION_SIZE(sec_hdr, segment) <= segment->p_offset + segment->p_filesz))) /* SHF_ALLOC sections must have VMAs within the segment. */ \
272  && ((sec_hdr->sh_flags & SHF_ALLOC) == 0 || (sec_hdr->sh_addr >= segment->p_vaddr && (sec_hdr->sh_addr + ELF_SECTION_SIZE(sec_hdr, segment) <= segment->p_vaddr + segment->p_memsz))))
273 
274 /* Decide if the given sec_hdr is in the given segment in file. */
275 #define ELF_IS_SECTION_IN_SEGMENT_FILE(sec_hdr, segment) \
276  (sec_hdr->sh_size > 0 && ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment))
277 
278 /* Decide if the given sec_hdr is in the given segment in memory. */
279 #define ELF_IS_SECTION_IN_SEGMENT_MEMORY(sec_hdr, segment) \
280  (ELF_SECTION_SIZE(sec_hdr, segment) > 0 && ELF_IS_SECTION_IN_SEGMENT(sec_hdr, segment))
281 
282 #endif /* _ELF_INTERNAL_H */
struct elf_internal_versym Elf_Internal_Versym
struct elf_internal_rela Elf_Internal_Rela
struct elf_internal_verdef Elf_Internal_Verdef
struct elf_internal_vernaux Elf_Internal_Vernaux
struct elf_internal_dyn Elf_Internal_Dyn
struct elf_internal_verdaux Elf_Internal_Verdaux
struct elf_internal_verneed Elf_Internal_Verneed
struct elf_internal_shdr Elf_Internal_Shdr
struct elf_internal_note Elf_Internal_Note
#define EI_NIDENT
Definition: internal.h:43
struct elf_internal_ehdr Elf_Internal_Ehdr
BFD_HOST_U_64_BIT bfd_size_type
Definition: mybfd.h:113
BFD_HOST_U_64_BIT bfd_vma
Definition: mybfd.h:111
BFD_HOST_64_BIT file_ptr
Definition: mybfd.h:166
bfd_vma a_type
Definition: internal.h:221
unsigned short int si_flags
Definition: internal.h:215
unsigned short int si_boundto
Definition: internal.h:214
Definition: mybfd.h:4212
bfd_vma d_ptr
Definition: internal.h:136
union elf_internal_dyn::@82 d_un
bfd_vma d_tag
Definition: internal.h:132
bfd_vma d_val
Definition: internal.h:135
unsigned int e_shnum
Definition: internal.h:58
unsigned int e_phnum
Definition: internal.h:56
unsigned short e_machine
Definition: internal.h:53
unsigned int e_shentsize
Definition: internal.h:57
unsigned int e_ehsize
Definition: internal.h:54
unsigned int e_shstrndx
Definition: internal.h:59
bfd_size_type e_phoff
Definition: internal.h:48
unsigned char e_ident[EI_NIDENT]
Definition: internal.h:46
bfd_size_type e_shoff
Definition: internal.h:49
unsigned long e_flags
Definition: internal.h:51
bfd_vma e_entry
Definition: internal.h:47
unsigned short e_type
Definition: internal.h:52
unsigned long e_version
Definition: internal.h:50
unsigned int e_phentsize
Definition: internal.h:55
bfd_vma descpos
Definition: internal.h:117
unsigned long type
Definition: internal.h:114
unsigned long namesz
Definition: internal.h:112
unsigned long descsz
Definition: internal.h:113
unsigned long p_flags
Definition: internal.h:66
unsigned long p_type
Definition: internal.h:65
bfd_vma p_memsz
Definition: internal.h:71
bfd_vma p_vaddr
Definition: internal.h:68
bfd_vma p_align
Definition: internal.h:72
bfd_vma p_offset
Definition: internal.h:67
bfd_vma p_filesz
Definition: internal.h:70
bfd_vma p_paddr
Definition: internal.h:69
bfd_vma rz_addend
Definition: internal.h:125
bfd_vma rz_info
Definition: internal.h:124
bfd_vma rz_offset
Definition: internal.h:123
unsigned long sh_info
Definition: internal.h:87
unsigned int sh_type
Definition: internal.h:81
bfd_size_type sh_size
Definition: internal.h:84
unsigned char * contents
Definition: internal.h:93
bfd_vma sh_addr
Definition: internal.h:83
unsigned int sh_addralign
Definition: internal.h:89
bfd_vma sh_flags
Definition: internal.h:82
file_ptr sh_offset
Definition: internal.h:88
bfd_size_type sh_entsize
Definition: internal.h:85
asection * bfd_section
Definition: internal.h:92
unsigned long sh_link
Definition: internal.h:86
unsigned int sh_name
Definition: internal.h:80
bfd_vma st_size
Definition: internal.h:100
unsigned int st_shndx
Definition: internal.h:104
unsigned char st_info
Definition: internal.h:102
unsigned char st_other
Definition: internal.h:103
unsigned long st_name
Definition: internal.h:101
bfd_vma st_value
Definition: internal.h:99
struct elf_internal_verdaux * vda_nextptr
Definition: internal.h:169
unsigned long vda_name
Definition: internal.h:163
const char * vda_nodename
Definition: internal.h:168
unsigned long vda_next
Definition: internal.h:164
unsigned short vd_flags
Definition: internal.h:144
struct elf_internal_verdef * vd_nextdef
Definition: internal.h:155
unsigned int vd_exp_refno
Definition: internal.h:157
const char * vd_nodename
Definition: internal.h:154
unsigned long vd_next
Definition: internal.h:149
unsigned short vd_ndx
Definition: internal.h:145
unsigned long vd_hash
Definition: internal.h:147
unsigned long vd_aux
Definition: internal.h:148
struct elf_internal_verdaux * vd_auxptr
Definition: internal.h:156
unsigned short vd_version
Definition: internal.h:143
unsigned short vd_cnt
Definition: internal.h:146
struct elf_internal_vernaux * vna_nextptr
Definition: internal.h:201
unsigned short vna_other
Definition: internal.h:194
const char * vna_nodename
Definition: internal.h:200
unsigned long vna_hash
Definition: internal.h:192
unsigned long vna_name
Definition: internal.h:195
unsigned short vna_flags
Definition: internal.h:193
unsigned long vna_next
Definition: internal.h:196
unsigned long vn_aux
Definition: internal.h:178
unsigned short vn_version
Definition: internal.h:175
struct elf_internal_vernaux * vn_auxptr
Definition: internal.h:185
const char * vn_filename
Definition: internal.h:184
struct elf_internal_verneed * vn_nextref
Definition: internal.h:186
unsigned short vn_cnt
Definition: internal.h:176
unsigned long vn_file
Definition: internal.h:177
unsigned long vn_next
Definition: internal.h:179
unsigned short vs_vers
Definition: internal.h:208
struct elf_segment_map * next
Definition: internal.h:230
bfd_vma p_align
Definition: internal.h:240
unsigned long p_flags
Definition: internal.h:234
asection * sections[1]
Definition: internal.h:257
bfd_vma p_paddr
Definition: internal.h:236
unsigned int count
Definition: internal.h:255
unsigned int includes_filehdr
Definition: internal.h:251
bfd_vma p_vaddr_offset
Definition: internal.h:238
unsigned int p_paddr_valid
Definition: internal.h:246
unsigned long p_type
Definition: internal.h:232
unsigned int includes_phdrs
Definition: internal.h:253
unsigned int p_align_valid
Definition: internal.h:249
unsigned int p_flags_valid
Definition: internal.h:243