1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-1998,2000-2002,2004-2006,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
27 #include <sys/param.h>
28 #include <sys/types.h>
30 #include <stdio-common/_itoa.h>
34 /* Minimal `malloc' allocator for use while loading shared libraries.
35 No block is ever freed. */
37 static void *alloc_ptr
, *alloc_end
, *alloc_last_block
;
39 /* Declarations of global functions. */
40 extern void weak_function
free (void *ptr
);
41 extern void * weak_function
realloc (void *ptr
, size_t n
);
42 extern unsigned long int weak_function
__strtoul_internal (const char *nptr
,
46 extern unsigned long int weak_function
strtoul (const char *nptr
,
47 char **endptr
, int base
);
50 /* Allocate an aligned memory block. */
52 __libc_memalign (size_t align
, size_t n
)
55 #define _dl_zerofd (-1)
57 extern int _dl_zerofd
;
60 _dl_zerofd
= _dl_sysdep_open_zero_fill ();
66 /* Consume any unused space in the last page of our data segment. */
67 extern int _end attribute_hidden
;
69 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0)
70 + GLRO(dl_pagesize
) - 1)
71 & ~(GLRO(dl_pagesize
) - 1));
74 /* Make sure the allocation pointer is ideally aligned. */
75 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + align
- 1)
78 if (alloc_ptr
+ n
>= alloc_end
|| n
>= -(uintptr_t) alloc_ptr
)
80 /* Insufficient space left; allocate another page. */
82 size_t nup
= (n
+ GLRO(dl_pagesize
) - 1) & ~(GLRO(dl_pagesize
) - 1);
83 if (__builtin_expect (nup
== 0, 0))
87 nup
= GLRO(dl_pagesize
);
89 page
= __mmap (0, nup
, PROT_READ
|PROT_WRITE
,
90 MAP_ANON
|MAP_PRIVATE
, _dl_zerofd
, 0);
91 if (page
== MAP_FAILED
)
93 if (page
!= alloc_end
)
95 alloc_end
= page
+ nup
;
98 alloc_last_block
= (void *) alloc_ptr
;
100 return alloc_last_block
;
106 return __libc_memalign (sizeof (double), n
);
109 /* We use this function occasionally since the real implementation may
110 be optimized when it can assume the memory it returns already is
113 calloc (size_t nmemb
, size_t size
)
115 /* New memory from the trivial malloc above is always already cleared.
116 (We make sure that's true in the rare occasion it might not be,
117 by clearing memory in free, below.) */
118 size_t bytes
= nmemb
* size
;
120 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
121 if (__builtin_expect ((nmemb
| size
) >= HALF_SIZE_T
, 0)
122 && size
!= 0 && bytes
/ size
!= nmemb
)
125 return malloc (bytes
);
128 /* This will rarely be called. */
132 /* We can free only the last block allocated. */
133 if (ptr
== alloc_last_block
)
135 /* Since this is rare, we clear the freed block here
136 so that calloc can presume malloc returns cleared memory. */
137 memset (alloc_last_block
, '\0', alloc_ptr
- alloc_last_block
);
138 alloc_ptr
= alloc_last_block
;
142 /* This is only called with the most recent block returned by malloc. */
144 realloc (void *ptr
, size_t n
)
148 assert (ptr
== alloc_last_block
);
149 size_t old_size
= alloc_ptr
- alloc_last_block
;
150 alloc_ptr
= alloc_last_block
;
151 void *new = malloc (n
);
152 return new != ptr
? memcpy (new, ptr
, old_size
) : new;
155 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
160 __sigjmp_save (sigjmp_buf env
, int savemask
__attribute__ ((unused
)))
162 env
[0].__mask_was_saved
= 0;
166 /* Define our own version of the internal function used by strerror. We
167 only provide the messages for some common errors. This avoids pulling
168 in the whole error list. */
171 __strerror_r (int errnum
, char *buf
, size_t buflen
)
178 msg
= (char *) "Cannot allocate memory";
181 msg
= (char *) "Invalid argument";
184 msg
= (char *) "No such file or directory";
187 msg
= (char *) "Operation not permitted";
190 msg
= (char *) "Input/output error";
193 msg
= (char *) "Permission denied";
196 /* No need to check buffer size, all calls in the dynamic linker
197 provide enough space. */
198 buf
[buflen
- 1] = '\0';
199 msg
= _itoa (errnum
, buf
+ buflen
- 1, 10, 0);
200 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
201 sizeof ("Error ") - 1);
210 /* Define (weakly) our own assert failure function which doesn't use stdio.
211 If we are linked into the user program (-ldl), the normal __assert_fail
212 defn can override this one. */
215 __assert_fail (const char *assertion
,
216 const char *file
, unsigned int line
, const char *function
)
219 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
220 file
, line
, function
?: "", function
? ": " : "",
224 rtld_hidden_weak(__assert_fail
)
227 __assert_perror_fail (int errnum
,
228 const char *file
, unsigned int line
,
229 const char *function
)
233 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
234 file
, line
, function
?: "", function
? ": " : "",
235 __strerror_r (errnum
, errbuf
, sizeof errbuf
));
238 rtld_hidden_weak (__assert_perror_fail
)
241 unsigned long int weak_function
242 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
244 unsigned long int result
= 0;
247 while (*nptr
== ' ' || *nptr
== '\t')
255 else if (*nptr
== '+')
258 if (*nptr
< '0' || *nptr
> '9')
261 *endptr
= (char *) nptr
;
269 if (nptr
[1] == 'x' || nptr
[1] == 'X')
278 while (*nptr
>= '0' && *nptr
<= '9')
280 unsigned long int digval
= *nptr
- '0';
281 if (result
> ULONG_MAX
/ 10
282 || (result
== ULONG_MAX
/ 10 && digval
> ULONG_MAX
% 10))
286 *endptr
= (char *) nptr
;
295 *endptr
= (char *) nptr
;
296 return result
* sign
;
301 /* We always use _itoa instead of _itoa_word in ld.so since the former
302 also has to be present and it is never about speed when these
303 functions are used. */
305 _itoa (value
, buflim
, base
, upper_case
)
306 unsigned long long int value
;
311 extern const char INTUSE(_itoa_lower_digits
)[] attribute_hidden
;
313 assert (! upper_case
);
316 *--buflim
= INTUSE(_itoa_lower_digits
)[value
% base
];
317 while ((value
/= base
) != 0);
323 /* The following is not a complete strsep implementation. It cannot
324 handle empty delimiter strings. But this isn't necessary for the
325 execution of ld.so. */
329 __strsep (char **stringp
, const char *delim
)
333 assert (delim
[0] != '\0');
340 while (*end
!= '\0' || (end
= NULL
))
342 const char *dp
= delim
;
347 while (*++dp
!= '\0');
363 weak_alias (__strsep
, strsep
)
364 strong_alias (__strsep
, __strsep_g
)
367 __attribute__ ((noreturn
))
372 rtld_hidden_def (__chk_fail
)
374 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
375 up to 36. We don't need this here. */
376 const char INTUSE(_itoa_lower_digits
)[16] attribute_hidden
377 = "0123456789abcdef";