[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 |
|
---|
[b1834a01] | 29 | /** @addtogroup rtld
|
---|
[1ea99cc] | 30 | * @brief
|
---|
| 31 | * @{
|
---|
[1b20da0] | 32 | */
|
---|
[1ea99cc] | 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
[1d6dd2a] | 39 | #include <str.h>
|
---|
[1ea99cc] | 40 |
|
---|
[bfdb5af1] | 41 | #include <elf/elf.h>
|
---|
[17341d4] | 42 | #include <rtld/module.h>
|
---|
[8a1fb09] | 43 | #include <rtld/rtld.h>
|
---|
| 44 | #include <rtld/rtld_debug.h>
|
---|
| 45 | #include <rtld/symbol.h>
|
---|
[1ea99cc] | 46 |
|
---|
| 47 | /*
|
---|
| 48 | * Hash tables are 32-bit (elf_word) even for 64-bit ELF files.
|
---|
| 49 | */
|
---|
| 50 | static elf_word elf_hash(const unsigned char *name)
|
---|
| 51 | {
|
---|
| 52 | elf_word h = 0, g;
|
---|
| 53 |
|
---|
| 54 | while (*name) {
|
---|
| 55 | h = (h << 4) + *name++;
|
---|
| 56 | g = h & 0xf0000000;
|
---|
[1433ecda] | 57 | if (g != 0)
|
---|
| 58 | h ^= g >> 24;
|
---|
[1ea99cc] | 59 | h &= ~g;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | return h;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[04803bf] | 65 | static elf_symbol_t *def_find_in_module(const char *name, module_t *m)
|
---|
[1ea99cc] | 66 | {
|
---|
| 67 | elf_symbol_t *sym_table;
|
---|
| 68 | elf_symbol_t *s, *sym;
|
---|
| 69 | elf_word nbucket;
|
---|
[e2098dd7] | 70 | /*elf_word nchain;*/
|
---|
[1ea99cc] | 71 | elf_word i;
|
---|
| 72 | char *s_name;
|
---|
| 73 | elf_word bucket;
|
---|
| 74 |
|
---|
| 75 | DPRINTF("def_find_in_module('%s', %s)\n", name, m->dyn.soname);
|
---|
| 76 |
|
---|
| 77 | sym_table = m->dyn.sym_tab;
|
---|
| 78 | nbucket = m->dyn.hash[0];
|
---|
[e2098dd7] | 79 | /*nchain = m->dyn.hash[1]; XXX Use to check HT range*/
|
---|
[1ea99cc] | 80 |
|
---|
| 81 | bucket = elf_hash((unsigned char *)name) % nbucket;
|
---|
| 82 | i = m->dyn.hash[2 + bucket];
|
---|
| 83 |
|
---|
| 84 | sym = NULL;
|
---|
| 85 | while (i != STN_UNDEF) {
|
---|
| 86 | s = &sym_table[i];
|
---|
| 87 | s_name = m->dyn.str_tab + s->st_name;
|
---|
| 88 |
|
---|
| 89 | if (str_cmp(name, s_name) == 0) {
|
---|
| 90 | sym = s;
|
---|
| 91 | break;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | i = m->dyn.hash[2 + nbucket + i];
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | if (!sym)
|
---|
| 98 | return NULL; /* Not found */
|
---|
| 99 |
|
---|
| 100 | if (sym->st_shndx == SHN_UNDEF) {
|
---|
| 101 | /* Not a definition */
|
---|
| 102 | return NULL;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | return sym; /* Found */
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /** Find the definition of a symbol in a module and its deps.
|
---|
| 109 | *
|
---|
| 110 | * Search the module dependency graph is breadth-first, beginning
|
---|
| 111 | * from the module @a start. Thus, @start and all its dependencies
|
---|
| 112 | * get searched.
|
---|
| 113 | *
|
---|
| 114 | * @param name Name of the symbol to search for.
|
---|
| 115 | * @param start Module in which to start the search..
|
---|
[1b20da0] | 116 | * @param mod (output) Will be filled with a pointer to the module
|
---|
[1ea99cc] | 117 | * that contains the symbol.
|
---|
| 118 | */
|
---|
[0f792c28] | 119 | elf_symbol_t *symbol_bfs_find(const char *name, module_t *start,
|
---|
[5035ba05] | 120 | module_t **mod)
|
---|
[1ea99cc] | 121 | {
|
---|
| 122 | module_t *m, *dm;
|
---|
| 123 | elf_symbol_t *sym, *s;
|
---|
[bfdb5af1] | 124 | list_t queue;
|
---|
[1ea99cc] | 125 | size_t i;
|
---|
| 126 |
|
---|
| 127 | /*
|
---|
| 128 | * Do a BFS using the queue_link and bfs_tag fields.
|
---|
| 129 | * Vertices (modules) are tagged the moment they are inserted
|
---|
| 130 | * into the queue. This prevents from visiting the same vertex
|
---|
| 131 | * more times in case of circular dependencies.
|
---|
| 132 | */
|
---|
| 133 |
|
---|
[1b20da0] | 134 | /* Mark all vertices (modules) as unvisited */
|
---|
[17341d4] | 135 | modules_untag(start->rtld);
|
---|
[1ea99cc] | 136 |
|
---|
| 137 | /* Insert root (the program) into the queue and tag it */
|
---|
[bfdb5af1] | 138 | list_initialize(&queue);
|
---|
[1ea99cc] | 139 | start->bfs_tag = true;
|
---|
[bfdb5af1] | 140 | list_append(&start->queue_link, &queue);
|
---|
[1ea99cc] | 141 |
|
---|
| 142 | /* If the symbol is found, it will be stored in 'sym' */
|
---|
| 143 | sym = NULL;
|
---|
| 144 |
|
---|
| 145 | /* While queue is not empty */
|
---|
[bfdb5af1] | 146 | while (!list_empty(&queue)) {
|
---|
[1ea99cc] | 147 | /* Pop first element from the queue */
|
---|
[bfdb5af1] | 148 | m = list_get_instance(list_first(&queue), module_t, queue_link);
|
---|
[1ea99cc] | 149 | list_remove(&m->queue_link);
|
---|
| 150 |
|
---|
[0f792c28] | 151 | /* If ssf_noroot is specified, do not look in start module */
|
---|
[5035ba05] | 152 | s = def_find_in_module(name, m);
|
---|
| 153 | if (s != NULL) {
|
---|
| 154 | /* Symbol found */
|
---|
| 155 | sym = s;
|
---|
| 156 | *mod = m;
|
---|
| 157 | break;
|
---|
[1ea99cc] | 158 | }
|
---|
| 159 |
|
---|
| 160 | /*
|
---|
| 161 | * Insert m's untagged dependencies into the queue
|
---|
| 162 | * and tag them.
|
---|
| 163 | */
|
---|
| 164 | for (i = 0; i < m->n_deps; ++i) {
|
---|
| 165 | dm = m->deps[i];
|
---|
| 166 |
|
---|
| 167 | if (dm->bfs_tag == false) {
|
---|
| 168 | dm->bfs_tag = true;
|
---|
[bfdb5af1] | 169 | list_append(&dm->queue_link, &queue);
|
---|
[1ea99cc] | 170 | }
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /* Empty the queue so that we leave it in a clean state */
|
---|
[bfdb5af1] | 175 | while (!list_empty(&queue))
|
---|
| 176 | list_remove(list_first(&queue));
|
---|
[1ea99cc] | 177 |
|
---|
| 178 | if (!sym) {
|
---|
| 179 | return NULL; /* Not found */
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | return sym; /* Symbol found */
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
[0f792c28] | 186 | /** Find the definition of a symbol.
|
---|
[1ea99cc] | 187 | *
|
---|
| 188 | * By definition in System V ABI, if module origin has the flag DT_SYMBOLIC,
|
---|
[5035ba05] | 189 | * origin is searched first. Otherwise, search global modules in the default
|
---|
| 190 | * order.
|
---|
[1ea99cc] | 191 | *
|
---|
| 192 | * @param name Name of the symbol to search for.
|
---|
| 193 | * @param origin Module in which the dependency originates.
|
---|
[5035ba05] | 194 | * @param flags @c ssf_none or @c ssf_noexec to not look for the symbol
|
---|
[0f792c28] | 195 | * in the executable program.
|
---|
[1b20da0] | 196 | * @param mod (output) Will be filled with a pointer to the module
|
---|
[1ea99cc] | 197 | * that contains the symbol.
|
---|
| 198 | */
|
---|
[0f792c28] | 199 | elf_symbol_t *symbol_def_find(const char *name, module_t *origin,
|
---|
| 200 | symbol_search_flags_t flags, module_t **mod)
|
---|
[1ea99cc] | 201 | {
|
---|
[04803bf] | 202 | elf_symbol_t *s;
|
---|
[1ea99cc] | 203 |
|
---|
[5035ba05] | 204 | DPRINTF("symbol_def_find('%s', origin='%s'\n",
|
---|
| 205 | name, origin->dyn.soname);
|
---|
| 206 | if (origin->dyn.symbolic && (!origin->exec || (flags & ssf_noexec) == 0)) {
|
---|
| 207 | DPRINTF("symbolic->find '%s' in module '%s'\n", name, origin->dyn.soname);
|
---|
| 208 | /*
|
---|
[1ea99cc] | 209 | * Origin module has a DT_SYMBOLIC flag.
|
---|
| 210 | * Try this module first
|
---|
| 211 | */
|
---|
[5035ba05] | 212 | s = def_find_in_module(name, origin);
|
---|
| 213 | if (s != NULL) {
|
---|
[1ea99cc] | 214 | /* Found */
|
---|
| 215 | *mod = origin;
|
---|
| 216 | return s;
|
---|
[5035ba05] | 217 | }
|
---|
[1ea99cc] | 218 | }
|
---|
| 219 |
|
---|
| 220 | /* Not DT_SYMBOLIC or no match. Now try other locations. */
|
---|
| 221 |
|
---|
[5035ba05] | 222 | list_foreach(origin->rtld->modules, modules_link, module_t, m) {
|
---|
| 223 | DPRINTF("module '%s' local?\n", m->dyn.soname);
|
---|
| 224 | if (!m->local && (!m->exec || (flags & ssf_noexec) == 0)) {
|
---|
| 225 | DPRINTF("!local->find '%s' in module '%s'\n", name, m->dyn.soname);
|
---|
| 226 | s = def_find_in_module(name, m);
|
---|
| 227 | if (s != NULL) {
|
---|
| 228 | /* Found */
|
---|
| 229 | *mod = m;
|
---|
| 230 | return s;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /* Finally, try origin. */
|
---|
| 236 |
|
---|
| 237 | DPRINTF("try finding '%s' in origin '%s'\n", name,
|
---|
| 238 | origin->dyn.soname);
|
---|
| 239 |
|
---|
| 240 | if (!origin->exec || (flags & ssf_noexec) == 0) {
|
---|
| 241 | s = def_find_in_module(name, origin);
|
---|
| 242 | if (s != NULL) {
|
---|
| 243 | /* Found */
|
---|
| 244 | *mod = origin;
|
---|
| 245 | return s;
|
---|
| 246 | }
|
---|
[1ea99cc] | 247 | }
|
---|
[5035ba05] | 248 |
|
---|
| 249 | DPRINTF("'%s' not found\n", name);
|
---|
| 250 | return NULL;
|
---|
[1ea99cc] | 251 | }
|
---|
| 252 |
|
---|
[d2bb25e7] | 253 | /** Get symbol address.
|
---|
| 254 | *
|
---|
| 255 | * @param sym Symbol
|
---|
| 256 | * @param m Module contaning the symbol
|
---|
| 257 | * @param tcb TCB of the thread whose thread-local variable instance should
|
---|
| 258 | * be returned. If @a tcb is @c NULL then @c NULL is returned for
|
---|
| 259 | * thread-local variables.
|
---|
| 260 | *
|
---|
| 261 | * @return Symbol address
|
---|
| 262 | */
|
---|
| 263 | void *symbol_get_addr(elf_symbol_t *sym, module_t *m, tcb_t *tcb)
|
---|
[1ea99cc] | 264 | {
|
---|
[e2f26002] | 265 | if (ELF_ST_TYPE(sym->st_info) == STT_TLS) {
|
---|
[d2bb25e7] | 266 | if (tcb == NULL)
|
---|
| 267 | return NULL;
|
---|
| 268 | return rtld_tls_get_addr(m->rtld, tcb, m->id, sym->st_value);
|
---|
[e2f26002] | 269 | } else if (sym->st_shndx == SHN_ABS) {
|
---|
[1ea99cc] | 270 | /* Do not add bias to absolute symbols */
|
---|
[04803bf] | 271 | return (void *) sym->st_value;
|
---|
[1ea99cc] | 272 | } else {
|
---|
[04803bf] | 273 | return (void *) (sym->st_value + m->bias);
|
---|
[1ea99cc] | 274 | }
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | /** @}
|
---|
| 278 | */
|
---|