1 /** $MirOS: src/sbin/modload/a.out.c,v 1.2 2005/11/17 12:07:57 tg Exp $ */
2 /* $OpenBSD: a.out.c,v 1.4 2002/12/11 18:28:22 deraadt Exp $ */
3 /* $NetBSD: a.out.c,v 1.1 1999/06/13 12:54:40 mrg Exp $ */
4
5 /*
6 * Copyright (c) 1993 Terrence R. Lambert.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Terrence R. Lambert.
20 * 4. The name Terrence R. Lambert may not be used to endorse or promote
21 * products derived from this software without specific prior written
22 * permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY TERRENCE R. LAMBERT ``AS IS'' AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE TERRENCE R. LAMBERT BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/lkm.h>
42
43 #include <a.out.h>
44 #include <err.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50
51 #include "modload.h"
52
53 __RCSID("$MirOS: src/sbin/modload/a.out.c,v 1.2 2005/11/17 12:07:57 tg Exp $");
54
55 /*
56 * Expected linker options:
57 *
58 * -A executable to link against
59 * -e entry point
60 * -o output file
61 * -T address to link to in hex (assumes it's a page boundry)
62 * <target> object file
63 */
64
65 #define LINKCMD "ld -A %s -e _%s -o %s -T %p %s"
66
67 static struct exec sinfo_buf; /* buffer for loading */
68 extern int devfd, modfd;
69 extern struct lmc_resrv resrv;
70 extern int symtab;
71
72 void
a_out_linkcmd(char * buf,size_t len,const char * kernel,const char * entry,const char * outfile,const void * address,const char * object)73 a_out_linkcmd(char *buf, size_t len, const char *kernel,
74 const char *entry, const char *outfile, const void *address,
75 const char *object)
76 {
77 ssize_t n;
78
79 n = snprintf(buf, len, LINKCMD, kernel, entry,
80 outfile, address, object);
81 if (n < 0 || n >= (ssize_t)len)
82 errx(1, "link command longer than %lu bytes", (u_long)len);
83 }
84
85 static int
a_out_read_header(int fd,struct exec * info_buf)86 a_out_read_header(int fd, struct exec *info_buf)
87 {
88 ssize_t n;
89
90 n = read(fd, info_buf, sizeof(*info_buf));
91 if (n < 0)
92 err(1, "failed reading %lu bytes", (u_long)sizeof(*info_buf));
93 if (n != sizeof(*info_buf)) {
94 if (debug)
95 fprintf(stderr, "failed to read %lu bytes",
96 (u_long)sizeof(*info_buf));
97 return -1;
98 }
99
100 /*
101 * Magic number...
102 */
103 if (N_BADMAG(*info_buf))
104 errx(4, "not an a.out format file");
105 return 0;
106 }
107
108 int
a_out_mod_sizes(int fd,size_t * modsize,int * strtablen,struct lmc_resrv * resrvp,struct stat * sp)109 a_out_mod_sizes(int fd, size_t *modsize, int *strtablen,
110 struct lmc_resrv *resrvp, struct stat *sp)
111 {
112 struct exec info_buf;
113
114 if (a_out_read_header(fd, &info_buf) < 0)
115 return -1;
116
117 /*
118 * Calculate the size of the module
119 */
120 *modsize = info_buf.a_text + info_buf.a_data + info_buf.a_bss;
121
122 *strtablen = sp->st_size - N_STROFF(info_buf);
123
124 if (symtab) {
125 /*
126 * XXX TODO: grovel through symbol table looking for
127 * just the symbol table stuff from the new module,
128 * and skip the stuff from the kernel.
129 */
130 resrvp->sym_size = info_buf.a_syms + *strtablen;
131 resrvp->sym_symsize = info_buf.a_syms;
132 } else
133 resrvp->sym_size = resrvp->sym_symsize = 0;
134
135 return (0);
136 }
137
138 void *
a_out_mod_load(int fd)139 a_out_mod_load(int fd)
140 {
141 size_t b;
142 ssize_t n;
143 char buf[MODIOBUF];
144
145 /*
146 * Get the load module post load size... do this by reading the
147 * header and doing page counts.
148 */
149 if (a_out_read_header(fd, &sinfo_buf) < 0)
150 return NULL;
151
152 /*
153 * Seek to the text offset to start loading...
154 */
155 if (lseek(fd, N_TXTOFF(sinfo_buf), 0) == -1)
156 err(12, "lseek");
157
158 /*
159 * Transfer the relinked module to kernel memory in chunks of
160 * MODIOBUF size at a time.
161 */
162 b = sinfo_buf.a_text + sinfo_buf.a_data;
163 while (b) {
164 n = read(fd, buf, MIN(b, sizeof(buf)));
165 if (n < 0)
166 err(1, "while reading from prelinked module");
167 if (n == 0)
168 errx(1, "EOF while reading from prelinked module");
169
170 loadbuf(buf, n);
171 b -= n;
172 }
173 return (void*)sinfo_buf.a_entry;
174 }
175
176 void
a_out_mod_symload(int strtablen)177 a_out_mod_symload(int strtablen)
178 {
179 struct lmc_loadbuf ldbuf;
180 char buf[MODIOBUF];
181 int bytesleft, sz;
182
183 /*
184 * Seek to the symbol table to start loading it...
185 */
186 if (lseek(modfd, N_SYMOFF(sinfo_buf), SEEK_SET) == -1)
187 err(12, "lseek");
188
189 /*
190 * we've fixed the symbol table entries, now load them
191 */
192 for (bytesleft = sinfo_buf.a_syms; bytesleft > 0; bytesleft -= sz) {
193 sz = MIN(bytesleft, MODIOBUF);
194 if (read(modfd, buf, sz) != sz)
195 err(14, "read");
196 ldbuf.cnt = sz;
197 ldbuf.data = buf;
198 if (ioctl(devfd, LMLOADSYMS, &ldbuf) == -1)
199 err(11, "error transferring sym buffer");
200 }
201
202 /* and now read the string table and load it. */
203 for (bytesleft = strtablen; bytesleft > 0; bytesleft -= sz) {
204 sz = MIN(bytesleft, MODIOBUF);
205 if (read(modfd, buf, sz) != sz)
206 err(14, "read");
207 ldbuf.cnt = sz;
208 ldbuf.data = buf;
209 if (ioctl(devfd, LMLOADSYMS, &ldbuf) == -1)
210 err(11, "error transferring stringtable buffer");
211 }
212 }
213