| 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 |
|
|---|
| 37 | #include <adt/list.h>
|
|---|
| 38 | #include <elf/elf_load.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <fcntl.h>
|
|---|
| 41 | #include <loader/pcb.h>
|
|---|
| 42 | #include <stdio.h>
|
|---|
| 43 | #include <stdlib.h>
|
|---|
| 44 | #include <unistd.h>
|
|---|
| 45 |
|
|---|
| 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>
|
|---|
| 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 |
|
|---|
| 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");
|
|---|
| 70 | rel_table_process(m, m->dyn.jmp_rel, m->dyn.plt_rel_sz);
|
|---|
| 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);
|
|---|
| 75 | }
|
|---|
| 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);
|
|---|
| 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 | */
|
|---|
| 99 | module_t *module_find(rtld_t *rtld, const char *name)
|
|---|
| 100 | {
|
|---|
| 101 | const char *p, *soname;
|
|---|
| 102 |
|
|---|
| 103 | DPRINTF("module_find('%s')\n", name);
|
|---|
| 104 |
|
|---|
| 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 */
|
|---|
| 114 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
|---|
| 115 | DPRINTF("m = %p\n", m);
|
|---|
| 116 | if (str_cmp(m->dyn.soname, soname) == 0) {
|
|---|
| 117 | return m; /* Found */
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 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 | */
|
|---|
| 130 | module_t *module_load(rtld_t *rtld, const char *name, mlflags_t flags)
|
|---|
| 131 | {
|
|---|
| 132 | elf_finfo_t info;
|
|---|
| 133 | char name_buf[NAME_BUF_SIZE];
|
|---|
| 134 | module_t *m;
|
|---|
| 135 | int rc;
|
|---|
| 136 |
|
|---|
| 137 | m = calloc(1, sizeof(module_t));
|
|---|
| 138 | if (m == NULL) {
|
|---|
| 139 | printf("malloc failed\n");
|
|---|
| 140 | exit(1);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | m->rtld = rtld;
|
|---|
| 144 | m->id = rtld_get_next_id(rtld);
|
|---|
| 145 |
|
|---|
| 146 | if ((flags & mlf_local) != 0)
|
|---|
| 147 | m->local = true;
|
|---|
| 148 |
|
|---|
| 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 */
|
|---|
| 159 | m->bias = rtld->next_bias;
|
|---|
| 160 | rtld->next_bias += 0x100000;
|
|---|
| 161 |
|
|---|
| 162 | DPRINTF("filename:'%s'\n", name_buf);
|
|---|
| 163 | DPRINTF("load '%s' at 0x%x\n", name_buf, m->bias);
|
|---|
| 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 */
|
|---|
| 185 | list_append(&m->modules_link, &rtld->modules);
|
|---|
| 186 |
|
|---|
| 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;
|
|---|
| 191 | m->tls_align = info.tls.tls_align;
|
|---|
| 192 |
|
|---|
| 193 | printf("tdata at %p size %zu, tbss size %zu\n",
|
|---|
| 194 | m->tdata, m->tdata_size, m->tbss_size);
|
|---|
| 195 |
|
|---|
| 196 | return m;
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | /** Load all modules on which m (transitively) depends.
|
|---|
| 200 | */
|
|---|
| 201 | void module_load_deps(module_t *m, mlflags_t flags)
|
|---|
| 202 | {
|
|---|
| 203 | elf_dyn_t *dp;
|
|---|
| 204 | char *dep_name;
|
|---|
| 205 | module_t *dm;
|
|---|
| 206 | size_t n, i;
|
|---|
| 207 |
|
|---|
| 208 | DPRINTF("module_load_deps('%s')\n", m->dyn.soname);
|
|---|
| 209 |
|
|---|
| 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);
|
|---|
| 244 | dm = module_find(m->rtld, dep_name);
|
|---|
| 245 | if (!dm) {
|
|---|
| 246 | dm = module_load(m->rtld, dep_name, flags);
|
|---|
| 247 | module_load_deps(dm, flags);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /* Save into deps table */
|
|---|
| 251 | m->deps[i++] = dm;
|
|---|
| 252 | }
|
|---|
| 253 | ++dp;
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 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 |
|
|---|
| 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 | */
|
|---|
| 275 | void modules_process_relocs(rtld_t *rtld, module_t *start)
|
|---|
| 276 | {
|
|---|
| 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) {
|
|---|
| 280 | module_process_relocs(m);
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | void modules_process_tls(rtld_t *rtld)
|
|---|
| 286 | {
|
|---|
| 287 | #ifdef CONFIG_TLS_VARIANT_1
|
|---|
| 288 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
|---|
| 289 | m->ioffs = rtld->tls_size;
|
|---|
| 290 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
|---|
| 291 | }
|
|---|
| 292 | #else /* CONFIG_TLS_VARIANT_2 */
|
|---|
| 293 | size_t offs;
|
|---|
| 294 |
|
|---|
| 295 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
|---|
| 296 | rtld->tls_size += m->tdata_size + m->tbss_size;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | offs = 0;
|
|---|
| 300 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
|---|
| 301 | offs += m->tdata_size + m->tbss_size;
|
|---|
| 302 | m->ioffs = rtld->tls_size - offs;
|
|---|
| 303 | }
|
|---|
| 304 | #endif
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /** Clear BFS tags of all modules.
|
|---|
| 308 | */
|
|---|
| 309 | void modules_untag(rtld_t *rtld)
|
|---|
| 310 | {
|
|---|
| 311 | list_foreach(rtld->modules, modules_link, module_t, m) {
|
|---|
| 312 | m->bfs_tag = false;
|
|---|
| 313 | }
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | /** @}
|
|---|
| 317 | */
|
|---|