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