1 /* ELF support for BFD.
2    Copyright (C) 1991-2020 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 externally by the BFD library.
27    I.E. it describes the in-file 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 /* The 64-bit stuff is kind of random.  Perhaps someone will publish a
32    spec someday.  */
33 
34 #ifndef _ELF_EXTERNAL_H
35 #define _ELF_EXTERNAL_H
36 
37 /* Special section indices, which may show up in st_shndx fields, among
38    other places.  */
39 
40 #define SHN_LORESERVE	0xFF00		/* Begin range of reserved indices */
41 #define SHN_LOPROC	0xFF00		/* Begin range of appl-specific */
42 #define SHN_HIPROC	0xFF1F		/* End range of appl-specific */
43 #define SHN_LOOS	0xFF20		/* OS specific semantics, lo */
44 #define SHN_HIOS	0xFF3F		/* OS specific semantics, hi */
45 #define SHN_ABS		0xFFF1		/* Associated symbol is absolute */
46 #define SHN_COMMON	0xFFF2		/* Associated symbol is in common */
47 #define SHN_XINDEX	0xFFFF		/* Section index is held elsewhere */
48 #define SHN_HIRESERVE	0xFFFF		/* End range of reserved indices */
49 
50 /* ELF Header (32-bit implementations) */
51 
52 typedef struct {
53   unsigned char	e_ident[16];		/* ELF "magic number" */
54   unsigned char	e_type[2];		/* Identifies object file type */
55   unsigned char	e_machine[2];		/* Specifies required architecture */
56   unsigned char	e_version[4];		/* Identifies object file version */
57   unsigned char	e_entry[4];		/* Entry point virtual address */
58   unsigned char	e_phoff[4];		/* Program header table file offset */
59   unsigned char	e_shoff[4];		/* Section header table file offset */
60   unsigned char	e_flags[4];		/* Processor-specific flags */
61   unsigned char	e_ehsize[2];		/* ELF header size in bytes */
62   unsigned char	e_phentsize[2];		/* Program header table entry size */
63   unsigned char	e_phnum[2];		/* Program header table entry count */
64   unsigned char	e_shentsize[2];		/* Section header table entry size */
65   unsigned char	e_shnum[2];		/* Section header table entry count */
66   unsigned char	e_shstrndx[2];		/* Section header string table index */
67 } Elf32_External_Ehdr;
68 
69 typedef struct {
70   unsigned char	e_ident[16];		/* ELF "magic number" */
71   unsigned char	e_type[2];		/* Identifies object file type */
72   unsigned char	e_machine[2];		/* Specifies required architecture */
73   unsigned char	e_version[4];		/* Identifies object file version */
74   unsigned char	e_entry[8];		/* Entry point virtual address */
75   unsigned char	e_phoff[8];		/* Program header table file offset */
76   unsigned char	e_shoff[8];		/* Section header table file offset */
77   unsigned char	e_flags[4];		/* Processor-specific flags */
78   unsigned char	e_ehsize[2];		/* ELF header size in bytes */
79   unsigned char	e_phentsize[2];		/* Program header table entry size */
80   unsigned char	e_phnum[2];		/* Program header table entry count */
81   unsigned char	e_shentsize[2];		/* Section header table entry size */
82   unsigned char	e_shnum[2];		/* Section header table entry count */
83   unsigned char	e_shstrndx[2];		/* Section header string table index */
84 } Elf64_External_Ehdr;
85 
86 /* Program header */
87 
88 typedef struct {
89   unsigned char	p_type[4];		/* Identifies program segment type */
90   unsigned char	p_offset[4];		/* Segment file offset */
91   unsigned char	p_vaddr[4];		/* Segment virtual address */
92   unsigned char	p_paddr[4];		/* Segment physical address */
93   unsigned char	p_filesz[4];		/* Segment size in file */
94   unsigned char	p_memsz[4];		/* Segment size in memory */
95   unsigned char	p_flags[4];		/* Segment flags */
96   unsigned char	p_align[4];		/* Segment alignment, file & memory */
97 } Elf32_External_Phdr;
98 
99 typedef struct {
100   unsigned char	p_type[4];		/* Identifies program segment type */
101   unsigned char	p_flags[4];		/* Segment flags */
102   unsigned char	p_offset[8];		/* Segment file offset */
103   unsigned char	p_vaddr[8];		/* Segment virtual address */
104   unsigned char	p_paddr[8];		/* Segment physical address */
105   unsigned char	p_filesz[8];		/* Segment size in file */
106   unsigned char	p_memsz[8];		/* Segment size in memory */
107   unsigned char	p_align[8];		/* Segment alignment, file & memory */
108 } Elf64_External_Phdr;
109 
110 /* Section header */
111 
112 typedef struct {
113   unsigned char	sh_name[4];		/* Section name, index in string tbl */
114   unsigned char	sh_type[4];		/* Type of section */
115   unsigned char	sh_flags[4];		/* Miscellaneous section attributes */
116   unsigned char	sh_addr[4];		/* Section virtual addr at execution */
117   unsigned char	sh_offset[4];		/* Section file offset */
118   unsigned char	sh_size[4];		/* Size of section in bytes */
119   unsigned char	sh_link[4];		/* Index of another section */
120   unsigned char	sh_info[4];		/* Additional section information */
121   unsigned char	sh_addralign[4];	/* Section alignment */
122   unsigned char	sh_entsize[4];		/* Entry size if section holds table */
123 } Elf32_External_Shdr;
124 
125 typedef struct {
126   unsigned char	sh_name[4];		/* Section name, index in string tbl */
127   unsigned char	sh_type[4];		/* Type of section */
128   unsigned char	sh_flags[8];		/* Miscellaneous section attributes */
129   unsigned char	sh_addr[8];		/* Section virtual addr at execution */
130   unsigned char	sh_offset[8];		/* Section file offset */
131   unsigned char	sh_size[8];		/* Size of section in bytes */
132   unsigned char	sh_link[4];		/* Index of another section */
133   unsigned char	sh_info[4];		/* Additional section information */
134   unsigned char	sh_addralign[8];	/* Section alignment */
135   unsigned char	sh_entsize[8];		/* Entry size if section holds table */
136 } Elf64_External_Shdr;
137 
138 /* Compression header */
139 
140 typedef struct {
141   unsigned char	ch_type[4];		/* Type of compression */
142   unsigned char	ch_size[4];		/* Size of uncompressed data in bytes */
143   unsigned char	ch_addralign[4];	/* Alignment of uncompressed data  */
144 } Elf32_External_Chdr;
145 
146 typedef struct {
147   unsigned char	ch_type[4];		/* Type of compression */
148   unsigned char	ch_reserved[4];		/* Padding */
149   unsigned char	ch_size[8];		/* Size of uncompressed data in bytes */
150   unsigned char	ch_addralign[8];	/* Alignment of uncompressed data  */
151 } Elf64_External_Chdr;
152 
153 /* Symbol table entry */
154 
155 typedef struct {
156   unsigned char	st_name[4];		/* Symbol name, index in string tbl */
157   unsigned char	st_value[4];		/* Value of the symbol */
158   unsigned char	st_size[4];		/* Associated symbol size */
159   unsigned char	st_info[1];		/* Type and binding attributes */
160   unsigned char	st_other[1];		/* No defined meaning, 0 */
161   unsigned char	st_shndx[2];		/* Associated section index */
162 } Elf32_External_Sym;
163 
164 typedef struct {
165   unsigned char	st_name[4];		/* Symbol name, index in string tbl */
166   unsigned char	st_info[1];		/* Type and binding attributes */
167   unsigned char	st_other[1];		/* No defined meaning, 0 */
168   unsigned char	st_shndx[2];		/* Associated section index */
169   unsigned char	st_value[8];		/* Value of the symbol */
170   unsigned char	st_size[8];		/* Associated symbol size */
171 } Elf64_External_Sym;
172 
173 typedef struct {
174   unsigned char est_shndx[4];		/* Section index */
175 } Elf_External_Sym_Shndx;
176 
177 /* Note segments */
178 
179 typedef struct {
180   unsigned char	namesz[4];		/* Size of entry's owner string */
181   unsigned char	descsz[4];		/* Size of the note descriptor */
182   unsigned char	type[4];		/* Interpretation of the descriptor */
183   char		name[1];		/* Start of the name+desc data */
184 } Elf_External_Note;
185 
186 /* Align an address upward to a boundary, expressed as a number of bytes.
187    E.g. align to an 8-byte boundary with argument of 8.  */
188 #define ELF_ALIGN_UP(addr, boundary) \
189   (((bfd_vma) (addr) + ((boundary) - 1)) & ~ (bfd_vma) ((boundary) -1))
190 
191 /* Compute the offset of the note descriptor from size of note entry's
192    owner string and note alignment.  */
193 #define ELF_NOTE_DESC_OFFSET(namesz, align) \
194   ELF_ALIGN_UP (offsetof (Elf_External_Note, name) + (namesz), (align))
195 
196 /* Compute the offset of the next note entry from size of note entry's
197    owner string, size of the note descriptor and note alignment.  */
198 #define ELF_NOTE_NEXT_OFFSET(namesz, descsz, align) \
199   ELF_ALIGN_UP (ELF_NOTE_DESC_OFFSET ((namesz), (align)) + (descsz), \
200 		(align))
201 
202 /* Relocation Entries */
203 typedef struct {
204   unsigned char r_offset[4];	/* Location at which to apply the action */
205   unsigned char	r_info[4];	/* index and type of relocation */
206 } Elf32_External_Rel;
207 
208 typedef struct {
209   unsigned char r_offset[4];	/* Location at which to apply the action */
210   unsigned char	r_info[4];	/* index and type of relocation */
211   unsigned char	r_addend[4];	/* Constant addend used to compute value */
212 } Elf32_External_Rela;
213 
214 typedef struct {
215   unsigned char r_offset[8];	/* Location at which to apply the action */
216   unsigned char	r_info[8];	/* index and type of relocation */
217 } Elf64_External_Rel;
218 
219 typedef struct {
220   unsigned char r_offset[8];	/* Location at which to apply the action */
221   unsigned char	r_info[8];	/* index and type of relocation */
222   unsigned char	r_addend[8];	/* Constant addend used to compute value */
223 } Elf64_External_Rela;
224 
225 /* dynamic section structure */
226 
227 typedef struct {
228   unsigned char	d_tag[4];		/* entry tag value */
229   union {
230     unsigned char	d_val[4];
231     unsigned char	d_ptr[4];
232   } d_un;
233 } Elf32_External_Dyn;
234 
235 typedef struct {
236   unsigned char	d_tag[8];		/* entry tag value */
237   union {
238     unsigned char	d_val[8];
239     unsigned char	d_ptr[8];
240   } d_un;
241 } Elf64_External_Dyn;
242 
243 /* The version structures are currently size independent.  They are
244    named without a 32 or 64.  If that ever changes, these structures
245    will need to be renamed.  */
246 
247 /* This structure appears in a SHT_GNU_verdef section.  */
248 
249 typedef struct {
250   unsigned char		vd_version[2];
251   unsigned char		vd_flags[2];
252   unsigned char		vd_ndx[2];
253   unsigned char		vd_cnt[2];
254   unsigned char		vd_hash[4];
255   unsigned char		vd_aux[4];
256   unsigned char		vd_next[4];
257 } Elf_External_Verdef;
258 
259 /* This structure appears in a SHT_GNU_verdef section.  */
260 
261 typedef struct {
262   unsigned char		vda_name[4];
263   unsigned char		vda_next[4];
264 } Elf_External_Verdaux;
265 
266 /* This structure appears in a SHT_GNU_verneed section.  */
267 
268 typedef struct {
269   unsigned char		vn_version[2];
270   unsigned char		vn_cnt[2];
271   unsigned char		vn_file[4];
272   unsigned char		vn_aux[4];
273   unsigned char		vn_next[4];
274 } Elf_External_Verneed;
275 
276 /* This structure appears in a SHT_GNU_verneed section.  */
277 
278 typedef struct {
279   unsigned char		vna_hash[4];
280   unsigned char		vna_flags[2];
281   unsigned char		vna_other[2];
282   unsigned char		vna_name[4];
283   unsigned char		vna_next[4];
284 } Elf_External_Vernaux;
285 
286 /* This structure appears in a SHT_GNU_versym section.  This is not a
287    standard ELF structure; ELF just uses Elf32_Half.  */
288 
289 typedef struct {
290   unsigned char		vs_vers[2];
291 } ATTRIBUTE_PACKED  Elf_External_Versym;
292 
293 /* Structure for syminfo section.  */
294 typedef struct
295 {
296   unsigned char		si_boundto[2];
297   unsigned char		si_flags[2];
298 } Elf_External_Syminfo;
299 
300 
301 /* This structure appears on the stack and in NT_AUXV core file notes.  */
302 typedef struct
303 {
304   unsigned char		a_type[4];
305   unsigned char		a_val[4];
306 } Elf32_External_Auxv;
307 
308 typedef struct
309 {
310   unsigned char		a_type[8];
311   unsigned char		a_val[8];
312 } Elf64_External_Auxv;
313 
314 /* Size of SHT_GROUP section entry.  */
315 
316 #define GRP_ENTRY_SIZE		4
317 
318 #endif /* _ELF_EXTERNAL_H */
319