| 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
|
|---|
| 30 | * @brief
|
|---|
| 31 | * @{
|
|---|
| 32 | */
|
|---|
| 33 | /**
|
|---|
| 34 | * @file
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <errno.h>
|
|---|
| 38 | #include <rtld/module.h>
|
|---|
| 39 | #include <rtld/rtld.h>
|
|---|
| 40 | #include <rtld/rtld_debug.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| 42 | #include <str.h>
|
|---|
| 43 |
|
|---|
| 44 | rtld_t *runtime_env;
|
|---|
| 45 |
|
|---|
| 46 | /** Initialize and process an executable.
|
|---|
| 47 | *
|
|---|
| 48 | * @param p_info Program info
|
|---|
| 49 | * @return EOK on success or non-zero error code
|
|---|
| 50 | */
|
|---|
| 51 | errno_t rtld_prog_process(elf_finfo_t *p_info, rtld_t **rre)
|
|---|
| 52 | {
|
|---|
| 53 | rtld_t *env;
|
|---|
| 54 | bool is_dynamic = p_info->dynamic != NULL;
|
|---|
| 55 | DPRINTF("rtld_prog_process\n");
|
|---|
| 56 |
|
|---|
| 57 | /* Allocate new RTLD environment to pass to the loaded program */
|
|---|
| 58 | env = calloc(1, sizeof(rtld_t));
|
|---|
| 59 | if (env == NULL)
|
|---|
| 60 | return ENOMEM;
|
|---|
| 61 |
|
|---|
| 62 | list_initialize(&env->modules);
|
|---|
| 63 | list_initialize(&env->imodules);
|
|---|
| 64 | env->next_id = 1;
|
|---|
| 65 |
|
|---|
| 66 | module_t *module;
|
|---|
| 67 | errno_t rc = module_create_entrypoint(p_info, env, &module);
|
|---|
| 68 | if (rc != EOK) {
|
|---|
| 69 | free(env);
|
|---|
| 70 | return rc;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /* Pointer to program module. Used as root of the module graph. */
|
|---|
| 74 | env->program = module;
|
|---|
| 75 |
|
|---|
| 76 | /*
|
|---|
| 77 | * Now we can continue with loading all other modules.
|
|---|
| 78 | */
|
|---|
| 79 |
|
|---|
| 80 | if (is_dynamic) {
|
|---|
| 81 | DPRINTF("Load all program dependencies\n");
|
|---|
| 82 | rc = module_load_deps(module, 0);
|
|---|
| 83 | if (rc != EOK) {
|
|---|
| 84 | free(module);
|
|---|
| 85 | free(env);
|
|---|
| 86 | return rc;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /* Compute static TLS size */
|
|---|
| 91 | modules_process_tls(env);
|
|---|
| 92 |
|
|---|
| 93 | /*
|
|---|
| 94 | * Now relocate/link all modules together.
|
|---|
| 95 | */
|
|---|
| 96 |
|
|---|
| 97 | if (is_dynamic) {
|
|---|
| 98 | /* Process relocations in all modules */
|
|---|
| 99 | DPRINTF("Relocate all modules\n");
|
|---|
| 100 | modules_process_relocs(env, module);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if (rre != NULL)
|
|---|
| 104 | *rre = env;
|
|---|
| 105 | return EOK;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /** Create TLS (Thread Local Storage) data structures.
|
|---|
| 109 | *
|
|---|
| 110 | * @return Pointer to TCB.
|
|---|
| 111 | */
|
|---|
| 112 | tcb_t *rtld_tls_make(rtld_t *rtld)
|
|---|
| 113 | {
|
|---|
| 114 | tcb_t *tcb;
|
|---|
| 115 | void **dtv;
|
|---|
| 116 | size_t nmods;
|
|---|
| 117 | size_t i;
|
|---|
| 118 |
|
|---|
| 119 | tcb = tls_alloc_arch(rtld->tls_size, rtld->tls_align);
|
|---|
| 120 | if (tcb == NULL)
|
|---|
| 121 | return NULL;
|
|---|
| 122 |
|
|---|
| 123 | /** Allocate dynamic thread vector */
|
|---|
| 124 | nmods = list_count(&rtld->imodules);
|
|---|
| 125 | dtv = malloc((nmods + 1) * sizeof(void *));
|
|---|
| 126 | if (dtv == NULL) {
|
|---|
| 127 | tls_free(tcb);
|
|---|
| 128 | return NULL;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | /*
|
|---|
| 132 | * We define generation number to be equal to vector length.
|
|---|
| 133 | * We start with a vector covering the initially loaded modules.
|
|---|
| 134 | */
|
|---|
| 135 | DTV_GN(dtv) = nmods;
|
|---|
| 136 |
|
|---|
| 137 | /*
|
|---|
| 138 | * Copy thread local data from the initialization images of initial
|
|---|
| 139 | * modules. Zero out thread-local uninitialized data.
|
|---|
| 140 | */
|
|---|
| 141 |
|
|---|
| 142 | i = 1;
|
|---|
| 143 | list_foreach(rtld->imodules, imodules_link, module_t, m) {
|
|---|
| 144 | assert(i++ == m->id);
|
|---|
| 145 |
|
|---|
| 146 | dtv[m->id] = (void *) tcb + m->tpoff;
|
|---|
| 147 |
|
|---|
| 148 | assert(((uintptr_t) dtv[m->id]) % m->tls_align == 0);
|
|---|
| 149 |
|
|---|
| 150 | if (m->tdata)
|
|---|
| 151 | memcpy(dtv[m->id], m->tdata, m->tdata_size);
|
|---|
| 152 |
|
|---|
| 153 | memset(dtv[m->id] + m->tdata_size, 0, m->tbss_size);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | tcb->dtv = dtv;
|
|---|
| 157 | return tcb;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | unsigned long rtld_get_next_id(rtld_t *rtld)
|
|---|
| 161 | {
|
|---|
| 162 | return rtld->next_id++;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /** Get address of thread-local variable.
|
|---|
| 166 | *
|
|---|
| 167 | * @param rtld RTLD instance
|
|---|
| 168 | * @param tcb TCB of the thread whose instance to return
|
|---|
| 169 | * @param mod_id Module ID
|
|---|
| 170 | * @param offset Offset within TLS block of the module
|
|---|
| 171 | *
|
|---|
| 172 | * @return Address of thread-local variable
|
|---|
| 173 | */
|
|---|
| 174 | void *rtld_tls_get_addr(rtld_t *rtld, tcb_t *tcb, unsigned long mod_id,
|
|---|
| 175 | unsigned long offset)
|
|---|
| 176 | {
|
|---|
| 177 | module_t *m;
|
|---|
| 178 | size_t dtv_len;
|
|---|
| 179 | void *tls_block;
|
|---|
| 180 |
|
|---|
| 181 | dtv_len = DTV_GN(tcb->dtv);
|
|---|
| 182 | if (dtv_len < mod_id) {
|
|---|
| 183 | /* Vector is short */
|
|---|
| 184 |
|
|---|
| 185 | tcb->dtv = realloc(tcb->dtv, (1 + mod_id) * sizeof(void *));
|
|---|
| 186 | /* XXX This can fail if OOM */
|
|---|
| 187 | assert(tcb->dtv != NULL);
|
|---|
| 188 | /* Zero out new part of vector */
|
|---|
| 189 | memset(tcb->dtv + (1 + dtv_len), 0, (mod_id - dtv_len) *
|
|---|
| 190 | sizeof(void *));
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | if (tcb->dtv[mod_id] == NULL) {
|
|---|
| 194 | /* TLS block is not allocated */
|
|---|
| 195 |
|
|---|
| 196 | m = module_by_id(rtld, mod_id);
|
|---|
| 197 | assert(m != NULL);
|
|---|
| 198 | /* Should not be initial module, those have TLS pre-allocated */
|
|---|
| 199 | assert(!link_used(&m->imodules_link));
|
|---|
| 200 |
|
|---|
| 201 | tls_block = memalign(m->tls_align, m->tdata_size + m->tbss_size);
|
|---|
| 202 | /* XXX This can fail if OOM */
|
|---|
| 203 | assert(tls_block != NULL);
|
|---|
| 204 |
|
|---|
| 205 | /* Copy tdata */
|
|---|
| 206 | memcpy(tls_block, m->tdata, m->tdata_size);
|
|---|
| 207 | /* Zero out tbss */
|
|---|
| 208 | memset(tls_block + m->tdata_size, 0, m->tbss_size);
|
|---|
| 209 |
|
|---|
| 210 | tcb->dtv[mod_id] = tls_block;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | return (uint8_t *)(tcb->dtv[mod_id]) + offset;
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /** @}
|
|---|
| 217 | */
|
|---|