Updated to fedora-glibc-20060802T1650
[glibc.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
blob3c93218dfc56f187ad290c03ac11374f1c6fd6e9
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/utsname.h>
24 #include <kernel-features.h>
25 #include <dl-sysdep.h>
26 #include <stdint.h>
27 #include <hp-timing.h>
28 #include <endian.h>
30 #ifndef MIN
31 # define MIN(a,b) (((a)<(b))?(a):(b))
32 #endif
34 #ifdef SHARED
35 /* This is the function used in the dynamic linker to print the fatal error
36 message. */
37 static inline void
38 __attribute__ ((__noreturn__))
39 dl_fatal (const char *str)
41 _dl_dprintf (2, str);
42 _exit (1);
44 #endif
46 static inline int __attribute__ ((always_inline))
47 _dl_discover_osversion (void)
49 #if (defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO) && defined SHARED
50 if (GLRO(dl_sysinfo_map) != NULL)
52 /* If the kernel-supplied DSO contains a note indicating the kernel's
53 version, we don't need to call uname or parse any strings. */
55 static const struct
57 ElfW(Word) vendorlen;
58 ElfW(Word) datalen;
59 ElfW(Word) type;
60 char vendor[8];
61 } expected_note = { sizeof "Linux", sizeof (ElfW(Word)), 0, "Linux" };
62 const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
63 const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
64 for (uint_fast16_t i = 0; i < phnum; ++i)
65 if (phdr[i].p_type == PT_NOTE)
67 const ElfW(Addr) start = (phdr[i].p_vaddr
68 + GLRO(dl_sysinfo_map)->l_addr);
69 const struct
71 ElfW(Word) vendorlen;
72 ElfW(Word) datalen;
73 ElfW(Word) type;
74 } *note = (const void *) start;
75 while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
77 if (!memcmp (note, &expected_note, sizeof expected_note))
78 return *(const ElfW(Word) *) ((const void *) note
79 + sizeof expected_note);
80 #define ROUND(len) (((len) + sizeof (ElfW(Word)) - 1) & -sizeof (ElfW(Word)))
81 note = ((const void *) (note + 1)
82 + ROUND (note->vendorlen) + ROUND (note->datalen));
86 #endif
88 char bufmem[64];
89 char *buf = bufmem;
90 unsigned int version;
91 int parts;
92 char *cp;
93 struct utsname uts;
95 /* Try the uname system call. */
96 if (__uname (&uts))
98 /* This was not successful. Now try reading the /proc filesystem. */
99 int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);
100 if (fd < 0)
101 return -1;
102 ssize_t reslen = __read (fd, bufmem, sizeof (bufmem));
103 __close (fd);
104 if (reslen <= 0)
105 /* This also didn't work. We give up since we cannot
106 make sure the library can actually work. */
107 return -1;
108 buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';
110 else
111 buf = uts.release;
113 /* Now convert it into a number. The string consists of at most
114 three parts. */
115 version = 0;
116 parts = 0;
117 cp = buf;
118 while ((*cp >= '0') && (*cp <= '9'))
120 unsigned int here = *cp++ - '0';
122 while ((*cp >= '0') && (*cp <= '9'))
124 here *= 10;
125 here += *cp++ - '0';
128 ++parts;
129 version <<= 8;
130 version |= here;
132 if (*cp++ != '.')
133 /* Another part following? */
134 break;
137 if (parts < 3)
138 version <<= 8 * (3 - parts);
140 return version;
143 #define DL_SYSDEP_OSCHECK(FATAL) \
144 do { \
145 /* Test whether the kernel is new enough. This test is only performed \
146 if the library is not compiled to run on all kernels. */ \
148 int version = _dl_discover_osversion (); \
149 if (__builtin_expect (version >= 0, 1)) \
151 if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \
152 || GLRO(dl_osversion) > version) \
153 GLRO(dl_osversion) = version; \
155 /* Now we can test with the required version. */ \
156 if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \
157 /* Not sufficent. */ \
158 FATAL ("FATAL: kernel too old\n"); \
160 else if (__LINUX_KERNEL_VERSION > 0) \
161 FATAL ("FATAL: cannot determine kernel version\n"); \
162 } while (0)
164 static inline uintptr_t __attribute__ ((always_inline))
165 _dl_setup_stack_chk_guard (void)
167 uintptr_t ret;
168 #ifdef ENABLE_STACKGUARD_RANDOMIZE
169 int fd = __open ("/dev/urandom", O_RDONLY);
170 if (fd >= 0)
172 ssize_t reslen = __read (fd, &ret, sizeof (ret));
173 __close (fd);
174 if (reslen == (ssize_t) sizeof (ret))
175 return ret;
177 #endif
178 ret = 0;
179 unsigned char *p = (unsigned char *) &ret;
180 p[sizeof (ret) - 1] = 255;
181 p[sizeof (ret) - 2] = '\n';
182 #ifdef HP_TIMING_NOW
183 hp_timing_t hpt;
184 HP_TIMING_NOW (hpt);
185 hpt = (hpt & 0xffff) << 8;
186 ret ^= hpt;
187 #endif
188 uintptr_t stk;
189 /* Avoid GCC being too smart. */
190 asm ("" : "=r" (stk) : "r" (p));
191 stk &= 0x7ffff0;
192 #if __BYTE_ORDER == __LITTLE_ENDIAN
193 stk <<= (__WORDSIZE - 23);
194 #elif __WORDSIZE == 64
195 stk <<= 31;
196 #endif
197 ret ^= stk;
198 /* Avoid GCC being too smart. */
199 p = (unsigned char *) &errno;
200 asm ("" : "=r" (stk) : "r" (p));
201 stk &= 0x7fff00;
202 #if __BYTE_ORDER == __LITTLE_ENDIAN
203 stk <<= (__WORDSIZE - 29);
204 #else
205 stk >>= 8;
206 #endif
207 ret ^= stk;
208 return ret;