Subversion Repositories HelenOS

Rev

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

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