1/* Miscellaneous support functions for dynamic linker
2 Copyright (C) 1997-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 <assert.h>
20#include <fcntl.h>
21#include <ldsodefs.h>
22#include <limits.h>
23#include <link.h>
24#include <stdarg.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28#include <stdint.h>
29#include <sys/mman.h>
30#include <sys/param.h>
31#include <sys/stat.h>
32#include <sys/uio.h>
33#include <sysdep.h>
34#include <_itoa.h>
35#include <dl-writev.h>
36
37
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
40 is returned. */
41
42void *
43internal_function
44_dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot)
45{
46 void *result = MAP_FAILED;
47 struct stat64 st;
48 int flags = O_RDONLY;
49#ifdef O_CLOEXEC
50 flags |= O_CLOEXEC;
51#endif
52 int fd = __open (file, flags);
53 if (fd >= 0)
54 {
55 if (__fxstat64 (_STAT_VER, fd, &st) >= 0)
56 {
57 *sizep = st.st_size;
58
59 /* No need to map the file if it is empty. */
60 if (*sizep != 0)
61 /* Map a copy of the file contents. */
62 result = __mmap (NULL, *sizep, prot,
63#ifdef MAP_COPY
64 MAP_COPY
65#else
66 MAP_PRIVATE
67#endif
68#ifdef MAP_FILE
69 | MAP_FILE
70#endif
71 , fd, 0);
72 }
73 __close (fd);
74 }
75 return result;
76}
77
78
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
81 the output. */
82static void
83_dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg)
84{
85# define NIOVMAX 64
86 struct iovec iov[NIOVMAX];
87 int niov = 0;
88 pid_t pid = 0;
89 char pidbuf[12];
90
91 while (*fmt != '\0')
92 {
93 const char *startp = fmt;
94
95 if (tag_p > 0)
96 {
97 /* Generate the tag line once. It consists of the PID and a
98 colon followed by a tab. */
99 if (pid == 0)
100 {
101 char *p;
102 pid = __getpid ();
103 assert (pid >= 0 && sizeof (pid_t) <= 4);
104 p = _itoa (pid, &pidbuf[10], 10, 0);
105 while (p > pidbuf)
106 *--p = ' ';
107 pidbuf[10] = ':';
108 pidbuf[11] = '\t';
109 }
110
111 /* Append to the output. */
112 assert (niov < NIOVMAX);
113 iov[niov].iov_len = 12;
114 iov[niov++].iov_base = pidbuf;
115
116 /* No more tags until we see the next newline. */
117 tag_p = -1;
118 }
119
120 /* Skip everything except % and \n (if tags are needed). */
121 while (*fmt != '\0' && *fmt != '%' && (! tag_p || *fmt != '\n'))
122 ++fmt;
123
124 /* Append constant string. */
125 assert (niov < NIOVMAX);
126 if ((iov[niov].iov_len = fmt - startp) != 0)
127 iov[niov++].iov_base = (char *) startp;
128
129 if (*fmt == '%')
130 {
131 /* It is a format specifier. */
132 char fill = ' ';
133 int width = -1;
134 int prec = -1;
135#if LONG_MAX != INT_MAX
136 int long_mod = 0;
137#endif
138
139 /* Recognize zero-digit fill flag. */
140 if (*++fmt == '0')
141 {
142 fill = '0';
143 ++fmt;
144 }
145
146 /* See whether with comes from a parameter. Note that no other
147 way to specify the width is implemented. */
148 if (*fmt == '*')
149 {
150 width = va_arg (arg, int);
151 ++fmt;
152 }
153
154 /* Handle precision. */
155 if (*fmt == '.' && fmt[1] == '*')
156 {
157 prec = va_arg (arg, int);
158 fmt += 2;
159 }
160
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')
165 {
166#if LONG_MAX != INT_MAX
167 long_mod = 1;
168#endif
169 ++fmt;
170 }
171
172 switch (*fmt)
173 {
174 /* Integer formatting. */
175 case 'u':
176 case 'x':
177 {
178 /* We have to make a difference if long and int have a
179 different size. */
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));
184#else
185 unsigned long int num = va_arg (arg, unsigned int);
186#endif
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);
193
194 /* Pad to the width the user specified. */
195 if (width != -1)
196 while (endp - cp < width)
197 *--cp = fill;
198
199 iov[niov].iov_base = cp;
200 iov[niov].iov_len = endp - cp;
201 ++niov;
202 }
203 break;
204
205 case 's':
206 /* Get the string argument. */
207 iov[niov].iov_base = va_arg (arg, char *);
208 iov[niov].iov_len = strlen (iov[niov].iov_base);
209 if (prec != -1)
210 iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len);
211 ++niov;
212 break;
213
214 case '%':
215 iov[niov].iov_base = (void *) fmt;
216 iov[niov].iov_len = 1;
217 ++niov;
218 break;
219
220 default:
221 assert (! "invalid format specifier");
222 }
223 ++fmt;
224 }
225 else if (*fmt == '\n')
226 {
227 /* See whether we have to print a single newline character. */
228 if (fmt == startp)
229 {
230 iov[niov].iov_base = (char *) startp;
231 iov[niov++].iov_len = 1;
232 }
233 else
234 /* No, just add it to the rest of the string. */
235 ++iov[niov - 1].iov_len;
236
237 /* Next line, print a tag again. */
238 tag_p = 1;
239 ++fmt;
240 }
241 }
242
243 /* Finally write the result. */
244 _dl_writev (fd, iov, niov);
245}
246
247
248/* Write to debug file. */
249void
250_dl_debug_printf (const char *fmt, ...)
251{
252 va_list arg;
253
254 va_start (arg, fmt);
255 _dl_debug_vdprintf (GLRO(dl_debug_fd), 1, fmt, arg);
256 va_end (arg);
257}
258
259
260/* Write to debug file but don't start with a tag. */
261void
262_dl_debug_printf_c (const char *fmt, ...)
263{
264 va_list arg;
265
266 va_start (arg, fmt);
267 _dl_debug_vdprintf (GLRO(dl_debug_fd), -1, fmt, arg);
268 va_end (arg);
269}
270
271
272/* Write the given file descriptor. */
273void
274_dl_dprintf (int fd, const char *fmt, ...)
275{
276 va_list arg;
277
278 va_start (arg, fmt);
279 _dl_debug_vdprintf (fd, 0, fmt, arg);
280 va_end (arg);
281}
282
283
284/* Test whether given NAME matches any of the names of the given object. */
285int
286internal_function
287_dl_name_match_p (const char *name, const struct link_map *map)
288{
289 if (strcmp (name, map->l_name) == 0)
290 return 1;
291
292 struct libname_list *runp = map->l_libname;
293
294 while (runp != NULL)
295 if (strcmp (name, runp->name) == 0)
296 return 1;
297 else
298 runp = runp->next;
299
300 return 0;
301}
302
303
304unsigned long int
305internal_function
306_dl_higher_prime_number (unsigned long int n)
307{
308 /* These are primes that are near, but slightly smaller than, a
309 power of two. */
310 static const uint32_t primes[] = {
311 UINT32_C (7),
312 UINT32_C (13),
313 UINT32_C (31),
314 UINT32_C (61),
315 UINT32_C (127),
316 UINT32_C (251),
317 UINT32_C (509),
318 UINT32_C (1021),
319 UINT32_C (2039),
320 UINT32_C (4093),
321 UINT32_C (8191),
322 UINT32_C (16381),
323 UINT32_C (32749),
324 UINT32_C (65521),
325 UINT32_C (131071),
326 UINT32_C (262139),
327 UINT32_C (524287),
328 UINT32_C (1048573),
329 UINT32_C (2097143),
330 UINT32_C (4194301),
331 UINT32_C (8388593),
332 UINT32_C (16777213),
333 UINT32_C (33554393),
334 UINT32_C (67108859),
335 UINT32_C (134217689),
336 UINT32_C (268435399),
337 UINT32_C (536870909),
338 UINT32_C (1073741789),
339 UINT32_C (2147483647),
340 /* 4294967291L */
341 UINT32_C (2147483647) + UINT32_C (2147483644)
342 };
343
344 const uint32_t *low = &primes[0];
345 const uint32_t *high = &primes[sizeof (primes) / sizeof (primes[0])];
346
347 while (low != high)
348 {
349 const uint32_t *mid = low + (high - low) / 2;
350 if (n > *mid)
351 low = mid + 1;
352 else
353 high = mid;
354 }
355
356#if 0
357 /* If we've run out of primes, abort. */
358 if (n > *low)
359 {
360 fprintf (stderr, "Cannot find prime bigger than %lu\n", n);
361 abort ();
362 }
363#endif
364
365 return *low;
366}
367