Updated to fedora-glibc-20060109T2152
[glibc/history.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
blob6cc0550f3fa02d721cae3ff6a4cef52c5aff025f
1 /* Operating system specific code for generic dynamic loader functions. Linux.
2 Copyright (C) 2000,2001,2002,2004,2005,2006 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 <string.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <sys/sysctl.h>
24 #include <sys/utsname.h>
25 #include <kernel-features.h>
26 #include <dl-sysdep.h>
27 #include <stdint.h>
28 #include <hp-timing.h>
29 #include <endian.h>
31 #ifndef MIN
32 # define MIN(a,b) (((a)<(b))?(a):(b))
33 #endif
35 #ifdef SHARED
36 /* This is the function used in the dynamic linker to print the fatal error
37 message. */
38 static inline void
39 __attribute__ ((__noreturn__))
40 dl_fatal (const char *str)
42 _dl_dprintf (2, str);
43 _exit (1);
45 #endif
47 static inline int __attribute__ ((always_inline))
48 _dl_discover_osversion (void)
50 #if (defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO) && defined SHARED
51 if (GLRO(dl_sysinfo_map) != NULL)
53 /* If the kernel-supplied DSO contains a note indicating the kernel's
54 version, we don't need to call uname or parse any strings. */
56 static const struct
58 ElfW(Word) vendorlen;
59 ElfW(Word) datalen;
60 ElfW(Word) type;
61 char vendor[8];
62 } expected_note = { sizeof "Linux", sizeof (ElfW(Word)), 0, "Linux" };
63 const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
64 const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
65 for (uint_fast16_t i = 0; i < phnum; ++i)
66 if (phdr[i].p_type == PT_NOTE)
68 const ElfW(Addr) start = (phdr[i].p_vaddr
69 + GLRO(dl_sysinfo_map)->l_addr);
70 const struct
72 ElfW(Word) vendorlen;
73 ElfW(Word) datalen;
74 ElfW(Word) type;
75 } *note = (const void *) start;
76 while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
78 if (!memcmp (note, &expected_note, sizeof expected_note))
79 return *(const ElfW(Word) *) ((const void *) note
80 + sizeof expected_note);
81 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
82 note = ((const void *) (note + 1)
83 + ROUND (note->vendorlen) + ROUND (note->datalen));
87 #endif
89 char bufmem[64];
90 char *buf = bufmem;
91 unsigned int version;
92 int parts;
93 char *cp;
94 struct utsname uts;
96 /* Try the uname system call. */
97 if (__uname (&uts))
99 /* This was not successful. Now try reading the /proc filesystem. */
100 int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);
101 if (fd < 0)
102 return -1;
103 ssize_t reslen = __read (fd, bufmem, sizeof (bufmem));
104 __close (fd);
105 if (reslen <= 0)
106 /* This also didn't work. We give up since we cannot
107 make sure the library can actually work. */
108 return -1;
109 buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';
111 else
112 buf = uts.release;
114 /* Now convert it into a number. The string consists of at most
115 three parts. */
116 version = 0;
117 parts = 0;
118 cp = buf;
119 while ((*cp >= '0') && (*cp <= '9'))
121 unsigned int here = *cp++ - '0';
123 while ((*cp >= '0') && (*cp <= '9'))
125 here *= 10;
126 here += *cp++ - '0';
129 ++parts;
130 version <<= 8;
131 version |= here;
133 if (*cp++ != '.')
134 /* Another part following? */
135 break;
138 if (parts < 3)
139 version <<= 8 * (3 - parts);
141 return version;
144 #define DL_SYSDEP_OSCHECK(FATAL) \
145 do { \
146 /* Test whether the kernel is new enough. This test is only performed \
147 if the library is not compiled to run on all kernels. */ \
149 int version = _dl_discover_osversion (); \
150 if (__builtin_expect (version >= 0, 1)) \
152 if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \
153 || GLRO(dl_osversion) > version) \
154 GLRO(dl_osversion) = version; \
156 /* Now we can test with the required version. */ \
157 if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \
158 /* Not sufficent. */ \
159 FATAL ("FATAL: kernel too old\n"); \
161 else if (__LINUX_KERNEL_VERSION > 0) \
162 FATAL ("FATAL: cannot determine kernel version\n"); \
163 } while (0)
165 static inline uintptr_t __attribute__ ((always_inline))
166 _dl_setup_stack_chk_guard (void)
168 uintptr_t ret;
169 #ifdef ENABLE_STACKGUARD_RANDOMIZE
170 int fd = __open ("/dev/urandom", O_RDONLY);
171 if (fd >= 0)
173 ssize_t reslen = __read (fd, &ret, sizeof (ret));
174 __close (fd);
175 if (reslen == (ssize_t) sizeof (ret))
176 return ret;
178 #endif
179 ret = 0;
180 unsigned char *p = (unsigned char *) &ret;
181 p[sizeof (ret) - 1] = 255;
182 p[sizeof (ret) - 2] = '\n';
183 #ifdef HP_TIMING_NOW
184 hp_timing_t hpt;
185 HP_TIMING_NOW (hpt);
186 hpt = (hpt & 0xffff) << 8;
187 ret ^= hpt;
188 #endif
189 uintptr_t stk;
190 /* Avoid GCC being too smart. */
191 asm ("" : "=r" (stk) : "r" (p));
192 stk &= 0x7ffff0;
193 #if __BYTE_ORDER == __LITTLE_ENDIAN
194 stk <<= (__WORDSIZE - 23);
195 #elif __WORDSIZE == 64
196 stk <<= 31;
197 #endif
198 ret ^= stk;
199 /* Avoid GCC being too smart. */
200 p = (unsigned char *) &errno;
201 asm ("" : "=r" (stk) : "r" (p));
202 stk &= 0x7fff00;
203 #if __BYTE_ORDER == __LITTLE_ENDIAN
204 stk <<= (__WORDSIZE - 29);
205 #else
206 stk >>= 8;
207 #endif
208 ret ^= stk;
209 return ret;