1 | /* Relocate a shared object and resolve its references to other loaded objects. |
2 | Copyright (C) 1995-2019 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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. |
9 | |
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. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <libintl.h> |
21 | #include <stdlib.h> |
22 | #include <unistd.h> |
23 | #include <ldsodefs.h> |
24 | #include <sys/mman.h> |
25 | #include <sys/param.h> |
26 | #include <sys/types.h> |
27 | #include <_itoa.h> |
28 | #include <libc-pointer-arith.h> |
29 | #include "dynamic-link.h" |
30 | |
31 | /* Statistics function. */ |
32 | #ifdef SHARED |
33 | # define bump_num_cache_relocations() ++GL(dl_num_cache_relocations) |
34 | #else |
35 | # define bump_num_cache_relocations() ((void) 0) |
36 | #endif |
37 | |
38 | |
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. |
44 | This function intentionally does not return any value but signals error |
45 | directly, as static TLS should be rare and code handling it should |
46 | not be inlined as much as possible. */ |
47 | int |
48 | _dl_try_allocate_static_tls (struct link_map *map) |
49 | { |
50 | /* If we've already used the variable with dynamic access, or if the |
51 | alignment requirements are too high, fail. */ |
52 | if (map->l_tls_offset == FORCED_DYNAMIC_TLS_OFFSET |
53 | || map->l_tls_align > GL(dl_tls_static_align)) |
54 | { |
55 | fail: |
56 | return -1; |
57 | } |
58 | |
59 | #if TLS_TCB_AT_TP |
60 | size_t freebytes = GL(dl_tls_static_size) - GL(dl_tls_static_used); |
61 | if (freebytes < TLS_TCB_SIZE) |
62 | goto fail; |
63 | freebytes -= TLS_TCB_SIZE; |
64 | |
65 | size_t blsize = map->l_tls_blocksize + map->l_tls_firstbyte_offset; |
66 | if (freebytes < blsize) |
67 | goto fail; |
68 | |
69 | size_t n = (freebytes - blsize) / map->l_tls_align; |
70 | |
71 | size_t offset = GL(dl_tls_static_used) + (freebytes - n * map->l_tls_align |
72 | - map->l_tls_firstbyte_offset); |
73 | |
74 | map->l_tls_offset = GL(dl_tls_static_used) = offset; |
75 | #elif TLS_DTV_AT_TP |
76 | /* dl_tls_static_used includes the TCB at the beginning. */ |
77 | size_t offset = (ALIGN_UP(GL(dl_tls_static_used) |
78 | - map->l_tls_firstbyte_offset, |
79 | map->l_tls_align) |
80 | + map->l_tls_firstbyte_offset); |
81 | size_t used = offset + map->l_tls_blocksize; |
82 | |
83 | if (used > GL(dl_tls_static_size)) |
84 | goto fail; |
85 | |
86 | map->l_tls_offset = offset; |
87 | map->l_tls_firstbyte_offset = GL(dl_tls_static_used); |
88 | GL(dl_tls_static_used) = used; |
89 | #else |
90 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" |
91 | #endif |
92 | |
93 | /* If the object is not yet relocated we cannot initialize the |
94 | static TLS region. Delay it. */ |
95 | if (map->l_real->l_relocated) |
96 | { |
97 | #ifdef SHARED |
98 | if (__builtin_expect (THREAD_DTV()[0].counter != GL(dl_tls_generation), |
99 | 0)) |
100 | /* Update the slot information data for at least the generation of |
101 | the DSO we are allocating data for. */ |
102 | (void) _dl_update_slotinfo (map->l_tls_modid); |
103 | #endif |
104 | |
105 | GL(dl_init_static_tls) (map); |
106 | } |
107 | else |
108 | map->l_need_tls_init = 1; |
109 | |
110 | return 0; |
111 | } |
112 | |
113 | void |
114 | __attribute_noinline__ |
115 | _dl_allocate_static_tls (struct link_map *map) |
116 | { |
117 | if (map->l_tls_offset == FORCED_DYNAMIC_TLS_OFFSET |
118 | || _dl_try_allocate_static_tls (map)) |
119 | { |
120 | _dl_signal_error (0, map->l_name, NULL, N_("\ |
121 | cannot allocate memory in static TLS block" )); |
122 | } |
123 | } |
124 | |
125 | /* Initialize static TLS area and DTV for current (only) thread. |
126 | libpthread implementations should provide their own hook |
127 | to handle all threads. */ |
128 | void |
129 | _dl_nothread_init_static_tls (struct link_map *map) |
130 | { |
131 | #if TLS_TCB_AT_TP |
132 | void *dest = (char *) THREAD_SELF - map->l_tls_offset; |
133 | #elif TLS_DTV_AT_TP |
134 | void *dest = (char *) THREAD_SELF + map->l_tls_offset + TLS_PRE_TCB_SIZE; |
135 | #else |
136 | # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" |
137 | #endif |
138 | |
139 | /* Initialize the memory. */ |
140 | memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size), |
141 | '\0', map->l_tls_blocksize - map->l_tls_initimage_size); |
142 | } |
143 | |
144 | |
145 | void |
146 | _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], |
147 | int reloc_mode, int consider_profiling) |
148 | { |
149 | struct textrels |
150 | { |
151 | caddr_t start; |
152 | size_t len; |
153 | int prot; |
154 | struct textrels *next; |
155 | } *textrels = NULL; |
156 | /* Initialize it to make the compiler happy. */ |
157 | const char *errstring = NULL; |
158 | int lazy = reloc_mode & RTLD_LAZY; |
159 | int skip_ifunc = reloc_mode & __RTLD_NOIFUNC; |
160 | |
161 | #ifdef SHARED |
162 | /* If we are auditing, install the same handlers we need for profiling. */ |
163 | if ((reloc_mode & __RTLD_AUDIT) == 0) |
164 | consider_profiling |= GLRO(dl_audit) != NULL; |
165 | #elif defined PROF |
166 | /* Never use dynamic linker profiling for gprof profiling code. */ |
167 | # define consider_profiling 0 |
168 | #endif |
169 | |
170 | if (l->l_relocated) |
171 | return; |
172 | |
173 | /* If DT_BIND_NOW is set relocate all references in this object. We |
174 | do not do this if we are profiling, of course. */ |
175 | // XXX Correct for auditing? |
176 | if (!consider_profiling |
177 | && __builtin_expect (l->l_info[DT_BIND_NOW] != NULL, 0)) |
178 | lazy = 0; |
179 | |
180 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_RELOC)) |
181 | _dl_debug_printf ("\nrelocation processing: %s%s\n" , |
182 | DSO_FILENAME (l->l_name), lazy ? " (lazy)" : "" ); |
183 | |
184 | /* DT_TEXTREL is now in level 2 and might phase out at some time. |
185 | But we rewrite the DT_FLAGS entry to a DT_TEXTREL entry to make |
186 | testing easier and therefore it will be available at all time. */ |
187 | if (__glibc_unlikely (l->l_info[DT_TEXTREL] != NULL)) |
188 | { |
189 | /* Bletch. We must make read-only segments writable |
190 | long enough to relocate them. */ |
191 | const ElfW(Phdr) *ph; |
192 | for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph) |
193 | if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0) |
194 | { |
195 | struct textrels *newp; |
196 | |
197 | newp = (struct textrels *) alloca (sizeof (*newp)); |
198 | newp->len = ALIGN_UP (ph->p_vaddr + ph->p_memsz, GLRO(dl_pagesize)) |
199 | - ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize)); |
200 | newp->start = PTR_ALIGN_DOWN (ph->p_vaddr, GLRO(dl_pagesize)) |
201 | + (caddr_t) l->l_addr; |
202 | |
203 | newp->prot = 0; |
204 | if (ph->p_flags & PF_R) |
205 | newp->prot |= PROT_READ; |
206 | if (ph->p_flags & PF_W) |
207 | newp->prot |= PROT_WRITE; |
208 | if (ph->p_flags & PF_X) |
209 | newp->prot |= PROT_EXEC; |
210 | |
211 | if (__mprotect (newp->start, newp->len, newp->prot|PROT_WRITE) < 0) |
212 | { |
213 | errstring = N_("cannot make segment writable for relocation" ); |
214 | call_error: |
215 | _dl_signal_error (errno, l->l_name, NULL, errstring); |
216 | } |
217 | |
218 | newp->next = textrels; |
219 | textrels = newp; |
220 | } |
221 | } |
222 | |
223 | { |
224 | /* Do the actual relocation of the object's GOT and other data. */ |
225 | |
226 | /* String table object symbols. */ |
227 | const char *strtab = (const void *) D_PTR (l, l_info[DT_STRTAB]); |
228 | |
229 | /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */ |
230 | #define RESOLVE_MAP(ref, version, r_type) \ |
231 | ((ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL \ |
232 | && __glibc_likely (!dl_symbol_visibility_binds_local_p (*ref))) \ |
233 | ? ((__builtin_expect ((*ref) == l->l_lookup_cache.sym, 0) \ |
234 | && elf_machine_type_class (r_type) == l->l_lookup_cache.type_class) \ |
235 | ? (bump_num_cache_relocations (), \ |
236 | (*ref) = l->l_lookup_cache.ret, \ |
237 | l->l_lookup_cache.value) \ |
238 | : ({ lookup_t _lr; \ |
239 | int _tc = elf_machine_type_class (r_type); \ |
240 | l->l_lookup_cache.type_class = _tc; \ |
241 | l->l_lookup_cache.sym = (*ref); \ |
242 | const struct r_found_version *v = NULL; \ |
243 | if ((version) != NULL && (version)->hash != 0) \ |
244 | v = (version); \ |
245 | _lr = _dl_lookup_symbol_x (strtab + (*ref)->st_name, l, (ref), \ |
246 | scope, v, _tc, \ |
247 | DL_LOOKUP_ADD_DEPENDENCY, NULL); \ |
248 | l->l_lookup_cache.ret = (*ref); \ |
249 | l->l_lookup_cache.value = _lr; })) \ |
250 | : l) |
251 | |
252 | #include "dynamic-link.h" |
253 | |
254 | ELF_DYNAMIC_RELOCATE (l, lazy, consider_profiling, skip_ifunc); |
255 | |
256 | #ifndef PROF |
257 | if (__glibc_unlikely (consider_profiling) |
258 | && l->l_info[DT_PLTRELSZ] != NULL) |
259 | { |
260 | /* Allocate the array which will contain the already found |
261 | relocations. If the shared object lacks a PLT (for example |
262 | if it only contains lead function) the l_info[DT_PLTRELSZ] |
263 | will be NULL. */ |
264 | size_t sizeofrel = l->l_info[DT_PLTREL]->d_un.d_val == DT_RELA |
265 | ? sizeof (ElfW(Rela)) |
266 | : sizeof (ElfW(Rel)); |
267 | size_t relcount = l->l_info[DT_PLTRELSZ]->d_un.d_val / sizeofrel; |
268 | l->l_reloc_result = calloc (sizeof (l->l_reloc_result[0]), relcount); |
269 | |
270 | if (l->l_reloc_result == NULL) |
271 | { |
272 | errstring = N_("\ |
273 | %s: out of memory to store relocation results for %s\n" ); |
274 | _dl_fatal_printf (errstring, RTLD_PROGNAME, l->l_name); |
275 | } |
276 | } |
277 | #endif |
278 | } |
279 | |
280 | /* Mark the object so we know this work has been done. */ |
281 | l->l_relocated = 1; |
282 | |
283 | /* Undo the segment protection changes. */ |
284 | while (__builtin_expect (textrels != NULL, 0)) |
285 | { |
286 | if (__mprotect (textrels->start, textrels->len, textrels->prot) < 0) |
287 | { |
288 | errstring = N_("cannot restore segment prot after reloc" ); |
289 | goto call_error; |
290 | } |
291 | |
292 | #ifdef CLEAR_CACHE |
293 | CLEAR_CACHE (textrels->start, textrels->start + textrels->len); |
294 | #endif |
295 | |
296 | textrels = textrels->next; |
297 | } |
298 | |
299 | /* In case we can protect the data now that the relocations are |
300 | done, do it. */ |
301 | if (l->l_relro_size != 0) |
302 | _dl_protect_relro (l); |
303 | } |
304 | |
305 | |
306 | void |
307 | _dl_protect_relro (struct link_map *l) |
308 | { |
309 | ElfW(Addr) start = ALIGN_DOWN((l->l_addr |
310 | + l->l_relro_addr), |
311 | GLRO(dl_pagesize)); |
312 | ElfW(Addr) end = ALIGN_DOWN((l->l_addr |
313 | + l->l_relro_addr |
314 | + l->l_relro_size), |
315 | GLRO(dl_pagesize)); |
316 | if (start != end |
317 | && __mprotect ((void *) start, end - start, PROT_READ) < 0) |
318 | { |
319 | static const char errstring[] = N_("\ |
320 | cannot apply additional memory protection after relocation" ); |
321 | _dl_signal_error (errno, l->l_name, NULL, errstring); |
322 | } |
323 | } |
324 | |
325 | void |
326 | __attribute_noinline__ |
327 | _dl_reloc_bad_type (struct link_map *map, unsigned int type, int plt) |
328 | { |
329 | #define DIGIT(b) _itoa_lower_digits[(b) & 0xf]; |
330 | |
331 | /* XXX We cannot translate these messages. */ |
332 | static const char msg[2][32 |
333 | #if __ELF_NATIVE_CLASS == 64 |
334 | + 6 |
335 | #endif |
336 | ] = { "unexpected reloc type 0x" , |
337 | "unexpected PLT reloc type 0x" }; |
338 | char msgbuf[sizeof (msg[0])]; |
339 | char *cp; |
340 | |
341 | cp = __stpcpy (msgbuf, msg[plt]); |
342 | #if __ELF_NATIVE_CLASS == 64 |
343 | if (__builtin_expect(type > 0xff, 0)) |
344 | { |
345 | *cp++ = DIGIT (type >> 28); |
346 | *cp++ = DIGIT (type >> 24); |
347 | *cp++ = DIGIT (type >> 20); |
348 | *cp++ = DIGIT (type >> 16); |
349 | *cp++ = DIGIT (type >> 12); |
350 | *cp++ = DIGIT (type >> 8); |
351 | } |
352 | #endif |
353 | *cp++ = DIGIT (type >> 4); |
354 | *cp++ = DIGIT (type); |
355 | *cp = '\0'; |
356 | |
357 | _dl_signal_error (0, map->l_name, NULL, msgbuf); |
358 | } |
359 | |