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