1e3616ca1SSimon South/*
23ddf0bbcSZoltán Mizsei * Copyright 2002-2019 Haiku, Inc. All rights reserved.
3e3616ca1SSimon South * Distributed under the terms of the MIT License.
4e3616ca1SSimon South */
5e3616ca1SSimon South#ifndef _ELF_H
6e3616ca1SSimon South#define _ELF_H
7e3616ca1SSimon South
8e3616ca1SSimon South
93ddf0bbcSZoltán Mizsei#include <BeBuild.h>
10e3616ca1SSimon South#include <ByteOrder.h>
113ddf0bbcSZoltán Mizsei#include <SupportDefs.h>
12e3616ca1SSimon South
13e3616ca1SSimon South
14e3616ca1SSimon Southtypedef uint32 Elf32_Addr;
15e3616ca1SSimon Southtypedef uint16 Elf32_Half;
16e3616ca1SSimon Southtypedef uint32 Elf32_Off;
17e3616ca1SSimon Southtypedef int32 Elf32_Sword;
18e3616ca1SSimon Southtypedef uint32 Elf32_Word;
19e3616ca1SSimon South
20e3616ca1SSimon Southtypedef Elf32_Half Elf32_Versym;
21e3616ca1SSimon South
22e3616ca1SSimon Southtypedef uint64 Elf64_Addr;
23e3616ca1SSimon Southtypedef uint64 Elf64_Off;
24e3616ca1SSimon Southtypedef uint16 Elf64_Half;
25e3616ca1SSimon Southtypedef uint32 Elf64_Word;
26e3616ca1SSimon Southtypedef int32 Elf64_Sword;
27e3616ca1SSimon Southtypedef uint64 Elf64_Xword;
28e3616ca1SSimon Southtypedef int64 Elf64_Sxword;
29e3616ca1SSimon South
30e3616ca1SSimon Southtypedef Elf64_Half Elf64_Versym;
31e3616ca1SSimon South
323ddf0bbcSZoltán Mizsei#if B_HAIKU_32_BIT
333ddf0bbcSZoltán Mizsei	#define Elf_Addr Elf32_Addr
343ddf0bbcSZoltán Mizsei	#define Elf_Phdr Elf32_Phdr
353ddf0bbcSZoltán Mizsei	#define Elf_Half Elf32_Half
363ddf0bbcSZoltán Mizsei#elif B_HAIKU_64_BIT
373ddf0bbcSZoltán Mizsei	#define Elf_Addr Elf64_Addr
383ddf0bbcSZoltán Mizsei	#define Elf_Phdr Elf64_Phdr
393ddf0bbcSZoltán Mizsei	#define Elf_Half Elf64_Half
403ddf0bbcSZoltán Mizsei#endif
413ddf0bbcSZoltán Mizsei
42e3616ca1SSimon South
43e3616ca1SSimon South/*** ELF header ***/
44e3616ca1SSimon South
45e3616ca1SSimon South#define EI_NIDENT	16
46e3616ca1SSimon South
47e3616ca1SSimon Southtypedef struct {
48e3616ca1SSimon South	uint8		e_ident[EI_NIDENT];
49e3616ca1SSimon South	Elf32_Half	e_type;
50e3616ca1SSimon South	Elf32_Half	e_machine;
51e3616ca1SSimon South	Elf32_Word	e_version;
52e3616ca1SSimon South	Elf32_Addr	e_entry;
53e3616ca1SSimon South	Elf32_Off	e_phoff;
54e3616ca1SSimon South	Elf32_Off	e_shoff;
55e3616ca1SSimon South	Elf32_Word	e_flags;
56e3616ca1SSimon South	Elf32_Half	e_ehsize;
57e3616ca1SSimon South	Elf32_Half	e_phentsize;
58e3616ca1SSimon South	Elf32_Half	e_phnum;
59e3616ca1SSimon South	Elf32_Half	e_shentsize;
60e3616ca1SSimon South	Elf32_Half	e_shnum;
61e3616ca1SSimon South	Elf32_Half	e_shstrndx;
62e3616ca1SSimon South
63e3616ca1SSimon South#ifdef __cplusplus
64e3616ca1SSimon South	bool IsHostEndian() const;
65e3616ca1SSimon South#endif
66e3616ca1SSimon South} Elf32_Ehdr;
67e3616ca1SSimon South
68e3616ca1SSimon Southtypedef struct {
69e3616ca1SSimon South	uint8		e_ident[EI_NIDENT];
70e3616ca1SSimon South	Elf64_Half	e_type;
71e3616ca1SSimon South	Elf64_Half	e_machine;
72e3616ca1SSimon South	Elf64_Word	e_version;
73e3616ca1SSimon South	Elf64_Addr	e_entry;
74e3616ca1SSimon South	Elf64_Off	e_phoff;
75e3616ca1SSimon South	Elf64_Off	e_shoff;
76e3616ca1SSimon South	Elf64_Word	e_flags;
77e3616ca1SSimon South	Elf64_Half	e_ehsize;
78e3616ca1SSimon South	Elf64_Half	e_phentsize;
79e3616ca1SSimon South	Elf64_Half	e_phnum;
80e3616ca1SSimon South	Elf64_Half	e_shentsize;
81e3616ca1SSimon South	Elf64_Half	e_shnum;
82e3616ca1SSimon South	Elf64_Half	e_shstrndx;
83e3616ca1SSimon South
84e3616ca1SSimon South#ifdef __cplusplus
85e3616ca1SSimon South	bool IsHostEndian() const;
86e3616ca1SSimon South#endif
87e3616ca1SSimon South} Elf64_Ehdr;
88e3616ca1SSimon South
89e3616ca1SSimon South/* e_ident[] indices */
90e3616ca1SSimon South#define EI_MAG0		0
91e3616ca1SSimon South#define EI_MAG1		1
92e3616ca1SSimon South#define EI_MAG2		2
93e3616ca1SSimon South#define EI_MAG3		3
94e3616ca1SSimon South#define EI_CLASS	4
95e3616ca1SSimon South#define EI_DATA		5
96e3616ca1SSimon South#define EI_VERSION	6
97e3616ca1SSimon South#define EI_PAD		7
98e3616ca1SSimon South
99b32f51ceSAdrien Destugues/* Values for the magic number bytes. */
100b32f51ceSAdrien Destugues#define ELFMAG0		0x7f
101b32f51ceSAdrien Destugues#define ELFMAG1		'E'
102b32f51ceSAdrien Destugues#define ELFMAG2		'L'
103b32f51ceSAdrien Destugues#define ELFMAG3		'F'
104b32f51ceSAdrien Destugues#define ELFMAG		"\x7f""ELF"
105b32f51ceSAdrien Destugues#define SELFMAG		4
106b32f51ceSAdrien Destugues
107076b1902SAdrien Destugues/* e_ident */
108076b1902SAdrien Destugues#define IS_ELF(ehdr)    ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
109076b1902SAdrien Destugues	(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
110076b1902SAdrien Destugues	(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
111076b1902SAdrien Destugues	(ehdr).e_ident[EI_MAG3] == ELFMAG3)
112076b1902SAdrien Destugues
113e3616ca1SSimon South/* e_type (Object file type) */
114e3616ca1SSimon South#define ET_NONE			0 /* No file type */
115e3616ca1SSimon South#define ET_REL			1 /* Relocatable file */
116e3616ca1SSimon South#define ET_EXEC			2 /* Executable file */
117e3616ca1SSimon South#define ET_DYN			3 /* Shared-object file */
118e3616ca1SSimon South#define ET_CORE			4 /* Core file */
119e3616ca1SSimon South#define ET_LOOS			0xfe00 /* OS-specific range start */
120e3616ca1SSimon South#define ET_HIOS			0xfeff /* OS-specific range end */
121e3616ca1SSimon South#define ET_LOPROC		0xff00 /* Processor-specific range start */
122e3616ca1SSimon South#define ET_HIPROC		0xffff /* Processor-specific range end */
123e3616ca1SSimon South
124e3616ca1SSimon South/* e_machine (Architecture) */
125e3616ca1SSimon South#define EM_NONE			0 /* No machine */
126e3616ca1SSimon South#define EM_M32			1 /* AT&T WE 32100 */
127e3616ca1SSimon South#define EM_SPARC		2 /* Sparc */
128e3616ca1SSimon South#define EM_386			3 /* Intel 80386 */
129e3616ca1SSimon South#define EM_68K			4 /* Motorola m68k family */
130e3616ca1SSimon South#define EM_88K			5 /* Motorola m88k family */
131e3616ca1SSimon South#define EM_486			6 /* Intel 80486, Reserved for future use */
132e3616ca1SSimon South#define EM_860			7 /* Intel 80860 */
133e3616ca1SSimon South#define EM_MIPS			8 /* MIPS R3000 (officially, big-endian only) */
134e3616ca1SSimon South#define EM_S370			9 /* IBM System/370 */
135e3616ca1SSimon South#define EM_MIPS_RS3_LE	10 /* MIPS R3000 little-endian, Deprecated */
136e3616ca1SSimon South#define EM_PARISC		15 /* HPPA */
137e3616ca1SSimon South#define EM_VPP550		17 /* Fujitsu VPP500 */
138e3616ca1SSimon South#define EM_SPARC32PLUS	18 /* Sun "v8plus" */
139e3616ca1SSimon South#define EM_960			19 /* Intel 80960 */
140e3616ca1SSimon South#define EM_PPC			20 /* PowerPC */
141e3616ca1SSimon South#define EM_PPC64		21 /* 64-bit PowerPC */
142e3616ca1SSimon South#define EM_S390			22 /* IBM S/390 */
143e3616ca1SSimon South#define EM_V800			36 /* NEC V800 series */
144e3616ca1SSimon South#define EM_FR20			37 /* Fujitsu FR20 */
145e3616ca1SSimon South#define EM_RH32			38 /* TRW RH32 */
146e3616ca1SSimon South#define EM_MCORE		39 /* Motorola M*Core */
147e3616ca1SSimon South#define EM_RCE			39 /* Old name for MCore */
148e3616ca1SSimon South#define EM_ARM			40 /* ARM */
149e3616ca1SSimon South#define EM_OLD_ALPHA	41 /* Digital Alpha */
150e3616ca1SSimon South#define EM_SH			42 /* Renesas / SuperH SH */
151e3616ca1SSimon South#define EM_SPARCV9		43 /* SPARC v9 64-bit */
152e3616ca1SSimon South#define EM_TRICORE		44 /* Siemens Tricore embedded processor */
153e3616ca1SSimon South#define EM_ARC			45 /* ARC Cores */
154e3616ca1SSimon South#define EM_H8_300		46 /* Renesas H8/300 */
155e3616ca1SSimon South#define EM_H8_300H		47 /* Renesas H8/300H */
156e3616ca1SSimon South#define EM_H8S			48 /* Renesas H8S */
157e3616ca1SSimon South#define EM_H8_500		49 /* Renesas H8/500 */
158e3616ca1SSimon South#define EM_IA_64		50 /* Intel IA-64 Processor */
159e3616ca1SSimon South#define EM_MIPS_X		51 /* Stanford MIPS-X */
160e3616ca1SSimon South#define EM_COLDFIRE		52 /* Motorola Coldfire */
161e3616ca1SSimon South#define EM_68HC12		53 /* Motorola M68HC12 */
162e3616ca1SSimon South#define EM_MMA			54 /* Fujitsu Multimedia Accelerator */
163e3616ca1SSimon South#define EM_PCP			55 /* Siemens PCP */
164e3616ca1SSimon South#define EM_NCPU			56 /* Sony nCPU embedded RISC processor */
165e3616ca1SSimon South#define EM_NDR1			57 /* Denso NDR1 microprocesspr */
166e3616ca1SSimon South#define EM_STARCORE		58 /* Motorola Star*Core processor */
167e3616ca1SSimon South#define EM_ME16			59 /* Toyota ME16 processor */
168e3616ca1SSimon South#define EM_ST100		60 /* STMicroelectronics ST100 processor */
169e3616ca1SSimon South#define EM_TINYJ		61 /* Advanced Logic Corp. TinyJ embedded processor */
170e3616ca1SSimon South#define EM_X86_64		62 /* Advanced Micro Devices X86-64 processor */
171b68cf9d6SAdrien Destugues#define EM_PDSP			63 /* Sony DSP Processor */
172b68cf9d6SAdrien Destugues#define EM_FX66			66 /* Siemens FX66 microcontroller */
173b68cf9d6SAdrien Destugues#define EM_ST9PLUS		67 /* STMicroelectronics ST9+ 8/16 mc */
174b68cf9d6SAdrien Destugues#define EM_ST7			68 /* STmicroelectronics ST7 8 bit mc */
175b68cf9d6SAdrien Destugues#define EM_68HC16		69 /* Motorola MC68HC16 microcontroller */
176b68cf9d6SAdrien Destugues#define EM_68HC11		70 /* Motorola MC68HC11 microcontroller */
177b68cf9d6SAdrien Destugues#define EM_68HC08		71 /* Motorola MC68HC08 microcontroller */
178b68cf9d6SAdrien Destugues#define EM_68HC05		72 /* Motorola MC68HC05 microcontroller */
179b68cf9d6SAdrien Destugues#define EM_SVX			73 /* Silicon Graphics SVx */
180b68cf9d6SAdrien Destugues#define EM_ST19			74 /* STMicroelectronics ST19 8 bit mc */
181b68cf9d6SAdrien Destugues#define EM_VAX			75 /* Digital VAX */
182b68cf9d6SAdrien Destugues#define EM_CRIS			76 /* Axis Communications 32-bit embedded processor */
183b68cf9d6SAdrien Destugues#define EM_JAVELIN		77 /* Infineon Technologies 32-bit embedded processor */
184b68cf9d6SAdrien Destugues#define EM_FIREPATH		78 /* Element 14 64-bit DSP Processor */
185b68cf9d6SAdrien Destugues#define EM_ZSP			79 /* LSI Logic 16-bit DSP Processor */
186b68cf9d6SAdrien Destugues#define EM_MMIX			80 /* Donald Knuth's educational 64-bit processor */
187b68cf9d6SAdrien Destugues#define EM_HUANY		81 /* Harvard University machine-independent object files */
188b68cf9d6SAdrien Destugues#define EM_PRISM		82 /* SiTera Prism */
189b68cf9d6SAdrien Destugues#define EM_AVR			83 /* Atmel AVR 8-bit microcontroller */
190b68cf9d6SAdrien Destugues#define EM_FR30			84 /* Fujitsu FR30 */
191b68cf9d6SAdrien Destugues#define EM_D10V			85 /* Mitsubishi D10V */
192b68cf9d6SAdrien Destugues#define EM_D30V			86 /* Mitsubishi D30V */
193b68cf9d6SAdrien Destugues#define EM_V850			87 /* NEC v850 */
194b68cf9d6SAdrien Destugues#define EM_M32R			88 /* Mitsubishi M32R */
195b68cf9d6SAdrien Destugues#define EM_MN10300		89 /* Matsushita MN10300 */
196b68cf9d6SAdrien Destugues#define EM_MN10200		90 /* Matsushita MN10200 */
197b68cf9d6SAdrien Destugues#define EM_PJ			91 /* picoJava */
198b68cf9d6SAdrien Destugues#define EM_OPENRISC		92 /* OpenRISC 32-bit embedded processor */
199b68cf9d6SAdrien Destugues#define EM_ARC_A5		93 /* ARC Cores Tangent-A5 */
200b68cf9d6SAdrien Destugues#define EM_XTENSA		94 /* Tensilica Xtensa Architecture */
201dd485ed4SAlexander von Gluck IV#define EM_VIDCORE		95 /* Alphamosaic VideoCore */
202dd485ed4SAlexander von Gluck IV#define EM_CR			103 /* Nat. Semi. CompactRISC microprocessor */
203dd485ed4SAlexander von Gluck IV#define EM_BLACKFIN		106 /* Analog Devices Blackfin (DSP) processor */
204dd485ed4SAlexander von Gluck IV#define EM_ARCA			109 /* Arca RISC Microprocessor */
205dd485ed4SAlexander von Gluck IV#define EM_VIDCORE3		137 /* Broadcom VideoCore III */
206fb4cc984SAugustin Cavalier#define EM_ARM64		183 /* ARM 64 bit */
207fb4cc984SAugustin Cavalier#define EM_AARCH64		EM_ARM64
208dd485ed4SAlexander von Gluck IV#define EM_AVR32		185 /* AVR-32 */
209dd485ed4SAlexander von Gluck IV#define EM_STM8			186 /* ST STM8S */
210dd485ed4SAlexander von Gluck IV#define EM_CUDA			190 /* Nvidia CUDA */
211dd485ed4SAlexander von Gluck IV#define EM_VIDCORE5		198 /* Broadcom VideoCore V */
212dd485ed4SAlexander von Gluck IV#define EM_NORC			218 /* Nanoradio Optimized RISC */
213dd485ed4SAlexander von Gluck IV#define EM_AMDGPU		224 /* AMD GPU */
214dd485ed4SAlexander von Gluck IV#define EM_RISCV		243 /* RISC-V */
215dd485ed4SAlexander von Gluck IV#define EM_BPF			247 /* Linux kernel bpf virtual machine */
216e3616ca1SSimon South
217076b1902SAdrien Destugues#define EM_ALPHA        0x9026 /* Digital Alpha (nonstandard, but the value
218076b1902SAdrien Destugues								  everyone uses) */
219076b1902SAdrien Destugues
220e3616ca1SSimon South/* architecture class (EI_CLASS) */
22196658eaeSAdrien Destugues#define ELFCLASSNONE	0
22296658eaeSAdrien Destugues#define ELFCLASS32		1
22396658eaeSAdrien Destugues#define ELFCLASS64		2
22496658eaeSAdrien Destugues
225e3616ca1SSimon South/* endian (EI_DATA) */
22696658eaeSAdrien Destugues#define ELFDATANONE	0	/* invalid */
227e3616ca1SSimon South#define ELFDATA2LSB	1	/* little endian */
228e3616ca1SSimon South#define ELFDATA2MSB	2	/* big endian */
229e3616ca1SSimon South
230467fe4caSIngo Weinhold/* ELF version (EI_VERSION) */
231467fe4caSIngo Weinhold#define EV_NONE		0	/* invalid */
232467fe4caSIngo Weinhold#define EV_CURRENT	1	/* current version */
233e3616ca1SSimon South
234e3616ca1SSimon South/*** section header ***/
235e3616ca1SSimon South
236e3616ca1SSimon Southtypedef struct {
237e3616ca1SSimon South	Elf32_Word	sh_name;
238e3616ca1SSimon South	Elf32_Word	sh_type;
239e3616ca1SSimon South	Elf32_Word	sh_flags;
240e3616ca1SSimon South	Elf32_Addr	sh_addr;
241e3616ca1SSimon South	Elf32_Off	sh_offset;
242e3616ca1SSimon South	Elf32_Word	sh_size;
243e3616ca1SSimon South	Elf32_Word	sh_link;
244e3616ca1SSimon South	Elf32_Word	sh_info;
245e3616ca1SSimon South	Elf32_Word	sh_addralign;
246e3616ca1SSimon South	Elf32_Word	sh_entsize;
247e3616ca1SSimon South} Elf32_Shdr;
248e3616ca1SSimon South
249e3616ca1SSimon Southtypedef struct {
250e3616ca1SSimon South	Elf64_Word	sh_name;
251e3616ca1SSimon South	Elf64_Word	sh_type;
252e3616ca1SSimon South	Elf64_Xword	sh_flags;
253e3616ca1SSimon South	Elf64_Addr	sh_addr;
254e3616ca1SSimon South	Elf64_Off	sh_offset;
255e3616ca1SSimon South	Elf64_Xword	sh_size;
256e3616ca1SSimon South	Elf64_Word	sh_link;
257e3616ca1SSimon South	Elf64_Word	sh_info;
258e3616ca1SSimon South	Elf64_Xword	sh_addralign;
259e3616ca1SSimon South	Elf64_Xword	sh_entsize;
260e3616ca1SSimon South} Elf64_Shdr;
261e3616ca1SSimon South
262e3616ca1SSimon South/* special section indices */
263e3616ca1SSimon South#define SHN_UNDEF		0
264e3616ca1SSimon South#define SHN_LORESERVE	0xff00
265e3616ca1SSimon South#define SHN_LOPROC		0xff00
266e3616ca1SSimon South#define SHN_HIPROC		0xff1f
267e3616ca1SSimon South#define SHN_ABS			0xfff1
268e3616ca1SSimon South#define SHN_COMMON		0xfff2
269e3616ca1SSimon South#define SHN_HIRESERVE	0xffff
270e3616ca1SSimon South
271e3616ca1SSimon South/* section header type */
272e3616ca1SSimon South#define SHT_NULL		0
273e3616ca1SSimon South#define SHT_PROGBITS	1
274e3616ca1SSimon South#define SHT_SYMTAB		2
275e3616ca1SSimon South#define SHT_STRTAB		3
276e3616ca1SSimon South#define SHT_RELA		4
277e3616ca1SSimon South#define SHT_HASH		5
278e3616ca1SSimon South#define SHT_DYNAMIC		6
279e3616ca1SSimon South#define SHT_NOTE		7
280e3616ca1SSimon South#define SHT_NOBITS		8
281e3616ca1SSimon South#define SHT_REL			9
282e3616ca1SSimon South#define SHT_SHLIB		10
283e3616ca1SSimon South#define SHT_DYNSYM		11
284e3616ca1SSimon South
285e3616ca1SSimon South#define SHT_GNU_verdef	0x6ffffffd    /* version definition section */
286e3616ca1SSimon South#define SHT_GNU_verneed	0x6ffffffe    /* version needs section */
287e3616ca1SSimon South#define SHT_GNU_versym	0x6fffffff    /* version symbol table */
288e3616ca1SSimon South
289e3616ca1SSimon South#define SHT_LOPROC		0x70000000
290e3616ca1SSimon South#define SHT_HIPROC		0x7fffffff
291e3616ca1SSimon South#define SHT_LOUSER		0x80000000
292e3616ca1SSimon South#define SHT_HIUSER		0xffffffff
293e3616ca1SSimon South
294e3616ca1SSimon South/* section header flags */
295e3616ca1SSimon South#define SHF_WRITE		1
296e3616ca1SSimon South#define SHF_ALLOC		2
297e3616ca1SSimon South#define SHF_EXECINSTR	4
298e3616ca1SSimon South
299e3616ca1SSimon South#define SHF_MASKPROC	0xf0000000
300e3616ca1SSimon South
301e3616ca1SSimon South
302e3616ca1SSimon South/*** program header ***/
303e3616ca1SSimon South
304e3616ca1SSimon Southtypedef struct {
305e3616ca1SSimon South	Elf32_Word	p_type;
306e3616ca1SSimon South	Elf32_Off	p_offset;	/* offset from the beginning of the file of the segment */
307e3616ca1SSimon South	Elf32_Addr	p_vaddr;	/* virtual address for the segment in memory */
308e3616ca1SSimon South	Elf32_Addr	p_paddr;
309e3616ca1SSimon South	Elf32_Word	p_filesz;	/* the size of the segment in the file */
310e3616ca1SSimon South	Elf32_Word	p_memsz;	/* the size of the segment in memory */
311e3616ca1SSimon South	Elf32_Word	p_flags;
312e3616ca1SSimon South	Elf32_Word	p_align;
313e3616ca1SSimon South
314e3616ca1SSimon South#ifdef __cplusplus
315e3616ca1SSimon South	bool IsReadWrite() const;
316e3616ca1SSimon South	bool IsExecutable() const;
317e3616ca1SSimon South#endif
318e3616ca1SSimon South} Elf32_Phdr;
319e3616ca1SSimon South
320e3616ca1SSimon Southtypedef struct {
321e3616ca1SSimon South	Elf64_Word	p_type;
322e3616ca1SSimon South	Elf64_Word	p_flags;
323e3616ca1SSimon South	Elf64_Off	p_offset;	/* offset from the beginning of the file of the segment */
324e3616ca1SSimon South	Elf64_Addr	p_vaddr;	/* virtual address for the segment in memory */
325e3616ca1SSimon South	Elf64_Addr	p_paddr;
326e3616ca1SSimon South	Elf64_Xword	p_filesz;	/* the size of the segment in the file */
327e3616ca1SSimon South	Elf64_Xword	p_memsz;	/* the size of the segment in memory */
328e3616ca1SSimon South	Elf64_Xword	p_align;
329e3616ca1SSimon South
330e3616ca1SSimon South#ifdef __cplusplus
331e3616ca1SSimon South	bool IsReadWrite() const;
332e3616ca1SSimon South	bool IsExecutable() const;
333e3616ca1SSimon South#endif
334e3616ca1SSimon South} Elf64_Phdr;
335e3616ca1SSimon South
336e3616ca1SSimon South/* program header segment types */
337e3616ca1SSimon South#define PT_NULL			0
338e3616ca1SSimon South#define PT_LOAD			1
339e3616ca1SSimon South#define PT_DYNAMIC		2
340e3616ca1SSimon South#define PT_INTERP		3
341e3616ca1SSimon South#define PT_NOTE			4
342e3616ca1SSimon South#define PT_SHLIB		5
343e3616ca1SSimon South#define PT_PHDR			6
344e3616ca1SSimon South#define PT_TLS			7
345ef593f61SAugustin Cavalier#define PT_EH_FRAME		0x6474e550
346e3616ca1SSimon South#define PT_STACK		0x6474e551
347e3616ca1SSimon South#define PT_RELRO		0x6474e552
348e3616ca1SSimon South
349e3616ca1SSimon South#define PT_LOPROC		0x70000000
350e3616ca1SSimon South#define PT_ARM_UNWIND	0x70000001
351e3616ca1SSimon South#define PT_HIPROC		0x7fffffff
352e3616ca1SSimon South
353e3616ca1SSimon South/* program header segment flags */
354e3616ca1SSimon South#define PF_EXECUTE	0x1
355e3616ca1SSimon South#define PF_WRITE	0x2
356e3616ca1SSimon South#define PF_READ		0x4
357e3616ca1SSimon South#define PF_PROTECTION_MASK (PF_EXECUTE | PF_WRITE | PF_READ)
358e3616ca1SSimon South
359e3616ca1SSimon South#define PF_MASKPROC	0xf0000000
360e3616ca1SSimon South
361e3616ca1SSimon South
362e3616ca1SSimon South/* symbol table entry */
363e3616ca1SSimon South
364e3616ca1SSimon Southtypedef struct {
365e3616ca1SSimon South	Elf32_Word	st_name;
366e3616ca1SSimon South	Elf32_Addr	st_value;
367e3616ca1SSimon South	Elf32_Word	st_size;
368e3616ca1SSimon South	uint8		st_info;
369e3616ca1SSimon South	uint8 		st_other;
370e3616ca1SSimon South	Elf32_Half	st_shndx;
371e3616ca1SSimon South
372e3616ca1SSimon South#ifdef __cplusplus
373e3616ca1SSimon South	uint8 Bind() const;
374e3616ca1SSimon South	uint8 Type() const;
375e3616ca1SSimon South	void SetInfo(uint8 bind, uint8 type);
376e3616ca1SSimon South#endif
377e3616ca1SSimon South} Elf32_Sym;
378e3616ca1SSimon South
379e3616ca1SSimon Southtypedef struct {
380e3616ca1SSimon South	Elf64_Word	st_name;
381e3616ca1SSimon South	uint8		st_info;
382e3616ca1SSimon South	uint8		st_other;
383e3616ca1SSimon South	Elf64_Half	st_shndx;
384e3616ca1SSimon South	Elf64_Addr	st_value;
385e3616ca1SSimon South	Elf64_Xword	st_size;
386e3616ca1SSimon South
387e3616ca1SSimon South#ifdef __cplusplus
388e3616ca1SSimon South	uint8 Bind() const;
389e3616ca1SSimon South	uint8 Type() const;
390e3616ca1SSimon South	void SetInfo(uint8 bind, uint8 type);
391e3616ca1SSimon South#endif
392e3616ca1SSimon South} Elf64_Sym;
393e3616ca1SSimon South
394e3616ca1SSimon South#define ELF32_ST_BIND(i) ((i) >> 4)
395e3616ca1SSimon South#define ELF32_ST_TYPE(i) ((i) & 0xf)
396e3616ca1SSimon South#define ELF32_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf))
397e3616ca1SSimon South
398e3616ca1SSimon South#define ELF64_ST_BIND(i) ((i) >> 4)
399e3616ca1SSimon South#define ELF64_ST_TYPE(i) ((i) & 0xf)
400e3616ca1SSimon South#define ELF64_ST_INFO(b, t) (((b) << 4) + ((t) & 0xf))
401e3616ca1SSimon South
402e3616ca1SSimon South/* symbol types */
403e3616ca1SSimon South#define STT_NOTYPE 0
404e3616ca1SSimon South#define STT_OBJECT 1
405e3616ca1SSimon South#define STT_FUNC 2
406e3616ca1SSimon South#define STT_SECTION 3
407e3616ca1SSimon South#define STT_FILE 4
408e3616ca1SSimon South#define STT_TLS		6
409e3616ca1SSimon South#define STT_LOPROC 13
410e3616ca1SSimon South#define STT_HIPROC 15
411e3616ca1SSimon South
412e3616ca1SSimon South/* symbol binding */
413e3616ca1SSimon South#define STB_LOCAL 0
414e3616ca1SSimon South#define STB_GLOBAL 1
415e3616ca1SSimon South#define STB_WEAK 2
416e3616ca1SSimon South#define STB_LOPROC 13
417e3616ca1SSimon South#define STB_HIPROC 15
418e3616ca1SSimon South
419e3616ca1SSimon South/* special symbol indices */
420e3616ca1SSimon South#define STN_UNDEF 0
421e3616ca1SSimon South
422e3616ca1SSimon South
423e3616ca1SSimon South/* relocation table entry */
424e3616ca1SSimon South
425e3616ca1SSimon Southtypedef struct {
426e3616ca1SSimon South	Elf32_Addr r_offset;
427e3616ca1SSimon South	Elf32_Word r_info;
428e3616ca1SSimon South
429e3616ca1SSimon South#ifdef __cplusplus
430e3616ca1SSimon South	uint8 SymbolIndex() const;
431e3616ca1SSimon South	uint8 Type() const;
432e3616ca1SSimon South#endif
433e3616ca1SSimon South} Elf32_Rel;
434e3616ca1SSimon South
435e3616ca1SSimon Southtypedef struct {
436e3616ca1SSimon South	Elf64_Addr	r_offset;
437e3616ca1SSimon South	Elf64_Xword	r_info;
438e3616ca1SSimon South
439e3616ca1SSimon South#ifdef __cplusplus
440e3616ca1SSimon South	uint8 SymbolIndex() const;
441e3616ca1SSimon South	uint8 Type() const;
442e3616ca1SSimon South#endif
443e3616ca1SSimon South} Elf64_Rel;
444e3616ca1SSimon South
445e3616ca1SSimon South#ifdef __cplusplus
446e3616ca1SSimon Southtypedef struct Elf32_Rela : public Elf32_Rel {
447e3616ca1SSimon South#else
448e3616ca1SSimon Southtypedef struct {
449e3616ca1SSimon South	Elf32_Addr r_offset;
450e3616ca1SSimon South	Elf32_Word r_info;
451e3616ca1SSimon South#endif
452e3616ca1SSimon South	Elf32_Sword r_addend;
453e3616ca1SSimon South} Elf32_Rela;
454e3616ca1SSimon South
455e3616ca1SSimon South#ifdef __cplusplus
456e3616ca1SSimon Southtypedef struct Elf64_Rela : public Elf64_Rel {
457e3616ca1SSimon South#else
458e3616ca1SSimon Southtypedef struct {
459e3616ca1SSimon South	Elf64_Addr		r_offset;
460e3616ca1SSimon South	Elf64_Xword		r_info;
461e3616ca1SSimon South#endif
462e3616ca1SSimon South	Elf64_Sxword	r_addend;
463e3616ca1SSimon South} Elf64_Rela;
464e3616ca1SSimon South
465e3616ca1SSimon South#define ELF32_R_SYM(i) ((i) >> 8)
466e3616ca1SSimon South#define ELF32_R_TYPE(i) ((unsigned char)(i))
467e3616ca1SSimon South#define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t))
468e3616ca1SSimon South
469e3616ca1SSimon South#define ELF64_R_SYM(i) ((i) >> 32)
470e3616ca1SSimon South#define ELF64_R_TYPE(i) ((i) & 0xffffffffL)
471e3616ca1SSimon South#define ELF64_R_INFO(s, t) ((((Elf64_Xword)(s)) << 32) + ((t) & 0xffffffffL))
472e3616ca1SSimon South
473e3616ca1SSimon South
474e3616ca1SSimon South/* dynamic section entry */
475e3616ca1SSimon South
476e3616ca1SSimon Southtypedef struct {
477e3616ca1SSimon South	Elf32_Sword d_tag;
478e3616ca1SSimon South	union {
479e3616ca1SSimon South		Elf32_Word d_val;
480e3616ca1SSimon South		Elf32_Addr d_ptr;
481e3616ca1SSimon South	} d_un;
482e3616ca1SSimon South} Elf32_Dyn;
483e3616ca1SSimon South
484e3616ca1SSimon Southtypedef struct {
485e3616ca1SSimon South	Elf64_Sxword d_tag;
486e3616ca1SSimon South	union {
487e3616ca1SSimon South		Elf64_Xword	d_val;
488e3616ca1SSimon South		Elf64_Addr	d_ptr;
489e3616ca1SSimon South	} d_un;
490e3616ca1SSimon South} Elf64_Dyn;
491e3616ca1SSimon South
492e3616ca1SSimon South/* dynamic entry type */
493e3616ca1SSimon South#define DT_NULL				0
494e3616ca1SSimon South#define DT_NEEDED			1
495e3616ca1SSimon South#define DT_PLTRELSZ			2
496e3616ca1SSimon South#define DT_PLTGOT			3
497e3616ca1SSimon South#define DT_HASH				4
498e3616ca1SSimon South#define DT_STRTAB			5
499e3616ca1SSimon South#define DT_SYMTAB			6
500e3616ca1SSimon South#define DT_RELA				7
501e3616ca1SSimon South#define DT_RELASZ			8
502e3616ca1SSimon South#define DT_RELAENT			9
503e3616ca1SSimon South#define DT_STRSZ			10
504e3616ca1SSimon South#define DT_SYMENT			11
505e3616ca1SSimon South#define DT_INIT				12
506e3616ca1SSimon South#define DT_FINI				13
507e3616ca1SSimon South#define DT_SONAME			14
508e3616ca1SSimon South#define DT_RPATH			15
509e3616ca1SSimon South#define DT_SYMBOLIC			16
510e3616ca1SSimon South#define DT_REL				17
511e3616ca1SSimon South#define DT_RELSZ			18
512e3616ca1SSimon South#define DT_RELENT			19
513e3616ca1SSimon South#define DT_PLTREL			20
514e3616ca1SSimon South#define DT_DEBUG			21
515e3616ca1SSimon South#define DT_TEXTREL			22
516e3616ca1SSimon South#define DT_JMPREL			23
517e3616ca1SSimon South#define DT_BIND_NOW			24	/* no lazy binding */
518e3616ca1SSimon South#define DT_INIT_ARRAY		25	/* init function array */
519e3616ca1SSimon South#define DT_FINI_ARRAY		26	/* termination function array */
520e3616ca1SSimon South#define DT_INIT_ARRAYSZ		27	/* init function array size */
521e3616ca1SSimon South#define DT_FINI_ARRAYSZ		28	/* termination function array size */
522e3616ca1SSimon South#define DT_RUNPATH			29	/* library search path (supersedes DT_RPATH) */
523e3616ca1SSimon South#define DT_FLAGS			30	/* flags (see below) */
524e3616ca1SSimon South#define DT_ENCODING			32
525e3616ca1SSimon South#define DT_PREINIT_ARRAY	32	/* preinitialization array */
526e3616ca1SSimon South#define DT_PREINIT_ARRAYSZ	33	/* preinitialization array size */
527e3616ca1SSimon South
528e3616ca1SSimon South#define DT_VERSYM       0x6ffffff0	/* symbol version table */
529e3616ca1SSimon South#define DT_VERDEF		0x6ffffffc	/* version definition table */
530e3616ca1SSimon South#define DT_VERDEFNUM	0x6ffffffd	/* number of version definitions */
531e3616ca1SSimon South#define DT_VERNEED		0x6ffffffe 	/* table with needed versions */
532e3616ca1SSimon South#define DT_VERNEEDNUM	0x6fffffff	/* number of needed versions */
533e3616ca1SSimon South
534e3616ca1SSimon South#define DT_LOPROC		0x70000000
535e3616ca1SSimon South#define DT_HIPROC		0x7fffffff
536e3616ca1SSimon South
537e3616ca1SSimon South/* DT_FLAGS values */
538e3616ca1SSimon South#define DF_ORIGIN		0x01
539e3616ca1SSimon South#define DF_SYMBOLIC		0x02
540e3616ca1SSimon South#define DF_TEXTREL		0x04
541e3616ca1SSimon South#define DF_BIND_NOW		0x08
542e3616ca1SSimon South#define DF_STATIC_TLS	0x10
543e3616ca1SSimon South
544e3616ca1SSimon South
545e3616ca1SSimon South/* version definition section */
546e3616ca1SSimon South
547e3616ca1SSimon Southtypedef struct {
548e3616ca1SSimon South	Elf32_Half	vd_version;		/* version revision */
549e3616ca1SSimon South	Elf32_Half	vd_flags;		/* version information flags */
550e3616ca1SSimon South	Elf32_Half	vd_ndx;			/* version index as specified in the
551e3616ca1SSimon South								   symbol version table */
552e3616ca1SSimon South	Elf32_Half	vd_cnt;			/* number of associated verdaux entries */
553e3616ca1SSimon South	Elf32_Word	vd_hash;		/* version name hash value */
554e3616ca1SSimon South	Elf32_Word	vd_aux;			/* byte offset to verdaux array */
555e3616ca1SSimon South	Elf32_Word	vd_next;		/* byte offset to next verdef entry */
556e3616ca1SSimon South} Elf32_Verdef;
557e3616ca1SSimon South
558e3616ca1SSimon Southtypedef struct {
559e3616ca1SSimon South	Elf64_Half	vd_version;		/* version revision */
560e3616ca1SSimon South	Elf64_Half	vd_flags;		/* version information flags */
561e3616ca1SSimon South	Elf64_Half	vd_ndx;			/* version index as specified in the
562e3616ca1SSimon South								   symbol version table */
563e3616ca1SSimon South	Elf64_Half	vd_cnt;			/* number of associated verdaux entries */
564e3616ca1SSimon South	Elf64_Word	vd_hash;		/* version name hash value */
565e3616ca1SSimon South	Elf64_Word	vd_aux;			/* byte offset to verdaux array */
566e3616ca1SSimon South	Elf64_Word	vd_next;		/* byte offset to next verdef entry */
567e3616ca1SSimon South} Elf64_Verdef;
568e3616ca1SSimon South
569e3616ca1SSimon South/* values for vd_version (version revision) */
570e3616ca1SSimon South#define VER_DEF_NONE		0		/* no version */
571e3616ca1SSimon South#define VER_DEF_CURRENT		1		/* current version */
572e3616ca1SSimon South#define VER_DEF_NUM			2		/* given version number */
573e3616ca1SSimon South
574e3616ca1SSimon South/* values for vd_flags (version information flags) */
575e3616ca1SSimon South#define VER_FLG_BASE		0x1		/* version definition of file itself */
576e3616ca1SSimon South#define VER_FLG_WEAK		0x2 	/* weak version identifier */
577e3616ca1SSimon South
578e3616ca1SSimon South/* values for versym symbol index */
579e3616ca1SSimon South#define VER_NDX_LOCAL		0		/* symbol is local */
580e3616ca1SSimon South#define VER_NDX_GLOBAL		1		/* symbol is global/unversioned */
581e3616ca1SSimon South#define VER_NDX_INITIAL		2		/* initial version -- that's the one given
582e3616ca1SSimon South									   to symbols when a library becomes
583e3616ca1SSimon South									   versioned; handled by the linker (and
584e3616ca1SSimon South									   runtime loader) similar to
585e3616ca1SSimon South									   VER_NDX_GLOBAL */
586e3616ca1SSimon South#define VER_NDX_LORESERVE	0xff00	/* beginning of reserved entries */
587e3616ca1SSimon South#define VER_NDX_ELIMINATE	0xff01	/* symbol is to be eliminated */
588e3616ca1SSimon South
589e3616ca1SSimon South#define VER_NDX_FLAG_HIDDEN	0x8000	/* flag: version is hidden */
590e3616ca1SSimon South#define VER_NDX_MASK		0x7fff	/* mask to get the actual version index */
591e3616ca1SSimon South#define VER_NDX(x)			((x) & VER_NDX_MASK)
592e3616ca1SSimon South
593e3616ca1SSimon South
594e3616ca1SSimon South/* auxiliary version information */
595e3616ca1SSimon South
596e3616ca1SSimon Southtypedef struct {
597e3616ca1SSimon South	Elf32_Word	vda_name;		/* string table offset to version or dependency
598e3616ca1SSimon South								   name */
599e3616ca1SSimon South	Elf32_Word	vda_next;		/* byte offset to next verdaux entry */
600e3616ca1SSimon South} Elf32_Verdaux;
601e3616ca1SSimon South
602e3616ca1SSimon Southtypedef struct {
603e3616ca1SSimon South	Elf64_Word	vda_name;		/* string table offset to version or dependency
604e3616ca1SSimon South								   name */
605e3616ca1SSimon South	Elf64_Word	vda_next;		/* byte offset to next verdaux entry */
606e3616ca1SSimon South} Elf64_Verdaux;
607e3616ca1SSimon South
608e3616ca1SSimon South
609e3616ca1SSimon South/* version dependency section */
610e3616ca1SSimon South
611e3616ca1SSimon Southtypedef struct {
612e3616ca1SSimon South	Elf32_Half	vn_version;		/* version of structure */
613e3616ca1SSimon South	Elf32_Half	vn_cnt;			/* number of associated vernaux entries */
614e3616ca1SSimon South	Elf32_Word	vn_file;		/* byte offset to file name for this
615e3616ca1SSimon South								   dependency */
616e3616ca1SSimon South	Elf32_Word	vn_aux;			/* byte offset to vernaux array */
617e3616ca1SSimon South	Elf32_Word	vn_next;		/* byte offset to next verneed entry */
618e3616ca1SSimon South} Elf32_Verneed;
619e3616ca1SSimon South
620e3616ca1SSimon Southtypedef struct {
621e3616ca1SSimon South	Elf64_Half	vn_version;		/* version of structure */
622e3616ca1SSimon South	Elf64_Half	vn_cnt;			/* number of associated vernaux entries */
623e3616ca1SSimon South	Elf64_Word	vn_file;		/* byte offset to file name for this
624e3616ca1SSimon South								   dependency */
625e3616ca1SSimon South	Elf64_Word	vn_aux;			/* byte offset to vernaux array */
626e3616ca1SSimon South	Elf64_Word	vn_next;		/* byte offset to next verneed entry */
627e3616ca1SSimon South} Elf64_Verneed;
628e3616ca1SSimon South
629e3616ca1SSimon South/* values for vn_version (version revision) */
630e3616ca1SSimon South#define VER_NEED_NONE		0	/* no version */
631e3616ca1SSimon South#define VER_NEED_CURRENT	1	/* current version */
632e3616ca1SSimon South#define VER_NEED_NUM		2	/* given version number */
633e3616ca1SSimon South
634e3616ca1SSimon South
635e3616ca1SSimon South/* auxiliary needed version information */
636e3616ca1SSimon South
637e3616ca1SSimon Southtypedef struct {
638e3616ca1SSimon South	Elf32_Word	vna_hash;		/* dependency name hash value */
639e3616ca1SSimon South	Elf32_Half	vna_flags;		/* dependency specific information flags */
640e3616ca1SSimon South	Elf32_Half	vna_other;		/* version index as specified in the symbol
641e3616ca1SSimon South								   version table */
642e3616ca1SSimon South	Elf32_Word	vna_name;		/* string table offset to dependency name */
643e3616ca1SSimon South	Elf32_Word	vna_next;		/* byte offset to next vernaux entry */
644e3616ca1SSimon South} Elf32_Vernaux;
645e3616ca1SSimon South
646e3616ca1SSimon Southtypedef struct {
647e3616ca1SSimon South	Elf64_Word	vna_hash;		/* dependency name hash value */
648e3616ca1SSimon South	Elf64_Half	vna_flags;		/* dependency specific information flags */
649e3616ca1SSimon South	Elf64_Half	vna_other;		/* version index as specified in the symbol
650e3616ca1SSimon South								   version table */
651e3616ca1SSimon South	Elf64_Word	vna_name;		/* string table offset to dependency name */
652e3616ca1SSimon South	Elf64_Word	vna_next;		/* byte offset to next vernaux entry */
653e3616ca1SSimon South} Elf64_Vernaux;
654e3616ca1SSimon South
655e3616ca1SSimon South/* values for vna_flags */
656e3616ca1SSimon South#define VER_FLG_WEAK	0x2		/* weak version identifier */
657e3616ca1SSimon South
658e3616ca1SSimon South
659467fe4caSIngo Weinhold/*** core files ***/
660467fe4caSIngo Weinhold
661467fe4caSIngo Weinhold/* note section header */
662467fe4caSIngo Weinhold
663467fe4caSIngo Weinholdtypedef struct {
664467fe4caSIngo Weinhold	Elf32_Word n_namesz;		/* length of the note's name */
665467fe4caSIngo Weinhold	Elf32_Word n_descsz;		/* length of the note's descriptor */
666467fe4caSIngo Weinhold	Elf32_Word n_type;			/* note type */
667467fe4caSIngo Weinhold} Elf32_Nhdr;
668467fe4caSIngo Weinhold
669467fe4caSIngo Weinholdtypedef struct {
670467fe4caSIngo Weinhold	Elf64_Word n_namesz;		/* length of the note's name */
671467fe4caSIngo Weinhold	Elf64_Word n_descsz;		/* length of the note's descriptor */
672467fe4caSIngo Weinhold	Elf64_Word n_type;			/* note type */
673467fe4caSIngo Weinhold} Elf64_Nhdr;
674467fe4caSIngo Weinhold
675467fe4caSIngo Weinhold/* values for note name */
676467fe4caSIngo Weinhold#define ELF_NOTE_CORE		"CORE"
677467fe4caSIngo Weinhold#define ELF_NOTE_HAIKU		"Haiku"
678467fe4caSIngo Weinhold
679467fe4caSIngo Weinhold/* values for note type (n_type) */
680467fe4caSIngo Weinhold/* ELF_NOTE_CORE/... */
681467fe4caSIngo Weinhold#define NT_FILE				0x46494c45 /* mapped files */
682467fe4caSIngo Weinhold
683467fe4caSIngo Weinhold/* ELF_NOTE_HAIKU/... */
684467fe4caSIngo Weinhold#define NT_TEAM				0x7465616d 	/* team */
685467fe4caSIngo Weinhold#define NT_AREAS			0x61726561 	/* areas */
686467fe4caSIngo Weinhold#define NT_IMAGES			0x696d6167 	/* images */
687467fe4caSIngo Weinhold#define NT_THREADS			0x74687264 	/* threads */
688c60453fdSIngo Weinhold#define NT_SYMBOLS			0x73796d73 	/* symbols */
689467fe4caSIngo Weinhold
69082185a52SIngo Weinhold/* NT_TEAM: uint32 entrySize; Elf32_Note_Team; char[] args */
691467fe4caSIngo Weinholdtypedef struct {
692467fe4caSIngo Weinhold	int32		nt_id;				/* team ID */
693467fe4caSIngo Weinhold	int32		nt_uid;				/* team owner ID */
694467fe4caSIngo Weinhold	int32		nt_gid;				/* team group ID */
695467fe4caSIngo Weinhold} Elf32_Note_Team;
696467fe4caSIngo Weinhold
697467fe4caSIngo Weinholdtypedef Elf32_Note_Team Elf64_Note_Team;
698467fe4caSIngo Weinhold
69982185a52SIngo Weinhold/* NT_AREAS:
70082185a52SIngo Weinhold * uint32 count;
70182185a52SIngo Weinhold * uint32 entrySize;
70282185a52SIngo Weinhold * Elf32_Note_Area_Entry[count];
70382185a52SIngo Weinhold * char[] names
70482185a52SIngo Weinhold */
705467fe4caSIngo Weinholdtypedef struct {
706467fe4caSIngo Weinhold	int32		na_id;				/* area ID */</