1 /* Minimal replacements for basic facilities used in the dynamic linker.
2 Copyright (C) 1995-2019 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 <https://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
);
46 /* Allocate an aligned memory block. */
52 /* Consume any unused space in the last page of our data segment. */
53 extern int _end attribute_hidden
;
55 alloc_end
= (void *) 0 + (((alloc_ptr
- (void *) 0)
56 + GLRO(dl_pagesize
) - 1)
57 & ~(GLRO(dl_pagesize
) - 1));
60 /* Make sure the allocation pointer is ideally aligned. */
61 alloc_ptr
= (void *) 0 + (((alloc_ptr
- (void *) 0) + MALLOC_ALIGNMENT
- 1)
62 & ~(MALLOC_ALIGNMENT
- 1));
64 if (alloc_ptr
+ n
>= alloc_end
|| n
>= -(uintptr_t) alloc_ptr
)
66 /* Insufficient space left; allocate another page plus one extra
67 page to reduce number of mmap calls. */
69 size_t nup
= (n
+ GLRO(dl_pagesize
) - 1) & ~(GLRO(dl_pagesize
) - 1);
70 if (__glibc_unlikely (nup
== 0 && n
!= 0))
72 nup
+= GLRO(dl_pagesize
);
73 page
= __mmap (0, nup
, PROT_READ
|PROT_WRITE
,
74 MAP_ANON
|MAP_PRIVATE
, -1, 0);
75 if (page
== MAP_FAILED
)
77 if (page
!= alloc_end
)
79 alloc_end
= page
+ nup
;
82 alloc_last_block
= (void *) alloc_ptr
;
84 return alloc_last_block
;
87 /* We use this function occasionally since the real implementation may
88 be optimized when it can assume the memory it returns already is
91 calloc (size_t nmemb
, size_t size
)
93 /* New memory from the trivial malloc above is always already cleared.
94 (We make sure that's true in the rare occasion it might not be,
95 by clearing memory in free, below.) */
96 size_t bytes
= nmemb
* size
;
98 #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
99 if (__builtin_expect ((nmemb
| size
) >= HALF_SIZE_T
, 0)
100 && size
!= 0 && bytes
/ size
!= nmemb
)
103 return malloc (bytes
);
106 /* This will rarely be called. */
110 /* We can free only the last block allocated. */
111 if (ptr
== alloc_last_block
)
113 /* Since this is rare, we clear the freed block here
114 so that calloc can presume malloc returns cleared memory. */
115 memset (alloc_last_block
, '\0', alloc_ptr
- alloc_last_block
);
116 alloc_ptr
= alloc_last_block
;
120 /* This is only called with the most recent block returned by malloc. */
122 realloc (void *ptr
, size_t n
)
126 assert (ptr
== alloc_last_block
);
127 size_t old_size
= alloc_ptr
- alloc_last_block
;
128 alloc_ptr
= alloc_last_block
;
129 void *new = malloc (n
);
130 return new != ptr
? memcpy (new, ptr
, old_size
) : new;
133 /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
138 __sigjmp_save (sigjmp_buf env
, int savemask
__attribute__ ((unused
)))
140 env
[0].__mask_was_saved
= 0;
144 /* Define our own version of the internal function used by strerror. We
145 only provide the messages for some common errors. This avoids pulling
146 in the whole error list. */
149 __strerror_r (int errnum
, char *buf
, size_t buflen
)
156 msg
= (char *) "Cannot allocate memory";
159 msg
= (char *) "Invalid argument";
162 msg
= (char *) "No such file or directory";
165 msg
= (char *) "Operation not permitted";
168 msg
= (char *) "Input/output error";
171 msg
= (char *) "Permission denied";
174 /* No need to check buffer size, all calls in the dynamic linker
175 provide enough space. */
176 buf
[buflen
- 1] = '\0';
177 msg
= _itoa (errnum
, buf
+ buflen
- 1, 10, 0);
178 msg
= memcpy (msg
- (sizeof ("Error ") - 1), "Error ",
179 sizeof ("Error ") - 1);
187 __libc_fatal (const char *message
)
189 _dl_fatal_printf ("%s", message
);
191 rtld_hidden_def (__libc_fatal
)
194 __attribute__ ((noreturn
))
199 rtld_hidden_def (__chk_fail
)
202 /* Define (weakly) our own assert failure function which doesn't use stdio.
203 If we are linked into the user program (-ldl), the normal __assert_fail
204 defn can override this one. */
207 __assert_fail (const char *assertion
,
208 const char *file
, unsigned int line
, const char *function
)
211 Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
212 file
, line
, function
?: "", function
? ": " : "",
216 rtld_hidden_weak (__assert_fail
)
219 __assert_perror_fail (int errnum
,
220 const char *file
, unsigned int line
,
221 const char *function
)
225 Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
226 file
, line
, function
?: "", function
? ": " : "",
227 __strerror_r (errnum
, errbuf
, sizeof errbuf
));
230 rtld_hidden_weak (__assert_perror_fail
)
234 /* We always use _itoa instead of _itoa_word in ld.so since the former
235 also has to be present and it is never about speed when these
236 functions are used. */
238 _itoa (unsigned long long int value
, char *buflim
, unsigned int base
,
241 assert (! upper_case
);
244 *--buflim
= _itoa_lower_digits
[value
% base
];
245 while ((value
/= base
) != 0);
250 /* The '_itoa_lower_digits' variable in libc.so is able to handle bases
251 up to 36. We don't need this here. */
252 const char _itoa_lower_digits
[16] = "0123456789abcdef";
253 rtld_hidden_data_def (_itoa_lower_digits
)
255 /* The following is not a complete strsep implementation. It cannot
256 handle empty delimiter strings. But this isn't necessary for the
257 execution of ld.so. */
261 __strsep (char **stringp
, const char *delim
)
265 assert (delim
[0] != '\0');
272 while (*end
!= '\0' || (end
= NULL
))
274 const char *dp
= delim
;
279 while (*++dp
!= '\0');
295 weak_alias (__strsep
, strsep
)
296 strong_alias (__strsep
, __strsep_g
)