OSDN Git Service

ldso_sh: add support for protected symbols to SH
[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, tpnt->symbol_scope, tpnt, 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 dyn_elf *scope,
99           unsigned long rel_addr, unsigned long rel_size,
100           int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *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 dyn_elf *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
165         reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
166         reloc_type = ELF32_R_TYPE(rpnt->r_info);
167         symtab_index = ELF32_R_SYM(rpnt->r_info);
168         symbol_addr = 0;
169
170         if (symtab_index) {
171                 symname = strtab + symtab[symtab_index].st_name;
172                 if (ELF32_ST_VISIBILITY(symtab[symtab_index].st_other)
173                          != STV_PROTECTED) {
174                         symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt,
175                                                                 elf_machine_type_class(reloc_type), &tls_tpnt);
176                         /*
177                          * We want to allow undefined references to weak symbols - this might
178                          * have been intentional.  We should not be linking local symbols
179                          * here, so all bases should be covered.
180                          */
181
182                         if (!symbol_addr
183                                 && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
184                                 && (ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
185                                 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
186                                             _dl_progname, strtab + symtab[symtab_index].st_name);
187
188                                 /* Let the caller to handle the error: it may be non fatal if called from dlopen */
189                                 return 1;
190                         }
191                 } else
192                         /* Resolve protected symbols locally */
193                         symbol_addr = DL_FIND_HASH_VALUE(tpnt, elf_machine_type_class(reloc_type),
194                                                                                         &symtab[symtab_index]);
195         }
196
197 #if defined (__SUPPORT_LD_DEBUG__)
198         old_val = *reloc_addr;
199 #endif
200
201 #if defined USE_TLS && USE_TLS
202         /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
203            symbol.  This is the case for a static tls variable, so the lookup
204            module is just that one is referencing the tls variable. */
205         if (!tls_tpnt)
206                 tls_tpnt = tpnt;
207 #endif
208         switch (reloc_type) {
209                 case R_SH_NONE:
210                         break;
211                 case R_SH_COPY:
212                         if (symbol_addr) {
213 #if defined (__SUPPORT_LD_DEBUG__)
214                                 if (_dl_debug_move)
215                                         _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
216                                                     symname, symtab[symtab_index].st_size,
217                                                     symbol_addr, reloc_addr);
218 #endif
219                                 _dl_memcpy((char *) reloc_addr, (char *) symbol_addr, symtab[symtab_index].st_size);
220                         }
221                         return 0; /* no further LD_DEBUG messages for copy relocs */
222                 case R_SH_DIR32:
223                 case R_SH_GLOB_DAT:
224                 case R_SH_JMP_SLOT:
225                         *reloc_addr = symbol_addr + rpnt->r_addend;
226                         break;
227                 case R_SH_REL32:
228                         *reloc_addr = symbol_addr + rpnt->r_addend -
229                                         (unsigned long) reloc_addr;
230                         break;
231                 case R_SH_RELATIVE:
232                         *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
233                         break;
234 #if defined USE_TLS && USE_TLS
235                 case R_SH_TLS_DTPMOD32:
236                         *reloc_addr = tls_tpnt->l_tls_modid;
237                         break;
238                 case R_SH_TLS_DTPOFF32:
239                         *reloc_addr = symbol_addr;
240                         break;
241                 case R_SH_TLS_TPOFF32:
242                         CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
243                         *reloc_addr = tls_tpnt->l_tls_offset + symbol_addr + rpnt->r_addend;
244                         break;
245 #endif
246                 default:
247
248                         return -1;
249         }
250 #if defined (__SUPPORT_LD_DEBUG__)
251             if (_dl_debug_reloc && _dl_debug_detail)
252                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
253 #endif
254
255         return 0;
256 }
257
258
259 static int
260 _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
261                    ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
262 {
263         int reloc_type;
264         unsigned long *reloc_addr;
265 #if defined (__SUPPORT_LD_DEBUG__)
266         unsigned long old_val;
267 #endif
268         (void)scope;
269         (void)symtab;
270         (void)strtab;
271
272         reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
273         reloc_type = ELF32_R_TYPE(rpnt->r_info);
274
275 #if defined (__SUPPORT_LD_DEBUG__)
276         old_val = *reloc_addr;
277 #endif
278         switch (reloc_type) {
279                 case R_SH_NONE:
280                         break;
281                 case R_SH_JMP_SLOT:
282                         *reloc_addr += (unsigned long) tpnt->loadaddr;
283                         break;
284                 default:
285                         return -1;
286         }
287 #if defined (__SUPPORT_LD_DEBUG__)
288         if (_dl_debug_reloc && _dl_debug_detail)
289                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
290 #endif
291         return 0;
292
293 }
294
295 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
296         unsigned long rel_addr, unsigned long rel_size)
297 {
298         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
299 }
300
301 int _dl_parse_relocation_information(struct dyn_elf *rpnt,
302         unsigned long rel_addr, unsigned long rel_size)
303 {
304         return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
305 }