1 /* vi: set sw=4 ts=4: */
2 /* x86_64 ELF shared library loader suppport
4 * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
5 * David Engel, Hongjiu Lu and Mitch D'Souza
6 * Copyright (C) 2001-2004 Erik Andersen
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. The name of the above contributors may not be
16 * used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* Program to load an ELF binary on a linux system, and run it.
35 References to symbols in sharable libraries can be resolved by either
36 an ELF sharable library or a linux style of shared library. */
38 /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
39 I ever taken any courses on internals. This program was developed using
40 information available through the book "UNIX SYSTEM V RELEASE 4,
41 Programmers guide: Ansi C and Programming Support Tools", which did
42 a more than adequate job of explaining everything required to get this
45 extern int _dl_linux_resolve(void);
48 _dl_linux_resolver(struct elf_resolve *tpnt, int reloc_entry)
50 ELF_RELOC *this_reloc;
57 ElfW(Addr) instr_addr;
60 rel_addr = (char *)tpnt->dynamic_info[DT_JMPREL];
61 this_reloc = (ELF_RELOC *)(rel_addr + reloc_entry);
62 symtab_index = ELF_R_SYM(this_reloc->r_info);
64 symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
65 strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
66 symname = strtab + symtab[symtab_index].st_name;
68 /* Address of the jump instruction to fix up. */
69 instr_addr = (this_reloc->r_offset + tpnt->loadaddr);
70 got_addr = (char **)instr_addr;
72 /* Get the address of the GOT entry. */
73 new_addr = _dl_find_hash(symname, &_dl_loaded_modules->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 < 0x40000000) {
81 if (_dl_debug_bindings) {
82 _dl_dprintf(_dl_debug_file, "\nresolve function: %s", symname);
84 _dl_dprintf(_dl_debug_file,
85 "\tpatched: %x ==> %x @ %x\n",
86 *got_addr, new_addr, got_addr);
89 if (!_dl_debug_nofixups)
93 return (unsigned long)new_addr;
97 _dl_parse(struct elf_resolve *tpnt, struct r_scope_elem *scope,
98 unsigned long rel_addr, unsigned long rel_size,
99 int (*reloc_fnc)(struct elf_resolve *tpnt, struct r_scope_elem *scope,
100 ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab))
108 /* Parse the relocation information. */
109 rpnt = (ELF_RELOC *)rel_addr;
110 rel_size /= sizeof(ELF_RELOC);
112 symtab = (ElfW(Sym) *)tpnt->dynamic_info[DT_SYMTAB];
113 strtab = (char *)tpnt->dynamic_info[DT_STRTAB];
115 for (i = 0; i < rel_size; i++, rpnt++) {
118 symtab_index = ELF_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);
128 _dl_dprintf(2, "\n%s: ", _dl_progname);
131 _dl_dprintf(2, "symbol '%s': ",
132 strtab + symtab[symtab_index].st_name);
134 if (unlikely(res < 0)) {
135 int reloc_type = ELF_R_TYPE(rpnt->r_info);
137 _dl_dprintf(2, "can't handle reloc type "
138 #if defined (__SUPPORT_LD_DEBUG__)
139 "%s\n", _dl_reltypes(reloc_type));
144 } else if (unlikely(res > 0)) {
145 _dl_dprintf(2, "can't resolve symbol\n");
154 _dl_do_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
155 ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
160 #if defined USE_TLS && USE_TLS
161 struct elf_resolve *tls_tpnt;
163 struct symbol_ref sym_ref;
164 ElfW(Addr) *reloc_addr;
165 ElfW(Addr) symbol_addr;
166 #if defined (__SUPPORT_LD_DEBUG__)
170 reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + (unsigned long)rpnt->r_offset);
171 reloc_type = ELF_R_TYPE(rpnt->r_info);
172 symtab_index = ELF_R_SYM(rpnt->r_info);
173 sym_ref.sym = &symtab[symtab_index];
176 symname = strtab + sym_ref.sym->st_name;
179 symbol_addr = (ElfW(Addr))_dl_find_hash(symname, scope, tpnt,
180 elf_machine_type_class(reloc_type), &sym_ref);
182 * We want to allow undefined references to weak symbols - this
183 * might have been intentional. We should not be linking local
184 * symbols here, so all bases should be covered.
186 if (unlikely(!symbol_addr && (ELF_ST_TYPE(sym_ref.sym->st_info) != STT_TLS)
187 && (ELF_ST_BIND(sym_ref.sym->st_info) != STB_WEAK))) {
188 /* This may be non-fatal if called from dlopen. */
191 if (_dl_trace_prelink) {
192 _dl_debug_lookup (symname, tpnt, &symtab[symtab_index],
193 &sym_ref, elf_machine_type_class(reloc_type));
195 #if defined USE_TLS && USE_TLS
196 tls_tpnt = sym_ref.tpnt;
199 /* Relocs against STN_UNDEF are usually treated as using a
200 * symbol value of zero, and using the module containing the
202 symbol_addr = sym_ref.sym->st_value;
203 #if defined USE_TLS && USE_TLS
208 #if defined (__SUPPORT_LD_DEBUG__)
209 old_val = *reloc_addr;
212 switch (reloc_type) {
217 *reloc_addr = symbol_addr + rpnt->r_addend;
221 *reloc_addr = symbol_addr + rpnt->r_addend - rpnt->r_offset;
224 case R_X86_64_GLOB_DAT:
225 case R_X86_64_JUMP_SLOT:
226 *reloc_addr = symbol_addr + rpnt->r_addend;
229 /* handled by elf_machine_relative()
230 case R_X86_64_RELATIVE:
231 *reloc_addr = map->l_addr + rpnt->r_addend;
234 #if defined USE_TLS && USE_TLS
235 case R_X86_64_DTPMOD64:
236 *reloc_addr = tls_tpnt->l_tls_modid;
238 case R_X86_64_DTPOFF64:
239 /* During relocation all TLS symbols are defined and used.
240 * Therefore the offset is already correct. */
241 *reloc_addr = symbol_addr + rpnt->r_addend;
243 case R_X86_64_TPOFF64:
244 /* The offset is negative, forward from the thread pointer.
245 * We know the offset of the object the symbol is contained in.
246 * It is a negative value which will be added to the
248 CHECK_STATIC_TLS ((struct link_map *) tls_tpnt);
249 *reloc_addr = symbol_addr - tls_tpnt->l_tls_offset + rpnt->r_addend;
253 *(unsigned int *) reloc_addr = symbol_addr + rpnt->r_addend;
254 /* XXX: should check for overflow eh ? */
256 #ifndef __DONE_BY_AW11__
257 case R_X86_64_RELATIVE:
258 *reloc_addr += (unsigned long)tpnt->loadaddr;
259 /* XXX: should check for overflow eh ? */
265 #if defined (__SUPPORT_LD_DEBUG__)
267 _dl_dprintf(_dl_debug_file,
268 "\t%s move %d bytes from %x to %x\n",
269 symname, sym_ref.sym->st_size,
270 symbol_addr, reloc_addr);
273 _dl_memcpy((char *)reloc_addr,
275 sym_ref.sym->st_size);
277 #if defined (__SUPPORT_LD_DEBUG__)
279 _dl_dprintf(_dl_debug_file, "no symbol_addr to copy !?\n");
284 return -1; /* Calls _dl_exit(1). */
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",
290 old_val, *reloc_addr, reloc_addr);
297 _dl_do_lazy_reloc(struct elf_resolve *tpnt, struct r_scope_elem *scope,
298 ELF_RELOC *rpnt, ElfW(Sym) *symtab, char *strtab)
301 ElfW(Addr) *reloc_addr;
302 #if defined (__SUPPORT_LD_DEBUG__)
309 reloc_addr = (ElfW(Addr)*)(tpnt->loadaddr + rpnt->r_offset);
310 reloc_type = ELF_R_TYPE(rpnt->r_info);
312 #if defined (__SUPPORT_LD_DEBUG__)
313 old_val = *reloc_addr;
316 switch (reloc_type) {
319 case R_X86_64_JUMP_SLOT:
320 *reloc_addr += (unsigned long)tpnt->loadaddr;
326 #if defined (__SUPPORT_LD_DEBUG__)
327 if (_dl_debug_reloc && _dl_debug_detail)
328 _dl_dprintf(_dl_debug_file, "\tpatched_lazy: %x ==> %x @ %x\n",
329 old_val, *reloc_addr, reloc_addr);
336 _dl_parse_lazy_relocation_information(struct dyn_elf *rpnt,
337 unsigned long rel_addr,
338 unsigned long rel_size)
340 (void)_dl_parse(rpnt->dyn, NULL, rel_addr, rel_size, _dl_do_lazy_reloc);
344 _dl_parse_relocation_information(struct dyn_elf *rpnt,
345 struct r_scope_elem *scope,
346 unsigned long rel_addr,
347 unsigned long rel_size)
349 return _dl_parse(rpnt->dyn, scope, rel_addr, rel_size, _dl_do_reloc);