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, 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);
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 dyn_elf *scope,
99 unsigned long rel_addr, unsigned long rel_size,
100 int (*reloc_fnc) (struct elf_resolve *tpnt, struct dyn_elf *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 dyn_elf *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;
165 reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
166 reloc_type = ELF32_R_TYPE(rpnt->r_info);
167 symtab_index = ELF32_R_SYM(rpnt->r_info);
171 symname = strtab + symtab[symtab_index].st_name;
172 if (ELF32_ST_VISIBILITY(symtab[symtab_index].st_other)
174 symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt,
175 elf_machine_type_class(reloc_type), &tls_tpnt);
177 * We want to allow undefined references to weak symbols - this might
178 * have been intentional. We should not be linking local symbols
179 * here, so all bases should be covered.
183 && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
184 && (ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
185 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
186 _dl_progname, strtab + symtab[symtab_index].st_name);
188 /* Let the caller to handle the error: it may be non fatal if called from dlopen */
192 /* Resolve protected symbols locally */
193 symbol_addr = DL_FIND_HASH_VALUE(tpnt, elf_machine_type_class(reloc_type),
194 &symtab[symtab_index]);
197 #if defined (__SUPPORT_LD_DEBUG__)
198 old_val = *reloc_addr;
201 #if defined USE_TLS && USE_TLS
202 /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
203 symbol. This is the case for a static tls variable, so the lookup
204 module is just that one is referencing the tls variable. */
208 switch (reloc_type) {
213 #if defined (__SUPPORT_LD_DEBUG__)
215 _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
216 symname, symtab[symtab_index].st_size,
217 symbol_addr, reloc_addr);
219 _dl_memcpy((char *) reloc_addr, (char *) symbol_addr, symtab[symtab_index].st_size);
221 return 0; /* no further LD_DEBUG messages for copy relocs */
225 *reloc_addr = symbol_addr + rpnt->r_addend;
228 *reloc_addr = symbol_addr + rpnt->r_addend -
229 (unsigned long) reloc_addr;
232 *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
234 #if defined USE_TLS && USE_TLS
235 case R_SH_TLS_DTPMOD32:
236 *reloc_addr = tls_tpnt->l_tls_modid;
238 case R_SH_TLS_DTPOFF32:
239 *reloc_addr = symbol_addr;
241 case R_SH_TLS_TPOFF32:
242 CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
243 *reloc_addr = tls_tpnt->l_tls_offset + symbol_addr + rpnt->r_addend;
250 #if defined (__SUPPORT_LD_DEBUG__)
251 if (_dl_debug_reloc && _dl_debug_detail)
252 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
260 _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
261 ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
264 unsigned long *reloc_addr;
265 #if defined (__SUPPORT_LD_DEBUG__)
266 unsigned long old_val;
272 reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
273 reloc_type = ELF32_R_TYPE(rpnt->r_info);
275 #if defined (__SUPPORT_LD_DEBUG__)
276 old_val = *reloc_addr;
278 switch (reloc_type) {
282 *reloc_addr += (unsigned long) tpnt->loadaddr;
287 #if defined (__SUPPORT_LD_DEBUG__)
288 if (_dl_debug_reloc && _dl_debug_detail)
289 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
295 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
296 unsigned long rel_addr, unsigned long rel_size)
298 (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
301 int _dl_parse_relocation_information(struct dyn_elf *rpnt,
302 unsigned long rel_addr, unsigned long rel_size)
304 return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);