1076b1902SAdrien Destugues/*
2076b1902SAdrien Destugues * Copyright 2002-2016 Haiku, Inc. All rights reserved.
3076b1902SAdrien Destugues * Distributed under the terms of the MIT License.
4076b1902SAdrien Destugues */
5076b1902SAdrien Destugues#ifndef _ELF_H
6076b1902SAdrien Destugues#define _ELF_H
7076b1902SAdrien Destugues
8076b1902SAdrien Destugues
9076b1902SAdrien Destugues#include <SupportDefs.h>
10076b1902SAdrien Destugues#include <ByteOrder.h>
11076b1902SAdrien Destugues
12076b1902SAdrien Destugues
13076b1902SAdrien Destuguestypedef uint32 Elf32_Addr;
14076b1902SAdrien Destuguestypedef uint16 Elf32_Half;
15076b1902SAdrien Destuguestypedef uint32 Elf32_Off;
16076b1902SAdrien Destuguestypedef int32 Elf32_Sword;
17076b1902SAdrien Destuguestypedef uint32 Elf32_Word;
18076b1902SAdrien Destugues
19076b1902SAdrien Destuguestypedef Elf32_Half Elf32_Versym;
20076b1902SAdrien Destugues
21076b1902SAdrien Destuguestypedef uint64 Elf64_Addr;
22076b1902SAdrien Destuguestypedef uint64 Elf64_Off;
23076b1902SAdrien Destuguestypedef uint16 Elf64_Half;
24076b1902SAdrien Destuguestypedef uint32 Elf64_Word;
25076b1902SAdrien Destuguestypedef int32 Elf64_Sword;
26076b1902SAdrien Destuguestypedef uint64 Elf64_Xword;
27076b1902SAdrien Destuguestypedef int64 Elf64_Sxword;
28076b1902SAdrien Destugues
29076b1902SAdrien Destuguestypedef Elf64_Half Elf64_Versym;
30076b1902SAdrien Destugues
31076b1902SAdrien Destugues
32076b1902SAdrien Destugues/*** ELF header ***/
33076b1902SAdrien Destugues
34076b1902SAdrien Destugues#define EI_NIDENT	16
35076b1902SAdrien Destugues
36076b1902SAdrien Destuguestypedef struct {
37076b1902SAdrien Destugues	uint8		e_ident[EI_NIDENT];
38076b1902SAdrien Destugues	Elf32_Half	e_type;
39076b1902SAdrien Destugues	Elf32_Half	e_machine;
40076b1902SAdrien Destugues	Elf32_Word	e_version;
41076b1902SAdrien Destugues	Elf32_Addr	e_entry;
42076b1902SAdrien Destugues	Elf32_Off	e_phoff;
43076b1902SAdrien Destugues	Elf32_Off	e_shoff;
44076b1902SAdrien Destugues	Elf32_Word	e_flags;
45076b1902SAdrien Destugues	Elf32_Half	e_ehsize;
46076b1902SAdrien Destugues	Elf32_Half	e_phentsize;
47076b1902SAdrien Destugues	Elf32_Half	e_phnum;
48076b1902SAdrien Destugues	Elf32_Half	e_shentsize;
49076b1902SAdrien Destugues	Elf32_Half	e_shnum;
50076b1902SAdrien Destugues	Elf32_Half	e_shstrndx;
51076b1902SAdrien Destugues
52076b1902SAdrien Destugues#ifdef __cplusplus
53076b1902SAdrien Destugues	bool IsHostEndian() const;
54076b1902SAdrien Destugues#endif
55076b1902SAdrien Destugues} Elf32_Ehdr;
56076b1902SAdrien Destugues
57076b1902SAdrien Destuguestypedef struct {
58076b1902SAdrien Destugues	uint8		e_ident[EI_NIDENT];
59076b1902SAdrien Destugues	Elf64_Half	e_type;
60076b1902SAdrien Destugues	Elf64_Half	e_machine;
61076b1902SAdrien Destugues	Elf64_Word	e_version;
62076b1902SAdrien Destugues	Elf64_Addr	e_entry;
63076b1902SAdrien Destugues	Elf64_Off	e_phoff;
64076b1902SAdrien Destugues	Elf64_Off	e_shoff;
65076b1902SAdrien Destugues	Elf64_Word	e_flags;
66076b1902SAdrien Destugues	Elf64_Half	e_ehsize;
67076b1902SAdrien Destugues	Elf64_Half	e_phentsize;
68076b1902SAdrien Destugues	Elf64_Half	e_phnum;
69076b1902SAdrien Destugues	Elf64_Half	e_shentsize;
70076b1902SAdrien Destugues	Elf64_Half	e_shnum;
71076b1902SAdrien Destugues	Elf64_Half	e_shstrndx;
72076b1902SAdrien Destugues
73076b1902SAdrien Destugues#ifdef __cplusplus
74076b1902SAdrien Destugues	bool IsHostEndian() const;
75076b1902SAdrien Destugues#endif
76076b1902SAdrien Destugues} Elf64_Ehdr;
77076b1902SAdrien Destugues
78076b1902SAdrien Destugues/* e_ident[] indices */
79076b1902SAdrien Destugues#define EI_MAG0		0
80076b1902SAdrien Destugues#define EI_MAG1		1
81076b1902SAdrien Destugues#define EI_MAG2		2
82076b1902SAdrien Destugues#define EI_MAG3		3
83076b1902SAdrien Destugues#define EI_CLASS	4
84076b1902SAdrien Destugues#define EI_DATA		5
85076b1902SAdrien Destugues#define EI_VERSION	6
86076b1902SAdrien Destugues#define EI_PAD		7
87076b1902SAdrien Destugues
88076b1902SAdrien Destugues/* Values for the magic number bytes. */
89076b1902SAdrien Destugues#define ELFMAG0		0x7f
90076b1902SAdrien Destugues#define ELFMAG1		'E'
91076b1902SAdrien Destugues#define ELFMAG2		'L'
92076b1902SAdrien Destugues#define ELFMAG3		'F'
93076b1902SAdrien Destugues#define ELFMAG		"\x7f""ELF"
94076b1902SAdrien Destugues#define SELFMAG		4
95076b1902SAdrien Destugues
96076b1902SAdrien Destugues/* e_ident */
97076b1902SAdrien Destugues#define IS_ELF(ehdr)    ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
98076b1902SAdrien Destugues	(ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
99076b1902SAdrien Destugues	(ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
100076b1902SAdrien Destugues	(ehdr).e_ident[EI_MAG3] == ELFMAG3)
101076b1902SAdrien Destugues
102076b1902SAdrien Destugues/* e_type (Object file type) */
103076b1902SAdrien Destugues#define ET_NONE			0 /* No file type */
104076b1902SAdrien Destugues#define ET_REL			1 /* Relocatable file */
105076b1902SAdrien Destugues#define ET_EXEC			2 /* Executable file */
106076b1902SAdrien Destugues#define ET_DYN			3 /* Shared-object file */
107076b1902SAdrien Destugues#define ET_CORE			4 /* Core file */
108076b1902SAdrien Destugues#define ET_LOOS			0xfe00 /* OS-specific range start */
109076b1902SAdrien Destugues#define ET_HIOS			0xfeff /* OS-specific range end */
110076b1902SAdrien Destugues#define ET_LOPROC		0xff00 /* Processor-specific range start */
111076b1902SAdrien Destugues#define ET_HIPROC		0xffff /* Processor-specific range end */
112076b1902SAdrien Destugues
113076b1902SAdrien Destugues/* e_machine (Architecture) */
114076b1902SAdrien Destugues#define EM_NONE			0 /* No machine */
115076b1902SAdrien Destugues#define EM_M32			1 /* AT&T WE 32100 */
116076b1902SAdrien Destugues#define EM_SPARC		2 /* Sparc */
117076b1902SAdrien Destugues#define EM_386			3 /* Intel 80386 */
118076b1902SAdrien Destugues#define EM_68K			4 /* Motorola m68k family */
119076b1902SAdrien Destugues#define EM_88K			5 /* Motorola m88k family */
120076b1902SAdrien Destugues#define EM_486			6 /* Intel 80486, Reserved for future use */
121076b1902SAdrien Destugues#define EM_860			7 /* Intel 80860 */
122076b1902SAdrien Destugues#define EM_MIPS			8 /* MIPS R3000 (officially, big-endian only) */
123076b1902SAdrien Destugues#define EM_S370			9 /* IBM System/370 */
124076b1902SAdrien Destugues#define EM_MIPS_RS3_LE	10 /* MIPS R3000 little-endian, Deprecated */
125076b1902SAdrien Destugues#define EM_PARISC		15 /* HPPA */
126076b1902SAdrien Destugues#define EM_VPP550		17 /* Fujitsu VPP500 */
127076b1902SAdrien Destugues#define EM_SPARC32PLUS	18 /* Sun "v8plus" */
128076b1902SAdrien Destugues#define EM_960			19 /* Intel 80960 */
129076b1902SAdrien Destugues#define EM_PPC			20 /* PowerPC */
130076b1902SAdrien Destugues#define EM_PPC64		21 /* 64-bit PowerPC */
131076b1902SAdrien Destugues#define EM_S390			22 /* IBM S/390 */
132076b1902SAdrien Destugues#define EM_V800			36 /* NEC V800 series */
133076b1902SAdrien Destugues#define EM_FR20			37 /* Fujitsu FR20 */
134076b1902SAdrien Destugues#define EM_RH32			38 /* TRW RH32 */
135076b1902SAdrien Destugues#define EM_MCORE		39 /* Motorola M*Core */
136076b1902SAdrien Destugues#define EM_RCE			39 /* Old name for MCore */
137076b1902SAdrien Destugues#define EM_ARM			40 /* ARM */
138076b1902SAdrien Destugues#define EM_OLD_ALPHA	41 /* Digital Alpha */
139076b1902SAdrien Destugues#define EM_SH			42 /* Renesas / SuperH SH */
140076b1902SAdrien Destugues#define EM_SPARCV9		43 /* SPARC v9 64-bit */
141076b1902SAdrien Destugues#define EM_TRICORE		44 /* Siemens Tricore embedded processor */
142076b1902SAdrien Destugues#define EM_ARC			45 /* ARC Cores */
143076b1902SAdrien Destugues#define EM_H8_300		46 /* Renesas H8/300 */
144076b1902SAdrien Destugues#define EM_H8_300H		47 /* Renesas H8/300H */
145076b1902SAdrien Destugues#define EM_H8S			48 /* Renesas H8S */
146076b1902SAdrien Destugues#define EM_H8_500		49 /* Renesas H8/500 */
147076b1902SAdrien Destugues#define EM_IA_64		50 /* Intel IA-64 Processor */
148076b1902SAdrien Destugues#define EM_MIPS_X		51 /* Stanford MIPS-X */
149076b1902SAdrien Destugues#define EM_COLDFIRE		52 /* Motorola Coldfire */
150076b1902SAdrien Destugues#define EM_68HC12		53 /* Motorola M68HC12 */
151076b1902SAdrien Destugues#define EM_MMA			54 /* Fujitsu Multimedia Accelerator */
152076b1902SAdrien Destugues#define EM_PCP			55 /* Siemens PCP */
153076b1902SAdrien Destugues#define EM_NCPU			56 /* Sony nCPU embedded RISC processor */
154076b1902SAdrien Destugues#define EM_NDR1			57 /* Denso NDR1 microprocesspr */
155076b1902SAdrien Destugues#define EM_STARCORE		58 /* Motorola Star*Core processor */
156076b1902SAdrien Destugues#define EM_ME16			59 /* Toyota ME16 processor */
157076b1902SAdrien Destugues#define EM_ST100		60 /* STMicroelectronics ST100 processor */
158076b1902SAdrien Destugues#define EM_TINYJ		61 /* Advanced Logic Corp. TinyJ embedded processor */
159076b1902SAdrien Destugues#define EM_X86_64		62 /* Advanced Micro Devices X86-64 processor */
160076b1902SAdrien Destugues#define EM_PDSP			63 /* Sony DSP Processor */
161076b1902SAdrien Destugues#define EM_FX66			66 /* Siemens FX66 microcontroller */
162076b1902SAdrien Destugues#define EM_ST9PLUS		67 /* STMicroelectronics ST9+ 8/16 mc */
163076b1902SAdrien Destugues#define EM_ST7			68 /* STmicroelectronics ST7 8 bit mc */
164076b1902SAdrien Destugues#define EM_68HC16		69 /* Motorola MC68HC16 microcontroller */
165076b1902SAdrien Destugues#define EM_68HC11		70 /* Motorola MC68HC11 microcontroller */
166076b1902SAdrien Destugues#define EM_68HC08		71 /* Motorola MC68HC08 microcontroller */
167076b1902SAdrien Destugues#define EM_68HC05		72 /* Motorola MC68HC05 microcontroller */
168076b1902SAdrien Destugues#define EM_SVX			73 /* Silicon Graphics SVx */
169076b1902SAdrien Destugues#define EM_ST19			74 /* STMicroelectronics ST19 8 bit mc */
170076b1902SAdrien Destugues#define EM_VAX			75 /* Digital VAX */
171076b1902SAdrien Destugues#define EM_CRIS			76 /* Axis Communications 32-bit embedded processor */
172076b1902SAdrien Destugues#define EM_JAVELIN		77 /* Infineon Technologies 32-bit embedded processor */
173076b1902SAdrien Destugues#define EM_FIREPATH		78 /* Element 14 64-bit DSP Processor */
174076b1902SAdrien Destugues#define EM_ZSP			79 /* LSI Logic 16-bit DSP Processor */
175076b1902SAdrien Destugues#define EM_MMIX			80 /* Donald Knuth's educational 64-bit processor */
176076b1902SAdrien Destugues#define EM_HUANY		81 /* Harvard University machine-independent object files */
177076b1902SAdrien Destugues#define EM_PRISM		82 /* SiTera Prism */
178076b1902SAdrien Destugues#define EM_AVR			83 /* Atmel AVR 8-bit microcontroller */
179076b1902SAdrien Destugues#define EM_FR30			84 /* Fujitsu FR30 */
180076b1902SAdrien Destugues#define EM_D10V			85 /* Mitsubishi D10V */
181076b1902SAdrien Destugues#define EM_D30V			86 /* Mitsubishi D30V */
182076b1902SAdrien Destugues#define EM_V850			87 /* NEC v850 */
183076b1902SAdrien Destugues#define EM_M32R			88 /* Mitsubishi M32R */
184076b1902SAdrien Destugues#define EM_MN10300		89 /* Matsushita MN10300 */
185076b1902SAdrien Destugues#define EM_MN10200		90 /* Matsushita MN10200 */
186076b1902SAdrien Destugues#define EM_PJ			91 /* picoJava */
187076b1902SAdrien Destugues#define EM_OPENRISC		92 /* OpenRISC 32-bit embedded processor */
188076b1902SAdrien Destugues#define EM_ARC_A5		93 /* ARC Cores Tangent-A5 */
189076b1902SAdrien Destugues#define EM_XTENSA		94 /* Tensilica Xtensa Architecture */
190076b1902SAdrien Destugues#define EM_VIDCORE		95 /* Alphamosaic VideoCore */
191076b1902SAdrien Destugues#define EM_CR			103 /* Nat. Semi. CompactRISC microprocessor */
192076b1902SAdrien Destugues#define EM_BLACKFIN		106 /* Analog Devices Blackfin (DSP) processor */
193076b1902SAdrien Destugues#define EM_ARCA			109 /* Arca RISC Microprocessor */
194076b1902SAdrien Destugues#define EM_VIDCORE3		137 /* Broadcom VideoCore III */
195076b1902SAdrien Destugues#define EM_ARM64		183 /* ARM 64 bit */
196076b1902SAdrien Destugues#define EM_AARCH64		EM_ARM64
197076b1902SAdrien Destugues#define EM_AVR32		185 /* AVR-32 */
198076b1902SAdrien Destugues#define EM_STM8			186 /* ST STM8S */
199076b1902SAdrien Destugues#define EM_CUDA			190 /* Nvidia CUDA */
200076b1902SAdrien Destugues#define EM_VIDCORE5		198 /* Broadcom VideoCore V */
201076b1902SAdrien Destugues#define EM_NORC			218 /* Nanoradio Optimized RISC */
202076b1902SAdrien Destugues#define EM_AMDGPU		224 /* AMD GPU */
203076b1902SAdrien Destugues#define EM_RISCV		243 /* RISC-V */
204076b1902SAdrien Destugues#define EM_BPF			247 /* Linux kernel bpf virtual machine */
205076b1902SAdrien Destugues
206076b1902SAdrien Destugues#define EM_ALPHA        0x9026 /* Digital Alpha (nonstandard, but the value
207076b1902SAdrien Destugues								  everyone uses) */
208076b1902SAdrien Destugues
209076b1902SAdrien Destugues/* architecture class (EI_CLASS) */
210076b1902SAdrien Destugues#define ELFCLASSNONE	0
211076b1902SAdrien Destugues#define ELFCLASS32		1
212076b1902SAdrien Destugues#define ELFCLASS64		2
213076b1902SAdrien Destugues
214076b1902SAdrien Destugues/* endian (EI_DATA) */
215076b1902SAdrien Destugues#define ELFDATANONE	0	/* invalid */
216076b1902SAdrien Destugues#define ELFDATA2LSB	1	/* little endian */
217076b1902SAdrien Destugues#define ELFDATA2MSB	2	/* big endian */
218076b1902SAdrien Destugues
219076b1902SAdrien Destugues/* ELF version (EI_VERSION) */
220076b1902SAdrien Destugues#define EV_NONE		0	/* invalid */
221076b1902SAdrien Destugues#define EV_CURRENT	1	/* current version */
222076b1902SAdrien Destugues
223076b1902SAdrien Destugues/*** section header ***/
224076b1902SAdrien Destugues
225076b1902SAdrien Destuguestypedef struct {
226076b1902SAdrien Destugues	Elf32_Word	sh_name;
227076b1902SAdrien Destugues	Elf32_Word	sh_type;
228076b1902SAdrien Destugues	Elf32_Word	sh_flags;
229076b1902SAdrien Destugues	Elf32_Addr	sh_addr;
230076b1902SAdrien Destugues	Elf32_Off	sh_offset;
231076b1902SAdrien Destugues	Elf32_Word	sh_size;
232076b1902SAdrien Destugues	Elf32_Word	sh_link;
233076b1902SAdrien Destugues	Elf32_Word	sh_info;
234076b1902SAdrien Destugues	Elf32_Word	sh_addralign;
235076b1902SAdrien Destugues	Elf32_Word	sh_entsize;
236076b1902SAdrien Destugues} Elf32_Shdr;
237076b1902SAdrien Destugues
238076b1902SAdrien Destuguestypedef struct {
239076b1902SAdrien Destugues	Elf64_Word	sh_name;
240076b1902SAdrien Destugues	Elf64_Word	sh_type;
241076b1902SAdrien Destugues	Elf64_Xword	sh_flags;
242076b1902SAdrien Destugues	Elf64_Addr	sh_addr;
243076b1902SAdrien Destugues	Elf64_Off	sh_offset;
244076b1902SAdrien Destugues	Elf64_Xword	sh_size;
245076b1902SAdrien Destugues	Elf64_Word	sh_link;
246076b1902SAdrien Destugues	Elf64_Word	sh_info;
247076b1902SAdrien Destugues	Elf64_Xword	sh_addralign;
248076b1902SAdrien Destugues	Elf64_Xword	sh_entsize;
249076b1902SAdrien Destugues} Elf64_Shdr;
250076b1902SAdrien Destugues
251076b1902SAdrien Destugues/* special section indices */
252076b1902SAdrien Destugues#define SHN_UNDEF		0
253076b1902SAdrien Destugues#define SHN_LORESERVE	0xff00
254076b1902SAdrien Destugues#define SHN_LOPROC		0xff00
255076b1902SAdrien Destugues#define SHN_HIPROC		0xff1f
256076b1902SAdrien Destugues#define SHN_ABS			0xfff1
257076b1902SAdrien Destugues#define SHN_COMMON		0xfff2
258076b1902SAdrien Destugues#define SHN_HIRESERVE	0xffff
259076b1902SAdrien Destugues
260076b1902SAdrien Destugues/* section header type */
261076b1902SAdrien Destugues#define SHT_NULL		0
262076b1902SAdrien Destugues#define SHT_PROGBITS	1
263076b1902SAdrien Destugues#define SHT_SYMTAB		2
264076b1902SAdrien Destugues#define SHT_STRTAB		3
265076b1902SAdrien Destugues#define SHT_RELA		4
266076b1902SAdrien Destugues#define