Update.
[glibc.git] / elf / dl-minimal.c
blob893a3b1f2e49ac2544e29004e87c01db43e043b2
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 <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 <stdio-common/_itoa.h>
31 #include <assert.h>
33 /* Minimal `malloc' allocator for use while loading shared libraries.
34 No block is ever freed. */
36 static void *alloc_ptr, *alloc_end, *alloc_last_block;
38 /* Declarations of global functions. */
39 extern void weak_function free (void *ptr);
40 extern void * weak_function realloc (void *ptr, size_t n);
41 extern unsigned long int weak_function __strtoul_internal (const char *nptr,
42 char **endptr,
43 int base,
44 int group);
45 extern unsigned long int weak_function strtoul (const char *nptr,
46 char **endptr, int base);
49 /* Allocate an aligned memory block. */
50 void * weak_function
51 __libc_memalign (size_t align, size_t n)
53 #ifdef MAP_ANON
54 #define _dl_zerofd (-1)
55 #else
56 extern int _dl_zerofd;
58 if (_dl_zerofd == -1)
59 _dl_zerofd = _dl_sysdep_open_zero_fill ();
60 #define MAP_ANON 0
61 #endif
63 if (alloc_end == 0)
65 /* Consume any unused space in the last page of our data segment. */
66 extern int _end attribute_hidden;
67 alloc_ptr = &_end;
68 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
69 + GL(dl_pagesize) - 1)
70 & ~(GL(dl_pagesize) - 1));
73 /* Make sure the allocation pointer is ideally aligned. */
74 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + align - 1)
75 & ~(align - 1));
77 if (alloc_ptr + n >= alloc_end)
79 /* Insufficient space left; allocate another page. */
80 caddr_t page;
81 size_t nup = (n + GL(dl_pagesize) - 1) & ~(GL(dl_pagesize) - 1);
82 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
83 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
84 assert (page != MAP_FAILED);
85 if (page != alloc_end)
86 alloc_ptr = page;
87 alloc_end = page + nup;
90 alloc_last_block = (void *) alloc_ptr;
91 alloc_ptr += n;
92 return alloc_last_block;
95 void * weak_function
96 malloc (size_t n)
98 return __libc_memalign (sizeof (double), n);
101 /* We use this function occasionally since the real implementation may
102 be optimized when it can assume the memory it returns already is
103 set to NUL. */
104 void * weak_function
105 calloc (size_t nmemb, size_t size)
107 size_t total = nmemb * size;
108 void *result = malloc (total);
109 return memset (result, '\0', total);
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)
118 alloc_ptr = alloc_last_block;
121 /* This is only called with the most recent block returned by malloc. */
122 void * weak_function
123 realloc (void *ptr, size_t n)
125 void *new;
126 if (ptr == NULL)
127 return malloc (n);
128 assert (ptr == alloc_last_block);
129 alloc_ptr = alloc_last_block;
130 new = malloc (n);
131 assert (new == ptr);
132 return new;
135 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
137 #include <setjmp.h>
139 int weak_function
140 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
142 env[0].__mask_was_saved = 0;
143 return 0;
146 void weak_function
147 longjmp (jmp_buf env, int val)
149 __longjmp (env[0].__jmpbuf, val);
152 /* Define our own version of the internal function used by strerror. We
153 only provide the messages for some common errors. This avoids pulling
154 in the whole error list. */
156 char * weak_function
157 __strerror_r (int errnum, char *buf, size_t buflen)
159 char *msg;
161 switch (errnum)
163 case ENOMEM:
164 msg = (char *) "Cannot allocate memory";
165 break;
166 case EINVAL:
167 msg = (char *) "Invalid argument";
168 break;
169 case ENOENT:
170 msg = (char *) "No such file or directory";
171 break;
172 case EPERM:
173 msg = (char *) "Operation not permitted";
174 break;
175 case EIO:
176 msg = (char *) "Input/output error";
177 break;
178 case EACCES:
179 msg = (char *) "Permission denied";
180 break;
181 default:
182 /* No need to check buffer size, all calls in the dynamic linker
183 provide enough space. */
184 buf[buflen - 1] = '\0';
185 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
186 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
187 sizeof ("Error ") - 1);
188 break;
191 return msg;
194 #ifndef NDEBUG
196 /* Define (weakly) our own assert failure function which doesn't use stdio.
197 If we are linked into the user program (-ldl), the normal __assert_fail
198 defn can override this one. */
200 void weak_function
201 __assert_fail (const char *assertion,
202 const char *file, unsigned int line, const char *function)
204 _dl_fatal_printf ("\
205 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
206 file, line, function ?: "", function ? ": " : "",
207 assertion);
210 rtld_hidden_weak(__assert_fail)
212 #endif
214 unsigned long int weak_function
215 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
217 unsigned long int result = 0;
218 long int sign = 1;
220 while (*nptr == ' ' || *nptr == '\t')
221 ++nptr;
223 if (*nptr == '-')
225 sign = -1;
226 ++nptr;
228 else if (*nptr == '+')
229 ++nptr;
231 if (*nptr < '0' || *nptr > '9')
233 if (endptr != NULL)
234 *endptr = (char *) nptr;
235 return 0UL;
238 assert (base == 0);
239 base = 10;
240 if (*nptr == '0')
242 if (nptr[1] == 'x' || nptr[1] == 'X')
244 base = 16;
245 nptr += 2;
247 else
248 base = 8;
251 while (*nptr >= '0' && *nptr <= '9')
253 unsigned long int digval = *nptr - '0';
254 if (result > LONG_MAX / 10
255 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
257 errno = ERANGE;
258 if (endptr != NULL)
259 *endptr = (char *) nptr;
260 return ULONG_MAX;
262 result *= base;
263 result += digval;
264 ++nptr;
267 if (endptr != NULL)
268 *endptr = (char *) nptr;
269 return result * sign;
273 /* We always use _itoa instead of _itoa_word in ld.so since the former
274 also has to be present and it is never about speed when these
275 functions are used. */
276 char *
277 _itoa (value, buflim, base, upper_case)
278 unsigned long long int value;
279 char *buflim;
280 unsigned int base;
281 int upper_case;
283 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
285 assert (! upper_case);
288 *--buflim = INTUSE(_itoa_lower_digits)[value % base];
289 while ((value /= base) != 0);
291 return buflim;
295 /* The following is not a complete strsep implementation. It cannot
296 handle empty delimiter strings. But this isn't necessary for the
297 execution of ld.so. */
298 #undef strsep
299 #undef __strsep
300 char *
301 __strsep (char **stringp, const char *delim)
303 char *begin;
305 assert (delim[0] != '\0');
307 begin = *stringp;
308 if (begin != NULL)
310 char *end = begin;
312 while (*end != '\0' || (end = NULL))
314 const char *dp = delim;
317 if (*dp == *end)
318 break;
319 while (*++dp != '\0');
321 if (*dp != '\0')
323 *end++ = '\0';
324 break;
327 ++end;
330 *stringp = end;
333 return begin;
335 weak_alias (__strsep, strsep)
336 strong_alias (__strsep, __strsep_g)
339 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
340 up to 36. We don't need this here. */
341 const char INTUSE(_itoa_lower_digits)[16] attribute_hidden
342 = "0123456789abcdef";
346 #undef errno
347 /* The 'errno' in ld.so is not exported. */
348 #if USE_TLS && HAVE___THREAD
349 extern __thread int errno attribute_hidden;
350 #else
351 extern int errno attribute_hidden;
353 int *
354 __errno_location (void)
356 return &errno;
358 #endif