Subversion Repositories HelenOS

Rev

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

Rev 3688 Rev 3689
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 <stdlib.h>
38
#include <stdlib.h>
39
#include <unistd.h>
39
#include <unistd.h>
40
#include <fcntl.h>
40
#include <fcntl.h>
41
#include <libadt/list.h>
41
#include <libadt/list.h>
42
#include <loader/pcb.h>
42
#include <loader/pcb.h>
43
 
43
 
44
#include <rtld.h>
44
#include <rtld.h>
45
#include <dynamic.h>
45
#include <dynamic.h>
46
#include <elf_load.h>
46
#include <elf_load.h>
47
#include <rtld_arch.h>
47
#include <rtld_arch.h>
48
#include <module.h>
48
#include <module.h>
49
 
49
 
50
/** (Eagerly) process all relocation tables in a module.
50
/** (Eagerly) process all relocation tables in a module.
51
 *
51
 *
52
 * Currently works as if LD_BIND_NOW was specified.
52
 * Currently works as if LD_BIND_NOW was specified.
53
 */
53
 */
54
void module_process_relocs(module_t *m)
54
void module_process_relocs(module_t *m)
55
{
55
{
56
    DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
56
    DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
-
 
57
 
-
 
58
    /* Do not relocate twice. */
-
 
59
    if (m->relocated) return;
-
 
60
 
57
    if (m->dyn.plt_rel == DT_REL) {
61
    if (m->dyn.plt_rel == DT_REL) {
58
        if (m->dyn.rel != NULL)
62
        if (m->dyn.rel != NULL)
59
            rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
63
            rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
60
        /* FIXME: this seems wrong */
64
        /* FIXME: this seems wrong */
61
        if (m->dyn.jmp_rel != NULL)
65
        if (m->dyn.jmp_rel != NULL)
62
            rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
66
            rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
63
    } else { /* (m->dyn.plt_rel == DT_RELA) */
67
    } else { /* (m->dyn.plt_rel == DT_RELA) */
64
        DPRINTF("table type DT_RELA\n");
68
        DPRINTF("table type DT_RELA\n");
65
        if (m->dyn.rela != NULL) {
69
        if (m->dyn.rela != NULL) {
66
            DPRINTF("non-empty\n");
70
            DPRINTF("non-empty\n");
67
            rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
71
            rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
68
        }
72
        }
69
    }
73
    }
-
 
74
 
-
 
75
    m->relocated = true;
70
}
76
}
71
 
77
 
72
/** Find module structure by soname/pathname.
78
/** Find module structure by soname/pathname.
73
 *
79
 *
74
 * Used primarily to see if a module has already been loaded.
80
 * Used primarily to see if a module has already been loaded.
75
 * Modules are compared according to their soname, i.e. possible
81
 * Modules are compared according to their soname, i.e. possible
76
 * path components are ignored.
82
 * path components are ignored.
77
 */
83
 */
78
module_t *module_find(char *name)
84
module_t *module_find(char *name)
79
{
85
{
80
    link_t *head = &runtime_env->modules_head;
86
    link_t *head = &runtime_env->modules_head;
81
 
87
 
82
    link_t *cur;
88
    link_t *cur;
83
    module_t *m;
89
    module_t *m;
84
    char *p, *soname;
90
    char *p, *soname;
85
 
91
 
86
    /*
92
    /*
87
     * If name contains slashes, treat it as a pathname and
93
     * If name contains slashes, treat it as a pathname and
88
     * construct soname by chopping off the path. Otherwise
94
     * construct soname by chopping off the path. Otherwise
89
     * treat it as soname.
95
     * treat it as soname.
90
     */
96
     */
91
    p = strrchr(name, '/');
97
    p = strrchr(name, '/');
92
    soname = p ? (p + 1) : name;
98
    soname = p ? (p + 1) : name;
93
 
99
 
94
    /* Traverse list of all modules. Not extremely fast, but simple */
100
    /* Traverse list of all modules. Not extremely fast, but simple */
95
    for (cur = head->next; cur != head; cur = cur->next) {
101
    for (cur = head->next; cur != head; cur = cur->next) {
96
        m = list_get_instance(cur, module_t, modules_link);
102
        m = list_get_instance(cur, module_t, modules_link);
97
        if (strcmp(m->dyn.soname, soname) == 0) {
103
        if (strcmp(m->dyn.soname, soname) == 0) {
98
            return m; /* Found */
104
            return m; /* Found */
99
        }
105
        }
100
    }
106
    }
101
   
107
   
102
    return NULL; /* Not found */
108
    return NULL; /* Not found */
103
}
109
}
104
 
