1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995,96,97,98,2000 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>
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
;
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 long int weak_function
__strtol_internal (const char *nptr
,
42 extern long int weak_function
strtol (const char *nptr
, char **endptr
,
44 extern unsigned long int weak_function __strtoul_internal
45 (const char *nptr
, char **endptr
, int base
, int group
);
46 extern unsigned long int weak_function
strtoul (const char *nptr
,
47 char **endptr
, int base
);
54 #define _dl_zerofd (-1)
56 extern int _dl_zerofd
;
59 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
65 /* Consume any unused space in the last page of our data segment. */
68 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0) + _dl_pagesize
- 1)
69 & ~(_dl_pagesize
- 1));
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));
76 if (alloc_ptr
+ n
>= alloc_end
)
78 /* Insufficient space left; allocate another page. */
80 size_t nup
= (n
+ _dl_pagesize
- 1) & ~(_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
)
86 alloc_end
= page
+ nup
;
89 alloc_last_block
= (void *) alloc_ptr
;
91 return alloc_last_block
;
94 /* We use this function occasionally since the real implementation may
95 be optimized when it can assume the memory it returns already is
98 calloc (size_t nmemb
, size_t size
)
100 size_t total
= nmemb
* size
;
101 void *result
= malloc (total
);
102 return memset (result
, '\0', total
);
105 /* This will rarely be called. */
109 /* We can free only the last block allocated. */
110 if (ptr
== alloc_last_block
)
111 alloc_ptr
= alloc_last_block
;
114 /* This is only called with the most recent block returned by malloc. */
116 realloc (void *ptr
, size_t n
)
119 assert (ptr
== alloc_last_block
);
120 alloc_ptr
= alloc_last_block
;
126 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
131 __sigjmp_save (sigjmp_buf env
, int savemask
)
133 env
[0].__mask_was_saved
= savemask
;
138 longjmp (jmp_buf env
, int val
)
140 __longjmp (env
[0].__jmpbuf
, val
);
143 /* Define our own version of the internal function used by strerror. We
144 only provide the messages for some common errors. This avoids pulling
145 in the whole error list. */
148 __strerror_r (int errnum
, char *buf
, size_t buflen
)
155 msg
= (char *) "Cannot allocate memory";
158 msg
= (char *) "Invalid argument";
161 msg
= (char *) "No such file or directory";
164 msg
= (char *) "Operation not permitted";
167 msg
= (char *) "Input/output error";
170 msg
= (char *) "Permission denied";
173 /* No need to check buffer size, all calls in the dynamic linker
174 provide enough space. */
175 buf
[buflen
- 1] = '\0';
176 msg
= _itoa_word (errnum
, buf
+ buflen
- 1, 10, 0);
177 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
178 sizeof ("Error ") - 1);
187 /* Define (weakly) our own assert failure function which doesn't use stdio.
188 If we are linked into the user program (-ldl), the normal __assert_fail
189 defn can override this one. */
192 __assert_fail (const char *assertion
,
193 const char *file
, unsigned int line
, const char *function
)
196 buf
[sizeof buf
- 1] = '\0';
197 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
198 file
, ": ", _itoa_word (line
, buf
+ sizeof buf
- 1, 10, 0),
199 ": ", function
?: "", function
? ": " : "",
200 "Assertion `", assertion
, "' failed!\n",
206 __assert_perror_fail (int errnum
,
207 const char *file
, unsigned int line
,
208 const char *function
)
212 buf
[sizeof buf
- 1] = '\0';
213 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
214 file
, ": ", _itoa_word (line
, buf
+ sizeof buf
- 1, 10, 0),
215 ": ", function
?: "", function
? ": " : "",
216 "Unexpected error: ",
217 __strerror_r (errnum
, errbuf
, sizeof (errbuf
)), "\n",
224 /* This function is only used in eval.c. */
225 long int weak_function
226 __strtol_internal (const char *nptr
, char **endptr
, int base
, int group
)
228 unsigned long int result
= 0;
231 while (*nptr
== ' ' || *nptr
== '\t')
239 else if (*nptr
== '+')
242 if (*nptr
< '0' || *nptr
> '9')
245 *endptr
= (char *) nptr
;
253 if (nptr
[1] == 'x' || nptr
[1] == 'X')
262 while (*nptr
>= '0' && *nptr
<= '9')
264 unsigned long int digval
= *nptr
- '0';
265 if (result
> LONG_MAX
/ 10
266 || (sign
> 0 ? result
== LONG_MAX
/ 10 && digval
> LONG_MAX
% 10
267 : (result
== ((unsigned long int) LONG_MAX
+ 1) / 10
268 && digval
> ((unsigned long int) LONG_MAX
+ 1) % 10)))
271 return sign
> 0 ? LONG_MAX
: LONG_MIN
;
278 return (long int) result
* sign
;
281 long int weak_function
282 strtol (const char *nptr
, char **endptr
, int base
)
284 return __strtol_internal (nptr
, endptr
, base
, 0);
287 unsigned long int weak_function
288 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
290 unsigned long int result
= 0;
293 while (*nptr
== ' ' || *nptr
== '\t')
301 else if (*nptr
== '+')
304 if (*nptr
< '0' || *nptr
> '9')
307 *endptr
= (char *) nptr
;
315 if (nptr
[1] == 'x' || nptr
[1] == 'X')
324 while (*nptr
>= '0' && *nptr
<= '9')
326 unsigned long int digval
= *nptr
- '0';
327 if (result
> LONG_MAX
/ 10
328 || (result
== ULONG_MAX
/ 10 && digval
> ULONG_MAX
% 10))
338 return result
* sign
;
341 unsigned long int weak_function
342 strtoul (const char *nptr
, char **endptr
, int base
)
344 return (unsigned long int) __strtoul_internal (nptr
, endptr
, base
, 0);