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 <sys/cdefs.h>
25__BEGIN_DECLS
26
27#include <stdint.h>
28#include <arch/types.h>
29#include <sys/queue.h>
30
31/** \defgroup elf ELF File Format
32 \brief API for loading and managing ELF files
33 \ingroup system_libraries
34*/
35
36/** \brief ELF file header.
37 \ingroup elf
38
39 This header is at the beginning of any valid ELF binary and serves to
40 identify the architecture of the binary and various data about it.
41
42 \headerfile kos/elf.h
43*/
44struct elf_hdr_t {
45 uint8 ident[16]; /**< \brief ELF identifier */
46 uint16 type; /**< \brief ELF file type */
47 uint16 machine; /**< \brief ELF file architecture */
48 uint32 version; /**< \brief Object file version */
49 uint32 entry; /**< \brief Entry point */
50 uint32 phoff; /**< \brief Program header offset */
51 uint32 shoff; /**< \brief Section header offset */
52 uint32 flags; /**< \brief Processor flags */
53 uint16 ehsize; /**< \brief ELF header size in bytes */
54 uint16 phentsize; /**< \brief Program header entry size */
55 uint16 phnum; /**< \brief Program header entry count */
56 uint16 shentsize; /**< \brief Section header entry size */
57 uint16 shnum; /**< \brief Section header entry count */
58 uint16 shstrndx; /**< \brief String table section index */
59};
60
61/** \defgroup elf_archs Architecture Types
62 \brief Relevant ELF architecture type codes
63 \ingroup elf
64
65 These are the various architectures that we might care about for ELF files.
66
67 @{
68*/
69#define EM_386 3 /**< \brief x86 (IA32) */
70#define EM_ARM 40 /**< \brief ARM */
71#define EM_SH 42 /**< \brief SuperH */
72/** @} */
73
74/** \defgroup elf_sections Section Header Types
75 \brief ELF section header type values
76 \ingroup elf
77
78 These are the various types of section headers that can exist in an ELF
79 file.
80
81 @{
82*/
83#define SHT_NULL 0 /**< \brief Inactive section */
84#define SHT_PROGBITS 1 /**< \brief Program code/data */
85#define SHT_SYMTAB 2 /**< \brief Full symbol table */
86#define SHT_STRTAB 3 /**< \brief String table */
87#define SHT_RELA 4 /**< \brief Relocation table, with addends */
88#define SHT_HASH 5 /**< \brief Symbol hash table */
89#define SHT_DYNAMIC 6 /**< \brief Dynamic linking info */
90#define SHT_NOTE 7 /**< \brief Notes section */
91#define SHT_NOBITS 8 /**< \brief A section that occupies no space in
92the file */
93#define SHT_REL 9 /**< \brief Relocation table, no addends */
94#define SHT_SHLIB 10 /**< \brief Reserved */
95#define SHT_DYNSYM 11 /**< \brief Dynamic-only sym tab */
96#define SHT_LOPROC 0x70000000 /**< \brief Start of processor specific types */
97#define SHT_HIPROC 0x7fffffff /**< \brief End of processor specific types */
98#define SHT_LOUSER 0x80000000 /**< \brief Start of program specific types */
99#define SHT_HIUSER 0xffffffff /**< \brief End of program specific types */
100/** @} */
101
102/** \defgroup elf_hdrflags Section Header Flags
103 \brief ELF section header flags
104 \ingroup elf
105
106 These are the flags that can be set on a section header. These are related
107 to whether the section should reside in memory and permissions on it.
108
109 @{
110*/
111#define SHF_WRITE 1 /**< \brief Writable data */
112#define SHF_ALLOC 2 /**< \brief Resident */
113#define SHF_EXECINSTR 4 /**< \brief Executable instructions */
114#define SHF_MASKPROC 0xf0000000 /**< \brief Processor specific mask */
115/** @} */
116
117/** \defgroup elf_specsec Special Section Indices
118 \brief ELF section indices
119 \ingroup elf
120
121 These are the indices to be used in special situations in the section array.
122
123 @{
124*/
125#define SHN_UNDEF 0 /**< \brief Undefined, missing, irrelevant */
126#define SHN_ABS 0xfff1 /**< \brief Absolute values */
127/** @} */
128
129/** \brief ELF Section header.
130 \ingroup elf
131
132 This structure represents the header on each ELF section.
133
134 \headerfile kos/elf.h
135*/
137 uint32 name; /**< \brief Index into string table */
138 uint32 type; /**< \brief Section type \see elf_sections */
139 uint32 flags; /**< \brief Section flags \see elf_hdrflags */
140 uint32 addr; /**< \brief In-memory offset */
141 uint32 offset; /**< \brief On-disk offset */
142 uint32 size; /**< \brief Size (if SHT_NOBITS, amount of 0s needed) */
143 uint32 link; /**< \brief Section header table index link */
144 uint32 info; /**< \brief Section header extra info */
145 uint32 addralign; /**< \brief Alignment constraints */
146 uint32 entsize; /**< \brief Fixed-size table entry sizes */
147};
148/* Link and info fields:
149
150switch (sh_type) {
151 case SHT_DYNAMIC:
152 link = section header index of the string table used by
153 the entries in this section
154 info = 0
155 case SHT_HASH:
156 ilnk = section header index of the string table to which
157 this info applies
158 info = 0
159 case SHT_REL, SHT_RELA:
160 link = section header index of associated symbol table
161 info = section header index of section to which reloc applies
162 case SHT_SYMTAB, SHT_DYNSYM:
163 link = section header index of associated string table
164 info = one greater than the symbol table index of the last
165 local symbol (binding STB_LOCAL)
166}
167
168*/
169
170/** \defgroup elf_binding Symbol Binding Types
171 \brief ELF symbol binding type values
172 \ingroup elf
173
174 These are the values that can be set to say how a symbol is bound in an ELF
175 binary. This is stored in the upper 4 bits of the info field in elf_sym_t.
176
177 @{
178*/
179#define STB_LOCAL 0 /**< \brief Local (non-exported) symbol */
180#define STB_GLOBAL 1 /**< \brief Global (exported) symbol */
181#define STB_WEAK 2 /**< \brief Weak-linked symbol */
182/** @} */
183
184/** \defgroup elf_symtype Symbol Types
185 \brief ELF symbol type values
186 \ingroup elf
187
188 These are the values that can be set to say what kind of symbol a given
189 symbol in an ELF file is. This is stored in the lower 4 bits of the info
190 field in elf_sym_t.
191
192 @{
193*/
194#define STT_NOTYPE 0 /**< \brief Symbol has no type */
195#define STT_OBJECT 1 /**< \brief Symbol is an object */
196#define STT_FUNC 2 /**< \brief Symbol is a function */
197#define STT_SECTION 3 /**< \brief Symbol is a section */
198#define STT_FILE 4 /**< \brief Symbol is a file name */
199/** @} */
200
201/** \brief Symbol table entry
202 \ingroup elf
203
204 This structure represents a single entry in a symbol table in an ELF file.
205
206 \headerfile kos/elf.h
207*/
208struct elf_sym_t {
209 uint32 name; /**< \brief Index into file's string table */
210 uint32 value; /**< \brief Value of the symbol */
211 uint32 size; /**< \brief Size of the symbol */
212 uint8 info; /**< \brief Symbol type and binding */
213 uint8 other; /**< \brief 0. Holds no meaning. */
214 uint16 shndx; /**< \brief Section index */
215};
216
217/** \brief Retrieve the binding type for a symbol.
218 \ingroup elf
219
220 \param info The info field of an elf_sym_t.
221 \return The binding type of the symbol.
222 \see elf_binding
223*/
224#define ELF32_ST_BIND(info) ((info) >> 4)
225
226/** \brief Retrieve the symbol type for a symbol.
227 \ingroup elf
228
229 \param info The info field of an elf_sym_t.
230 \return The symbol type of the symbol.
231 \see elf_symtype
232*/
233#define ELF32_ST_TYPE(info) ((info) & 0xf)
234
235/** \brief ELF Relocation entry (with explicit addend).
236 \ingroup elf
237
238 This structure represents an ELF relocation entry with an explicit addend.
239 This structure is used on some architectures, whereas others use the
240 elf_rel_t structure instead.
241
242 \headerfile kos/elf.h
243*/
245 uint32 offset; /**< \brief Offset within section */
246 uint32 info; /**< \brief Symbol and type */
247 int32 addend; /**< \brief Constant addend for the symbol */
248};
249
250/** \brief ELF Relocation entry (without explicit addend).
251 \ingroup elf
252
253 This structure represents an ELF relocation entry without an explicit
254 addend. This structure is used on some architectures, whereas others use the
255 elf_rela_t structure instead.
256
257 \headerfile kos/elf.h
258*/
259struct elf_rel_t {
260 uint32 offset; /**< \brief Offset within section */
261 uint32 info; /**< \brief Symbol and type */
262};
263
264/** \defgroup elf_reltypes Relocation Types
265 \brief ELF relocation type values
266 \ingroup elf
267
268 These define the types of operations that can be done to calculate
269 relocations within ELF files.
270
271 @{
272*/
273#define R_SH_DIR32 1 /**< \brief SuperH: Rel = Symbol + Addend */
274#define R_386_32 1 /**< \brief x86: Rel = Symbol + Addend */
275#define R_386_PC32 2 /**< \brief x86: Rel = Symbol + Addend - Value */
276/** @} */
277
278/** \brief Retrieve the symbol index from a relocation entry.
279 \ingroup elf
280
281 \param i The info field of an elf_rel_t or elf_rela_t.
282 \return The symbol table index from that relocation entry.
283*/
284#define ELF32_R_SYM(i) ((i) >> 8)
285
286/** \brief Retrieve the relocation type from a relocation entry.
287 \ingroup elf
288
289 \param i The info field of an elf_rel_t or an elf_rela_t.
290 \return The relocation type of that relocation.
291 \see elf_reltypes
292*/
293#define ELF32_R_TYPE(i) ((uint8)(i))
294
295struct klibrary;
296
297/** \brief Kernel-specific definition of a loaded ELF binary.
298 \ingroup elf
299
300 This structure represents the internal representation of a loaded ELF binary
301 in KallistiOS (specifically as a dynamically loaded library).
302
303 \headerfile kos/elf.h
304*/
305typedef struct elf_prog {
306 void *data; /**< \brief Pointer to program in memory */
307 uint32 size; /**< \brief Memory image size (rounded up to page size) */
308
309 /* Library exports */
310 uintptr_t lib_get_name; /**< \brief Pointer to get_name() function */
311 uintptr_t lib_get_version; /**< \brief Pointer to get_version() function */
312 uintptr_t lib_open; /**< \brief Pointer to library's open function */
313 uintptr_t lib_close; /**< \brief Pointer to library's close function */
314
315 char fn[256]; /**< \brief Filename of library */
316} elf_prog_t;
317
318/** \brief Load an ELF binary.
319 \ingroup elf
320
321 This function loads an ELF binary from the VFS and fills in an elf_prog_t
322 for it.
323
324 \param fn The filename of the binary on the VFS.
325 \param shell Unused?
326 \param out Storage for the binary that will be loaded.
327 \return 0 on success, <0 on failure.
328*/
329int elf_load(const char *fn, struct klibrary * shell, elf_prog_t * out);
330
331/** \brief Free a loaded ELF program.
332 \ingroup elf
333
334 This function cleans up an ELF binary that was loaded with elf_load().
335
336 \param prog The loaded binary to clean up.
337*/
339
340__END_DECLS
341
342#endif /* __OS_ELF_H */
343
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.
unsigned short uint16
16-bit unsigned integer
Definition types.h:34
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
long int32
32-bit signed integer
Definition types.h:37
unsigned char uint8
8-bit unsigned integer
Definition types.h:35
ELF file header.
Definition elf.h:44
uint16 shnum
Section header entry count.
Definition elf.h:57
uint32 flags
Processor flags.
Definition elf.h:52
uint16 ehsize
ELF header size in bytes.
Definition elf.h:53
uint16 shentsize
Section header entry size.
Definition elf.h:56
uint16 type
ELF file type.
Definition elf.h:46
uint16 shstrndx
String table section index.
Definition elf.h:58
uint32 version
Object file version.
Definition elf.h:48
uint16 phnum
Program header entry count.
Definition elf.h:55
uint32 entry
Entry point.
Definition elf.h:49
uint32 phoff
Program header offset.
Definition elf.h:50
uint16 machine
ELF file architecture.
Definition elf.h:47
uint8 ident[16]
ELF identifier.
Definition elf.h:45
uint16 phentsize
Program header entry size.
Definition elf.h:54
uint32 shoff
Section header offset.
Definition elf.h:51
Kernel-specific definition of a loaded ELF binary.
Definition elf.h:305
uintptr_t lib_get_name
Pointer to get_name() function.
Definition elf.h:310
void * data
Pointer to program in memory.
Definition elf.h:306
uintptr_t lib_get_version
Pointer to get_version() function.
Definition elf.h:311
uint32 size
Memory image size (rounded up to page size)
Definition elf.h:307
uintptr_t lib_close
Pointer to library's close function.
Definition elf.h:313
uintptr_t lib_open
Pointer to library's open function.
Definition elf.h:312
ELF Relocation entry (without explicit addend).
Definition elf.h:259
uint32 info
Symbol and type.
Definition elf.h:261
uint32 offset
Offset within section.
Definition elf.h:260
ELF Relocation entry (with explicit addend).
Definition elf.h:244
int32 addend
Constant addend for the symbol.
Definition elf.h:247
uint32 info
Symbol and type.
Definition elf.h:246
uint32 offset
Offset within section.
Definition elf.h:245
ELF Section header.
Definition elf.h:136
uint32 name
Index into string table.
Definition elf.h:137
uint32 type
Section type.
Definition elf.h:138
uint32 link
Section header table index link.
Definition elf.h:143
uint32 addralign
Alignment constraints.
Definition elf.h:145
uint32 flags
Section flags.
Definition elf.h:139
uint32 addr
In-memory offset.
Definition elf.h:140
uint32 entsize
Fixed-size table entry sizes.
Definition elf.h:146
uint32 info
Section header extra info.
Definition elf.h:144
uint32 offset
On-disk offset.
Definition elf.h:141
uint32 size
Size (if SHT_NOBITS, amount of 0s needed)
Definition elf.h:142
Symbol table entry.
Definition elf.h:208
uint16 shndx
Section index.
Definition elf.h:214
uint8 other
0.
Definition elf.h:213
uint8 info
Symbol type and binding.
Definition elf.h:212
uint32 value
Value of the symbol.
Definition elf.h:210
uint32 size
Size of the symbol.
Definition elf.h:211
uint32 name
Index into file's string table.
Definition elf.h:209
Common integer types.