Update.
[glibc.git] / elf / dl-runtime.c
blobbe5d8b68adb2bbbde0105f4bf950b39952dab78f
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-1999, 2000, 2001, 2002 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <alloca.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <ldsodefs.h>
24 #include "dynamic-link.h"
26 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
27 || ELF_MACHINE_NO_REL
28 # define PLTREL ElfW(Rela)
29 #else
30 # define PLTREL ElfW(Rel)
31 #endif
33 #ifndef VERSYMIDX
34 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
35 #endif
38 /* This function is called through a special trampoline from the PLT the
39 first time each PLT entry is called. We must perform the relocation
40 specified in the PLT of the given shared object, and return the resolved
41 function address to the trampoline, which will restart the original call
42 to that address. Future calls will bounce directly from the PLT to the
43 function. */
45 #ifndef ELF_MACHINE_NO_PLT
46 static ElfW(Addr) __attribute_used__
47 fixup (
48 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
49 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
50 # endif
51 /* GKM FIXME: Fix trampoline to pass bounds so we can do
52 without the `__unbounded' qualifier. */
53 struct link_map *__unbounded l, ElfW(Word) reloc_offset)
55 const ElfW(Sym) *const symtab
56 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
57 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
59 const PLTREL *const reloc
60 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
61 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
62 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
63 lookup_t result;
64 ElfW(Addr) value;
66 /* The use of `alloca' here looks ridiculous but it helps. The goal is
67 to prevent the function from being inlined and thus optimized out.
68 There is no official way to do this so we use this trick. gcc never
69 inlines functions which use `alloca'. */
70 alloca (sizeof (int));
72 /* Sanity check that we're really looking at a PLT relocation. */
73 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
75 /* Look up the target symbol. If the normal lookup rules are not
76 used don't look in the global scope. */
77 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
79 switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
81 default:
83 const ElfW(Half) *vernum =
84 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
85 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)];
86 const struct r_found_version *version = &l->l_versions[ndx];
88 if (version->hash != 0)
90 result = INT(_dl_lookup_versioned_symbol) (strtab
91 + sym->st_name,
92 l, &sym, l->l_scope,
93 version,
94 ELF_RTYPE_CLASS_PLT,
95 0);
96 break;
99 case 0:
100 result = INT(_dl_lookup_symbol) (strtab + sym->st_name, l, &sym,
101 l->l_scope, ELF_RTYPE_CLASS_PLT, 0);
104 /* Currently result contains the base load address (or link map)
105 of the object that defines sym. Now add in the symbol
106 offset. */
107 value = (sym ? LOOKUP_VALUE_ADDRESS (result) + sym->st_value : 0);
109 else
111 /* We already found the symbol. The module (and therefore its load
112 address) is also known. */
113 value = l->l_addr + sym->st_value;
114 #ifdef DL_LOOKUP_RETURNS_MAP
115 result = l;
116 #endif
119 /* And now perhaps the relocation addend. */
120 value = elf_machine_plt_value (l, reloc, value);
122 /* Finally, fix up the plt itself. */
123 if (__builtin_expect (GL(dl_bind_not), 0))
124 return value;
126 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
128 #endif
130 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
132 static ElfW(Addr) __attribute_used__
133 profile_fixup (
134 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
135 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
136 #endif
137 struct link_map *l, ElfW(Word) reloc_offset, ElfW(Addr) retaddr)
139 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = _dl_mcount;
140 ElfW(Addr) *resultp;
141 lookup_t result;
142 ElfW(Addr) value;
144 /* The use of `alloca' here looks ridiculous but it helps. The goal is
145 to prevent the function from being inlined, and thus optimized out.
146 There is no official way to do this so we use this trick. gcc never
147 inlines functions which use `alloca'. */
148 alloca (sizeof (int));
150 /* This is the address in the array where we store the result of previous
151 relocations. */
152 resultp = &l->l_reloc_result[reloc_offset / sizeof (PLTREL)];
154 value = *resultp;
155 if (value == 0)
157 /* This is the first time we have to relocate this object. */
158 const ElfW(Sym) *const symtab
159 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
160 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
162 const PLTREL *const reloc
163 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
164 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
166 /* Sanity check that we're really looking at a PLT relocation. */
167 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
169 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
170 don't look in the global scope. */
171 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
173 switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
175 default:
177 const ElfW(Half) *vernum =
178 (const void *) D_PTR (l,l_info[VERSYMIDX (DT_VERSYM)]);
179 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)];
180 const struct r_found_version *version = &l->l_versions[ndx];
182 if (version->hash != 0)
184 result = INT(_dl_lookup_versioned_symbol) (strtab
185 + sym->st_name,
186 l, &sym,
187 l->l_scope,
188 version,
189 ELF_RTYPE_CLASS_PLT,
191 break;
194 case 0:
195 result = INT(_dl_lookup_symbol) (strtab + sym->st_name, l, &sym,
196 l->l_scope, ELF_RTYPE_CLASS_PLT,
200 /* Currently result contains the base load address (or link map)
201 of the object that defines sym. Now add in the symbol
202 offset. */
203 value = (sym ? LOOKUP_VALUE_ADDRESS (result) + sym->st_value : 0);
205 else
207 /* We already found the symbol. The module (and therefore its load
208 address) is also known. */
209 value = l->l_addr + sym->st_value;
210 #ifdef DL_LOOKUP_RETURNS_MAP
211 result = l;
212 #endif
214 /* And now perhaps the relocation addend. */
215 value = elf_machine_plt_value (l, reloc, value);
217 /* Store the result for later runs. */
218 if (__builtin_expect (! GL(dl_bind_not), 1))
219 *resultp = value;
222 (*mcount_fct) (retaddr, value);
224 return value;
227 #endif /* PROF && ELF_MACHINE_NO_PLT */
230 /* This macro is defined in dl-machine.h to define the entry point called
231 by the PLT. The `fixup' function above does the real work, but a little
232 more twiddling is needed to get the stack right and jump to the address
233 finally resolved. */
235 ELF_MACHINE_RUNTIME_TRAMPOLINE