]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/ldso/ldso/cris/elfinterp.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / 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         ElfW(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 = ELF_R_SYM(this_reloc->r_info);
58
59         symtab = (ElfW(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, ElfW(Sym) *symtab, char *strtab))
98 {
99         int symtab_index;
100         unsigned int i;
101         char *strtab;
102         ElfW(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 = (ElfW(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 = ELF_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 = ELF_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, ElfW(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 = ELF_R_TYPE(rpnt->r_info);
168         symtab_index = ELF_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                     ELF_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 && ELF_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
195 #if defined (__SUPPORT_LD_DEBUG__)
196         old_val = *reloc_addr;
197 #endif
198
199         switch (reloc_type) {
200                 case R_CRIS_NONE:
201                         break;
202                 case R_CRIS_GLOB_DAT:
203                 case R_CRIS_JUMP_SLOT:
204                 case R_CRIS_32:
205                         *reloc_addr = symbol_addr;
206                         break;
207                 case R_CRIS_COPY:
208 #if defined (__SUPPORT_LD_DEBUG__)
209                         if (_dl_debug_move)
210                                 _dl_dprintf(_dl_debug_file,
211                                             "\n%s move %d bytes from %x to %x",
212                                             symname, symtab[symtab_index].st_size,
213                                             symbol_addr, reloc_addr);
214 #endif
215
216                         _dl_memcpy((char *)reloc_addr,
217                                    (char *)symbol_addr,
218                                    symtab[symtab_index].st_size);
219                         break;
220                 case R_CRIS_RELATIVE:
221                         *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
222                         break;
223                 default:
224                         return -1;      /* Calls _dl_exit(1). */
225         }
226
227 #if defined (__SUPPORT_LD_DEBUG__)
228         if (_dl_debug_reloc && _dl_debug_detail)
229                 _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
230                             old_val, *reloc_addr, reloc_addr);
231 #endif
232
233         return 0;
234 }
235
236 static int
237 _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
238                   ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
239 {
240         int reloc_type;
241         unsigned long *reloc_addr;
242 #if defined (__SUPPORT_LD_DEBUG__)
243         unsigned long old_val;
244 #endif
245
246         /* Don't care about these, just keep the compiler happy. */
247         (void)scope;
248         (void)symtab;
249         (void)strtab;
250
251         reloc_addr = (unsigned long *)(intptr_t)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
252         reloc_type = ELF_R_TYPE(rpnt->r_info);
253
254 #if defined (__SUPPORT_LD_DEBUG__)
255         old_val = *reloc_addr;
256 #endif
257
258         switch (reloc_type) {
259                 case R_CRIS_NONE:
260                         break;
261                 case R_CRIS_JUMP_SLOT:
262                         *reloc_addr += (unsigned long)tpnt->loadaddr;
263                         break;
264                 default:
265                         return -1;      /* Calls _dl_exit(1). */
266         }
267
268 #if defined (__SUPPORT_LD_DEBUG__)
269         if (_dl_debug_reloc && _dl_debug_detail)
270                 _dl_dprintf(_dl_debug_file, "\n\tpatched: %x ==> %x @ %x\n",
271                             old_val, *reloc_addr, reloc_addr);
272 #endif
273
274         return 0;
275 }
276
277 /* External interface to the generic part of the dynamic linker. */
278
279 void
280 _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
281                                       unsigned long rel_addr,
282                                       unsigned long rel_size)
283 {
284         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
285 }
286
287 int
288 _dl_parse_relocation_information(struct dyn_elf *rpnt,
289                                  struct r_scope_elem *scope,
290                                  unsigned long rel_addr,
291                                  unsigned long rel_size)
292 {
293         return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);
294 }