Update.
[glibc.git] / elf / dl-minimal.c
blob564da7dfcf783e758f0544b5802722c0d4bbf44d
1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995,96,97,98,2000,2001 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/types.h>
25 #include <sys/mman.h>
26 #include <ldsodefs.h>
27 #include <stdio-common/_itoa.h>
29 #include <assert.h>
31 /* Minimal `malloc' allocator for use while loading shared libraries.
32 No block is ever freed. */
34 static void *alloc_ptr, *alloc_end, *alloc_last_block;
36 /* Declarations of global functions. */
37 extern void weak_function free (void *ptr);
38 extern void * weak_function realloc (void *ptr, size_t n);
39 extern unsigned long int weak_function __strtoul_internal
40 (const char *nptr, char **endptr, int base, int group);
41 extern unsigned long int weak_function strtoul (const char *nptr,
42 char **endptr, int base);
45 void * weak_function
46 malloc (size_t n)
48 #ifdef MAP_ANON
49 #define _dl_zerofd (-1)
50 #else
51 extern int _dl_zerofd;
53 if (_dl_zerofd == -1)
54 _dl_zerofd = _dl_sysdep_open_zero_fill ();
55 #define MAP_ANON 0
56 #endif
58 if (alloc_end == 0)
60 /* Consume any unused space in the last page of our data segment. */
61 extern int _end;
62 alloc_ptr = &_end;
63 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + _dl_pagesize - 1)
64 & ~(_dl_pagesize - 1));
67 /* Make sure the allocation pointer is ideally aligned. */
68 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
69 & ~(sizeof (double) - 1));
71 if (alloc_ptr + n >= alloc_end)
73 /* Insufficient space left; allocate another page. */
74 caddr_t page;
75 size_t nup = (n + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
76 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
77 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
78 assert (page != MAP_FAILED);
79 if (page != alloc_end)
80 alloc_ptr = page;
81 alloc_end = page + nup;
84 alloc_last_block = (void *) alloc_ptr;
85 alloc_ptr += n;
86 return alloc_last_block;
89 /* We use this function occasionally since the real implementation may
90 be optimized when it can assume the memory it returns already is
91 set to NUL. */
92 void * weak_function
93 calloc (size_t nmemb, size_t size)
95 size_t total = nmemb * size;
96 void *result = malloc (total);
97 return memset (result, '\0', total);
100 /* This will rarely be called. */
101 void weak_function
102 free (void *ptr)
104 /* We can free only the last block allocated. */
105 if (ptr == alloc_last_block)
106 alloc_ptr = alloc_last_block;
109 /* This is only called with the most recent block returned by malloc. */
110 void * weak_function
111 realloc (void *ptr, size_t n)
113 void *new;
114 assert (ptr == alloc_last_block);
115 alloc_ptr = alloc_last_block;
116 new = malloc (n);
117 assert (new == ptr);
118 return new;
121 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
123 #include <setjmp.h>
125 int weak_function
126 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
128 env[0].__mask_was_saved = 0;
129 return 0;
132 void weak_function
133 longjmp (jmp_buf env, int val)
135 __longjmp (env[0].__jmpbuf, val);
138 /* Define our own version of the internal function used by strerror. We
139 only provide the messages for some common errors. This avoids pulling
140 in the whole error list. */
142 char * weak_function
143 __strerror_r (int errnum, char *buf, size_t buflen)
145 char *msg;
147 switch (errnum)
149 case ENOMEM:
150 msg = (char *) "Cannot allocate memory";
151 break;
152 case EINVAL:
153 msg = (char *) "Invalid argument";
154 break;
155 case ENOENT:
156 msg = (char *) "No such file or directory";
157 break;
158 case EPERM:
159 msg = (char *) "Operation not permitted";
160 break;
161 case EIO:
162 msg = (char *) "Input/output error";
163 break;
164 case EACCES:
165 msg = (char *) "Permission denied";
166 break;
167 default:
168 /* No need to check buffer size, all calls in the dynamic linker
169 provide enough space. */
170 buf[buflen - 1] = '\0';
171 msg = _itoa_word (errnum, buf + buflen - 1, 10, 0);
172 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
173 sizeof ("Error ") - 1);
174 break;
177 return msg;
180 #ifndef NDEBUG
182 /* Define (weakly) our own assert failure function which doesn't use stdio.
183 If we are linked into the user program (-ldl), the normal __assert_fail
184 defn can override this one. */
186 void weak_function
187 __assert_fail (const char *assertion,
188 const char *file, unsigned int line, const char *function)
190 _dl_fatal_printf ("\
191 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
192 file, line, function ?: "", function ? ": " : "",
193 assertion);
197 void weak_function
198 __assert_perror_fail (int errnum,
199 const char *file, unsigned int line,
200 const char *function)
202 char errbuf[64];
203 _dl_fatal_printf ("\
204 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
205 file, line, function ?: "", function ? ": " : "",
206 __strerror_r (errnum, errbuf, sizeof (errbuf)));
209 #endif
211 unsigned long int weak_function
212 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
214 unsigned long int result = 0;
215 long int sign = 1;
217 while (*nptr == ' ' || *nptr == '\t')
218 ++nptr;
220 if (*nptr == '-')
222 sign = -1;
223 ++nptr;
225 else if (*nptr == '+')
226 ++nptr;
228 if (*nptr < '0' || *nptr > '9')
230 if (endptr != NULL)
231 *endptr = (char *) nptr;
232 return 0UL;
235 assert (base == 0);
236 base = 10;
237 if (*nptr == '0')
239 if (nptr[1] == 'x' || nptr[1] == 'X')
241 base = 16;
242 nptr += 2;
244 else
245 base = 8;
248 while (*nptr >= '0' && *nptr <= '9')
250 unsigned long int digval = *nptr - '0';
251 if (result > LONG_MAX / 10
252 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
254 errno = ERANGE;
255 if (endptr != NULL)
256 *endptr = (char *) nptr;
257 return ULONG_MAX;
259 result *= base;
260 result += digval;
261 ++nptr;
264 if (endptr != NULL)
265 *endptr = (char *) nptr;
266 return result * sign;