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 (const char *nptr
,
44 extern unsigned long int weak_function
strtoul (const char *nptr
,
45 char **endptr
, int base
);
52 #define _dl_zerofd (-1)
54 extern int _dl_zerofd
;
57 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
63 /* Consume any unused space in the last page of our data segment. */
64 extern int _end attribute_hidden
;
66 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0)
67 + GL(dl_pagesize
) - 1)
68 & ~(GL(dl_pagesize
) - 1));
71 /* Make sure the allocation pointer is ideally aligned. */
72 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + sizeof (double) - 1)
73 & ~(sizeof (double) - 1));
75 if (alloc_ptr
+ n
>= alloc_end
)
77 /* Insufficient space left; allocate another page. */
79 size_t nup
= (n
+ GL(dl_pagesize
) - 1) & ~(GL(dl_pagesize
) - 1);
80 page
= __mmap (0, nup
, PROT_READ
|PROT_WRITE
,
81 MAP_ANON
|MAP_PRIVATE
, _dl_zerofd
, 0);
82 assert (page
!= MAP_FAILED
);
83 if (page
!= alloc_end
)
85 alloc_end
= page
+ nup
;
88 alloc_last_block
= (void *) alloc_ptr
;
90 return alloc_last_block
;
93 /* We use this function occasionally since the real implementation may
94 be optimized when it can assume the memory it returns already is
97 calloc (size_t nmemb
, size_t size
)
99 size_t total
= nmemb
* size
;
100 void *result
= malloc (total
);
101 return memset (result
, '\0', total
);
104 /* This will rarely be called. */
108 /* We can free only the last block allocated. */
109 if (ptr
== alloc_last_block
)
110 alloc_ptr
= alloc_last_block
;
113 /* This is only called with the most recent block returned by malloc. */
115 realloc (void *ptr
, size_t n
)
120 assert (ptr
== alloc_last_block
);
121 alloc_ptr
= alloc_last_block
;
127 /* Return alligned memory block. */
129 __libc_memalign (size_t align
, size_t n
)
131 void *newp
= malloc (n
+ align
- 1);
133 return (void *) roundup ((uintptr_t) newp
, align
);
136 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
141 __sigjmp_save (sigjmp_buf env
, int savemask
__attribute__ ((unused
)))
143 env
[0].__mask_was_saved
= 0;
148 longjmp (jmp_buf env
, int val
)
150 __longjmp (env
[0].__jmpbuf
, val
);
153 /* Define our own version of the internal function used by strerror. We
154 only provide the messages for some common errors. This avoids pulling
155 in the whole error list. */
158 __strerror_r (int errnum
, char *buf
, size_t buflen
)
165 msg
= (char *) "Cannot allocate memory";
168 msg
= (char *) "Invalid argument";
171 msg
= (char *) "No such file or directory";
174 msg
= (char *) "Operation not permitted";
177 msg
= (char *) "Input/output error";
180 msg
= (char *) "Permission denied";
183 /* No need to check buffer size, all calls in the dynamic linker
184 provide enough space. */
185 buf
[buflen
- 1] = '\0';
186 msg
= _itoa (errnum
, buf
+ buflen
- 1, 10, 0);
187 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
188 sizeof ("Error ") - 1);
197 /* Define (weakly) our own assert failure function which doesn't use stdio.
198 If we are linked into the user program (-ldl), the normal __assert_fail
199 defn can override this one. */
202 __assert_fail (const char *assertion
,
203 const char *file
, unsigned int line
, const char *function
)
206 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
207 file
, line
, function
?: "", function
? ": " : "",
213 __assert_perror_fail (int errnum
,
214 const char *file
, unsigned int line
,
215 const char *function
)
219 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n",
220 file
, line
, function
?: "", function
? ": " : "",
221 __strerror_r (errnum
, errbuf
, sizeof (errbuf
)));
226 unsigned long int weak_function
227 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
229 unsigned long int result
= 0;
232 while (*nptr
== ' ' || *nptr
== '\t')
240 else if (*nptr
== '+')
243 if (*nptr
< '0' || *nptr
> '9')
246 *endptr
= (char *) nptr
;
254 if (nptr
[1] == 'x' || nptr
[1] == 'X')
263 while (*nptr
>= '0' && *nptr
<= '9')
265 unsigned long int digval
= *nptr
- '0';
266 if (result
> LONG_MAX
/ 10
267 || (result
== ULONG_MAX
/ 10 && digval
> ULONG_MAX
% 10))
271 *endptr
= (char *) nptr
;
280 *endptr
= (char *) nptr
;
281 return result
* sign
;
285 /* We always use _itoa instead of _itoa_word in ld.so since the former
286 also has to be present and it is never about speed when these
287 functions are used. */
289 _itoa (value
, buflim
, base
, upper_case
)
290 unsigned long long int value
;
295 extern const char INTUSE(_itoa_lower_digits
)[] attribute_hidden
;
297 assert (! upper_case
);
300 *--buflim
= INTUSE(_itoa_lower_digits
)[value
% base
];
301 while ((value
/= base
) != 0);
307 /* The following is not a complete strsep implementation. It cannot
308 handle empty delimiter strings. But this isn't necessary for the
309 execution of ld.so. */
313 __strsep (char **stringp
, const char *delim
)
317 assert (delim
[0] != '\0');
324 while (*end
!= '\0' || (end
= NULL
))
326 const char *dp
= delim
;
331 while (*++dp
!= '\0');
347 weak_alias (__strsep
, strsep
)
348 strong_alias (__strsep
, __strsep_g
)
351 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
352 up to 36. We don't need this here. */
353 const char INTUSE(_itoa_lower_digits
)[16] attribute_hidden
354 = "0123456789abcdef";
358 /* The 'errno' in ld.so is not exported. */
359 extern int errno attribute_hidden
;
362 __errno_location (void)