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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 /* Linux needs some special initialization, but otherwise uses
21 the generic dynamic linker system interface code. */
26 #include <sys/utsname.h>
28 #include <kernel-features.h>
31 # define DL_SYSDEP_INIT frob_brk ()
36 __brk (0); /* Initialize the break. */
38 #if ! __ASSUME_BRK_PAGE_ROUNDED
39 /* If the dynamic linker was executed as a program, then the break may
40 start immediately after our data segment. However, dl-minimal.c has
41 already stolen the remainder of the page for internal allocations.
42 If we don't adjust the break location recorded by the kernel, the
43 normal program startup will inquire, find the value at our &_end,
44 and start allocating its own data there, clobbering dynamic linker
45 data structures allocated there during startup.
47 Later Linux kernels have changed this behavior so that the initial
48 break value is rounded up to the page boundary before we start. */
50 extern char *__curbrk attribute_hidden
;
51 extern char _end
[] attribute_hidden
;
52 char *const endpage
= (void *) 0 + (((__curbrk
- (char *) 0)
53 + GLRO(dl_pagesize
) - 1)
54 & -GLRO(dl_pagesize
));
55 if (__builtin_expect (__curbrk
>= _end
&& __curbrk
< endpage
, 0))
60 # include <elf/dl-sysdep.c>
66 _dl_discover_osversion (void)
68 #if (defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO) && defined SHARED
69 if (GLRO(dl_sysinfo_map
) != NULL
)
71 /* If the kernel-supplied DSO contains a note indicating the kernel's
72 version, we don't need to call uname or parse any strings. */
78 } expected_note
= { { sizeof "Linux", sizeof (ElfW(Word
)), 0 }, "Linux" };
79 const ElfW(Phdr
) *const phdr
= GLRO(dl_sysinfo_map
)->l_phdr
;
80 const ElfW(Word
) phnum
= GLRO(dl_sysinfo_map
)->l_phnum
;
81 for (uint_fast16_t i
= 0; i
< phnum
; ++i
)
82 if (phdr
[i
].p_type
== PT_NOTE
)
84 const ElfW(Addr
) start
= (phdr
[i
].p_vaddr
85 + GLRO(dl_sysinfo_map
)->l_addr
);
86 const ElfW(Nhdr
) *note
= (const void *) start
;
87 while ((ElfW(Addr
)) (note
+ 1) - start
< phdr
[i
].p_memsz
)
89 if (!memcmp (note
, &expected_note
, sizeof expected_note
))
90 return *(const ElfW(Word
) *) ((const void *) note
91 + sizeof expected_note
);
92 #define ROUND(len) (((len) + sizeof note->n_type - 1) & -sizeof note->n_type)
93 note
= ((const void *) (note
+ 1)
94 + ROUND (note
->n_namesz
) + ROUND (note
->n_descsz
));
103 unsigned int version
;
108 /* Try the uname system call. */
111 /* This was not successful. Now try reading the /proc filesystem. */
112 int fd
= __open ("/proc/sys/kernel/osrelease", O_RDONLY
);
115 ssize_t reslen
= __read (fd
, bufmem
, sizeof (bufmem
));
118 /* This also didn't work. We give up since we cannot
119 make sure the library can actually work. */
121 buf
[MIN (reslen
, (ssize_t
) sizeof (bufmem
) - 1)] = '\0';
126 /* Now convert it into a number. The string consists of at most
131 while ((*cp
>= '0') && (*cp
<= '9'))
133 unsigned int here
= *cp
++ - '0';
135 while ((*cp
>= '0') && (*cp
<= '9'))
145 if (*cp
++ != '.' || parts
== 3)
146 /* Another part following? */
151 version
<<= 8 * (3 - parts
);