]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-minimal.c
1168ac786edc2d97a3ec5d97661fe4b22481747b
[thirdparty/glibc.git] / elf / dl-minimal.c
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-2020 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 <https://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <tls.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <ldsodefs.h>
29 #include <dl-irel.h>
30 #include <dl-hash.h>
31 #include <dl-sym-post.h>
32 #include <_itoa.h>
33 #include <malloc/malloc-internal.h>
34
35 #include <assert.h>
36
37 /* The rtld startup code calls __rtld_malloc_init_stubs after the
38 first self-relocation to adjust the pointers to the minimal
39 implementation below. Before the final relocation,
40 __rtld_malloc_init_real is called to replace the pointers with the
41 real implementation. */
42 __typeof (calloc) *__rtld_calloc attribute_relro;
43 __typeof (free) *__rtld_free attribute_relro;
44 __typeof (malloc) *__rtld_malloc attribute_relro;
45 __typeof (realloc) *__rtld_realloc attribute_relro;
46
47 /* Defined below. */
48 static __typeof (calloc) rtld_calloc;
49 static __typeof (free) rtld_free;
50 static __typeof (malloc) rtld_malloc;
51 static __typeof (realloc) rtld_realloc;
52
53 void
54 __rtld_malloc_init_stubs (void)
55 {
56 __rtld_calloc = &rtld_calloc;
57 __rtld_free = &rtld_free;
58 __rtld_malloc = &rtld_malloc;
59 __rtld_realloc = &rtld_realloc;
60 }
61
62 bool
63 __rtld_malloc_is_complete (void)
64 {
65 /* The caller assumes that there is an active malloc. */
66 assert (__rtld_malloc != NULL);
67 return __rtld_malloc != &rtld_malloc;
68 }
69
70 /* Lookup NAME at VERSION in the scope of MATCH. */
71 static void *
72 lookup_malloc_symbol (struct link_map *main_map, const char *name,
73 struct r_found_version *version)
74 {
75
76 const ElfW(Sym) *ref = NULL;
77 lookup_t result = _dl_lookup_symbol_x (name, main_map, &ref,
78 main_map->l_scope,
79 version, 0, 0, NULL);
80
81 assert (ELFW(ST_TYPE) (ref->st_info) != STT_TLS);
82 void *value = DL_SYMBOL_ADDRESS (result, ref);
83
84 return _dl_sym_post (result, ref, value, 0, main_map);
85 }
86
87 void
88 __rtld_malloc_init_real (struct link_map *main_map)
89 {
90 /* We cannot use relocations and initializers for this because the
91 changes made by __rtld_malloc_init_stubs break REL-style
92 (non-RELA) relocations that depend on the previous pointer
93 contents. Also avoid direct relocation depedencies for the
94 malloc symbols so this function can be called before the final
95 rtld relocation (which enables RELRO, after which the pointer
96 variables cannot be written to). */
97
98 struct r_found_version version;
99 version.name = symbol_version_string (libc, GLIBC_2_0);
100 version.hidden = 0;
101 version.hash = _dl_elf_hash (version.name);
102 version.filename = NULL;
103
104 void *new_calloc = lookup_malloc_symbol (main_map, "calloc", &version);
105 void *new_free = lookup_malloc_symbol (main_map, "free", &version);
106 void *new_malloc = lookup_malloc_symbol (main_map, "malloc", &version);
107 void *new_realloc = lookup_malloc_symbol (main_map, "realloc", &version);
108
109 /* Update the pointers in one go, so that any internal allocations
110 performed by lookup_malloc_symbol see a consistent
111 implementation. */
112 __rtld_calloc = new_calloc;
113 __rtld_free = new_free;
114 __rtld_malloc = new_malloc;
115 __rtld_realloc = new_realloc;
116 }
117
118 /* Minimal malloc allocator for used during initial link. After the
119 initial link, a full malloc implementation is interposed, either
120 the one in libc, or a different one supplied by the user through
121 interposition. */
122
123 static void *alloc_ptr, *alloc_end, *alloc_last_block;
124
125 /* Allocate an aligned memory block. */
126 static void *
127 rtld_malloc (size_t n)
128 {
129 if (alloc_end == 0)
130 {
131 /* Consume any unused space in the last page of our data segment. */
132 extern int _end attribute_hidden;
133 alloc_ptr = &_end;
134 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
135 + GLRO(dl_pagesize) - 1)
136 & ~(GLRO(dl_pagesize) - 1));
137 }
138
139 /* Make sure the allocation pointer is ideally aligned. */
140 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + MALLOC_ALIGNMENT - 1)
141 & ~(MALLOC_ALIGNMENT - 1));
142
143 if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
144 {
145 /* Insufficient space left; allocate another page plus one extra
146 page to reduce number of mmap calls. */
147 caddr_t page;
148 size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
149 if (__glibc_unlikely (nup == 0 && n != 0))
150 return NULL;
151 nup += GLRO(dl_pagesize);
152 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
153 MAP_ANON|MAP_PRIVATE, -1, 0);
154 if (page == MAP_FAILED)
155 return NULL;
156 if (page != alloc_end)
157 alloc_ptr = page;
158 alloc_end = page + nup;
159 }
160
161 alloc_last_block = (void *) alloc_ptr;
162 alloc_ptr += n;
163 return alloc_last_block;
164 }
165
166 /* We use this function occasionally since the real implementation may
167 be optimized when it can assume the memory it returns already is
168 set to NUL. */
169 static void *
170 rtld_calloc (size_t nmemb, size_t size)
171 {
172 /* New memory from the trivial malloc above is always already cleared.
173 (We make sure that's true in the rare occasion it might not be,
174 by clearing memory in free, below.) */
175 size_t bytes = nmemb * size;
176
177 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
178 if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
179 && size != 0 && bytes / size != nmemb)
180 return NULL;
181
182 return malloc (bytes);
183 }
184
185 /* This will rarely be called. */
186 void
187 rtld_free (void *ptr)
188 {
189 /* We can free only the last block allocated. */
190 if (ptr == alloc_last_block)
191 {
192 /* Since this is rare, we clear the freed block here
193 so that calloc can presume malloc returns cleared memory. */
194 memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block);
195 alloc_ptr = alloc_last_block;
196 }
197 }
198
199 /* This is only called with the most recent block returned by malloc. */
200 void *
201 rtld_realloc (void *ptr, size_t n)
202 {
203 if (ptr == NULL)
204 return malloc (n);
205 assert (ptr == alloc_last_block);
206 size_t old_size = alloc_ptr - alloc_last_block;
207 alloc_ptr = alloc_last_block;
208 void *new = malloc (n);
209 return new != ptr ? memcpy (new, ptr, old_size) : new;
210 }
211 \f
212 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
213
214 #include <setjmp.h>
215
216 int weak_function
217 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
218 {
219 env[0].__mask_was_saved = 0;
220 return 0;
221 }
222 \f
223 /* Define our own version of the internal function used by strerror. We
224 only provide the messages for some common errors. This avoids pulling
225 in the whole error list. */
226
227 char * weak_function
228 __strerror_r (int errnum, char *buf, size_t buflen)
229 {
230 char *msg;
231
232 switch (errnum)
233 {
234 case ENOMEM:
235 msg = (char *) "Cannot allocate memory";
236 break;
237 case EINVAL:
238 msg = (char *) "Invalid argument";
239 break;
240 case ENOENT:
241 msg = (char *) "No such file or directory";
242 break;
243 case EPERM:
244 msg = (char *) "Operation not permitted";
245 break;
246 case EIO:
247 msg = (char *) "Input/output error";
248 break;
249 case EACCES:
250 msg = (char *) "Permission denied";
251 break;
252 default:
253 /* No need to check buffer size, all calls in the dynamic linker
254 provide enough space. */
255 buf[buflen - 1] = '\0';
256 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
257 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
258 sizeof ("Error ") - 1);
259 break;
260 }
261
262 return msg;
263 }
264 \f
265 void
266 __libc_fatal (const char *message)
267 {
268 _dl_fatal_printf ("%s", message);
269 }
270 rtld_hidden_def (__libc_fatal)
271
272 void
273 __attribute__ ((noreturn))
274 __chk_fail (void)
275 {
276 _exit (127);
277 }
278 rtld_hidden_def (__chk_fail)
279
280 #ifndef NDEBUG
281 /* Define (weakly) our own assert failure function which doesn't use stdio.
282 If we are linked into the user program (-ldl), the normal __assert_fail
283 defn can override this one. */
284
285 void weak_function
286 __assert_fail (const char *assertion,
287 const char *file, unsigned int line, const char *function)
288 {
289 _dl_fatal_printf ("\
290 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
291 file, line, function ?: "", function ? ": " : "",
292 assertion);
293
294 }
295 # ifndef NO_RTLD_HIDDEN
296 rtld_hidden_weak (__assert_fail)
297 # endif
298
299 void weak_function
300 __assert_perror_fail (int errnum,
301 const char *file, unsigned int line,
302 const char *function)
303 {
304 char errbuf[400];
305 _dl_fatal_printf ("\
306 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
307 file, line, function ?: "", function ? ": " : "",
308 __strerror_r (errnum, errbuf, sizeof errbuf));
309
310 }
311 # ifndef NO_RTLD_HIDDEN
312 rtld_hidden_weak (__assert_perror_fail)
313 # endif
314 #endif
315 \f
316 #undef _itoa
317 /* We always use _itoa instead of _itoa_word in ld.so since the former
318 also has to be present and it is never about speed when these
319 functions are used. */
320 char *
321 _itoa (unsigned long long int value, char *buflim, unsigned int base,
322 int upper_case)
323 {
324 assert (! upper_case);
325
326 do
327 *--buflim = _itoa_lower_digits[value % base];
328 while ((value /= base) != 0);
329
330 return buflim;
331 }
332
333 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
334 up to 36. We don't need this here. */
335 const char _itoa_lower_digits[16] = "0123456789abcdef";
336 rtld_hidden_data_def (_itoa_lower_digits)
337 \f
338 /* The following is not a complete strsep implementation. It cannot
339 handle empty delimiter strings. But this isn't necessary for the
340 execution of ld.so. */
341 #undef strsep
342 #undef __strsep
343 char *
344 __strsep (char **stringp, const char *delim)
345 {
346 char *begin;
347
348 assert (delim[0] != '\0');
349
350 begin = *stringp;
351 if (begin != NULL)
352 {
353 char *end = begin;
354
355 while (*end != '\0' || (end = NULL))
356 {
357 const char *dp = delim;
358
359 do
360 if (*dp == *end)
361 break;
362 while (*++dp != '\0');
363
364 if (*dp != '\0')
365 {
366 *end++ = '\0';
367 break;
368 }
369
370 ++end;
371 }
372
373 *stringp = end;
374 }
375
376 return begin;
377 }
378 weak_alias (__strsep, strsep)
379 strong_alias (__strsep, __strsep_g)