1 /* Relocate a shared object and resolve its references to other loaded objects.
2 Copyright (C) 1995,96,97,98,99,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
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 return false. If it fits,
43 we set MAP->l_tls_offset and return true. */
45 allocate_static_tls (struct link_map
*map
)
47 size_t offset
= roundup (GL(dl_tls_static_used
), map
->l_tls_align
);
48 if (offset
+ map
->l_tls_blocksize
> (GL(dl_tls_static_size
)
52 /* dl_tls_static_used includes the TCB at the beginning. */
54 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
58 map
->l_tls_offset
= offset
;
59 GL(dl_tls_static_used
) = offset
+ map
->l_tls_blocksize
;
66 _dl_relocate_object (struct link_map
*l
, struct r_scope_elem
*scope
[],
67 int lazy
, int consider_profiling
)
74 struct textrels
*next
;
76 /* Initialize it to make the compiler happy. */
77 const char *errstring
= NULL
;
82 /* If DT_BIND_NOW is set relocate all references in this object. We
83 do not do this if we are profiling, of course. */
84 if (!consider_profiling
85 && __builtin_expect (l
->l_info
[DT_BIND_NOW
] != NULL
, 0))
88 if (__builtin_expect (GL(dl_debug_mask
) & DL_DEBUG_RELOC
, 0))
89 INTUSE(_dl_debug_printf
) ("\nrelocation processing: %s%s\n",
90 l
->l_name
[0] ? l
->l_name
: rtld_progname
,
91 lazy
? " (lazy)" : "");
93 /* DT_TEXTREL is now in level 2 and might phase out at some time.
94 But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make
95 testing easier and therefore it will be available at all time. */
96 if (__builtin_expect (l
->l_info
[DT_TEXTREL
] != NULL
, 0))
98 /* Bletch. We must make read-only segments writable
99 long enough to relocate them. */
100 const ElfW(Phdr
) *ph
;
101 for (ph
= l
->l_phdr
; ph
< &l
->l_phdr
[l
->l_phnum
]; ++ph
)
102 if (ph
->p_type
== PT_LOAD
&& (ph
->p_flags
& PF_W
) == 0)
104 struct textrels
*newp
;
106 newp
= (struct textrels
*) alloca (sizeof (*newp
));
107 newp
->len
= (((ph
->p_vaddr
+ ph
->p_memsz
+ GL(dl_pagesize
) - 1)
108 & ~(GL(dl_pagesize
) - 1))
109 - (ph
->p_vaddr
& ~(GL(dl_pagesize
) - 1)));
110 newp
->start
= ((ph
->p_vaddr
& ~(GL(dl_pagesize
) - 1))
111 + (caddr_t
) l
->l_addr
);
113 if (__mprotect (newp
->start
, newp
->len
, PROT_READ
|PROT_WRITE
) < 0)
115 errstring
= N_("cannot make segment writable for relocation");
117 INTUSE(_dl_signal_error
) (errno
, l
->l_name
, NULL
, errstring
);
120 #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
121 newp
->prot
= (PF_TO_PROT
122 >> ((ph
->p_flags
& (PF_R
| PF_W
| PF_X
)) * 4)) & 0xf;
125 if (ph
->p_flags
& PF_R
)
126 newp
->prot
|= PROT_READ
;
127 if (ph
->p_flags
& PF_W
)
128 newp
->prot
|= PROT_WRITE
;
129 if (ph
->p_flags
& PF_X
)
130 newp
->prot
|= PROT_EXEC
;
132 newp
->next
= textrels
;
138 /* Do the actual relocation of the object's GOT and other data. */
140 /* String table object symbols. */
141 const char *strtab
= (const void *) D_PTR (l
, l_info
[DT_STRTAB
]);
143 /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
144 #define RESOLVE_MAP(ref, version, r_type) \
145 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
146 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
147 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
148 ? (bump_num_cache_relocations (), \
149 (*ref) = l->l_lookup_cache.ret, \
150 l->l_lookup_cache.value) \
152 int _tc = elf_machine_type_class (r_type); \
153 l->l_lookup_cache.type_class = _tc; \
154 l->l_lookup_cache.sym = (*ref); \
155 _lr = ((version) != NULL && (version)->hash != 0 \
156 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
160 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
162 DL_LOOKUP_ADD_DEPENDENCY)); \
163 l->l_lookup_cache.ret = (*ref); \
164 l->l_lookup_cache.value = _lr; })) \
166 #define RESOLVE(ref, version, r_type) \
167 (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \
168 ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \
169 && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \
170 ? (bump_num_cache_relocations (), \
171 (*ref) = l->l_lookup_cache.ret, \
172 l->l_lookup_cache.value) \
174 int _tc = elf_machine_type_class (r_type); \
175 l->l_lookup_cache.type_class = _tc; \
176 l->l_lookup_cache.sym = (*ref); \
177 _lr = ((version) != NULL && (version)->hash != 0 \
178 ? INTUSE(_dl_lookup_versioned_symbol) (strtab \
182 : INTUSE(_dl_lookup_symbol) (strtab + (*ref)->st_name, l, \
184 DL_LOOKUP_ADD_DEPENDENCY)); \
185 l->l_lookup_cache.ret = (*ref); \
186 l->l_lookup_cache.value = _lr; })) \
189 /* This macro is used as a callback from elf_machine_rel{a,} when a
190 static TLS reloc is about to be performed. Since (in dl-load.c) we
191 permit dynamic loading of objects that might use such relocs, we
192 have to check whether each use is actually doable. If the object
193 whose TLS segment the reference resolves to was allocated space in
194 the static TLS block at startup, then it's ok. Otherwise, we make
195 an attempt to allocate it in surplus space on the fly. If that
196 can't be done, we fall back to the error that DF_STATIC_TLS is
197 intended to produce. */
198 #define CHECK_STATIC_TLS(map, sym_map) \
200 if (__builtin_expect ((sym_map)->l_tls_offset == 0, 0) \
201 && !allocate_static_tls (sym_map)) \
203 errstring = N_("shared object cannot be dlopen()ed"); \
204 INTUSE(_dl_signal_error) (0, (map)->l_name, NULL, errstring); \
208 #include "dynamic-link.h"
210 ELF_DYNAMIC_RELOCATE (l
, lazy
, consider_profiling
);
212 if (__builtin_expect (consider_profiling
, 0))
214 /* Allocate the array which will contain the already found
215 relocations. If the shared object lacks a PLT (for example
216 if it only contains lead function) the l_info[DT_PLTRELSZ]
218 if (l
->l_info
[DT_PLTRELSZ
] == NULL
)
220 errstring
= N_("%s: profiler found no PLTREL in object %s\n");
222 _dl_fatal_printf (errstring
,
223 rtld_progname
?: "<program name unknown>",
228 (ElfW(Addr
) *) calloc (sizeof (ElfW(Addr
)),
229 l
->l_info
[DT_PLTRELSZ
]->d_un
.d_val
);
230 if (l
->l_reloc_result
== NULL
)
233 %s: profiler out of memory shadowing PLTREL of %s\n");
239 /* Mark the object so we know this work has been done. */
242 /* Undo the segment protection changes. */
243 while (__builtin_expect (textrels
!= NULL
, 0))
245 if (__mprotect (textrels
->start
, textrels
->len
, textrels
->prot
) < 0)
247 errstring
= N_("cannot restore segment prot after reloc");
251 textrels
= textrels
->next
;
254 INTDEF (_dl_relocate_object
)
259 _dl_reloc_bad_type (struct link_map
*map
, unsigned int type
, int plt
)
261 extern const char INTUSE(_itoa_lower_digits
)[] attribute_hidden
;
262 #define DIGIT(b) INTUSE(_itoa_lower_digits)[(b) & 0xf];
264 /* XXX We cannot translate these messages. */
265 static const char msg
[2][32
266 #if __ELF_NATIVE_CLASS == 64
269 ] = { "unexpected reloc type 0x",
270 "unexpected PLT reloc type 0x" };
271 char msgbuf
[sizeof (msg
[0])];
274 cp
= __stpcpy (msgbuf
, msg
[plt
]);
275 #if __ELF_NATIVE_CLASS == 64
276 if (__builtin_expect(type
> 0xff, 0))
278 *cp
++ = DIGIT (type
>> 28);
279 *cp
++ = DIGIT (type
>> 24);
280 *cp
++ = DIGIT (type
>> 20);
281 *cp
++ = DIGIT (type
>> 16);
282 *cp
++ = DIGIT (type
>> 12);
283 *cp
++ = DIGIT (type
>> 8);
286 *cp
++ = DIGIT (type
>> 4);
287 *cp
++ = DIGIT (type
);
290 INTUSE(_dl_signal_error
) (0, map
->l_name
, NULL
, msgbuf
);