Subversion Repositories HelenOS

Rev

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

Rev 2999 Rev 3000
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 <string.h>
39
#include <string.h>
40
 
40
 
41
#include <rtld.h>
41
#include <rtld.h>
42
#include <symbol.h>
42
#include <symbol.h>
43
#include <elf.h>
43
#include <elf.h>
44
 
44
 
45
/*
45
/*
46
 * Hash tables are 32-bit (elf_word) even for 64-bit ELF files.
46
 * Hash tables are 32-bit (elf_word) even for 64-bit ELF files.
47
 */
47
 */
48
static elf_word elf_hash(const unsigned char *name)
48
static elf_word elf_hash(const unsigned char *name)
49
{
49
{
50
    elf_word h = 0, g;
50
    elf_word h = 0, g;
51
 
51
 
52
    while (*name) {
52
    while (*name) {
53
        h = (h << 4) + *name++;
53
        h = (h << 4) + *name++;
54
        g = h & 0xf0000000;
54
        g = h & 0xf0000000;
55
        if (g != 0) h ^= g >> 24;
55
        if (g != 0) h ^= g >> 24;
56
        h &= ~g;
56
        h &= ~g;
57
    }
57
    }
58
 
58
 
59
    return h;
59
    return h;
60
}
60
}
61
 
61
 
62
static elf_symbol_t *def_find_in_module(char *name, module_t *m)
62
static elf_symbol_t *def_find_in_module(char *name, module_t *m)
63
{
63
{
64
    elf_symbol_t *sym_table;
64
    elf_symbol_t *sym_table;
65
    elf_symbol_t *s, *sym;
65
    elf_symbol_t *s, *sym;
66
    elf_word nbucket;
66
    elf_word nbucket;
67
    elf_word nchain;
67
    elf_word nchain;
68
    elf_word i;
68
    elf_word i;
69
    char *s_name;
69
    char *s_name;
70
    elf_word bucket;
70
    elf_word bucket;
71
 
71
 
72
//  module_name = m->dyn.soname;
72
//  module_name = m->dyn.soname;
73
//  printf("def_find_in_module('%s', %s)\n", name, module_name);
73
//  printf("def_find_in_module('%s', %s)\n", name, module_name);
74
 
74
 
75
    sym_table = m->dyn.sym_tab;
75
    sym_table = m->dyn.sym_tab;
76
    nbucket = m->dyn.hash[0];
76
    nbucket = m->dyn.hash[0];
77
    nchain = m->dyn.hash[1];
77
    nchain = m->dyn.hash[1];
78
 
78
 
79
    bucket = elf_hash((unsigned char *)name) % nbucket;
79
    bucket = elf_hash((unsigned char *)name) % nbucket;
80
    i = m->dyn.hash[2 + bucket];
80
    i = m->dyn.hash[2 + bucket];
81
 
81
 
82
    sym = NULL;
82
    sym = NULL;
83
    while (i != STN_UNDEF) {
83
    while (i != STN_UNDEF) {
84
        s = &sym_table[i];
84
        s = &sym_table[i];
85
        s_name = m->dyn.str_tab + s->st_name;
85
        s_name = m->dyn.str_tab + s->st_name;
86
 
86
 
87
        if (strcmp(name, s_name) == 0) {
87
        if (strcmp(name, s_name) == 0) {
88
            sym = s;
88
            sym = s;
89
            break;
89
            break;
90
        }
90
        }
91
 
91
 
92
        i = m->dyn.hash[2 + nbucket + i];
92
        i = m->dyn.hash[2 + nbucket + i];
93
    }
93
    }
94
 
94
 
95
    if (!sym)
95
    if (!sym)
96
        return NULL;    /* Not found */
96
        return NULL;    /* Not found */
97
 
97
 
98
    if (sym->st_shndx == SHN_UNDEF) {
98
    if (sym->st_shndx == SHN_UNDEF) {
99
        /* Not a definition */
99
        /* Not a definition */
100
        return NULL;
100
        return NULL;
101
    }
101
    }
102
 
102
 
103
    return sym; /* Found */
103
    return sym; /* Found */
104
}
104
}
105
 
105
 
106
/** Find the definition of a symbol.
106
/** Find the definition of a symbol.
107
 *
107
 *
108
 * By definition in System V ABI, if module origin has the flag DT_SYMBOLIC,
108
 * By definition in System V ABI, if module origin has the flag DT_SYMBOLIC,
109
 * origin is searched first. Otherwise, or if the symbol hasn't been found,
109
 * origin is searched first. Otherwise, or if the symbol hasn't been found,
110
 * the module dependency graph is searched breadth-first, beginning
110
 * the module dependency graph is searched breadth-first, beginning
111
 * from the executable program.
111
 * from the executable program.
112
 *
112
 *
113
 * @param name      Name of the symbol to search for.
113
 * @param name      Name of the symbol to search for.
114
 * @param origin    Module in which the dependency originates.
114
 * @param origin    Module in which the dependency originates.
115
 * @param mod       (output) Will be filled with a pointer to the module
115
 * @param mod       (output) Will be filled with a pointer to the module
116
 *          that contains the symbol.
116
 *          that contains the symbol.
117
 */
