Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
2928 svoboda 1
/*
2
 * Copyright (c) 2006 Sergey Bondari
3
 * Copyright (c) 2006 Jakub Jermar
4
 * Copyright (c) 2008 Jiri Svoboda
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 * - Redistributions of source code must retain the above copyright
12
 *   notice, this list of conditions and the following disclaimer.
13
 * - Redistributions in binary form must reproduce the above copyright
14
 *   notice, this list of conditions and the following disclaimer in the
15
 *   documentation and/or other materials provided with the distribution.
16
 * - The name of the author may not be used to endorse or promote products
17
 *   derived from this software without specific prior written permission.
18
 *
19
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
 */
30
 
31
/** @addtogroup generic
32
 * @{
33
 */
34
 
35
/**
36
 * @file
37
 * @brief   Kernel ELF loader.
38
 */
39
 
40
#include <stdio.h>
41
#include <sys/types.h>
42
#include <align.h>
43
#include <assert.h>
44
#include <as.h>
2962 svoboda 45
#include <unistd.h>
46
#include <fcntl.h>
47
 
2928 svoboda 48
#include "elf.h"
2962 svoboda 49
#include "pcb.h"
50
#include "elf_load.h"
2928 svoboda 51
 
2932 svoboda 52
#define RTLD_BIAS 0x80000
2949 svoboda 53
//#define RTLD_BIAS 0
2928 svoboda 54
 
55
static char *error_codes[] = {
56
    "no error",
57
    "invalid image",
58
    "address space error",
59
    "incompatible image",
60
    "unsupported image type",
61
    "irrecoverable error"
62
};
63
 
2964 svoboda 64
static unsigned int elf_load(elf_ld_t *elf);
65
static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry);
66
static int section_header(elf_ld_t *elf, elf_section_header_t *entry);
67
static int load_segment(elf_ld_t *elf, elf_segment_header_t *entry);
2928 svoboda 68
 
2964 svoboda 69
int elf_load_file(char *file_name, elf_info_t *info)
70
{
71
    elf_ld_t elf;
2928 svoboda 72
 
2962 svoboda 73
    int fd;
74
    int rc;
75
 
76
    printf("open and read '%s'...\n", file_name);
77
 
78
    fd = open(file_name, 0);
79
    if (fd < 0) {
80
        printf("failed opening file\n");
81
        return -1;
82
    }
83
 
2964 svoboda 84
    elf.fd = fd;
85
    elf.info = info;
86
 
87
    rc = elf_load(&elf);
2962 svoboda 88
    printf("elf_load() -> %d\n", rc);
89
 
90
    close(fd);
91
 
92
    return rc;
93
}
94
 
2964 svoboda 95
void elf_run(elf_info_t *info)
2962 svoboda 96
{
2964 svoboda 97
    (*info->entry)();
2962 svoboda 98
 
99
    /* not reached */
100
}
101
 
2964 svoboda 102
int elf_create_pcb(elf_info_t *info)
2962 svoboda 103
{
104
    pcb_t *pcb;
105
    void *a;
106
 
107
    pcb = (pcb_t *)PCB_ADDRESS;
108
 
109
    a = as_area_create(pcb, sizeof(pcb_t), AS_AREA_READ | AS_AREA_WRITE);
110
    if (a == (void *)(-1)) {
111
        printf("elf_create_pcb: memory mapping failed\n");
112
        return EE_MEMORY;
113
    }
114
 
2964 svoboda 115
    pcb->entry = info->entry;
2965 svoboda 116
    pcb->dynamic = info->dynamic;
2962 svoboda 117
 
118
    return 0;
119
}
120
 
121
 
2928 svoboda 122
/** ELF loader
123
 *
124
 * @param header Pointer to ELF header in memory
125
 * @return EE_OK on success
126
 */
