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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a30c04d was e8975278, checked in by Jiri Svoboda <jiri@…>, 8 years ago

A few more cstyle fixes.

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