]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-runtime.c
Update.
[thirdparty/glibc.git] / elf / dl-runtime.c
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20 #include <link.h>
21 #include <stddef.h>
22
23
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];
28
29
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)
35 {
36 if (l->l_info[DT_SYMBOLIC])
37 {
38 /* This object's global references are to be resolved first
39 in the object itself, and only secondarily in more global
40 scopes. */
41
42 if (! l->l_searchlist)
43 /* We must construct the searchlist for this object. */
44 _dl_map_object_deps (l, NULL, 0, 0);
45
46 /* The primary scope is this object itself and its
47 dependencies. */
48 _dl_global_scope[0] = l;
49
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;
55
56 /* Finally, the global scope follows. */
57
58 return _dl_global_scope;
59 }
60 else
61 {
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];
68 }
69 }
70 \f
71 #include "dynamic-link.h"
72
73 /* Figure out the right type, Rel or Rela. */
74 #define elf_machine_rel 1
75 #define elf_machine_rela 2
76 #if elf_machine_relplt == elf_machine_rel
77 # define PLTREL ElfW(Rel)
78 #elif elf_machine_relplt == elf_machine_rela
79 # define PLTREL ElfW(Rela)
80 #else
81 # error "dl-machine.h bug: elf_machine_relplt not rel or rela"
82 #endif
83 #undef elf_machine_rel
84 #undef elf_machine_rela
85
86 #ifndef VERSYMIDX
87 # define VERSYMIDX(sym) (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (sym))
88 #endif
89
90 /* We need to define the function as a local symbol so that the reference
91 in the trampoline code will be a local PC-relative call. Tell the
92 compiler not to worry that the function appears not to be called. */
93
94 static ElfW(Addr) fixup (
95 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
96 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
97 #endif
98 struct link_map *l, ElfW(Word) reloc_offset)
99 __attribute__ ((unused));
100
101 /* This function is called through a special trampoline from the PLT the
102 first time each PLT entry is called. We must perform the relocation
103 specified in the PLT of the given shared object, and return the resolved
104 function address to the trampoline, which will restart the original call
105 to that address. Future calls will bounce directly from the PLT to the
106 function. */
107
108 static ElfW(Addr)
109 fixup (
110 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
111 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
112 #endif
113 struct link_map *l, ElfW(Word) reloc_offset)
114 {
115 const ElfW(Sym) *const symtab
116 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);
117 const char *strtab =
118 (const char *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
119
120 const PLTREL *const reloc
121 = (const void *) (l->l_addr + l->l_info[DT_JMPREL]->d_un.d_ptr +
122 reloc_offset);
123
124 /* Set up the scope to find symbols referenced by this object. */
125 struct link_map **scope = _dl_object_relocation_scope (l);
126
127 {
128 /* This macro is used as a callback from the elf_machine_relplt code. */
129 #define RESOLVE(ref, version, flags) \
130 ((version) != NULL && (version)->hash != 0 \
131 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, (ref), scope, \
132 l->l_name, (version), (flags)) \
133 : _dl_lookup_symbol (strtab + (*ref)->st_name, (ref), scope, \
134 l->l_name, (flags)))
135 #include "dynamic-link.h"
136
137 /* Perform the specified relocation. */
138 if (l->l_info[VERSYMIDX (DT_VERSYM)])
139 {
140 const ElfW(Half) *version =
141 (const ElfW(Half) *) (l->l_addr +
142 l->l_info[VERSYMIDX (DT_VERSYM)]->d_un.d_ptr);
143 ElfW(Half) ndx = version[ELFW(R_SYM) (reloc->r_info)];
144
145 elf_machine_relplt (l, reloc, &symtab[ELFW(R_SYM) (reloc->r_info)],
146 &l->l_versions[ndx],
147 (void *) (l->l_addr + reloc->r_offset));
148 }
149 else
150 elf_machine_relplt (l, reloc, &symtab[ELFW(R_SYM) (reloc->r_info)],
151 NULL, (void *) (l->l_addr + reloc->r_offset));
152 }
153
154 *_dl_global_scope_end = NULL;
155
156 /* Return the address that was written by the relocation. */
157 #ifdef ELF_FIXUP_RETURNS_ADDRESS
158 return (ElfW(Addr))(l->l_addr + reloc->r_offset);
159 #else
160 return *(ElfW(Addr) *) (l->l_addr + reloc->r_offset);
161 #endif
162 }
163
164
165 #ifndef PROF
166 static ElfW(Addr)
167 profile_fixup (
168 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
169 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
170 #endif
171 struct link_map *l, ElfW(Word) reloc_offset, ElfW(Addr) retaddr)
172 __attribute__ ((unused));
173
174 static ElfW(Addr)
175 profile_fixup (
176 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
177 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
178 #endif
179 struct link_map *l, ElfW(Word) reloc_offset, ElfW(Addr) retaddr)
180 {
181 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = _dl_mcount;
182 const ElfW(Sym) *const symtab
183 = (const ElfW(Sym) *) (l->l_addr + l->l_info[DT_SYMTAB]->d_un.d_ptr);
184 const char *strtab =
185 (const char *) (l->l_addr + l->l_info[DT_STRTAB]->d_un.d_ptr);
186
187 const PLTREL *const reloc
188 = (const void *) (l->l_addr + l->l_info[DT_JMPREL]->d_un.d_ptr +
189 reloc_offset);
190 ElfW(Addr) result;
191
192 /* Set up the scope to find symbols referenced by this object. */
193 struct link_map **scope = _dl_object_relocation_scope (l);
194
195 {
196 /* This macro is used as a callback from the elf_machine_relplt code. */
197 #define RESOLVE(ref, version, flags) \
198 ((version) != NULL && (version)->hash != 0 \
199 ? _dl_lookup_versioned_symbol (strtab + (*ref)->st_name, (ref), scope, \
200 l->l_name, (version), (flags)) \
201 : _dl_lookup_symbol (strtab + (*ref)->st_name, (ref), scope, \
202 l->l_name, (flags)))
203 #include "dynamic-link.h"
204
205 /* Perform the specified relocation. */
206 if (l->l_info[VERSYMIDX (DT_VERSYM)])
207 {
208 const ElfW(Half) *version =
209 (const ElfW(Half) *) (l->l_addr +
210 l->l_info[VERSYMIDX (DT_VERSYM)]->d_un.d_ptr);
211 ElfW(Half) ndx = version[ELFW(R_SYM) (reloc->r_info)];
212
213 elf_machine_relplt (l, reloc, &symtab[ELFW(R_SYM) (reloc->r_info)],
214 &l->l_versions[ndx], (void *) &result);
215 }
216 else
217 elf_machine_relplt (l, reloc, &symtab[ELFW(R_SYM) (reloc->r_info)],
218 NULL, (void *) &result);
219 }
220
221 *_dl_global_scope_end = NULL;
222
223 /* Return the address that was written by the relocation. */
224 #ifdef ELF_FIXUP_RETURNS_ADDRESS
225 (*mcount_fct) (retaddr, result);
226
227 return &result; /* XXX This cannot work. What to do??? --drepper */
228 #else
229 (*mcount_fct) (retaddr, result);
230
231 return result;
232 #endif
233 }
234 #endif
235
236
237 /* This macro is defined in dl-machine.h to define the entry point called
238 by the PLT. The `fixup' function above does the real work, but a little
239 more twiddling is needed to get the stack right and jump to the address
240 finally resolved. */
241
242 ELF_MACHINE_RUNTIME_TRAMPOLINE