]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/ldso/ldso/x86_64/elfinterp.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / ldso / ldso / x86_64 / elfinterp.c
1 /* vi: set sw=4 ts=4: */
2 /* x86_64 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  * Copyright (C) 2001-2004 Erik Andersen
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the above contributors may not be
16  *    used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include "ldso.h"
33
34 /* Program to load an ELF binary on a linux system, and run it.
35    References to symbols in sharable libraries can be resolved by either
36    an ELF sharable library or a linux style of shared library. */
37
38 /* Disclaimer:  I have never seen any AT&T source code for SVr4, nor have
39    I ever taken any courses on internals.  This program was developed using
40    information available through the book "UNIX SYSTEM V RELEASE 4,
41    Programmers guide: Ansi C and Programming Support Tools", which did
42    a more than adequate job of explaining everything required to get this
43    working. */
44
45 extern int _dl_linux_resolve(void);
46
47 unsigned long
48 _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
49 {
50         ELF_RELOC *this_reloc;
51         char *strtab;
52         ElfW(Sym) *symtab;
53         int symtab_index;
54         char *rel_addr;
55         char *new_addr;
56         char **got_addr;
57         ElfW(Addr) instr_addr;
58         char *symname;
59
60         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
61         this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
62         symtab_index = ELF_R_SYM(this_reloc->r_info);
63
64         symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
65         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
66         symname = strtab + symtab[symtab_index].st_name;
67
68         /* Address of the jump instruction to fix up. */
69         instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
70         got_addr = (char **)instr_addr;
71
72         /* Get the address of the GOT entry. */
73         new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT, NULL);
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 < 0x40000000) {
81                 if (_dl_debug_bindings) {
82                         _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
83                         if (_dl_debug_detail)
84                                 _dl_dprintf(_dl_debug_file,
85                                             "\tpatched: %x ==> %x @ %x\n",
86                                             *got_addr, new_addr, got_addr);
87                 }
88         }
89         if (!_dl_debug_nofixups)
90 #endif
91                 *got_addr = new_addr;
92
93         return (unsigned long)new_addr;
94 }
95
96 static int
97 _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
98           unsigned long rel_addr, unsigned long rel_size,
99           int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
100                            ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
101 {
102         unsigned int i;
103         char *strtab;
104         ElfW(Sym) *symtab;
105         ELF_RELOC *rpnt;
106         int symtab_index;
107
108         /* Parse the relocation information. */
109         rpnt = (ELF_RELOC *)rel_addr;
110         rel_size /= sizeof(ELF_RELOC);
111
112         symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
113         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
114
115         for (i = 0; i < rel_size; i++, rpnt++) {
116                 int res;
117
118                 symtab_index = ELF_R_SYM(rpnt->r_info);
119
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)
126                         continue;
127
128                 _dl_dprintf(2, "\n%s: ", _dl_progname);
129
130                 if (symtab_index)
131                         _dl_dprintf(2, "symbol '%s': ",
132                                     strtab + symtab[symtab_index].st_name);
133
134                 if (unlikely(res < 0)) {
135                         int reloc_type = ELF_R_TYPE(rpnt->r_info);
136
137                         _dl_dprintf(2, "can't handle reloc type "
138 #if defined (__SUPPORT_LD_DEBUG__)
139                                     "%s\n", _dl_reltypes(reloc_type));
140 #else
141                                     "%x\n", reloc_type);
142 #endif
143                         _dl_exit(-res);
144                 } else if (unlikely(res > 0)) {
145                         _dl_dprintf(2, "can't resolve symbol\n");
146                         return res;
147                 }
148         }
149
150         return 0;
151 }
152
153 static int
154 _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
155              ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
156 {
157         int reloc_type;
158         int symtab_index;
159         char *symname;
160         struct elf_resolve *tls_tpnt = NULL;
161         struct symbol_ref sym_ref;
162         ElfW(Addr) *reloc_addr;
163         ElfW(Addr) symbol_addr;
164 #if defined (__SUPPORT_LD_DEBUG__)
165         ElfW(Addr) old_val;
166 #endif
167
168         reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
169         reloc_type = ELF_R_TYPE(rpnt->r_info);
170         symtab_index = ELF_R_SYM(rpnt->r_info);
171         sym_ref.sym = &symtab[symtab_index];
172         sym_ref.tpnt = NULL;
173         symbol_addr = 0;
174         symname = strtab + sym_ref.sym->st_name;
175
176         if (symtab_index) {
177                 symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
178                                 elf_machine_type_class(reloc_type), &sym_ref);
179                 /*
180                  * We want to allow undefined references to weak symbols - this
181                  * might have been intentional.  We should not be linking local
182                  * symbols here, so all bases should be covered.
183                  */
184                 if (unlikely(!symbol_addr && (ELF_ST_TYPE(sym_ref.sym->st_info) != STT_TLS)
185                                         && (ELF_ST_BIND(sym_ref.sym->st_info) != STB_WEAK))) {
186                         /* This may be non-fatal if called from dlopen. */
187                         return 1;
188                 }
189                 tls_tpnt = sym_ref.tpnt;
190         } else {
191                 /* Relocs against STN_UNDEF are usually treated as using a
192                  * symbol value of zero, and using the module containing the
193                  * reloc itself. */
194                 symbol_addr = sym_ref.sym->st_value;
195                 tls_tpnt = tpnt;
196         }
197
198
199 #if defined (__SUPPORT_LD_DEBUG__)
200         old_val = *reloc_addr;
201 #endif
202
203         switch (reloc_type) {
204                 case R_X86_64_NONE:
205                         break;
206
207                 case R_X86_64_64:
208                         *reloc_addr = symbol_addr + rpnt->r_addend;
209                         break;
210
211                 case R_X86_64_PC32:
212                         *reloc_addr = symbol_addr + rpnt->r_addend - rpnt->r_offset;
213                         break;
214
215                 case R_X86_64_GLOB_DAT:
216                 case R_X86_64_JUMP_SLOT:
217                         *reloc_addr = symbol_addr + rpnt->r_addend;
218                         break;
219
220                 /* handled by elf_machine_relative()
221                 case R_X86_64_RELATIVE:
222                         *reloc_addr = map->l_addr + rpnt->r_addend;
223                         break;
224                 */
225 #if defined USE_TLS && USE_TLS
226                 case R_X86_64_DTPMOD64:
227                         *reloc_addr = tls_tpnt->l_tls_modid;
228                         break;
229                 case R_X86_64_DTPOFF64:
230                         /* During relocation all TLS symbols are defined and used.
231                          * Therefore the offset is already correct.  */
232                         *reloc_addr = symbol_addr + rpnt->r_addend;
233                         break;
234                 case R_X86_64_TPOFF64:
235                         /* The offset is negative, forward from the thread pointer.
236                          * We know the offset of the object the symbol is contained in.
237                          * It is a negative value which will be added to the
238                          * thread pointer.  */
239                         CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
240                         *reloc_addr = symbol_addr - tls_tpnt->l_tls_offset + rpnt->r_addend;
241                         break;
242 #endif
243                 case R_X86_64_32:
244                         *(unsigned int *) reloc_addr = symbol_addr + rpnt->r_addend;
245                         /* XXX: should check for overflow eh ? */
246                         break;
247 #ifndef __DONE_BY_AW11__
248                 case R_X86_64_RELATIVE:
249                         *reloc_addr += (unsigned long)tpnt->loadaddr;
250                         /* XXX: should check for overflow eh ? */
251                         break;
252 #endif
253
254                 case R_X86_64_COPY:
255                         if (symbol_addr) {
256 #if defined (__SUPPORT_LD_DEBUG__)
257                                 if (_dl_debug_move)
258                                         _dl_dprintf(_dl_debug_file,
259                                                     "\t%s move %d bytes from %x to %x\n",
260                                                     symname, sym_ref.sym->st_size,
261                                                     symbol_addr, reloc_addr);
262 #endif
263
264                                 _dl_memcpy((char *)reloc_addr,
265                                            (char *)symbol_addr,
266                                            sym_ref.sym->st_size);
267                         } else
268                                 _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
269                         break;
270
271                 default:
272                         return -1;      /* Calls _dl_exit(1). */
273         }
274
275 #if defined (__SUPPORT_LD_DEBUG__)
276         if (_dl_debug_reloc && _dl_debug_detail)
277                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
278                             old_val, *reloc_addr, reloc_addr);
279 #endif
280
281         return 0;
282 }
283
284 static int
285 _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
286                   ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
287 {
288         int reloc_type;
289         int symtab_index;
290         ElfW(Addr) *reloc_addr;
291 #if defined (__SUPPORT_LD_DEBUG__)
292         ElfW(Addr) old_val;
293 #endif
294
295         (void)scope;
296         symtab_index = ELF_R_SYM(rpnt->r_info);
297         (void)strtab;
298
299         reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
300         reloc_type = ELF_R_TYPE(rpnt->r_info);
301
302 #if defined (__SUPPORT_LD_DEBUG__)
303         old_val = *reloc_addr;
304 #endif
305
306         switch (reloc_type) {
307                 case R_X86_64_NONE:
308                         break;
309                 case R_X86_64_JUMP_SLOT:
310                         *reloc_addr += (unsigned long)tpnt->loadaddr;
311                         break;
312                 default:
313                         _dl_exit(1);
314         }
315
316 #if defined (__SUPPORT_LD_DEBUG__)
317         if (_dl_debug_reloc && _dl_debug_detail)
318                 _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
319                             old_val, *reloc_addr, reloc_addr);
320 #endif
321
322         return 0;
323 }
324
325 void
326 _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
327                                       unsigned long rel_addr,
328                                       unsigned long rel_size)
329 {
330         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
331 }
332
333 int
334 _dl_parse_relocation_information(struct dyn_elf *rpnt,
335                                  unsigned long rel_addr,
336                                  unsigned long rel_size)
337 {
338         return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
339 }