OSDN Git Service

Merge commit 'origin/master' into prelink
[uclinux-h8/uClibc.git] / ldso / ldso / sh / elfinterp.c
1 /* vi: set sw=4 ts=4: */
2 /* SuperH ELF shared library loader suppport
3  *
4  * Copyright (C) 2002, Stefan Allius <allius@atecom.com> and
5  *                     Eddie C. Dost <ecd@atecom.com>
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. The name of the above contributors may not be
15  *    used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30
31 /* Program to load an ELF binary on a linux system, and run it.
32    References to symbols in sharable libraries can be resolved by either
33    an ELF sharable library or a linux style of shared library. */
34
35 /* Disclaimer:  I have never seen any AT&T source code for SVr4, nor have
36    I ever taken any courses on internals.  This program was developed using
37    information available through the book "UNIX SYSTEM V RELEASE 4,
38    Programmers guide: Ansi C and Programming Support Tools", which did
39    a more than adequate job of explaining everything required to get this
40    working. */
41
42 #include "ldso.h"
43
44 extern int _dl_linux_resolve(void);
45
46 unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
47 {
48         ELF_RELOC *this_reloc;
49         char *strtab;
50         Elf32_Sym *symtab;
51         int symtab_index;
52         char *rel_addr;
53         char *new_addr;
54         char **got_addr;
55         unsigned long instr_addr;
56         char *symname;
57
58         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
59
60         this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
61         symtab_index = ELF32_R_SYM(this_reloc->r_info);
62
63         symtab = (Elf32_Sym *)(intptr_t) tpnt->dynamic_info[DT_SYMTAB];
64         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
65         symname = strtab + symtab[symtab_index].st_name;
66
67         /* Address of jump instruction to fix up */
68         instr_addr = (unsigned long) (this_reloc->r_offset + tpnt->loadaddr);
69         got_addr = (char **) instr_addr;
70
71         /* Get the address of the GOT entry */
72         new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, NULL, ELF_RTYPE_CLASS_PLT, NULL);
73
74         if (unlikely(!new_addr)) {
75                 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
76                 _dl_exit(1);
77         }
78
79 #if defined (__SUPPORT_LD_DEBUG__)
80         if ((unsigned long) got_addr < 0x20000000) {
81                 if (_dl_debug_bindings) {
82                         _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
83                         if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
84                                         "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
85                 }
86         }
87         if (!_dl_debug_nofixups)
88                 *got_addr = new_addr;
89 #else
90         *got_addr = new_addr;
91 #endif
92
93         return (unsigned long) new_addr;
94 }
95
96
97 static int
98 _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
99           unsigned long rel_addr, unsigned long rel_size,
100           int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
101                             ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
102 {
103         unsigned int i;
104         char *strtab;
105         Elf32_Sym *symtab;
106         ELF_RELOC *rpnt;
107         int symtab_index;
108         /* Now parse the relocation information */
109
110         rpnt = (ELF_RELOC *)(intptr_t) rel_addr;
111         rel_size = rel_size / sizeof(ELF_RELOC);
112
113         symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
114         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
115
116         for (i = 0; i < rel_size; i++, rpnt++) {
117                 int res;
118
119                 symtab_index = ELF32_R_SYM(rpnt->r_info);
120                 debug_sym(symtab,strtab,symtab_index);
121                 debug_reloc(symtab,strtab,rpnt);
122
123                 res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
124
125                 if (res == 0) continue;
126
127                 _dl_dprintf(2, "\n%s: ",_dl_progname);
128
129                 if (symtab_index)
130                         _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
131
132                 if (unlikely(res < 0)) {
133                         int reloc_type = ELF32_R_TYPE(rpnt->r_info);
134 #if defined (__SUPPORT_LD_DEBUG__)
135                         _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
136 #else
137                         _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
138 #endif
139                         _dl_exit(-res);
140                 }
141                 if (unlikely(res > 0)) {
142                         _dl_dprintf(2, "can't resolve symbol\n");
143                         return res;
144                 }
145         }
146         return 0;
147 }
148
149
150 static int
151 _dl_do_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
152               ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
153 {
154         int reloc_type;
155         int symtab_index;
156         char *symname;
157         unsigned long *reloc_addr;
158         unsigned long symbol_addr;
159 #if defined (__SUPPORT_LD_DEBUG__)
160         unsigned long old_val;
161 #endif
162
163         struct elf_resolve *tls_tpnt = NULL;
164         struct sym_val current_value = { NULL, NULL };
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
171         if (symtab_index) {
172                 symname = strtab + symtab[symtab_index].st_name;
173                 if (ELF32_ST_VISIBILITY(symtab[symtab_index].st_other)
174                         != STV_PROTECTED) {
175                         symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt, &current_value,
176                                                                         elf_machine_type_class(reloc_type), &tls_tpnt);
177                         /*
178                          * We want to allow undefined references to weak symbols - this might
179                          * have been intentional.  We should not be linking local symbols
180                          * here, so all bases should be covered.
181                          */
182
183                         if (!symbol_addr
184                                 && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
185                                 && (ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
186                                 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
187                                                 _dl_progname, strtab + symtab[symtab_index].st_name);
188
189                                 /* Let the caller to handle the error: it may be non fatal if called from dlopen */
190                                 return 1;
191                         }
192                 } else
193                         /* Resolve protected symbols locally */
194                         symbol_addr = DL_FIND_HASH_VALUE(tpnt, elf_machine_type_class(reloc_type),
195                                                                                         &symtab[symtab_index]);
196
197                 if (_dl_trace_prelink)
198                         _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
199                                                         &current_value, elf_machine_type_class(reloc_type));
200         }
201
202 #if defined (__SUPPORT_LD_DEBUG__)
203         old_val = *reloc_addr;
204 #endif
205
206 #if defined USE_TLS && USE_TLS
207         /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
208            symbol.  This is the case for a static tls variable, so the lookup
209            module is just that one is referencing the tls variable. */
210         if (!tls_tpnt)
211                 tls_tpnt = tpnt;
212 #endif
213         switch (reloc_type) {
214                 case R_SH_NONE:
215                         break;
216                 case R_SH_COPY:
217                         if (symbol_addr) {
218 #if defined (__SUPPORT_LD_DEBUG__)
219                                 if (_dl_debug_move)
220                                         _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
221                                                     symname, symtab[symtab_index].st_size,
222                                                     symbol_addr, reloc_addr);
223 #endif
224                                 _dl_memcpy((char *) reloc_addr, (char *) symbol_addr, symtab[symtab_index].st_size);
225                         }
226                         return 0; /* no further LD_DEBUG messages for copy relocs */
227                 case R_SH_DIR32:
228                 case R_SH_GLOB_DAT:
229                 case R_SH_JMP_SLOT:
230                         *reloc_addr = symbol_addr + rpnt->r_addend;
231                         break;
232                 case R_SH_REL32:
233                         *reloc_addr = symbol_addr + rpnt->r_addend -
234                                         (unsigned long) reloc_addr;
235                         break;
236                 case R_SH_RELATIVE:
237                         *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
238                         break;
239 #if defined USE_TLS && USE_TLS
240                 case R_SH_TLS_DTPMOD32:
241                         *reloc_addr = tls_tpnt->l_tls_modid;
242                         break;
243                 case R_SH_TLS_DTPOFF32:
244                         *reloc_addr = symbol_addr;
245                         break;
246                 case R_SH_TLS_TPOFF32:
247                         CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
248                         *reloc_addr = tls_tpnt->l_tls_offset + symbol_addr + rpnt->r_addend;
249                         break;
250 #endif
251                 default:
252
253                         return -1;
254         }
255 #if defined (__SUPPORT_LD_DEBUG__)
256             if (_dl_debug_reloc && _dl_debug_detail)
257                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
258 #endif
259
260         return 0;
261 }
262
263
264 static int
265 _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
266                    ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
267 {
268         int reloc_type;
269         unsigned long *reloc_addr;
270 #if defined (__SUPPORT_LD_DEBUG__)
271         unsigned long old_val;
272 #endif
273         (void)scope;
274         (void)symtab;
275         (void)strtab;
276
277         reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
278         reloc_type = ELF32_R_TYPE(rpnt->r_info);
279
280 #if defined (__SUPPORT_LD_DEBUG__)
281         old_val = *reloc_addr;
282 #endif
283         switch (reloc_type) {
284                 case R_SH_NONE:
285                         break;
286                 case R_SH_JMP_SLOT:
287                         *reloc_addr += (unsigned long) tpnt->loadaddr;
288                         break;
289                 default:
290                         return -1;
291         }
292 #if defined (__SUPPORT_LD_DEBUG__)
293         if (_dl_debug_reloc && _dl_debug_detail)
294                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
295 #endif
296         return 0;
297
298 }
299
300 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
301         unsigned long rel_addr, unsigned long rel_size)
302 {
303         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
304 }
305
306 int _dl_parse_relocation_information(struct dyn_elf *rpnt,
307         struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
308 {
309         return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
310 }