source: mainline/uspace/lib/c/arch/ia32/src/rtld/reloc.c@ bd76871

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bd76871 was 33f86a3, checked in by jzr <zarevucky.jiri@…>, 8 years ago

Add PRI*PTR macros and correct a few printfs.

  • Property mode set to 100644
File size: 5.3 KB
Line 
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 libcia32
30 * @brief
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <inttypes.h>
40
41#include <libarch/rtld/elf_dyn.h>
42#include <rtld/symbol.h>
43#include <rtld/rtld.h>
44#include <rtld/rtld_debug.h>
45#include <rtld/rtld_arch.h>
46
47void module_process_pre_arch(module_t *m)
48{
49 /* Unused */
50}
51
52
53/**
54 * Process (fixup) all relocations in a relocation table.
55 */
56void rel_table_process(module_t *m, elf_rel_t *rt, size_t rt_size)
57{
58 unsigned i;
59
60 size_t rt_entries;
61 size_t r_offset;
62 elf_word r_info;
63 unsigned rel_type;
64 elf_word sym_idx;
65 uint32_t sym_addr;
66
67 elf_symbol_t *sym_table;
68 elf_symbol_t *sym;
69 uint32_t *r_ptr;
70 uint32_t sym_size;
71 char *str_tab;
72
73 elf_symbol_t *sym_def;
74 module_t *dest;
75
76 DPRINTF("parse relocation table\n");
77
78 sym_table = m->dyn.sym_tab;
79 rt_entries = rt_size / sizeof(elf_rel_t);
80 str_tab = m->dyn.str_tab;
81
82 DPRINTF("address: 0x%" PRIxPTR ", entries: %zd\n", (uintptr_t)rt, rt_entries);
83
84 for (i = 0; i < rt_entries; ++i) {
85// DPRINTF("symbol %d: ", i);
86 r_offset = rt[i].r_offset;
87 r_info = rt[i].r_info;
88
89 sym_idx = ELF32_R_SYM(r_info);
90 sym = &sym_table[sym_idx];
91
92/* DPRINTF("name '%s', value 0x%x, size 0x%x\n",
93 str_tab + sym->st_name,
94 sym->st_value,
95 sym->st_size);
96*/
97 rel_type = ELF32_R_TYPE(r_info);
98 r_ptr = (uint32_t *)(r_offset + m->bias);
99
100 if (sym->st_name != 0) {
101// DPRINTF("rel_type: %x, rel_offset: 0x%x\n", rel_type, r_offset);
102 sym_def = symbol_def_find(str_tab + sym->st_name,
103 m, ssf_none, &dest);
104 DPRINTF("dest name: '%s'\n", dest->dyn.soname);
105// DPRINTF("dest bias: 0x%x\n", dest->bias);
106 if (sym_def) {
107 sym_addr = (uint32_t)
108 symbol_get_addr(sym_def, dest, NULL);
109// DPRINTF("symbol definition found, addr=0x%x\n", sym_addr);
110 } else {
111 printf("Definition of '%s' not found.\n",
112 str_tab + sym->st_name);
113 continue;
114 }
115 } else {
116 sym_addr = 0;
117 sym_def = NULL;
118
119 /*
120 * DTPMOD with null st_name should return the index
121 * of the current module.
122 */
123 dest = m;
124 }
125
126 switch (rel_type) {
127 case R_386_GLOB_DAT:
128 case R_386_JUMP_SLOT:
129 DPRINTF("fixup R_386_GLOB_DAT/JUMP_SLOT (b+v)\n");
130 *r_ptr = sym_addr;
131 break;
132
133 case R_386_32:
134 DPRINTF("fixup R_386_32 (b+v+a)\n");
135 *r_ptr += sym_addr;
136 break;
137
138 case R_386_PC32:
139 DPRINTF("fixup R_386_PC32 (b+v+a-p)\n");
140 *r_ptr += sym_addr - (uint32_t) r_ptr;
141 break;
142
143 case R_386_COPY:
144 /*
145 * Copy symbol data from shared object to specified
146 * location. Need to find the 'source', i.e. the
147 * other instance of the object than the one in the
148 * executable program.
149 */
150 DPRINTF("fixup R_386_COPY (s)\n");
151
152 sym_def = symbol_def_find(str_tab + sym->st_name,
153 m, ssf_noexec, &dest);
154
155 if (sym_def) {
156 sym_addr = (uint32_t)
157 symbol_get_addr(sym_def, dest, NULL);
158 } else {
159 printf("Source definition of '%s' not found.\n",
160 str_tab + sym->st_name);
161 continue;
162 }
163
164 sym_size = sym->st_size;
165 if (sym_size != sym_def->st_size) {
166 printf("Warning: Mismatched symbol sizes.\n");
167 /* Take the lower value. */
168 if (sym_size > sym_def->st_size)
169 sym_size = sym_def->st_size;
170 }
171
172 memcpy(r_ptr, (const void *)sym_addr, sym_size);
173 break;
174
175 case R_386_RELATIVE:
176 DPRINTF("fixup R_386_RELATIVE (b+a)\n");
177 *r_ptr += m->bias;
178 break;
179
180 case R_386_TLS_TPOFF:
181 DPRINTF("fixup R_386_TLS_TPOFF\n");
182 *r_ptr = (dest->ioffs + sym_def->st_value) - dest->rtld->tls_size;
183 break;
184
185 case R_386_TLS_DTPOFF32:
186 DPRINTF("fixup R_386_TLS_DTPOFF32\n");
187 *r_ptr = sym_def->st_value;
188 break;
189
190 case R_386_TLS_DTPMOD32:
191 DPRINTF("fixup R_386_TLS_DTPMOD32\n");
192 *r_ptr = dest->id;
193 break;
194
195 default:
196 printf("Error: Unknown relocation type %d\n",
197 rel_type);
198 exit(1);
199 }
200
201 }
202
203}
204
205void rela_table_process(module_t *m, elf_rela_t *rt, size_t rt_size)
206{
207 /* Unused */
208 (void)m; (void)rt; (void)rt_size;
209}
210
211/** @}
212 */
Note: See TracBrowser for help on using the repository browser.