[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 <loader/pcb.h>
|
---|
[1ea99cc] | 41 | #include <stdio.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 |
|
---|
[8a1fb09] | 44 | #include <rtld/rtld.h>
|
---|
| 45 | #include <rtld/rtld_debug.h>
|
---|
| 46 | #include <rtld/dynamic.h>
|
---|
| 47 | #include <rtld/rtld_arch.h>
|
---|
| 48 | #include <rtld/module.h>
|
---|
[1ea99cc] | 49 |
|
---|
[153c7a29] | 50 | /** Create module for static executable.
|
---|
| 51 | *
|
---|
| 52 | * @param rtld Run-time dynamic linker
|
---|
| 53 | * @param rmodule Place to store pointer to new module or @c NULL
|
---|
| 54 | * @return EOK on success, ENOMEM if out of memory
|
---|
| 55 | */
|
---|
| 56 | int module_create_static_exec(rtld_t *rtld, module_t **rmodule)
|
---|
| 57 | {
|
---|
| 58 | module_t *module;
|
---|
| 59 |
|
---|
| 60 | module = calloc(1, sizeof(module_t));
|
---|
| 61 | if (module == NULL)
|
---|
| 62 | return ENOMEM;
|
---|
| 63 |
|
---|
| 64 | module->id = rtld_get_next_id(rtld);
|
---|
| 65 | module->dyn.soname = "[program]";
|
---|
| 66 |
|
---|
| 67 | module->rtld = rtld;
|
---|
| 68 | module->exec = true;
|
---|
| 69 | module->local = true;
|
---|
| 70 |
|
---|
| 71 | module->tdata = &_tdata_start;
|
---|
| 72 | module->tdata_size = &_tdata_end - &_tdata_start;
|
---|
| 73 | module->tbss_size = &_tbss_end - &_tbss_start;
|
---|
| 74 | module->tls_align = (uintptr_t)&_tls_alignment;
|
---|
| 75 |
|
---|
| 76 | list_append(&module->modules_link, &rtld->modules);
|
---|
| 77 |
|
---|
| 78 | if (rmodule != NULL)
|
---|
| 79 | *rmodule = module;
|
---|
| 80 | return EOK;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[1ea99cc] | 83 | /** (Eagerly) process all relocation tables in a module.
|
---|
| 84 | *
|
---|
| 85 | * Currently works as if LD_BIND_NOW was specified.
|
---|
| 86 | */
|
---|
| 87 | void module_process_relocs(module_t *m)
|
---|
| 88 | {
|
---|
| 89 | DPRINTF("module_process_relocs('%s')\n", m->dyn.soname);
|
---|
| 90 |
|
---|
| 91 | /* Do not relocate twice. */
|
---|
| 92 | if (m->relocated) return;
|
---|
| 93 |
|
---|
| 94 | module_process_pre_arch(m);
|
---|
| 95 |
|
---|
[634e020] | 96 | /* jmp_rel table */
|
---|
| 97 | if (m->dyn.jmp_rel != NULL) {
|
---|
| 98 | DPRINTF("jmp_rel table\n");
|
---|
| 99 | if (m->dyn.plt_rel == DT_REL) {
|
---|
| 100 | DPRINTF("jmp_rel table type DT_REL\n");
|
---|
[1ea99cc] | 101 | rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
[634e020] | 102 | } else {
|
---|
| 103 | assert(m->dyn.plt_rel == DT_RELA);
|
---|
| 104 | DPRINTF("jmp_rel table type DT_RELA\n");
|
---|
| 105 | rela_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
---|
[1ea99cc] | 106 | }
|
---|
[634e020] | 107 | }
|
---|
| 108 |
|
---|
| 109 | /* rel table */
|
---|
| 110 | if (m->dyn.rel != NULL) {
|
---|
| 111 | DPRINTF("rel table\n");
|
---|
| 112 | rel_table_process(m, m->dyn.rel, m->dyn.rel_sz);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /* rela table */
|
---|
| 116 | if (m->dyn.rela != NULL) {
|
---|
| 117 | DPRINTF("rela table\n");
|
---|
| 118 | rela_table_process(m, m->dyn.rela, m->dyn.rela_sz);
|
---|
[1ea99cc] | 119 | }
|
---|
| 120 |
|
---|
| 121 | m->relocated = true;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /** Find module structure by soname/pathname.
|
---|
| 125 | *
|
---|
| 126 | * Used primarily to see if a module has already been loaded.
|
---|
| 127 | * Modules are compared according to their soname, i.e. possible
|
---|
| 128 | * path components are ignored.
|
---|
| 129 | */
|
---|
[17341d4] | 130 | module_t *module_find(rtld_t *rtld, const char *name)
|
---|
[1ea99cc] | 131 | {
|
---|
[04803bf] | 132 | const char *p, *soname;
|
---|
[1ea99cc] | 133 |
|
---|
[a6dffb8] | 134 | DPRINTF("module_find('%s')\n", name);
|
---|
| 135 |
|
---|
[1ea99cc] | 136 | /*
|
---|
| 137 | * If name contains slashes, treat it as a pathname and
|
---|
| 138 | * construct soname by chopping off the path. Otherwise
|
---|
| 139 | * treat it as soname.
|
---|
| 140 | */
|
---|
| 141 | p = str_rchr(name, '/');
|
---|
| 142 | soname = p ? (p + 1) : name;
|
---|
| 143 |
|
---|
| 144 | /* Traverse list of all modules. Not extremely fast, but simple */
|
---|
[17341d4] | 145 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[4b63316] | 146 | DPRINTF("m = %p\n", m);
|
---|
[1ea99cc] | 147 | if (str_cmp(m->dyn.soname, soname) == 0) {
|
---|
| 148 | return m; /* Found */
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
[58563585] | 151 |
|
---|
[1ea99cc] | 152 | return NULL; /* Not found */
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | #define NAME_BUF_SIZE 64
|
---|
| 156 |
|
---|
| 157 | /** Load a module.
|
---|
| 158 | *
|
---|
| 159 | * Currently this trivially tries to load '/<name>'.
|
---|
| 160 | */
|
---|
[5035ba05] | 161 | module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
|
---|
[1ea99cc] | 162 | {
|
---|
[17341d4] | 163 | elf_finfo_t info;
|
---|
[1ea99cc] | 164 | char name_buf[NAME_BUF_SIZE];
|
---|
| 165 | module_t *m;
|
---|
| 166 | int rc;
|
---|
[58563585] | 167 |
|
---|
[6adb775f] | 168 | m = calloc(1, sizeof(module_t));
|
---|
| 169 | if (m == NULL) {
|
---|
[1ea99cc] | 170 | printf("malloc failed\n");
|
---|
| 171 | exit(1);
|
---|
| 172 | }
|
---|
[58563585] | 173 |
|
---|
[17341d4] | 174 | m->rtld = rtld;
|
---|
[6adb775f] | 175 | m->id = rtld_get_next_id(rtld);
|
---|
[17341d4] | 176 |
|
---|
[5035ba05] | 177 | if ((flags & mlf_local) != 0)
|
---|
| 178 | m->local = true;
|
---|
[17341d4] | 179 |
|
---|
[1ea99cc] | 180 | if (str_size(name) > NAME_BUF_SIZE - 2) {
|
---|
| 181 | printf("soname too long. increase NAME_BUF_SIZE\n");
|
---|
| 182 | exit(1);
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /* Prepend soname with '/lib/' */
|
---|
| 186 | str_cpy(name_buf, NAME_BUF_SIZE, "/lib/");
|
---|
| 187 | str_cpy(name_buf + 5, NAME_BUF_SIZE - 5, name);
|
---|
| 188 |
|
---|
| 189 | /* FIXME: need to real allocation of address space */
|
---|
[17341d4] | 190 | m->bias = rtld->next_bias;
|
---|
| 191 | rtld->next_bias += 0x100000;
|
---|
[1ea99cc] | 192 |
|
---|
| 193 | DPRINTF("filename:'%s'\n", name_buf);
|
---|
[33f86a3] | 194 | DPRINTF("load '%s' at 0x%zx\n", name_buf, m->bias);
|
---|
[1ea99cc] | 195 |
|
---|
[bb9ec2d] | 196 | rc = elf_load_file_name(name_buf, m->bias, ELDF_RW, &info);
|
---|
[1ea99cc] | 197 | if (rc != EE_OK) {
|
---|
| 198 | printf("Failed to load '%s'\n", name_buf);
|
---|
| 199 | exit(1);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | if (info.dynamic == NULL) {
|
---|
| 203 | printf("Error: '%s' is not a dynamically-linked object.\n",
|
---|
| 204 | name_buf);
|
---|
| 205 | exit(1);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* Pending relocation. */
|
---|
| 209 | m->relocated = false;
|
---|
| 210 |
|
---|
| 211 | DPRINTF("parse dynamic section\n");
|
---|
| 212 | /* Parse ELF .dynamic section. Store info to m->dyn. */
|
---|
| 213 | dynamic_parse(info.dynamic, m->bias, &m->dyn);
|
---|
| 214 |
|
---|
| 215 | /* Insert into the list of loaded modules */
|
---|
[17341d4] | 216 | list_append(&m->modules_link, &rtld->modules);
|
---|
[58563585] | 217 |
|
---|
[6adb775f] | 218 | /* Copy TLS info */
|
---|
| 219 | m->tdata = info.tls.tdata;
|
---|
| 220 | m->tdata_size = info.tls.tdata_size;
|
---|
| 221 | m->tbss_size = info.tls.tbss_size;
|
---|
[29405ac] | 222 | m->tls_align = info.tls.tls_align;
|
---|
[58563585] | 223 |
|
---|
[153c7a29] | 224 | DPRINTF("tdata at %p size %zu, tbss size %zu\n",
|
---|
[6adb775f] | 225 | m->tdata, m->tdata_size, m->tbss_size);
|
---|
| 226 |
|
---|
[1ea99cc] | 227 | return m;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | /** Load all modules on which m (transitively) depends.
|
---|
| 231 | */
|
---|
[5035ba05] | 232 | void module_load_deps(module_t *m, mlflags_t flags)
|
---|
[1ea99cc] | 233 | {
|
---|
| 234 | elf_dyn_t *dp;
|
---|
| 235 | char *dep_name;
|
---|
| 236 | module_t *dm;
|
---|
| 237 | size_t n, i;
|
---|
| 238 |
|
---|
[a6dffb8] | 239 | DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
|
---|
| 240 |
|
---|
[1ea99cc] | 241 | /* Count direct dependencies */
|
---|
| 242 |
|
---|
| 243 | dp = m->dyn.dynamic;
|
---|
| 244 | n = 0;
|
---|
| 245 |
|
---|
| 246 | while (dp->d_tag != DT_NULL) {
|
---|
| 247 | if (dp->d_tag == DT_NEEDED) ++n;
|
---|
| 248 | ++dp;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | /* Create an array of pointers to direct dependencies */
|
---|
| 252 |
|
---|
| 253 | m->n_deps = n;
|
---|
| 254 |
|
---|
| 255 | if (n == 0) {
|
---|
| 256 | /* There are no dependencies, so we are done. */
|
---|
| 257 | m->deps = NULL;
|
---|
| 258 | return;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | m->deps = malloc(n * sizeof(module_t *));
|
---|
| 262 | if (!m->deps) {
|
---|
| 263 | printf("malloc failed\n");
|
---|
| 264 | exit(1);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | i = 0; /* Current dependency index */
|
---|
| 268 | dp = m->dyn.dynamic;
|
---|
| 269 |
|
---|
| 270 | while (dp->d_tag != DT_NULL) {
|
---|
| 271 | if (dp->d_tag == DT_NEEDED) {
|
---|
| 272 | dep_name = m->dyn.str_tab + dp->d_un.d_val;
|
---|
| 273 |
|
---|
| 274 | DPRINTF("%s needs %s\n", m->dyn.soname, dep_name);
|
---|
[17341d4] | 275 | dm = module_find(m->rtld, dep_name);
|
---|
[1ea99cc] | 276 | if (!dm) {
|
---|
[5035ba05] | 277 | dm = module_load(m->rtld, dep_name, flags);
|
---|
| 278 | module_load_deps(dm, flags);
|
---|
[1ea99cc] | 279 | }
|
---|
| 280 |
|
---|
| 281 | /* Save into deps table */
|
---|
| 282 | m->deps[i++] = dm;
|
---|
| 283 | }
|
---|
| 284 | ++dp;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[6adb775f] | 288 | /** Find module structure by ID. */
|
---|
| 289 | module_t *module_by_id(rtld_t *rtld, unsigned long id)
|
---|
| 290 | {
|
---|
| 291 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 292 | if (m->id == id)
|
---|
| 293 | return m;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | return NULL;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[1ea99cc] | 299 | /** Process relocations in modules.
|
---|
| 300 | *
|
---|
| 301 | * Processes relocations in @a start and all its dependencies.
|
---|
| 302 | * Modules that have already been relocated are unaffected.
|
---|
| 303 | *
|
---|
| 304 | * @param start The module where to start from.
|
---|
| 305 | */
|
---|
[17341d4] | 306 | void modules_process_relocs(rtld_t *rtld, module_t *start)
|
---|
[1ea99cc] | 307 | {
|
---|
[17341d4] | 308 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 309 | /* Skip rtld module, since it has already been processed */
|
---|
| 310 | if (m != &rtld->rtld) {
|
---|
[1ea99cc] | 311 | module_process_relocs(m);
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
[6adb775f] | 316 | void modules_process_tls(rtld_t *rtld)
|
---|
| 317 | {
|
---|
[29405ac] | 318 | #ifdef CONFIG_TLS_VARIANT_1
|
---|
[6adb775f] | 319 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 320 | m->ioffs = rtld->tls_size;
|
---|
[bab0f42] | 321 | list_append(&m->imodules_link, &rtmd->imodules);
|
---|
[6adb775f] | 322 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
| 323 | }
|
---|
[29405ac] | 324 | #else /* CONFIG_TLS_VARIANT_2 */
|
---|
| 325 | size_t offs;
|
---|
| 326 |
|
---|
| 327 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 328 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | offs = 0;
|
---|
| 332 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
| 333 | offs += m->tdata_size + m->tbss_size;
|
---|
| 334 | m->ioffs = rtld->tls_size - offs;
|
---|
[bab0f42] | 335 | list_append(&m->imodules_link, &rtld->imodules);
|
---|
[29405ac] | 336 | }
|
---|
| 337 | #endif
|
---|
[6adb775f] | 338 | }
|
---|
| 339 |
|
---|
[1ea99cc] | 340 | /** Clear BFS tags of all modules.
|
---|
| 341 | */
|
---|
[17341d4] | 342 | void modules_untag(rtld_t *rtld)
|
---|
[1ea99cc] | 343 | {
|
---|
[17341d4] | 344 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
---|
[1ea99cc] | 345 | m->bfs_tag = false;
|
---|
| 346 | }
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /** @}
|
---|
| 350 | */
|
---|