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