1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995, 1996, 1997, 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <sys/types.h>
26 #include <elf/ldsodefs.h>
27 #include <stdio-common/_itoa.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
;
40 #define _dl_zerofd (-1)
42 extern int _dl_zerofd
;
45 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
51 /* Consume any unused space in the last page of our data segment. */
54 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0) + _dl_pagesize
- 1)
55 & ~(_dl_pagesize
- 1));
58 /* Make sure the allocation pointer is ideally aligned. */
59 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + sizeof (double) - 1)
60 & ~(sizeof (double) - 1));
62 if (alloc_ptr
+ n
>= alloc_end
)
64 /* Insufficient space left; allocate another page. */
66 size_t nup
= (n
+ _dl_pagesize
- 1) & ~(_dl_pagesize
- 1);
67 page
= __mmap (0, nup
, PROT_READ
|PROT_WRITE
,
68 MAP_ANON
|MAP_PRIVATE
, _dl_zerofd
, 0);
69 assert (page
!= MAP_FAILED
);
70 if (page
!= alloc_end
)
72 alloc_end
= page
+ nup
;
75 alloc_last_block
= (void *) alloc_ptr
;
77 return alloc_last_block
;
80 /* We use this function occasionally since the real implementation may
81 be optimized when it can assume the memory it returns already is
84 calloc (size_t nmemb
, size_t size
)
86 size_t total
= nmemb
* size
;
87 void *result
= malloc (total
);
88 return memset (result
, '\0', total
);
91 /* This will rarely be called. */
95 /* We can free only the last block allocated. */
96 if (ptr
== alloc_last_block
)
97 alloc_ptr
= alloc_last_block
;
100 /* This is only called with the most recent block returned by malloc. */
102 realloc (void *ptr
, size_t n
)
105 assert (ptr
== alloc_last_block
);
106 alloc_ptr
= alloc_last_block
;
112 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
117 __sigjmp_save (sigjmp_buf env
, int savemask
)
119 env
[0].__mask_was_saved
= savemask
;
124 longjmp (jmp_buf env
, int val
)
126 __longjmp (env
[0].__jmpbuf
, val
);
129 /* Define our own version of the internal function used by strerror. We
130 only provide the messages for some common errors. This avoids pulling
131 in the whole error list. */
134 __strerror_r (int errnum
, char *buf
, size_t buflen
)
141 msg
= (char *) "Cannot allocate memory";
144 msg
= (char *) "Invalid argument";
147 msg
= (char *) "No such file or directory";
150 msg
= (char *) "Operation not permitted";
153 msg
= (char *) "Input/output error";
156 msg
= (char *) "Permission denied";
159 /* No need to check buffer size, all calls in the dynamic linker
160 provide enough space. */
161 buf
[buflen
- 1] = '\0';
162 msg
= _itoa_word (errnum
, buf
+ buflen
- 1, 10, 0);
163 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
164 sizeof ("Error ") - 1);
173 /* Define (weakly) our own assert failure function which doesn't use stdio.
174 If we are linked into the user program (-ldl), the normal __assert_fail
175 defn can override this one. */
178 __assert_fail (const char *assertion
,
179 const char *file
, unsigned int line
, const char *function
)
182 buf
[sizeof buf
- 1] = '\0';
183 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
184 file
, ": ", _itoa_word (line
, buf
+ sizeof buf
- 1, 10, 0),
185 ": ", function
?: "", function
? ": " : "",
186 "Assertion `", assertion
, "' failed!\n",
192 __assert_perror_fail (int errnum
,
193 const char *file
, unsigned int line
,
194 const char *function
)
198 buf
[sizeof buf
- 1] = '\0';
199 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
200 file
, ": ", _itoa_word (line
, buf
+ sizeof buf
- 1, 10, 0),
201 ": ", function
?: "", function
? ": " : "",
202 "Unexpected error: ",
203 __strerror_r (errnum
, errbuf
, sizeof (errbuf
)), "\n",
210 /* This function is only used in eval.c. */
211 long int weak_function
212 __strtol_internal (const char *nptr
, char **endptr
, int base
, int group
)
214 unsigned long int result
= 0;
217 while (*nptr
== ' ' || *nptr
== '\t')
225 else if (*nptr
== '+')
228 if (*nptr
< '0' || *nptr
> '9')
231 *endptr
= (char *) nptr
;
239 if (nptr
[1] == 'x' || nptr
[1] == 'X')
248 while (*nptr
>= '0' && *nptr
<= '9')
250 unsigned long int digval
= *nptr
- '0';
251 if (result
> LONG_MAX
/ 10
252 || (sign
> 0 ? result
== LONG_MAX
/ 10 && digval
> LONG_MAX
% 10
253 : (result
== ((unsigned long int) LONG_MAX
+ 1) / 10
254 && digval
> ((unsigned long int) LONG_MAX
+ 1) % 10)))
257 return sign
> 0 ? LONG_MAX
: LONG_MIN
;
264 return (long int) result
* sign
;
267 long int weak_function
268 strtol (const char *nptr
, char **endptr
, int base
)
270 return __strtol_internal (nptr
, endptr
, base
, 0);
273 unsigned long int weak_function
274 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
276 unsigned long int result
= 0;
279 while (*nptr
== ' ' || *nptr
== '\t')
287 else if (*nptr
== '+')
290 if (*nptr
< '0' || *nptr
> '9')
293 *endptr
= (char *) nptr
;
301 if (nptr
[1] == 'x' || nptr
[1] == 'X')
310 while (*nptr
>= '0' && *nptr
<= '9')
312 unsigned long int digval
= *nptr
- '0';
313 if (result
> LONG_MAX
/ 10
314 || (result
== ULONG_MAX
/ 10 && digval
> ULONG_MAX
% 10))
324 return result
* sign
;
327 unsigned long int weak_function
328 strtoul (const char *nptr
, char **endptr
, int base
)
330 return (unsigned long int) __strtoul_internal (nptr
, endptr
, base
, 0);