OSDN Git Service

Merge commit 'origin/master' into prelink
[uclinux-h8/uClibc.git] / ldso / ldso / sparc / elfinterp.c
1 /* vi: set sw=4 ts=4: */
2 /* sparc ELF shared library loader suppport
3  *
4  * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
5  *                              David Engel, Hongjiu Lu and Mitch D'Souza
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 /* Some SPARC opcodes we need to use for self-modifying code.  */
43 #define OPCODE_NOP      0x01000000 /* nop */
44 #define OPCODE_CALL     0x40000000 /* call ?; add PC-rel word address */
45 #define OPCODE_SETHI_G1 0x03000000 /* sethi ?, %g1; add value>>10 */
46 #define OPCODE_JMP_G1   0x81c06000 /* jmp %g1+?; add lo 10 bits of value */
47 #define OPCODE_SAVE_SP  0x9de3bfa8 /* save %sp, -(16+6)*4, %sp */
48 #define OPCODE_BA       0x30800000 /* b,a ?; add PC-rel word address */
49
50 extern int _dl_linux_resolve(void);
51
52 unsigned long
53 _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
54 {
55         ELF_RELOC *this_reloc;
56         char *strtab;
57         ElfW(Sym) *symtab;
58         int symtab_index;
59         char *rel_addr;
60         char *new_addr;
61         char **got_addr;
62         ElfW(Addr) instr_addr;
63         char *symname;
64
65         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
66         /*
67          * Generate the correct relocation index into the .rela.plt section.
68          */
69         reloc_entry = (reloc_entry >> 10) - 0xc;
70
71         this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
72         symtab_index = ELF_R_SYM(this_reloc->r_info);
73
74         symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
75         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
76         symname = strtab + symtab[symtab_index].st_name;
77
78         /* Address of the jump instruction to fix up. */
79         instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
80         got_addr = (char **)instr_addr;
81
82         /* Get the address of the GOT entry */
83         new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, NULL, ELF_RTYPE_CLASS_PLT, NULL);
84         if (unlikely(!new_addr)) {
85                 _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
86                 _dl_exit(1);
87         }
88
89 #if defined (__SUPPORT_LD_DEBUG__)
90         if ((unsigned long)got_addr < 0x40000000) {
91                 if (_dl_debug_bindings) {
92                         _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
93                         if (_dl_debug_detail)
94                                 _dl_dprintf(_dl_debug_file,
95                                             "\tpatched: %x ==> %x @ %x\n",
96                                             *got_addr, new_addr, got_addr);
97                 }
98         }
99         if (!_dl_debug_nofixups)
100 #endif
101         {
102                 got_addr[1] = (char *) (OPCODE_SETHI_G1 | (((unsigned int) new_addr >> 10) & 0x3fffff));
103                 got_addr[2] = (char *) (OPCODE_JMP_G1 | ((unsigned int) new_addr & 0x3ff));
104         }
105
106         return (unsigned long)new_addr;
107 }
108
109 static int
110 _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
111                 unsigned long rel_addr, unsigned long rel_size,
112                 int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
113                            ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
114 {
115         unsigned int i;
116         char *strtab;
117         ElfW(Sym) *symtab;
118         ELF_RELOC *rpnt;
119         int symtab_index;
120
121         /* Parse the relocation information. */
122         rpnt = (ELF_RELOC *)rel_addr;
123         rel_size /= sizeof(ELF_RELOC);
124
125         symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
126         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
127
128         for (i = 0; i < rel_size; i++, rpnt++) {
129                 int res;
130
131                 symtab_index = ELF_R_SYM(rpnt->r_info);
132
133                 debug_sym(symtab, strtab, symtab_index);
134                 debug_reloc(symtab, strtab, rpnt);
135
136                 res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
137
138                 if (res == 0)
139                         continue;
140
141                 _dl_dprintf(2, "\n%s: ", _dl_progname);
142
143                 if (symtab_index)
144                         _dl_dprintf(2, "symbol '%s': ",
145                                     strtab + symtab[symtab_index].st_name);
146
147                 if (unlikely(res < 0)) {
148                         int reloc_type = ELF_R_TYPE(rpnt->r_info);
149
150                         _dl_dprintf(2, "can't handle reloc type "
151 #if defined (__SUPPORT_LD_DEBUG__)
152                                     "%s\n", _dl_reltypes(reloc_type));
153 #else
154                                     "%x\n", reloc_type);
155 #endif
156                         _dl_exit(-res);
157                 } else if (unlikely(res > 0)) {
158                         _dl_dprintf(2, "can't resolve symbol\n");
159                         return res;
160                 }
161         }
162
163         return 0;
164 }
165
166 static int
167 _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
168                          ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
169 {
170         int reloc_type;
171         int symtab_index;
172         char *symname;
173         struct elf_resolve *tls_tpnt = 0;
174         ElfW(Sym) *sym;
175         ElfW(Addr) *reloc_addr;
176         ElfW(Addr) symbol_addr;
177 #if defined (__SUPPORT_LD_DEBUG__)
178         ElfW(Addr) old_val;
179 #endif
180
181         struct sym_val current_value = { NULL, NULL };
182
183         reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
184         reloc_type = ELF_R_TYPE(rpnt->r_info);
185         symtab_index = ELF_R_SYM(rpnt->r_info);
186         sym = &symtab[symtab_index];
187         symbol_addr = 0;
188         symname = strtab + sym->st_name;
189
190         if (symtab_index) {
191                 symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt, &current_value,
192                                                             elf_machine_type_class(reloc_type), &tls_tpnt);
193                 /*
194                  * We want to allow undefined references to weak symbols - this
195                  * might have been intentional.  We should not be linking local
196                  * symbols here, so all bases should be covered.
197                  */
198                 if (unlikely(!symbol_addr && (ELF_ST_TYPE(sym->st_info) != STT_TLS)
199                     && (ELF_ST_BIND(sym->st_info) != STB_WEAK))) {
200                         /* This may be non-fatal if called from dlopen. */
201                         return 1;
202
203                 }
204                 if (_dl_trace_prelink)
205                         _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
206                                                 &current_value, elf_machine_type_class(reloc_type));
207         } else {
208                 /* Relocs against STN_UNDEF are usually treated as using a
209                  * symbol value of zero, and using the module containing the
210                  * reloc itself. */
211                 symbol_addr = sym->st_value;
212                 tls_tpnt = tpnt;
213         }
214
215 #if defined (__SUPPORT_LD_DEBUG__)
216         old_val = *reloc_addr;
217 #endif
218
219         symbol_addr += rpnt->r_addend;  /* Assume copy relocs have zero addend.  */
220
221         switch (reloc_type) {
222                 case R_SPARC_NONE:
223                         break;
224
225                 case R_SPARC_DISP32:
226                         *reloc_addr = symbol_addr - (unsigned int) reloc_addr;
227                         break;
228
229                 case R_SPARC_LO10:
230                         if (!symbol_addr)
231                                 symbol_addr = tpnt->loadaddr + rpnt->r_addend;
232                         else
233                                 symbol_addr += rpnt->r_addend;
234                         *reloc_addr = (*reloc_addr & ~0x3ff) | (symbol_addr & 0x3ff);
235                         break;
236
237                 case R_SPARC_GLOB_DAT:
238                 case R_SPARC_32:
239                         *reloc_addr = symbol_addr;
240                         break;
241
242                 case R_SPARC_JMP_SLOT:
243                         reloc_addr[1] = OPCODE_SETHI_G1 | (( symbol_addr >> 10 ) & 0x3fffff);
244                         reloc_addr[2] = OPCODE_JMP_G1 | ( symbol_addr & 0x3ff );
245                         break;
246
247                 case R_SPARC_RELATIVE:
248                         *reloc_addr += tpnt->loadaddr + rpnt->r_addend;
249                         break;
250
251                 case R_SPARC_WDISP30:
252                         *reloc_addr = (*reloc_addr & 0xc0000000)|
253                                  ((symbol_addr - (unsigned int) reloc_addr) >> 2);
254                         break;
255
256                 case R_SPARC_HI22:
257                         if (!symbol_addr)
258                                 symbol_addr = tpnt->loadaddr + rpnt->r_addend;
259                         else
260                                 symbol_addr += rpnt->r_addend;
261                         *reloc_addr = (*reloc_addr & 0xffc00000) | (symbol_addr >> 10);
262                         break;
263
264                 case R_SPARC_COPY:
265                         if (symbol_addr) {
266 #if defined (__SUPPORT_LD_DEBUG__)
267                                 if (_dl_debug_move)
268                                         _dl_dprintf(_dl_debug_file,
269                                                     "\t%s move %d bytes from %x to %x\n",
270                                                     symname, sym->st_size,
271                                                     symbol_addr, reloc_addr);
272 #endif
273
274                                 _dl_memcpy((char *)reloc_addr,
275                                            (char *)symbol_addr,
276                                            sym->st_size);
277                         } else
278                                 _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
279                         break;
280 #if defined USE_TLS && USE_TLS
281                 case R_SPARC_TLS_DTPMOD32:
282                         *reloc_addr = tls_tpnt->l_tls_modid;
283                         break;
284
285                 case R_SPARC_TLS_DTPOFF32:
286                         /* During relocation all TLS symbols are defined and used.
287                          * Therefore the offset is already correct.  */
288                         *reloc_addr = sym->st_value + rpnt->r_addend;
289                         break;
290
291                 case R_SPARC_TLS_TPOFF32:
292                         /* The offset is negative, forward from the thread pointer.
293                          * We know the offset of the object the symbol is contained in.
294                          * It is a negative value which will be added to the
295                          * thread pointer.  */
296                         CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
297                         *reloc_addr = sym->st_value - tls_tpnt->l_tls_offset + rpnt->r_addend;
298                         break;
299 #endif
300                 default:
301                         return -1;      /* Calls _dl_exit(1). */
302         }
303
304 #if defined (__SUPPORT_LD_DEBUG__)
305         if (_dl_debug_reloc && _dl_debug_detail)
306                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
307                             old_val, *reloc_addr, reloc_addr);
308 #endif
309
310         return 0;
311 }
312
313 #undef __SPARC_LAZY_RELOC_WORKS
314 #ifdef __SPARC_LAZY_RELOC_WORKS
315 static int
316 _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
317                   ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
318 {
319         int reloc_type;
320         int symtab_index;
321         ElfW(Addr) *reloc_addr;
322 #if defined (__SUPPORT_LD_DEBUG__)
323         ElfW(Addr) old_val;
324 #endif
325
326         (void)scope;
327         symtab_index = ELF_R_SYM(rpnt->r_info);
328         (void)strtab;
329
330         reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
331         reloc_type = ELF_R_TYPE(rpnt->r_info);
332
333 #if defined (__SUPPORT_LD_DEBUG__)
334         old_val = *reloc_addr;
335 #endif
336
337         switch (reloc_type) {
338                 case R_SPARC_NONE:
339                         break;
340                 case R_SPARC_JMP_SLOT:
341                         break;
342                 default:
343                         _dl_exit(1);
344         }
345
346 #if defined (__SUPPORT_LD_DEBUG__)
347         if (_dl_debug_reloc && _dl_debug_detail)
348                 _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
349                             old_val, *reloc_addr, reloc_addr);
350 #endif
351
352         return 0;
353 }
354 #endif
355
356 void
357 _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
358                                       unsigned long rel_addr,
359                                       unsigned long rel_size)
360 {
361 #ifdef __SPARC_LAZY_RELOC_WORKS
362         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
363 #else
364         _dl_parse_relocation_information(rpnt, &_dl_loaded_modules->symbol_scope,
365                                                                         rel_addr, rel_size);
366 #endif
367 }
368
369 int
370 _dl_parse_relocation_information(struct dyn_elf *rpnt,
371                                  struct r_scope_elem *scope,
372                                  unsigned long rel_addr,
373                                  unsigned long rel_size)
374 {
375         return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
376 }