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