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);
169 symname = strtab + symtab[symtab_index].st_name;
172 symbol_addr = (unsigned long) _dl_find_hash(symname, scope, tpnt,
173 elf_machine_type_class(reloc_type), &tls_tpnt);
175 * We want to allow undefined references to weak symbols - this might
176 * have been intentional. We should not be linking local symbols
177 * here, so all bases should be covered.
181 && (ELF_ST_TYPE(symtab[symtab_index].st_info) != STT_TLS)
182 && (ELF32_ST_BIND(symtab[symtab_index].st_info) != STB_WEAK)) {
183 _dl_dprintf(2, "%s: can't resolve symbol '%s'\n",
184 _dl_progname, strtab + symtab[symtab_index].st_name);
186 /* Let the caller to handle the error: it may be non fatal if called from dlopen */
191 #if defined (__SUPPORT_LD_DEBUG__)
192 old_val = *reloc_addr;
196 /* In case of a TLS reloc, tls_tpnt NULL means we have an 'anonymous'
197 symbol. This is the case for a static tls variable, so the lookup
198 module is just that one is referencing the tls variable. */
202 switch (reloc_type) {
207 #if defined (__SUPPORT_LD_DEBUG__)
209 _dl_dprintf(_dl_debug_file,"\n%s move %x bytes from %x to %x",
210 symname, symtab[symtab_index].st_size,
211 symbol_addr, reloc_addr);
213 _dl_memcpy((char *) reloc_addr, (char *) symbol_addr, symtab[symtab_index].st_size);
215 return 0; /* no further LD_DEBUG messages for copy relocs */
219 *reloc_addr = symbol_addr + rpnt->r_addend;
222 *reloc_addr = symbol_addr + rpnt->r_addend -
223 (unsigned long) reloc_addr;
226 *reloc_addr = (unsigned long) tpnt->loadaddr + rpnt->r_addend;
229 case R_SH_TLS_DTPMOD32:
230 *reloc_addr = tls_tpnt->l_tls_modid;
232 case R_SH_TLS_DTPOFF32:
233 *reloc_addr = symbol_addr;
235 case R_SH_TLS_TPOFF32:
236 CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
237 *reloc_addr = tls_tpnt->l_tls_offset + symbol_addr + rpnt->r_addend;
244 #if defined (__SUPPORT_LD_DEBUG__)
245 if (_dl_debug_reloc && _dl_debug_detail)
246 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
254 _dl_do_lazy_reloc (struct elf_resolve *tpnt, struct dyn_elf *scope,
255 ELF_RELOC *rpnt, Elf32_Sym *symtab, char *strtab)
258 unsigned long *reloc_addr;
259 #if defined (__SUPPORT_LD_DEBUG__)
260 unsigned long old_val;
266 reloc_addr = (unsigned long *)(intptr_t) (tpnt->loadaddr + (unsigned long) rpnt->r_offset);
267 reloc_type = ELF32_R_TYPE(rpnt->r_info);
269 #if defined (__SUPPORT_LD_DEBUG__)
270 old_val = *reloc_addr;
272 switch (reloc_type) {
276 *reloc_addr += (unsigned long) tpnt->loadaddr;
281 #if defined (__SUPPORT_LD_DEBUG__)
282 if (_dl_debug_reloc && _dl_debug_detail)
283 _dl_dprintf(_dl_debug_file, "\tpatched: %x ==> %x @ %x\n", old_val, *reloc_addr, reloc_addr);
289 void _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
290 unsigned long rel_addr, unsigned long rel_size)
292 (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
295 int _dl_parse_relocation_information(struct dyn_elf *rpnt,
296 unsigned long rel_addr, unsigned long rel_size)
298 return _dl_parse(rpnt->dyn, rpnt->dyn->symbol_scope, rel_addr, rel_size, _dl_do_reloc);