FreeBSD/Linux Kernel Cross Reference
sys/sys/exec.h
1 /* $OpenBSD: exec.h,v 1.51 2022/10/30 17:43:40 guenther Exp $ */
2 /* $NetBSD: exec.h,v 1.59 1996/02/09 18:25:09 christos Exp $ */
3
4 /*-
5 * Copyright (c) 1994 Christopher G. Demetriou
6 * Copyright (c) 1993 Theo de Raadt
7 * Copyright (c) 1992, 1993
8 * The Regents of the University of California. All rights reserved.
9 * (c) UNIX System Laboratories, Inc.
10 * All or some portions of this file are derived from material licensed
11 * to the University of California by American Telephone and Telegraph
12 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
13 * the permission of UNIX System Laboratories, Inc.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)exec.h 8.3 (Berkeley) 1/21/94
40 */
41
42 #ifndef _SYS_EXEC_H_
43 #define _SYS_EXEC_H_
44
45 /*
46 * The following structure is found at the top of the user stack of each
47 * user process. The ps program uses it to locate argv and environment
48 * strings. Programs that wish ps to display other information may modify
49 * it; normally ps_argvstr points to argv[0], and ps_nargvstr is the same
50 * as the program's argc. The fields ps_envstr and ps_nenvstr are the
51 * equivalent for the environment.
52 */
53 struct ps_strings {
54 char **ps_argvstr; /* first of 0 or more argument strings */
55 int ps_nargvstr; /* the number of argument strings */
56 char **ps_envstr; /* first of 0 or more environment strings */
57 int ps_nenvstr; /* the number of environment strings */
58 };
59
60 /*
61 * the following structures allow execve() to put together processes
62 * in a more extensible and cleaner way.
63 *
64 * the exec_package struct defines an executable being execve()'d.
65 * it contains the header, the vmspace-building commands, the vnode
66 * information, and the arguments associated with the newly-execve'd
67 * process.
68 *
69 * the exec_vmcmd struct defines a command description to be used
70 * in creating the new process's vmspace.
71 */
72
73 struct proc;
74 struct exec_package;
75
76 typedef int (*exec_makecmds_fcn)(struct proc *, struct exec_package *);
77
78 struct execsw {
79 u_int es_hdrsz; /* size of header for this format */
80 exec_makecmds_fcn es_check; /* function to check exec format */
81 };
82
83 struct exec_vmcmd {
84 int (*ev_proc)(struct proc *p, struct exec_vmcmd *cmd);
85 /* procedure to run for region of vmspace */
86 u_long ev_len; /* length of the segment to map */
87 u_long ev_addr; /* address in the vmspace to place it at */
88 struct vnode *ev_vp; /* vnode pointer for the file w/the data */
89 u_long ev_offset; /* offset in the file for the data */
90 u_int ev_prot; /* protections for segment */
91 int ev_flags;
92 #define VMCMD_RELATIVE 0x0001 /* ev_addr is relative to base entry */
93 #define VMCMD_BASE 0x0002 /* marks a base entry */
94 #define VMCMD_STACK 0x0004 /* create with UVM_FLAG_STACK */
95 #define VMCMD_SYSCALL 0x0008 /* create with UVM_FLAG_SYSCALL */
96 #define VMCMD_IMMUTABLE 0x0010 /* create with UVM_ET_IMMUTABLE */
97 #define VMCMD_TEXTREL 0x0020 /* terrible binary contains terrible textrel */
98 };
99
100 #define EXEC_DEFAULT_VMCMD_SETSIZE 12 /* # of cmds in set to start */
101
102 /* exec vmspace-creation command set; see below */
103 struct exec_vmcmd_set {
104 u_int evs_cnt;
105 u_int evs_used;
106 struct exec_vmcmd *evs_cmds;
107 struct exec_vmcmd evs_start[EXEC_DEFAULT_VMCMD_SETSIZE];
108 };
109
110 struct elf_args;
111 struct exec_package {
112 char *ep_name; /* file's name */
113 void *ep_hdr; /* file's exec header */
114 u_int ep_hdrlen; /* length of ep_hdr */
115 u_int ep_hdrvalid; /* bytes of ep_hdr that are valid */
116 struct nameidata *ep_ndp; /* namei data pointer for lookups */
117 struct exec_vmcmd_set ep_vmcmds; /* vmcmds used to build vmspace */
118 struct vnode *ep_vp; /* executable's vnode */
119 struct vattr *ep_vap; /* executable's attributes */
120 u_long ep_taddr; /* process's text address */
121 u_long ep_tsize; /* size of process's text */
122 u_long ep_daddr; /* process's data(+bss) address */
123 u_long ep_dsize; /* size of process's data(+bss) */
124 u_long ep_maxsaddr; /* proc's max stack addr ("top") */
125 u_long ep_minsaddr; /* proc's min stack addr ("bottom") */
126 u_long ep_ssize; /* size of process's stack */
127 u_long ep_entry; /* process's entry point */
128 u_int ep_flags; /* flags; see below. */
129 char **ep_fa; /* a fake args vector for scripts */
130 int ep_fd; /* a file descriptor we're holding */
131 struct elf_args *ep_args; /* ELF info */
132 void *ep_auxinfo; /* userspace auxinfo address */
133 char *ep_interp; /* name of interpreter if any */
134 };
135 #define EXEC_INDIR 0x0001 /* script handling already done */
136 #define EXEC_HASFD 0x0002 /* holding a shell script */
137 #define EXEC_HASARGL 0x0004 /* has fake args vector */
138 #define EXEC_SKIPARG 0x0008 /* don't copy user-supplied argv[0] */
139 #define EXEC_DESTR 0x0010 /* destructive ops performed */
140 #define EXEC_WXNEEDED 0x0020 /* executable will violate W^X */
141
142 #ifdef _KERNEL
143 /*
144 * functions used either by execve() or the various cpu-dependent execve()
145 * hooks.
146 */
147 void vmcmdset_extend(struct exec_vmcmd_set *);
148 void kill_vmcmds(struct exec_vmcmd_set *evsp);
149 int vmcmd_map_pagedvn(struct proc *, struct exec_vmcmd *);
150 int vmcmd_map_readvn(struct proc *, struct exec_vmcmd *);
151 int vmcmd_map_zero(struct proc *, struct exec_vmcmd *);
152 int vmcmd_mutable(struct proc *, struct exec_vmcmd *);
153 int vmcmd_randomize(struct proc *, struct exec_vmcmd *);
154 int copyargs(struct exec_package *, struct ps_strings *, void *, void *);
155 void setregs(struct proc *, struct exec_package *, u_long,
156 struct ps_strings *);
157 int check_exec(struct proc *, struct exec_package *);
158 int exec_setup_stack(struct proc *, struct exec_package *);
159 int exec_process_vmcmds(struct proc *, struct exec_package *);
160
161 #ifdef DEBUG
162 void new_vmcmd(struct exec_vmcmd_set *evsp,
163 int (*proc)(struct proc *p, struct exec_vmcmd *),
164 u_long len, u_long addr, struct vnode *vp, u_long offset,
165 u_int prot, int flags);
166 #define NEW_VMCMD(evsp,proc,len,addr,vp,offset,prot) \
167 new_vmcmd(evsp,proc,len,addr,vp,offset,prot, 0);
168 #define NEW_VMCMD2(evsp,proc,len,addr,vp,offset,prot,flags) \
169 new_vmcmd(evsp,proc,len,addr,vp,offset,prot,flags)
170 #else /* DEBUG */
171 #define NEW_VMCMD(evsp,proc,len,addr,vp,offset,prot) \
172 NEW_VMCMD2(evsp,proc,len,addr,vp,offset,prot,0)
173 #define NEW_VMCMD2(evsp,proc,len,addr,vp,offset,prot,flags) do { \
174 struct exec_vmcmd *__vcp; \
175 if ((evsp)->evs_used >= (evsp)->evs_cnt) \
176 vmcmdset_extend(evsp); \
177 __vcp = &(evsp)->evs_cmds[(evsp)->evs_used++]; \
178 __vcp->ev_proc = (proc); \
179 __vcp->ev_len = (len); \
180 __vcp->ev_addr = (addr); \
181 if ((__vcp->ev_vp = (vp)) != NULLVP) \
182 vref(vp); \
183 __vcp->ev_offset = (offset); \
184 __vcp->ev_prot = (prot); \
185 __vcp->ev_flags = (flags); \
186 } while (0)
187
188 #endif /* DEBUG */
189
190 /* Initialize an empty vmcmd set */
191 #define VMCMDSET_INIT(vmc) do { \
192 (vmc)->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE; \
193 (vmc)->evs_cmds = (vmc)->evs_start; \
194 (vmc)->evs_used = 0; \
195 } while (0)
196
197 /*
198 * Exec function switch:
199 *
200 * Note that each makecmds function is responsible for loading the
201 * exec package with the necessary functions for any exec-type-specific
202 * handling.
203 *
204 * Functions for specific exec types should be defined in their own
205 * header file.
206 */
207 extern const struct execsw execsw[];
208 extern int nexecs;
209 extern int exec_maxhdrsz;
210
211 /*
212 * If non-zero, stackgap_random specifies the upper limit of the random gap size
213 * added to the fixed stack position. Must be n^2.
214 */
215 extern int stackgap_random;
216
217 /* Limit on total PT_OPENBSD_RANDOMIZE bytes. */
218 #define ELF_RANDOMIZE_LIMIT 1024*1024
219
220 #endif /* _KERNEL */
221
222 #ifndef N_PAGSIZ
223 #define N_PAGSIZ(ex) (__LDPGSZ)
224 #endif
225
226 /*
227 * Legacy a.out structures and defines; start deleting these when
228 * external use no longer exist.
229 */
230
231
232 /*
233 * Header prepended to each a.out file.
234 * only manipulate the a_midmag field via the
235 * N_SETMAGIC/N_GET{MAGIC,MID,FLAG} macros below.
236 */
237 struct exec {
238 u_int32_t a_midmag; /* htonl(flags<<26|mid<<16|magic) */
239 u_int32_t a_text; /* text segment size */
240 u_int32_t a_data; /* initialized data size */
241 u_int32_t a_bss; /* uninitialized data size */
242 u_int32_t a_syms; /* symbol table size */
243 u_int32_t a_entry; /* entry point */
244 u_int32_t a_trsize; /* text relocation size */
245 u_int32_t a_drsize; /* data relocation size */
246 };
247
248 /* a_magic */
249 #define OMAGIC 0407 /* old impure format */
250 #define NMAGIC 0410 /* read-only text */
251 #define ZMAGIC 0413 /* demand load format */
252 #define QMAGIC 0314 /* "compact" demand load format; deprecated */
253
254 /*
255 * a_mid - keep sorted in numerical order for sanity's sake
256 * ensure that: 0 < mid < 0x3ff
257 */
258 #define MID_ZERO 0 /* unknown - implementation dependent */
259 #define MID_SUN010 1 /* sun 68010/68020 binary */
260 #define MID_SUN020 2 /* sun 68020-only binary */
261 #define MID_PC386 100 /* 386 PC binary. (so quoth BFD) */
262 #define MID_ROMPAOS 104 /* old IBM RT */
263 #define MID_I386 134 /* i386 BSD binary */
264 #define MID_M68K 135 /* m68k BSD binary with 8K page sizes */
265 #define MID_M68K4K 136 /* DO NOT USE: m68k BSD binary with 4K page sizes */
266 #define MID_NS32532 137 /* ns32532 */
267 #define MID_SPARC 138 /* sparc */
268 #define MID_PMAX 139 /* pmax */
269 #define MID_VAX1K 140 /* vax 1k page size */
270 #define MID_ALPHA 141 /* Alpha BSD binary */
271 #define MID_MIPS 142 /* big-endian MIPS */
272 #define MID_ARM6 143 /* ARM6 */
273 #define MID_SH3 145 /* SH3 */
274 #define MID_POWERPC 149 /* big-endian PowerPC */
275 #define MID_VAX 150 /* vax */
276 #define MID_SPARC64 151 /* LP64 sparc */
277 #define MID_MIPS2 152 /* MIPS2 */
278 #define MID_M88K 153 /* m88k BSD binary */
279 #define MID_HPPA 154 /* hppa */
280 #define MID_AMD64 157 /* AMD64 */
281 #define MID_MIPS64 158 /* big-endian MIPS64 */
282 #define MID_ARM64 159 /* ARM64 */
283 #define MID_POWERPC64 160 /* big-endian 64-bit PowerPC */
284 #define MID_RISCV64 161 /* Little-endian 64-bit RISC-V */
285 #define MID_HP200 200 /* hp200 (68010) BSD binary */
286 #define MID_HP300 300 /* hp300 (68020+68881) BSD binary */
287 #define MID_HPUX 0x20C /* hp200/300 HP-UX binary */
288 #define MID_HPUX800 0x20B /* hp800 HP-UX binary pa1.0 */
289 #define MID_HPPA11 0x210 /* hp700 HP-UX binary pa1.1 */
290 #define MID_HPPA20 0x214 /* hp700 HP-UX binary pa2.0 */
291
292 /*
293 * a_flags
294 */
295 #define EX_DYNAMIC 0x20
296 #define EX_PIC 0x10
297 #define EX_DPMASK 0x30
298 /*
299 * Interpretation of the (a_flags & EX_DPMASK) bits:
300 *
301 * 00 traditional executable or object file
302 * 01 object file contains PIC code (set by `as -k')
303 * 10 dynamic executable
304 * 11 position independent executable image
305 * (eg. a shared library)
306 *
307 */
308
309 /*
310 * The a.out structure's a_midmag field is a network-byteorder encoding
311 * of this int
312 * FFFFFFmmmmmmmmmmMMMMMMMMMMMMMMMM
313 * Where `F' is 6 bits of flag like EX_DYNAMIC,
314 * `m' is 10 bits of machine-id like MID_I386, and
315 * `M' is 16 bits worth of magic number, ie. ZMAGIC.
316 * The macros below will set/get the needed fields.
317 */
318 #define N_GETMAGIC(ex) \
319 ( (((ex).a_midmag)&0xffff0000) ? (ntohl(((ex).a_midmag))&0xffff) : ((ex).a_midmag))
320 #define N_GETMAGIC2(ex) \
321 ( (((ex).a_midmag)&0xffff0000) ? (ntohl(((ex).a_midmag))&0xffff) : \
322 (((ex).a_midmag) | 0x10000) )
323 #define N_GETMID(ex) \
324 ( (((ex).a_midmag)&0xffff0000) ? ((ntohl(((ex).a_midmag))>>16)&0x03ff) : MID_ZERO )
325 #define N_GETFLAG(ex) \
326 ( (((ex).a_midmag)&0xffff0000) ? ((ntohl(((ex).a_midmag))>>26)&0x3f) : 0 )
327 #define N_SETMAGIC(ex,mag,mid,flag) \
328 ( (ex).a_midmag = htonl( (((flag)&0x3f)<<26) | (((mid)&0x03ff)<<16) | \
329 (((mag)&0xffff)) ) )
330
331 #define N_ALIGN(ex,x) \
332 (N_GETMAGIC(ex) == ZMAGIC || N_GETMAGIC(ex) == QMAGIC ? \
333 ((x) + __LDPGSZ - 1) & ~(__LDPGSZ - 1) : (x))
334
335 /* Valid magic number check. */
336 #define N_BADMAG(ex) \
337 (N_GETMAGIC(ex) != NMAGIC && N_GETMAGIC(ex) != OMAGIC && \
338 N_GETMAGIC(ex) != ZMAGIC && N_GETMAGIC(ex) != QMAGIC)
339
340 /* Address of the bottom of the text segment. */
341 #define N_TXTADDR(ex) (N_GETMAGIC2(ex) == (ZMAGIC|0x10000) ? 0 : __LDPGSZ)
342
343 /* Address of the bottom of the data segment. */
344 #define N_DATADDR(ex) \
345 (N_GETMAGIC(ex) == OMAGIC ? N_TXTADDR(ex) + (ex).a_text : \
346 (N_TXTADDR(ex) + (ex).a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1))
347
348 /* Address of the bottom of the bss segment. */
349 #define N_BSSADDR(ex) \
350 (N_DATADDR(ex) + (ex).a_data)
351
352 /* Text segment offset. */
353 #define N_TXTOFF(ex) \
354 ( N_GETMAGIC2(ex)==ZMAGIC || N_GETMAGIC2(ex)==(QMAGIC|0x10000) ? \
355 0 : (N_GETMAGIC2(ex)==(ZMAGIC|0x10000) ? __LDPGSZ : \
356 sizeof(struct exec)) )
357
358 /* Data segment offset. */
359 #define N_DATOFF(ex) \
360 N_ALIGN(ex, N_TXTOFF(ex) + (ex).a_text)
361
362 /* Text relocation table offset. */
363 #define N_TRELOFF(ex) \
364 (N_DATOFF(ex) + (ex).a_data)
365
366 /* Data relocation table offset. */
367 #define N_DRELOFF(ex) \
368 (N_TRELOFF(ex) + (ex).a_trsize)
369
370 /* Symbol table offset. */
371 #define N_SYMOFF(ex) \
372 (N_DRELOFF(ex) + (ex).a_drsize)
373
374 /* String table offset. */
375 #define N_STROFF(ex) \
376 (N_SYMOFF(ex) + (ex).a_syms)
377
378 #include <machine/exec.h>
379
380 #endif /* !_SYS_EXEC_H_ */
Cache object: 0b4c6dab80db387d46cf287d13375687
|