Rev 2980 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2980 | Rev 2982 | ||
---|---|---|---|
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 | /** |
44 | /** |
45 | * Process (fixup) all relocations in a relocation table. |
45 | * Process (fixup) all relocations in a relocation table. |
46 | */ |
46 | */ |
47 | void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size) |
47 | void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size) |
48 | { |
48 | { |
49 | int i; |
49 | int i; |
50 | 50 | ||
51 | size_t rt_entries; |
51 | size_t rt_entries; |
52 | size_t r_offset; |
52 | size_t r_offset; |
53 | elf_word r_info; |
53 | elf_word r_info; |
54 | unsigned rel_type; |
54 | unsigned rel_type; |
55 | elf_word sym_idx; |
55 | elf_word sym_idx; |
56 | uintptr_t sym_addr; |
56 | uintptr_t sym_addr; |
57 | 57 | ||
58 | elf_symbol_t *sym_table; |
58 | elf_symbol_t *sym_table; |
59 | elf_symbol_t *sym; |
59 | elf_symbol_t *sym; |
60 | uint32_t *r_ptr; |
60 | uint32_t *r_ptr; |
61 | char *str_tab; |
61 | char *str_tab; |
62 | 62 | ||
63 | elf_symbol_t *sym_def; |
63 | elf_symbol_t *sym_def; |
64 | module_t *dest; |
64 | module_t *dest; |
65 | 65 | ||
66 | printf("parse relocation table\n"); |
66 | printf("parse relocation table\n"); |
67 | 67 | ||
68 | sym_table = m->dyn.sym_tab; |
68 | sym_table = m->dyn.sym_tab; |
69 | rt_entries = rt_size / sizeof(elf_rel_t); |
69 | rt_entries = rt_size / sizeof(elf_rel_t); |
70 | str_tab = m->dyn.str_tab; |
70 | str_tab = m->dyn.str_tab; |
71 | 71 | ||
72 | printf("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries); |
72 | printf("address: 0x%x, entries: %d\n", (uintptr_t)rt, rt_entries); |
73 | 73 | ||
74 | for (i = 0; i < rt_entries; ++i) { |
74 | for (i = 0; i < rt_entries; ++i) { |
75 | printf("symbol %d: ", i); |
75 | printf("symbol %d: ", i); |
76 | r_offset = rt[i].r_offset; |
76 | r_offset = rt[i].r_offset; |
77 | r_info = rt[i].r_info; |
77 | r_info = rt[i].r_info; |
78 | 78 | ||
79 | sym_idx = ELF32_R_SYM(r_info); |
79 | sym_idx = ELF32_R_SYM(r_info); |
80 | sym = &sym_table[sym_idx]; |
80 | sym = &sym_table[sym_idx]; |
81 | 81 | ||
82 | printf("name '%s', value 0x%x, size 0x%x\n", |
82 | printf("name '%s', value 0x%x, size 0x%x\n", |
83 | str_tab + sym->st_name, |
83 | str_tab + sym->st_name, |
84 | sym->st_value, |
84 | sym->st_value, |
85 | sym->st_size); |
85 | sym->st_size); |
86 | 86 | ||
87 | rel_type = ELF32_R_TYPE(r_info); |
87 | rel_type = ELF32_R_TYPE(r_info); |
88 | r_ptr = (uint32_t *)(r_offset + m->bias); |
88 | r_ptr = (uint32_t *)(r_offset + m->bias); |
89 | 89 | ||
- | 90 | if (sym->st_name != 0) { |
|
90 | printf("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset); |
91 | printf("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset); |
91 | sym_def = symbol_def_find(str_tab + sym->st_name, &dest); |
92 | sym_def = symbol_def_find(str_tab + sym->st_name, &dest); |
92 | printf("dest name: '%s'\n", dest->dyn.soname); |
93 | printf("dest name: '%s'\n", dest->dyn.soname); |
93 | printf("dest bias: 0x%x\n", dest->bias); |
94 | printf("dest bias: 0x%x\n", dest->bias); |
94 | if (sym_def) { |
95 | if (sym_def) { |
95 | sym_addr = sym_def->st_value + dest->bias; |
96 | sym_addr = sym_def->st_value + dest->bias; |
96 | printf("symbol definition found, addr=0x%x\n", sym_addr); |
97 | printf("symbol definition found, addr=0x%x\n", sym_addr); |
97 | } else { |
98 | } else { |
98 | printf("symbol definition not found\n"); |
99 | printf("symbol definition not found\n"); |
99 | continue; |
100 | continue; |
- | 101 | } |
|
100 | } |
102 | } |
101 | 103 | ||
102 | switch (rel_type) { |
104 | switch (rel_type) { |
103 | case R_386_GLOB_DAT: |
105 | case R_386_GLOB_DAT: |
104 | case R_386_JUMP_SLOT: |
106 | case R_386_JUMP_SLOT: |
105 | printf("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n"); |
107 | printf("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n"); |
106 | *r_ptr = sym_addr; |
108 | *r_ptr = sym_addr; |
107 | break; |
109 | break; |
108 | 110 | ||
109 | case R_386_32: |
111 | case R_386_32: |
110 | printf("fixup R_386_32 (b+v+a)\n"); |
112 | printf("fixup R_386_32 (b+v+a)\n"); |
111 | *r_ptr += sym_addr; |
113 | *r_ptr += sym_addr; |
112 | break; |
114 | break; |
113 | 115 | ||
114 | case R_386_RELATIVE: |
116 | case R_386_RELATIVE: |
115 | printf("fixup R_386_RELATIVE (b+a)\n"); |
117 | printf("fixup R_386_RELATIVE (b+a)\n"); |
116 | *r_ptr += m->bias; |
118 | *r_ptr += m->bias; |
117 | break; |
119 | break; |
118 | } |
120 | } |
119 | } |
121 | } |
120 | 122 | ||
121 | } |
123 | } |
122 | 124 | ||
123 | /** @} |
125 | /** @} |
124 | */ |
126 | */ |
125 | 127 |