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.
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)
64 + GL(dl_pagesize
) - 1)
65 & ~(GL(dl_pagesize
) - 1));
68 /* Make sure the allocation pointer is ideally aligned. */
69 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + sizeof (double) - 1)
70 & ~(sizeof (double) - 1));
72 if (alloc_ptr
+ n
>= alloc_end
)
74 /* Insufficient space left; allocate another page. */
76 size_t nup
= (n
+ GL(dl_pagesize
) - 1) & ~(GL(dl_pagesize
) - 1);
77 page
= __mmap (0, nup
, PROT_READ
|PROT_WRITE
,
78 MAP_ANON
|MAP_PRIVATE
, _dl_zerofd
, 0);
79 assert (page
!= MAP_FAILED
);
80 if (page
!= alloc_end
)
82 alloc_end
= page
+ nup
;
85 alloc_last_block
= (void *) alloc_ptr
;
87 return alloc_last_block
;
90 /* We use this function occasionally since the real implementation may
91 be optimized when it can assume the memory it returns already is
94 calloc (size_t nmemb
, size_t size
)
96 size_t total
= nmemb
* size
;
97 void *result
= malloc (total
);
98 return memset (result
, '\0', total
);
101 /* This will rarely be called. */
105 /* We can free only the last block allocated. */
106 if (ptr
== alloc_last_block
)
107 alloc_ptr
= alloc_last_block
;
110 /* This is only called with the most recent block returned by malloc. */
112 realloc (void *ptr
, size_t n
)
117 assert (ptr
== alloc_last_block
);
118 alloc_ptr
= alloc_last_block
;
124 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
129 __sigjmp_save (sigjmp_buf env
, int savemask
__attribute__ ((unused
)))
131 env
[0].__mask_was_saved
= 0;
136 longjmp (jmp_buf env
, int val
)
138 __longjmp (env
[0].__jmpbuf
, val
);
141 /* Define our own version of the internal function used by strerror. We
142 only provide the messages for some common errors. This avoids pulling
143 in the whole error list. */
146 __strerror_r (int errnum
, char *buf
, size_t buflen
)
153 msg
= (char *) "Cannot allocate memory";
156 msg
= (char *) "Invalid argument";
159 msg
= (char *) "No such file or directory";
162 msg
= (char *) "Operation not permitted";
165 msg
= (char *) "Input/output error";
168 msg
= (char *) "Permission denied";
171 /* No need to check buffer size, all calls in the dynamic linker
172 provide enough space. */
173 buf
[buflen
- 1] = '\0';
174 msg
= _itoa_word (errnum
, buf
+ buflen
- 1, 10, 0);
175 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
176 sizeof ("Error ") - 1);
185 /* Define (weakly) our own assert failure function which doesn't use stdio.
186 If we are linked into the user program (-ldl), the normal __assert_fail
187 defn can override this one. */
190 __assert_fail (const char *assertion
,
191 const char *file
, unsigned int line
, const char *function
)
194 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
195 file
, line
, function
?: "", function
? ": " : "",
201 __assert_perror_fail (int errnum
,
202 const char *file
, unsigned int line
,
203 const char *function
)
207 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
208 file
, line
, function
?: "", function
? ": " : "",
209 __strerror_r (errnum
, errbuf
, sizeof (errbuf
)));
214 unsigned long int weak_function
215 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
217 unsigned long int result
= 0;
220 while (*nptr
== ' ' || *nptr
== '\t')
228 else if (*nptr
== '+')
231 if (*nptr
< '0' || *nptr
> '9')
234 *endptr
= (char *) nptr
;
242 if (nptr
[1] == 'x' || nptr
[1] == 'X')
251 while (*nptr
>= '0' && *nptr
<= '9')
253 unsigned long int digval
= *nptr
- '0';
254 if (result
> LONG_MAX
/ 10
255 || (result
== ULONG_MAX
/ 10 && digval
> ULONG_MAX
% 10))
259 *endptr
= (char *) nptr
;
268 *endptr
= (char *) nptr
;
269 return result
* sign
;
273 #if HP_TIMING_AVAIL && ULONG_MAX <= 4294967295UL
274 /* We need this function to print the cycle count. On 64-bit machines the
275 _itoa_word function should be used. */
277 _itoa (value
, buflim
, base
, upper_case
)
278 unsigned long long int value
;
288 *--bp
= '0' + value
% 10;
289 while ((value
/= 10) != 0);