Update.
[glibc.git] / elf / dl-minimal.c
blob275ad86fe00e97b4940aa2024f88797a1ce8341f
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
41 (const char *nptr, char **endptr, int base, int group);
42 extern unsigned long int weak_function strtoul (const char *nptr,
43 char **endptr, int base);
46 void * weak_function
47 malloc (size_t n)
49 #ifdef MAP_ANON
50 #define _dl_zerofd (-1)
51 #else
52 extern int _dl_zerofd;
54 if (_dl_zerofd == -1)
55 _dl_zerofd = _dl_sysdep_open_zero_fill ();
56 #define MAP_ANON 0
57 #endif
59 if (alloc_end == 0)
61 /* Consume any unused space in the last page of our data segment. */
62 extern int _end;
63 alloc_ptr = &_end;
64 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
65 + GL(dl_pagesize) - 1)
66 & ~(GL(dl_pagesize) - 1));
69 /* Make sure the allocation pointer is ideally aligned. */
70 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
71 & ~(sizeof (double) - 1));
73 if (alloc_ptr + n >= alloc_end)
75 /* Insufficient space left; allocate another page. */
76 caddr_t page;
77 size_t nup = (n + GL(dl_pagesize) - 1) & ~(GL(dl_pagesize) - 1);
78 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
79 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
80 assert (page != MAP_FAILED);
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 /* We use this function occasionally since the real implementation may
92 be optimized when it can assume the memory it returns already is
93 set to NUL. */
94 void * weak_function
95 calloc (size_t nmemb, size_t size)
97 size_t total = nmemb * size;
98 void *result = malloc (total);
99 return memset (result, '\0', total);
102 /* This will rarely be called. */
103 void weak_function
104 free (void *ptr)
106 /* We can free only the last block allocated. */
107 if (ptr == alloc_last_block)
108 alloc_ptr = alloc_last_block;
111 /* This is only called with the most recent block returned by malloc. */
112 void * weak_function
113 realloc (void *ptr, size_t n)
115 void *new;
116 if (ptr == NULL)
117 return malloc (n);
118 assert (ptr == alloc_last_block);
119 alloc_ptr = alloc_last_block;
120 new = malloc (n);
121 assert (new == ptr);
122 return new;
125 /* Return alligned memory block. */
126 void * weak_function
127 __libc_memalign (size_t align, size_t n)
129 void *newp = malloc (n + align - 1);
131 return (void *) roundup ((uintptr_t) newp, align);
134 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
136 #include <setjmp.h>
138 int weak_function
139 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
141 env[0].__mask_was_saved = 0;
142 return 0;
145 void weak_function
146 longjmp (jmp_buf env, int val)
148 __longjmp (env[0].__jmpbuf, val);
151 /* Define our own version of the internal function used by strerror. We
152 only provide the messages for some common errors. This avoids pulling
153 in the whole error list. */
155 char * weak_function
156 __strerror_r (int errnum, char *buf, size_t buflen)
158 char *msg;
160 switch (errnum)
162 case ENOMEM:
163 msg = (char *) "Cannot allocate memory";
164 break;
165 case EINVAL:
166 msg = (char *) "Invalid argument";
167 break;
168 case ENOENT:
169 msg = (char *) "No such file or directory";
170 break;
171 case EPERM:
172 msg = (char *) "Operation not permitted";
173 break;
174 case EIO:
175 msg = (char *) "Input/output error";
176 break;
177 case EACCES:
178 msg = (char *) "Permission denied";
179 break;
180 default:
181 /* No need to check buffer size, all calls in the dynamic linker
182 provide enough space. */
183 buf[buflen - 1] = '\0';
184 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
185 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
186 sizeof ("Error ") - 1);
187 break;
190 return msg;
193 #ifndef NDEBUG
195 /* Define (weakly) our own assert failure function which doesn't use stdio.
196 If we are linked into the user program (-ldl), the normal __assert_fail
197 defn can override this one. */
199 void weak_function
200 __assert_fail (const char *assertion,
201 const char *file, unsigned int line, const char *function)
203 _dl_fatal_printf ("\
204 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
205 file, line, function ?: "", function ? ": " : "",
206 assertion);
210 void weak_function
211 __assert_perror_fail (int errnum,
212 const char *file, unsigned int line,
213 const char *function)
215 char errbuf[64];
216 _dl_fatal_printf ("\
217 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
218 file, line, function ?: "", function ? ": " : "",
219 __strerror_r (errnum, errbuf, sizeof (errbuf)));
222 #endif
224 unsigned long int weak_function
225 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
227 unsigned long int result = 0;
228 long int sign = 1;
230 while (*nptr == ' ' || *nptr == '\t')
231 ++nptr;
233 if (*nptr == '-')
235 sign = -1;
236 ++nptr;
238 else if (*nptr == '+')
239 ++nptr;
241 if (*nptr < '0' || *nptr > '9')
243 if (endptr != NULL)
244 *endptr = (char *) nptr;
245 return 0UL;
248 assert (base == 0);
249 base = 10;
250 if (*nptr == '0')
252 if (nptr[1] == 'x' || nptr[1] == 'X')
254 base = 16;
255 nptr += 2;
257 else
258 base = 8;
261 while (*nptr >= '0' && *nptr <= '9')
263 unsigned long int digval = *nptr - '0';
264 if (result > LONG_MAX / 10
265 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
267 errno = ERANGE;
268 if (endptr != NULL)
269 *endptr = (char *) nptr;
270 return ULONG_MAX;
272 result *= base;
273 result += digval;
274 ++nptr;
277 if (endptr != NULL)
278 *endptr = (char *) nptr;
279 return result * sign;
283 /* We always use _itoa instead of _itoa_word in ld.so since the former
284 also has to be present and it is never about speed when these
285 functions are used. */
286 char *
287 _itoa (value, buflim, base, upper_case)
288 unsigned long long int value;
289 char *buflim;
290 unsigned int base;
291 int upper_case;
293 extern const char _itoa_lower_digits[];
295 assert (! upper_case);
298 *--buflim = _itoa_lower_digits[value % base];
299 while ((value /= base) != 0);
301 return buflim;
305 /* The following is not a complete strsep implementation. It cannot
306 handle empty delimiter strings. But this isn't necessary for the
307 execution of ld.so. */
308 #undef strsep
309 #undef __strsep
310 char *
311 __strsep (char **stringp, const char *delim)
313 char *begin;
315 assert (delim[0] != '\0');
317 begin = *stringp;
318 if (begin != NULL)
320 char *end = begin;
322 while (*end != '\0' || (end = NULL))
324 const char *dp = delim;
327 if (*dp == *end)
328 break;
329 while (*++dp != '\0');
331 if (*dp != '\0')
333 *end++ = '\0';
334 break;
337 ++end;
340 *stringp = end;
343 return begin;
345 weak_alias (__strsep, strsep)
346 strong_alias (__strsep, __strsep_g)
349 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
350 up to 36. We don't need this here. */
351 const char _itoa_lower_digits[16] = "0123456789abcdef";