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