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.
9 Copyright (C) 1988 Free Software Foundation, Inc.
11 This file is part of GNU Emacs.
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)
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.
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. */
29 #include <sys/types.h>
39 #if defined (IRIS_4D) || defined (sony)
40 #include "getpagesize.h"
44 static void fatal_unexec ();
45 static void mark_x ();
47 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
49 if (read (_fd, _buffer, _size) != _size) \
50 fatal_unexec (_error_message, _error_arg);
52 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
53 if (write (_fd, _buffer, _size) != _size) \
54 fatal_unexec (_error_message, _error_arg);
56 #define SEEK(_fd, _position, _error_message, _error_arg) \
58 if (lseek (_fd, _position, L_SET) != _position) \
59 fatal_unexec (_error_message, _error_arg);
63 extern char *sys_errlist
[];
66 static struct scnhdr
*text_section
;
67 static struct scnhdr
*init_section
;
68 static struct scnhdr
*finit_section
;
69 static struct scnhdr
*rdata_section
;
70 static struct scnhdr
*data_section
;
71 static struct scnhdr
*lit8_section
;
72 static struct scnhdr
*lit4_section
;
73 static struct scnhdr
*sdata_section
;
74 static struct scnhdr
*sbss_section
;
75 static struct scnhdr
*bss_section
;
80 struct scnhdr section
[10];
83 /* Define name of label for entry point for the dumped executable. */
85 #ifndef DEFAULT_ENTRY_ADDRESS
86 #define DEFAULT_ENTRY_ADDRESS __start
89 unexec (new_name
, a_name
, data_start
, bss_start
, entry_address
)
90 char *new_name
, *a_name
;
91 unsigned data_start
, bss_start
, entry_address
;
101 char buffer
[BUFSIZE
];
103 old
= open (a_name
, O_RDONLY
, 0);
104 if (old
< 0) fatal_unexec ("opening %s", a_name
);
106 new = creat (new_name
, 0666);
107 if (new < 0) fatal_unexec ("creating %s", new_name
);
109 hdr
= *((struct headers
*)TEXT_START
);
111 if (hdr
.fhdr
.f_magic
!= MIPSELMAGIC
112 && hdr
.fhdr
.f_magic
!= MIPSEBMAGIC
113 && hdr
.fhdr
.f_magic
!= (MIPSELMAGIC
| 1)
114 && hdr
.fhdr
.f_magic
!= (MIPSEBMAGIC
| 1))
117 "unexec: input file magic number is %x, not %x, %x, %x or %x.\n",
119 MIPSELMAGIC
, MIPSEBMAGIC
,
120 MIPSELMAGIC
| 1, MIPSEBMAGIC
| 1);
123 #else /* not MIPS2 */
124 if (hdr
.fhdr
.f_magic
!= MIPSELMAGIC
125 && hdr
.fhdr
.f_magic
!= MIPSEBMAGIC
)
127 fprintf (stderr
, "unexec: input file magic number is %x, not %x or %x.\n",
128 hdr
.fhdr
.f_magic
, MIPSELMAGIC
, MIPSEBMAGIC
);
131 #endif /* not MIPS2 */
132 if (hdr
.fhdr
.f_opthdr
!= sizeof (hdr
.aout
))
134 fprintf (stderr
, "unexec: input a.out header is %d bytes, not %d.\n",
135 hdr
.fhdr
.f_opthdr
, sizeof (hdr
.aout
));
138 if (hdr
.aout
.magic
!= ZMAGIC
)
140 fprintf (stderr
, "unexec: input file a.out magic number is %o, not %o.\n",
141 hdr
.aout
.magic
, ZMAGIC
);
145 #define CHECK_SCNHDR(ptr, name, flags) \
147 for (i = 0; i < hdr.fhdr.f_nscns && !ptr; i++) \
148 if (strcmp (hdr.section[i].s_name, name) == 0) \
150 if (hdr.section[i].s_flags != flags) \
151 fprintf (stderr, "unexec: %x flags (%x expected) in %s section.\n", \
152 hdr.section[i].s_flags, flags, name); \
153 ptr = hdr.section + i; \
156 CHECK_SCNHDR (text_section, _TEXT, STYP_TEXT);
157 CHECK_SCNHDR (init_section
, _INIT
, STYP_INIT
);
158 CHECK_SCNHDR (rdata_section
, _RDATA
, STYP_RDATA
);
159 CHECK_SCNHDR (data_section
, _DATA
, STYP_DATA
);
161 CHECK_SCNHDR (lit8_section
, _LIT8
, STYP_LIT8
);
162 CHECK_SCNHDR (lit4_section
, _LIT4
, STYP_LIT4
);
164 CHECK_SCNHDR (sdata_section
, _SDATA
, STYP_SDATA
);
165 CHECK_SCNHDR (sbss_section
, _SBSS
, STYP_SBSS
);
166 CHECK_SCNHDR (bss_section
, _BSS
, STYP_BSS
);
167 #if 0 /* Apparently this error check goes off on irix 3.3,
168 but it doesn't indicate a real problem. */
169 if (i
!= hdr
.fhdr
.f_nscns
)
170 fprintf (stderr
, "unexec: %d sections found instead of %d.\n",
171 i
, hdr
.fhdr
.f_nscns
);
174 text_section
->s_scnptr
= 0;
176 pagesize
= getpagesize ();
177 brk
= (sbrk (0) + pagesize
- 1) & (-pagesize
);
178 hdr
.aout
.dsize
= brk
- DATA_START
;
180 if (entry_address
== 0)
182 extern DEFAULT_ENTRY_ADDRESS ();
183 hdr
.aout
.entry
= (unsigned)DEFAULT_ENTRY_ADDRESS
;
186 hdr
.aout
.entry
= entry_address
;
188 hdr
.aout
.bss_start
= hdr
.aout
.data_start
+ hdr
.aout
.dsize
;
189 rdata_section
->s_size
= data_start
- DATA_START
;
191 /* Adjust start and virtual addresses of rdata_section, too. */
192 rdata_section
->s_vaddr
= DATA_START
;
193 rdata_section
->s_paddr
= DATA_START
;
194 rdata_section
->s_scnptr
= text_section
->s_scnptr
+ hdr
.aout
.tsize
;
196 data_section
->s_vaddr
= data_start
;
197 data_section
->s_paddr
= data_start
;
198 data_section
->s_size
= brk
- data_start
;
199 data_section
->s_scnptr
= rdata_section
->s_scnptr
+ rdata_section
->s_size
;
200 vaddr
= data_section
->s_vaddr
+ data_section
->s_size
;
201 scnptr
= data_section
->s_scnptr
+ data_section
->s_size
;
202 if (lit8_section
!= NULL
)
204 lit8_section
->s_vaddr
= vaddr
;
205 lit8_section
->s_paddr
= vaddr
;
206 lit8_section
->s_size
= 0;
207 lit8_section
->s_scnptr
= scnptr
;
209 if (lit4_section
!= NULL
)
211 lit4_section
->s_vaddr
= vaddr
;
212 lit4_section
->s_paddr
= vaddr
;
213 lit4_section
->s_size
= 0;
214 lit4_section
->s_scnptr
= scnptr
;
216 if (sdata_section
!= NULL
)
218 sdata_section
->s_vaddr
= vaddr
;
219 sdata_section
->s_paddr
= vaddr
;
220 sdata_section
->s_size
= 0;
221 sdata_section
->s_scnptr
= scnptr
;
223 if (sbss_section
!= NULL
)
225 sbss_section
->s_vaddr
= vaddr
;
226 sbss_section
->s_paddr
= vaddr
;
227 sbss_section
->s_size
= 0;
228 sbss_section
->s_scnptr
= scnptr
;
230 if (bss_section
!= NULL
)
232 bss_section
->s_vaddr
= vaddr
;
233 bss_section
->s_paddr
= vaddr
;
234 bss_section
->s_size
= 0;
235 bss_section
->s_scnptr
= scnptr
;
238 WRITE (new, TEXT_START
, hdr
.aout
.tsize
,
239 "writing text section to %s", new_name
);
240 WRITE (new, DATA_START
, hdr
.aout
.dsize
,
241 "writing text section to %s", new_name
);
243 SEEK (old
, hdr
.fhdr
.f_symptr
, "seeking to start of symbols in %s", a_name
);
245 nread
= read (old
, buffer
, BUFSIZE
);
246 if (nread
< sizeof (HDRR
)) fatal_unexec ("reading symbols from %s", a_name
);
247 #define symhdr ((pHDRR)buffer)
248 newsyms
= hdr
.aout
.tsize
+ hdr
.aout
.dsize
;
249 symrel
= newsyms
- hdr
.fhdr
.f_symptr
;
250 hdr
.fhdr
.f_symptr
= newsyms
;
251 symhdr
->cbLineOffset
+= symrel
;
252 symhdr
->cbDnOffset
+= symrel
;
253 symhdr
->cbPdOffset
+= symrel
;
254 symhdr
->cbSymOffset
+= symrel
;
255 symhdr
->cbOptOffset
+= symrel
;
256 symhdr
->cbAuxOffset
+= symrel
;
257 symhdr
->cbSsOffset
+= symrel
;
258 symhdr
->cbSsExtOffset
+= symrel
;
259 symhdr
->cbFdOffset
+= symrel
;
260 symhdr
->cbRfdOffset
+= symrel
;
261 symhdr
->cbExtOffset
+= symrel
;
265 if (write (new, buffer
, nread
) != nread
)
266 fatal_unexec ("writing symbols to %s", new_name
);
267 nread
= read (old
, buffer
, BUFSIZE
);
268 if (nread
< 0) fatal_unexec ("reading symbols from %s", a_name
);
270 } while (nread
!= 0);
272 SEEK (new, 0, "seeking to start of header in %s", new_name
);
273 WRITE (new, &hdr
, sizeof (hdr
),
274 "writing header of %s", new_name
);
284 * After succesfully building the new a.out, mark it executable
292 int um
= umask (777);
294 if (stat (name
, &sbuf
) < 0)
295 fatal_unexec ("getting protection on %s", name
);
296 sbuf
.st_mode
|= 0111 & ~um
;
297 if (chmod (name
, sbuf
.st_mode
) < 0)
298 fatal_unexec ("setting protection on %s", name
);
302 fatal_unexec (s
, va_alist
)
307 fputs ("unexec: unexpected end of file, ", stderr
);
308 else if (errno
< sys_nerr
)
309 fprintf (stderr
, "unexec: %s, ", sys_errlist
[errno
]);
311 fprintf (stderr
, "unexec: error code %d, ", errno
);
313 _doprnt (s
, ap
, stderr
);
314 fputs (".\n", stderr
);