ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
35 typedef uint16 Elf32_Half, Elf32_Section;
36 typedef uint32 Elf32_Word, Elf32_Addr, Elf32_Off;
37 typedef int32 Elf32_Sword;
38 typedef 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)
46 typedef 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_MIPS 8
85 #define EM_PPC 20
86 #define EM_ARM 40
87 #define EM_SH 42
88 
89 // Program header (contains info about segment)
90 typedef struct {
91  Elf32_Word p_type; /* Segment type */
92  Elf32_Off p_offset; /* Segment file offset */
93  Elf32_Addr p_vaddr; /* Segment virtual address */
94  Elf32_Addr p_paddr; /* Segment physical address */
95  Elf32_Word p_filesz; /* Segment size in file */
96  Elf32_Word p_memsz; /* Segment size in memory */
97  Elf32_Word p_flags; /* Segment flags */
98  Elf32_Word p_align; /* Segment alignment */
99 } Elf32_Phdr;
100 
101 // p_type values
102 #define PT_NULL 0 /* ignored */
103 #define PT_LOAD 1 /* loadable segment */
104 #define PT_DYNAMIC 2 /* dynamic linking info */
105 #define PT_INTERP 3 /* info about interpreter */
106 #define PT_NOTE 4 /* note segment */
107 #define PT_SHLIB 5 /* reserved */
108 #define PT_PHDR 6 /* Program header table */
109 #define PT_MIPS_REGINFO 0x70000000 /* Register usage info for MIPS */
110 #define PT_ARM_ARCHEXT 0x70000000 /* Platform architecture compatibility info for ARM */
111 #define PT_ARM_EXIDX 0x70000001 /* Exception unwind tables for ARM */
112 
113 // p_flags value
114 #define PF_X 1 /* execute */
115 #define PF_W 2 /* write */
116 #define PF_R 4 /* read */
117 
118 // Section header (contains info about section)
119 typedef struct {
120  Elf32_Word sh_name; /* Section name (string tbl index) */
121  Elf32_Word sh_type; /* Section type */
122  Elf32_Word sh_flags; /* Section flags */
123  Elf32_Addr sh_addr; /* Section virtual addr at execution */
124  Elf32_Off sh_offset; /* Section file offset */
125  Elf32_Word sh_size; /* Section size in bytes */
126  Elf32_Word sh_link; /* Link to another section */
127  Elf32_Word sh_info; /* Additional section information */
128  Elf32_Word sh_addralign; /* Section alignment */
129  Elf32_Word sh_entsize; /* Entry size if section holds table */
130 } Elf32_Shdr;
131 
132 // sh_type values
133 #define SHT_NULL 0 /* Inactive section */
134 #define SHT_PROGBITS 1 /* Proprietary */
135 #define SHT_SYMTAB 2 /* Symbol table */
136 #define SHT_STRTAB 3 /* String table */
137 #define SHT_RELA 4 /* Relocation entries with addend */
138 #define SHT_HASH 5 /* Symbol hash table */
139 #define SHT_DYNAMIC 6 /* Info for dynamic linking */
140 #define SHT_NOTE 7 /* Note section */
141 #define SHT_NOBITS 8 /* Occupies no space */
142 #define SHT_REL 9 /* Relocation entries without addend */
143 #define SHT_SHLIB 10 /* Reserved */
144 #define SHT_DYNSYM 11 /* Minimal set of dynamic linking symbols */
145 #define SHT_MIPS_LIBLSIT 0x70000000 /* Info about dynamic shared object libs for MIPS*/
146 #define SHT_MIPS_CONFLICT 0x70000002 /* Conflicts btw executables and shared objects for MIPS */
147 #define SHT_MIPS_GPTAB 0x70000003 /* Global pointer table for MIPS*/
148 #define SHT_ARM_EXIDX 0x70000001 /* Exception Index table for ARM*/
149 #define SHT_ARM_PREEMPTMAP 0x70000002 /* BPABI DLL dynamic linking pre-emption map for ARM */
150 #define SHT_ARM_ATTRIBUTES 0x70000003 /* Object file compatibility attributes for ARM*/
151 
152 // sh_flags values
153 #define SHF_WRITE 0 /* writable section */
154 #define SHF_ALLOC 2 /* section occupies memory */
155 #define SHF_EXECINSTR 4 /* machine instructions */
156 #define SHF_MIPS_GPREL 0x10000000 /* Must be made part of global data area for MIPS */
157 
158 // Symbol entry (contain info about a symbol)
159 typedef struct {
160  Elf32_Word st_name; /* Symbol name (string tbl index) */
161  Elf32_Addr st_value; /* Symbol value */
162  Elf32_Word st_size; /* Symbol size */
163  byte st_info; /* Symbol type and binding */
164  byte st_other; /* Symbol visibility */
165  Elf32_Section st_shndx; /* Section index */
166 } Elf32_Sym;
167 
168 // Extract from the st_info
169 #define SYM_TYPE(x) ((x) & 0xf)
170 #define SYM_BIND(x) ((x) >> 4)
171 
172 // Symbol binding values from st_info
173 #define STB_LOCAL 0 /* Symbol not visible outside object */
174 #define STB_GLOBAL 1 /* Symbol visible to all object files */
175 #define STB_WEAK 2 /* Similar to STB_GLOBAL */
176 
177 // Symbol type values from st_info
178 #define STT_NOTYPE 0 /* Not specified */
179 #define STT_OBJECT 1 /* Data object e.g. variable */
180 #define STT_FUNC 2 /* Function */
181 #define STT_SECTION 3 /* Section */
182 #define STT_FILE 4 /* Source file associated with object file */
183 
184 // Special section header index values from st_shndex
185 #define SHN_UNDEF 0
186 #define SHN_LOPROC 0xFF00 /* Extended values */
187 #define SHN_ABS 0xFFF1 /* Absolute value: don't relocate */
188 #define SHN_COMMON 0xFFF2 /* Common block. Not allocated yet */
189 #define SHN_HIPROC 0xFF1F
190 #define SHN_HIRESERVE 0xFFFF
191 
192 // Relocation entry with implicit addend (info about how to relocate)
193 typedef struct {
194  Elf32_Addr r_offset; /* Address */
195  Elf32_Word r_info; /* Relocation type and symbol index */
196 } Elf32_Rel;
197 
198 // Relocation entry with explicit addend (info about how to relocate)
199 typedef struct {
200  Elf32_Addr r_offset; /* Address */
201  Elf32_Word r_info; /* Relocation type and symbol index */
202  Elf32_Sword r_addend; /* Addend */
203 } Elf32_Rela;
204 
205 // Access macros for the relocation info
206 #define REL_TYPE(x) ((byte) (x)) /* Extract relocation type */
207 #define REL_INDEX(x) ((x)>>8) /* Extract relocation index into symbol table */
208 
209 //MIPS relocation types
210 #define R_MIPS_NONE 0
211 #define R_MIPS_16 1
212 #define R_MIPS_32 2
213 #define R_MIPS_REL32 3
214 #define R_MIPS_26 4
215 #define R_MIPS_HI16 5
216 #define R_MIPS_LO16 6
217 #define R_MIPS_GPREL16 7
218 #define R_MIPS_LITERAL 8
219 #define R_MIPS_GOT16 9
220 #define R_MIPS_PC16 10
221 #define R_MIPS_CALL16 11
222 #define R_MIPS_GPREL32 12
223 #define R_MIPS_GOTHI16 13
224 #define R_MIPS_GOTLO16 14
225 #define R_MIPS_CALLHI16 15
226 #define R_MIPS_CALLLO16 16
227 
228 // ARM relocation types
229 #define R_ARM_NONE 0
230 #define R_ARM_PC24 1
231 #define R_ARM_ABS32 2
232 #define R_ARM_THM_CALL 10
233 #define R_ARM_CALL 28
234 #define R_ARM_JUMP24 29
235 #define R_ARM_TARGET1 38
236 #define R_ARM_V4BX 40
237 #define R_ARM_PREL31 42
238 
239 // PPC relocation types
240 #define R_PPC_NONE 0
241 #define R_PPC_ADDR32 1
242 #define R_PPC_ADDR16_LO 4
243 #define R_PPC_ADDR16_HI 5
244 #define R_PPC_ADDR16_HA 6
245 #define R_PPC_REL24 10
246 #define R_PPC_REL32 26
247 
248 #endif // defined(DYNAMIC_MODULES) && defined(USE_ELF_LOADER)
249 
250 #endif /* BACKENDS_ELF_H */