Updated to fedora-glibc-20090427T1419
[glibc.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
blob430166a5788755cec369da518902570af618dc1a
1 /* Operating system specific code for generic dynamic loader functions. Linux.
2 Copyright (C) 2000-2002,2004-2008, 2009 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <kernel-features.h>
22 #include <dl-sysdep.h>
23 #include <fcntl.h>
24 #include <stdint.h>
25 #include <hp-timing.h>
26 #include <endian.h>
28 #ifndef MIN
29 # define MIN(a,b) (((a)<(b))?(a):(b))
30 #endif
32 #ifdef SHARED
33 /* This is the function used in the dynamic linker to print the fatal error
34 message. */
35 static void
36 __attribute__ ((__noreturn__))
37 dl_fatal (const char *str)
39 _dl_dprintf (2, str);
40 _exit (1);
42 #endif
44 #define DL_SYSDEP_OSCHECK(FATAL) \
45 do { \
46 /* Test whether the kernel is new enough. This test is only performed \
47 if the library is not compiled to run on all kernels. */ \
49 int version = _dl_discover_osversion (); \
50 if (__builtin_expect (version >= 0, 1)) \
51 { \
52 if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \
53 || GLRO(dl_osversion) > version) \
54 GLRO(dl_osversion) = version; \
56 /* Now we can test with the required version. */ \
57 if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \
58 /* Not sufficent. */ \
59 FATAL ("FATAL: kernel too old\n"); \
60 } \
61 else if (__LINUX_KERNEL_VERSION > 0) \
62 FATAL ("FATAL: cannot determine kernel version\n"); \
63 } while (0)
65 static inline uintptr_t __attribute__ ((always_inline))
66 _dl_setup_stack_chk_guard (void *dl_random)
68 uintptr_t ret;
69 #ifndef __ASSUME_AT_RANDOM
70 if (__builtin_expect (dl_random == NULL, 0))
72 # ifdef ENABLE_STACKGUARD_RANDOMIZE
73 int fd = __open ("/dev/urandom", O_RDONLY);
74 if (fd >= 0)
76 ssize_t reslen = __read (fd, &ret, sizeof (ret));
77 __close (fd);
78 if (reslen == (ssize_t) sizeof (ret))
79 return ret;
81 # endif
82 ret = 0;
83 unsigned char *p = (unsigned char *) &ret;
84 p[sizeof (ret) - 1] = 255;
85 p[sizeof (ret) - 2] = '\n';
86 #ifdef HP_TIMING_NOW
87 hp_timing_t hpt;
88 HP_TIMING_NOW (hpt);
89 hpt = (hpt & 0xffff) << 8;
90 ret ^= hpt;
91 #endif
92 uintptr_t stk;
93 /* Avoid GCC being too smart. */
94 asm ("" : "=r" (stk) : "r" (p));
95 stk &= 0x7ffff0;
96 #if __BYTE_ORDER == __LITTLE_ENDIAN
97 stk <<= (__WORDSIZE - 23);
98 #elif __WORDSIZE == 64
99 stk <<= 31;
100 #endif
101 ret ^= stk;
102 /* Avoid GCC being too smart. */
103 p = (unsigned char *) &errno;
104 asm ("" : "=r" (stk) : "r" (p));
105 stk &= 0x7fff00;
106 #if __BYTE_ORDER == __LITTLE_ENDIAN
107 stk <<= (__WORDSIZE - 29);
108 #else
109 stk >>= 8;
110 #endif
111 ret ^= stk;
113 else
114 #endif
115 /* We need in the moment only 8 bytes on 32-bit platforms and 16
116 bytes on 64-bit platforms. Therefore we can use the data
117 directly and not use the kernel-provided data to seed a PRNG. */
118 memcpy (&ret, dl_random, sizeof (ret));
119 return ret;
122 static inline uintptr_t __attribute__ ((always_inline))
123 _dl_setup_pointer_guard (void *dl_random, uintptr_t stack_chk_guard)
125 uintptr_t ret;
126 #ifndef __ASSUME_AT_RANDOM
127 if (dl_random == NULL)
129 ret = stack_chk_guard;
130 # ifndef HP_TIMING_NONAVAIL
131 hp_timing_t now;
132 HP_TIMING_NOW (now);
133 ret ^= now;
134 # endif
136 else
137 #endif
138 memcpy (&ret, (char *) dl_random + sizeof (ret), sizeof (ret));
139 return ret;