xref: /netbsd-src/tools/amiga-elf2bb/sys/exec_elf.h (revision c2f76ff004a2cb67efe5b12d97bd3ef7fe89e18d)
1 /*	$NetBSD: exec_elf.h,v 1.3 2010/08/28 21:30:02 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _SYS_EXEC_ELF_H_
33 #define	_SYS_EXEC_ELF_H_
34 
35 /*
36  * The current ELF ABI specification is available at:
37  *	http://www.sco.com/developers/gabi/
38  *
39  * Current header definitions are in:
40  *	http://www.sco.com/developers/gabi/latest/ch4.eheader.html
41  */
42 
43 #if defined(_KERNEL) || defined(_STANDALONE)
44 #include <sys/types.h>
45 #else
46 #include <inttypes.h>
47 #endif /* _KERNEL || _STANDALONE */
48 
49 typedef	uint8_t  	Elf_Byte;
50 
51 typedef	uint32_t	Elf32_Addr;
52 #define	ELF32_FSZ_ADDR	4
53 typedef	uint32_t	Elf32_Off;
54 #define	ELF32_FSZ_OFF	4
55 typedef	int32_t		Elf32_Sword;
56 #define	ELF32_FSZ_SWORD	4
57 typedef	uint32_t	Elf32_Word;
58 #define	ELF32_FSZ_WORD	4
59 typedef	uint16_t	Elf32_Half;
60 #define	ELF32_FSZ_HALF	2
61 
62 typedef	uint64_t	Elf64_Addr;
63 #define	ELF64_FSZ_ADDR	8
64 typedef	uint64_t	Elf64_Off;
65 #define	ELF64_FSZ_OFF	8
66 typedef	int32_t		Elf64_Shalf;
67 #define	ELF64_FSZ_SHALF	4
68 
69 #ifndef ELF64_FSZ_SWORD
70 typedef	int32_t		Elf64_Sword;
71 #define	ELF64_FSZ_SWORD	4
72 #endif /* ELF64_FSZ_SWORD */
73 #ifndef ELF64_FSZ_WORD
74 typedef	uint32_t	Elf64_Word;
75 #define	ELF64_FSZ_WORD	4
76 #endif /* ELF64_FSZ_WORD */
77 
78 typedef	int64_t		Elf64_Sxword;
79 #define	ELF64_FSZ_XWORD	8
80 typedef	uint64_t	Elf64_Xword;
81 #define	ELF64_FSZ_XWORD	8
82 typedef	uint16_t	Elf64_Half;
83 #define	ELF64_FSZ_HALF 2
84 
85 /*
86  * ELF Header
87  */
88 #define	ELF_NIDENT	16
89 
90 typedef struct {
91 	unsigned char	e_ident[ELF_NIDENT];	/* Id bytes */
92 	Elf32_Half	e_type;			/* file type */
93 	Elf32_Half	e_machine;		/* machine type */
94 	Elf32_Word	e_version;		/* version number */
95 	Elf32_Addr	e_entry;		/* entry point */
96 	Elf32_Off	e_phoff;		/* Program hdr offset */
97 	Elf32_Off	e_shoff;		/* Section hdr offset */
98 	Elf32_Word	e_flags;		/* Processor flags */
99 	Elf32_Half      e_ehsize;		/* sizeof ehdr */
100 	Elf32_Half      e_phentsize;		/* Program header entry size */
101 	Elf32_Half      e_phnum;		/* Number of program headers */
102 	Elf32_Half      e_shentsize;		/* Section header entry size */
103 	Elf32_Half      e_shnum;		/* Number of section headers */
104 	Elf32_Half      e_shstrndx;		/* String table index */
105 } Elf32_Ehdr;
106 
107 typedef struct {
108 	unsigned char	e_ident[ELF_NIDENT];	/* Id bytes */
109 	Elf64_Half	e_type;			/* file type */
110 	Elf64_Half	e_machine;		/* machine type */
111 	Elf64_Word	e_version;		/* version number */
112 	Elf64_Addr	e_entry;		/* entry point */
113 	Elf64_Off	e_phoff;		/* Program hdr offset */
114 	Elf64_Off	e_shoff;		/* Section hdr offset */
115 	Elf64_Word	e_flags;		/* Processor flags */
116 	Elf64_Half	e_ehsize;		/* sizeof ehdr */
117 	Elf64_Half	e_phentsize;		/* Program header entry size */
118 	Elf64_Half	e_phnum;		/* Number of program headers */
119 	Elf64_Half	e_shentsize;		/* Section header entry size */
120 	Elf64_Half	e_shnum;		/* Number of section headers */
121 	Elf64_Half	e_shstrndx;		/* String table index */
122 } Elf64_Ehdr;
123 
124 /* e_ident offsets */
125 #define	EI_MAG0		0	/* '\177' */
126 #define	EI_MAG1		1	/* 'E'    */
127 #define	EI_MAG2		2	/* 'L'    */
128 #define	EI_MAG3		3	/* 'F'    */
129 #define	EI_CLASS	4	/* File class */
130 #define	EI_DATA		5	/* Data encoding */
131 #define	EI_VERSION	6	/* File version */
132 #define	EI_OSABI	7	/* Operating system/ABI identification */
133 #define	EI_ABIVERSION	8	/* ABI version */
134 #define	EI_PAD		9	/* Start of padding bytes up to EI_NIDENT*/
135 #define	EI_NIDENT	16	/* First non-ident header byte */
136 
137 /* e_ident[EI_MAG0,EI_MAG3] */
138 #define	ELFMAG0		0x7f
139 #define	ELFMAG1		'E'
140 #define	ELFMAG2		'L'
141 #define	ELFMAG3		'F'
142 #define	ELFMAG		"\177ELF"
143 #define	SELFMAG		4
144 
145 /* e_ident[EI_CLASS] */
146 #define	ELFCLASSNONE	0	/* Invalid class */
147 #define	ELFCLASS32	1	/* 32-bit objects */
148 #define	ELFCLASS64	2	/* 64-bit objects */
149 #define	ELFCLASSNUM	3
150 
151 /* e_ident[EI_DATA] */
152 #define	ELFDATANONE	0	/* Invalid data encoding */
153 #define	ELFDATA2LSB	1	/* 2's complement values, LSB first */
154 #define	ELFDATA2MSB	2	/* 2's complement values, MSB first */
155 
156 /* e_ident[EI_VERSION] */
157 #define	EV_NONE		0	/* Invalid version */
158 #define	EV_CURRENT	1	/* Current version */
159 #define	EV_NUM		2
160 
161 /* e_ident[EI_OSABI] */
162 #define	ELFOSABI_SYSV		0	/* UNIX System V ABI */
163 #define	ELFOSABI_HPUX		1	/* HP-UX operating system */
164 #define ELFOSABI_NETBSD		2	/* NetBSD */
165 #define ELFOSABI_LINUX		3	/* GNU/Linux */
166 #define ELFOSABI_HURD		4	/* GNU/Hurd */
167 #define ELFOSABI_86OPEN		5	/* 86Open */
168 #define ELFOSABI_SOLARIS	6	/* Solaris */
169 #define ELFOSABI_MONTEREY	7	/* Monterey */
170 #define ELFOSABI_IRIX		8	/* IRIX */
171 #define ELFOSABI_FREEBSD	9	/* FreeBSD */
172 #define ELFOSABI_TRU64		10	/* TRU64 UNIX */
173 #define ELFOSABI_MODESTO	11	/* Novell Modesto */
174 #define ELFOSABI_OPENBSD	12	/* OpenBSD */
175 /* Unofficial OSABIs follow */
176 #define ELFOSABI_ARM		97	/* ARM */
177 #define	ELFOSABI_STANDALONE	255	/* Standalone (embedded) application */
178 
179 /* e_type */
180 #define	ET_NONE		0	/* No file type */
181 #define	ET_REL		1	/* Relocatable file */
182 #define	ET_EXEC		2	/* Executable file */
183 #define	ET_DYN		3	/* Shared object file */
184 #define	ET_CORE		4	/* Core file */
185 #define	ET_NUM		5
186 
187 #define	ET_LOOS		0xfe00	/* Operating system specific range */
188 #define	ET_HIOS		0xfeff
189 #define	ET_LOPROC	0xff00	/* Processor-specific range */
190 #define	ET_HIPROC	0xffff
191 
192 /* e_machine */
193 #define	EM_NONE		0	/* No machine */
194 #define	EM_M32		1	/* AT&T WE 32100 */
195 #define	EM_SPARC	2	/* SPARC */
196 #define	EM_386		3	/* Intel 80386 */
197 #define	EM_68K		4	/* Motorola 68000 */
198 #define	EM_88K		5	/* Motorola 88000 */
199 #define	EM_486		6	/* Intel 80486 */
200 #define	EM_860		7	/* Intel 80860 */
201 #define	EM_MIPS		8	/* MIPS I Architecture */
202 #define	EM_S370		9	/* Amdahl UTS on System/370 */
203 #define	EM_MIPS_RS3_LE	10	/* MIPS RS3000 Little-endian */
204 			/* 11-14 - Reserved */
205 #define	EM_RS6000	11	/* IBM RS/6000 XXX reserved */
206 #define	EM_PARISC	15	/* Hewlett-Packard PA-RISC */
207 #define	EM_NCUBE	16	/* NCube XXX reserved */
208 #define	EM_VPP500	17	/* Fujitsu VPP500 */
209 #define	EM_SPARC32PLUS	18	/* Enhanced instruction set SPARC */
210 #define	EM_960		19	/* Intel 80960 */
211 #define	EM_PPC		20	/* PowerPC */
212 #define	EM_PPC64	21	/* 64-bit PowerPC */
213 			/* 22-35 - Reserved */
214 #define	EM_V800		36	/* NEC V800 */
215 #define	EM_FR20		37	/* Fujitsu FR20 */
216 #define	EM_RH32		38	/* TRW RH-32 */
217 #define	EM_RCE		39	/* Motorola RCE */
218 #define	EM_ARM		40	/* Advanced RISC Machines ARM */
219 #define	EM_ALPHA	41	/* DIGITAL Alpha */
220 #define	EM_SH		42	/* Hitachi Super-H */
221 #define	EM_SPARCV9	43	/* SPARC Version 9 */
222 #define	EM_TRICORE	44	/* Siemens Tricore */
223 #define	EM_ARC		45	/* Argonaut RISC Core */
224 #define	EM_H8_300	46	/* Hitachi H8/300 */
225 #define	EM_H8_300H	47	/* Hitachi H8/300H */
226 #define	EM_H8S		48	/* Hitachi H8S */
227 #define	EM_H8_500	49	/* Hitachi H8/500 */
228 #define	EM_IA_64	50	/* Intel Merced Processor */
229 #define	EM_MIPS_X	51	/* Stanford MIPS-X */
230 #define	EM_COLDFIRE	52	/* Motorola Coldfire */
231 #define	EM_68HC12	53	/* Motorola MC68HC12 */
232 #define	EM_MMA		54	/* Fujitsu MMA Multimedia Accelerator */
233 #define	EM_PCP		55	/* Siemens PCP */
234 #define	EM_NCPU		56	/* Sony nCPU embedded RISC processor */
235 #define	EM_NDR1		57	/* Denso NDR1 microprocessor */
236 #define	EM_STARCORE	58	/* Motorola Star*Core processor */
237 #define	EM_ME16		59	/* Toyota ME16 processor */
238 #define	EM_ST100	60	/* STMicroelectronics ST100 processor */
239 #define	EM_TINYJ	61	/* Advanced Logic Corp. TinyJ embedded family processor */
240 #define	EM_X86_64	62	/* AMD x86-64 architecture */
241 #define	EM_PDSP		63	/* Sony DSP Processor */
242 #define	EM_PDP10	64	/* Digital Equipment Corp. PDP-10 */
243 #define	EM_PDP11	65	/* Digital Equipment Corp. PDP-11 */
244 #define	EM_FX66		66	/* Siemens FX66 microcontroller */
245 #define	EM_ST9PLUS	67	/* STMicroelectronics ST9+ 8/16 bit microcontroller */
246 #define	EM_ST7		68	/* STMicroelectronics ST7 8-bit microcontroller */
247 #define	EM_68HC16	69	/* Motorola MC68HC16 Microcontroller */
248 #define	EM_68HC11	70	/* Motorola MC68HC11 Microcontroller */
249 #define	EM_68HC08	71	/* Motorola MC68HC08 Microcontroller */
250 #define	EM_68HC05	72	/* Motorola MC68HC05 Microcontroller */
251 #define	EM_SVX		73	/* Silicon Graphics SVx */
252 #define	EM_ST19		74	/* STMicroelectronics ST19 8-bit CPU */
253 #define	EM_VAX		75	/* Digital VAX */
254 #define	EM_CRIS		76	/* Axis Communications 32-bit embedded processor */
255 #define	EM_JAVELIN	77	/* Infineon Technologies 32-bit embedded CPU */
256 #define	EM_FIREPATH	78	/* Element 14 64-bit DSP processor */
257 #define	EM_ZSP		79	/* LSI Logic's 16-bit DSP processor */
258 #define	EM_MMIX		80	/* Donald Knuth's educational 64-bit processor */
259 #define	EM_HUANY	81	/* Harvard's machine-independent format */
260 #define	EM_PRISM	82	/* SiTera Prism */
261 #define	EM_AVR		83	/* Atmel AVR 8-bit microcontroller */
262 #define	EM_FR30		84	/* Fujitsu FR30 */
263 #define	EM_D10V		85	/* Mitsubishi D10V */
264 #define	EM_D30V		86	/* Mitsubishi D30V */
265 #define	EM_V850		87	/* NEC v850 */
266 #define	EM_M32R		88	/* Mitsubishi M32R */
267 #define	EM_MN10300	89	/* Matsushita MN10300 */
268 #define	EM_MN10200	90	/* Matsushita MN10200 */
269 #define	EM_PJ		91	/* picoJava */
270 #define	EM_OPENRISC	92	/* OpenRISC 32-bit embedded processor */
271 #define	EM_ARC_A5	93	/* ARC Cores Tangent-A5 */
272 #define	EM_XTENSA	94	/* Tensilica Xtensa Architecture */
273 #define	EM_NS32K	97	/* National Semiconductor 32000 series */
274 
275 /* Unofficial machine types follow */
276 #define	EM_ALPHA_EXP	36902	/* used by NetBSD/alpha; obsolete */
277 #define	EM_NUM		36903
278 
279 /*
280  * Program Header
281  */
282 typedef struct {
283 	Elf32_Word	p_type;		/* entry type */
284 	Elf32_Off	p_offset;	/* offset */
285 	Elf32_Addr	p_vaddr;	/* virtual address */
286 	Elf32_Addr	p_paddr;	/* physical address */
287 	Elf32_Word	p_filesz;	/* file size */
288 	Elf32_Word	p_memsz;	/* memory size */
289 	Elf32_Word	p_flags;	/* flags */
290 	Elf32_Word	p_align;	/* memory & file alignment */
291 } Elf32_Phdr;
292 
293 typedef struct {
294 	Elf64_Word	p_type;		/* entry type */
295 	Elf64_Word	p_flags;	/* flags */
296 	Elf64_Off	p_offset;	/* offset */
297 	Elf64_Addr	p_vaddr;	/* virtual address */
298 	Elf64_Addr	p_paddr;	/* physical address */
299 	Elf64_Xword	p_filesz;	/* file size */
300 	Elf64_Xword	p_memsz;	/* memory size */
301 	Elf64_Xword	p_align;	/* memory & file alignment */
302 } Elf64_Phdr;
303 
304 /* p_type */
305 #define	PT_NULL		0		/* Program header table entry unused */
306 #define	PT_LOAD		1		/* Loadable program segment */
307 #define	PT_DYNAMIC	2		/* Dynamic linking information */
308 #define	PT_INTERP	3		/* Program interpreter */
309 #define	PT_NOTE		4		/* Auxiliary information */
310 #define	PT_SHLIB	5		/* Reserved, unspecified semantics */
311 #define	PT_PHDR		6		/* Entry for header table itself */
312 #define	PT_NUM		7
313 
314 #define	PT_LOOS         0x60000000	/* OS-specific range */
315 #define	PT_HIOS         0x6fffffff
316 #define	PT_LOPROC	0x70000000	/* Processor-specific range */
317 #define	PT_HIPROC	0x7fffffff
318 
319 #define	PT_MIPS_REGINFO	0x70000000
320 
321 /* p_flags */
322 #define	PF_R		0x4	/* Segment is readable */
323 #define	PF_W		0x2	/* Segment is writable */
324 #define	PF_X		0x1	/* Segment is executable */
325 
326 #define	PF_MASKOS	0x0ff00000	/* Operating system specific values */
327 #define	PF_MASKPROC	0xf0000000	/* Processor-specific values */
328 
329 /*
330  * Section Headers
331  */
332 typedef struct {
333 	Elf32_Word	sh_name;	/* section name (.shstrtab index) */
334 	Elf32_Word	sh_type;	/* section type */
335 	Elf32_Word	sh_flags;	/* section flags */
336 	Elf32_Addr	sh_addr;	/* virtual address */
337 	Elf32_Off	sh_offset;	/* file offset */
338 	Elf32_Word	sh_size;	/* section size */
339 	Elf32_Word	sh_link;	/* link to another */
340 	Elf32_Word	sh_info;	/* misc info */
341 	Elf32_Word	sh_addralign;	/* memory alignment */
342 	Elf32_Word	sh_entsize;	/* table entry size */
343 } Elf32_Shdr;
344 
345 typedef struct {
346 	Elf64_Word	sh_name;	/* section name (.shstrtab index) */
347 	Elf64_Word	sh_type;	/* section type */
348 	Elf64_Xword	sh_flags;	/* section flags */
349 	Elf64_Addr	sh_addr;	/* virtual address */
350 	Elf64_Off	sh_offset;	/* file offset */
351 	Elf64_Xword	sh_size;	/* section size */
352 	Elf64_Word	sh_link;	/* link to another */
353 	Elf64_Word	sh_info;	/* misc info */
354 	Elf64_Xword	sh_addralign;	/* memory alignment */
355 	Elf64_Xword	sh_entsize;	/* table entry size */
356 } Elf64_Shdr;
357 
358 /* sh_type */
359 #define	SHT_NULL	0		/* Section header table entry unused */
360 #define	SHT_PROGBITS	1		/* Program information */
361 #define	SHT_SYMTAB	2		/* Symbol table */
362 #define	SHT_STRTAB	3		/* String table */
363 #define	SHT_RELA	4		/* Relocation information w/ addend */
364 #define	SHT_HASH	5		/* Symbol hash table */
365 #define	SHT_DYNAMIC	6		/* Dynamic linking information */
366 #define	SHT_NOTE	7		/* Auxiliary information */
367 #define	SHT_NOBITS	8		/* No space allocated in file image */
368 #define	SHT_REL		9		/* Relocation information w/o addend */
369 #define	SHT_SHLIB	10		/* Reserved, unspecified semantics */
370 #define	SHT_DYNSYM	11		/* Symbol table for dynamic linker */
371 #define	SHT_NUM		12
372 
373 #define	SHT_LOOS	0x60000000	/* Operating system specific range */
374 #define	SHT_HIOS	0x6fffffff
375 #define	SHT_LOPROC	0x70000000	/* Processor-specific range */
376 #define	SHT_HIPROC	0x7fffffff
377 #define	SHT_LOUSER	0x80000000	/* Application-specific range */
378 #define	SHT_HIUSER	0xffffffff
379 
380 /* sh_flags */
381 #define	SHF_WRITE	0x1		/* Section contains writable data */
382 #define	SHF_ALLOC	0x2		/* Section occupies memory */
383 #define	SHF_EXECINSTR	0x4		/* Section contains executable insns */
384 
385 #define	SHF_MASKOS	0x0f000000	/* Operating system specific values */
386 #define	SHF_MASKPROC	0xf0000000	/* Processor-specific values */
387 
388 /*
389  * Symbol Table
390  */
391 typedef struct {
392 	Elf32_Word	st_name;	/* Symbol name (.symtab index) */
393 	Elf32_Word	st_value;	/* value of symbol */
394 	Elf32_Word	st_size;	/* size of symbol */
395 	Elf_Byte	st_info;	/* type / binding attrs */
396 	Elf_Byte	st_other;	/* unused */
397 	Elf32_Half	st_shndx;	/* section index of symbol */
398 } Elf32_Sym;
399 
400 typedef struct {
401 	Elf64_Word	st_name;	/* Symbol name (.symtab index) */
402 	Elf_Byte	st_info;	/* type / binding attrs */
403 	Elf_Byte	st_other;	/* unused */
404 	Elf64_Half	st_shndx;	/* section index of symbol */
405 	Elf64_Addr	st_value;	/* value of symbol */
406 	Elf64_Xword	st_size;	/* size of symbol */
407 } Elf64_Sym;
408 
409 /* Symbol Table index of the undefined symbol */
410 #define	ELF_SYM_UNDEFINED	0
411 
412 /* st_info: Symbol Bindings */
413 #define	STB_LOCAL		0	/* local symbol */
414 #define	STB_GLOBAL		1	/* global symbol */
415 #define	STB_WEAK		2	/* weakly defined global symbol */
416 #define	STB_NUM			3
417 
418 #define	STB_LOOS		10	/* Operating system specific range */
419 #define	STB_HIOS		12
420 #define	STB_LOPROC		13	/* Processor-specific range */
421 #define	STB_HIPROC		15
422 
423 /* st_info: Symbol Types */
424 #define	STT_NOTYPE		0	/* Type not specified */
425 #define	STT_OBJECT		1	/* Associated with a data object */
426 #define	STT_FUNC		2	/* Associated with a function */
427 #define	STT_SECTION		3	/* Associated with a section */
428 #define	STT_FILE		4	/* Associated with a file name */
429 #define	STT_NUM			5
430 
431 #define	STT_LOOS		10	/* Operating system specific range */
432 #define	STT_HIOS		12
433 #define	STT_LOPROC		13	/* Processor-specific range */
434 #define	STT_HIPROC		15
435 
436 /* st_info utility macros */
437 #define	ELF32_ST_BIND(info)		((Elf32_Word)(info) >> 4)
438 #define	ELF32_ST_TYPE(info)		((Elf32_Word)(info) & 0xf)
439 #define	ELF32_ST_INFO(bind,type)	((Elf_Byte)(((bind) << 4) | ((type) & 0xf)))
440 
441 #define	ELF64_ST_BIND(info)		((Elf64_Xword)(info) >> 4)
442 #define	ELF64_ST_TYPE(info)		((Elf64_Xword)(info) & 0xf)
443 #define	ELF64_ST_INFO(bind,type)	((Elf_Byte)(((bind) << 4) | ((type) & 0xf)))
444 
445 /*
446  * Special section indexes
447  */
448 #define	SHN_UNDEF	0		/* Undefined section */
449 
450 #define	SHN_LORESERVE	0xff00		/* Reserved range */
451 #define	SHN_ABS		0xfff1		/*  Absolute symbols */
452 #define	SHN_COMMON	0xfff2		/*  Common symbols */
453 #define	SHN_HIRESERVE	0xffff
454 
455 #define	SHN_LOPROC	0xff00		/* Processor-specific range */
456 #define	SHN_HIPROC	0xff1f
457 #define	SHN_LOOS	0xff20		/* Operating system specific range */
458 #define	SHN_HIOS	0xff3f
459 
460 #define	SHN_MIPS_ACOMMON 0xff00
461 #define	SHN_MIPS_TEXT	0xff01
462 #define	SHN_MIPS_DATA	0xff02
463 #define	SHN_MIPS_SCOMMON 0xff03
464 
465 /*
466  * Relocation Entries
467  */
468 typedef struct {
469 	Elf32_Word	r_offset;	/* where to do it */
470 	Elf32_Word	r_info;		/* index & type of relocation */
471 } Elf32_Rel;
472 
473 typedef struct {
474 	Elf32_Word	r_offset;	/* where to do it */
475 	Elf32_Word	r_info;		/* index & type of relocation */
476 	Elf32_Sword	r_addend;	/* adjustment value */
477 } Elf32_Rela;
478 
479 /* r_info utility macros */
480 #define	ELF32_R_SYM(info)	((info) >> 8)
481 #define	ELF32_R_TYPE(info)	((info) & 0xff)
482 #define	ELF32_R_INFO(sym, type)	(((sym) << 8) + (unsigned char)(type))
483 
484 typedef struct {
485 	Elf64_Addr	r_offset;	/* where to do it */
486 	Elf64_Xword	r_info;		/* index & type of relocation */
487 } Elf64_Rel;
488 
489 typedef struct {
490 	Elf64_Addr	r_offset;	/* where to do it */
491 	Elf64_Xword	r_info;		/* index & type of relocation */
492 	Elf64_Sxword	r_addend;	/* adjustment value */
493 } Elf64_Rela;
494 
495 /* r_info utility macros */
496 #define	ELF64_R_SYM(info)	((info) >> 32)
497 #define	ELF64_R_TYPE(info)	((info) & 0xffffffff)
498 #define	ELF64_R_INFO(sym,type)	(((sym) << 32) + (type))
499 
500 /*
501  * Dynamic Section structure array
502  */
503 typedef struct {
504 	Elf32_Word	d_tag;		/* entry tag value */
505 	union {
506 	    Elf32_Addr	d_ptr;
507 	    Elf32_Word	d_val;
508 	} d_un;
509 } Elf32_Dyn;
510 
511 typedef struct {
512 	Elf64_Xword	d_tag;		/* entry tag value */
513 	union {
514 	    Elf64_Addr	d_ptr;
515 	    Elf64_Xword	d_val;
516 	} d_un;
517 } Elf64_Dyn;
518 
519 /* d_tag */
520 #define	DT_NULL		0	/* Marks end of dynamic array */
521 #define	DT_NEEDED	1	/* Name of needed library (DT_STRTAB offset) */
522 #define	DT_PLTRELSZ	2	/* Size, in bytes, of relocations in PLT */
523 #define	DT_PLTGOT	3	/* Address of PLT and/or GOT */
524 #define	DT_HASH		4	/* Address of symbol hash table */
525 #define	DT_STRTAB	5	/* Address of string table */
526 #define	DT_SYMTAB	6	/* Address of symbol table */
527 #define	DT_RELA		7	/* Address of Rela relocation table */
528 #define	DT_RELASZ	8	/* Size, in bytes, of DT_RELA table */
529 #define	DT_RELAENT	9	/* Size, in bytes, of one DT_RELA entry */
530 #define	DT_STRSZ	10	/* Size, in bytes, of DT_STRTAB table */
531 #define	DT_SYMENT	11	/* Size, in bytes, of one DT_SYMTAB entry */
532 #define	DT_INIT		12	/* Address of initialization function */
533 #define	DT_FINI		13	/* Address of termination function */
534 #define	DT_SONAME	14	/* Shared object name (DT_STRTAB offset) */
535 #define	DT_RPATH	15	/* Library search path (DT_STRTAB offset) */
536 #define	DT_SYMBOLIC	16	/* Start symbol search within local object */
537 #define	DT_REL		17	/* Address of Rel relocation table */
538 #define	DT_RELSZ	18	/* Size, in bytes, of DT_REL table */
539 #define	DT_RELENT	19	/* Size, in bytes, of one DT_REL entry */
540 #define	DT_PLTREL	20 	/* Type of PLT relocation entries */
541 #define	DT_DEBUG	21	/* Used for debugging; unspecified */
542 #define	DT_TEXTREL	22	/* Relocations might modify non-writable seg */
543 #define	DT_JMPREL	23	/* Address of relocations associated with PLT */
544 #define	DT_BIND_NOW	24	/* Process all relocations at load-time */
545 #define	DT_INIT_ARRAY	25	/* Address of initialization function array */
546 #define	DT_FINI_ARRAY	26	/* Size, in bytes, of DT_INIT_ARRAY array */
547 #define	DT_INIT_ARRAYSZ	27	/* Address of termination function array */
548 #define	DT_FINI_ARRAYSZ	28	/* Size, in bytes, of DT_FINI_ARRAY array*/
549 #define	DT_NUM		29
550 
551 #define	DT_LOOS		0x60000000	/* Operating system specific range */
552 #define	DT_HIOS		0x6fffffff
553 #define	DT_LOPROC	0x70000000	/* Processor-specific range */
554 #define	DT_HIPROC	0x7fffffff
555 
556 /*
557  * Auxiliary Vectors
558  */
559 typedef struct {
560 	Elf32_Word	a_type;				/* 32-bit id */
561 	Elf32_Word	a_v;				/* 32-bit id */
562 } Aux32Info;
563 
564 typedef struct {
565 	Elf64_Word	a_type;				/* 32-bit id */
566 	Elf64_Xword	a_v;				/* 64-bit id */
567 } Aux64Info;
568 
569 /* a_type */
570 #define	AT_NULL		0	/* Marks end of array */
571 #define	AT_IGNORE	1	/* No meaning, a_un is undefined */
572 #define	AT_EXECFD	2	/* Open file descriptor of object file */
573 #define	AT_PHDR		3	/* &phdr[0] */
574 #define	AT_PHENT	4	/* sizeof(phdr[0]) */
575 #define	AT_PHNUM	5	/* # phdr entries */
576 #define	AT_PAGESZ	6	/* PAGESIZE */
577 #define	AT_BASE		7	/* Interpreter base addr */
578 #define	AT_FLAGS	8	/* Processor flags */
579 #define	AT_ENTRY	9	/* Entry address of executable */
580 #define	AT_DCACHEBSIZE	10	/* Data cache block size */
581 #define	AT_ICACHEBSIZE	11	/* Instruction cache block size */
582 #define	AT_UCACHEBSIZE	12	/* Unified cache block size */
583 
584 	/* Vendor specific */
585 #define	AT_MIPS_NOTELF	10	/* XXX a_val != 0 -> MIPS XCOFF executable */
586 
587 #define	AT_EUID		2000	/* euid (solaris compatible numbers) */
588 #define	AT_RUID		2001	/* ruid (solaris compatible numbers) */
589 #define	AT_EGID		2002	/* egid (solaris compatible numbers) */
590 #define	AT_RGID		2003	/* rgid (solaris compatible numbers) */
591 
592 	/* Solaris kernel specific */
593 #define	AT_SUN_LDELF	2004	/* dynamic linker's ELF header */
594 #define	AT_SUN_LDSHDR	2005	/* dynamic linker's section header */
595 #define	AT_SUN_LDNAME	2006	/* dynamic linker's name */
596 #define	AT_SUN_LPGSIZE	2007	/* large pagesize */
597 
598 	/* Other information */
599 #define	AT_SUN_PLATFORM	2008	/* sysinfo(SI_PLATFORM) */
600 #define	AT_SUN_HWCAP	2009	/* process hardware capabilities */
601 #define	AT_SUN_IFLUSH	2010	/* do we need to flush the instruction cache? */
602 #define	AT_SUN_CPU	2011	/* CPU name */
603 	/* ibcs2 emulation band aid */
604 #define	AT_SUN_EMUL_ENTRY 2012	/* coff entry point */
605 #define	AT_SUN_EMUL_EXECFD 2013	/* coff file descriptor */
606 	/* Executable's fully resolved name */
607 #define	AT_SUN_EXECNAME	2014
608 
609 /*
610  * Note Headers
611  */
612 typedef struct {
613 	Elf32_Word n_namesz;
614 	Elf32_Word n_descsz;
615 	Elf32_Word n_type;
616 } Elf32_Nhdr;
617 
618 typedef struct {
619 	Elf64_Word n_namesz;
620 	Elf64_Word n_descsz;
621 	Elf64_Word n_type;
622 } Elf64_Nhdr;
623 
624 #define	ELF_NOTE_TYPE_ABI_TAG		1
625 
626 /* GNU-specific note name and description sizes */
627 #define	ELF_NOTE_ABI_NAMESZ		4
628 #define	ELF_NOTE_ABI_DESCSZ		16
629 /* GNU-specific note name */
630 #define	ELF_NOTE_ABI_NAME		"GNU\0"
631 
632 /* GNU-specific OS/version value stuff */
633 #define	ELF_NOTE_ABI_OS_LINUX		0
634 #define	ELF_NOTE_ABI_OS_HURD		1
635 #define	ELF_NOTE_ABI_OS_SOLARIS		2
636 
637 /* NetBSD-specific note type: Emulation name.  desc is emul name string. */
638 #define	ELF_NOTE_TYPE_NETBSD_TAG	1
639 
640 /* NetBSD-specific note name and description sizes */
641 #define	ELF_NOTE_NETBSD_NAMESZ		7
642 #define	ELF_NOTE_NETBSD_DESCSZ		4
643 /* NetBSD-specific note name */
644 #define	ELF_NOTE_NETBSD_NAME		"NetBSD\0\0"
645 
646 /*
647  * NetBSD-specific core file information.
648  *
649  * NetBSD ELF core files use notes to provide information about
650  * the process's state.  The note name is "NetBSD-CORE" for
651  * information that is global to the process, and "NetBSD-CORE@nn",
652  * where "nn" is the lwpid of the LWP that the information belongs
653  * to (such as register state).
654  *
655  * We use the following note identifiers:
656  *
657  *	ELF_NOTE_NETBSD_CORE_PROCINFO
658  *		Note is a "netbsd_elfcore_procinfo" structure.
659  *
660  * We also use ptrace(2) request numbers (the ones that exist in
661  * machine-dependent space) to identify register info notes.  The
662  * info in such notes is in the same format that ptrace(2) would
663  * export that information.
664  *
665  * Please try to keep the members of this structure nicely aligned,
666  * and if you add elements, add them to the end and bump the version.
667  */
668 
669 #define	ELF_NOTE_NETBSD_CORE_NAME	"NetBSD-CORE"
670 
671 #define	ELF_NOTE_NETBSD_CORE_PROCINFO	1
672 
673 #define	NETBSD_ELFCORE_PROCINFO_VERSION	1
674 
675 struct netbsd_elfcore_procinfo {
676 	/* Version 1 fields start here. */
677 	uint32_t	cpi_version;	/* netbsd_elfcore_procinfo version */
678 	uint32_t	cpi_cpisize;	/* sizeof(netbsd_elfcore_procinfo) */
679 	uint32_t	cpi_signo;	/* killing signal */
680 	uint32_t	cpi_sigcode;	/* signal code */
681 	uint32_t	cpi_sigpend[4];	/* pending signals */
682 	uint32_t	cpi_sigmask[4];	/* blocked signals */
683 	uint32_t	cpi_sigignore[4];/* blocked signals */
684 	uint32_t	cpi_sigcatch[4];/* blocked signals */
685 	int32_t		cpi_pid;	/* process ID */
686 	int32_t		cpi_ppid;	/* parent process ID */
687 	int32_t		cpi_pgrp;	/* process group ID */
688 	int32_t		cpi_sid;	/* session ID */
689 	uint32_t	cpi_ruid;	/* real user ID */
690 	uint32_t	cpi_euid;	/* effective user ID */
691 	uint32_t	cpi_svuid;	/* saved user ID */
692 	uint32_t	cpi_rgid;	/* real group ID */
693 	uint32_t	cpi_egid;	/* effective group ID */
694 	uint32_t	cpi_svgid;	/* saved group ID */
695 	uint32_t	cpi_nlwps;	/* number of LWPs */
696 	int8_t		cpi_name[32];	/* copy of p->p_comm */
697 	/* Add version 2 fields below here. */
698 	int32_t		cpi_siglwp;	/* LWP target of killing signal */
699 };
700 
701 #if defined(ELFSIZE)
702 #define	CONCAT(x,y)	__CONCAT(x,y)
703 #define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
704 #define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
705 #define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
706 #define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
707 #endif
708 
709 #if defined(ELFSIZE) && (ELFSIZE == 32)
710 #define	Elf_Ehdr	Elf32_Ehdr
711 #define	Elf_Phdr	Elf32_Phdr
712 #define	Elf_Shdr	Elf32_Shdr
713 #define	Elf_Sym		Elf32_Sym
714 #define	Elf_Rel		Elf32_Rel
715 #define	Elf_Rela	Elf32_Rela
716 #define	Elf_Dyn		Elf32_Dyn
717 #define	Elf_Word	Elf32_Word
718 #define	Elf_Sword	Elf32_Sword
719 #define	Elf_Addr	Elf32_Addr
720 #define	Elf_Off		Elf32_Off
721 #define	Elf_Nhdr	Elf32_Nhdr
722 
723 #define	ELF_R_SYM	ELF32_R_SYM
724 #define	ELF_R_TYPE	ELF32_R_TYPE
725 #define	ELFCLASS	ELFCLASS32
726 
727 #define	ELF_ST_BIND	ELF32_ST_BIND
728 #define	ELF_ST_TYPE	ELF32_ST_TYPE
729 #define	ELF_ST_INFO	ELF32_ST_INFO
730 
731 #define	AuxInfo		Aux32Info
732 #elif defined(ELFSIZE) && (ELFSIZE == 64)
733 #define	Elf_Ehdr	Elf64_Ehdr
734 #define	Elf_Phdr	Elf64_Phdr
735 #define	Elf_Shdr	Elf64_Shdr
736 #define	Elf_Sym		Elf64_Sym
737 #define	Elf_Rel		Elf64_Rel
738 #define	Elf_Rela	Elf64_Rela
739 #define	Elf_Dyn		Elf64_Dyn
740 #define	Elf_Word	Elf64_Word
741 #define	Elf_Sword	Elf64_Sword
742 #define	Elf_Addr	Elf64_Addr
743 #define	Elf_Off		Elf64_Off
744 #define	Elf_Nhdr	Elf64_Nhdr
745 
746 #define	ELF_R_SYM	ELF64_R_SYM
747 #define	ELF_R_TYPE	ELF64_R_TYPE
748 #define	ELFCLASS	ELFCLASS64
749 
750 #define	ELF_ST_BIND	ELF64_ST_BIND
751 #define	ELF_ST_TYPE	ELF64_ST_TYPE
752 #define	ELF_ST_INFO	ELF64_ST_INFO
753 
754 #define	AuxInfo		Aux64Info
755 #endif
756 
757 #ifdef _KERNEL
758 
759 #define ELF_AUX_ENTRIES	12		/* Size of aux array passed to loader */
760 #define ELF32_NO_ADDR	(~(Elf32_Addr)0) /* Indicates addr. not yet filled in */
761 #define ELF32_LINK_ADDR	((Elf32_Addr)-2) /* advises to use link address */
762 #define ELF64_NO_ADDR	(~(Elf64_Addr)0) /* Indicates addr. not yet filled in */
763 #define ELF64_LINK_ADDR	((Elf64_Addr)-2) /* advises to use link address */
764 
765 #if defined(ELFSIZE) && (ELFSIZE == 64)
766 #define ELF_NO_ADDR	ELF64_NO_ADDR
767 #define ELF_LINK_ADDR	ELF64_LINK_ADDR
768 #elif defined(ELFSIZE) && (ELFSIZE == 32)
769 #define ELF_NO_ADDR	ELF32_NO_ADDR
770 #define ELF_LINK_ADDR	ELF32_LINK_ADDR
771 #endif
772 
773 #ifndef ELF32_EHDR_FLAGS_OK
774 #define	ELF32_EHDR_FLAGS_OK(eh)	1
775 #endif
776 
777 #ifndef ELF64_EHDR_FLAGS_OK
778 #define	ELF64_EHDR_FLAGS_OK(eh)	1
779 #endif
780 
781 #if defined(ELFSIZE) && (ELFSIZE == 64)
782 #define	ELF_EHDR_FLAGS_OK(eh)	ELF64_EHDR_FLAGS_OK(eh)
783 #else
784 #define	ELF_EHDR_FLAGS_OK(eh)	ELF32_EHDR_FLAGS_OK(eh)
785 #endif
786 
787 #if defined(ELFSIZE)
788 struct elf_args {
789         Elf_Addr  arg_entry;      /* program entry point */
790         Elf_Addr  arg_interp;     /* Interpreter load address */
791         Elf_Addr  arg_phaddr;     /* program header address */
792         Elf_Addr  arg_phentsize;  /* Size of program header */
793         Elf_Addr  arg_phnum;      /* Number of program headers */
794 };
795 #endif
796 
797 #ifndef _LKM
798 #include "opt_execfmt.h"
799 #endif
800 
801 #ifdef EXEC_ELF32
802 int	exec_elf32_makecmds(struct proc *, struct exec_package *);
803 int	elf32_copyargs(struct proc *, struct exec_package *,
804     	    struct ps_strings *, char **, void *);
805 
806 int	coredump_elf32(struct lwp *, struct vnode *, struct ucred *);
807 int	coredump_writenote_elf32(struct proc *, struct vnode *,
808 	    struct ucred *, off_t, Elf32_Nhdr *, const char *, void *);
809 
810 int	elf32_check_header(Elf32_Ehdr *, int);
811 #endif
812 
813 #ifdef EXEC_ELF64
814 int	exec_elf64_makecmds(struct proc *, struct exec_package *);
815 int	elf64_read_from(struct proc *, struct vnode *, u_long, caddr_t, int);
816 int	elf64_copyargs(struct proc *, struct exec_package *,
817 	    struct ps_strings *, char **, void *);
818 
819 int	coredump_elf64 __P((struct lwp *, struct vnode *, struct ucred *));
820 int	coredump_writenote_elf64 __P((struct proc *, struct vnode *,
821 	    struct ucred *, off_t, Elf64_Nhdr *, const char *, void *));
822 
823 int	elf64_check_header(Elf64_Ehdr *, int);
824 #endif
825 
826 #endif /* _KERNEL */
827 
828 #endif /* !_SYS_EXEC_ELF_H_ */
829