]> git.ipfire.org Git - thirdparty/glibc.git/blame - elf/dl-minimal.c
Update.
[thirdparty/glibc.git] / elf / dl-minimal.c
CommitLineData
42d2676e 1/* Minimal replacements for basic facilities used in the dynamic linker.
ea7eb7e3 2 Copyright (C) 1995, 1996, 1997, 1998 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
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
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
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
42d2676e 19
a853022c 20#include <errno.h>
27a5bb33 21#include <limits.h>
a853022c
UD
22#include <string.h>
23#include <unistd.h>
42d2676e
RM
24#include <sys/types.h>
25#include <sys/mman.h>
a853022c 26#include <elf/ldsodefs.h>
ca34d7a7 27#include <stdio-common/_itoa.h>
a853022c
UD
28
29#include <assert.h>
42d2676e
RM
30
31/* Minimal `malloc' allocator for use while loading shared libraries.
ea7eb7e3 32 No block is ever freed. */
42d2676e
RM
33
34static void *alloc_ptr, *alloc_end, *alloc_last_block;
35
af5b3bc3 36void * weak_function
42d2676e
RM
37malloc (size_t n)
38{
2064087b
RM
39#ifdef MAP_ANON
40#define _dl_zerofd (-1)
41#else
42d2676e 42 extern int _dl_zerofd;
42d2676e 43
42d2676e
RM
44 if (_dl_zerofd == -1)
45 _dl_zerofd = _dl_sysdep_open_zero_fill ();
2064087b
RM
46#define MAP_ANON 0
47#endif
48
42d2676e
RM
49 if (alloc_end == 0)
50 {
51 /* Consume any unused space in the last page of our data segment. */
52 extern int _end;
53 alloc_ptr = &_end;
266180eb
RM
54 alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) + _dl_pagesize - 1)
55 & ~(_dl_pagesize - 1));
42d2676e
RM
56 }
57
58 /* Make sure the allocation pointer is ideally aligned. */
59 alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1)
60 & ~(sizeof (double) - 1));
61
62 if (alloc_ptr + n >= alloc_end)
63 {
64 /* Insufficient space left; allocate another page. */
65 caddr_t page;
ea7eb7e3
UD
66 size_t nup = (n + _dl_pagesize - 1) & ~(_dl_pagesize - 1);
67 page = __mmap (0, nup, PROT_READ|PROT_WRITE,
266180eb 68 MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
0413b54c 69 assert (page != MAP_FAILED);
42d2676e
RM
70 if (page != alloc_end)
71 alloc_ptr = page;
ea7eb7e3 72 alloc_end = page + nup;
42d2676e
RM
73 }
74
75 alloc_last_block = (void *) alloc_ptr;
76 alloc_ptr += n;
77 return alloc_last_block;
78}
42d2676e 79
c131718c
UD
80/* We use this function occasionally since the real implementation may
81 be optimized when it can assume the memory it returns already is
82 set to NUL. */
83void * weak_function
84calloc (size_t nmemb, size_t size)
85{
86 size_t total = nmemb * size;
87 void *result = malloc (total);
88 return memset (result, '\0', total);
89}
90
42d2676e 91/* This will rarely be called. */
af5b3bc3 92void weak_function
42d2676e
RM
93free (void *ptr)
94{
95 /* We can free only the last block allocated. */
96 if (ptr == alloc_last_block)
97 alloc_ptr = alloc_last_block;
98}
42d2676e 99
efec1d0c 100/* This is only called with the most recent block returned by malloc. */
af5b3bc3 101void * weak_function
42d2676e 102realloc (void *ptr, size_t n)
efec1d0c
RM
103{
104 void *new;
105 assert (ptr == alloc_last_block);
106 alloc_ptr = alloc_last_block;
107 new = malloc (n);
108 assert (new == ptr);
109 return new;
110}
42d2676e
RM
111\f
112/* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */
113
114#include <setjmp.h>
115
af5b3bc3
RM
116int weak_function
117__sigjmp_save (sigjmp_buf env, int savemask)
ca34d7a7
UD
118{
119 env[0].__mask_was_saved = savemask;
120 return 0;
121}
42d2676e 122
af5b3bc3 123void weak_function
ca34d7a7
UD
124longjmp (jmp_buf env, int val)
125{
126 __longjmp (env[0].__jmpbuf, val);
127}
42d2676e 128\f
da832465
UD
129/* Define our own version of the internal function used by strerror. We
130 only provide the messages for some common errors. This avoids pulling
131 in the whole error list. */
42d2676e 132
af5b3bc3 133char * weak_function
310b3460 134__strerror_r (int errnum, char *buf, size_t buflen)
42d2676e 135{
da832465
UD
136 char *msg;
137
138 switch (errnum)
139 {
140 case ENOMEM:
141 msg = (char *) "Cannot allocate memory";
142 break;
143 case EINVAL:
144 msg = (char *) "Invalid argument";
145 break;
146 case ENOENT:
147 msg = (char *) "No such file or directory";
148 break;
149 case EPERM:
150 msg = (char *) "Operation not permitted";
151 break;
152 case EIO:
153 msg = (char *) "Input/output error";
154 break;
155 case EACCES:
156 msg = (char *) "Permission denied";
157 break;
158 default:
159 /* No need to check buffer size, all calls in the dynamic linker
160 provide enough space. */
161 buf[buflen - 1] = '\0';
162 msg = _itoa_word (errnum, buf + buflen - 1, 10, 0);
163 msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
164 sizeof ("Error ") - 1);
165 break;
166 }
167
168 return msg;
42d2676e 169}
42d2676e
RM
170\f
171#ifndef NDEBUG
172
173/* Define (weakly) our own assert failure function which doesn't use stdio.
174 If we are linked into the user program (-ldl), the normal __assert_fail
175 defn can override this one. */
176
af5b3bc3 177void weak_function
42d2676e
RM
178__assert_fail (const char *assertion,
179 const char *file, unsigned int line, const char *function)
180{
181 char buf[64];
182 buf[sizeof buf - 1] = '\0';
183 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
af6f3906 184 file, ": ", _itoa_word (line, buf + sizeof buf - 1, 10, 0),
42d2676e
RM
185 ": ", function ?: "", function ? ": " : "",
186 "Assertion `", assertion, "' failed!\n",
187 NULL);
188
189}
42d2676e 190
af5b3bc3 191void weak_function
42d2676e
RM
192__assert_perror_fail (int errnum,
193 const char *file, unsigned int line,
194 const char *function)
195{
196 char buf[64];
197 buf[sizeof buf - 1] = '\0';
198 _dl_sysdep_fatal ("BUG IN DYNAMIC LINKER ld.so: ",
af6f3906 199 file, ": ", _itoa_word (line, buf + sizeof buf - 1, 10, 0),
42d2676e
RM
200 ": ", function ?: "", function ? ": " : "",
201 "Unexpected error: ", strerror (errnum), "\n", NULL);
202
203}
42d2676e
RM
204
205#endif
27a5bb33
UD
206
207/* This function is only used in eval.c. */
310b3460 208long int weak_function
27a5bb33
UD
209__strtol_internal (const char *nptr, char **endptr, int base, int group)
210{
310b3460 211 unsigned long int result = 0;
27a5bb33
UD
212 long int sign = 1;
213
214 while (*nptr == ' ' || *nptr == '\t')
215 ++nptr;
216
217 if (*nptr == '-')
218 {
219 sign = -1;
220 ++nptr;
221 }
222 else if (*nptr == '+')
223 ++nptr;
224
225 if (*nptr < '0' || *nptr > '9')
226 {
227 if (endptr != NULL)
228 *endptr = (char *) nptr;
229 return 0L;
230 }
231
232 assert (base == 0);
233 if (*nptr == '0')
234 {
235 if (nptr[1] == 'x' || nptr[1] == 'X')
236 {
237 base = 16;
238 nptr += 2;
239 }
240 else
241 base = 8;
242 }
243 else
244 base = 10;
245
246 while (*nptr >= '0' && *nptr <= '9')
247 {
310b3460 248 unsigned long int digval = *nptr - '0';
27a5bb33 249 if (result > LONG_MAX / 10
310b3460
UD
250 || (result == (sign
251 ? (unsigned long int) LONG_MAX
252 : (unsigned long int) LONG_MAX + 1) / 10
253 && digval > (sign
254 ? (unsigned long int) LONG_MAX
255 : (unsigned long int) LONG_MAX + 1) % 10))
27a5bb33
UD
256 {
257 errno = ERANGE;
258 return LONG_MAX * sign;
259 }
260 result *= 10;
261 result += digval;
262 }
263
310b3460 264 return (long int) result * sign;
27a5bb33
UD
265}
266
310b3460 267long int weak_function
27a5bb33
UD
268strtol (const char *nptr, char **endptr, int base)
269{
270 return __strtol_internal (nptr, endptr, base, 0);
271}
272
310b3460 273unsigned long int weak_function
27a5bb33
UD
274__strtoul_internal (const char *nptr, char **endptr, int base, int group)
275{
310b3460 276 unsigned long int result = 0;
27a5bb33
UD
277 long int sign = 1;
278
279 while (*nptr == ' ' || *nptr == '\t')
280 ++nptr;
281
282 if (*nptr == '-')
283 {
284 sign = -1;
285 ++nptr;
286 }
287 else if (*nptr == '+')
288 ++nptr;
289
290 if (*nptr < '0' || *nptr > '9')
291 {
292 if (endptr != NULL)
293 *endptr = (char *) nptr;
294 return 0UL;
295 }
296
297 assert (base == 0);
298 if (*nptr == '0')
299 {
300 if (nptr[1] == 'x' || nptr[1] == 'X')
301 {
302 base = 16;
303 nptr += 2;
304 }
305 else
306 base = 8;
307 }
308 else
309 base = 10;
310
311 while (*nptr >= '0' && *nptr <= '9')
312 {
310b3460 313 unsigned long int digval = *nptr - '0';
27a5bb33
UD
314 if (result > LONG_MAX / 10
315 || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
316 {
317 errno = ERANGE;
318 return ULONG_MAX;
319 }
320 result *= 10;
321 result += digval;
322 }
323
324 return result * sign;
325}
326
310b3460 327unsigned long int weak_function
27a5bb33
UD
328strtoul (const char *nptr, char **endptr, int base)
329{
330 return (unsigned long int) __strtoul_internal (nptr, endptr, base, 0);
331}