]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/dl-sysdep.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / dl-sysdep.c
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.
4
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.
9
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.
14
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/>. */
18
19 /* Linux needs some special initialization, but otherwise uses
20 the generic dynamic linker system interface code. */
21
22 #include <string.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/utsname.h>
26 #include <ldsodefs.h>
27 #include <kernel-features.h>
28
29 #ifdef SHARED
30 # define DL_SYSDEP_INIT frob_brk ()
31
32 static inline void
33 frob_brk (void)
34 {
35 __brk (0); /* Initialize the break. */
36
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.
45
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. */
48
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))
55 __brk (endpage);
56 #endif
57 }
58
59 # include <elf/dl-sysdep.c>
60 #endif
61
62
63 int
64 attribute_hidden
65 _dl_discover_osversion (void)
66 {
67 #if (defined NEED_DL_SYSINFO || defined NEED_DL_SYSINFO_DSO) && defined SHARED
68 if (GLRO(dl_sysinfo_map) != NULL)
69 {
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. */
72
73 static const struct
74 {
75 ElfW(Nhdr) hdr;
76 char vendor[8];
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)
82 {
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)
87 {
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));
94 #undef ROUND
95 }
96 }
97 }
98 #endif
99
100 char bufmem[64];
101 char *buf = bufmem;
102 unsigned int version;
103 int parts;
104 char *cp;
105 struct utsname uts;
106
107 /* Try the uname system call. */
108 if (__uname (&uts))
109 {
110 /* This was not successful. Now try reading the /proc filesystem. */
111 int fd = __open ("/proc/sys/kernel/osrelease", O_RDONLY);
112 if (fd < 0)
113 return -1;
114 ssize_t reslen = __read (fd, bufmem, sizeof (bufmem));
115 __close (fd);
116 if (reslen <= 0)
117 /* This also didn't work. We give up since we cannot
118 make sure the library can actually work. */
119 return -1;
120 buf[MIN (reslen, (ssize_t) sizeof (bufmem) - 1)] = '\0';
121 }
122 else
123 buf = uts.release;
124
125 /* Now convert it into a number. The string consists of at most
126 three parts. */
127 version = 0;
128 parts = 0;
129 cp = buf;
130 while ((*cp >= '0') && (*cp <= '9'))
131 {
132 unsigned int here = *cp++ - '0';
133
134 while ((*cp >= '0') && (*cp <= '9'))
135 {
136 here *= 10;
137 here += *cp++ - '0';
138 }
139
140 ++parts;
141 version <<= 8;
142 version |= here;
143
144 if (*cp++ != '.' || parts == 3)
145 /* Another part following? */
146 break;
147 }
148
149 if (parts < 3)
150 version <<= 8 * (3 - parts);
151
152 return version;
153 }