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