xref: /netbsd-src/external/bsd/file/dist/src/readelf.h (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /*	$NetBSD: readelf.h,v 1.1.1.7 2017/02/10 17:42:57 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Christos Zoulas 2003.
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 /*
30  * @(#)Id: readelf.h,v 1.9 2002/05/16 18:45:56 christos Exp
31  *
32  * Provide elf data structures for non-elf machines, allowing file
33  * non-elf hosts to determine if an elf binary is stripped.
34  * Note: cobbled from the linux header file, with modifications
35  */
36 #ifndef __fake_elf_h__
37 #define	__fake_elf_h__
38 
39 #if HAVE_STDINT_H
40 #include <stdint.h>
41 #endif
42 
43 typedef uint32_t	Elf32_Addr;
44 typedef uint32_t	Elf32_Off;
45 typedef uint16_t	Elf32_Half;
46 typedef uint32_t	Elf32_Word;
47 typedef uint8_t		Elf32_Char;
48 
49 typedef	uint64_t 	Elf64_Addr;
50 typedef	uint64_t 	Elf64_Off;
51 typedef uint64_t 	Elf64_Xword;
52 typedef uint16_t	Elf64_Half;
53 typedef uint32_t	Elf64_Word;
54 typedef uint8_t		Elf64_Char;
55 
56 #define	EI_NIDENT	16
57 
58 typedef struct {
59 	Elf32_Word	a_type;		/* 32-bit id */
60 	Elf32_Word	a_v;		/* 32-bit id */
61 } Aux32Info;
62 
63 typedef struct {
64 	Elf64_Xword	a_type;		/* 64-bit id */
65 	Elf64_Xword	a_v;		/* 64-bit id */
66 } Aux64Info;
67 
68 #define AT_NULL   0     /* end of vector */
69 #define AT_IGNORE 1     /* entry should be ignored */
70 #define AT_EXECFD 2     /* file descriptor of program */
71 #define AT_PHDR   3     /* program headers for program */
72 #define AT_PHENT  4     /* size of program header entry */
73 #define AT_PHNUM  5     /* number of program headers */
74 #define AT_PAGESZ 6     /* system page size */
75 #define AT_BASE   7     /* base address of interpreter */
76 #define AT_FLAGS  8     /* flags */
77 #define AT_ENTRY  9     /* entry point of program */
78 #define AT_LINUX_NOTELF 10    /* program is not ELF */
79 #define AT_LINUX_UID    11    /* real uid */
80 #define AT_LINUX_EUID   12    /* effective uid */
81 #define AT_LINUX_GID    13    /* real gid */
82 #define AT_LINUX_EGID   14    /* effective gid */
83 #define AT_LINUX_PLATFORM 15  /* string identifying CPU for optimizations */
84 #define AT_LINUX_HWCAP  16    /* arch dependent hints at CPU capabilities */
85 #define AT_LINUX_CLKTCK 17    /* frequency at which times() increments */
86 /* AT_* values 18 through 22 are reserved */
87 #define AT_LINUX_SECURE 23   /* secure mode boolean */
88 #define AT_LINUX_BASE_PLATFORM 24     /* string identifying real platform, may
89                                  * differ from AT_PLATFORM. */
90 #define AT_LINUX_RANDOM 25    /* address of 16 random bytes */
91 #define AT_LINUX_HWCAP2 26    /* extension of AT_HWCAP */
92 #define AT_LINUX_EXECFN 31   /* filename of program */
93 
94 typedef struct {
95     Elf32_Char	e_ident[EI_NIDENT];
96     Elf32_Half	e_type;
97     Elf32_Half	e_machine;
98     Elf32_Word	e_version;
99     Elf32_Addr	e_entry;  /* Entry point */
100     Elf32_Off	e_phoff;
101     Elf32_Off	e_shoff;
102     Elf32_Word	e_flags;
103     Elf32_Half	e_ehsize;
104     Elf32_Half	e_phentsize;
105     Elf32_Half	e_phnum;
106     Elf32_Half	e_shentsize;
107     Elf32_Half	e_shnum;
108     Elf32_Half	e_shstrndx;
109 } Elf32_Ehdr;
110 
111 typedef struct {
112     Elf64_Char	e_ident[EI_NIDENT];
113     Elf64_Half	e_type;
114     Elf64_Half	e_machine;
115     Elf64_Word	e_version;
116     Elf64_Addr	e_entry;  /* Entry point */
117     Elf64_Off	e_phoff;
118     Elf64_Off	e_shoff;
119     Elf64_Word	e_flags;
120     Elf64_Half	e_ehsize;
121     Elf64_Half	e_phentsize;
122     Elf64_Half	e_phnum;
123     Elf64_Half	e_shentsize;
124     Elf64_Half	e_shnum;
125     Elf64_Half	e_shstrndx;
126 } Elf64_Ehdr;
127 
128 /* e_type */
129 #define	ET_REL		1
130 #define	ET_EXEC		2
131 #define	ET_DYN		3
132 #define	ET_CORE		4
133 
134 /* e_machine (used only for SunOS 5.x hardware capabilities) */
135 #define	EM_SPARC	2
136 #define	EM_386		3
137 #define	EM_SPARC32PLUS	18
138 #define	EM_SPARCV9	43
139 #define	EM_IA_64	50
140 #define	EM_AMD64	62
141 
142 /* sh_type */
143 #define	SHT_SYMTAB	2
144 #define	SHT_NOTE	7
145 #define	SHT_DYNSYM	11
146 #define	SHT_SUNW_cap	0x6ffffff5	/* SunOS 5.x hw/sw capabilites */
147 
148 /* elf type */
149 #define	ELFDATANONE	0		/* e_ident[EI_DATA] */
150 #define	ELFDATA2LSB	1
151 #define	ELFDATA2MSB	2
152 
153 /* elf class */
154 #define	ELFCLASSNONE	0
155 #define	ELFCLASS32	1
156 #define	ELFCLASS64	2
157 
158 /* magic number */
159 #define	EI_MAG0		0		/* e_ident[] indexes */
160 #define	EI_MAG1		1
161 #define	EI_MAG2		2
162 #define	EI_MAG3		3
163 #define	EI_CLASS	4
164 #define	EI_DATA		5
165 #define	EI_VERSION	6
166 #define	EI_PAD		7
167 
168 #define	ELFMAG0		0x7f		/* EI_MAG */
169 #define	ELFMAG1		'E'
170 #define	ELFMAG2		'L'
171 #define	ELFMAG3		'F'
172 #define	ELFMAG		"\177ELF"
173 
174 #define	OLFMAG1		'O'
175 #define	OLFMAG		"\177OLF"
176 
177 typedef struct {
178     Elf32_Word	p_type;
179     Elf32_Off	p_offset;
180     Elf32_Addr	p_vaddr;
181     Elf32_Addr	p_paddr;
182     Elf32_Word	p_filesz;
183     Elf32_Word	p_memsz;
184     Elf32_Word	p_flags;
185     Elf32_Word	p_align;
186 } Elf32_Phdr;
187 
188 typedef struct {
189     Elf64_Word	p_type;
190     Elf64_Word	p_flags;
191     Elf64_Off	p_offset;
192     Elf64_Addr	p_vaddr;
193     Elf64_Addr	p_paddr;
194     Elf64_Xword	p_filesz;
195     Elf64_Xword	p_memsz;
196     Elf64_Xword	p_align;
197 } Elf64_Phdr;
198 
199 #define	PT_NULL		0		/* p_type */
200 #define	PT_LOAD		1
201 #define	PT_DYNAMIC	2
202 #define	PT_INTERP	3
203 #define	PT_NOTE		4
204 #define	PT_SHLIB	5
205 #define	PT_PHDR		6
206 #define	PT_NUM		7
207 
208 typedef struct {
209     Elf32_Word	sh_name;
210     Elf32_Word	sh_type;
211     Elf32_Word	sh_flags;
212     Elf32_Addr	sh_addr;
213     Elf32_Off	sh_offset;
214     Elf32_Word	sh_size;
215     Elf32_Word	sh_link;
216     Elf32_Word	sh_info;
217     Elf32_Word	sh_addralign;
218     Elf32_Word	sh_entsize;
219 } Elf32_Shdr;
220 
221 typedef struct {
222     Elf64_Word	sh_name;
223     Elf64_Word	sh_type;
224     Elf64_Off	sh_flags;
225     Elf64_Addr	sh_addr;
226     Elf64_Off	sh_offset;
227     Elf64_Off	sh_size;
228     Elf64_Word	sh_link;
229     Elf64_Word	sh_info;
230     Elf64_Off	sh_addralign;
231     Elf64_Off	sh_entsize;
232 } Elf64_Shdr;
233 
234 #define	NT_NETBSD_CORE_PROCINFO		1
235 
236 /* Note header in a PT_NOTE section */
237 typedef struct elf_note {
238     Elf32_Word	n_namesz;	/* Name size */
239     Elf32_Word	n_descsz;	/* Content size */
240     Elf32_Word	n_type;		/* Content type */
241 } Elf32_Nhdr;
242 
243 typedef struct {
244     Elf64_Word	n_namesz;
245     Elf64_Word	n_descsz;
246     Elf64_Word	n_type;
247 } Elf64_Nhdr;
248 
249 /* Notes used in ET_CORE */
250 #define	NT_PRSTATUS	1
251 #define	NT_PRFPREG	2
252 #define	NT_PRPSINFO	3
253 #define	NT_PRXREG	4
254 #define	NT_TASKSTRUCT	4
255 #define	NT_PLATFORM	5
256 #define	NT_AUXV		6
257 
258 /* Note types used in executables */
259 /* NetBSD executables (name = "NetBSD") */
260 #define	NT_NETBSD_VERSION	1
261 #define	NT_NETBSD_EMULATION	2
262 #define	NT_FREEBSD_VERSION	1
263 #define	NT_OPENBSD_VERSION	1
264 #define	NT_DRAGONFLY_VERSION	1
265 /*
266  * GNU executables (name = "GNU")
267  * word[0]: GNU OS tags
268  * word[1]: major version
269  * word[2]: minor version
270  * word[3]: tiny version
271  */
272 #define	NT_GNU_VERSION		1
273 
274 /* GNU OS tags */
275 #define	GNU_OS_LINUX	0
276 #define	GNU_OS_HURD	1
277 #define	GNU_OS_SOLARIS	2
278 #define	GNU_OS_KFREEBSD	3
279 #define	GNU_OS_KNETBSD	4
280 
281 /*
282  * GNU Hardware capability information
283  * word[0]: Number of entries
284  * word[1]: Bitmask of enabled entries
285  * Followed by a byte id, and a NUL terminated string per entry
286  */
287 #define	NT_GNU_HWCAP		2
288 
289 /*
290  * GNU Build ID generated by ld
291  * 160 bit SHA1 [default]
292  * 128 bit md5 or uuid
293  */
294 #define	NT_GNU_BUILD_ID		3
295 
296 /*
297  * NetBSD-specific note type: PaX.
298  * There should be 1 NOTE per executable.
299  * name: PaX\0
300  * namesz: 4
301  * desc:
302  *	word[0]: capability bitmask
303  * descsz: 4
304  */
305 #define NT_NETBSD_PAX		3
306 #define NT_NETBSD_PAX_MPROTECT		0x01	/* Force enable Mprotect */
307 #define NT_NETBSD_PAX_NOMPROTECT	0x02	/* Force disable Mprotect */
308 #define NT_NETBSD_PAX_GUARD		0x04	/* Force enable Segvguard */
309 #define NT_NETBSD_PAX_NOGUARD		0x08	/* Force disable Servguard */
310 #define NT_NETBSD_PAX_ASLR		0x10	/* Force enable ASLR */
311 #define NT_NETBSD_PAX_NOASLR		0x20	/* Force disable ASLR */
312 
313 /*
314  * NetBSD-specific note type: MACHINE_ARCH.
315  * There should be 1 NOTE per executable.
316  * name:	NetBSD\0
317  * namesz:	7
318  * desc:	string
319  * descsz:	variable
320  */
321 #define NT_NETBSD_MARCH		5
322 
323 /*
324  * NetBSD-specific note type: COMPILER MODEL.
325  * There should be 1 NOTE per executable.
326  * name:	NetBSD\0
327  * namesz:	7
328  * desc:	string
329  * descsz:	variable
330  */
331 #define NT_NETBSD_CMODEL	6
332 
333 #if !defined(ELFSIZE) && defined(ARCH_ELFSIZE)
334 #define ELFSIZE ARCH_ELFSIZE
335 #endif
336 /* SunOS 5.x hardware/software capabilities */
337 typedef struct {
338 	Elf32_Word	c_tag;
339 	union {
340 		Elf32_Word	c_val;
341 		Elf32_Addr	c_ptr;
342 	} c_un;
343 } Elf32_Cap;
344 
345 typedef struct {
346 	Elf64_Xword	c_tag;
347 	union {
348 		Elf64_Xword	c_val;
349 		Elf64_Addr	c_ptr;
350 	} c_un;
351 } Elf64_Cap;
352 
353 /* SunOS 5.x hardware/software capability tags */
354 #define	CA_SUNW_NULL	0
355 #define	CA_SUNW_HW_1	1
356 #define	CA_SUNW_SF_1	2
357 
358 /* SunOS 5.x software capabilities */
359 #define	SF1_SUNW_FPKNWN	0x01
360 #define	SF1_SUNW_FPUSED	0x02
361 #define	SF1_SUNW_MASK	0x03
362 
363 /* SunOS 5.x hardware capabilities: sparc */
364 #define	AV_SPARC_MUL32		0x0001
365 #define	AV_SPARC_DIV32		0x0002
366 #define	AV_SPARC_FSMULD		0x0004
367 #define	AV_SPARC_V8PLUS		0x0008
368 #define	AV_SPARC_POPC		0x0010
369 #define	AV_SPARC_VIS		0x0020
370 #define	AV_SPARC_VIS2		0x0040
371 #define	AV_SPARC_ASI_BLK_INIT	0x0080
372 #define	AV_SPARC_FMAF		0x0100
373 #define	AV_SPARC_FJFMAU		0x4000
374 #define	AV_SPARC_IMA		0x8000
375 
376 /* SunOS 5.x hardware capabilities: 386 */
377 #define	AV_386_FPU		0x00000001
378 #define	AV_386_TSC		0x00000002
379 #define	AV_386_CX8		0x00000004
380 #define	AV_386_SEP		0x00000008
381 #define	AV_386_AMD_SYSC		0x00000010
382 #define	AV_386_CMOV		0x00000020
383 #define	AV_386_MMX		0x00000040
384 #define	AV_386_AMD_MMX		0x00000080
385 #define	AV_386_AMD_3DNow	0x00000100
386 #define	AV_386_AMD_3DNowx	0x00000200
387 #define	AV_386_FXSR		0x00000400
388 #define	AV_386_SSE		0x00000800
389 #define	AV_386_SSE2		0x00001000
390 #define	AV_386_PAUSE		0x00002000
391 #define	AV_386_SSE3		0x00004000
392 #define	AV_386_MON		0x00008000
393 #define	AV_386_CX16		0x00010000
394 #define	AV_386_AHF		0x00020000
395 #define	AV_386_TSCP		0x00040000
396 #define	AV_386_AMD_SSE4A	0x00080000
397 #define	AV_386_POPCNT		0x00100000
398 #define	AV_386_AMD_LZCNT	0x00200000
399 #define	AV_386_SSSE3		0x00400000
400 #define	AV_386_SSE4_1		0x00800000
401 #define	AV_386_SSE4_2		0x01000000
402 
403 #endif
404