1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21: #include <errno.h>
22: #include <limits.h>
23: #include <string.h>
24: #include <tls.h>
25: #include <unistd.h>
26: #include <sys/mman.h>
27: #include <sys/param.h>
28: #include <sys/types.h>
29: #include <ldsodefs.h>
30: #include <stdio-common/_itoa.h>
31:
32: #include <assert.h>
33:
34:
35:
36:
37: static void *alloc_ptr, *alloc_end, *alloc_last_block;
38:
39:
40: extern void weak_function free (void *ptr);
41: extern void * weak_function realloc (void *ptr, size_t n);
42: extern unsigned long int weak_function __strtoul_internal (const char *nptr,
43: char **endptr,
44: int base,
45: int group);
46: extern unsigned long int weak_function strtoul (const char *nptr,
47: char **endptr, int base);
48:
49:
50:
51: void * weak_function
52: __libc_memalign (size_t align, size_t n)
53: {
54: #ifdef MAP_ANON
55: #define _dl_zerofd (-1)
56: #else
57: extern int _dl_zerofd;
58:
59: if (_dl_zerofd == -1)
60: _dl_zerofd = _dl_sysdep_open_zero_fill ();
61: #define MAP_ANON 0
62: #endif
63:
64: if (alloc_end == 0)
65: {
66:
67: extern int _end attribute_hidden;
68: alloc_ptr = &_end;
69: alloc_end = (void *) 0 + (((alloc_ptr - (void *) 0)
70: + GLRO(dl_pagesize) - 1)
71: & ~(GLRO(dl_pagesize) - 1));
72: }
73:
74:
75: alloc_ptr = (void *) 0 + (((alloc_ptr - (void *) 0) + align - 1)
76: & ~(align - 1));
77:
78: if (alloc_ptr + n >= alloc_end || n >= -(uintptr_t) alloc_ptr)
79: {
80:
81: caddr_t page;
82: size_t nup = (n + GLRO(dl_pagesize) - 1) & ~(GLRO(dl_pagesize) - 1);
83: if (__builtin_expect (nup == 0, 0))
84: {
85: if (n)
86: return NULL;
87: nup = GLRO(dl_pagesize);
88: }
89: page = __mmap (0, nup, PROT_READ|PROT_WRITE,
90: MAP_ANON|MAP_PRIVATE, _dl_zerofd, 0);
91: if (page == MAP_FAILED)
92: return NULL;
93: if (page != alloc_end)
94: alloc_ptr = page;
95: alloc_end = page + nup;
96: }
97:
98: alloc_last_block = (void *) alloc_ptr;
99: alloc_ptr += n;
100: return alloc_last_block;
101: }
102:
103: void * weak_function
104: malloc (size_t n)
105: {
106: return __libc_memalign (sizeof (double), n);
107: }
108:
109:
110:
111:
112: void * weak_function
113: calloc (size_t nmemb, size_t size)
114: {
115:
116:
117:
118: size_t bytes = nmemb * size;
119:
120: #define HALF_SIZE_T (((size_t) 1) << (8 * sizeof (size_t) / 2))
121: if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0)
122: && size != 0 && bytes / size != nmemb)
123: return NULL;
124:
125: return malloc (bytes);
126: }
127:
128:
129: void weak_function
130: free (void *ptr)
131: {
132:
133: if (ptr == alloc_last_block)
134: {
135:
136:
137: memset (alloc_last_block, '\0', alloc_ptr - alloc_last_block);
138: alloc_ptr = alloc_last_block;
139: }
140: }
141:
142:
143: void * weak_function
144: realloc (void *ptr, size_t n)
145: {
146: if (ptr == NULL)
147: return malloc (n);
148: assert (ptr == alloc_last_block);
149: size_t old_size = alloc_ptr - alloc_last_block;
150: alloc_ptr = alloc_last_block;
151: void *new = malloc (n);
152: return new != ptr ? memcpy (new, ptr, old_size) : new;
153: }
154: ^L
155:
156:
157: #include <setjmp.h>
158:
159: int weak_function
160: __sigjmp_save (sigjmp_buf env, int savemask __attribute__ ((unused)))
161: {
162: env[0].__mask_was_saved = 0;
163: return 0;
164: }
165: ^L
166:
167:
168:
169:
170: char * weak_function
171: __strerror_r (int errnum, char *buf, size_t buflen)
172: {
173: char *msg;
174:
175: switch (errnum)
176: {
177: case ENOMEM:
178: msg = (char *) "Cannot allocate memory";
179: break;
180: case EINVAL:
181: msg = (char *) "Invalid argument";
182: break;
183: case ENOENT:
184: msg = (char *) "No such file or directory";
185: break;
186: case EPERM:
187: msg = (char *) "Operation not permitted";
188: break;
189: case EIO:
190: msg = (char *) "Input/output error";
191: break;
192: case EACCES:
193: msg = (char *) "Permission denied";
194: break;
195: default:
196:
197:
198: buf[buflen - 1] = '\0';
199: msg = _itoa (errnum, buf + buflen - 1, 10, 0);
200: msg = memcpy (msg - (sizeof ("Error ") - 1), "Error ",
201: sizeof ("Error ") - 1);
202: break;
203: }
204:
205: return msg;
206: }
207: ^L
208: #ifndef NDEBUG
209:
210:
211:
212:
213:
214: void weak_function
215: __assert_fail (const char *assertion,
216: const char *file, unsigned int line, const char *function)
217: {
218: _dl_fatal_printf ("\
219: Inconsistency detected by ld.so: %s: %u: %s%sAssertion `%s' failed!\n",
220: file, line, function ?: "", function ? ": " : "",
221: assertion);
222:
223: }
224: rtld_hidden_weak(__assert_fail)
225:
226: void weak_function
227: __assert_perror_fail (int errnum,
228: const char *file, unsigned int line,
229: const char *function)
230: {
231: char errbuf[400];
232: _dl_fatal_printf ("\
233: Inconsistency detected by ld.so: %s: %u: %s%sUnexpected error: %s.\n",
234: file, line, function ?: "", function ? ": " : "",
235: __strerror_r (errnum, errbuf, sizeof errbuf));
236:
237: }
238: rtld_hidden_weak (__assert_perror_fail)
239: #endif
240:
241: unsigned long int weak_function
242: __strtoul_internal (const char *nptr, char **endptr, int base, int group)
243: {
244: unsigned long int result = 0;
245: long int sign = 1;
246:
247: while (*nptr == ' ' || *nptr == '\t')
248: ++nptr;
249:
250: if (*nptr == '-')
251: {
252: sign = -1;
253: ++nptr;
254: }
255: else if (*nptr == '+')
256: ++nptr;
257:
258: if (*nptr < '0' || *nptr > '9')
259: {
260: if (endptr != NULL)
261: *endptr = (char *) nptr;
262: return 0UL;
263: }
264:
265: assert (base == 0);
266: base = 10;
267: if (*nptr == '0')
268: {
269: if (nptr[1] == 'x' || nptr[1] == 'X')
270: {
271: base = 16;
272: nptr += 2;
273: }
274: else
275: base = 8;
276: }
277:
278: while (*nptr >= '0' && *nptr <= '9')
279: {
280: unsigned long int digval = *nptr - '0';
281: if (result > ULONG_MAX / 10
282: || (result == ULONG_MAX / 10 && digval > ULONG_MAX % 10))
283: {
284: errno = ERANGE;
285: if (endptr != NULL)
286: *endptr = (char *) nptr;
287: return ULONG_MAX;
288: }
289: result *= base;
290: result += digval;
291: ++nptr;
292: }
293:
294: if (endptr != NULL)
295: *endptr = (char *) nptr;
296: return result * sign;
297: }
298:
299:
300: #undef _itoa
301:
302:
303:
304: char *
305: _itoa (value, buflim, base, upper_case)
306: unsigned long long int value;
307: char *buflim;
308: unsigned int base;
309: int upper_case;
310: {
311: extern const char INTUSE(_itoa_lower_digits)[] attribute_hidden;
312:
313: assert (! upper_case);
314:
315: do
316: *--buflim = INTUSE(_itoa_lower_digits)[value % base];
317: while ((value /= base) != 0);
318:
319: return buflim;
320: }
321:
322:
323:
324:
325:
326: #undef strsep
327: #undef __strsep
328: char *
329: __strsep (char **stringp, const char *delim)
330: {
331: char *begin;
332:
333: assert (delim[0] != '\0');
334:
335: begin = *stringp;
336: if (begin != NULL)
337: {
338: char *end = begin;
339:
340: while (*end != '\0' || (end = NULL))
341: {
342: const char *dp = delim;
343:
344: do
345: if (*dp == *end)
346: break;
347: while (*++dp != '\0');
348:
349: if (*dp != '\0')
350: {
351: *end++ = '\0';
352: break;
353: }
354:
355: ++end;
356: }
357:
358: *stringp = end;
359: }
360:
361: return begin;
362: }
363: weak_alias (__strsep, strsep)
364: strong_alias (__strsep, __strsep_g)
365:
366: void
367: __attribute__ ((noreturn))
368: __chk_fail (void)
369: {
370: _exit (127);
371: }
372: rtld_hidden_def (__chk_fail)
373:
374:
375:
376: const char INTUSE(_itoa_lower_digits)[16] attribute_hidden
377: = "0123456789abcdef";