Rev 2996 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2996 | Rev 2998 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup rtld rtld |
29 | /** @addtogroup rtld rtld |
30 | * @brief |
30 | * @brief |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <stdio.h> |
37 | #include <stdio.h> |
38 | 38 | ||
39 | #include <arch.h> |
39 | #include <arch.h> |
40 | #include <elf_dyn.h> |
40 | #include <elf_dyn.h> |
41 | #include <symbol.h> |
41 | #include <symbol.h> |
42 | #include <rtld.h> |
42 | #include <rtld.h> |
43 | 43 | ||
44 | void module_process_pre_arch(module_t *m) |
44 | void module_process_pre_arch(module_t *m) |
45 | { |
45 | { |
46 | /* Unused */ |
46 | /* Unused */ |
47 | } |
47 | } |
48 | 48 | ||
49 | 49 | ||
50 | /** |
50 | /** |
51 | * Process (fixup) all relocations in a relocation table. |
51 | * Process (fixup) all relocations in a relocation table. |
52 | */ |
52 | */ |
53 | void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size) |
53 | void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size) |
54 | { |
54 | { |
55 | int i; |
55 | int i; |
56 | 56 | ||
57 | size_t rt_entries; |
57 | size_t rt_entries; |
58 | size_t r_offset; |
58 | size_t r_offset; |
59 | elf_word r_info; |
59 | elf_word r_info; |
60 | unsigned rel_type; |
60 | unsigned rel_type; |
61 | elf_word sym_idx; |
61 | elf_word sym_idx; |
62 | uintptr_t sym_addr; |
62 | uintptr_t sym_addr; |
63 | 63 | ||
64 | elf_symbol_t *sym_table; |
64 | elf_symbol_t *sym_table; |
65 | elf_symbol_t *sym; |
65 | elf_symbol_t *sym; |
66 | uint32_t *r_ptr; |
66 | uint32_t *r_ptr; |
67 | char *str_tab; |
67 | char *str_tab; |
68 | 68 | ||
69 | elf_symbol_t *sym_def; |
69 | elf_symbol_t *sym_def; |
70 | module_t *dest; |
70 | module_t *dest; |
71 | 71 | ||
72 | printf("parse relocation table\n"); |
72 | printf("parse relocation table\n"); |
73 | 73 | ||
74 | sym_table = m->dyn.sym_tab; |
74 | sym_table = m->dyn.sym_tab; |
75 | rt_entries = rt_size / sizeof(elf_rel_t); |
75 | rt_entries = rt_size / sizeof(elf_rel_t); |
76 | str_tab = m->dyn.str_tab; |
76 | str_tab = m->dyn.str_tab; |
77 | 77 | ||
78 | printf("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries); |
78 | printf("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries); |
79 | 79 | ||
80 | for (i = 0; i < rt_entries; ++i) { |
80 | for (i = 0; i < rt_entries; ++i) { |
81 | // printf("symbol %d: ", i); |
81 | // printf("symbol %d: ", i); |
82 | r_offset = rt[i].r_offset; |
82 | r_offset = rt[i].r_offset; |
83 | r_info = rt[i].r_info; |
83 | r_info = rt[i].r_info; |
84 | 84 | ||
85 | sym_idx = ELF32_R_SYM(r_info); |
85 | sym_idx = ELF32_R_SYM(r_info); |
86 | sym = &sym_table[sym_idx]; |
86 | sym = &sym_table[sym_idx]; |
87 | 87 | ||
88 | /* printf("name '%s', value 0x%x, size 0x%x\n", |
88 | /* printf("name '%s', value 0x%x, size 0x%x\n", |
89 | str_tab + sym->st_name, |
89 | str_tab + sym->st_name, |
90 | sym->st_value, |
90 | sym->st_value, |
91 | sym->st_size); |
91 | sym->st_size); |
92 | */ |
92 | */ |
93 | rel_type = ELF32_R_TYPE(r_info); |
93 | rel_type = ELF32_R_TYPE(r_info); |
94 | r_ptr = (uint32_t *)(r_offset + m->bias); |
94 | r_ptr = (uint32_t *)(r_offset + m->bias); |
95 | 95 | ||
96 | if (sym->st_name != 0) { |
96 | if (sym->st_name != 0) { |
97 | // printf("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset); |
97 | // printf("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset); |
98 | sym_def = symbol_def_find(str_tab + sym->st_name, &dest); |
98 | sym_def = symbol_def_find(str_tab + sym->st_name, &dest); |
99 | // printf("dest name: '%s'\n", dest->dyn.soname); |
99 | // printf("dest name: '%s'\n", dest->dyn.soname); |
100 | // printf("dest bias: 0x%x\n", dest->bias); |
100 | // printf("dest bias: 0x%x\n", dest->bias); |
101 | if (sym_def) { |
101 | if (sym_def) { |
102 | if (sym_def->st_shndx == SHN_ABS) { |
- | |
103 | /* Do not add bias to absolute symbols */ |
- | |
104 | sym_addr = sym_def->st_value; |
- | |
105 | } else { |
- | |
106 | sym_addr = sym_def->st_value + dest->bias; |
102 | sym_addr = symbol_get_addr(sym_def, dest); |
107 | } |
- | |
108 | // printf("symbol definition found, addr=0x%x\n", sym_addr); |
103 | // printf("symbol definition found, addr=0x%x\n", sym_addr); |
109 | } else { |
104 | } else { |
110 | printf("symbol definition not found\n"); |
105 | printf("symbol definition not found\n"); |
111 | continue; |
106 | continue; |
112 | } |
107 | } |
113 | } |
108 | } |
114 | 109 | ||
115 | switch (rel_type) { |
110 | switch (rel_type) { |
116 | case R_386_GLOB_DAT: |
111 | case R_386_GLOB_DAT: |
117 | case R_386_JUMP_SLOT: |
112 | case R_386_JUMP_SLOT: |
118 | // printf("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n"); |
113 | // printf("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n"); |
119 | *r_ptr = sym_addr; |
114 | *r_ptr = sym_addr; |
120 | break; |
115 | break; |
121 | 116 | ||
122 | case R_386_32: |
117 | case R_386_32: |
123 | // printf("fixup R_386_32 (b+v+a)\n"); |
118 | // printf("fixup R_386_32 (b+v+a)\n"); |
124 | *r_ptr += sym_addr; |
119 | *r_ptr += sym_addr; |
125 | break; |
120 | break; |
126 | 121 | ||
127 | case R_386_RELATIVE: |
122 | case R_386_RELATIVE: |
128 | // printf("fixup R_386_RELATIVE (b+a)\n"); |
123 | // printf("fixup R_386_RELATIVE (b+a)\n"); |
129 | *r_ptr += m->bias; |
124 | *r_ptr += m->bias; |
130 | break; |
125 | break; |
131 | } |
126 | } |
132 | } |
127 | } |
133 | 128 | ||
134 | } |
129 | } |
135 | 130 | ||
136 | void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size) |
131 | void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size) |
137 | { |
132 | { |
138 | /* Unused */ |
133 | /* Unused */ |
139 | (void)m; (void)rt; (void)rt_size; |
134 | (void)m; (void)rt; (void)rt_size; |
140 | } |
135 | } |
141 | 136 | ||
142 | /** @} |
137 | /** @} |
143 | */ |
138 | */ |
144 | 139 |