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