1 | /* Minimal replacements for basic facilities used in the dynamic linker. |
2 | Copyright (C) 1995-2016 Free Software Foundation, Inc. |
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 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. |
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 | Lesser General Public License for more details. |
14 | |
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/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <limits.h> |
21 | #include <stdio.h> |
22 | #include <string.h> |
23 | #include <tls.h> |
24 | #include <unistd.h> |
25 | #include <sys/mman.h> |
26 | #include <sys/param.h> |
27 | #include <sys/types.h> |
28 | #include <ldsodefs.h> |
29 | #include <_itoa.h> |
30 | |
31 | #include <assert.h> |
32 | |
33 | /* Minimal `malloc' allocator for use while loading shared libraries. |
34 | No block is ever freed. */ |
35 | |
36 | static void *alloc_ptr, *alloc_end, *alloc_last_block; |
37 | |
38 | /* Declarations of global functions. */ |
39 | extern void weak_function free (void *ptr); |
40 | extern void * weak_function realloc (void *ptr, size_t n); |
41 | extern unsigned long int weak_function __strtoul_internal (const char *nptr, |
42 | char **endptr, |
43 | int base, |
44 | int group); |
45 | extern unsigned long int weak_function strtoul (const char *nptr, |
46 | char **endptr, int base); |
47 | |
48 | |
49 | /* Allocate an aligned memory block. */ |
50 | void * weak_function |
51 | __libc_memalign (size_t align, size_t n) |
52 | { |
53 | if (alloc_end == 0) |
54 | { |
55 | /* Consume any unused space in the last page of our data segment. */ |
56 | extern int _end attribute_hidden; |
57 | alloc_ptr = &_end; |
58 | alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0) |
59 | + GLRO(dl_pagesize) - 1) |
60 | & ~(GLRO(dl_pagesize) - 1)); |
61 | } |
62 | |
63 | /* Make sure the allocation pointer is ideally aligned. */ |
64 | alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + align - 1) |
65 | & ~(align - 1)); |
66 | |
67 | if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr) |
68 | { |
69 | /* Insufficient space left; allocate another page plus one extra |
70 | page to reduce number of mmap calls. */ |
71 | caddr_t page; |
72 | size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1); |
73 | if (__glibc_unlikely (nup == 0 && n != 0)) |
74 | return NULL; |
75 | nup += GLRO(dl_pagesize); |
76 | page = __mmap (0, nup, PROT_READ|PROT_WRITE, |
77 | MAP_ANON|MAP_PRIVATE, -1, 0); |
78 | if (page == MAP_FAILED) |
79 | return NULL; |
80 | if (page != alloc_end) |
81 | alloc_ptr = page; |
82 | alloc_end = page + nup; |
83 | } |
84 | |
85 | alloc_last_block = (void *) alloc_ptr; |
86 | alloc_ptr += n; |
87 | return alloc_last_block; |
88 | } |
89 | |
90 | void * weak_function |
91 | malloc (size_t n) |
92 | { |
93 | return __libc_memalign (sizeof (double), n); |
94 | } |
95 | |
96 | /* We use this function occasionally since the real implementation may |
97 | be optimized when it can assume the memory it returns already is |
98 | set to NUL. */ |
99 | void * weak_function |
100 | calloc (size_t nmemb, size_t size) |
101 | { |
102 | /* New memory from the trivial malloc above is always already cleared. |
103 | (We make sure that's true in the rare occasion it might not be, |
104 | by clearing memory in free, below.) */ |
105 | size_t bytes = nmemb * size; |
106 | |
107 | #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2)) |
108 | if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0) |
109 | && size != 0 && bytes / size != nmemb) |
110 | return NULL; |
111 | |
112 | return malloc (bytes); |
113 | } |
114 | |
115 | /* This will rarely be called. */ |
116 | void weak_function |
117 | free (void *ptr) |
118 | { |
119 | /* We can free only the last block allocated. */ |
120 | if (ptr == alloc_last_block) |
121 | { |
122 | /* Since this is rare, we clear the freed block here |
123 | so that calloc can presume malloc returns cleared memory. */ |
124 | memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block); |
125 | alloc_ptr = alloc_last_block; |
126 | } |
127 | } |
128 | |
129 | /* This is only called with the most recent block returned by malloc. */ |
130 | void * weak_function |
131 | realloc (void *ptr, size_t n) |
132 | { |
133 | if (ptr == NULL) |
134 | return malloc (n); |
135 | assert (ptr == alloc_last_block); |
136 | size_t old_size = alloc_ptr - alloc_last_block; |
137 | alloc_ptr = alloc_last_block; |
138 | void *new = malloc (n); |
139 | return new != ptr ? memcpy (new, ptr, old_size) : new; |
140 | } |
141 | |
142 | /* Avoid signal frobnication in setjmp/longjmp. Keeps things smaller. */ |
143 | |
144 | #include <setjmp.h> |
145 | |
146 | int weak_function |
147 | __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused))) |
148 | { |
149 | env[0].__mask_was_saved = 0; |
150 | return 0; |
151 | } |
152 | |
153 | /* Define our own version of the internal function used by strerror. We |
154 | only provide the messages for some common errors. This avoids pulling |
155 | in the whole error list. */ |
156 | |
157 | char * weak_function |
158 | __strerror_r (int errnum, char *buf, size_t buflen) |
159 | { |
160 | char *msg; |
161 | |
162 | switch (errnum) |
163 | { |
164 | case ENOMEM: |
165 | msg = (char *) "Cannot allocate memory" ; |
166 | break; |
167 | case EINVAL: |
168 | msg = (char *) "Invalid argument" ; |
169 | break; |
170 | case ENOENT: |
171 | msg = (char *) "No such file or directory" ; |
172 | break; |
173 | case EPERM: |
174 | msg = (char *) "Operation not permitted" ; |
175 | break; |
176 | case EIO: |
177 | msg = (char *) "Input/output error" ; |
178 | break; |
179 | case EACCES: |
180 | msg = (char *) "Permission denied" ; |
181 | break; |
182 | default: |
183 | /* No need to check buffer size, all calls in the dynamic linker |
184 | provide enough space. */ |
185 | buf[buflen - 1] = '\0'; |
186 | msg = _itoa (errnum, buf + buflen - 1, 10, 0); |
187 | msg = memcpy (msg - (sizeof ("Error " ) - 1), "Error " , |
188 | sizeof ("Error " ) - 1); |
189 | break; |
190 | } |
191 | |
192 | return msg; |
193 | } |
194 | |
195 | void |
196 | __libc_fatal (const char *message) |
197 | { |
198 | _dl_fatal_printf ("%s" , message); |
199 | } |
200 | rtld_hidden_def (__libc_fatal) |
201 | |
202 | void |
203 | __attribute__ ((noreturn)) |
204 | __chk_fail (void) |
205 | { |
206 | _exit (127); |
207 | } |
208 | rtld_hidden_def (__chk_fail) |
209 | |
210 | #ifndef NDEBUG |
211 | /* Define (weakly) our own assert failure function which doesn't use stdio. |
212 | If we are linked into the user program (-ldl), the normal __assert_fail |
213 | defn can override this one. */ |
214 | |
215 | void weak_function |
216 | __assert_fail (const char *assertion, |
217 | const char *file, unsigned int line, const char *function) |
218 | { |
219 | _dl_fatal_printf ("\ |
220 | Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n" , |
221 | file, line, function ?: "" , function ? ": " : "" , |
222 | assertion); |
223 | |
224 | } |
225 | rtld_hidden_weak (__assert_fail) |
226 | |
227 | void weak_function |
228 | __assert_perror_fail (int errnum, |
229 | const char *file, unsigned int line, |
230 | const char *function) |
231 | { |
232 | char errbuf[400]; |
233 | _dl_fatal_printf ("\ |
234 | Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n" , |
235 | file, line, function ?: "" , function ? ": " : "" , |
236 | __strerror_r (errnum, errbuf, sizeof errbuf)); |
237 | |
238 | } |
239 | rtld_hidden_weak (__assert_perror_fail) |
240 | #endif |
241 | |
242 | unsigned long int weak_function |
243 | __strtoul_internal (const char *nptr, char **endptr, int base, int group) |
244 | { |
245 | unsigned long int result = 0; |
246 | long int sign = 1; |
247 | unsigned max_digit; |
248 | |
249 | while (*nptr == ' ' || *nptr == '\t') |
250 | ++nptr; |
251 | |
252 | if (*nptr == '-') |
253 | { |
254 | sign = -1; |
255 | ++nptr; |
256 | } |
257 | else if (*nptr == '+') |
258 | ++nptr; |
259 | |
260 | if (*nptr < '0' || *nptr > '9') |
261 | { |
262 | if (endptr != NULL) |
263 | *endptr = (char *) nptr; |
264 | return 0UL; |
265 | } |
266 | |
267 | assert (base == 0); |
268 | base = 10; |
269 | max_digit = 9; |
270 | if (*nptr == '0') |
271 | { |
272 | if (nptr[1] == 'x' || nptr[1] == 'X') |
273 | { |
274 | base = 16; |
275 | nptr += 2; |
276 | } |
277 | else |
278 | { |
279 | base = 8; |
280 | max_digit = 7; |
281 | } |
282 | } |
283 | |
284 | while (1) |
285 | { |
286 | unsigned long int digval; |
287 | if (*nptr >= '0' && *nptr <= '0' + max_digit) |
288 | digval = *nptr - '0'; |
289 | else if (base == 16) |
290 | { |
291 | if (*nptr >= 'a' && *nptr <= 'f') |
292 | digval = *nptr - 'a' + 10; |
293 | else if (*nptr >= 'A' && *nptr <= 'F') |
294 | digval = *nptr - 'A' + 10; |
295 | else |
296 | break; |
297 | } |
298 | else |
299 | break; |
300 | |
301 | if (result > ULONG_MAX / base |
302 | || (result == ULONG_MAX / base && digval > ULONG_MAX % base)) |
303 | { |
304 | errno = ERANGE; |
305 | if (endptr != NULL) |
306 | *endptr = (char *) nptr; |
307 | return ULONG_MAX; |
308 | } |
309 | result *= base; |
310 | result += digval; |
311 | ++nptr; |
312 | } |
313 | |
314 | if (endptr != NULL) |
315 | *endptr = (char *) nptr; |
316 | return result * sign; |
317 | } |
318 | |
319 | |
320 | #undef _itoa |
321 | /* We always use _itoa instead of _itoa_word in ld.so since the former |
322 | also has to be present and it is never about speed when these |
323 | functions are used. */ |
324 | char * |
325 | _itoa (unsigned long long int value, char *buflim, unsigned int base, |
326 | int upper_case) |
327 | { |
328 | assert (! upper_case); |
329 | |
330 | do |
331 | *--buflim = _itoa_lower_digits[value % base]; |
332 | while ((value /= base) != 0); |
333 | |
334 | return buflim; |
335 | } |
336 | |
337 | /* The '_itoa_lower_digits' variable in libc.so is able to handle bases |
338 | up to 36. We don't need this here. */ |
339 | const char _itoa_lower_digits[16] = "0123456789abcdef" ; |
340 | rtld_hidden_data_def (_itoa_lower_digits) |
341 | |
342 | /* The following is not a complete strsep implementation. It cannot |
343 | handle empty delimiter strings. But this isn't necessary for the |
344 | execution of ld.so. */ |
345 | #undef strsep |
346 | #undef __strsep |
347 | char * |
348 | __strsep (char **stringp, const char *delim) |
349 | { |
350 | char *begin; |
351 | |
352 | assert (delim[0] != '\0'); |
353 | |
354 | begin = *stringp; |
355 | if (begin != NULL) |
356 | { |
357 | char *end = begin; |
358 | |
359 | while (*end != '\0' || (end = NULL)) |
360 | { |
361 | const char *dp = delim; |
362 | |
363 | do |
364 | if (*dp == *end) |
365 | break; |
366 | while (*++dp != '\0'); |
367 | |
368 | if (*dp != '\0') |
369 | { |
370 | *end++ = '\0'; |
371 | break; |
372 | } |
373 | |
374 | ++end; |
375 | } |
376 | |
377 | *stringp = end; |
378 | } |
379 | |
380 | return begin; |
381 | } |
382 | weak_alias (__strsep, strsep) |
383 | strong_alias (__strsep, __strsep_g) |
384 | |