OSDN Git Service

Merge remote branch 'origin/master' into prelink
[uclinux-h8/uClibc.git] / ldso / ldso / cris / elfinterp.c
1 /*
2  * CRIS ELF shared library loader support.
3  *
4  * Program to load an elf binary on a linux system, and run it.
5  * References to symbols in sharable libraries can be resolved
6  * by either an ELF sharable library or a linux style of shared
7  * library.
8  *
9  * Copyright (C) 2002-2004, Axis Communications AB
10  * All rights reserved
11  *
12  * Author: Tobias Anderberg, <tobiasa@axis.com>
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. The name of the above contributors may not be
20  *    used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 #include "ldso.h"
37
38 /* Defined in resolve.S. */
39 extern int _dl_linux_resolve(void);
40
41 unsigned long
42 _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
43 {
44         int symtab_index;
45         char *strtab;
46         char *symname;
47         char *new_addr;
48         char *rel_addr;
49         char **got_addr;
50         Elf32_Sym *symtab;
51         ELF_RELOC *this_reloc;
52         unsigned long instr_addr;
53
54         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
55
56         this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
57         symtab_index = ELF32_R_SYM(this_reloc->r_info);
58
59         symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
60         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
61         symname = strtab + symtab[symtab_index].st_name;
62
63         /* Address of the jump instruction to fix up. */
64         instr_addr = ((unsigned long)this_reloc->r_offset +
65                       (unsigned long)tpnt->loadaddr);
66         got_addr = (char **)instr_addr;
67
68         /* Get the address of the GOT entry. */
69         new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
70         if (unlikely(!new_addr)) {
71                 _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
72                 _dl_exit(1);
73         }
74
75 #if defined (__SUPPORT_LD_DEBUG__)
76         if (_dl_debug_bindings) {
77                 _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
78                 if (_dl_debug_detail)
79                         _dl_dprintf(_dl_debug_file,
80                                     "\n\tpatched: %x ==> %x @ %x\n",
81                                     *got_addr, new_addr, got_addr);
82         }
83         if (!_dl_debug_nofixups) {
84                 *got_addr = new_addr;
85         }
86 #else
87         *got_addr = new_addr;
88 #endif
89
90         return (unsigned long)new_addr;
91 }
92
93 static int
94 _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
95           unsigned long rel_addr, unsigned long rel_size,
96           int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
97                            ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
98 {
99         int symtab_index;
100         unsigned int i;
101         char *strtab;
102         Elf32_Sym *symtab;
103         ELF_RELOC *rpnt;
104
105         /* Parse the relocation information. */
106         rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
107         rel_size /= sizeof(ELF_RELOC);
108
109         symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
110         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
111
112         for (i = 0; i < rel_size; i++, rpnt++) {
113                 int res;
114
115                 symtab_index = ELF32_R_SYM(rpnt->r_info);
116
117                 debug_sym(symtab, strtab, symtab_index);
118                 debug_reloc(symtab, strtab, rpnt);
119
120                 /* Pass over to actual relocation function. */
121                 res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
122
123                 if (res == 0)
124                         continue;
125
126                 _dl_dprintf(2, "\n%s: ", _dl_progname);
127
128                 if (symtab_index)
129                         _dl_dprintf(2, "symbol '%s': ",
130                                     strtab + symtab[symtab_index].st_name);
131
132                 if (unlikely(res < 0)) {
133                         int reloc_type = ELF32_R_TYPE(rpnt->r_info);
134
135 #if defined (__SUPPORT_LD_DEBUG__)
136                         _dl_dprintf(2, "can't handle reloc type %s\n",
137                                     _dl_reltypes(reloc_type));
138 #else
139                         _dl_dprintf(2, "can't handle reloc type %x\n",
140                                     reloc_type);
141 #endif
142                         _dl_exit(-res);
143                 } else if (unlikely(res > 0)) {
144                         _dl_dprintf(2, "can't resolve symbol\n");
145                         return res;
146                 }
147         }
148
149         return 0;
150 }
151
152 static int
153 _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
154              ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
155 {
156         int reloc_type;
157         int symtab_index;
158         char *symname;
159         unsigned long *reloc_addr;
160         unsigned long symbol_addr;
161 #if defined (__SUPPORT_LD_DEBUG__)
162         unsigned long old_val;
163 #endif
164         struct symbol_ref sym_ref;
165
166         reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
167         reloc_type = ELF32_R_TYPE(rpnt->r_info);
168         symtab_index = ELF32_R_SYM(rpnt->r_info);
169         symbol_addr = 0;
170         sym_ref.sym = &symtab[symtab_index];
171         sym_ref.tpnt = NULL;
172         symname = strtab + symtab[symtab_index].st_name;
173
174         if (symtab_index) {
175                 if (symtab[symtab_index].st_shndx != SHN_UNDEF &&
176                     ELF32_ST_BIND(symtab[symtab_index].st_info) == STB_LOCAL) {
177                         symbol_addr = (unsigned long)tpnt->loadaddr;
178                 } else {
179                   symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
180                                                                    elf_machine_type_class(reloc_type), &sym_ref);
181                 }
182
183                 if (unlikely(!symbol_addr && ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
184                         _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
185                         _dl_exit(1);
186                 }
187
188                 symbol_addr += rpnt->r_addend;
189                 if (_dl_trace_prelink)
190                         _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
191                                 &sym_ref, elf_machine_type_class(reloc_type));
192         }
193
194 #if defined (__SUPPORT_LD_DEBUG__)
195         old_val = *reloc_addr;
196 #endif
197
198         switch (reloc_type) {
199                 case R_CRIS_NONE:
200                         break;
201                 case R_CRIS_GLOB_DAT:
202                 case R_CRIS_JUMP_SLOT:
203                 case R_CRIS_32:
204                         *reloc_addr = symbol_addr;
205                         break;
206                 case R_CRIS_COPY:
207 #if defined (__SUPPORT_LD_DEBUG__)
208                         if (_dl_debug_move)
209                                 _dl_dprintf(_dl_debug_file,
210                                             "\n%s move %d bytes from %x to %x",
211                                             symname, symtab[symtab_index].st_size,
212                                             symbol_addr, reloc_addr);
213 #endif
214
215                         _dl_memcpy((char *)reloc_addr,
216                                    (char *)symbol_addr,
217                                    symtab[symtab_index].st_size);
218                         break;
219                 case R_CRIS_RELATIVE:
220                         *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
221                         break;
222                 default:
223                         return -1;      /* Calls _dl_exit(1). */
224         }
225
226 #if defined (__SUPPORT_LD_DEBUG__)
227         if (_dl_debug_reloc && _dl_debug_detail)
228                 _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
229                             old_val, *reloc_addr, reloc_addr);
230 #endif
231
232         return 0;
233 }
234
235 static int
236 _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
237                   ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
238 {
239         int reloc_type;
240         unsigned long *reloc_addr;
241 #if defined (__SUPPORT_LD_DEBUG__)
242         unsigned long old_val;
243 #endif
244
245         /* Don't care about these, just keep the compiler happy. */
246         (void)scope;
247         (void)symtab;
248         (void)strtab;
249
250         reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
251         reloc_type = ELF32_R_TYPE(rpnt->r_info);
252
253 #if defined (__SUPPORT_LD_DEBUG__)
254         old_val = *reloc_addr;
255 #endif
256
257         switch (reloc_type) {
258                 case R_CRIS_NONE:
259                         break;
260                 case R_CRIS_JUMP_SLOT:
261                         *reloc_addr += (unsigned long)tpnt->loadaddr;
262                         break;
263                 default:
264                         return -1;      /* Calls _dl_exit(1). */
265         }
266
267 #if defined (__SUPPORT_LD_DEBUG__)
268         if (_dl_debug_reloc && _dl_debug_detail)
269                 _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
270                             old_val, *reloc_addr, reloc_addr);
271 #endif
272
273         return 0;
274 }
275
276 /* External interface to the generic part of the dynamic linker. */
277
278 void
279 _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
280                                       unsigned long rel_addr,
281                                       unsigned long rel_size)
282 {
283         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
284 }
285
286 int
287 _dl_parse_relocation_information(struct dyn_elf *rpnt,
288                                  struct r_scope_elem *scope,
289                                  unsigned long rel_addr,
290                                  unsigned long rel_size)
291 {
292         return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
293 }