(pututline_r): Since we assign RESULT from lseek now, check that it's >= 0, not...
[glibc.git] / elf / dl-runtime.c
blob8ad2c0ffa416d56f54073599ea0e5c1d27aafd4b
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
20 #include <link.h>
21 #include "dynamic-link.h"
24 /* The global scope we will use for symbol lookups.
25 This will be modified by _dl_open if RTLD_GLOBAL is used. */
26 struct link_map **_dl_global_scope = _dl_default_scope;
27 struct link_map **_dl_global_scope_end = &_dl_default_scope[3];
30 /* Hack _dl_global_scope[0] and [1] as necessary, and return a pointer into
31 _dl_global_scope that should be passed to _dl_lookup_symbol for symbol
32 references made in the object L's relocations. */
33 inline struct link_map **
34 _dl_object_relocation_scope (struct link_map *l)
36 if (l->l_info[DT_SYMBOLIC])
38 /* This object's global references are to be resolved first
39 in the object itself, and only secondarily in more global
40 scopes. */
42 if (! l->l_searchlist)
43 /* We must construct the searchlist for this object. */
44 _dl_map_object_deps (l);
46 /* The primary scope is this object itself and its
47 dependencies. */
48 _dl_global_scope[0] = l;
50 /* Secondary is the dependency tree that reached L; the object
51 requested directly by the user is at the root of that tree. */
52 while (l->l_loader)
53 l = l->l_loader;
54 _dl_global_scope[1] = l;
56 /* Finally, the global scope follows. */
58 return _dl_global_scope;
60 else
62 /* Use first the global scope, and then the scope of the root of the
63 dependency tree that first caused this object to be loaded. */
64 while (l->l_loader)
65 l = l->l_loader;
66 *_dl_global_scope_end = l;
67 return &_dl_global_scope[2];
71 /* Figure out the right type, Rel or Rela. */
72 #define elf_machine_rel 1
73 #define elf_machine_rela 2
74 #if elf_machine_relplt == elf_machine_rel
75 #define PLTREL ElfW(Rel)
76 #elif elf_machine_relplt == elf_machine_rela
77 #define PLTREL ElfW(Rela)
78 #else
79 #error "dl-machine.h bug: elf_machine_relplt not rel or rela"
80 #endif
81 #undef elf_machine_rel
82 #undef elf_machine_rela
84 /* We need to define the function as a local symbol so that the reference
85 in the trampoline code will be a local PC-relative call. Tell the
86 compiler not to worry that the function appears not to be called. */
88 static ElfW(Addr) fixup (
89 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
90 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
91 #endif
92 struct link_map *l, ElfW(Word) reloc_offset)
93 __attribute__ ((unused));
95 /* This function is called through a special trampoline from the PLT the
96 first time each PLT entry is called. We must perform the relocation
97 specified in the PLT of the given shared object, and return the resolved
98 function address to the trampoline, which will restart the original call
99 to that address. Future calls will bounce directly from the PLT to the
100 function. */
102 static ElfW(Addr)
103 fixup (
104 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
105 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
106 #endif
107 struct link_map *l, ElfW(Word) reloc_offset)
109 const ElfW(Sym) *const symtab
110 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);
111 const char *strtab =
112 (const char *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
114 const PLTREL *const reloc
115 = (const void *) (l->l_addr + l->l_info[DT_JMPREL]->d_un.d_ptr +
116 reloc_offset);
118 /* Set up the scope to find symbols referenced by this object. */
119 struct link_map **scope = _dl_object_relocation_scope (l);
121 /* Perform the specified relocation. */
122 ElfW(Addr) resolve (const ElfW(Sym) **ref,
123 ElfW(Addr) reloc_addr, int noplt)
125 return _dl_lookup_symbol (strtab + (*ref)->st_name, ref,
126 scope, l->l_name, reloc_addr, noplt);
128 elf_machine_relplt (l, reloc, &symtab[ELFW(R_SYM) (reloc->r_info)], resolve);
130 *_dl_global_scope_end = NULL;
132 /* Return the address that was written by the relocation. */
133 return *(ElfW(Addr) *) (l->l_addr + reloc->r_offset);
137 /* This macro is defined in dl-machine.h to define the entry point called
138 by the PLT. The `fixup' function above does the real work, but a little
139 more twiddling is needed to get the stack right and jump to the address
140 finally resolved. */
142 ELF_MACHINE_RUNTIME_TRAMPOLINE