2964 svoboda 127
static unsigned int elf_load(elf_ld_t *elf)
2928 svoboda 128
{
2964 svoboda 129
    elf_header_t header_buf;
130
    elf_header_t *header = &header_buf;
2928 svoboda 131
    int i, rc;
132
 
2964 svoboda 133
    rc = read(elf->fd, header, sizeof(elf_header_t));
2928 svoboda 134
    if (rc < 0) {
135
        printf("read error\n");
136
        return EE_INVALID;
137
    }
138
 
2964 svoboda 139
    elf->header = header;
140
 
2928 svoboda 141
    printf("ELF-load:");
142
    /* Identify ELF */
143
    if (header->e_ident[EI_MAG0] != ELFMAG0 ||
144
        header->e_ident[EI_MAG1] != ELFMAG1 ||
145
        header->e_ident[EI_MAG2] != ELFMAG2 ||
146
        header->e_ident[EI_MAG3] != ELFMAG3) {
147
        printf("invalid header\n");
148
        return EE_INVALID;
149
    }
150
 
151
    /* Identify ELF compatibility */
152
    if (header->e_ident[EI_DATA] != ELF_DATA_ENCODING ||
153
        header->e_machine != ELF_MACHINE ||
154
        header->e_ident[EI_VERSION] != EV_CURRENT ||
155
        header->e_version != EV_CURRENT ||
156
        header->e_ident[EI_CLASS] != ELF_CLASS) {
157
        printf("incompatible data/version/class\n");
158
        return EE_INCOMPATIBLE;
159
    }
160
 
161
    if (header->e_phentsize != sizeof(elf_segment_header_t))
162
        return EE_INCOMPATIBLE;
163
 
164
    if (header->e_shentsize != sizeof(elf_section_header_t))
165
        return EE_INCOMPATIBLE;
166
 
167
    /* Check if the object type is supported. */
168
    if (header->e_type != ET_EXEC && header->e_type != ET_DYN) {
169
        printf("Object type %d is not supported\n", header->e_type);
170
        return EE_UNSUPPORTED;
171
    }
172
 
2964 svoboda 173
    /* The run-time dynamic linker is loaded with a bias */
2971 svoboda 174
    printf("Object type: %d\n", header->e_type);
2964 svoboda 175
    if (header->e_type == ET_DYN)
176
        elf->bias = RTLD_BIAS;
177
    else
178
        elf->bias = 0;
179
 
2971 svoboda 180
    printf("Bias set to 0x%d\n", elf->bias);
181
 
2928 svoboda 182
    printf("parse segments\n");
183
 
184
    /* Walk through all segment headers and process them. */
185
    for (i = 0; i < header->e_phnum; i++) {
2964 svoboda 186
        elf_segment_header_t segment_hdr;
2928 svoboda 187
 
2961 svoboda 188
        /* Seek to start of segment header */
2964 svoboda 189
        lseek(elf->fd, header->e_phoff
190
                + i * sizeof(elf_segment_header_t), SEEK_SET);
2928 svoboda 191
 
2964 svoboda 192
        rc = read(elf->fd, &segment_hdr, sizeof(elf_segment_header_t));
193
        if (rc < 0) { printf("read error\n"); return EE_INVALID; }
194
 
195
        rc = segment_header(elf, &segment_hdr);
2928 svoboda 196
        if (rc != EE_OK)
197
            return rc;
198
    }
199
 
200
    printf("parse sections\n");
201
 
202
    /* Inspect all section headers and proccess them. */
203
    for (i = 0; i < header->e_shnum; i++) {
2964 svoboda 204
        elf_section_header_t section_hdr;
2928 svoboda 205
 
2961 svoboda 206
        /* Seek to start of section header */
2964 svoboda 207
        lseek(elf->fd, header->e_shoff
208
            + i * sizeof(elf_section_header_t), SEEK_SET);
2961 svoboda 209
 
2964 svoboda 210
        rc = read(elf->fd, &section_hdr, sizeof(elf_section_header_t));
211
        if (rc < 0) { printf("read error\n"); return EE_INVALID; }
212
 
213
        rc = section_header(elf, &section_hdr);
2928 svoboda 214
        if (rc != EE_OK)
2961 svoboda 215
            return rc;
2928 svoboda 216
    }
217
 
2964 svoboda 218
    elf->info->entry =
219
        (entry_point_t)((uint8_t *)header->e_entry + elf->bias);
220
 
2928 svoboda 221
    printf("done\n");
222
 
223
    return EE_OK;
224
}
225
 
226
/** Print error message according to error code.
227
 *
228
 * @param rc Return code returned by elf_load().
229
 *
230
 * @return NULL terminated description of error.
231
 */
232
char *elf_error(unsigned int rc)
233
{
234
    assert(rc < sizeof(error_codes) / sizeof(char *));
235
 
236
    return error_codes[rc];
237
}
238
 
239
/** Process segment header.
240
 *
241
 * @param entry Segment header.
242
 * @param elf ELF header.
243
 *
244
 * @return EE_OK on success, error code otherwise.
245
 */
2964 svoboda 246
static int segment_header(elf_ld_t *elf, elf_segment_header_t *entry)
2928 svoboda 247
{
248
    switch (entry->p_type) {
249
    case PT_NULL:
250
    case PT_PHDR:
251
        break;
252
    case PT_LOAD:
2964 svoboda 253
        return load_segment(elf, entry);
2928 svoboda 254
        break;
255
    case PT_DYNAMIC:
256
    case PT_INTERP:
257
    case PT_SHLIB:
258
    case PT_NOTE:
259
    case PT_LOPROC:
260
    case PT_HIPROC:
261
    default:
262
        printf("segment p_type %d unknown\n", entry->p_type);
263
        return EE_UNSUPPORTED;
264
        break;
265
    }
266
    return EE_OK;
267
}
268
 
269
/** Load segment described by program header entry.
270
 *
271
 * @param entry Program header entry describing segment to be loaded.
272
 * @param elf ELF header.
273
 *
274
 * @return EE_OK on success, error code otherwise.
275
 */
