Fix ldbl-128ibm iscanonical for -mlong-double-64.
[glibc.git] / elf / dl-minimal.c
blob6034b5a3fa601299d94da88c6c821d707c10b063
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-2016 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, see
17 <http://www.gnu.org/licenses/>. */
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 <_itoa.h>
30 #include <malloc/malloc-internal.h>
32 #include <assert.h>
34 /* Minimal `malloc' allocator for use while loading shared libraries.
35 No block is ever freed. */
37 static void *alloc_ptr, *alloc_end, *alloc_last_block;
39 /* Declarations of global functions. */
40 extern void weak_function free (void *ptr);
41 extern void * weak_function realloc (void *ptr, size_t n);
42 extern unsigned long int weak_function __strtoul_internal (const char *nptr,
43 char **endptr,
44 int base,
45 int group);
46 extern unsigned long int weak_function strtoul (const char *nptr,
47 char **endptr, int base);
50 /* Allocate an aligned memory block. */
51 void * weak_function
52 __libc_memalign (size_t align, size_t n)
54 if (alloc_end == 0)
56 /* Consume any unused space in the last page of our data segment. */
57 extern int _end attribute_hidden;
58 alloc_ptr = &_end;
59 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
60 + GLRO(dl_pagesize) - 1)
61 & ~(GLRO(dl_pagesize) - 1));
64 /* Make sure the allocation pointer is ideally aligned. */
65 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + align - 1)
66 & ~(align - 1));
68 if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
70 /* Insufficient space left; allocate another page plus one extra
71 page to reduce number of mmap calls. */
72 caddr_t page;
73 size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
74 if (__glibc_unlikely (nup == 0 && n != 0))
75 return NULL;
76 nup += GLRO(dl_pagesize);
77 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
78 MAP_ANON|MAP_PRIVATE, -1, 0);
79 if (page == MAP_FAILED)
80 return NULL;
81 if (page != alloc_end)
82 alloc_ptr = page;
83 alloc_end = page + nup;
86 alloc_last_block = (void *) alloc_ptr;
87 alloc_ptr += n;
88 return alloc_last_block;
91 void * weak_function
92 malloc (size_t n)
94 return __libc_memalign (MALLOC_ALIGNMENT, n);
97 /* We use this function occasionally since the real implementation may
98 be optimized when it can assume the memory it returns already is
99 set to NUL. */
100 void * weak_function
101 calloc (size_t nmemb, size_t size)
103 /* New memory from the trivial malloc above is always already cleared.
104 (We make sure that's true in the rare occasion it might not be,
105 by clearing memory in free, below.) */
106 size_t bytes = nmemb * size;
108 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
109 if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
110 && size != 0 && bytes / size != nmemb)
111 return NULL;
113 return malloc (bytes);
116 /* This will rarely be called. */
117 void weak_function
118 free (void *ptr)
120 /* We can free only the last block allocated. */
121 if (ptr == alloc_last_block)
123 /* Since this is rare, we clear the freed block here
124 so that calloc can presume malloc returns cleared memory. */
125 memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block);
126 alloc_ptr = alloc_last_block;
130 /* This is only called with the most recent block returned by malloc. */
131 void * weak_function
132 realloc (void *ptr, size_t n)
134 if (ptr == NULL)
135 return malloc (n);
136 assert (ptr == alloc_last_block);
137 size_t old_size = alloc_ptr - alloc_last_block;
138 alloc_ptr = alloc_last_block;
139 void *new = malloc (n);
140 return new != ptr ? memcpy (new, ptr, old_size) : new;
143 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
145 #include <setjmp.h>
147 int weak_function
148 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
150 env[0].__mask_was_saved = 0;
151 return 0;
154 /* Define our own version of the internal function used by strerror. We
155 only provide the messages for some common errors. This avoids pulling
156 in the whole error list. */
158 char * weak_function
159 __strerror_r (int errnum, char *buf, size_t buflen)
161 char *msg;
163 switch (errnum)
165 case ENOMEM:
166 msg = (char *) "Cannot allocate memory";
167 break;
168 case EINVAL:
169 msg = (char *) "Invalid argument";
170 break;
171 case ENOENT:
172 msg = (char *) "No such file or directory";
173 break;
174 case EPERM:
175 msg = (char *) "Operation not permitted";
176 break;
177 case EIO:
178 msg = (char *) "Input/output error";
179 break;
180 case EACCES:
181 msg = (char *) "Permission denied";
182 break;
183 default:
184 /* No need to check buffer size, all calls in the dynamic linker
185 provide enough space. */
186 buf[buflen - 1] = '\0';
187 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
188 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
189 sizeof ("Error ") - 1);
190 break;
193 return msg;
196 void
197 __libc_fatal (const char *message)
199 _dl_fatal_printf ("%s", message);
201 rtld_hidden_def (__libc_fatal)
203 void
204 __attribute__ ((noreturn))
205 __chk_fail (void)
207 _exit (127);
209 rtld_hidden_def (__chk_fail)
211 #ifndef NDEBUG
212 /* Define (weakly) our own assert failure function which doesn't use stdio.
213 If we are linked into the user program (-ldl), the normal __assert_fail
214 defn can override this one. */
216 void weak_function
217 __assert_fail (const char *assertion,
218 const char *file, unsigned int line, const char *function)
220 _dl_fatal_printf ("\
221 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
222 file, line, function ?: "", function ? ": " : "",
223 assertion);
226 rtld_hidden_weak (__assert_fail)
228 void weak_function
229 __assert_perror_fail (int errnum,
230 const char *file, unsigned int line,
231 const char *function)
233 char errbuf[400];
234 _dl_fatal_printf ("\
235 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
236 file, line, function ?: "", function ? ": " : "",
237 __strerror_r (errnum, errbuf, sizeof errbuf));
240 rtld_hidden_weak (__assert_perror_fail)
241 #endif
243 unsigned long int weak_function
244 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
246 unsigned long int result = 0;
247 long int sign = 1;
248 unsigned max_digit;
250 while (*nptr == ' ' || *nptr == '\t')
251 ++nptr;
253 if (*nptr == '-')
255 sign = -1;
256 ++nptr;
258 else if (*nptr == '+')
259 ++nptr;
261 if (*nptr < '0' || *nptr > '9')
263 if (endptr != NULL)
264 *endptr = (char *) nptr;
265 return 0UL;
268 assert (base == 0);
269 base = 10;
270 max_digit = 9;
271 if (*nptr == '0')
273 if (nptr[1] == 'x' || nptr[1] == 'X')
275 base = 16;
276 nptr += 2;
278 else
280 base = 8;
281 max_digit = 7;
285 while (1)
287 unsigned long int digval;
288 if (*nptr >= '0' && *nptr <= '0' + max_digit)
289 digval = *nptr - '0';
290 else if (base == 16)
292 if (*nptr >= 'a' && *nptr <= 'f')
293 digval = *nptr - 'a' + 10;
294 else if (*nptr >= 'A' && *nptr <= 'F')
295 digval = *nptr - 'A' + 10;
296 else
297 break;
299 else
300 break;
302 if (result > ULONG_MAX / base
303 || (result == ULONG_MAX / base && digval > ULONG_MAX % base))
305 errno = ERANGE;
306 if (endptr != NULL)
307 *endptr = (char *) nptr;
308 return ULONG_MAX;
310 result *= base;
311 result += digval;
312 ++nptr;
315 if (endptr != NULL)
316 *endptr = (char *) nptr;
317 return result * sign;
321 #undef _itoa
322 /* We always use _itoa instead of _itoa_word in ld.so since the former
323 also has to be present and it is never about speed when these
324 functions are used. */
325 char *
326 _itoa (unsigned long long int value, char *buflim, unsigned int base,
327 int upper_case)
329 assert (! upper_case);
332 *--buflim = _itoa_lower_digits[value % base];
333 while ((value /= base) != 0);
335 return buflim;
338 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
339 up to 36. We don't need this here. */
340 const char _itoa_lower_digits[16] = "0123456789abcdef";
341 rtld_hidden_data_def (_itoa_lower_digits)
343 /* The following is not a complete strsep implementation. It cannot
344 handle empty delimiter strings. But this isn't necessary for the
345 execution of ld.so. */
346 #undef strsep
347 #undef __strsep
348 char *
349 __strsep (char **stringp, const char *delim)
351 char *begin;
353 assert (delim[0] != '\0');
355 begin = *stringp;
356 if (begin != NULL)
358 char *end = begin;
360 while (*end != '\0' || (end = NULL))
362 const char *dp = delim;
365 if (*dp == *end)
366 break;
367 while (*++dp != '\0');
369 if (*dp != '\0')
371 *end++ = '\0';
372 break;
375 ++end;
378 *stringp = end;
381 return begin;
383 weak_alias (__strsep, strsep)
384 strong_alias (__strsep, __strsep_g)