Mercurial > emacs
annotate src/unexmips.c @ 5879:66aadd6ba5e6
(mouse-start-end): For double click with START on openparen,
start forward scan from START.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 10 Feb 1994 07:39:09 +0000 |
parents | d1d144ed5b76 |
children | 999daf5b65b6 |
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 | |
5521
d1d144ed5b76
Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
9 Copyright (C) 1988, 1994 Free Software Foundation, Inc. |
486 | 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 | |
5521
d1d144ed5b76
Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
15 the Free Software Foundation; either version 2, or (at your option) |
486 | 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 | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
3591
diff
changeset
|
28 #include <config.h> |
486 | 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 | |
1486
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
39 #if defined (IRIS_4D) || defined (sony) |
486 | 40 #include "getpagesize.h" |
1486
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
41 #include <fcntl.h> |
486 | 42 #endif |
43 | |
44 static void fatal_unexec (); | |
1122
42922d56e5a6
* unexmips.c (mark_x): Declare this as static void at the top of
Jim Blandy <jimb@redhat.com>
parents:
1094
diff
changeset
|
45 static void mark_x (); |
486 | 46 |
47 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \ | |
48 errno = EEOF; \ | |
49 if (read (_fd, _buffer, _size) != _size) \ | |
50 fatal_unexec (_error_message, _error_arg); | |
51 | |
52 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \ | |
53 if (write (_fd, _buffer, _size) != _size) \ | |
54 fatal_unexec (_error_message, _error_arg); | |
55 | |
56 #define SEEK(_fd, _position, _error_message, _error_arg) \ | |
57 errno = EEOF; \ | |
58 if (lseek (_fd, _position, L_SET) != _position) \ | |
59 fatal_unexec (_error_message, _error_arg); | |
60 | |
61 extern int errno; | |
5521
d1d144ed5b76
Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
62 extern char *strerror (); |
486 | 63 #define EEOF -1 |
64 | |
65 static struct scnhdr *text_section; | |
66 static struct scnhdr *init_section; | |
67 static struct scnhdr *finit_section; | |
68 static struct scnhdr *rdata_section; | |
69 static struct scnhdr *data_section; | |
70 static struct scnhdr *lit8_section; | |
71 static struct scnhdr *lit4_section; | |
72 static struct scnhdr *sdata_section; | |
73 static struct scnhdr *sbss_section; | |
74 static struct scnhdr *bss_section; | |
75 | |
76 struct headers { | |
77 struct filehdr fhdr; | |
78 struct aouthdr aout; | |
79 struct scnhdr section[10]; | |
80 }; | |
81 | |
82 /* Define name of label for entry point for the dumped executable. */ | |
83 | |
84 #ifndef DEFAULT_ENTRY_ADDRESS | |
85 #define DEFAULT_ENTRY_ADDRESS __start | |
86 #endif | |
87 | |
88 unexec (new_name, a_name, data_start, bss_start, entry_address) | |
89 char *new_name, *a_name; | |
90 unsigned data_start, bss_start, entry_address; | |
91 { | |
92 int new, old; | |
93 int pagesize, brk; | |
94 int newsyms, symrel; | |
95 int nread; | |
96 struct headers hdr; | |
97 int i; | |
98 int vaddr, scnptr; | |
99 #define BUFSIZE 8192 | |
100 char buffer[BUFSIZE]; | |
101 | |
102 old = open (a_name, O_RDONLY, 0); | |
103 if (old < 0) fatal_unexec ("opening %s", a_name); | |
104 | |
105 new = creat (new_name, 0666); | |
106 if (new < 0) fatal_unexec ("creating %s", new_name); | |
107 | |
108 hdr = *((struct headers *)TEXT_START); | |
109 #ifdef MIPS2 | |
110 if (hdr.fhdr.f_magic != MIPSELMAGIC | |
111 && hdr.fhdr.f_magic != MIPSEBMAGIC | |
112 && hdr.fhdr.f_magic != (MIPSELMAGIC | 1) | |
113 && hdr.fhdr.f_magic != (MIPSEBMAGIC | 1)) | |
114 { | |
1486
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
115 fprintf (stderr, |
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
116 "unexec: input file magic number is %x, not %x, %x, %x or %x.\n", |
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
117 hdr.fhdr.f_magic, |
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
118 MIPSELMAGIC, MIPSEBMAGIC, |
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
119 MIPSELMAGIC | 1, MIPSEBMAGIC | 1); |
486 | 120 exit(1); |
121 } | |
122 #else /* not MIPS2 */ | |
123 if (hdr.fhdr.f_magic != MIPSELMAGIC | |
124 && hdr.fhdr.f_magic != MIPSEBMAGIC) | |
125 { | |
126 fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n", | |
127 hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC); | |
128 exit (1); | |
129 } | |
130 #endif /* not MIPS2 */ | |
131 if (hdr.fhdr.f_opthdr != sizeof (hdr.aout)) | |
132 { | |
133 fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n", | |
134 hdr.fhdr.f_opthdr, sizeof (hdr.aout)); | |
135 exit (1); | |
136 } | |
137 if (hdr.aout.magic != ZMAGIC) | |
138 { | |
139 fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n", | |
140 hdr.aout.magic, ZMAGIC); | |
141 exit (1); | |
142 } | |
143 | |
144 #define CHECK_SCNHDR(ptr, name, flags) \ | |
1094
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
145 ptr = NULL; \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
146 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
|
147 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
|
148 { \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
149 if (hdr.section[i].s_flags != flags) \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
150 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
|
151 hdr.section[i].s_flags, flags, name); \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
152 ptr = hdr.section + i; \ |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
153 } \ |
486 | 154 |
155 CHECK_SCNHDR (text_section, _TEXT, STYP_TEXT); | |
156 CHECK_SCNHDR (init_section, _INIT, STYP_INIT); | |
157 CHECK_SCNHDR (rdata_section, _RDATA, STYP_RDATA); | |
158 CHECK_SCNHDR (data_section, _DATA, STYP_DATA); | |
159 #ifdef _LIT8 | |
160 CHECK_SCNHDR (lit8_section, _LIT8, STYP_LIT8); | |
161 CHECK_SCNHDR (lit4_section, _LIT4, STYP_LIT4); | |
162 #endif /* _LIT8 */ | |
163 CHECK_SCNHDR (sdata_section, _SDATA, STYP_SDATA); | |
164 CHECK_SCNHDR (sbss_section, _SBSS, STYP_SBSS); | |
165 CHECK_SCNHDR (bss_section, _BSS, STYP_BSS); | |
1486
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
166 #if 0 /* Apparently this error check goes off on irix 3.3, |
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
167 but it doesn't indicate a real problem. */ |
486 | 168 if (i != hdr.fhdr.f_nscns) |
169 fprintf (stderr, "unexec: %d sections found instead of %d.\n", | |
170 i, hdr.fhdr.f_nscns); | |
1486
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
171 #endif |
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
172 |
1a12c5f1c04d
[sony, IRIS_4D]: Include getpagesize.h and fcntl.h.
Richard M. Stallman <rms@gnu.org>
parents:
1122
diff
changeset
|
173 text_section->s_scnptr = 0; |
486 | 174 |
175 pagesize = getpagesize (); | |
176 brk = (sbrk (0) + pagesize - 1) & (-pagesize); | |
177 hdr.aout.dsize = brk - DATA_START; | |
178 hdr.aout.bsize = 0; | |
179 if (entry_address == 0) | |
180 { | |
181 extern DEFAULT_ENTRY_ADDRESS (); | |
182 hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS; | |
183 } | |
184 else | |
185 hdr.aout.entry = entry_address; | |
186 | |
187 hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize; | |
188 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
|
189 |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
190 /* 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
|
191 rdata_section->s_vaddr = DATA_START; |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
192 rdata_section->s_paddr = DATA_START; |
861c2997afee
(unexec): Allow sections in any order.
Richard M. Stallman <rms@gnu.org>
parents:
568
diff
changeset
|
193 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
|
194 |
486 | 195 data_section->s_vaddr = data_start; |
196 data_section->s_paddr = data_start; | |
568 | 197 data_section->s_size = brk - data_start; |
486 | 198 data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size; |
199 vaddr = data_section->s_vaddr + data_section->s_size; | |
200 scnptr = data_section->s_scnptr + data_section->s_size; | |
201 if (lit8_section != NULL) | |
202 { | |
203 lit8_section->s_vaddr = vaddr; | |
204 lit8_section->s_paddr = vaddr; | |
205 lit8_section->s_size = 0; | |
206 lit8_section->s_scnptr = scnptr; | |
207 } | |
208 if (lit4_section != NULL) | |
209 { | |
210 lit4_section->s_vaddr = vaddr; | |
211 lit4_section->s_paddr = vaddr; | |
212 lit4_section->s_size = 0; | |
213 lit4_section->s_scnptr = scnptr; | |
214 } | |
215 if (sdata_section != NULL) | |
216 { | |
217 sdata_section->s_vaddr = vaddr; | |
218 sdata_section->s_paddr = vaddr; | |
219 sdata_section->s_size = 0; | |
220 sdata_section->s_scnptr = scnptr; | |
221 } | |
222 if (sbss_section != NULL) | |
223 { | |
224 sbss_section->s_vaddr = vaddr; | |
225 sbss_section->s_paddr = vaddr; | |
226 sbss_section->s_size = 0; | |
227 sbss_section->s_scnptr = scnptr; | |
228 } | |
229 if (bss_section != NULL) | |
230 { | |
231 bss_section->s_vaddr = vaddr; | |
232 bss_section->s_paddr = vaddr; | |
233 bss_section->s_size = 0; | |
234 bss_section->s_scnptr = scnptr; | |
235 } | |
236 | |
237 WRITE (new, TEXT_START, hdr.aout.tsize, | |
238 "writing text section to %s", new_name); | |
239 WRITE (new, DATA_START, hdr.aout.dsize, | |
240 "writing text section to %s", new_name); | |
241 | |
242 SEEK (old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name); | |
243 errno = EEOF; | |
244 nread = read (old, buffer, BUFSIZE); | |
245 if (nread < sizeof (HDRR)) fatal_unexec ("reading symbols from %s", a_name); | |
246 #define symhdr ((pHDRR)buffer) | |
247 newsyms = hdr.aout.tsize + hdr.aout.dsize; | |
248 symrel = newsyms - hdr.fhdr.f_symptr; | |
249 hdr.fhdr.f_symptr = newsyms; | |
250 symhdr->cbLineOffset += symrel; | |
251 symhdr->cbDnOffset += symrel; | |
252 symhdr->cbPdOffset += symrel; | |
253 symhdr->cbSymOffset += symrel; | |
254 symhdr->cbOptOffset += symrel; | |
255 symhdr->cbAuxOffset += symrel; | |
256 symhdr->cbSsOffset += symrel; | |
257 symhdr->cbSsExtOffset += symrel; | |
258 symhdr->cbFdOffset += symrel; | |
259 symhdr->cbRfdOffset += symrel; | |
260 symhdr->cbExtOffset += symrel; | |
261 #undef symhdr | |
262 do | |
263 { | |
264 if (write (new, buffer, nread) != nread) | |
265 fatal_unexec ("writing symbols to %s", new_name); | |
266 nread = read (old, buffer, BUFSIZE); | |
267 if (nread < 0) fatal_unexec ("reading symbols from %s", a_name); | |
268 #undef BUFSIZE | |
269 } while (nread != 0); | |
270 | |
271 SEEK (new, 0, "seeking to start of header in %s", new_name); | |
272 WRITE (new, &hdr, sizeof (hdr), | |
273 "writing header of %s", new_name); | |
274 | |
275 close (old); | |
276 close (new); | |
277 mark_x (new_name); | |
278 } | |
279 | |
280 /* | |
281 * mark_x | |
282 * | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
1486
diff
changeset
|
283 * After successfully building the new a.out, mark it executable |
486 | 284 */ |
285 | |
1122
42922d56e5a6
* unexmips.c (mark_x): Declare this as static void at the top of
Jim Blandy <jimb@redhat.com>
parents:
1094
diff
changeset
|
286 static void |
486 | 287 mark_x (name) |
288 char *name; | |
289 { | |
290 struct stat sbuf; | |
291 int um = umask (777); | |
292 umask (um); | |
293 if (stat (name, &sbuf) < 0) | |
294 fatal_unexec ("getting protection on %s", name); | |
295 sbuf.st_mode |= 0111 & ~um; | |
296 if (chmod (name, sbuf.st_mode) < 0) | |
297 fatal_unexec ("setting protection on %s", name); | |
298 } | |
299 | |
300 static void | |
301 fatal_unexec (s, va_alist) | |
5521
d1d144ed5b76
Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
302 va_dcl |
486 | 303 { |
304 va_list ap; | |
305 if (errno == EEOF) | |
306 fputs ("unexec: unexpected end of file, ", stderr); | |
307 else | |
5521
d1d144ed5b76
Don't declare sys_errlist; declare strerror instead.
Roland McGrath <roland@gnu.org>
parents:
4696
diff
changeset
|
308 fprintf (stderr, "unexec: %s, ", strerror (errno)); |
486 | 309 va_start (ap); |
310 _doprnt (s, ap, stderr); | |
311 fputs (".\n", stderr); | |
312 exit (1); | |
313 } |