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