117
 */
118
elf_symbol_t *symbol_def_find(char *name, module_t *origin, module_t **mod)
118
elf_symbol_t *symbol_def_find(char *name, module_t *origin, module_t **mod)
119
{
119
{
120
    module_t *m;
120
    module_t *m, *dm;
121
    elf_symbol_t *sym;
121
    elf_symbol_t *sym, *s;
-
 
122
    link_t queue_head;
-
 
123
    size_t i;
122
 
124
 
-
 
125
    if (origin->dyn.symbolic) {
-
 
126
        /*
123
    /* FIXME: support DT_SYMBOLIC */
127
         * Origin module has a DT_SYMBOLIC flag.
-
 
128
         * Try this module first
-
 
129
         */
-
 
130
         s = def_find_in_module(name, origin);
-
 
131
         if (s != NULL) {
-
 
132
            /* Found */
124
    //m = origin;
133
            *mod = origin;
-
 
134
            return s;
-
 
135
         }
-
 
136
    }
125
 
137
 
126
    /* Start in the executable program */
138
    /* Otherwise start in the executable program */
127
    m = runtime_env.program;
-
 
128
 
139
 
-
 
140
    /*
-
 
141
     * Do a BFS using the queue_link and bfs_tag fields.
-
 
142
     * Vertices (modules) are tagged the moment they are inserted
-
 
143
     * into the queue. This prevents from visiting the same vertex
-
 
144
     * more times in case of circular dependencies.
-
 
145
     */
-
 
146
 
-
 
147
    /* Mark all vertices (modules) as unvisited */ 
129
    while (true) {
148
    modules_untag();
-
 
149
 
-
 
150
    /* Insert root (the program) into the queue and tag it */
-
 
151
    list_initialize(&queue_head);
-
 
152
    runtime_env.program->bfs_tag = true;
-
 
153
    list_append(&runtime_env.program->queue_link, &queue_head);
-
 
154
 
-
 
155
    /* If the symbol is found, it will be stored in 'sym' */
-
 
156
    sym = NULL;
-
 
157
 
-
 
158
    /* While queue is not empty */
-
 
159
    while (!list_empty(&queue_head)) {
-
 
160
        /* Pop first element from the queue */
-
 
161
        m = list_get_instance(queue_head.next, module_t, queue_link);
-
 
162
        list_remove(&m->queue_link);
-
 
163
 
130
        sym = def_find_in_module(name, m);
164
        s = def_find_in_module(name, m);
131
        if (sym != NULL) {
165
        if (s != NULL) {
-
 
166
            /* Symbol found */
-
 
167
            sym = s;
132
            *mod = m;
168
            *mod = m;
133
            return sym;
169
            break;
134
        }
170
        }
135
 
171
 
-
 
172
        /*
-
 
173
         * Insert m's untagged dependencies into the queue
-
 
174
         * and tag them.
-
 
175
         */
-
 
176
        for (i = 0; i < m->n_deps; ++i) {
136
        if (m->n_deps < 1) break;
177
            dm = m->deps[i];
137
 
178
 
138
        /* FIXME: support branching */
179
            if (dm->bfs_tag == false) {
139
        if (m->n_deps > 1) {
180
                dm->bfs_tag = true;
140
            printf("error: BFS unimplemented\n");
181
                list_append(&dm->queue_link, &queue_head);
141
            exit(1);
182
            }
142
        }
183
        }
-
 
184
    }
143
 
185
 
-
 
186
    /* Empty the queue so that we leave it in a clean state */
-
 
187
    while (!list_empty(&queue_head))
-
 
188
        list_remove(queue_head.next);
-
 
189
 
-
 
190
    if (!sym) {
-
 
191
        printf("Error, symbol '%s' not found anywhere\n", name);
144
        m = m->deps[0];
192
        exit(1);
-
 
193
        return NULL; /* Not found */
145
    }
194
    }
146
 
195
 
147
    printf("Error, symbol '%s' not found anywhere\n", name);
-
 
148
    exit(1);
-
 
149
    return NULL; /* Not found */
196
    return sym; /* Symbol found */
150
}
197
}
151
 
198
 
152
uintptr_t symbol_get_addr(elf_symbol_t *sym, module_t *m)
199
uintptr_t symbol_get_addr(elf_symbol_t *sym, module_t *m)
153
{
200
{
154
    if (sym->st_shndx == SHN_ABS) {
201
    if (sym->st_shndx == SHN_ABS) {
155
        /* Do not add bias to absolute symbols */
202
        /* Do not add bias to absolute symbols */
156
        return sym->st_value;
203
        return sym->st_value;
157
    } else {
204
    } else {
158
        return sym->st_value + m->bias;
205
        return sym->st_value + m->bias;
159
    }
206
    }
160
}
207
}
161
 
208
 
162
/** @}
209
/** @}
163
 */
210
 */
164
 
211