Rev 2981 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2978 | svoboda | 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 <stdio.h> |
||
38 | #include <stdlib.h> |
||
39 | #include <string.h> |
||
40 | |||
41 | #include <rtld.h> |
||
42 | #include <symbol.h> |
||
43 | #include <elf.h> |
||
44 | |||
2998 | svoboda | 45 | /* |
46 | * Hash tables are 32-bit (elf_word) even for 64-bit ELF files. |
||
47 | */ |
||
48 | static elf_word elf_hash(const unsigned char *name) |
||
49 | { |
||
50 | elf_word h = 0, g; |
||
51 | |||
52 | while (*name) { |
||
53 | h = (h << 4) + *name++; |
||
54 | g = h & 0xf0000000; |
||
55 | if (g != 0) h ^= g >> 24; |
||
56 | h &= ~g; |
||
57 | } |
||
58 | |||
59 | return h; |
||
60 | } |
||
61 | |||
2980 | svoboda | 62 | static elf_symbol_t *def_find_in_module(char *name, module_t *m) |
2978 | svoboda | 63 | { |
64 | elf_symbol_t *sym_table; |
||
2998 | svoboda | 65 | elf_symbol_t *s, *sym; |
66 | elf_word nbucket; |
||
2978 | svoboda | 67 | elf_word nchain; |
2998 | svoboda | 68 | elf_word i; |
2978 | svoboda | 69 | char *s_name; |
2998 | svoboda | 70 | elf_word bucket; |
2978 | svoboda | 71 | |
2998 | svoboda | 72 | // module_name = m->dyn.soname; |
2981 | svoboda | 73 | // printf("def_find_in_module('%s', %s)\n", name, module_name); |
2978 | svoboda | 74 | |
75 | sym_table = m->dyn.sym_tab; |
||
2998 | svoboda | 76 | nbucket = m->dyn.hash[0]; |
2978 | svoboda | 77 | nchain = m->dyn.hash[1]; |
78 | |||
2998 | svoboda | 79 | bucket = elf_hash((unsigned char *)name) % nbucket; |
80 | i = m->dyn.hash[2 + bucket]; |
||
81 | |||
82 | sym = NULL; |
||
83 | while (i != STN_UNDEF) { |
||
84 | s = &sym_table[i]; |
||
85 | s_name = m->dyn.str_tab + s->st_name; |
||
86 | |||
2978 | svoboda | 87 | if (strcmp(name, s_name) == 0) { |
2998 | svoboda | 88 | sym = s; |
89 | break; |
||
2978 | svoboda | 90 | } |
2998 | svoboda | 91 | |
92 | i = m->dyn.hash[2 + nbucket + i]; |
||
2978 | svoboda | 93 | } |
94 | |||
2998 | svoboda | 95 | if (!sym) |
96 | return NULL; /* Not found */ |
||
97 | |||
98 | if (sym->st_shndx == SHN_UNDEF) { |
||
99 | /* Not a definition */ |
||
100 | return NULL; |
||
101 | } |
||
102 | |||
103 | return sym; /* Found */ |
||
2978 | svoboda | 104 | } |
105 | |||
2980 | svoboda | 106 | |
107 | elf_symbol_t *symbol_def_find(char *name, module_t **m) |
||
108 | { |
||
109 | elf_symbol_t *sym; |
||
110 | |||
111 | sym = def_find_in_module(name, runtime_env.program); |
||
112 | if (sym != NULL) { *m = runtime_env.program; return sym; } |
||
113 | |||
114 | sym = def_find_in_module(name, runtime_env.libc); |
||
115 | if (sym != NULL) { *m = runtime_env.libc; return sym; } |
||
116 | |||
2981 | svoboda | 117 | sym = def_find_in_module(name, &runtime_env.rtld); |
118 | if (sym != NULL) { *m = &runtime_env.rtld; return sym; } |
||
2980 | svoboda | 119 | |
120 | printf("symbol found nowhere\n"); |
||
121 | *m = NULL; |
||
122 | return NULL; |
||
123 | } |
||
124 | |||
2998 | svoboda | 125 | uintptr_t symbol_get_addr(elf_symbol_t *sym, module_t *m) |
126 | { |
||
127 | if (sym->st_shndx == SHN_ABS) { |
||
128 | /* Do not add bias to absolute symbols */ |
||
129 | return sym->st_value; |
||
130 | } else { |
||
131 | return sym->st_value + m->bias; |
||
132 | } |
||
133 | } |
||
134 | |||
2978 | svoboda | 135 | /** @} |
136 | */ |