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.
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. */
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
42 if (! l
->l_searchlist
)
43 /* We must construct the searchlist for this object. */
44 _dl_map_object_deps (l
, NULL
, 0, 0);
46 /* The primary scope is this object itself and its
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. */
54 _dl_global_scope
[1] = l
;
56 /* Finally, the global scope follows. */
58 return _dl_global_scope
;
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. */
66 *_dl_global_scope_end
= l
;
67 return &_dl_global_scope
[2];
71 #include "dynamic-link.h"
73 #if !defined ELF_MACHINE_NO_RELA || ELF_MACHINE_NO_REL
74 # define PLTREL ElfW(Rela)
76 # define PLTREL ElfW(Rel)
80 # define VERSYMIDX(sym) (DT_NUM + DT_PROCNUM + DT_VERSIONTAGIDX (sym))
84 /* This function is called through a special trampoline from the PLT the
85 first time each PLT entry is called. We must perform the relocation
86 specified in the PLT of the given shared object, and return the resolved
87 function address to the trampoline, which will restart the original call
88 to that address. Future calls will bounce directly from the PLT to the
91 static ElfW(Addr
) __attribute__ ((unused
))
93 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
94 ELF_MACHINE_RUNTIME_FIXUP_ARGS
,
96 struct link_map
*l
, ElfW(Word
) reloc_offset
)
98 const ElfW(Sym
) *const symtab
99 = (const ElfW(Sym
) *) (l
->l_addr
+ l
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
101 (const char *) (l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
103 const PLTREL
*const reloc
104 = (const void *) (l
->l_addr
+ l
->l_info
[DT_JMPREL
]->d_un
.d_ptr
+
106 const ElfW(Sym
) *sym
= &symtab
[ELFW(R_SYM
) (reloc
->r_info
)];
107 void *const rel_addr
= (void *)(l
->l_addr
+ reloc
->r_offset
);
110 /* Set up the scope to find symbols referenced by this object. */
111 struct link_map
**scope
= _dl_object_relocation_scope (l
);
113 /* Sanity check that we're really looking at a PLT relocation. */
114 assert (ELFW(R_TYPE
)(reloc
->r_info
) == ELF_MACHINE_JMP_SLOT
);
116 /* Look up the target symbol. */
117 switch (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
121 const ElfW(Half
) *vernum
= (const ElfW(Half
) *)
122 (l
->l_addr
+ l
->l_info
[VERSYMIDX (DT_VERSYM
)]->d_un
.d_ptr
);
123 ElfW(Half
) ndx
= vernum
[ELFW(R_SYM
) (reloc
->r_info
)];
124 const struct r_found_version
*version
= &l
->l_versions
[ndx
];
126 if (version
->hash
!= 0)
128 value
= _dl_lookup_versioned_symbol(strtab
+ sym
->st_name
,
129 &sym
, scope
, l
->l_name
,
130 version
, ELF_MACHINE_JMP_SLOT
);
135 value
= _dl_lookup_symbol (strtab
+ sym
->st_name
, &sym
, scope
,
136 l
->l_name
, ELF_MACHINE_JMP_SLOT
);
139 /* Currently value contains the base load address of the object
140 that defines sym. Now add in the symbol offset. */
141 value
= (sym
? value
+ sym
->st_value
: 0);
143 /* And now the relocation addend. */
144 #ifndef ELF_MACHINE_NO_RELA
145 if (l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
== sizeof (ElfW(Rela
)))
146 value
+= reloc
->r_addend
;
147 #elif ELF_MACHINE_NO_REL
148 value
+= reloc
->r_addend
;
151 /* Finally, fix up the plt itself. */
152 elf_machine_fixup_plt (l
, reloc
, rel_addr
, value
);
154 *_dl_global_scope_end
= NULL
;
162 static ElfW(Addr
) __attribute__ ((unused
))
164 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
165 ELF_MACHINE_RUNTIME_FIXUP_ARGS
,
167 struct link_map
*l
, ElfW(Word
) reloc_offset
, ElfW(Addr
) retaddr
)
169 void (*mcount_fct
) (ElfW(Addr
), ElfW(Addr
)) = _dl_mcount
;
171 const ElfW(Sym
) *const symtab
172 = (const ElfW(Sym
) *) (l
->l_addr
+ l
->l_info
[DT_SYMTAB
]->d_un
.d_ptr
);
174 (const char *) (l
->l_addr
+ l
->l_info
[DT_STRTAB
]->d_un
.d_ptr
);
176 const PLTREL
*const reloc
177 = (const void *) (l
->l_addr
+ l
->l_info
[DT_JMPREL
]->d_un
.d_ptr
+
179 const ElfW(Sym
) *sym
= &symtab
[ELFW(R_SYM
) (reloc
->r_info
)];
182 /* Set up the scope to find symbols referenced by this object. */
183 struct link_map
**scope
= _dl_object_relocation_scope (l
);
185 /* Sanity check that we're really looking at a PLT relocation. */
186 assert (ELFW(R_TYPE
)(reloc
->r_info
) == ELF_MACHINE_JMP_SLOT
);
188 /* Look up the target symbol. */
189 switch (l
->l_info
[VERSYMIDX (DT_VERSYM
)] != NULL
)
193 const ElfW(Half
) *vernum
= (const ElfW(Half
) *)
194 (l
->l_addr
+ l
->l_info
[VERSYMIDX (DT_VERSYM
)]->d_un
.d_ptr
);
195 ElfW(Half
) ndx
= vernum
[ELFW(R_SYM
) (reloc
->r_info
)];
196 const struct r_found_version
*version
= &l
->l_versions
[ndx
];
198 if (version
->hash
!= 0)
200 value
= _dl_lookup_versioned_symbol(strtab
+ sym
->st_name
,
201 &sym
, scope
, l
->l_name
,
202 version
, ELF_MACHINE_JMP_SLOT
);
207 value
= _dl_lookup_symbol (strtab
+ sym
->st_name
, &sym
, scope
,
208 l
->l_name
, ELF_MACHINE_JMP_SLOT
);
211 /* Currently value contains the base load address of the object
212 that defines sym. Now add in the symbol offset. */
213 value
= (sym
? value
+ sym
->st_value
: 0);
215 /* And now the relocation addend. */
216 #ifndef ELF_MACHINE_NO_RELA
217 if (l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
== sizeof (ElfW(Rela
)))
218 value
+= reloc
->r_addend
;
219 #elif ELF_MACHINE_NO_REL
220 value
+= reloc
->r_addend
;
223 *_dl_global_scope_end
= NULL
;
224 (*mcount_fct
) (retaddr
, value
);
232 /* This macro is defined in dl-machine.h to define the entry point called
233 by the PLT. The `fixup' function above does the real work, but a little
234 more twiddling is needed to get the stack right and jump to the address
237 ELF_MACHINE_RUNTIME_TRAMPOLINE