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
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 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
);
49 #define _dl_zerofd (-1)
51 extern int _dl_zerofd
;
54 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
60 /* Consume any unused space in the last page of our data segment. */
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. */
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
)
81 alloc_end
= page
+ nup
;
84 alloc_last_block
= (void *) alloc_ptr
;
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
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. */
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. */
111 realloc (void *ptr
, size_t n
)
114 assert (ptr
== alloc_last_block
);
115 alloc_ptr
= alloc_last_block
;
121 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
126 __sigjmp_save (sigjmp_buf env
, int savemask
__attribute__ ((unused
)))
128 env
[0].__mask_was_saved
= 0;
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. */
143 __strerror_r (int errnum
, char *buf
, size_t buflen
)
150 msg
= (char *) "Cannot allocate memory";
153 msg
= (char *) "Invalid argument";
156 msg
= (char *) "No such file or directory";
159 msg
= (char *) "Operation not permitted";
162 msg
= (char *) "Input/output error";
165 msg
= (char *) "Permission denied";
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);
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. */
187 __assert_fail (const char *assertion
,
188 const char *file
, unsigned int line
, const char *function
)
191 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
192 file
, line
, function
?: "", function
? ": " : "",
198 __assert_perror_fail (int errnum
,
199 const char *file
, unsigned int line
,
200 const char *function
)
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
)));
211 unsigned long int weak_function
212 __strtoul_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 || (result
== ULONG_MAX
/ 10 && digval
> ULONG_MAX
% 10))
256 *endptr
= (char *) nptr
;
265 *endptr
= (char *) nptr
;
266 return result
* sign
;