1/* Dynamic linker system dependencies for Linux.
2 Copyright (C) 1995-2024 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 <https://www.gnu.org/licenses/>. */
18
19#include <_itoa.h>
20#include <assert.h>
21#include <dl-auxv.h>
22#include <dl-osinfo.h>
23#include <dl-parse_auxv.h>
24#include <dl-procinfo.h>
25#include <dl-tunables.h>
26#include <elf.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <ldsodefs.h>
30#include <libc-internal.h>
31#include <libintl.h>
32#include <not-cancel.h>
33#include <stdlib.h>
34#include <string.h>
35#include <string.h>
36#include <sys/mman.h>
37#include <sys/param.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <sys/utsname.h>
41#include <tls.h>
42#include <unistd.h>
43#include <dl-symbol-redir-ifunc.h>
44
45#include <dl-machine.h>
46#include <dl-hwcap-check.h>
47
48#ifdef SHARED
49extern char **_environ attribute_hidden;
50extern char _end[] attribute_hidden;
51
52/* Protect SUID program against misuse of file descriptors. */
53extern void __libc_check_standard_fds (void);
54
55int __libc_enable_secure attribute_relro = 0;
56rtld_hidden_data_def (__libc_enable_secure)
57/* This variable contains the lowest stack address ever used. */
58void *__libc_stack_end attribute_relro = NULL;
59rtld_hidden_data_def(__libc_stack_end)
60void *_dl_random attribute_relro = NULL;
61
62#ifndef DL_STACK_END
63# define DL_STACK_END(cookie) ((void *) (cookie))
64#endif
65
66/* Arguments passed to dl_main. */
67struct dl_main_arguments
68{
69 const ElfW(Phdr) *phdr;
70 ElfW(Word) phnum;
71 ElfW(Addr) user_entry;
72};
73
74/* Separate function, so that dl_main can be called without the large
75 array on the stack. */
76static void
77_dl_sysdep_parse_arguments (void **start_argptr,
78 struct dl_main_arguments *args)
79{
80 _dl_argc = (intptr_t) *start_argptr;
81 _dl_argv = (char **) (start_argptr + 1); /* Necessary aliasing violation. */
82 _environ = _dl_argv + _dl_argc + 1;
83 for (char **tmp = _environ; ; ++tmp)
84 if (*tmp == NULL)
85 {
86 /* Another necessary aliasing violation. */
87 GLRO(dl_auxv) = (ElfW(auxv_t) *) (tmp + 1);
88 break;
89 }
90
91 dl_parse_auxv_t auxv_values = { 0, };
92 _dl_parse_auxv (GLRO(dl_auxv), auxv_values);
93
94 args->phdr = (const ElfW(Phdr) *) auxv_values[AT_PHDR];
95 args->phnum = auxv_values[AT_PHNUM];
96 args->user_entry = auxv_values[AT_ENTRY];
97}
98
99ElfW(Addr)
100_dl_sysdep_start (void **start_argptr,
101 void (*dl_main) (const ElfW(Phdr) *phdr, ElfW(Word) phnum,
102 ElfW(Addr) *user_entry, ElfW(auxv_t) *auxv))
103{
104 __libc_stack_end = DL_STACK_END (start_argptr);
105
106 struct dl_main_arguments dl_main_args;
107 _dl_sysdep_parse_arguments (start_argptr, &dl_main_args);
108
109 dl_hwcap_check ();
110
111 __tunables_init (_environ);
112
113 /* Initialize DSO sorting algorithm after tunables. */
114 _dl_sort_maps_init ();
115
116 __brk (0); /* Initialize the break. */
117
118#ifdef DL_PLATFORM_INIT
119 DL_PLATFORM_INIT;
120#endif
121
122 /* Determine the length of the platform name. */
123 if (GLRO(dl_platform) != NULL)
124 GLRO(dl_platformlen) = strlen (GLRO(dl_platform));
125
126 if (__sbrk (0) == _end)
127 /* The dynamic linker was run as a program, and so the initial break
128 starts just after our bss, at &_end. The malloc in dl-minimal.c
129 will consume the rest of this page, so tell the kernel to move the
130 break up that far. When the user program examines its break, it
131 will see this new value and not clobber our data. */
132 __sbrk (GLRO(dl_pagesize)
133 - (((uintptr_t) _end) & (GLRO(dl_pagesize) - 1)));
134
135 /* If this is a SUID program we make sure that FDs 0, 1, and 2 are
136 allocated. If necessary we are doing it ourself. If it is not
137 possible we stop the program. */
138 if (__builtin_expect (__libc_enable_secure, 0))
139 __libc_check_standard_fds ();
140
141 (*dl_main) (dl_main_args.phdr, dl_main_args.phnum,
142 &dl_main_args.user_entry, GLRO(dl_auxv));
143 return dl_main_args.user_entry;
144}
145
146void
147_dl_sysdep_start_cleanup (void)
148{
149}
150
151void
152_dl_show_auxv (void)
153{
154 char buf[64];
155 ElfW(auxv_t) *av;
156
157 /* Terminate string. */
158 buf[63] = '\0';
159
160 /* The following code assumes that the AT_* values are encoded
161 starting from 0 with AT_NULL, 1 for AT_IGNORE, and all other values
162 close by (otherwise the array will be too large). In case we have
163 to support a platform where these requirements are not fulfilled
164 some alternative implementation has to be used. */
165 for (av = GLRO(dl_auxv); av->a_type != AT_NULL; ++av)
166 {
167 static const struct
168 {
169 const char label[22];
170 enum { unknown = 0, dec, hex, str, ignore } form : 8;
171 } auxvars[] =
172 {
173 [AT_EXECFD - 2] = { "EXECFD: ", dec },
174 [AT_EXECFN - 2] = { "EXECFN: ", str },
175 [AT_PHDR - 2] = { "PHDR: 0x", hex },
176 [AT_PHENT - 2] = { "PHENT: ", dec },
177 [AT_PHNUM - 2] = { "PHNUM: ", dec },
178 [AT_PAGESZ - 2] = { "PAGESZ: ", dec },
179 [AT_BASE - 2] = { "BASE: 0x", hex },
180 [AT_FLAGS - 2] = { "FLAGS: 0x", hex },
181 [AT_ENTRY - 2] = { "ENTRY: 0x", hex },
182 [AT_NOTELF - 2] = { "NOTELF: ", hex },
183 [AT_UID - 2] = { "UID: ", dec },
184 [AT_EUID - 2] = { "EUID: ", dec },
185 [AT_GID - 2] = { "GID: ", dec },
186 [AT_EGID - 2] = { "EGID: ", dec },
187 [AT_PLATFORM - 2] = { "PLATFORM: ", str },
188 [AT_HWCAP - 2] = { "HWCAP: ", hex },
189 [AT_CLKTCK - 2] = { "CLKTCK: ", dec },
190 [AT_FPUCW - 2] = { "FPUCW: ", hex },
191 [AT_DCACHEBSIZE - 2] = { "DCACHEBSIZE: 0x", hex },
192 [AT_ICACHEBSIZE - 2] = { "ICACHEBSIZE: 0x", hex },
193 [AT_UCACHEBSIZE - 2] = { "UCACHEBSIZE: 0x", hex },
194 [AT_IGNOREPPC - 2] = { "IGNOREPPC", ignore },
195 [AT_SECURE - 2] = { "SECURE: ", dec },
196 [AT_BASE_PLATFORM - 2] = { "BASE_PLATFORM: ", str },
197 [AT_SYSINFO - 2] = { "SYSINFO: 0x", hex },
198 [AT_SYSINFO_EHDR - 2] = { "SYSINFO_EHDR: 0x", hex },
199 [AT_RANDOM - 2] = { "RANDOM: 0x", hex },
200 [AT_HWCAP2 - 2] = { "HWCAP2: 0x", hex },
201 [AT_HWCAP3 - 2] = { "HWCAP3: 0x", hex },
202 [AT_HWCAP4 - 2] = { "HWCAP4: 0x", hex },
203 [AT_MINSIGSTKSZ - 2] = { "MINSIGSTKSZ: ", dec },
204 [AT_L1I_CACHESIZE - 2] = { "L1I_CACHESIZE: ", dec },
205 [AT_L1I_CACHEGEOMETRY - 2] = { "L1I_CACHEGEOMETRY: 0x", hex },
206 [AT_L1D_CACHESIZE - 2] = { "L1D_CACHESIZE: ", dec },
207 [AT_L1D_CACHEGEOMETRY - 2] = { "L1D_CACHEGEOMETRY: 0x", hex },
208 [AT_L2_CACHESIZE - 2] = { "L2_CACHESIZE: ", dec },
209 [AT_L2_CACHEGEOMETRY - 2] = { "L2_CACHEGEOMETRY: 0x", hex },
210 [AT_L3_CACHESIZE - 2] = { "L3_CACHESIZE: ", dec },
211 [AT_L3_CACHEGEOMETRY - 2] = { "L3_CACHEGEOMETRY: 0x", hex },
212 };
213 unsigned int idx = (unsigned int) (av->a_type - 2);
214
215 if ((unsigned int) av->a_type < 2u
216 || (idx < sizeof (auxvars) / sizeof (auxvars[0])
217 && auxvars[idx].form == ignore))
218 continue;
219
220 assert (AT_NULL == 0);
221 assert (AT_IGNORE == 1);
222
223 /* Some entries are handled in a special way per platform. */
224 if (_dl_procinfo (av->a_type, av->a_un.a_val) == 0)
225 continue;
226
227 if (idx < sizeof (auxvars) / sizeof (auxvars[0])
228 && auxvars[idx].form != unknown)
229 {
230 const char *val = (char *) av->a_un.a_val;
231
232 if (__builtin_expect (auxvars[idx].form, dec) == dec)
233 val = _itoa ((unsigned long int) av->a_un.a_val,
234 buf + sizeof buf - 1, 10, 0);
235 else if (__builtin_expect (auxvars[idx].form, hex) == hex)
236 val = _itoa ((unsigned long int) av->a_un.a_val,
237 buf + sizeof buf - 1, 16, 0);
238
239 _dl_printf ("AT_%s%s\n", auxvars[idx].label, val);
240
241 continue;
242 }
243
244 /* Unknown value: print a generic line. */
245 char buf2[17];
246 buf2[sizeof (buf2) - 1] = '\0';
247 const char *val2 = _itoa ((unsigned long int) av->a_un.a_val,
248 buf2 + sizeof buf2 - 1, 16, 0);
249 const char *val = _itoa ((unsigned long int) av->a_type,
250 buf + sizeof buf - 1, 16, 0);
251 _dl_printf ("AT_??? (0x%s): 0x%s\n", val, val2);
252 }
253}
254
255#endif /* SHARED */
256

source code of glibc/sysdeps/unix/sysv/linux/dl-sysdep.c