Add sysdeps/ieee754/soft-fp.
[glibc.git] / elf / dl-minimal.c
blob59e159a0cd2cca84ce3c3ec4adf57238b3faaefb
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-2017 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);
46 /* Allocate an aligned memory block. */
47 void * weak_function
48 malloc (size_t n)
50 if (alloc_end == 0)
52 /* Consume any unused space in the last page of our data segment. */
53 extern int _end attribute_hidden;
54 alloc_ptr = &_end;
55 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
56 + GLRO(dl_pagesize) - 1)
57 & ~(GLRO(dl_pagesize) - 1));
60 /* Make sure the allocation pointer is ideally aligned. */
61 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + MALLOC_ALIGNMENT - 1)
62 & ~(MALLOC_ALIGNMENT - 1));
64 if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
66 /* Insufficient space left; allocate another page plus one extra
67 page to reduce number of mmap calls. */
68 caddr_t page;
69 size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
70 if (__glibc_unlikely (nup == 0 && n != 0))
71 return NULL;
72 nup += GLRO(dl_pagesize);
73 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
74 MAP_ANON|MAP_PRIVATE, -1, 0);
75 if (page == MAP_FAILED)
76 return NULL;
77 if (page != alloc_end)
78 alloc_ptr = page;
79 alloc_end = page + nup;
82 alloc_last_block = (void *) alloc_ptr;
83 alloc_ptr += n;
84 return alloc_last_block;
87 /* We use this function occasionally since the real implementation may
88 be optimized when it can assume the memory it returns already is
89 set to NUL. */
90 void * weak_function
91 calloc (size_t nmemb, size_t size)
93 /* New memory from the trivial malloc above is always already cleared.
94 (We make sure that's true in the rare occasion it might not be,
95 by clearing memory in free, below.) */
96 size_t bytes = nmemb * size;
98 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
99 if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
100 && size != 0 && bytes / size != nmemb)
101 return NULL;
103 return malloc (bytes);
106 /* This will rarely be called. */
107 void weak_function
108 free (void *ptr)
110 /* We can free only the last block allocated. */
111 if (ptr == alloc_last_block)
113 /* Since this is rare, we clear the freed block here
114 so that calloc can presume malloc returns cleared memory. */
115 memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block);
116 alloc_ptr = alloc_last_block;
120 /* This is only called with the most recent block returned by malloc. */
121 void * weak_function
122 realloc (void *ptr, size_t n)
124 if (ptr == NULL)
125 return malloc (n);
126 assert (ptr == alloc_last_block);
127 size_t old_size = alloc_ptr - alloc_last_block;
128 alloc_ptr = alloc_last_block;
129 void *new = malloc (n);
130 return new != ptr ? memcpy (new, ptr, old_size) : new;
133 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
135 #include <setjmp.h>
137 int weak_function
138 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
140 env[0].__mask_was_saved = 0;
141 return 0;
144 /* Define our own version of the internal function used by strerror. We
145 only provide the messages for some common errors. This avoids pulling
146 in the whole error list. */
148 char * weak_function
149 __strerror_r (int errnum, char *buf, size_t buflen)
151 char *msg;
153 switch (errnum)
155 case ENOMEM:
156 msg = (char *) "Cannot allocate memory";
157 break;
158 case EINVAL:
159 msg = (char *) "Invalid argument";
160 break;
161 case ENOENT:
162 msg = (char *) "No such file or directory";
163 break;
164 case EPERM:
165 msg = (char *) "Operation not permitted";
166 break;
167 case EIO:
168 msg = (char *) "Input/output error";
169 break;
170 case EACCES:
171 msg = (char *) "Permission denied";
172 break;
173 default:
174 /* No need to check buffer size, all calls in the dynamic linker
175 provide enough space. */
176 buf[buflen - 1] = '\0';
177 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
178 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
179 sizeof ("Error ") - 1);
180 break;
183 return msg;
186 void
187 __libc_fatal (const char *message)
189 _dl_fatal_printf ("%s", message);
191 rtld_hidden_def (__libc_fatal)
193 void
194 __attribute__ ((noreturn))
195 __chk_fail (void)
197 _exit (127);
199 rtld_hidden_def (__chk_fail)
201 #ifndef NDEBUG
202 /* Define (weakly) our own assert failure function which doesn't use stdio.
203 If we are linked into the user program (-ldl), the normal __assert_fail
204 defn can override this one. */
206 void weak_function
207 __assert_fail (const char *assertion,
208 const char *file, unsigned int line, const char *function)
210 _dl_fatal_printf ("\
211 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
212 file, line, function ?: "", function ? ": " : "",
213 assertion);
216 rtld_hidden_weak (__assert_fail)
218 void weak_function
219 __assert_perror_fail (int errnum,
220 const char *file, unsigned int line,
221 const char *function)
223 char errbuf[400];
224 _dl_fatal_printf ("\
225 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
226 file, line, function ?: "", function ? ": " : "",
227 __strerror_r (errnum, errbuf, sizeof errbuf));
230 rtld_hidden_weak (__assert_perror_fail)
231 #endif
233 #undef _itoa
234 /* We always use _itoa instead of _itoa_word in ld.so since the former
235 also has to be present and it is never about speed when these
236 functions are used. */
237 char *
238 _itoa (unsigned long long int value, char *buflim, unsigned int base,
239 int upper_case)
241 assert (! upper_case);
244 *--buflim = _itoa_lower_digits[value % base];
245 while ((value /= base) != 0);
247 return buflim;
250 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
251 up to 36. We don't need this here. */
252 const char _itoa_lower_digits[16] = "0123456789abcdef";
253 rtld_hidden_data_def (_itoa_lower_digits)
255 /* The following is not a complete strsep implementation. It cannot
256 handle empty delimiter strings. But this isn't necessary for the
257 execution of ld.so. */
258 #undef strsep
259 #undef __strsep
260 char *
261 __strsep (char **stringp, const char *delim)
263 char *begin;
265 assert (delim[0] != '\0');
267 begin = *stringp;
268 if (begin != NULL)
270 char *end = begin;
272 while (*end != '\0' || (end = NULL))
274 const char *dp = delim;
277 if (*dp == *end)
278 break;
279 while (*++dp != '\0');
281 if (*dp != '\0')
283 *end++ = '\0';
284 break;
287 ++end;
290 *stringp = end;
293 return begin;
295 weak_alias (__strsep, strsep)
296 strong_alias (__strsep, __strsep_g)