New HPPA Linux version of pt-initfini.
[glibc.git] / elf / dl-runtime.c
blob01beb19633025c6b572cc3bb3f238169531cf3a3
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-1999, 2000 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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 || ELF_MACHINE_NO_REL
27 # define PLTREL ElfW(Rela)
28 #else
29 # define PLTREL ElfW(Rel)
30 #endif
32 #ifndef VERSYMIDX
33 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
34 #endif
37 /* This function is called through a special trampoline from the PLT the
38 first time each PLT entry is called. We must perform the relocation
39 specified in the PLT of the given shared object, and return the resolved
40 function address to the trampoline, which will restart the original call
41 to that address. Future calls will bounce directly from the PLT to the
42 function. */
44 #ifndef ELF_MACHINE_NO_PLT
45 static ElfW(Addr) __attribute__ ((unused))
46 fixup (
47 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
48 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
49 # endif
50 /* GKM FIXME: Fix trampoline to pass bounds so we can do
51 without the `__unbounded' qualifier. */
52 struct link_map *__unbounded l, ElfW(Word) reloc_offset)
54 const ElfW(Sym) *const symtab
55 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
56 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
58 const PLTREL *const reloc
59 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
60 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
61 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
62 lookup_t result;
63 ElfW(Addr) value;
65 /* The use of `alloca' here looks ridiculous but it helps. The goal is
66 to prevent the function from being inlined and thus optimized out.
67 There is no official way to do this so we use this trick. gcc never
68 inlines functions which use `alloca'. */
69 alloca (sizeof (int));
71 /* Sanity check that we're really looking at a PLT relocation. */
72 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
74 /* Look up the target symbol. If the normal lookup rules are not
75 used don't look in the global scope. */
76 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
78 switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
80 default:
82 const ElfW(Half) *vernum =
83 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
84 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)];
85 const struct r_found_version *version = &l->l_versions[ndx];
87 if (version->hash != 0)
89 result = _dl_lookup_versioned_symbol (strtab + sym->st_name,
90 l, &sym, l->l_scope,
91 version,
92 ELF_MACHINE_JMP_SLOT, 0);
93 break;
96 case 0:
97 result = _dl_lookup_symbol (strtab + sym->st_name, l, &sym,
98 l->l_scope, ELF_MACHINE_JMP_SLOT, 0);
101 /* Currently result contains the base load address (or link map)
102 of the object that defines sym. Now add in the symbol
103 offset. */
104 value = (sym ? LOOKUP_VALUE_ADDRESS (result) + sym->st_value : 0);
106 else
108 /* We already found the symbol. The module (and therefore its load
109 address) is also known. */
110 value = l->l_addr + sym->st_value;
111 #ifdef DL_LOOKUP_RETURNS_MAP
112 result = l;
113 #endif
116 /* And now perhaps the relocation addend. */
117 value = elf_machine_plt_value (l, reloc, value);
119 /* Finally, fix up the plt itself. */
120 if (__builtin_expect (_dl_bind_not, 0))
121 return value;
123 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
125 #endif
127 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
129 static ElfW(Addr) __attribute__ ((unused))
130 profile_fixup (
131 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
132 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
133 #endif
134 struct link_map *l, ElfW(Word) reloc_offset, ElfW(Addr) retaddr)
136 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = _dl_mcount;
137 ElfW(Addr) *resultp;
138 lookup_t result;
139 ElfW(Addr) value;
141 /* The use of `alloca' here looks ridiculous but it helps. The goal is
142 to prevent the function from being inlined, and thus optimized out.
143 There is no official way to do this so we use this trick. gcc never
144 inlines functions which use `alloca'. */
145 alloca (sizeof (int));
147 /* This is the address in the array where we store the result of previous
148 relocations. */
149 resultp = &l->l_reloc_result[reloc_offset / sizeof (PLTREL)];
151 value = *resultp;
152 if (value == 0)
154 /* This is the first time we have to relocate this object. */
155 const ElfW(Sym) *const symtab
156 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
157 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
159 const PLTREL *const reloc
160 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
161 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
163 /* Sanity check that we're really looking at a PLT relocation. */
164 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
166 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
167 don't look in the global scope. */
168 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
170 switch (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
172 default:
174 const ElfW(Half) *vernum =
175 (const void *) D_PTR (l,l_info[VERSYMIDX (DT_VERSYM)]);
176 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)];
177 const struct r_found_version *version = &l->l_versions[ndx];
179 if (version->hash != 0)
181 result = _dl_lookup_versioned_symbol(strtab + sym->st_name,
182 l, &sym, l->l_scope,
183 version,
184 ELF_MACHINE_JMP_SLOT,
186 break;
189 case 0:
190 result = _dl_lookup_symbol (strtab + sym->st_name, l, &sym,
191 l->l_scope, ELF_MACHINE_JMP_SLOT, 0);
194 /* Currently result contains the base load address (or link map)
195 of the object that defines sym. Now add in the symbol
196 offset. */
197 value = (sym ? LOOKUP_VALUE_ADDRESS (result) + sym->st_value : 0);
199 else
201 /* We already found the symbol. The module (and therefore its load
202 address) is also known. */
203 value = l->l_addr + sym->st_value;
204 #ifdef DL_LOOKUP_RETURNS_MAP
205 result = l;
206 #endif
208 /* And now perhaps the relocation addend. */
209 value = elf_machine_plt_value (l, reloc, value);
211 /* Store the result for later runs. */
212 if (__builtin_expect (! _dl_bind_not, 1))
213 *resultp = value;
216 (*mcount_fct) (retaddr, value);
218 return value;
221 #endif /* PROF && ELF_MACHINE_NO_PLT */
224 /* This macro is defined in dl-machine.h to define the entry point called
225 by the PLT. The `fixup' function above does the real work, but a little
226 more twiddling is needed to get the stack right and jump to the address
227 finally resolved. */
229 ELF_MACHINE_RUNTIME_TRAMPOLINE