1 /* $OpenBSD: exec_aout.c,v 1.8 2003/09/26 17:00:27 deraadt Exp $ */
2
3 /*
4 * Copyright (c) 1999 Mats O Jansson. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 #include <err.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <nlist.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <sys/exec.h>
35 #include <sys/types.h>
36
37 #include "config.h"
38 #include "ukc.h"
39
40 __RCSID("$MirOS: src/usr.sbin/config/exec_aout.c,v 1.3 2010/07/25 16:31:08 tg Exp $");
41
42 caddr_t aout_p, aout_r;
43 unsigned long aout_psz = 0, aout_rsz = 0;
44 struct exec aout_ex;
45 unsigned long aout_adjvalue = 0;
46 unsigned long aout_datashift = 0;
47
48 void
aout_computeadj(void)49 aout_computeadj(void)
50 {
51 aout_adjvalue = (unsigned long)aout_p +
52 N_TXTOFF(aout_ex) - nl[P_KERNEL_TEXT].n_value;
53
54 /*
55 * On m68k a.out ZMAGIC kernel, kernel_text begins _after_ the a.out
56 * header, so compensate for it.
57 */
58 if (nl[P_KERNEL_TEXT].n_value & (__LDPGSZ - 1))
59 aout_adjvalue += sizeof(aout_ex);
60
61 /*
62 * On NMAGIC kernel, we need an extra relocation for the data area
63 */
64 aout_datashift = (N_DATADDR(aout_ex) - N_TXTADDR(aout_ex)) -
65 aout_ex.a_text;
66 }
67
68 /* ``kernel'' vaddr -> in-memory address */
69 caddr_t
aout_adjust(caddr_t x)70 aout_adjust(caddr_t x)
71 {
72
73 if (aout_adjvalue == 0)
74 aout_computeadj();
75
76 if (aout_datashift != 0 &&
77 (unsigned long)x >= N_DATADDR(aout_ex))
78 x -= aout_datashift;
79
80 return (x + aout_adjvalue);
81 }
82
83 /* in-memory address -> ``kernel'' vaddr */
84 caddr_t
aout_readjust(x)85 aout_readjust(x)
86 caddr_t x;
87 {
88 caddr_t y;
89
90 #if 0 /* unnecessary, aout_adjust() is always invoked first */
91 if (aout_adjvalue == 0)
92 aout_computeadj();
93 #endif
94
95 y = x - aout_adjvalue;
96 if (aout_datashift != 0 &&
97 (unsigned long)y >= N_TXTADDR(aout_ex) + aout_ex.a_text)
98 y += aout_datashift;
99
100 return (y);
101 }
102
103 int
aout_check(file)104 aout_check(file)
105 char *file;
106 {
107 int fd, ret = 1;
108
109 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
110 return (0);
111 if (read(fd, (char *)&aout_ex, sizeof(aout_ex)) != sizeof(aout_ex))
112 ret = 0;
113 if (ret) {
114 if (N_BADMAG(aout_ex))
115 ret = 0;
116 }
117
118 close(fd);
119 return (ret);
120 }
121
122 void
aout_loadkernel(const char * file)123 aout_loadkernel(const char *file)
124 {
125 int fd;
126 off_t cur, end;
127
128 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0)
129 err(1, "%s", file);
130
131 if (read(fd, (char *)&aout_ex, sizeof(aout_ex)) != sizeof(aout_ex))
132 errx(1, "can't read a.out header");
133
134 if (N_BADMAG(aout_ex))
135 errx(1, "bad a.out magic");
136
137 lseek(fd, (off_t)0, SEEK_SET);
138
139 aout_psz = (int)(aout_ex.a_text + N_TXTOFF(aout_ex) +
140 aout_ex.a_data);
141
142 aout_p = emalloc(aout_psz);
143
144 if (read(fd, aout_p, aout_psz) != aout_psz)
145 errx(1, "can't read a.out text and data");
146
147 cur = lseek(fd, (off_t)0, SEEK_CUR);
148 end = lseek(fd, (off_t)0, SEEK_END);
149 (void)lseek(fd, cur, SEEK_SET);
150
151 aout_rsz = (int)(end - cur);
152
153 aout_r = emalloc(aout_rsz);
154
155 if (read(fd, aout_r, aout_rsz) != aout_rsz)
156 errx(1, "can't read rest of file %s", file);
157
158 close(fd);
159 }
160
161 void
aout_savekernel(const char * outfile)162 aout_savekernel(const char *outfile)
163 {
164 int fd;
165
166 if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0)
167 err(1, "%s", outfile);
168
169 if (write(fd, aout_p, aout_psz) != aout_psz)
170 errx(1, "can't write a.out text and data");
171
172 if (write(fd, aout_r, aout_rsz) != aout_rsz)
173 errx(1, "can't write rest of file %s", outfile);
174
175 close(fd);
176 }
177