Update.
[glibc.git] / elf / dl-minimal.c
blob4ec2ebcbb44fd33ffe7678c5ec818ab63f82d43e
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-1998,2000-2002,2004 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 + GLRO(dl_pagesize) - 1)
70 & ~(GLRO(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 + GLRO(dl_pagesize) - 1) & ~(GLRO(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 /* New memory from the trivial malloc above is always already cleared.
108 (We make sure that's true in the rare occasion it might not be,
109 by clearing memory in free, below.) */
110 return malloc (nmemb * size);
113 /* This will rarely be called. */
114 void weak_function
115 free (void *ptr)
117 /* We can free only the last block allocated. */
118 if (ptr == alloc_last_block)
120 /* Since this is rare, we clear the freed block here
121 so that calloc can presume malloc returns cleared memory. */
122 memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block);
123 alloc_ptr = alloc_last_block;
127 /* This is only called with the most recent block returned by malloc. */
128 void * weak_function
129 realloc (void *ptr, size_t n)
131 void *new;
132 if (ptr == NULL)
133 return malloc (n);
134 assert (ptr == alloc_last_block);
135 alloc_ptr = alloc_last_block;
136 new = malloc (n);
137 assert (new == ptr);
138 return new;
141 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
143 #include <setjmp.h>
145 int weak_function
146 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
148 env[0].__mask_was_saved = 0;
149 return 0;
152 void weak_function
153 longjmp (jmp_buf env, int val)
155 __longjmp (env[0].__jmpbuf, val);
158 /* Define our own version of the internal function used by strerror. We
159 only provide the messages for some common errors. This avoids pulling
160 in the whole error list. */
162 char * weak_function
163 __strerror_r (int errnum, char *buf, size_t buflen)
165 char *msg;
167 switch (errnum)
169 case ENOMEM:
170 msg = (char *) "Cannot allocate memory";
171 break;
172 case EINVAL:
173 msg = (char *) "Invalid argument";
174 break;
175 case ENOENT:
176 msg = (char *) "No such file or directory";
177 break;
178 case EPERM:
179 msg = (char *) "Operation not permitted";
180 break;
181 case EIO:
182 msg = (char *) "Input/output error";
183 break;
184 case EACCES:
185 msg = (char *) "Permission denied";
186 break;
187 default:
188 /* No need to check buffer size, all calls in the dynamic linker
189 provide enough space. */
190 buf[buflen - 1] = '\0';
191 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
192 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
193 sizeof ("Error ") - 1);
194 break;
197 return msg;
200 #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 unsigned long int weak_function
234 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
236 unsigned long int result = 0;
237 long int sign = 1;
239 while (*nptr == ' ' || *nptr == '\t')
240 ++nptr;
242 if (*nptr == '-')
244 sign = -1;
245 ++nptr;
247 else if (*nptr == '+')
248 ++nptr;
250 if (*nptr < '0' || *nptr > '9')
252 if (endptr != NULL)
253 *endptr = (char *) nptr;
254 return 0UL;
257 assert (base == 0);
258 base = 10;
259 if (*nptr == '0')
261 if (nptr[1] == 'x' || nptr[1] == 'X')
263 base = 16;
264 nptr += 2;
266 else
267 base = 8;
270 while (*nptr >= '0' && *nptr <= '9')
272 unsigned long int digval = *nptr - '0';
273 if (result > LONG_MAX / 10
274 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
276 errno = ERANGE;
277 if (endptr != NULL)
278 *endptr = (char *) nptr;
279 return ULONG_MAX;
281 result *= base;
282 result += digval;
283 ++nptr;
286 if (endptr != NULL)
287 *endptr = (char *) nptr;
288 return result * sign;
292 /* We always use _itoa instead of _itoa_word in ld.so since the former
293 also has to be present and it is never about speed when these
294 functions are used. */
295 char *
296 _itoa (value, buflim, base, upper_case)
297 unsigned long long int value;
298 char *buflim;
299 unsigned int base;
300 int upper_case;
302 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
304 assert (! upper_case);
307 *--buflim = INTUSE(_itoa_lower_digits)[value % base];
308 while ((value /= base) != 0);
310 return buflim;
314 /* The following is not a complete strsep implementation. It cannot
315 handle empty delimiter strings. But this isn't necessary for the
316 execution of ld.so. */
317 #undef strsep
318 #undef __strsep
319 char *
320 __strsep (char **stringp, const char *delim)
322 char *begin;
324 assert (delim[0] != '\0');
326 begin = *stringp;
327 if (begin != NULL)
329 char *end = begin;
331 while (*end != '\0' || (end = NULL))
333 const char *dp = delim;
336 if (*dp == *end)
337 break;
338 while (*++dp != '\0');
340 if (*dp != '\0')
342 *end++ = '\0';
343 break;
346 ++end;
349 *stringp = end;
352 return begin;
354 weak_alias (__strsep, strsep)
355 strong_alias (__strsep, __strsep_g)
358 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
359 up to 36. We don't need this here. */
360 const char INTUSE(_itoa_lower_digits)[16] attribute_hidden
361 = "0123456789abcdef";