]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-minimal.c
Update.
[thirdparty/glibc.git] / elf / dl-minimal.c
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.
4
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.
9
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.
14
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. */
19
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>
30
31 #include <assert.h>
32
33 /* Minimal `malloc' allocator for use while loading shared libraries.
34 No block is ever freed. */
35
36 static void *alloc_ptr, *alloc_end, *alloc_last_block;
37
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);
47
48
49 void * weak_function
50 malloc (size_t n)
51 {
52 #ifdef MAP_ANON
53 #define _dl_zerofd (-1)
54 #else
55 extern int _dl_zerofd;
56
57 if (_dl_zerofd == -1)
58 _dl_zerofd = _dl_sysdep_open_zero_fill ();
59 #define MAP_ANON 0
60 #endif
61
62 if (alloc_end == 0)
63 {
64 /* Consume any unused space in the last page of our data segment. */
65 extern int _end attribute_hidden;
66 alloc_ptr = &_end;
67 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
68 + GL(dl_pagesize) - 1)
69 & ~(GL(dl_pagesize) - 1));
70 }
71
72 /* Make sure the allocation pointer is ideally aligned. */
73 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
74 & ~(sizeof (double) - 1));
75
76 if (alloc_ptr + n >= alloc_end)
77 {
78 /* Insufficient space left; allocate another page. */
79 caddr_t page;
80 size_t nup = (n + GL(dl_pagesize) - 1) & ~(GL(dl_pagesize) - 1);
81 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
82 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
83 assert (page != MAP_FAILED);
84 if (page != alloc_end)
85 alloc_ptr = page;
86 alloc_end = page + nup;
87 }
88
89 alloc_last_block = (void *) alloc_ptr;
90 alloc_ptr += n;
91 return alloc_last_block;
92 }
93
94 /* We use this function occasionally since the real implementation may
95 be optimized when it can assume the memory it returns already is
96 set to NUL. */
97 void * weak_function
98 calloc (size_t nmemb, size_t size)
99 {
100 size_t total = nmemb * size;
101 void *result = malloc (total);
102 return memset (result, '\0', total);
103 }
104
105 /* This will rarely be called. */
106 void weak_function
107 free (void *ptr)
108 {
109 /* We can free only the last block allocated. */
110 if (ptr == alloc_last_block)
111 alloc_ptr = alloc_last_block;
112 }
113
114 /* This is only called with the most recent block returned by malloc. */
115 void * weak_function
116 realloc (void *ptr, size_t n)
117 {
118 void *new;
119 if (ptr == NULL)
120 return malloc (n);
121 assert (ptr == alloc_last_block);
122 alloc_ptr = alloc_last_block;
123 new = malloc (n);
124 assert (new == ptr);
125 return new;
126 }
127
128 /* Return alligned memory block. */
129 void * weak_function
130 __libc_memalign (size_t align, size_t n)
131 {
132 void *newp = malloc (n + align - 1);
133
134 return (void *) roundup ((uintptr_t) newp, align);
135 }
136 \f
137 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
138
139 #include <setjmp.h>
140
141 int weak_function
142 __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
143 {
144 env[0].__mask_was_saved = 0;
145 return 0;
146 }
147
148 void weak_function
149 longjmp (jmp_buf env, int val)
150 {
151 __longjmp (env[0].__jmpbuf, val);
152 }
153 \f
154 /* Define our own version of the internal function used by strerror. We
155 only provide the messages for some common errors. This avoids pulling
156 in the whole error list. */
157
158 char * weak_function
159 __strerror_r (int errnum, char *buf, size_t buflen)
160 {
161 char *msg;
162
163 switch (errnum)
164 {
165 case ENOMEM:
166 msg = (char *) "Cannot allocate memory";
167 break;
168 case EINVAL:
169 msg = (char *) "Invalid argument";
170 break;
171 case ENOENT:
172 msg = (char *) "No such file or directory";
173 break;
174 case EPERM:
175 msg = (char *) "Operation not permitted";
176 break;
177 case EIO:
178 msg = (char *) "Input/output error";
179 break;
180 case EACCES:
181 msg = (char *) "Permission denied";
182 break;
183 default:
184 /* No need to check buffer size, all calls in the dynamic linker
185 provide enough space. */
186 buf[buflen - 1] = '\0';
187 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
188 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
189 sizeof ("Error ") - 1);
190 break;
191 }
192
193 return msg;
194 }
195 \f
196 #ifndef NDEBUG
197
198 /* Define (weakly) our own assert failure function which doesn't use stdio.
199 If we are linked into the user program (-ldl), the normal __assert_fail
200 defn can override this one. */
201
202 void weak_function
203 __assert_fail (const char *assertion,
204 const char *file, unsigned int line, const char *function)
205 {
206 _dl_fatal_printf ("\
207 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
208 file, line, function ?: "", function ? ": " : "",
209 assertion);
210
211 }
212
213 void weak_function
214 __assert_perror_fail (int errnum,
215 const char *file, unsigned int line,
216 const char *function)
217 {
218 char errbuf[64];
219 _dl_fatal_printf ("\
220 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
221 file, line, function ?: "", function ? ": " : "",
222 __strerror_r (errnum, errbuf, sizeof (errbuf)));
223 }
224
225 #endif
226
227 unsigned long int weak_function
228 __strtoul_internal (const char *nptr, char **endptr, int base, int group)
229 {
230 unsigned long int result = 0;
231 long int sign = 1;
232
233 while (*nptr == ' ' || *nptr == '\t')
234 ++nptr;
235
236 if (*nptr == '-')
237 {
238 sign = -1;
239 ++nptr;
240 }
241 else if (*nptr == '+')
242 ++nptr;
243
244 if (*nptr < '0' || *nptr > '9')
245 {
246 if (endptr != NULL)
247 *endptr = (char *) nptr;
248 return 0UL;
249 }
250
251 assert (base == 0);
252 base = 10;
253 if (*nptr == '0')
254 {
255 if (nptr[1] == 'x' || nptr[1] == 'X')
256 {
257 base = 16;
258 nptr += 2;
259 }
260 else
261 base = 8;
262 }
263
264 while (*nptr >= '0' && *nptr <= '9')
265 {
266 unsigned long int digval = *nptr - '0';
267 if (result > LONG_MAX / 10
268 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
269 {
270 errno = ERANGE;
271 if (endptr != NULL)
272 *endptr = (char *) nptr;
273 return ULONG_MAX;
274 }
275 result *= base;
276 result += digval;
277 ++nptr;
278 }
279
280 if (endptr != NULL)
281 *endptr = (char *) nptr;
282 return result * sign;
283 }
284
285
286 /* We always use _itoa instead of _itoa_word in ld.so since the former
287 also has to be present and it is never about speed when these
288 functions are used. */
289 char *
290 _itoa (value, buflim, base, upper_case)
291 unsigned long long int value;
292 char *buflim;
293 unsigned int base;
294 int upper_case;
295 {
296 extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
297
298 assert (! upper_case);
299
300 do
301 *--buflim = INTUSE(_itoa_lower_digits)[value % base];
302 while ((value /= base) != 0);
303
304 return buflim;
305 }
306
307
308 /* The following is not a complete strsep implementation. It cannot
309 handle empty delimiter strings. But this isn't necessary for the
310 execution of ld.so. */
311 #undef strsep
312 #undef __strsep
313 char *
314 __strsep (char **stringp, const char *delim)
315 {
316 char *begin;
317
318 assert (delim[0] != '\0');
319
320 begin = *stringp;
321 if (begin != NULL)
322 {
323 char *end = begin;
324
325 while (*end != '\0' || (end = NULL))
326 {
327 const char *dp = delim;
328
329 do
330 if (*dp == *end)
331 break;
332 while (*++dp != '\0');
333
334 if (*dp != '\0')
335 {
336 *end++ = '\0';
337 break;
338 }
339
340 ++end;
341 }
342
343 *stringp = end;
344 }
345
346 return begin;
347 }
348 weak_alias (__strsep, strsep)
349 strong_alias (__strsep, __strsep_g)
350
351
352 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
353 up to 36. We don't need this here. */
354 const char INTUSE(_itoa_lower_digits)[16] attribute_hidden
355 = "0123456789abcdef";
356
357
358
359 #undef errno
360 /* The 'errno' in ld.so is not exported. */
361 #if USE_TLS && HAVE___THREAD
362 extern __thread int errno attribute_hidden;
363 #else
364 extern int errno attribute_hidden;
365
366 int *
367 __errno_location (void)
368 {
369 return &errno;
370 }
371 #endif