]>
Commit | Line | Data |
---|---|---|
0501d603 | 1 | /* Miscellaneous support functions for dynamic linker |
b168057a | 2 | Copyright (C) 1997-2015 Free Software Foundation, Inc. |
9498096c UD |
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 | |
41bdb6e2 AJ |
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. | |
9498096c UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
9498096c | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
9498096c | 18 | |
0501d603 UD |
19 | #include <assert.h> |
20 | #include <fcntl.h> | |
48896b9d | 21 | #include <ldsodefs.h> |
b5ba0659 | 22 | #include <limits.h> |
fc7f617d | 23 | #include <link.h> |
9498096c | 24 | #include <stdarg.h> |
b5ba0659 | 25 | #include <stdlib.h> |
9498096c | 26 | #include <string.h> |
8193034b | 27 | #include <unistd.h> |
e054f494 | 28 | #include <stdint.h> |
0501d603 | 29 | #include <sys/mman.h> |
b5ba0659 | 30 | #include <sys/param.h> |
0501d603 | 31 | #include <sys/stat.h> |
b5ba0659 | 32 | #include <sys/uio.h> |
5bbcba0d | 33 | #include <sysdep.h> |
eb96ffb0 | 34 | #include <_itoa.h> |
d5e82754 RM |
35 | #include <dl-writev.h> |
36 | ||
0501d603 | 37 | |
0501d603 | 38 | /* Read the whole contents of FILE into new mmap'd space with given |
40b07f5b UD |
39 | protections. *SIZEP gets the size of the file. On error MAP_FAILED |
40 | is returned. */ | |
0501d603 UD |
41 | |
42 | void * | |
48896b9d | 43 | internal_function |
0501d603 UD |
44 | _dl_sysdep_read_whole_file (const char *file, size_t *sizep, int prot) |
45 | { | |
40b07f5b | 46 | void *result = MAP_FAILED; |
5763742f | 47 | struct stat64 st; |
554881ef UD |
48 | int flags = O_RDONLY; |
49 | #ifdef O_CLOEXEC | |
50 | flags |= O_CLOEXEC; | |
51 | #endif | |
52 | int fd = __open (file, flags); | |
40b07f5b | 53 | if (fd >= 0) |
0501d603 | 54 | { |
40b07f5b UD |
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, | |
0501d603 | 63 | #ifdef MAP_COPY |
40b07f5b | 64 | MAP_COPY |
0501d603 | 65 | #else |
40b07f5b | 66 | MAP_PRIVATE |
0501d603 UD |
67 | #endif |
68 | #ifdef MAP_FILE | |
40b07f5b | 69 | | MAP_FILE |
0501d603 | 70 | #endif |
40b07f5b UD |
71 | , fd, 0); |
72 | } | |
73 | __close (fd); | |
0501d603 | 74 | } |
0501d603 UD |
75 | return result; |
76 | } | |
9498096c UD |
77 | |
78 | ||
5bbcba0d | 79 | /* Bare-bones printf implementation. This function only knows about |
b5ba0659 UD |
80 | the formats and flags needed and can handle only up to 64 stripes in |
81 | the output. */ | |
82 | static void | |
83 | _dl_debug_vdprintf (int fd, int tag_p, const char *fmt, va_list arg) | |
9498096c | 84 | { |
db2f05ba RM |
85 | # define NIOVMAX 64 |
86 | struct iovec iov[NIOVMAX]; | |
b5ba0659 UD |
87 | int niov = 0; |
88 | pid_t pid = 0; | |
d94e16c4 | 89 | char pidbuf[12]; |
9498096c | 90 | |
b5ba0659 | 91 | while (*fmt != '\0') |
9498096c | 92 | { |
b5ba0659 UD |
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 (); | |
d94e16c4 UD |
103 | assert (pid >= 0 && sizeof (pid_t) <= 4); |
104 | p = _itoa (pid, &pidbuf[10], 10, 0); | |
b5ba0659 | 105 | while (p > pidbuf) |
d94e16c4 UD |
106 | *--p = ' '; |
107 | pidbuf[10] = ':'; | |
108 | pidbuf[11] = '\t'; | |
b5ba0659 UD |
109 | } |
110 | ||
111 | /* Append to the output. */ | |
db2f05ba | 112 | assert (niov < NIOVMAX); |
d94e16c4 | 113 | iov[niov].iov_len = 12; |
b5ba0659 UD |
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. */ | |
db2f05ba | 125 | assert (niov < NIOVMAX); |
b5ba0659 UD |
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; | |
37d8b778 | 134 | int prec = -1; |
b5ba0659 UD |
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 | ||
37d8b778 UD |
154 | /* Handle precision. */ |
155 | if (*fmt == '.' && fmt[1] == '*') | |
156 | { | |
157 | prec = va_arg (arg, int); | |
158 | fmt += 2; | |
159 | } | |
160 | ||
b5ba0659 UD |
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 | |
7b97934b | 182 | ? va_arg (arg, unsigned long int) |
b5ba0659 UD |
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)]; | |
9710f75d | 192 | char *cp = _itoa (num, endp, *fmt == 'x' ? 16 : 10, 0); |
b5ba0659 UD |
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); | |
37d8b778 | 209 | if (prec != -1) |
6dd67bd5 | 210 | iov[niov].iov_len = MIN ((size_t) prec, iov[niov].iov_len); |
b5ba0659 UD |
211 | ++niov; |
212 | break; | |
213 | ||
cf44e2dd UD |
214 | case '%': |
215 | iov[niov].iov_base = (void *) fmt; | |
216 | iov[niov].iov_len = 1; | |
217 | ++niov; | |
218 | break; | |
219 | ||
b5ba0659 UD |
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 | } | |
8193034b | 241 | } |
b5ba0659 UD |
242 | |
243 | /* Finally write the result. */ | |
d5e82754 | 244 | _dl_writev (fd, iov, niov); |
8193034b UD |
245 | } |
246 | ||
247 | ||
b5ba0659 | 248 | /* Write to debug file. */ |
8193034b | 249 | void |
b5ba0659 | 250 | _dl_debug_printf (const char *fmt, ...) |
8193034b | 251 | { |
b5ba0659 UD |
252 | va_list arg; |
253 | ||
254 | va_start (arg, fmt); | |
dd70526e | 255 | _dl_debug_vdprintf (GLRO(dl_debug_fd), 1, fmt, arg); |
b5ba0659 UD |
256 | va_end (arg); |
257 | } | |
258 | ||
259 | ||
260 | /* Write to debug file but don't start with a tag. */ | |
261 | void | |
262 | _dl_debug_printf_c (const char *fmt, ...) | |
263 | { | |
264 | va_list arg; | |
265 | ||
266 | va_start (arg, fmt); | |
dd70526e | 267 | _dl_debug_vdprintf (GLRO(dl_debug_fd), -1, fmt, arg); |
b5ba0659 UD |
268 | va_end (arg); |
269 | } | |
270 | ||
271 | ||
272 | /* Write the given file descriptor. */ | |
273 | void | |
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); | |
9498096c | 281 | } |
dd272e57 UD |
282 | |
283 | ||
284 | /* Test whether given NAME matches any of the names of the given object. */ | |
285 | int | |
286 | internal_function | |
871b9158 | 287 | _dl_name_match_p (const char *name, const struct link_map *map) |
dd272e57 UD |
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 | } | |
eba0994e UD |
302 | |
303 | ||
304 | unsigned long int | |
55c4ce68 | 305 | internal_function |
eba0994e UD |
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), | |
554881ef | 340 | /* 4294967291L */ |
eba0994e UD |
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 | } |