1 /* SPDX-License-Identifier: BSD-2-Clause
2  *
3  * Copyright (c) 1996-1998 John D. Polstra.  All rights reserved.
4  * Copyright (c) 2001 David E. O'Brien
5  * Portions Copyright 2009 The Go Authors.  All rights reserved.
6  */
7 
8 /*
9  * ELF definitions that are independent of architecture or word size.
10  */
11 
12 /*
13  * Note header.  The ".note" section contains an array of notes.  Each
14  * begins with this header, aligned to a word boundary.  Immediately
15  * following the note header is n_namesz bytes of name, padded to the
16  * next word boundary.  Then comes n_descsz bytes of descriptor, again
17  * padded to a word boundary.  The values of n_namesz and n_descsz do
18  * not include the padding.
19  */
20 
21 #ifndef __INCLUDE_ELF_DEFS_H__
22 #define __INCLUDE_ELF_DEFS_H__
23 
24 #include <stdbool.h>
25 #include <stdint.h>
26 
27 typedef uint64_t uint64;
28 typedef uint32_t uint32;
29 typedef uint16_t uint16;
30 typedef uint8_t uint8;
31 
32 typedef int64_t int64;
33 typedef int32_t int32;
34 typedef int16_t int16;
35 typedef int8_t int8;
36 
37 typedef struct {
38 	uint32	n_namesz;	/* Length of name. */
39 	uint32	n_descsz;	/* Length of descriptor. */
40 	uint32	n_type;		/* Type of this note. */
41 } Elf_Note;
42 
43 /* Indexes into the e_ident array.  Keep synced with
44    http://www.sco.com/developer/gabi/ch4.eheader.html */
45 #define EI_MAG0		0	/* Magic number, byte 0. */
46 #define EI_MAG1		1	/* Magic number, byte 1. */
47 #define EI_MAG2		2	/* Magic number, byte 2. */
48 #define EI_MAG3		3	/* Magic number, byte 3. */
49 #define EI_CLASS	4	/* Class of machine. */
50 #define EI_DATA		5	/* Data format. */
51 #define EI_VERSION	6	/* ELF format version. */
52 #define EI_OSABI	7	/* Operating system / ABI identification */
53 #define EI_ABIVERSION	8	/* ABI version */
54 #define OLD_EI_BRAND	8	/* Start of architecture identification. */
55 #define EI_PAD		9	/* Start of padding (per SVR4 ABI). */
56 #define EI_NIDENT	16	/* Size of e_ident array. */
57 
58 /* Values for the magic number bytes. */
59 #define ELFMAG0		0x7f
60 #define ELFMAG1		'E'
61 #define ELFMAG2		'L'
62 #define ELFMAG3		'F'
63 #define ELFMAG		"\177ELF"	/* magic string */
64 #define SELFMAG		4		/* magic string size */
65 
66 /* Values for e_ident[EI_VERSION] and e_version. */
67 #define EV_NONE		0
68 #define EV_CURRENT	1
69 
70 /* Values for e_ident[EI_CLASS]. */
71 #define ELFCLASSNONE	0	/* Unknown class. */
72 #define ELFCLASS32	1	/* 32-bit architecture. */
73 #define ELFCLASS64	2	/* 64-bit architecture. */
74 
75 /* Values for e_ident[EI_DATA]. */
76 #define ELFDATANONE	0	/* Unknown data format. */
77 #define ELFDATA2LSB	1	/* 2's complement little-endian. */
78 #define ELFDATA2MSB	2	/* 2's complement big-endian. */
79 
80 /* Values for e_ident[EI_OSABI]. */
81 #define ELFOSABI_NONE		0	/* UNIX System V ABI */
82 #define ELFOSABI_HPUX		1	/* HP-UX operating system */
83 #define ELFOSABI_NETBSD		2	/* NetBSD */
84 #define ELFOSABI_LINUX		3	/* GNU/Linux */
85 #define ELFOSABI_HURD		4	/* GNU/Hurd */
86 #define ELFOSABI_86OPEN		5	/* 86Open common IA32 ABI */
87 #define ELFOSABI_SOLARIS	6	/* Solaris */
88 #define ELFOSABI_AIX		7	/* AIX */
89 #define ELFOSABI_IRIX		8	/* IRIX */
90 #define ELFOSABI_FREEBSD	9	/* FreeBSD */
91 #define ELFOSABI_TRU64		10	/* TRU64 UNIX */
92 #define ELFOSABI_MODESTO	11	/* Novell Modesto */
93 #define ELFOSABI_OPENBSD	12	/* OpenBSD */
94 #define ELFOSABI_OPENVMS	13	/* Open VMS */
95 #define ELFOSABI_NSK		14	/* HP Non-Stop Kernel */
96 #define ELFOSABI_ARM		97	/* ARM */
97 #define ELFOSABI_STANDALONE	255	/* Standalone (embedded) application */
98 
99 #define ELFOSABI_SYSV		ELFOSABI_NONE	/* symbol used in old spec */
100 #define ELFOSABI_MONTEREY	ELFOSABI_AIX	/* Monterey */
101 
102 /* e_ident */
103 #define IS_ELF(ehdr)	((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
104 			 (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
105 			 (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
106 			 (ehdr).e_ident[EI_MAG3] == ELFMAG3)
107 
108 /* Values for e_type. */
109 #define ET_NONE		0	/* Unknown type. */
110 #define ET_REL		1	/* Relocatable. */
111 #define ET_EXEC		2	/* Executable. */
112 #define ET_DYN		3	/* Shared object. */
113 #define ET_CORE		4	/* Core file. */
114 #define ET_LOOS		0xfe00	/* First operating system specific. */
115 #define ET_HIOS		0xfeff	/* Last operating system-specific. */
116 #define ET_LOPROC	0xff00	/* First processor-specific. */
117 #define ET_HIPROC	0xffff	/* Last processor-specific. */
118 
119 /* Values for e_machine. */
120 #define EM_NONE		0	/* Unknown machine. */
121 #define EM_M32		1	/* AT&T WE32100. */
122 #define EM_SPARC	2	/* Sun SPARC. */
123 #define EM_386		3	/* Intel i386. */
124 #define EM_68K		4	/* Motorola 68000. */
125 #define EM_88K		5	/* Motorola 88000. */
126 #define EM_860		7	/* Intel i860. */
127 #define EM_MIPS		8	/* MIPS R3000 Big-Endian only. */
128 #define EM_S370		9	/* IBM System/370. */
129 #define EM_MIPS_RS3_LE	10	/* MIPS R3000 Little-Endian. */
130 #define EM_PARISC	15	/* HP PA-RISC. */
131 #define EM_VPP500	17	/* Fujitsu VPP500. */
132 #define EM_SPARC32PLUS	18	/* SPARC v8plus. */
133 #define EM_960		19	/* Intel 80960. */
134 #define EM_PPC		20	/* PowerPC 32-bit. */
135 #define EM_PPC64	21	/* PowerPC 64-bit. */
136 #define EM_S390		22	/* IBM System/390. */
137 #define EM_V800		36	/* NEC V800. */
138 #define EM_FR20		37	/* Fujitsu FR20. */
139 #define EM_RH32		38	/* TRW RH-32. */
140 #define EM_RCE		39	/* Motorola RCE. */
141 #define EM_ARM		40	/* ARM. */
142 #define EM_SH		42	/* Hitachi SH. */
143 #define EM_SPARCV9	43	/* SPARC v9 64-bit. */
144 #define EM_TRICORE	44	/* Siemens TriCore embedded processor. */
145 #define EM_ARC		45	/* Argonaut RISC Core. */
146 #define EM_H8_300	46	/* Hitachi H8/300. */
147 #define EM_H8_300H	47	/* Hitachi H8/300H. */
148 #define EM_H8S		48	/* Hitachi H8S. */
149 #define EM_H8_500	49	/* Hitachi H8/500. */
150 #define EM_IA_64	50	/* Intel IA-64 Processor. */
151 #define EM_MIPS_X	51	/* Stanford MIPS-X. */
152 #define EM_COLDFIRE	52	/* Motorola ColdFire. */
153 #define EM_68HC12	53	/* Motorola M68HC12. */
154 #define EM_MMA		54	/* Fujitsu MMA. */
155 #define EM_PCP		55	/* Siemens PCP. */
156 #define EM_NCPU		56	/* Sony nCPU. */
157 #define EM_NDR1		57	/* Denso NDR1 microprocessor. */
158 #define EM_STARCORE	58	/* Motorola Star*Core processor. */
159 #define EM_ME16		59	/* Toyota ME16 processor. */
160 #define EM_ST100	60	/* STMicroelectronics ST100 processor. */
161 #define EM_TINYJ	61	/* Advanced Logic Corp. TinyJ processor. */
162 #define EM_X86_64	62	/* Advanced Micro Devices x86-64 */
163 
164 /* Non-standard or deprecated. */
165 #define EM_486		6	/* Intel i486. */
166 #define EM_MIPS_RS4_BE	10	/* MIPS R4000 Big-Endian */
167 #define EM_ALPHA_STD	41	/* Digital Alpha (standard value). */
168 #define EM_ALPHA	0x9026	/* Alpha (written in the absence of an ABI) */
169 
170 /* Special section indexes. */
171 #define SHN_UNDEF	     0		/* Undefined, missing, irrelevant. */
172 #define SHN_LORESERVE	0xff00		/* First of reserved range. */
173 #define SHN_LOPROC	0xff00		/* First processor-specific. */
174 #define SHN_HIPROC	0xff1f		/* Last processor-specific. */
175 #define SHN_LOOS	0xff20		/* First operating system-specific. */
176 #define SHN_HIOS	0xff3f		/* Last operating system-specific. */
177 #define SHN_ABS		0xfff1		/* Absolute values. */
178 #define SHN_COMMON	0xfff2		/* Common data. */
179 #define SHN_XINDEX	0xffff		/* Escape -- index stored elsewhere. */
180 #define SHN_HIRESERVE	0xffff		/* Last of reserved range. */
181 
182 /* sh_type */
183 #define SHT_NULL		0	/* inactive */
184 #define SHT_PROGBITS		1	/* program defined information */
185 #define SHT_SYMTAB		2	/* symbol table section */
186 #define SHT_STRTAB		3	/* string table section */
187 #define SHT_RELA		4	/* relocation section with addends */
188 #define SHT_HASH		5	/* symbol hash table section */
189 #define SHT_DYNAMIC		6	/* dynamic section */
190 #define SHT_NOTE		7	/* note section */
191 #define SHT_NOBITS		8	/* no space section */
192 #define SHT_REL			9	/* relocation section - no addends */
193 #define SHT_SHLIB		10	/* reserved - purpose unknown */
194 #define SHT_DYNSYM		11	/* dynamic symbol table section */
195 #define SHT_INIT_ARRAY		14	/* Initialization function pointers. */
196 #define SHT_FINI_ARRAY		15	/* Termination function pointers. */
197 #define SHT_PREINIT_ARRAY	16	/* Pre-initialization function ptrs. */
198 #define SHT_GROUP		17	/* Section group. */
199 #define SHT_SYMTAB_SHNDX	18	/* Section indexes (see SHN_XINDEX). */
200 #define SHT_LOOS	0x60000000	/* First of OS specific semantics */
201 #define SHT_HIOS	0x6fffffff	/* Last of OS specific semantics */
202 #define SHT_GNU_VERDEF	0x6ffffffd
203 #define SHT_GNU_VERNEED	0x6ffffffe
204 #define SHT_GNU_VERSYM	0x6fffffff
205 #define SHT_LOPROC	0x70000000	/* reserved range for processor */
206 #define SHT_HIPROC	0x7fffffff	/* specific section header types */
207 #define SHT_LOUSER	0x80000000	/* reserved range for application */
208 #define SHT_HIUSER	0xffffffff	/* specific indexes */
209 
210 /* Flags for sh_flags. */
211 #define SHF_WRITE		0x1	/* Section contains writable data. */
212 #define SHF_ALLOC		0x2	/* Section occupies memory. */
213 #define SHF_EXECINSTR		0x4	/* Section contains instructions. */
214 #define SHF_MERGE		0x10	/* Section may be merged. */
215 #define SHF_STRINGS		0x20	/* Section contains strings. */
216 #define SHF_INFO_LINK		0x40	/* sh_info holds section index. */
217 #define SHF_LINK_ORDER		0x80	/* Special ordering requirements. */
218 #define SHF_OS_NONCONFORMING	0x100	/* OS-specific processing required. */
219 #define SHF_GROUP		0x200	/* Member of section group. */
220 #define SHF_TLS			0x400	/* Section contains TLS data. */
221 #define SHF_MASKOS	0x0ff00000	/* OS-specific semantics. */
222 #define SHF_MASKPROC	0xf0000000	/* Processor-specific semantics. */
223 
224 /* Values for p_type. */
225 #define PT_NULL		0	/* Unused entry. */
226 #define PT_LOAD		1	/* Loadable segment. */
227 #define PT_DYNAMIC	2	/* Dynamic linking information segment. */
228 #define PT_INTERP	3	/* Pathname of interpreter. */
229 #define PT_NOTE		4	/* Auxiliary information. */
230 #define PT_SHLIB	5	/* Reserved (not used). */
231 #define PT_PHDR		6	/* Location of program header itself. */
232 #define PT_TLS		7	/* Thread local storage segment */
233 #define PT_LOOS		0x60000000	/* First OS-specific. */
234 #define PT_HIOS		0x6fffffff	/* Last OS-specific. */
235 #define PT_LOPROC	0x70000000	/* First processor-specific type. */
236 #define PT_HIPROC	0x7fffffff	/* Last processor-specific type. */
237 #define PT_GNU_STACK	0x6474e551
238 #define PT_PAX_FLAGS	0x65041580
239 
240 /* Values for p_flags. */
241 #define PF_X		0x1		/* Executable. */
242 #define PF_W		0x2		/* Writable. */
243 #define PF_R		0x4		/* Readable. */
244 #define PF_MASKOS	0x0ff00000	/* Operating system-specific. */
245 #define PF_MASKPROC	0xf0000000	/* Processor-specific. */
246 
247 /* Values for d_tag. */
248 #define DT_NULL		0	/* Terminating entry. */
249 /* String table offset of a needed shared library. */
250 #define DT_NEEDED	1
251 #define DT_PLTRELSZ	2	/* Total size in bytes of PLT relocations. */
252 #define DT_PLTGOT	3	/* Processor-dependent address. */
253 #define DT_HASH		4	/* Address of symbol hash table. */
254 #define DT_STRTAB	5	/* Address of string table. */
255 #define DT_SYMTAB	6	/* Address of symbol table. */
256 #define DT_RELA		7	/* Address of ElfNN_Rela relocations. */
257 #define DT_RELASZ	8	/* Total size of ElfNN_Rela relocations. */
258 #define DT_RELAENT	9	/* Size of each ElfNN_Rela relocation entry. */
259 #define DT_STRSZ	10	/* Size of string table. */
260 #define DT_SYMENT	11	/* Size of each symbol table entry. */
261 #define DT_INIT		12	/* Address of initialization function. */
262 #define DT_FINI		13	/* Address of finalization function. */
263 /* String table offset of shared object name. */
264 #define DT_SONAME	14
265 #define DT_RPATH	15	/* String table offset of library path. [sup] */
266 #define DT_SYMBOLIC	16	/* Indicates "symbolic" linking. [sup] */
267 #define DT_REL		17	/* Address of ElfNN_Rel relocations. */
268 #define DT_RELSZ	18	/* Total size of ElfNN_Rel relocations. */
269 #define DT_RELENT	19	/* Size of each ElfNN_Rel relocation. */
270 #define DT_PLTREL	20	/* Type of relocation used for PLT. */
271 #define DT_DEBUG	21	/* Reserved (not used). */
272 /* Indicates there may be relocations in non-writable segments. [sup] */
273 #define DT_TEXTREL	22
274 #define DT_JMPREL	23	/* Address of PLT relocations. */
275 #define	DT_BIND_NOW	24	/* [sup] */
276 /* Address of the array of pointers to initialization functions */
277 #define	DT_INIT_ARRAY	25
278 /* Address of the array of pointers to termination functions */
279 #define	DT_FINI_ARRAY	26
280 /* Size in bytes of the array of initialization functions. */
281 #define	DT_INIT_ARRAYSZ	27
282 /* Size in bytes of the array of terminationfunctions. */
283 #define	DT_FINI_ARRAYSZ	28
284 /* String table offset of a null-terminated library search path string. */
285 #define	DT_RUNPATH	29
286 #define	DT_FLAGS	30	/* Object specific flag values. */
287 /*	Values greater than or equal to DT_ENCODING and less than
288 	DT_LOOS follow the rules for the interpretation of the d_un
289 	union as follows: even == 'd_ptr', even == 'd_val' or none */
290 #define	DT_ENCODING	32
291 /* Address of the array of pointers to pre-initialization functions. */
292 #define	DT_PREINIT_ARRAY 32
293 /* Size in bytes of the array of pre-initialization functions. */
294 #define	DT_PREINIT_ARRAYSZ 33
295 #define	DT_LOOS		0x6000000d	/* First OS-specific */
296 #define	DT_HIOS		0x6ffff000	/* Last OS-specific */
297 #define	DT_LOPROC	0x70000000	/* First processor-specific type. */
298 #define	DT_HIPROC	0x7fffffff	/* Last processor-specific type. */
299 
300 #define	DT_VERNEED	0x6ffffffe
301 #define	DT_VERNEEDNUM	0x6fffffff
302 #define	DT_VERSYM	0x6ffffff0
303 
304 /* Values for DT_FLAGS */
305 /*	Indicates that the object being loaded may make reference to
306 	the $ORIGIN substitution string */
307 #define	DF_ORIGIN	0x0001
308 #define	DF_SYMBOLIC	0x0002	/* Indicates "symbolic" linking. */
309 /* Indicates there may be relocations in non-writable segments. */
310 #define	DF_TEXTREL	0x0004
311 /*	Indicates that the dynamic linker should process all
312 	relocations for the object containing this entry before
313 	transferring control to the program.  */
314 #define	DF_BIND_NOW	0x0008
315 /*	Indicates that the shared object or executable contains code
316 	using a static thread-local storage scheme.  */
317 #define	DF_STATIC_TLS	0x0010
318 
319 /* Values for n_type.  Used in core files. */
320 #define NT_PRSTATUS	1	/* Process status. */
321 #define NT_FPREGSET	2	/* Floating point registers. */
322 #define NT_PRPSINFO	3	/* Process state info. */
323 
324 /* Symbol Binding - ELFNN_ST_BIND - st_info */
325 #define STB_LOCAL	0	/* Local symbol */
326 #define STB_GLOBAL	1	/* Global symbol */
327 #define STB_WEAK	2	/* like global - lower precedence */
328 #define STB_LOOS	10	/* Reserved range for operating system */
329 #define STB_HIOS	12	/*   specific semantics. */
330 #define STB_LOPROC	13	/* reserved range for processor */
331 #define STB_HIPROC	15	/*   specific semantics. */
332 
333 /* Symbol type - ELFNN_ST_TYPE - st_info */
334 #define STT_NOTYPE	0	/* Unspecified type. */
335 #define STT_OBJECT	1	/* Data object. */
336 #define STT_FUNC	2	/* Function. */
337 #define STT_SECTION	3	/* Section. */
338 #define STT_FILE	4	/* Source file. */
339 #define STT_COMMON	5	/* Uninitialized common block. */
340 #define STT_TLS		6	/* TLS object. */
341 #define STT_LOOS	10	/* Reserved range for operating system */
342 #define STT_HIOS	12	/*   specific semantics. */
343 #define STT_LOPROC	13	/* reserved range for processor */
344 #define STT_HIPROC	15	/*   specific semantics. */
345 
346 /* Symbol visibility - ELFNN_ST_VISIBILITY - st_other */
347 #define STV_DEFAULT	0x0	/* Default visibility (see binding). */
348 #define STV_INTERNAL	0x1	/* Special meaning in relocatable objects. */
349 #define STV_HIDDEN	0x2	/* Not visible. */
350 #define STV_PROTECTED	0x3	/* Visible but not preemptible. */
351 
352 /* Special symbol table indexes. */
353 #define STN_UNDEF	0	/* Undefined symbol index. */
354 
355 /*
356  * ELF definitions common to all 32-bit architectures.
357  */
358 
359 typedef uint32	Elf32_Addr;
360 typedef uint16	Elf32_Half;
361 typedef uint32	Elf32_Off;
362 typedef int32	Elf32_Sword;
363 typedef uint32	Elf32_Word;
364 
365 typedef Elf32_Word	Elf32_Hashelt;
366 
367 /* Non-standard class-dependent datatype used for abstraction. */
368 typedef Elf32_Word	Elf32_Size;
369 typedef Elf32_Sword	Elf32_Ssize;
370 
371 /*
372  * ELF header.
373  */
374 
375 typedef struct {
376 	unsigned char	ident[EI_NIDENT];	/* File identification. */
377 	Elf32_Half	type;		/* File type. */
378 	Elf32_Half	machine;	/* Machine architecture. */
379 	Elf32_Word	version;	/* ELF format version. */
380 	Elf32_Addr	entry;	/* Entry point. */
381 	Elf32_Off	phoff;	/* Program header file offset. */
382 	Elf32_Off	shoff;	/* Section header file offset. */
383 	Elf32_Word	flags;	/* Architecture-specific flags. */
384 	Elf32_Half	ehsize;	/* Size of ELF header in bytes. */
385 	Elf32_Half	phentsize;	/* Size of program header entry. */
386 	Elf32_Half	phnum;	/* Number of program header entries. */
387 	Elf32_Half	shentsize;	/* Size of section header entry. */
388 	Elf32_Half	shnum;	/* Number of section header entries. */
389 	Elf32_Half	shstrndx;	/* Section name strings section. */
390 } Elf32_Ehdr;
391 
392 /*
393  * Section header.
394  */
395 
396 typedef struct {
397 	Elf32_Word	name;	/* Section name (index into the
398 					   section header string table). */
399 	Elf32_Word	type;	/* Section type. */
400 	Elf32_Word	flags;	/* Section flags. */
401 	Elf32_Addr	vaddr;	/* Address in memory image. */
402 	Elf32_Off	off;	/* Offset in file. */
403 	Elf32_Word	size;	/* Size in bytes. */
404 	Elf32_Word	link;	/* Index of a related section. */
405 	Elf32_Word	info;	/* Depends on section type. */
406 	Elf32_Word	addralign;	/* Alignment in bytes. */
407 	Elf32_Word	entsize;	/* Size of each entry in section. */
408 } Elf32_Shdr;
409 
410 /*
411  * Program header.
412  */
413 
414 typedef struct {
415 	Elf32_Word	type;		/* Entry type. */
416 	Elf32_Off	off;	/* File offset of contents. */
417 	Elf32_Addr	vaddr;	/* Virtual address in memory image. */
418 	Elf32_Addr	paddr;	/* Physical address (not used). */
419 	Elf32_Word	filesz;	/* Size of contents in file. */
420 	Elf32_Word	memsz;	/* Size of contents in memory. */
421 	Elf32_Word	flags;	/* Access permission flags. */
422 	Elf32_Word	align;	/* Alignment in memory and file. */
423 } Elf32_Phdr;
424 
425 /*
426  * Dynamic structure.  The ".dynamic" section contains an array of them.
427  */
428 
429 typedef struct {
430 	Elf32_Sword	d_tag;		/* Entry type. */
431 	union {
432 		Elf32_Word	d_val;	/* Integer value. */
433 		Elf32_Addr	d_ptr;	/* Address value. */
434 	} d_un;
435 } Elf32_Dyn;
436 
437 /*
438  * Relocation entries.
439  */
440 
441 /* Relocations that don't need an addend field. */
442 typedef struct {
443 	Elf32_Addr	off;	/* Location to be relocated. */
444 	Elf32_Word	info;		/* Relocation type and symbol index. */
445 } Elf32_Rel;
446 
447 /* Relocations that need an addend field. */
448 typedef struct {
449 	Elf32_Addr	off;	/* Location to be relocated. */
450 	Elf32_Word	info;		/* Relocation type and symbol index. */
451 	Elf32_Sword	addend;	/* Addend. */
452 } Elf32_Rela;
453 
454 /* Macros for accessing the fields of r_info. */
455 #define ELF32_R_SYM(info)	((info) >> 8)
456 #define ELF32_R_TYPE(info)	((unsigned char)(info))
457 
458 /* Macro for constructing r_info from field values. */
459 #define ELF32_R_INFO(sym, type)	(((sym) << 8) + (unsigned char)(type))
460 
461 /*
462  * Relocation types.
463  */
464 
465 #define	R_X86_64_NONE	0	/* No relocation. */
466 #define	R_X86_64_64	1	/* Add 64 bit symbol value. */
467 #define	R_X86_64_PC32	2	/* PC-relative 32 bit signed sym value. */
468 #define	R_X86_64_GOT32	3	/* PC-relative 32 bit GOT offset. */
469 #define	R_X86_64_PLT32	4	/* PC-relative 32 bit PLT offset. */
470 #define	R_X86_64_COPY	5	/* Copy data from shared object. */
471 #define	R_X86_64_GLOB_DAT 6	/* Set GOT entry to data address. */
472 #define	R_X86_64_JMP_SLOT 7	/* Set GOT entry to code address. */
473 #define	R_X86_64_RELATIVE 8	/* Add load address of shared object. */
474 #define	R_X86_64_GOTPCREL 9	/* Add 32 bit signed pcrel offset to GOT. */
475 #define	R_X86_64_32	10	/* Add 32 bit zero extended symbol value */
476 #define	R_X86_64_32S	11	/* Add 32 bit sign extended symbol value */
477 #define	R_X86_64_16	12	/* Add 16 bit zero extended symbol value */
478 #define	R_X86_64_PC16	13	/* Add 16 bit signed extended pc relative symbol value */
479 #define	R_X86_64_8	14	/* Add 8 bit zero extended symbol value */
480 #define	R_X86_64_PC8	15	/* Add 8 bit signed extended pc relative symbol value */
481 #define	R_X86_64_DTPMOD64 16	/* ID of module containing symbol */
482 #define	R_X86_64_DTPOFF64 17	/* Offset in TLS block */
483 #define	R_X86_64_TPOFF64 18	/* Offset in static TLS block */
484 #define	R_X86_64_TLSGD	19	/* PC relative offset to GD GOT entry */
485 #define	R_X86_64_TLSLD	20	/* PC relative offset to LD GOT entry */
486 #define	R_X86_64_DTPOFF32 21	/* Offset in TLS block */
487 #define	R_X86_64_GOTTPOFF 22	/* PC relative offset to IE GOT entry */
488 #define	R_X86_64_TPOFF32 23	/* Offset in static TLS block */
489 
490 #define	R_X86_64_COUNT	24	/* Count of defined relocation types. */
491 
492 #define	R_ALPHA_NONE		0	/* No reloc */
493 #define	R_ALPHA_REFLONG		1	/* Direct 32 bit */
494 #define	R_ALPHA_REFQUAD		2	/* Direct 64 bit */
495 #define	R_ALPHA_GPREL32		3	/* GP relative 32 bit */
496 #define	R_ALPHA_LITERAL		4	/* GP relative 16 bit w/optimization */
497 #define	R_ALPHA_LITUSE		5	/* Optimization hint for LITERAL */
498 #define	R_ALPHA_GPDISP		6	/* Add displacement to GP */
499 #define	R_ALPHA_BRADDR		7	/* PC+4 relative 23 bit shifted */
500 #define	R_ALPHA_HINT		8	/* PC+4 relative 16 bit shifted */
501 #define	R_ALPHA_SREL16		9	/* PC relative 16 bit */
502 #define	R_ALPHA_SREL32		10	/* PC relative 32 bit */
503 #define	R_ALPHA_SREL64		11	/* PC relative 64 bit */
504 #define	R_ALPHA_OP_PUSH		12	/* OP stack push */
505 #define	R_ALPHA_OP_STORE	13	/* OP stack pop and store */
506 #define	R_ALPHA_OP_PSUB		14	/* OP stack subtract */
507 #define	R_ALPHA_OP_PRSHIFT	15	/* OP stack right shift */
508 #define	R_ALPHA_GPVALUE		16
509 #define	R_ALPHA_GPRELHIGH	17
510 #define	R_ALPHA_GPRELLOW	18
511 #define	R_ALPHA_IMMED_GP_16	19
512 #define	R_ALPHA_IMMED_GP_HI32	20
513 #define	R_ALPHA_IMMED_SCN_HI32	21
514 #define	R_ALPHA_IMMED_BR_HI32	22
515 #define	R_ALPHA_IMMED_LO32	23
516 #define	R_ALPHA_COPY		24	/* Copy symbol at runtime */
517 #define	R_ALPHA_GLOB_DAT	25	/* Create GOT entry */
518 #define	R_ALPHA_JMP_SLOT	26	/* Create PLT entry */
519 #define	R_ALPHA_RELATIVE	27	/* Adjust by program base */
520 
521 #define	R_ALPHA_COUNT		28
522 
523 #define	R_ARM_NONE		0	/* No relocation. */
524 #define	R_ARM_PC24		1
525 #define	R_ARM_ABS32		2
526 #define	R_ARM_REL32		3
527 #define	R_ARM_PC13		4
528 #define	R_ARM_ABS16		5
529 #define	R_ARM_ABS12		6
530 #define	R_ARM_THM_ABS5		7
531 #define	R_ARM_ABS8		8
532 #define	R_ARM_SBREL32		9
533 #define	R_ARM_THM_PC22		10
534 #define	R_ARM_THM_PC8		11
535 #define	R_ARM_AMP_VCALL9	12
536 #define	R_ARM_SWI24		13
537 #define	R_ARM_THM_SWI8		14
538 #define	R_ARM_XPC25		15
539 #define	R_ARM_THM_XPC22		16
540 #define	R_ARM_COPY		20	/* Copy data from shared object. */
541 #define	R_ARM_GLOB_DAT		21	/* Set GOT entry to data address. */
542 #define	R_ARM_JUMP_SLOT		22	/* Set GOT entry to code address. */
543 #define	R_ARM_RELATIVE		23	/* Add load address of shared object. */
544 #define	R_ARM_GOTOFF		24	/* Add GOT-relative symbol address. */
545 #define	R_ARM_GOTPC		25	/* Add PC-relative GOT table address. */
546 #define	R_ARM_GOT32		26	/* Add PC-relative GOT offset. */
547 #define	R_ARM_PLT32		27	/* Add PC-relative PLT offset. */
548 #define	R_ARM_CALL		28
549 #define	R_ARM_JUMP24	29
550 #define	R_ARM_V4BX		40
551 #define	R_ARM_GOT_PREL		96
552 #define	R_ARM_GNU_VTENTRY	100
553 #define	R_ARM_GNU_VTINHERIT	101
554 #define	R_ARM_TLS_IE32		107
555 #define	R_ARM_TLS_LE32		108
556 #define	R_ARM_RSBREL32		250
557 #define	R_ARM_THM_RPC22		251
558 #define	R_ARM_RREL32		252
559 #define	R_ARM_RABS32		253
560 #define	R_ARM_RPC24		254
561 #define	R_ARM_RBASE		255
562 
563 #define	R_ARM_COUNT		38	/* Count of defined relocation types. */
564 
565 #define	R_386_NONE	0	/* No relocation. */
566 #define	R_386_32	1	/* Add symbol value. */
567 #define	R_386_PC32	2	/* Add PC-relative symbol value. */
568 #define	R_386_GOT32	3	/* Add PC-relative GOT offset. */
569 #define	R_386_PLT32	4	/* Add PC-relative PLT offset. */
570 #define	R_386_COPY	5	/* Copy data from shared object. */
571 #define	R_386_GLOB_DAT	6	/* Set GOT entry to data address. */
572 #define	R_386_JMP_SLOT	7	/* Set GOT entry to code address. */
573 #define	R_386_RELATIVE	8	/* Add load address of shared object. */
574 #define	R_386_GOTOFF	9	/* Add GOT-relative symbol address. */
575 #define	R_386_GOTPC	10	/* Add PC-relative GOT table address. */
576 #define	R_386_TLS_TPOFF	14	/* Negative offset in static TLS block */
577 #define	R_386_TLS_IE	15	/* Absolute address of GOT for -ve static TLS */
578 #define	R_386_TLS_GOTIE	16	/* GOT entry for negative static TLS block */
579 #define	R_386_TLS_LE	17	/* Negative offset relative to static TLS */
580 #define	R_386_TLS_GD	18	/* 32 bit offset to GOT (index,off) pair */
581 #define	R_386_TLS_LDM	19	/* 32 bit offset to GOT (index,zero) pair */
582 #define	R_386_TLS_GD_32	24	/* 32 bit offset to GOT (index,off) pair */
583 #define	R_386_TLS_GD_PUSH 25	/* pushl instruction for Sun ABI GD sequence */
584 #define	R_386_TLS_GD_CALL 26	/* call instruction for Sun ABI GD sequence */
585 #define	R_386_TLS_GD_POP 27	/* popl instruction for Sun ABI GD sequence */
586 #define	R_386_TLS_LDM_32 28	/* 32 bit offset to GOT (index,zero) pair */
587 #define	R_386_TLS_LDM_PUSH 29	/* pushl instruction for Sun ABI LD sequence */
588 #define	R_386_TLS_LDM_CALL 30	/* call instruction for Sun ABI LD sequence */
589 #define	R_386_TLS_LDM_POP 31	/* popl instruction for Sun ABI LD sequence */
590 #define	R_386_TLS_LDO_32 32	/* 32 bit offset from start of TLS block */
591 #define	R_386_TLS_IE_32	33	/* 32 bit offset to GOT static TLS offset entry */
592 #define	R_386_TLS_LE_32	34	/* 32 bit offset within static TLS block */
593 #define	R_386_TLS_DTPMOD32 35	/* GOT entry containing TLS index */
594 #define	R_386_TLS_DTPOFF32 36	/* GOT entry containing TLS offset */
595 #define	R_386_TLS_TPOFF32 37	/* GOT entry of -ve static TLS offset */
596 
597 #define	R_386_COUNT	38	/* Count of defined relocation types. */
598 
599 #define	R_PPC_NONE		0	/* No relocation. */
600 #define	R_PPC_ADDR32		1
601 #define	R_PPC_ADDR24		2
602 #define	R_PPC_ADDR16		3
603 #define	R_PPC_ADDR16_LO		4
604 #define	R_PPC_ADDR16_HI		5
605 #define	R_PPC_ADDR16_HA		6
606 #define	R_PPC_ADDR14		7
607 #define	R_PPC_ADDR14_BRTAKEN	8
608 #define	R_PPC_ADDR14_BRNTAKEN	9
609 #define	R_PPC_REL24		10
610 #define	R_PPC_REL14		11
611 #define	R_PPC_REL14_BRTAKEN	12
612 #define	R_PPC_REL14_BRNTAKEN	13
613 #define	R_PPC_GOT16		14
614 #define	R_PPC_GOT16_LO		15
615 #define	R_PPC_GOT16_HI		16
616 #define	R_PPC_GOT16_HA		17
617 #define	R_PPC_PLTREL24		18
618 #define	R_PPC_COPY		19
619 #define	R_PPC_GLOB_DAT		20
620 #define	R_PPC_JMP_SLOT		21
621 #define	R_PPC_RELATIVE		22
622 #define	R_PPC_LOCAL24PC		23
623 #define	R_PPC_UADDR32		24
624 #define	R_PPC_UADDR16		25
625 #define	R_PPC_REL32		26
626 #define	R_PPC_PLT32		27
627 #define	R_PPC_PLTREL32		28
628 #define	R_PPC_PLT16_LO		29
629 #define	R_PPC_PLT16_HI		30
630 #define	R_PPC_PLT16_HA		31
631 #define	R_PPC_SDAREL16		32
632 #define	R_PPC_SECTOFF		33
633 #define	R_PPC_SECTOFF_LO	34
634 #define	R_PPC_SECTOFF_HI	35
635 #define	R_PPC_SECTOFF_HA	36
636 
637 #define	R_PPC_COUNT		37	/* Count of defined relocation types. */
638 
639 #define R_PPC_TLS		67
640 #define R_PPC_DTPMOD32		68
641 #define R_PPC_TPREL16		69
642 #define R_PPC_TPREL16_LO	70
643 #define R_PPC_TPREL16_HI	71
644 #define R_PPC_TPREL16_HA	72
645 #define R_PPC_TPREL32		73
646 #define R_PPC_DTPREL16		74
647 #define R_PPC_DTPREL16_LO	75
648 #define R_PPC_DTPREL16_HI	76
649 #define R_PPC_DTPREL16_HA	77
650 #define R_PPC_DTPREL32		78
651 #define R_PPC_GOT_TLSGD16	79
652 #define R_PPC_GOT_TLSGD16_LO	80
653 #define R_PPC_GOT_TLSGD16_HI	81
654 #define R_PPC_GOT_TLSGD16_HA	82
655 #define R_PPC_GOT_TLSLD16	83
656 #define R_PPC_GOT_TLSLD16_LO	84
657 #define R_PPC_GOT_TLSLD16_HI	85
658 #define R_PPC_GOT_TLSLD16_HA	86
659 #define R_PPC_GOT_TPREL16	87
660 #define R_PPC_GOT_TPREL16_LO	88
661 #define R_PPC_GOT_TPREL16_HI	89
662 #define R_PPC_GOT_TPREL16_HA	90
663 
664 #define	R_PPC_EMB_NADDR32	101
665 #define	R_PPC_EMB_NADDR16	102
666 #define	R_PPC_EMB_NADDR16_LO	103
667 #define	R_PPC_EMB_NADDR16_HI	104
668 #define	R_PPC_EMB_NADDR16_HA	105
669 #define	R_PPC_EMB_SDAI16	106
670 #define	R_PPC_EMB_SDA2I16	107
671 #define	R_PPC_EMB_SDA2REL	108
672 #define	R_PPC_EMB_SDA21		109
673 #define	R_PPC_EMB_MRKREF	110
674 #define	R_PPC_EMB_RELSEC16	111
675 #define	R_PPC_EMB_RELST_LO	112
676 #define	R_PPC_EMB_RELST_HI	113
677 #define	R_PPC_EMB_RELST_HA	114
678 #define	R_PPC_EMB_BIT_FLD	115
679 #define	R_PPC_EMB_RELSDA	116
680 
681 					/* Count of defined relocation types. */
682 #define	R_PPC_EMB_COUNT		(R_PPC_EMB_RELSDA - R_PPC_EMB_NADDR32 + 1)
683 
684 #define R_SPARC_NONE		0
685 #define R_SPARC_8		1
686 #define R_SPARC_16		2
687 #define R_SPARC_32		3
688 #define R_SPARC_DISP8		4
689 #define R_SPARC_DISP16		5
690 #define R_SPARC_DISP32		6
691 #define R_SPARC_WDISP30		7
692 #define R_SPARC_WDISP22		8
693 #define R_SPARC_HI22		9
694 #define R_SPARC_22		10
695 #define R_SPARC_13		11
696 #define R_SPARC_LO10		12
697 #define R_SPARC_GOT10		13
698 #define R_SPARC_GOT13		14
699 #define R_SPARC_GOT22		15
700 #define R_SPARC_PC10		16
701 #define R_SPARC_PC22		17
702 #define R_SPARC_WPLT30		18
703 #define R_SPARC_COPY		19
704 #define R_SPARC_GLOB_DAT	20
705 #define R_SPARC_JMP_SLOT	21
706 #define R_SPARC_RELATIVE	22
707 #define R_SPARC_UA32		23
708 #define R_SPARC_PLT32		24
709 #define R_SPARC_HIPLT22		25
710 #define R_SPARC_LOPLT10		26
711 #define R_SPARC_PCPLT32		27
712 #define R_SPARC_PCPLT22		28
713 #define R_SPARC_PCPLT10		29
714 #define R_SPARC_10		30
715 #define R_SPARC_11		31
716 #define R_SPARC_64		32
717 #define R_SPARC_OLO10		33
718 #define R_SPARC_HH22		34
719 #define R_SPARC_HM10		35
720 #define R_SPARC_LM22		36
721 #define R_SPARC_PC_HH22		37
722 #define R_SPARC_PC_HM10		38
723 #define R_SPARC_PC_LM22		39
724 #define R_SPARC_WDISP16		40
725 #define R_SPARC_WDISP19		41
726 #define R_SPARC_GLOB_JMP	42
727 #define R_SPARC_7		43
728 #define R_SPARC_5		44
729 #define R_SPARC_6		45
730 #define	R_SPARC_DISP64		46
731 #define	R_SPARC_PLT64		47
732 #define	R_SPARC_HIX22		48
733 #define	R_SPARC_LOX10		49
734 #define	R_SPARC_H44		50
735 #define	R_SPARC_M44		51
736 #define	R_SPARC_L44		52
737 #define	R_SPARC_REGISTER	53
738 #define	R_SPARC_UA64		54
739 #define	R_SPARC_UA16		55
740 
741 /*
742  * Magic number for the elf trampoline, chosen wisely to be an immediate
743  * value.
744  */
745 #define ARM_MAGIC_TRAMP_NUMBER	0x5c000003
746 
747 /*
748  * Symbol table entries.
749  */
750 
751 typedef struct {
752 	Elf32_Word	name;	/* String table index of name. */
753 	Elf32_Addr	value;	/* Symbol value. */
754 	Elf32_Word	size;	/* Size of associated object. */
755 	unsigned char	info;	/* Type and binding information. */
756 	unsigned char	other;	/* Reserved (not used). */
757 	Elf32_Half	shndx;	/* Section index of symbol. */
758 } Elf32_Sym;
759 
760 /* Macros for accessing the fields of st_info. */
761 #define ELF32_ST_BIND(info)		((info) >> 4)
762 #define ELF32_ST_TYPE(info)		((info) & 0xf)
763 
764 /* Macro for constructing st_info from field values. */
765 #define ELF32_ST_INFO(bind, type)	(((bind) << 4) + ((type) & 0xf))
766 
767 /* Macro for accessing the fields of st_other. */
768 #define ELF32_ST_VISIBILITY(oth)	((oth) & 0x3)
769 
770 /*
771  * ELF definitions common to all 64-bit architectures.
772  */
773 
774 typedef uint64	Elf64_Addr;
775 typedef uint16	Elf64_Half;
776 typedef uint64	Elf64_Off;
777 typedef int32		Elf64_Sword;
778 typedef int64		Elf64_Sxword;
779 typedef uint32	Elf64_Word;
780 typedef uint64	Elf64_Xword;
781 
782 /*
783  * Types of dynamic symbol hash table bucket and chain elements.
784  *
785  * This is inconsistent among 64 bit architectures, so a machine dependent
786  * typedef is required.
787  */
788 
789 #ifdef __alpha__
790 typedef Elf64_Off	Elf64_Hashelt;
791 #else
792 typedef Elf64_Word	Elf64_Hashelt;
793 #endif
794 
795 /* Non-standard class-dependent datatype used for abstraction. */
796 typedef Elf64_Xword	Elf64_Size;
797 typedef Elf64_Sxword	Elf64_Ssize;
798 
799 /*
800  * ELF header.
801  */
802 
803 typedef struct {
804 	unsigned char	ident[EI_NIDENT];	/* File identification. */
805 	Elf64_Half	type;		/* File type. */
806 	Elf64_Half	machine;	/* Machine architecture. */
807 	Elf64_Word	version;	/* ELF format version. */
808 	Elf64_Addr	entry;	/* Entry point. */
809 	Elf64_Off	phoff;	/* Program header file offset. */
810 	Elf64_Off	shoff;	/* Section header file offset. */
811 	Elf64_Word	flags;	/* Architecture-specific flags. */
812 	Elf64_Half	ehsize;	/* Size of ELF header in bytes. */
813 	Elf64_Half	phentsize;	/* Size of program header entry. */
814 	Elf64_Half	phnum;	/* Number of program header entries. */
815 	Elf64_Half	shentsize;	/* Size of section header entry. */
816 	Elf64_Half	shnum;	/* Number of section header entries. */
817 	Elf64_Half	shstrndx;	/* Section name strings section. */
818 } Elf64_Ehdr;
819 
820 /*
821  * Section header.
822  */
823 
824 typedef struct Elf64_Shdr Elf64_Shdr;
825 struct Elf64_Shdr {
826 	Elf64_Word	name;	/* Section name (index into the
827 					   section header string table). */
828 	Elf64_Word	type;	/* Section type. */
829 	Elf64_Xword	flags;	/* Section flags. */
830 	Elf64_Addr	addr;	/* Address in memory image. */
831 	Elf64_Off	off;	/* Offset in file. */
832 	Elf64_Xword	size;	/* Size in bytes. */
833 	Elf64_Word	link;	/* Index of a related section. */
834 	Elf64_Word	info;	/* Depends on section type. */
835 	Elf64_Xword	addralign;	/* Alignment in bytes. */
836 	Elf64_Xword	entsize;	/* Size of each entry in section. */
837 };
838 
839 /*
840  * Program header.
841  */
842 
843 typedef struct {
844 	Elf64_Word	type;		/* Entry type. */
845 	Elf64_Word	flags;	/* Access permission flags. */
846 	Elf64_Off	off;	/* File offset of contents. */
847 	Elf64_Addr	vaddr;	/* Virtual address in memory image. */
848 	Elf64_Addr	paddr;	/* Physical address (not used). */
849 	Elf64_Xword	filesz;	/* Size of contents in file. */
850 	Elf64_Xword	memsz;	/* Size of contents in memory. */
851 	Elf64_Xword	align;	/* Alignment in memory and file. */
852 } Elf64_Phdr;
853 
854 /*
855  * Dynamic structure.  The ".dynamic" section contains an array of them.
856  */
857 
858 typedef struct {
859 	Elf64_Sxword	d_tag;		/* Entry type. */
860 	union {
861 		Elf64_Xword	d_val;	/* Integer value. */
862 		Elf64_Addr	d_ptr;	/* Address value. */
863 	} d_un;
864 } Elf64_Dyn;
865 
866 /*
867  * Relocation entries.
868  */
869 
870 /* Relocations that don't need an addend field. */
871 typedef struct {
872 	Elf64_Addr	off;	/* Location to be relocated. */
873 	Elf64_Xword	info;		/* Relocation type and symbol index. */
874 } Elf64_Rel;
875 
876 /* Relocations that need an addend field. */
877 typedef struct {
878 	Elf64_Addr	off;	/* Location to be relocated. */
879 	Elf64_Xword	info;		/* Relocation type and symbol index. */
880 	Elf64_Sxword	addend;	/* Addend. */
881 } Elf64_Rela;
882 
883 /* Macros for accessing the fields of r_info. */
884 #define ELF64_R_SYM(info)	((info) >> 32)
885 #define ELF64_R_TYPE(info)	((info) & 0xffffffffL)
886 
887 /* Macro for constructing r_info from field values. */
888 #define ELF64_R_INFO(sym, type)	((((uint64)(sym)) << 32) + (((uint64)(type)) & 0xffffffffULL))
889 
890 /*
891  * Symbol table entries.
892  */
893 
894 typedef struct {
895 	Elf64_Word	name;	/* String table index of name. */
896 	unsigned char	info;	/* Type and binding information. */
897 	unsigned char	other;	/* Reserved (not used). */
898 	Elf64_Half	shndx;	/* Section index of symbol. */
899 	Elf64_Addr	value;	/* Symbol value. */
900 	Elf64_Xword	size;	/* Size of associated object. */
901 } Elf64_Sym;
902 
903 /* Macros for accessing the fields of st_info. */
904 #define ELF64_ST_BIND(info)		((info) >> 4)
905 #define ELF64_ST_TYPE(info)		((info) & 0xf)
906 
907 /* Macro for constructing st_info from field values. */
908 #define ELF64_ST_INFO(bind, type)	(((bind) << 4) + ((type) & 0xf))
909 
910 /* Macro for accessing the fields of st_other. */
911 #define ELF64_ST_VISIBILITY(oth)	((oth) & 0x3)
912 
913 /*
914  * Go linker interface
915  */
916 
917 #define	ELF64HDRSIZE	64
918 #define	ELF64PHDRSIZE	56
919 #define	ELF64SHDRSIZE	64
920 #define	ELF64RELSIZE	16
921 #define	ELF64RELASIZE	24
922 #define	ELF64SYMSIZE	sizeof(Elf64_Sym)
923 
924 #define	ELF32HDRSIZE	sizeof(Elf32_Ehdr)
925 #define	ELF32PHDRSIZE	sizeof(Elf32_Phdr)
926 #define	ELF32SHDRSIZE	sizeof(Elf32_Shdr)
927 #define	ELF32SYMSIZE	sizeof(Elf32_Sym)
928 #define	ELF32RELSIZE	8
929 
930 #endif /* __INCLUDE_ELF_DEFS_H__ */
931