Subversion Repositories HelenOS

Rev

Rev 2071 | Rev 2128 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
854 bondari 1
/*
2071 jermar 2
 * Copyright (c) 2006 Sergey Bondari
854 bondari 3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
1819 decky 29
/** @addtogroup generic
1702 cejka 30
 * @{
31
 */
32
/** @file
33
 */
34
 
1888 jermar 35
#ifndef KERN_ELF_H_
36
#define KERN_ELF_H_
854 bondari 37
 
38
#include <arch/elf.h>
938 jermar 39
#include <arch/types.h>
854 bondari 40
 
938 jermar 41
/**
42
 * current ELF version
43
 */
44
#define EV_CURRENT  1
45
 
46
/**
47
 * ELF types
48
 */
49
#define ET_NONE     0   /* No type */
50
#define ET_REL      1   /* Relocatable file */
51
#define ET_EXEC     2   /* Executable */
52
#define ET_DYN      3   /* Shared object */
53
#define ET_CORE     4   /* Core */
54
#define ET_LOPROC   0xff00  /* Processor specific */
55
#define ET_HIPROC   0xffff  /* Processor specific */
56
 
57
/**
58
 * ELF machine types
59
 */
60
#define EM_NO       0   /* No machine */
61
#define EM_SPARC    2   /* SPARC */
62
#define EM_386      3   /* i386 */
63
#define EM_MIPS     8   /* MIPS RS3000 */
64
#define EM_MIPS_RS3_LE  10  /* MIPS RS3000 LE */
65
#define EM_PPC      20  /* PPC32 */
66
#define EM_PPC64    21  /* PPC64 */
67
#define EM_SPARCV9  43  /* SPARC64 */
68
#define EM_IA_64    50  /* IA-64 */
69
#define EM_X86_64   62  /* AMD64/EMT64 */
70
 
71
/**
72
 * ELF identification indexes
73
 */
74
#define EI_MAG0     0
75
#define EI_MAG1     1
76
#define EI_MAG2     2
77
#define EI_MAG3     3
78
#define EI_CLASS    4       /* File class */
79
#define EI_DATA     5       /* Data encoding */
80
#define EI_VERSION  6       /* File version */
81
#define EI_OSABI    7
82
#define EI_ABIVERSION   8
83
#define EI_PAD      9       /* Start of padding bytes */
84
#define EI_NIDENT   16      /* ELF identification table size */
85
 
86
/**
87
 * ELF magic number
88
 */
89
#define ELFMAG0     0x7f
90
#define ELFMAG1     'E'
91
#define ELFMAG2     'L'
92
#define ELFMAG3     'F'
93
 
94
/**
95
 * ELF file classes
96
 */
97
#define ELFCLASSNONE    0
98
#define ELFCLASS32  1
99
#define ELFCLASS64  2
100
 
101
/**
102
 * ELF data encoding types
103
 */
104
#define ELFDATANONE 0
105
#define ELFDATA2LSB 1       /* Least significant byte first (little endian) */
106
#define ELFDATA2MSB 2       /* Most signigicant byte first (big endian) */
107
 
108
/**
109
 * ELF error return codes
110
 */
111
#define EE_OK           0   /* No error */
112
#define EE_INVALID      1   /* Invalid ELF image */
113
#define EE_MEMORY       2   /* Cannot allocate address space */
114
#define EE_INCOMPATIBLE     3   /* ELF image is not compatible with current architecture */
115
#define EE_UNSUPPORTED      4   /* Non-supported ELF (e.g. dynamic ELFs) */
116
#define EE_IRRECOVERABLE    5
117
 
118
/**
119
 * ELF section types
120
 */
121
#define SHT_NULL        0
122
#define SHT_PROGBITS        1
123
#define SHT_SYMTAB      2
124
#define SHT_STRTAB      3
125
#define SHT_RELA        4
126
#define SHT_HASH        5
127
#define SHT_DYNAMIC     6
128
#define SHT_NOTE        7
129
#define SHT_NOBITS      8
130
#define SHT_REL         9
131
#define SHT_SHLIB       10
132
#define SHT_DYNSYM      11
133
#define SHT_LOOS        0x60000000
134
#define SHT_HIOS        0x6fffffff
135
#define SHT_LOPROC      0x70000000
136
#define SHT_HIPROC      0x7fffffff
137
#define SHT_LOUSER      0x80000000
138
#define SHT_HIUSER      0xffffffff
139
 
140
/**
141
 * ELF section flags
142
 */
143
#define SHF_WRITE       0x1 
144
#define SHF_ALLOC       0x2
145
#define SHF_EXECINSTR       0x4
146
#define SHF_MASKPROC        0xf0000000
147
 
148
/**
149
 * Symbol binding
150
 */
151
#define STB_LOCAL       0
152
#define STB_GLOBAL      1
153
#define STB_WEAK        2
154
#define STB_LOPROC      13
155
#define STB_HIPROC      15
156
 
157
/**
158
 * Symbol types
159
 */
160
#define STT_NOTYPE      0
161
#define STT_OBJECT      1
162
#define STT_FUNC        2
163
#define STT_SECTION     3
164
#define STT_FILE        4
165
#define STT_LOPROC      13
166
#define STT_HIPROC      15
167
 
168
/**
169
 * Program segment types
170
 */
171
#define PT_NULL         0
172
#define PT_LOAD         1
173
#define PT_DYNAMIC      2
174
#define PT_INTERP       3
175
#define PT_NOTE         4
176
#define PT_SHLIB        5
177
#define PT_PHDR         6
178
#define PT_LOPROC       0x70000000
179
#define PT_HIPROC       0x7fffffff
180
 
