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
23 #include <sys/sysctl.h>
24 #include <sys/utsname.h>
25 #include <kernel-features.h>
26 #include <dl-sysdep.h>
28 #include <hp-timing.h>
32 # define MIN(a,b) (((a)<(b))?(a):(b))
36 /* This is the function used in the dynamic linker to print the fatal error
39 __attribute__ ((__noreturn__
))
40 dl_fatal (const char *str
)
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. */
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
);
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
));
96 /* Try the uname system call. */
99 /* This was not successful. Now try reading the /proc filesystem. */
100 int fd
= __open ("/proc/sys/kernel/osrelease", O_RDONLY
);
103 ssize_t reslen
= __read (fd
, bufmem
, sizeof (bufmem
));
106 /* This also didn't work. We give up since we cannot
107 make sure the library can actually work. */
109 buf
[MIN (reslen
, (ssize_t
) sizeof (bufmem
) - 1)] = '\0';
114 /* Now convert it into a number. The string consists of at most
119 while ((*cp
>= '0') && (*cp
<= '9'))
121 unsigned int here
= *cp
++ - '0';
123 while ((*cp
>= '0') && (*cp
<= '9'))
134 /* Another part following? */
139 version
<<= 8 * (3 - parts
);
144 #define DL_SYSDEP_OSCHECK(FATAL) \
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"); \
165 static inline uintptr_t __attribute__ ((always_inline
))
166 _dl_setup_stack_chk_guard (void)
169 #ifdef ENABLE_STACKGUARD_RANDOMIZE
170 int fd
= __open ("/dev/urandom", O_RDONLY
);
173 ssize_t reslen
= __read (fd
, &ret
, sizeof (ret
));
175 if (reslen
== (ssize_t
) sizeof (ret
))
180 unsigned char *p
= (unsigned char *) &ret
;
181 p
[sizeof (ret
) - 1] = 255;
182 p
[sizeof (ret
) - 2] = '\n';
186 hpt
= (hpt
& 0xffff) << 8;
190 /* Avoid GCC being too smart. */
191 asm ("" : "=r" (stk
) : "r" (p
));
193 #if __BYTE_ORDER == __LITTLE_ENDIAN
194 stk
<<= (__WORDSIZE
- 23);
195 #elif __WORDSIZE == 64
199 /* Avoid GCC being too smart. */
200 p
= (unsigned char *) &errno
;
201 asm ("" : "=r" (stk
) : "r" (p
));
203 #if __BYTE_ORDER == __LITTLE_ENDIAN
204 stk
<<= (__WORDSIZE
- 29);