]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-minimal.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / elf / dl-minimal.c
CommitLineData
42d2676e 1/* Minimal replacements for basic facilities used in the dynamic linker.
04277e02 2 Copyright (C) 1995-2019 Free Software Foundation, Inc.
afd4eb37
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
afd4eb37
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
afd4eb37 14
41bdb6e2 15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
42d2676e 18
a853022c 19#include <errno.h>
27a5bb33 20#include <limits.h>
2193ce87 21#include <stdio.h>
a853022c 22#include <string.h>
739d440d 23#include <tls.h>
a853022c 24#include <unistd.h>
42d2676e 25#include <sys/mman.h>
aed283dd
UD
26#include <sys/param.h>
27#include <sys/types.h>
a42195db 28#include <ldsodefs.h>
eb96ffb0 29#include <_itoa.h>
5bc17330 30#include <malloc/malloc-internal.h>
a853022c
UD
31
32#include <assert.h>
42d2676e 33
b04beebf
FW
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
37 interposition. */
42d2676e
RM
38
39static void *alloc_ptr, *alloc_end, *alloc_last_block;
40
728c24ff
AJ
41/* Declarations of global functions. */
42extern void weak_function free (void *ptr);
43extern void * weak_function realloc (void *ptr, size_t n);
728c24ff
AJ
44
45
a816b435 46/* Allocate an aligned memory block. */
af5b3bc3 47void * weak_function
b04beebf 48malloc (size_t n)
42d2676e 49{
42d2676e
RM
50 if (alloc_end == 0)
51 {
52 /* Consume any unused space in the last page of our data segment. */
36461455 53 extern int _end attribute_hidden;
42d2676e 54 alloc_ptr = &_end;
d6b5d570 55 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
afdca0f2
UD
56 + GLRO(dl_pagesize) - 1)
57 & ~(GLRO(dl_pagesize) - 1));
42d2676e
RM
58 }
59
60 /* Make sure the allocation pointer is ideally aligned. */
b04beebf
FW
61 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + MALLOC_ALIGNMENT - 1)
62 & ~(MALLOC_ALIGNMENT - 1));
42d2676e 63
1311e86e 64 if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
42d2676e 65 {
2bc983b7
L
66 /* Insufficient space left; allocate another page plus one extra
67 page to reduce number of mmap calls. */
42d2676e 68 caddr_t page;
afdca0f2 69 size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
2bc983b7
L
70 if (__glibc_unlikely (nup == 0 && n != 0))
71 return NULL;
72 nup += GLRO(dl_pagesize);
ea7eb7e3 73 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
20739e54 74 MAP_ANON|MAP_PRIVATE, -1, 0);
1311e86e
UD
75 if (page == MAP_FAILED)
76 return NULL;
42d2676e
RM
77 if (page != alloc_end)
78 alloc_ptr = page;
ea7eb7e3 79 alloc_end = page + nup;
42d2676e
RM
80 }
81
82 alloc_last_block = (void *) alloc_ptr;
83 alloc_ptr += n;
84 return alloc_last_block;
85}
42d2676e 86
c131718c
UD
87/* We use this function occasionally since the real implementation may
88 be optimized when it can assume the memory it returns already is
89 set to NUL. */
90void * weak_function
91calloc (size_t nmemb, size_t size)
92{
fde89ad0
RM
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.) */
1311e86e
UD
96 size_t bytes = nmemb * size;
97
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)
101 return NULL;
102
103 return malloc (bytes);
c131718c
UD
104}
105
42d2676e 106/* This will rarely be called. */
af5b3bc3 107void weak_function
42d2676e
RM
108free (void *ptr)
109{
110 /* We can free only the last block allocated. */
111 if (ptr == alloc_last_block)
fde89ad0
RM
112 {
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;
117 }
42d2676e 118}
42d2676e 119
efec1d0c 120/* This is only called with the most recent block returned by malloc. */
af5b3bc3 121void * weak_function
42d2676e 122realloc (void *ptr, size_t n)
efec1d0c 123{
d252c479
UD
124 if (ptr == NULL)
125 return malloc (n);
efec1d0c 126 assert (ptr == alloc_last_block);
90a0991a 127 size_t old_size = alloc_ptr - alloc_last_block;
efec1d0c 128 alloc_ptr = alloc_last_block;
90a0991a 129 void *new = malloc (n);
ff8d96be 130 return new != ptr ? memcpy (new, ptr, old_size) : new;
efec1d0c 131}
42d2676e
RM
132\f
133/* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
134
135#include <setjmp.h>
136
af5b3bc3 137int weak_function
1e71bd52 138__sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
ca34d7a7 139{
1e71bd52 140 env[0].__mask_was_saved = 0;
ca34d7a7
UD
141 return 0;
142}
42d2676e 143\f
da832465
UD
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. */
42d2676e 147
af5b3bc3 148char * weak_function
310b3460 149__strerror_r (int errnum, char *buf, size_t buflen)
42d2676e 150{
da832465
UD
151 char *msg;
152
153 switch (errnum)
154 {
155 case ENOMEM:
156 msg = (char *) "Cannot allocate memory";
157 break;
158 case EINVAL:
159 msg = (char *) "Invalid argument";
160 break;
161 case ENOENT:
162 msg = (char *) "No such file or directory";
163 break;
164 case EPERM:
165 msg = (char *) "Operation not permitted";
166 break;
167 case EIO:
168 msg = (char *) "Input/output error";
169 break;
170 case EACCES:
171 msg = (char *) "Permission denied";
172 break;
173 default:
174 /* No need to check buffer size, all calls in the dynamic linker
175 provide enough space. */
176 buf[buflen - 1] = '\0';
9710f75d 177 msg = _itoa (errnum, buf + buflen - 1, 10, 0);
da832465
UD
178 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
179 sizeof ("Error ") - 1);
180 break;
181 }
182
183 return msg;
42d2676e 184}
42d2676e 185\f
2193ce87
RM
186void
187__libc_fatal (const char *message)
188{
189 _dl_fatal_printf ("%s", message);
190}
191rtld_hidden_def (__libc_fatal)
42d2676e 192
2193ce87
RM
193void
194__attribute__ ((noreturn))
195__chk_fail (void)
196{
197 _exit (127);
198}
199rtld_hidden_def (__chk_fail)
200
201#ifndef NDEBUG
42d2676e
RM
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. */
205
af5b3bc3 206void weak_function
42d2676e
RM
207__assert_fail (const char *assertion,
208 const char *file, unsigned int line, const char *function)
209{
35fc382a 210 _dl_fatal_printf ("\
28966366 211Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
35fc382a
UD
212 file, line, function ?: "", function ? ": " : "",
213 assertion);
42d2676e
RM
214
215}
2193ce87 216rtld_hidden_weak (__assert_fail)
42d2676e 217
7a9ce79a
RM
218void weak_function
219__assert_perror_fail (int errnum,
220 const char *file, unsigned int line,
221 const char *function)
222{
223 char errbuf[400];
224 _dl_fatal_printf ("\
225Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
226 file, line, function ?: "", function ? ": " : "",
227 __strerror_r (errnum, errbuf, sizeof errbuf));
228
229}
230rtld_hidden_weak (__assert_perror_fail)
42d2676e 231#endif
2193ce87 232\f
8aaf53ee 233#undef _itoa
9710f75d
UD
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. */
ccdf0cab 237char *
9dd346ff
JM
238_itoa (unsigned long long int value, char *buflim, unsigned int base,
239 int upper_case)
ccdf0cab 240{
9710f75d 241 assert (! upper_case);
ccdf0cab
UD
242
243 do
5be8418c 244 *--buflim = _itoa_lower_digits[value % base];
9710f75d 245 while ((value /= base) != 0);
ccdf0cab 246
9710f75d 247 return buflim;
ccdf0cab 248}
5c1159b6 249
2193ce87
RM
250/* The '_itoa_lower_digits' variable in libc.so is able to handle bases
251 up to 36. We don't need this here. */
252const char _itoa_lower_digits[16] = "0123456789abcdef";
253rtld_hidden_data_def (_itoa_lower_digits)
254\f
5c1159b6
UD
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. */
258#undef strsep
259#undef __strsep
260char *
261__strsep (char **stringp, const char *delim)
262{
263 char *begin;
264
9710f75d
UD
265 assert (delim[0] != '\0');
266
5c1159b6
UD
267 begin = *stringp;
268 if (begin != NULL)
269 {
270 char *end = begin;
271
272 while (*end != '\0' || (end = NULL))
273 {
274 const char *dp = delim;
275
276 do
277 if (*dp == *end)
278 break;
279 while (*++dp != '\0');
280
281 if (*dp != '\0')
282 {
283 *end++ = '\0';
284 break;
285 }
286
287 ++end;
288 }
289
290 *stringp = end;
291 }
292
293 return begin;
294}
295weak_alias (__strsep, strsep)
296strong_alias (__strsep, __strsep_g)