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