110
 
105
#define NAME_BUF_SIZE 64
111
#define NAME_BUF_SIZE 64
106
 
112
 
107
/** Load a module.
113
/** Load a module.
108
 *
114
 *
109
 * Currently this trivially tries to load '/<name>'.
115
 * Currently this trivially tries to load '/<name>'.
110
 */
116
 */
111
module_t *module_load(char *name)
117
module_t *module_load(char *name)
112
{
118
{
113
    elf_info_t info;
119
    elf_info_t info;
114
    char name_buf[NAME_BUF_SIZE];
120
    char name_buf[NAME_BUF_SIZE];
115
    module_t *m;
121
    module_t *m;
116
    int rc;
122
    int rc;
117
   
123
   
118
    m = malloc(sizeof(module_t));
124
    m = malloc(sizeof(module_t));
119
    if (!m) {
125
    if (!m) {
120
        printf("malloc failed\n");
126
        printf("malloc failed\n");
121
        exit(1);
127
        exit(1);
122
    }
128
    }
123
 
129
 
124
    if (strlen(name) > NAME_BUF_SIZE - 2) {
130
    if (strlen(name) > NAME_BUF_SIZE - 2) {
125
        printf("soname too long. increase NAME_BUF_SIZE\n");
131
        printf("soname too long. increase NAME_BUF_SIZE\n");
126
        exit(1);
132
        exit(1);
127
    }
133
    }
128
 
134
 
129
    /* Prepend soname with '/lib/' */
135
    /* Prepend soname with '/lib/' */
130
    name_buf[0] = '/';
136
    name_buf[0] = '/';
131
    strcpy(name_buf, "/lib/");
137
    strcpy(name_buf, "/lib/");
132
    strcpy(name_buf + 5, name);
138
    strcpy(name_buf + 5, name);
133
 
139
 
134
    /* FIXME: need to vary bias / allocate address space */
140
    /* FIXME: need to real allocation of address space */
-
 
141
    m->bias = runtime_env->next_bias;
135
    m->bias = 0x20000;
142
    runtime_env->next_bias += 0x100000;
-
 
143
 
136
    DPRINTF("filename:'%s'\n", name_buf);
144
    DPRINTF("filename:'%s'\n", name_buf);
137
 
145
 
138
    rc = elf_load_file(name_buf, m->bias, ELDF_RW, &info);
146
    rc = elf_load_file(name_buf, m->bias, ELDF_RW, &info);
139
    if (rc < 0) {
147
    if (rc != EE_OK) {
140
        printf("Failed to load '%s'\n", name_buf);
148
        printf("Failed to load '%s'\n", name_buf);
141
        exit(1);
149
        exit(1);
142
    }
150
    }
143
 
151
 
-
 
152
    /* Pending relocation. */
-
 
153
    m->relocated = false;
-
 
154
 
144
    DPRINTF("parse dynamic section\n");
155
    DPRINTF("parse dynamic section\n");
145
    /* Parse ELF .dynamic section. Store info to m->dyn. */
156
    /* Parse ELF .dynamic section. Store info to m->dyn. */
146
    dynamic_parse(info.dynamic, m->bias, &m->dyn);
157
    dynamic_parse(info.dynamic, m->bias, &m->dyn);
147
 
158
 
148
    /* Insert into the list of loaded modules */
159
    /* Insert into the list of loaded modules */
149
    list_append(&m->modules_link, &runtime_env->modules_head);
160
    list_append(&m->modules_link, &runtime_env->modules_head);
150
 
161
 
151
    return m;
162
    return m;
152
}
163
}
153
 
164
 
154
/** Load all modules on which m (transitively) depends.
165
/** Load all modules on which m (transitively) depends.
155
 */
166
 */
