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