xref: /haiku/headers/build/os/kernel/elf.h (revision 02354704729d38c3b078c696adc1bbbd33cbcf72)
1 /*
2  * Copyright 2002-2016 Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _ELF_H
6 #define _ELF_H
7 
8 
9 #include <SupportDefs.h>
10 #include <ByteOrder.h>
11 
12 
13 typedef uint32 Elf32_Addr;
14 typedef uint16 Elf32_Half;
15 typedef uint32 Elf32_Off;
16 typedef int32 Elf32_Sword;
17 typedef uint32 Elf32_Word;
18 
19 typedef Elf32_Half Elf32_Versym;
20 
21 typedef uint64 Elf64_Addr;
22 typedef uint64 Elf64_Off;
23 typedef uint16 Elf64_Half;
24 typedef uint32 Elf64_Word;
25 typedef int32 Elf64_Sword;
26 typedef uint64 Elf64_Xword;
27 typedef int64 Elf64_Sxword;
28 
29 typedef Elf64_Half Elf64_Versym;
30 
31 
32 /*** ELF header ***/
33 
34 #define EI_NIDENT	16
35 
36 typedef struct {
37 	uint8		e_ident[EI_NIDENT];
38 	Elf32_Half	e_type;
39 	Elf32_Half	e_machine;
40 	Elf32_Word	e_version;
41 	Elf32_Addr	e_entry;
42 	Elf32_Off	e_phoff;
43 	Elf32_Off	e_shoff;
44 	Elf32_Word	e_flags;
45 	Elf32_Half	e_ehsize;
46 	Elf32_Half	e_phentsize;
47 	Elf32_Half	e_phnum;
48 	Elf32_Half	e_shentsize;
49 	Elf32_Half	e_shnum;
50 	Elf32_Half	e_shstrndx;
51 
52 #ifdef __cplusplus
53 	bool IsHostEndian() const;
54 #endif
55 } Elf32_Ehdr;
56 
57 typedef struct {
58 	uint8		e_ident[EI_NIDENT];
59 	Elf64_Half	e_type;
60 	Elf64_Half	e_machine;
61 	Elf64_Word	e_version;
62 	Elf64_Addr	e_entry;
63 	Elf64_Off	e_phoff;
64 	Elf64_Off	e_shoff;
65 	Elf64_Word	e_flags;
66 	Elf64_Half	e_ehsize;
67 	Elf64_Half	e_phentsize;
68 	Elf64_Half	e_phnum;
69 	Elf64_Half	e_shentsize;
70 	Elf64_Half	e_shnum;
71 	Elf64_Half	e_shstrndx;
72 
73 #ifdef __cplusplus
74 	bool IsHostEndian() const;
75 #endif
76 } Elf64_Ehdr;
77 
78 /* e_ident[] indices */
79 #define EI_MAG0		0
80 #define EI_MAG1		1
81 #define EI_MAG2		2
82 #define EI_MAG3		3
83 #define EI_CLASS	4
84 #define EI_DATA		5
85 #define EI_VERSION	6
86 #define EI_PAD		7
87 
88 /* Values for the magic number bytes. */
89 #define ELFMAG0		0x7f
90 #define ELFMAG1		'E'
91 #define ELFMAG2		'L'
92 #define ELFMAG3		'F'
93 #define ELFMAG		"\x7f""ELF"
94 #define SELFMAG		4
95 
96 /* e_ident */
97 #define IS_ELF(ehdr)    ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
98 	(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
99 	(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
100 	(ehdr).e_ident[EI_MAG3] == ELFMAG3)
101 
102 /* e_type (Object file type) */
103 #define ET_NONE			0 /* No file type */
104 #define ET_REL			1 /* Relocatable file */
105 #define ET_EXEC			2 /* Executable file */
106 #define ET_DYN			3 /* Shared-object file */
107 #define ET_CORE			4 /* Core file */
108 #define ET_LOOS			0xfe00 /* OS-specific range start */
109 #define ET_HIOS			0xfeff /* OS-specific range end */
110 #define ET_LOPROC		0xff00 /* Processor-specific range start */
111 #define ET_HIPROC		0xffff /* Processor-specific range end */
112 
113 /* e_machine (Architecture) */
114 #define EM_NONE			0 /* No machine */
115 #define EM_M32			1 /* AT&T WE 32100 */
116 #define EM_SPARC		2 /* Sparc */
117 #define EM_386			3 /* Intel 80386 */
118 #define EM_68K			4 /* Motorola m68k family */
119 #define EM_88K			5 /* Motorola m88k family */
120 #define EM_486			6 /* Intel 80486, Reserved for future use */
121 #define EM_860			7 /* Intel 80860 */
122 #define EM_MIPS			8 /* MIPS R3000 (officially, big-endian only) */
123 #define EM_S370			9 /* IBM System/370 */
124 #define EM_MIPS_RS3_LE	10 /* MIPS R3000 little-endian, Deprecated */
125 #define EM_PARISC		15 /* HPPA */
126 #define EM_VPP550		17 /* Fujitsu VPP500 */
127 #define EM_SPARC32PLUS	18 /* Sun "v8plus" */
128 #define EM_960			19 /* Intel 80960 */
129 #define EM_PPC			20 /* PowerPC */
130 #define EM_PPC64		21 /* 64-bit PowerPC */
131 #define EM_S390			22 /* IBM S/390 */
132 #define EM_V800			36 /* NEC V800 series */
133 #define EM_FR20			37 /* Fujitsu FR20 */
134 #define EM_RH32			38 /* TRW RH32 */
135 #define EM_MCORE		39 /* Motorola M*Core */
136 #define EM_RCE			39 /* Old name for MCore */
137 #define EM_ARM			40 /* ARM */
138 #define EM_OLD_ALPHA	41 /* Digital Alpha */
139 #define EM_SH			42 /* Renesas / SuperH SH */
140 #define EM_SPARCV9		43 /* SPARC v9 64-bit */
141 #define EM_TRICORE		44 /* Siemens Tricore embedded processor */
142 #define EM_ARC			45 /* ARC Cores */
143 #define EM_H8_300		46 /* Renesas H8/300 */
144 #define EM_H8_300H		47 /* Renesas H8/300H */
145 #define EM_H8S			48 /* Renesas H8S */
146 #define EM_H8_500		49 /* Renesas H8/500 */
147 #define EM_IA_64		50 /* Intel IA-64 Processor */
148 #define EM_MIPS_X		51 /* Stanford MIPS-X */
149 #define EM_COLDFIRE		52 /* Motorola Coldfire */
150 #define EM_68HC12		53 /* Motorola M68HC12 */
151 #define EM_MMA			54 /* Fujitsu Multimedia Accelerator */
152 #define EM_PCP			55 /* Siemens PCP */
153 #define EM_NCPU			56 /* Sony nCPU embedded RISC processor */
154 #define EM_NDR1			57 /* Denso NDR1 microprocesspr */
155 #define EM_STARCORE		58 /* Motorola Star*Core processor */
156 #define EM_ME16			59 /* Toyota ME16 processor */
157 #define EM_ST100		60 /* STMicroelectronics ST100 processor */
158 #define EM_TINYJ		61 /* Advanced Logic Corp. TinyJ embedded processor */
159 #define EM_X86_64		62 /* Advanced Micro Devices X86-64 processor */
160 #define EM_PDSP			63 /* Sony DSP Processor */
161 #define EM_FX66			66 /* Siemens FX66 microcontroller */
162 #define EM_ST9PLUS		67 /* STMicroelectronics ST9+ 8/16 mc */
163 #define EM_ST7			68 /* STmicroelectronics ST7 8 bit mc */
164 #define EM_68HC16		69 /* Motorola MC68HC16 microcontroller */
165 #define EM_68HC11		70 /* Motorola MC68HC11 microcontroller */
166 #define EM_68HC08		71 /* Motorola MC68HC08 microcontroller */
167 #define EM_68HC05		72 /* Motorola MC68HC05 microcontroller */
168 #define EM_SVX			73 /* Silicon Graphics SVx */
169 #define EM_ST19			74 /* STMicroelectronics ST19 8 bit mc */
170 #define EM_VAX			75 /* Digital VAX */
171 #define EM_CRIS			76 /* Axis Communications 32-bit embedded processor */
172 #define EM_JAVELIN		77 /* Infineon Technologies 32-bit embedded processor */
173 #define EM_FIREPATH		78 /* Element 14 64-bit DSP Processor */
174 #define EM_ZSP			79 /* LSI Logic 16-bit DSP Processor */
175 #define EM_MMIX			80 /* Donald Knuth's educational 64-bit processor */
176 #define EM_HUANY		81 /* Harvard University machine-independent object files */
177 #define EM_PRISM		82 /* SiTera Prism */
178 #define EM_AVR			83 /* Atmel AVR 8-bit microcontroller */
179 #define EM_FR30			84 /* Fujitsu FR30 */
180 #define EM_D10V			85 /* Mitsubishi D10V */
181 #define EM_D30V			86 /* Mitsubishi D30V */
182 #define EM_V850			87 /* NEC v850 */
183 #define EM_M32R			88 /* Mitsubishi M32R */
184 #define EM_MN10300		89 /* Matsushita MN10300 */
185 #define EM_MN10200		90 /* Matsushita MN10200 */
186 #define EM_PJ			91 /* picoJava */
187 #define EM_OPENRISC		92 /* OpenRISC 32-bit embedded processor */
188 #define EM_ARC_A5		93 /* ARC Cores Tangent-A5 */
189 #define EM_XTENSA		94 /* Tensilica Xtensa Architecture */
190 #define EM_VIDCORE		95 /* Alphamosaic VideoCore */
191 #define EM_CR			103 /* Nat. Semi. CompactRISC microprocessor */
192 #define EM_BLACKFIN		106 /* Analog Devices Blackfin (DSP) processor */
193 #define EM_ARCA			109 /* Arca RISC Microprocessor */
194 #define EM_VIDCORE3		137 /* Broadcom VideoCore III */
195 #define EM_ARM64		183 /* ARM 64 bit */
196 #define EM_AARCH64		EM_ARM64
197 #define EM_AVR32		185 /* AVR-32 */
198 #define EM_STM8			186 /* ST STM8S */
199 #define EM_CUDA			190 /* Nvidia CUDA */
200 #define EM_VIDCORE5		198 /* Broadcom VideoCore V */
201 #define EM_NORC			218 /* Nanoradio Optimized RISC */
202 #define EM_AMDGPU		224 /* AMD GPU */
203 #define EM_RISCV		243 /* RISC-V */
204 #define EM_BPF			247 /* Linux kernel bpf virtual machine */
205 
206 #define EM_ALPHA        0x9026 /* Digital Alpha (nonstandard, but the value
207 								  everyone uses) */
208 
209 /* architecture class (EI_CLASS) */
210 #define ELFCLASSNONE	0
211 #define ELFCLASS32		1
212 #define ELFCLASS64		2
213 
214 /* endian (EI_DATA) */
215 #define ELFDATANONE	0	/* invalid */
216 #define ELFDATA2LSB	1	/* little endian */
217 #define ELFDATA2MSB	2	/* big endian */
218 
219 /* ELF version (EI_VERSION) */
220 #define EV_NONE		0	/* invalid */
221 #define EV_CURRENT	1	/* current version */
222 
223 /*** section header ***/
224 
225 typedef struct {
226 	Elf32_Word	sh_name;
227 	Elf32_Word	sh_type;
228 	Elf32_Word	sh_flags;
229 	Elf32_Addr	sh_addr;
230 	Elf32_Off	sh_offset;
231 	Elf32_Word	sh_size;
232 	Elf32_Word	sh_link;
233 	Elf32_Word	sh_info;
234 	Elf32_Word	sh_addralign;
235 	Elf32_Word	sh_entsize;
236 } Elf32_Shdr;
237 
238 typedef struct {
239 	Elf64_Word	sh_name;
240 	Elf64_Word	sh_type;
241 	Elf64_Xword	sh_flags;
242 	Elf64_Addr	sh_addr;
243 	Elf64_Off	sh_offset;
244 	Elf64_Xword	sh_size;
245 	Elf64_Word	sh_link;
246 	Elf64_Word	sh_info;
247 	Elf64_Xword	sh_addralign;
248 	Elf64_Xword	sh_entsize;
249 } Elf64_Shdr;
250 
251 /* special section indices */
252 #define SHN_UNDEF		0
253 #define SHN_LORESERVE	0xff00
254 #define SHN_LOPROC		0xff00
255 #define SHN_HIPROC		0xff1f
256 #define SHN_ABS			0xfff1
257 #define SHN_COMMON		0xfff2
258 #define SHN_HIRESERVE	0xffff
259 
260 /* section header type */
261 #define SHT_NULL		0
262 #define SHT_PROGBITS	1
263 #define SHT_SYMTAB		2
264 #define SHT_STRTAB		3
265 #define SHT_RELA		4
266 #define SHT_HASH		5
267 #define SHT_DYNAMIC		6
268 #define SHT_NOTE		7
269 #define SHT_NOBITS		8
270 #define SHT_REL			9
271 #define SHT_SHLIB		10
272 #define SHT_DYNSYM		11
273 #define SHT_INIT_ARRAY	14
274 #define SHT_FINI_ARRAY	15
275 
276 #define SHT_GNU_verdef	0x6ffffffd    /* version definition section */
277 #define SHT_GNU_verneed	0x6ffffffe    /* version needs section */
278 #define SHT_GNU_versym	0x6fffffff    /* version symbol table */
279 
280 #define SHT_LOPROC		0x70000000
281 #define SHT_HIPROC		0x7fffffff
282 #define SHT_LOUSER		0x80000000
283 #define SHT_HIUSER		0xffffffff
284 
285 /* section header flags */
286 #define SHF_WRITE		1
287 #define SHF_ALLOC		2
288 #define SHF_EXECINSTR	4
289 
290 #define SHF_MASKPROC	0xf0000000
291 
292 
293 /*** program header ***/
294 
295 typedef struct {
296 	Elf32_Word	p_type;
297 	Elf32_Off	p_offset;	/* offset from the beginning of the file of the segment */
298 	Elf32_Addr	p_vaddr;	/* virtual address for the segment in memory */
299 	Elf32_Addr	p_paddr;
300 	Elf32_Word	p_filesz;	/* the size of the segment in the file */
301 	Elf32_Word	p_memsz;	/* the size of the segment in memory */
302 	Elf32_Word	p_flags;
303 	Elf32_Word	p_align;
304 
305 #ifdef __cplusplus
306 	bool IsReadWrite() const;
307 	bool IsExecutable() const;
308 #endif
309 } Elf32_Phdr;
310 
311 typedef struct {
312 	Elf64_Word	p_type;
313 	Elf64_Word	p_flags;
314 	Elf64_Off	p_offset;	/* offset from the beginning of the file of the segment */
315 	Elf64_Addr	p_vaddr;	/* virtual address for the segment in memory */
316 	Elf64_Addr	p_paddr;
317 	Elf64_Xword	p_filesz;	/* the size of the segment in the file */
318 	Elf64_Xword	p_memsz;	/* the size of the segment in memory */
319 	Elf64_Xword	p_align;
320 
321 #ifdef __cplusplus
322 	bool IsReadWrite() const;
323 	bool IsExecutable() const;
324 #endif
325 } Elf64_Phdr;
326 
327 /* program header segment types */
328 #define PT_NULL			0
329 #define PT_LOAD			1
330 #define PT_DYNAMIC		2
331 #define PT_INTERP		3
332 #define PT_NOTE			4
333 #define PT_SHLIB		5
334 #define PT_PHDR			6
335 #define PT_TLS			7
336 #define PT_EH_FRAME		0x6474e550
337 #define PT_STACK		0x6474e551
338 #define PT_RELRO		0x6474e552
339 
340 #define PT_LOPROC		0x70000000
341 #define PT_ARM_UNWIND	0x70000001
342 #define PT_HIPROC		0x7fffffff
343 
344 /* program header segment flags */
345 #define PF_EXECUTE	0x1
346 #define PF_WRITE	0x2
347 #define PF_READ		0x4
348 #define PF_PROTECTION_MASK (PF_EXECUTE | PF_WRITE | PF_READ)
349 
350 #define PF_MASKPROC	0xf0000000
351 
352 
353 /* symbol table entry */
354 
355 typedef struct {
356 	Elf32_Word	st_name;
357 	Elf32_Addr	st_value;
358 	Elf32_Word	st_size;
359 	uint8		st_info;
360 	uint8 		st_other;
361 	Elf32_Half	st_shndx;
362 
363 #ifdef __cplusplus
364 	uint8 Bind() const;
365 	uint8 Type() const;
366 	void SetInfo(uint8 bind, uint8 type);
367 #endif
368 } Elf32_Sym;
369 
370 typedef struct {
371 	Elf64_Word	st_name;
372 	uint8		st_info;
373 	uint8		st_other;
374 	Elf64_Half	st_shndx;
375 	Elf64_Addr	st_value;
376 	Elf64_Xword	st_size;
377 
378 #ifdef __cplusplus
379 	uint8 Bind() const;
380 	uint8 Type() const;
381 	void SetInfo(uint8 bind, uint8 type);
382 #endif
383 } Elf64_Sym;
384 
385 #define ELF32_ST_BIND(i) ((i) >> 4)
386 #define ELF32_ST_TYPE(i) ((i) & 0xf)
387 #define ELF32_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf))
388 
389 #define ELF64_ST_BIND(i) ((i) >> 4)
390 #define ELF64_ST_TYPE(i) ((i) & 0xf)
391 #define ELF64_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf))
392 
393 /* symbol types */
394 #define STT_NOTYPE 0
395 #define STT_OBJECT 1
396 #define STT_FUNC 2
397 #define STT_SECTION 3
398 #define STT_FILE 4
399 #define STT_TLS		6
400 #define STT_LOPROC 13
401 #define STT_HIPROC 15
402 
403 /* symbol binding */
404 #define STB_LOCAL 0
405 #define STB_GLOBAL 1
406 #define STB_WEAK 2
407 #define STB_LOPROC 13
408 #define STB_HIPROC 15
409 
410 /* special symbol indices */
411 #define STN_UNDEF 0
412 
413 /* relocation types */
414 
415 #define R_386_NONE		0
416 #define R_386_32		1	/* add symbol value */
417 #define R_386_PC32		2	/* add PC relative symbol value */
418 #define R_386_GOT32		3	/* add PC relative GOT offset */
419 #define R_386_PLT32		4	/* add PC relative PLT offset */
420 #define R_386_COPY		5	/* copy data from shared object */
421 #define R_386_GLOB_DAT	6	/* set GOT entry to data address */
422 #define R_386_JMP_SLOT	7	/* set GOT entry to code address */
423 #define R_386_RELATIVE	8	/* add load address of shared object */
424 #define R_386_GOTOFF	9	/* add GOT relative symbol address */
425 #define R_386_GOTPC		10	/* add PC relative GOT table address */
426 #define R_386_TLS_DTPMOD32	35
427 #define R_386_TLS_DTPOFF32	36
428 
429 
430 /* relocation table entry */
431 
432 typedef struct {
433 	Elf32_Addr r_offset;
434 	Elf32_Word r_info;
435 
436 #ifdef __cplusplus
437 	uint8 SymbolIndex() const;
438 	uint8 Type() const;
439 #endif
440 } Elf32_Rel;
441 
442 typedef struct {
443 	Elf64_Addr	r_offset;
444 	Elf64_Xword	r_info;
445 
446 #ifdef __cplusplus
447 	uint8 SymbolIndex() const;
448 	uint8 Type() const;
449 #endif
450 } Elf64_Rel;
451 
452 #ifdef __cplusplus
453 typedef struct Elf32_Rela : public Elf32_Rel {
454 #else
455 typedef struct {
456 	Elf32_Addr r_offset;
457 	Elf32_Word r_info;
458 #endif
459 	Elf32_Sword r_addend;
460 } Elf32_Rela;
461 
462 #ifdef __cplusplus
463 typedef struct Elf64_Rela : public Elf64_Rel {
464 #else
465 typedef struct {
466 	Elf64_Addr		r_offset;
467 	Elf64_Xword		r_info;
468 #endif
469 	Elf64_Sxword	r_addend;
470 } Elf64_Rela;
471 
472 #define ELF32_R_SYM(i) ((i) >> 8)
473 #define ELF32_R_TYPE(i) ((unsigned char)(i))
474 #define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t))
475 
476 #define ELF64_R_SYM(i) ((i) >> 32)
477 #define ELF64_R_TYPE(i) ((i) & 0xffffffffL)
478 #define ELF64_R_INFO(s, t) ((((Elf64_Xword)(s)) << 32) + ((t) & 0xffffffffL))
479 
480 
481 /* dynamic section entry */
482 
483 typedef struct {
484 	Elf32_Sword d_tag;
485 	union {
486 		Elf32_Word d_val;
487 		Elf32_Addr d_ptr;
488 	} d_un;
489 } Elf32_Dyn;
490 
491 typedef struct {
492 	Elf64_Sxword d_tag;
493 	union {
494 		Elf64_Xword	d_val;
495 		Elf64_Addr	d_ptr;
496 	} d_un;
497 } Elf64_Dyn;
498 
499 /* dynamic entry type */
500 #define DT_NULL				0
501 #define DT_NEEDED			1
502 #define DT_PLTRELSZ			2
503 #define DT_PLTGOT			3
504 #define DT_HASH				4
505 #define DT_STRTAB			5
506 #define DT_SYMTAB			6
507 #define DT_RELA				7
508 #define DT_RELASZ			8
509 #define DT_RELAENT			9
510 #define DT_STRSZ			10
511 #define DT_SYMENT			11
512 #define DT_INIT				12
513 #define DT_FINI				13
514 #define DT_SONAME			14
515 #define DT_RPATH			15
516 #define DT_SYMBOLIC			16
517 #define DT_REL				17
518 #define DT_RELSZ			18
519 #define DT_RELENT			19
520 #define DT_PLTREL			20
521 #define DT_DEBUG			21
522 #define DT_TEXTREL			22
523 #define DT_JMPREL			23
524 #define DT_BIND_NOW			24	/* no lazy binding */
525 #define DT_INIT_ARRAY		25	/* init function array */
526 #define DT_FINI_ARRAY		26	/* termination function array */
527 #define DT_INIT_ARRAYSZ		27	/* init function array size */
528 #define DT_FINI_ARRAYSZ		28	/* termination function array size */
529 #define DT_RUNPATH			29	/* library search path (supersedes DT_RPATH) */
530 #define DT_FLAGS			30	/* flags (see below) */
531 #define DT_ENCODING			32
532 #define DT_PREINIT_ARRAY	32	/* preinitialization array */
533 #define DT_PREINIT_ARRAYSZ	33	/* preinitialization array size */
534 
535 #define DT_VERSYM       0x6ffffff0	/* symbol version table */
536 #define DT_VERDEF		0x6ffffffc	/* version definition table */
537 #define DT_VERDEFNUM	0x6ffffffd	/* number of version definitions */
538 #define DT_VERNEED		0x6ffffffe 	/* table with needed versions */
539 #define DT_VERNEEDNUM	0x6fffffff	/* number of needed versions */
540 
541 #define DT_LOPROC		0x70000000
542 #define DT_HIPROC		0x7fffffff
543 
544 /* DT_FLAGS values */
545 #define DF_ORIGIN		0x01
546 #define DF_SYMBOLIC		0x02
547 #define DF_TEXTREL		0x04
548 #define DF_BIND_NOW		0x08
549 #define DF_STATIC_TLS	0x10
550 
551 
552 /* version definition section */
553 
554 typedef struct {
555 	Elf32_Half	vd_version;		/* version revision */
556 	Elf32_Half	vd_flags;		/* version information flags */
557 	Elf32_Half	vd_ndx;			/* version index as specified in the
558 								   symbol version table */
559 	Elf32_Half	vd_cnt;			/* number of associated verdaux entries */
560 	Elf32_Word	vd_hash;		/* version name hash value */
561 	Elf32_Word	vd_aux;			/* byte offset to verdaux array */
562 	Elf32_Word	vd_next;		/* byte offset to next verdef entry */
563 } Elf32_Verdef;
564 
565 typedef struct {
566 	Elf64_Half	vd_version;		/* version revision */
567 	Elf64_Half	vd_flags;		/* version information flags */
568 	Elf64_Half	vd_ndx;			/* version index as specified in the
569 								   symbol version table */
570 	Elf64_Half	vd_cnt;			/* number of associated verdaux entries */
571 	Elf64_Word	vd_hash;		/* version name hash value */
572 	Elf64_Word	vd_aux;			/* byte offset to verdaux array */
573 	Elf64_Word	vd_next;		/* byte offset to next verdef entry */
574 } Elf64_Verdef;
575 
576 /* values for vd_version (version revision) */
577 #define VER_DEF_NONE		0		/* no version */
578 #define VER_DEF_CURRENT		1		/* current version */
579 #define VER_DEF_NUM			2		/* given version number */
580 
581 /* values for vd_flags (version information flags) */
582 #define VER_FLG_BASE		0x1		/* version definition of file itself */
583 #define VER_FLG_WEAK		0x2 	/* weak version identifier */
584 
585 /* values for versym symbol index */
586 #define VER_NDX_LOCAL		0		/* symbol is local */
587 #define VER_NDX_GLOBAL		1		/* symbol is global/unversioned */
588 #define VER_NDX_INITIAL		2		/* initial version -- that's the one given
589 									   to symbols when a library becomes
590 									   versioned; handled by the linker (and
591 									   runtime loader) similar to
592 									   VER_NDX_GLOBAL */
593 #define VER_NDX_LORESERVE	0xff00	/* beginning of reserved entries */
594 #define VER_NDX_ELIMINATE	0xff01	/* symbol is to be eliminated */
595 
596 #define VER_NDX_FLAG_HIDDEN	0x8000	/* flag: version is hidden */
597 #define VER_NDX_MASK		0x7fff	/* mask to get the actual version index */
598 #define VER_NDX(x)			((x) & VER_NDX_MASK)
599 
600 
601 /* auxiliary version information */
602 
603 typedef struct {
604 	Elf32_Word	vda_name;		/* string table offset to version or dependency
605 								   name */
606 	Elf32_Word	vda_next;		/* byte offset to next verdaux entry */
607 } Elf32_Verdaux;
608 
609 typedef struct {
610 	Elf64_Word	vda_name;		/* string table offset to version or dependency
611 								   name */
612 	Elf64_Word	vda_next;		/* byte offset to next verdaux entry */
613 } Elf64_Verdaux;
614 
615 
616 /* version dependency section */
617 
618 typedef struct {
619 	Elf32_Half	vn_version;		/* version of structure */
620 	Elf32_Half	vn_cnt;			/* number of associated vernaux entries */
621 	Elf32_Word	vn_file;		/* byte offset to file name for this
622 								   dependency */
623 	Elf32_Word	vn_aux;			/* byte offset to vernaux array */
624 	Elf32_Word	vn_next;		/* byte offset to next verneed entry */
625 } Elf32_Verneed;
626 
627 typedef struct {
628 	Elf64_Half	vn_version;		/* version of structure */
629 	Elf64_Half	vn_cnt;			/* number of associated vernaux entries */
630 	Elf64_Word	vn_file;		/* byte offset to file name for this
631 								   dependency */
632 	Elf64_Word	vn_aux;			/* byte offset to vernaux array */
633 	Elf64_Word	vn_next;		/* byte offset to next verneed entry */
634 } Elf64_Verneed;
635 
636 /* values for vn_version (version revision) */
637 #define VER_NEED_NONE		0	/* no version */
638 #define VER_NEED_CURRENT	1	/* current version */
639 #define VER_NEED_NUM		2	/* given version number */
640 
641 
642 /* auxiliary needed version information */
643 
644 typedef struct {
645 	Elf32_Word	vna_hash;		/* dependency name hash value */
646 	Elf32_Half	vna_flags;		/* dependency specific information flags */
647 	Elf32_Half	vna_other;		/* version index as specified in the symbol
648 								   version table */
649 	Elf32_Word	vna_name;		/* string table offset to dependency name */
650 	Elf32_Word	vna_next;		/* byte offset to next vernaux entry */
651 } Elf32_Vernaux;
652 
653 typedef struct {
654 	Elf64_Word	vna_hash;		/* dependency name hash value */
655 	Elf64_Half	vna_flags;		/* dependency specific information flags */
656 	Elf64_Half	vna_other;		/* version index as specified in the symbol
657 								   version table */
658 	Elf64_Word	vna_name;		/* string table offset to dependency name */
659 	Elf64_Word	vna_next;		/* byte offset to next vernaux entry */
660 } Elf64_Vernaux;
661 
662 /* values for vna_flags */
663 #define VER_FLG_WEAK	0x2		/* weak version identifier */
664 
665 
666 /*** core files ***/
667 
668 /* note section header */
669 
670 typedef struct {
671 	Elf32_Word n_namesz;		/* length of the note's name */
672 	Elf32_Word n_descsz;		/* length of the note's descriptor */
673 	Elf32_Word n_type;			/* note type */
674 } Elf32_Nhdr;
675 
676 typedef struct {
677 	Elf64_Word n_namesz;		/* length of the note's name */
678 	Elf64_Word n_descsz;		/* length of the note's descriptor */
679 	Elf64_Word n_type;			/* note type */
680 } Elf64_Nhdr;
681 
682 /* values for note name */
683 #define ELF_NOTE_CORE		"CORE"
684 #define ELF_NOTE_HAIKU		"Haiku"
685 
686 /* values for note type (n_type) */
687 /* ELF_NOTE_CORE/... */
688 #define NT_FILE				0x46494c45 /* mapped files */
689 
690 /* ELF_NOTE_HAIKU/... */
691 #define NT_TEAM				0x7465616d 	/* team */
692 #define NT_AREAS			0x61726561 	/* areas */
693 #define NT_IMAGES			0x696d6167 	/* images */
694 #define NT_THREADS			0x74687264 	/* threads */
695 #define NT_SYMBOLS			0x73796d73 	/* symbols */
696 
697 /* NT_TEAM: uint32 entrySize; Elf32_Note_Team; char[] args */
698 typedef struct {
699 	int32		nt_id;				/* team ID */
700 	int32		nt_uid;				/* team owner ID */
701 	int32		nt_gid;				/* team group ID */
702 } Elf32_Note_Team;
703 
704 typedef Elf32_Note_Team Elf64_Note_Team;
705 
706 /* NT_AREAS:
707  * uint32 count;
708  * uint32 entrySize;
709  * Elf32_Note_Area_Entry[count];
710  * char[] names
711  */
712 typedef struct {
713 	int32		na_id;				/* area ID */
714 	uint32		na_lock;			/* lock type (B_NO_LOCK, ...) */
715 	uint32		na_protection;		/* protection (B_READ_AREA | ...) */
716 	uint32		na_base;			/* area base address */
717 	uint32		na_size;			/* area size */
718 	uint32		na_ram_size;		/* physical memory used */
719 } Elf32_Note_Area_Entry;
720 
721 /* NT_AREAS:
722  * uint32 count;
723  * uint32 entrySize;
724  * Elf64_Note_Area_Entry[count];
725  * char[] names
726  */
727 typedef struct {
728 	int32		na_id;				/* area ID */
729 	uint32		na_lock;			/* lock type (B_NO_LOCK, ...) */
730 	uint32		na_protection;		/* protection (B_READ_AREA | ...) */
731 	uint32		na_pad1;
732 	uint64		na_base;			/* area base address */
733 	uint64		na_size;			/* area size */
734 	uint64		na_ram_size;		/* physical memory used */
735 } Elf64_Note_Area_Entry;
736 
737 /* NT_IMAGES:
738  * uint32 count;
739  * uint32 entrySize;
740  * Elf32_Note_Image_Entry[count];
741  * char[] names
742  */
743 typedef struct {
744 	int32		ni_id;				/* image ID */
745 	int32		ni_type;			/* image type (B_APP_IMAGE, ...) */
746 	uint32		ni_init_routine;	/* address of init function */
747 	uint32		ni_term_routine;	/* address of termination function */
748 	int32		ni_device;			/* device ID of mapped file */
749 	int64		ni_node;			/* node ID of mapped file */
750 	uint32		ni_text_base;		/* base address of text segment */
751 	uint32		ni_text_size;		/* size of text segment */
752 	int32		ni_text_delta;		/* delta of the text segment relative to
753 									   load address specified in the ELF file */
754 	uint32		ni_data_base;		/* base address of data segment */
755 	uint32		ni_data_size;		/* size of data segment */
756 	uint32		ni_symbol_table;	/* address of dynamic symbol table */
757 	uint32		ni_symbol_hash;		/* address of dynamic symbol hash */
758 	uint32		ni_string_table;	/* address of dynamic string table */
759 } Elf32_Note_Image_Entry;
760 
761 /* NT_IMAGES:
762  * uint32 count;
763  * uint32 entrySize;
764  * Elf64_Note_Image_Entry[count];
765  * char[] names
766  */
767 typedef struct {
768 	int32		ni_id;				/* image ID */
769 	int32		ni_type;			/* image type (B_APP_IMAGE, ...) */
770 	uint64		ni_init_routine;	/* address of init function */
771 	uint64		ni_term_routine;	/* address of termination function */
772 	uint32		ni_pad1;
773 	int32		ni_device;			/* device ID of mapped file */
774 	int64		ni_node;			/* node ID of mapped file */
775 	uint64		ni_text_base;		/* base address of text segment */
776 	uint64		ni_text_size;		/* size of text segment */
777 	int64		ni_text_delta;		/* delta of the text segment relative to
778 									   load address specified in the ELF file */
779 	uint64		ni_data_base;		/* base address of data segment */
780 	uint64		ni_data_size;		/* size of data segment */
781 	uint64		ni_symbol_table;	/* address of dynamic symbol table */
782 	uint64		ni_symbol_hash;		/* address of dynamic symbol hash */
783 	uint64		ni_string_table;	/* address of dynamic string table */
784 } Elf64_Note_Image_Entry;
785 
786 /* NT_THREADS:
787  * uint32 count;
788  * uint32 entrySize;
789  * uint32 cpuStateSize;
790  * {Elf32_Note_Thread_Entry, uint8[cpuStateSize] cpuState}[count];
791  * char[] names
792  */
793 typedef struct {
794 	int32		nth_id;				/* thread ID */
795 	int32		nth_state;			/* thread state (B_THREAD_RUNNING, ...) */
796 	int32		nth_priority;		/* thread priority */
797 	uint32		nth_stack_base;		/* thread stack base address */
798 	uint32		nth_stack_end;		/* thread stack end address */
799 } Elf32_Note_Thread_Entry;
800 
801 /* NT_THREADS:
802  * uint32 count;
803  * uint32 entrySize;
804  * uint32 cpuStateSize;
805  * {Elf64_Note_Thread_Entry, uint8[cpuStateSize] cpuState}[count];
806  * char[] names
807  */
808 typedef struct {
809 	int32		nth_id;				/* thread ID */
810 	int32		nth_state;			/* thread state (B_THREAD_RUNNING, ...) */
811 	int32		nth_priority;		/* thread priority */
812 	uint32		nth_pad1;
813 	uint64		nth_stack_base;		/* thread stack base address */
814 	uint64		nth_stack_end;		/* thread stack end address */
815 } Elf64_Note_Thread_Entry;
816 
817 /* NT_SYMBOLS:
818  * int32 imageId;
819  * uint32 symbolCount;
820  * uint32 entrySize;
821  * Elf{32,64}_Sym[count];
822  * char[] strings
823  */
824 
825 
826 /*** inline functions ***/
827 
828 #ifdef __cplusplus
829 
830 inline bool
831 Elf32_Ehdr::IsHostEndian() const
832 {
833 #if B_HOST_IS_LENDIAN
834 	return e_ident[EI_DATA] == ELFDATA2LSB;
835 #elif B_HOST_IS_BENDIAN
836 	return e_ident[EI_DATA] == ELFDATA2MSB;
837 #endif
838 }
839 
840 
841 inline bool
842 Elf64_Ehdr::IsHostEndian() const
843 {
844 #if B_HOST_IS_LENDIAN
845 	return e_ident[EI_DATA] == ELFDATA2LSB;
846 #elif B_HOST_IS_BENDIAN
847 	return e_ident[EI_DATA] == ELFDATA2MSB;
848 #endif
849 }
850 
851 
852 inline bool
853 Elf32_Phdr::IsReadWrite() const
854 {
855 	return !(~p_flags & (PF_READ | PF_WRITE));
856 }
857 
858 
859 inline bool
860 Elf32_Phdr::IsExecutable() const
861 {
862 	return (p_flags & PF_EXECUTE) != 0;
863 }
864 
865 
866 inline bool
867 Elf64_Phdr::IsReadWrite() const
868 {
869 	return !(~p_flags & (PF_READ | PF_WRITE));
870 }
871 
872 
873 inline bool
874 Elf64_Phdr::IsExecutable() const
875 {
876 	return (p_flags & PF_EXECUTE) != 0;
877 }
878 
879 
880 inline uint8
881 Elf32_Sym::Bind() const
882 {
883 	return ELF32_ST_BIND(st_info);
884 }
885 
886 
887 inline uint8
888 Elf32_Sym::Type() const
889 {
890 	return ELF32_ST_TYPE(st_info);
891 }
892 
893 
894 inline void
895 Elf32_Sym::SetInfo(uint8 bind, uint8 type)
896 {
897 	st_info = ELF32_ST_INFO(bind, type);
898 }
899 
900 
901 inline uint8
902 Elf64_Sym::Bind() const
903 {
904 	return ELF64_ST_BIND(st_info);
905 }
906 
907 
908 inline uint8
909 Elf64_Sym::Type() const
910 {
911 	return ELF64_ST_TYPE(st_info);
912 }
913 
914 
915 inline void
916 Elf64_Sym::SetInfo(uint8 bind, uint8 type)
917 {
918 	st_info = ELF64_ST_INFO(bind, type);
919 }
920 
921 
922 inline uint8
923 Elf32_Rel::SymbolIndex() const
924 {
925 	return ELF32_R_SYM(r_info);
926 }
927 
928 
929 inline uint8
930 Elf32_Rel::Type() const
931 {
932 	return ELF32_R_TYPE(r_info);
933 }
934 
935 
936 inline uint8
937 Elf64_Rel::SymbolIndex() const
938 {
939 	return ELF64_R_SYM(r_info);
940 }
941 
942 
943 inline uint8
944 Elf64_Rel::Type() const
945 {
946 	return ELF64_R_TYPE(r_info);
947 }
948 
949 #endif	/* __cplusplus */
950 
951 
952 #endif	/* _ELF_H */
953