Commit | Line | Data |
---|---|---|
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 | ||
67 | typedef 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 | ||
86 | struct 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. */ | |
66631823 CE |
94 | bfd_vma p_align; /* Segment alignment in bytes, file |
95 | & memory */ | |
252b5132 RH |
96 | }; |
97 | ||
98 | typedef struct elf_internal_phdr Elf_Internal_Phdr; | |
252b5132 RH |
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 */ | |
502794d4 CE |
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. */ | |
72de5009 AM |
110 | unsigned int sh_link; /* Index of another section */ |
111 | unsigned int sh_info; /* Additional section information */ | |
112 | bfd_vma sh_addralign; /* Section alignment */ | |
252b5132 | 113 | bfd_size_type sh_entsize; /* Entry size if section holds table */ |
252b5132 RH |
114 | |
115 | /* The internal rep also has some cached info associated with it. */ | |
116 | asection * bfd_section; /* Associated BFD section. */ | |
3f1c8673 | 117 | unsigned char *contents; /* Section contents. */ |
252b5132 RH |
118 | } Elf_Internal_Shdr; |
119 | ||
6aa76120 L |
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 | ||
252b5132 RH |
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 */ | |
2b99395a | 135 | unsigned char st_other; /* Visibilty, and target specific */ |
35fc36a8 | 136 | unsigned char st_target_internal; /* Internal-only information */ |
9ad5cbcf | 137 | unsigned int st_shndx; /* Associated section index */ |
252b5132 RH |
138 | }; |
139 | ||
140 | typedef struct elf_internal_sym Elf_Internal_Sym; | |
141 | ||
252b5132 RH |
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; | |
252b5132 RH |
152 | |
153 | /* Relocation Entries */ | |
154 | ||
252b5132 RH |
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 */ | |
6587b929 | 158 | bfd_vma r_addend; /* Constant addend used to compute value */ |
252b5132 RH |
159 | } Elf_Internal_Rela; |
160 | ||
252b5132 RH |
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 | ||
252b5132 RH |
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 | ||
eecca0f7 RM |
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 | ||
252b5132 | 258 | |
252b5132 RH |
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; | |
66631823 | 270 | /* Program segment physical address in octets. */ |
252b5132 | 271 | bfd_vma p_paddr; |
502794d4 | 272 | /* Program segment virtual address offset from section vma in bytes. */ |
3271a814 | 273 | bfd_vma p_vaddr_offset; |
3f570048 AM |
274 | /* Program segment alignment. */ |
275 | bfd_vma p_align; | |
66631823 | 276 | /* Segment size in file and memory in octets. */ |
b10a8ae0 | 277 | bfd_vma p_size; |
252b5132 RH |
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; | |
3f570048 AM |
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; | |
b10a8ae0 L |
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; | |
252b5132 RH |
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; | |
30fe1832 AM |
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; | |
252b5132 RH |
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 | ||
1224efb8 L |
307 | /* .tbss is special. It doesn't contribute memory space to normal |
308 | segments and it doesn't take file space in normal segments. */ | |
f4638467 AM |
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 | ||
1224efb8 | 314 | #define ELF_SECTION_SIZE(sec_hdr, segment) \ |
f4638467 AM |
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 | |
15a44d57 | 320 | is also zero size. Regardless of STRICT and CHECK_VMA, zero size |
95475e5d L |
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. */ | |
f4638467 AM |
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) \ | |
9a83a553 AM |
327 | && ((segment)->p_type == PT_TLS \ |
328 | || (segment)->p_type == PT_GNU_RELRO \ | |
329 | || (segment)->p_type == PT_LOAD)) \ | |
f4638467 AM |
330 | /* PT_TLS segment contains only SHF_TLS sections, PT_PHDR no \ |
331 | sections at all. */ \ | |
9a83a553 AM |
332 | || (((sec_hdr)->sh_flags & SHF_TLS) == 0 \ |
333 | && (segment)->p_type != PT_TLS \ | |
334 | && (segment)->p_type != PT_PHDR)) \ | |
44bd1acd AM |
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 \ | |
7137a1ed | 340 | || (segment)->p_type == PT_GNU_STACK \ |
44bd1acd | 341 | || (segment)->p_type == PT_GNU_RELRO \ |
7137a1ed AM |
342 | || ((segment)->p_type >= PT_GNU_MBIND_LO \ |
343 | && (segment)->p_type <= PT_GNU_MBIND_HI))) \ | |
f4638467 AM |
344 | /* Any section besides one of type SHT_NOBITS must have file \ |
345 | offsets within the segment. */ \ | |
9a83a553 AM |
346 | && ((sec_hdr)->sh_type == SHT_NOBITS \ |
347 | || ((bfd_vma) (sec_hdr)->sh_offset >= (segment)->p_offset \ | |
f4638467 AM |
348 | && (!(strict) \ |
349 | || ((sec_hdr)->sh_offset - (segment)->p_offset \ | |
350 | <= (segment)->p_filesz - 1)) \ | |
351 | && (((sec_hdr)->sh_offset - (segment)->p_offset \ | |
352 | + ELF_SECTION_SIZE(sec_hdr, segment)) \ | |
353 | <= (segment)->p_filesz))) \ | |
2393a7e3 | 354 | /* SHF_ALLOC sections must have VMAs within the segment. */ \ |
9a83a553 AM |
355 | && (!(check_vma) \ |
356 | || ((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \ | |
357 | || ((sec_hdr)->sh_addr >= (segment)->p_vaddr \ | |
f4638467 AM |
358 | && (!(strict) \ |
359 | || ((sec_hdr)->sh_addr - (segment)->p_vaddr \ | |
360 | <= (segment)->p_memsz - 1)) \ | |
361 | && (((sec_hdr)->sh_addr - (segment)->p_vaddr \ | |
362 | + ELF_SECTION_SIZE(sec_hdr, segment)) \ | |
15a44d57 | 363 | <= (segment)->p_memsz))) \ |
95475e5d L |
364 | /* No zero size sections at start or end of PT_DYNAMIC nor \ |
365 | PT_NOTE. */ \ | |
366 | && (((segment)->p_type != PT_DYNAMIC \ | |
367 | && (segment)->p_type != PT_NOTE) \ | |
15a44d57 AM |
368 | || (sec_hdr)->sh_size != 0 \ |
369 | || (segment)->p_memsz == 0 \ | |
370 | || (((sec_hdr)->sh_type == SHT_NOBITS \ | |
371 | || ((bfd_vma) (sec_hdr)->sh_offset > (segment)->p_offset \ | |
372 | && ((sec_hdr)->sh_offset - (segment)->p_offset \ | |
373 | < (segment)->p_filesz))) \ | |
374 | && (((sec_hdr)->sh_flags & SHF_ALLOC) == 0 \ | |
375 | || ((sec_hdr)->sh_addr > (segment)->p_vaddr \ | |
376 | && ((sec_hdr)->sh_addr - (segment)->p_vaddr \ | |
377 | < (segment)->p_memsz)))))) | |
9a83a553 AM |
378 | |
379 | #define ELF_SECTION_IN_SEGMENT(sec_hdr, segment) \ | |
f4638467 AM |
380 | (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 0)) |
381 | ||
382 | #define ELF_SECTION_IN_SEGMENT_STRICT(sec_hdr, segment) \ | |
383 | (ELF_SECTION_IN_SEGMENT_1 (sec_hdr, segment, 1, 1)) | |
84d1d650 | 384 | |
252b5132 | 385 | #endif /* _ELF_INTERNAL_H */ |