Mercurial > emacs
annotate src/unexmips.c @ 1118:a43d53261506
* frame.c (choose_minibuf_frame): abort if the selected frame has
nil in its minibuffer_window slot; this shouldn't happen any more.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Sun, 13 Sep 1992 12:29:01 +0000 |
parents | 861c2997afee |
children | 42922d56e5a6 |
rev | line source |
---|---|
486 | 1 /* Unexec for MIPS (including IRIS4D). |
2 Note that the GNU project considers support for MIPS operation | |
3 a peripheral activity which should not be allowed to divert effort | |
4 from development of the GNU system. Changes in this code will be | |
5 installed when users send them in, but aside from that | |
6 we don't plan to think about it, or about whether other Emacs | |
7 maintenance might break it. | |
8 | |
9 Copyright (C) 1988 Free Software Foundation, Inc. | |
10 | |
11 This file is part of GNU Emacs. | |
12 | |
13 GNU Emacs is free software; you can redistribute it and/or modify | |
14 it under the terms of the GNU General Public License as published by | |
15 the Free Software Foundation; either version 1, or (at your option) | |
16 any later version. | |
17 | |
18 GNU Emacs is distributed in the hope that it will be useful, | |
19 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 GNU General Public License for more details. | |
22 | |
23 You should have received a copy of the GNU General Public License | |
24 along with GNU Emacs; see the file COPYING. If not, write to | |
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
26 | |
27 | |
28 #include "config.h" | |
29 #include <sys/types.h> | |
30 #include <sys/file.h> | |
31 #include <sys/stat.h> | |
32 #include <stdio.h> | |
33 #include <varargs.h> | |
34 #include <filehdr.h> | |
35 #include <aouthdr.h> | |
36 #include <scnhdr.h> | |
37 #include <sym.h> | |
38 | |
39 #ifdef IRIS_4D | |
40 #include "getpagesize.h" | |
41 #endif | |
42 | |
43 static void fatal_unexec (); | |
44 | |
45 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \ | |
46 errno = EEOF; \ | |
47 if (read (_fd, _buffer, _size) != _size) \ | |
48 fatal_unexec (_error_message, _error_arg); | |
49 | |
50 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \ | |
51 if (write (_fd, _buffer, _size) != _size) \ | |
52 fatal_unexec (_error_message, _error_arg); | |
53 | |
54 #define SEEK(_fd, _position, _error_message, _error_arg) \ | |
55 errno = EEOF; \ | |
56 if (lseek (_fd, _position, L_SET) != _position) \ | |
57 fatal_unexec (_error_message, _error_arg); | |
58 | |
59 extern int errno; | |
60 extern int sys_nerr; | |
61 extern char *sys_errlist[]; | |
62 #define EEOF -1 | |
63 | |
64 static struct scnhdr *text_section; | |
65 static struct scnhdr *init_section; | |
66 static struct scnhdr *finit_section; | |
67 static struct scnhdr *rdata_section; | |
68 static struct scnhdr *data_section; | |
69 static struct scnhdr *lit8_section; | |
70 static struct scnhdr *lit4_section; | |
71 static struct scnhdr *sdata_section; | |
72 static struct scnhdr *sbss_section; | |
73 static struct scnhdr *bss_section; | |
74 | |
75 struct headers { | |
76 struct filehdr fhdr; | |
77 struct aouthdr aout; | |
78 struct scnhdr section[10]; | |
79 }; | |
80 | |
81 /* Define name of label for entry point for the dumped executable. */ | |
82 | |
83 #ifndef DEFAULT_ENTRY_ADDRESS | |
84 #define DEFAULT_ENTRY_ADDRESS __start | |
85 #endif | |
86 | |
87 unexec (new_name, a_name, data_start, bss_start, entry_address) | |
88 char *new_name, *a_name; | |
89 unsigned data_start, bss_start, entry_address; | |
90 { | |
91 int new, old; | |
92 int pagesize, brk; | |
93 int newsyms, symrel; | |
94 int nread; | |
95 struct headers hdr; | |
96 int i; | |
97 int vaddr, scnptr; | |
98 #define BUFSIZE 8192 | |
99 char buffer[BUFSIZE]; | |
100 | |
101 old = open (a_name, O_RDONLY, 0); | |
102 if (old < 0) fatal_unexec ("opening %s", a_name); | |
103 | |
104 new = creat (new_name, 0666); | |
105 if (new < 0) fatal_unexec ("creating %s", new_name); | |
106 | |
107 hdr = *((struct headers *)TEXT_START); | |
108 #ifdef MIPS2 | |
109 if (hdr.fhdr.f_magic != MIPSELMAGIC | |
110 && hdr.fhdr.f_magic != MIPSEBMAGIC | |
111 && hdr.fhdr.f_magic != (MIPSELMAGIC | 1) | |
112 && hdr.fhdr.f_magic != (MIPSEBMAGIC | 1)) | |
113 { | |
114 fprintf(stderr, | |
115 "unexec: input file magic number is %x, not %x, %x, %x or %x.\n", | |
116 hdr.fhdr.f_magic, | |
117 MIPSELMAGIC, MIPSEBMAGIC, | |
118 MIPSELMAGIC | 1, MIPSEBMAGIC | 1); | |
119 exit(1); | |
120 } | |
121 #else /* not MIPS2 */ | |
122 if (hdr.fhdr.f_magic != MIPSELMAGIC | |
123 && hdr.fhdr.f_magic != MIPSEBMAGIC) | |
124 { | |
125 fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n", | |
126 hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC); | |
127 exit (1); | |
128 } | |
129 #endif /* not MIPS2 */ | |
130 if (hdr.fhdr.f_opthdr != sizeof (hdr.aout)) | |
131 { | |
132 fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n", | |
133 hdr.fhdr.f_opthdr, sizeof (hdr.aout)); | |
134 exit (1); | |
135 } | |
136 if (hdr.aout.magic != ZMAGIC) | |
137 { | |
138 fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n", | |
139 hdr.aout.magic, ZMAGIC); | |
140 exit (1); | |
141 } | |
142 | |
143 #define CHECK_SCNHDR(ptr, name, flags) \ | |
1094
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
144 ptr = NULL; \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
145 for (i = 0; i < hdr.fhdr.f_nscns && !ptr; i++) \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
146 if (strcmp (hdr.section[i].s_name, name) == 0) \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
147 { \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
148 if (hdr.section[i].s_flags != flags) \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
149 fprintf (stderr, "unexec: %x flags (%x expected) in %s section.\n", \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
150 hdr.section[i].s_flags, flags, name); \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
151 ptr = hdr.section + i; \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
152 } \ |
486 | 153 |
154 CHECK_SCNHDR (text_section, _TEXT, STYP_TEXT); | |
155 CHECK_SCNHDR (init_section, _INIT, STYP_INIT); | |
156 CHECK_SCNHDR (rdata_section, _RDATA, STYP_RDATA); | |
157 CHECK_SCNHDR (data_section, _DATA, STYP_DATA); | |
158 #ifdef _LIT8 | |
159 CHECK_SCNHDR (lit8_section, _LIT8, STYP_LIT8); | |
160 CHECK_SCNHDR (lit4_section, _LIT4, STYP_LIT4); | |
161 #endif /* _LIT8 */ | |
162 CHECK_SCNHDR (sdata_section, _SDATA, STYP_SDATA); | |
163 CHECK_SCNHDR (sbss_section, _SBSS, STYP_SBSS); | |
164 CHECK_SCNHDR (bss_section, _BSS, STYP_BSS); | |
165 if (i != hdr.fhdr.f_nscns) | |
166 fprintf (stderr, "unexec: %d sections found instead of %d.\n", | |
167 i, hdr.fhdr.f_nscns); | |
168 | |
169 pagesize = getpagesize (); | |
170 brk = (sbrk (0) + pagesize - 1) & (-pagesize); | |
171 hdr.aout.dsize = brk - DATA_START; | |
172 hdr.aout.bsize = 0; | |
173 if (entry_address == 0) | |
174 { | |
175 extern DEFAULT_ENTRY_ADDRESS (); | |
176 hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS; | |
177 } | |
178 else | |
179 hdr.aout.entry = entry_address; | |
180 | |
181 hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize; | |
182 rdata_section->s_size = data_start - DATA_START; | |
1094
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
183 |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
184 /* Adjust start and virtual addresses of rdata_section, too. */ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
185 rdata_section->s_vaddr = DATA_START; |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
186 rdata_section->s_paddr = DATA_START; |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
187 rdata_section->s_scnptr = text_section->s_scnptr + hdr.aout.tsize; |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
188 |
486 | 189 data_section->s_vaddr = data_start; |
190 data_section->s_paddr = data_start; | |
568 | 191 data_section->s_size = brk - data_start; |
486 | 192 data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size; |
193 vaddr = data_section->s_vaddr + data_section->s_size; | |
194 scnptr = data_section->s_scnptr + data_section->s_size; | |
195 if (lit8_section != NULL) | |
196 { | |
197 lit8_section->s_vaddr = vaddr; | |
198 lit8_section->s_paddr = vaddr; | |
199 lit8_section->s_size = 0; | |
200 lit8_section->s_scnptr = scnptr; | |
201 } | |
202 if (lit4_section != NULL) | |
203 { | |
204 lit4_section->s_vaddr = vaddr; | |
205 lit4_section->s_paddr = vaddr; | |
206 lit4_section->s_size = 0; | |
207 lit4_section->s_scnptr = scnptr; | |
208 } | |
209 if (sdata_section != NULL) | |
210 { | |
211 sdata_section->s_vaddr = vaddr; | |
212 sdata_section->s_paddr = vaddr; | |
213 sdata_section->s_size = 0; | |
214 sdata_section->s_scnptr = scnptr; | |
215 } | |
216 if (sbss_section != NULL) | |
217 { | |
218 sbss_section->s_vaddr = vaddr; | |
219 sbss_section->s_paddr = vaddr; | |
220 sbss_section->s_size = 0; | |
221 sbss_section->s_scnptr = scnptr; | |
222 } | |
223 if (bss_section != NULL) | |
224 { | |
225 bss_section->s_vaddr = vaddr; | |
226 bss_section->s_paddr = vaddr; | |
227 bss_section->s_size = 0; | |
228 bss_section->s_scnptr = scnptr; | |
229 } | |
230 | |
231 WRITE (new, TEXT_START, hdr.aout.tsize, | |
232 "writing text section to %s", new_name); | |
233 WRITE (new, DATA_START, hdr.aout.dsize, | |
234 "writing text section to %s", new_name); | |
235 | |
236 SEEK (old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name); | |
237 errno = EEOF; | |
238 nread = read (old, buffer, BUFSIZE); | |
239 if (nread < sizeof (HDRR)) fatal_unexec ("reading symbols from %s", a_name); | |
240 #define symhdr ((pHDRR)buffer) | |
241 newsyms = hdr.aout.tsize + hdr.aout.dsize; | |
242 symrel = newsyms - hdr.fhdr.f_symptr; | |
243 hdr.fhdr.f_symptr = newsyms; | |
244 symhdr->cbLineOffset += symrel; | |
245 symhdr->cbDnOffset += symrel; | |
246 symhdr->cbPdOffset += symrel; | |
247 symhdr->cbSymOffset += symrel; | |
248 symhdr->cbOptOffset += symrel; | |
249 symhdr->cbAuxOffset += symrel; | |
250 symhdr->cbSsOffset += symrel; | |
251 symhdr->cbSsExtOffset += symrel; | |
252 symhdr->cbFdOffset += symrel; | |
253 symhdr->cbRfdOffset += symrel; | |
254 symhdr->cbExtOffset += symrel; | |
255 #undef symhdr | |
256 do | |
257 { | |
258 if (write (new, buffer, nread) != nread) | |
259 fatal_unexec ("writing symbols to %s", new_name); | |
260 nread = read (old, buffer, BUFSIZE); | |
261 if (nread < 0) fatal_unexec ("reading symbols from %s", a_name); | |
262 #undef BUFSIZE | |
263 } while (nread != 0); | |
264 | |
265 SEEK (new, 0, "seeking to start of header in %s", new_name); | |
266 WRITE (new, &hdr, sizeof (hdr), | |
267 "writing header of %s", new_name); | |
268 | |
269 close (old); | |
270 close (new); | |
271 mark_x (new_name); | |
272 } | |
273 | |
274 /* | |
275 * mark_x | |
276 * | |
277 * After succesfully building the new a.out, mark it executable | |
278 */ | |
279 | |
280 static | |
281 mark_x (name) | |
282 char *name; | |
283 { | |
284 struct stat sbuf; | |
285 int um = umask (777); | |
286 umask (um); | |
287 if (stat (name, &sbuf) < 0) | |
288 fatal_unexec ("getting protection on %s", name); | |
289 sbuf.st_mode |= 0111 & ~um; | |
290 if (chmod (name, sbuf.st_mode) < 0) | |
291 fatal_unexec ("setting protection on %s", name); | |
292 } | |
293 | |
294 static void | |
295 fatal_unexec (s, va_alist) | |
296 va_dcl | |
297 { | |
298 va_list ap; | |
299 if (errno == EEOF) | |
300 fputs ("unexec: unexpected end of file, ", stderr); | |
301 else if (errno < sys_nerr) | |
302 fprintf (stderr, "unexec: %s, ", sys_errlist[errno]); | |
303 else | |
304 fprintf (stderr, "unexec: error code %d, ", errno); | |
305 va_start (ap); | |
306 _doprnt (s, ap, stderr); | |
307 fputs (".\n", stderr); | |
308 exit (1); | |
309 } |