1 /* $NetBSD: exec.c,v 1.28 2009/12/29 20:21:46 elad Exp $ */
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
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
32 #include <sys/param.h>
33 #include <sys/reboot.h>
34 #ifndef SA_EXEC_ANYOWNER
37 #include <sys/exec_aout.h>
39 #include <lib/libkern/libkern.h>
47 exec(char *path
, char *loadaddr
, int howto
)
49 #ifndef SA_EXEC_ANYOWNER
54 char *addr
, *ssym
, *esym
;
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");
70 i
= read(io
, (char *)&x
, sizeof(x
));
71 if (i
!= sizeof(x
) || N_BADMAG(x
)) {
77 printf("%ld", x
.a_text
);
79 if (N_GETMAGIC(x
) == ZMAGIC
) {
80 (void)memcpy(addr
, &x
, sizeof(x
));
82 x
.a_text
-= sizeof(x
);
84 if (read(io
, (char *)addr
, x
.a_text
) != (ssize_t
)x
.a_text
)
87 if (N_GETMAGIC(x
) == ZMAGIC
|| N_GETMAGIC(x
) == NMAGIC
)
88 while ((long)addr
& (N_PAGSIZ(x
) - 1))
92 printf("+%ld", x
.a_data
);
93 if (read(io
, addr
, x
.a_data
) != (ssize_t
)x
.a_data
)
98 printf("+%ld", x
.a_bss
);
99 for (i
= 0; i
< (int)x
.a_bss
; i
++)
104 (void)memcpy(addr
, &x
.a_syms
, sizeof(x
.a_syms
));
105 addr
+= sizeof(x
.a_syms
);
107 printf("+[%ld", x
.a_syms
);
108 if (read(io
, addr
, x
.a_syms
) != (ssize_t
)x
.a_syms
)
114 if (x
.a_syms
&& read(io
, &i
, sizeof(int)) != sizeof(int))
117 (void)memcpy(addr
, &i
, sizeof(int));
121 if (read(io
, addr
, i
) != i
)
127 /* and that many bytes of (debug symbols?) */
133 #define round_to_size(x) \
134 (((int)(x) + sizeof(int) - 1) & ~(sizeof(int) - 1))
135 esym
= (char *)round_to_size(addr
- loadaddr
);
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.
150 printf("ssym=0x%x esym=0x%x\n", ssym
, esym
);
151 printf("\n\nReturn to boot...\n");
155 machdep_start((char *)x
.a_entry
, howto
, loadaddr
, ssym
, esym
);