* sysdeps/powerpc/fpu/w_sqrt.c: Add sqrtl alias.
[glibc.git] / elf / dl-minimal.c
blob0f284d0a80d3c0f3435e1d5464705dc0ae6f9e11
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995,96,97,98,2000,2001,2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <limits.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/param.h>
26 #include <sys/types.h>
27 #include <ldsodefs.h>
28 #include <stdio-common/_itoa.h>
30 #include <assert.h>
32 /* Minimal `malloc' allocator for use while loading shared libraries.
33 No block is ever freed. */
35 static void *alloc_ptr, *alloc_end, *alloc_last_block;
37 /* Declarations of global functions. */
38 extern void weak_function free (void *ptr);
39 extern void * weak_function realloc (void *ptr, size_t n);
40 extern unsigned long int weak_function __strtoul_internal (const char *nptr,
41 char **endptr,
42 int base,
43 int group);
44 extern unsigned long int weak_function strtoul (const char *nptr,
45 char **endptr, int base);
48 void * weak_function
49 malloc (size_t n)
51 #ifdef MAP_ANON
52 #define _dl_zerofd (-1)
53 #else
54 extern int _dl_zerofd;
56 if (_dl_zerofd == -1)
57 _dl_zerofd = _dl_sysdep_open_zero_fill ();
58 #define MAP_ANON 0
59 #endif
61 if (alloc_end == 0)
63 /* Consume any unused space in the last page of our data segment. */
64 extern int _end attribute_hidden;
65 alloc_ptr = &_end;
66 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
67 + GL(dl_pagesize) - 1)
68 & ~(GL(dl_pagesize) - 1));
71 /* Make sure the allocation pointer is ideally aligned. */
72 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
73 & ~(sizeof (double) - 1));
75 if (alloc_ptr + n >= alloc_end)
77 /* Insufficient space left; allocate another page. */
78 caddr_t page;
79 size_t nup = (n + GL(dl_pagesize) - 1) & ~(GL(dl_pagesize) - 1);
80 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
81 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
82 assert (page != MAP_FAILED);
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 size_t total = nmemb * size;
100 void *result = malloc (total);
101 return memset (result, '\0', total);
104 /* This will rarely be called. */
105 void weak_function
106 free (void *ptr)
108 /* We can free only the last block allocated. */
109 if (ptr == alloc_last_block)
110 alloc_ptr = alloc_last_block;
113 /* This is only called with the most recent block returned by malloc. */
114 void * weak_function
115 realloc (void *ptr, size_t n)
117 void *new;
118 if (ptr == NULL)
119 return malloc (n);
120 assert (ptr == alloc_last_block);
121 alloc_ptr = alloc_last_block;
122 new = malloc (n);
123 assert (new == ptr);
124 return new;
127 /* Return alligned memory block. */
128 void * weak_function
129 __libc_memalign (size_t align, size_t n)
131 void *newp = malloc (n + align - 1);
133 return (void *) roundup ((uintptr_t) newp, align);
136 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
138 #include <setjmp.h>
140 int weak_function
141 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
143 env[0].__mask_was_saved = 0;
144 return 0;
147 void weak_function
148 longjmp (jmp_buf env, int val)
150 __longjmp (env[0].__jmpbuf, val);
153 /* Define our own version of the internal function used by strerror. We
154 only provide the messages for some common errors. This avoids pulling
155 in the whole error list. */
157 char * weak_function
158 __strerror_r (int errnum, char *buf, size_t buflen)
160 char *msg;
162 switch (errnum)
164 case ENOMEM:
165 msg = (char *) "Cannot allocate memory";
166 break;
167 case EINVAL:
168 msg = (char *) "Invalid argument";
169 break;
170 case ENOENT:
171 msg = (char *) "No such file or directory";
172 break;
173 case EPERM:
174 msg = (char *) "Operation not permitted";
175 break;
176 case EIO:
177 msg = (char *) "Input/output error";
178 break;
179 case EACCES:
180 msg = (char *) "Permission denied";
181 break;
182 default:
183 /* No need to check buffer size, all calls in the dynamic linker
184 provide enough space. */
185 buf[buflen - 1] = '\0';
186 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
187 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
188 sizeof ("Error ") - 1);
189 break;
192 return msg;
195 #ifndef NDEBUG
197 /* Define (weakly) our own assert failure function which doesn't use stdio.
198 If we are linked into the user program (-ldl), the normal __assert_fail
199 defn can override this one. */
201 void weak_function
202 __assert_fail (const char *assertion,
203 const char *file, unsigned int line, const char *function)
205 _dl_fatal_printf ("\
206 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
207 file, line, function ?: "", function ? ": " : "",
208 assertion);
212 void weak_function
213 __assert_perror_fail (int errnum,
214 const char *file, unsigned int line,
215 const char *function)
217 char errbuf[64];
218 _dl_fatal_printf ("\
219 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
220 file, line, function ?: "", function ? ": " : "",
221 __strerror_r (errnum, errbuf, sizeof (errbuf)));
224 #endif
226 unsigned long int weak_function
227 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
229 unsigned long int result = 0;
230 long int sign = 1;
232 while (*nptr == ' ' || *nptr == '\t')
233 ++nptr;
235 if (*nptr == '-')
237 sign = -1;
238 ++nptr;
240 else if (*nptr == '+')
241 ++nptr;
243 if (*nptr < '0' || *nptr > '9')
245 if (endptr != NULL)
246 *endptr = (char *) nptr;
247 return 0UL;
250 assert (base == 0);
251 base = 10;
252 if (*nptr == '0')
254 if (nptr[1] == 'x' || nptr[1] == 'X')
256 base = 16;
257 nptr += 2;
259 else
260 base = 8;
263 while (*nptr >= '0' && *nptr <= '9')
265 unsigned long int digval = *nptr - '0';
266 if (result > LONG_MAX / 10
267 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
269 errno = ERANGE;
270 if (endptr != NULL)
271 *endptr = (char *) nptr;
272 return ULONG_MAX;
274 result *= base;
275 result += digval;
276 ++nptr;
279 if (endptr != NULL)
280 *endptr = (char *) nptr;
281 return result * sign;
285 /* We always use _itoa instead of _itoa_word in ld.so since the former
286 also has to be present and it is never about speed when these
287 functions are used. */
288 char *
289 _itoa (value, buflim, base, upper_case)
290 unsigned long long int value;
291 char *buflim;
292 unsigned int base;
293 int upper_case;
295 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
297 assert (! upper_case);
300 *--buflim = INTUSE(_itoa_lower_digits)[value % base];
301 while ((value /= base) != 0);
303 return buflim;
307 /* The following is not a complete strsep implementation. It cannot
308 handle empty delimiter strings. But this isn't necessary for the
309 execution of ld.so. */
310 #undef strsep
311 #undef __strsep
312 char *
313 __strsep (char **stringp, const char *delim)
315 char *begin;
317 assert (delim[0] != '\0');
319 begin = *stringp;
320 if (begin != NULL)
322 char *end = begin;
324 while (*end != '\0' || (end = NULL))
326 const char *dp = delim;
329 if (*dp == *end)
330 break;
331 while (*++dp != '\0');
333 if (*dp != '\0')
335 *end++ = '\0';
336 break;
339 ++end;
342 *stringp = end;
345 return begin;
347 weak_alias (__strsep, strsep)
348 strong_alias (__strsep, __strsep_g)
351 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
352 up to 36. We don't need this here. */
353 const char INTUSE(_itoa_lower_digits)[16] attribute_hidden
354 = "0123456789abcdef";
357 #undef errno
358 /* The 'errno' in ld.so is not exported. */
359 extern int errno attribute_hidden;
361 int *
362 __errno_location (void)
364 return &errno;