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