Subversion Repositories HelenOS

Rev

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

Rev 3681 Rev 3686
1
/*
1
/*
2
 * Copyright (c) 2008 Jiri Svoboda
2
 * Copyright (c) 2008 Jiri Svoboda
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 rtld rtld
29
/** @addtogroup rtld rtld
30
 * @brief
30
 * @brief
31
 * @{
31
 * @{
32
 */
32
 */
33
/**
33
/**
34
 * @file
34
 * @file
35
 */
35
 */
36
 
36
 
37
#include <stdio.h>
37
#include <stdio.h>
38
#include <unistd.h>
38
#include <unistd.h>
39
#include <fcntl.h>
39
#include <fcntl.h>
40
#include <loader/pcb.h>
40
#include <loader/pcb.h>
41
 
41
 
42
/* from librtld */
42
/* from librtld */
43
#include <rtld.h>
43
#include <rtld.h>
44
#include <dynamic.h>
44
#include <dynamic.h>
45
#include <elf_load.h>
45
#include <elf_load.h>
46
#include <module.h>
46
#include <module.h>
47
 
47
 
48
void program_run(void *entry, pcb_t *pcb);
48
void program_run(void *entry, pcb_t *pcb);
49
 
49
 
-
 
50
runtime_env_t dload_re;
-
 
51
 
50
int main(int argc, char *argv[])
52
int main(int argc, char *argv[])
51
{
53
{
52
    static module_t prog;
54
    static module_t prog;
53
 
55
 
-
 
56
    runtime_env = &dload_re;
-
 
57
 
54
    DPRINTF("Hello, world! (from dload)\n");
58
    DPRINTF("Hello, world! (from dload)\n");
55
    if (__pcb->dynamic == NULL) {
59
    if (__pcb->dynamic == NULL) {
56
        printf("This is the dynamic loader. It is not supposed "
60
        printf("This is the dynamic loader. It is not supposed "
57
            "to be executed directly.\n");
61
            "to be executed directly.\n");
58
        return -1;
62
        return -1;
59
    }
63
    }
60
 
64
 
61
    /*
65
    /*
62
     * First we need to process dynamic sections of the executable
66
     * First we need to process dynamic sections of the executable
63
     * program and insert it into the module graph.
67
     * program and insert it into the module graph.
64
     */
68
     */
65
 
69
 
66
    DPRINTF("Parse program .dynamic section at 0x%x\n", __pcb->dynamic);
70
    DPRINTF("Parse program .dynamic section at 0x%x\n", __pcb->dynamic);
67
    dynamic_parse(__pcb->dynamic, 0, &prog.dyn);
71
    dynamic_parse(__pcb->dynamic, 0, &prog.dyn);
68
    prog.bias = 0;
72
    prog.bias = 0;
69
    prog.dyn.soname = "[program]";
73
    prog.dyn.soname = "[program]";
70
 
74
 
71
    /* Initialize list of loaded modules */
75
    /* Initialize list of loaded modules */
72
    list_initialize(&runtime_env.modules_head);
76
    list_initialize(&runtime_env->modules_head);
73
    list_append(&prog.modules_link, &runtime_env.modules_head);
77
    list_append(&prog.modules_link, &runtime_env->modules_head);
74
 
78
 
75
    /* Pointer to program module. Used as root of the module graph. */
79
    /* Pointer to program module. Used as root of the module graph. */
76
    runtime_env.program = &prog;
80
    runtime_env->program = &prog;
77
 
81
 
78
    /*
82
    /*
79
     * Now we can continue with loading all other modules.
83
     * Now we can continue with loading all other modules.
80
     */
84
     */
81
 
85
 
82
    DPRINTF("Load all program dependencies\n");
86
    DPRINTF("Load all program dependencies\n");
83
    module_load_deps(&prog);
87
    module_load_deps(&prog);
84
 
88
 
85
    /*
89
    /*
86
     * Now relocate/link all modules together.
90
     * Now relocate/link all modules together.
87
     */
91
     */
88
 
92
 
89
    /* Process relocations in all modules */
93
    /* Process relocations in all modules */
90
    DPRINTF("Relocate all modules\n");
94
    DPRINTF("Relocate all modules\n");
91
    modules_process_relocs();
95
    modules_process_relocs();
92
 
96
 
-
 
97
    /* Pass runtime evironment pointer through PCB. */
-
 
98
    __pcb->rtld_runtime = (void *) runtime_env;
-
 
99
 
93
    /*
100
    /*
94
     * Finally, run the main program.
101
     * Finally, run the main program.
95
     */
102
     */
96
    DPRINTF("Run program.. (at 0x%lx)\n", (uintptr_t)__pcb->entry);
103
    DPRINTF("Run program.. (at 0x%lx)\n", (uintptr_t)__pcb->entry);
97
 
104
 
98
#ifndef RTLD_DEBUG
105
#ifndef RTLD_DEBUG
99
    close_console();
106
    close_console();
100
#endif
107
#endif
101
    program_run(__pcb->entry, __pcb);
108
    program_run(__pcb->entry, __pcb);
102
 
109
 
103
    /* not reached */
110
    /* not reached */
104
    return 0;
111
    return 0;
105
}
112
}
106
 
113
 
107
/** @}
114
/** @}
108
 */
115
 */
109
 
116