| 1 | /*	$OpenBSD: exec.h,v 1.57 2025/05/24 06:49:16 deraadt 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_IMMUTABLE	0x0010 /* create with UVM_ET_IMMUTABLE */ |
| 96 | #define VMCMD_TEXTREL	0x0020 /* terrible binary contains terrible textrel */ |
| 97 | }; |
| 98 | |
| 99 | #define	EXEC_DEFAULT_VMCMD_SETSIZE	12	/* # of cmds in set to start */ |
| 100 | |
| 101 | /* exec vmspace-creation command set; see below */ |
| 102 | struct exec_vmcmd_set { |
| 103 | 	u_int	evs_cnt; |
| 104 | 	u_int	evs_used; |
| 105 | 	struct	exec_vmcmd *evs_cmds; |
| 106 | 	struct	exec_vmcmd evs_start[EXEC_DEFAULT_VMCMD_SETSIZE]; |
| 107 | }; |
| 108 | |
| 109 | struct elf_args; |
| 110 | struct exec_package { |
| 111 | 	char	*ep_name;		/* file's name */ |
| 112 | 	void	*ep_hdr;		/* file's exec header */ |
| 113 | 	u_int	ep_hdrlen;		/* length of ep_hdr */ |
| 114 | 	u_int	ep_hdrvalid;		/* bytes of ep_hdr that are valid */ |
| 115 | 	struct nameidata *ep_ndp;	/* namei data pointer for lookups */ |
| 116 | 	struct	exec_vmcmd_set ep_vmcmds; /* vmcmds used to build vmspace */ |
| 117 | 	struct	vnode *ep_vp;		/* executable's vnode */ |
| 118 | 	struct	vattr *ep_vap;		/* executable's attributes */ |
| 119 | 	u_long	ep_taddr;		/* process's text address */ |
| 120 | 	u_long	ep_tsize;		/* size of process's text */ |
| 121 | 	u_long	ep_daddr;		/* process's data(+bss) address */ |
| 122 | 	u_long	ep_dsize;		/* size of process's data(+bss) */ |
| 123 | 	u_long	ep_maxsaddr;		/* proc's max stack addr ("top") */ |
| 124 | 	u_long	ep_minsaddr;		/* proc's min stack addr ("bottom") */ |
| 125 | 	u_long	ep_ssize;		/* size of process's stack */ |
| 126 | 	u_long	ep_entry;		/* process's entry point */ |
| 127 | 	u_int	ep_flags;		/* flags; see below. */ |
| 128 | 	char	**ep_fa;		/* a fake args vector for scripts */ |
| 129 | 	int	ep_fd;			/* a file descriptor we're holding */ |
| 130 | 	struct	elf_args *ep_args;	/* ELF info */ |
| 131 | 	void	*ep_auxinfo;		/* userspace auxinfo address */ |
| 132 | 	char	*ep_interp;		/* name of interpreter if any */ |
| 133 | 	vaddr_t	ep_pinstart, ep_pinend;	/* executable region */ |
| 134 | 	u_int	*ep_pins;		/* array of system call offsets */ |
| 135 | 	int	ep_npins;		/* entries in array */ |
| 136 | }; |
| 137 | #define	EXEC_INDIR	0x0001		/* script handling already done */ |
| 138 | #define	EXEC_HASFD	0x0002		/* holding a shell script */ |
| 139 | #define	EXEC_HASARGL	0x0004		/* has fake args vector */ |
| 140 | #define	EXEC_SKIPARG	0x0008		/* don't copy user-supplied argv[0] */ |
| 141 | #define	EXEC_DESTR	0x0010		/* destructive ops performed */ |
| 142 | #define	EXEC_WXNEEDED	0x0020		/* executable will violate W^X */ |
| 143 | #define	EXEC_NOBTCFI	0x0040		/* no branch target CFI */ |
| 144 | #define	EXEC_PROFILE	0x0080		/* profiled binary */ |
| 145 | |
| 146 | #ifdef _KERNEL |
| 147 | /* |
| 148 | * functions used either by execve() or the various cpu-dependent execve() |
| 149 | * hooks. |
| 150 | */ |
| 151 | void	vmcmdset_extend(struct exec_vmcmd_set *); |
| 152 | void	kill_vmcmds(struct exec_vmcmd_set *evsp); |
| 153 | int	vmcmd_map_pagedvn(struct proc *, struct exec_vmcmd *); |
| 154 | int	vmcmd_map_readvn(struct proc *, struct exec_vmcmd *); |
| 155 | int	vmcmd_map_zero(struct proc *, struct exec_vmcmd *); |
| 156 | int	vmcmd_mutable(struct proc *, struct exec_vmcmd *); |
| 157 | int	vmcmd_randomize(struct proc *, struct exec_vmcmd *); |
| 158 | int	copyargs(struct exec_package *, struct ps_strings *, void *, void *); |
| 159 | void	setregs(struct proc *, struct exec_package *, u_long, |
| 160 | 	 struct ps_strings *); |
| 161 | int	check_exec(struct proc *, struct exec_package *); |
| 162 | int	exec_setup_stack(struct proc *, struct exec_package *); |
| 163 | int	exec_process_vmcmds(struct proc *, struct exec_package *); |
| 164 | |
| 165 | void	new_vmcmd(struct exec_vmcmd_set *evsp, |
| 166 | 		 int (*proc)(struct proc *p, struct exec_vmcmd *), |
| 167 | 		 u_long len, u_long addr, struct vnode *vp, u_long offset, |
| 168 | 		 u_int prot, int flags); |
| 169 | #define	NEW_VMCMD(evsp,proc,len,addr,vp,offset,prot) \ |
| 170 | 	new_vmcmd(evsp,proc,len,addr,vp,offset,prot, 0); |
| 171 | #define NEW_VMCMD2(evsp,proc,len,addr,vp,offset,prot,flags) \ |
| 172 | 	new_vmcmd(evsp,proc,len,addr,vp,offset,prot,flags) |
| 173 | |
| 174 | /* Initialize an empty vmcmd set */ |
| 175 | #define VMCMDSET_INIT(vmc) do { \ |
| 176 | 	(vmc)->evs_cnt = EXEC_DEFAULT_VMCMD_SETSIZE; \ |
| 177 | 	(vmc)->evs_cmds = (vmc)->evs_start; \ |
| 178 | 	(vmc)->evs_used = 0; \ |
| 179 | } while (0)	 |
| 180 | |
| 181 | /* |
| 182 | * Exec function switch: |
| 183 | * |
| 184 | * Note that each makecmds function is responsible for loading the |
| 185 | * exec package with the necessary functions for any exec-type-specific |
| 186 | * handling. |
| 187 | * |
| 188 | * Functions for specific exec types should be defined in their own |
| 189 | * header file. |
| 190 | */ |
| 191 | extern const struct	execsw execsw[]; |
| 192 | extern int	nexecs; |
| 193 | extern int	exec_maxhdrsz; |
| 194 | |
| 195 | /* |
| 196 | * If non-zero, stackgap_random specifies the upper limit of the random gap size |
| 197 | * added to the fixed stack position. Must be n^2. |
| 198 | */ |
| 199 | extern int	stackgap_random; |
| 200 | |
| 201 | /* Limit on total PT_OPENBSD_RANDOMIZE bytes. */ |
| 202 | #define ELF_RANDOMIZE_LIMIT 1024*1024 |
| 203 | |
| 204 | #endif /* _KERNEL */ |
| 205 | |
| 206 | /* |
| 207 | * a_mid - keep sorted in numerical order for sanity's sake |
| 208 | * ensure that: 0 < mid < 0x3ff |
| 209 | */ |
| 210 | #define	MID_ZERO	0	/* unknown - implementation dependent */ |
| 211 | #define	MID_SUN010	1	/* sun 68010/68020 binary */ |
| 212 | #define	MID_SUN020	2	/* sun 68020-only binary */ |
| 213 | #define	MID_PC386	100	/* 386 PC binary. (so quoth BFD) */ |
| 214 | #define	MID_ROMPAOS	104	/* old IBM RT */ |
| 215 | #define	MID_I386	134	/* i386 BSD binary */ |
| 216 | #define	MID_M68K	135	/* m68k BSD binary with 8K page sizes */ |
| 217 | #define	MID_M68K4K	136	/* DO NOT USE: m68k BSD binary with 4K page sizes */ |
| 218 | #define	MID_NS32532	137	/* ns32532 */ |
| 219 | #define	MID_SPARC	138	/* sparc */ |
| 220 | #define	MID_PMAX	139	/* pmax */ |
| 221 | #define	MID_VAX1K	140	/* vax 1k page size */ |
| 222 | #define	MID_ALPHA	141	/* Alpha BSD binary */ |
| 223 | #define	MID_MIPS	142	/* big-endian MIPS */ |
| 224 | #define	MID_ARM6	143	/* ARM6 */ |
| 225 | #define	MID_SH3		145	/* SH3 */ |
| 226 | #define	MID_POWERPC	149	/* big-endian PowerPC */ |
| 227 | #define	MID_VAX		150	/* vax */ |
| 228 | #define	MID_SPARC64	151	/* LP64 sparc */ |
| 229 | #define MID_MIPS2	152	/* MIPS2 */ |
| 230 | #define	MID_M88K	153	/* m88k BSD binary */ |
| 231 | #define	MID_HPPA	154	/* hppa */ |
| 232 | #define	MID_AMD64	157	/* AMD64 */ |
| 233 | #define	MID_MIPS64	158	/* big-endian MIPS64 */ |
| 234 | #define	MID_ARM64	159	/* ARM64 */ |
| 235 | #define	MID_POWERPC64	160	/* big-endian 64-bit PowerPC */ |
| 236 | #define	MID_RISCV64	161	/* Little-endian 64-bit RISC-V */ |
| 237 | #define	MID_HP200	200	/* hp200 (68010) BSD binary */ |
| 238 | #define	MID_HP300	300	/* hp300 (68020+68881) BSD binary */ |
| 239 | #define	MID_HPUX	0x20C	/* hp200/300 HP-UX binary */ |
| 240 | #define	MID_HPUX800	0x20B	/* hp800 HP-UX binary pa1.0 */ |
| 241 | #define	MID_HPPA11	0x210	/* hp700 HP-UX binary pa1.1 */ |
| 242 | #define	MID_HPPA20	0x214	/* hp700 HP-UX binary pa2.0 */ |
| 243 | |
| 244 | #include <machine/exec.h> |
| 245 | |
| 246 | #endif /* !_SYS_EXEC_H_ */ |