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
14typedef uint32 Elf32_Addr;
15typedef uint16 Elf32_Half;
16typedef uint32 Elf32_Off;
17typedef int32 Elf32_Sword;
18typedef uint32 Elf32_Word;
19
20typedef Elf32_Half Elf32_Versym;
21
22typedef uint64 Elf64_Addr;
23typedef uint64 Elf64_Off;
24typedef uint16 Elf64_Half;
25typedef uint32 Elf64_Word;
26typedef int32 Elf64_Sword;
27typedef uint64 Elf64_Xword;
28typedef int64 Elf64_Sxword;
29
30typedef 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
47typedef 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
68typedef 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
236typedef 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
249typedef 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
285#define SHT_GNU_verdef	0x6ffffffd    /* version definition section */
286#define SHT_GNU_verneed	0x6ffffffe    /* version needs section */
287#define SHT_GNU_versym	0x6fffffff    /* version symbol table */
288
289#define SHT_LOPROC		0x70000000
290#define SHT_HIPROC		0x7fffffff
291#define SHT_LOUSER		0x80000000
292#define SHT_HIUSER		0xffffffff
293
294/* section header flags */
295#define SHF_WRITE		1
296#define SHF_ALLOC		2
297#define SHF_EXECINSTR	4
298
299#define SHF_MASKPROC	0xf0000000
300
301
302/*** program header ***/
303
304typedef struct {
305	Elf32_Word	p_type;
306	Elf32_Off	p_offset;	/* offset from the beginning of the file of the segment */
307	Elf32_Addr	p_vaddr;	/* virtual address for the segment in memory */
308	Elf32_Addr	p_paddr;
309	Elf32_Word	p_filesz;	/* the size of the segment in the file */
310	Elf32_Word	p_memsz;	/* the size of the segment in memory */
311	Elf32_Word	p_flags;
312	Elf32_Word	p_align;
313
314#ifdef __cplusplus
315	bool IsReadWrite() const;
316	bool IsExecutable() const;
317#endif
318} Elf32_Phdr;
319
320typedef struct {
321	Elf64_Word	p_type;
322	Elf64_Word	p_flags;
323	Elf64_Off	p_offset;	/* offset from the beginning of the file of the segment */
324	Elf64_Addr	p_vaddr;	/* virtual address for the segment in memory */
325	Elf64_Addr	p_paddr;
326	Elf64_Xword	p_filesz;	/* the size of the segment in the file */
327	Elf64_Xword	p_memsz;	/* the size of the segment in memory */
328	Elf64_Xword	p_align;
329
330#ifdef __cplusplus
331	bool IsReadWrite() const;
332	bool IsExecutable() const;
333#endif
334} Elf64_Phdr;
335
336/* program header segment types */
337#define PT_NULL			0
338#define PT_LOAD			1
339#define PT_DYNAMIC		2
340#define PT_INTERP		3
341#define PT_NOTE			4
342#define PT_SHLIB		5
343#define PT_PHDR			6
344#define PT_TLS			7
345#define PT_EH_FRAME		0x6474e550
346#define PT_STACK		0x6474e551
347#define PT_RELRO		0x6474e552
348
349#define PT_LOPROC		0x70000000
350#define PT_ARM_UNWIND	0x70000001
351#define PT_HIPROC		0x7fffffff
352
353/* program header segment flags */
354#define PF_EXECUTE	0x1
355#define PF_WRITE	0x2
356#define PF_READ		0x4
357#define PF_PROTECTION_MASK (PF_EXECUTE | PF_WRITE | PF_READ)
358
359#define PF_MASKPROC	0xf0000000
360
361
362/* symbol table entry */
363
364typedef struct {
365	Elf32_Word	st_name;
366	Elf32_Addr	st_value;
367	Elf32_Word	st_size;
368	uint8		st_info;
369	uint8 		st_other;
370	Elf32_Half	st_shndx;
371
372#ifdef __cplusplus
373	uint8 Bind() const;
374	uint8 Type() const;
375	void SetInfo(uint8 bind, uint8 type);
376#endif
377} Elf32_Sym;
378
379typedef struct {
380	Elf64_Word	st_name;
381	uint8		st_info;
382	uint8		st_other;
383	Elf64_Half	st_shndx;
384	Elf64_Addr	st_value;
385	Elf64_Xword	st_size;
386
387#ifdef __cplusplus
388	uint8 Bind() const;
389	uint8 Type() const;
390	void SetInfo(uint8 bind, uint8 type);
391#endif
392} Elf64_Sym;
393
394#define ELF32_ST_BIND(i) ((i) >> 4)
395#define ELF32_ST_TYPE(i) ((i) & 0xf)
396#define ELF32_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf))
397
398#define ELF64_ST_BIND(i) ((i) >> 4)
399#define ELF64_ST_TYPE(i) ((i) & 0xf)
400#define ELF64_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf))
401
402/* symbol types */
403#define STT_NOTYPE 0
404#define STT_OBJECT 1
405#define STT_FUNC 2
406#define STT_SECTION 3
407#define STT_FILE 4
408#define STT_TLS		6
409#define STT_LOPROC 13
410#define STT_HIPROC 15
411
412/* symbol binding */
413#define STB_LOCAL 0
414#define STB_GLOBAL 1
415#define STB_WEAK 2
416#define STB_LOPROC 13
417#define STB_HIPROC 15
418
419/* special symbol indices */
420#define STN_UNDEF 0
421
422
423/* relocation table entry */
424
425typedef struct {
426	Elf32_Addr r_offset;
427	Elf32_Word r_info;
428
429#ifdef __cplusplus
430	uint8 SymbolIndex() const;
431	uint8 Type() const;
432#endif
433} Elf32_Rel;
434
435typedef struct {
436	Elf64_Addr	r_offset;
437	Elf64_Xword	r_info;
438
439#ifdef __cplusplus
440	uint8 SymbolIndex() const;
441	uint8 Type() const;
442#endif
443} Elf64_Rel;
444
445#ifdef __cplusplus
446typedef struct Elf32_Rela : public Elf32_Rel {
447#else
448typedef struct {
449	Elf32_Addr r_offset;
450	Elf32_Word r_info;
451#endif
452	Elf32_Sword r_addend;
453} Elf32_Rela;
454
455#ifdef __cplusplus
456typedef struct Elf64_Rela : public Elf64_Rel {
457#else
458typedef struct {
459	Elf64_Addr		r_offset;
460	Elf64_Xword		r_info;
461#endif
462	Elf64_Sxword	r_addend;
463} Elf64_Rela;
464
465#define ELF32_R_SYM(i) ((i) >> 8)
466#define ELF32_R_TYPE(i) ((unsigned char)(i))
467#define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t))
468
469#define ELF64_R_SYM(i) ((i) >> 32)
470#define ELF64_R_TYPE(i) ((i) & 0xffffffffL)
471#define ELF64_R_INFO(s, t) ((((Elf64_Xword)(s)) << 32) + ((t) & 0xffffffffL))
472
473
474/* dynamic section entry */
475
476typedef struct {
477	Elf32_Sword d_tag;
478	union {
479		Elf32_Word d_val;
480		Elf32_Addr d_ptr;
481	} d_un;
482} Elf32_Dyn;
483
484typedef struct {
485	Elf64_Sxword d_tag;
486	union {
487		Elf64_Xword	d_val;
488		Elf64_Addr	d_ptr;
489	} d_un;
490} Elf64_Dyn;
491
492/* dynamic entry type */
493#define DT_NULL				0
494#define DT_NEEDED			1
495#define DT_PLTRELSZ			2
496#define DT_PLTGOT			3
497#define DT_HASH				4
498#define DT_STRTAB			5
499#define DT_SYMTAB			6
500#define DT_RELA				7
501#define DT_RELASZ			8
502#define DT_RELAENT			9
503#define DT_STRSZ			10
504#define DT_SYMENT			11
505#define DT_INIT				12
506#define DT_FINI				13
507#define DT_SONAME			14
508#define DT_RPATH			15
509#define DT_SYMBOLIC			16
510#define DT_REL				17
511#define DT_RELSZ			18
512#define DT_RELENT			19
513#define DT_PLTREL			20
514#define DT_DEBUG			21
515#define DT_TEXTREL			22
516#define DT_JMPREL			23
517#define DT_BIND_NOW			24	/* no lazy binding */
518#define DT_INIT_ARRAY		25	/* init function array */
519#define DT_FINI_ARRAY		26	/* termination function array */
520#define DT_INIT_ARRAYSZ		27	/* init function array size */
521#define DT_FINI_ARRAYSZ		28	/* termination function array size */
522#define DT_RUNPATH			29	/* library search path (supersedes DT_RPATH) */
523#define DT_FLAGS			30	/* flags (see below) */
524#define DT_ENCODING			32
525#define DT_PREINIT_ARRAY	32	/* preinitialization array */
526#define DT_PREINIT_ARRAYSZ	33	/* preinitialization array size */
527
528#define DT_VERSYM       0x6ffffff0	/* symbol version table */
529#define DT_VERDEF		0x6ffffffc	/* version definition table */
530#define DT_VERDEFNUM	0x6ffffffd	/* number of version definitions */
531#define DT_VERNEED		0x6ffffffe 	/* table with needed versions */
532#define DT_VERNEEDNUM	0x6fffffff	/* number of needed versions */
533
534#define DT_LOPROC		0x70000000
535#define DT_HIPROC		0x7fffffff
536
537/* DT_FLAGS values */
538#define DF_ORIGIN		0x01
539#define DF_SYMBOLIC		0x02
540#define DF_TEXTREL		0x04
541#define DF_BIND_NOW		0x08
542#define DF_STATIC_TLS	0x10
543
544
545/* version definition section */
546
547typedef struct {
548	Elf32_Half	vd_version;		/* version revision */
549	Elf32_Half	vd_flags;		/* version information flags */
550	Elf32_Half	vd_ndx;			/* version index as specified in the
551								   symbol version table */
552	Elf32_Half	vd_cnt;			/* number of associated verdaux entries */
553	Elf32_Word	vd_hash;		/* version name hash value */
554	Elf32_Word	vd_aux;			/* byte offset to verdaux array */
555	Elf32_Word	vd_next;		/* byte offset to next verdef entry */
556} Elf32_Verdef;
557
558typedef struct {
559	Elf64_Half	vd_version;		/* version revision */
560	Elf64_Half	vd_flags;		/* version information flags */
561	Elf64_Half	vd_ndx;			/* version index as specified in the
562								   symbol version table */
563	Elf64_Half	vd_cnt;			/* number of associated verdaux entries */
564	Elf64_Word	vd_hash;		/* version name hash value */
565	Elf64_Word	vd_aux;			/* byte offset to verdaux array */
566	Elf64_Word	vd_next;		/* byte offset to next verdef entry */
567} Elf64_Verdef;
568
569/* values for vd_version (version revision) */
570#define VER_DEF_NONE		0		/* no version */
571#define VER_DEF_CURRENT		1		/* current version */
572#define VER_DEF_NUM			2		/* given version number */
573
574/* values for vd_flags (version information flags) */
575#define VER_FLG_BASE		0x1		/* version definition of file itself */
576#define VER_FLG_WEAK		0x2 	/* weak version identifier */
577
578/* values for versym symbol index */
579#define VER_NDX_LOCAL		0		/* symbol is local */
580#define VER_NDX_GLOBAL		1		/* symbol is global/unversioned */
581#define VER_NDX_INITIAL		2		/* initial version -- that's the one given
582									   to symbols when a library becomes
583									   versioned; handled by the linker (and
584									   runtime loader) similar to
585									   VER_NDX_GLOBAL */
586#define VER_NDX_LORESERVE	0xff00	/* beginning of reserved entries */
587#define VER_NDX_ELIMINATE	0xff01	/* symbol is to be eliminated */
588
589#define VER_NDX_FLAG_HIDDEN	0x8000	/* flag: version is hidden */
590#define VER_NDX_MASK		0x7fff	/* mask to get the actual version index */
591#define VER_NDX(x)			((x) & VER_NDX_MASK)
592
593
594/* auxiliary version information */
595
596typedef struct {
597	Elf32_Word	vda_name;		/* string table offset to version or dependency
598								   name */
599	Elf32_Word	vda_next;		/* byte offset to next verdaux entry */
600} Elf32_Verdaux;
601
602typedef struct {
603	Elf64_Word	vda_name;		/* string table offset to version or dependency
604								   name */
605	Elf64_Word	vda_next;		/* byte offset to next verdaux entry */
606} Elf64_Verdaux;
607
608
609/* version dependency section */
610
611typedef struct {
612	Elf32_Half	vn_version;		/* version of structure */
613	Elf32_Half	vn_cnt;			/* number of associated vernaux entries */
614	Elf32_Word	vn_file;		/* byte offset to file name for this
615								   dependency */
616	Elf32_Word	vn_aux;			/* byte offset to vernaux array */
617	Elf32_Word	vn_next;		/* byte offset to next verneed entry */
618} Elf32_Verneed;
619
620typedef struct {
621	Elf64_Half	vn_version;		/* version of structure */
622	Elf64_Half	vn_cnt;			/* number of associated vernaux entries */
623	Elf64_Word	vn_file;		/* byte offset to file name for this
624								   dependency */
625	Elf64_Word	vn_aux;			/* byte offset to vernaux array */
626	Elf64_Word	vn_next;		/* byte offset to next verneed entry */
627} Elf64_Verneed;
628
629/* values for vn_version (version revision) */
630#define VER_NEED_NONE		0	/* no version */
631#define VER_NEED_CURRENT	1	/* current version */
632#define VER_NEED_NUM		2	/* given version number */
633
634
635/* auxiliary needed version information */
636
637typedef struct {
638	Elf32_Word	vna_hash;		/* dependency name hash value */
639	Elf32_Half	vna_flags;		/* dependency specific information flags */
640	Elf32_Half	vna_other;		/* version index as specified in the symbol
641								   version table */
642	Elf32_Word	vna_name;		/* string table offset to dependency name */
643	Elf32_Word	vna_next;		/* byte offset to next vernaux entry */
644} Elf32_Vernaux;
645
646typedef struct {
647	Elf64_Word	vna_hash;		/* dependency name hash value */
648	Elf64_Half	vna_flags;		/* dependency specific information flags */
649	Elf64_Half	vna_other;		/* version index as specified in the symbol
650								   version table */
651	Elf64_Word	vna_name;		/* string table offset to dependency name */
652	Elf64_Word	vna_next;		/* byte offset to next vernaux entry */
653} Elf64_Vernaux;
654
655/* values for vna_flags */
656#define VER_FLG_WEAK	0x2		/* weak version identifier */
657
658
659/*** core files ***/
660
661/* note section header */
662
663typedef struct {
664	Elf32_Word n_namesz;		/* length of the note's name */
665	Elf32_Word n_descsz;		/* length of the note's descriptor */
666	Elf32_Word n_type;			/* note type */
667} Elf32_Nhdr;
668
669typedef struct {
670	Elf64_Word n_namesz;		/* length of the note's name */
671	Elf64_Word n_descsz;		/* length of the note's descriptor */
672	Elf64_Word n_type;			/* note type */
673} Elf64_Nhdr;
674
675/* values for note name */
676#define ELF_NOTE_CORE		"CORE"
677#define ELF_NOTE_HAIKU		"Haiku"
678
679/* values for note type (n_type) */
680/* ELF_NOTE_CORE/... */
681#define NT_FILE				0x46494c45 /* mapped files */
682
683/* ELF_NOTE_HAIKU/... */
684#define NT_TEAM				0x7465616d 	/* team */
685#define NT_AREAS			0x61726561 	/* areas */
686#define NT_IMAGES			0x696d6167 	/* images */
687#define NT_THREADS			0x74687264 	/* threads */
688#define NT_SYMBOLS			0x73796d73 	/* symbols */
689
690/* NT_TEAM: uint32 entrySize; Elf32_Note_Team; char[] args */
691typedef struct {
692	int32		nt_id;				/* team ID */
693	int32		nt_uid;				/* team owner ID */
694	int32		nt_gid;				/* team group ID */
695} Elf32_Note_Team;
696
697typedef Elf32_Note_Team Elf64_Note_Team;
698
699/* NT_AREAS:
700 * uint32 count;
701 * uint32 entrySize;
702 * Elf32_Note_Area_Entry[count];
703 * char[] names
704 */
705typedef struct {
706	int32		na_id;				/* area ID */
707	uint32		na_lock;			/* lock type (B_NO_LOCK, ...) */
708	uint32		na_protection;		/* protection (B_READ_AREA | ...) */
709	uint32		na_base;			/* area base address */
710	uint32		na_size;			/* area size */
711	uint32		na_ram_size;		/* physical memory used */
712} Elf32_Note_Area_Entry;
713
714/* NT_AREAS:
715 * uint32 count;
716 * uint32 entrySize;
717 * Elf64_Note_Area_Entry[count];
718 * char[] names
719 */
720typedef struct {
721	int32		na_id;				/* area ID */
722	uint32		na_lock;			/* lock type (B_NO_LOCK, ...) */
723	uint32		na_protection;		/* protection (B_READ_AREA | ...) */
724	uint32		na_pad1;
725	uint64		na_base;			/* area base address */
726	uint64		na_size;			/* area size */
727	uint64		na_ram_size;		/* physical memory used */
728} Elf64_Note_Area_Entry;
729
730/* NT_IMAGES:
731 * uint32 count;
732 * uint32 entrySize;
733 * Elf32_Note_Image_Entry[count];
734 * char[] names
735 */
736typedef struct {
737	int32		ni_id;				/* image ID */
738	int32		ni_type;			/* image type (B_APP_IMAGE, ...) */
739	uint32		ni_init_routine;	/* address of init function */
740	uint32		ni_term_routine;	/* address of termination function */
741	int32		ni_device;			/* device ID of mapped file */
742	int64		ni_node;			/* node ID of mapped file */
743	uint32		ni_text_base;		/* base address of text segment */
744	uint32		ni_text_size;		/* size of text segment */
745	int32		ni_text_delta;		/* delta of the text segment relative to
746									   load address specified in the ELF file */
747	uint32		ni_data_base;		/* base address of data segment */
748	uint32		ni_data_size;		/* size of data segment */
749	uint32		ni_symbol_table;	/* address of dynamic symbol table */
750	uint32		ni_symbol_hash;		/* address of dynamic symbol hash */
751	uint32		ni_string_table;	/* address of dynamic string table */
752} Elf32_Note_Image_Entry;
753
754/* NT_IMAGES:
755 * uint32 count;
756 * uint32 entrySize;
757 * Elf64_Note_Image_Entry[count];
758 * char[] names
759 */
760typedef struct {
761	int32		ni_id;				/* image ID */
762	int32		ni_type;			/* image type (B_APP_IMAGE, ...) */
763	uint64		ni_init_routine;	/* address of init function */
764	uint64		ni_term_routine;	/* address of termination function */
765	uint32		ni_pad1;
766	int32		ni_device;			/* device ID of mapped file */
767	int64		ni_node;			/* node ID of mapped file */
768	uint64		ni_text_base;		/* base address of text segment */
769	uint64		ni_text_size;		/* size of text segment */
770	int64		ni_text_delta;		/* delta of the text segment relative to
771									   load address specified in the ELF file */
772	uint64		ni_data_base;		/* base address of data segment */
773	uint64		ni_data_size;		/* size of data segment */
774	uint64		ni_symbol_table;	/* address of dynamic symbol table */
775	uint64		ni_symbol_hash;		/* address of dynamic symbol hash */
776	uint64		ni_string_table;	/* address of dynamic string table */
777} Elf64_Note_Image_Entry;
778
779/* NT_THREADS:
780 * uint32 count;
781 * uint32 entrySize;
782 * uint32 cpuStateSize;
783 * {Elf32_Note_Thread_Entry, uint8[cpuStateSize] cpuState}[count];
784 * char[] names
785 */
786typedef struct {
787	int32		nth_id;				/* thread ID */
788	int32		nth_state;			/* thread state (B_THREAD_RUNNING, ...) */
789	int32		nth_priority;		/* thread priority */
790	uint32		nth_stack_base;		/* thread stack base address */
791	uint32		nth_stack_end;		/* thread stack end address */
792} Elf32_Note_Thread_Entry;
793
794/* NT_THREADS:
795 * uint32 count;
796 * uint32 entrySize;
797 * uint32 cpuStateSize;
798 * {Elf64_Note_Thread_Entry, uint8[cpuStateSize] cpuState}[count];
799 * char[] names
800 */
801typedef struct {
802	int32		nth_id;				/* thread ID */
803	int32		nth_state;			/* thread state (B_THREAD_RUNNING, ...) */
804	int32		nth_priority;		/* thread priority */
805	uint32		nth_pad1;
806	uint64		nth_stack_base;		/* thread stack base address */
807	uint64		nth_stack_end;		/* thread stack end address */
808} Elf64_Note_Thread_Entry;
809
810/* NT_SYMBOLS:
811 * int32 imageId;
812 * uint32 symbolCount;
813 * uint32 entrySize;
814 * Elf{32,64}_Sym[count];
815 * char[] strings
816 */
817
818
819/*** inline functions ***/
820
821#ifdef __cplusplus
822
823inline bool
824Elf32_Ehdr::IsHostEndian() const
825{
826#if B_HOST_IS_LENDIAN
827	return e_ident[EI_DATA] == ELFDATA2LSB;
828#elif B_HOST_IS_BENDIAN
829	return e_ident[EI_DATA] == ELFDATA2MSB;
830#endif
831}
832
833
834inline bool
835Elf64_Ehdr::IsHostEndian() const
836{
837#if B_HOST_IS_LENDIAN
838	return e_ident[EI_DATA] == ELFDATA2LSB;
839#elif B_HOST_IS_BENDIAN
840	return e_ident[EI_DATA] == ELFDATA2MSB;
841#endif
842}
843
844
845inline bool
846Elf32_Phdr::IsReadWrite() const
847{
848	return !(~p_flags & (PF_READ | PF_WRITE));
849}
850
851
852inline bool
853Elf32_Phdr::IsExecutable() const
854{
855	return (p_flags & PF_EXECUTE) != 0;
856}
857
858
859inline bool
860Elf64_Phdr::IsReadWrite() const
861{
862	return !(~p_flags & (PF_READ | PF_WRITE));
863}
864
865
866inline bool
867Elf64_Phdr::IsExecutable() const
868{
869	return (p_flags & PF_EXECUTE) != 0;
870}
871
872
873inline uint8
874Elf32_Sym::Bind() const
875{
876	return ELF32_ST_BIND(st_info);
877}
878
879
880inline uint8
881Elf32_Sym::Type() const
882{
883	return ELF32_ST_TYPE(st_info);
884}
885
886
887inline void
888Elf32_Sym::SetInfo(uint8 bind, uint8 type)
889{
890	st_info = ELF32_ST_INFO(bind, type);
891}
892
893
894inline uint8
895Elf64_Sym::Bind() const
896{
897	return ELF64_ST_BIND(st_info);
898}
899
900
901inline uint8
902Elf64_Sym::Type() const
903{
904	return ELF64_ST_TYPE(st_info);
905}
906
907
908inline void
909Elf64_Sym::SetInfo(uint8 bind, uint8 type)
910{
911	st_info = ELF64_ST_INFO(bind, type);
912}
913
914
915inline uint8
916Elf32_Rel::SymbolIndex() const
917{
918	return ELF32_R_SYM(r_info);
919}
920
921
922inline uint8
923Elf32_Rel::Type() const
924{
925	return ELF32_R_TYPE(r_info);
926}
927
928
929inline uint8
930Elf64_Rel::SymbolIndex() const
931{
932	return ELF64_R_SYM(r_info);
933}
934
935
936inline uint8
937Elf64_Rel::Type() const
938{
939	return ELF64_R_TYPE(r_info);
940}
941
942#endif	/* __cplusplus */
943
944
945#endif	/* _ELF_H */
946