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
25 #include <sys/param.h>
26 #include <sys/types.h>
28 #include <stdio-common/_itoa.h>
32 /* Minimal `malloc' allocator for use while loading shared libraries.
33 No block is ever freed. */
35 static void *alloc_ptr
, *alloc_end
, *alloc_last_block
;
37 /* Declarations of global functions. */
38 extern void weak_function
free (void *ptr
);
39 extern void * weak_function
realloc (void *ptr
, size_t n
);
40 extern unsigned long int weak_function __strtoul_internal
41 (const char *nptr
, char **endptr
, int base
, int group
);
42 extern unsigned long int weak_function
strtoul (const char *nptr
,
43 char **endptr
, int base
);
50 #define _dl_zerofd (-1)
52 extern int _dl_zerofd
;
55 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
61 /* Consume any unused space in the last page of our data segment. */
64 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0)
65 + GL(dl_pagesize
) - 1)
66 & ~(GL(dl_pagesize
) - 1));
69 /* Make sure the allocation pointer is ideally aligned. */
70 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + sizeof (double) - 1)
71 & ~(sizeof (double) - 1));
73 if (alloc_ptr
+ n
>= alloc_end
)
75 /* Insufficient space left; allocate another page. */
77 size_t nup
= (n
+ GL(dl_pagesize
) - 1) & ~(GL(dl_pagesize
) - 1);
78 page
= __mmap (0, nup
, PROT_READ
|PROT_WRITE
,
79 MAP_ANON
|MAP_PRIVATE
, _dl_zerofd
, 0);
80 assert (page
!= MAP_FAILED
);
81 if (page
!= alloc_end
)
83 alloc_end
= page
+ nup
;
86 alloc_last_block
= (void *) alloc_ptr
;
88 return alloc_last_block
;
91 /* We use this function occasionally since the real implementation may
92 be optimized when it can assume the memory it returns already is
95 calloc (size_t nmemb
, size_t size
)
97 size_t total
= nmemb
* size
;
98 void *result
= malloc (total
);
99 return memset (result
, '\0', total
);
102 /* This will rarely be called. */
106 /* We can free only the last block allocated. */
107 if (ptr
== alloc_last_block
)
108 alloc_ptr
= alloc_last_block
;
111 /* This is only called with the most recent block returned by malloc. */
113 realloc (void *ptr
, size_t n
)
118 assert (ptr
== alloc_last_block
);
119 alloc_ptr
= alloc_last_block
;
125 /* Return alligned memory block. */
127 __libc_memalign (size_t align
, size_t n
)
129 void *newp
= malloc (n
+ align
- 1);
131 return (void *) roundup ((uintptr_t) newp
, align
);
134 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
139 __sigjmp_save (sigjmp_buf env
, int savemask
__attribute__ ((unused
)))
141 env
[0].__mask_was_saved
= 0;
146 longjmp (jmp_buf env
, int val
)
148 __longjmp (env
[0].__jmpbuf
, val
);
151 /* Define our own version of the internal function used by strerror. We
152 only provide the messages for some common errors. This avoids pulling
153 in the whole error list. */
156 __strerror_r (int errnum
, char *buf
, size_t buflen
)
163 msg
= (char *) "Cannot allocate memory";
166 msg
= (char *) "Invalid argument";
169 msg
= (char *) "No such file or directory";
172 msg
= (char *) "Operation not permitted";
175 msg
= (char *) "Input/output error";
178 msg
= (char *) "Permission denied";
181 /* No need to check buffer size, all calls in the dynamic linker
182 provide enough space. */
183 buf
[buflen
- 1] = '\0';
184 msg
= _itoa (errnum
, buf
+ buflen
- 1, 10, 0);
185 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
186 sizeof ("Error ") - 1);
195 /* Define (weakly) our own assert failure function which doesn't use stdio.
196 If we are linked into the user program (-ldl), the normal __assert_fail
197 defn can override this one. */
200 __assert_fail (const char *assertion
,
201 const char *file
, unsigned int line
, const char *function
)
204 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
205 file
, line
, function
?: "", function
? ": " : "",
211 __assert_perror_fail (int errnum
,
212 const char *file
, unsigned int line
,
213 const char *function
)
217 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
218 file
, line
, function
?: "", function
? ": " : "",
219 __strerror_r (errnum
, errbuf
, sizeof (errbuf
)));
224 unsigned long int weak_function
225 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
227 unsigned long int result
= 0;
230 while (*nptr
== ' ' || *nptr
== '\t')
238 else if (*nptr
== '+')
241 if (*nptr
< '0' || *nptr
> '9')
244 *endptr
= (char *) nptr
;
252 if (nptr
[1] == 'x' || nptr
[1] == 'X')
261 while (*nptr
>= '0' && *nptr
<= '9')
263 unsigned long int digval
= *nptr
- '0';
264 if (result
> LONG_MAX
/ 10
265 || (result
== ULONG_MAX
/ 10 && digval
> ULONG_MAX
% 10))
269 *endptr
= (char *) nptr
;
278 *endptr
= (char *) nptr
;
279 return result
* sign
;
283 /* We always use _itoa instead of _itoa_word in ld.so since the former
284 also has to be present and it is never about speed when these
285 functions are used. */
287 _itoa (value
, buflim
, base
, upper_case
)
288 unsigned long long int value
;
293 extern const char _itoa_lower_digits
[];
295 assert (! upper_case
);
298 *--buflim
= _itoa_lower_digits
[value
% base
];
299 while ((value
/= base
) != 0);
305 /* The following is not a complete strsep implementation. It cannot
306 handle empty delimiter strings. But this isn't necessary for the
307 execution of ld.so. */
311 __strsep (char **stringp
, const char *delim
)
315 assert (delim
[0] != '\0');
322 while (*end
!= '\0' || (end
= NULL
))
324 const char *dp
= delim
;
329 while (*++dp
!= '\0');
345 weak_alias (__strsep
, strsep
)
346 strong_alias (__strsep
, __strsep_g
)
349 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
350 up to 36. We don't need this here. */
351 const char _itoa_lower_digits
[16] = "0123456789abcdef";