Fix powerpc fmax, fmin sNaN handling (bug 20947).
[glibc.git] / elf / dl-minimal.c
blob116ec4978c2a28de1df79e640b23470942bfefd9
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 used during initial link. After the
35 initial link, a full malloc implementation is interposed, either
36 the one in libc, or a different one supplied by the user through
37 interposition. */
39 static void *alloc_ptr, *alloc_end, *alloc_last_block;
41 /* Declarations of global functions. */
42 extern void weak_function free (void *ptr);
43 extern void * weak_function realloc (void *ptr, size_t n);
44 extern unsigned long int weak_function __strtoul_internal (const char *nptr,
45 char **endptr,
46 int base,
47 int group);
48 extern unsigned long int weak_function strtoul (const char *nptr,
49 char **endptr, int base);
52 /* Allocate an aligned memory block. */
53 void * weak_function
54 malloc (size_t n)
56 if (alloc_end == 0)
58 /* Consume any unused space in the last page of our data segment. */
59 extern int _end attribute_hidden;
60 alloc_ptr = &_end;
61 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
62 + GLRO(dl_pagesize) - 1)
63 & ~(GLRO(dl_pagesize) - 1));
66 /* Make sure the allocation pointer is ideally aligned. */
67 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + MALLOC_ALIGNMENT - 1)
68 & ~(MALLOC_ALIGNMENT - 1));
70 if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
72 /* Insufficient space left; allocate another page plus one extra
73 page to reduce number of mmap calls. */
74 caddr_t page;
75 size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
76 if (__glibc_unlikely (nup == 0 && n != 0))
77 return NULL;
78 nup += GLRO(dl_pagesize);
79 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
80 MAP_ANON|MAP_PRIVATE, -1, 0);
81 if (page == MAP_FAILED)
82 return NULL;
83 if (page != alloc_end)
84 alloc_ptr = page;
85 alloc_end = page + nup;
88 alloc_last_block = (void *) alloc_ptr;
89 alloc_ptr += n;
90 return alloc_last_block;
93 /* We use this function occasionally since the real implementation may
94 be optimized when it can assume the memory it returns already is
95 set to NUL. */
96 void * weak_function
97 calloc (size_t nmemb, size_t size)
99 /* New memory from the trivial malloc above is always already cleared.
100 (We make sure that's true in the rare occasion it might not be,
101 by clearing memory in free, below.) */
102 size_t bytes = nmemb * size;
104 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
105 if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
106 && size != 0 && bytes / size != nmemb)
107 return NULL;
109 return malloc (bytes);
112 /* This will rarely be called. */
113 void weak_function
114 free (void *ptr)
116 /* We can free only the last block allocated. */
117 if (ptr == alloc_last_block)
119 /* Since this is rare, we clear the freed block here
120 so that calloc can presume malloc returns cleared memory. */
121 memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block);
122 alloc_ptr = alloc_last_block;
126 /* This is only called with the most recent block returned by malloc. */
127 void * weak_function
128 realloc (void *ptr, size_t n)
130 if (ptr == NULL)
131 return malloc (n);
132 assert (ptr == alloc_last_block);
133 size_t old_size = alloc_ptr - alloc_last_block;
134 alloc_ptr = alloc_last_block;
135 void *new = malloc (n);
136 return new != ptr ? memcpy (new, ptr, old_size) : new;
139 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
141 #include <setjmp.h>
143 int weak_function
144 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
146 env[0].__mask_was_saved = 0;
147 return 0;
150 /* Define our own version of the internal function used by strerror. We
151 only provide the messages for some common errors. This avoids pulling
152 in the whole error list. */
154 char * weak_function
155 __strerror_r (int errnum, char *buf, size_t buflen)
157 char *msg;
159 switch (errnum)
161 case ENOMEM:
162 msg = (char *) "Cannot allocate memory";
163 break;
164 case EINVAL:
165 msg = (char *) "Invalid argument";
166 break;
167 case ENOENT:
168 msg = (char *) "No such file or directory";
169 break;
170 case EPERM:
171 msg = (char *) "Operation not permitted";
172 break;
173 case EIO:
174 msg = (char *) "Input/output error";
175 break;
176 case EACCES:
177 msg = (char *) "Permission denied";
178 break;
179 default:
180 /* No need to check buffer size, all calls in the dynamic linker
181 provide enough space. */
182 buf[buflen - 1] = '\0';
183 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
184 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
185 sizeof ("Error ") - 1);
186 break;
189 return msg;
192 void
193 __libc_fatal (const char *message)
195 _dl_fatal_printf ("%s", message);
197 rtld_hidden_def (__libc_fatal)
199 void
200 __attribute__ ((noreturn))
201 __chk_fail (void)
203 _exit (127);
205 rtld_hidden_def (__chk_fail)
207 #ifndef NDEBUG
208 /* Define (weakly) our own assert failure function which doesn't use stdio.
209 If we are linked into the user program (-ldl), the normal __assert_fail
210 defn can override this one. */
212 void weak_function
213 __assert_fail (const char *assertion,
214 const char *file, unsigned int line, const char *function)
216 _dl_fatal_printf ("\
217 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
218 file, line, function ?: "", function ? ": " : "",
219 assertion);
222 rtld_hidden_weak (__assert_fail)
224 void weak_function
225 __assert_perror_fail (int errnum,
226 const char *file, unsigned int line,
227 const char *function)
229 char errbuf[400];
230 _dl_fatal_printf ("\
231 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
232 file, line, function ?: "", function ? ": " : "",
233 __strerror_r (errnum, errbuf, sizeof errbuf));
236 rtld_hidden_weak (__assert_perror_fail)
237 #endif
239 unsigned long int weak_function
240 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
242 unsigned long int result = 0;
243 long int sign = 1;
244 unsigned max_digit;
246 while (*nptr == ' ' || *nptr == '\t')
247 ++nptr;
249 if (*nptr == '-')
251 sign = -1;
252 ++nptr;
254 else if (*nptr == '+')
255 ++nptr;
257 if (*nptr < '0' || *nptr > '9')
259 if (endptr != NULL)
260 *endptr = (char *) nptr;
261 return 0UL;
264 assert (base == 0);
265 base = 10;
266 max_digit = 9;
267 if (*nptr == '0')
269 if (nptr[1] == 'x' || nptr[1] == 'X')
271 base = 16;
272 nptr += 2;
274 else
276 base = 8;
277 max_digit = 7;
281 while (1)
283 unsigned long int digval;
284 if (*nptr >= '0' && *nptr <= '0' + max_digit)
285 digval = *nptr - '0';
286 else if (base == 16)
288 if (*nptr >= 'a' && *nptr <= 'f')
289 digval = *nptr - 'a' + 10;
290 else if (*nptr >= 'A' && *nptr <= 'F')
291 digval = *nptr - 'A' + 10;
292 else
293 break;
295 else
296 break;
298 if (result > ULONG_MAX / base
299 || (result == ULONG_MAX / base && digval > ULONG_MAX % base))
301 errno = ERANGE;
302 if (endptr != NULL)
303 *endptr = (char *) nptr;
304 return ULONG_MAX;
306 result *= base;
307 result += digval;
308 ++nptr;
311 if (endptr != NULL)
312 *endptr = (char *) nptr;
313 return result * sign;
317 #undef _itoa
318 /* We always use _itoa instead of _itoa_word in ld.so since the former
319 also has to be present and it is never about speed when these
320 functions are used. */
321 char *
322 _itoa (unsigned long long int value, char *buflim, unsigned int base,
323 int upper_case)
325 assert (! upper_case);
328 *--buflim = _itoa_lower_digits[value % base];
329 while ((value /= base) != 0);
331 return buflim;
334 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
335 up to 36. We don't need this here. */
336 const char _itoa_lower_digits[16] = "0123456789abcdef";
337 rtld_hidden_data_def (_itoa_lower_digits)
339 /* The following is not a complete strsep implementation. It cannot
340 handle empty delimiter strings. But this isn't necessary for the
341 execution of ld.so. */
342 #undef strsep
343 #undef __strsep
344 char *
345 __strsep (char **stringp, const char *delim)
347 char *begin;
349 assert (delim[0] != '\0');
351 begin = *stringp;
352 if (begin != NULL)
354 char *end = begin;
356 while (*end != '\0' || (end = NULL))
358 const char *dp = delim;
361 if (*dp == *end)
362 break;
363 while (*++dp != '\0');
365 if (*dp != '\0')
367 *end++ = '\0';
368 break;
371 ++end;
374 *stringp = end;
377 return begin;
379 weak_alias (__strsep, strsep)
380 strong_alias (__strsep, __strsep_g)