Subversion Repositories HelenOS

Rev

Rev 4354 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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