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