Updated to fedora-glibc-20070809T0939
[glibc.git] / sysdeps / unix / sysv / linux / dl-osinfo.h
blob89bad440c91eb558d040e38324494033c48e8142
1 /* Operating system specific code for generic dynamic loader functions. Linux.
2 Copyright (C) 2000,2001,2002,2004,2005,2006,2007
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <string.h>
22 #include <errno.h>
23 #include <fcntl.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(Nhdr) hdr;
59 char vendor[8];
60 } expected_note = { { sizeof "Linux", sizeof (ElfW(Word)), 0 }, "Linux" };
61 const ElfW(Phdr) *const phdr = GLRO(dl_sysinfo_map)->l_phdr;
62 const ElfW(Word) phnum = GLRO(dl_sysinfo_map)->l_phnum;
63 for (uint_fast16_t i = 0; i < phnum; ++i)
64 if (phdr[i].p_type == PT_NOTE)
66 const ElfW(Addr) start = (phdr[i].p_vaddr
67 + GLRO(dl_sysinfo_map)->l_addr);
68 const ElfW(Nhdr) *note = (const void *) start;
69 while ((ElfW(Addr)) (note + 1) - start < phdr[i].p_memsz)
71 if (!memcmp (note, &expected_note, sizeof expected_note))
72 return *(const ElfW(Word) *) ((const void *) note
73 + sizeof expected_note);
74 #define ROUND(len) (((len) + sizeof note->n_type - 1) & -sizeof note->n_type)
75 note = ((const void *) (note + 1)
76 + ROUND (note->n_namesz) + ROUND (note->n_descsz));
80 #endif
82 char bufmem[64];
83 char *buf = bufmem;
84 unsigned int version;
85 int parts;
86 char *cp;
87 struct utsname uts;
89 /* Try the uname system call. */
90 if (__uname (&uts))
92 /* This was not successful. Now try reading the /proc filesystem. */
93 int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);
94 if (fd < 0)
95 return -1;
96 ssize_t reslen = __read (fd, bufmem, sizeof (bufmem));
97 __close (fd);
98 if (reslen <= 0)
99 /* This also didn't work. We give up since we cannot
100 make sure the library can actually work. */
101 return -1;
102 buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';
104 else
105 buf = uts.release;
107 /* Now convert it into a number. The string consists of at most
108 three parts. */
109 version = 0;
110 parts = 0;
111 cp = buf;
112 while ((*cp >= '0') && (*cp <= '9'))
114 unsigned int here = *cp++ - '0';
116 while ((*cp >= '0') && (*cp <= '9'))
118 here *= 10;
119 here += *cp++ - '0';
122 ++parts;
123 version <<= 8;
124 version |= here;
126 if (*cp++ != '.')
127 /* Another part following? */
128 break;
131 if (parts < 3)
132 version <<= 8 * (3 - parts);
134 return version;
137 #define DL_SYSDEP_OSCHECK(FATAL) \
138 do { \
139 /* Test whether the kernel is new enough. This test is only performed \
140 if the library is not compiled to run on all kernels. */ \
142 int version = _dl_discover_osversion (); \
143 if (__builtin_expect (version >= 0, 1)) \
145 if (__builtin_expect (GLRO(dl_osversion) == 0, 1) \
146 || GLRO(dl_osversion) > version) \
147 GLRO(dl_osversion) = version; \
149 /* Now we can test with the required version. */ \
150 if (__LINUX_KERNEL_VERSION > 0 && version < __LINUX_KERNEL_VERSION) \
151 /* Not sufficent. */ \
152 FATAL ("FATAL: kernel too old\n"); \
154 else if (__LINUX_KERNEL_VERSION > 0) \
155 FATAL ("FATAL: cannot determine kernel version\n"); \
156 } while (0)
158 static inline uintptr_t __attribute__ ((always_inline))
159 _dl_setup_stack_chk_guard (void)
161 uintptr_t ret;
162 #ifdef ENABLE_STACKGUARD_RANDOMIZE
163 int fd = __open ("/dev/urandom", O_RDONLY);
164 if (fd >= 0)
166 ssize_t reslen = __read (fd, &ret, sizeof (ret));
167 __close (fd);
168 if (reslen == (ssize_t) sizeof (ret))
169 return ret;
171 #endif
172 ret = 0;
173 unsigned char *p = (unsigned char *) &ret;
174 p[sizeof (ret) - 1] = 255;
175 p[sizeof (ret) - 2] = '\n';
176 #ifdef HP_TIMING_NOW
177 hp_timing_t hpt;
178 HP_TIMING_NOW (hpt);
179 hpt = (hpt & 0xffff) << 8;
180 ret ^= hpt;
181 #endif
182 uintptr_t stk;
183 /* Avoid GCC being too smart. */
184 asm ("" : "=r" (stk) : "r" (p));
185 stk &= 0x7ffff0;
186 #if __BYTE_ORDER == __LITTLE_ENDIAN
187 stk <<= (__WORDSIZE - 23);
188 #elif __WORDSIZE == 64
189 stk <<= 31;
190 #endif
191 ret ^= stk;
192 /* Avoid GCC being too smart. */
193 p = (unsigned char *) &errno;
194 asm ("" : "=r" (stk) : "r" (p));
195 stk &= 0x7fff00;
196 #if __BYTE_ORDER == __LITTLE_ENDIAN
197 stk <<= (__WORDSIZE - 29);
198 #else
199 stk >>= 8;
200 #endif
201 ret ^= stk;
202 return ret;