xref: /openbsd/sys/sys/exec_elf.h (revision e49cad03)
1 /*	$OpenBSD: exec_elf.h,v 1.106 2024/11/25 04:37:06 jsg Exp $	*/
2 /*
3  * Copyright (c) 1995, 1996 Erik Theisen.  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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /*
29  * This is the ELF ABI header file
30  * formerly known as "elf_abi.h".
31  */
32 
33 #ifndef _SYS_EXEC_ELF_H_
34 #define _SYS_EXEC_ELF_H_
35 
36 #include <sys/types.h>
37 #include <machine/exec.h>
38 
39 typedef __uint32_t	Elf32_Addr;	/* Unsigned program address */
40 typedef __uint32_t	Elf32_Off;	/* Unsigned file offset */
41 typedef __int32_t	Elf32_Sword;	/* Signed large integer */
42 typedef __uint32_t	Elf32_Word;	/* Unsigned large integer */
43 typedef __uint16_t	Elf32_Half;	/* Unsigned medium integer */
44 typedef __uint64_t	Elf32_Lword;
45 
46 typedef __uint64_t	Elf64_Addr;
47 typedef __uint64_t	Elf64_Off;
48 typedef __int32_t	Elf64_Shalf;
49 
50 typedef __int32_t	Elf64_Sword;
51 typedef __uint32_t	Elf64_Word;
52 
53 typedef __int64_t	Elf64_Sxword;
54 typedef __uint64_t	Elf64_Xword;
55 typedef __uint64_t	Elf64_Lword;
56 
57 typedef __uint16_t	Elf64_Half;
58 
59 /*
60  * e_ident[] identification indexes
61  * See http://www.sco.com/developers/gabi/latest/ch4.eheader.html
62  */
63 #define EI_MAG0		0		/* file ID */
64 #define EI_MAG1		1		/* file ID */
65 #define EI_MAG2		2		/* file ID */
66 #define EI_MAG3		3		/* file ID */
67 #define EI_CLASS	4		/* file class */
68 #define EI_DATA		5		/* data encoding */
69 #define EI_VERSION	6		/* ELF header version */
70 #define EI_OSABI	7		/* OS/ABI ID */
71 #define EI_ABIVERSION	8		/* ABI version */
72 #define EI_PAD		9		/* start of pad bytes */
73 #define EI_NIDENT	16		/* Size of e_ident[] */
74 
75 /* e_ident[] magic number */
76 #define	ELFMAG0		0x7f		/* e_ident[EI_MAG0] */
77 #define	ELFMAG1		'E'		/* e_ident[EI_MAG1] */
78 #define	ELFMAG2		'L'		/* e_ident[EI_MAG2] */
79 #define	ELFMAG3		'F'		/* e_ident[EI_MAG3] */
80 #define	ELFMAG		"\177ELF"	/* magic */
81 #define	SELFMAG		4		/* size of magic */
82 
83 /* e_ident[] file class */
84 #define	ELFCLASSNONE	0		/* invalid */
85 #define	ELFCLASS32	1		/* 32-bit objs */
86 #define	ELFCLASS64	2		/* 64-bit objs */
87 #define	ELFCLASSNUM	3		/* number of classes */
88 
89 /* e_ident[] data encoding */
90 #define ELFDATANONE	0		/* invalid */
91 #define ELFDATA2LSB	1		/* Little-Endian */
92 #define ELFDATA2MSB	2		/* Big-Endian */
93 #define ELFDATANUM	3		/* number of data encode defines */
94 
95 /* e_ident[] Operating System/ABI */
96 #define ELFOSABI_SYSV		0	/* UNIX System V ABI */
97 #define ELFOSABI_HPUX		1	/* HP-UX operating system */
98 #define ELFOSABI_NETBSD		2	/* NetBSD */
99 #define ELFOSABI_LINUX		3	/* GNU/Linux */
100 #define ELFOSABI_HURD		4	/* GNU/Hurd */
101 #define ELFOSABI_86OPEN		5	/* 86Open common IA32 ABI */
102 #define ELFOSABI_SOLARIS	6	/* Solaris */
103 #define ELFOSABI_MONTEREY	7	/* Monterey */
104 #define ELFOSABI_IRIX		8	/* IRIX */
105 #define ELFOSABI_FREEBSD	9	/* FreeBSD */
106 #define ELFOSABI_TRU64		10	/* TRU64 UNIX */
107 #define ELFOSABI_MODESTO	11	/* Novell Modesto */
108 #define ELFOSABI_OPENBSD	12	/* OpenBSD */
109 #define ELFOSABI_ARM		97	/* ARM */
110 #define ELFOSABI_STANDALONE	255	/* Standalone (embedded) application */
111 
112 /* e_ident */
113 #define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
114                       (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
115                       (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
116                       (ehdr).e_ident[EI_MAG3] == ELFMAG3)
117 
118 /* ELF Header */
119 typedef struct elfhdr {
120 	unsigned char	e_ident[EI_NIDENT]; /* ELF Identification */
121 	Elf32_Half	e_type;		/* object file type */
122 	Elf32_Half	e_machine;	/* machine */
123 	Elf32_Word	e_version;	/* object file version */
124 	Elf32_Addr	e_entry;	/* virtual entry point */
125 	Elf32_Off	e_phoff;	/* program header table offset */
126 	Elf32_Off	e_shoff;	/* section header table offset */
127 	Elf32_Word	e_flags;	/* processor-specific flags */
128 	Elf32_Half	e_ehsize;	/* ELF header size */
129 	Elf32_Half	e_phentsize;	/* program header entry size */
130 	Elf32_Half	e_phnum;	/* number of program header entries */
131 	Elf32_Half	e_shentsize;	/* section header entry size */
132 	Elf32_Half	e_shnum;	/* number of section header entries */
133 	Elf32_Half	e_shstrndx;	/* section header table's "section
134 					   header string table" entry offset */
135 } Elf32_Ehdr;
136 
137 typedef struct {
138 	unsigned char	e_ident[EI_NIDENT];	/* Id bytes */
139 	Elf64_Half	e_type;			/* file type */
140 	Elf64_Half	e_machine;		/* machine type */
141 	Elf64_Word	e_version;		/* version number */
142 	Elf64_Addr	e_entry;		/* entry point */
143 	Elf64_Off	e_phoff;		/* Program hdr offset */
144 	Elf64_Off	e_shoff;		/* Section hdr offset */
145 	Elf64_Word	e_flags;		/* Processor flags */
146 	Elf64_Half	e_ehsize;		/* sizeof ehdr */
147 	Elf64_Half	e_phentsize;		/* Program header entry size */
148 	Elf64_Half	e_phnum;		/* Number of program headers */
149 	Elf64_Half	e_shentsize;		/* Section header entry size */
150 	Elf64_Half	e_shnum;		/* Number of section headers */
151 	Elf64_Half	e_shstrndx;		/* String table index */
152 } Elf64_Ehdr;
153 
154 /* e_type */
155 #define ET_NONE		0		/* No file type */
156 #define ET_REL		1		/* relocatable file */
157 #define ET_EXEC		2		/* executable file */
158 #define ET_DYN		3		/* shared object file */
159 #define ET_CORE		4		/* core file */
160 #define ET_NUM		5		/* number of types */
161 #define ET_LOPROC	0xff00		/* reserved range for processor */
162 #define ET_HIPROC	0xffff		/*  specific e_type */
163 
164 /* e_machine */
165 #define EM_NONE		0		/* No Machine */
166 #define EM_M32		1		/* AT&T WE 32100 */
167 #define EM_SPARC	2		/* SPARC */
168 #define EM_386		3		/* Intel 80386 */
169 #define EM_68K		4		/* Motorola 68000 */
170 #define EM_88K		5		/* Motorola 88000 */
171 #define EM_486		6		/* Intel 80486 - unused? */
172 #define EM_860		7		/* Intel 80860 */
173 #define EM_MIPS		8		/* MIPS R3000 Big-Endian only */
174 /*
175  * Don't know if EM_MIPS_RS4_BE,
176  * EM_SPARC64, EM_PARISC,
177  * or EM_PPC are ABI compliant
178  */
179 #define EM_MIPS_RS4_BE	10		/* MIPS R4000 Big-Endian */
180 #define EM_SPARC64	11		/* SPARC v9 64-bit unofficial */
181 #define EM_PARISC	15		/* HPPA */
182 #define EM_SPARC32PLUS	18		/* Enhanced instruction set SPARC */
183 #define EM_PPC		20		/* PowerPC */
184 #define EM_PPC64	21		/* PowerPC 64 */
185 #define EM_ARM		40		/* Advanced RISC Machines ARM */
186 #define EM_ALPHA	41		/* DEC ALPHA */
187 #define EM_SH		42		/* Hitachi/Renesas Super-H */
188 #define EM_SPARCV9	43		/* SPARC version 9 */
189 #define EM_IA_64	50		/* Intel IA-64 Processor */
190 #define EM_AMD64	62		/* AMD64 architecture */
191 #define EM_X86_64	EM_AMD64
192 #define EM_VAX		75		/* DEC VAX */
193 #define EM_AARCH64	183		/* ARM 64-bit architecture (AArch64) */
194 #define EM_RISCV	243		/* RISC-V */
195 
196 /* Non-standard */
197 #define EM_ALPHA_EXP	0x9026		/* DEC ALPHA */
198 #define EM__LAST__	(EM_ALPHA_EXP + 1)
199 
200 /* Version */
201 #define EV_NONE		0		/* Invalid */
202 #define EV_CURRENT	1		/* Current */
203 #define EV_NUM		2		/* number of versions */
204 
205 /* Magic for e_phnum: get real value from sh_info of first section header */
206 #define PN_XNUM		0xffff
207 
208 /* Section Header */
209 typedef struct {
210 	Elf32_Word	sh_name;	/* name - index into section header
211 					   string table section */
212 	Elf32_Word	sh_type;	/* type */
213 	Elf32_Word	sh_flags;	/* flags */
214 	Elf32_Addr	sh_addr;	/* address */
215 	Elf32_Off	sh_offset;	/* file offset */
216 	Elf32_Word	sh_size;	/* section size */
217 	Elf32_Word	sh_link;	/* section header table index link */
218 	Elf32_Word	sh_info;	/* extra information */
219 	Elf32_Word	sh_addralign;	/* address alignment */
220 	Elf32_Word	sh_entsize;	/* section entry size */
221 } Elf32_Shdr;
222 
223 typedef struct {
224 	Elf64_Word	sh_name;	/* section name */
225 	Elf64_Word	sh_type;	/* section type */
226 	Elf64_Xword	sh_flags;	/* section flags */
227 	Elf64_Addr	sh_addr;	/* virtual address */
228 	Elf64_Off	sh_offset;	/* file offset */
229 	Elf64_Xword	sh_size;	/* section size */
230 	Elf64_Word	sh_link;	/* link to another */
231 	Elf64_Word	sh_info;	/* misc info */
232 	Elf64_Xword	sh_addralign;	/* memory alignment */
233 	Elf64_Xword	sh_entsize;	/* table entry size */
234 } Elf64_Shdr;
235 
236 /* Special Section Indexes */
237 #define SHN_UNDEF	0		/* undefined */
238 #define SHN_LORESERVE	0xff00		/* lower bounds of reserved indexes */
239 #define SHN_LOPROC	0xff00		/* reserved range for processor */
240 #define SHN_HIPROC	0xff1f		/*   specific section indexes */
241 #define SHN_ABS		0xfff1		/* absolute value */
242 #define SHN_COMMON	0xfff2		/* common symbol */
243 #define SHN_XINDEX	0xffff		/* Escape -- index stored elsewhere. */
244 #define SHN_HIRESERVE	0xffff		/* upper bounds of reserved indexes */
245 
246 /* sh_type */
247 #define SHT_NULL		0	/* inactive */
248 #define SHT_PROGBITS		1	/* program defined information */
249 #define SHT_SYMTAB		2	/* symbol table section */
250 #define SHT_STRTAB		3	/* string table section */
251 #define SHT_RELA		4	/* relocation section with addends*/
252 #define SHT_HASH		5	/* symbol hash table section */
253 #define SHT_DYNAMIC		6	/* dynamic section */
254 #define SHT_NOTE		7	/* note section */
255 #define SHT_NOBITS		8	/* no space section */
256 #define SHT_REL			9	/* relocation section without addends */
257 #define SHT_SHLIB		10	/* reserved - purpose unknown */
258 #define SHT_DYNSYM		11	/* dynamic symbol table section */
259 #define SHT_NUM			12	/* number of section types */
260 #define SHT_INIT_ARRAY		14	/* pointers to init functions */
261 #define SHT_FINI_ARRAY		15	/* pointers to termination functions */
262 #define SHT_PREINIT_ARRAY	16	/* ptrs to funcs called before init */
263 #define SHT_GROUP		17	/* defines a section group */
264 #define SHT_SYMTAB_SHNDX	18	/* Section indexes (see SHN_XINDEX). */
265 #define SHT_RELR		19	/* relative-only relocation section */
266 #define SHT_LOOS	0x60000000	/* reserved range for OS specific */
267 #define SHT_SUNW_dof	0x6ffffff4	/* used by dtrace */
268 #define SHT_GNU_LIBLIST	0x6ffffff7	/* libraries to be prelinked */
269 #define SHT_SUNW_move	0x6ffffffa	/* inf for partially init'ed symbols */
270 #define SHT_SUNW_syminfo	0x6ffffffc	/* ad symbol information */
271 #define SHT_SUNW_verdef		0x6ffffffd	/* symbol versioning inf */
272 #define SHT_SUNW_verneed	0x6ffffffe	/* symbol versioning req */
273 #define SHT_SUNW_versym		0x6fffffff	/* symbol versioning table */
274 #define SHT_HIOS	0x6fffffff	/*  section header types */
275 #define SHT_LOPROC	0x70000000	/* reserved range for processor */
276 #define SHT_HIPROC	0x7fffffff	/*  specific section header types */
277 #define SHT_LOUSER	0x80000000	/* reserved range for application */
278 #define SHT_HIUSER	0xffffffff	/*  specific indexes */
279 
280 #define SHT_GNU_HASH	0x6ffffff6	/* GNU-style hash table section */
281 
282 /* Section names */
283 #define ELF_BSS         ".bss"		/* uninitialized data */
284 #define ELF_DATA        ".data"		/* initialized data */
285 #define ELF_CTF		".SUNW_ctf"	/* CTF data */
286 #define ELF_DEBUG       ".debug"	/* debug */
287 #define ELF_DYNAMIC     ".dynamic"	/* dynamic linking information */
288 #define ELF_DYNSTR      ".dynstr"	/* dynamic string table */
289 #define ELF_DYNSYM      ".dynsym"	/* dynamic symbol table */
290 #define ELF_FINI        ".fini"		/* termination code */
291 #define ELF_GOT         ".got"		/* global offset table */
292 #define ELF_HASH        ".hash"		/* symbol hash table */
293 #define ELF_INIT        ".init"		/* initialization code */
294 #define ELF_REL_DATA    ".rel.data"	/* relocation data */
295 #define ELF_REL_FINI    ".rel.fini"	/* relocation termination code */
296 #define ELF_REL_INIT    ".rel.init"	/* relocation initialization code */
297 #define ELF_REL_DYN     ".rel.dyn"	/* relocation dynamic link info */
298 #define ELF_REL_RODATA  ".rel.rodata"	/* relocation read-only data */
299 #define ELF_REL_TEXT    ".rel.text"	/* relocation code */
300 #define ELF_RODATA      ".rodata"	/* read-only data */
301 #define ELF_SHSTRTAB    ".shstrtab"	/* section header string table */
302 #define ELF_STRTAB      ".strtab"	/* string table */
303 #define ELF_SYMTAB      ".symtab"	/* symbol table */
304 #define ELF_TEXT        ".text"		/* code */
305 #define ELF_OPENBSDRANDOMDATA ".openbsd.randomdata" /* constant randomdata */
306 #define ELF_OPENBSDMUTABLE ".openbsd.mutable" /* mutable bss */
307 
308 
309 /* Section Attribute Flags - sh_flags */
310 #define SHF_WRITE		0x1	/* Writable */
311 #define SHF_ALLOC		0x2	/* occupies memory */
312 #define SHF_EXECINSTR		0x4	/* executable */
313 #define SHF_MERGE		0x10	/* may be merged */
314 #define SHF_STRINGS		0x20	/* contains strings */
315 #define SHF_INFO_LINK		0x40	/* sh_info holds section index */
316 #define SHF_LINK_ORDER		0x80	/* ordering requirements */
317 #define SHF_OS_NONCONFORMING	0x100	/* OS-specific processing required */
318 #define SHF_GROUP		0x200	/* member of section group */
319 #define SHF_TLS			0x400	/* thread local storage */
320 #define SHF_COMPRESSED		0x800	/* contains compressed data */
321 #define SHF_MASKOS	0x0ff00000	/* OS-specific semantics */
322 #define SHF_MASKPROC	0xf0000000	/* reserved bits for processor */
323 					/*  specific section attributes */
324 
325 /* Symbol Table Entry */
326 typedef struct elf32_sym {
327 	Elf32_Word	st_name;	/* name - index into string table */
328 	Elf32_Addr	st_value;	/* symbol value */
329 	Elf32_Word	st_size;	/* symbol size */
330 	unsigned char	st_info;	/* type and binding */
331 	unsigned char	st_other;	/* 0 - no defined meaning */
332 	Elf32_Half	st_shndx;	/* section header index */
333 } Elf32_Sym;
334 
335 typedef struct {
336 	Elf64_Word	st_name;	/* Symbol name index in str table */
337 	unsigned char	st_info;	/* type / binding attrs */
338 	unsigned char	st_other;	/* unused */
339 	Elf64_Half	st_shndx;	/* section index of symbol */
340 	Elf64_Addr	st_value;	/* value of symbol */
341 	Elf64_Xword	st_size;	/* size of symbol */
342 } Elf64_Sym;
343 
344 /* Symbol table index */
345 #define STN_UNDEF	0		/* undefined */
346 
347 /* Extract symbol info - st_info */
348 #define ELF32_ST_BIND(x)	((x) >> 4)
349 #define ELF32_ST_TYPE(x)	(((unsigned int) x) & 0xf)
350 #define ELF32_ST_INFO(b,t)	(((b) << 4) + ((t) & 0xf))
351 
352 #define ELF64_ST_BIND(x)	((x) >> 4)
353 #define ELF64_ST_TYPE(x)	(((unsigned int) x) & 0xf)
354 #define ELF64_ST_INFO(b,t)	(((b) << 4) + ((t) & 0xf))
355 
356 /* Symbol Binding - ELF32_ST_BIND - st_info */
357 #define STB_LOCAL	0		/* Local symbol */
358 #define STB_GLOBAL	1		/* Global symbol */
359 #define STB_WEAK	2		/* like global - lower precedence */
360 #define STB_NUM		3		/* number of symbol bindings */
361 #define STB_LOPROC	13		/* reserved range for processor */
362 #define STB_HIPROC	15		/*  specific symbol bindings */
363 
364 /* Symbol type - ELF32_ST_TYPE - st_info */
365 #define STT_NOTYPE	0		/* not specified */
366 #define STT_OBJECT	1		/* data object */
367 #define STT_FUNC	2		/* function */
368 #define STT_SECTION	3		/* section */
369 #define STT_FILE	4		/* file */
370 #define STT_TLS		6		/* thread local storage */
371 #define STT_LOPROC	13		/* reserved range for processor */
372 #define STT_HIPROC	15		/*  specific symbol types */
373 
374 /* Extract symbol visibility - st_other */
375 #define ELF_ST_VISIBILITY(v)		((v) & 0x3)
376 #define ELF32_ST_VISIBILITY		ELF_ST_VISIBILITY
377 #define ELF64_ST_VISIBILITY		ELF_ST_VISIBILITY
378 
379 #define STV_DEFAULT	0		/* Visibility set by binding type */
380 #define STV_INTERNAL	1		/* OS specific version of STV_HIDDEN */
381 #define STV_HIDDEN	2		/* can only be seen inside own .so */
382 #define STV_PROTECTED	3		/* HIDDEN inside, DEFAULT outside */
383 
384 /* Relocation entry with implicit addend */
385 typedef struct {
386 	Elf32_Addr	r_offset;	/* offset of relocation */
387 	Elf32_Word	r_info;		/* symbol table index and type */
388 } Elf32_Rel;
389 
390 /* Relocation entry with explicit addend */
391 typedef struct {
392 	Elf32_Addr	r_offset;	/* offset of relocation */
393 	Elf32_Word	r_info;		/* symbol table index and type */
394 	Elf32_Sword	r_addend;
395 } Elf32_Rela;
396 
397 /* Extract relocation info - r_info */
398 #define ELF32_R_SYM(i)		((i) >> 8)
399 #define ELF32_R_TYPE(i)		((unsigned char) (i))
400 #define ELF32_R_INFO(s,t) 	(((s) << 8) + (unsigned char)(t))
401 
402 typedef struct {
403 	Elf64_Addr	r_offset;	/* where to do it */
404 	Elf64_Xword	r_info;		/* index & type of relocation */
405 } Elf64_Rel;
406 
407 typedef struct {
408 	Elf64_Addr	r_offset;	/* where to do it */
409 	Elf64_Xword	r_info;		/* index & type of relocation */
410 	Elf64_Sxword	r_addend;	/* adjustment value */
411 } Elf64_Rela;
412 
413 #define	ELF64_R_SYM(info)	((info) >> 32)
414 #define	ELF64_R_TYPE(info)	((info) & 0xFFFFFFFF)
415 #define ELF64_R_INFO(s,t) 	(((s) << 32) + (__uint32_t)(t))
416 
417 #if defined(__mips64__) && defined(__MIPSEL__)
418 /*
419  * The 64-bit MIPS ELF ABI uses a slightly different relocation format
420  * than the regular ELF ABI: the r_info field is split into several
421  * pieces (see gnu/usr.bin/binutils-2.17/include/elf/mips.h for details).
422  */
423 #undef	ELF64_R_SYM
424 #undef	ELF64_R_TYPE
425 #undef	ELF64_R_INFO
426 #define	ELF64_R_TYPE(info)	((__uint64_t)swap32((info) >> 32))
427 #define	ELF64_R_SYM(info)	((info) & 0xFFFFFFFF)
428 #define	ELF64_R_INFO(s,t)	(((__uint64_t)swap32(t) << 32) + (__uint32_t)(s))
429 #endif	/* __mips64__ && __MIPSEL__ */
430 
431 /*
432  * Relative Relocation info.
433  * c.f. decode_relrs() in gnu/llvm/llvm/lib/Object/ELF.cpp
434  */
435 typedef Elf32_Word	Elf32_Relr;
436 typedef Elf64_Xword	Elf64_Relr;
437 
438 /* Program Header */
439 typedef struct {
440 	Elf32_Word	p_type;		/* segment type */
441 	Elf32_Off	p_offset;	/* segment offset */
442 	Elf32_Addr	p_vaddr;	/* virtual address of segment */
443 	Elf32_Addr	p_paddr;	/* physical address - ignored? */
444 	Elf32_Word	p_filesz;	/* number of bytes in file for seg. */
445 	Elf32_Word	p_memsz;	/* number of bytes in mem. for seg. */
446 	Elf32_Word	p_flags;	/* flags */
447 	Elf32_Word	p_align;	/* memory alignment */
448 } Elf32_Phdr;
449 
450 typedef struct {
451 	Elf64_Word	p_type;		/* entry type */
452 	Elf64_Word	p_flags;	/* flags */
453 	Elf64_Off	p_offset;	/* offset */
454 	Elf64_Addr	p_vaddr;	/* virtual address */
455 	Elf64_Addr	p_paddr;	/* physical address */
456 	Elf64_Xword	p_filesz;	/* file size */
457 	Elf64_Xword	p_memsz;	/* memory size */
458 	Elf64_Xword	p_align;	/* memory & file alignment */
459 } Elf64_Phdr;
460 
461 /* Segment types - p_type */
462 #define PT_NULL		0		/* unused */
463 #define PT_LOAD		1		/* loadable segment */
464 #define PT_DYNAMIC	2		/* dynamic linking section */
465 #define PT_INTERP	3		/* the RTLD */
466 #define PT_NOTE		4		/* auxiliary information */
467 #define PT_SHLIB	5		/* reserved - purpose undefined */
468 #define PT_PHDR		6		/* program header */
469 #define PT_TLS		7		/* thread local storage */
470 #define PT_LOOS		0x60000000	/* reserved range for OS */
471 #define PT_HIOS		0x6fffffff	/*  specific segment types */
472 #define PT_LOPROC	0x70000000	/* reserved range for processor */
473 #define PT_HIPROC	0x7fffffff	/*  specific segment types */
474 
475 #define PT_GNU_EH_FRAME		0x6474e550	/* Exception handling info */
476 #define PT_GNU_RELRO		0x6474e552	/* Read-only after relocation */
477 #define PT_GNU_PROPERTY		0x6474e553	/* Program property note */
478 
479 #define PT_OPENBSD_MUTABLE	0x65a3dbe5	/* like bss, but not immutable */
480 #define PT_OPENBSD_RANDOMIZE	0x65a3dbe6	/* fill with random data */
481 #define PT_OPENBSD_WXNEEDED	0x65a3dbe7	/* program performs W^X violations */
482 #define PT_OPENBSD_NOBTCFI	0x65a3dbe8	/* no branch target CFI */
483 #define PT_OPENBSD_SYSCALLS	0x65a3dbe9	/* syscall locations */
484 #define PT_OPENBSD_BOOTDATA	0x65a41be6	/* section for boot arguments */
485 
486 /* Segment flags - p_flags */
487 #define PF_X		0x1		/* Executable */
488 #define PF_W		0x2		/* Writable */
489 #define PF_R		0x4		/* Readable */
490 #define PF_MASKOS	0x0ff00000	/* reserved bits for OS */
491 					/*  specific segment flags */
492 #define PF_MASKPROC	0xf0000000	/* reserved bits for processor */
493 					/*  specific segment flags */
494 
495 #define PF_OPENBSD_MUTABLE	0x08000000	/* Mutable */
496 
497 #ifdef	_KERNEL
498 #define PF_ISVNODE	0x00100000	/* For coredump segments */
499 #endif
500 
501 /* Dynamic structure */
502 typedef struct {
503 	Elf32_Sword	d_tag;		/* controls meaning of d_val */
504 	union {
505 		Elf32_Word	d_val;	/* Multiple meanings - see d_tag */
506 		Elf32_Addr	d_ptr;	/* program virtual address */
507 	} d_un;
508 } Elf32_Dyn;
509 
510 typedef struct {
511 	Elf64_Xword	d_tag;		/* controls meaning of d_val */
512 	union {
513 		Elf64_Addr	d_ptr;
514 		Elf64_Xword	d_val;
515 	} d_un;
516 } Elf64_Dyn;
517 
518 /* Dynamic Array Tags - d_tag */
519 #define DT_NULL		0		/* marks end of _DYNAMIC array */
520 #define DT_NEEDED	1		/* string table offset of needed lib */
521 #define DT_PLTRELSZ	2		/* size of relocation entries in PLT */
522 #define DT_PLTGOT	3		/* address PLT/GOT */
523 #define DT_HASH		4		/* address of symbol hash table */
524 #define DT_STRTAB	5		/* address of string table */
525 #define DT_SYMTAB	6		/* address of symbol table */
526 #define DT_RELA		7		/* address of relocation table */
527 #define DT_RELASZ	8		/* size of relocation table */
528 #define DT_RELAENT	9		/* size of relocation entry */
529 #define DT_STRSZ	10		/* size of string table */
530 #define DT_SYMENT	11		/* size of symbol table entry */
531 #define DT_INIT		12		/* address of initialization func. */
532 #define DT_FINI		13		/* address of termination function */
533 #define DT_SONAME	14		/* string table offset of shared obj */
534 #define DT_RPATH	15		/* string table offset of library
535 					   search path */
536 #define DT_SYMBOLIC	16		/* start sym search in shared obj. */
537 #define DT_REL		17		/* address of rel. tbl. w addends */
538 #define DT_RELSZ	18		/* size of DT_REL relocation table */
539 #define DT_RELENT	19		/* size of DT_REL relocation entry */
540 #define DT_PLTREL	20		/* PLT referenced relocation entry */
541 #define DT_DEBUG	21		/* bugger */
542 #define DT_TEXTREL	22		/* Allow rel. mod. to unwritable seg */
543 #define DT_JMPREL	23		/* add. of PLT's relocation entries */
544 #define DT_BIND_NOW	24		/* Bind now regardless of env setting */
545 #define DT_INIT_ARRAY	25		/* address of array of init func */
546 #define DT_FINI_ARRAY	26		/* address of array of term func */
547 #define DT_INIT_ARRAYSZ	27		/* size of array of init func */
548 #define DT_FINI_ARRAYSZ	28		/* size of array of term func */
549 #define DT_RUNPATH	29		/* strtab offset of lib search path */
550 #define DT_FLAGS	30		/* Set of DF_* flags */
551 #define DT_ENCODING	31		/* further DT_* follow encoding rules */
552 #define DT_PREINIT_ARRAY	32	/* address of array of preinit func */
553 #define DT_PREINIT_ARRAYSZ	33	/* size of array of preinit func */
554 #define DT_RELRSZ	35		/* size of DT_RELR relocation table */
555 #define DT_RELR		36		/* addr of DT_RELR relocation table */
556 #define DT_RELRENT	37		/* size of DT_RELR relocation entry */
557 #define DT_LOOS		0x6000000d	/* reserved range for OS */
558 #define DT_HIOS		0x6ffff000	/*  specific dynamic array tags */
559 #define DT_LOPROC	0x70000000	/* reserved range for processor */
560 #define DT_HIPROC	0x7fffffff	/*  specific dynamic array tags */
561 
562 /* some other useful tags */
563 #define DT_GNU_HASH	0x6ffffef5	/* address of GNU hash table */
564 #define DT_RELACOUNT	0x6ffffff9	/* if present, number of RELATIVE */
565 #define DT_RELCOUNT	0x6ffffffa	/* relocs, which must come first */
566 #define DT_FLAGS_1      0x6ffffffb
567 
568 /* Dynamic Flags - DT_FLAGS .dynamic entry */
569 #define DF_ORIGIN       0x00000001
570 #define DF_SYMBOLIC     0x00000002
571 #define DF_TEXTREL      0x00000004
572 #define DF_BIND_NOW     0x00000008
573 #define DF_STATIC_TLS   0x00000010
574 
575 /* Dynamic Flags - DT_FLAGS_1 .dynamic entry */
576 #define DF_1_NOW	0x00000001
577 #define DF_1_GLOBAL	0x00000002
578 #define DF_1_GROUP	0x00000004
579 #define DF_1_NODELETE	0x00000008
580 #define DF_1_LOADFLTR	0x00000010
581 #define DF_1_INITFIRST	0x00000020
582 #define DF_1_NOOPEN	0x00000040
583 #define DF_1_ORIGIN	0x00000080
584 #define DF_1_DIRECT	0x00000100
585 #define DF_1_TRANS	0x00000200
586 #define DF_1_INTERPOSE	0x00000400
587 #define DF_1_NODEFLIB	0x00000800
588 #define DF_1_NODUMP	0x00001000
589 #define DF_1_CONLFAT	0x00002000
590 #define DF_1_ENDFILTEE	0x00004000
591 #define DF_1_DISPRELDNE	0x00008000
592 #define DF_1_DISPRELPND	0x00010000
593 #define DF_1_NODIRECT	0x00020000
594 #define DF_1_IGNMULDEF	0x00040000
595 #define DF_1_NOKSYMS	0x00080000
596 #define DF_1_NOHDR	0x00100000
597 #define DF_1_EDITED	0x00200000
598 #define DF_1_NORELOC	0x00400000
599 #define DF_1_SYMINTPOSE	0x00800000
600 #define DF_1_GLOBAUDIT	0x01000000
601 #define DF_1_SINGLETON	0x02000000
602 #define DF_1_PIE	0x08000000
603 
604 /*
605  * Note header
606  */
607 typedef struct {
608 	Elf32_Word n_namesz;
609 	Elf32_Word n_descsz;
610 	Elf32_Word n_type;
611 } Elf32_Nhdr;
612 
613 typedef struct {
614 	Elf64_Word n_namesz;
615 	Elf64_Word n_descsz;
616 	Elf64_Word n_type;
617 } Elf64_Nhdr;
618 
619 /*
620  * Note Definitions
621  */
622 typedef struct {
623 	Elf32_Word namesz;
624 	Elf32_Word descsz;
625 	Elf32_Word type;
626 } Elf32_Note;
627 
628 typedef struct {
629 	Elf64_Word namesz;
630 	Elf64_Word descsz;
631 	Elf64_Word type;
632 } Elf64_Note;
633 
634 /* Values for n_type. */
635 #define NT_PRSTATUS		1	/* Process status. */
636 #define NT_FPREGSET		2	/* Floating point registers. */
637 #define NT_PRPSINFO		3	/* Process state info. */
638 
639 /*
640  * OpenBSD-specific core file information.
641  *
642  * OpenBSD ELF core files use notes to provide information about
643  * the process's state.  The note name is "OpenBSD" for information
644  * that is global to the process, and "OpenBSD@nn", where "nn" is the
645  * thread ID of the thread that the information belongs to (such as
646  * register state).
647  *
648  * We use the following note identifiers:
649  *
650  *	NT_OPENBSD_PROCINFO
651  *		Note is a "elfcore_procinfo" structure.
652  *	NT_OPENBSD_AUXV
653  *		Note is a bunch of Auxiliary Vectors, terminated by
654  *		an AT_NULL entry.
655  *	NT_OPENBSD_REGS
656  *		Note is a "reg" structure.
657  *	NT_OPENBSD_FPREGS
658  *		Note is a "fpreg" structure.
659  *
660  * Please try to keep the members of the "elfcore_procinfo" structure
661  * nicely aligned, and if you add elements, add them to the end and
662  * bump the version.
663  */
664 
665 #define NT_OPENBSD_PROCINFO	10
666 #define NT_OPENBSD_AUXV		11
667 
668 #define NT_OPENBSD_REGS		20
669 #define NT_OPENBSD_FPREGS	21
670 #define NT_OPENBSD_XFPREGS	22
671 #define NT_OPENBSD_WCOOKIE	23
672 #define NT_OPENBSD_PACMASK	24
673 
674 struct elfcore_procinfo {
675 	/* Version 1 fields start here. */
676 	uint32_t	cpi_version;	/* elfcore_procinfo version */
677 #define ELFCORE_PROCINFO_VERSION	1
678 	uint32_t	cpi_cpisize;	/* sizeof(elfcore_procinfo) */
679 	uint32_t	cpi_signo;	/* killing signal */
680 	uint32_t	cpi_sigcode;	/* signal code */
681 	uint32_t	cpi_sigpend;	/* pending signals */
682 	uint32_t	cpi_sigmask;	/* blocked signals */
683 	uint32_t	cpi_sigignore;	/* ignored signals */
684 	uint32_t	cpi_sigcatch;	/* signals being caught by user */
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 	int8_t		cpi_name[32];	/* copy of pr->ps_comm */
696 };
697 
698 /*
699  * XXX - these _KERNEL items aren't part of the ABI!
700  */
701 #if defined(_KERNEL) || defined(_DYN_LOADER)
702 
703 #define ELF32_NO_ADDR	((uint32_t) ~0)	/* Indicates addr. not yet filled in */
704 
705 typedef struct {
706 	Elf32_Sword	au_id;				/* 32-bit id */
707 	Elf32_Word	au_v;				/* 32-bit value */
708 } Aux32Info;
709 
710 #define ELF64_NO_ADDR	((__uint64_t) ~0)/* Indicates addr. not yet filled in */
711 
712 typedef struct {
713 	Elf64_Shalf	au_id;				/* 32-bit id */
714 	Elf64_Xword	au_v;				/* 64-bit value */
715 } Aux64Info;
716 
717 enum AuxID {
718 	AUX_null = 0,
719 	AUX_ignore = 1,
720 	AUX_execfd = 2,
721 	AUX_phdr = 3,			/* &phdr[0] */
722 	AUX_phent = 4,			/* sizeof(phdr[0]) */
723 	AUX_phnum = 5,			/* # phdr entries */
724 	AUX_pagesz = 6,			/* PAGESIZE */
725 	AUX_base = 7,			/* base addr for ld.so or static PIE */
726 	AUX_flags = 8,			/* processor flags */
727 	AUX_entry = 9,			/* a.out entry */
728 	AUX_hwcap = 25,			/* processor flags */
729 	AUX_hwcap2 = 26,		/* processor flags (continued) */
730 	AUX_sun_uid = 2000,		/* euid */
731 	AUX_sun_ruid = 2001,		/* ruid */
732 	AUX_sun_gid = 2002,		/* egid */
733 	AUX_sun_rgid = 2003,		/* rgid */
734 	AUX_openbsd_timekeep = 4000,	/* userland clock_gettime */
735 };
736 
737 struct elf_args {
738         u_long  arg_entry;		/* program entry point */
739         u_long  arg_interp;		/* Interpreter load address */
740         u_long  arg_phaddr;		/* program header address */
741         u_long  arg_phentsize;		/* Size of program header */
742         u_long  arg_phnum;		/* Number of program headers */
743 };
744 
745 #endif
746 
747 #if !defined(ELFSIZE) && defined(ARCH_ELFSIZE)
748 #define ELFSIZE ARCH_ELFSIZE
749 #endif
750 
751 #if defined(ELFSIZE)
752 #define CONCAT(x,y)	__CONCAT(x,y)
753 #define ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
754 #define ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
755 #endif
756 
757 #if defined(ELFSIZE) && (ELFSIZE == 32)
758 #define Elf_Ehdr	Elf32_Ehdr
759 #define Elf_Phdr	Elf32_Phdr
760 #define Elf_Shdr	Elf32_Shdr
761 #define Elf_Sym		Elf32_Sym
762 #define Elf_Rel		Elf32_Rel
763 #define Elf_RelA	Elf32_Rela
764 #define Elf_Relr	Elf32_Relr
765 #define Elf_Dyn		Elf32_Dyn
766 #define Elf_Half	Elf32_Half
767 #define Elf_Word	Elf32_Word
768 #define Elf_Sword	Elf32_Sword
769 #define Elf_Addr	Elf32_Addr
770 #define Elf_Off		Elf32_Off
771 #define Elf_Nhdr	Elf32_Nhdr
772 #define Elf_Note	Elf32_Note
773 
774 #define ELF_R_SYM	ELF32_R_SYM
775 #define ELF_R_TYPE	ELF32_R_TYPE
776 #define ELF_R_INFO	ELF32_R_INFO
777 #define ELFCLASS	ELFCLASS32
778 
779 #define ELF_ST_BIND	ELF32_ST_BIND
780 #define ELF_ST_TYPE	ELF32_ST_TYPE
781 #define ELF_ST_INFO	ELF32_ST_INFO
782 
783 #define ELF_NO_ADDR	ELF32_NO_ADDR
784 #define AuxInfo		Aux32Info
785 #elif defined(ELFSIZE) && (ELFSIZE == 64)
786 #define Elf_Ehdr	Elf64_Ehdr
787 #define Elf_Phdr	Elf64_Phdr
788 #define Elf_Shdr	Elf64_Shdr
789 #define Elf_Sym		Elf64_Sym
790 #define Elf_Rel		Elf64_Rel
791 #define Elf_RelA	Elf64_Rela
792 #define Elf_Relr	Elf64_Relr
793 #define Elf_Dyn		Elf64_Dyn
794 #define Elf_Half	Elf64_Half
795 #define Elf_Word	Elf64_Word
796 #define Elf_Sword	Elf64_Sword
797 #define Elf_Addr	Elf64_Addr
798 #define Elf_Off		Elf64_Off
799 #define Elf_Nhdr	Elf64_Nhdr
800 #define Elf_Note	Elf64_Note
801 
802 #define ELF_R_SYM	ELF64_R_SYM
803 #define ELF_R_TYPE	ELF64_R_TYPE
804 #define ELF_R_INFO	ELF64_R_INFO
805 #define ELFCLASS	ELFCLASS64
806 
807 #define ELF_ST_BIND	ELF64_ST_BIND
808 #define ELF_ST_TYPE	ELF64_ST_TYPE
809 #define ELF_ST_INFO	ELF64_ST_INFO
810 
811 #define ELF_NO_ADDR	ELF64_NO_ADDR
812 #define AuxInfo		Aux64Info
813 #endif
814 
815 #ifndef _KERNEL
816 extern Elf_Dyn		_DYNAMIC[];
817 #endif
818 
819 #ifdef	_KERNEL
820 /*
821  * How many entries are in the AuxInfo array we pass to the process?
822  */
823 #define	ELF_AUX_ENTRIES	11
824 #define	ELF_AUX_WORDS	(sizeof(AuxInfo) * ELF_AUX_ENTRIES / sizeof(char *))
825 
826 struct exec_package;
827 
828 int	exec_elf_makecmds(struct proc *, struct exec_package *);
829 int	exec_elf_fixup(struct proc *, struct exec_package *);
830 int	coredump_elf(struct proc *, void *);
831 #endif /* _KERNEL */
832 
833 #define ELF_TARG_VER	1	/* The ver for which this code is intended */
834 
835 #endif /* _SYS_EXEC_ELF_H_ */
836