Subversion Repositories HelenOS

Rev

Rev 2071 | Rev 2128 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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