156
void module_load_deps(module_t *m)
167
void module_load_deps(module_t *m)
157
{
168
{
158
    elf_dyn_t *dp;
169
    elf_dyn_t *dp;
159
    char *dep_name;
170
    char *dep_name;
160
    module_t *dm;
171
    module_t *dm;
161
    size_t n, i;
172
    size_t n, i;
162
 
173
 
163
    /* Count direct dependencies */
174
    /* Count direct dependencies */
164
   
175
   
165
    dp = m->dyn.dynamic;
176
    dp = m->dyn.dynamic;
166
    n = 0;
177
    n = 0;
167
 
178
 
168
    while (dp->d_tag != DT_NULL) {
179
    while (dp->d_tag != DT_NULL) {
169
        if (dp->d_tag == DT_NEEDED) ++n;
180
        if (dp->d_tag == DT_NEEDED) ++n;
170
        ++dp;
181
        ++dp;
171
    }
182
    }
172
 
183
 
173
    /* Create an array of pointers to direct dependencies */
184
    /* Create an array of pointers to direct dependencies */
174
 
185
 
175
    m->n_deps = n;
186
    m->n_deps = n;
176
 
187
 
177
    if (n == 0) {
188
    if (n == 0) {
178
        /* There are no dependencies, so we are done. */
189
        /* There are no dependencies, so we are done. */
179
        m->deps = NULL;
190
        m->deps = NULL;
180
        return;
191
        return;
181
    }
192
    }
182
 
193
 
183
    m->deps = malloc(n * sizeof(module_t *));
194
    m->deps = malloc(n * sizeof(module_t *));
184
    if (!m->deps) {
195
    if (!m->deps) {
185
        printf("malloc failed\n");
196
        printf("malloc failed\n");
186
        exit(1);
197
        exit(1);
187
    }
198
    }
188
 
199
 
189
    i = 0; /* Current dependency index */
200
    i = 0; /* Current dependency index */
190
    dp = m->dyn.dynamic;
201
    dp = m->dyn.dynamic;
191
 
202
 
192
    while (dp->d_tag != DT_NULL) {
203
    while (dp->d_tag != DT_NULL) {
193
        if (dp->d_tag == DT_NEEDED) {
204
        if (dp->d_tag == DT_NEEDED) {
194
            dep_name = m->dyn.str_tab + dp->d_un.d_val;
205
            dep_name = m->dyn.str_tab + dp->d_un.d_val;
195
 
206
 
196
            DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
207
            DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
197
            dm = module_find(dep_name);
208
            dm = module_find(dep_name);
198
            if (!dm) {
209
            if (!dm) {
199
                dm = module_load(dep_name);
210
                dm = module_load(dep_name);
200
                module_load_deps(dm);
211
                module_load_deps(dm);
201
            }
212
            }
202
 
213
 
203
            /* Save into deps table */
214
            /* Save into deps table */
204
            m->deps[i++] = dm;
215
            m->deps[i++] = dm;
205
        }
216
        }
206
        ++dp;
217
        ++dp;
207
    }
218
    }
208
}
219
}
209
 
220
 
-
 
221
/** Process relocations in modules.
-
 
222
 *
-
 
223
 * Processes relocations in @a start and all its dependencies.
-
 
224
 * Modules that have already been relocated are unaffected.
-
 
225
 *
-
 
226
 * @param   start   The module where to start from.
-
 
227
 */
210
void modules_process_relocs(void)
228
void modules_process_relocs(module_t *start)
211
{
229
{
212
    link_t *head = &runtime_env->modules_head;
230
    link_t *head = &runtime_env->modules_head;
213
 
231
 
214
    link_t *cur;
232
    link_t *cur;
215
    module_t *m;
233
    module_t *m;
216
 
234
 
217
    for (cur = head->next; cur != head; cur = cur->next) {
235
    for (cur = head->next; cur != head; cur = cur->next) {
218
        m = list_get_instance(cur, module_t, modules_link);
236
        m = list_get_instance(cur, module_t, modules_link);
219
 
237
 
220
        /* Skip rtld, since it has already been processed */
238
        /* Skip rtld, since it has already been processed */
221
        if (m != &runtime_env->rtld) {
239
        if (m != &runtime_env->rtld) {
222
            module_process_relocs(m);
240
            module_process_relocs(m);
223
        }
241
        }
224
    }
242
    }
225
}
243
}
226
 
244
 
227
/** Clear BFS tags of all modules.
245
/** Clear BFS tags of all modules.
228
 */
246
 */
229
void modules_untag(void)
247
void modules_untag(void)
230
{
248
{
231
    link_t *head = &runtime_env->modules_head;
249
    link_t *head = &runtime_env->modules_head;
232
 
250
 
233
    link_t *cur;
251
    link_t *cur;
234
    module_t *m;
252
    module_t *m;
235
 
253
 
236
    for (cur = head->next; cur != head; cur = cur->next) {
254
    for (cur = head->next; cur != head; cur = cur->next) {
237
        m = list_get_instance(cur, module_t, modules_link);
255
        m = list_get_instance(cur, module_t, modules_link);
238
        m->bfs_tag = false;
256
        m->bfs_tag = false;
239
    }
257
    }
240
}
258
}
241
 
259
 
242
/** @}
260
/** @}
243
 */
261
 */
244
 
262