OSDN Git Service

a90e1636b4499db74d4532b8d8c045920200e5f0
[uclinux-h8/uClibc.git] / ldso / ldso / mips / elfinterp.c
1 /* vi: set sw=4 ts=4: */
2 /* mips/mipsel ELF shared library loader suppport
3  *
4    Copyright (C) 2002, Steven J. Hill (sjhill@realitydiluted.com)
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. The name of the above contributors may not be
14  *    used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include "ldso.h"
31
32 extern int _dl_runtime_resolve(void);
33 extern int _dl_runtime_pltresolve(void);
34
35 #define OFFSET_GP_GOT 0x7ff0
36
37 unsigned long __dl_runtime_resolve(unsigned long sym_index,
38         unsigned long old_gpreg)
39 {
40         unsigned long *got = (unsigned long *) (old_gpreg - OFFSET_GP_GOT);
41         struct elf_resolve *tpnt = (struct elf_resolve *) got[1];
42         ElfW(Sym) *sym;
43         char *strtab;
44         unsigned long local_gotno;
45         unsigned long gotsym;
46         unsigned long new_addr;
47         unsigned long instr_addr;
48         char **got_addr;
49         char *symname;
50
51         gotsym = tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
52         local_gotno = tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX];
53
54         sym = ((ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB]) + sym_index;
55         strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
56         symname = strtab + sym->st_name;
57
58         new_addr = (unsigned long) _dl_find_hash(symname,
59                         &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
60         if (unlikely(!new_addr)) {
61                 _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
62                                 _dl_progname, symname);
63                 _dl_exit (1);
64         }
65
66         /* Address of jump instruction to fix up */
67         instr_addr = (unsigned long) (got + local_gotno + sym_index - gotsym);
68         got_addr = (char **) instr_addr;
69
70 #if defined (__SUPPORT_LD_DEBUG__)
71         if (_dl_debug_bindings)
72         {
73                 _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
74                 if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
75                                 "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
76         }
77         if (!_dl_debug_nofixups) {
78                 *got_addr = (char*)new_addr;
79         }
80 #else
81         *got_addr = (char*)new_addr;
82 #endif
83
84         return new_addr;
85 }
86
87 unsigned long
88 __dl_runtime_pltresolve(struct elf_resolve *tpnt, int reloc_entry)
89 {
90         ELF_RELOC *this_reloc;
91         char *strtab;
92         ElfW(Sym) *symtab;
93         int symtab_index;
94         char *rel_addr;
95         char *new_addr;
96         char **got_addr;
97         unsigned long instr_addr;
98         char *symname;
99
100         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
101         this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
102         symtab_index = ELF_R_SYM(this_reloc->r_info);
103
104         symtab = (ElfW(Sym) *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
105         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
106         symname = strtab + symtab[symtab_index].st_name;
107
108         /* Address of the jump instruction to fix up. */
109         instr_addr = ((unsigned long)this_reloc->r_offset +
110                       (unsigned long)tpnt->loadaddr);
111         got_addr = (char **)instr_addr;
112
113         /* Get the address of the GOT entry. */
114         new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
115         if (unlikely(!new_addr)) {
116                 _dl_dprintf(2, "%s: can't resolve symbol '%s' in lib '%s'.\n", _dl_progname, symname, tpnt->libname);
117                 _dl_exit(1);
118         }
119
120 #if defined (__SUPPORT_LD_DEBUG__)
121         if ((unsigned long)got_addr < 0x40000000) {
122                 if (_dl_debug_bindings) {
123                         _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
124                         if (_dl_debug_detail)
125                                 _dl_dprintf(_dl_debug_file,
126                                             "\n\tpatched: %x ==> %x @ %x",
127                                             *got_addr, new_addr, got_addr);
128                 }
129         }
130         if (!_dl_debug_nofixups) {
131                 *got_addr = new_addr;
132         }
133 #else
134         *got_addr = new_addr;
135 #endif
136
137         return (unsigned long)new_addr;
138 }
139
140 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
141         unsigned long rel_addr, unsigned long rel_size)
142 {
143         /* Nothing to do */
144         return;
145 }
146
147 int _dl_parse_relocation_information(struct dyn_elf *xpnt,
148         struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
149 {
150         ElfW(Sym) *symtab;
151         ELF_RELOC *rpnt;
152         char *strtab;
153         unsigned long i;
154         unsigned long *got;
155         unsigned long *reloc_addr=NULL;
156         unsigned long symbol_addr;
157         int reloc_type, symtab_index;
158         struct elf_resolve *tpnt = xpnt->dyn;
159         char *symname = NULL;
160 #if defined (__SUPPORT_LD_DEBUG__)
161         unsigned long old_val=0;
162 #endif
163
164         struct symbol_ref sym_ref;
165         /* Now parse the relocation information */
166         rel_size = rel_size / sizeof(ElfW(Rel));
167         rpnt = (ELF_RELOC *) rel_addr;
168
169         symtab = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB];
170         strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
171         got = (unsigned long *) tpnt->dynamic_info[DT_PLTGOT];
172
173
174         for (i = 0; i < rel_size; i++, rpnt++) {
175                 reloc_addr = (unsigned long *) (tpnt->loadaddr +
176                         (unsigned long) rpnt->r_offset);
177                 reloc_type = ELF_R_TYPE(rpnt->r_info);
178                 symtab_index = ELF_R_SYM(rpnt->r_info);
179                 symbol_addr = 0;
180
181                 debug_sym(symtab,strtab,symtab_index);
182                 debug_reloc(symtab,strtab,rpnt);
183                 symname = strtab + symtab[symtab_index].st_name;
184 #if defined (__SUPPORT_LD_DEBUG__)
185                 if (reloc_addr)
186                         old_val = *reloc_addr;
187 #endif
188
189                 if (reloc_type == R_MIPS_JUMP_SLOT || reloc_type == R_MIPS_COPY) {
190                         sym_ref.tpnt = NULL;
191                         sym_ref.sym = &symtab[symtab_index];
192                         symbol_addr = (unsigned long)_dl_find_hash(symname,
193                                                                    scope,
194                                                                    tpnt,
195                                                                    elf_machine_type_class(reloc_type), &sym_ref);
196                         if (unlikely(!symbol_addr && ELF_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK))
197                                 return 1;
198                         if (_dl_trace_prelink) {
199                                 _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
200                                                         &sym_ref, elf_machine_type_class(reloc_type));
201                         }
202                 }
203                 if (!symtab_index) {
204                         /* Relocs against STN_UNDEF are usually treated as using a
205                         * symbol value of zero, and using the module containing the
206                         * reloc itself.
207                         */
208                         symbol_addr = symtab[symtab_index].st_value;
209                 }
210
211                 switch (reloc_type) {
212 #if defined USE_TLS && USE_TLS
213 # if _MIPS_SIM == _MIPS_SIM_ABI64
214                 case R_MIPS_TLS_DTPMOD64:
215                 case R_MIPS_TLS_DTPREL64:
216                 case R_MIPS_TLS_TPREL64:
217 # else
218                 case R_MIPS_TLS_DTPMOD32:
219                 case R_MIPS_TLS_DTPREL32:
220                 case R_MIPS_TLS_TPREL32:
221 # endif
222                         {
223                                 struct elf_resolve *tls_tpnt = NULL;
224                                 sym_ref.sym =  &symtab[symtab_index];
225                                 sym_ref.tpnt =  NULL;
226
227                                 if (ELF_ST_BIND(symtab[symtab_index].st_info) != STB_LOCAL) {
228                                         symbol_addr = (unsigned long) _dl_find_hash(symname, scope,
229                                                 tpnt, elf_machine_type_class(reloc_type), &sym_ref);
230                                         tls_tpnt = sym_ref.tpnt;
231                                 }
232                             /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
233                                symbol.  This is the case for a static tls variable, so the lookup
234                                module is just that one is referencing the tls variable. */
235                             if (!tls_tpnt)
236                                 tls_tpnt = tpnt;
237
238                                 switch (reloc_type) {
239                                         case R_MIPS_TLS_DTPMOD64:
240                                         case R_MIPS_TLS_DTPMOD32:
241                                                 if (tls_tpnt)
242                                                         *(ElfW(Word) *)reloc_addr = tls_tpnt->l_tls_modid;
243 #ifdef __SUPPORT_LD_DEBUG__
244                                                 _dl_dprintf(2, "TLS_DTPMOD : %s, %d, %d\n",
245                                                         symname, old_val, *((unsigned int *)reloc_addr));
246 #endif
247                                                 break;
248
249                                         case R_MIPS_TLS_DTPREL64:
250                                         case R_MIPS_TLS_DTPREL32:
251                                                 *(ElfW(Word) *)reloc_addr +=
252                                                         TLS_DTPREL_VALUE (symbol_addr);
253 #ifdef __SUPPORT_LD_DEBUG__
254                                                 _dl_dprintf(2, "TLS_DTPREL : %s, %x, %x\n",
255                                                         symname, old_val, *((unsigned int *)reloc_addr));
256 #endif
257                                                 break;
258
259                                         case R_MIPS_TLS_TPREL32:
260                                         case R_MIPS_TLS_TPREL64:
261                                                 CHECK_STATIC_TLS((struct link_map *)tls_tpnt);
262                                                 *(ElfW(Addr) *)reloc_addr +=
263                                                         TLS_TPREL_VALUE (tls_tpnt, symbol_addr);
264 #ifdef __SUPPORT_LD_DEBUG__
265                                                 _dl_dprintf(2, "TLS_TPREL  : %s, %x, %x\n",
266                                                         symname, old_val, *((unsigned long *)reloc_addr));
267 #endif
268                                                 break;
269                                 }
270
271                                 break;
272                         }
273 #endif /* USE_TLS */
274 #if _MIPS_SIM == _MIPS_SIM_ABI64
275                 case (R_MIPS_64 << 8) | R_MIPS_REL32:
276 #else   /* O32 || N32 */
277                 case R_MIPS_REL32:
278 #endif  /* O32 || N32 */
279                         if (symtab_index) {
280                                 if (symtab_index < tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX])
281                                         *reloc_addr +=
282                                                 symtab[symtab_index].st_value +
283                                                 (unsigned long) tpnt->loadaddr;
284                                 else {
285                                         *reloc_addr += got[symtab_index + tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX] -
286                                                 tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX]];
287                                 }
288                         }
289                         else {
290                                 *reloc_addr += (unsigned long) tpnt->loadaddr;
291                         }
292                         break;
293                 case R_MIPS_JUMP_SLOT:
294                         *reloc_addr = symbol_addr;
295                         break;
296                 case R_MIPS_COPY:
297                         if (symbol_addr) {
298 #if defined (__SUPPORT_LD_DEBUG__)
299                                 if (_dl_debug_move)
300                                         _dl_dprintf(_dl_debug_file,
301                                                     "\n%s move %d bytes from %x to %x",
302                                                     symname, symtab[symtab_index].st_size,
303                                                     symbol_addr, reloc_addr);
304 #endif
305
306                                 _dl_memcpy((char *)reloc_addr,
307                                            (char *)symbol_addr,
308                                            symtab[symtab_index].st_size);
309                         }
310                         break;
311                 case R_MIPS_NONE:
312                         break;
313                 default:
314                         {
315                                 _dl_dprintf(2, "\n%s: ",_dl_progname);
316
317                                 if (symtab_index)
318                                         _dl_dprintf(2, "symbol '%s': ", symname);
319
320 #if defined (__SUPPORT_LD_DEBUG__)
321                                 _dl_dprintf(2, "can't handle reloc type '%s' in lib '%s'\n", _dl_reltypes(reloc_type), tpnt->libname);
322 #else
323                                 _dl_dprintf(2, "can't handle reloc type %x in lib '%s'\n", reloc_type, tpnt->libname);
324 #endif
325                                 _dl_exit(1);
326                         }
327                 }
328         }
329 #if defined (__SUPPORT_LD_DEBUG__)
330         if (_dl_debug_reloc && _dl_debug_detail && reloc_addr)
331                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
332 #endif
333
334         return 0;
335 }
336
337 /* Relocate the global GOT entries for the object */
338 void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy)
339 {
340         ElfW(Sym) *sym;
341         char *strtab;
342         unsigned long i, tmp_lazy;
343         unsigned long *got_entry;
344
345         for (; tpnt ; tpnt = tpnt->next) {
346
347                 /* We don't touch the dynamic linker */
348                 if (tpnt->libtype == program_interpreter)
349                         continue;
350
351                 /* Setup the loop variables */
352                 got_entry = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT])
353                         + tpnt->dynamic_info[DT_MIPS_LOCAL_GOTNO_IDX];
354                 sym = (ElfW(Sym) *) tpnt->dynamic_info[DT_SYMTAB] + tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
355                 strtab = (char *) tpnt->dynamic_info[DT_STRTAB];
356                 i = tpnt->dynamic_info[DT_MIPS_SYMTABNO_IDX] - tpnt->dynamic_info[DT_MIPS_GOTSYM_IDX];
357
358 #if defined (__SUPPORT_LD_DEBUG__)
359                 if (_dl_debug_reloc)
360                         _dl_dprintf(2, "_dl_perform_mips_global_got_relocations for '%s'\n", tpnt->libname);
361 #endif
362                 tmp_lazy = lazy && !tpnt->dynamic_info[DT_BIND_NOW];
363                 /* Relocate the global GOT entries for the object */
364                 while (i--) {
365                         if (sym->st_shndx == SHN_UNDEF) {
366                                 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC && sym->st_value && tmp_lazy) {
367                                         *got_entry = sym->st_value + (unsigned long) tpnt->loadaddr;
368                                 }
369                                 else {
370                                         *got_entry = (unsigned long) _dl_find_hash(strtab +
371                                                 sym->st_name, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
372                                 }
373                         }
374                         else if (sym->st_shndx == SHN_COMMON) {
375                                 *got_entry = (unsigned long) _dl_find_hash(strtab +
376                                         sym->st_name, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
377                         }
378                         else if (ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
379                                 *got_entry != sym->st_value && tmp_lazy) {
380                                 *got_entry += (unsigned long) tpnt->loadaddr;
381                         }
382                         else if (ELF_ST_TYPE(sym->st_info) == STT_SECTION) {
383                                 if (sym->st_other == 0)
384                                         *got_entry += (unsigned long) tpnt->loadaddr;
385                         }
386                         else {
387                                 struct symbol_ref sym_ref;
388                                 sym_ref.sym = sym;
389                                 sym_ref.tpnt = NULL;
390                                 *got_entry = (unsigned long) _dl_find_hash(strtab +
391                                         sym->st_name, &_dl_loaded_modules->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, &sym_ref);
392                         }
393
394                         got_entry++;
395                         sym++;
396                 }
397         }
398 }
399