1 /* vi: set sw=8 ts=8: */
3 * ldso/ldso/sh64/elfinterp.c
5 * SuperH (sh64) ELF shared library loader suppport
7 * Copyright (C) 2003 Paul Mundt <lethal@linux-sh.org>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
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",
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",
53 [45] = "R_SH_DIR5U", "R_SH_DIR6U",
54 "R_SH_DIR6S", "R_SH_DIR10S",
55 [49] = "R_SH_DIR10SW", "R_SH_DIR10SL",
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",
82 static const char *_dl_reltypes(int type)
88 tabsize = sizeof(_dl_reltypes_tab)/sizeof(_dl_reltypes_tab[0]);
89 str = _dl_reltypes_tab[type];
91 if (type >= tabsize || str == NULL)
92 str =_dl_simple_ltoa(buf, (unsigned long)(type));
97 static void debug_sym(Elf32_Sym *symtab, char *strtab, int symtab_index)
99 if (!_dl_debug_symbols || !symtab_index)
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);
112 static void debug_reloc(Elf32_Sym *symtab, char *strtab, ELF_RELOC *rpnt)
114 if (!_dl_debug_reloc)
117 if (_dl_debug_symbols) {
118 _dl_dprintf(_dl_debug_file, "\n\t");
123 symtab_index = ELF32_R_SYM(rpnt->r_info);
124 sym = symtab_index ? strtab + symtab[symtab_index].st_name
127 _dl_dprintf(_dl_debug_file, "\n%s\n\t", sym);
130 _dl_dprintf(_dl_debug_file, "%s\toffset=%x",
131 _dl_reltypes(ELF32_R_TYPE(rpnt->r_info)),
134 #ifdef ELF_USES_RELOCA
135 _dl_dprintf(_dl_debug_file, "\taddend=%x", rpnt->r_addend);
138 _dl_dprintf(_dl_debug_file, "\n");
141 #endif /* __SUPPORT_LD_DEBUG__ */
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. */
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
154 extern int _dl_linux_resolve(void);
156 unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
159 ELF_RELOC *this_reloc;
166 unsigned long instr_addr;
169 rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
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);
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;
179 if (unlikely(reloc_type != R_SH_JMP_SLOT)) {
180 _dl_dprintf(2, "%s: Incorrect relocation type in jump reloc\n",
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;
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);
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",
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);
212 if (!_dl_debug_nofixups)
213 *got_addr = new_addr;
215 *got_addr = new_addr;
218 return (unsigned long)new_addr;
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,
234 /* Now parse the relocation information */
235 rpnt = (ELF_RELOC *)(intptr_t)rel_addr;
236 rel_size = rel_size / sizeof(ELF_RELOC);
238 symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
239 strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
241 for (i = 0; i < rel_size; i++, rpnt++) {
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);
250 res = reloc_fnc (tpnt, scope, rpnt, symtab, strtab);
254 _dl_dprintf(2, "\n%s: ",_dl_progname);
257 _dl_dprintf(2, "symbol '%s': ",
258 strtab + symtab[symtab_index].st_name);
260 if (unlikely(res < 0)) {
261 int reloc_type = ELF32_R_TYPE(rpnt->r_info);
263 _dl_dprintf(2, "can't handle reloc type "
264 #ifdef __SUPPORT_LD_DEBUG__
265 "%s\n", _dl_reltypes(reloc_type)
273 if (unlikely(res > 0)) {
274 _dl_dprintf(2, "can't resolve symbol\n");
283 static int _dl_do_reloc(struct elf_resolve *tpnt,struct dyn_elf *scope,
284 ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
287 int symtab_index, lsb;
289 unsigned long *reloc_addr;
290 unsigned long symbol_addr;
291 #ifdef __SUPPORT_LD_DEBUG__
292 unsigned long old_val;
295 reloc_type = ELF32_R_TYPE(rpnt->r_info);
296 symtab_index = ELF32_R_SYM(rpnt->r_info);
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);
306 symbol_addr = (unsigned long)_dl_find_hash(symname, scope, tpnt,
307 elf_machine_type_class(reloc_type));
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.
314 stb = ELF32_ST_BIND(symtab[symtab_index].st_info);
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);
323 #ifdef __SUPPORT_LD_DEBUG__
324 old_val = *reloc_addr;
327 switch (reloc_type) {
331 _dl_memcpy((char *)reloc_addr,
332 (char *)symbol_addr, symtab[symtab_index].st_size);
337 *reloc_addr = (symbol_addr + rpnt->r_addend) | lsb;
340 *reloc_addr = symbol_addr + rpnt->r_addend -
341 (unsigned long)reloc_addr;
344 *reloc_addr = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
346 case R_SH_RELATIVE_LOW16:
347 case R_SH_RELATIVE_MEDLOW16:
349 unsigned long word, value;
351 word = (unsigned long)reloc_addr & ~0x3fffc00;
352 value = (unsigned long)tpnt->loadaddr + rpnt->r_addend;
354 if (reloc_type == R_SH_RELATIVE_MEDLOW16)
357 word |= (value & 0xffff) << 10;
363 case R_SH_IMM_MEDLOW16:
365 unsigned long word, value;
367 word = (unsigned long)reloc_addr & ~0x3fffc00;
368 value = (symbol_addr + rpnt->r_addend) | lsb;
370 if (reloc_type == R_SH_IMM_MEDLOW16)
373 word |= (value & 0xffff) << 10;
378 case R_SH_IMM_LOW16_PCREL:
379 case R_SH_IMM_MEDLOW16_PCREL:
381 unsigned long word, value;
383 word = (unsigned long)reloc_addr & ~0x3fffc00;
384 value = symbol_addr + rpnt->r_addend -
385 (unsigned long)reloc_addr;
387 if (reloc_type == R_SH_IMM_MEDLOW16_PCREL)
390 word |= (value & 0xffff) << 10;
396 return -1; /*call _dl_exit(1) */
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);
408 static int _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct dyn_elf *scope,
409 ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
411 int reloc_type, symtab_index, lsb;
412 unsigned long *reloc_addr;
413 #ifdef __SUPPORT_LD_DEBUG__
414 unsigned long old_val;
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);
423 #ifdef __SUPPORT_LD_DEBUG__
424 old_val = *reloc_addr;
427 switch (reloc_type) {
431 *reloc_addr += (unsigned long)tpnt->loadaddr | lsb;
434 return -1; /*call _dl_exit(1) */
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);
446 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
447 unsigned long rel_addr, unsigned long rel_size)
449 (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
452 int _dl_parse_relocation_information(struct dyn_elf *rpnt,
453 unsigned long rel_addr, unsigned long rel_size)
455 return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);