1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995-2002, 2003 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
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include "dynamic-link.h"
30 /* Statistics function. */
32 # define bump_num_cache_relocations() ++GL(dl_num_cache_relocations)
34 # define bump_num_cache_relocations() ((void) 0)
39 /* We are trying to perform a static TLS relocation in MAP, but it was
40 dynamically loaded. This can only work if there is enough surplus in
41 the static TLS area already allocated for each running thread. If this
42 object's TLS segment is too big to fit, we fail. If it fits,
43 we set MAP->l_tls_offset and return. */
45 internal_function __attribute_noinline__
46 _dl_allocate_static_tls (struct link_map
*map
)
53 offset
= roundup (GL(dl_tls_static_used
) + map
->l_tls_blocksize
,
56 check
= offset
+ TLS_TCB_SIZE
;
58 offset
= roundup (GL(dl_tls_static_used
), map
->l_tls_align
);
59 used
= offset
+ map
->l_tls_blocksize
;
61 /* dl_tls_static_used includes the TCB at the beginning. */
63 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
66 if (check
> GL(dl_tls_static_size
))
68 const char *errstring
= N_("\
69 shared object cannot be dlopen()ed: static TLS memory too small");
70 INTUSE(_dl_signal_error
) (0, (map
)->l_name
, NULL
, errstring
);
73 map
->l_tls_offset
= offset
;
74 GL(dl_tls_static_used
) = used
;
80 _dl_relocate_object (struct link_map
*l
, struct r_scope_elem
*scope
[],
81 int lazy
, int consider_profiling
)
88 struct textrels
*next
;
90 /* Initialize it to make the compiler happy. */
91 const char *errstring
= NULL
;
96 /* If DT_BIND_NOW is set relocate all references in this object. We
97 do not do this if we are profiling, of course. */
98 if (!consider_profiling
99 && __builtin_expect (l
->l_info
[DT_BIND_NOW
] != NULL
, 0))
102 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_RELOC
, 0))
103 INTUSE(_dl_debug_printf
) ("\nrelocation processing: %s%s\n",
104 l
->l_name
[0] ? l
->l_name
: rtld_progname
,
105 lazy
? " (lazy)" : "");
107 /* DT_TEXTREL is now in level 2 and might phase out at some time.
108 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
109 testing easier and therefore it will be available at all time. */
110 if (__builtin_expect (l
->l_info
[DT_TEXTREL
] != NULL
, 0))
112 /* Bletch. We must make read-only segments writable
113 long enough to relocate them. */
114 const ElfW(Phdr
) *ph
;
115 for (ph
= l
->l_phdr
; ph
< &l
->l_phdr
[l
->l_phnum
]; ++ph
)
116 if (ph
->p_type
== PT_LOAD
&& (ph
->p_flags
& PF_W
) == 0)
118 struct textrels
*newp
;
120 newp
= (struct textrels
*) alloca (sizeof (*newp
));
121 newp
->len
= (((ph
->p_vaddr
+ ph
->p_memsz
+ GL(dl_pagesize
) - 1)
122 & ~(GL(dl_pagesize
) - 1))
123 - (ph
->p_vaddr
& ~(GL(dl_pagesize
) - 1)));
124 newp
->start
= ((ph
->p_vaddr
& ~(GL(dl_pagesize
) - 1))
125 + (caddr_t
) l
->l_addr
);
127 if (__mprotect (newp
->start
, newp
->len
, PROT_READ
|PROT_WRITE
) < 0)
129 errstring
= N_("cannot make segment writable for relocation");
131 INTUSE(_dl_signal_error
) (errno
, l
->l_name
, NULL
, errstring
);
134 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
135 newp
->prot
= (PF_TO_PROT
136 >> ((ph
->p_flags
& (PF_R
| PF_W
| PF_X
)) * 4)) & 0xf;
139 if (ph
->p_flags
& PF_R
)
140 newp
->prot
|= PROT_READ
;
141 if (ph
->p_flags
& PF_W
)
142 newp
->prot
|= PROT_WRITE
;
143 if (ph
->p_flags
& PF_X
)
144 newp
->prot
|= PROT_EXEC
;
146 newp
->next
= textrels
;
152 /* Do the actual relocation of the object's GOT and other data. */
154 /* String table object symbols. */
155 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
157 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
158 #define RESOLVE_MAP(ref, version, r_type) \
159 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
160 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
161 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
162 ? (bump_num_cache_relocations (), \
163 (*ref) = l->l_lookup_cache.ret, \
164 l->l_lookup_cache.value) \
166 int _tc = elf_machine_type_class (r_type); \
167 l->l_lookup_cache.type_class = _tc; \
168 l->l_lookup_cache.sym = (*ref); \
169 _lr = ((version) != NULL && (version)->hash != 0 \
170 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
174 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
176 DL_LOOKUP_ADD_DEPENDENCY)); \
177 l->l_lookup_cache.ret = (*ref); \
178 l->l_lookup_cache.value = _lr; })) \
180 #define RESOLVE(ref, version, r_type) \
181 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
182 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
183 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
184 ? (bump_num_cache_relocations (), \
185 (*ref) = l->l_lookup_cache.ret, \
186 l->l_lookup_cache.value) \
188 int _tc = elf_machine_type_class (r_type); \
189 l->l_lookup_cache.type_class = _tc; \
190 l->l_lookup_cache.sym = (*ref); \
191 _lr = ((version) != NULL && (version)->hash != 0 \
192 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
196 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
198 DL_LOOKUP_ADD_DEPENDENCY)); \
199 l->l_lookup_cache.ret = (*ref); \
200 l->l_lookup_cache.value = _lr; })) \
203 /* This macro is used as a callback from elf_machine_rel{a,} when a
204 static TLS reloc is about to be performed. Since (in dl-load.c) we
205 permit dynamic loading of objects that might use such relocs, we
206 have to check whether each use is actually doable. If the object
207 whose TLS segment the reference resolves to was allocated space in
208 the static TLS block at startup, then it's ok. Otherwise, we make
209 an attempt to allocate it in surplus space on the fly. If that
210 can't be done, we fall back to the error that DF_STATIC_TLS is
211 intended to produce. */
212 #define CHECK_STATIC_TLS(map, sym_map) \
214 if (__builtin_expect ((sym_map)->l_tls_offset == NO_TLS_OFFSET, 0)) \
215 _dl_allocate_static_tls (sym_map); \
218 #include "dynamic-link.h"
220 ELF_DYNAMIC_RELOCATE (l
, lazy
, consider_profiling
);
222 if (__builtin_expect (consider_profiling
, 0))
224 /* Allocate the array which will contain the already found
225 relocations. If the shared object lacks a PLT (for example
226 if it only contains lead function) the l_info[DT_PLTRELSZ]
228 if (l
->l_info
[DT_PLTRELSZ
] == NULL
)
230 errstring
= N_("%s: profiler found no PLTREL in object %s\n");
232 _dl_fatal_printf (errstring
,
233 rtld_progname
?: "<program name unknown>",
238 (ElfW(Addr
) *) calloc (sizeof (ElfW(Addr
)),
239 l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
);
240 if (l
->l_reloc_result
== NULL
)
243 %s: profiler out of memory shadowing PLTREL of %s\n");
249 /* Mark the object so we know this work has been done. */
252 /* Undo the segment protection changes. */
253 while (__builtin_expect (textrels
!= NULL
, 0))
255 if (__mprotect (textrels
->start
, textrels
->len
, textrels
->prot
) < 0)
257 errstring
= N_("cannot restore segment prot after reloc");
261 textrels
= textrels
->next
;
264 INTDEF (_dl_relocate_object
)
268 internal_function __attribute_noinline__
269 _dl_reloc_bad_type (struct link_map
*map
, unsigned int type
, int plt
)
271 extern const char INTUSE(_itoa_lower_digits
)[] attribute_hidden
;
272 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
274 /* XXX We cannot translate these messages. */
275 static const char msg
[2][32
276 #if __ELF_NATIVE_CLASS == 64
279 ] = { "unexpected reloc type 0x",
280 "unexpected PLT reloc type 0x" };
281 char msgbuf
[sizeof (msg
[0])];
284 cp
= __stpcpy (msgbuf
, msg
[plt
]);
285 #if __ELF_NATIVE_CLASS == 64
286 if (__builtin_expect(type
> 0xff, 0))
288 *cp
++ = DIGIT (type
>> 28);
289 *cp
++ = DIGIT (type
>> 24);
290 *cp
++ = DIGIT (type
>> 20);
291 *cp
++ = DIGIT (type
>> 16);
292 *cp
++ = DIGIT (type
>> 12);
293 *cp
++ = DIGIT (type
>> 8);
296 *cp
++ = DIGIT (type
>> 4);
297 *cp
++ = DIGIT (type
);
300 INTUSE(_dl_signal_error
) (0, map
->l_name
, NULL
, msgbuf
);