(gaih_inet): Fix problem with AF_UNSPEC lookup with AI_CANONNAME of name which has...
[glibc.git] / elf / dl-runtime.c
bloba0aecda2e83cb04e4d3a54bdefa68446ecf34383
1 /* On-demand PLT fixup for shared objects.
2 Copyright (C) 1995-2002, 2003, 2004 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 #define IN_DL_RUNTIME 1 /* This can be tested in dl-machine.h. */
22 #include <alloca.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <ldsodefs.h>
26 #include "dynamic-link.h"
28 #if (!defined ELF_MACHINE_NO_RELA && !defined ELF_MACHINE_PLT_REL) \
29 || ELF_MACHINE_NO_REL
30 # define PLTREL ElfW(Rela)
31 #else
32 # define PLTREL ElfW(Rel)
33 #endif
35 #ifndef VERSYMIDX
36 # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
37 #endif
39 /* The fixup functions might have need special attributes. If none
40 are provided define the macro as empty. */
41 #ifndef ARCH_FIXUP_ATTRIBUTE
42 # define ARCH_FIXUP_ATTRIBUTE
43 #endif
46 /* This function is called through a special trampoline from the PLT the
47 first time each PLT entry is called. We must perform the relocation
48 specified in the PLT of the given shared object, and return the resolved
49 function address to the trampoline, which will restart the original call
50 to that address. Future calls will bounce directly from the PLT to the
51 function. */
53 #ifndef ELF_MACHINE_NO_PLT
54 static ElfW(Addr)
55 __attribute ((used, noinline)) ARCH_FIXUP_ATTRIBUTE
56 fixup (
57 # ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
58 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
59 # endif
60 /* GKM FIXME: Fix trampoline to pass bounds so we can do
61 without the `__unbounded' qualifier. */
62 struct link_map *__unbounded l, ElfW(Word) reloc_offset)
64 const ElfW(Sym) *const symtab
65 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
66 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
68 const PLTREL *const reloc
69 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
70 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
71 void *const rel_addr = (void *)(l->l_addr + reloc->r_offset);
72 lookup_t result;
73 ElfW(Addr) value;
75 /* Sanity check that we're really looking at a PLT relocation. */
76 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
78 /* Look up the target symbol. If the normal lookup rules are not
79 used don't look in the global scope. */
80 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
82 const struct r_found_version *version = NULL;
83 // XXX Why exactly do we have the differentiation of the flags here?
84 int flags = DL_LOOKUP_ADD_DEPENDENCY;
86 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
88 const ElfW(Half) *vernum =
89 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
90 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
91 version = &l->l_versions[ndx];
92 if (version->hash == 0)
93 version = NULL;
94 else
95 flags = 0;
98 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
99 l->l_scope, version, ELF_RTYPE_CLASS_PLT,
100 DL_LOOKUP_ADD_DEPENDENCY, NULL);
102 /* Currently result contains the base load address (or link map)
103 of the object that defines sym. Now add in the symbol
104 offset. */
105 value = (sym ? LOOKUP_VALUE_ADDRESS (result) + sym->st_value : 0);
107 else
109 /* We already found the symbol. The module (and therefore its load
110 address) is also known. */
111 value = l->l_addr + sym->st_value;
112 #ifdef DL_LOOKUP_RETURNS_MAP
113 result = l;
114 #endif
117 /* And now perhaps the relocation addend. */
118 value = elf_machine_plt_value (l, reloc, value);
120 /* Finally, fix up the plt itself. */
121 if (__builtin_expect (GLRO(dl_bind_not), 0))
122 return value;
124 return elf_machine_fixup_plt (l, result, reloc, rel_addr, value);
126 #endif
128 #if !defined PROF && !defined ELF_MACHINE_NO_PLT && !__BOUNDED_POINTERS__
130 static ElfW(Addr)
131 __attribute ((used, noinline)) ARCH_FIXUP_ATTRIBUTE
132 profile_fixup (
133 #ifdef ELF_MACHINE_RUNTIME_FIXUP_ARGS
134 ELF_MACHINE_RUNTIME_FIXUP_ARGS,
135 #endif
136 struct link_map *l, ElfW(Word) reloc_offset, ElfW(Addr) retaddr)
138 void (*mcount_fct) (ElfW(Addr), ElfW(Addr)) = INTUSE(_dl_mcount);
139 ElfW(Addr) *resultp;
140 lookup_t result;
141 ElfW(Addr) value;
143 /* This is the address in the array where we store the result of previous
144 relocations. */
145 resultp = &l->l_reloc_result[reloc_offset / sizeof (PLTREL)];
147 value = *resultp;
148 if (value == 0)
150 /* This is the first time we have to relocate this object. */
151 const ElfW(Sym) *const symtab
152 = (const void *) D_PTR (l, l_info[DT_SYMTAB]);
153 const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]);
155 const PLTREL *const reloc
156 = (const void *) (D_PTR (l, l_info[DT_JMPREL]) + reloc_offset);
157 const ElfW(Sym) *sym = &symtab[ELFW(R_SYM) (reloc->r_info)];
159 /* Sanity check that we're really looking at a PLT relocation. */
160 assert (ELFW(R_TYPE)(reloc->r_info) == ELF_MACHINE_JMP_SLOT);
162 /* Look up the target symbol. If the symbol is marked STV_PROTECTED
163 don't look in the global scope. */
164 if (__builtin_expect (ELFW(ST_VISIBILITY) (sym->st_other), 0) == 0)
166 const struct r_found_version *version = NULL;
167 // XXX Why exactly do we have the differentiation of the flags here?
168 int flags = DL_LOOKUP_ADD_DEPENDENCY;
170 if (l->l_info[VERSYMIDX (DT_VERSYM)] != NULL)
172 const ElfW(Half) *vernum =
173 (const void *) D_PTR (l, l_info[VERSYMIDX (DT_VERSYM)]);
174 ElfW(Half) ndx = vernum[ELFW(R_SYM) (reloc->r_info)] & 0x7fff;
175 version = &l->l_versions[ndx];
176 if (version->hash == 0)
177 version = NULL;
178 else
179 flags = 0;
182 result = _dl_lookup_symbol_x (strtab + sym->st_name, l, &sym,
183 l->l_scope, version,
184 ELF_RTYPE_CLASS_PLT,
185 DL_LOOKUP_ADD_DEPENDENCY, NULL);
187 /* Currently result contains the base load address (or link map)
188 of the object that defines sym. Now add in the symbol
189 offset. */
190 value = (sym ? LOOKUP_VALUE_ADDRESS (result) + sym->st_value : 0);
192 else
194 /* We already found the symbol. The module (and therefore its load
195 address) is also known. */
196 value = l->l_addr + sym->st_value;
197 #ifdef DL_LOOKUP_RETURNS_MAP
198 result = l;
199 #endif
201 /* And now perhaps the relocation addend. */
202 value = elf_machine_plt_value (l, reloc, value);
204 /* Store the result for later runs. */
205 if (__builtin_expect (! GLRO(dl_bind_not), 1))
206 *resultp = value;
209 (*mcount_fct) (retaddr, value);
211 return value;
214 #endif /* PROF && ELF_MACHINE_NO_PLT */
217 /* This macro is defined in dl-machine.h to define the entry point called
218 by the PLT. The `fixup' function above does the real work, but a little
219 more twiddling is needed to get the stack right and jump to the address
220 finally resolved. */
222 ELF_MACHINE_RUNTIME_TRAMPOLINE