2964 svoboda 276
int load_segment(elf_ld_t *elf, elf_segment_header_t *entry)
2928 svoboda 277
{
278
    void *a;
279
    int flags = 0;
2932 svoboda 280
    uintptr_t bias;
2928 svoboda 281
    int rc;
282
 
283
    printf("load segment at addr 0x%x, size 0x%x\n", entry->p_vaddr,
284
        entry->p_memsz);
285
 
2964 svoboda 286
    bias = elf->bias;
2928 svoboda 287
 
288
    if (entry->p_align > 1) {
289
        if ((entry->p_offset % entry->p_align) !=
290
            (entry->p_vaddr % entry->p_align)) {
291
            printf("align check 1 failed offset%%align=%d, vaddr%%align=%d\n",
292
            entry->p_offset % entry->p_align,
293
            entry->p_vaddr % entry->p_align
294
            );
295
            return EE_INVALID;
296
        }
297
    }
298
 
299
/*  if (entry->p_flags & PF_X)
300
        flags |= AS_AREA_EXEC;
301
    if (entry->p_flags & PF_W)
302
        flags |= AS_AREA_WRITE;
303
    if (entry->p_flags & PF_R)
304
        flags |= AS_AREA_READ;
305
    flags |= AS_AREA_CACHEABLE;
306
*/
307
    /* FIXME: Kernel won't normally allow this, unless you "patch" it */
2949 svoboda 308
//  flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_EXEC | AS_AREA_CACHEABLE;
309
    flags = AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE;
2928 svoboda 310
 
311
    /*
312
     * Check if the virtual address starts on page boundary.
313
     */
314
    if (ALIGN_UP(entry->p_vaddr, PAGE_SIZE) != entry->p_vaddr) {
315
        printf("align check 2 failed - not page-aligned\n");
316
        printf("vaddr = 0x%x, should be 0x%x\n",
317
            entry->p_vaddr, ALIGN_UP(entry->p_vaddr, PAGE_SIZE));
318
        return EE_UNSUPPORTED;
319
    }
320
 
2932 svoboda 321
    printf("map to p_vaddr=0x%x-0x%x...\n", entry->p_vaddr + bias,
322
    entry->p_vaddr + bias + ALIGN_UP(entry->p_memsz, PAGE_SIZE));
2928 svoboda 323
 
2932 svoboda 324
    a = as_area_create((uint8_t *)entry->p_vaddr + bias,
2928 svoboda 325
        entry->p_memsz, flags);
326
    if (a == (void *)(-1)) {
327
        printf("memory mapping failed\n");
328
        return EE_MEMORY;
329
    }
330
 
2949 svoboda 331
    printf("as_area_create(0x%x, 0x%x, %d) -> 0x%x\n",
332
        entry->p_vaddr+bias, entry->p_memsz, flags, (unsigned)a);
2928 svoboda 333
 
334
    /*
335
     * Load segment data
336
     */
2949 svoboda 337
    printf("seek to %d\n", entry->p_offset);
2964 svoboda 338
    rc = lseek(elf->fd, entry->p_offset, SEEK_SET);
2928 svoboda 339
    if (rc < 0) { printf("seek error\n"); return EE_INVALID; }
340
 
2949 svoboda 341
    printf("read 0x%x bytes to address 0x%x\n", entry->p_filesz, entry->p_vaddr+bias);
342
/*  rc = read(fd, (void *)(entry->p_vaddr + bias), entry->p_filesz);
343
    if (rc < 0) { printf("read error\n"); return EE_INVALID; }*/
344
    unsigned left, now;
345
    uint8_t *dp;
2928 svoboda 346
 
2949 svoboda 347
    left = entry->p_filesz;
348
    dp = (uint8_t *)(entry->p_vaddr + bias);
349
 
350
    while (left > 0) {
351
        now = 4096;
352
        if (now > left) now=left;
353
        printf("read %d...", now);
2964 svoboda 354
        rc = read(elf->fd, dp, now);
2949 svoboda 355
        if (rc < 0) { printf("read error\n"); return EE_INVALID; }
356
        printf("->%d\n", rc);
357
        left -= now;
358
        dp += now;
359
    }
360
 
2928 svoboda 361
    return EE_OK;
362
}
363
 
364
/** Process section header.
365
 *
366
 * @param entry Segment header.
367
 * @param elf ELF header.
368
 *
369
 * @return EE_OK on success, error code otherwise.
370
 */
2964 svoboda 371
static int section_header(elf_ld_t *elf, elf_section_header_t *entry)
2928 svoboda 372
{
373
    switch (entry->sh_type) {
374
    case SHT_PROGBITS:
375
        if (entry->sh_flags & SHF_TLS) {
376
            /* .tdata */
377
        }
378
        break;
379
    case SHT_NOBITS:
380
        if (entry->sh_flags & SHF_TLS) {
381
            /* .tbss */
382
        }
383
        break;
2961 svoboda 384
    case SHT_DYNAMIC:
2964 svoboda 385
        elf->info->dynamic =
386
            (void *)((uint8_t *)entry->sh_addr + elf->bias);
387
        printf("dynamic section found at 0x%x\n",
388
            (uintptr_t)elf->info->dynamic);
2961 svoboda 389
        break;
2928 svoboda 390
    default:
391
        break;
392
    }
393
 
394
    return EE_OK;
395
}
396
 
397
/** @}
398
 */