0
|
1 /* Unexec for MIPS (including IRIS4D).
|
|
2 Copyright (C) 1988, 1992, 1993, 1994
|
|
3 Free Software Foundation, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: FSF 19.31. */
|
|
23
|
|
24 #include <config.h>
|
|
25 #include <sys/types.h>
|
|
26 #include <sys/file.h>
|
|
27 #include <sys/stat.h>
|
|
28 #include <stdio.h>
|
|
29 #include <varargs.h>
|
|
30
|
|
31 #ifdef MACH
|
|
32
|
|
33 #include <a.out.h>
|
|
34
|
|
35 /* I don't know why this isn't defined. */
|
|
36 #ifndef STYP_INIT
|
|
37 #define STYP_INIT 0x80000000
|
|
38 #endif
|
|
39
|
|
40 /* I don't know why this isn't defined. */
|
|
41 #ifndef _RDATA
|
|
42 #define _RDATA ".rdata"
|
|
43 #define STYP_RDATA 0x00000100
|
|
44 #endif
|
|
45
|
|
46 /* Small ("near") data section. */
|
|
47 #ifndef _SDATA
|
|
48 #define _SDATA ".sdata"
|
|
49 #define STYP_SDATA 0x00000200
|
|
50 #endif
|
|
51
|
|
52 /* Small ("near") bss section. */
|
|
53 #ifndef _SBSS
|
|
54 #define _SBSS ".sbss"
|
|
55 #define STYP_SBSS 0x00000400
|
|
56 #endif
|
|
57
|
|
58 /* We don't seem to have a sym.h or syms.h anywhere, so we'll do it the
|
|
59 hard way. This stinks. */
|
|
60 typedef struct {
|
|
61 short magic;
|
|
62 short vstamp;
|
|
63 long ilineMax;
|
|
64 struct { long foo, offset; } offsets[11];
|
|
65 } HDRR, *pHDRR;
|
|
66
|
|
67 #else /* not MACH */
|
|
68
|
|
69 #include <filehdr.h>
|
|
70 #include <aouthdr.h>
|
|
71 #include <scnhdr.h>
|
|
72 #include <sym.h>
|
|
73
|
|
74 #endif /* not MACH */
|
|
75
|
|
76 #if defined (IRIS_4D) || defined (sony)
|
|
77 #include "getpagesize.h"
|
|
78 #include <fcntl.h>
|
|
79 #endif
|
|
80
|
|
81 static void fatal_unexec ();
|
|
82 static void mark_x ();
|
|
83
|
|
84 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
|
|
85 errno = EEOF; \
|
|
86 if (read (_fd, _buffer, _size) != _size) \
|
|
87 fatal_unexec (_error_message, _error_arg);
|
|
88
|
|
89 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
|
|
90 if (write (_fd, _buffer, _size) != _size) \
|
|
91 fatal_unexec (_error_message, _error_arg);
|
|
92
|
|
93 #define SEEK(_fd, _position, _error_message, _error_arg) \
|
|
94 errno = EEOF; \
|
|
95 if (lseek (_fd, _position, L_SET) != _position) \
|
|
96 fatal_unexec (_error_message, _error_arg);
|
|
97
|
|
98 extern int errno;
|
|
99 extern char *strerror ();
|
|
100 #define EEOF -1
|
|
101
|
|
102 static struct scnhdr *text_section;
|
|
103 static struct scnhdr *init_section;
|
|
104 static struct scnhdr *finit_section;
|
|
105 static struct scnhdr *rdata_section;
|
|
106 static struct scnhdr *data_section;
|
|
107 static struct scnhdr *lit8_section;
|
|
108 static struct scnhdr *lit4_section;
|
|
109 static struct scnhdr *sdata_section;
|
|
110 static struct scnhdr *sbss_section;
|
|
111 static struct scnhdr *bss_section;
|
|
112
|
|
113 struct headers {
|
|
114 struct filehdr fhdr;
|
|
115 struct aouthdr aout;
|
|
116 struct scnhdr section[10];
|
|
117 };
|
|
118
|
|
119 /* Define name of label for entry point for the dumped executable. */
|
|
120
|
|
121 #ifndef DEFAULT_ENTRY_ADDRESS
|
|
122 #define DEFAULT_ENTRY_ADDRESS __start
|
|
123 #endif
|
|
124
|
|
125 unexec (new_name, a_name, data_start, bss_start, entry_address)
|
|
126 char *new_name, *a_name;
|
|
127 unsigned data_start, bss_start, entry_address;
|
|
128 {
|
|
129 int new, old;
|
|
130 int pagesize, brk;
|
|
131 int newsyms, symrel;
|
|
132 int nread;
|
|
133 struct headers hdr;
|
|
134 int i;
|
|
135 int vaddr, scnptr;
|
|
136 #define BUFSIZE 8192
|
|
137 char buffer[BUFSIZE];
|
|
138
|
|
139 old = open (a_name, O_RDONLY, 0);
|
|
140 if (old < 0) fatal_unexec ("opening %s", a_name);
|
|
141
|
|
142 new = creat (new_name, 0666);
|
|
143 if (new < 0) fatal_unexec ("creating %s", new_name);
|
|
144
|
|
145 hdr = *((struct headers *)TEXT_START);
|
|
146 #ifdef MIPS2
|
|
147 if (hdr.fhdr.f_magic != MIPSELMAGIC
|
|
148 && hdr.fhdr.f_magic != MIPSEBMAGIC
|
|
149 && hdr.fhdr.f_magic != (MIPSELMAGIC | 1)
|
|
150 && hdr.fhdr.f_magic != (MIPSEBMAGIC | 1))
|
|
151 {
|
|
152 fprintf (stderr,
|
|
153 "unexec: input file magic number is %x, not %x, %x, %x or %x.\n",
|
|
154 hdr.fhdr.f_magic,
|
|
155 MIPSELMAGIC, MIPSEBMAGIC,
|
|
156 MIPSELMAGIC | 1, MIPSEBMAGIC | 1);
|
|
157 exit(1);
|
|
158 }
|
|
159 #else /* not MIPS2 */
|
|
160 if (hdr.fhdr.f_magic != MIPSELMAGIC
|
|
161 && hdr.fhdr.f_magic != MIPSEBMAGIC)
|
|
162 {
|
|
163 fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n",
|
|
164 hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC);
|
|
165 exit (1);
|
|
166 }
|
|
167 #endif /* not MIPS2 */
|
|
168 if (hdr.fhdr.f_opthdr != sizeof (hdr.aout))
|
|
169 {
|
|
170 fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n",
|
|
171 hdr.fhdr.f_opthdr, sizeof (hdr.aout));
|
|
172 exit (1);
|
|
173 }
|
|
174 if (hdr.aout.magic != ZMAGIC)
|
|
175 {
|
|
176 fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n",
|
|
177 hdr.aout.magic, ZMAGIC);
|
|
178 exit (1);
|
|
179 }
|
|
180
|
|
181 #define CHECK_SCNHDR(ptr, name, flags) \
|
|
182 ptr = NULL; \
|
|
183 for (i = 0; i < hdr.fhdr.f_nscns && !ptr; i++) \
|
|
184 if (strcmp (hdr.section[i].s_name, name) == 0) \
|
|
185 { \
|
|
186 if (hdr.section[i].s_flags != flags) \
|
|
187 fprintf (stderr, "unexec: %x flags (%x expected) in %s section.\n", \
|
|
188 hdr.section[i].s_flags, flags, name); \
|
|
189 ptr = hdr.section + i; \
|
|
190 }
|
|
191
|
|
192 CHECK_SCNHDR (text_section, _TEXT, STYP_TEXT);
|
|
193 CHECK_SCNHDR (init_section, _INIT, STYP_INIT);
|
|
194 CHECK_SCNHDR (rdata_section, _RDATA, STYP_RDATA);
|
|
195 CHECK_SCNHDR (data_section, _DATA, STYP_DATA);
|
|
196 #ifdef _LIT8
|
|
197 CHECK_SCNHDR (lit8_section, _LIT8, STYP_LIT8);
|
|
198 CHECK_SCNHDR (lit4_section, _LIT4, STYP_LIT4);
|
|
199 #endif /* _LIT8 */
|
|
200 CHECK_SCNHDR (sdata_section, _SDATA, STYP_SDATA);
|
|
201 CHECK_SCNHDR (sbss_section, _SBSS, STYP_SBSS);
|
|
202 CHECK_SCNHDR (bss_section, _BSS, STYP_BSS);
|
|
203 #if 0 /* Apparently this error check goes off on irix 3.3,
|
|
204 but it doesn't indicate a real problem. */
|
|
205 if (i != hdr.fhdr.f_nscns)
|
|
206 fprintf (stderr, "unexec: %d sections found instead of %d.\n",
|
|
207 i, hdr.fhdr.f_nscns);
|
|
208 #endif
|
|
209
|
|
210 text_section->s_scnptr = 0;
|
|
211
|
|
212 pagesize = getpagesize ();
|
|
213 /* Casting to int avoids compiler error on NEWS-OS 5.0.2. */
|
|
214 brk = (((int) (sbrk (0))) + pagesize - 1) & (-pagesize);
|
|
215 hdr.aout.dsize = brk - DATA_START;
|
|
216 hdr.aout.bsize = 0;
|
|
217 if (entry_address == 0)
|
|
218 {
|
|
219 extern DEFAULT_ENTRY_ADDRESS ();
|
|
220 hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS;
|
|
221 }
|
|
222 else
|
|
223 hdr.aout.entry = entry_address;
|
|
224
|
|
225 hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize;
|
|
226 rdata_section->s_size = data_start - DATA_START;
|
|
227
|
|
228 /* Adjust start and virtual addresses of rdata_section, too. */
|
|
229 rdata_section->s_vaddr = DATA_START;
|
|
230 rdata_section->s_paddr = DATA_START;
|
|
231 rdata_section->s_scnptr = text_section->s_scnptr + hdr.aout.tsize;
|
|
232
|
|
233 data_section->s_vaddr = data_start;
|
|
234 data_section->s_paddr = data_start;
|
|
235 data_section->s_size = brk - data_start;
|
|
236 data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size;
|
|
237 vaddr = data_section->s_vaddr + data_section->s_size;
|
|
238 scnptr = data_section->s_scnptr + data_section->s_size;
|
|
239 if (lit8_section != NULL)
|
|
240 {
|
|
241 lit8_section->s_vaddr = vaddr;
|
|
242 lit8_section->s_paddr = vaddr;
|
|
243 lit8_section->s_size = 0;
|
|
244 lit8_section->s_scnptr = scnptr;
|
|
245 }
|
|
246 if (lit4_section != NULL)
|
|
247 {
|
|
248 lit4_section->s_vaddr = vaddr;
|
|
249 lit4_section->s_paddr = vaddr;
|
|
250 lit4_section->s_size = 0;
|
|
251 lit4_section->s_scnptr = scnptr;
|
|
252 }
|
|
253 if (sdata_section != NULL)
|
|
254 {
|
|
255 sdata_section->s_vaddr = vaddr;
|
|
256 sdata_section->s_paddr = vaddr;
|
|
257 sdata_section->s_size = 0;
|
|
258 sdata_section->s_scnptr = scnptr;
|
|
259 }
|
|
260 if (sbss_section != NULL)
|
|
261 {
|
|
262 sbss_section->s_vaddr = vaddr;
|
|
263 sbss_section->s_paddr = vaddr;
|
|
264 sbss_section->s_size = 0;
|
|
265 sbss_section->s_scnptr = scnptr;
|
|
266 }
|
|
267 if (bss_section != NULL)
|
|
268 {
|
|
269 bss_section->s_vaddr = vaddr;
|
|
270 bss_section->s_paddr = vaddr;
|
|
271 bss_section->s_size = 0;
|
|
272 bss_section->s_scnptr = scnptr;
|
|
273 }
|
|
274
|
|
275 WRITE (new, (void *) TEXT_START, hdr.aout.tsize,
|
|
276 "writing text section to %s", new_name);
|
|
277 WRITE (new, (void *) DATA_START, hdr.aout.dsize,
|
|
278 "writing data section to %s", new_name);
|
|
279
|
|
280 SEEK (old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name);
|
|
281 errno = EEOF;
|
|
282 nread = read (old, buffer, BUFSIZE);
|
|
283 if (nread < sizeof (HDRR)) fatal_unexec ("reading symbols from %s", a_name);
|
|
284 newsyms = hdr.aout.tsize + hdr.aout.dsize;
|
|
285 symrel = newsyms - hdr.fhdr.f_symptr;
|
|
286 hdr.fhdr.f_symptr = newsyms;
|
|
287 #define symhdr ((pHDRR)buffer)
|
|
288 #ifdef MACH
|
|
289 for (i = 0; i < 11; i++)
|
|
290 symhdr->offsets[i].offset += symrel;
|
|
291 #else
|
|
292 symhdr->cbLineOffset += symrel;
|
|
293 symhdr->cbDnOffset += symrel;
|
|
294 symhdr->cbPdOffset += symrel;
|
|
295 symhdr->cbSymOffset += symrel;
|
|
296 symhdr->cbOptOffset += symrel;
|
|
297 symhdr->cbAuxOffset += symrel;
|
|
298 symhdr->cbSsOffset += symrel;
|
|
299 symhdr->cbSsExtOffset += symrel;
|
|
300 symhdr->cbFdOffset += symrel;
|
|
301 symhdr->cbRfdOffset += symrel;
|
|
302 symhdr->cbExtOffset += symrel;
|
|
303 #endif
|
|
304 #undef symhdr
|
|
305 do
|
|
306 {
|
|
307 if (write (new, buffer, nread) != nread)
|
|
308 fatal_unexec ("writing symbols to %s", new_name);
|
|
309 nread = read (old, buffer, BUFSIZE);
|
|
310 if (nread < 0) fatal_unexec ("reading symbols from %s", a_name);
|
|
311 #undef BUFSIZE
|
|
312 } while (nread != 0);
|
|
313
|
|
314 SEEK (new, 0, "seeking to start of header in %s", new_name);
|
|
315 WRITE (new, &hdr, sizeof (hdr),
|
|
316 "writing header of %s", new_name);
|
|
317
|
|
318 close (old);
|
|
319 close (new);
|
|
320 mark_x (new_name);
|
|
321 }
|
|
322
|
|
323 /*
|
|
324 * mark_x
|
|
325 *
|
|
326 * After successfully building the new a.out, mark it executable
|
|
327 */
|
|
328
|
|
329 static void
|
|
330 mark_x (name)
|
|
331 char *name;
|
|
332 {
|
|
333 struct stat sbuf;
|
|
334 int um = umask (777);
|
|
335 umask (um);
|
|
336 if (stat (name, &sbuf) < 0)
|
|
337 fatal_unexec ("getting protection on %s", name);
|
|
338 sbuf.st_mode |= 0111 & ~um;
|
|
339 if (chmod (name, sbuf.st_mode) < 0)
|
|
340 fatal_unexec ("setting protection on %s", name);
|
|
341 }
|
|
342
|
|
343 static void
|
|
344 fatal_unexec (s, va_alist)
|
|
345 va_dcl
|
|
346 {
|
|
347 va_list ap;
|
|
348 if (errno == EEOF)
|
|
349 fputs ("unexec: unexpected end of file, ", stderr);
|
|
350 else
|
|
351 fprintf (stderr, "unexec: %s, ", strerror (errno));
|
|
352 va_start (ap);
|
|
353 _doprnt (s, ap, stderr);
|
|
354 fputs (".\n", stderr);
|
|
355 exit (1);
|
|
356 }
|