1 /* Miscellaneous support functions for dynamic linker
2 Copyright (C) 1997-2013 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #include <libc-symbols.h>
31 #include <sys/param.h>
36 #include <dl-writev.h>
39 /* Read the whole contents of FILE into new mmap'd space with given
40 protections. *SIZEP gets the size of the file. On error MAP_FAILED
45 _dl_sysdep_read_whole_file (const char *file
, size_t *sizep
, int prot
)
47 void *result
= MAP_FAILED
;
53 int fd
= __open (file
, flags
);
56 if (__fxstat64 (_STAT_VER
, fd
, &st
) >= 0)
60 /* No need to map the file if it is empty. */
62 /* Map a copy of the file contents. */
63 result
= __mmap (NULL
, *sizep
, prot
,
80 /* Bare-bones printf implementation. This function only knows about
81 the formats and flags needed and can handle only up to 64 stripes in
84 _dl_debug_vdprintf (int fd
, int tag_p
, const char *fmt
, va_list arg
)
87 struct iovec iov
[NIOVMAX
];
94 const char *startp
= fmt
;
98 /* Generate the tag line once. It consists of the PID and a
99 colon followed by a tab. */
104 assert (pid
>= 0 && sizeof (pid_t
) <= 4);
105 p
= _itoa (pid
, &pidbuf
[10], 10, 0);
112 /* Append to the output. */
113 assert (niov
< NIOVMAX
);
114 iov
[niov
].iov_len
= 12;
115 iov
[niov
++].iov_base
= pidbuf
;
117 /* No more tags until we see the next newline. */
121 /* Skip everything except % and \n (if tags are needed). */
122 while (*fmt
!= '\0' && *fmt
!= '%' && (! tag_p
|| *fmt
!= '\n'))
125 /* Append constant string. */
126 assert (niov
< NIOVMAX
);
127 if ((iov
[niov
].iov_len
= fmt
- startp
) != 0)
128 iov
[niov
++].iov_base
= (char *) startp
;
132 /* It is a format specifier. */
136 #if LONG_MAX != INT_MAX
140 /* Recognize zero-digit fill flag. */
147 /* See whether with comes from a parameter. Note that no other
148 way to specify the width is implemented. */
151 width
= va_arg (arg
, int);
155 /* Handle precision. */
156 if (*fmt
== '.' && fmt
[1] == '*')
158 prec
= va_arg (arg
, int);
162 /* Recognize the l modifier. It is only important on some
163 platforms where long and int have a different size. We
164 can use the same code for size_t. */
165 if (*fmt
== 'l' || *fmt
== 'Z')
167 #if LONG_MAX != INT_MAX
175 /* Integer formatting. */
179 /* We have to make a difference if long and int have a
181 #if LONG_MAX != INT_MAX
182 unsigned long int num
= (long_mod
183 ? va_arg (arg
, unsigned long int)
184 : va_arg (arg
, unsigned int));
186 unsigned long int num
= va_arg (arg
, unsigned int);
188 /* We use alloca() to allocate the buffer with the most
189 pessimistic guess for the size. Using alloca() allows
190 having more than one integer formatting in a call. */
191 char *buf
= (char *) alloca (3 * sizeof (unsigned long int));
192 char *endp
= &buf
[3 * sizeof (unsigned long int)];
193 char *cp
= _itoa (num
, endp
, *fmt
== 'x' ? 16 : 10, 0);
195 /* Pad to the width the user specified. */
197 while (endp
- cp
< width
)
200 iov
[niov
].iov_base
= cp
;
201 iov
[niov
].iov_len
= endp
- cp
;
207 /* Get the string argument. */
208 iov
[niov
].iov_base
= va_arg (arg
, char *);
209 iov
[niov
].iov_len
= strlen (iov
[niov
].iov_base
);
211 iov
[niov
].iov_len
= MIN ((size_t) prec
, iov
[niov
].iov_len
);
216 iov
[niov
].iov_base
= (void *) fmt
;
217 iov
[niov
].iov_len
= 1;
222 assert (! "invalid format specifier");
226 else if (*fmt
== '\n')
228 /* See whether we have to print a single newline character. */
231 iov
[niov
].iov_base
= (char *) startp
;
232 iov
[niov
++].iov_len
= 1;
235 /* No, just add it to the rest of the string. */
236 ++iov
[niov
- 1].iov_len
;
238 /* Next line, print a tag again. */
244 /* Finally write the result. */
245 _dl_writev (fd
, iov
, niov
);
249 /* Write to debug file. */
251 _dl_debug_printf (const char *fmt
, ...)
256 _dl_debug_vdprintf (GLRO(dl_debug_fd
), 1, fmt
, arg
);
261 /* Write to debug file but don't start with a tag. */
263 _dl_debug_printf_c (const char *fmt
, ...)
268 _dl_debug_vdprintf (GLRO(dl_debug_fd
), -1, fmt
, arg
);
273 /* Write the given file descriptor. */
275 _dl_dprintf (int fd
, const char *fmt
, ...)
280 _dl_debug_vdprintf (fd
, 0, fmt
, arg
);
285 /* Test whether given NAME matches any of the names of the given object. */
288 _dl_name_match_p (const char *name
, const struct link_map
*map
)
290 if (strcmp (name
, map
->l_name
) == 0)
293 struct libname_list
*runp
= map
->l_libname
;
296 if (strcmp (name
, runp
->name
) == 0)
307 _dl_higher_prime_number (unsigned long int n
)
309 /* These are primes that are near, but slightly smaller than, a
311 static const uint32_t primes
[] = {
336 UINT32_C (134217689),
337 UINT32_C (268435399),
338 UINT32_C (536870909),
339 UINT32_C (1073741789),
340 UINT32_C (2147483647),
342 UINT32_C (2147483647) + UINT32_C (2147483644)
345 const uint32_t *low
= &primes
[0];
346 const uint32_t *high
= &primes
[sizeof (primes
) / sizeof (primes
[0])];
350 const uint32_t *mid
= low
+ (high
- low
) / 2;
358 /* If we've run out of primes, abort. */
361 fprintf (stderr
, "Cannot find prime bigger than %lu\n", n
);
369 /* To support accessing TLS variables from signal handlers, we need an
370 async signal safe memory allocator. These routines are never
371 themselves invoked reentrantly (all calls to them are surrounded by
372 signal masks) but may be invoked concurrently from many threads.
373 The current implementation is not particularly performant nor space
374 efficient, but it will be used rarely (and only in binaries that use
375 dlopen.) The API matches that of malloc() and friends. */
377 struct __signal_safe_allocator_header
383 static inline struct __signal_safe_allocator_header
*
384 ptr_to_signal_safe_allocator_header (void *ptr
)
386 return (struct __signal_safe_allocator_header
*)
387 ((char *) (ptr
) - sizeof (struct __signal_safe_allocator_header
));
391 __signal_safe_memalign (size_t boundary
, size_t size
)
393 struct __signal_safe_allocator_header
*header
;
395 if (boundary
< sizeof (*header
))
396 boundary
= sizeof (*header
);
398 /* Boundary must be a power of two. */
399 if (!powerof2 (boundary
))
402 size_t pg
= GLRO (dl_pagesize
);
406 /* We'll get a pointer certainly aligned to boundary, so just
407 add one more boundary-sized chunk to hold the header. */
408 padded_size
= roundup (size
, boundary
) + boundary
;
412 /* If we want K pages aligned to a J-page boundary, K+J+1 pages
413 contains at least one such region that isn't directly at the start
414 (so we can place the header.) This is wasteful, but you're the one
415 who wanted 64K-aligned TLS. */
416 padded_size
= roundup (size
, pg
) + boundary
+ pg
;
420 size_t actual_size
= roundup (padded_size
, pg
);
421 void *actual
= mmap (NULL
, actual_size
, PROT_READ
| PROT_WRITE
,
422 MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
423 if (actual
== MAP_FAILED
)
428 header
= actual
+ boundary
- sizeof (*header
);
432 intptr_t actual_pg
= ((intptr_t) actual
) / pg
;
433 intptr_t boundary_pg
= boundary
/ pg
;
434 intptr_t start_pg
= actual_pg
+ boundary_pg
;
435 start_pg
-= start_pg
% boundary_pg
;
436 if (start_pg
> (actual_pg
+ 1))
438 int ret
= munmap (actual
, (start_pg
- actual_pg
- 1) * pg
);
440 actual
= (void *) ((start_pg
- 1) * pg
);
442 char *start
= (void *) (start_pg
* pg
);
443 header
= ptr_to_signal_safe_allocator_header (start
);
446 header
->size
= actual_size
;
447 header
->start
= actual
;
449 ptr
+= sizeof (*header
);
450 if (((intptr_t) ptr
) % boundary
!= 0)
451 _dl_fatal_printf ("__signal_safe_memalign produced incorrect alignment\n");
456 __signal_safe_malloc (size_t size
)
458 return __signal_safe_memalign (1, size
);
462 __signal_safe_free (void *ptr
)
467 struct __signal_safe_allocator_header
*header
468 = ptr_to_signal_safe_allocator_header (ptr
);
469 int ret
= munmap (header
->start
, header
->size
);
475 __signal_safe_realloc (void *ptr
, size_t size
)
479 __signal_safe_free (ptr
);
483 return __signal_safe_malloc (size
);
485 struct __signal_safe_allocator_header
*header
486 = ptr_to_signal_safe_allocator_header (ptr
);
487 size_t old_size
= header
->size
;
488 if (old_size
- sizeof (*header
) >= size
)
491 void *new_ptr
= __signal_safe_malloc (size
);
495 memcpy (new_ptr
, ptr
, old_size
);
496 __signal_safe_free (ptr
);
502 __signal_safe_calloc (size_t nmemb
, size_t size
)
504 void *ptr
= __signal_safe_malloc (nmemb
* size
);
507 return memset (ptr
, 0, nmemb
* size
);