]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - include/elf/internal.h
Fix several mix up between octets and bytes in ELF program headers
[thirdparty/binutils-gdb.git] / include / elf / internal.h
CommitLineData
252b5132 1/* ELF support for BFD.
b3adc24a 2 Copyright (C) 1991-2020 Free Software Foundation, Inc.
252b5132
RH
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
e4e42b45 8 This file is part of BFD, the Binary File Descriptor library.
252b5132 9
e4e42b45
NC
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.
252b5132 14
e4e42b45
NC
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.
252b5132 19
e4e42b45
NC
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. */
252b5132
RH
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
e4e42b45 29 both the internal and external representations. */
252b5132
RH
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
4fbb74a6
AM
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
252b5132
RH
63/* ELF Header */
64
65#define EI_NIDENT 16 /* Size of e_ident[] */
66
67typedef struct elf_internal_ehdr {
39e74e81
AM
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 */
2d4d1975
AM
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 */
252b5132
RH
82} Elf_Internal_Ehdr;
83
252b5132
RH
84/* Program header */
85
86struct elf_internal_phdr {
502794d4
CE
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, file & memory. */
252b5132
RH
95};
96
97typedef struct elf_internal_phdr Elf_Internal_Phdr;
252b5132
RH
98
99/* Section header */
100
101typedef struct elf_internal_shdr {
102 unsigned int sh_name; /* Section name, index in string tbl */
103 unsigned int sh_type; /* Type of section */
104 bfd_vma sh_flags; /* Miscellaneous section attributes */
502794d4
CE
105 bfd_vma sh_addr; /* Section virtual addr at execution in
106 octets. */
107 file_ptr sh_offset; /* Section file offset in octets. */
108 bfd_size_type sh_size; /* Size of section in octets. */
72de5009
AM
109 unsigned int sh_link; /* Index of another section */
110 unsigned int sh_info; /* Additional section information */
111 bfd_vma sh_addralign; /* Section alignment */
252b5132 112 bfd_size_type sh_entsize; /* Entry size if section holds table */
252b5132
RH
113
114 /* The internal rep also has some cached info associated with it. */
115 asection * bfd_section; /* Associated BFD section. */
3f1c8673 116 unsigned char *contents; /* Section contents. */
252b5132
RH
117} Elf_Internal_Shdr;
118
6aa76120
L
119/* Compression header */
120
121typedef struct elf_internal_chdr {
122 unsigned int ch_type; /* Type of compression */
123 bfd_size_type ch_size; /* Size of uncompressed data in bytes */
124 bfd_vma ch_addralign; /* Alignment of uncompressed data */
125} Elf_Internal_Chdr;
126
252b5132
RH
127/* Symbol table entry */
128
129struct elf_internal_sym {
130 bfd_vma st_value; /* Value of the symbol */
131 bfd_vma st_size; /* Associated symbol size */
132 unsigned long st_name; /* Symbol name, index in string tbl */
133 unsigned char st_info; /* Type and binding attributes */
2b99395a 134 unsigned char st_other; /* Visibilty, and target specific */
35fc36a8 135 unsigned char st_target_internal; /* Internal-only information */
9ad5cbcf 136 unsigned int st_shndx; /* Associated section index */
252b5132
RH
137};
138
139typedef struct elf_internal_sym Elf_Internal_Sym;
140
252b5132
RH
141/* Note segments */
142
143typedef struct elf_internal_note {
144 unsigned long namesz; /* Size of entry's owner string */
145 unsigned long descsz; /* Size of the note descriptor */
146 unsigned long type; /* Interpretation of the descriptor */
147 char * namedata; /* Start of the name+desc data */
148 char * descdata; /* Start of the desc data */
149 bfd_vma descpos; /* File offset of the descdata */
150} Elf_Internal_Note;
252b5132
RH
151
152/* Relocation Entries */
153
252b5132
RH
154typedef struct elf_internal_rela {
155 bfd_vma r_offset; /* Location at which to apply the action */
156 bfd_vma r_info; /* Index and Type of relocation */
6587b929 157 bfd_vma r_addend; /* Constant addend used to compute value */
252b5132
RH
158} Elf_Internal_Rela;
159
252b5132
RH
160/* dynamic section structure */
161
162typedef struct elf_internal_dyn {
163 /* This needs to support 64-bit values in elf64. */
164 bfd_vma d_tag; /* entry tag value */
165 union {
166 /* This needs to support 64-bit values in elf64. */
167 bfd_vma d_val;
168 bfd_vma d_ptr;
169 } d_un;
170} Elf_Internal_Dyn;
171
252b5132
RH
172/* This structure appears in a SHT_GNU_verdef section. */
173
174typedef struct elf_internal_verdef {
175 unsigned short vd_version; /* Version number of structure. */
176 unsigned short vd_flags; /* Flags (VER_FLG_*). */
177 unsigned short vd_ndx; /* Version index. */
178 unsigned short vd_cnt; /* Number of verdaux entries. */
179 unsigned long vd_hash; /* Hash of name. */
180 unsigned long vd_aux; /* Offset to verdaux entries. */
181 unsigned long vd_next; /* Offset to next verdef. */
182
183 /* These fields are set up when BFD reads in the structure. FIXME:
184 It would be cleaner to store these in a different structure. */
185 bfd *vd_bfd; /* BFD. */
186 const char *vd_nodename; /* Version name. */
187 struct elf_internal_verdef *vd_nextdef; /* vd_next as pointer. */
188 struct elf_internal_verdaux *vd_auxptr; /* vd_aux as pointer. */
189 unsigned int vd_exp_refno; /* Used by the linker. */
190} Elf_Internal_Verdef;
191
192/* This structure appears in a SHT_GNU_verdef section. */
193
194typedef struct elf_internal_verdaux {
195 unsigned long vda_name; /* String table offset of name. */
196 unsigned long vda_next; /* Offset to next verdaux. */
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 *vda_nodename; /* vda_name as pointer. */
201 struct elf_internal_verdaux *vda_nextptr; /* vda_next as pointer. */
202} Elf_Internal_Verdaux;
203
204/* This structure appears in a SHT_GNU_verneed section. */
205
206typedef struct elf_internal_verneed {
207 unsigned short vn_version; /* Version number of structure. */
208 unsigned short vn_cnt; /* Number of vernaux entries. */
209 unsigned long vn_file; /* String table offset of library name. */
210 unsigned long vn_aux; /* Offset to vernaux entries. */
211 unsigned long vn_next; /* Offset to next verneed. */
212
213 /* These fields are set up when BFD reads in the structure. FIXME:
214 It would be cleaner to store these in a different structure. */
215 bfd *vn_bfd; /* BFD. */
216 const char *vn_filename; /* vn_file as pointer. */
217 struct elf_internal_vernaux *vn_auxptr; /* vn_aux as pointer. */
218 struct elf_internal_verneed *vn_nextref; /* vn_nextref as pointer. */
219} Elf_Internal_Verneed;
220
221/* This structure appears in a SHT_GNU_verneed section. */
222
223typedef struct elf_internal_vernaux {
224 unsigned long vna_hash; /* Hash of dependency name. */
225 unsigned short vna_flags; /* Flags (VER_FLG_*). */
226 unsigned short vna_other; /* Unused. */
227 unsigned long vna_name; /* String table offset to version name. */
228 unsigned long vna_next; /* Offset to next vernaux. */
229
230 /* These fields are set up when BFD reads in the structure. FIXME:
231 It would be cleaner to store these in a different structure. */
232 const char *vna_nodename; /* vna_name as pointer. */
233 struct elf_internal_vernaux *vna_nextptr; /* vna_next as pointer. */
234} Elf_Internal_Vernaux;
235
236/* This structure appears in a SHT_GNU_versym section. This is not a
237 standard ELF structure; ELF just uses Elf32_Half. */
238
239typedef struct elf_internal_versym {
240 unsigned short vs_vers;
241} Elf_Internal_Versym;
242
243/* Structure for syminfo section. */
244typedef struct
245{
246 unsigned short int si_boundto;
247 unsigned short int si_flags;
248} Elf_Internal_Syminfo;
249
eecca0f7
RM
250/* This structure appears on the stack and in NT_AUXV core file notes. */
251typedef struct
252{
253 bfd_vma a_type;
254 bfd_vma a_val;
255} Elf_Internal_Auxv;
256
252b5132 257
252b5132
RH
258/* This structure is used to describe how sections should be assigned
259 to program segments. */
260
261struct elf_segment_map
262{
263 /* Next program segment. */
264 struct elf_segment_map *next;
265 /* Program segment type. */
266 unsigned long p_type;
267 /* Program segment flags. */
268 unsigned long p_flags;
269 /* Program segment physical address. */
270 bfd_vma p_paddr;
502794d4 271 /* Program segment virtual address offset from section vma in bytes. */
3271a814 272 bfd_vma p_vaddr_offset;
3f570048
AM
273 /* Program segment alignment. */
274 bfd_vma p_align;
b10a8ae0
L
275 /* Segment size in file and memory */
276 bfd_vma p_size;
252b5132
RH
277 /* Whether the p_flags field is valid; if not, the flags are based
278 on the section flags. */
279 unsigned int p_flags_valid : 1;
280 /* Whether the p_paddr field is valid; if not, the physical address
281 is based on the section lma values. */
282 unsigned int p_paddr_valid : 1;
3f570048
AM
283 /* Whether the p_align field is valid; if not, PT_LOAD segment
284 alignment is based on the default maximum page size. */
285 unsigned int p_align_valid : 1;
b10a8ae0
L
286 /* Whether the p_size field is valid; if not, the size are based
287 on the section sizes. */
288 unsigned int p_size_valid : 1;
252b5132
RH
289 /* Whether this segment includes the file header. */
290 unsigned int includes_filehdr : 1;
291 /* Whether this segment includes the program headers. */
292 unsigned int includes_phdrs : 1;
30fe1832
AM
293 /* Assume this PT_LOAD header has an lma of zero when sorting
294 headers before assigning file offsets. PT_LOAD headers with this
295 flag set are placed after one with includes_filehdr set, and
296 before PT_LOAD headers without this flag set. */
297 unsigned int no_sort_lma : 1;
298 /* Index holding original order before sorting segments. */
299 unsigned int idx;
252b5132
RH
300 /* Number of sections (may be 0). */
301 unsigned int count;
302 /* Sections. Actual number of elements is in count field. */
303 asection *sections[1];
304};
305
1224efb8
L
306/* .tbss is special. It doesn't contribute memory space to normal
307 segments and it doesn't take file space in normal segments. */
f4638467
AM
308#define ELF_TBSS_SPECIAL(sec_hdr, segment) \
309 (((sec_hdr)->sh_flags & SHF_TLS) != 0 \
310 && (sec_hdr)->sh_type == SHT_NOBITS \
311 && (segment)->p_type != PT_TLS)
312
1224efb8 313#define ELF_SECTION_SIZE(sec_hdr, segment) \
f4638467
AM
314 (ELF_TBSS_SPECIAL(sec_hdr, segment) ? 0 : (sec_hdr)->sh_size)
315
316/* Decide if the section SEC_HDR is in SEGMENT. If CHECK_VMA, then
317 VMAs are checked for alloc sections. If STRICT, then a zero size
318 section won't match at the end of a segment, unless the segment
15a44d57 319 is also zero size. Regardless of STRICT and CHECK_VMA, zero size
95475e5d
L
320 sections won't match at the start or end of PT_DYNAMIC nor PT_NOTE,
321 unless PT_DYNAMIC and PT_NOTE are themselves zero sized. */
f4638467
AM
322#define ELF_SECTION_IN_SEGMENT_1(sec_hdr, segment, check_vma, strict) \
323 ((/* Only PT_LOAD, PT_GNU_RELRO and PT_TLS segments can contain \
324 SHF_TLS sections. */ \
325 ((((sec_hdr)->sh_flags & SHF_TLS) != 0) \
9a83a553
AM
326 && ((segment)->p_type == PT_TLS \
327 || (segment)->p_type == PT_GNU_RELRO \
328 || (segment)->p_type == PT_LOAD)) \
f4638467
AM
329 /* PT_TLS segment contains only SHF_TLS sections, PT_PHDR no \
330 sections at all. */ \
9a83a553
AM
331 || (((sec_hdr)->sh_flags & SHF_TLS) == 0 \
332 && (segment)->p_type != PT_TLS \
333 && (segment)->p_type != PT_PHDR)) \
44bd1acd
AM
334 /* PT_LOAD and similar segments only have SHF_ALLOC sections. */ \
335 && !(((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \
336 && ((segment)->p_type == PT_LOAD \
337 || (segment)->p_type == PT_DYNAMIC \
338 || (segment)->p_type == PT_GNU_EH_FRAME \
7137a1ed 339 || (segment)->p_type == PT_GNU_STACK \
44bd1acd 340 || (segment)->p_type == PT_GNU_RELRO \
7137a1ed
AM
341 || ((segment)->p_type >= PT_GNU_MBIND_LO \
342 && (segment)->p_type <= PT_GNU_MBIND_HI))) \
f4638467
AM
343 /* Any section besides one of type SHT_NOBITS must have file \
344 offsets within the segment. */ \
9a83a553
AM
345 && ((sec_hdr)->sh_type == SHT_NOBITS \
346 || ((bfd_vma) (sec_hdr)->sh_offset >= (segment)->p_offset \
f4638467
AM
347 && (!(strict) \
348 || ((sec_hdr)->sh_offset - (segment)->p_offset \
349 <= (segment)->p_filesz - 1)) \
350 && (((sec_hdr)->sh_offset - (segment)->p_offset \
351 + ELF_SECTION_SIZE(sec_hdr, segment)) \
352 <= (segment)->p_filesz))) \
2393a7e3 353 /* SHF_ALLOC sections must have VMAs within the segment. */ \
9a83a553
AM
354 && (!(check_vma) \
355 || ((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \
356 || ((sec_hdr)->sh_addr >= (segment)->p_vaddr \
f4638467
AM
357 && (!(strict) \
358 || ((sec_hdr)->sh_addr - (segment)->p_vaddr \
359 <= (segment)->p_memsz - 1)) \
360 && (((sec_hdr)->sh_addr - (segment)->p_vaddr \
361 + ELF_SECTION_SIZE(sec_hdr, segment)) \
15a44d57 362 <= (segment)->p_memsz))) \
95475e5d
L
363 /* No zero size sections at start or end of PT_DYNAMIC nor \
364 PT_NOTE. */ \
365 && (((segment)->p_type != PT_DYNAMIC \
366 && (segment)->p_type != PT_NOTE) \
15a44d57
AM
367 || (sec_hdr)->sh_size != 0 \
368 || (segment)->p_memsz == 0 \
369 || (((sec_hdr)->sh_type == SHT_NOBITS \
370 || ((bfd_vma) (sec_hdr)->sh_offset > (segment)->p_offset \
371 && ((sec_hdr)->sh_offset - (segment)->p_offset \
372 < (segment)->p_filesz))) \
373 && (((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \
374 || ((sec_hdr)->sh_addr > (segment)->p_vaddr \
375 && ((sec_hdr)->sh_addr - (segment)->p_vaddr \
376 < (segment)->p_memsz))))))
9a83a553
AM
377
378#define ELF_SECTION_IN_SEGMENT(sec_hdr, segment) \
f4638467
AM
379 (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 0))
380
381#define ELF_SECTION_IN_SEGMENT_STRICT(sec_hdr, segment) \
382 (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 1))
84d1d650 383
252b5132 384#endif /* _ELF_INTERNAL_H */