exec.h 14 KB

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