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