custom message type for VM_INFO
[minix3.git] / sys / lib / libsa / exec.c
blob325a68fc72823ad2cce3a78c81c34e6990a4e63e
1 /* $NetBSD: exec.c,v 1.28 2009/12/29 20:21:46 elad Exp $ */
3 /*-
4 * Copyright (c) 1982, 1986, 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/param.h>
33 #include <sys/reboot.h>
34 #ifndef SA_EXEC_ANYOWNER
35 #include <sys/stat.h>
36 #endif
37 #include <sys/exec_aout.h>
38 #ifdef _STANDALONE
39 #include <lib/libkern/libkern.h>
40 #else
41 #include <string.h>
42 #endif
44 #include "stand.h"
46 void
47 exec(char *path, char *loadaddr, int howto)
49 #ifndef SA_EXEC_ANYOWNER
50 struct stat sb;
51 #endif
52 struct exec x;
53 int io, i;
54 char *addr, *ssym, *esym;
56 io = open(path, 0);
57 if (io < 0)
58 return;
60 #ifndef SA_EXEC_ANYOWNER
61 (void) fstat(io, &sb);
62 if (sb.st_uid || (sb.st_mode & 2)) {
63 printf("non-secure file, will not load\n");
64 close(io);
65 errno = EPERM;
66 return;
68 #endif
70 i = read(io, (char *)&x, sizeof(x));
71 if (i != sizeof(x) || N_BADMAG(x)) {
72 errno = EFTYPE;
73 return;
76 /* Text */
77 printf("%ld", x.a_text);
78 addr = loadaddr;
79 if (N_GETMAGIC(x) == ZMAGIC) {
80 (void)memcpy(addr, &x, sizeof(x));
81 addr += sizeof(x);
82 x.a_text -= sizeof(x);
84 if (read(io, (char *)addr, x.a_text) != (ssize_t)x.a_text)
85 goto shread;
86 addr += x.a_text;
87 if (N_GETMAGIC(x) == ZMAGIC || N_GETMAGIC(x) == NMAGIC)
88 while ((long)addr & (N_PAGSIZ(x) - 1))
89 *addr++ = 0;
91 /* Data */
92 printf("+%ld", x.a_data);
93 if (read(io, addr, x.a_data) != (ssize_t)x.a_data)
94 goto shread;
95 addr += x.a_data;
97 /* Bss */
98 printf("+%ld", x.a_bss);
99 for (i = 0; i < (int)x.a_bss; i++)
100 *addr++ = 0;
102 /* Symbols */
103 ssym = addr;
104 (void)memcpy(addr, &x.a_syms, sizeof(x.a_syms));
105 addr += sizeof(x.a_syms);
106 if (x.a_syms) {
107 printf("+[%ld", x.a_syms);
108 if (read(io, addr, x.a_syms) != (ssize_t)x.a_syms)
109 goto shread;
110 addr += x.a_syms;
113 i = 0;
114 if (x.a_syms && read(io, &i, sizeof(int)) != sizeof(int))
115 goto shread;
117 (void)memcpy(addr, &i, sizeof(int));
118 if (i) {
119 i -= sizeof(int);
120 addr += sizeof(int);
121 if (read(io, addr, i) != i)
122 goto shread;
123 addr += i;
126 if (x.a_syms) {
127 /* and that many bytes of (debug symbols?) */
128 printf("+%d]", i);
131 close(io);
133 #define round_to_size(x) \
134 (((int)(x) + sizeof(int) - 1) & ~(sizeof(int) - 1))
135 esym = (char *)round_to_size(addr - loadaddr);
136 #undef round_to_size
138 /* and note the end address of all this */
139 printf(" total=0x%lx\n", (u_long)addr);
142 * Machine-dependent code must now adjust the
143 * entry point. This used to be done here,
144 * but some systems may need to relocate the
145 * loaded file before jumping to it, and the
146 * displayed start address would be wrong.
149 #ifdef EXEC_DEBUG
150 printf("ssym=0x%x esym=0x%x\n", ssym, esym);
151 printf("\n\nReturn to boot...\n");
152 getchar();
153 #endif
155 machdep_start((char *)x.a_entry, howto, loadaddr, ssym, esym);
157 /* exec failed */
158 errno = ENOEXEC;
159 return;
161 shread:
162 close(io);
163 errno = EIO;
164 return;