Updated to fedora-glibc-20090427T1419
[glibc.git] / elf / dl-minimal.c
blobe07029326c913aa617d09c9cf2e5453b4ef3ede6
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-1998,2000-2002,2004-2006,2007,2009
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <limits.h>
23 #include <string.h>
24 #include <tls.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <ldsodefs.h>
30 #include <stdio-common/_itoa.h>
32 #include <assert.h>
34 /* Minimal `malloc' allocator for use while loading shared libraries.
35 No block is ever freed. */
37 static void *alloc_ptr, *alloc_end, *alloc_last_block;
39 /* Declarations of global functions. */
40 extern void weak_function free (void *ptr);
41 extern void * weak_function realloc (void *ptr, size_t n);
42 extern unsigned long int weak_function __strtoul_internal (const char *nptr,
43 char **endptr,
44 int base,
45 int group);
46 extern unsigned long int weak_function strtoul (const char *nptr,
47 char **endptr, int base);
50 /* Allocate an aligned memory block. */
51 void * weak_function
52 __libc_memalign (size_t align, size_t n)
54 if (alloc_end == 0)
56 /* Consume any unused space in the last page of our data segment. */
57 extern int _end attribute_hidden;
58 alloc_ptr = &_end;
59 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
60 + GLRO(dl_pagesize) - 1)
61 & ~(GLRO(dl_pagesize) - 1));
64 /* Make sure the allocation pointer is ideally aligned. */
65 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + align - 1)
66 & ~(align - 1));
68 if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
70 /* Insufficient space left; allocate another page. */
71 caddr_t page;
72 size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
73 if (__builtin_expect (nup == 0, 0))
75 if (n)
76 return NULL;
77 nup = GLRO(dl_pagesize);
79 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
80 MAP_ANON|MAP_PRIVATE, -1, 0);
81 if (page == MAP_FAILED)
82 return NULL;
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 void * weak_function
94 malloc (size_t n)
96 return __libc_memalign (sizeof (double), n);
99 /* We use this function occasionally since the real implementation may
100 be optimized when it can assume the memory it returns already is
101 set to NUL. */
102 void * weak_function
103 calloc (size_t nmemb, size_t size)
105 /* New memory from the trivial malloc above is always already cleared.
106 (We make sure that's true in the rare occasion it might not be,
107 by clearing memory in free, below.) */
108 size_t bytes = nmemb * size;
110 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
111 if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
112 && size != 0 && bytes / size != nmemb)
113 return NULL;
115 return malloc (bytes);
118 /* This will rarely be called. */
119 void weak_function
120 free (void *ptr)
122 /* We can free only the last block allocated. */
123 if (ptr == alloc_last_block)
125 /* Since this is rare, we clear the freed block here
126 so that calloc can presume malloc returns cleared memory. */
127 memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block);
128 alloc_ptr = alloc_last_block;
132 /* This is only called with the most recent block returned by malloc. */
133 void * weak_function
134 realloc (void *ptr, size_t n)
136 if (ptr == NULL)
137 return malloc (n);
138 assert (ptr == alloc_last_block);
139 size_t old_size = alloc_ptr - alloc_last_block;
140 alloc_ptr = alloc_last_block;
141 void *new = malloc (n);
142 return new != ptr ? memcpy (new, ptr, old_size) : new;
145 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
147 #include <setjmp.h>
149 int weak_function
150 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
152 env[0].__mask_was_saved = 0;
153 return 0;
156 /* Define our own version of the internal function used by strerror. We
157 only provide the messages for some common errors. This avoids pulling
158 in the whole error list. */
160 char * weak_function
161 __strerror_r (int errnum, char *buf, size_t buflen)
163 char *msg;
165 switch (errnum)
167 case ENOMEM:
168 msg = (char *) "Cannot allocate memory";
169 break;
170 case EINVAL:
171 msg = (char *) "Invalid argument";
172 break;
173 case ENOENT:
174 msg = (char *) "No such file or directory";
175 break;
176 case EPERM:
177 msg = (char *) "Operation not permitted";
178 break;
179 case EIO:
180 msg = (char *) "Input/output error";
181 break;
182 case EACCES:
183 msg = (char *) "Permission denied";
184 break;
185 default:
186 /* No need to check buffer size, all calls in the dynamic linker
187 provide enough space. */
188 buf[buflen - 1] = '\0';
189 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
190 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
191 sizeof ("Error ") - 1);
192 break;
195 return msg;
198 #ifndef NDEBUG
200 /* Define (weakly) our own assert failure function which doesn't use stdio.
201 If we are linked into the user program (-ldl), the normal __assert_fail
202 defn can override this one. */
204 void weak_function
205 __assert_fail (const char *assertion,
206 const char *file, unsigned int line, const char *function)
208 _dl_fatal_printf ("\
209 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
210 file, line, function ?: "", function ? ": " : "",
211 assertion);
214 rtld_hidden_weak(__assert_fail)
216 void weak_function
217 __assert_perror_fail (int errnum,
218 const char *file, unsigned int line,
219 const char *function)
221 char errbuf[400];
222 _dl_fatal_printf ("\
223 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
224 file, line, function ?: "", function ? ": " : "",
225 __strerror_r (errnum, errbuf, sizeof errbuf));
228 rtld_hidden_weak (__assert_perror_fail)
229 #endif
231 unsigned long int weak_function
232 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
234 unsigned long int result = 0;
235 long int sign = 1;
237 while (*nptr == ' ' || *nptr == '\t')
238 ++nptr;
240 if (*nptr == '-')
242 sign = -1;
243 ++nptr;
245 else if (*nptr == '+')
246 ++nptr;
248 if (*nptr < '0' || *nptr > '9')
250 if (endptr != NULL)
251 *endptr = (char *) nptr;
252 return 0UL;
255 assert (base == 0);
256 base = 10;
257 if (*nptr == '0')
259 if (nptr[1] == 'x' || nptr[1] == 'X')
261 base = 16;
262 nptr += 2;
264 else
265 base = 8;
268 while (*nptr >= '0' && *nptr <= '9')
270 unsigned long int digval = *nptr - '0';
271 if (result > ULONG_MAX / 10
272 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
274 errno = ERANGE;
275 if (endptr != NULL)
276 *endptr = (char *) nptr;
277 return ULONG_MAX;
279 result *= base;
280 result += digval;
281 ++nptr;
284 if (endptr != NULL)
285 *endptr = (char *) nptr;
286 return result * sign;
290 #undef _itoa
291 /* We always use _itoa instead of _itoa_word in ld.so since the former
292 also has to be present and it is never about speed when these
293 functions are used. */
294 char *
295 _itoa (value, buflim, base, upper_case)
296 unsigned long long int value;
297 char *buflim;
298 unsigned int base;
299 int upper_case;
301 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
303 assert (! upper_case);
306 *--buflim = INTUSE(_itoa_lower_digits)[value % base];
307 while ((value /= base) != 0);
309 return buflim;
313 /* The following is not a complete strsep implementation. It cannot
314 handle empty delimiter strings. But this isn't necessary for the
315 execution of ld.so. */
316 #undef strsep
317 #undef __strsep
318 char *
319 __strsep (char **stringp, const char *delim)
321 char *begin;
323 assert (delim[0] != '\0');
325 begin = *stringp;
326 if (begin != NULL)
328 char *end = begin;
330 while (*end != '\0' || (end = NULL))
332 const char *dp = delim;
335 if (*dp == *end)
336 break;
337 while (*++dp != '\0');
339 if (*dp != '\0')
341 *end++ = '\0';
342 break;
345 ++end;
348 *stringp = end;
351 return begin;
353 weak_alias (__strsep, strsep)
354 strong_alias (__strsep, __strsep_g)
356 void
357 __attribute__ ((noreturn))
358 __chk_fail (void)
360 _exit (127);
362 rtld_hidden_def (__chk_fail)
364 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
365 up to 36. We don't need this here. */
366 const char INTUSE(_itoa_lower_digits)[16] attribute_hidden
367 = "0123456789abcdef";