OSDN Git Service

Moved the addition of load address from the fast path
[uclinux-h8/uClibc.git] / ldso / ldso / sh64 / elfinterp.c
1 /* vi: set sw=8 ts=8: */
2 /*
3  * ldso/ldso/sh64/elfinterp.c
4  *
5  * SuperH (sh64) ELF shared library loader suppport
6  *
7  * Copyright (C) 2003  Paul Mundt <lethal@linux-sh.org>
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. The name of the above contributors may not be
17  *    used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #ifdef __SUPPORT_LD_DEBUG__
34 static const char *_dl_reltypes_tab[] = {
35         /* SHcompact relocs */
36           [0] = "R_SH_NONE",            "R_SH_DIR32",
37                 "R_SH_REL32",           "R_SH_DIR8WPN",
38           [4] = "R_SH_IND12W",          "R_SH_DIR8WPL",
39                 "R_SH_DIR8WPZ",         "R_SH_DIR8BP",
40           [8] = "R_SH_DIR8W",           "R_SH_DIR8L",
41          [25] = "R_SH_SWITCH16",        "R_SH_SWITCH32",
42                 "R_SH_USES",            "R_SH_COUNT",
43          [29] = "R_SH_ALIGN",           "R_SH_CODE",
44                 "R_SH_DATA",            "R_SH_LABEL",
45          [33] = "R_SH_SWITCH8",         "R_SH_GNU_VTINHERIT",
46                 "R_SH_GNU_VTENTRY",
47         [160] = "R_SH_GOT32",           "R_SH_PLT32",
48                 "R_SH_COPY",            "R_SH_GLOB_DAT",
49         [164] = "R_SH_JMP_SLOT",        "R_SH_RELATIVE",
50                 "R_SH_GOTOFF",          "R_SH_GOTPC",
51
52         /* SHmedia relocs */
53          [45] = "R_SH_DIR5U",           "R_SH_DIR6U",
54                 "R_SH_DIR6S",           "R_SH_DIR10S",
55          [49] = "R_SH_DIR10SW",         "R_SH_DIR10SL",
56                 "R_SH_DIR10SQ",
57         [169] = "R_SH_GOT_LOW16",       "R_SH_GOT_MEDLOW16",
58                 "R_SH_GOT_MEDHI16",     "R_SH_GOT_HI16",
59         [173] = "R_SH_GOTPLT_LOW16",    "R_SH_GOTPLT_MEDLOW16",
60                 "R_SH_GOTPLT_MEDHI16",  "R_SH_GOTPLT_HI16",
61         [177] = "R_SH_PLT_LOW16",       "R_SH_PLT_MEDLOW16",
62                 "R_SH_PLT_MEDHI16",     "R_SH_PLT_HI16",
63         [181] = "R_SH_GOTOFF_LOW16",    "R_SH_GOTOFF_MEDLOW16",
64                 "R_SH_GOTOFF_MEDHI16",  "R_SH_GOTOFF_HI16",
65         [185] = "R_SH_GOTPC_LOW16",     "R_SH_GOTPC_MEDLOW16",
66                 "R_SH_GOTPC_MEDHI16",   "R_SH_GOTPC_HI16",
67         [189] = "R_SH_GOT10BY4",        "R_SH_GOTPLT10BY4",
68                 "R_SH_GOT10BY8",        "R_SH_GOTPLT10BY8",
69         [193] = "R_SH_COPY64",          "R_SH_GLOB_DAT64",
70                 "R_SH_JMP_SLOT64",      "R_SH_RELATIVE64",
71         [197] = "R_SH_RELATIVE_LOW16",  "R_SH_RELATIVE_MEDLOW16",
72                 "R_SH_RELATIVE_MEDHI16","R_SH_RELATIVE_HI16",
73         [242] = "R_SH_SHMEDIA_CODE",    "R_SH_PT_16",
74                 "R_SH_IMMS16",          "R_SH_IMMU16",
75         [246] = "R_SH_IMM_LOW16",       "R_SH_IMM_LOW16_PCREL",
76                 "R_SH_IMM_MEDLOW16",    "R_SH_IMM_MEDLOW16_PCREL",
77         [250] = "R_SH_IMM_MEDHI16",     "R_SH_IMM_MEDHI16_PCREL",
78                 "R_SH_IMM_HI16",        "R_SH_IMM_HI16_PCREL",
79         [254] = "R_SH_64",              "R_SH_64_PCREL",
80 };
81
82 static const char *_dl_reltypes(int type)
83 {
84         static char buf[22];
85         const char *str;
86         int tabsize;
87
88         tabsize = sizeof(_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0]);
89         str     = _dl_reltypes_tab[type];
90
91         if (type >= tabsize || str == NULL)
92                 str =_dl_simple_ltoa(buf, (unsigned long)(type));
93
94         return str;
95 }
96
97 static void debug_sym(Elf32_Sym *symtab, char *strtab, int symtab_index)
98 {
99         if (!_dl_debug_symbols || !symtab_index)
100                 return;
101
102         _dl_dprintf(_dl_debug_file,
103                 "\n%s\tvalue=%x\tsize=%x\tinfo=%x\tother=%x\tshndx=%x",
104                 strtab + symtab[symtab_index].st_name,
105                 symtab[symtab_index].st_value,
106                 symtab[symtab_index].st_size,
107                 symtab[symtab_index].st_info,
108                 symtab[symtab_index].st_other,
109                 symtab[symtab_index].st_shndx);
110 }
111
112 static void debug_reloc(Elf32_Sym *symtab, char *strtab, ELF_RELOC *rpnt)
113 {
114         if (!_dl_debug_reloc)
115                 return;
116
117         if (_dl_debug_symbols) {
118                 _dl_dprintf(_dl_debug_file, "\n\t");
119         } else {
120                 int symtab_index;
121                 const char *sym;
122
123                 symtab_index = ELF32_R_SYM(rpnt->r_info);
124                 sym = symtab_index ? strtab + symtab[symtab_index].st_name
125                                    : "sym=0x0";
126
127                 _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
128         }
129
130         _dl_dprintf(_dl_debug_file, "%s\toffset=%x",
131                     _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
132                     rpnt->r_offset);
133
134 #ifdef ELF_USES_RELOCA
135         _dl_dprintf(_dl_debug_file, "\taddend=%x", rpnt->r_addend);
136 #endif
137
138         _dl_dprintf(_dl_debug_file, "\n");
139
140 }
141 #endif /* __SUPPORT_LD_DEBUG__ */
142
143 /* Program to load an ELF binary on a linux system, and run it.
144    References to symbols in sharable libraries can be resolved by either
145    an ELF sharable library or a linux style of shared library. */
146
147 /* Disclaimer:  I have never seen any AT&T source code for SVr4, nor have
148    I ever taken any courses on internals.  This program was developed using
149    information available through the book "UNIX SYSTEM V RELEASE 4,
150    Programmers guide: Ansi C and Programming Support Tools", which did
151    a more than adequate job of explaining everything required to get this
152    working. */
153
154 extern int _dl_linux_resolve(void);
155
156 unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
157 {
158         int reloc_type;
159         ELF_RELOC *this_reloc;
160         char *strtab;
161         Elf32_Sym *symtab;
162         int symtab_index;
163         char *rel_addr;
164         char *new_addr;
165         char **got_addr;
166         unsigned long instr_addr;
167         char *symname;
168
169         rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
170
171         this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
172         reloc_type = ELF32_R_TYPE(this_reloc->r_info);
173         symtab_index = ELF32_R_SYM(this_reloc->r_info);
174
175         symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
176         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
177         symname = strtab + symtab[symtab_index].st_name;
178
179         if (unlikely(reloc_type != R_SH_JMP_SLOT)) {
180                 _dl_dprintf(2, "%s: Incorrect relocation type in jump reloc\n",
181                             _dl_progname);
182                 _dl_exit(1);
183         }
184
185         /* Address of jump instruction to fix up */
186         instr_addr = ((unsigned long)this_reloc->r_offset +
187                         (unsigned long)tpnt->loadaddr);
188         got_addr = (char **)instr_addr;
189
190
191         /* Get the address of the GOT entry */
192         new_addr = _dl_find_hash(symname, tpnt->symbol_scope, tpnt, ELF_RTYPE_CLASS_PLT);
193         if (unlikely(!new_addr)) {
194                 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
195                             _dl_progname, symname);
196                 _dl_exit(1);
197         }
198
199 #ifdef __SUPPORT_LD_DEBUG__
200         if ((unsigned long)got_addr < 0x20000000) {
201                 if (_dl_debug_bindings) {
202                         _dl_dprintf(_dl_debug_file, "\nresolve function: %s",
203                                     symname);
204
205                         if (_dl_debug_detail)
206                                 _dl_dprintf(_dl_debug_file,
207                                             "\n\tpatched %x ==> %x @ %x\n",
208                                             *got_addr, new_addr, got_addr);
209                 }
210         }
211
212         if (!_dl_debug_nofixups)
213                 *got_addr = new_addr;
214 #else
215         *got_addr = new_addr;
216 #endif
217
218         return (unsigned long)new_addr;
219 }
220
221 static int _dl_parse(struct elf_resolve *tpnt, struct dyn_elf *scope,
222                      unsigned long rel_addr, unsigned long rel_size,
223                      int (*reloc_fnc)(struct elf_resolve *tpnt,
224                                       struct dyn_elf *scope,
225                                       ELF_RELOC *rpnt, Elf32_Sym *symtab,
226                                       char *strtab))
227 {
228         unsigned int i;
229         char *strtab;
230         Elf32_Sym *symtab;
231         ELF_RELOC *rpnt;
232         int symtab_index;
233
234         /* Now parse the relocation information */
235         rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
236         rel_size = rel_size / sizeof(ELF_RELOC);
237
238         symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
239         strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
240
241         for (i = 0; i < rel_size; i++, rpnt++) {
242                 int res;
243
244                 symtab_index = ELF32_R_SYM(rpnt->r_info);
245 #ifdef __SUPPORT_LD_DEBUG__
246                 debug_sym(symtab,strtab,symtab_index);
247                 debug_reloc(symtab,strtab,rpnt);
248 #endif
249
250                 res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
251                 if (res == 0)
252                         continue;
253
254                 _dl_dprintf(2, "\n%s: ",_dl_progname);
255
256                 if (symtab_index)
257                         _dl_dprintf(2, "symbol '%s': ",
258                                 strtab + symtab[symtab_index].st_name);
259
260                 if (unlikely(res < 0)) {
261                         int reloc_type = ELF32_R_TYPE(rpnt->r_info);
262
263                         _dl_dprintf(2, "can't handle reloc type "
264 #ifdef __SUPPORT_LD_DEBUG__
265                                         "%s\n", _dl_reltypes(reloc_type)
266 #else
267                                         "%x\n", reloc_type
268 #endif
269                         );
270
271                         _dl_exit(-res);
272                 }
273                 if (unlikely(res > 0)) {
274                         _dl_dprintf(2, "can't resolve symbol\n");
275
276                         return res;
277                 }
278         }
279
280         return 0;
281 }
282
283 static int _dl_do_reloc(struct elf_resolve *tpnt,struct dyn_elf *scope,
284                         ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
285 {
286         int reloc_type;
287         int symtab_index, lsb;
288         char *symname;
289         unsigned long *reloc_addr;
290         unsigned long symbol_addr;
291 #ifdef __SUPPORT_LD_DEBUG__
292         unsigned long old_val;
293 #endif
294
295         reloc_type   = ELF32_R_TYPE(rpnt->r_info);
296         symtab_index = ELF32_R_SYM(rpnt->r_info);
297         symbol_addr  = 0;
298         lsb          = symtab[symtab_index].st_other & 4;
299         symname      = strtab + symtab[symtab_index].st_name;
300         reloc_addr   = (unsigned long *)(intptr_t)
301                 (tpnt->loadaddr + (unsigned long)rpnt->r_offset);
302
303         if (symtab_index) {
304                 int stb;
305
306                 symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
307                                                            elf_machine_type_class(reloc_type));
308
309                 /*
310                  * We want to allow undefined references to weak symbols - this
311                  * might have been intentional. We should not be linking local
312                  * symbols here, so all bases should be covered.
313                  */
314                 stb = ELF32_ST_BIND(symtab[symtab_index].st_info);
315
316                 if (stb != STB_WEAK && !symbol_addr) {
317                         _dl_dprintf (2, "%s: can't resolve symbol '%s'\n",
318                                      _dl_progname, strtab + symtab[symtab_index].st_name);
319                         _dl_exit (1);
320                 }
321         }
322
323 #ifdef __SUPPORT_LD_DEBUG__
324         old_val = *reloc_addr;
325 #endif
326
327         switch (reloc_type) {
328         case R_SH_NONE:
329                 break;
330         case R_SH_COPY:
331                 _dl_memcpy((char *)reloc_addr,
332                            (char *)symbol_addr, symtab[symtab_index].st_size);
333                 break;
334         case R_SH_DIR32:
335         case R_SH_GLOB_DAT:
336         case R_SH_JMP_SLOT:
337                 *reloc_addr = (symbol_addr + rpnt->r_addend) | lsb;
338                 break;
339         case R_SH_REL32:
340                 *reloc_addr = symbol_addr + rpnt->r_addend -
341                         (unsigned long)reloc_addr;
342                 break;
343         case R_SH_RELATIVE:
344                 *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
345                 break;
346         case R_SH_RELATIVE_LOW16:
347         case R_SH_RELATIVE_MEDLOW16:
348             {
349                 unsigned long word, value;
350
351                 word = (unsigned long)reloc_addr & ~0x3fffc00;
352                 value = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
353
354                 if (reloc_type == R_SH_RELATIVE_MEDLOW16)
355                         value >>= 16;
356
357                 word |= (value & 0xffff) << 10;
358                 *reloc_addr = word;
359
360                 break;
361             }
362         case R_SH_IMM_LOW16:
363         case R_SH_IMM_MEDLOW16:
364             {
365                 unsigned long word, value;
366
367                 word = (unsigned long)reloc_addr & ~0x3fffc00;
368                 value = (symbol_addr + rpnt->r_addend) | lsb;
369
370                 if (reloc_type == R_SH_IMM_MEDLOW16)
371                         value >>= 16;
372
373                 word |= (value & 0xffff) << 10;
374                 *reloc_addr = word;
375
376                 break;
377             }
378         case R_SH_IMM_LOW16_PCREL:
379         case R_SH_IMM_MEDLOW16_PCREL:
380             {
381                 unsigned long word, value;
382
383                 word = (unsigned long)reloc_addr & ~0x3fffc00;
384                 value = symbol_addr + rpnt->r_addend -
385                         (unsigned long)reloc_addr;
386
387                 if (reloc_type == R_SH_IMM_MEDLOW16_PCREL)
388                         value >>= 16;
389
390                 word |= (value & 0xffff) << 10;
391                 *reloc_addr = word;
392
393                 break;
394             }
395         default:
396                 return -1; /*call _dl_exit(1) */
397         }
398
399 #ifdef __SUPPORT_LD_DEBUG__
400         if (_dl_debug_reloc && _dl_debug_detail)
401                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x",
402                             old_val, *reloc_addr, reloc_addr);
403 #endif
404
405         return 0;
406 }
407
408 static int _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
409                              ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
410 {
411         int reloc_type, symtab_index, lsb;
412         unsigned long *reloc_addr;
413 #ifdef __SUPPORT_LD_DEBUG__
414         unsigned long old_val;
415 #endif
416
417         reloc_type   = ELF32_R_TYPE(rpnt->r_info);
418         symtab_index = ELF32_R_SYM(rpnt->r_info);
419         lsb          = symtab[symtab_index].st_other & 4;
420         reloc_addr   = (unsigned long *)(intptr_t)
421                 (tpnt->loadaddr + (unsigned long)rpnt->r_offset);
422
423 #ifdef __SUPPORT_LD_DEBUG__
424         old_val = *reloc_addr;
425 #endif
426
427         switch (reloc_type) {
428         case R_SH_NONE:
429                 break;
430         case R_SH_JMP_SLOT:
431                 *reloc_addr += (unsigned long)tpnt->loadaddr | lsb;
432                 break;
433         default:
434                 return -1; /*call _dl_exit(1) */
435         }
436
437 #ifdef __SUPPORT_LD_DEBUG__
438         if (_dl_debug_reloc && _dl_debug_detail)
439                 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x",
440                             old_val, *reloc_addr, reloc_addr);
441 #endif
442
443         return 0;
444 }
445
446 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
447         unsigned long rel_addr, unsigned long rel_size)
448 {
449         (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
450 }
451
452 int _dl_parse_relocation_information(struct dyn_elf *rpnt,
453         unsigned long rel_addr, unsigned long rel_size)
454 {
455         return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);
456 }