[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 |
|
---|
[b1834a01] | 29 | /** @addtogroup rtld
|
---|
[1ea99cc] | 30 | * @brief
|
---|
| 31 | * @{
|
---|
[1b20da0] | 32 | */
|
---|
[1ea99cc] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[4f205248] | 37 | #include <align.h>
|
---|
[bfdb5af1] | 38 | #include <adt/list.h>
|
---|
| 39 | #include <elf/elf_load.h>
|
---|
[6adb775f] | 40 | #include <errno.h>
|
---|
[bfdb5af1] | 41 | #include <loader/pcb.h>
|
---|
[1ea99cc] | 42 | #include <stdio.h>
|
---|
| 43 | #include <stdlib.h>
|
---|
[1d6dd2a] | 44 | #include <str.h>
|
---|
[4f205248] | 45 | #include <macros.h>
|
---|
[1ea99cc] | 46 |
|
---|
[8a1fb09] | 47 | #include <rtld/rtld.h>
|
---|
| 48 | #include <rtld/rtld_debug.h>
|
---|
| 49 | #include <rtld/dynamic.h>
|
---|
| 50 | #include <rtld/rtld_arch.h>
|
---|
| 51 | #include <rtld/module.h>
|
---|
[1567471] | 52 | #include <libarch/rtld/module.h>
|
---|
[1ea99cc] | 53 |
|
---|
[2eadda9] | 54 | #include "../private/libc.h"
|
---|
| 55 |
|
---|
[153c7a29] | 56 | /** Create module for static executable.
|
---|
| 57 | *
|
---|
| 58 | * @param rtld Run-time dynamic linker
|
---|
| 59 | * @param rmodule Place to store pointer to new module or @c NULL
|
---|
| 60 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 61 | */
|
---|
[b7fd2a0] | 62 | errno_t module_create_static_exec(rtld_t *rtld, module_t **rmodule)
|
---|
[153c7a29] | 63 | {
|
---|
| 64 | module_t *module;
|
---|
| 65 |
|
---|
| 66 | module = calloc(1, sizeof(module_t));
|
---|
[87bc11f] | 67 | if (module == NULL) {
|
---|
| 68 | DPRINTF("malloc failed\n");
|
---|
[153c7a29] | 69 | return ENOMEM;
|
---|
[87bc11f] | 70 | }
|
---|
[153c7a29] | 71 |
|
---|
| 72 | module->id = rtld_get_next_id(rtld);
|
---|
| 73 | module->dyn.soname = "[program]";
|
---|
| 74 |
|
---|
| 75 | module->rtld = rtld;
|
---|
| 76 | module->exec = true;
|
---|
| 77 | module->local = true;
|
---|
| 78 |
|
---|
[2c4e1cc] | 79 | const elf_segment_header_t *tls =
|
---|
[2eadda9] | 80 | elf_get_phdr(__progsymbols.elfstart, PT_TLS);
|
---|
[2c4e1cc] | 81 |
|
---|
[4f205248] | 82 | if (tls) {
|
---|
[2eadda9] | 83 | uintptr_t bias = elf_get_bias(__progsymbols.elfstart);
|
---|
[4f205248] | 84 | module->tdata = (void *) (tls->p_vaddr + bias);
|
---|
| 85 | module->tdata_size = tls->p_filesz;
|
---|
| 86 | module->tbss_size = tls->p_memsz - tls->p_filesz;
|
---|
| 87 | module->tls_align = tls->p_align;
|
---|
| 88 | } else {
|
---|
| 89 | module->tdata = NULL;
|
---|
| 90 | module->tdata_size = 0;
|
---|
| 91 | module->tbss_size = 0;
|
---|
| 92 | module->tls_align = 1;
|
---|
| 93 | }
|
---|
[153c7a29] | 94 |
|
---|
| 95 | list_append(&module->modules_link, &rtld->modules);
|
---|
| 96 |
|
---|
| 97 | if (rmodule != NULL)
|
---|
| 98 | *rmodule = module;
|
---|
| 99 | return EOK;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[1ea99cc] | 102 | /** (Eagerly) process all relocation tables in a module.
|
---|
| 103 | *
|
---|
| 104 | * Currently works as if LD_BIND_NOW was specified.
|
---|
| 105 | */
|
---|
| 106 | void module_process_relocs(module_t *m)
|
---|
| 107 | {
|
---|
| 108 | DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
|
---|
| 109 |
|
---|
| 110 | /* Do not relocate twice. */
|
---|
[1433ecda] | 111 | if (m->relocated)
|
---|
| 112 | return;
|
---|
[1ea99cc] | 113 |
|
---|
| 114 | module_process_pre_arch(m);
|
---|
| 115 |
|
---|
[634e020] | 116 | /* jmp_rel table */
|
---|
| 117 | if (m->dyn.jmp_rel != NULL) {
|
---|
| 118 | DPRINTF("jmp_rel table\n");
|
---|
| 119 | if (m->dyn.plt_rel == DT_REL) {
|
---|
| 120 | DPRINTF("jmp_rel table type DT_REL\n");
|
---|
[1ea99cc] | 121 | rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
[634e020] | 122 | } else {
|
---|
| 123 | assert(m->dyn.plt_rel == DT_RELA);
|
---|
| 124 | DPRINTF("jmp_rel table type DT_RELA\n");
|
---|
| 125 | rela_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
[1ea99cc] | 126 | }
|
---|
[634e020] | 127 | }
|
---|
| 128 |
|
---|
| 129 | /* rel table */
|
---|
| 130 | if (m->dyn.rel != NULL) {
|
---|
| 131 | DPRINTF("rel table\n");
|
---|
| 132 | rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /* rela table */
|
---|
| 136 | if (m->dyn.rela != NULL) {
|
---|
| 137 | DPRINTF("rela table\n");
|
---|
| 138 | rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
|
---|
[1ea99cc] | 139 | }
|
---|
| 140 |
|
---|
| 141 | m->relocated = true;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** Find module structure by soname/pathname.
|
---|
| 145 | *
|
---|
| 146 | * Used primarily to see if a module has already been loaded.
|
---|
| 147 | * Modules are compared according to their soname, i.e. possible
|
---|
| 148 | * path components are ignored.
|
---|
| 149 | */
|
---|
[17341d4] | 150 | module_t *module_find(rtld_t *rtld, const char *name)
|
---|
[1ea99cc] | 151 | {
|
---|
[04803bf] | 152 | const char *p, *soname;
|
---|
[1ea99cc] | 153 |
|
---|
[a6dffb8] | 154 | DPRINTF("module_find('%s')\n", name);
|
---|
| 155 |
|
---|
[1ea99cc] | 156 | /*
|
---|
| 157 | * If name contains slashes, treat it as a pathname and
|
---|
| 158 | * construct soname by chopping off the path. Otherwise
|
---|
| 159 | * treat it as soname.
|
---|
| 160 | */
|
---|
| 161 | p = str_rchr(name, '/');
|
---|
| 162 | soname = p ? (p + 1) : name;
|
---|
| 163 |
|
---|
| 164 | /* Traverse list of all modules. Not extremely fast, but simple */
|
---|
[17341d4] | 165 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[4b63316] | 166 | DPRINTF("m = %p\n", m);
|
---|
[1ea99cc] | 167 | if (str_cmp(m->dyn.soname, soname) == 0) {
|
---|
| 168 | return m; /* Found */
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
[a35b458] | 171 |
|
---|
[1ea99cc] | 172 | return NULL; /* Not found */
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | #define NAME_BUF_SIZE 64
|
---|
| 176 |
|
---|
| 177 | /** Load a module.
|
---|
| 178 | *
|
---|
| 179 | * Currently this trivially tries to load '/<name>'.
|
---|
| 180 | */
|
---|
[5035ba05] | 181 | module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
|
---|
[1ea99cc] | 182 | {
|
---|
[17341d4] | 183 | elf_finfo_t info;
|
---|
[1ea99cc] | 184 | char name_buf[NAME_BUF_SIZE];
|
---|
| 185 | module_t *m;
|
---|
[87bc11f] | 186 | errno_t rc;
|
---|
[a35b458] | 187 |
|
---|
[6adb775f] | 188 | m = calloc(1, sizeof(module_t));
|
---|
| 189 | if (m == NULL) {
|
---|
[87bc11f] | 190 | DPRINTF("malloc failed\n");
|
---|
[985e0f15] | 191 | goto error;
|
---|
[1ea99cc] | 192 | }
|
---|
[a35b458] | 193 |
|
---|
[17341d4] | 194 | m->rtld = rtld;
|
---|
[6adb775f] | 195 | m->id = rtld_get_next_id(rtld);
|
---|
[17341d4] | 196 |
|
---|
[5035ba05] | 197 | if ((flags & mlf_local) != 0)
|
---|
| 198 | m->local = true;
|
---|
[17341d4] | 199 |
|
---|
[1ea99cc] | 200 | if (str_size(name) > NAME_BUF_SIZE - 2) {
|
---|
[87bc11f] | 201 | DPRINTF("soname too long. increase NAME_BUF_SIZE\n");
|
---|
[985e0f15] | 202 | goto error;
|
---|
[1ea99cc] | 203 | }
|
---|
| 204 |
|
---|
| 205 | /* Prepend soname with '/lib/' */
|
---|
| 206 | str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
|
---|
| 207 | str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
|
---|
| 208 |
|
---|
| 209 | DPRINTF("filename:'%s'\n", name_buf);
|
---|
| 210 |
|
---|
[1567471] | 211 | rc = elf_load_file_name(name_buf, RTLD_MODULE_LDF, &info);
|
---|
[87bc11f] | 212 | if (rc != EOK) {
|
---|
| 213 | DPRINTF("Failed to load '%s'\n", name_buf);
|
---|
[985e0f15] | 214 | goto error;
|
---|
[1ea99cc] | 215 | }
|
---|
| 216 |
|
---|
[742fc98e] | 217 | m->bias = elf_get_bias(info.base);
|
---|
| 218 |
|
---|
| 219 | DPRINTF("loaded '%s' at 0x%zx\n", name_buf, m->bias);
|
---|
| 220 |
|
---|
[1ea99cc] | 221 | if (info.dynamic == NULL) {
|
---|
[87bc11f] | 222 | DPRINTF("Error: '%s' is not a dynamically-linked object.\n",
|
---|
[1ea99cc] | 223 | name_buf);
|
---|
[985e0f15] | 224 | goto error;
|
---|
[1ea99cc] | 225 | }
|
---|
| 226 |
|
---|
| 227 | /* Pending relocation. */
|
---|
| 228 | m->relocated = false;
|
---|
| 229 |
|
---|
| 230 | DPRINTF("parse dynamic section\n");
|
---|
| 231 | /* Parse ELF .dynamic section. Store info to m->dyn. */
|
---|
| 232 | dynamic_parse(info.dynamic, m->bias, &m->dyn);
|
---|
| 233 |
|
---|
| 234 | /* Insert into the list of loaded modules */
|
---|
[17341d4] | 235 | list_append(&m->modules_link, &rtld->modules);
|
---|
[a35b458] | 236 |
|
---|
[6adb775f] | 237 | /* Copy TLS info */
|
---|
| 238 | m->tdata = info.tls.tdata;
|
---|
| 239 | m->tdata_size = info.tls.tdata_size;
|
---|
| 240 | m->tbss_size = info.tls.tbss_size;
|
---|
[29405ac] | 241 | m->tls_align = info.tls.tls_align;
|
---|
[a35b458] | 242 |
|
---|
[153c7a29] | 243 | DPRINTF("tdata at %p size %zu, tbss size %zu\n",
|
---|
[6adb775f] | 244 | m->tdata, m->tdata_size, m->tbss_size);
|
---|
| 245 |
|
---|
[1ea99cc] | 246 | return m;
|
---|
[985e0f15] | 247 |
|
---|
| 248 | error:
|
---|
| 249 | if (m)
|
---|
| 250 | free(m);
|
---|
| 251 |
|
---|
| 252 | return NULL;
|
---|
[1ea99cc] | 253 | }
|
---|
| 254 |
|
---|
| 255 | /** Load all modules on which m (transitively) depends.
|
---|
| 256 | */
|
---|
[985e0f15] | 257 | errno_t module_load_deps(module_t *m, mlflags_t flags)
|
---|
[1ea99cc] | 258 | {
|
---|
| 259 | elf_dyn_t *dp;
|
---|
| 260 | char *dep_name;
|
---|
| 261 | module_t *dm;
|
---|
| 262 | size_t n, i;
|
---|
| 263 |
|
---|
[a6dffb8] | 264 | DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
|
---|
| 265 |
|
---|
[1ea99cc] | 266 | /* Count direct dependencies */
|
---|
[a35b458] | 267 |
|
---|
[1ea99cc] | 268 | dp = m->dyn.dynamic;
|
---|
| 269 | n = 0;
|
---|
| 270 |
|
---|
| 271 | while (dp->d_tag != DT_NULL) {
|
---|
[1433ecda] | 272 | if (dp->d_tag == DT_NEEDED)
|
---|
| 273 | ++n;
|
---|
[1ea99cc] | 274 | ++dp;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /* Create an array of pointers to direct dependencies */
|
---|
| 278 |
|
---|
| 279 | m->n_deps = n;
|
---|
| 280 |
|
---|
| 281 | if (n == 0) {
|
---|
| 282 | /* There are no dependencies, so we are done. */
|
---|
| 283 | m->deps = NULL;
|
---|
[985e0f15] | 284 | return EOK;
|
---|
[1ea99cc] | 285 | }
|
---|
| 286 |
|
---|
| 287 | m->deps = malloc(n * sizeof(module_t *));
|
---|
| 288 | if (!m->deps) {
|
---|
[87bc11f] | 289 | DPRINTF("malloc failed\n");
|
---|
[985e0f15] | 290 | return ENOMEM;
|
---|
[1ea99cc] | 291 | }
|
---|
| 292 |
|
---|
| 293 | i = 0; /* Current dependency index */
|
---|
| 294 | dp = m->dyn.dynamic;
|
---|
| 295 |
|
---|
| 296 | while (dp->d_tag != DT_NULL) {
|
---|
| 297 | if (dp->d_tag == DT_NEEDED) {
|
---|
| 298 | dep_name = m->dyn.str_tab + dp->d_un.d_val;
|
---|
| 299 |
|
---|
| 300 | DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
|
---|
[17341d4] | 301 | dm = module_find(m->rtld, dep_name);
|
---|
[1ea99cc] | 302 | if (!dm) {
|
---|
[5035ba05] | 303 | dm = module_load(m->rtld, dep_name, flags);
|
---|
[985e0f15] | 304 | errno_t rc = module_load_deps(dm, flags);
|
---|
| 305 | if (rc != EOK) {
|
---|
| 306 | return rc;
|
---|
| 307 | }
|
---|
[1ea99cc] | 308 | }
|
---|
| 309 |
|
---|
| 310 | /* Save into deps table */
|
---|
| 311 | m->deps[i++] = dm;
|
---|
| 312 | }
|
---|
| 313 | ++dp;
|
---|
| 314 | }
|
---|
[985e0f15] | 315 |
|
---|
| 316 | return EOK;
|
---|
[1ea99cc] | 317 | }
|
---|
| 318 |
|
---|
[6adb775f] | 319 | /** Find module structure by ID. */
|
---|
| 320 | module_t *module_by_id(rtld_t *rtld, unsigned long id)
|
---|
| 321 | {
|
---|
| 322 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 323 | if (m->id == id)
|
---|
| 324 | return m;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | return NULL;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
[1ea99cc] | 330 | /** Process relocations in modules.
|
---|
| 331 | *
|
---|
| 332 | * Processes relocations in @a start and all its dependencies.
|
---|
| 333 | * Modules that have already been relocated are unaffected.
|
---|
| 334 | *
|
---|
| 335 | * @param start The module where to start from.
|
---|
| 336 | */
|
---|
[17341d4] | 337 | void modules_process_relocs(rtld_t *rtld, module_t *start)
|
---|
[1ea99cc] | 338 | {
|
---|
[17341d4] | 339 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 340 | /* Skip rtld module, since it has already been processed */
|
---|
| 341 | if (m != &rtld->rtld) {
|
---|
[1ea99cc] | 342 | module_process_relocs(m);
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[6adb775f] | 347 | void modules_process_tls(rtld_t *rtld)
|
---|
| 348 | {
|
---|
[29405ac] | 349 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
[4f205248] | 350 | rtld->tls_size = sizeof(tcb_t);
|
---|
| 351 | rtld->tls_align = _Alignof(tcb_t);
|
---|
[29405ac] | 352 |
|
---|
| 353 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[4f205248] | 354 | list_append(&m->imodules_link, &rtld->imodules);
|
---|
| 355 | rtld->tls_align = max(rtld->tls_align, m->tls_align);
|
---|
| 356 |
|
---|
| 357 | rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
|
---|
| 358 | m->tpoff = rtld->tls_size;
|
---|
[29405ac] | 359 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
[4f205248] | 362 | #else
|
---|
| 363 | rtld->tls_size = 0;
|
---|
| 364 | rtld->tls_align = _Alignof(tcb_t);
|
---|
| 365 |
|
---|
[29405ac] | 366 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[bab0f42] | 367 | list_append(&m->imodules_link, &rtld->imodules);
|
---|
[4f205248] | 368 | rtld->tls_align = max(rtld->tls_align, m->tls_align);
|
---|
| 369 |
|
---|
[7c3fb9b] | 370 | /*
|
---|
| 371 | * We are allocating spans "backwards", here,
|
---|
[4f205248] | 372 | * as described in U. Drepper's paper.
|
---|
| 373 | */
|
---|
| 374 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
| 375 | rtld->tls_size = ALIGN_UP(rtld->tls_size, m->tls_align);
|
---|
| 376 | m->tpoff = -(ptrdiff_t) rtld->tls_size;
|
---|
[29405ac] | 377 | }
|
---|
[4f205248] | 378 |
|
---|
[7c3fb9b] | 379 | /*
|
---|
| 380 | * We are in negative offsets. In order for the alignments to
|
---|
[4f205248] | 381 | * be correct, "zero" offset (i.e. the total size) must be aligned
|
---|
| 382 | * to the strictest alignment present.
|
---|
[40abf56] | 383 | * Note that the padding is actually in front of the TLS data,
|
---|
| 384 | * not after it.
|
---|
[4f205248] | 385 | */
|
---|
| 386 | rtld->tls_size = ALIGN_UP(rtld->tls_size, rtld->tls_align);
|
---|
| 387 |
|
---|
| 388 | /* Space for the TCB. */
|
---|
| 389 | rtld->tls_size += sizeof(tcb_t);
|
---|
[29405ac] | 390 | #endif
|
---|
[6adb775f] | 391 | }
|
---|
| 392 |
|
---|
[1ea99cc] | 393 | /** Clear BFS tags of all modules.
|
---|
| 394 | */
|
---|
[17341d4] | 395 | void modules_untag(rtld_t *rtld)
|
---|
[1ea99cc] | 396 | {
|
---|
[17341d4] | 397 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[1ea99cc] | 398 | m->bfs_tag = false;
|
---|
| 399 | }
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | /** @}
|
---|
| 403 | */
|
---|