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