[1ea99cc] | 1 | /*
|
---|
| 2 | * Copyright (c) 2008 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 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
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 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
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 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
|
---|
| 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
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup rtld rtld
|
---|
| 30 | * @brief
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[bfdb5af1] | 37 | #include <adt/list.h>
|
---|
| 38 | #include <elf/elf_load.h>
|
---|
[6adb775f] | 39 | #include <errno.h>
|
---|
[bfdb5af1] | 40 | #include <fcntl.h>
|
---|
| 41 | #include <loader/pcb.h>
|
---|
[1ea99cc] | 42 | #include <stdio.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
| 44 | #include <unistd.h>
|
---|
| 45 |
|
---|
[8a1fb09] | 46 | #include <rtld/rtld.h>
|
---|
| 47 | #include <rtld/rtld_debug.h>
|
---|
| 48 | #include <rtld/dynamic.h>
|
---|
| 49 | #include <rtld/rtld_arch.h>
|
---|
| 50 | #include <rtld/module.h>
|
---|
[1ea99cc] | 51 |
|
---|
| 52 | /** (Eagerly) process all relocation tables in a module.
|
---|
| 53 | *
|
---|
| 54 | * Currently works as if LD_BIND_NOW was specified.
|
---|
| 55 | */
|
---|
| 56 | void module_process_relocs(module_t *m)
|
---|
| 57 | {
|
---|
| 58 | DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
|
---|
| 59 |
|
---|
| 60 | /* Do not relocate twice. */
|
---|
| 61 | if (m->relocated) return;
|
---|
| 62 |
|
---|
| 63 | module_process_pre_arch(m);
|
---|
| 64 |
|
---|
[634e020] | 65 | /* jmp_rel table */
|
---|
| 66 | if (m->dyn.jmp_rel != NULL) {
|
---|
| 67 | DPRINTF("jmp_rel table\n");
|
---|
| 68 | if (m->dyn.plt_rel == DT_REL) {
|
---|
| 69 | DPRINTF("jmp_rel table type DT_REL\n");
|
---|
[1ea99cc] | 70 | rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
[634e020] | 71 | } else {
|
---|
| 72 | assert(m->dyn.plt_rel == DT_RELA);
|
---|
| 73 | DPRINTF("jmp_rel table type DT_RELA\n");
|
---|
| 74 | rela_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
[1ea99cc] | 75 | }
|
---|
[634e020] | 76 | }
|
---|
| 77 |
|
---|
| 78 | /* rel table */
|
---|
| 79 | if (m->dyn.rel != NULL) {
|
---|
| 80 | DPRINTF("rel table\n");
|
---|
| 81 | rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /* rela table */
|
---|
| 85 | if (m->dyn.rela != NULL) {
|
---|
| 86 | DPRINTF("rela table\n");
|
---|
| 87 | rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
|
---|
[1ea99cc] | 88 | }
|
---|
| 89 |
|
---|
| 90 | m->relocated = true;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /** Find module structure by soname/pathname.
|
---|
| 94 | *
|
---|
| 95 | * Used primarily to see if a module has already been loaded.
|
---|
| 96 | * Modules are compared according to their soname, i.e. possible
|
---|
| 97 | * path components are ignored.
|
---|
| 98 | */
|
---|
[17341d4] | 99 | module_t *module_find(rtld_t *rtld, const char *name)
|
---|
[1ea99cc] | 100 | {
|
---|
[04803bf] | 101 | const char *p, *soname;
|
---|
[1ea99cc] | 102 |
|
---|
[a6dffb8] | 103 | DPRINTF("module_find('%s')\n", name);
|
---|
| 104 |
|
---|
[1ea99cc] | 105 | /*
|
---|
| 106 | * If name contains slashes, treat it as a pathname and
|
---|
| 107 | * construct soname by chopping off the path. Otherwise
|
---|
| 108 | * treat it as soname.
|
---|
| 109 | */
|
---|
| 110 | p = str_rchr(name, '/');
|
---|
| 111 | soname = p ? (p + 1) : name;
|
---|
| 112 |
|
---|
| 113 | /* Traverse list of all modules. Not extremely fast, but simple */
|
---|
[17341d4] | 114 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[4b63316] | 115 | DPRINTF("m = %p\n", m);
|
---|
[1ea99cc] | 116 | if (str_cmp(m->dyn.soname, soname) == 0) {
|
---|
| 117 | return m; /* Found */
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
[5035ba05] | 120 |
|
---|
[1ea99cc] | 121 | return NULL; /* Not found */
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | #define NAME_BUF_SIZE 64
|
---|
| 125 |
|
---|
| 126 | /** Load a module.
|
---|
| 127 | *
|
---|
| 128 | * Currently this trivially tries to load '/<name>'.
|
---|
| 129 | */
|
---|
[5035ba05] | 130 | module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
|
---|
[1ea99cc] | 131 | {
|
---|
[17341d4] | 132 | elf_finfo_t info;
|
---|
[1ea99cc] | 133 | char name_buf[NAME_BUF_SIZE];
|
---|
| 134 | module_t *m;
|
---|
| 135 | int rc;
|
---|
[5035ba05] | 136 |
|
---|
[6adb775f] | 137 | m = calloc(1, sizeof(module_t));
|
---|
| 138 | if (m == NULL) {
|
---|
[1ea99cc] | 139 | printf("malloc failed\n");
|
---|
| 140 | exit(1);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[17341d4] | 143 | m->rtld = rtld;
|
---|
[6adb775f] | 144 | m->id = rtld_get_next_id(rtld);
|
---|
[17341d4] | 145 |
|
---|
[5035ba05] | 146 | if ((flags & mlf_local) != 0)
|
---|
| 147 | m->local = true;
|
---|
[17341d4] | 148 |
|
---|
[1ea99cc] | 149 | if (str_size(name) > NAME_BUF_SIZE - 2) {
|
---|
| 150 | printf("soname too long. increase NAME_BUF_SIZE\n");
|
---|
| 151 | exit(1);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /* Prepend soname with '/lib/' */
|
---|
| 155 | str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
|
---|
| 156 | str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
|
---|
| 157 |
|
---|
| 158 | /* FIXME: need to real allocation of address space */
|
---|
[17341d4] | 159 | m->bias = rtld->next_bias;
|
---|
| 160 | rtld->next_bias += 0x100000;
|
---|
[1ea99cc] | 161 |
|
---|
| 162 | DPRINTF("filename:'%s'\n", name_buf);
|
---|
[5d4763b] | 163 | DPRINTF("load '%s' at 0x%x\n", name_buf, m->bias);
|
---|
[1ea99cc] | 164 |
|
---|
| 165 | rc = elf_load_file(name_buf, m->bias, ELDF_RW, &info);
|
---|
| 166 | if (rc != EE_OK) {
|
---|
| 167 | printf("Failed to load '%s'\n", name_buf);
|
---|
| 168 | exit(1);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | if (info.dynamic == NULL) {
|
---|
| 172 | printf("Error: '%s' is not a dynamically-linked object.\n",
|
---|
| 173 | name_buf);
|
---|
| 174 | exit(1);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | /* Pending relocation. */
|
---|
| 178 | m->relocated = false;
|
---|
| 179 |
|
---|
| 180 | DPRINTF("parse dynamic section\n");
|
---|
| 181 | /* Parse ELF .dynamic section. Store info to m->dyn. */
|
---|
| 182 | dynamic_parse(info.dynamic, m->bias, &m->dyn);
|
---|
| 183 |
|
---|
| 184 | /* Insert into the list of loaded modules */
|
---|
[17341d4] | 185 | list_append(&m->modules_link, &rtld->modules);
|
---|
[1ea99cc] | 186 |
|
---|
[6adb775f] | 187 | /* Copy TLS info */
|
---|
| 188 | m->tdata = info.tls.tdata;
|
---|
| 189 | m->tdata_size = info.tls.tdata_size;
|
---|
| 190 | m->tbss_size = info.tls.tbss_size;
|
---|
[29405ac] | 191 | m->tls_align = info.tls.tls_align;
|
---|
[6adb775f] | 192 |
|
---|
| 193 | printf("tdata at %p size %zu, tbss size %zu\n",
|
---|
| 194 | m->tdata, m->tdata_size, m->tbss_size);
|
---|
| 195 |
|
---|
[1ea99cc] | 196 | return m;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /** Load all modules on which m (transitively) depends.
|
---|
| 200 | */
|
---|
[5035ba05] | 201 | void module_load_deps(module_t *m, mlflags_t flags)
|
---|
[1ea99cc] | 202 | {
|
---|
| 203 | elf_dyn_t *dp;
|
---|
| 204 | char *dep_name;
|
---|
| 205 | module_t *dm;
|
---|
| 206 | size_t n, i;
|
---|
| 207 |
|
---|
[a6dffb8] | 208 | DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
|
---|
| 209 |
|
---|
[1ea99cc] | 210 | /* Count direct dependencies */
|
---|
| 211 |
|
---|
| 212 | dp = m->dyn.dynamic;
|
---|
| 213 | n = 0;
|
---|
| 214 |
|
---|
| 215 | while (dp->d_tag != DT_NULL) {
|
---|
| 216 | if (dp->d_tag == DT_NEEDED) ++n;
|
---|
| 217 | ++dp;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /* Create an array of pointers to direct dependencies */
|
---|
| 221 |
|
---|
| 222 | m->n_deps = n;
|
---|
| 223 |
|
---|
| 224 | if (n == 0) {
|
---|
| 225 | /* There are no dependencies, so we are done. */
|
---|
| 226 | m->deps = NULL;
|
---|
| 227 | return;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | m->deps = malloc(n * sizeof(module_t *));
|
---|
| 231 | if (!m->deps) {
|
---|
| 232 | printf("malloc failed\n");
|
---|
| 233 | exit(1);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | i = 0; /* Current dependency index */
|
---|
| 237 | dp = m->dyn.dynamic;
|
---|
| 238 |
|
---|
| 239 | while (dp->d_tag != DT_NULL) {
|
---|
| 240 | if (dp->d_tag == DT_NEEDED) {
|
---|
| 241 | dep_name = m->dyn.str_tab + dp->d_un.d_val;
|
---|
| 242 |
|
---|
| 243 | DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
|
---|
[17341d4] | 244 | dm = module_find(m->rtld, dep_name);
|
---|
[1ea99cc] | 245 | if (!dm) {
|
---|
[5035ba05] | 246 | dm = module_load(m->rtld, dep_name, flags);
|
---|
| 247 | module_load_deps(dm, flags);
|
---|
[1ea99cc] | 248 | }
|
---|
| 249 |
|
---|
| 250 | /* Save into deps table */
|
---|
| 251 | m->deps[i++] = dm;
|
---|
| 252 | }
|
---|
| 253 | ++dp;
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[6adb775f] | 257 | /** Find module structure by ID. */
|
---|
| 258 | module_t *module_by_id(rtld_t *rtld, unsigned long id)
|
---|
| 259 | {
|
---|
| 260 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 261 | if (m->id == id)
|
---|
| 262 | return m;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | return NULL;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
[1ea99cc] | 268 | /** Process relocations in modules.
|
---|
| 269 | *
|
---|
| 270 | * Processes relocations in @a start and all its dependencies.
|
---|
| 271 | * Modules that have already been relocated are unaffected.
|
---|
| 272 | *
|
---|
| 273 | * @param start The module where to start from.
|
---|
| 274 | */
|
---|
[17341d4] | 275 | void modules_process_relocs(rtld_t *rtld, module_t *start)
|
---|
[1ea99cc] | 276 | {
|
---|
[17341d4] | 277 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 278 | /* Skip rtld module, since it has already been processed */
|
---|
| 279 | if (m != &rtld->rtld) {
|
---|
[1ea99cc] | 280 | module_process_relocs(m);
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[6adb775f] | 285 | void modules_process_tls(rtld_t *rtld)
|
---|
| 286 | {
|
---|
[29405ac] | 287 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
[6adb775f] | 288 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 289 | m->ioffs = rtld->tls_size;
|
---|
[bab0f42] | 290 | list_append(&m->imodules_link, &rtmd->imodules);
|
---|
[6adb775f] | 291 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
| 292 | }
|
---|
[29405ac] | 293 | #else /* CONFIG_TLS_VARIANT_2 */
|
---|
| 294 | size_t offs;
|
---|
| 295 |
|
---|
| 296 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 297 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | offs = 0;
|
---|
| 301 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 302 | offs += m->tdata_size + m->tbss_size;
|
---|
| 303 | m->ioffs = rtld->tls_size - offs;
|
---|
[bab0f42] | 304 | list_append(&m->imodules_link, &rtld->imodules);
|
---|
[29405ac] | 305 | }
|
---|
| 306 | #endif
|
---|
[6adb775f] | 307 | }
|
---|
| 308 |
|
---|
[1ea99cc] | 309 | /** Clear BFS tags of all modules.
|
---|
| 310 | */
|
---|
[17341d4] | 311 | void modules_untag(rtld_t *rtld)
|
---|
[1ea99cc] | 312 | {
|
---|
[17341d4] | 313 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[1ea99cc] | 314 | m->bfs_tag = false;
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | /** @}
|
---|
| 319 | */
|
---|