1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-2017 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, see
17 <http://www.gnu.org/licenses/>. */
26 #include <sys/param.h>
27 #include <sys/types.h>
30 #include <malloc/malloc-internal.h>
34 /* Minimal malloc allocator for used during initial link. After the
35 initial link, a full malloc implementation is interposed, either
36 the one in libc, or a different one supplied by the user through
39 static void *alloc_ptr
, *alloc_end
, *alloc_last_block
;
41 /* Declarations of global functions. */
42 extern void weak_function
free (void *ptr
);
43 extern void * weak_function
realloc (void *ptr
, size_t n
);
44 extern unsigned long int weak_function
__strtoul_internal (const char *nptr
,
48 extern unsigned long int weak_function
strtoul (const char *nptr
,
49 char **endptr
, int base
);
52 /* Allocate an aligned memory block. */
58 /* Consume any unused space in the last page of our data segment. */
59 extern int _end attribute_hidden
;
61 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0)
62 + GLRO(dl_pagesize
) - 1)
63 & ~(GLRO(dl_pagesize
) - 1));
66 /* Make sure the allocation pointer is ideally aligned. */
67 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + MALLOC_ALIGNMENT
- 1)
68 & ~(MALLOC_ALIGNMENT
- 1));
70 if (alloc_ptr
+ n
>= alloc_end
|| n
>= -(uintptr_t) alloc_ptr
)
72 /* Insufficient space left; allocate another page plus one extra
73 page to reduce number of mmap calls. */
75 size_t nup
= (n
+ GLRO(dl_pagesize
) - 1) & ~(GLRO(dl_pagesize
) - 1);
76 if (__glibc_unlikely (nup
== 0 && n
!= 0))
78 nup
+= GLRO(dl_pagesize
);
79 page
= __mmap (0, nup
, PROT_READ
|PROT_WRITE
,
80 MAP_ANON
|MAP_PRIVATE
, -1, 0);
81 if (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 /* New memory from the trivial malloc above is always already cleared.
100 (We make sure that's true in the rare occasion it might not be,
101 by clearing memory in free, below.) */
102 size_t bytes
= nmemb
* size
;
104 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
105 if (__builtin_expect ((nmemb
| size
) >= HALF_SIZE_T
, 0)
106 && size
!= 0 && bytes
/ size
!= nmemb
)
109 return malloc (bytes
);
112 /* This will rarely be called. */
116 /* We can free only the last block allocated. */
117 if (ptr
== alloc_last_block
)
119 /* Since this is rare, we clear the freed block here
120 so that calloc can presume malloc returns cleared memory. */
121 memset (alloc_last_block
, '\0', alloc_ptr
- alloc_last_block
);
122 alloc_ptr
= alloc_last_block
;
126 /* This is only called with the most recent block returned by malloc. */
128 realloc (void *ptr
, size_t n
)
132 assert (ptr
== alloc_last_block
);
133 size_t old_size
= alloc_ptr
- alloc_last_block
;
134 alloc_ptr
= alloc_last_block
;
135 void *new = malloc (n
);
136 return new != ptr
? memcpy (new, ptr
, old_size
) : new;
139 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
144 __sigjmp_save (sigjmp_buf env
, int savemask
__attribute__ ((unused
)))
146 env
[0].__mask_was_saved
= 0;
150 /* Define our own version of the internal function used by strerror. We
151 only provide the messages for some common errors. This avoids pulling
152 in the whole error list. */
155 __strerror_r (int errnum
, char *buf
, size_t buflen
)
162 msg
= (char *) "Cannot allocate memory";
165 msg
= (char *) "Invalid argument";
168 msg
= (char *) "No such file or directory";
171 msg
= (char *) "Operation not permitted";
174 msg
= (char *) "Input/output error";
177 msg
= (char *) "Permission denied";
180 /* No need to check buffer size, all calls in the dynamic linker
181 provide enough space. */
182 buf
[buflen
- 1] = '\0';
183 msg
= _itoa (errnum
, buf
+ buflen
- 1, 10, 0);
184 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
185 sizeof ("Error ") - 1);
193 __libc_fatal (const char *message
)
195 _dl_fatal_printf ("%s", message
);
197 rtld_hidden_def (__libc_fatal
)
200 __attribute__ ((noreturn
))
205 rtld_hidden_def (__chk_fail
)
208 /* Define (weakly) our own assert failure function which doesn't use stdio.
209 If we are linked into the user program (-ldl), the normal __assert_fail
210 defn can override this one. */
213 __assert_fail (const char *assertion
,
214 const char *file
, unsigned int line
, const char *function
)
217 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
218 file
, line
, function
?: "", function
? ": " : "",
222 rtld_hidden_weak (__assert_fail
)
225 __assert_perror_fail (int errnum
,
226 const char *file
, unsigned int line
,
227 const char *function
)
231 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
232 file
, line
, function
?: "", function
? ": " : "",
233 __strerror_r (errnum
, errbuf
, sizeof errbuf
));
236 rtld_hidden_weak (__assert_perror_fail
)
239 unsigned long int weak_function
240 __strtoul_internal (const char *nptr
, char **endptr
, int base
, int group
)
242 unsigned long int result
= 0;
246 while (*nptr
== ' ' || *nptr
== '\t')
254 else if (*nptr
== '+')
257 if (*nptr
< '0' || *nptr
> '9')
260 *endptr
= (char *) nptr
;
269 if (nptr
[1] == 'x' || nptr
[1] == 'X')
283 unsigned long int digval
;
284 if (*nptr
>= '0' && *nptr
<= '0' + max_digit
)
285 digval
= *nptr
- '0';
288 if (*nptr
>= 'a' && *nptr
<= 'f')
289 digval
= *nptr
- 'a' + 10;
290 else if (*nptr
>= 'A' && *nptr
<= 'F')
291 digval
= *nptr
- 'A' + 10;
298 if (result
> ULONG_MAX
/ base
299 || (result
== ULONG_MAX
/ base
&& digval
> ULONG_MAX
% base
))
303 *endptr
= (char *) nptr
;
312 *endptr
= (char *) nptr
;
313 return result
* sign
;
318 /* We always use _itoa instead of _itoa_word in ld.so since the former
319 also has to be present and it is never about speed when these
320 functions are used. */
322 _itoa (unsigned long long int value
, char *buflim
, unsigned int base
,
325 assert (! upper_case
);
328 *--buflim
= _itoa_lower_digits
[value
% base
];
329 while ((value
/= base
) != 0);
334 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
335 up to 36. We don't need this here. */
336 const char _itoa_lower_digits
[16] = "0123456789abcdef";
337 rtld_hidden_data_def (_itoa_lower_digits
)
339 /* The following is not a complete strsep implementation. It cannot
340 handle empty delimiter strings. But this isn't necessary for the
341 execution of ld.so. */
345 __strsep (char **stringp
, const char *delim
)
349 assert (delim
[0] != '\0');
356 while (*end
!= '\0' || (end
= NULL
))
358 const char *dp
= delim
;
363 while (*++dp
!= '\0');
379 weak_alias (__strsep
, strsep
)
380 strong_alias (__strsep
, __strsep_g
)