Updated to fedora-glibc-20080310T1651
[glibc/history.git] / sysdeps / unix / sysv / linux / dl-sysdep.c
blob08ae9aa86d24f15fcb5b162e9e2afc3e5b0f45f9
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
18 02111-1307 USA. */
20 /* Linux needs some special initialization, but otherwise uses
21 the generic dynamic linker system interface code. */
23 #include <string.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <sys/utsname.h>
27 #include <ldsodefs.h>
28 #include <kernel-features.h>
30 #ifdef SHARED
31 # define DL_SYSDEP_INIT frob_brk ()
33 static inline void
34 frob_brk (void)
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 void *__curbrk attribute_hidden;
51 extern void _end attribute_hidden;
52 void *const endpage = (void *) 0 + (((__curbrk - (void *) 0)
53 + GLRO(dl_pagesize) - 1)
54 & -GLRO(dl_pagesize));
55 if (__builtin_expect (__curbrk >= &_end && __curbrk < endpage, 0))
56 __brk (endpage);
57 #endif
60 # include <elf/dl-sysdep.c>
61 #endif
64 int
65 attribute_hidden
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. */
74 static const struct
76 ElfW(Nhdr) hdr;
77 char vendor[8];
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));
95 #undef ROUND
99 #endif
101 char bufmem[64];
102 char *buf = bufmem;
103 unsigned int version;
104 int parts;
105 char *cp;
106 struct utsname uts;
108 /* Try the uname system call. */
109 if (__uname (&uts))
111 /* This was not successful. Now try reading the /proc filesystem. */
112 int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);
113 if (fd < 0)
114 return -1;
115 ssize_t reslen = __read (fd, bufmem, sizeof (bufmem));
116 __close (fd);
117 if (reslen <= 0)
118 /* This also didn't work. We give up since we cannot
119 make sure the library can actually work. */
120 return -1;
121 buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';
123 else
124 buf = uts.release;
126 /* Now convert it into a number. The string consists of at most
127 three parts. */
128 version = 0;
129 parts = 0;
130 cp = buf;
131 while ((*cp >= '0') && (*cp <= '9'))
133 unsigned int here = *cp++ - '0';
135 while ((*cp >= '0') && (*cp <= '9'))
137 here *= 10;
138 here += *cp++ - '0';
141 ++parts;
142 version <<= 8;
143 version |= here;
145 if (*cp++ != '.' || parts == 3)
146 /* Another part following? */
147 break;
150 if (parts < 3)
151 version <<= 8 * (3 - parts);
153 return version;