]>
git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-misc.c
e669a2b3de8c84bf386a4caae78b9e2179bf284f
1 /* Miscellaneous support functions for dynamic linker
2 Copyright (C) 1997-2025 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 <https://www.gnu.org/licenses/>. */
23 #include <not-cancel.h>
30 /* Read the whole contents of FILE into new mmap'd space with given
31 protections. *SIZEP gets the size of the file. On error MAP_FAILED
35 _dl_sysdep_read_whole_file (const char *file
, size_t *sizep
, int prot
)
37 void *result
= MAP_FAILED
;
38 struct __stat64_t64 st
;
39 int fd
= __open64_nocancel (file
, O_RDONLY
| O_CLOEXEC
);
42 if (__fstat64_time64 (fd
, &st
) >= 0)
46 /* No need to map the file if it is empty. */
48 /* Map a copy of the file contents. */
49 result
= __mmap (NULL
, *sizep
, prot
,
60 __close_nocancel (fd
);
65 /* Test whether given NAME matches any of the names of the given object. */
67 _dl_name_match_p (const char *name
, const struct link_map
*map
)
69 if (strcmp (name
, map
->l_name
) == 0)
72 struct libname_list
*runp
= map
->l_libname
;
75 if (strcmp (name
, runp
->name
) == 0)
84 _dl_higher_prime_number (unsigned long int n
)
86 /* These are primes that are near, but slightly smaller than, a
88 static const uint32_t primes
[] = {
113 UINT32_C (134217689),
114 UINT32_C (268435399),
115 UINT32_C (536870909),
116 UINT32_C (1073741789),
117 UINT32_C (2147483647),
119 UINT32_C (2147483647) + UINT32_C (2147483644)
122 const uint32_t *low
= &primes
[0];
123 const uint32_t *high
= &primes
[sizeof (primes
) / sizeof (primes
[0])];
127 const uint32_t *mid
= low
+ (high
- low
) / 2;
135 /* If we've run out of primes, abort. */
138 fprintf (stderr
, "Cannot find prime bigger than %lu\n", n
);
146 /* A stripped down strtoul-like implementation for very early use. It
147 does not set errno if the result is outside bounds because it may get
148 called before errno may have been set up. */
151 _dl_strtoul (const char *nptr
, char **endptr
)
154 bool positive
= true;
157 while (*nptr
== ' ' || *nptr
== '\t')
165 else if (*nptr
== '+')
168 if (*nptr
< '0' || *nptr
> '9')
171 *endptr
= (char *) nptr
;
175 uint64_t cutoff
= (UINT64_MAX
* 2UL + 1UL) / 10;
176 uint64_t cutlim
= (UINT64_MAX
* 2UL + 1UL) % 10;
182 if (nptr
[1] == 'x' || nptr
[1] == 'X')
186 cutoff
= (UINT64_MAX
* 2UL + 1UL) / 16;
187 cutlim
= (UINT64_MAX
* 2UL + 1UL) % 16;
193 cutoff
= (UINT64_MAX
* 2UL + 1UL) / 8;
194 cutlim
= (UINT64_MAX
* 2UL + 1UL) % 8;
201 if (*nptr
>= '0' && *nptr
<= '0' + max_digit
)
202 digval
= *nptr
- '0';
205 if (*nptr
>= 'a' && *nptr
<= 'f')
206 digval
= *nptr
- 'a' + 10;
207 else if (*nptr
>= 'A' && *nptr
<= 'F')
208 digval
= *nptr
- 'A' + 10;
215 if (result
> cutoff
|| (result
== cutoff
&& digval
> cutlim
))
218 *endptr
= (char *) nptr
;
227 *endptr
= (char *) nptr
;
229 /* Avoid 64-bit multiplication. */