1 /* Dynamic linker system dependencies for Linux.
2 Copyright (C) 1995,1997,2001,2004,2005,2006, 2008 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, see
17 <http://www.gnu.org/licenses/>. */
19 /* Linux needs some special initialization, but otherwise uses
20 the generic dynamic linker system interface code. */
25 #include <sys/utsname.h>
27 #include <kernel-features.h>
30 # define DL_SYSDEP_INIT frob_brk ()
35 __brk (0); /* Initialize the break. */
37 #if ! __ASSUME_BRK_PAGE_ROUNDED
38 /* If the dynamic linker was executed as a program, then the break may
39 start immediately after our data segment. However, dl-minimal.c has
40 already stolen the remainder of the page for internal allocations.
41 If we don't adjust the break location recorded by the kernel, the
42 normal program startup will inquire, find the value at our &_end,
43 and start allocating its own data there, clobbering dynamic linker
44 data structures allocated there during startup.
46 Later Linux kernels have changed this behavior so that the initial
47 break value is rounded up to the page boundary before we start. */
49 extern char *__curbrk attribute_hidden
;
50 extern char _end
[] attribute_hidden
;
51 char *const endpage
= (void *) 0 + (((__curbrk
- (char *) 0)
52 + GLRO(dl_pagesize
) - 1)
53 & -GLRO(dl_pagesize
));
54 if (__builtin_expect (__curbrk
>= _end
&& __curbrk
< endpage
, 0))
59 # include <elf/dl-sysdep.c>
65 _dl_discover_osversion (void)
67 #if (defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO) && defined SHARED
68 if (GLRO(dl_sysinfo_map
) != NULL
)
70 /* If the kernel-supplied DSO contains a note indicating the kernel's
71 version, we don't need to call uname or parse any strings. */
77 } expected_note
= { { sizeof "Linux", sizeof (ElfW(Word
)), 0 }, "Linux" };
78 const ElfW(Phdr
) *const phdr
= GLRO(dl_sysinfo_map
)->l_phdr
;
79 const ElfW(Word
) phnum
= GLRO(dl_sysinfo_map
)->l_phnum
;
80 for (uint_fast16_t i
= 0; i
< phnum
; ++i
)
81 if (phdr
[i
].p_type
== PT_NOTE
)
83 const ElfW(Addr
) start
= (phdr
[i
].p_vaddr
84 + GLRO(dl_sysinfo_map
)->l_addr
);
85 const ElfW(Nhdr
) *note
= (const void *) start
;
86 while ((ElfW(Addr
)) (note
+ 1) - start
< phdr
[i
].p_memsz
)
88 if (!memcmp (note
, &expected_note
, sizeof expected_note
))
89 return *(const ElfW(Word
) *) ((const void *) note
90 + sizeof expected_note
);
91 #define ROUND(len) (((len) + sizeof note->n_type - 1) & -sizeof note->n_type)
92 note
= ((const void *) (note
+ 1)
93 + ROUND (note
->n_namesz
) + ROUND (note
->n_descsz
));
102 unsigned int version
;
107 /* Try the uname system call. */
110 /* This was not successful. Now try reading the /proc filesystem. */
111 int fd
= __open ("/proc/sys/kernel/osrelease", O_RDONLY
);
114 ssize_t reslen
= __read (fd
, bufmem
, sizeof (bufmem
));
117 /* This also didn't work. We give up since we cannot
118 make sure the library can actually work. */
120 buf
[MIN (reslen
, (ssize_t
) sizeof (bufmem
) - 1)] = '\0';
125 /* Now convert it into a number. The string consists of at most
130 while ((*cp
>= '0') && (*cp
<= '9'))
132 unsigned int here
= *cp
++ - '0';
134 while ((*cp
>= '0') && (*cp
<= '9'))
144 if (*cp
++ != '.' || parts
== 3)
145 /* Another part following? */
150 version
<<= 8 * (3 - parts
);