181
/**
182
 * Program segment attributes.
183
 */
184
#define PF_X    1
185
#define PF_W    2
186
#define PF_R    4
187
 
188
/**
189
 * ELF data types
190
 *
191
 * These types are found to be identical in both 32-bit and 64-bit
192
 * ELF object file specifications. They are the only types used
193
 * in ELF header.
194
 */
1780 jermar 195
typedef uint64_t elf_xword;
196
typedef int64_t elf_sxword;
197
typedef uint32_t elf_word;
198
typedef int32_t elf_sword;
199
typedef uint16_t elf_half;
938 jermar 200
 
201
/**
202
 * 32-bit ELF data types.
203
 *
204
 * These types are specific for 32-bit format.
205
 */
1780 jermar 206
typedef uint32_t elf32_addr;
207
typedef uint32_t elf32_off;
938 jermar 208
 
209
/**
210
 * 64-bit ELF data types.
211
 *
212
 * These types are specific for 64-bit format.
213
 */
1780 jermar 214
typedef uint64_t elf64_addr;
215
typedef uint64_t elf64_off;
938 jermar 216
 
217
/** ELF header */
218
struct elf32_header {
1780 jermar 219
    uint8_t e_ident[EI_NIDENT];
938 jermar 220
    elf_half e_type;
221
    elf_half e_machine;
222
    elf_word e_version;
223
    elf32_addr e_entry;
224
    elf32_off e_phoff;
225
    elf32_off e_shoff;
226
    elf_word e_flags;
227
    elf_half e_ehsize;
228
    elf_half e_phentsize;
229
    elf_half e_phnum;
230
    elf_half e_shentsize;
231
    elf_half e_shnum;
232
    elf_half e_shstrndx;
233
};
234
struct elf64_header {
1780 jermar 235
    uint8_t e_ident[EI_NIDENT];
938 jermar 236
    elf_half e_type;
237
    elf_half e_machine;
238
    elf_word e_version;
239
    elf64_addr e_entry;
240
    elf64_off e_phoff;
241
    elf64_off e_shoff;
242
    elf_word e_flags;
243
    elf_half e_ehsize;
244
    elf_half e_phentsize;
245
    elf_half e_phnum;
246
    elf_half e_shentsize;
247
    elf_half e_shnum;
248
    elf_half e_shstrndx;
249
};
250
 
251
/*
952 jermar 252
 * ELF segment header.
253
 * Segments headers are also known as program headers.
254
 */
255
struct elf32_segment_header {
256
    elf_word p_type;
257
    elf32_off p_offset;
258
    elf32_addr p_vaddr;
259
    elf32_addr p_paddr;
260
    elf_word p_filesz;
261
    elf_word p_memsz;
262
    elf_word p_flags;
263
    elf_word p_align;
264
};
265
struct elf64_segment_header {
266
    elf_word p_type;
267
    elf_word p_flags;
268
    elf64_off p_offset;
269
    elf64_addr p_vaddr;
270
    elf64_addr p_paddr;
271
    elf_xword p_filesz;
272
    elf_xword p_memsz;
273
    elf_xword p_align;
274
};
275
 
276
/*
938 jermar 277
 * ELF section header
278
 */
279
struct elf32_section_header {
280
    elf_word sh_name;
281
    elf_word sh_type;
282
    elf_word sh_flags;
283
    elf32_addr sh_addr;
284
    elf32_off sh_offset;
285
    elf_word sh_size;
286
    elf_word sh_link;
287
    elf_word sh_info;
288
    elf_word sh_addralign;
289
    elf_word sh_entsize;
290
};
291
struct elf64_section_header {
292
    elf_word sh_name;
293
    elf_word sh_type;
294
    elf_xword sh_flags;
295
    elf64_addr sh_addr;
296
    elf64_off sh_offset;
297
    elf_xword sh_size;
298
    elf_word sh_link;
299
    elf_word sh_info;
300
    elf_xword sh_addralign;
301
    elf_xword sh_entsize;
302
};
303
 
304
/*
305
 * ELF symbol table entry
306
 */
307
struct elf32_symbol {
308
    elf_word st_name;
309
    elf32_addr st_value;
310
    elf_word st_size;
1780 jermar 311
    uint8_t st_info;
312
    uint8_t st_other;
938 jermar 313
    elf_half st_shndx;
314
};
315
struct elf64_symbol {
316
    elf_word st_name;
1780 jermar 317
    uint8_t st_info;
318
    uint8_t st_other;
938 jermar 319
    elf_half st_shndx;
320
    elf64_addr st_value;
321
    elf_xword st_size;
322
};
323
 
324
#ifdef __32_BITS__ 
325
typedef struct elf32_header elf_header_t;
952 jermar 326
typedef struct elf32_segment_header elf_segment_header_t;
938 jermar 327
typedef struct elf32_section_header elf_section_header_t;
328
typedef struct elf32_symbol elf_symbol_t;
854 bondari 329
#endif
938 jermar 330
#ifdef __64_BITS__
331
typedef struct elf64_header elf_header_t;
952 jermar 332
typedef struct elf64_segment_header elf_segment_header_t;
938 jermar 333
typedef struct elf64_section_header elf_section_header_t;
334
typedef struct elf64_symbol elf_symbol_t;
335
#endif
336
 
337
extern char *elf_error(int rc);
338
 
339
#endif
1702 cejka 340
 
1819 decky 341
/** @}
1702 cejka 342
 */