linux32_exec_elf32.c revision 1.22 1 1.22 ryo /* $NetBSD: linux32_exec_elf32.c,v 1.22 2021/11/25 03:08:04 ryo Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 1995, 1998, 2000, 2001,2006 The NetBSD Foundation, Inc.
5 1.1 manu * All rights reserved.
6 1.1 manu *
7 1.1 manu * This code is derived from software contributed to The NetBSD Foundation
8 1.1 manu * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and
9 1.1 manu * Emmanuel Dreyfus.
10 1.1 manu *
11 1.1 manu * Redistribution and use in source and binary forms, with or without
12 1.1 manu * modification, are permitted provided that the following conditions
13 1.1 manu * are met:
14 1.1 manu * 1. Redistributions of source code must retain the above copyright
15 1.1 manu * notice, this list of conditions and the following disclaimer.
16 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 manu * notice, this list of conditions and the following disclaimer in the
18 1.1 manu * documentation and/or other materials provided with the distribution.
19 1.1 manu *
20 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 manu * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 manu * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
31 1.1 manu */
32 1.1 manu
33 1.1 manu #include <sys/cdefs.h>
34 1.22 ryo __KERNEL_RCSID(0, "$NetBSD: linux32_exec_elf32.c,v 1.22 2021/11/25 03:08:04 ryo Exp $");
35 1.1 manu
36 1.1 manu #define ELFSIZE 32
37 1.1 manu
38 1.1 manu #include <sys/param.h>
39 1.1 manu #include <sys/systm.h>
40 1.1 manu #include <sys/proc.h>
41 1.1 manu #include <sys/vnode.h>
42 1.1 manu #include <sys/exec.h>
43 1.1 manu #include <sys/exec_elf.h>
44 1.3 skd #include <sys/kauth.h>
45 1.1 manu #include <sys/kernel.h>
46 1.1 manu #include <sys/resourcevar.h>
47 1.1 manu #include <sys/signal.h>
48 1.1 manu #include <sys/signalvar.h>
49 1.14 chs #include <sys/cprng.h>
50 1.1 manu
51 1.1 manu #include <compat/linux/common/linux_exec.h>
52 1.1 manu #include <compat/netbsd32/netbsd32.h>
53 1.1 manu #include <compat/netbsd32/netbsd32_exec.h>
54 1.1 manu #include <compat/linux32/common/linux32_exec.h>
55 1.1 manu
56 1.22 ryo #ifndef __aarch64__
57 1.10 jym #include <machine/cpuvar.h>
58 1.22 ryo #endif
59 1.1 manu #include <machine/frame.h>
60 1.1 manu
61 1.1 manu #ifdef DEBUG_LINUX
62 1.1 manu #define DPRINTF(a) uprintf a
63 1.1 manu #else
64 1.1 manu #define DPRINTF(a)
65 1.1 manu #endif
66 1.1 manu
67 1.1 manu int linux32_copyinargs(struct exec_package *, struct ps_strings *,
68 1.1 manu void *, size_t, const void *, const void *);
69 1.1 manu
70 1.1 manu int
71 1.9 cegger ELFNAME2(linux32,probe)(struct lwp *l, struct exec_package *epp,
72 1.9 cegger void *eh, char *itp, vaddr_t *pos)
73 1.1 manu {
74 1.1 manu int error;
75 1.1 manu
76 1.1 manu if (((error = ELFNAME2(linux,signature)(l, epp, eh, itp)) != 0) &&
77 1.5 manu #ifdef LINUX32_GCC_SIGNATURE
78 1.1 manu ((error = ELFNAME2(linux,gcc_signature)(l, epp, eh)) != 0) &&
79 1.1 manu #endif
80 1.5 manu #ifdef LINUX32_ATEXIT_SIGNATURE
81 1.1 manu ((error = ELFNAME2(linux,atexit_signature)(l, epp, eh)) != 0) &&
82 1.1 manu #endif
83 1.5 manu #ifdef LINUX32_DEBUGLINK_SIGNATURE
84 1.5 manu ((error = ELFNAME2(linux,debuglink_signature)(l, epp, eh)) != 0) &&
85 1.5 manu #endif
86 1.1 manu 1)
87 1.1 manu return error;
88 1.1 manu
89 1.1 manu if (itp) {
90 1.7 dsl if ((error = emul_find_interp(l, epp, itp)))
91 1.1 manu return (error);
92 1.1 manu }
93 1.1 manu #if 0
94 1.1 manu DPRINTF(("linux32_probe: returning 0\n"));
95 1.1 manu #endif
96 1.1 manu
97 1.12 chs epp->ep_flags |= EXEC_32 | EXEC_FORCEAUX;
98 1.19 maxv epp->ep_vm_minaddr = exec_vm_minaddr(VM_MIN_ADDRESS);
99 1.1 manu epp->ep_vm_maxaddr = USRSTACK32;
100 1.1 manu
101 1.1 manu return 0;
102 1.1 manu }
103 1.1 manu
104 1.1 manu /*
105 1.1 manu * Copy arguments onto the stack in the normal way, but add some
106 1.1 manu * extra information in case of dynamic binding.
107 1.1 manu */
108 1.1 manu int
109 1.1 manu linux32_elf32_copyargs(struct lwp *l, struct exec_package *pack,
110 1.1 manu struct ps_strings *arginfo, char **stackp, void *argp)
111 1.1 manu {
112 1.21 ryo struct linux32_extra_stack_data esd, *esdp;
113 1.21 ryo Aux32Info *a, *ai __diagused;
114 1.1 manu struct elf_args *ap;
115 1.1 manu struct vattr *vap;
116 1.1 manu int error;
117 1.1 manu
118 1.1 manu if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0)
119 1.1 manu return error;
120 1.1 manu
121 1.21 ryo esdp = (struct linux32_extra_stack_data *)(*stackp); /* userspace */
122 1.11 chs
123 1.21 ryo memset(&esd, 0, sizeof(esd));
124 1.21 ryo ai = a = esd.ai;
125 1.18 maxv
126 1.1 manu /*
127 1.1 manu * Push extra arguments on the stack needed by dynamically
128 1.1 manu * linked binaries and static binaries as well.
129 1.1 manu */
130 1.1 manu
131 1.11 chs a->a_type = AT_PAGESZ;
132 1.11 chs a->a_v = PAGE_SIZE;
133 1.11 chs a++;
134 1.1 manu
135 1.11 chs if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
136 1.1 manu
137 1.11 chs a->a_type = AT_PHDR;
138 1.11 chs a->a_v = ap->arg_phaddr;
139 1.11 chs a++;
140 1.1 manu
141 1.11 chs a->a_type = AT_PHENT;
142 1.11 chs a->a_v = ap->arg_phentsize;
143 1.11 chs a++;
144 1.1 manu
145 1.11 chs a->a_type = AT_PHNUM;
146 1.11 chs a->a_v = ap->arg_phnum;
147 1.11 chs a++;
148 1.1 manu
149 1.11 chs a->a_type = AT_BASE;
150 1.11 chs a->a_v = ap->arg_interp;
151 1.11 chs a++;
152 1.1 manu
153 1.11 chs a->a_type = AT_FLAGS;
154 1.11 chs a->a_v = 0;
155 1.11 chs a++;
156 1.1 manu
157 1.11 chs a->a_type = AT_ENTRY;
158 1.11 chs a->a_v = ap->arg_entry;
159 1.11 chs a++;
160 1.1 manu
161 1.13 matt exec_free_emul_arg(pack);
162 1.11 chs }
163 1.1 manu
164 1.11 chs /* Linux-specific items */
165 1.11 chs a->a_type = LINUX_AT_CLKTCK;
166 1.11 chs a->a_v = hz;
167 1.11 chs a++;
168 1.1 manu
169 1.11 chs vap = pack->ep_vap;
170 1.1 manu
171 1.11 chs a->a_type = LINUX_AT_UID;
172 1.11 chs a->a_v = kauth_cred_getuid(l->l_cred);
173 1.11 chs a++;
174 1.1 manu
175 1.11 chs a->a_type = LINUX_AT_EUID;
176 1.11 chs a->a_v = ((vap->va_mode & S_ISUID) ?
177 1.11 chs vap->va_uid : kauth_cred_geteuid(l->l_cred));
178 1.11 chs a++;
179 1.1 manu
180 1.11 chs a->a_type = LINUX_AT_GID;
181 1.11 chs a->a_v = kauth_cred_getgid(l->l_cred);
182 1.11 chs a++;
183 1.1 manu
184 1.11 chs a->a_type = LINUX_AT_EGID;
185 1.11 chs a->a_v = ((vap->va_mode & S_ISGID) ?
186 1.4 ad vap->va_gid : kauth_cred_getegid(l->l_cred));
187 1.11 chs a++;
188 1.1 manu
189 1.11 chs a->a_type = LINUX_AT_SECURE;
190 1.11 chs a->a_v = 0;
191 1.11 chs a++;
192 1.1 manu
193 1.14 chs a->a_type = LINUX_AT_RANDOM;
194 1.21 ryo a->a_v = NETBSD32PTR32I(&esdp->randbytes[0]);
195 1.14 chs a++;
196 1.21 ryo esd.randbytes[0] = cprng_strong32();
197 1.21 ryo esd.randbytes[1] = cprng_strong32();
198 1.21 ryo esd.randbytes[2] = cprng_strong32();
199 1.21 ryo esd.randbytes[3] = cprng_strong32();
200 1.14 chs
201 1.21 ryo #if 0 /* defined(__amd64__) */
202 1.21 ryo /* XXX: broken. vsyscall must be placed in the executable page */
203 1.11 chs a->a_type = LINUX_AT_SYSINFO;
204 1.11 chs a->a_v = NETBSD32PTR32I(&esdp->kernel_vsyscall[0]);
205 1.11 chs a++;
206 1.21 ryo memcpy(esd.kernel_vsyscall, linux32_kernel_vsyscall,
207 1.21 ryo sizeof(linux32_kernel_vsyscall));
208 1.21 ryo #endif
209 1.11 chs
210 1.21 ryo #if 0 /* notyet */
211 1.11 chs a->a_type = LINUX_AT_SYSINFO_EHDR;
212 1.11 chs a->a_v = NETBSD32PTR32I(&esdp->elfhdr);
213 1.11 chs a++;
214 1.21 ryo memcpy(&esd.elfhdr, eh, sizeof(*eh));
215 1.21 ryo #endif
216 1.11 chs
217 1.21 ryo #ifdef LINUX32_CPUCAP
218 1.11 chs a->a_type = LINUX_AT_HWCAP;
219 1.11 chs a->a_v = LINUX32_CPUCAP;
220 1.11 chs a++;
221 1.21 ryo #endif
222 1.11 chs
223 1.21 ryo #ifdef LINUX32_PLATFORM
224 1.11 chs a->a_type = LINUX_AT_PLATFORM;
225 1.11 chs a->a_v = NETBSD32PTR32I(&esdp->hw_platform[0]);
226 1.11 chs a++;
227 1.21 ryo strncpy(esd.hw_platform, LINUX32_PLATFORM, sizeof(esd.hw_platform));
228 1.11 chs #endif
229 1.11 chs
230 1.11 chs a->a_type = AT_NULL;
231 1.11 chs a->a_v = 0;
232 1.11 chs a++;
233 1.1 manu
234 1.21 ryo KASSERTMSG(a <= &ai[LINUX32_ELF_AUX_ENTRIES],
235 1.21 ryo "increase LINUX32_ELF_AUX_ENTRIES to %lu", a - ai);
236 1.1 manu
237 1.1 manu /*
238 1.1 manu * Copy out the ELF auxiliary table and hw platform name
239 1.1 manu */
240 1.1 manu if ((error = copyout(&esd, esdp, sizeof(esd))) != 0)
241 1.1 manu return error;
242 1.1 manu *stackp += sizeof(esd);
243 1.11 chs
244 1.1 manu return 0;
245 1.1 manu }
246