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