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/>. */
30 #include <sys/param.h>
35 #include <dl-writev.h>
38 /* Read the whole contents of FILE into new mmap'd space with given
39 protections. *SIZEP gets the size of the file. On error MAP_FAILED
44 _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
46 void *result = MAP_FAILED;
52 int fd = __open (file, flags);
55 if (__fxstat64 (_STAT_VER, fd, &st) >= 0)
59 /* No need to map the file if it is empty. */
61 /* Map a copy of the file contents. */
62 result = __mmap (NULL, *sizep, prot,
79 /* Bare-bones printf implementation. This function only knows about
80 the formats and flags needed and can handle only up to 64 stripes in
83 _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
86 struct iovec iov[NIOVMAX];
93 const char *startp = fmt;
97 /* Generate the tag line once. It consists of the PID and a
98 colon followed by a tab. */
103 assert (pid >= 0 && sizeof (pid_t) <= 4);
104 p = _itoa (pid, &pidbuf[10], 10, 0);
111 /* Append to the output. */
112 assert (niov < NIOVMAX);
113 iov[niov].iov_len = 12;
114 iov[niov++].iov_base = pidbuf;
116 /* No more tags until we see the next newline. */
120 /* Skip everything except % and \n (if tags are needed). */
121 while (*fmt != '\0' && *fmt != '%' && (! tag_p || *fmt != '\n'))
124 /* Append constant string. */
125 assert (niov < NIOVMAX);
126 if ((iov[niov].iov_len = fmt - startp) != 0)
127 iov[niov++].iov_base = (char *) startp;
131 /* It is a format specifier. */
135 #if LONG_MAX != INT_MAX
139 /* Recognize zero-digit fill flag. */
146 /* See whether with comes from a parameter. Note that no other
147 way to specify the width is implemented. */
150 width = va_arg (arg, int);
154 /* Handle precision. */
155 if (*fmt == '.' && fmt[1] == '*')
157 prec = va_arg (arg, int);
161 /* Recognize the l modifier. It is only important on some
162 platforms where long and int have a different size. We
163 can use the same code for size_t. */
164 if (*fmt == 'l' || *fmt == 'Z')
166 #if LONG_MAX != INT_MAX
174 /* Integer formatting. */
178 /* We have to make a difference if long and int have a
180 #if LONG_MAX != INT_MAX
181 unsigned long int num = (long_mod
182 ? va_arg (arg, unsigned long int)
183 : va_arg (arg, unsigned int));
185 unsigned long int num = va_arg (arg, unsigned int);
187 /* We use alloca() to allocate the buffer with the most
188 pessimistic guess for the size. Using alloca() allows
189 having more than one integer formatting in a call. */
190 char *buf = (char *) alloca (3 * sizeof (unsigned long int));
191 char *endp = &buf[3 * sizeof (unsigned long int)];
192 char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0);
194 /* Pad to the width the user specified. */
196 while (endp - cp < width)
199 iov[niov].iov_base = cp;
200 iov[niov].iov_len = endp - cp;
206 /* Get the string argument. */
207 iov[niov].iov_base = va_arg (arg, char *);
208 iov[niov].iov_len = strlen (iov[niov].iov_base);
210 iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len);
215 iov[niov].iov_base = (void *) fmt;
216 iov[niov].iov_len = 1;
221 assert (! "invalid format specifier");
225 else if (*fmt == '\n')
227 /* See whether we have to print a single newline character. */
230 iov[niov].iov_base = (char *) startp;
231 iov[niov++].iov_len = 1;
234 /* No, just add it to the rest of the string. */
235 ++iov[niov - 1].iov_len;
237 /* Next line, print a tag again. */
243 /* Finally write the result. */
244 _dl_writev (fd, iov, niov);
248 /* Write to debug file. */
250 _dl_debug_printf (const char *fmt, ...)
255 _dl_debug_vdprintf (GLRO(dl_debug_fd), 1, fmt, arg);
260 /* Write to debug file but don't start with a tag. */
262 _dl_debug_printf_c (const char *fmt, ...)
267 _dl_debug_vdprintf (GLRO(dl_debug_fd), -1, fmt, arg);
272 /* Write the given file descriptor. */
274 _dl_dprintf (int fd, const char *fmt, ...)
279 _dl_debug_vdprintf (fd, 0, fmt, arg);
284 /* Test whether given NAME matches any of the names of the given object. */
287 _dl_name_match_p (const char *name, const struct link_map *map)
289 if (strcmp (name, map->l_name) == 0)
292 struct libname_list *runp = map->l_libname;
295 if (strcmp (name, runp->name) == 0)
306 _dl_higher_prime_number (unsigned long int n)
308 /* These are primes that are near, but slightly smaller than, a
310 static const uint32_t primes[] = {
335 UINT32_C (134217689),
336 UINT32_C (268435399),
337 UINT32_C (536870909),
338 UINT32_C (1073741789),
339 UINT32_C (2147483647),
341 UINT32_C (2147483647) + UINT32_C (2147483644)
344 const uint32_t *low = &primes[0];
345 const uint32_t *high = &primes[sizeof (primes) / sizeof (primes[0])];
349 const uint32_t *mid = low + (high - low) / 2;
357 /* If we've run out of primes, abort. */
360 fprintf (stderr, "Cannot find prime bigger than %lu\n", n);