1 /* vi: set sw=4 ts=4: */
2 /* SuperH ELF shared library loader suppport
4 * Copyright (C) 2002, Stefan Allius <allius@atecom.com> and
5 * Eddie C. Dost <ecd@atecom.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. The name of the above contributors may not be
15 * used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 /* Program to load an ELF binary on a linux system, and run it.
32 References to symbols in sharable libraries can be resolved by either
33 an ELF sharable library or a linux style of shared library. */
35 /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
36 I ever taken any courses on internals. This program was developed using
37 information available through the book "UNIX SYSTEM V RELEASE 4,
38 Programmers guide: Ansi C and Programming Support Tools", which did
39 a more than adequate job of explaining everything required to get this
44 extern int _dl_linux_resolve(void);
46 unsigned long _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
48 ELF_RELOC *this_reloc;
55 unsigned long instr_addr;
58 rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
60 this_reloc = (ELF_RELOC *)(intptr_t)(rel_addr + reloc_entry);
61 symtab_index = ELF32_R_SYM(this_reloc->r_info);
63 symtab = (Elf32_Sym *)(intptr_t) tpnt->dynamic_info[DT_SYMTAB];
64 strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
65 symname = strtab + symtab[symtab_index].st_name;
67 /* Address of jump instruction to fix up */
68 instr_addr = (unsigned long) (this_reloc->r_offset + tpnt->loadaddr);
69 got_addr = (char **) instr_addr;
71 /* Get the address of the GOT entry */
72 new_addr = _dl_find_hash(symname, &_dl_loaded_modules->symbol_scope, tpnt, NULL, ELF_RTYPE_CLASS_PLT, NULL);
74 if (unlikely(!new_addr)) {
75 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n", _dl_progname, symname);
79 #if defined (__SUPPORT_LD_DEBUG__)
80 if ((unsigned long) got_addr < 0x20000000) {
81 if (_dl_debug_bindings) {
82 _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
83 if (_dl_debug_detail) _dl_dprintf(_dl_debug_file,
84 "\n\tpatched %x ==> %x @ %x\n", *got_addr, new_addr, got_addr);
87 if (!_dl_debug_nofixups)
93 return (unsigned long) new_addr;
98 _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
99 unsigned long rel_addr, unsigned long rel_size,
100 int (*reloc_fnc) (struct elf_resolve *tpnt, struct r_scope_elem *scope,
101 ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab))
108 /* Now parse the relocation information */
110 rpnt = (ELF_RELOC *)(intptr_t) rel_addr;
111 rel_size = rel_size / sizeof(ELF_RELOC);
113 symtab = (Elf32_Sym *)(intptr_t)tpnt->dynamic_info[DT_SYMTAB];
114 strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
116 for (i = 0; i < rel_size; i++, rpnt++) {
119 symtab_index = ELF32_R_SYM(rpnt->r_info);
120 debug_sym(symtab,strtab,symtab_index);
121 debug_reloc(symtab,strtab,rpnt);
123 res = reloc_fnc(tpnt, scope, rpnt, symtab, strtab);
125 if (res == 0) continue;
127 _dl_dprintf(2, "\n%s: ",_dl_progname);
130 _dl_dprintf(2, "symbol '%s': ", strtab + symtab[symtab_index].st_name);
132 if (unlikely(res < 0)) {
133 int reloc_type = ELF32_R_TYPE(rpnt->r_info);
134 #if defined (__SUPPORT_LD_DEBUG__)
135 _dl_dprintf(2, "can't handle reloc type %s\n ", _dl_reltypes(reloc_type));
137 _dl_dprintf(2, "can't handle reloc type %x\n", reloc_type);
141 if (unlikely(res > 0)) {
142 _dl_dprintf(2, "can't resolve symbol\n");
151 _dl_do_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
152 ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
157 unsigned long *reloc_addr;
158 unsigned long symbol_addr;
159 #if defined (__SUPPORT_LD_DEBUG__)
160 unsigned long old_val;
163 struct elf_resolve *tls_tpnt = NULL;
164 struct sym_val current_value = { NULL, NULL };
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);
172 symname = strtab + symtab[symtab_index].st_name;
173 if (ELF32_ST_VISIBILITY(symtab[symtab_index].st_other)
175 symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt, ¤t_value,
176 elf_machine_type_class(reloc_type), &tls_tpnt);
178 * We want to allow undefined references to weak symbols - this might
179 * have been intentional. We should not be linking local symbols
180 * here, so all bases should be covered.
184 && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
185 && (ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
186 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
187 _dl_progname, strtab + symtab[symtab_index].st_name);
189 /* Let the caller to handle the error: it may be non fatal if called from dlopen */
193 /* Resolve protected symbols locally */
194 symbol_addr = DL_FIND_HASH_VALUE(tpnt, elf_machine_type_class(reloc_type),
195 &symtab[symtab_index]);
197 if (_dl_trace_prelink)
198 _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
199 ¤t_value, elf_machine_type_class(reloc_type));
202 #if defined (__SUPPORT_LD_DEBUG__)
203 old_val = *reloc_addr;
206 #if defined USE_TLS && USE_TLS
207 /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
208 symbol. This is the case for a static tls variable, so the lookup
209 module is just that one is referencing the tls variable. */
213 switch (reloc_type) {
218 #if defined (__SUPPORT_LD_DEBUG__)
220 _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
221 symname, symtab[symtab_index].st_size,
222 symbol_addr, reloc_addr);
224 _dl_memcpy((char *) reloc_addr, (char *) symbol_addr, symtab[symtab_index].st_size);
226 return 0; /* no further LD_DEBUG messages for copy relocs */
230 *reloc_addr = symbol_addr + rpnt->r_addend;
233 *reloc_addr = symbol_addr + rpnt->r_addend -
234 (unsigned long) reloc_addr;
237 *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
239 #if defined USE_TLS && USE_TLS
240 case R_SH_TLS_DTPMOD32:
241 *reloc_addr = tls_tpnt->l_tls_modid;
243 case R_SH_TLS_DTPOFF32:
244 *reloc_addr = symbol_addr;
246 case R_SH_TLS_TPOFF32:
247 CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
248 *reloc_addr = tls_tpnt->l_tls_offset + symbol_addr + rpnt->r_addend;
255 #if defined (__SUPPORT_LD_DEBUG__)
256 if (_dl_debug_reloc && _dl_debug_detail)
257 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
265 _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct r_scope_elem *scope,
266 ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
269 unsigned long *reloc_addr;
270 #if defined (__SUPPORT_LD_DEBUG__)
271 unsigned long old_val;
277 reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
278 reloc_type = ELF32_R_TYPE(rpnt->r_info);
280 #if defined (__SUPPORT_LD_DEBUG__)
281 old_val = *reloc_addr;
283 switch (reloc_type) {
287 *reloc_addr += (unsigned long) tpnt->loadaddr;
292 #if defined (__SUPPORT_LD_DEBUG__)
293 if (_dl_debug_reloc && _dl_debug_detail)
294 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
300 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
301 unsigned long rel_addr, unsigned long rel_size)
303 (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
306 int _dl_parse_relocation_information(struct dyn_elf *rpnt,
307 struct r_scope_elem *scope, unsigned long rel_addr, unsigned long rel_size)
309 return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);