ScummVM API documentation
elf32.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#ifndef BACKENDS_ELF_H
23#define BACKENDS_ELF_H
24
25#include "common/scummsys.h"
26
27#if defined(DYNAMIC_MODULES) && defined(USE_ELF_LOADER)
28
35typedef uint16 Elf32_Half, Elf32_Section;
36typedef uint32 Elf32_Word, Elf32_Addr, Elf32_Off;
37typedef int32 Elf32_Sword;
38typedef Elf32_Half Elf32_Versym;
39
40#define EI_NIDENT (16)
41#define SELFMAG 4
42
43/* ELF File format structures. Look up ELF structure for more details */
44
45// ELF header (contains info about the file)
46typedef struct {
47 byte e_ident[EI_NIDENT]; /* Magic number and other info */
48 Elf32_Half e_type; /* Object file type */
49 Elf32_Half e_machine; /* Architecture */
50 Elf32_Word e_version; /* Object file version */
51 Elf32_Addr e_entry; /* Entry point virtual address */
52 Elf32_Off e_phoff; /* Program header table file offset */
53 Elf32_Off e_shoff; /* Section header table file offset */
54 Elf32_Word e_flags; /* Processor-specific flags */
55 Elf32_Half e_ehsize; /* ELF header size in bytes */
56 Elf32_Half e_phentsize; /* Program header table entry size */
57 Elf32_Half e_phnum; /* Program header table entry count */
58 Elf32_Half e_shentsize; /* Section header table entry size */
59 Elf32_Half e_shnum; /* Section header table entry count */
60 Elf32_Half e_shstrndx; /* Section header string table index */
61} Elf32_Ehdr;
62
63// Should be in e_ident
64#define ELFMAG "\177ELF" /* ELF Magic number */
65
66#define EI_CLASS 4 /* File class byte index */
67#define ELFCLASS32 1 /* 32-bit objects */
68
69#define EI_DATA 5 /* Data encoding byte index */
70#define ELFDATA2LSB 1 /* 2's complement, little endian */
71#define ELFDATA2MSB 2 /* 2's complement, big endian */
72
73#define EI_VERSION 6
74#define EV_CURRENT 1 /* Current version */
75
76// e_type values
77#define ET_NONE 0 /* no file type */
78#define ET_REL 1 /* relocatable */
79#define ET_EXEC 2 /* executable */
80#define ET_DYN 3 /* shared object */
81#define ET_CORE 4 /* core file */
82
83// e_machine values
84#define EM_68K 4
85#define EM_MIPS 8
86#define EM_PPC 20
87#define EM_ARM 40
88#define EM_SH 42
89
90// Program header (contains info about segment)
91typedef struct {
92 Elf32_Word p_type; /* Segment type */
93 Elf32_Off p_offset; /* Segment file offset */
94 Elf32_Addr p_vaddr; /* Segment virtual address */
95 Elf32_Addr p_paddr; /* Segment physical address */
96 Elf32_Word p_filesz; /* Segment size in file */
97 Elf32_Word p_memsz; /* Segment size in memory */
98 Elf32_Word p_flags; /* Segment flags */
99 Elf32_Word p_align; /* Segment alignment */
100} Elf32_Phdr;
101
102// p_type values
103#define PT_NULL 0 /* ignored */
104#define PT_LOAD 1 /* loadable segment */
105#define PT_DYNAMIC 2 /* dynamic linking info */
106#define PT_INTERP 3 /* info about interpreter */
107#define PT_NOTE 4 /* note segment */
108#define PT_SHLIB 5 /* reserved */
109#define PT_PHDR 6 /* Program header table */
110#define PT_MIPS_REGINFO 0x70000000 /* Register usage info for MIPS */
111#define PT_ARM_ARCHEXT 0x70000000 /* Platform architecture compatibility info for ARM */
112#define PT_ARM_EXIDX 0x70000001 /* Exception unwind tables for ARM */
113
114// p_flags value
115#define PF_X 1 /* execute */
116#define PF_W 2 /* write */
117#define PF_R 4 /* read */
118
119// Section header (contains info about section)
120typedef struct {
121 Elf32_Word sh_name; /* Section name (string tbl index) */
122 Elf32_Word sh_type; /* Section type */
123 Elf32_Word sh_flags; /* Section flags */
124 Elf32_Addr sh_addr; /* Section virtual addr at execution */
125 Elf32_Off sh_offset; /* Section file offset */
126 Elf32_Word sh_size; /* Section size in bytes */
127 Elf32_Word sh_link; /* Link to another section */
128 Elf32_Word sh_info; /* Additional section information */
129 Elf32_Word sh_addralign; /* Section alignment */
130 Elf32_Word sh_entsize; /* Entry size if section holds table */
131} Elf32_Shdr;
132
133// sh_type values
134#define SHT_NULL 0 /* Inactive section */
135#define SHT_PROGBITS 1 /* Proprietary */
136#define SHT_SYMTAB 2 /* Symbol table */
137#define SHT_STRTAB 3 /* String table */
138#define SHT_RELA 4 /* Relocation entries with addend */
139#define SHT_HASH 5 /* Symbol hash table */
140#define SHT_DYNAMIC 6 /* Info for dynamic linking */
141#define SHT_NOTE 7 /* Note section */
142#define SHT_NOBITS 8 /* Occupies no space */
143#define SHT_REL 9 /* Relocation entries without addend */
144#define SHT_SHLIB 10 /* Reserved */
145#define SHT_DYNSYM 11 /* Minimal set of dynamic linking symbols */
146#define SHT_MIPS_LIBLSIT 0x70000000 /* Info about dynamic shared object libs for MIPS*/
147#define SHT_MIPS_CONFLICT 0x70000002 /* Conflicts btw executables and shared objects for MIPS */
148#define SHT_MIPS_GPTAB 0x70000003 /* Global pointer table for MIPS*/
149#define SHT_ARM_EXIDX 0x70000001 /* Exception Index table for ARM*/
150#define SHT_ARM_PREEMPTMAP 0x70000002 /* BPABI DLL dynamic linking pre-emption map for ARM */
151#define SHT_ARM_ATTRIBUTES 0x70000003 /* Object file compatibility attributes for ARM*/
152
153// sh_flags values
154#define SHF_WRITE 0 /* writable section */
155#define SHF_ALLOC 2 /* section occupies memory */
156#define SHF_EXECINSTR 4 /* machine instructions */
157#define SHF_MIPS_GPREL 0x10000000 /* Must be made part of global data area for MIPS */
158
159// Symbol entry (contain info about a symbol)
160typedef struct {
161 Elf32_Word st_name; /* Symbol name (string tbl index) */
162 Elf32_Addr st_value; /* Symbol value */
163 Elf32_Word st_size; /* Symbol size */
164 byte st_info; /* Symbol type and binding */
165 byte st_other; /* Symbol visibility */
166 Elf32_Section st_shndx; /* Section index */
167} Elf32_Sym;
168
169// Extract from the st_info
170#define SYM_TYPE(x) ((x) & 0xf)
171#define SYM_BIND(x) ((x) >> 4)
172
173// Symbol binding values from st_info
174#define STB_LOCAL 0 /* Symbol not visible outside object */
175#define STB_GLOBAL 1 /* Symbol visible to all object files */
176#define STB_WEAK 2 /* Similar to STB_GLOBAL */
177
178// Symbol type values from st_info
179#define STT_NOTYPE 0 /* Not specified */
180#define STT_OBJECT 1 /* Data object e.g. variable */
181#define STT_FUNC 2 /* Function */
182#define STT_SECTION 3 /* Section */
183#define STT_FILE 4 /* Source file associated with object file */
184
185// Special section header index values from st_shndex
186#define SHN_UNDEF 0
187#define SHN_LOPROC 0xFF00 /* Extended values */
188#define SHN_ABS 0xFFF1 /* Absolute value: don't relocate */
189#define SHN_COMMON 0xFFF2 /* Common block. Not allocated yet */
190#define SHN_HIPROC 0xFF1F
191#define SHN_HIRESERVE 0xFFFF
192
193// Relocation entry with implicit addend (info about how to relocate)
194typedef struct {
195 Elf32_Addr r_offset; /* Address */
196 Elf32_Word r_info; /* Relocation type and symbol index */
197} Elf32_Rel;
198
199// Relocation entry with explicit addend (info about how to relocate)
200typedef struct {
201 Elf32_Addr r_offset; /* Address */
202 Elf32_Word r_info; /* Relocation type and symbol index */
203 Elf32_Sword r_addend; /* Addend */
204} Elf32_Rela;
205
206// Access macros for the relocation info
207#define REL_TYPE(x) ((byte) (x)) /* Extract relocation type */
208#define REL_INDEX(x) ((x)>>8) /* Extract relocation index into symbol table */
209
210//MIPS relocation types
211#define R_MIPS_NONE 0
212#define R_MIPS_16 1
213#define R_MIPS_32 2
214#define R_MIPS_REL32 3
215#define R_MIPS_26 4
216#define R_MIPS_HI16 5
217#define R_MIPS_LO16 6
218#define R_MIPS_GPREL16 7
219#define R_MIPS_LITERAL 8
220#define R_MIPS_GOT16 9
221#define R_MIPS_PC16 10
222#define R_MIPS_CALL16 11
223#define R_MIPS_GPREL32 12
224#define R_MIPS_GOTHI16 13
225#define R_MIPS_GOTLO16 14
226#define R_MIPS_CALLHI16 15
227#define R_MIPS_CALLLO16 16
228
229// ARM relocation types
230#define R_ARM_NONE 0
231#define R_ARM_PC24 1
232#define R_ARM_ABS32 2
233#define R_ARM_THM_CALL 10
234#define R_ARM_CALL 28
235#define R_ARM_JUMP24 29
236#define R_ARM_TARGET1 38
237#define R_ARM_V4BX 40
238#define R_ARM_PREL31 42
239
240// PPC relocation types
241#define R_PPC_NONE 0
242#define R_PPC_ADDR32 1
243#define R_PPC_ADDR16_LO 4
244#define R_PPC_ADDR16_HI 5
245#define R_PPC_ADDR16_HA 6
246#define R_PPC_REL24 10
247#define R_PPC_REL32 26
248
249// m68k relocation types
250#define R_68K_NONE 0
251#define R_68K_32 1
252#define R_68K_16 2
253#define R_68K_8 3
254#define R_68K_PC32 4
255#define R_68K_PC16 5
256#define R_68K_PC8 6
257
258#endif // defined(DYNAMIC_MODULES) && defined(USE_ELF_LOADER)
259
260#endif /* BACKENDS_ELF_H */