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