KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
elf.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 kos/elf.h
4 Copyright (C)2000,2001,2003 Megan Potter
5
6*/
7
8/** \file kos/elf.h
9 \brief ELF binary loading support.
10 \ingroup elf
11
12 This file contains the support functionality for loading ELF binaries in
13 KOS. This includes the various header structures and whatnot that are used
14 in ELF files to store code/data/relocations/etc. This isn't necessarily
15 meant for running multiple processes, but more for loadable library support
16 within KOS.
17
18 \author Megan Potter
19*/
20
21#ifndef __KOS_ELF_H
22#define __KOS_ELF_H
23
24#include <kos/cdefs.h>
25__BEGIN_DECLS
26
27#include <stdint.h>
28#include <sys/queue.h>
29#include <kos/regfield.h>
30
31/** \defgroup elf ELF File Format
32 \brief API for loading and managing ELF files
33 \ingroup system_libraries
34*/
35
36/** \defgroup elf_ident ELF Identification Bytes
37 \ingroup elf
38
39 Initial bytes of the ELF file, specifying how it should be
40 interpreted. This group contains first the indexes of each
41 ident field, then defines for the values they can contain.
42
43 Some of these are shared by other header fields.
44 @{
45*/
46#define EI_MAG0 0 /**< \brief File identification: 0x7f */
47#define EI_MAG1 1 /**< \brief File identification: 'E' */
48#define EI_MAG2 2 /**< \brief File identification: 'L' */
49#define EI_MAG3 3 /**< \brief File identification: 'F' */
50#define EI_CLASS 4 /**< \brief File class (32/64-bit) */
51#define EI_DATA 5 /**< \brief Data encoding (LSB/MSB) */
52#define EI_VERSION 6 /**< \brief File version (must be 1) */
53#define EI_OSABI 7 /**< \brief Operating System/ABI identification */
54#define EI_ABIVERSION 8 /**< \brief ABI version */
55#define EI_PAD 9 /**< \brief Start of padding bytes */
56
57#define EI_NIDENT 16 /**< \brief Size of elf_hdr::ident */
58
59#define ELFCLASSNONE 0 /**< \brief Invalid class */
60#define ELFCLASS32 1 /**< \brief 32-bit objects */
61#define ELFCLASS64 2 /**< \brief 64-bit objects */
62
63#define ELFDATANONE 0 /**< \brief Invalid encoding */
64#define ELFDATA2LSB 1 /**< \brief 2's complement, little endian */
65#define ELFDATA2MSB 2 /**< \brief 2's complement, big Endian */
66
67#define EV_NONE 0 /**< \brief Invalid version */
68#define EV_CURRENT 1 /**< \brief Current version */
69/** @} */
70
71/** \brief ELF file header.
72 \ingroup elf
73
74 This header is at the beginning of any valid ELF binary and serves to
75 identify the architecture of the binary and various data about it.
76
77*/
78typedef struct elf_hdr {
79 uint8_t ident[EI_NIDENT]; /**< \brief ELF identifier */
80 uint16_t type; /**< \brief ELF file type */
81 uint16_t machine; /**< \brief ELF file architecture */
82 uint32_t version; /**< \brief Object file version */
83 uint32_t entry; /**< \brief Entry point */
84 uint32_t phoff; /**< \brief Program header offset */
85 uint32_t shoff; /**< \brief Section header offset */
86 uint32_t flags; /**< \brief Processor flags */
87 uint16_t ehsize; /**< \brief ELF header size in bytes */
88 uint16_t phentsize; /**< \brief Program header entry size */
89 uint16_t phnum; /**< \brief Program header entry count */
90 uint16_t shentsize; /**< \brief Section header entry size */
91 uint16_t shnum; /**< \brief Section header entry count */
92 uint16_t shstrndx; /**< \brief String table section index */
93} elf_hdr_t;
94
95/** \defgroup elf_archs Architecture Types
96 \brief Relevant ELF architecture type codes
97 \ingroup elf
98
99 These are the various architectures that we might care about for ELF files.
100
101 @{
102*/
103#define EM_386 3 /**< \brief x86 (IA32) */
104#define EM_PPC 20 /**< \brief PowerPC */
105#define EM_ARM 40 /**< \brief ARM */
106#define EM_SH 42 /**< \brief SuperH */
107/** @} */
108
109/** \defgroup elf_sections Section Header Types
110 \brief ELF section header type values
111 \ingroup elf
112
113 These are the various types of section headers that can exist in an ELF
114 file.
115
116 @{
117*/
118#define SHT_NULL 0 /**< \brief Inactive section */
119#define SHT_PROGBITS 1 /**< \brief Program code/data */
120#define SHT_SYMTAB 2 /**< \brief Full symbol table */
121#define SHT_STRTAB 3 /**< \brief String table */
122#define SHT_RELA 4 /**< \brief Relocation table, with addends */
123#define SHT_HASH 5 /**< \brief Symbol hash table */
124#define SHT_DYNAMIC 6 /**< \brief Dynamic linking info */
125#define SHT_NOTE 7 /**< \brief Notes section */
126#define SHT_NOBITS 8 /**< \brief A section that occupies no space in
127the file */
128#define SHT_REL 9 /**< \brief Relocation table, no addends */
129#define SHT_SHLIB 10 /**< \brief Reserved */
130#define SHT_DYNSYM 11 /**< \brief Dynamic linker symbol table */
131#define SHT_INIT_ARRAY 14 /**< \brief Array of constructors */
132#define SHT_FINI_ARRAY 15 /**< \brief Array of destructors */
133#define SHT_PREINIT_ARRAY 16 /**< \brief Array of pre-constructors */
134#define SHT_GROUP 17 /**< \brief Section group */
135#define SHT_SYMTAB_SHNDX 18 /**< \brief Extended section indices */
136#define SHT_NUM 19 /**< \brief Number of defined types. */
137
138#define SHT_LOPROC 0x70000000 /**< \brief Start of processor specific types */
139#define SHT_HIPROC 0x7fffffff /**< \brief End of processor specific types */
140#define SHT_LOUSER 0x80000000 /**< \brief Start of program specific types */
141#define SHT_HIUSER 0xffffffff /**< \brief End of program specific types */
142/** @} */
143
144/** \defgroup elf_hdrflags Section Header Flags
145 \brief ELF section header flags
146 \ingroup elf
147
148 These are the flags that can be set on a section header. These are related
149 to whether the section should reside in memory and permissions on it.
150
151 @{
152*/
153#define SHF_WRITE BIT(0) /**< \brief Writable data */
154#define SHF_ALLOC BIT(1) /**< \brief Resident */
155#define SHF_EXECINSTR BIT(2) /**< \brief Executable instructions */
156#define SHF_MERGE BIT(4) /**< \brief Might be merged */
157#define SHF_STRINGS BIT(5) /**< \brief Contains nul-terminated strings */
158#define SHF_INFO_LINK BIT(6) /**< \brief `sh_info' contains SHT index */
159#define SHF_LINK_ORDER BIT(7) /**< \brief Preserve order after combining */
160#define SHF_GROUP BIT(9) /**< \brief Section is member of a group. */
161#define SHF_TLS BIT(10) /**< \brief Section hold thread-local data. */
162#define SHF_MASKPROC 0xf0000000 /**< \brief Processor specific mask */
163/** @} */
164
165/** \defgroup elf_specsec Special Section Indices
166 \brief ELF section indices
167 \ingroup elf
168
169 These are the indices to be used in special situations in the section array.
170
171 @{
172*/
173#define SHN_UNDEF 0 /**< \brief Undefined, missing, irrelevant */
174#define SHN_ABS 0xfff1 /**< \brief Absolute values */
175/** @} */
176
177/** \brief ELF Section header.
178 \ingroup elf
179
180 This structure represents the header on each ELF section.
181
182 \headerfile kos/elf.h
183*/
184typedef struct elf_shdr {
185 uint32_t name; /**< \brief Index into string table */
186 uint32_t type; /**< \brief Section type \see elf_sections */
187 uint32_t flags; /**< \brief Section flags \see elf_hdrflags */
188 uint32_t addr; /**< \brief In-memory offset */
189 uint32_t offset; /**< \brief On-disk offset */
190 uint32_t size; /**< \brief Size (if SHT_NOBITS, amount of 0s needed) */
191 uint32_t link; /**< \brief Section header table index link */
192 uint32_t info; /**< \brief Section header extra info */
193 uint32_t addralign; /**< \brief Alignment constraints */
194 uint32_t entsize; /**< \brief Fixed-size table entry sizes */
195} elf_shdr_t;
196/* Link and info fields:
197
198switch (sh_type) {
199 case SHT_DYNAMIC:
200 link = section header index of the string table used by
201 the entries in this section
202 info = 0
203 case SHT_HASH:
204 ilnk = section header index of the string table to which
205 this info applies
206 info = 0
207 case SHT_REL, SHT_RELA:
208 link = section header index of associated symbol table
209 info = section header index of section to which reloc applies
210 case SHT_SYMTAB, SHT_DYNSYM:
211 link = section header index of associated string table
212 info = one greater than the symbol table index of the last
213 local symbol (binding STB_LOCAL)
214}
215
216*/
217
218/** \defgroup elf_binding Symbol Binding Types
219 \brief ELF symbol binding type values
220 \ingroup elf
221
222 These are the values that can be set to say how a symbol is bound in an ELF
223 binary. This is stored in the upper 4 bits of the info field in elf_sym_t.
224
225 @{
226*/
227#define STB_LOCAL 0 /**< \brief Local (non-exported) symbol */
228#define STB_GLOBAL 1 /**< \brief Global (exported) symbol */
229#define STB_WEAK 2 /**< \brief Weak-linked symbol */
230/** @} */
231
232/** \defgroup elf_symtype Symbol Types
233 \brief ELF symbol type values
234 \ingroup elf
235
236 These are the values that can be set to say what kind of symbol a given
237 symbol in an ELF file is. This is stored in the lower 4 bits of the info
238 field in elf_sym_t.
239
240 @{
241*/
242#define STT_NOTYPE 0 /**< \brief Symbol has no type */
243#define STT_OBJECT 1 /**< \brief Symbol is an object */
244#define STT_FUNC 2 /**< \brief Symbol is a function */
245#define STT_SECTION 3 /**< \brief Symbol is a section */
246#define STT_FILE 4 /**< \brief Symbol is a file name */
247/** @} */
248
249/** \brief Symbol table entry
250 \ingroup elf
251
252 This structure represents a single entry in a symbol table in an ELF file.
253
254 \headerfile kos/elf.h
255*/
256typedef struct elf_sym {
257 uint32_t name; /**< \brief Index into file's string table */
258 uint32_t value; /**< \brief Value of the symbol */
259 uint32_t size; /**< \brief Size of the symbol */
260 uint8_t info; /**< \brief Symbol type and binding */
261 uint8_t other; /**< \brief 0. Holds no meaning. */
262 uint16_t shndx; /**< \brief Section index */
263} elf_sym_t;
264
265/** \brief Retrieve the binding type for a symbol.
266 \ingroup elf
267
268 \param info The info field of an elf_sym_t.
269 \return The binding type of the symbol.
270 \see elf_binding
271*/
272#define ELF32_ST_BIND(info) ((info) >> 4)
273
274/** \brief Retrieve the symbol type for a symbol.
275 \ingroup elf
276
277 \param info The info field of an elf_sym_t.
278 \return The symbol type of the symbol.
279 \see elf_symtype
280*/
281#define ELF32_ST_TYPE(info) ((info) & 0xf)
282
283/** \brief ELF Relocation entry (with explicit addend).
284 \ingroup elf
285
286 This structure represents an ELF relocation entry with an explicit addend.
287 This structure is used on some architectures, whereas others use the
288 elf_rel_t structure instead.
289
290 \headerfile kos/elf.h
291*/
292typedef struct elf_rela {
293 uint32_t offset; /**< \brief Offset within section */
294 uint32_t info; /**< \brief Symbol and type */
295 int32_t addend; /**< \brief Constant addend for the symbol */
296} elf_rela_t;
297
298/** \brief ELF Relocation entry (without explicit addend).
299 \ingroup elf
300
301 This structure represents an ELF relocation entry without an explicit
302 addend. This structure is used on some architectures, whereas others use the
303 elf_rela_t structure instead.
304
305 \headerfile kos/elf.h
306*/
307typedef struct elf_rel {
308 uint32_t offset; /**< \brief Offset within section */
309 uint32_t info; /**< \brief Symbol and type */
310} elf_rel_t;
311
312/** \defgroup elf_reltypes Relocation Types
313 \brief ELF relocation type values
314 \ingroup elf
315
316 These define the types of operations that can be done to calculate
317 relocations within ELF files.
318
319 @{
320*/
321#define R_SH_DIR32 1 /**< \brief SuperH: Rel = Symbol + Addend */
322#define R_386_32 1 /**< \brief x86: Rel = Symbol + Addend */
323#define R_386_PC32 2 /**< \brief x86: Rel = Symbol + Addend - Value */
324/** @} */
325
326/** \brief Retrieve the symbol index from a relocation entry.
327 \ingroup elf
328
329 \param i The info field of an elf_rel_t or elf_rela_t.
330 \return The symbol table index from that relocation entry.
331*/
332#define ELF32_R_SYM(i) ((i) >> 8)
333
334/** \brief Retrieve the relocation type from a relocation entry.
335 \ingroup elf
336
337 \param i The info field of an elf_rel_t or an elf_rela_t.
338 \return The relocation type of that relocation.
339 \see elf_reltypes
340*/
341#define ELF32_R_TYPE(i) ((uint8_t)(i))
342
343struct klibrary;
344
345/** \brief Kernel-specific definition of a loaded ELF binary.
346 \ingroup elf
347
348 This structure represents the internal representation of a loaded ELF binary
349 in KallistiOS (specifically as a dynamically loaded library).
350
351 \headerfile kos/elf.h
352*/
353typedef struct elf_prog {
354 void *data; /**< \brief Pointer to program in memory */
355 uint32_t size; /**< \brief Memory image size (rounded up to page size) */
356
357 /* Library exports */
358 uintptr_t lib_get_name; /**< \brief Pointer to get_name() function */
359 uintptr_t lib_get_version; /**< \brief Pointer to get_version() function */
360 uintptr_t lib_open; /**< \brief Pointer to library's open function */
361 uintptr_t lib_close; /**< \brief Pointer to library's close function */
362
363 char fn[256]; /**< \brief Filename of library */
364} elf_prog_t;
365
366/** \brief Load an ELF binary.
367 \ingroup elf
368
369 This function loads an ELF binary from the VFS and fills in an elf_prog_t
370 for it.
371
372 \param fn The filename of the binary on the VFS.
373 \param shell Unused?
374 \param out Storage for the binary that will be loaded.
375 \return 0 on success, <0 on failure.
376*/
377int elf_load(const char *fn, struct klibrary * shell, elf_prog_t * out);
378
379/** \brief Free a loaded ELF program.
380 \ingroup elf
381
382 This function cleans up an ELF binary that was loaded with elf_load().
383
384 \param prog The loaded binary to clean up.
385*/
387
388__END_DECLS
389
390#endif /* __KOS_ELF_H */
391
Various common macros used throughout the codebase.
#define EI_NIDENT
Size of elf_hdr::ident.
Definition elf.h:57
int elf_load(const char *fn, struct klibrary *shell, elf_prog_t *out)
Load an ELF binary.
void elf_free(elf_prog_t *prog)
Free a loaded ELF program.
static uint32_t("Please see purupuru_effect_t for modern equivalent.") PURUPURU_EFFECT2_UINTENSITY(uint8_t x)
Definition purupuru.h:96
Macros to help dealing with register fields.
ELF file header.
Definition elf.h:78
uint32_t entry
Entry point.
Definition elf.h:83
uint16_t shnum
Section header entry count.
Definition elf.h:91
uint16_t shstrndx
String table section index.
Definition elf.h:92
uint16_t ehsize
ELF header size in bytes.
Definition elf.h:87
uint16_t phnum
Program header entry count.
Definition elf.h:89
uint16_t phentsize
Program header entry size.
Definition elf.h:88
uint32_t shoff
Section header offset.
Definition elf.h:85
uint16_t shentsize
Section header entry size.
Definition elf.h:90
uint16_t machine
ELF file architecture.
Definition elf.h:81
uint32_t phoff
Program header offset.
Definition elf.h:84
uint16_t type
ELF file type.
Definition elf.h:80
uint32_t version
Object file version.
Definition elf.h:82
uint32_t flags
Processor flags.
Definition elf.h:86
Kernel-specific definition of a loaded ELF binary.
Definition elf.h:353
uintptr_t lib_get_name
Pointer to get_name() function.
Definition elf.h:358
void * data
Pointer to program in memory.
Definition elf.h:354
uintptr_t lib_get_version
Pointer to get_version() function.
Definition elf.h:359
uintptr_t lib_close
Pointer to library's close function.
Definition elf.h:361
uintptr_t lib_open
Pointer to library's open function.
Definition elf.h:360
uint32_t size
Memory image size (rounded up to page size)
Definition elf.h:355
ELF Relocation entry (without explicit addend).
Definition elf.h:307
uint32_t info
Symbol and type.
Definition elf.h:309
uint32_t offset
Offset within section.
Definition elf.h:308
ELF Relocation entry (with explicit addend).
Definition elf.h:292
uint32_t info
Symbol and type.
Definition elf.h:294
uint32_t offset
Offset within section.
Definition elf.h:293
int32_t addend
Constant addend for the symbol.
Definition elf.h:295
ELF Section header.
Definition elf.h:184
uint32_t name
Index into string table.
Definition elf.h:185
uint32_t info
Section header extra info.
Definition elf.h:192
uint32_t type
Section type.
Definition elf.h:186
uint32_t addralign
Alignment constraints.
Definition elf.h:193
uint32_t flags
Section flags.
Definition elf.h:187
uint32_t offset
On-disk offset.
Definition elf.h:189
uint32_t entsize
Fixed-size table entry sizes.
Definition elf.h:194
uint32_t link
Section header table index link.
Definition elf.h:191
uint32_t addr
In-memory offset.
Definition elf.h:188
uint32_t size
Size (if SHT_NOBITS, amount of 0s needed)
Definition elf.h:190
Symbol table entry.
Definition elf.h:256
uint16_t shndx
Section index.
Definition elf.h:262
uint32_t size
Size of the symbol.
Definition elf.h:259
uint32_t value
Value of the symbol.
Definition elf.h:258
uint8_t other
0.
Definition elf.h:261
uint8_t info
Symbol type and binding.
Definition elf.h:260
uint32_t name
Index into file's string table.
Definition elf.h:257