Fix __ASSUME_RECVMMSG issues (bug 16610).
[glibc.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
blobaf1343822fbb0bd9ef0a5a1685d1d96f8111adb9
1 /* Operating system specific code for generic dynamic loader functions. Linux.
2 Copyright (C) 2000-2014 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/>. */
19 #include <kernel-features.h>
20 #include <dl-sysdep.h>
21 #include <endian.h>
22 #include <fcntl.h>
23 #include <stdint.h>
24 #include <not-cancel.h>
26 #ifndef MIN
27 # define MIN(a,b) (((a)<(b))?(a):(b))
28 #endif
30 #define DL_SYSDEP_OSCHECK(FATAL) \
31 do { \
32 /* Test whether the kernel is new enough. This test is only performed \
33 if the library is not compiled to run on all kernels. */ \
35 int version = _dl_discover_osversion (); \
36 if (__glibc_likely (version >= 0)) \
37 { \
38 if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \
39 || GLRO(dl_osversion) > version) \
40 GLRO(dl_osversion) = version; \
42 /* Now we can test with the required version. */ \
43 if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \
44 /* Not sufficent. */ \
45 FATAL ("FATAL: kernel too old\n"); \
46 } \
47 else if (__LINUX_KERNEL_VERSION > 0) \
48 FATAL ("FATAL: cannot determine kernel version\n"); \
49 } while (0)
51 static inline uintptr_t __attribute__ ((always_inline))
52 _dl_setup_stack_chk_guard (void *dl_random)
54 union
56 uintptr_t num;
57 unsigned char bytes[sizeof (uintptr_t)];
58 } ret;
60 #ifndef __ASSUME_AT_RANDOM
61 if (__glibc_unlikely (dl_random == NULL))
63 const size_t filllen = sizeof (ret.bytes) - 1;
64 ret.num = 0;
65 # ifdef ENABLE_STACKGUARD_RANDOMIZE
66 int fd = open_not_cancel_2 ("/dev/urandom", O_RDONLY);
67 if (fd >= 0)
69 ssize_t reslen = read_not_cancel (fd, ret.bytes + 1, filllen);
70 close_not_cancel_no_status (fd);
71 if (reslen == (ssize_t) filllen)
72 return ret.num;
74 # endif
75 ret.bytes[filllen] = 255;
76 ret.bytes[filllen - 1] = '\n';
78 else
79 #endif
81 /* We need in the moment only 8 bytes on 32-bit platforms and 16
82 bytes on 64-bit platforms. Therefore we can use the data
83 directly and not use the kernel-provided data to seed a PRNG. */
84 memcpy (ret.bytes, dl_random, sizeof (ret));
85 #if BYTE_ORDER == LITTLE_ENDIAN
86 ret.num &= ~(uintptr_t) 0xff;
87 #elif BYTE_ORDER == BIG_ENDIAN
88 ret.num &= ~((uintptr_t) 0xff << (8 * (sizeof (ret) - 1)));
89 #else
90 # error "BYTE_ORDER unknown"
91 #endif
93 return ret.num;
96 static inline uintptr_t __attribute__ ((always_inline))
97 _dl_setup_pointer_guard (void *dl_random, uintptr_t stack_chk_guard)
99 uintptr_t ret;
100 #ifndef __ASSUME_AT_RANDOM
101 if (dl_random == NULL)
103 ret = stack_chk_guard;
104 # ifndef HP_TIMING_NONAVAIL
105 hp_timing_t now;
106 HP_TIMING_NOW (now);
107 ret ^= now;
108 # endif
110 else
111 #endif
112 memcpy (&ret, (char *) dl_random + sizeof (ret), sizeof (ret));
113 return ret;