OSDN Git Service

support a few more relocation types
[uclinux-h8/uClibc.git] / 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         int reloc_type;
51         ELF_RELOC *this_reloc;
52         char *strtab;
53         ElfW(Sym) *symtab;
54         int symtab_index;
55         char *rel_addr;
56         char *new_addr;
57         char **got_addr;
58         ElfW(Addr) instr_addr;
59         char *symname;
60
61         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
62         this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
63         reloc_type = ELF_R_TYPE(this_reloc->r_info);
64         symtab_index = ELF_R_SYM(this_reloc->r_info);
65
66         symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
67         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
68         symname = strtab + symtab[symtab_index].st_name;
69
70         if (unlikely(reloc_type != R_X86_64_JUMP_SLOT)) {
71                 _dl_dprintf(2, "%s: Incorrect relocation type in jump relocations\n",
72                             _dl_progname);
73                 _dl_exit(1);
74         }
75
76         /* Address of the jump instruction to fix up. */
77         instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
78         got_addr = (char **)instr_addr;
79
80         /* Get the address of the GOT entry. */
81         new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
82         if (unlikely(!new_addr)) {
83                 _dl_dprintf(2, "%s: Can't resolve symbol '%s'\n", _dl_progname, symname);
84                 _dl_exit(1);
85         }
86
87 #if defined (__SUPPORT_LD_DEBUG__)
88         if ((unsigned long)got_addr < 0x40000000) {
89                 if (_dl_debug_bindings) {
90                         _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
91                         if (_dl_debug_detail)
92                                 _dl_dprintf(_dl_debug_file,
93                                             "\tpatched: %x ==> %x @ %x\n",
94                                             *got_addr, new_addr, got_addr);
95                 }
96         }
97         if (!_dl_debug_nofixups)
98 #endif
99                 *got_addr = new_addr;
100
101         return (unsigned long)new_addr;
102 }
103
104 static int
105 _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
106           unsigned long rel_addr, unsigned long rel_size,
107           int (*reloc_fnc)(struct elf_resolve *tpnt, struct dyn_elf *scope,
108                            ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
109 {
110         unsigned int i;
111         char *strtab;
112         ElfW(Sym) *symtab;
113         ELF_RELOC *rpnt;
114         int symtab_index;
115
116         /* Parse the relocation information. */
117         rpnt = (ELF_RELOC *)rel_addr;
118         rel_size /= sizeof(ELF_RELOC);
119
120         symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
121         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
122
123         for (i = 0; i < rel_size; i++, rpnt++) {
124                 int res;
125
126                 symtab_index = ELF_R_SYM(rpnt->r_info);
127
128                 debug_sym(symtab, strtab, symtab_index);
129                 debug_reloc(symtab, strtab, rpnt);
130
131                 res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
132
133                 if (res == 0)
134                         continue;
135
136                 _dl_dprintf(2, "\n%s: ", _dl_progname);
137
138                 if (symtab_index)
139                         _dl_dprintf(2, "symbol '%s': ",
140                                     strtab + symtab[symtab_index].st_name);
141
142                 if (unlikely(res < 0)) {
143                         int reloc_type = ELF_R_TYPE(rpnt->r_info);
144
145                         _dl_dprintf(2, "can't handle reloc type "
146 #if defined (__SUPPORT_LD_DEBUG__)
147                                     "%s\n", _dl_reltypes(reloc_type));
148 #else
149                                     "%x\n", reloc_type);
150 #endif
151                         _dl_exit(-res);
152                 } else if (unlikely(res > 0)) {
153                         _dl_dprintf(2, "can't resolve symbol\n");
154                         return res;
155                 }
156         }
157
158         return 0;
159 }
160
161 static int
162 _dl_do_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
163              ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
164 {
165         int reloc_type;
166         int symtab_index;
167         char *symname;
168         ElfW(Sym) *sym;
169         ElfW(Addr) *reloc_addr;
170         ElfW(Addr) symbol_addr;
171 #if defined (__SUPPORT_LD_DEBUG__)
172         ElfW(Addr) old_val;
173 #endif
174
175         reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
176         reloc_type = ELF_R_TYPE(rpnt->r_info);
177         symtab_index = ELF_R_SYM(rpnt->r_info);
178         sym = &symtab[symtab_index];
179         symbol_addr = 0;
180         symname = strtab + sym->st_name;
181
182         if (symtab_index) {
183                 symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
184                                                             elf_machine_type_class(reloc_type));
185                 /*
186                  * We want to allow undefined references to weak symbols - this
187                  * might have been intentional.  We should not be linking local
188                  * symbols here, so all bases should be covered.
189                  */
190                 if (unlikely(!symbol_addr && ELF_ST_BIND(sym->st_info) != STB_WEAK)) {
191                         _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
192                         _dl_exit(1);
193                 };
194         }
195
196 #if defined (__SUPPORT_LD_DEBUG__)
197         old_val = *reloc_addr;
198 #endif
199
200         switch (reloc_type) {
201                 case R_X86_64_NONE:
202                         break;
203
204                 case R_X86_64_64:
205                         *reloc_addr = symbol_addr + rpnt->r_addend;
206                         break;
207
208                 case R_X86_64_PC32:
209                         *reloc_addr = symbol_addr + rpnt->r_addend - rpnt->r_offset;
210                         break;
211
212                 case R_X86_64_GLOB_DAT:
213                 case R_X86_64_JUMP_SLOT:
214                         *reloc_addr = symbol_addr + rpnt->r_addend;
215                         break;
216
217                 /* handled by elf_machine_relative()
218                 case R_X86_64_RELATIVE:
219                         *reloc_addr = map->l_addr + rpnt->r_addend;
220                         break;
221                 */
222                 case R_X86_64_DTPMOD64:
223                         *reloc_addr = 1;
224                         break;
225                 case R_X86_64_DTPOFF64:
226                         *reloc_addr = sym->st_value + rpnt->r_addend;
227                         break;
228                 case R_X86_64_TPOFF64:
229                         *reloc_addr = sym->st_value + rpnt->r_addend - symbol_addr;
230                         break;
231                 case R_X86_64_32:
232                         *(unsigned int *) reloc_addr = symbol_addr + rpnt->r_addend;
233                         /* XXX: should check for overflow eh ? */
234                         break;
235
236                 case R_X86_64_COPY:
237                         if (symbol_addr) {
238 #if defined (__SUPPORT_LD_DEBUG__)
239                                 if (_dl_debug_move)
240                                         _dl_dprintf(_dl_debug_file,
241                                                     "\t%s move %d bytes from %x to %x\n",
242                                                     symname, sym->st_size,
243                                                     symbol_addr, reloc_addr);
244 #endif
245
246                                 _dl_memcpy((char *)reloc_addr,
247                                            (char *)symbol_addr,
248                                            sym->st_size);
249                         } else
250                                 _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
251                         break;
252
253                 default:
254                         return -1;      /* Calls _dl_exit(1). */
255         }
256
257 #if defined (__SUPPORT_LD_DEBUG__)
258         if (_dl_debug_reloc && _dl_debug_detail)
259                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n",
260                             old_val, *reloc_addr, reloc_addr);
261 #endif
262
263         return 0;
264 }
265
266 static int
267 _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
268                   ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
269 {
270         int reloc_type;
271         int symtab_index;
272         ElfW(Addr) *reloc_addr;
273 #if defined (__SUPPORT_LD_DEBUG__)
274         ElfW(Addr) old_val;
275 #endif
276
277         (void)scope;
278         symtab_index = ELF_R_SYM(rpnt->r_info);
279         (void)strtab;
280
281         reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
282         reloc_type = ELF_R_TYPE(rpnt->r_info);
283
284 #if defined (__SUPPORT_LD_DEBUG__)
285         old_val = *reloc_addr;
286 #endif
287
288         switch (reloc_type) {
289                 case R_X86_64_NONE:
290                         break;
291                 case R_X86_64_JUMP_SLOT:
292                         *reloc_addr += (unsigned long)tpnt->loadaddr;
293                         break;
294                 default:
295                         _dl_exit(1);
296         }
297
298 #if defined (__SUPPORT_LD_DEBUG__)
299         if (_dl_debug_reloc && _dl_debug_detail)
300                 _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
301                             old_val, *reloc_addr, reloc_addr);
302 #endif
303
304         return 0;
305 }
306
307 void
308 _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
309                                       unsigned long rel_addr,
310                                       unsigned long rel_size)
311 {
312         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
313 }
314
315 int
316 _dl_parse_relocation_information(struct dyn_elf *rpnt,
317                                  unsigned long rel_addr,
318                                  unsigned long rel_size)
319 {
320         return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
321 }