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
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. */
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
));
89 /* Try the uname system call. */
92 /* This was not successful. Now try reading the /proc filesystem. */
93 int fd
= __open ("/proc/sys/kernel/osrelease", O_RDONLY
);
96 ssize_t reslen
= __read (fd
, bufmem
, sizeof (bufmem
));
99 /* This also didn't work. We give up since we cannot
100 make sure the library can actually work. */
102 buf
[MIN (reslen
, (ssize_t
) sizeof (bufmem
) - 1)] = '\0';
107 /* Now convert it into a number. The string consists of at most
112 while ((*cp
>= '0') && (*cp
<= '9'))
114 unsigned int here
= *cp
++ - '0';
116 while ((*cp
>= '0') && (*cp
<= '9'))
127 /* Another part following? */
132 version
<<= 8 * (3 - parts
);
137 #define DL_SYSDEP_OSCHECK(FATAL) \
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"); \
158 static inline uintptr_t __attribute__ ((always_inline
))
159 _dl_setup_stack_chk_guard (void)
162 #ifdef ENABLE_STACKGUARD_RANDOMIZE
163 int fd
= __open ("/dev/urandom", O_RDONLY
);
166 ssize_t reslen
= __read (fd
, &ret
, sizeof (ret
));
168 if (reslen
== (ssize_t
) sizeof (ret
))
173 unsigned char *p
= (unsigned char *) &ret
;
174 p
[sizeof (ret
) - 1] = 255;
175 p
[sizeof (ret
) - 2] = '\n';
179 hpt
= (hpt
& 0xffff) << 8;
183 /* Avoid GCC being too smart. */
184 asm ("" : "=r" (stk
) : "r" (p
));
186 #if __BYTE_ORDER == __LITTLE_ENDIAN
187 stk
<<= (__WORDSIZE
- 23);
188 #elif __WORDSIZE == 64
192 /* Avoid GCC being too smart. */
193 p
= (unsigned char *) &errno
;
194 asm ("" : "=r" (stk
) : "r" (p
));
196 #if __BYTE_ORDER == __LITTLE_ENDIAN
197 stk
<<= (__WORDSIZE
- 29);