1 /* Unexec for MIPS (including IRIS4D).
2 Copyright (C) 1988, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of XEmacs.
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
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
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. */
22 /* Synched up with: FSF 19.31. */
25 #include <sys/types.h>
36 /* I don't know why this isn't defined. */
38 #define STYP_INIT 0x80000000
41 /* I don't know why this isn't defined. */
43 #define _RDATA ".rdata"
44 #define STYP_RDATA 0x00000100
47 /* Small ("near") data section. */
49 #define _SDATA ".sdata"
50 #define STYP_SDATA 0x00000200
53 /* Small ("near") bss section. */
56 #define STYP_SBSS 0x00000400
59 /* We don't seem to have a sym.h or syms.h anywhere, so we'll do it the
60 hard way. This stinks. */
65 struct { long foo, offset; } offsets[11];
77 #if defined (IRIS_4D) || defined (sony)
78 #include "getpagesize.h"
82 static void fatal_unexec ();
83 static void mark_x ();
85 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
87 if (read (_fd, _buffer, _size) != _size) \
88 fatal_unexec (_error_message, _error_arg);
90 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
91 if (write (_fd, _buffer, _size) != _size) \
92 fatal_unexec (_error_message, _error_arg);
94 #define SEEK(_fd, _position, _error_message, _error_arg) \
96 if (lseek (_fd, _position, L_SET) != _position) \
97 fatal_unexec (_error_message, _error_arg);
99 extern char *strerror ();
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;
116 struct scnhdr section[10];
119 /* Define name of label for entry point for the dumped executable. */
121 #ifndef DEFAULT_ENTRY_ADDRESS
122 #define DEFAULT_ENTRY_ADDRESS __start
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;
137 char buffer[BUFSIZE];
139 old = open (a_name, O_RDONLY, 0);
140 if (old < 0) fatal_unexec ("opening %s", a_name);
142 new = creat (new_name, 0666);
143 if (new < 0) fatal_unexec ("creating %s", new_name);
145 hdr = *((struct headers *)TEXT_START);
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))
153 "unexec: input file magic number is %x, not %x, %x, %x or %x.\n",
155 MIPSELMAGIC, MIPSEBMAGIC,
156 MIPSELMAGIC | 1, MIPSEBMAGIC | 1);
159 #else /* not MIPS2 */
160 if (hdr.fhdr.f_magic != MIPSELMAGIC
161 && hdr.fhdr.f_magic != MIPSEBMAGIC)
163 fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n",
164 hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC);
167 #endif /* not MIPS2 */
168 if (hdr.fhdr.f_opthdr != sizeof (hdr.aout))
170 fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n",
171 hdr.fhdr.f_opthdr, sizeof (hdr.aout));
174 if (hdr.aout.magic != ZMAGIC)
176 fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n",
177 hdr.aout.magic, ZMAGIC);
181 #define CHECK_SCNHDR(ptr, name, flags) \
183 for (i = 0; i < hdr.fhdr.f_nscns && !ptr; i++) \
184 if (strcmp (hdr.section[i].s_name, name) == 0) \
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; \
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);
197 CHECK_SCNHDR (lit8_section, _LIT8, STYP_LIT8);
198 CHECK_SCNHDR (lit4_section, _LIT4, STYP_LIT4);
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);
210 text_section->s_scnptr = 0;
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;
217 if (entry_address == 0)
219 extern DEFAULT_ENTRY_ADDRESS ();
220 hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS;
223 hdr.aout.entry = entry_address;
225 hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize;
226 rdata_section->s_size = data_start - DATA_START;
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;
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)
241 lit8_section->s_vaddr = vaddr;
242 lit8_section->s_paddr = vaddr;
243 lit8_section->s_size = 0;
244 lit8_section->s_scnptr = scnptr;
246 if (lit4_section != NULL)
248 lit4_section->s_vaddr = vaddr;
249 lit4_section->s_paddr = vaddr;
250 lit4_section->s_size = 0;
251 lit4_section->s_scnptr = scnptr;
253 if (sdata_section != NULL)
255 sdata_section->s_vaddr = vaddr;
256 sdata_section->s_paddr = vaddr;
257 sdata_section->s_size = 0;
258 sdata_section->s_scnptr = scnptr;
260 if (sbss_section != NULL)
262 sbss_section->s_vaddr = vaddr;
263 sbss_section->s_paddr = vaddr;
264 sbss_section->s_size = 0;
265 sbss_section->s_scnptr = scnptr;
267 if (bss_section != NULL)
269 bss_section->s_vaddr = vaddr;
270 bss_section->s_paddr = vaddr;
271 bss_section->s_size = 0;
272 bss_section->s_scnptr = scnptr;
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);
280 SEEK (old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name);
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)
289 for (i = 0; i < 11; i++)
290 symhdr->offsets[i].offset += symrel;
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;
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);
312 } while (nread != 0);
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);
326 * After successfully building the new a.out, mark it executable
334 int um = umask (777);
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);
344 fatal_unexec (s, va_alist)
349 fputs ("unexec: unexpected end of file, ", stderr);
351 fprintf (stderr, "unexec: %s, ", strerror (errno));
353 _doprnt (s, ap, stderr);
354 fputs (".\n", stderr);