Subversion Repositories HelenOS

Rev

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

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