]>
Commit | Line | Data |
---|---|---|
42d2676e | 1 | /* Minimal replacements for basic facilities used in the dynamic linker. |
d6b5d570 | 2 | Copyright (C) 1995,96,97,98,2000,2001,2002 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 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 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> | |
a42195db | 26 | #include <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 | |
34 | static void *alloc_ptr, *alloc_end, *alloc_last_block; | |
35 | ||
728c24ff AJ |
36 | /* Declarations of global functions. */ |
37 | extern void weak_function free (void *ptr); | |
38 | extern void * weak_function realloc (void *ptr, size_t n); | |
728c24ff AJ |
39 | extern unsigned long int weak_function __strtoul_internal |
40 | (const char *nptr, char **endptr, int base, int group); | |
41 | extern unsigned long int weak_function strtoul (const char *nptr, | |
42 | char **endptr, int base); | |
43 | ||
44 | ||
af5b3bc3 | 45 | void * weak_function |
42d2676e RM |
46 | malloc (size_t n) |
47 | { | |
2064087b RM |
48 | #ifdef MAP_ANON |
49 | #define _dl_zerofd (-1) | |
50 | #else | |
42d2676e | 51 | extern int _dl_zerofd; |
42d2676e | 52 | |
42d2676e RM |
53 | if (_dl_zerofd == -1) |
54 | _dl_zerofd = _dl_sysdep_open_zero_fill (); | |
2064087b RM |
55 | #define MAP_ANON 0 |
56 | #endif | |
57 | ||
42d2676e RM |
58 | if (alloc_end == 0) |
59 | { | |
60 | /* Consume any unused space in the last page of our data segment. */ | |
61 | extern int _end; | |
62 | alloc_ptr = &_end; | |
d6b5d570 UD |
63 | alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) |
64 | + GL(dl_pagesize) - 1) | |
65 | & ~(GL(dl_pagesize) - 1)); | |
42d2676e RM |
66 | } |
67 | ||
68 | /* Make sure the allocation pointer is ideally aligned. */ | |
69 | alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + sizeof (double) - 1) | |
70 | & ~(sizeof (double) - 1)); | |
71 | ||
72 | if (alloc_ptr + n >= alloc_end) | |
73 | { | |
74 | /* Insufficient space left; allocate another page. */ | |
75 | caddr_t page; | |
d6b5d570 | 76 | size_t nup = (n + GL(dl_pagesize) - 1) & ~(GL(dl_pagesize) - 1); |
ea7eb7e3 | 77 | page = __mmap (0, nup, PROT_READ|PROT_WRITE, |
266180eb | 78 | MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0); |
0413b54c | 79 | assert (page != MAP_FAILED); |
42d2676e RM |
80 | if (page != alloc_end) |
81 | alloc_ptr = page; | |
ea7eb7e3 | 82 | alloc_end = page + nup; |
42d2676e RM |
83 | } |
84 | ||
85 | alloc_last_block = (void *) alloc_ptr; | |
86 | alloc_ptr += n; | |
87 | return alloc_last_block; | |
88 | } | |
42d2676e | 89 | |
c131718c UD |
90 | /* We use this function occasionally since the real implementation may |
91 | be optimized when it can assume the memory it returns already is | |
92 | set to NUL. */ | |
93 | void * weak_function | |
94 | calloc (size_t nmemb, size_t size) | |
95 | { | |
96 | size_t total = nmemb * size; | |
97 | void *result = malloc (total); | |
98 | return memset (result, '\0', total); | |
99 | } | |
100 | ||
42d2676e | 101 | /* This will rarely be called. */ |
af5b3bc3 | 102 | void weak_function |
42d2676e RM |
103 | free (void *ptr) |
104 | { | |
105 | /* We can free only the last block allocated. */ | |
106 | if (ptr == alloc_last_block) | |
107 | alloc_ptr = alloc_last_block; | |
108 | } | |
42d2676e | 109 | |
efec1d0c | 110 | /* This is only called with the most recent block returned by malloc. */ |
af5b3bc3 | 111 | void * weak_function |
42d2676e | 112 | realloc (void *ptr, size_t n) |
efec1d0c RM |
113 | { |
114 | void *new; | |
d252c479 UD |
115 | if (ptr == NULL) |
116 | return malloc (n); | |
efec1d0c RM |
117 | assert (ptr == alloc_last_block); |
118 | alloc_ptr = alloc_last_block; | |
119 | new = malloc (n); | |
120 | assert (new == ptr); | |
121 | return new; | |
122 | } | |
42d2676e RM |
123 | \f |
124 | /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */ | |
125 | ||
126 | #include <setjmp.h> | |
127 | ||
af5b3bc3 | 128 | int weak_function |
1e71bd52 | 129 | __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused))) |
ca34d7a7 | 130 | { |
1e71bd52 | 131 | env[0].__mask_was_saved = 0; |
ca34d7a7 UD |
132 | return 0; |
133 | } | |
42d2676e | 134 | |
af5b3bc3 | 135 | void weak_function |
ca34d7a7 UD |
136 | longjmp (jmp_buf env, int val) |
137 | { | |
138 | __longjmp (env[0].__jmpbuf, val); | |
139 | } | |
42d2676e | 140 | \f |
da832465 UD |
141 | /* Define our own version of the internal function used by strerror. We |
142 | only provide the messages for some common errors. This avoids pulling | |
143 | in the whole error list. */ | |
42d2676e | 144 | |
af5b3bc3 | 145 | char * weak_function |
310b3460 | 146 | __strerror_r (int errnum, char *buf, size_t buflen) |
42d2676e | 147 | { |
da832465 UD |
148 | char *msg; |
149 | ||
150 | switch (errnum) | |
151 | { | |
152 | case ENOMEM: | |
153 | msg = (char *) "Cannot allocate memory"; | |
154 | break; | |
155 | case EINVAL: | |
156 | msg = (char *) "Invalid argument"; | |
157 | break; | |
158 | case ENOENT: | |
159 | msg = (char *) "No such file or directory"; | |
160 | break; | |
161 | case EPERM: | |
162 | msg = (char *) "Operation not permitted"; | |
163 | break; | |
164 | case EIO: | |
165 | msg = (char *) "Input/output error"; | |
166 | break; | |
167 | case EACCES: | |
168 | msg = (char *) "Permission denied"; | |
169 | break; | |
170 | default: | |
171 | /* No need to check buffer size, all calls in the dynamic linker | |
172 | provide enough space. */ | |
173 | buf[buflen - 1] = '\0'; | |
174 | msg = _itoa_word (errnum, buf + buflen - 1, 10, 0); | |
175 | msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ", | |
176 | sizeof ("Error ") - 1); | |
177 | break; | |
178 | } | |
179 | ||
180 | return msg; | |
42d2676e | 181 | } |
42d2676e RM |
182 | \f |
183 | #ifndef NDEBUG | |
184 | ||
185 | /* Define (weakly) our own assert failure function which doesn't use stdio. | |
186 | If we are linked into the user program (-ldl), the normal __assert_fail | |
187 | defn can override this one. */ | |
188 | ||
af5b3bc3 | 189 | void weak_function |
42d2676e RM |
190 | __assert_fail (const char *assertion, |
191 | const char *file, unsigned int line, const char *function) | |
192 | { | |
35fc382a | 193 | _dl_fatal_printf ("\ |
28966366 | 194 | Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n", |
35fc382a UD |
195 | file, line, function ?: "", function ? ": " : "", |
196 | assertion); | |
42d2676e RM |
197 | |
198 | } | |
42d2676e | 199 | |
af5b3bc3 | 200 | void weak_function |
42d2676e RM |
201 | __assert_perror_fail (int errnum, |
202 | const char *file, unsigned int line, | |
203 | const char *function) | |
204 | { | |
8619129f | 205 | char errbuf[64]; |
35fc382a | 206 | _dl_fatal_printf ("\ |
28966366 | 207 | Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s\n", |
35fc382a UD |
208 | file, line, function ?: "", function ? ": " : "", |
209 | __strerror_r (errnum, errbuf, sizeof (errbuf))); | |
42d2676e | 210 | } |
42d2676e RM |
211 | |
212 | #endif | |
27a5bb33 | 213 | |
310b3460 | 214 | unsigned long int weak_function |
27a5bb33 UD |
215 | __strtoul_internal (const char *nptr, char **endptr, int base, int group) |
216 | { | |
310b3460 | 217 | unsigned long int result = 0; |
27a5bb33 UD |
218 | long int sign = 1; |
219 | ||
220 | while (*nptr == ' ' || *nptr == '\t') | |
221 | ++nptr; | |
222 | ||
223 | if (*nptr == '-') | |
224 | { | |
225 | sign = -1; | |
226 | ++nptr; | |
227 | } | |
228 | else if (*nptr == '+') | |
229 | ++nptr; | |
230 | ||
231 | if (*nptr < '0' || *nptr > '9') | |
232 | { | |
233 | if (endptr != NULL) | |
234 | *endptr = (char *) nptr; | |
235 | return 0UL; | |
236 | } | |
237 | ||
238 | assert (base == 0); | |
5aa8ff62 | 239 | base = 10; |
27a5bb33 UD |
240 | if (*nptr == '0') |
241 | { | |
242 | if (nptr[1] == 'x' || nptr[1] == 'X') | |
243 | { | |
244 | base = 16; | |
245 | nptr += 2; | |
246 | } | |
247 | else | |
248 | base = 8; | |
249 | } | |
27a5bb33 UD |
250 | |
251 | while (*nptr >= '0' && *nptr <= '9') | |
252 | { | |
310b3460 | 253 | unsigned long int digval = *nptr - '0'; |
27a5bb33 UD |
254 | if (result > LONG_MAX / 10 |
255 | || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10)) | |
256 | { | |
257 | errno = ERANGE; | |
a986484f UD |
258 | if (endptr != NULL) |
259 | *endptr = (char *) nptr; | |
27a5bb33 UD |
260 | return ULONG_MAX; |
261 | } | |
5aa8ff62 | 262 | result *= base; |
27a5bb33 | 263 | result += digval; |
5aa8ff62 | 264 | ++nptr; |
27a5bb33 UD |
265 | } |
266 | ||
a986484f UD |
267 | if (endptr != NULL) |
268 | *endptr = (char *) nptr; | |
27a5bb33 UD |
269 | return result * sign; |
270 | } | |
ccdf0cab UD |
271 | |
272 | ||
273 | #if HP_TIMING_AVAIL && ULONG_MAX <= 4294967295UL | |
274 | /* We need this function to print the cycle count. On 64-bit machines the | |
275 | _itoa_word function should be used. */ | |
276 | char * | |
277 | _itoa (value, buflim, base, upper_case) | |
278 | unsigned long long int value; | |
279 | char *buflim; | |
280 | unsigned int base; | |
281 | int upper_case; | |
282 | { | |
283 | char *bp = buflim; | |
284 | ||
285 | assert (base == 10); | |
286 | ||
287 | do | |
288 | *--bp = '0' + value % 10; | |
289 | while ((value /= 10) != 0); | |
290 | ||
291 | return bp; | |
292 | } | |
293 | #endif | |
5c1159b6 UD |
294 | |
295 | ||
296 | /* The following is not a complete strsep implementation. It cannot | |
297 | handle empty delimiter strings. But this isn't necessary for the | |
298 | execution of ld.so. */ | |
299 | #undef strsep | |
300 | #undef __strsep | |
301 | char * | |
302 | __strsep (char **stringp, const char *delim) | |
303 | { | |
304 | char *begin; | |
305 | ||
306 | begin = *stringp; | |
307 | if (begin != NULL) | |
308 | { | |
309 | char *end = begin; | |
310 | ||
311 | while (*end != '\0' || (end = NULL)) | |
312 | { | |
313 | const char *dp = delim; | |
314 | ||
315 | do | |
316 | if (*dp == *end) | |
317 | break; | |
318 | while (*++dp != '\0'); | |
319 | ||
320 | if (*dp != '\0') | |
321 | { | |
322 | *end++ = '\0'; | |
323 | break; | |
324 | } | |
325 | ||
326 | ++end; | |
327 | } | |
328 | ||
329 | *stringp = end; | |
330 | } | |
331 | ||
332 | return begin; | |
333 | } | |
334 | weak_alias (__strsep, strsep) | |
335 | strong_alias (__strsep, __strsep_g) |